operators.hpp 34.5 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPHX_GUARD_OPERATORS_HPP
#define MIGRAPHX_GUARD_OPERATORS_HPP
Paul's avatar
Paul committed
3

4
#include <array>
Paul's avatar
Paul committed
5
6
7
8
#include <migraphx/operation.hpp>
#include <migraphx/check_shapes.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/streamutils.hpp>
9
10
#include <migraphx/literal.hpp>
#include <migraphx/shape_for_each.hpp>
Paul's avatar
Paul committed
11
#include <migraphx/config.hpp>
Paul's avatar
Paul committed
12
#include <cmath>
Paul's avatar
Paul committed
13
#include <utility>
Paul's avatar
Paul committed
14

Paul's avatar
Paul committed
15
namespace migraphx {
Paul's avatar
Paul committed
16
inline namespace MIGRAPHX_INLINE_NS {
17
namespace op {
Paul's avatar
Paul committed
18

19
20
21
22
23
24
25
enum padding_mode_t
{
    default_, // NOLINT
    same,
    valid
};

Paul's avatar
Paul committed
26
27
struct not_computable
{
Paul's avatar
Paul committed
28
    argument compute(const shape&, const std::vector<argument>&) const
Paul's avatar
Paul committed
29
    {
Paul's avatar
Paul committed
30
        MIGRAPHX_THROW("not computable");
Paul's avatar
Paul committed
31
    }
Paul's avatar
Paul committed
32
33
};

34
35
struct batch_norm_inference
{
36
37
    float epsilon  = 1.0e-6f;
    float momentum = 0.9f;
38
39
40

    std::string name() const { return "batch_norm_inference"; }

41
42
43
44
45
46
47
48
    enum bn_infer_mode_t
    {
        per_activation,
        spatial,
    };

    bn_infer_mode_t bn_mode = spatial;

Paul's avatar
Paul committed
49
50
51
52
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(
Paul's avatar
Paul committed
53
            f(self.epsilon, "epsilon"), f(self.momentum, "momentum"), f(self.bn_mode, "bn_mode"));
Paul's avatar
Paul committed
54
    }
55

56
57
58
59
60
61
62
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(5);
        return inputs.front();
    }
};

Khalique's avatar
Khalique committed
63
struct lrn
Khalique's avatar
Khalique committed
64
65
{
    float alpha = 0.0001;
Khalique's avatar
Khalique committed
66
67
    float beta  = 0.75;
    float bias  = 1.0;
Khalique's avatar
Khalique committed
68
    int size    = 1;
Khalique's avatar
Khalique committed
69
    std::string name() const { return "lrn"; }
Khalique's avatar
Khalique committed
70

Khalique's avatar
Khalique committed
71
72
73
74
75
76
77
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(
            f(self.alpha, "alpha"), f(self.beta, "beta"), f(self.bias, "bias"), f(self.size, "size"));
    }

Khalique's avatar
Khalique committed
78
79
80
81
82
83
84
85
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(1);
        return inputs.front();
    }

};

Paul's avatar
Paul committed
86
struct convolution
Paul's avatar
Paul committed
87
{
Paul's avatar
Paul committed
88
89
90
    std::array<std::size_t, 2> padding  = {{0, 0}};
    std::array<std::size_t, 2> stride   = {{1, 1}};
    std::array<std::size_t, 2> dilation = {{1, 1}};
Khalique's avatar
Khalique committed
91

Paul's avatar
Paul committed
92
    padding_mode_t padding_mode = default_;
Khalique's avatar
Khalique committed
93
    int group                   = 1;
Paul's avatar
Paul committed
94
95
96
97

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
98
99
100
        return pack(f(self.padding, "padding"),
                    f(self.stride, "stride"),
                    f(self.dilation, "dilation"),
Khalique's avatar
Khalique committed
101
102
                    f(self.padding_mode, "padding_mode"),
                    f(self.group, "group"));
Paul's avatar
Paul committed
103
104
    }

Paul's avatar
Paul committed
105
    std::string name() const { return "convolution"; }
Paul's avatar
Paul committed
106
107
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
108
        check_shapes{inputs, *this}.has(2).same_type().same_ndims().only_dims(4);
Paul's avatar
Paul committed
109

Paul's avatar
Paul committed
110
        const shape& input   = inputs.at(0);
Paul's avatar
Paul committed
111
        const shape& weights = inputs.at(1);
Paul's avatar
Paul committed
112
        auto t               = input.type();
Paul's avatar
Paul committed
113
114
        if(padding_mode == default_)
        {
Paul's avatar
Paul committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
            return {t,
                    {
                        input.lens()[0],
                        weights.lens()[0],
                        std::size_t(std::max<std::ptrdiff_t>(
                            1,
                            (input.lens()[2] - (1 + dilation[0] * (weights.lens()[2] - 1)) +
                             2 * padding[0]) /
                                    stride[0] +
                                1)),
                        std::size_t(std::max<std::ptrdiff_t>(
                            1,
                            (input.lens()[3] - (1 + dilation[1] * (weights.lens()[3] - 1)) +
                             2 * padding[1]) /
                                    stride[1] +
                                1)),
                    }};
Paul's avatar
Paul committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
        }
        else if(padding_mode == same)
        {
            return {t,
                    {input.lens()[0],
                     weights.lens()[0],
                     static_cast<std::size_t>(
                         std::ceil(static_cast<double>(input.lens()[2]) / stride[0])),
                     static_cast<std::size_t>(
                         std::ceil(static_cast<double>(input.lens()[3]) / stride[1]))}};
        }
        else if(padding_mode == valid)
        {
            return {
                t,
                {input.lens()[0],
                 weights.lens()[0],
                 static_cast<std::size_t>(std::ceil(
                     static_cast<double>(input.lens()[2] - weights.lens()[2] + 1) / stride[0])),
                 static_cast<std::size_t>(std::ceil(
                     static_cast<double>(input.lens()[3] - weights.lens()[3] + 1) / stride[1]))}};
        }
        else
        {
Paul's avatar
Paul committed
156
            MIGRAPHX_THROW("Invalid padding mode");
Paul's avatar
Paul committed
157
        }
Paul's avatar
Paul committed
158
159
160
    }
};

Scott Thornton's avatar
Scott Thornton committed
161
162
struct im2col
{
Scott Thornton's avatar
Scott Thornton committed
163
164
165
    std::array<std::size_t, 2> padding  = {{0, 0}};
    std::array<std::size_t, 2> stride   = {{1, 1}};
    std::array<std::size_t, 2> dilation = {{1, 1}};
Khalique's avatar
Khalique committed
166

Paul's avatar
Paul committed
167
168
169
170
171
    padding_mode_t padding_mode = default_;

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
172
173
174
175
        return pack(f(self.padding, "padding"),
                    f(self.stride, "stride"),
                    f(self.dilation, "dilation"),
                    f(self.padding_mode, "padding_mode"));
Paul's avatar
Paul committed
176
    }
Scott Thornton's avatar
Scott Thornton committed
177
178
179
180
181

    std::string name() const { return "im2col"; }

    shape compute_shape(std::vector<shape> inputs) const
    {
Scott Thornton's avatar
Scott Thornton committed
182
183
184
        auto input          = inputs[0];
        auto weights        = inputs[1];
        auto batch_size     = input.lens()[0];
Scott Thornton's avatar
Scott Thornton committed
185
        auto input_channels = weights.lens()[1];
Scott Thornton's avatar
Scott Thornton committed
186
187
        auto kernel_height  = weights.lens()[2];
        auto kernel_width   = weights.lens()[3];
Scott Thornton's avatar
Scott Thornton committed
188
        check_shapes{inputs, *this}.has(2);
Scott Thornton's avatar
Scott Thornton committed
189
        if(batch_size != 1)
Paul's avatar
Paul committed
190
            MIGRAPHX_THROW("im2col only support batch_size 1");
Scott Thornton's avatar
Scott Thornton committed
191
        auto output_height = std::size_t(std::max<std::ptrdiff_t>(
Scott Thornton's avatar
Scott Thornton committed
192
193
194
            1,
            (input.lens()[2] - (1 + dilation[0] * (kernel_height - 1)) + 2 * padding[0]) /
                    stride[0] +
Scott Thornton's avatar
Scott Thornton committed
195
                1));
Scott Thornton's avatar
Scott Thornton committed
196
197
198
199
        auto output_width  = std::size_t(std::max<std::ptrdiff_t>(
            1,
            (input.lens()[3] - (1 + dilation[1] * (kernel_width - 1)) + 2 * padding[1]) /
                    stride[1] +
Scott Thornton's avatar
Scott Thornton committed
200
                1));
Scott Thornton's avatar
Scott Thornton committed
201
202
        auto channels_col  = kernel_height * kernel_width * input_channels;
        return {input.type(), {output_height * output_width, channels_col}};
Scott Thornton's avatar
Scott Thornton committed
203
204
205
    }
};

Paul's avatar
Paul committed
206
struct pooling
Paul's avatar
Paul committed
207
{
Paul's avatar
Paul committed
208
    std::string mode                   = "average";
Paul's avatar
Paul committed
209
210
211
    std::array<std::size_t, 2> padding = {{0, 0}};
    std::array<std::size_t, 2> stride  = {{1, 1}};
    std::array<std::size_t, 2> lengths = {{1, 1}};
Khalique's avatar
Khalique committed
212
    padding_mode_t padding_mode        = default_;
Paul's avatar
Paul committed
213
214
215
216

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
217
218
        return pack(f(self.mode, "mode"),
                    f(self.padding, "padding"),
219
                    f(self.padding, "padding_mode"),
Paul's avatar
Paul committed
220
221
                    f(self.stride, "stride"),
                    f(self.lengths, "lengths"));
Paul's avatar
Paul committed
222
223
    }

Paul's avatar
Paul committed
224
    std::string name() const { return "pooling"; }
Scott Thornton's avatar
Scott Thornton committed
225

Paul's avatar
Paul committed
226
227
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
228
        check_shapes{inputs, *this}.has(1).only_dims(4);
Paul's avatar
Paul committed
229

Paul's avatar
Paul committed
230
        const shape& input = inputs.at(0);
Paul's avatar
Paul committed
231
        auto t             = input.type();
Paul's avatar
Paul committed
232

Paul's avatar
Paul committed
233
234
        assert(lengths[0] <= (input.lens()[2] + 2 * padding[0]));
        assert(lengths[1] <= (input.lens()[3] + 2 * padding[1]));
Paul's avatar
Paul committed
235

236
237
        if(padding_mode == default_)
        {
Khalique's avatar
Khalique committed
238
239
            return {
                t,
Scott Thornton's avatar
Scott Thornton committed
240
241
242
243
244
                {
                    input.lens()[0],
                    input.lens()[1],
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
Paul's avatar
Paul committed
245
                        std::ptrdiff_t(std::floor((input.lens()[2] + 2 * padding[0] - lengths[0]) /
Paul's avatar
Paul committed
246
                                                  static_cast<float>(stride[0]))) +
Scott Thornton's avatar
Scott Thornton committed
247
248
249
                            1)),
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
Paul's avatar
Paul committed
250
                        std::ptrdiff_t(std::floor((input.lens()[3] + 2 * padding[1] - lengths[1]) /
Paul's avatar
Paul committed
251
                                                  static_cast<float>(stride[1]))) +
Scott Thornton's avatar
Scott Thornton committed
252
253
                            1)),
                }};
254
255
256
257
258
259
260
261
262
263
264
265
266
267
        }
        else if(padding_mode == same)
        {
            return {t,
                    {input.lens()[0],
                     input.lens()[1],
                     static_cast<std::size_t>(
                         std::ceil(static_cast<double>(input.lens()[2]) / stride[0])),
                     static_cast<std::size_t>(
                         std::ceil(static_cast<double>(input.lens()[3]) / stride[1]))}};
        }
        else if(padding_mode == valid)
        {
            return {t,
Khalique's avatar
Khalique committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
                    {
                        input.lens()[0],
                        input.lens()[1],
                        std::size_t(std::max<std::ptrdiff_t>(
                            1,
                            std::ptrdiff_t(std::floor((input.lens()[2] - lengths[0]) /
                                                      static_cast<float>(stride[0]))) +
                                1)),
                        std::size_t(std::max<std::ptrdiff_t>(
                            1,
                            std::ptrdiff_t(std::floor((input.lens()[3] - lengths[1]) /
                                                      static_cast<float>(stride[1]))) +
                                1)),
                    }};
282
283
284
285
286
        }
        else
        {
            MIGRAPHX_THROW("Invalid padding mode");
        }
Paul's avatar
Paul committed
287
288
289
    }
};

Khalique's avatar
Khalique committed
290
291
292
293
294
295
296
297
298
struct leaky_relu
{
    std::string name() const { return "leaky_relu"; }
    float alpha;
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(1);
        return inputs.front();
    }
Khalique's avatar
Khalique committed
299
300
301

    template <class Self, class F>
    static auto reflect(Self& self, F f)
Khalique's avatar
Khalique committed
302
    {
Khalique's avatar
Khalique committed
303
        return pack(f(self.alpha, "alpha"));
Khalique's avatar
Khalique committed
304
305
306
307
308
309
310
311
312
313
314
315
    }
};

struct elu
{
    std::string name() const { return "elu"; }
    float alpha;
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(1);
        return inputs.front();
    }
Khalique's avatar
Khalique committed
316
317
318

    template <class Self, class F>
    static auto reflect(Self& self, F f)
Khalique's avatar
Khalique committed
319
    {
Khalique's avatar
Khalique committed
320
        return pack(f(self.alpha, "alpha"));
Khalique's avatar
Khalique committed
321
    }
Khalique's avatar
Khalique committed
322
323
};

324
325
326
struct transpose
{
    std::vector<int64_t> dims;
Paul's avatar
Paul committed
327
328
329
330

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
331
        return pack(f(self.dims, "dims"));
Paul's avatar
Paul committed
332
333
    }

334
335
336
    std::string name() const { return "transpose"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
337
        check_shapes{inputs, *this}.has(1);
338
        auto input         = inputs.at(0);
339
        auto input_lens    = input.lens();
340
341
        auto input_strides = input.strides();
        auto t             = input.type();
Paul's avatar
Paul committed
342
343
        if(dims.size() != input_lens.size())
        {
Paul's avatar
Paul committed
344
            MIGRAPHX_THROW("Permutation has wrong number of axes");
345
346
347
        }
        std::vector<int64_t> axes(dims.size());
        std::iota(axes.begin(), axes.end(), 0);
Paul's avatar
Paul committed
348
349
        if(!std::is_permutation(axes.begin(), axes.end(), dims.begin()))
        {
Paul's avatar
Paul committed
350
            MIGRAPHX_THROW("Invalid permutation");
351
        }
352
353
        std::vector<size_t> output_lens(input_lens.size());
        std::vector<size_t> output_strides(input_lens.size());
Paul's avatar
Paul committed
354
        for(std::size_t i = 0; i < output_lens.size(); i++)
Paul's avatar
Paul committed
355
356
        {
            output_lens[i]    = input_lens[dims[i]];
357
358
            output_strides[i] = input_strides[dims[i]];
        }
359
        return {t, output_lens, output_strides};
360
    }
Paul's avatar
Paul committed
361
    argument compute(shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
362
    {
Paul's avatar
Paul committed
363
        return {std::move(output_shape), std::move(args.front().data)};
Paul's avatar
Paul committed
364
    }
Paul's avatar
Paul committed
365
    int output_alias(const std::vector<shape>&) const { return 0; }
366
367
};

wsttiger's avatar
fixes  
wsttiger committed
368
369
370
371
372
373
/// The contiguous operator takes a non-standard input tensor and returns
/// the same tensor but in standard form. For example, if input tensor A which has lens = (4,5)
/// is first transposed, i.e. lens = (5,4), this tensor's data layout remained the same
/// during the transpose operation; only it's shape lengths and strides were changed.
/// This leaves the tensor in a non-standard form. The contiguous operator copies the
/// underlying data such that resulting tensor is returned to a standard form.
Paul's avatar
Paul committed
374
struct contiguous
375
376
377
378
{
    std::string name() const { return "contiguous"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
379
        check_shapes{inputs, *this}.has(1);
Paul's avatar
Paul committed
380
381
        auto lens = inputs.at(0).lens();
        auto t    = inputs.at(0).type();
382
383
384
385
        return {t, lens};
    }
};

386
387
388
389
struct concat
{
    std::size_t axis = 0;
    std::string name() const { return "concat"; }
390
    std::vector<std::size_t> compute_offsets(const shape& output_shape,
Paul's avatar
Paul committed
391
                                             const std::vector<argument>& args) const
392
393
394
395
396
397
398
399
400
401
402
    {
        std::vector<std::size_t> offsets;
        std::vector<std::size_t> offset(args[0].get_shape().lens().size(), 0);
        offset[axis] = 0;
        for(const auto& arg : args)
        {
            offsets.push_back(output_shape.index(offset));
            offset[axis] += arg.get_shape().lens()[axis];
        }
        return offsets;
    }
403
404
    shape compute_shape(std::vector<shape> inputs) const
    {
Scott Thornton's avatar
Scott Thornton committed
405
        if(inputs.empty())
406
        {
Paul's avatar
Paul committed
407
            MIGRAPHX_THROW("Number of input tensors should exceed 0");
408
409
410
        }

        const auto& first_shape_lens = inputs.front().lens();
Scott Thornton's avatar
Scott Thornton committed
411
412
413
414
415
416
417
418
419
        const auto& type             = inputs.front().type();
        for(std::size_t l = 0; l < first_shape_lens.size(); l++)
        {
            if(l != axis)
            {
                if(!std::all_of(inputs.begin(), inputs.end(), [&](auto s) {
                       return s.lens()[l] == first_shape_lens[l];
                   }))
                {
Paul's avatar
Paul committed
420
                    MIGRAPHX_THROW("Non-axis dimensions should match");
421
422
423
424
                }
            }
        }
        std::size_t new_dim_axis = 0;
Scott Thornton's avatar
Scott Thornton committed
425
        for(const auto& input : inputs)
426
427
428
429
430
431
432
433
434
        {
            const auto& lens = input.lens();
            new_dim_axis += lens[axis];
        }
        std::vector<std::size_t> new_lens;
        std::copy(first_shape_lens.begin(), first_shape_lens.end(), std::back_inserter(new_lens));
        new_lens[axis] = new_dim_axis;
        return {type, new_lens};
    }
Paul's avatar
Paul committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
    argument compute(const shape& output_shape, std::vector<argument> args) const
    {
        argument result{output_shape};
        std::vector<std::size_t> coffsets = compute_offsets(output_shape, args);
        for(std::size_t l = 0; l < args.size(); l++)
        {
            auto argl             = args[l];
            std::size_t nelements = argl.get_shape().elements();
            visit_all(result, argl)([&](auto output, auto input) {
                auto slice_shape =
                    shape{output_shape.type(), input.get_shape().lens(), output_shape.strides()};
                auto slice = make_view(slice_shape, output.data() + coffsets[l]);
                // cppcheck-suppress useStlAlgorithm
                for(std::size_t i = 0; i < nelements; i++)
                {
                    slice[i] = input[i];
                }
            });
        }
        return result;
    }
Paul's avatar
Paul committed
456
    int output_alias(const std::vector<shape>&) const { return 0; }
457
458
};

459
460
461
462
463
struct slice
{
    std::vector<int64_t> axes;
    std::vector<int64_t> starts;
    std::vector<int64_t> ends;
Paul's avatar
Paul committed
464
465
466
467

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
468
        return pack(f(self.axes, "axes"), f(self.starts, "starts"), f(self.ends, "ends"));
Paul's avatar
Paul committed
469
470
    }

471
    std::string name() const { return "slice"; }
Scott Thornton's avatar
Scott Thornton committed
472
473

    auto fix_index(const std::vector<std::size_t>& lens, std::size_t axis, int64_t index) const
474
    {
Scott Thornton's avatar
Scott Thornton committed
475
        int64_t r = std::min(index, static_cast<int64_t>(lens[axis]));
Scott Thornton's avatar
Scott Thornton committed
476
477
        if(r < 0)
            r += lens[axis];
Scott Thornton's avatar
Scott Thornton committed
478
        return std::size_t(r);
Scott Thornton's avatar
Scott Thornton committed
479
480
481
482
483
484
485
    }

    auto compute_offset(const shape& s) const
    {
        const std::vector<std::size_t>& lens    = s.lens();
        const std::vector<std::size_t>& strides = s.strides();
        auto offset                             = 0;
Scott Thornton's avatar
Scott Thornton committed
486
        if(!axes.empty())
Scott Thornton's avatar
Scott Thornton committed
487
        {
Scott Thornton's avatar
Scott Thornton committed
488
489
490
491
492
            for(std::size_t i = 0; i < axes.size(); i++)
            {
                auto axis = axes[i];
                offset += fix_index(lens, axis, starts[i]) * strides[axis];
            }
493
        }
Scott Thornton's avatar
Scott Thornton committed
494
495
        else
        {
Scott Thornton's avatar
Scott Thornton committed
496
497
498
499
            for(std::size_t axis = 0; axis < lens.size(); axis++)
            {
                offset += fix_index(lens, axis, starts[axis]) * strides[axis];
            }
500
        }
Scott Thornton's avatar
Scott Thornton committed
501
502
503
504
505
        return offset;
    }

    shape compute_shape(std::vector<shape> inputs) const
    {
Scott Thornton's avatar
Scott Thornton committed
506
507
508
509
        auto input_shape        = inputs[0];
        auto t                  = input_shape.type();
        const auto& old_lens    = input_shape.lens();
        const auto& old_strides = input_shape.strides();
Scott Thornton's avatar
Scott Thornton committed
510
        if(starts.size() != axes.size() || axes.size() != ends.size())
Scott Thornton's avatar
Scott Thornton committed
511
        {
Paul's avatar
Paul committed
512
            MIGRAPHX_THROW("inconsistent sizes");
513
        }
Scott Thornton's avatar
Scott Thornton committed
514
515
        std::vector<std::size_t> new_lens = old_lens;
        for(std::size_t i = 0; i < axes.size(); i++)
Scott Thornton's avatar
Scott Thornton committed
516
        {
Scott Thornton's avatar
Scott Thornton committed
517
518
519
            auto axis = axes[i];
            new_lens[axis] =
                fix_index(old_lens, axis, ends[i]) - fix_index(old_lens, axis, starts[i]);
520
521
522
        }
        return shape{t, new_lens, old_strides};
    }
Paul's avatar
Paul committed
523
    argument compute(shape output_shape, std::vector<argument> args) const
524
    {
Scott Thornton's avatar
Scott Thornton committed
525
526
527
        auto input  = args[0];
        auto offset = compute_offset(input.get_shape()) * output_shape.type_size();
        return {std::move(output_shape), [=] { return input.data() + offset; }};
528
    }
Paul's avatar
Paul committed
529
    int output_alias(const std::vector<shape>&) const { return 0; }
530
531
532
533
534
};

struct squeeze
{
    std::vector<int64_t> axes;
Paul's avatar
Paul committed
535
536
537
538

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
539
        return pack(f(self.axes, "axes"));
Paul's avatar
Paul committed
540
541
    }

542
543
544
545
    std::string name() const { return "squeeze"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
        auto input_shape = inputs[0];
Scott Thornton's avatar
Scott Thornton committed
546
547
        auto type        = input_shape.type();
        auto old_lens    = input_shape.lens();
wsttiger's avatar
wsttiger committed
548
549
        if(std::any_of(
               axes.begin(), axes.end(), [&](auto axis) { return input_shape.lens()[axis] != 1; }))
Scott Thornton's avatar
Scott Thornton committed
550
        {
Paul's avatar
Paul committed
551
            MIGRAPHX_THROW("squeeze axis dimension should be equal to 1");
552
553
        }
        std::vector<std::size_t> new_lens;
Scott Thornton's avatar
Scott Thornton committed
554
        if(axes.empty())
Scott Thornton's avatar
Scott Thornton committed
555
        {
wsttiger's avatar
wsttiger committed
556
557
558
559
            std::copy_if(old_lens.begin(),
                         old_lens.end(),
                         std::back_inserter(new_lens),
                         [](auto len) { return len != 1; });
560
        }
Scott Thornton's avatar
Scott Thornton committed
561
562
563
564
565
566
        else
        {
            for(std::size_t i = 0; i < old_lens.size(); i++)
            {
                if(std::find(axes.begin(), axes.end(), i) == axes.end())
                {
567
568
569
570
571
572
                    new_lens.push_back(old_lens[i]);
                }
            }
        }
        return shape{type, new_lens};
    }
Paul's avatar
Paul committed
573
    argument compute(shape output_shape, std::vector<argument> args) const
574
575
    {
        return {std::move(output_shape), std::move(args.front().data)};
Scott Thornton's avatar
Scott Thornton committed
576
    }
Paul's avatar
Paul committed
577
    int output_alias(const std::vector<shape>&) const { return 0; }
578
579
580
581
582
};

struct unsqueeze
{
    std::vector<int64_t> axes;
Paul's avatar
Paul committed
583
584
585
586

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
587
        return pack(f(self.axes, "axes"));
Paul's avatar
Paul committed
588
589
    }

590
591
592
    std::string name() const { return "unsqueeze"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Scott Thornton's avatar
Scott Thornton committed
593
594
595
        auto input_shape     = inputs[0];
        auto type            = input_shape.type();
        auto old_lens        = input_shape.lens();
596
597
598
        std::size_t new_size = old_lens.size() + axes.size();
        std::vector<std::size_t> new_lens(new_size);
        std::size_t p = 0;
Scott Thornton's avatar
Scott Thornton committed
599
600
601
602
        for(std::size_t i = 0; i < new_size; i++)
        {
            if(std::find(axes.begin(), axes.end(), i) != axes.end())
            {
603
                new_lens[i] = 1;
Scott Thornton's avatar
Scott Thornton committed
604
605
606
            }
            else
            {
607
608
609
610
611
                new_lens[i] = old_lens[p++];
            }
        }
        return shape{type, new_lens};
    }
Paul's avatar
Paul committed
612
    argument compute(shape output_shape, std::vector<argument> args) const
613
614
615
    {
        return {std::move(output_shape), std::move(args.front().data)};
    }
Paul's avatar
Paul committed
616
    int output_alias(const std::vector<shape>&) const { return 0; }
617
618
};

Paul's avatar
Paul committed
619
620
621
struct reshape
{
    std::vector<int64_t> dims;
Paul's avatar
Paul committed
622
623
624
625

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
626
        return pack(f(self.dims, "dims"));
Paul's avatar
Paul committed
627
628
    }

Paul's avatar
Paul committed
629
    std::string name() const { return "reshape"; }
Paul's avatar
Paul committed
630
631
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
632
        check_shapes{inputs, *this}.has(1);
Paul's avatar
Paul committed
633
634
        auto&& idims = inputs.front().lens();
        std::vector<std::size_t> rdims(dims.begin(), dims.end());
635
636
        auto n_neg_dims = std::count(dims.begin(), dims.end(), -1);
        if(n_neg_dims > 1)
Paul's avatar
Paul committed
637
            MIGRAPHX_THROW("Dimensions for reshape can only have one -1 dim");
Paul's avatar
Paul committed
638
        for(std::size_t i = 0; i < dims.size(); i++)
Paul's avatar
Paul committed
639
640
641
642
        {
            if(dims[i] == 0)
                rdims[i] = idims[i];
        }
643
644
645
646
647
648
649
650
651
652
653
        if(n_neg_dims > 0)
        {
            size_t missing_dim =
                -inputs.front().elements() /
                std::accumulate(rdims.begin(), rdims.end(), 1, std::multiplies<int64_t>());
            for(std::size_t i = 0; i < rdims.size(); i++)
            {
                if(dims[i] == -1)
                    rdims[i] = missing_dim;
            }
        }
Paul's avatar
Paul committed
654
655
656
        if(dims.back() == -1)
        {
            rdims.pop_back();
Paul's avatar
Paul committed
657
            std::copy(idims.begin() + rdims.size(), idims.end(), std::back_inserter(rdims));
Paul's avatar
Paul committed
658
        }
Scott Thornton's avatar
Scott Thornton committed
659
        shape s{inputs.front().type(), rdims};
Paul's avatar
Paul committed
660
        if(s.elements() != inputs.front().elements())
Paul's avatar
Paul committed
661
            MIGRAPHX_THROW("Wrong number of elements for reshape");
Scott Thornton's avatar
Scott Thornton committed
662
        return s;
Paul's avatar
Paul committed
663
    }
Paul's avatar
Paul committed
664
    argument compute(shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
665
    {
Paul's avatar
Paul committed
666
        return {std::move(output_shape), std::move(args.front().data)};
Paul's avatar
Paul committed
667
    }
Paul's avatar
Paul committed
668
    int output_alias(const std::vector<shape>&) const { return 0; }
Paul's avatar
Paul committed
669
670
};

Khalique's avatar
Khalique committed
671
672
673
struct pad
{
    std::vector<int64_t> pads;
Khalique's avatar
Khalique committed
674
    float value = 0.0f;
Khalique's avatar
Khalique committed
675
    enum pad_op_mode_t
Khalique's avatar
Khalique committed
676
    {
Khalique's avatar
Khalique committed
677
        constant_pad,
Khalique's avatar
Khalique committed
678
        reflect_pad,
Khalique's avatar
Khalique committed
679
        edge_pad
Khalique's avatar
Khalique committed
680
    };
Khalique's avatar
Khalique committed
681
    pad_op_mode_t mode = constant_pad;
Khalique's avatar
Khalique committed
682
683
684
685
686
687
688
689
690
691
692
693
694

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.mode, "mode"), f(self.pads, "pads"), f(self.value, "value"));
    }

    std::string name() const { return "pad"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(1);
        auto&& idims = inputs.front().lens();
        std::vector<std::size_t> rdims(idims.begin(), idims.end());
Khalique's avatar
Khalique committed
695
        std::size_t num_dims = rdims.size();
Khalique's avatar
Khalique committed
696

Khalique's avatar
Khalique committed
697
        for(std::size_t i = 0; i < num_dims; i++)
Khalique's avatar
Khalique committed
698
699
700
        {
            rdims[i] += pads[i] + pads[i + num_dims];
        }
Khalique's avatar
Khalique committed
701

Khalique's avatar
Khalique committed
702
703
704
705
706
        shape s{inputs.front().type(), rdims};
        return s;
    }
};

Paul's avatar
Paul committed
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
struct as_shape
{
    shape s;
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.s, "shape"));
    }

    std::string name() const { return "as_shape"; }
    shape compute_shape(const std::vector<shape>& inputs) const
    {
        check_shapes{inputs, *this}.has(1).standard();
        assert(inputs.front().elements() == s.elements());
        return s;
    }
    argument compute(shape output_shape, std::vector<argument> args) const
    {
        return {std::move(output_shape), std::move(args.front().data)};
    }
    int output_alias(const std::vector<shape>&) const { return 0; }
};

730
731
struct gather
{
732
    int axis = 0;
733
734
735
736
737
738
    std::string name() const { return "gather"; }

    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(2);
        auto lens = inputs[0].lens();
739
740
        int n_dim = static_cast<int>(lens.size());
        if(axis >= n_dim || axis < -n_dim)
741
        {
742
            MIGRAPHX_THROW("Gather: axis is out of range.");
743
        }
744
745

        // negative axis means counting dimensions from back
746
        int axis_index = (axis < 0) ? (n_dim + axis) : axis;
747

Shucai Xiao's avatar
Shucai Xiao committed
748
        auto type        = inputs[0].type();
749
        lens[axis_index] = inputs[1].elements();
750
751
752
753
754

        return {type, lens};
    }

    template <class T>
Shucai Xiao's avatar
Shucai Xiao committed
755
    void compute_index(const T& out_idx,
Shucai Xiao's avatar
Shucai Xiao committed
756
                       const int axis_index,
Shucai Xiao's avatar
Shucai Xiao committed
757
758
759
                       const std::vector<std::size_t>& vec_indices,
                       const std::size_t max_dim,
                       T& in_idx) const
760
    {
Shucai Xiao's avatar
Shucai Xiao committed
761
        in_idx          = out_idx;
762
        std::size_t idx = vec_indices.at(out_idx[axis_index]);
763
764
        if(idx >= max_dim)
        {
765
            MIGRAPHX_THROW("Gather: indices are out of range in input tensor");
766
        }
767
        in_idx[axis_index] = idx;
768
769
770
771
772
    }

    argument compute(const shape& output_shape, std::vector<argument> args) const
    {
        argument result{output_shape};
773
774
775
        // negative axis means counting dimensions from back
        int axis_index = (axis < 0) ? (output_shape.lens().size() + axis) : axis;

776
        // max dimension in axis
777
        std::size_t max_dim = args[0].get_shape().lens()[axis_index];
778
779
        std::vector<std::size_t> vec_indices;
        args[1].visit([&](auto indices) { vec_indices.assign(indices.begin(), indices.end()); });
780
        visit_all(result, args[0])([&](auto output, auto input) {
781
            std::vector<std::size_t> in_idx;
782
            shape_for_each(output.get_shape(), [&](const auto& idx) {
783
                this->compute_index(idx, axis_index, vec_indices, max_dim, in_idx);
784
785
786
787
788
789
790
791
792
793
                output(idx.begin(), idx.end()) = input(in_idx.begin(), in_idx.end());
            });
        });

        return result;
    }

    int output_alias(const std::vector<shape>&) const { return 0; }
};

Shucai Xiao's avatar
Shucai Xiao committed
794
struct dot
795
{
Paul's avatar
Paul committed
796
    float alpha = 1.0;
Paul's avatar
Paul committed
797
    float beta  = 0.0;
Paul's avatar
Paul committed
798
799
800
801

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
802
        return pack(f(self.alpha, "alpha"), f(self.beta, "beta"));
Paul's avatar
Paul committed
803
804
    }

Shucai Xiao's avatar
Shucai Xiao committed
805
    std::string name() const { return "dot"; }
806
807
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
808
        check_shapes{inputs, *this}.has(2).same_type();
809
810
        const shape& a = inputs.at(0);
        const shape& b = inputs.at(1);
Scott Thornton's avatar
Scott Thornton committed
811
        auto t         = a.type();
812

813
        if(a.lens()[1] != b.lens()[0])
Paul's avatar
Paul committed
814
815
            MIGRAPHX_THROW("Inner dimensions do not match: {" + to_string_range(a.lens()) +
                           "} x {" + to_string_range(b.lens()) + "}");
Scott Thornton's avatar
Scott Thornton committed
816
        return {t, {a.lens()[0], b.lens()[1]}};
817
818
819
    }
};

820
struct unary
Scott Thornton's avatar
Scott Thornton committed
821
{
822
823
    shape compute_shape(std::vector<shape> inputs) const
    {
824
825
        check_shapes{inputs}.has(1);
        return inputs.at(0);
826
    }
Scott Thornton's avatar
Scott Thornton committed
827
828
};

829
struct identity
830
{
831
    std::string name() const { return "identity"; }
Scott Thornton's avatar
Scott Thornton committed
832
    shape compute_shape(std::vector<shape> inputs) const { return inputs.at(0); }
Paul's avatar
Paul committed
833
    argument compute(shape output_shape, std::vector<argument> args) const
834
835
836
    {
        return {std::move(output_shape), std::move(args.at(0).data)};
    }
Paul's avatar
Paul committed
837
    int output_alias(const std::vector<shape>&) const { return 0; }
838
839
840
};

struct abs : unary
Scott Thornton's avatar
Scott Thornton committed
841
{
842
    std::string name() const { return "abs"; }
Scott Thornton's avatar
Scott Thornton committed
843
844
};

845
struct exp : unary
Scott Thornton's avatar
Scott Thornton committed
846
{
847
    std::string name() const { return "exp"; }
Scott Thornton's avatar
Scott Thornton committed
848
849
};

Shucai Xiao's avatar
Shucai Xiao committed
850
851
852
853
854
struct log : unary
{
    std::string name() const { return "log"; }
};

855
struct sin : unary
Scott Thornton's avatar
Scott Thornton committed
856
{
857
    std::string name() const { return "sin"; }
Scott Thornton's avatar
Scott Thornton committed
858
859
};

860
struct cos : unary
Scott Thornton's avatar
Scott Thornton committed
861
{
862
    std::string name() const { return "cos"; }
Scott Thornton's avatar
Scott Thornton committed
863
864
};

865
struct tan : unary
Scott Thornton's avatar
Scott Thornton committed
866
{
867
    std::string name() const { return "tan"; }
Scott Thornton's avatar
Scott Thornton committed
868
869
};

870
struct asin : unary
Scott Thornton's avatar
Scott Thornton committed
871
{
872
    std::string name() const { return "asin"; }
Scott Thornton's avatar
Scott Thornton committed
873
874
};

875
struct acos : unary
Scott Thornton's avatar
Scott Thornton committed
876
{
877
    std::string name() const { return "acos"; }
Scott Thornton's avatar
Scott Thornton committed
878
879
};

880
struct atan : unary
Scott Thornton's avatar
Scott Thornton committed
881
{
882
    std::string name() const { return "atan"; }
Scott Thornton's avatar
Scott Thornton committed
883
884
};

885
886
887
888
889
890
891
892
893
894
struct sinh : unary
{
    std::string name() const { return "sinh"; }
};

struct cosh : unary
{
    std::string name() const { return "cosh"; }
};

895
struct tanh : unary
Scott Thornton's avatar
Scott Thornton committed
896
{
897
    std::string name() const { return "tanh"; }
Scott Thornton's avatar
Scott Thornton committed
898
899
};

900
struct sigmoid : unary
Scott Thornton's avatar
Scott Thornton committed
901
{
902
    std::string name() const { return "sigmoid"; }
Scott Thornton's avatar
Scott Thornton committed
903
904
};

905
struct neg : unary
Scott Thornton's avatar
Scott Thornton committed
906
{
907
    std::string name() const { return "neg"; }
Scott Thornton's avatar
Scott Thornton committed
908
909
};

Khalique's avatar
Khalique committed
910
911
912
913
914
struct relu : unary
{
    std::string name() const { return "relu"; }
};

Paul's avatar
Paul committed
915
916
917
918
919
920
921
922
923
924
struct softmax
{
    std::string name() const { return "softmax"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs}.has(1).only_dims(4);
        return inputs.at(0);
    }
};

925
struct flatten
Scott Thornton's avatar
Scott Thornton committed
926
{
Paul's avatar
Paul committed
927
    uint64_t axis = 0;
Paul's avatar
Paul committed
928
929
930
931

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
932
        return pack(f(self.axis, "axis"));
Paul's avatar
Paul committed
933
934
    }

Scott Thornton's avatar
Scott Thornton committed
935
    std::string name() const { return "flatten"; }
Paul's avatar
Paul committed
936
937
938
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs}.has(1);
Paul's avatar
Paul committed
939
940
        auto&& lens = inputs.front().lens();

Paul's avatar
Paul committed
941
        if(axis > lens.size())
Paul's avatar
Paul committed
942
        {
Paul's avatar
Paul committed
943
            MIGRAPHX_THROW("axis for flatten must be less than tensor rank");
Paul's avatar
Paul committed
944
        }
Paul's avatar
Paul committed
945
946
947
948
        auto x =
            std::accumulate(lens.begin(), lens.begin() + axis, std::size_t{1}, std::multiplies<>{});
        auto y =
            std::accumulate(lens.begin() + axis, lens.end(), std::size_t{1}, std::multiplies<>{});
949
        return {inputs.at(0).type(), {x, y}};
Paul's avatar
Paul committed
950
    }
Paul's avatar
Paul committed
951
    argument compute(shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
952
    {
Paul's avatar
Paul committed
953
        return {std::move(output_shape), std::move(args.front().data)};
Paul's avatar
Paul committed
954
    }
Paul's avatar
Paul committed
955
    int output_alias(const std::vector<shape>&) const { return 0; }
Scott Thornton's avatar
Scott Thornton committed
956
};
957

wsttiger's avatar
fixes  
wsttiger committed
958
959
960
961
962
963
964
965
/// The broadcast operator performs the numpy-style broadcasting of an axis of a given tensor. This
/// is achieved primarily by setting the stride of the broadcasted axis to zero. Linear indicies are
/// computed from multi-indicies by computing the inner product on the multi-index with the strides.
/// For example, if we have a tensor A(2,3) it has lengths of (2,3) and strides of (3,1). If we want
/// to compute the linear offset that corresponds to the element on the 2nd row (i = 1) and 3rd
/// column (j = 2), we compute the following inner product (1,2) dot (3, 1) = 1*3 + 2*1 = 5. It is
/// obvious from there that we can negate the effects of a given axis by setting the stride of that
/// axis to zero.
966
967
968
struct broadcast
{
    uint64_t axis = 0;
Paul's avatar
Paul committed
969
970
971
972

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
973
        return pack(f(self.axis, "axis"));
Paul's avatar
Paul committed
974
975
    }

Scott Thornton's avatar
Scott Thornton committed
976
    shape broadcast_shape;
977
978
979
    std::string name() const { return "broadcast"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Scott Thornton's avatar
Scott Thornton committed
980
981
        auto t     = inputs.at(0).type();
        auto input = inputs.at(0);
Paul's avatar
Paul committed
982

Scott Thornton's avatar
Scott Thornton committed
983
        std::vector<size_t> bcast_strides(broadcast_shape.lens().size(), 0);
984

Scott Thornton's avatar
Scott Thornton committed
985
986
987
        if(std::all_of(broadcast_shape.lens().cbegin(), broadcast_shape.lens().cend(), [&](auto x) {
               return x == 1;
           }))
988
        {
Scott Thornton's avatar
Scott Thornton committed
989
            if(axis != 0)
Paul's avatar
Paul committed
990
                MIGRAPHX_THROW("when broadcasting tensor of size 1, axis should be 0");
Scott Thornton's avatar
Scott Thornton committed
991
            return {t, broadcast_shape.lens(), std::move(bcast_strides)};
992
993
994
        }
        else
        {
Scott Thornton's avatar
Scott Thornton committed
995
            assert(broadcast_shape.lens().size() - axis >= input.lens().size());
Scott Thornton's avatar
Scott Thornton committed
996
997
            if(!std::equal(
                   input.lens().begin(), input.lens().end(), broadcast_shape.lens().begin() + axis))
Paul's avatar
Paul committed
998
                MIGRAPHX_THROW("when broadcasting success sizes must match");
Paul's avatar
Paul committed
999
            std::copy(input.strides().begin(), input.strides().end(), bcast_strides.begin() + axis);
Scott Thornton's avatar
Scott Thornton committed
1000
            return {t, broadcast_shape.lens(), std::move(bcast_strides)};
1001
1002
        }
    }
Paul's avatar
Paul committed
1003
    argument compute(shape output_shape, std::vector<argument> args) const
Scott Thornton's avatar
Scott Thornton committed
1004
    {
Scott Thornton's avatar
Scott Thornton committed
1005
        return {std::move(output_shape), std::move(args.at(0).data)};
Scott Thornton's avatar
Scott Thornton committed
1006
    }
Paul's avatar
Paul committed
1007
    int output_alias(const std::vector<shape>&) const { return 0; }
1008
1009
};

Scott Thornton's avatar
Scott Thornton committed
1010
1011
1012
struct multibroadcast
{
    std::vector<std::size_t> output_lens;
1013
1014
1015
1016
1017
1018
1019

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.output_lens, "output_lens"));
    }

Scott Thornton's avatar
Scott Thornton committed
1020
    std::string name() const { return "multibroadcast"; }
1021

Scott Thornton's avatar
Scott Thornton committed
1022
1023
1024
1025
1026
1027
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(1);
        auto t     = inputs.at(0).type();
        auto input = inputs.at(0);

wsttiger's avatar
wsttiger committed
1028
        if(input.lens().empty())
Paul's avatar
Paul committed
1029
            MIGRAPHX_THROW("inputs dimensions should be > 0");
Scott Thornton's avatar
Scott Thornton committed
1030

Scott Thornton's avatar
Scott Thornton committed
1031
        if(input.lens().size() > output_lens.size())
Paul's avatar
Paul committed
1032
            MIGRAPHX_THROW("inputs dimensions should <= output size");
Scott Thornton's avatar
Scott Thornton committed
1033
1034

        std::vector<size_t> bcast_strides(output_lens.size(), 0);
Scott Thornton's avatar
Scott Thornton committed
1035
1036
        auto offset = output_lens.size() - input.lens().size();
        for(int i = input.lens().size() - 1; i >= 0; i--)
Scott Thornton's avatar
Scott Thornton committed
1037
        {
Scott Thornton's avatar
Scott Thornton committed
1038
            if(output_lens[i + offset] == input.lens()[i])
Scott Thornton's avatar
Scott Thornton committed
1039
            {
Scott Thornton's avatar
Scott Thornton committed
1040
                bcast_strides[i + offset] = input.strides()[i];
Scott Thornton's avatar
Scott Thornton committed
1041
1042
1043
1044
            }
        }
        return {t, output_lens, bcast_strides};
    }
Paul's avatar
Paul committed
1045
    argument compute(shape output_shape, std::vector<argument> args) const
Scott Thornton's avatar
Scott Thornton committed
1046
1047
1048
1049
1050
1051
    {
        return {std::move(output_shape), std::move(args.at(0).data)};
    }
    int output_alias(const std::vector<shape>&) const { return 0; }
};

Khalique's avatar
Khalique committed
1052
1053
1054
1055
1056
1057
1058
1059
1060
struct scalar
{
    shape scalar_bcast;

    std::string name() const { return "scalar"; }

    shape compute_shape(std::vector<shape> inputs) const
    {
        assert(check_shapes{inputs}.has(1).only_dims(1).size() == 1);
Paul's avatar
Paul committed
1061
        auto t = inputs.at(0).type();
Khalique's avatar
Khalique committed
1062
1063
1064
1065
        std::vector<std::size_t> strides(scalar_bcast.lens().size(), 0);
        return {t, scalar_bcast.lens(), strides};
    }

Paul's avatar
Paul committed
1066
    argument compute(shape output_shape, std::vector<argument> args) const
Khalique's avatar
Khalique committed
1067
1068
1069
    {
        return {std::move(output_shape), std::move(args.at(0).data)};
    }
Paul's avatar
Paul committed
1070
    int output_alias(const std::vector<shape>&) const { return 0; }
Khalique's avatar
Khalique committed
1071
1072
};

1073
struct binary
Scott Thornton's avatar
Scott Thornton committed
1074
{
1075
1076
    shape compute_shape(std::vector<shape> inputs) const
    {
1077
        check_shapes{inputs}.has(2).same_type().same_dims();
Scott Thornton's avatar
Scott Thornton committed
1078
        auto t    = inputs.at(0).type();
1079
1080
        auto lens = inputs.at(0).lens();
        return {t, lens};
1081
    }
Scott Thornton's avatar
Scott Thornton committed
1082
1083
};

1084
1085
1086
1087
1088
1089
struct add : binary
{
    std::string name() const { return "add"; }
};

struct sub : binary
Scott Thornton's avatar
Scott Thornton committed
1090
1091
1092
1093
{
    std::string name() const { return "sub"; }
};

1094
struct mul : binary
Scott Thornton's avatar
Scott Thornton committed
1095
1096
1097
1098
{
    std::string name() const { return "mul"; }
};

1099
struct div : binary
Scott Thornton's avatar
Scott Thornton committed
1100
1101
1102
1103
{
    std::string name() const { return "div"; }
};

Khalique's avatar
Khalique committed
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
struct max : binary
{
    std::string name() const { return "max"; }
};

struct min : binary
{
    std::string name() const { return "min"; }
};

Paul's avatar
Paul committed
1114
1115
1116
1117
struct load
{
    shape s;
    std::size_t offset = 0;
Paul's avatar
Paul committed
1118
1119
1120
1121

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
1122
        return pack(f(self.s, "shape"), f(self.offset, "offset"));
Paul's avatar
Paul committed
1123
1124
    }

Paul's avatar
Paul committed
1125
1126
1127
1128
1129
1130
    std::string name() const { return "load"; }
    shape compute_shape(const std::vector<shape>& inputs) const
    {
        check_shapes{inputs}.has(1);
        return s;
    }
Paul's avatar
Paul committed
1131
    argument compute(const shape&, const std::vector<argument>& args) const
Paul's avatar
Paul committed
1132
1133
1134
    {
        return {s, args[0].data() + offset};
    }
Paul's avatar
Paul committed
1135
    int output_alias(const std::vector<shape>&) const { return 0; }
Paul's avatar
Paul committed
1136
1137
};

Paul's avatar
Paul committed
1138
struct outline
Scott Thornton's avatar
Scott Thornton committed
1139
{
Paul's avatar
Paul committed
1140
    shape s;
Paul's avatar
Paul committed
1141
1142
1143
1144

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
1145
        return pack(f(self.s, "shape"));
Paul's avatar
Paul committed
1146
1147
    }

Paul's avatar
Paul committed
1148
    std::string name() const { return "outline"; }
Paul's avatar
Paul committed
1149
    shape compute_shape(const std::vector<shape>& inputs) const
Paul's avatar
Paul committed
1150
    {
Paul's avatar
Paul committed
1151
        check_shapes{inputs, *this}.has(0);
Paul's avatar
Paul committed
1152
1153
        return s;
    }
Paul's avatar
Paul committed
1154
    argument compute(const shape&, const std::vector<argument>&) const { return {s, nullptr}; }
Scott Thornton's avatar
Scott Thornton committed
1155
1156
};

1157
} // namespace op
Paul's avatar
Paul committed
1158
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
1159
} // namespace migraphx
Paul's avatar
Paul committed
1160
1161

#endif