eliminate_contiguous.cpp 3.24 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
#include <migraphx/eliminate_contiguous.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
7
8
#include <migraphx/op/contiguous.hpp>
#include <migraphx/op/identity.hpp>
Paul's avatar
Paul committed
9
#include <utility>
10

Paul's avatar
Paul committed
11
namespace migraphx {
Paul's avatar
Paul committed
12
inline namespace MIGRAPHX_INLINE_NS {
13

14
15
16
static bool try_compute_shape(instruction_ref ins,
                              const std::vector<shape>& inputs,
                              const std::vector<module_ref>& mods)
17
18
19
{
    try
    {
20
        shape new_shape = ins->get_operator().compute_shape(inputs, mods);
Shucai Xiao's avatar
Shucai Xiao committed
21
22
        // If the output shape is a standard shape, no need to try its output
        if(new_shape.standard())
23
24
25
26
        {
            return true;
        }

27
        // if no changes for the shape, the contiguous can also be removed
Shucai Xiao's avatar
Shucai Xiao committed
28
        if(new_shape == ins->get_shape())
29
30
31
32
        {
            return true;
        }

33
        auto outputs = ins->outputs();
34
        // If the current instruction has no output, it means it is the last
35
36
        // instruction and generates a non-standard output shape, and the last
        // output shape is different from the case with the contiguous operator
Shucai Xiao's avatar
Shucai Xiao committed
37
        if(outputs.empty())
38
        {
39
            return false;
40
41
        }

Shucai Xiao's avatar
Shucai Xiao committed
42
        for(auto output : outputs)
43
44
        {
            auto args = output->inputs();
45
46
47
48
            std::vector<shape> input_shapes(args.size());
            std::transform(args.begin(), args.end(), input_shapes.begin(), [&](auto& arg) {
                return (arg == ins) ? new_shape : arg->get_shape();
            });
Shucai Xiao's avatar
Shucai Xiao committed
49

50
            if(!try_compute_shape(output, input_shapes, mods))
51
52
53
54
            {
                return false;
            }
        }
55
56
57
58
59
    }
    catch(...)
    {
        return false;
    }
60

61
62
63
    return true;
}

64
65
66
static bool try_compute_shape(instruction_ref ins,
                              const std::vector<instruction_ref>& args,
                              const std::vector<module_ref>& mods)
67
68
{
    auto inputs = to_shapes(args);
69
    return try_compute_shape(ins, inputs, mods);
70
71
}

72
void eliminate_contiguous::apply(module& p) const
73
74
75
{
    for(auto ins : iterator_for(p))
    {
76
77
78
79
        // return instruction should have inputs with standard shape
        if(ins->name() == "@return")
            continue;

80
        // Make a copy so we can modify it while we iterate
Paul's avatar
Paul committed
81
        auto args = ins->inputs();
82
83
        auto new_args = args;
        auto mod_args = ins->module_inputs();
Paul's avatar
Paul committed
84
        for(auto arg : ins->inputs())
85
        {
86
            if(arg->name() == op_name)
87
            {
Paul's avatar
Paul committed
88
                auto prev     = arg->inputs().front();
89
                replace(new_args, arg, prev);
90
                if(try_compute_shape(ins, new_args, mod_args))
91
                {
Paul's avatar
Paul committed
92
                    instruction::replace_argument(ins, arg, prev);
93
                }
Paul's avatar
Paul committed
94
                else if(prev->can_eval())
Paul's avatar
Paul committed
95
                {
Paul's avatar
Paul committed
96
97
98
99
100
                    auto c = op::contiguous{};
                    auto r = c.compute(c.compute_shape({prev->get_shape()}), {prev->eval()});

                    auto l = p.add_literal(r.get_shape(), r.data());
                    p.replace_instruction(arg, l);
Paul's avatar
Paul committed
101
                }
102
103
104
105
106
            }
        }
    }
}

Paul's avatar
Paul committed
107
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
108
} // namespace migraphx