parse_slice.cpp 5.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul Fultz II's avatar
Paul Fultz II committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <migraphx/onnx/op_parser.hpp>
#include <migraphx/onnx/checks.hpp>
#include <migraphx/op/slice.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/make_op.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace onnx {

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

    instruction_ref parse(const op_desc& /*opd*/,
                          const onnx_parser& parser,
                          onnx_parser::node_info info,
                          std::vector<instruction_ref> args) const
    {
        op::slice op;

Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
46
47
        std::vector<int64_t> steps;

Paul Fultz II's avatar
Paul Fultz II committed
48
        // slice can have up to 5 inputs, we first check the 5th one
Brian Pickrell's avatar
Brian Pickrell committed
49
        // to decide whether MIGRAPHX can handle this slice.
Paul Fultz II's avatar
Paul Fultz II committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        if(args.size() == 5)
        {
            migraphx::argument step_arg = args.back()->eval();
            check_arg_empty(step_arg, "PARSE_SLICE: cannot handle variable steps for slice");
            step_arg.visit([&](auto s) { steps.assign(s.begin(), s.end()); });
        }

        if(args.size() >= 4)
        {
            migraphx::argument axes_arg = args.at(3)->eval();
            check_arg_empty(axes_arg, "PARSE_SLICE: cannot handle variable axes for slice");
            axes_arg.visit([&](auto s) { op.axes.assign(s.begin(), s.end()); });
        }
        else if(contains(info.attributes, "axes"))
        {
            literal s = parser.parse_value(info.attributes.at("axes"));
            s.visit([&](auto v) { copy(v, std::back_inserter(op.axes)); });
        }

        if(args.size() >= 3)
        {
            migraphx::argument end_arg = args.at(2)->eval();
            check_arg_empty(end_arg, "PARSE_SLICE: cannot handle variable ends for slice");
            end_arg.visit([&](auto s) { op.ends.assign(s.begin(), s.end()); });
        }
        else if(contains(info.attributes, "ends"))
        {
            literal s = parser.parse_value(info.attributes.at("ends"));
            s.visit([&](auto v) { copy(v, std::back_inserter(op.ends)); });
        }

        if(args.size() >= 2)
        {
            migraphx::argument start_arg = args.at(1)->eval();
            check_arg_empty(start_arg, "PARSE_SLICE: cannot handle variable starts for slice");
            start_arg.visit([&](auto s) { op.starts.assign(s.begin(), s.end()); });
        }
        else if(contains(info.attributes, "starts"))
        {
            literal s = parser.parse_value(info.attributes.at("starts"));
            s.visit([&](auto v) { copy(v, std::back_inserter(op.starts)); });
        }

Brian Pickrell's avatar
Brian Pickrell committed
93
        // If axes arg is not given, the default is all of them.
Paul Fultz II's avatar
Paul Fultz II committed
94
95
        if(op.axes.empty())
        {
Brian Pickrell's avatar
Brian Pickrell committed
96
            std::vector<int64_t> axes(args[0]->get_shape().ndim());
Paul Fultz II's avatar
Paul Fultz II committed
97
98
99
100
            std::iota(axes.begin(), axes.end(), int64_t{0});
            op.axes = axes;
        }

Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
101
102
103
104
105
106
        std::vector<int64_t> raxes;

        assert(steps.empty() or steps.size() == op.axes.size());
        assert(op.axes.size() == op.starts.size());
        assert(op.axes.size() == op.ends.size());

Brian Pickrell's avatar
Brian Pickrell committed
107
        // If any axes have negative step, prepare to add a "reverse" op
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        for(auto i : range(steps.size()))
        {
            if(steps[i] >= 0)
                continue;
            op.starts[i] += 1;
            if(op.starts[i] == 0)
                op.starts[i] = INT_MAX;
            op.ends[i] += 1;
            raxes.push_back(op.axes[i]);
            std::swap(op.starts[i], op.ends[i]);
        }

        auto ins = info.add_instruction(op, args[0]);
        if(not raxes.empty())
Brian Pickrell's avatar
Brian Pickrell committed
122
        {
123
            ins = info.add_instruction(make_op("reverse", {{"axes", raxes}}), ins);
Brian Pickrell's avatar
Brian Pickrell committed
124
125
        }
        // If any steps are other than default 1, add a "steps" op
126
127
128
129
130
131
132
133
134
        if(std::any_of(steps.begin(), steps.end(), [](auto s) { return std::abs(s) != 1; }))
        {
            std::vector<int64_t> nsteps;
            std::transform(steps.begin(), steps.end(), std::back_inserter(nsteps), [](auto s) {
                return std::abs(s);
            });
            return ins = info.add_instruction(
                       make_op("step", {{"axes", op.axes}, {"steps", nsteps}}), ins);
        }
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
135
136
        else
            return ins;
Paul Fultz II's avatar
Paul Fultz II committed
137
138
139
140
141
142
    }
};

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