|
32 | 32 | /** |
33 | 33 | * @brief Common Calculate Switch Cases (F16 & F32) |
34 | 34 | */ |
35 | | -#define _IMPL_CALC_CASES_COMMON \ |
36 | | - case INFINI_DTYPE_F16: \ |
| 35 | +#define _IMPL_CALC_CASES_COMMON \ |
| 36 | + case INFINI_DTYPE_F16: \ |
37 | 37 | return _device_info->template calculate<Op, fp16_t>(_info, output, inputs, stream); \ |
38 | | - case INFINI_DTYPE_F32: \ |
| 38 | + case INFINI_DTYPE_F32: \ |
39 | 39 | return _device_info->template calculate<Op, float>(_info, output, inputs, stream); |
40 | 40 |
|
41 | 41 | /** |
42 | 42 | * @brief Extended Calculate Switch Cases (Adds F64 & BF16) |
43 | 43 | */ |
44 | | -#define _IMPL_CALC_CASES_EXTENDED \ |
45 | | - _IMPL_CALC_CASES_COMMON \ |
46 | | - case INFINI_DTYPE_F64: \ |
| 44 | +#define _IMPL_CALC_CASES_EXTENDED \ |
| 45 | + _IMPL_CALC_CASES_COMMON \ |
| 46 | + case INFINI_DTYPE_F64: \ |
47 | 47 | return _device_info->template calculate<Op, double>(_info, output, inputs, stream); \ |
48 | | - case INFINI_DTYPE_BF16: \ |
| 48 | + case INFINI_DTYPE_BF16: \ |
49 | 49 | return _device_info->template calculate<Op, bf16_t>(_info, output, inputs, stream); |
50 | 50 |
|
| 51 | +/** |
| 52 | + * @brief Integral Calculate Switch Cases (I32, I64, U8) |
| 53 | + * For bitwise operations that only support integral types |
| 54 | + */ |
| 55 | +#define _IMPL_CALC_CASES_INTEGRAL \ |
| 56 | + case INFINI_DTYPE_I32: \ |
| 57 | + return _device_info->template calculate<Op, int32_t>(_info, output, inputs, stream); \ |
| 58 | + case INFINI_DTYPE_I64: \ |
| 59 | + return _device_info->template calculate<Op, int64_t>(_info, output, inputs, stream); \ |
| 60 | + case INFINI_DTYPE_U8: \ |
| 61 | + return _device_info->template calculate<Op, uint8_t>(_info, output, inputs, stream); |
| 62 | + |
51 | 63 | /** |
52 | 64 | * @brief Generic Template for the Calculate method |
53 | 65 | * @param CASES_MACRO The macro containing the switch cases to use |
54 | 66 | */ |
55 | | -#define _IMPL_CALCULATE_METHOD(CASES_MACRO) \ |
56 | | - infiniStatus_t Descriptor::calculate( \ |
57 | | - void *workspace, \ |
58 | | - size_t workspace_size, \ |
59 | | - void *output, \ |
60 | | - std::vector<const void *> inputs, \ |
61 | | - void *stream) const { \ |
62 | | - switch (_dtype) { \ |
63 | | - CASES_MACRO \ |
64 | | - default: \ |
65 | | - return INFINI_STATUS_BAD_TENSOR_DTYPE; \ |
66 | | - } \ |
| 67 | +#define _IMPL_CALCULATE_METHOD(CASES_MACRO) \ |
| 68 | + infiniStatus_t Descriptor::calculate( \ |
| 69 | + void *workspace, \ |
| 70 | + size_t workspace_size, \ |
| 71 | + void *output, \ |
| 72 | + std::vector<const void *> inputs, \ |
| 73 | + void *stream) const { \ |
| 74 | + switch (_dtype) { \ |
| 75 | + CASES_MACRO \ |
| 76 | + default: \ |
| 77 | + return INFINI_STATUS_BAD_TENSOR_DTYPE; \ |
| 78 | + } \ |
67 | 79 | } |
68 | 80 |
|
69 | 81 | /** |
70 | 82 | * @brief Generic Template for the Create method |
71 | 83 | * @param SHAPE_CHECK_BLOCK Code block to execute for shape checking |
72 | 84 | * @param ... Variadic arguments for allowed data types in CHECK_DTYPE |
73 | 85 | */ |
74 | | -#define _IMPL_CREATE_METHOD(SHAPE_CHECK_BLOCK, ...) \ |
75 | | - Descriptor::~Descriptor() = default; \ |
76 | | - infiniStatus_t Descriptor::create( \ |
77 | | - infiniopHandle_t handle_, \ |
78 | | - Descriptor **desc_ptr, \ |
79 | | - infiniopTensorDescriptor_t out_desc, \ |
80 | | - std::vector<infiniopTensorDescriptor_t> input_desc_vec) { \ |
81 | | - auto handle = reinterpret_cast<device::cpu::Handle *>(handle_); \ |
82 | | - auto dtype = out_desc->dtype(); \ |
83 | | - const auto &out_shape = out_desc->shape(); \ |
84 | | - SHAPE_CHECK_BLOCK \ |
85 | | - CHECK_DTYPE(dtype, __VA_ARGS__); \ |
| 86 | +#define _IMPL_CREATE_METHOD(SHAPE_CHECK_BLOCK, ...) \ |
| 87 | + Descriptor::~Descriptor() = default; \ |
| 88 | + infiniStatus_t Descriptor::create( \ |
| 89 | + infiniopHandle_t handle_, \ |
| 90 | + Descriptor **desc_ptr, \ |
| 91 | + infiniopTensorDescriptor_t out_desc, \ |
| 92 | + std::vector<infiniopTensorDescriptor_t> input_desc_vec) { \ |
| 93 | + auto handle = reinterpret_cast<device::cpu::Handle *>(handle_); \ |
| 94 | + auto dtype = out_desc->dtype(); \ |
| 95 | + const auto &out_shape = out_desc->shape(); \ |
| 96 | + SHAPE_CHECK_BLOCK \ |
| 97 | + CHECK_DTYPE(dtype, __VA_ARGS__); \ |
86 | 98 | CREATE_ELEMENTWISE_CPU_DESCRIPTOR(handle, dtype, out_desc, input_desc_vec); \ |
87 | | - return INFINI_STATUS_SUCCESS; \ |
| 99 | + return INFINI_STATUS_SUCCESS; \ |
88 | 100 | } |
89 | 101 |
|
90 | 102 | // ========================================================================= |
|
103 | 115 | * ELEMENTWISE_CPU_IMPL_BINARY(pow) |
104 | 116 | * } |
105 | 117 | */ |
106 | | -#define ELEMENTWISE_CPU_IMPL_BINARY(OP) \ |
107 | | - _IMPL_CREATE_METHOD( \ |
108 | | - const auto &a_desc = input_desc_vec.at(0); \ |
109 | | - const auto &b_desc = input_desc_vec.at(1); \ |
110 | | - const auto &a_shape = a_desc->shape(); \ |
111 | | - const auto &b_shape = b_desc->shape(); \ |
112 | | - CHECK_SAME_SHAPE(out_shape, a_shape, b_shape);, \ |
113 | | - INFINI_DTYPE_F16, INFINI_DTYPE_F32 \ |
114 | | - ) \ |
| 118 | +#define ELEMENTWISE_CPU_IMPL_BINARY(OP) \ |
| 119 | + _IMPL_CREATE_METHOD( \ |
| 120 | + const auto &a_desc = input_desc_vec.at(0); \ |
| 121 | + const auto &b_desc = input_desc_vec.at(1); \ |
| 122 | + const auto &a_shape = a_desc->shape(); \ |
| 123 | + const auto &b_shape = b_desc->shape(); \ |
| 124 | + CHECK_SAME_SHAPE(out_shape, a_shape, b_shape);, \ |
| 125 | + INFINI_DTYPE_F16, INFINI_DTYPE_F32) \ |
115 | 126 | _IMPL_CALCULATE_METHOD(_IMPL_CALC_CASES_COMMON) |
116 | 127 |
|
117 | 128 | /** |
|
126 | 137 | * ELEMENTWISE_CPU_IMPL_UNARY(sqrt) |
127 | 138 | * } |
128 | 139 | */ |
129 | | -#define ELEMENTWISE_CPU_IMPL_UNARY(OP) \ |
130 | | - _IMPL_CREATE_METHOD( \ |
131 | | - const auto &x_desc = input_desc_vec.at(0); \ |
132 | | - const auto &x_shape = x_desc->shape(); \ |
133 | | - CHECK_SAME_SHAPE(out_shape, x_shape);, \ |
134 | | - INFINI_DTYPE_F16, INFINI_DTYPE_F32 \ |
135 | | - ) \ |
| 140 | +#define ELEMENTWISE_CPU_IMPL_UNARY(OP) \ |
| 141 | + _IMPL_CREATE_METHOD( \ |
| 142 | + const auto &x_desc = input_desc_vec.at(0); \ |
| 143 | + const auto &x_shape = x_desc->shape(); \ |
| 144 | + CHECK_SAME_SHAPE(out_shape, x_shape);, \ |
| 145 | + INFINI_DTYPE_F16, INFINI_DTYPE_F32) \ |
136 | 146 | _IMPL_CALCULATE_METHOD(_IMPL_CALC_CASES_COMMON) |
137 | 147 |
|
138 | 148 | /** |
|
147 | 157 | * ELEMENTWISE_CPU_IMPL_UNARY_EXTENDED(exp) |
148 | 158 | * } |
149 | 159 | */ |
150 | | -#define ELEMENTWISE_CPU_IMPL_UNARY_EXTENDED(OP) \ |
151 | | - _IMPL_CREATE_METHOD( \ |
152 | | - const auto &x_desc = input_desc_vec.at(0); \ |
153 | | - const auto &x_shape = x_desc->shape(); \ |
154 | | - CHECK_SAME_SHAPE(out_shape, x_shape);, \ |
155 | | - INFINI_DTYPE_F16, INFINI_DTYPE_F32, INFINI_DTYPE_F64, INFINI_DTYPE_BF16 \ |
156 | | - ) \ |
| 160 | +#define ELEMENTWISE_CPU_IMPL_UNARY_EXTENDED(OP) \ |
| 161 | + _IMPL_CREATE_METHOD( \ |
| 162 | + const auto &x_desc = input_desc_vec.at(0); \ |
| 163 | + const auto &x_shape = x_desc->shape(); \ |
| 164 | + CHECK_SAME_SHAPE(out_shape, x_shape);, \ |
| 165 | + INFINI_DTYPE_F16, INFINI_DTYPE_F32, INFINI_DTYPE_F64, INFINI_DTYPE_BF16) \ |
157 | 166 | _IMPL_CALCULATE_METHOD(_IMPL_CALC_CASES_EXTENDED) |
158 | 167 |
|
| 168 | +/** |
| 169 | + * @brief Implementation for Binary Operators with Integral Types (I32, I64, U8) |
| 170 | + * |
| 171 | + * This macro generates the Descriptor destructor, create, and calculate methods |
| 172 | + * for binary operators that only support integral types (e.g., bitwise operations). |
| 173 | + * |
| 174 | + * Usage: |
| 175 | + * namespace op::bitwise_and::cpu { |
| 176 | + * using Op = op::elementwise::binary::BinaryOp<BinaryMode::BitwiseAnd>; |
| 177 | + * ELEMENTWISE_CPU_IMPL_BINARY_INTEGRAL(bitwise_and) |
| 178 | + * } |
| 179 | + */ |
| 180 | +#define ELEMENTWISE_CPU_IMPL_BINARY_INTEGRAL(OP) \ |
| 181 | + _IMPL_CREATE_METHOD( \ |
| 182 | + const auto &a_desc = input_desc_vec.at(0); \ |
| 183 | + const auto &b_desc = input_desc_vec.at(1); \ |
| 184 | + const auto &a_shape = a_desc->shape(); \ |
| 185 | + const auto &b_shape = b_desc->shape(); \ |
| 186 | + CHECK_SAME_SHAPE(out_shape, a_shape, b_shape);, \ |
| 187 | + INFINI_DTYPE_I32, INFINI_DTYPE_I64, INFINI_DTYPE_U8) \ |
| 188 | + _IMPL_CALCULATE_METHOD(_IMPL_CALC_CASES_INTEGRAL) |
| 189 | + |
159 | 190 | #endif // __INFINIOP_ELEMENTWISE_CPU_IMPL_H__ |
0 commit comments