|
| 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/kernels/portable/cpu/vec_ops.h> |
| 10 | +#include <executorch/runtime/kernel/kernel_includes.h> |
| 11 | + |
| 12 | +namespace torch { |
| 13 | +namespace executor { |
| 14 | +namespace native { |
| 15 | + |
| 16 | +using Tensor = exec_aten::Tensor; |
| 17 | + |
| 18 | +bool check_quantized_mixed_linear_args( |
| 19 | + const Tensor& in, |
| 20 | + const Tensor& weight, |
| 21 | + const Tensor& weight_scales, |
| 22 | + const optional<Tensor>& opt_weight_zero_points, |
| 23 | + const optional<ScalarType> dtype, |
| 24 | + Tensor& out) { |
| 25 | + ET_LOG_AND_RETURN_IF_FALSE(tensor_is_rank(in, 2)); |
| 26 | + ET_LOG_AND_RETURN_IF_FALSE(tensor_is_rank(weight, 2)); |
| 27 | + ET_LOG_AND_RETURN_IF_FALSE(tensor_is_rank(weight_scales, 1)); |
| 28 | + ET_LOG_AND_RETURN_IF_FALSE(tensor_is_rank(out, 2)); |
| 29 | + |
| 30 | + ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_size_at_dims(in, 1, weight, 1)); |
| 31 | + ET_LOG_AND_RETURN_IF_FALSE( |
| 32 | + tensors_have_same_size_at_dims(weight_scales, 0, weight, 0)); |
| 33 | + ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_size_at_dims(in, 1, weight, 1)); |
| 34 | + |
| 35 | + ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, weight_scales)); |
| 36 | + if (dtype.has_value()) { |
| 37 | + ET_LOG_AND_RETURN_IF_FALSE(out.scalar_type() == dtype.value()); |
| 38 | + ET_LOG_MSG_AND_RETURN_IF_FALSE( |
| 39 | + dtype.value() == ScalarType::Float || dtype.value() == ScalarType::Half, |
| 40 | + "dtype must be Float or Half"); |
| 41 | + } |
| 42 | + ET_LOG_MSG_AND_RETURN_IF_FALSE( |
| 43 | + weight.scalar_type() == ScalarType::Char, "weight dtype must be int8"); |
| 44 | + ET_LOG_MSG_AND_RETURN_IF_FALSE( |
| 45 | + in.scalar_type() == ScalarType::Float || |
| 46 | + in.scalar_type() == ScalarType::Half, |
| 47 | + "input dtype must be Float or Half"); |
| 48 | + |
| 49 | + if (opt_weight_zero_points.has_value()) { |
| 50 | + ET_LOG_AND_RETURN_IF_FALSE( |
| 51 | + tensors_have_same_shape(opt_weight_zero_points.value(), weight_scales)); |
| 52 | + ET_LOG_AND_RETURN_IF_FALSE( |
| 53 | + tensors_have_same_dtype(opt_weight_zero_points.value(), in)); |
| 54 | + } |
| 55 | + |
| 56 | + // Support for non-null zero points is not implemented yet. |
| 57 | + ET_LOG_MSG_AND_RETURN_IF_FALSE( |
| 58 | + !opt_weight_zero_points.has_value(), "zero points not supported yet."); |
| 59 | + return true; |
| 60 | +} |
| 61 | + |
| 62 | +Tensor& quantized_mixed_linear_out( |
| 63 | + const Tensor& in, |
| 64 | + const Tensor& weight, |
| 65 | + const Tensor& weight_scales, |
| 66 | + const optional<Tensor>& opt_weight_zero_points, |
| 67 | + const optional<ScalarType> dtype, |
| 68 | + Tensor& out) { |
| 69 | + ET_KERNEL_CHECK( |
| 70 | + ctx, |
| 71 | + check_quantized_mixed_linear_args( |
| 72 | + in, weight, weight_scales, opt_weight_zero_points, dtype, out), |
| 73 | + InvalidArgument, |
| 74 | + out); |
| 75 | + |
| 76 | + ScalarType out_dtype = dtype.has_value() ? dtype.value() : out.scalar_type(); |
| 77 | + |
| 78 | + size_t output_ndim = 2; |
| 79 | + exec_aten::SizesType output_sizes[kTensorDimensionLimit]; |
| 80 | + output_sizes[0] = in.size(0); |
| 81 | + output_sizes[1] = weight.size(0); |
| 82 | + |
| 83 | + ET_KERNEL_CHECK( |
| 84 | + ctx, |
| 85 | + resize_tensor(out, {output_sizes, output_ndim}) == Error::Ok, |
| 86 | + InvalidArgument, |
| 87 | + out); |
| 88 | + |
| 89 | + constexpr auto name = "quantized_decomposed::mixed_linear.out"; |
| 90 | + |
| 91 | + ET_SWITCH_TWO_TYPES(Float, Half, in.scalar_type(), ctx, name, CTYPE, [&]() { |
| 92 | + ET_SWITCH_FLOAT_TYPES_AND(Half, out_dtype, ctx, name, CTYPE_OUT, [&]() { |
| 93 | + size_t m = in.size(0); |
| 94 | + size_t n = in.size(1); |
| 95 | + size_t p = weight.size(0); |
| 96 | + size_t g = n; |
| 97 | + |
| 98 | + if (weight_scales.dim() == 2) { |
| 99 | + g = (n + weight_scales.size(1) - 1) / weight_scales.size(1); |
| 100 | + }; |
| 101 | + |
| 102 | + // FIXME: this currently ignores dtype |
| 103 | + vec_quantized_matmul_transb_int8< |
| 104 | + CTYPE_OUT, // T *z |
| 105 | + CTYPE>( // U *x, U *s |
| 106 | + out.mutable_data_ptr<CTYPE_OUT>(), |
| 107 | + in.const_data_ptr<CTYPE>(), |
| 108 | + weight.const_data_ptr<int8_t>(), |
| 109 | + weight_scales.const_data_ptr<CTYPE>(), |
| 110 | + m, |
| 111 | + n, |
| 112 | + p, |
| 113 | + g); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + return out; |
| 118 | +} |
| 119 | + |
| 120 | +Tensor& quantized_mixed_linear_out( |
| 121 | + RuntimeContext& ctx, |
| 122 | + const Tensor& in, |
| 123 | + const Tensor& weight, |
| 124 | + const Tensor& weight_scales, |
| 125 | + const optional<Tensor>& opt_weight_zero_points, |
| 126 | + const optional<ScalarType> dtype, |
| 127 | + Tensor& out) { |
| 128 | + // TODO(mcandales): Remove the need for this wrapper |
| 129 | + // TODO(mkg): add support for dtype |
| 130 | + (void)ctx; |
| 131 | + return quantized_mixed_linear_out( |
| 132 | + in, weight, weight_scales, opt_weight_zero_points, dtype, out); |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace native |
| 136 | +} // namespace executor |
| 137 | +} // namespace torch |
0 commit comments