eliminate_contiguous.cpp 2.58 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
        // if no changes for the shape, the contiguous can also be removed
Shucai Xiao's avatar
Shucai Xiao committed
24
        if(new_shape == ins->get_shape())
25
26
27
28
        {
            return true;
        }

29
        auto outputs = ins->outputs();
30
        // If the current instruction has no output, it means it is the last
31
32
        // 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
33
        if(outputs.empty())
34
        {
35
            return false;
36
37
        }

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

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

57
58
59
    return true;
}

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

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

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