|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This source code is licensed under the license found in the |
| 5 | +// LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <cstdint> |
| 10 | + |
| 11 | +namespace torchao::kernels::cpu::fallback::quantized_matmul { |
| 12 | +namespace channelwise_8bit_a_channelwise_8bit_b::internal { |
| 13 | + |
| 14 | +template < |
| 15 | + bool a_has_zeros, |
| 16 | + bool b_has_zeros, |
| 17 | + bool a_transposed, |
| 18 | + bool b_tranposed> |
| 19 | +struct KernelImpl { |
| 20 | + static void run( |
| 21 | + int m, |
| 22 | + int n, |
| 23 | + int k, |
| 24 | + const void* lhs, |
| 25 | + int lhs_stride_m, |
| 26 | + const void* rhs, |
| 27 | + int rhs_stride_n, |
| 28 | + float* output, |
| 29 | + int out_stride_m, |
| 30 | + const int8_t* lhs_zero_points, |
| 31 | + const int8_t* rhs_zero_points, |
| 32 | + const float* lhs_scales, |
| 33 | + const float* rhs_scales, |
| 34 | + const int lhs_qparams_stride, |
| 35 | + const int rhs_qparams_stride); |
| 36 | +}; |
| 37 | + |
| 38 | +template <bool b_transposed> |
| 39 | +struct KernelImpl<true, true, false, b_transposed> { |
| 40 | + static void run( |
| 41 | + int m, |
| 42 | + int n, |
| 43 | + int k, |
| 44 | + const void* lhs, |
| 45 | + int lhs_stride_m, |
| 46 | + const void* rhs, |
| 47 | + int rhs_stride_n, |
| 48 | + float* output, |
| 49 | + int out_stride_m, |
| 50 | + const int8_t* lhs_zero_points, |
| 51 | + const int8_t* rhs_zero_points, |
| 52 | + const float* lhs_scales, |
| 53 | + const float* rhs_scales, |
| 54 | + const int lhs_qparams_stride, |
| 55 | + const int rhs_qparams_stride) { |
| 56 | + const int8_t* lhs_qvals = static_cast<const int8_t*>(lhs); |
| 57 | + const int8_t* rhs_qvals = static_cast<const int8_t*>(rhs); |
| 58 | + for (int m_idx = 0; m_idx < m; m_idx++) { |
| 59 | + for (int n_idx = 0; n_idx < n; n_idx++) { |
| 60 | + float res = 0.0; |
| 61 | + for (int k_idx = 0; k_idx < k; k_idx++) { |
| 62 | + int lhs_idx = m_idx * lhs_stride_m + k_idx; |
| 63 | + int rhs_idx = k_idx * rhs_stride_n + n_idx; |
| 64 | + if (b_transposed) { |
| 65 | + rhs_idx = n_idx * rhs_stride_n + k_idx; |
| 66 | + } |
| 67 | + |
| 68 | + float lhs_dequant = lhs_scales[m_idx * lhs_qparams_stride] * |
| 69 | + (static_cast<int16_t>(lhs_qvals[lhs_idx]) - |
| 70 | + static_cast<int16_t>( |
| 71 | + lhs_zero_points[m_idx * lhs_qparams_stride])); |
| 72 | + |
| 73 | + float rhs_dequant = rhs_scales[n_idx * rhs_qparams_stride] * |
| 74 | + (static_cast<int16_t>(rhs_qvals[rhs_idx]) - |
| 75 | + static_cast<int16_t>( |
| 76 | + rhs_zero_points[n_idx * rhs_qparams_stride])); |
| 77 | + |
| 78 | + res += lhs_dequant * rhs_dequant; |
| 79 | + } |
| 80 | + output[m_idx * n + n_idx] = res; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +} // namespace |
| 87 | + // channelwise_8bit_a_channelwise_8bit_b::internal |
| 88 | +} // namespace torchao::kernels::cpu::fallback::quantized_matmul |
| 89 | + |
| 90 | +// TODO: Remove all ::kernels. No need for extra namespace. |
| 91 | +namespace torchao::kernels::cpu::fallback::quantized_matmul { |
| 92 | +namespace channelwise_8bit_a_channelwise_8bit_b { |
| 93 | +template < |
| 94 | + bool a_has_zeros, |
| 95 | + bool b_has_zeros, |
| 96 | + bool a_transposed, |
| 97 | + bool b_transposed> |
| 98 | +void kernel( |
| 99 | + int m, |
| 100 | + int n, |
| 101 | + int k, |
| 102 | + const void* lhs, |
| 103 | + int lhs_stride_m, |
| 104 | + const void* rhs, |
| 105 | + int rhs_stride_n, |
| 106 | + float* output, |
| 107 | + int out_stride_m, |
| 108 | + const int8_t* lhs_zero_points, |
| 109 | + const int8_t* rhs_zero_points, |
| 110 | + const float* lhs_scales, |
| 111 | + const float* rhs_scales, |
| 112 | + const int lhs_qparams_stride, |
| 113 | + const int rhs_qparams_stride) { |
| 114 | + channelwise_8bit_a_channelwise_8bit_b::internal:: |
| 115 | + KernelImpl<a_has_zeros, b_has_zeros, a_transposed, b_transposed>::run( |
| 116 | + m, |
| 117 | + n, |
| 118 | + k, |
| 119 | + lhs, |
| 120 | + lhs_stride_m, |
| 121 | + rhs, |
| 122 | + rhs_stride_n, |
| 123 | + output, |
| 124 | + out_stride_m, |
| 125 | + lhs_zero_points, |
| 126 | + rhs_zero_points, |
| 127 | + lhs_scales, |
| 128 | + rhs_scales, |
| 129 | + lhs_qparams_stride, |
| 130 | + rhs_qparams_stride); |
| 131 | +} |
| 132 | +} // namespace channelwise_8bit_a_channelwise_8bit_b |
| 133 | +} // namespace torchao::kernels::cpu::fallback::quantized_matmul |
0 commit comments