kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com> | ||
| 3 | // | ||
| 4 | // SPDX-License-Identifier: Apache-2.0 | ||
| 5 | // | ||
| 6 | #if !defined(__aarch64__) && !defined(__ARM_FEATURE_DOTPROD) && !defined(_M_ARM64) | ||
| 7 | #error "Dotprod extension required to compile this micro-kernel" | ||
| 8 | #else // Architectural features check. | ||
| 9 | |||
| 10 | #include "kai_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod.h" | ||
| 11 | |||
| 12 | #include <stddef.h> | ||
| 13 | #include <stdint.h> | ||
| 14 | |||
| 15 | #include "kai/kai_common.h" | ||
| 16 | |||
| 17 | typedef struct { | ||
| 18 | float* dst; | ||
| 19 | const void* lhs_packed; | ||
| 20 | const void* rhs_packed; | ||
| 21 | const float* clamp_vals; | ||
| 22 | size_t dst_stride_row; | ||
| 23 | size_t m; | ||
| 24 | size_t n; | ||
| 25 | size_t num_blocks; | ||
| 26 | size_t num_subblocks; | ||
| 27 | } KernelArgs; | ||
| 28 | |||
| 29 | void kai_kernel_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod(KernelArgs* args_ptr); | ||
| 30 | |||
| 31 | // Compute args | ||
| 32 | static const size_t kai_m_step = 1; | ||
| 33 | static const size_t kai_n_step = 4; | ||
| 34 | // Packing args | ||
| 35 | static const size_t kai_mr = 1; | ||
| 36 | static const size_t kai_nr = 4; | ||
| 37 | static const size_t kai_kr = 8; | ||
| 38 | static const size_t kai_sr = 2; | ||
| 39 | // LHS format args (num. bytes per value, multiplier, zero_point (if asymmetric)) | ||
| 40 | static const size_t kai_num_bytes_qvalue_lhs = 1; | ||
| 41 | static const size_t kai_num_bytes_multiplier_lhs = 4; | ||
| 42 | static const size_t kai_num_bytes_zp_lhs = 4; | ||
| 43 | // RHS format args (num. bytes per value, multiplier, zero_point (if asymmetric), and reduction sum (if LHS is | ||
| 44 | // asymmetric)) | ||
| 45 | static const size_t kai_num_bytes_recip_qvalue_rhs = 2; | ||
| 46 | static const size_t kai_num_bytes_multiplier_rhs = 2; | ||
| 47 | static const size_t kai_num_bytes_rsum_rhs = 4; | ||
| 48 | // DST format args | ||
| 49 | static const size_t kai_num_bytes_dst_value = 4; | ||
| 50 | // Extra args | ||
| 51 | static const size_t kai_num_bytes_bias = 4; | ||
| 52 | static const size_t kai_k_multiple_of = 32; | ||
| 53 | static const size_t kai_bl = 32; | ||
| 54 | |||
| 55 | 216 | inline static size_t kai_get_k_roundedup(size_t k) { | |
| 56 | 216 | return kai_roundup(k, kai_k_multiple_of); | |
| 57 | } | ||
| 58 | |||
| 59 | 768 | inline static size_t kai_get_num_bytes_per_block_rhs(size_t bl) { | |
| 60 | − | KAI_ASSUME((bl % kai_bl) == 0); | |
| 61 | 768 | size_t num_bytes_per_block_rhs = (bl / kai_num_bytes_recip_qvalue_rhs) + kai_num_bytes_multiplier_rhs; | |
| 62 | 1536 | return num_bytes_per_block_rhs; | |
| 63 | 768 | } | |
| 64 | |||
| 65 | 990 | inline static size_t kai_get_num_blocks_per_row(size_t k, size_t bl) { | |
| 66 | − | KAI_ASSUME((bl % kai_bl) == 0); | |
| 67 | |||
| 68 | 990 | return kai_roundup(k, bl) / bl; | |
| 69 | } | ||
| 70 | |||
| 71 | 216 | inline static size_t kai_get_lhs_packed_stride(size_t k) { | |
| 72 | 216 | const size_t k_internal = kai_get_k_roundedup(k); | |
| 73 | 216 | size_t lhs_packed_stride = kai_mr * ((k_internal * kai_num_bytes_qvalue_lhs) + kai_num_bytes_multiplier_lhs); | |
| 74 | // Since the LHS matrix is asymmetric with per-row quantization, we must include the | ||
| 75 | // the number of bytes to hold the zero point value | ||
| 76 | 216 | lhs_packed_stride += kai_mr * kai_num_bytes_zp_lhs; | |
| 77 | |||
| 78 | 432 | return lhs_packed_stride; | |
| 79 | 216 | } | |
| 80 | |||
| 81 | 768 | inline static size_t kai_get_rhs_packed_stride(size_t k, size_t bl) { | |
| 82 | − | KAI_ASSUME((bl % kai_bl) == 0); | |
| 83 | |||
| 84 | 768 | const size_t num_blocks_per_row = kai_get_num_blocks_per_row(k, bl); | |
| 85 | 768 | const size_t num_bytes_per_block = kai_get_num_bytes_per_block_rhs(bl); | |
| 86 | |||
| 87 | 768 | size_t rhs_packed_stride = kai_nr * (num_bytes_per_block * num_blocks_per_row); | |
| 88 | // Since the LHS matrix is quantized asymmetric with per-row quantization, we also include | ||
| 89 | // the number of bytes for the reduction sum | ||
| 90 | 768 | rhs_packed_stride += kai_nr * kai_num_bytes_rsum_rhs; | |
| 91 | // Since the bias is packed with the RHS matrix, the stride is adjusted with the number of bytes of the bias | ||
| 92 | 768 | rhs_packed_stride += kai_nr * kai_num_bytes_bias; | |
| 93 | |||
| 94 | 1536 | return rhs_packed_stride; | |
| 95 | 768 | } | |
| 96 | |||
| 97 | 2120 | size_t kai_get_m_step_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod(void) { | |
| 98 | 2120 | return kai_m_step; | |
| 99 | } | ||
| 100 | |||
| 101 | 2488 | size_t kai_get_n_step_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod(void) { | |
| 102 | 2488 | return kai_n_step; | |
| 103 | } | ||
| 104 | |||
| 105 | 984 | size_t kai_get_mr_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod(void) { | |
| 106 | 984 | return kai_mr; | |
| 107 | } | ||
| 108 | |||
| 109 | 952 | size_t kai_get_nr_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod(void) { | |
| 110 | 952 | return kai_nr; | |
| 111 | } | ||
| 112 | |||
| 113 | 1168 | size_t kai_get_kr_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod(void) { | |
| 114 | 1168 | return kai_kr; | |
| 115 | } | ||
| 116 | |||
| 117 | 1168 | size_t kai_get_sr_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod(void) { | |
| 118 | 1168 | return kai_sr; | |
| 119 | } | ||
| 120 | |||
| 121 | 216 | size_t kai_get_lhs_packed_offset_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod(size_t m_idx, size_t k) { | |
| 122 | − | KAI_ASSUME((m_idx % kai_m_step) == 0); | |
| 123 | |||
| 124 | 216 | return (m_idx / kai_mr) * kai_get_lhs_packed_stride(k); | |
| 125 | } | ||
| 126 | |||
| 127 | 768 | size_t kai_get_rhs_packed_offset_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod( | |
| 128 | size_t n_idx, size_t k, size_t bl) { | ||
| 129 | − | KAI_ASSUME((k % bl) == 0); | |
| 130 | − | KAI_ASSUME((n_idx % kai_n_step) == 0); | |
| 131 | |||
| 132 | 768 | return (n_idx / kai_nr) * kai_get_rhs_packed_stride(k, bl); | |
| 133 | } | ||
| 134 | |||
| 135 | 952 | size_t kai_get_dst_offset_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod( | |
| 136 | size_t m_idx, size_t n_idx, size_t dst_stride) { | ||
| 137 | − | KAI_ASSUME((m_idx % kai_m_step) == 0); | |
| 138 | − | KAI_ASSUME((n_idx % kai_n_step) == 0); | |
| 139 | |||
| 140 | 952 | return (n_idx * kai_num_bytes_dst_value) + m_idx * dst_stride; | |
| 141 | } | ||
| 142 | |||
| 143 | 216 | size_t kai_get_dst_size_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod(size_t m, size_t n) { | |
| 144 | 216 | return m * n * kai_num_bytes_dst_value; | |
| 145 | } | ||
| 146 | |||
| 147 | 222 | void kai_run_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod( | |
| 148 | size_t m, // | ||
| 149 | size_t n, // | ||
| 150 | size_t k, // | ||
| 151 | size_t bl, // | ||
| 152 | const void* restrict lhs_packed, // | ||
| 153 | const void* restrict rhs_packed, // | ||
| 154 | float* restrict dst, // NOLINT(readability-non-const-parameter) | ||
| 155 | size_t dst_stride_row, // | ||
| 156 | size_t dst_stride_col, // | ||
| 157 | float scalar_min, // | ||
| 158 | float scalar_max) { | ||
| 159 | − | KAI_ASSUME(dst_stride_col == sizeof(float)); | |
| 160 | − | KAI_ASSUME((k % bl) == 0); | |
| 161 | − | KAI_ASSUME((bl % kai_bl) == 0); | |
| 162 | |||
| 163 |
1/2✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
|
222 | if (m == 0) { |
| 164 | ✗ | return; | |
| 165 | } | ||
| 166 | 222 | const size_t num_subblocks = bl / kai_bl; | |
| 167 | 222 | const size_t num_blocks = kai_get_num_blocks_per_row(k, bl); | |
| 168 | 222 | const float clamp_vals[2] = {scalar_min, scalar_max}; | |
| 169 | |||
| 170 | 222 | KernelArgs args; | |
| 171 | |||
| 172 | 222 | args.dst = dst; | |
| 173 | 222 | args.lhs_packed = lhs_packed; | |
| 174 | 222 | args.rhs_packed = rhs_packed; | |
| 175 | 222 | args.clamp_vals = clamp_vals; | |
| 176 | 222 | args.dst_stride_row = dst_stride_row; | |
| 177 | 222 | args.m = m; | |
| 178 | 222 | args.n = n; | |
| 179 | 222 | args.num_blocks = num_blocks; | |
| 180 | 222 | args.num_subblocks = num_subblocks; | |
| 181 | |||
| 182 | 222 | kai_kernel_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod(&args); | |
| 183 | 222 | } | |
| 184 | |||
| 185 | #endif // Architectural features check. | ||
| 186 |