"configs/vscode:/vscode.git/clone" did not exist on "3d8eaa2a2166d075d57c312411b879af234f4ea8"
parse_slice.cpp 6.23 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
    struct slice_desc
    {
        op::slice op;
        std::vector<instruction_ref> op_args;
        std::vector<int64_t> steps;
        std::vector<int64_t> raxes;

47
48
        void always_insert(instruction_ref arg) { op_args.insert(op_args.begin(), arg); }

Charlie Lin's avatar
Charlie Lin committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        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
65
66
    instruction_ref parse(const op_desc& /*opd*/,
                          const onnx_parser& parser,
Charlie Lin's avatar
Charlie Lin committed
67
68
                          const onnx_parser::node_info& info,
                          const std::vector<instruction_ref>& args) const
Paul Fultz II's avatar
Paul Fultz II committed
69
    {
Charlie Lin's avatar
Charlie Lin committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        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
90

Charlie Lin's avatar
Charlie Lin committed
91
92
93
94
95
    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
96

Paul Fultz II's avatar
Paul Fultz II committed
97
        // slice can have up to 5 inputs, we first check the 5th one
Brian Pickrell's avatar
Brian Pickrell committed
98
        // to decide whether MIGRAPHX can handle this slice.
Paul Fultz II's avatar
Paul Fultz II committed
99
100
101
102
        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
103
            step_arg.visit([&](auto s) { sd.steps.assign(s.begin(), s.end()); });
Paul Fultz II's avatar
Paul Fultz II committed
104
105
106
107
        }

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

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

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

Charlie Lin's avatar
Charlie Lin committed
136
        // data input argument
137
        sd.always_insert(args.at(0));
Charlie Lin's avatar
Charlie Lin committed
138

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

Charlie Lin's avatar
Charlie Lin committed
147
148
149
150
151
152
153
        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
154

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

Brian Pickrell's avatar
Brian Pickrell committed
157
        // If any axes have negative step, prepare to add a "reverse" op
Charlie Lin's avatar
Charlie Lin committed
158
        for(auto i : range(sd.steps.size()))
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
159
        {
Charlie Lin's avatar
Charlie Lin committed
160
            if(sd.steps[i] >= 0)
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
161
                continue;
Charlie Lin's avatar
Charlie Lin committed
162
163
164
165
166
167
            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
168
        }
Charlie Lin's avatar
Charlie Lin committed
169
        return sd;
Paul Fultz II's avatar
Paul Fultz II committed
170
171
172
173
174
175
    }
};

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