#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_OPERAND_HPP #define MIGRAPHX_GUARD_MIGRAPHLIB_OPERAND_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include namespace migraphx { inline namespace MIGRAPHX_INLINE_NS { struct context; #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; /// An optional method that can be used to finalize the operator before running void finalize(context& ctx); /// 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. shape compute_shape(const std::vector& input) const; /** * @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. * * @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`. * @param input This is the `argument` result from the previous instruction's computation. * @return Return an `argument` of the result computation. The `shape` of `argument` should be * the same the `output` shape. */ argument compute(context& ctx, const shape& output, const std::vector& input) const; /// An optional method to return which argument the output will alias. If /// there is no aliased output then -1 can be returned. std::ptrdiff_t output_alias(const std::vector& input) const; /// 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); }; /// Returns true if operation does not require a context to run compute bool is_context_free(const operation& x); /// Returns true if operation needs normalization before running compute bool need_normalization(const operation& x); /// Returns true if the operation has a finalize method bool has_finalize(const operation& x); #else namespace detail { namespace operation_operators { template auto operator<<(std::ostream& os, const T& x) -> decltype(os << x.name()) { os << x.name(); char delim = '['; reflect_each(x, [&](auto&& y, auto name) { os << delim; os << name << "="; stream_write_value(os, y); delim = ','; }); if(delim == ',') os << "]"; return os; } template auto operator==(const T& x, const U& y) -> decltype(x.name() == y.name()) { static_assert(is_reflectable{} or sizeof(T) <= 1, "Missing equality operator or reflect method."); if(x.name() != y.name()) return false; const auto& yy = any_cast(y); return reflect_tie(x) == reflect_tie(yy); } } // namespace operation_operators template shape normalize_compute_shape_op(T&& x, std::vector inputs) { dependent_type y = x; normalize_attributes(y, inputs[0].lens()); return any_cast(y).normalize_compute_shape(inputs); } template auto compute_op(rank<2>, const T& x, context& ctx, const shape& output_shape, const std::vector& input) -> decltype(x.compute(auto_any_cast(ctx), output_shape, input)) { return x.compute(auto_any_cast(ctx), output_shape, input); } template auto compute_op( rank<1>, const T& x, context&, const shape& output_shape, const std::vector& input) -> decltype(x.compute(output_shape, input)) { return x.compute(output_shape, input); } template argument compute_op(rank<0>, const T& x, context&, const shape&, const std::vector&) { std::string name = x.name(); MIGRAPHX_THROW("Not computable: " + name); } template argument compute_op(const T& x, context& ctx, const shape& output_shape, const std::vector& input) { return compute_op(rank<2>{}, x, ctx, output_shape, input); } template auto compute_op(rank<2>, const T& x, const shape& output_shape, const std::vector& input) -> decltype(x.compute(output_shape, input)) { return x.compute(output_shape, input); } template auto compute_op(rank<1>, const T& x, const shape& output_shape, const std::vector& input) -> decltype(x.compute(auto_any_cast(std::declval()), output_shape, input)) { std::string name = x.name(); MIGRAPHX_THROW("Not computable without a context: " + name); } template argument compute_op(rank<0>, const T& x, const shape&, const std::vector&) { std::string name = x.name(); MIGRAPHX_THROW("Not computable: " + name); } template argument compute_op(const T& x, const shape& output_shape, const std::vector& input) { return compute_op(rank<2>{}, x, output_shape, input); } template auto is_context_free_op(rank<1>, const T& x, const shape& output_shape, const std::vector& input) -> decltype(x.compute(output_shape, input), std::true_type{}); template auto is_context_free_op(rank<0>, const T&, const shape&, const std::vector&) -> std::false_type; template auto is_context_free_op(const T& x) -> decltype(is_context_free_op( rank<1>{}, x, std::declval(), std::declval>())) { return {}; } template auto need_normalization_op(rank<1>, const T& x, const std::vector& inputs) -> decltype(x.normalize_compute_shape(inputs), std::true_type{}); template auto need_normalization_op(rank<0>, const T&, const std::vector&) -> std::false_type; template auto need_normalization_op(const T& x) -> decltype(need_normalization_op(rank<1>{}, x, std::declval>())) { return {}; } template std::ptrdiff_t output_alias_op(const T&, const std::vector&) { return -1; } template auto finalize_op( rank<1>, T& x, context& ctx, const shape& output_shape, const std::vector& input) -> decltype(x.finalize(auto_any_cast(ctx), output_shape, input), void()) { x.finalize(auto_any_cast(ctx), output_shape, input); } template void finalize_op(rank<0>, T&, context&, const shape&, const std::vector&) { } template void finalize_op(T& x, context& ctx, const shape& output_shape, const std::vector& input) { finalize_op(rank<1>{}, x, ctx, output_shape, input); } template auto has_finalize_op( rank<1>, T& x, context& ctx, const shape& output_shape, const std::vector& input) -> decltype(x.finalize(auto_any_cast(ctx), output_shape, input), std::true_type{}); template auto has_finalize_op(rank<0>, T&, context&, const shape&, const std::vector&) -> std::false_type; template auto has_finalize_op(const T&) -> decltype(has_finalize_op(rank<1>{}, std::declval(), std::declval(), std::declval(), std::declval>())) { return {}; } template value attributes_op(const T&) { return value::object{}; } template value to_value_op(const T& x) { return migraphx::to_value(x); } template void from_value_op(T& x, const value& v) { if(not(v.is_object() or (v.empty() and v.is_array()))) MIGRAPHX_THROW("Value is not an object"); return migraphx::from_value(v, x); } } // namespace detail <% interface( 'operation', virtual('name', returns = 'std::string', const = True), virtual( 'is_context_free', returns = 'bool', const = True, default = 'detail::is_context_free_op'), virtual('need_normalization', returns = 'bool', const = True, default = 'detail::need_normalization_op'), virtual('has_finalize', returns = 'bool', const = True, default = 'detail::has_finalize_op'), virtual('output_alias', returns = 'std::ptrdiff_t', input = 'const std::vector&', const = True, default = 'detail::output_alias_op'), virtual('finalize', ctx = 'context&', output = 'const shape&', input = 'const std::vector&', default = 'detail::finalize_op'), virtual('compute_shape', returns = 'shape', input = 'const std::vector&', const = True, default = 'detail::normalize_compute_shape_op'), virtual('compute', returns = 'argument', ctx = 'context&', output = 'const shape&', input = 'const std::vector&', const = True, default = 'detail::compute_op'), virtual('compute', returns = 'argument', output = 'const shape&', input = 'const std::vector&', const = True, default = 'detail::compute_op'), virtual('to_value', returns = 'value', const = True, default = 'detail::to_value_op'), virtual('from_value', v = 'const value&', default = 'detail::from_value_op'), virtual('attributes', returns = 'value', const = True, default = 'detail::attributes_op'), friend('operator<<', returns = 'std::ostream &', os = 'std::ostream &', op = 'const operation &', using = 'migraphx::detail::operation_operators::operator<<'), friend('operator==', returns = 'bool', x = 'const operation &', y = 'const operation &', using = 'migraphx::detail::operation_operators::operator==')) %> inline bool operator!=(const operation& x, const operation& y) { return !(x == y); } inline shape compute_shape(const operation& op, const std::vector& inputs) { return op.compute_shape(inputs); } template inline auto compute_shape(const T& op, const std::vector& inputs) -> decltype(op.compute_shape(inputs)) { return op.compute_shape(inputs); } template inline auto compute_shape(const T& op, const std::vector& inputs) -> decltype(op.normalize_compute_shape(inputs)) { return detail::normalize_compute_shape_op(op, inputs); } inline bool is_context_free(const operation& op) { return op.is_context_free(); } template bool is_context_free(const T& x) { return detail::is_context_free_op(x); } inline bool need_normalization(const operation& op) { return op.need_normalization(); } template bool need_normalization(const T& x) { return detail::need_normalization_op(x); } inline bool has_finalize(const operation& op) { return op.has_finalize(); } template bool has_finalize(const T& x) { return detail::has_finalize_op(x); } void migraphx_to_value(value& v, const operation& op); void migraphx_from_value(const value& v, operation& op); #endif } // namespace MIGRAPHX_INLINE_NS } // namespace migraphx #endif