eliminate_contiguous.cpp 3.4 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
40
41
42
43
44
45
            return false;
        }

        if (std::any_of(outputs.begin(), outputs.end(), [](auto o) {
            return o->name() == "@return";
        }))
        {
46
            return false;
47
48
        }

Shucai Xiao's avatar
Shucai Xiao committed
49
        for(auto output : outputs)
50
51
        {
            auto args = output->inputs();
52
53
54
55
            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
56

57
            if(!try_compute_shape(output, input_shapes, mods))
58
59
60
61
            {
                return false;
            }
        }
62
63
64
65
66
    }
    catch(...)
    {
        return false;
    }
67

68
69
70
    return true;
}

71
72
73
static bool try_compute_shape(instruction_ref ins,
                              const std::vector<instruction_ref>& args,
                              const std::vector<module_ref>& mods)
74
75
{
    auto inputs = to_shapes(args);
76
    return try_compute_shape(ins, inputs, mods);
77
78
}

79
void eliminate_contiguous::apply(module& p) const
80
81
82
{
    for(auto ins : iterator_for(p))
    {
83
        // return instruction should have inputs with standard shape
Shucai Xiao's avatar
Shucai Xiao committed
84
        if(ins->name() == "@return")
85
86
            continue;

87
        // Make a copy so we can modify it while we iterate
Shucai Xiao's avatar
Shucai Xiao committed
88
        auto args     = ins->inputs();
89
90
        auto new_args = args;
        auto mod_args = ins->module_inputs();
Paul's avatar
Paul committed
91
        for(auto arg : ins->inputs())
92
        {
93
            if(arg->name() == op_name)
94
            {
Shucai Xiao's avatar
Shucai Xiao committed
95
                auto prev = arg->inputs().front();
96
                replace(new_args, arg, prev);
97
                if(try_compute_shape(ins, new_args, mod_args))
98
                {
Paul's avatar
Paul committed
99
                    instruction::replace_argument(ins, arg, prev);
100
                }
Paul's avatar
Paul committed
101
                else if(prev->can_eval())
Paul's avatar
Paul committed
102
                {
Paul's avatar
Paul committed
103
104
105
106
107
                    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
108
                }
109
110
111
112
113
            }
        }
    }
}

Paul's avatar
Paul committed
114
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
115
} // namespace migraphx