"vscode:/vscode.git/clone" did not exist on "be9efdb981ad9f8529d647190f24f4d7f3a61ae5"
eliminate_contiguous.cpp 1.52 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
#include <migraphx/eliminate_contiguous.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
8
#include <utility>
9

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

Paul's avatar
Paul committed
13
bool try_compute_shape(const operation& op, const std::vector<instruction_ref>& args)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
    try
    {
        compute_shape(op, args);
    }
    catch(...)
    {
        return false;
    }
    return true;
}

void eliminate_contiguous::apply(program& p) const
{
    for(auto ins : iterator_for(p))
    {
30
31
        // 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
32
        if(ins->name() == "reshape")
33
34
35
36
        {
            continue;
        }

37
        // Make a copy so we can modify it while we iterate
Paul's avatar
Paul committed
38
39
        auto args = ins->inputs();
        for(auto arg : ins->inputs())
40
41
42
        {
            // TODO: Pass in names for the operator in the constructor instead
            // of using ends_with
Paul's avatar
Paul committed
43
            if(ends_with(arg->name(), "contiguous"))
44
45
            {
                auto new_args = args;
Paul's avatar
Paul committed
46
                auto prev     = arg->inputs().front();
47
                replace(new_args, arg, prev);
48
                if(try_compute_shape(ins->get_operator(), new_args))
49
                {
Paul's avatar
Paul committed
50
                    instruction::replace_argument(ins, arg, prev);
51
52
53
54
55
56
                }
            }
        }
    }
}

Paul's avatar
Paul committed
57
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
58
} // namespace migraphx