eliminate_contiguous.cpp 2.49 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
#include <utility>
8

Paul's avatar
Paul committed
9
namespace migraphx {
Paul's avatar
Paul committed
10
inline namespace MIGRAPHX_INLINE_NS {
11

12
bool try_compute_shape(instruction_ref ins, const std::vector<shape>& inputs)
13
14
15
{
    try
    {
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
        shape new_shape = ins->get_operator().compute_shape(inputs);
        // If the output shape is a standard shape, no need to try its output 
        if (new_shape.standard())
        {
            return true;
        }

        auto outputs = ins->outputs();
        // If the current instruction has no output, it means the last output shape
        // is non-standard, then we cannot eliminate the contiguous
        if (outputs.empty())
        {
            return false;
        }

        for (auto output : outputs)
        {
            auto args = output->inputs();
            std::vector<shape> input_shapes;
            for (auto arg : args)
            {
                input_shapes.push_back((arg == ins) ? new_shape : arg->get_shape());
            }
            
            if (!try_compute_shape(output, input_shapes))
            {
                return false;
            }
        }
45
46
47
48
49
    }
    catch(...)
    {
        return false;
    }
50

51
52
53
    return true;
}

54
55
56
57
58
59
bool try_compute_shape(instruction_ref ins, const std::vector<instruction_ref>& args)
{
    auto inputs = to_shapes(args);
    return try_compute_shape(ins, inputs);
}

60
61
62
63
void eliminate_contiguous::apply(program& p) const
{
    for(auto ins : iterator_for(p))
    {
64
65
        // skip the reshape operator for now, since there is a bug
        // for the transpose followed by a reshape
Shucai Xiao's avatar
Shucai Xiao committed
66
        if(ins->name() == "reshape")
67
68
69
70
        {
            continue;
        }

71
        // Make a copy so we can modify it while we iterate
Paul's avatar
Paul committed
72
73
        auto args = ins->inputs();
        for(auto arg : ins->inputs())
74
75
76
        {
            // TODO: Pass in names for the operator in the constructor instead
            // of using ends_with
Paul's avatar
Paul committed
77
            if(ends_with(arg->name(), "contiguous"))
78
79
            {
                auto new_args = args;
Paul's avatar
Paul committed
80
                auto prev     = arg->inputs().front();
81
                replace(new_args, arg, prev);
82
                if(try_compute_shape(ins, new_args))
83
                {
Paul's avatar
Paul committed
84
                    instruction::replace_argument(ins, arg, prev);
85
86
87
88
89
90
                }
            }
        }
    }
}

Paul's avatar
Paul committed
91
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
92
} // namespace migraphx