basic_ops.hpp 7.25 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's avatar
Paul committed
24
25
26
#include <migraphx/program.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/shape.hpp>
Paul's avatar
Paul committed
27
28
29
30

struct sum_op
{
    std::string name() const { return "sum"; }
Paul's avatar
Paul committed
31
32
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
Paul's avatar
Paul committed
33
    {
Paul's avatar
Paul committed
34
        migraphx::argument result;
Paul's avatar
Paul committed
35
        if(args.size() != 2)
Paul's avatar
Paul committed
36
            MIGRAPHX_THROW("Wrong args");
Paul's avatar
Paul committed
37
        if(args[0].get_shape() != args[1].get_shape())
Paul's avatar
Paul committed
38
            MIGRAPHX_THROW("Wrong args");
Paul's avatar
Paul committed
39
        if(args[0].get_shape().lens().size() != 1)
Paul's avatar
Paul committed
40
            MIGRAPHX_THROW("Wrong args");
Paul's avatar
Paul committed
41
        if(args[0].get_shape().lens().front() != 1)
Paul's avatar
Paul committed
42
            MIGRAPHX_THROW("Wrong args");
Paul's avatar
Paul committed
43
44

        args[0].visit_at([&](auto x) {
Paul's avatar
Paul committed
45
            args[1].visit_at([&](auto y) { result = migraphx::literal{x + y}.get_argument(); });
Paul's avatar
Paul committed
46
47
48
49
        });
        return result;
    }

Paul's avatar
Paul committed
50
    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
Paul's avatar
Paul committed
51
52
    {
        if(inputs.size() != 2)
Paul's avatar
Paul committed
53
            MIGRAPHX_THROW("Wrong inputs");
Paul's avatar
Paul committed
54
55
56
57
58
59
60
        return inputs.front();
    }
};

struct minus_op
{
    std::string name() const { return "minus"; }
Paul's avatar
Paul committed
61
62
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
Paul's avatar
Paul committed
63
    {
Paul's avatar
Paul committed
64
        migraphx::argument result;
Paul's avatar
Paul committed
65
        if(args.size() != 2)
Paul's avatar
Paul committed
66
            MIGRAPHX_THROW("Wrong args");
Paul's avatar
Paul committed
67
        if(args[0].get_shape() != args[1].get_shape())
Paul's avatar
Paul committed
68
            MIGRAPHX_THROW("Wrong args");
Paul's avatar
Paul committed
69
        if(args[0].get_shape().lens().size() != 1)
Paul's avatar
Paul committed
70
            MIGRAPHX_THROW("Wrong args");
Paul's avatar
Paul committed
71
        if(args[0].get_shape().lens().front() != 1)
Paul's avatar
Paul committed
72
            MIGRAPHX_THROW("Wrong args");
Paul's avatar
Paul committed
73
74

        args[0].visit_at([&](auto x) {
Paul's avatar
Paul committed
75
            args[1].visit_at([&](auto y) { result = migraphx::literal{x - y}.get_argument(); });
Paul's avatar
Paul committed
76
77
78
79
        });
        return result;
    }

Paul's avatar
Paul committed
80
    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
Paul's avatar
Paul committed
81
82
    {
        if(inputs.size() != 2)
Paul's avatar
Paul committed
83
            MIGRAPHX_THROW("Wrong inputs");
Paul's avatar
Paul committed
84
85
86
        return inputs.front();
    }
};
Paul's avatar
Paul committed
87
88

struct pass_op
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{
    std::string name() const { return "pass"; }
    migraphx::argument compute(const migraphx::shape&, std::vector<migraphx::argument> args) const
    {
        if(args.empty())
            return {};
        return args.front();
    }

    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
    {
        if(inputs.empty())
            return {};
        return inputs.front();
    }
    int output_alias(const std::vector<migraphx::shape>& s) const { return s.empty() ? -1 : 0; }
};

struct non_const_pass_op
Paul's avatar
Paul committed
108
109
{
    std::string name() const { return "pass"; }
Paul's avatar
Paul committed
110
111
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
Paul's avatar
Paul committed
112
113
114
115
116
117
    {
        if(args.empty())
            return {};
        return args.front();
    }

Paul's avatar
Paul committed
118
    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
Paul's avatar
Paul committed
119
120
121
122
123
    {
        if(inputs.empty())
            return {};
        return inputs.front();
    }
Paul Fultz II's avatar
Paul Fultz II committed
124
    int output_alias(const std::vector<migraphx::shape>& s) const { return s.empty() ? -1 : 0; }
Shucai Xiao's avatar
Shucai Xiao committed
125
126
127
128
129
130
131
132
133
};

struct mod_pass_op
{
    std::string name() const { return "mod_pass"; }

    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs,
                                  std::vector<migraphx::module_ref> mods) const
    {
134
        if(not mods.empty())
Shucai Xiao's avatar
Shucai Xiao committed
135
136
137
138
        {
            auto out_shapes = mods[0]->get_output_shapes();
            return out_shapes[0];
        }
139
        if(not inputs.empty())
Shucai Xiao's avatar
Shucai Xiao committed
140
141
142
143
144
145
146
        {
            return inputs.front();
        }

        return {};
    }

Paul's avatar
Paul committed
147
    int output_alias(const std::vector<migraphx::shape>&) const { return 0; }
Paul's avatar
Paul committed
148
};
149

Paul's avatar
Paul committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
struct unary_pass_op
{
    std::string name() const { return "unary_pass"; }
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
    {
        if(args.empty())
            return {};
        return args.front();
    }

    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
    {
        if(inputs.size() != 1)
            MIGRAPHX_THROW("Wrong inputs");
        return inputs.front();
    }
    int output_alias(const std::vector<migraphx::shape>&) const { return 0; }
};

170
171
172
struct pass_standard_op
{
    std::string name() const { return "pass"; }
Paul's avatar
Paul committed
173
174
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
175
176
177
178
179
180
    {
        if(args.empty())
            return {};
        return args.front();
    }

Paul's avatar
Paul committed
181
    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
182
    {
Paul's avatar
Paul committed
183
        for(auto&& input : inputs)
184
185
186
187
188
189
190
191
        {
            if(not input.standard())
                throw std::runtime_error("Not standard shape");
        }
        if(inputs.empty())
            return {};
        return inputs.front();
    }
Paul's avatar
Paul committed
192
    int output_alias(const std::vector<migraphx::shape>&) const { return 0; }
193
194
};

195
196
197
struct nop
{
    std::string name() const { return "nop"; }
198
    migraphx::argument compute(const migraphx::shape&, const std::vector<migraphx::argument>&) const
199
200
201
202
    {
        return {};
    }

Paul's avatar
Paul committed
203
    migraphx::shape compute_shape(const std::vector<migraphx::shape>&) const { return {}; }
204
};
205

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
struct tuple_op
{
    std::string name() const { return "tuple_op"; }
    migraphx::shape compute_shape(const std::vector<migraphx::shape>& inputs) const
    {
        return {inputs};
    }
    migraphx::argument compute(migraphx::context&,
                               const migraphx::shape&,
                               const std::vector<migraphx::argument>& input_args) const
    {
        return input_args;
    }
};

221
inline migraphx::literal get_2x2(int base = 0)
222
{
223
224
    return migraphx::literal{{migraphx::shape::float_type, {2, 2}},
                             {base + 1, base + 2, base + 3, base + 4}};
225
226
}

Paul's avatar
Paul committed
227
inline migraphx::literal get_2x2_transposed()
228
{
Paul's avatar
Paul committed
229
    return migraphx::literal{{migraphx::shape::float_type, {2, 2}, {1, 2}}, {1, 2, 3, 4}};
230
231
}

Paul's avatar
Paul committed
232
inline migraphx::literal get_2()
Paul's avatar
Paul committed
233
{
Paul's avatar
Paul committed
234
    return migraphx::literal{{migraphx::shape::float_type, {2}}, {1, 2}};
Paul's avatar
Paul committed
235
}
236

Paul's avatar
Paul committed
237
inline migraphx::literal get_2_broadcasted()
238
{
Paul's avatar
Paul committed
239
    return migraphx::literal{{migraphx::shape::float_type, {2, 1}, {1, 0}}, {1, 2}};
240
}