"deploy/lite/include/keypoint_postprocess.h" did not exist on "dcc7bf4f1a243d90d6c4f7c51551cea3f256325f"
operators.hpp 16.3 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
6
7
#include <migraph/operation.hpp>
#include <migraph/stringutils.hpp>
#include <migraph/streamutils.hpp>
Paul's avatar
Paul committed
8
#include <cmath>
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
namespace migraph {
Paul's avatar
Paul committed
11

Paul's avatar
Paul committed
12
13
14
struct check_shapes
{
    const std::vector<shape>* shapes;
Paul's avatar
Paul committed
15
    const std::string name;
Paul's avatar
Paul committed
16

Paul's avatar
Paul committed
17
    check_shapes(const std::vector<shape>& s) : shapes(&s) {}
Paul's avatar
Paul committed
18

Paul's avatar
Paul committed
19
20
21
22
    template <class Op>
    check_shapes(const std::vector<shape>& s, const Op& op) : shapes(&s), name(op.name())
    {
    }
Paul's avatar
Paul committed
23
24
25

    std::string prefix() const
    {
Paul's avatar
Paul committed
26
27
28
29
        if(name.empty())
            return "";
        else
            return name + ": ";
Paul's avatar
Paul committed
30
    }
Paul's avatar
Paul committed
31
32
33
34
35

    const check_shapes& has(std::size_t n) const
    {
        assert(shapes != nullptr);
        if(shapes->size() != n)
Paul's avatar
Paul committed
36
            MIGRAPH_THROW(prefix() + "Wrong number of arguments: expected " + std::to_string(n) +
Paul's avatar
Paul committed
37
                          " but given " + std::to_string(shapes->size()));
Paul's avatar
Paul committed
38
39
40
41
42
43
        return *this;
    }

    const check_shapes& only_dims(std::size_t n) const
    {
        assert(shapes != nullptr);
Paul's avatar
Paul committed
44
45
        if(!shapes->empty())
        {
Paul's avatar
Paul committed
46
            if(shapes->front().lens().size() != n)
Paul's avatar
Paul committed
47
                MIGRAPH_THROW(prefix() + "Only " + std::to_string(n) + "d supported");
Paul's avatar
Paul committed
48
49
50
51
52
53
54
        }
        return *this;
    }

    const check_shapes& same_shape() const
    {
        if(!this->same([](const shape& s) { return s; }))
Paul's avatar
Paul committed
55
            MIGRAPH_THROW(prefix() + "Shapes do not match");
Paul's avatar
Paul committed
56
57
58
59
60
61
        return *this;
    }

    const check_shapes& same_type() const
    {
        if(!this->same([](const shape& s) { return s.type(); }))
Paul's avatar
Paul committed
62
            MIGRAPH_THROW(prefix() + "Types do not match");
Paul's avatar
Paul committed
63
64
65
66
67
68
        return *this;
    }

    const check_shapes& same_dims() const
    {
        if(!this->same([](const shape& s) { return s.lens(); }))
Paul's avatar
Paul committed
69
            MIGRAPH_THROW(prefix() + "Dimensions do not match");
Paul's avatar
Paul committed
70
71
72
        return *this;
    }

73
74
75
    const check_shapes& same_ndims() const
    {
        if(!this->same([](const shape& s) { return s.lens().size(); }))
Paul's avatar
Paul committed
76
            MIGRAPH_THROW(prefix() + "Dimensions do not match");
77
78
79
        return *this;
    }

Paul's avatar
Paul committed
80
    template <class F>
Paul's avatar
Paul committed
81
82
83
84
85
86
    bool same(F f) const
    {
        assert(shapes != nullptr);
        if(shapes->empty())
            return true;
        auto&& key = f(shapes->front());
Paul's avatar
Paul committed
87
        return this->all_of([&](const shape& s) { return f(s) == key; });
Paul's avatar
Paul committed
88
89
    }

Paul's avatar
Paul committed
90
    template <class Predicate>
Paul's avatar
Paul committed
91
92
93
94
95
96
97
    bool all_of(Predicate p) const
    {
        assert(shapes != nullptr);
        return std::all_of(shapes->begin(), shapes->end(), p);
    }
};

Paul's avatar
Paul committed
98
99
struct not_computable
{
Paul's avatar
Paul committed
100
101
102
103
    argument compute(context&, shape, std::vector<argument>) const
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
104
105
};

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
struct batch_norm_inference
{
    double epsilon = 1.0e-6;

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

    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(5);
        return inputs.front();
    }

    argument compute(context&, shape, std::vector<argument>) const
    {
        MIGRAPH_THROW("not computable");
    }
};

Paul's avatar
Paul committed
124
struct convolution
Paul's avatar
Paul committed
125
{
Paul's avatar
Paul committed
126
127
128
    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
129
130
131
132
133
134
135
    enum padding_mode_t
    {
        default_, // NOLINT
        same,
        valid
    };
    padding_mode_t padding_mode = default_;
Paul's avatar
Paul committed
136
    std::string name() const { return "convolution"; }
Paul's avatar
Paul committed
137
138
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
139
        check_shapes{inputs, *this}.has(2).same_type().same_ndims().only_dims(4);
Paul's avatar
Paul committed
140

Paul's avatar
Paul committed
141
        const shape& input   = inputs.at(0);
Paul's avatar
Paul committed
142
        const shape& weights = inputs.at(1);
Paul's avatar
Paul committed
143
        auto t               = input.type();
Paul's avatar
Paul committed
144
145
        if(padding_mode == default_)
        {
Paul's avatar
Paul committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
            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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
        }
        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
187
            MIGRAPH_THROW("Invalid padding mode");
Paul's avatar
Paul committed
188
        }
Paul's avatar
Paul committed
189
    }
Paul's avatar
Paul committed
190

Paul's avatar
Paul committed
191
192
193
194
    argument compute(context&, shape, std::vector<argument>) const
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
195

Paul's avatar
Paul committed
196
    friend std::ostream& operator<<(std::ostream& os, const convolution& op)
Paul's avatar
Paul committed
197
    {
Paul's avatar
Paul committed
198
199
200
201
202
        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
203
204
        return os;
    }
Paul's avatar
Paul committed
205
206
};

Paul's avatar
Paul committed
207
struct pooling
Paul's avatar
Paul committed
208
209
{
    std::string mode;
Paul's avatar
Paul committed
210
211
212
    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
213
    std::string name() const { return "pooling"; }
Scott Thornton's avatar
Scott Thornton committed
214

Paul's avatar
Paul committed
215
216
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
217
        check_shapes{inputs, *this}.has(1).only_dims(4);
Paul's avatar
Paul committed
218

Paul's avatar
Paul committed
219
        const shape& input = inputs.at(0);
Paul's avatar
Paul committed
220
        auto t             = input.type();
Paul's avatar
Paul committed
221

Paul's avatar
Paul committed
222
223
        assert(lengths[0] < (input.lens()[2] + 2 * padding[0]));
        assert(lengths[1] < (input.lens()[3] + 2 * padding[1]));
Paul's avatar
Paul committed
224

Scott Thornton's avatar
Scott Thornton committed
225
226
227
228
229
230
        return {t,
                {
                    input.lens()[0],
                    input.lens()[1],
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
Paul's avatar
Paul committed
231
                        std::ptrdiff_t(std::ceil((input.lens()[2] + 2 * padding[0] - lengths[0]) /
Paul's avatar
Paul committed
232
                                                 static_cast<float>(stride[0]))) +
Scott Thornton's avatar
Scott Thornton committed
233
234
235
                            1)),
                    std::size_t(std::max<std::ptrdiff_t>(
                        1,
Paul's avatar
Paul committed
236
                        std::ptrdiff_t(std::ceil((input.lens()[3] + 2 * padding[1] - lengths[1]) /
Paul's avatar
Paul committed
237
                                                 static_cast<float>(stride[1]))) +
Scott Thornton's avatar
Scott Thornton committed
238
239
                            1)),
                }};
Paul's avatar
Paul committed
240
    }
Paul's avatar
Paul committed
241

Paul's avatar
Paul committed
242
243
244
245
    argument compute(context&, shape, std::vector<argument>) const
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
246

Paul's avatar
Paul committed
247
    friend std::ostream& operator<<(std::ostream& os, const pooling& op)
Paul's avatar
Paul committed
248
    {
Paul's avatar
Paul committed
249
250
251
252
253
        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
254
255
        return os;
    }
Paul's avatar
Paul committed
256
257
};

Paul's avatar
Paul committed
258
struct activation
Paul's avatar
Paul committed
259
260
{
    std::string mode;
Paul's avatar
Paul committed
261
    std::string name() const { return "activation"; }
Paul's avatar
Paul committed
262
263
    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
        return inputs.front();
    }
Paul's avatar
Paul committed
267

Paul's avatar
Paul committed
268
269
270
271
    argument compute(context&, shape, std::vector<argument>) const
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
272
    friend std::ostream& operator<<(std::ostream& os, const activation& op)
Paul's avatar
Paul committed
273
    {
Paul's avatar
Paul committed
274
        os << op.name() << ":" << op.mode;
Paul's avatar
Paul committed
275
276
        return os;
    }
Paul's avatar
Paul committed
277
278
};

279
280
281
282
283
284
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
285
        check_shapes{inputs, *this}.has(1);
286
        auto input         = inputs.at(0);
287
        auto input_lens    = input.lens();
288
289
        auto input_strides = input.strides();
        auto t             = input.type();
Paul's avatar
Paul committed
290
291
        if(dims.size() != input_lens.size())
        {
Paul's avatar
Paul committed
292
            MIGRAPH_THROW("Permutation has wrong number of axes");
293
294
295
        }
        std::vector<int64_t> axes(dims.size());
        std::iota(axes.begin(), axes.end(), 0);
Paul's avatar
Paul committed
296
297
        if(!std::is_permutation(axes.begin(), axes.end(), dims.begin()))
        {
Paul's avatar
Paul committed
298
            MIGRAPH_THROW("Invalid permutation");
299
        }
300
301
        std::vector<size_t> output_lens(input_lens.size());
        std::vector<size_t> output_strides(input_lens.size());
Paul's avatar
Paul committed
302
303
304
        for(int i = 0; i < output_lens.size(); i++)
        {
            output_lens[i]    = input_lens[dims[i]];
305
306
            output_strides[i] = input_strides[dims[i]];
        }
307
        return {t, output_lens, output_strides};
308
    }
Paul's avatar
Paul committed
309
310
311
312
    argument compute(context&, shape, std::vector<argument>) const
    {
        MIGRAPH_THROW("not computable");
    }
313
314
};

Paul's avatar
Paul committed
315
struct contiguous
316
317
318
319
{
    std::string name() const { return "contiguous"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
320
        check_shapes{inputs, *this}.has(1);
Paul's avatar
Paul committed
321
322
323
324
        auto lens = inputs.at(0).lens();
        auto t    = inputs.at(0).type();
        if(lens.size() < 2)
        {
Paul's avatar
Paul committed
325
            MIGRAPH_THROW("Number of dimensions should exceed 1");
326
327
328
        }
        return {t, lens};
    }
Paul's avatar
Paul committed
329
330
331
332
    argument compute(context&, shape, std::vector<argument>) const
    {
        MIGRAPH_THROW("not computable");
    }
333
334
};

Paul's avatar
Paul committed
335
336
337
struct reshape
{
    std::vector<int64_t> dims;
Paul's avatar
Paul committed
338
    std::string name() const { return "reshape"; }
Paul's avatar
Paul committed
339
340
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
341
        check_shapes{inputs, *this}.has(1);
Paul's avatar
Paul committed
342
343
        auto&& idims = inputs.front().lens();
        std::vector<std::size_t> rdims(dims.begin(), dims.end());
Paul's avatar
Paul committed
344
        for(std::size_t i = 0; i < dims.size(); i++)
Paul's avatar
Paul committed
345
346
347
348
349
350
351
        {
            if(dims[i] == 0)
                rdims[i] = idims[i];
        }
        if(dims.back() == -1)
        {
            rdims.pop_back();
Paul's avatar
Paul committed
352
            std::copy(idims.begin() + rdims.size(), idims.end(), std::back_inserter(rdims));
Paul's avatar
Paul committed
353
        }
Scott Thornton's avatar
Scott Thornton committed
354
        shape s{inputs.front().type(), rdims};
Paul's avatar
Paul committed
355
        if(s.elements() != inputs.front().elements())
Paul's avatar
Paul committed
356
            MIGRAPH_THROW("Wrong number of elements for reshape");
Scott Thornton's avatar
Scott Thornton committed
357
        return s;
Paul's avatar
Paul committed
358
359
    }

Paul's avatar
Paul committed
360
361
362
363
    argument compute(context&, shape, std::vector<argument>) const
    {
        MIGRAPH_THROW("not computable");
    }
Paul's avatar
Paul committed
364

Paul's avatar
Paul committed
365
    friend std::ostream& operator<<(std::ostream& os, const reshape& op)
Paul's avatar
Paul committed
366
    {
Paul's avatar
Paul committed
367
368
369
        os << op.name() << "[";
        os << "dims={" << stream_range(op.dims) << "}, ";
        os << "]";
Paul's avatar
Paul committed
370
371
        return os;
    }
Paul's avatar
Paul committed
372
373
};

374
375
struct gemm
{
376
    std::string name() const { return "gemm"; }
377
378
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
379
        check_shapes{inputs, *this}.has(2).same_type();
380
381
        const shape& a = inputs.at(0);
        const shape& b = inputs.at(1);
Scott Thornton's avatar
Scott Thornton committed
382
        auto t         = a.type();
383

384
        if(a.lens()[1] != b.lens()[0])
Paul's avatar
Paul committed
385
            MIGRAPH_THROW("Inner dimensions do not match");
Scott Thornton's avatar
Scott Thornton committed
386
        return {t, {a.lens()[0], b.lens()[1]}};
387
    }
388

Paul's avatar
Paul committed
389
390
391
392
    argument compute(context&, shape, std::vector<argument>) const
    {
        MIGRAPH_THROW("not computable");
    }
393
394

    friend std::ostream& operator<<(std::ostream& os, const gemm& op)
395
396
    {
        os << op.name() << "[";
397
        os << "]";
Scott Thornton's avatar
Scott Thornton committed
398
        return os;
399
400
401
    }
};

402
struct unary
Scott Thornton's avatar
Scott Thornton committed
403
{
404
405
    shape compute_shape(std::vector<shape> inputs) const
    {
406
407
        check_shapes{inputs}.has(1);
        return inputs.at(0);
408
    }
Paul's avatar
Paul committed
409
410
411
412
    argument compute(context&, shape, std::vector<argument>) const
    {
        MIGRAPH_THROW("not computable");
    }
Scott Thornton's avatar
Scott Thornton committed
413
414
};

415
416
struct identity : unary
{
417
    std::string name() const { return "identity"; }
418
419
420
};

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

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

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

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

440
struct tan : unary
Scott Thornton's avatar
Scott Thornton committed
441
{
442
    std::string name() const { return "tan"; }
Scott Thornton's avatar
Scott Thornton committed
443
444
};

445
struct asin : unary
Scott Thornton's avatar
Scott Thornton committed
446
{
447
    std::string name() const { return "asin"; }
Scott Thornton's avatar
Scott Thornton committed
448
449
};

450
struct acos : unary
Scott Thornton's avatar
Scott Thornton committed
451
{
452
    std::string name() const { return "acos"; }
Scott Thornton's avatar
Scott Thornton committed
453
454
};

455
struct atan : unary
Scott Thornton's avatar
Scott Thornton committed
456
{
457
    std::string name() const { return "atan"; }
Scott Thornton's avatar
Scott Thornton committed
458
459
};

460
struct softmax : unary
Scott Thornton's avatar
Scott Thornton committed
461
{
462
    std::string name() const { return "softmax"; }
Scott Thornton's avatar
Scott Thornton committed
463
464
};

465
struct tanh : unary
Scott Thornton's avatar
Scott Thornton committed
466
{
467
    std::string name() const { return "tanh"; }
Scott Thornton's avatar
Scott Thornton committed
468
469
};

470
struct sigmoid : unary
Scott Thornton's avatar
Scott Thornton committed
471
{
472
    std::string name() const { return "sigmoid"; }
Scott Thornton's avatar
Scott Thornton committed
473
474
};

475
struct neg : unary
Scott Thornton's avatar
Scott Thornton committed
476
{
477
    std::string name() const { return "neg"; }
Scott Thornton's avatar
Scott Thornton committed
478
479
};

480
struct flatten
Scott Thornton's avatar
Scott Thornton committed
481
482
483
484
{
    std::string name() const { return "flatten"; }
};

485
486
487
488
489
490
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
491
492
493
494
        auto t      = inputs.at(0).type();
        auto result = inputs.at(0);
        auto input  = inputs.at(1);

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

Paul's avatar
Paul committed
497
498
        if(std::all_of(
               result.lens().cbegin(), result.lens().cend(), [&](auto x) { return x == 1; }))
499
        {
Scott Thornton's avatar
Scott Thornton committed
500
            if(axis != 0)
Paul's avatar
Paul committed
501
                MIGRAPH_THROW("when broadcasting tensor of size 1, axis should be 0");
Paul's avatar
Paul committed
502
            return {t, result.lens(), std::move(bcast_strides)};
503
504
505
        }
        else
        {
Paul's avatar
Paul committed
506
507
            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
508
                MIGRAPH_THROW("when broadcasting success sizes must match");
Paul's avatar
Paul committed
509
            std::copy(input.strides().begin(), input.strides().end(), bcast_strides.begin() + axis);
Paul's avatar
Paul committed
510
            return {t, result.lens(), std::move(bcast_strides)};
511
512
        }
    }
Paul's avatar
Paul committed
513
    argument compute(context&, shape output_shape, std::vector<argument> args) const
Scott Thornton's avatar
Scott Thornton committed
514
    {
515
        return {output_shape, std::move(args.at(1).data)};
Scott Thornton's avatar
Scott Thornton committed
516
    }
517
518
};

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

533
534
535
536
537
538
struct add : binary
{
    std::string name() const { return "add"; }
};

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

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

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

mei-ye's avatar
mei-ye committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
struct get_mem_ptr 
{
    std::string name() const { return "get_mem_ptr:" + std::to_string(offset); }
    shape compute_shape(std::vector<shape> inputs) const
    {
        return inputs.at(1);
    }
    argument compute(context&, shape output_shape, std::vector<argument> args) const {
        return {output_shape, args.at(0).data() + offset};
    }
    std::size_t offset = 0;
};

struct write_literal
{
    std::string name() const { return "write_literal"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
        return inputs.at(2);
    }
    argument compute(context&, shape output_shape, std::vector<argument> args) const {
        assert(false);
    }
};        

Paul's avatar
Paul committed
578
struct outline
Scott Thornton's avatar
Scott Thornton committed
579
{
Paul's avatar
Paul committed
580
581
582
583
    shape s;
    std::string name() const { return "outline"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
584
        check_shapes{inputs, *this}.has(0);
Paul's avatar
Paul committed
585
586
        return s;
    }
Paul's avatar
Paul committed
587
    argument compute(context&, shape, std::vector<argument>) const { return {s, nullptr}; }
Scott Thornton's avatar
Scott Thornton committed
588
589
};

Paul's avatar
Paul committed
590
template <class T>
Paul's avatar
Paul committed
591
592
593
struct check_context
{
    std::string name() const { return "check_context"; }
Paul's avatar
Paul committed
594
595
    shape compute_shape(std::vector<shape>) const { return {}; }
    argument compute(context& ctx, shape, std::vector<argument>) const
Paul's avatar
Paul committed
596
597
598
    {
        T* x = any_cast<T>(&ctx);
        if(x == nullptr)
Paul's avatar
Paul committed
599
            MIGRAPH_THROW(std::string("Unexpected context type: ") + ctx.type_id().name());
Paul's avatar
Paul committed
600
601
        return {};
    }
Scott Thornton's avatar
Scott Thornton committed
602
603
};

Paul's avatar
Paul committed
604
} // namespace migraph
Paul's avatar
Paul committed
605
606

#endif