|
| 1 | +#ifndef __INFINIOP_ELEMENTWISE_CPU_IMPL_H__ |
| 2 | +#define __INFINIOP_ELEMENTWISE_CPU_IMPL_H__ |
| 3 | + |
| 4 | +#include "elementwise_cpu.h" |
| 5 | +#include "../../devices/cpu/common_cpu.h" |
| 6 | +#include "../../../utils/check.h" |
| 7 | +#include "../../../utils/result.hpp" |
| 8 | + |
| 9 | +/** |
| 10 | + * @brief Generic implementation for elementwise CPU operators. |
| 11 | + * |
| 12 | + * This file provides a generic implementation template that can be used |
| 13 | + * by all binary and unary operators to reduce code duplication. |
| 14 | + * |
| 15 | + * Usage: |
| 16 | + * #include "elementwise_cpu_impl.h" |
| 17 | + * namespace op::pow::cpu { |
| 18 | + * using Op = op::elementwise::binary::BinaryOp<BinaryMode::Pow>; |
| 19 | + * ELEMENTWISE_CPU_IMPL_BINARY(pow) |
| 20 | + * } |
| 21 | + * |
| 22 | + * namespace op::sqrt::cpu { |
| 23 | + * using Op = op::elementwise::unary::UnaryOp<UnaryMode::Sqrt>; |
| 24 | + * ELEMENTWISE_CPU_IMPL_UNARY(sqrt) |
| 25 | + * } |
| 26 | + */ |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * @brief Macro to generate binary operator implementation. |
| 31 | + * |
| 32 | + * This macro generates the Descriptor destructor, create, and calculate methods |
| 33 | + * for binary operators, using the generic implementation. |
| 34 | + * |
| 35 | + * Usage: |
| 36 | + * namespace op::pow::cpu { |
| 37 | + * using Op = op::elementwise::binary::BinaryOp<BinaryMode::Pow>; |
| 38 | + * ELEMENTWISE_CPU_IMPL_BINARY(pow) |
| 39 | + * } |
| 40 | + */ |
| 41 | +#define ELEMENTWISE_CPU_IMPL_BINARY(OP) \ |
| 42 | + \ |
| 43 | + Descriptor::~Descriptor() = default; \ |
| 44 | + \ |
| 45 | + infiniStatus_t Descriptor::create( \ |
| 46 | + infiniopHandle_t handle_, \ |
| 47 | + Descriptor **desc_ptr, \ |
| 48 | + infiniopTensorDescriptor_t out_desc, \ |
| 49 | + std::vector<infiniopTensorDescriptor_t> input_desc_vec) { \ |
| 50 | + auto handle = reinterpret_cast<device::cpu::Handle *>(handle_); \ |
| 51 | + auto dtype = out_desc->dtype(); \ |
| 52 | + const auto &a_desc = input_desc_vec.at(0); \ |
| 53 | + const auto &b_desc = input_desc_vec.at(1); \ |
| 54 | + const auto &out_shape = out_desc->shape(); \ |
| 55 | + const auto &a_shape = a_desc->shape(); \ |
| 56 | + const auto &b_shape = b_desc->shape(); \ |
| 57 | + CHECK_DTYPE(dtype, INFINI_DTYPE_F16, INFINI_DTYPE_F32); \ |
| 58 | + CHECK_SAME_SHAPE(out_shape, a_shape, b_shape); \ |
| 59 | + CREATE_ELEMENTWISE_CPU_DESCRIPTOR(handle, dtype, out_desc, input_desc_vec); \ |
| 60 | + return INFINI_STATUS_SUCCESS; \ |
| 61 | + } \ |
| 62 | + \ |
| 63 | + infiniStatus_t Descriptor::calculate( \ |
| 64 | + void *workspace, \ |
| 65 | + size_t workspace_size, \ |
| 66 | + void *output, \ |
| 67 | + std::vector<const void *> inputs, \ |
| 68 | + void *stream) const { \ |
| 69 | + switch (_dtype) { \ |
| 70 | + case INFINI_DTYPE_F16: \ |
| 71 | + return _device_info->template calculate<Op, fp16_t>( \ |
| 72 | + _info, output, inputs, stream); \ |
| 73 | + case INFINI_DTYPE_F32: \ |
| 74 | + return _device_info->template calculate<Op, float>( \ |
| 75 | + _info, output, inputs, stream); \ |
| 76 | + default: \ |
| 77 | + return INFINI_STATUS_BAD_TENSOR_DTYPE; \ |
| 78 | + } \ |
| 79 | + } |
| 80 | + |
| 81 | +/** |
| 82 | + * @brief Macro to generate unary operator implementation. |
| 83 | + * |
| 84 | + * This macro generates the Descriptor destructor, create, and calculate methods |
| 85 | + * for unary operators, using the generic implementation. |
| 86 | + * |
| 87 | + * Usage: |
| 88 | + * namespace op::sqrt::cpu { |
| 89 | + * using Op = op::elementwise::unary::UnaryOp<UnaryMode::Sqrt>; |
| 90 | + * ELEMENTWISE_CPU_IMPL_UNARY(sqrt) |
| 91 | + * } |
| 92 | + */ |
| 93 | +#define ELEMENTWISE_CPU_IMPL_UNARY(OP) \ |
| 94 | + \ |
| 95 | + Descriptor::~Descriptor() = default; \ |
| 96 | + \ |
| 97 | + infiniStatus_t Descriptor::create( \ |
| 98 | + infiniopHandle_t handle_, \ |
| 99 | + Descriptor **desc_ptr, \ |
| 100 | + infiniopTensorDescriptor_t out_desc, \ |
| 101 | + std::vector<infiniopTensorDescriptor_t> input_desc_vec) { \ |
| 102 | + auto handle = reinterpret_cast<device::cpu::Handle *>(handle_); \ |
| 103 | + auto dtype = out_desc->dtype(); \ |
| 104 | + const auto &x_desc = input_desc_vec.at(0); \ |
| 105 | + const auto &y_shape = out_desc->shape(); \ |
| 106 | + const auto &x_shape = x_desc->shape(); \ |
| 107 | + CHECK_DTYPE(dtype, INFINI_DTYPE_F16, INFINI_DTYPE_F32); \ |
| 108 | + CHECK_SAME_SHAPE(y_shape, x_shape); \ |
| 109 | + CREATE_ELEMENTWISE_CPU_DESCRIPTOR(handle, dtype, out_desc, input_desc_vec); \ |
| 110 | + return INFINI_STATUS_SUCCESS; \ |
| 111 | + } \ |
| 112 | + \ |
| 113 | + infiniStatus_t Descriptor::calculate( \ |
| 114 | + void *workspace, \ |
| 115 | + size_t workspace_size, \ |
| 116 | + void *output, \ |
| 117 | + std::vector<const void *> inputs, \ |
| 118 | + void *stream) const { \ |
| 119 | + switch (_dtype) { \ |
| 120 | + case INFINI_DTYPE_F16: \ |
| 121 | + return _device_info->template calculate<Op, fp16_t>( \ |
| 122 | + _info, output, inputs, stream); \ |
| 123 | + case INFINI_DTYPE_F32: \ |
| 124 | + return _device_info->template calculate<Op, float>( \ |
| 125 | + _info, output, inputs, stream); \ |
| 126 | + default: \ |
| 127 | + return INFINI_STATUS_BAD_TENSOR_DTYPE; \ |
| 128 | + } \ |
| 129 | + } |
| 130 | + |
| 131 | +#endif // __INFINIOP_ELEMENTWISE_CPU_IMPL_H__ |
0 commit comments