|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/backends/cadence/generic/operators/op_quantized_add.h> |
| 10 | + |
| 11 | +#include <executorch/backends/cadence/generic/kernels/kernels.h> |
| 12 | +#include <executorch/backends/cadence/generic/operators/quantized_op_macros.h> |
| 13 | +#include <executorch/kernels/portable/cpu/scalar_utils.h> |
| 14 | +#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> |
| 15 | + |
| 16 | +namespace impl::generic::native { |
| 17 | + |
| 18 | +using ::executorch::aten::Scalar; |
| 19 | +using ::executorch::aten::ScalarType; |
| 20 | +using ::executorch::aten::Tensor; |
| 21 | +using ::executorch::runtime::KernelRuntimeContext; |
| 22 | +using ::impl::generic::kernels::dequantize; |
| 23 | +using ::impl::generic::kernels::quantize; |
| 24 | + |
| 25 | +DECLARE_POINTWISE_TENSOR_QUANTIZED_BINARY_OP(quantized_add_, +); |
| 26 | + |
| 27 | +#define DECLARE_POINTWISE_SCALAR_QUANTIZED_BINARY_OP(BINARY_FUNC_NAME, OP) \ |
| 28 | + template <typename T> \ |
| 29 | + void BINARY_FUNC_NAME( \ |
| 30 | + const Tensor& X, \ |
| 31 | + float X_scale, \ |
| 32 | + int32_t X_zero_point, \ |
| 33 | + const float Y, \ |
| 34 | + float out_scale, \ |
| 35 | + int32_t out_zero_point, \ |
| 36 | + Tensor& out) { \ |
| 37 | + const T* __restrict__ X_data = X.const_data_ptr<T>(); \ |
| 38 | + T* __restrict__ out_data = out.mutable_data_ptr<T>(); \ |
| 39 | + float inv_out_scale = 1.0f / out_scale; \ |
| 40 | + for (size_t i = 0, e = X.numel(); i < e; ++i) { \ |
| 41 | + float x = dequantize<T>(X_data[i], X_scale, X_zero_point); \ |
| 42 | + float z = x OP Y; \ |
| 43 | + out_data[i] = quantize<T>(z, inv_out_scale, out_zero_point); \ |
| 44 | + } \ |
| 45 | + } |
| 46 | + |
| 47 | +DECLARE_POINTWISE_SCALAR_QUANTIZED_BINARY_OP(quantized_add_Scalar_, +); |
| 48 | + |
| 49 | +Tensor& quantized_add_out( |
| 50 | + ET_UNUSED KernelRuntimeContext& ctx, |
| 51 | + const Tensor& X, |
| 52 | + const Tensor& X_scale_t, |
| 53 | + const Tensor& X_zero_point_t, |
| 54 | + const Tensor& Y, |
| 55 | + const Tensor& Y_scale_t, |
| 56 | + const Tensor& Y_zero_point_t, |
| 57 | + double out_scale, |
| 58 | + int64_t out_zero_point, |
| 59 | + Tensor& out) { |
| 60 | + float X_scale = X_scale_t.const_data_ptr<float>()[0]; |
| 61 | + int32_t X_zero_point = X_zero_point_t.const_data_ptr<int32_t>()[0]; |
| 62 | + float Y_scale = Y_scale_t.const_data_ptr<float>()[0]; |
| 63 | + int32_t Y_zero_point = Y_zero_point_t.const_data_ptr<int32_t>()[0]; |
| 64 | + |
| 65 | +#define typed_quantized_add(ctype, dtype) \ |
| 66 | + case ScalarType::dtype: { \ |
| 67 | + quantized_add_<ctype>( \ |
| 68 | + X, \ |
| 69 | + X_scale, \ |
| 70 | + X_zero_point, \ |
| 71 | + Y, \ |
| 72 | + Y_scale, \ |
| 73 | + Y_zero_point, \ |
| 74 | + static_cast<float>(out_scale), \ |
| 75 | + static_cast<int32_t>(out_zero_point), \ |
| 76 | + out); \ |
| 77 | + break; \ |
| 78 | + } |
| 79 | + |
| 80 | + ScalarType dtype = out.scalar_type(); |
| 81 | + switch (dtype) { |
| 82 | + ET_FORALL_CADENCE_QUANTIZED_TYPES(typed_quantized_add); |
| 83 | + default: |
| 84 | + ET_DCHECK_MSG( |
| 85 | + false, "Unhandled dtype %s", torch::executor::toString(dtype)); |
| 86 | + } |
| 87 | +#undef typed_quantized_add |
| 88 | + |
| 89 | + return out; |
| 90 | +} |
| 91 | + |
| 92 | +Tensor& quantized_add_per_tensor_out( |
| 93 | + ET_UNUSED KernelRuntimeContext& ctx, |
| 94 | + const Tensor& X, |
| 95 | + double X_scale, |
| 96 | + int64_t X_zero_point, |
| 97 | + const Tensor& Y, |
| 98 | + double Y_scale, |
| 99 | + int64_t Y_zero_point, |
| 100 | + double out_scale, |
| 101 | + int64_t out_zero_point, |
| 102 | + Tensor& out) { |
| 103 | +#define typed_quantized_add(ctype, dtype) \ |
| 104 | + case ScalarType::dtype: { \ |
| 105 | + quantized_add_<ctype>( \ |
| 106 | + X, \ |
| 107 | + static_cast<float>(X_scale), \ |
| 108 | + static_cast<int32_t>(X_zero_point), \ |
| 109 | + Y, \ |
| 110 | + static_cast<float>(Y_scale), \ |
| 111 | + static_cast<int32_t>(Y_zero_point), \ |
| 112 | + static_cast<float>(out_scale), \ |
| 113 | + static_cast<int32_t>(out_zero_point), \ |
| 114 | + out); \ |
| 115 | + break; \ |
| 116 | + } |
| 117 | + |
| 118 | + ScalarType dtype = out.scalar_type(); |
| 119 | + switch (dtype) { |
| 120 | + ET_FORALL_CADENCE_QUANTIZED_TYPES(typed_quantized_add); |
| 121 | + default: |
| 122 | + ET_DCHECK_MSG( |
| 123 | + false, "Unhandled dtype %s", torch::executor::toString(dtype)); |
| 124 | + } |
| 125 | +#undef typed_quantized_add |
| 126 | + return out; |
| 127 | +} |
| 128 | + |
| 129 | +Tensor& quantized_add_asym8sxasym8s_asym8s_per_tensor_out( |
| 130 | + ET_UNUSED KernelRuntimeContext& ctx, |
| 131 | + const Tensor& X, |
| 132 | + double X_scale, |
| 133 | + int64_t X_zero_point, |
| 134 | + const Tensor& Y, |
| 135 | + double Y_scale, |
| 136 | + int64_t Y_zero_point, |
| 137 | + double out_scale, |
| 138 | + int64_t out_zero_point, |
| 139 | + Tensor& out) { |
| 140 | + quantized_add_<int8_t>( |
| 141 | + X, |
| 142 | + static_cast<float>(X_scale), |
| 143 | + static_cast<int32_t>(X_zero_point), |
| 144 | + Y, |
| 145 | + static_cast<float>(Y_scale), |
| 146 | + static_cast<int32_t>(Y_zero_point), |
| 147 | + static_cast<float>(out_scale), |
| 148 | + static_cast<int32_t>(out_zero_point), |
| 149 | + out); |
| 150 | + return out; |
| 151 | +} |
| 152 | + |
| 153 | +Tensor& quantized_add_asym8uxasym8u_asym8u_per_tensor_out( |
| 154 | + ET_UNUSED KernelRuntimeContext& ctx, |
| 155 | + const Tensor& X, |
| 156 | + double X_scale, |
| 157 | + int64_t X_zero_point, |
| 158 | + const Tensor& Y, |
| 159 | + double Y_scale, |
| 160 | + int64_t Y_zero_point, |
| 161 | + double out_scale, |
| 162 | + int64_t out_zero_point, |
| 163 | + Tensor& out) { |
| 164 | + quantized_add_<uint8_t>( |
| 165 | + X, |
| 166 | + static_cast<float>(X_scale), |
| 167 | + static_cast<int32_t>(X_zero_point), |
| 168 | + Y, |
| 169 | + static_cast<float>(Y_scale), |
| 170 | + static_cast<int32_t>(Y_zero_point), |
| 171 | + static_cast<float>(out_scale), |
| 172 | + static_cast<int32_t>(out_zero_point), |
| 173 | + out); |
| 174 | + return out; |
| 175 | +} |
| 176 | + |
| 177 | +Tensor& quantized_add_Scalar_out( |
| 178 | + ET_UNUSED KernelRuntimeContext& ctx, |
| 179 | + const Tensor& X, |
| 180 | + const Tensor& X_scale_t, |
| 181 | + const Tensor& X_zero_point_t, |
| 182 | + const Scalar& Y_scalar, |
| 183 | + double out_scale, |
| 184 | + int64_t out_zero_point, |
| 185 | + Tensor& out) { |
| 186 | + float X_scale = X_scale_t.const_data_ptr<float>()[0]; |
| 187 | + int32_t X_zero_point = X_zero_point_t.const_data_ptr<int32_t>()[0]; |
| 188 | + float Y = static_cast<float>( |
| 189 | + ::torch::executor::native::utils::scalar_to<double>(Y_scalar)); |
| 190 | +#define typed_quantized_add_Scalar(ctype, dtype) \ |
| 191 | + case ScalarType::dtype: { \ |
| 192 | + quantized_add_Scalar_<ctype>( \ |
| 193 | + X, \ |
| 194 | + X_scale, \ |
| 195 | + X_zero_point, \ |
| 196 | + Y, \ |
| 197 | + static_cast<float>(out_scale), \ |
| 198 | + static_cast<int32_t>(out_zero_point), \ |
| 199 | + out); \ |
| 200 | + break; \ |
| 201 | + } |
| 202 | + |
| 203 | + ScalarType dtype = out.scalar_type(); |
| 204 | + switch (dtype) { |
| 205 | + ET_FORALL_CADENCE_QUANTIZED_TYPES(typed_quantized_add_Scalar) |
| 206 | + default: |
| 207 | + ET_DCHECK_MSG( |
| 208 | + false, "Unhandled dtype %s", torch::executor::toString(dtype)); |
| 209 | + } |
| 210 | +#undef typed_quantized_add_Scalar |
| 211 | + return out; |
| 212 | +} |
| 213 | + |
| 214 | +#undef DECLARE_POINTWISE_SCALAR_QUANTIZED_BINARY_OP |
| 215 | + |
| 216 | +} // namespace impl::generic::native |
0 commit comments