operation.hpp 23.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_OPERAND_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_OPERAND_HPP
Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
#include <cassert>
Paul's avatar
Paul committed
28
29
30
31
32
#include <string>
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
Shucai Xiao's avatar
Shucai Xiao committed
33
#include <unordered_map>
Paul's avatar
Paul committed
34
#include <migraphx/reflect.hpp>
charlie's avatar
charlie committed
35
#include <migraphx/functional.hpp>
Paul's avatar
Paul committed
36
#include <migraphx/streamutils.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
37
#include <migraphx/normalize_attributes.hpp>
Paul's avatar
Paul committed
38
#include <migraphx/argument.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
39
#include <migraphx/module_ref.hpp>
40
#include <migraphx/serialize.hpp>
Paul's avatar
Paul committed
41
#include <migraphx/auto_any_cast.hpp>
42
#include <migraphx/lifetime.hpp>
Paul's avatar
Paul committed
43
#include <migraphx/config.hpp>
Paul's avatar
Paul committed
44

Paul's avatar
Paul committed
45
namespace migraphx {
Paul's avatar
Paul committed
46
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
47

Paul's avatar
Paul committed
48
49
struct context;

charlie's avatar
charlie committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
struct dyn_output
{
    F ins_inputs;

    dyn_output(F f) : ins_inputs(f){};

    shape get_input_shape()
    {
        if(ins_shape.empty())
        {
            ins_shape = unpack(
                [&](const auto&, shape s, const std::vector<argument>&) { return s; }, ins_inputs);
        }
        return ins_shape;
    }

    shape get_output_shape()
    {
        if(computed_shape.empty())
        {
            computed_shape = unpack(
                [&](const auto& x, shape, const std::vector<argument>& inputs) {
                    return compute_shape(x, to_shapes(inputs));
                },
                ins_inputs);
        }
        return computed_shape;
    }

    private:
    // original shape from the instruction
    shape ins_shape;
    // shape computed at eval time using input arguments
    shape computed_shape;
};

/**
 * Handle dynamic and static shape at evaluation time.
 * If converted to shape type, returns original ins_shape.
 * If converted to dyn_output type, will compute an output shape using the input arguments.
 */
template <class F>
struct compute_output_shape
{
    F ins_inputs;

    operator dyn_output<F>() const
    {
        /*
         return unpack([](const auto& x, shape ins_shape, const std::vector<argument>& inputs)
            {
                return dyn_output{ins_shape, compute_shape(x, to_shapes(inputs))};
            },
            ins_inputs
        );
        */
        return dyn_output<F>{ins_inputs};
    }

    operator shape() const
    {
        return unpack(
            [](const auto&, shape ins_shape, const std::vector<argument>&) { return ins_shape; },
            ins_inputs);
    }
};

template <class T>
auto make_compute_output_shape(const T& x, shape output_shape, const std::vector<argument>& inputs)
    -> decltype(compute_output_shape{pack(x, output_shape, inputs)})
{
    return compute_output_shape{pack(x, output_shape, inputs)};
}

Paul's avatar
Paul committed
124
125
126
127
128
129
130
131
#ifdef DOXYGEN

/// The operation interface represents an action an instruction will perform. All
/// operation classes must be CopyConstructible.
struct operation
{
    /// A unique name identifying the operation
    std::string name() const;
Paul's avatar
Paul committed
132
133
    /// An optional method that can be used to finalize the operator before running
    void finalize(context& ctx);
Paul's avatar
Paul committed
134
135
136
    /// This is used to compute the resulting shape from an operation. If an
    /// operation cannot be run with input shapes, then it should throw an
    /// exception.
Paul's avatar
Paul committed
137
    shape compute_shape(const std::vector<shape>& input) const;
Paul's avatar
Paul committed
138
    /**
Paul's avatar
Paul committed
139
140
     * @brief This performs the operation's computation.
     *
Paul's avatar
Paul committed
141
142
     * This method can be optional when the operation is only used as a placeholder to be lowered
     * later on.
Paul's avatar
Paul committed
143
144
145
     *
     * @param ctx This is the context created by the `target` during compilation. Implementations
     * can use the target's `context` class rather than the `context` interface class.
146
147
148
149
     * @param output Equivalent to running `compute_shape` with each `shape` of the `argument`.
     * For a fixed shape, the returned argument will have the same shape as `output`.
     * For a dynamic shape, the returned `argument` will be a fixed shape within the bounds
     * set in the dynamic shape `output`.
Paul's avatar
Paul committed
150
     * @param input This is the `argument` result from the previous instruction's computation.
Paul's avatar
Paul committed
151
152
     * @return Return an `argument` of the result computation. The `shape` of `argument` should be
     * the same the `output` shape.
Paul's avatar
Paul committed
153
     */
Paul's avatar
Paul committed
154
    argument compute(context& ctx, const shape& output, const std::vector<argument>& input) const;
Paul's avatar
Paul committed
155
156
    /// An optional method to return which argument the output will alias. If
    /// there is no aliased output then -1 can be returned.
Paul's avatar
Paul committed
157
    std::ptrdiff_t output_alias(const std::vector<shape>& input) const;
Paul's avatar
Paul committed
158
159
    /// An optional stream operator to print the operation. When this is not
    /// implemented, it will just print the operation's name.
Paul's avatar
Paul committed
160
    friend std::ostream& operator<<(std::ostream& os, const operation& op);
Paul's avatar
Paul committed
161
162
};

Paul's avatar
Paul committed
163
164
/// Returns true if operation does not require a context to run compute
bool is_context_free(const operation& x);
Shucai Xiao's avatar
Shucai Xiao committed
165
166
/// Returns true if operation needs normalization before running compute
bool need_normalization(const operation& x);
Paul's avatar
Paul committed
167
168
/// Returns true if the operation has a finalize method
bool has_finalize(const operation& x);
Paul's avatar
Paul committed
169

Paul's avatar
Paul committed
170
171
#else

172
173
174
namespace detail {

namespace operation_operators {
Paul's avatar
Paul committed
175

Paul's avatar
Paul committed
176
177
template <class T>
auto operator<<(std::ostream& os, const T& x) -> decltype(os << x.name())
Paul's avatar
Paul committed
178
{
Paul's avatar
Paul committed
179
180
    os << x.name();
    char delim = '[';
Paul's avatar
Paul committed
181
    reflect_each(x, [&](auto&& y, auto name) {
Paul's avatar
Paul committed
182
        os << delim;
Paul's avatar
Paul committed
183
184
        os << name << "=";
        stream_write_value(os, y);
Paul's avatar
Paul committed
185
186
        delim = ',';
    });
Paul's avatar
Paul committed
187
188
    if(delim == ',')
        os << "]";
Paul's avatar
Paul committed
189
    return os;
Paul's avatar
Paul committed
190
191
}

Paul's avatar
Paul committed
192
193
194
template <class T, class U>
auto operator==(const T& x, const U& y) -> decltype(x.name() == y.name())
{
Paul's avatar
Paul committed
195
196
    static_assert(is_reflectable<T>{} or sizeof(T) <= 1,
                  "Missing equality operator or reflect method.");
Paul's avatar
Paul committed
197
198
199
200
201
202
    if(x.name() != y.name())
        return false;
    const auto& yy = any_cast<T>(y);
    return reflect_tie(x) == reflect_tie(yy);
}

203
} // namespace operation_operators
Paul's avatar
Paul committed
204

Shucai Xiao's avatar
Shucai Xiao committed
205
template <class T>
206
207
208
209
210
211
212
213
auto compute_shape_op(rank<3>, const T& x, const std::vector<shape>& inputs)
    -> decltype(x.compute_shape(inputs))
{
    return x.compute_shape(inputs);
}

template <class T>
auto compute_shape_op(rank<2>, const T& x, const std::vector<shape>& inputs)
Shucai Xiao's avatar
Shucai Xiao committed
214
    -> decltype(x.normalize_compute_shape(inputs))
Shucai Xiao's avatar
Shucai Xiao committed
215
216
{
    dependent_type<operation, T> y = x;
217
    normalize_attributes(y, inputs[0].max_lens());
Shucai Xiao's avatar
Shucai Xiao committed
218
219
220
    return any_cast<T>(y).normalize_compute_shape(inputs);
}

221
template <class T>
222
auto compute_shape_op(rank<1>, const T& x, const std::vector<shape>& inputs)
223
224
225
226
227
    -> decltype(x.compute_shape(inputs, {}))
{
    return x.compute_shape(inputs, {});
}

Shucai Xiao's avatar
Shucai Xiao committed
228
template <class T>
229
shape compute_shape_op(rank<0>, const T& x, const std::vector<shape>&)
Shucai Xiao's avatar
Shucai Xiao committed
230
231
232
233
234
235
{
    std::string name = x.name();
    MIGRAPHX_THROW("Shape not computable: " + name);
}

template <class T>
236
shape compute_shape_op(const T& x, const std::vector<shape>& inputs)
Shucai Xiao's avatar
Shucai Xiao committed
237
{
238
    return compute_shape_op(rank<3>{}, x, inputs);
Shucai Xiao's avatar
Shucai Xiao committed
239
240
241
}

template <class T>
242
243
244
245
auto mod_compute_shape_op(rank<1>,
                          const T& x,
                          const std::vector<shape>& inputs,
                          const std::vector<module_ref>& mod_args)
Shucai Xiao's avatar
Shucai Xiao committed
246
247
248
249
250
251
    -> decltype(x.compute_shape(inputs, mod_args))
{
    return x.compute_shape(inputs, mod_args);
}

template <class T>
252
253
254
255
shape mod_compute_shape_op(rank<0>,
                           const T& x,
                           const std::vector<shape>& inputs,
                           const std::vector<module_ref>& mod_args)
Shucai Xiao's avatar
Shucai Xiao committed
256
{
257
258
    if(mod_args.empty())
        return compute_shape_op(x, inputs);
Shucai Xiao's avatar
Shucai Xiao committed
259
260
261
262
263
    std::string name = x.name();
    MIGRAPHX_THROW("Shape not computable: " + name);
}

template <class T>
264
265
266
shape mod_compute_shape_op(const T& x,
                           const std::vector<shape>& inputs,
                           const std::vector<module_ref>& mod_args)
Shucai Xiao's avatar
Shucai Xiao committed
267
{
268
    return mod_compute_shape_op(rank<1>{}, x, inputs, mod_args);
Shucai Xiao's avatar
Shucai Xiao committed
269
270
}

Paul's avatar
Paul committed
271
template <class T>
Shucai Xiao's avatar
Shucai Xiao committed
272
auto compute_op(rank<1>,
Paul's avatar
Paul committed
273
274
275
276
                const T& x,
                context& ctx,
                const shape& output_shape,
                const std::vector<argument>& input)
charlie's avatar
charlie committed
277
278
279
    -> decltype(x.compute(auto_any_cast(ctx),
                          make_compute_output_shape(x, output_shape, input),
                          input))
Paul's avatar
Paul committed
280
{
charlie's avatar
charlie committed
281
    return x.compute(auto_any_cast(ctx), make_compute_output_shape(x, output_shape, input), input);
Paul's avatar
Paul committed
282
283
284
}

template <class T>
Paul's avatar
Paul committed
285
argument compute_op(rank<0>, const T& x, context&, const shape&, const std::vector<argument>&)
Paul's avatar
Paul committed
286
{
Paul's avatar
Paul committed
287
    std::string name = x.name();
Paul's avatar
Paul committed
288
    MIGRAPHX_THROW("Not computable: " + name);
Paul's avatar
Paul committed
289
290
}

Paul's avatar
Paul committed
291
template <class T>
Paul's avatar
Paul committed
292
293
argument
compute_op(const T& x, context& ctx, const shape& output_shape, const std::vector<argument>& input)
Paul's avatar
Paul committed
294
{
Shucai Xiao's avatar
Shucai Xiao committed
295
    return compute_op(rank<1>{}, x, ctx, output_shape, input);
Paul's avatar
Paul committed
296
297
298
}

template <class T>
Shucai Xiao's avatar
Shucai Xiao committed
299
auto compute_op(rank<1>, const T& x, const shape& output_shape, const std::vector<argument>& input)
charlie's avatar
charlie committed
300
    -> decltype(x.compute(make_compute_output_shape(x, output_shape, input), input))
Paul's avatar
Paul committed
301
{
charlie's avatar
Initial  
charlie committed
302
    return x.compute(make_compute_output_shape(x, output_shape, input), input);
Paul's avatar
Paul committed
303
304
305
}

template <class T>
Shucai Xiao's avatar
Shucai Xiao committed
306
argument compute_op(rank<0>, const T& x, const shape&, const std::vector<argument>&)
Paul's avatar
Paul committed
307
308
{
    std::string name = x.name();
Shucai Xiao's avatar
Shucai Xiao committed
309
    MIGRAPHX_THROW("Not computable: " + name);
Paul's avatar
Paul committed
310
311
312
}

template <class T>
Shucai Xiao's avatar
Shucai Xiao committed
313
314
315
316
317
318
319
320
321
322
323
argument compute_op(const T& x, const shape& output_shape, const std::vector<argument>& input)
{
    return compute_op(rank<1>{}, x, output_shape, input);
}

template <class T, class F>
auto compute_op(rank<1>,
                const T& x,
                const shape& output,
                const std::vector<argument>& inputs,
                const std::vector<module_ref>& module_args,
charlie's avatar
charlie committed
324
325
                F f)
    -> decltype(x.compute(make_compute_output_shape(x, output, inputs), inputs, module_args, f))
Shucai Xiao's avatar
Shucai Xiao committed
326
{
charlie's avatar
charlie committed
327
    return x.compute(make_compute_output_shape(x, output, inputs), inputs, module_args, f);
Shucai Xiao's avatar
Shucai Xiao committed
328
329
330
331
332
333
334
335
336
}

template <class T, class F>
argument compute_op(rank<0>,
                    const T& x,
                    const shape&,
                    const std::vector<argument>&,
                    const std::vector<module_ref>&,
                    F)
Paul's avatar
Paul committed
337
338
339
340
341
{
    std::string name = x.name();
    MIGRAPHX_THROW("Not computable: " + name);
}

Shucai Xiao's avatar
Shucai Xiao committed
342
343
344
345
346
347
template <class T, class F>
argument compute_op(const T& x,
                    const shape& output,
                    const std::vector<argument>& inputs,
                    const std::vector<module_ref>& module_args,
                    F f)
Paul's avatar
Paul committed
348
{
Shucai Xiao's avatar
Shucai Xiao committed
349
    return compute_op(rank<1>{}, x, output, inputs, module_args, f);
Paul's avatar
Paul committed
350
351
}

Shucai Xiao's avatar
Shucai Xiao committed
352
353
354
355
356
357
358
template <class T, class F>
auto compute_op(rank<4>,
                const T& x,
                context& ctx,
                const shape& output,
                const std::vector<argument>& inputs,
                const std::vector<module_ref>& module_args,
charlie's avatar
charlie committed
359
360
361
                F f)
    -> decltype(x.compute(
        auto_any_cast(ctx), make_compute_output_shape(x, output, inputs), inputs, module_args, f))
Shucai Xiao's avatar
Shucai Xiao committed
362
{
charlie's avatar
charlie committed
363
364
    return x.compute(
        auto_any_cast(ctx), make_compute_output_shape(x, output, inputs), inputs, module_args, f);
Shucai Xiao's avatar
Shucai Xiao committed
365
366
}

Shucai Xiao's avatar
Shucai Xiao committed
367
template <class T, class F>
Shucai Xiao's avatar
Shucai Xiao committed
368
auto compute_op(rank<3>,
Shucai Xiao's avatar
Shucai Xiao committed
369
                const T& x,
Shucai Xiao's avatar
Shucai Xiao committed
370
371
                context&,
                const shape& output,
Shucai Xiao's avatar
Shucai Xiao committed
372
373
                const std::vector<argument>& inputs,
                const std::vector<module_ref>& module_args,
charlie's avatar
charlie committed
374
375
                F f)
    -> decltype(x.compute(make_compute_output_shape(x, output, inputs), inputs, module_args, f))
Shucai Xiao's avatar
Shucai Xiao committed
376
{
charlie's avatar
charlie committed
377
    return x.compute(make_compute_output_shape(x, output, inputs), inputs, module_args, f);
Shucai Xiao's avatar
Shucai Xiao committed
378
379
380
}

template <class T, class F>
Shucai Xiao's avatar
Shucai Xiao committed
381
382
383
384
385
386
auto compute_op(rank<2>,
                const T& x,
                context&,
                const shape& output,
                const std::vector<argument>& inputs,
                const std::vector<module_ref>&,
charlie's avatar
charlie committed
387
                F) -> decltype(x.compute(make_compute_output_shape(x, output, inputs), inputs))
Shucai Xiao's avatar
Shucai Xiao committed
388
{
charlie's avatar
charlie committed
389
    return x.compute(make_compute_output_shape(x, output, inputs), inputs);
Shucai Xiao's avatar
Shucai Xiao committed
390
391
392
393
394
395
396
397
398
}

template <class T, class F>
auto compute_op(rank<1>,
                const T& x,
                context& ctx,
                const shape& output,
                const std::vector<argument>& inputs,
                const std::vector<module_ref>&,
charlie's avatar
charlie committed
399
400
                F)
    -> decltype(x.compute(auto_any_cast(ctx), compute_output_shape<T>{x, output, inputs}, inputs))
Shucai Xiao's avatar
Shucai Xiao committed
401
{
charlie's avatar
charlie committed
402
    return x.compute(auto_any_cast(ctx), make_compute_output_shape(x, output, inputs), inputs);
Shucai Xiao's avatar
Shucai Xiao committed
403
404
405
406
407
408
409
410
411
412
}

template <class T, class F>
argument compute_op(rank<0>,
                    const T& x,
                    context&,
                    const shape&,
                    const std::vector<argument>&,
                    const std::vector<module_ref>&,
                    F)
Shucai Xiao's avatar
Shucai Xiao committed
413
414
415
416
417
418
419
{
    std::string name = x.name();
    MIGRAPHX_THROW("Not computable: " + name);
}

template <class T, class F>
argument compute_op(const T& x,
Shucai Xiao's avatar
Shucai Xiao committed
420
421
                    context& ctx,
                    const shape& output,
Shucai Xiao's avatar
Shucai Xiao committed
422
423
424
425
                    const std::vector<argument>& inputs,
                    const std::vector<module_ref>& module_args,
                    F f)
{
Shucai Xiao's avatar
Shucai Xiao committed
426
    return compute_op(rank<4>{}, x, ctx, output, inputs, module_args, f);
Shucai Xiao's avatar
Shucai Xiao committed
427
428
}

Paul's avatar
Paul committed
429
template <class T>
Paul's avatar
Paul committed
430
431
432
433
434
auto is_context_free_op(rank<1>,
                        const T& x,
                        const shape& output_shape,
                        const std::vector<argument>& input)
    -> decltype(x.compute(output_shape, input), std::true_type{});
Paul's avatar
Paul committed
435
436

template <class T>
Paul's avatar
Paul committed
437
438
auto is_context_free_op(rank<0>, const T&, const shape&, const std::vector<argument>&)
    -> std::false_type;
Paul's avatar
Paul committed
439
440

template <class T>
Paul's avatar
Paul committed
441
442
auto is_context_free_op(const T& x) -> decltype(is_context_free_op(
    rank<1>{}, x, std::declval<const shape&>(), std::declval<std::vector<argument>>()))
Paul's avatar
Paul committed
443
444
{
    return {};
Paul's avatar
Paul committed
445
446
}

Shucai Xiao's avatar
Shucai Xiao committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
template <class T>
auto need_normalization_op(rank<1>, const T& x, const std::vector<shape>& inputs)
    -> decltype(x.normalize_compute_shape(inputs), std::true_type{});

template <class T>
auto need_normalization_op(rank<0>, const T&, const std::vector<shape>&) -> std::false_type;

template <class T>
auto need_normalization_op(const T& x)
    -> decltype(need_normalization_op(rank<1>{}, x, std::declval<std::vector<shape>>()))
{
    return {};
}

Paul's avatar
Paul committed
461
template <class T>
462
std::ptrdiff_t output_alias_op(const T&, const std::vector<shape>&)
Paul's avatar
Paul committed
463
464
465
466
{
    return -1;
}

Paul's avatar
Paul committed
467
template <class T>
Paul's avatar
Paul committed
468
469
auto finalize_op(
    rank<1>, T& x, context& ctx, const shape& output_shape, const std::vector<shape>& input)
Paul's avatar
Paul committed
470
471
472
473
474
475
476
    -> decltype(x.finalize(auto_any_cast(ctx), output_shape, input), void())
{
    x.finalize(auto_any_cast(ctx), output_shape, input);
}

template <class T>
void finalize_op(rank<0>, T&, context&, const shape&, const std::vector<shape>&)
Paul's avatar
Paul committed
477
478
{
}
Paul's avatar
Paul committed
479
480
481
482
483
484
485
486

template <class T>
void finalize_op(T& x, context& ctx, const shape& output_shape, const std::vector<shape>& input)
{
    finalize_op(rank<1>{}, x, ctx, output_shape, input);
}

template <class T>
Paul's avatar
Paul committed
487
488
auto has_finalize_op(
    rank<1>, T& x, context& ctx, const shape& output_shape, const std::vector<shape>& input)
Paul's avatar
Paul committed
489
490
491
492
493
494
495
    -> decltype(x.finalize(auto_any_cast(ctx), output_shape, input), std::true_type{});

template <class T>
auto has_finalize_op(rank<0>, T&, context&, const shape&, const std::vector<shape>&)
    -> std::false_type;

template <class T>
Paul's avatar
Paul committed
496
497
498
499
500
auto has_finalize_op(const T&) -> decltype(has_finalize_op(rank<1>{},
                                                           std::declval<T&>(),
                                                           std::declval<context&>(),
                                                           std::declval<const shape&>(),
                                                           std::declval<std::vector<shape>>()))
Paul's avatar
Paul committed
501
502
503
504
{
    return {};
}

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
template <class T>
auto compile_op(
    rank<1>, T& x, context& ctx, const shape& output_shape, const std::vector<shape>& input)
    -> decltype(x.compile(auto_any_cast(ctx), output_shape, input))
{
    return x.compile(auto_any_cast(ctx), output_shape, input);
}

template <class T>
value compile_op(rank<0>, T&, context&, const shape&, const std::vector<shape>&)
{
    return value::object{};
}

template <class T>
value compile_op(const T& x,
                 context& ctx,
                 const shape& output_shape,
                 const std::vector<shape>& input)
{
    return compile_op(rank<1>{}, x, ctx, output_shape, input);
}

528
529
530
531
532
533
template <class T>
value attributes_op(const T&)
{
    return value::object{};
}

534
535
536
537
538
539
540
541
542
template <class T>
value to_value_op(const T& x)
{
    return migraphx::to_value(x);
}

template <class T>
void from_value_op(T& x, const value& v)
{
543
544
    if(not(v.is_object() or (v.empty() and v.is_array())))
        MIGRAPHX_THROW("Value is not an object");
545
546
547
    return migraphx::from_value(v, x);
}

548
template <class T>
549
lifetime get_lifetime_op(const T&)
550
{
551
    return lifetime::local;
552
553
}

554
555
} // namespace detail

556
<%
Paul's avatar
Paul committed
557
558
559
 interface(
     'operation',
     virtual('name', returns = 'std::string', const = True),
Paul's avatar
Paul committed
560
561
     virtual(
         'is_context_free', returns = 'bool', const = True, default = 'detail::is_context_free_op'),
Shucai Xiao's avatar
Shucai Xiao committed
562
563
564
565
     virtual('need_normalization',
             returns = 'bool',
             const   = True,
             default = 'detail::need_normalization_op'),
566
     virtual('has_finalize', returns = 'bool', const = True, default = 'detail::has_finalize_op'),
567
568
     virtual(
         'get_lifetime', returns = 'lifetime', const = True, default = 'detail::get_lifetime_op'),
Paul's avatar
Paul committed
569
     virtual('output_alias',
Paul's avatar
Paul committed
570
             returns = 'std::ptrdiff_t',
Paul's avatar
Paul committed
571
572
             input   = 'const std::vector<shape>&',
             const   = True,
573
             default = 'detail::output_alias_op'),
574
575
576
577
578
579
     virtual('compile',
             returns = 'value',
             ctx     = 'context&',
             output  = 'const shape&',
             input   = 'const std::vector<shape>&',
             default = 'detail::compile_op'),
Paul's avatar
Paul committed
580
581
582
583
     virtual('finalize',
             ctx     = 'context&',
             output  = 'const shape&',
             input   = 'const std::vector<shape>&',
584
             default = 'detail::finalize_op'),
Shucai Xiao's avatar
Shucai Xiao committed
585
586
587
588
     virtual('compute_shape',
             returns = 'shape',
             input   = 'const std::vector<shape>&',
             const   = True,
589
             default = 'detail::compute_shape_op'),
Shucai Xiao's avatar
Shucai Xiao committed
590
591
592
593
594
     virtual('compute_shape',
             returns  = 'shape',
             inputs   = 'const std::vector<shape>&',
             mod_args = 'const std::vector<module_ref>&',
             const    = True,
595
             default  = 'detail::mod_compute_shape_op'),
Paul's avatar
Paul committed
596
597
598
599
600
601
     virtual('compute',
             returns = 'argument',
             ctx     = 'context&',
             output  = 'const shape&',
             input   = 'const std::vector<argument>&',
             const   = True,
602
             default = 'detail::compute_op'),
Paul's avatar
Paul committed
603
604
605
606
607
     virtual('compute',
             returns = 'argument',
             output  = 'const shape&',
             input   = 'const std::vector<argument>&',
             const   = True,
608
             default = 'detail::compute_op'),
Shucai Xiao's avatar
Shucai Xiao committed
609
610
611
     virtual(
         'compute',
         returns     = 'argument',
Shucai Xiao's avatar
Shucai Xiao committed
612
613
614
615
616
617
618
619
620
621
622
623
         output      = 'const shape&',
         input       = 'const std::vector<argument>&',
         module_args = 'const std::vector<module_ref>&',
         run =
             'std::function<std::vector<argument>(module_ref&, const std::unordered_map<std::string, argument>&)>',
         const   = True,
         default = 'detail::compute_op'),
     virtual(
         'compute',
         returns     = 'argument',
         ctx         = 'context&',
         output      = 'const shape&',
Shucai Xiao's avatar
Shucai Xiao committed
624
625
626
         input       = 'const std::vector<argument>&',
         module_args = 'const std::vector<module_ref>&',
         run =
Shucai Xiao's avatar
Shucai Xiao committed
627
             'std::function<std::vector<argument>(module_ref&, const std::unordered_map<std::string, argument>&)>',
Shucai Xiao's avatar
Shucai Xiao committed
628
629
         const   = True,
         default = 'detail::compute_op'),
630
631
     virtual('to_value', returns = 'value', const = True, default = 'detail::to_value_op'),
     virtual('from_value', v = 'const value&', default = 'detail::from_value_op'),
632
     virtual('attributes', returns = 'value', const = True, default = 'detail::attributes_op'),
Paul's avatar
Paul committed
633
634
635
636
     friend('operator<<',
            returns = 'std::ostream &',
            os      = 'std::ostream &',
            op      = 'const operation &',
637
            using   = 'migraphx::detail::operation_operators::operator<<'),
Paul's avatar
Paul committed
638
639
640
641
     friend('operator==',
            returns = 'bool',
            x       = 'const operation &',
            y       = 'const operation &',
642
            using   = 'migraphx::detail::operation_operators::operator==')) %>
Paul's avatar
Paul committed
643
644

    inline bool operator!=(const operation& x, const operation& y)
Paul's avatar
Paul committed
645
{
646
    return not(x == y);
Paul's avatar
Paul committed
647
648
}

649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
inline value
compile(operation& op, context& ctx, const shape& output_shape, const std::vector<shape>& input)
{
    return op.compile(ctx, output_shape, input);
}
template <class Context>
inline value
compile(operation& op, Context& ctx, const shape& output_shape, const std::vector<shape>& input)
{
    dependent_type<context, Context> ctx2 = std::ref(ctx);
    return compile(op, ctx2, output_shape, input);
}
template <class T, class Context>
inline auto compile(T& op, Context& ctx, const shape& output_shape, const std::vector<shape>& input)
    -> decltype(op.compile(ctx, ctx, output_shape, input))
{
    return op.compile(ctx, ctx, output_shape, input);
}
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
inline shape compute_shape(const operation& op, const std::vector<shape>& inputs)
{
    return op.compute_shape(inputs);
}

template <class T>
inline auto compute_shape(const T& op, const std::vector<shape>& inputs)
    -> decltype(op.compute_shape(inputs))
{
    return op.compute_shape(inputs);
}

template <class T>
inline auto compute_shape(const T& op, const std::vector<shape>& inputs)
    -> decltype(op.normalize_compute_shape(inputs))
{
683
    return detail::compute_shape_op(op, inputs);
684
685
}

Shucai Xiao's avatar
Shucai Xiao committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
inline shape compute_shape(const operation& op,
                           const std::vector<shape>& inputs,
                           const std::vector<module_ref>& mod_args)
{
    return op.compute_shape(inputs, mod_args);
}

template <class T>
inline auto compute_shape(const T& op,
                          const std::vector<shape>& inputs,
                          const std::vector<module_ref>& mod_args)
    -> decltype(op.compute_shape(inputs, mod_args))
{
    return op.compute_shape(inputs, mod_args);
}

template <class T>
inline auto compute_shape(const T& op,
                          const std::vector<shape>& inputs,
                          const std::vector<module_ref>& mod_args)
    -> decltype(op.normalize_compute_shape(inputs, mod_args))
{
708
    return detail::compute_shape_op(op, inputs, mod_args);
Shucai Xiao's avatar
Shucai Xiao committed
709
710
}

Paul's avatar
Paul committed
711
inline bool is_context_free(const operation& op) { return op.is_context_free(); }
Paul's avatar
Paul committed
712

Paul's avatar
Paul committed
713
template <class T>
Paul's avatar
Paul committed
714
715
bool is_context_free(const T& x)
{
716
    return detail::is_context_free_op(x);
Paul's avatar
Paul committed
717
718
}

Shucai Xiao's avatar
Shucai Xiao committed
719
720
721
722
723
724
725
726
inline bool need_normalization(const operation& op) { return op.need_normalization(); }

template <class T>
bool need_normalization(const T& x)
{
    return detail::need_normalization_op(x);
}

Paul's avatar
Paul committed
727
728
729
730
731
inline bool has_finalize(const operation& op) { return op.has_finalize(); }

template <class T>
bool has_finalize(const T& x)
{
732
    return detail::has_finalize_op(x);
Paul's avatar
Paul committed
733
734
}

735
736
737
void migraphx_to_value(value& v, const operation& op);
void migraphx_from_value(const value& v, operation& op);

Paul's avatar
Paul committed
738
739
#endif

Paul's avatar
Paul committed
740
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
741
} // namespace migraphx
Paul's avatar
Paul committed
742
743

#endif