parse_transpose.cpp 1.38 KB
Newer Older
Paul Fultz II's avatar
Paul Fultz II committed
1
2
3
#include <migraphx/onnx/op_parser.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/make_op.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
4
#include <migraphx/instruction.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace onnx {

struct parse_transpose : op_parser<parse_transpose>
{
    std::vector<op_desc> operators() const { return {{"Transpose"}}; }

    instruction_ref parse(const op_desc& /*opd*/,
                          const onnx_parser& /*parser*/,
                          onnx_parser::node_info info,
                          std::vector<instruction_ref> args) const
    {
        std::vector<int64_t> perm{};
        if(contains(info.attributes, "perm"))
        {
            auto&& perm_vals = info.attributes["perm"].ints();
            perm             = std::vector<int64_t>(perm_vals.begin(), perm_vals.end());
        }
Shucai Xiao's avatar
Shucai Xiao committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38

        // if perm is empty, use the default value
        auto n_dim = args.front()->get_shape().lens().size();
        if(perm.empty())
        {
            perm.resize(n_dim);
            std::iota(perm.rbegin(), perm.rend(), 0);
        }

        if(perm.size() != n_dim)
        {
            MIGRAPHX_THROW("PARSE_TRANSPOSE: perm and input have diffferent number of dims!");
        }

39
        return info.add_instruction(make_op("transpose", {{"permutation", perm}}), args.front());
Paul Fultz II's avatar
Paul Fultz II committed
40
41
42
43
44
45
    }
};

} // namespace onnx
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx