auto_contiguous.cpp 1001 Bytes
Newer Older
1
#include <algorithm>
Paul's avatar
Paul committed
2
3
4
#include <migraphx/auto_contiguous.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
5
6
#include <migraphx/make_op.hpp>

Paul's avatar
Paul committed
7
#include <migraphx/iterator_for.hpp>
Paul's avatar
Paul committed
8

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

12
void auto_contiguous::apply(module& p) const
Paul's avatar
Paul committed
13
{
14
15
    std::string key = "standard_input_shape";
    for(auto ins : reverse_iterator_for(p))
Paul's avatar
Paul committed
16
    {
17
18
        auto&& attr = ins->get_operator().attributes();
        if((attr.contains(key) and attr.at(key).to<bool>()))
Paul's avatar
Paul committed
19
        {
Shucai Xiao's avatar
Shucai Xiao committed
20
            auto args     = ins->inputs();
21
22
23
24
25
26
27
28
29
            auto new_args = args;
            std::transform(args.begin(), args.end(), new_args.begin(), [&](auto in) {
                return p.replace_instruction(ins, make_op("contiguous"), in);
            });

            if(new_args != args)
            {
                p.replace_instruction(ins, ins->get_operator(), new_args);
            }
Paul's avatar
Paul committed
30
31
32
33
        }
    }
}

Paul's avatar
Paul committed
34
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
35
} // namespace migraphx