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

4
#include <array>
Paul's avatar
Paul committed
5
#include <migraph/operation.hpp>
Paul's avatar
Paul committed
6
#include <migraph/check_shapes.hpp>
Paul's avatar
Paul committed
7
8
#include <migraph/stringutils.hpp>
#include <migraph/streamutils.hpp>
Paul's avatar
Paul committed
9
#include <cmath>
Paul's avatar
Paul committed
10
#include <utility>
Paul's avatar
Paul committed
11

Paul's avatar
Paul committed
12
namespace migraph {
Paul's avatar
Paul committed
13

Paul's avatar
Paul committed
14
15
struct not_computable
{
Paul's avatar
Paul committed
16
    argument compute(context&, const shape&, const std::vector<argument>&) const
Paul's avatar
Paul committed
17
18
19
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
20
21
};

22
23
struct batch_norm_inference
{
24
25
    float epsilon  = 1.0e-6f;
    float momentum = 0.9f;
26
27
28

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

29
30
31
32
33
34
35
36
    enum bn_infer_mode_t
    {
        per_activation,
        spatial,
    };

    bn_infer_mode_t bn_mode = spatial;

37
38
    bool is_test = false;

39
40
41
42
43
44
45
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(5);
        return inputs.front();
    }
};

Paul's avatar
Paul committed
46
struct convolution
Paul's avatar
Paul committed
47
{
Paul's avatar
Paul committed
48
49
50
    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}};
Paul's avatar
Paul committed
51
52
53
54
55
56
57
    enum padding_mode_t
    {
        default_, // NOLINT
        same,
        valid
    };
    padding_mode_t padding_mode = default_;
Paul's avatar
Paul committed
58
    std::string name() const { return "convolution"; }
Paul's avatar
Paul committed
59
60
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
61
        check_shapes{inputs, *this}.has(2).same_type().same_ndims().only_dims(4);
Paul's avatar
Paul committed
62

Paul's avatar
Paul committed
63
        const shape& input   = inputs.at(0);
Paul's avatar
Paul committed
64
        const shape& weights = inputs.at(1);
Paul's avatar
Paul committed
65
        auto t               = input.type();
Paul's avatar
Paul committed
66
67
        if(padding_mode == default_)
        {
Paul's avatar
Paul committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
            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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
        }
        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
109
            MIGRAPH_THROW("Invalid padding mode");
Paul's avatar
Paul committed
110
        }
Paul's avatar
Paul committed
111
    }
Paul's avatar
Paul committed
112

Paul's avatar
Paul committed
113
    friend std::ostream& operator<<(std::ostream& os, const convolution& op)
Paul's avatar
Paul committed
114
    {
Paul's avatar
Paul committed
115
116
117
118
119
        os << op.name() << "[";
        os << "padding={" << stream_range(op.padding) << "}, ";
        os << "stride={" << stream_range(op.stride) << "}, ";
        os << "dilation={" << stream_range(op.dilation) << "}";
        os << "]";
Paul's avatar
Paul committed
120
121
        return os;
    }
Paul's avatar
Paul committed
122
123
};

Scott Thornton's avatar
Scott Thornton committed
124
125
struct im2col
{
Scott Thornton's avatar
Scott Thornton committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    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}};
    enum padding_mode_t
    {
        default_, // NOLINT
        same,
        valid
    };

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

    shape compute_shape(std::vector<shape> inputs) const
    {
Scott Thornton's avatar
Scott Thornton committed
140
141
142
        auto input          = inputs[0];
        auto weights        = inputs[1];
        auto batch_size     = input.lens()[0];
Scott Thornton's avatar
Scott Thornton committed
143
        auto input_channels = weights.lens()[1];
Scott Thornton's avatar
Scott Thornton committed
144
145
        auto kernel_height  = weights.lens()[2];
        auto kernel_width   = weights.lens()[3];
Scott Thornton's avatar
Scott Thornton committed
146
        check_shapes{inputs, *this}.has(2);
Scott Thornton's avatar
Scott Thornton committed
147
148
        if(batch_size != 1)
            MIGRAPH_THROW("im2col only support batch_size 1");
Scott Thornton's avatar
Scott Thornton committed
149
        auto output_height = std::size_t(std::max<std::ptrdiff_t>(
Scott Thornton's avatar
Scott Thornton committed
150
151
152
            1,
            (input.lens()[2] - (1 + dilation[0] * (kernel_height - 1)) + 2 * padding[0]) /
                    stride[0] +
Scott Thornton's avatar
Scott Thornton committed
153
                1));
Scott Thornton's avatar
Scott Thornton committed
154
155
156
157
        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
158
                1));
Scott Thornton's avatar
Scott Thornton committed
159
160
        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
161
162
163
    }
};

Paul's avatar
Paul committed
164
struct pooling
Paul's avatar
Paul committed
165
{
Paul's avatar
Paul committed
166
    std::string mode                   = "average";
Paul's avatar
Paul committed
167
168
169
    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}};
Paul's avatar
Paul committed
170
    std::string name() const { return "pooling"; }
Scott Thornton's avatar
Scott Thornton committed
171

Paul's avatar
Paul committed
172
173
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
174
        check_shapes{inputs, *this}.has(1).only_dims(4);
Paul's avatar
Paul committed
175

Paul's avatar
Paul committed
176
        const shape& input = inputs.at(0);
Paul's avatar
Paul committed
177
        auto t             = input.type();
Paul's avatar
Paul committed
178

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

Scott Thornton's avatar
Scott Thornton committed
182
183
184
185
186
187
        return {t,
                {
                    input.lens()[0],
                    input.lens()[1],
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
Paul's avatar
Paul committed
188
                        std::ptrdiff_t(std::floor((input.lens()[2] + 2 * padding[0] - lengths[0]) /
Paul's avatar
Paul committed
189
                                                  static_cast<float>(stride[0]))) +
Scott Thornton's avatar
Scott Thornton committed
190
191
192
                            1)),
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
Paul's avatar
Paul committed
193
                        std::ptrdiff_t(std::floor((input.lens()[3] + 2 * padding[1] - lengths[1]) /
Paul's avatar
Paul committed
194
                                                  static_cast<float>(stride[1]))) +
Scott Thornton's avatar
Scott Thornton committed
195
196
                            1)),
                }};
Paul's avatar
Paul committed
197
    }
Paul's avatar
Paul committed
198

Paul's avatar
Paul committed
199
    friend std::ostream& operator<<(std::ostream& os, const pooling& op)
Paul's avatar
Paul committed
200
    {
Paul's avatar
Paul committed
201
202
203
204
205
        os << op.name() << "[";
        os << "padding={" << stream_range(op.padding) << "}, ";
        os << "stride={" << stream_range(op.stride) << "}, ";
        os << "lengths={" << stream_range(op.lengths) << "}";
        os << "]";
Paul's avatar
Paul committed
206
207
        return os;
    }
Paul's avatar
Paul committed
208
209
};

Paul's avatar
Paul committed
210
struct activation
Paul's avatar
Paul committed
211
212
{
    std::string mode;
Paul's avatar
Paul committed
213
    std::string name() const { return "activation"; }
Paul's avatar
Paul committed
214
215
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
216
        check_shapes{inputs, *this}.has(1);
Paul's avatar
Paul committed
217
218
        return inputs.front();
    }
Paul's avatar
Paul committed
219
    friend std::ostream& operator<<(std::ostream& os, const activation& op)
Paul's avatar
Paul committed
220
    {
Paul's avatar
Paul committed
221
        os << op.name() << ":" << op.mode;
Paul's avatar
Paul committed
222
223
        return os;
    }
Paul's avatar
Paul committed
224
225
};

226
227
228
229
230
231
struct transpose
{
    std::vector<int64_t> dims;
    std::string name() const { return "transpose"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
232
        check_shapes{inputs, *this}.has(1);
233
        auto input         = inputs.at(0);
234
        auto input_lens    = input.lens();
235
236
        auto input_strides = input.strides();
        auto t             = input.type();
Paul's avatar
Paul committed
237
238
        if(dims.size() != input_lens.size())
        {
Paul's avatar
Paul committed
239
            MIGRAPH_THROW("Permutation has wrong number of axes");
240
241
242
        }
        std::vector<int64_t> axes(dims.size());
        std::iota(axes.begin(), axes.end(), 0);
Paul's avatar
Paul committed
243
244
        if(!std::is_permutation(axes.begin(), axes.end(), dims.begin()))
        {
Paul's avatar
Paul committed
245
            MIGRAPH_THROW("Invalid permutation");
246
        }
247
248
        std::vector<size_t> output_lens(input_lens.size());
        std::vector<size_t> output_strides(input_lens.size());
Paul's avatar
Paul committed
249
250
251
        for(int i = 0; i < output_lens.size(); i++)
        {
            output_lens[i]    = input_lens[dims[i]];
252
253
            output_strides[i] = input_strides[dims[i]];
        }
254
        return {t, output_lens, output_strides};
255
    }
Paul's avatar
Paul committed
256
    argument compute(context&, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
257
    {
Paul's avatar
Paul committed
258
        return {std::move(output_shape), std::move(args.front().data)};
Paul's avatar
Paul committed
259
    }
Paul's avatar
Paul committed
260
261
262
263
264
265
266
    friend std::ostream& operator<<(std::ostream& os, const transpose& op)
    {
        os << op.name() << "[";
        os << "dims={" << stream_range(op.dims) << "}";
        os << "]";
        return os;
    }
267
268
};

Paul's avatar
Paul committed
269
struct contiguous
270
271
272
273
{
    std::string name() const { return "contiguous"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
274
        check_shapes{inputs, *this}.has(1);
Paul's avatar
Paul committed
275
276
277
278
        auto lens = inputs.at(0).lens();
        auto t    = inputs.at(0).type();
        if(lens.size() < 2)
        {
Paul's avatar
Paul committed
279
            MIGRAPH_THROW("Number of dimensions should exceed 1");
280
281
282
283
284
        }
        return {t, lens};
    }
};

285
286
287
288
289
290
struct slice
{
    std::vector<int64_t> axes;
    std::vector<int64_t> starts;
    std::vector<int64_t> ends;
    std::string name() const { return "slice"; }
Scott Thornton's avatar
Scott Thornton committed
291
292

    auto fix_index(const std::vector<std::size_t>& lens, std::size_t axis, int64_t index) const
293
    {
Scott Thornton's avatar
Scott Thornton committed
294
        int64_t r = std::min(index, static_cast<int64_t>(lens[axis]));
Scott Thornton's avatar
Scott Thornton committed
295
296
        if(r < 0)
            r += lens[axis];
Scott Thornton's avatar
Scott Thornton committed
297
        return std::size_t(r);
Scott Thornton's avatar
Scott Thornton committed
298
299
300
301
302
303
304
    }

    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
305
        if(!axes.empty())
Scott Thornton's avatar
Scott Thornton committed
306
        {
Scott Thornton's avatar
Scott Thornton committed
307
308
309
310
311
            for(std::size_t i = 0; i < axes.size(); i++)
            {
                auto axis = axes[i];
                offset += fix_index(lens, axis, starts[i]) * strides[axis];
            }
312
        }
Scott Thornton's avatar
Scott Thornton committed
313
314
        else
        {
Scott Thornton's avatar
Scott Thornton committed
315
316
317
318
            for(std::size_t axis = 0; axis < lens.size(); axis++)
            {
                offset += fix_index(lens, axis, starts[axis]) * strides[axis];
            }
319
        }
Scott Thornton's avatar
Scott Thornton committed
320
321
322
323
324
        return offset;
    }

    shape compute_shape(std::vector<shape> inputs) const
    {
Scott Thornton's avatar
Scott Thornton committed
325
326
327
328
        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
329
330
331
332
333
334
335
336
337
338
        // std::vector<int64_t> t_axes(old_lens.size());
        // if(axes.size() == 0)
        // {
        //     std::iota(t_axes.begin(), t_axes.end(), 0);
        // }
        // else
        // {
        //     std::copy(axes.begin(), axes.end(), t_axes.begin());
        // }
        if(starts.size() != axes.size() || axes.size() != ends.size())
Scott Thornton's avatar
Scott Thornton committed
339
        {
340
341
            MIGRAPH_THROW("inconsistent sizes");
        }
Scott Thornton's avatar
Scott Thornton committed
342
343
        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
344
        {
Scott Thornton's avatar
Scott Thornton committed
345
346
347
            auto axis = axes[i];
            new_lens[axis] =
                fix_index(old_lens, axis, ends[i]) - fix_index(old_lens, axis, starts[i]);
348
349
350
351
352
        }
        return shape{t, new_lens, old_strides};
    }
    argument compute(context&, shape output_shape, std::vector<argument> args) const
    {
Scott Thornton's avatar
Scott Thornton committed
353
354
355
        auto input  = args[0];
        auto offset = compute_offset(input.get_shape()) * output_shape.type_size();
        return {std::move(output_shape), [=] { return input.data() + offset; }};
356
357
358
359
360
361
362
363
364
365
    }
};

struct squeeze
{
    std::vector<int64_t> axes;
    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
366
367
368
369
370
371
        auto type        = input_shape.type();
        auto old_lens    = input_shape.lens();
        for(auto axis : axes)
        {
            if(input_shape.lens()[axis] != 1)
            {
372
373
374
375
                MIGRAPH_THROW("squeeze axis dimension should be equal to 1");
            }
        }
        std::vector<std::size_t> new_lens;
Scott Thornton's avatar
Scott Thornton committed
376
        if(axes.empty())
Scott Thornton's avatar
Scott Thornton committed
377
        {
wsttiger's avatar
wsttiger committed
378
379
380
381
            std::copy_if(old_lens.begin(),
                         old_lens.end(),
                         std::back_inserter(new_lens),
                         [](auto len) { return len != 1; });
382
        }
Scott Thornton's avatar
Scott Thornton committed
383
384
385
386
387
388
        else
        {
            for(std::size_t i = 0; i < old_lens.size(); i++)
            {
                if(std::find(axes.begin(), axes.end(), i) == axes.end())
                {
389
390
391
392
393
394
395
396
397
                    new_lens.push_back(old_lens[i]);
                }
            }
        }
        return shape{type, new_lens};
    }
    argument compute(context&, shape output_shape, std::vector<argument> args) const
    {
        return {std::move(output_shape), std::move(args.front().data)};
Scott Thornton's avatar
Scott Thornton committed
398
    }
399
400
401
402
403
404
405
406
};

struct unsqueeze
{
    std::vector<int64_t> axes;
    std::string name() const { return "unsqueeze"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Scott Thornton's avatar
Scott Thornton committed
407
408
409
        auto input_shape     = inputs[0];
        auto type            = input_shape.type();
        auto old_lens        = input_shape.lens();
410
411
412
        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
413
414
415
416
        for(std::size_t i = 0; i < new_size; i++)
        {
            if(std::find(axes.begin(), axes.end(), i) != axes.end())
            {
417
                new_lens[i] = 1;
Scott Thornton's avatar
Scott Thornton committed
418
419
420
            }
            else
            {
421
422
423
424
425
426
427
428
429
430
431
                new_lens[i] = old_lens[p++];
            }
        }
        return shape{type, new_lens};
    }
    argument compute(context&, shape output_shape, std::vector<argument> args) const
    {
        return {std::move(output_shape), std::move(args.front().data)};
    }
};

Paul's avatar
Paul committed
432
433
434
struct reshape
{
    std::vector<int64_t> dims;
Paul's avatar
Paul committed
435
    std::string name() const { return "reshape"; }
Paul's avatar
Paul committed
436
437
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
438
        check_shapes{inputs, *this}.has(1);
Paul's avatar
Paul committed
439
440
        auto&& idims = inputs.front().lens();
        std::vector<std::size_t> rdims(dims.begin(), dims.end());
441
442
443
        auto n_neg_dims = std::count(dims.begin(), dims.end(), -1);
        if(n_neg_dims > 1)
            MIGRAPH_THROW("Dimensions for reshape can only have one -1 dim");
Paul's avatar
Paul committed
444
        for(std::size_t i = 0; i < dims.size(); i++)
Paul's avatar
Paul committed
445
446
447
448
        {
            if(dims[i] == 0)
                rdims[i] = idims[i];
        }
449
450
451
452
453
454
455
456
457
458
459
        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
460
461
462
        if(dims.back() == -1)
        {
            rdims.pop_back();
Paul's avatar
Paul committed
463
            std::copy(idims.begin() + rdims.size(), idims.end(), std::back_inserter(rdims));
Paul's avatar
Paul committed
464
        }
Scott Thornton's avatar
Scott Thornton committed
465
        shape s{inputs.front().type(), rdims};
Paul's avatar
Paul committed
466
        if(s.elements() != inputs.front().elements())
Paul's avatar
Paul committed
467
            MIGRAPH_THROW("Wrong number of elements for reshape");
Scott Thornton's avatar
Scott Thornton committed
468
        return s;
Paul's avatar
Paul committed
469
    }
Paul's avatar
Paul committed
470
    argument compute(context&, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
471
    {
Paul's avatar
Paul committed
472
        return {std::move(output_shape), std::move(args.front().data)};
Paul's avatar
Paul committed
473
    }
Paul's avatar
Paul committed
474
    friend std::ostream& operator<<(std::ostream& os, const reshape& op)
Paul's avatar
Paul committed
475
    {
Paul's avatar
Paul committed
476
        os << op.name() << "[";
Paul's avatar
Paul committed
477
        os << "dims={" << stream_range(op.dims) << "}";
Paul's avatar
Paul committed
478
        os << "]";
Paul's avatar
Paul committed
479
480
        return os;
    }
Paul's avatar
Paul committed
481
482
};

483
484
struct gemm
{
Paul's avatar
Paul committed
485
    float alpha = 1.0;
Paul's avatar
Paul committed
486
    float beta  = 0.0;
487
    std::string name() const { return "gemm"; }
488
489
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
490
        check_shapes{inputs, *this}.has(2).same_type();
491
492
        const shape& a = inputs.at(0);
        const shape& b = inputs.at(1);
Scott Thornton's avatar
Scott Thornton committed
493
        auto t         = a.type();
494

495
        if(a.lens()[1] != b.lens()[0])
Paul's avatar
Paul committed
496
497
            MIGRAPH_THROW("Inner dimensions do not match: {" + to_string_range(a.lens()) + "} x {" +
                          to_string_range(b.lens()) + "}");
Scott Thornton's avatar
Scott Thornton committed
498
        return {t, {a.lens()[0], b.lens()[1]}};
499
    }
500
501

    friend std::ostream& operator<<(std::ostream& os, const gemm& op)
502
503
    {
        os << op.name() << "[";
504
        os << "]";
Scott Thornton's avatar
Scott Thornton committed
505
        return os;
506
507
508
    }
};

509
struct unary
Scott Thornton's avatar
Scott Thornton committed
510
{
511
512
    shape compute_shape(std::vector<shape> inputs) const
    {
513
514
        check_shapes{inputs}.has(1);
        return inputs.at(0);
515
    }
Scott Thornton's avatar
Scott Thornton committed
516
517
};

518
519
struct identity : unary
{
520
    std::string name() const { return "identity"; }
521
522
523
};

struct abs : unary
Scott Thornton's avatar
Scott Thornton committed
524
{
525
    std::string name() const { return "abs"; }
Scott Thornton's avatar
Scott Thornton committed
526
527
};

528
struct exp : unary
Scott Thornton's avatar
Scott Thornton committed
529
{
530
    std::string name() const { return "exp"; }
Scott Thornton's avatar
Scott Thornton committed
531
532
};

533
struct sin : unary
Scott Thornton's avatar
Scott Thornton committed
534
{
535
    std::string name() const { return "sin"; }
Scott Thornton's avatar
Scott Thornton committed
536
537
};

538
struct cos : unary
Scott Thornton's avatar
Scott Thornton committed
539
{
540
    std::string name() const { return "cos"; }
Scott Thornton's avatar
Scott Thornton committed
541
542
};

543
struct tan : unary
Scott Thornton's avatar
Scott Thornton committed
544
{
545
    std::string name() const { return "tan"; }
Scott Thornton's avatar
Scott Thornton committed
546
547
};

548
struct asin : unary
Scott Thornton's avatar
Scott Thornton committed
549
{
550
    std::string name() const { return "asin"; }
Scott Thornton's avatar
Scott Thornton committed
551
552
};

553
struct acos : unary
Scott Thornton's avatar
Scott Thornton committed
554
{
555
    std::string name() const { return "acos"; }
Scott Thornton's avatar
Scott Thornton committed
556
557
};

558
struct atan : unary
Scott Thornton's avatar
Scott Thornton committed
559
{
560
    std::string name() const { return "atan"; }
Scott Thornton's avatar
Scott Thornton committed
561
562
};

563
struct tanh : unary
Scott Thornton's avatar
Scott Thornton committed
564
{
565
    std::string name() const { return "tanh"; }
Scott Thornton's avatar
Scott Thornton committed
566
567
};

568
struct sigmoid : unary
Scott Thornton's avatar
Scott Thornton committed
569
{
570
    std::string name() const { return "sigmoid"; }
Scott Thornton's avatar
Scott Thornton committed
571
572
};

573
struct neg : unary
Scott Thornton's avatar
Scott Thornton committed
574
{
575
    std::string name() const { return "neg"; }
Scott Thornton's avatar
Scott Thornton committed
576
577
};

Paul's avatar
Paul committed
578
579
580
581
582
583
584
585
586
587
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);
    }
};

588
struct flatten
Scott Thornton's avatar
Scott Thornton committed
589
{
Paul's avatar
Paul committed
590
    uint64_t axis = 0;
Scott Thornton's avatar
Scott Thornton committed
591
    std::string name() const { return "flatten"; }
Paul's avatar
Paul committed
592
593
594
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs}.has(1);
Paul's avatar
Paul committed
595
596
        auto&& lens = inputs.front().lens();

Paul's avatar
Paul committed
597
        if(axis > lens.size())
Paul's avatar
Paul committed
598
        {
Paul's avatar
Paul committed
599
            MIGRAPH_THROW("axis for flatten must be less than tensor rank");
Paul's avatar
Paul committed
600
        }
Paul's avatar
Paul committed
601
602
603
604
        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<>{});
605
        return {inputs.at(0).type(), {x, y}};
Paul's avatar
Paul committed
606
607
608
    }
    argument compute(context&, shape output_shape, std::vector<argument> args) const
    {
Paul's avatar
Paul committed
609
        return {std::move(output_shape), std::move(args.front().data)};
Paul's avatar
Paul committed
610
    }
Paul's avatar
Paul committed
611
612
613
614
615
616
617
    friend std::ostream& operator<<(std::ostream& os, const flatten& op)
    {
        os << op.name() << "[";
        os << "axis=" << op.axis;
        os << "]";
        return os;
    }
Scott Thornton's avatar
Scott Thornton committed
618
};
619
620
621
622
623
624
struct broadcast
{
    uint64_t axis = 0;
    std::string name() const { return "broadcast"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
625
626
627
628
        auto t      = inputs.at(0).type();
        auto result = inputs.at(0);
        auto input  = inputs.at(1);

Paul's avatar
Paul committed
629
        std::vector<size_t> bcast_strides(result.lens().size(), 0);
630

Paul's avatar
Paul committed
631
632
        if(std::all_of(
               result.lens().cbegin(), result.lens().cend(), [&](auto x) { return x == 1; }))
633
        {
Scott Thornton's avatar
Scott Thornton committed
634
            if(axis != 0)
Paul's avatar
Paul committed
635
                MIGRAPH_THROW("when broadcasting tensor of size 1, axis should be 0");
Paul's avatar
Paul committed
636
            return {t, result.lens(), std::move(bcast_strides)};
637
638
639
        }
        else
        {
Paul's avatar
Paul committed
640
641
            assert(result.lens().size() - axis >= input.lens().size());
            if(!std::equal(input.lens().begin(), input.lens().end(), result.lens().begin() + axis))
Paul's avatar
Paul committed
642
                MIGRAPH_THROW("when broadcasting success sizes must match");
Paul's avatar
Paul committed
643
            std::copy(input.strides().begin(), input.strides().end(), bcast_strides.begin() + axis);
Paul's avatar
Paul committed
644
            return {t, result.lens(), std::move(bcast_strides)};
645
646
        }
    }
Paul's avatar
Paul committed
647
    argument compute(context&, shape output_shape, std::vector<argument> args) const
Scott Thornton's avatar
Scott Thornton committed
648
    {
Paul's avatar
Paul committed
649
        return {std::move(output_shape), std::move(args.at(1).data)};
Scott Thornton's avatar
Scott Thornton committed
650
    }
Paul's avatar
Paul committed
651
652
653
654
655
656
657
    friend std::ostream& operator<<(std::ostream& os, const broadcast& op)
    {
        os << op.name() << "[";
        os << "axis=" << op.axis;
        os << "]";
        return os;
    }
658
659
};

660
struct binary
Scott Thornton's avatar
Scott Thornton committed
661
{
662
    uint64_t broadcast = 0;
663
664
    shape compute_shape(std::vector<shape> inputs) const
    {
665
666
        check_shapes{inputs}.has(2).same_type().same_dims();
        return inputs.at(0);
667
    }
Scott Thornton's avatar
Scott Thornton committed
668
669
};

670
671
672
673
674
675
struct add : binary
{
    std::string name() const { return "add"; }
};

struct sub : binary
Scott Thornton's avatar
Scott Thornton committed
676
677
678
679
{
    std::string name() const { return "sub"; }
};

680
struct mul : binary
Scott Thornton's avatar
Scott Thornton committed
681
682
683
684
{
    std::string name() const { return "mul"; }
};

685
struct div : binary
Scott Thornton's avatar
Scott Thornton committed
686
687
688
689
{
    std::string name() const { return "div"; }
};

Paul's avatar
Paul committed
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
struct load
{
    shape s;
    std::size_t offset = 0;
    std::string name() const { return "load"; }
    shape compute_shape(const std::vector<shape>& inputs) const
    {
        check_shapes{inputs}.has(1);
        return s;
    }
    argument compute(context&, const shape&, const std::vector<argument>& args) const
    {
        return {s, args[0].data() + offset};
    }
};

Paul's avatar
Paul committed
706
struct outline
Scott Thornton's avatar
Scott Thornton committed
707
{
Paul's avatar
Paul committed
708
709
    shape s;
    std::string name() const { return "outline"; }
Paul's avatar
Paul committed
710
    shape compute_shape(const std::vector<shape>& inputs) const
Paul's avatar
Paul committed
711
    {
Paul's avatar
Paul committed
712
        check_shapes{inputs, *this}.has(0);
Paul's avatar
Paul committed
713
714
        return s;
    }
Paul's avatar
Paul committed
715
716
717
718
    argument compute(context&, const shape&, const std::vector<argument>&) const
    {
        return {s, nullptr};
    }
Scott Thornton's avatar
Scott Thornton committed
719
720
};

Paul's avatar
Paul committed
721
} // namespace migraph
Paul's avatar
Paul committed
722
723

#endif