operation.hpp 16 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_OPERAND_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_OPERAND_HPP
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
#include <cassert>
Paul's avatar
Paul committed
5
#include <string>
Paul's avatar
Paul committed
6
#include <functional>
Paul's avatar
Paul committed
7
8
9
#include <memory>
#include <type_traits>
#include <utility>
Paul's avatar
Paul committed
10
11
12
13
14
15
16
17
18
#include <migraphx/shape.hpp>
#include <migraphx/reflect.hpp>
#include <migraphx/streamutils.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/context.hpp>
#include <migraphx/auto_any_cast.hpp>
#include <migraphx/config.hpp>

namespace migraphx {
Paul's avatar
Paul committed
19
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
20

Paul's avatar
Paul committed
21
22
#ifdef DOXYGEN

Paul's avatar
Paul committed
23
/// The operation interface represents an action an instruction will perform. All
Paul's avatar
Paul committed
24
25
26
27
28
29
30
31
/// operation classes must be CopyConstructible.
struct operation
{
    /// A unique name identifying the operation
    std::string name() const;
    /// 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
32
    shape compute_shape(const std::vector<shape>& input) const;
Paul's avatar
Paul committed
33
    /**
Paul's avatar
Paul committed
34
35
36
37
     * @brief This performs the operation's computation.
     *
     * This method can be optional when the operation is only used as a placeholder to be lowered
     * later on.
Paul's avatar
Paul committed
38
39
40
41
42
     *
     * @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.
     * @param output This is the output shape. It is equivalent to running `compute_shape` with each
     * `shape` of the `argument`.
Paul's avatar
Paul committed
43
     * @param input This is the `argument` result from the previous instruction's computation.
Paul's avatar
Paul committed
44
45
46
     * @return Return an `argument` of the result computation. The `shape` of `argument` should be
     * the same the `output` shape.
     */
Paul's avatar
Paul committed
47
    argument compute(context& ctx, const shape& output, const std::vector<argument>& input) const;
Paul's avatar
Paul committed
48
49
50
    /// An optional method to return which argument the output will alias. If
    /// there is no aliased output then -1 can be returned.
    int output_alias(const std::vector<shape>& input) const;
Paul's avatar
Paul committed
51
52
53
54
55
    /// An optional stream operator to print the operation. When this is not
    /// implemented, it will just print the operation's name.
    friend std::ostream& operator<<(std::ostream& os, const operation& op);
};

Paul's avatar
Paul committed
56
57
58
/// Returns true if operation does not require a context to run compute
bool is_context_free(const operation& x);

Paul's avatar
Paul committed
59
60
#else

Paul's avatar
Paul committed
61
62
63
64
65
namespace operation_stream {

template <class T>
auto operator<<(std::ostream& os, const T& x) -> decltype(os << x.name())
{
Paul's avatar
Paul committed
66
67
    os << x.name();
    char delim = '[';
Paul's avatar
Paul committed
68
    reflect_each(x, [&](auto& y, auto name) {
Paul's avatar
Paul committed
69
        os << delim;
Paul's avatar
Paul committed
70
71
        os << name << "=";
        stream_write_value(os, y);
Paul's avatar
Paul committed
72
73
74
75
76
        delim = ',';
    });
    if(delim == ',')
        os << "]";
    return os;
Paul's avatar
Paul committed
77
78
79
80
}

} // namespace operation_stream

Paul's avatar
Paul committed
81
82
83
84
85
86
87
88
89
90
91
92
93
namespace operation_equal {

template <class T, class U>
auto operator==(const T& x, const U& y) -> decltype(x.name() == y.name())
{
    if(x.name() != y.name())
        return false;
    const auto& yy = any_cast<T>(y);
    return reflect_tie(x) == reflect_tie(yy);
}

} // namespace operation_equal

Paul's avatar
Paul committed
94
template <class T>
Paul's avatar
Paul committed
95
auto compute_op(rank<2>,
Paul's avatar
Paul committed
96
97
98
99
100
101
102
103
104
                const T& x,
                context& ctx,
                const shape& output_shape,
                const std::vector<argument>& input)
    -> decltype(x.compute(auto_any_cast(ctx), output_shape, input))
{
    return x.compute(auto_any_cast(ctx), output_shape, input);
}

Paul's avatar
Paul committed
105
106
107
108
109
110
111
112
template <class T>
auto compute_op(
    rank<1>, const T& x, context&, const shape& output_shape, const std::vector<argument>& input)
    -> decltype(x.compute(output_shape, input))
{
    return x.compute(output_shape, input);
}

Paul's avatar
Paul committed
113
114
115
template <class T>
argument compute_op(rank<0>, const T& x, context&, const shape&, const std::vector<argument>&)
{
Paul's avatar
Paul committed
116
    std::string name = x.name();
Paul's avatar
Paul committed
117
    MIGRAPHX_THROW("Not computable: " + name);
Paul's avatar
Paul committed
118
119
}

Paul's avatar
Paul committed
120
template <class T>
Paul's avatar
Paul committed
121
122
argument
compute_op(const T& x, context& ctx, const shape& output_shape, const std::vector<argument>& input)
Paul's avatar
Paul committed
123
{
Paul's avatar
Paul committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    return compute_op(rank<2>{}, x, ctx, output_shape, input);
}

template <class T>
auto compute_op(rank<2>, const T& x, const shape& output_shape, const std::vector<argument>& input)
    -> decltype(x.compute(output_shape, input))
{
    return x.compute(output_shape, input);
}

template <class T>
auto compute_op(rank<1>, const T& x, const shape& output_shape, const std::vector<argument>& input)
    -> decltype(x.compute(auto_any_cast(std::declval<context&>()), output_shape, input))
{
    std::string name = x.name();
    MIGRAPHX_THROW("Not computable without a context: " + name);
}

template <class T>
argument compute_op(rank<0>, const T& x, const shape&, const std::vector<argument>&)
{
    std::string name = x.name();
    MIGRAPHX_THROW("Not computable: " + name);
}

template <class T>
argument compute_op(const T& x, const shape& output_shape, const std::vector<argument>& input)
{
    return compute_op(rank<2>{}, x, output_shape, input);
}

template <class T>
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{});

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

template <class T>
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>>()))
{
    return {};
Paul's avatar
Paul committed
171
172
}

Paul's avatar
Paul committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
template <class T>
int output_alias_op(rank<0>, const T&, const std::vector<shape>&)
{
    return -1;
}

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

template <class T>
int output_alias_op(const T& x, const std::vector<shape>& shapes)
{
    return output_alias_op(rank<1>{}, x, shapes);
}

Paul's avatar
Paul committed
192
/*
Paul's avatar
Paul committed
193
194
 * Type-erased interface for:
 *
Paul's avatar
Paul committed
195
 * struct operation
Paul's avatar
Paul committed
196
 * {
Paul's avatar
Paul committed
197
 *      std::string name() const;
Paul's avatar
Paul committed
198
 *      bool is_context_free() const;
Paul's avatar
Paul committed
199
 *      int output_alias(const std::vector<shape>& input) const;
Paul's avatar
Paul committed
200
201
 *      shape compute_shape(const std::vector<shape>& input) const;
 *      argument compute(context& ctx,const shape& output,const std::vector<argument>& input) const;
Paul's avatar
Paul committed
202
 *      argument compute(const shape& output,const std::vector<argument>& input) const;
Paul's avatar
Paul committed
203
 *     friend std::ostream & operator<<(std::ostream & os,const operation & op) ;
Paul's avatar
Paul committed
204
 *     friend bool operator==(const operation & x,const operation & y) ;
Paul's avatar
Paul committed
205
206
207
 * };
 *
 */
Paul's avatar
Paul committed
208

Paul's avatar
Paul committed
209
struct operation
Paul's avatar
Paul committed
210
{
Paul's avatar
Paul committed
211
    // Constructors
Paul's avatar
Paul committed
212
    operation() = default;
Paul's avatar
Paul committed
213

Paul's avatar
Paul committed
214
    template <typename PrivateDetailTypeErasedT>
Paul's avatar
Paul committed
215
    operation(PrivateDetailTypeErasedT value)
Paul's avatar
Paul committed
216
217
218
219
        : private_detail_te_handle_mem_var(
              std::make_shared<private_detail_te_handle_type<
                  typename std::remove_reference<PrivateDetailTypeErasedT>::type>>(
                  std::forward<PrivateDetailTypeErasedT>(value)))
Paul's avatar
Paul committed
220
221
222
223
    {
    }

    // Assignment
Paul's avatar
Paul committed
224
    template <typename PrivateDetailTypeErasedT>
Paul's avatar
Paul committed
225
    operation& operator=(PrivateDetailTypeErasedT value)
Paul's avatar
Paul committed
226
    {
Paul's avatar
Paul committed
227
228
229
230
231
        if(private_detail_te_handle_mem_var.unique())
            *private_detail_te_handle_mem_var = std::forward<PrivateDetailTypeErasedT>(value);
        else if(!private_detail_te_handle_mem_var)
            private_detail_te_handle_mem_var = std::make_shared<PrivateDetailTypeErasedT>(
                std::forward<PrivateDetailTypeErasedT>(value));
Paul's avatar
Paul committed
232
233
234
        return *this;
    }

Paul's avatar
Paul committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    // Cast
    template <typename PrivateDetailTypeErasedT>
    PrivateDetailTypeErasedT* any_cast()
    {
        return private_detail_te_get_handle().type() == typeid(PrivateDetailTypeErasedT)
                   ? std::addressof(static_cast<private_detail_te_handle_type<
                                        typename std::remove_cv<PrivateDetailTypeErasedT>::type>&>(
                                        private_detail_te_get_handle())
                                        .private_detail_te_value)
                   : nullptr;
    }

    template <typename PrivateDetailTypeErasedT>
    const typename std::remove_cv<PrivateDetailTypeErasedT>::type* any_cast() const
    {
        return private_detail_te_get_handle().type() == typeid(PrivateDetailTypeErasedT)
                   ? std::addressof(static_cast<const private_detail_te_handle_type<
                                        typename std::remove_cv<PrivateDetailTypeErasedT>::type>&>(
                                        private_detail_te_get_handle())
                                        .private_detail_te_value)
                   : nullptr;
    }

Paul's avatar
Paul committed
258
259
260
261
262
263
264
265
    const std::type_info& type_id() const
    {
        if(private_detail_te_handle_empty())
            return typeid(std::nullptr_t);
        else
            return private_detail_te_get_handle().type();
    }

Paul's avatar
Paul committed
266
267
    std::string name() const
    {
Paul's avatar
Paul committed
268
269
        assert((*this).private_detail_te_handle_mem_var);
        return (*this).private_detail_te_get_handle().name();
Paul's avatar
Paul committed
270
271
    }

Paul's avatar
Paul committed
272
273
274
275
276
277
    bool is_context_free() const
    {
        assert((*this).private_detail_te_handle_mem_var);
        return (*this).private_detail_te_get_handle().is_context_free();
    }

Paul's avatar
Paul committed
278
279
280
281
282
283
    int output_alias(const std::vector<shape>& input) const
    {
        assert((*this).private_detail_te_handle_mem_var);
        return (*this).private_detail_te_get_handle().output_alias(input);
    }

Paul's avatar
Paul committed
284
    shape compute_shape(const std::vector<shape>& input) const
Paul's avatar
Paul committed
285
    {
Paul's avatar
Paul committed
286
        assert((*this).private_detail_te_handle_mem_var);
Paul's avatar
Paul committed
287
        return (*this).private_detail_te_get_handle().compute_shape(input);
Paul's avatar
Paul committed
288
289
    }

Paul's avatar
Paul committed
290
    argument compute(context& ctx, const shape& output, const std::vector<argument>& input) const
Paul's avatar
Paul committed
291
    {
Paul's avatar
Paul committed
292
        assert((*this).private_detail_te_handle_mem_var);
Paul's avatar
Paul committed
293
        return (*this).private_detail_te_get_handle().compute(ctx, output, input);
Paul's avatar
Paul committed
294
295
    }

Paul's avatar
Paul committed
296
297
298
299
300
301
    argument compute(const shape& output, const std::vector<argument>& input) const
    {
        assert((*this).private_detail_te_handle_mem_var);
        return (*this).private_detail_te_get_handle().compute(output, input);
    }

Paul's avatar
Paul committed
302
303
304
305
    friend std::ostream& operator<<(std::ostream& os, const operation& op)
    {
        assert(op.private_detail_te_handle_mem_var);
        return op.private_detail_te_get_handle().operator_shift_left(os);
Paul's avatar
Paul committed
306
307
    }

Paul's avatar
Paul committed
308
309
310
311
312
313
    friend bool operator==(const operation& x, const operation& y)
    {
        assert(x.private_detail_te_handle_mem_var);
        return x.private_detail_te_get_handle().operator==(y);
    }

Paul's avatar
Paul committed
314
    private:
Paul's avatar
Paul committed
315
    struct private_detail_te_handle_base_type
Paul's avatar
Paul committed
316
    {
Paul's avatar
Paul committed
317
318
        virtual ~private_detail_te_handle_base_type() {}
        virtual std::shared_ptr<private_detail_te_handle_base_type> clone() const = 0;
Paul's avatar
Paul committed
319
        virtual const std::type_info& type() const                                = 0;
Paul's avatar
Paul committed
320

Paul's avatar
Paul committed
321
        virtual std::string name() const                                   = 0;
Paul's avatar
Paul committed
322
        virtual bool is_context_free() const                               = 0;
Paul's avatar
Paul committed
323
        virtual int output_alias(const std::vector<shape>& input) const    = 0;
Paul's avatar
Paul committed
324
325
        virtual shape compute_shape(const std::vector<shape>& input) const = 0;
        virtual argument
Paul's avatar
Paul committed
326
327
328
329
        compute(context& ctx, const shape& output, const std::vector<argument>& input) const    = 0;
        virtual argument compute(const shape& output, const std::vector<argument>& input) const = 0;
        virtual std::ostream& operator_shift_left(std::ostream& os) const                       = 0;
        virtual bool operator==(const operation& y) const                                       = 0;
Paul's avatar
Paul committed
330
331
    };

Paul's avatar
Paul committed
332
333
    template <typename PrivateDetailTypeErasedT>
    struct private_detail_te_handle_type : private_detail_te_handle_base_type
Paul's avatar
Paul committed
334
    {
Paul's avatar
Paul committed
335
336
337
338
339
340
        template <typename PrivateDetailTypeErasedU = PrivateDetailTypeErasedT>
        private_detail_te_handle_type(
            PrivateDetailTypeErasedT value,
            typename std::enable_if<std::is_reference<PrivateDetailTypeErasedU>::value>::type* =
                nullptr)
            : private_detail_te_value(value)
Paul's avatar
Paul committed
341
342
343
        {
        }

Paul's avatar
Paul committed
344
345
346
347
348
349
        template <typename PrivateDetailTypeErasedU = PrivateDetailTypeErasedT>
        private_detail_te_handle_type(
            PrivateDetailTypeErasedT value,
            typename std::enable_if<!std::is_reference<PrivateDetailTypeErasedU>::value,
                                    int>::type* = nullptr) noexcept
            : private_detail_te_value(std::move(value))
Paul's avatar
Paul committed
350
351
352
        {
        }

Paul's avatar
Paul committed
353
        std::shared_ptr<private_detail_te_handle_base_type> clone() const override
Paul's avatar
Paul committed
354
        {
Paul's avatar
Paul committed
355
            return std::make_shared<private_detail_te_handle_type>(private_detail_te_value);
Paul's avatar
Paul committed
356
357
        }

Paul's avatar
Paul committed
358
        const std::type_info& type() const override { return typeid(private_detail_te_value); }
Paul's avatar
Paul committed
359

Paul's avatar
Paul committed
360
        std::string name() const override { return private_detail_te_value.name(); }
Paul's avatar
Paul committed
361

Paul's avatar
Paul committed
362
363
364
365
366
367
        bool is_context_free() const override
        {

            return is_context_free_op(private_detail_te_value);
        }

Paul's avatar
Paul committed
368
369
370
371
372
373
        int output_alias(const std::vector<shape>& input) const override
        {

            return output_alias_op(private_detail_te_value, input);
        }

Paul's avatar
Paul committed
374
        shape compute_shape(const std::vector<shape>& input) const override
Paul's avatar
Paul committed
375
        {
Paul's avatar
Paul committed
376

Paul's avatar
Paul committed
377
            return private_detail_te_value.compute_shape(input);
Paul's avatar
Paul committed
378
379
        }

Paul's avatar
Paul committed
380
381
382
        argument compute(context& ctx,
                         const shape& output,
                         const std::vector<argument>& input) const override
Paul's avatar
Paul committed
383
        {
Paul's avatar
Paul committed
384

Paul's avatar
Paul committed
385
            return compute_op(private_detail_te_value, ctx, output, input);
Paul's avatar
Paul committed
386
387
        }

Paul's avatar
Paul committed
388
389
390
391
392
393
        argument compute(const shape& output, const std::vector<argument>& input) const override
        {

            return compute_op(private_detail_te_value, output, input);
        }

Paul's avatar
Paul committed
394
395
        std::ostream& operator_shift_left(std::ostream& os) const override
        {
Paul's avatar
Paul committed
396
            using migraphx::operation_stream::operator<<;
Paul's avatar
Paul committed
397
398
399
            return os << private_detail_te_value;
        }

Paul's avatar
Paul committed
400
401
        bool operator==(const operation& y) const override
        {
Paul's avatar
Paul committed
402
            using migraphx::operation_equal::operator==;
Paul's avatar
Paul committed
403
404
405
            return private_detail_te_value == y;
        }

Paul's avatar
Paul committed
406
        PrivateDetailTypeErasedT private_detail_te_value;
Paul's avatar
Paul committed
407
408
    };

Paul's avatar
Paul committed
409
410
411
    template <typename PrivateDetailTypeErasedT>
    struct private_detail_te_handle_type<std::reference_wrapper<PrivateDetailTypeErasedT>>
        : private_detail_te_handle_type<PrivateDetailTypeErasedT&>
Paul's avatar
Paul committed
412
    {
Paul's avatar
Paul committed
413
414
        private_detail_te_handle_type(std::reference_wrapper<PrivateDetailTypeErasedT> ref)
            : private_detail_te_handle_type<PrivateDetailTypeErasedT&>(ref.get())
Paul's avatar
Paul committed
415
416
417
418
        {
        }
    };

Paul's avatar
Paul committed
419
420
421
422
423
    bool private_detail_te_handle_empty() const
    {
        return private_detail_te_handle_mem_var == nullptr;
    }

Paul's avatar
Paul committed
424
425
    const private_detail_te_handle_base_type& private_detail_te_get_handle() const
    {
Paul's avatar
Paul committed
426
        assert(private_detail_te_handle_mem_var != nullptr);
Paul's avatar
Paul committed
427
428
        return *private_detail_te_handle_mem_var;
    }
Paul's avatar
Paul committed
429

Paul's avatar
Paul committed
430
    private_detail_te_handle_base_type& private_detail_te_get_handle()
Paul's avatar
Paul committed
431
    {
Paul's avatar
Paul committed
432
        assert(private_detail_te_handle_mem_var != nullptr);
Paul's avatar
Paul committed
433
434
435
        if(!private_detail_te_handle_mem_var.unique())
            private_detail_te_handle_mem_var = private_detail_te_handle_mem_var->clone();
        return *private_detail_te_handle_mem_var;
Paul's avatar
Paul committed
436
437
    }

Paul's avatar
Paul committed
438
    std::shared_ptr<private_detail_te_handle_base_type> private_detail_te_handle_mem_var;
Paul's avatar
Paul committed
439
440
};

Paul's avatar
Paul committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
template <typename ValueType>
inline const ValueType* any_cast(const operation* x)
{
    return x->any_cast<ValueType>();
}

template <typename ValueType>
inline ValueType* any_cast(operation* x)
{
    return x->any_cast<ValueType>();
}

template <typename ValueType>
inline ValueType& any_cast(operation& x)
{
Paul's avatar
Paul committed
456
    auto* y = x.any_cast<typename std::remove_reference<ValueType>::type>();
Paul's avatar
Paul committed
457
458
459
460
461
462
463
464
    if(y == nullptr)
        throw std::bad_cast();
    return *y;
}

template <typename ValueType>
inline const ValueType& any_cast(const operation& x)
{
Paul's avatar
Paul committed
465
    const auto* y = x.any_cast<typename std::remove_reference<ValueType>::type>();
Paul's avatar
Paul committed
466
467
468
469
470
    if(y == nullptr)
        throw std::bad_cast();
    return *y;
}

Paul's avatar
Paul committed
471
472
inline bool operator!=(const operation& x, const operation& y) { return !(x == y); }

Paul's avatar
Paul committed
473
474
475
476
477
478
479
480
inline bool is_context_free(const operation& op) { return op.is_context_free(); }

template <class T>
bool is_context_free(const T& x)
{
    return is_context_free_op(x);
}

Paul's avatar
Paul committed
481
482
#endif

Paul's avatar
Paul committed
483
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
484
} // namespace migraphx
Paul's avatar
Paul committed
485
486

#endif