operators.hpp 16.1 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
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(5);
        return inputs.front();
    }

Paul's avatar
Paul committed
45
    argument compute(context&, const shape&, const std::vector<argument>&) const
46
47
48
49
50
    {
        MIGRAPH_THROW("not computable");
    }
};

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

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

Paul's avatar
Paul committed
118
    argument compute(context&, const shape&, const std::vector<argument>&) const
Paul's avatar
Paul committed
119
120
121
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
122

Paul's avatar
Paul committed
123
    friend std::ostream& operator<<(std::ostream& os, const convolution& op)
Paul's avatar
Paul committed
124
    {
Paul's avatar
Paul committed
125
126
127
128
129
        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
130
131
        return os;
    }
Paul's avatar
Paul committed
132
133
};

Paul's avatar
Paul committed
134
struct pooling
Paul's avatar
Paul committed
135
{
Paul's avatar
Paul committed
136
    std::string mode                   = "average";
Paul's avatar
Paul committed
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> lengths = {{1, 1}};
Paul's avatar
Paul committed
140
    std::string name() const { return "pooling"; }
Scott Thornton's avatar
Scott Thornton committed
141

Paul's avatar
Paul committed
142
143
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
144
        check_shapes{inputs, *this}.has(1).only_dims(4);
Paul's avatar
Paul committed
145

Paul's avatar
Paul committed
146
        const shape& input = inputs.at(0);
Paul's avatar
Paul committed
147
        auto t             = input.type();
Paul's avatar
Paul committed
148

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

Scott Thornton's avatar
Scott Thornton committed
152
153
154
155
156
157
        return {t,
                {
                    input.lens()[0],
                    input.lens()[1],
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
158
                        std::ptrdiff_t(std::floor((input.lens()[2] + 2 * padding[0] - lengths[0]) /
159
                                                  static_cast<float>(stride[0]))) +
Scott Thornton's avatar
Scott Thornton committed
160
161
162
                            1)),
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
163
                        std::ptrdiff_t(std::floor((input.lens()[3] + 2 * padding[1] - lengths[1]) /
164
                                                  static_cast<float>(stride[1]))) +
Scott Thornton's avatar
Scott Thornton committed
165
                            1)),
166
167
168
169
170
171
172
173
174
175
                    // std::size_t(std::max<std::ptrdiff_t>(
                    //         1,
                    //         std::ptrdiff_t((input.lens()[2] + 2 * padding[0] - lengths[0]) /
                    //                                  static_cast<float>(stride[0])) +
                    //         1)),
                    // std::size_t(std::max<std::ptrdiff_t>(
                    //         1,
                    //         std::ptrdiff_t((input.lens()[3] + 2 * padding[1] - lengths[1]) /
                    //                                  static_cast<float>(stride[1])) +
                    //         1)),
Scott Thornton's avatar
Scott Thornton committed
176
                }};
Paul's avatar
Paul committed
177
    }
Paul's avatar
Paul committed
178

Paul's avatar
Paul committed
179
    argument compute(context&, const shape&, const std::vector<argument>&) const
Paul's avatar
Paul committed
180
181
182
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
183

Paul's avatar
Paul committed
184
    friend std::ostream& operator<<(std::ostream& os, const pooling& op)
Paul's avatar
Paul committed
185
    {
Paul's avatar
Paul committed
186
187
188
189
190
        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
191
192
        return os;
    }
Paul's avatar
Paul committed
193
194
};

Paul's avatar
Paul committed
195
struct activation
Paul's avatar
Paul committed
196
197
{
    std::string mode;
Paul's avatar
Paul committed
198
    std::string name() const { return "activation"; }
Paul's avatar
Paul committed
199
200
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
201
        check_shapes{inputs, *this}.has(1);
Paul's avatar
Paul committed
202
203
        return inputs.front();
    }
Paul's avatar
Paul committed
204

Paul's avatar
Paul committed
205
    argument compute(context&, const shape&, const std::vector<argument>&) const
Paul's avatar
Paul committed
206
207
208
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
209
    friend std::ostream& operator<<(std::ostream& os, const activation& op)
Paul's avatar
Paul committed
210
    {
Paul's avatar
Paul committed
211
        os << op.name() << ":" << op.mode;
Paul's avatar
Paul committed
212
213
        return os;
    }
Paul's avatar
Paul committed
214
215
};

216
217
218
219
220
221
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
222
        check_shapes{inputs, *this}.has(1);
223
        auto input         = inputs.at(0);
224
        auto input_lens    = input.lens();
225
226
        auto input_strides = input.strides();
        auto t             = input.type();
Paul's avatar
Paul committed
227
228
        if(dims.size() != input_lens.size())
        {
Paul's avatar
Paul committed
229
            MIGRAPH_THROW("Permutation has wrong number of axes");
230
231
232
        }
        std::vector<int64_t> axes(dims.size());
        std::iota(axes.begin(), axes.end(), 0);
Paul's avatar
Paul committed
233
234
        if(!std::is_permutation(axes.begin(), axes.end(), dims.begin()))
        {
Paul's avatar
Paul committed
235
            MIGRAPH_THROW("Invalid permutation");
236
        }
237
238
        std::vector<size_t> output_lens(input_lens.size());
        std::vector<size_t> output_strides(input_lens.size());
Paul's avatar
Paul committed
239
240
241
        for(int i = 0; i < output_lens.size(); i++)
        {
            output_lens[i]    = input_lens[dims[i]];
242
243
            output_strides[i] = input_strides[dims[i]];
        }
244
        return {t, output_lens, output_strides};
245
    }
Paul's avatar
Paul committed
246
    argument compute(context&, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
247
    {
Paul's avatar
Paul committed
248
        return {std::move(output_shape), std::move(args.front().data)};
Paul's avatar
Paul committed
249
    }
Paul's avatar
Paul committed
250
251
252
253
254
255
    friend std::ostream& operator<<(std::ostream& os, const transpose& op)
    {
        os << op.name() << "[";
        os << "dims={" << stream_range(op.dims) << "}";
        os << "]";
        return os;
Paul's avatar
Paul committed
256
    }
257
258
};

Paul's avatar
Paul committed
259
struct contiguous
260
261
262
263
{
    std::string name() const { return "contiguous"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
264
        check_shapes{inputs, *this}.has(1);
Paul's avatar
Paul committed
265
266
267
268
        auto lens = inputs.at(0).lens();
        auto t    = inputs.at(0).type();
        if(lens.size() < 2)
        {
Paul's avatar
Paul committed
269
            MIGRAPH_THROW("Number of dimensions should exceed 1");
270
271
272
        }
        return {t, lens};
    }
Paul's avatar
Paul committed
273
    argument compute(context&, const shape&, const std::vector<argument>&) const
Paul's avatar
Paul committed
274
275
276
    {
        MIGRAPH_THROW("not computable");
    }
277
278
};

Paul's avatar
Paul committed
279
280
281
struct reshape
{
    std::vector<int64_t> dims;
Paul's avatar
Paul committed
282
    std::string name() const { return "reshape"; }
Paul's avatar
Paul committed
283
284
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
285
        check_shapes{inputs, *this}.has(1);
Paul's avatar
Paul committed
286
287
        auto&& idims = inputs.front().lens();
        std::vector<std::size_t> rdims(dims.begin(), dims.end());
288
289
290
        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
291
        for(std::size_t i = 0; i < dims.size(); i++)
Paul's avatar
Paul committed
292
293
294
295
        {
            if(dims[i] == 0)
                rdims[i] = idims[i];
        }
296
297
298
299
300
301
302
303
304
305
306
        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
307
308
309
        if(dims.back() == -1)
        {
            rdims.pop_back();
Paul's avatar
Paul committed
310
            std::copy(idims.begin() + rdims.size(), idims.end(), std::back_inserter(rdims));
Paul's avatar
Paul committed
311
        }
Scott Thornton's avatar
Scott Thornton committed
312
        shape s{inputs.front().type(), rdims};
Paul's avatar
Paul committed
313
        if(s.elements() != inputs.front().elements())
Paul's avatar
Paul committed
314
            MIGRAPH_THROW("Wrong number of elements for reshape");
Scott Thornton's avatar
Scott Thornton committed
315
        return s;
Paul's avatar
Paul committed
316
317
    }

Paul's avatar
Paul committed
318
    argument compute(context&, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
319
    {
Paul's avatar
Paul committed
320
        return {std::move(output_shape), std::move(args.front().data)};
Paul's avatar
Paul committed
321
    }
Paul's avatar
Paul committed
322

Paul's avatar
Paul committed
323
    friend std::ostream& operator<<(std::ostream& os, const reshape& op)
Paul's avatar
Paul committed
324
    {
Paul's avatar
Paul committed
325
        os << op.name() << "[";
Paul's avatar
Paul committed
326
        os << "dims={" << stream_range(op.dims) << "}";
Paul's avatar
Paul committed
327
        os << "]";
Paul's avatar
Paul committed
328
329
        return os;
    }
Paul's avatar
Paul committed
330
331
};

332
333
struct gemm
{
Paul's avatar
Paul committed
334
    float alpha = 1.0;
Paul's avatar
Paul committed
335
    float beta  = 0.0;
336
    std::string name() const { return "gemm"; }
337
338
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
339
        check_shapes{inputs, *this}.has(2).same_type();
340
341
        const shape& a = inputs.at(0);
        const shape& b = inputs.at(1);
Scott Thornton's avatar
Scott Thornton committed
342
        auto t         = a.type();
343

344
        if(a.lens()[1] != b.lens()[0])
Paul's avatar
Paul committed
345
346
            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
347
        return {t, {a.lens()[0], b.lens()[1]}};
348
    }
349

Paul's avatar
Paul committed
350
    argument compute(context&, const shape&, const std::vector<argument>&) const
Paul's avatar
Paul committed
351
352
353
    {
        MIGRAPH_THROW("not computable");
    }
354
355

    friend std::ostream& operator<<(std::ostream& os, const gemm& op)
356
357
    {
        os << op.name() << "[";
358
        os << "]";
Scott Thornton's avatar
Scott Thornton committed
359
        return os;
360
361
362
    }
};

363
struct unary
Scott Thornton's avatar
Scott Thornton committed
364
{
365
366
    shape compute_shape(std::vector<shape> inputs) const
    {
367
368
        check_shapes{inputs}.has(1);
        return inputs.at(0);
369
    }
Paul's avatar
Paul committed
370
    argument compute(context&, const shape&, const std::vector<argument>&) const
Paul's avatar
Paul committed
371
372
373
    {
        MIGRAPH_THROW("not computable");
    }
Scott Thornton's avatar
Scott Thornton committed
374
375
};

376
377
struct identity : unary
{
378
    std::string name() const { return "identity"; }
379
380
381
};

struct abs : unary
Scott Thornton's avatar
Scott Thornton committed
382
{
383
    std::string name() const { return "abs"; }
Scott Thornton's avatar
Scott Thornton committed
384
385
};

386
struct exp : unary
Scott Thornton's avatar
Scott Thornton committed
387
{
388
    std::string name() const { return "exp"; }
Scott Thornton's avatar
Scott Thornton committed
389
390
};

391
struct sin : unary
Scott Thornton's avatar
Scott Thornton committed
392
{
393
    std::string name() const { return "sin"; }
Scott Thornton's avatar
Scott Thornton committed
394
395
};

396
struct cos : unary
Scott Thornton's avatar
Scott Thornton committed
397
{
398
    std::string name() const { return "cos"; }
Scott Thornton's avatar
Scott Thornton committed
399
400
};

401
struct tan : unary
Scott Thornton's avatar
Scott Thornton committed
402
{
403
    std::string name() const { return "tan"; }
Scott Thornton's avatar
Scott Thornton committed
404
405
};

406
struct asin : unary
Scott Thornton's avatar
Scott Thornton committed
407
{
408
    std::string name() const { return "asin"; }
Scott Thornton's avatar
Scott Thornton committed
409
410
};

411
struct acos : unary
Scott Thornton's avatar
Scott Thornton committed
412
{
413
    std::string name() const { return "acos"; }
Scott Thornton's avatar
Scott Thornton committed
414
415
};

416
struct atan : unary
Scott Thornton's avatar
Scott Thornton committed
417
{
418
    std::string name() const { return "atan"; }
Scott Thornton's avatar
Scott Thornton committed
419
420
};

421
struct softmax : unary
Scott Thornton's avatar
Scott Thornton committed
422
{
423
    std::string name() const { return "softmax"; }
Scott Thornton's avatar
Scott Thornton committed
424
425
};

426
struct tanh : unary
Scott Thornton's avatar
Scott Thornton committed
427
{
428
    std::string name() const { return "tanh"; }
Scott Thornton's avatar
Scott Thornton committed
429
430
};

431
struct sigmoid : unary
Scott Thornton's avatar
Scott Thornton committed
432
{
433
    std::string name() const { return "sigmoid"; }
Scott Thornton's avatar
Scott Thornton committed
434
435
};

436
struct neg : unary
Scott Thornton's avatar
Scott Thornton committed
437
{
438
    std::string name() const { return "neg"; }
Scott Thornton's avatar
Scott Thornton committed
439
440
};

441
struct flatten
Scott Thornton's avatar
Scott Thornton committed
442
{
Scott Thornton's avatar
Scott Thornton committed
443
    uint64_t axis = 0;
Scott Thornton's avatar
Scott Thornton committed
444
    std::string name() const { return "flatten"; }
Scott Thornton's avatar
Scott Thornton committed
445
446
447
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs}.has(1);
Paul's avatar
Paul committed
448
449
        auto&& lens = inputs.front().lens();

Paul's avatar
Paul committed
450
        if(axis > lens.size())
Scott Thornton's avatar
Scott Thornton committed
451
        {
Paul's avatar
Paul committed
452
            MIGRAPH_THROW("axis for flatten must be less than tensor rank");
Scott Thornton's avatar
Scott Thornton committed
453
        }
Paul's avatar
Paul committed
454
455
456
457
        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<>{});
458
        return {inputs.at(0).type(), {x, y}};
Scott Thornton's avatar
Scott Thornton committed
459
460
461
    }
    argument compute(context&, shape output_shape, std::vector<argument> args) const
    {
Paul's avatar
Paul committed
462
        return {std::move(output_shape), std::move(args.front().data)};
Paul's avatar
Paul committed
463
    }
Paul's avatar
Paul committed
464
465
466
467
468
469
    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
470
    }
Scott Thornton's avatar
Scott Thornton committed
471
};
472
473
474
475
476
477
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
478
479
480
481
        auto t      = inputs.at(0).type();
        auto result = inputs.at(0);
        auto input  = inputs.at(1);

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

Paul's avatar
Paul committed
484
485
        if(std::all_of(
               result.lens().cbegin(), result.lens().cend(), [&](auto x) { return x == 1; }))
486
        {
Scott Thornton's avatar
Scott Thornton committed
487
            if(axis != 0)
Paul's avatar
Paul committed
488
                MIGRAPH_THROW("when broadcasting tensor of size 1, axis should be 0");
Paul's avatar
Paul committed
489
            return {t, result.lens(), std::move(bcast_strides)};
490
491
492
        }
        else
        {
Paul's avatar
Paul committed
493
494
            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
495
                MIGRAPH_THROW("when broadcasting success sizes must match");
Paul's avatar
Paul committed
496
            std::copy(input.strides().begin(), input.strides().end(), bcast_strides.begin() + axis);
Paul's avatar
Paul committed
497
            return {t, result.lens(), std::move(bcast_strides)};
498
499
        }
    }
Paul's avatar
Paul committed
500
    argument compute(context&, shape output_shape, std::vector<argument> args) const
Scott Thornton's avatar
Scott Thornton committed
501
    {
Paul's avatar
Paul committed
502
        return {std::move(output_shape), std::move(args.at(1).data)};
Scott Thornton's avatar
Scott Thornton committed
503
    }
Paul's avatar
Paul committed
504
505
506
507
508
509
    friend std::ostream& operator<<(std::ostream& os, const broadcast& op)
    {
        os << op.name() << "[";
        os << "axis=" << op.axis;
        os << "]";
        return os;
Scott Thornton's avatar
Scott Thornton committed
510
    }
511
512
};

513
struct binary
Scott Thornton's avatar
Scott Thornton committed
514
{
515
    uint64_t broadcast = 0;
516
517
    shape compute_shape(std::vector<shape> inputs) const
    {
518
519
        check_shapes{inputs}.has(2).same_type().same_dims();
        return inputs.at(0);
520
    }
Paul's avatar
Paul committed
521
    argument compute(context&, const shape&, const std::vector<argument>&) const
Paul's avatar
Paul committed
522
523
524
    {
        MIGRAPH_THROW("not computable");
    }
Scott Thornton's avatar
Scott Thornton committed
525
526
};

527
528
529
530
531
532
struct add : binary
{
    std::string name() const { return "add"; }
};

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

537
struct mul : binary
Scott Thornton's avatar
Scott Thornton committed
538
539
540
541
{
    std::string name() const { return "mul"; }
};

542
struct div : binary
Scott Thornton's avatar
Scott Thornton committed
543
544
545
546
{
    std::string name() const { return "div"; }
};

Paul's avatar
Paul committed
547
struct outline
Scott Thornton's avatar
Scott Thornton committed
548
{
Paul's avatar
Paul committed
549
550
    shape s;
    std::string name() const { return "outline"; }
Paul's avatar
Paul committed
551
    shape compute_shape(const std::vector<shape>& inputs) const
Paul's avatar
Paul committed
552
    {
Paul's avatar
Paul committed
553
        check_shapes{inputs, *this}.has(0);
Paul's avatar
Paul committed
554
555
        return s;
    }
Paul's avatar
Paul committed
556
557
558
559
    argument compute(context&, const shape&, const std::vector<argument>&) const
    {
        return {s, nullptr};
    }
Scott Thornton's avatar
Scott Thornton committed
560
561
};

Paul's avatar
Paul committed
562
} // namespace migraph
Paul's avatar
Paul committed
563
564

#endif