eliminate_contiguous.cpp 2.84 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
static bool try_compute_shape(instruction_ref ins, const std::vector<shape>& inputs)
13
14
15
{
    try
    {
16
        shape new_shape = ins->get_operator().compute_shape(inputs);
Shucai Xiao's avatar
Shucai Xiao committed
17
18
        // If the output shape is a standard shape, no need to try its output
        if(new_shape.standard())
19
20
21
22
        {
            return true;
        }

23
24
25
26
27
28
        // if no changes for the shape, the contiguous can also be removed
        if (new_shape == ins->get_shape())
        {
            return true;
        }

29
        auto outputs = ins->outputs();
30
31
32
33
        // If the current instruction has no output, it means it is the last
        // instruction and generates a non-standard output. But for unary
        // and binary operators, we can still remove it and reshape the output
        // to be standard since these operator can handle non-standard inputs
Shucai Xiao's avatar
Shucai Xiao committed
34
        if(outputs.empty())
35
        {
36
            return true;
37
38
        }

Shucai Xiao's avatar
Shucai Xiao committed
39
        for(auto output : outputs)
40
41
        {
            auto args = output->inputs();
42
43
44
45
            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
46
47

            if(!try_compute_shape(output, input_shapes))
48
49
50
51
            {
                return false;
            }
        }
52
53
54
55
56
    }
    catch(...)
    {
        return false;
    }
57

58
59
60
    return true;
}

61
static bool try_compute_shape(instruction_ref ins, const std::vector<instruction_ref>& args)
62
63
64
65
66
{
    auto inputs = to_shapes(args);
    return try_compute_shape(ins, inputs);
}

67
68
69
70
void eliminate_contiguous::apply(program& p) const
{
    for(auto ins : iterator_for(p))
    {
71
72
        // 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
73
        if(ins->name() == "reshape")
74
75
76
77
        {
            continue;
        }

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

Paul's avatar
Paul committed
98
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
99
} // namespace migraphx