parse_slice.cpp 6.14 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
#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>
{
Charlie Lin's avatar
Charlie Lin committed
37

Paul Fultz II's avatar
Paul Fultz II committed
38
39
    std::vector<op_desc> operators() const { return {{"Slice"}}; }

Charlie Lin's avatar
Charlie Lin committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    struct slice_desc
    {
        op::slice op;
        std::vector<instruction_ref> op_args;
        std::vector<int64_t> steps;
        std::vector<int64_t> raxes;

        std::vector<int64_t> insert(instruction_ref arg)
        {
            std::vector<int64_t> result;
            migraphx::argument arg_value = arg->eval();
            if(arg_value.empty())
            {
                op_args.insert(op_args.begin(), arg);
            }
            else
            {
                arg_value.visit([&](auto s) { result.assign(s.begin(), s.end()); });
            }
            return result;
        }
    };

Paul Fultz II's avatar
Paul Fultz II committed
63
64
    instruction_ref parse(const op_desc& /*opd*/,
                          const onnx_parser& parser,
Charlie Lin's avatar
Charlie Lin committed
65
66
                          const onnx_parser::node_info& info,
                          const std::vector<instruction_ref>& args) const
Paul Fultz II's avatar
Paul Fultz II committed
67
    {
Charlie Lin's avatar
Charlie Lin committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
        auto sd  = construct_slice_desc(parser, info, args);
        auto ins = info.add_instruction(sd.op, sd.op_args);
        if(not sd.raxes.empty())
        {
            ins = info.add_instruction(make_op("reverse", {{"axes", sd.raxes}}), ins);
        }
        // If any steps are other than default 1, add a "steps" op
        if(std::any_of(sd.steps.begin(), sd.steps.end(), [](auto s) { return std::abs(s) != 1; }))
        {
            std::vector<int64_t> nsteps;
            std::transform(sd.steps.begin(),
                           sd.steps.end(),
                           std::back_inserter(nsteps),
                           [](auto s) { return std::abs(s); });
            return ins = info.add_instruction(
                       make_op("step", {{"axes", sd.op.axes}, {"steps", nsteps}}), ins);
        }
        else
            return ins;
    }
Paul Fultz II's avatar
Paul Fultz II committed
88

Charlie Lin's avatar
Charlie Lin committed
89
90
91
92
93
    slice_desc construct_slice_desc(const onnx_parser& parser,
                                    onnx_parser::node_info info,
                                    std::vector<instruction_ref> args) const
    {
        slice_desc sd;
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
94

Paul Fultz II's avatar
Paul Fultz II committed
95
        // slice can have up to 5 inputs, we first check the 5th one
Brian Pickrell's avatar
Brian Pickrell committed
96
        // to decide whether MIGRAPHX can handle this slice.
Paul Fultz II's avatar
Paul Fultz II committed
97
98
99
100
        if(args.size() == 5)
        {
            migraphx::argument step_arg = args.back()->eval();
            check_arg_empty(step_arg, "PARSE_SLICE: cannot handle variable steps for slice");
Charlie Lin's avatar
Charlie Lin committed
101
            step_arg.visit([&](auto s) { sd.steps.assign(s.begin(), s.end()); });
Paul Fultz II's avatar
Paul Fultz II committed
102
103
104
105
        }

        if(args.size() >= 4)
        {
Charlie Lin's avatar
Charlie Lin committed
106
            sd.op.axes = sd.insert(args.at(3));
Paul Fultz II's avatar
Paul Fultz II committed
107
108
109
110
        }
        else if(contains(info.attributes, "axes"))
        {
            literal s = parser.parse_value(info.attributes.at("axes"));
Charlie Lin's avatar
Charlie Lin committed
111
            s.visit([&](auto v) { copy(v, std::back_inserter(sd.op.axes)); });
Paul Fultz II's avatar
Paul Fultz II committed
112
113
114
115
        }

        if(args.size() >= 3)
        {
Charlie Lin's avatar
Charlie Lin committed
116
            sd.op.ends = sd.insert(args.at(2));
Paul Fultz II's avatar
Paul Fultz II committed
117
118
119
120
        }
        else if(contains(info.attributes, "ends"))
        {
            literal s = parser.parse_value(info.attributes.at("ends"));
Charlie Lin's avatar
Charlie Lin committed
121
            s.visit([&](auto v) { copy(v, std::back_inserter(sd.op.ends)); });
Paul Fultz II's avatar
Paul Fultz II committed
122
123
124
125
        }

        if(args.size() >= 2)
        {
Charlie Lin's avatar
Charlie Lin committed
126
            sd.op.starts = sd.insert(args.at(1));
Paul Fultz II's avatar
Paul Fultz II committed
127
128
129
130
        }
        else if(contains(info.attributes, "starts"))
        {
            literal s = parser.parse_value(info.attributes.at("starts"));
Charlie Lin's avatar
Charlie Lin committed
131
            s.visit([&](auto v) { copy(v, std::back_inserter(sd.op.starts)); });
Paul Fultz II's avatar
Paul Fultz II committed
132
133
        }

Charlie Lin's avatar
Charlie Lin committed
134
135
136
        // data input argument
        sd.insert(args.at(0));

Brian Pickrell's avatar
Brian Pickrell committed
137
        // If axes arg is not given, the default is all of them.
Charlie Lin's avatar
Charlie Lin committed
138
        if(sd.op.axes.empty() and sd.op_args.size() < 3)
Paul Fultz II's avatar
Paul Fultz II committed
139
        {
Brian Pickrell's avatar
Brian Pickrell committed
140
            std::vector<int64_t> axes(args[0]->get_shape().ndim());
Paul Fultz II's avatar
Paul Fultz II committed
141
            std::iota(axes.begin(), axes.end(), int64_t{0});
Charlie Lin's avatar
Charlie Lin committed
142
            sd.op.axes = axes;
Paul Fultz II's avatar
Paul Fultz II committed
143
144
        }

Charlie Lin's avatar
Charlie Lin committed
145
146
147
148
149
150
151
        if(not sd.steps.empty())
        {
            if(sd.op.starts.empty() or sd.op.ends.empty())
                MIGRAPHX_THROW("PARSE_SLICE: steps and variable starts and ends is not supported");
            if(sd.op.axes.empty())
                MIGRAPHX_THROW("PARSE_SLICE: steps and variable axes is not supported");
        }
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
152

Charlie Lin's avatar
Charlie Lin committed
153
        assert(sd.steps.empty() or sd.steps.size() == sd.op.axes.size());
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
154

Brian Pickrell's avatar
Brian Pickrell committed
155
        // If any axes have negative step, prepare to add a "reverse" op
Charlie Lin's avatar
Charlie Lin committed
156
        for(auto i : range(sd.steps.size()))
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
157
        {
Charlie Lin's avatar
Charlie Lin committed
158
            if(sd.steps[i] >= 0)
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
159
                continue;
Charlie Lin's avatar
Charlie Lin committed
160
161
162
163
164
165
            sd.op.starts[i] += 1;
            if(sd.op.starts[i] == 0)
                sd.op.starts[i] = INT_MAX;
            sd.op.ends[i] += 1;
            sd.raxes.push_back(sd.op.axes[i]);
            std::swap(sd.op.starts[i], sd.op.ends[i]);
Brian Pickrell's avatar
Brian Pickrell committed
166
        }
Charlie Lin's avatar
Charlie Lin committed
167
        return sd;
Paul Fultz II's avatar
Paul Fultz II committed
168
169
170
171
172
173
    }
};

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