basic_ops.hpp 6.31 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
89
90

struct pass_op
{
    std::string name() const { return "pass"; }
Paul's avatar
Paul committed
91
92
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
Paul's avatar
Paul committed
93
94
95
96
97
98
    {
        if(args.empty())
            return {};
        return args.front();
    }

Paul's avatar
Paul committed
99
    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
Paul's avatar
Paul committed
100
101
102
103
104
    {
        if(inputs.empty())
            return {};
        return inputs.front();
    }
Paul Fultz II's avatar
Paul Fultz II committed
105
    int output_alias(const std::vector<migraphx::shape>& s) const { return s.empty() ? -1 : 0; }
Shucai Xiao's avatar
Shucai Xiao committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
};

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
    {
        if(!mods.empty())
        {
            auto out_shapes = mods[0]->get_output_shapes();
            return out_shapes[0];
        }
        if(!inputs.empty())
        {
            return inputs.front();
        }

        return {};
    }

Paul's avatar
Paul committed
128
    int output_alias(const std::vector<migraphx::shape>&) const { return 0; }
Paul's avatar
Paul committed
129
};
130

Paul's avatar
Paul committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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; }
};

151
152
153
struct pass_standard_op
{
    std::string name() const { return "pass"; }
Paul's avatar
Paul committed
154
155
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
156
157
158
159
160
161
    {
        if(args.empty())
            return {};
        return args.front();
    }

Paul's avatar
Paul committed
162
    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
163
    {
Paul's avatar
Paul committed
164
        for(auto&& input : inputs)
165
166
167
168
169
170
171
172
        {
            if(not input.standard())
                throw std::runtime_error("Not standard shape");
        }
        if(inputs.empty())
            return {};
        return inputs.front();
    }
Paul's avatar
Paul committed
173
    int output_alias(const std::vector<migraphx::shape>&) const { return 0; }
174
175
};

176
177
178
struct nop
{
    std::string name() const { return "nop"; }
Paul's avatar
Paul committed
179
180
181
    migraphx::argument compute(migraphx::context&,
                               const migraphx::shape&,
                               const std::vector<migraphx::argument>&) const
182
183
184
185
    {
        return {};
    }

Paul's avatar
Paul committed
186
    migraphx::shape compute_shape(const std::vector<migraphx::shape>&) const { return {}; }
187
};
188

Paul's avatar
Paul committed
189
inline migraphx::literal get_2x2()
190
{
Paul's avatar
Paul committed
191
    return migraphx::literal{{migraphx::shape::float_type, {2, 2}}, {1, 2, 3, 4}};
192
193
}

Paul's avatar
Paul committed
194
inline migraphx::literal get_2x2_transposed()
195
{
Paul's avatar
Paul committed
196
    return migraphx::literal{{migraphx::shape::float_type, {2, 2}, {1, 2}}, {1, 2, 3, 4}};
197
198
}

Paul's avatar
Paul committed
199
inline migraphx::literal get_2()
Paul's avatar
Paul committed
200
{
Paul's avatar
Paul committed
201
    return migraphx::literal{{migraphx::shape::float_type, {2}}, {1, 2}};
Paul's avatar
Paul committed
202
}
203

Paul's avatar
Paul committed
204
inline migraphx::literal get_2_broadcasted()
205
{
Paul's avatar
Paul committed
206
    return migraphx::literal{{migraphx::shape::float_type, {2, 1}, {1, 0}}, {1, 2}};
207
}