Commit bd9a15f6a for llama.cpp
commit bd9a15f6ad037eb5b7f52af27f86a67e77d2f6ec
Author: Max Krasnyansky <maxk@qti.qualcomm.com>
Date: Fri Dec 4 11:14:22 2026 -0800
hexagon: optimize fused GLU and rescale W8A8 matmul (#33581)
* hexagon : optimize W8A8 signed-m rescale and FP16 GLU epilogue
Use hmx_qf32_to_i32_zp128 + xor 0x80 for signed-m rescale, stage 32x64 W8A8 1g/2g GLU tiles
in L1D with direct Wqf32->Vhf pack.
SW-pipeline 1-vec FP16 GLU strips (2-loop scratch split for GEGLU).
* hexagon : move GLU strip scratch to ddr_spad and unify F16 path
Stage per-worker GLU strip intermediates in ctx->ddr_spad_base instead of worker stack,
unify FP16 HMX and W8A8 ng/chunked GLU paths onto store_strip_fn, and drop obsolete store_quad_f16 helpers.
* hexagon: apply f8_e4m3 scale in dequant
* hexagon: improve fwht with f8 rescaling
* hexagon: update dev guide to clarify register spills optimizations
* hexagon: auto-enable FWHT for Q6_K, Q8_0 and F8 types
diff --git a/docs/backend/snapdragon/README.md b/docs/backend/snapdragon/README.md
index 35329136e..8b3c785ab 100644
--- a/docs/backend/snapdragon/README.md
+++ b/docs/backend/snapdragon/README.md
@@ -317,9 +317,9 @@ try stepping down to higher-precision configurations in the following order:
| MMSEL | FWHT | OPLWR | Matmul | Intermediates | Notes |
| :---: | :--: | :---: | --------------- | :-----------: | ----------------------------------------------------------------------------------- |
-| 3 | 0 | 1 | `W8A8` HMX | `FP16` | Default; maximum prefill speed |
-| 3 | 1 | 1 | `W8A8` HMX+FWHT | `FP16` | Higher accuracy on models with activation outliers |
-| 3 | 1 | 0 | `W8A8` HMX+FWHT | `FP32` | Avoids `FP16` activation/residual overflow while keeping `W8A8` HMX (higher DDR BW) |
+| 3 | 1 | 1 | `W8A8` HMX | `FP16` | Default; auto FWHT for `Q6_K`/`Q8_0`/`F8_E4M3`, maximum speed on lower-bit quants |
+| 3 | 2 | 1 | `W8A8` HMX+FWHT | `FP16` | Force FWHT on all quant types; higher accuracy on models with activation outliers |
+| 3 | 2 | 0 | `W8A8` HMX+FWHT | `FP32` | Avoids `FP16` activation/residual overflow while keeping `W8A8` HMX (higher DDR BW) |
| 2 | - | 1 | `FP16` HMX | `FP16` | Higher matmul precision without `W8A8` quantization |
| 2 | - | 0 | `FP16` HMX | `FP32` | Maximum HMX precision (`FP16` HMX + `FP32` intermediates) |
@@ -434,11 +434,12 @@ Notes:
All HMX modes fall back to HVX vector kernels for small batch sizes (`M <= 4`).
-- `GGML_HEXAGON_MM_FWHT=0|1`
- Enables online block-32 Fast Walsh-Hadamard Transform (FWHT) rotation along `K` in `W8A8` HMX matmul (configurable via `--hex-mm-fwht` in `run.py`):
+- `GGML_HEXAGON_MM_FWHT=0|1|2`
+ Controls online block-32 Fast Walsh-Hadamard Transform (FWHT) rotation along `K` in `W8A8` HMX matmul (configurable via `--hex-mm-fwht` in `run.py`):
- - `0`: Disabled (default)
- - `1`: Enabled - higher accuracy on models with activation outliers
+ - `0`: Disabled
+ - `1`: Auto (default) - enabled for `Q6_K`, `Q8_0`, and `F8_E4M3` weights unless activations are already Hadamard-rotated
+ - `2`: Always enabled - higher accuracy on models with activation outliers across all quant types
- `GGML_HEXAGON_OPPOLL=N`
Controls host polling while waiting for HTP opbatch completion (configurable via `--hex-oppoll` in `run.py`):
diff --git a/docs/backend/snapdragon/developer.md b/docs/backend/snapdragon/developer.md
index 74c084676..913564604 100644
--- a/docs/backend/snapdragon/developer.md
+++ b/docs/backend/snapdragon/developer.md
@@ -353,18 +353,19 @@ Writing high-performance operators for Hexagon requires following specific guide
### Binary Inspection and Spill Analysis
-Use [`scripts/snapdragon/ggml-hexagon-inspect.py`](../../../scripts/snapdragon/ggml-hexagon-inspect.py) to audit Hexagon binaries for register
-spills, unexpected float promotions, or disassembly:
+Use [`scripts/snapdragon/ggml-hexagon-inspect.py`](../../../scripts/snapdragon/ggml-hexagon-inspect.py) to audit Hexagon binaries for register spills, unexpected float promotions, software divisions, or disassembly:
-- Always verify that compute kernels have zero in-loop vector spills (`--spills --strict`) and no float promotions (`--promotions`).
+- Minimize in-loop vector spills (`--spills`), especially `[V-ROUNDTRIP!]` store/load pairs (`--strict-stores-only`), and verify zero in-loop float promotions (`--promotions`) and software divisions (`--swdiv`).
+- Do **not** chase spills at the expense of VLIW packet count. A `v-readonly` reload of a hoisted constant hits L1D on a load slot in parallel with HVX ALU ops, whereas rematerializing constants or inserting `asm volatile` barriers inhibits scheduling and VLIW packetization. A ~1-2% packet increase may be acceptable when eliminating real roundtrip spills, but larger packet increases typically add net overhead - always check packet count and benchmark before keeping a spill-reduction change.
- Avoid excessive loop unrolling (`#pragma unroll`), which increases register pressure and causes spills.
```bash
# Check for vector and scalar register spills
python3 scripts/snapdragon/ggml-hexagon-inspect.py --spills --strict --func "^compute_"
-# Check for float promotions
+# Check for float promotions and software divisions
python3 scripts/snapdragon/ggml-hexagon-inspect.py --promotions --func "^compute_"
+python3 scripts/snapdragon/ggml-hexagon-inspect.py --swdiv --func "^compute_"
# Disassemble with annotated loops and spill markers
python3 scripts/snapdragon/ggml-hexagon-inspect.py --disasm compute_same_shape_div_f32
diff --git a/ggml/src/ggml-hexagon/ggml-hexagon.cpp b/ggml/src/ggml-hexagon/ggml-hexagon.cpp
index ddbd35a9e..24dccfa91 100644
--- a/ggml/src/ggml-hexagon/ggml-hexagon.cpp
+++ b/ggml/src/ggml-hexagon/ggml-hexagon.cpp
@@ -135,7 +135,7 @@ size_t opt_mbuf_static = 1ul * 1024 * 1024 * 1024; // max static (weight/KV) buf
size_t opt_mbuf_total = 0; // total buffer space limit (0 = unconstrained)
int opt_mm_select = 3; // 3 = W8A8 INT HMX -> FP16 HMX -> HVX -> CPU (default), 2 = FP16 HMX -> HVX -> CPU, 1 = HVX -> CPU, 0 = CPU
-int opt_mm_fwht = 0; // 1 = enable online block-32 FWHT rotation in W8A8 HMX matmul, 0 = off (default)
+int opt_mm_fwht = 1; // 0 = off, 1 = auto (default, Q6_K/Q8_0/F8_E4M3), 2 = always enable online block-32 FWHT in W8A8 HMX matmul
int opt_fa_select = 3; // 3 = INT HMX W8A8 (default) -> FP16 HMX -> HVX -> CPU, 2 = FP16 HMX -> HVX -> CPU, 1 = HVX -> CPU, 0 = CPU
int opt_fa_split = 1; // 1 = head-split + team-split when n_kv_heads < N_cores (default), 0 = off
int opt_li_select = 2; // 2 = HMX -> HVX, 1 = HVX, 0 = CPU (unsupported)
diff --git a/ggml/src/ggml-hexagon/htp-kparams.cpp b/ggml/src/ggml-hexagon/htp-kparams.cpp
index 41ec31b64..84e1075f3 100644
--- a/ggml/src/ggml-hexagon/htp-kparams.cpp
+++ b/ggml/src/ggml-hexagon/htp-kparams.cpp
@@ -743,6 +743,20 @@ static inline size_t htp_mm_scale_vtcm_bytes(const struct ggml_tensor * src0, en
}
}
+static inline bool htp_mm_use_fwht(int wtype, const struct ggml_tensor * act) {
+ if (opt_mm_fwht <= 0) {
+ return false;
+ }
+ if (opt_mm_fwht >= 2) {
+ return true;
+ }
+ const struct ggml_tensor * a = (act && act->view_src) ? act->view_src : act;
+ if (htp_is_hadamard(act) || htp_is_hadamard(a)) {
+ return false;
+ }
+ return wtype == GGML_TYPE_Q6_K || wtype == GGML_TYPE_Q8_0 || wtype == GGML_TYPE_F8_E4M3;
+}
+
static inline bool htp_mm_can_fuse_scale(const struct ggml_tensor * src0, const struct ggml_tensor * scale) {
return scale != nullptr &&
scale->type == GGML_TYPE_F32 &&
@@ -771,7 +785,6 @@ static bool htp_kparams_solve_hmx_mm(
size_t scale_size = 0,
bool has_norm = false
) {
- GGML_UNUSED(act);
const bool bias_is_2d = bias && (bias->ne[1] > 1);
const bool bias_batched = bias && (bias->ne[2] > 1 || bias->ne[3] > 1);
const bool is_tiled = ggml_hexagon_tensor_will_tile(src0);
@@ -881,7 +894,7 @@ static bool htp_kparams_solve_hmx_mm(
if (pipeline) {
flags |= HTP_MM_FLAG_PIPELINE;
}
- if (htp_mm_kernel_is_hmx_w8a8(kernel_type) && opt_mm_fwht != 0) {
+ if (htp_mm_kernel_is_hmx_w8a8(kernel_type) && htp_mm_use_fwht(wtype, act)) {
flags |= HTP_MM_FLAG_FWHT;
}
if (use_grouped) {
diff --git a/ggml/src/ggml-hexagon/htp/hmx-fa-w8a8-kernels.h b/ggml/src/ggml-hexagon/htp/hmx-fa-w8a8-kernels.h
index a60e13fd4..488d056d2 100644
--- a/ggml/src/ggml-hexagon/htp/hmx-fa-w8a8-kernels.h
+++ b/ggml/src/ggml-hexagon/htp/hmx-fa-w8a8-kernels.h
@@ -668,10 +668,10 @@ static __attribute__((noinline)) void hmx_fa_w8a8_v_prep_tile(
for (uint32_t kt = 0; kt < n_col_tiles; ++kt) {
HVX_Vector * tile_dst = tile_dst_all + kt * 8;
- HVX_Vector v_sc_qf = Q6_V_lo_W(Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(tile_dst[7]), v_one_f16));
- HVX_Vector v_m_biased = hvx_vec_i32_from_qf32_rnd(Q6_Vqf32_vmpy_Vqf32Vqf32(v_sc_qf, v_inv127_qf));
- HVX_Vector v_m_h = Q6_Vh_vpacke_VwVw(v_m_biased, v_m_biased);
- HVX_Vector v_m_b = Q6_Vb_vpacke_VhVh(v_m_h, v_m_h);
+ HVX_Vector v_sc_qf = Q6_V_lo_W(Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(tile_dst[7]), v_one_f16));
+ HVX_Vector v_m_u8 = hmx_qf32_to_i32_zp128(Q6_Vqf32_vmpy_Vqf32Vqf32(v_sc_qf, v_inv127_qf));
+ HVX_Vector v_m_h = Q6_Vh_vpacke_VwVw(v_m_u8, v_m_u8);
+ HVX_Vector v_m_b = Q6_V_vxor_VV(Q6_Vb_vpacke_VhVh(v_m_h, v_m_h), Q6_Vb_vsplat_R(0x80));
#pragma clang loop pipeline(disable) unroll(disable)
for (uint32_t g = 0; g < 8; ++g) {
diff --git a/ggml/src/ggml-hexagon/htp/hmx-mm-f16-kernels.h b/ggml/src/ggml-hexagon/htp/hmx-mm-f16-kernels.h
index 426d32e38..a111fd78e 100644
--- a/ggml/src/ggml-hexagon/htp/hmx-mm-f16-kernels.h
+++ b/ggml/src/ggml-hexagon/htp/hmx-mm-f16-kernels.h
@@ -577,6 +577,50 @@ static void dequantize_tiled_weight_to_fp16_task_f8_e4m3(
}
}
+static void dequantize_tiled_weight_to_fp16_task_f8_e4m3_scaled(
+ const tiled_dequantize_state_t *state,
+ uint32_t start_tile, uint32_t end_tile) {
+
+ const size_t tile_stride = htp_tensor_get_tile_padded_size(HTP_TYPE_F8_E4M3);
+ const uint32_t n_k_tiles = state->n_k_tiles;
+ const float * w_scale = state->w_scale;
+ const uint32_t w_scale_ct_stride = state->w_scale_ct_stride;
+ const uint8_t * restrict tile_src = state->src + (size_t) start_tile * tile_stride;
+ __fp16 * restrict dst_ptr = state->dst + (size_t) start_tile * HTP_MM_HMX_TILE_N_ELMS;
+
+ uint32_t ct = fastdiv(start_tile, &state->n_k_tiles_div);
+ uint32_t kt = start_tile - ct * n_k_tiles;
+
+ #pragma clang loop pipeline(disable) unroll(disable)
+ for (uint32_t t = start_tile; t < end_tile; t++) {
+ HVX_Vector v_ws = hvx_vmem(w_scale + (size_t) ct * w_scale_ct_stride);
+
+ #pragma unroll
+ for (int i = 0; i < 8; i++) {
+ HVX_VectorPair vp_u16 = Q6_Wuh_vunpack_Vub(hvx_vmem(tile_src + i * 128));
+ HVX_VectorPair vp_k = Q6_W_vdeal_VVR(Q6_V_hi_W(vp_u16), Q6_V_lo_W(vp_u16), -4);
+
+ // even/odd fp16 lanes hold the two k values for rows 0..31, matching v_ws lane order
+ HVX_VectorPair vp_w0 = hvx_vec_f16_to_f32_shuff(f8_e4m3_to_f16(Q6_V_lo_W(vp_k)));
+ HVX_VectorPair vp_w1 = hvx_vec_f16_to_f32_shuff(f8_e4m3_to_f16(Q6_V_hi_W(vp_k)));
+
+ HVX_Vector q0_e = Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(vp_w0), v_ws);
+ HVX_Vector q0_o = Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(vp_w0), v_ws);
+ HVX_Vector q1_e = Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(vp_w1), v_ws);
+ HVX_Vector q1_o = Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(vp_w1), v_ws);
+
+ hvx_vmem(dst_ptr + (2 * i + 0) * 64) = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(q0_o, q0_e));
+ hvx_vmem(dst_ptr + (2 * i + 1) * 64) = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(q1_o, q1_e));
+ }
+ tile_src += tile_stride;
+ dst_ptr += HTP_MM_HMX_TILE_N_ELMS;
+ if (++kt == n_k_tiles) {
+ kt = 0;
+ ++ct;
+ }
+ }
+}
+
// IQ2_XXS: int8 grid weights from the VTCM LUT, scaled by d / 8 * (2 * scale + 1) per row
static void dequantize_tiled_weight_to_fp16_task_iq2_xxs(
const tiled_dequantize_state_t *state,
@@ -1643,13 +1687,66 @@ static void transfer_activation_row_pair_f16_to_f16_col_chunk(
// --- F16-HMX GLU Epilogue ---
+static __attribute__((noinline)) void fp16_glu_stage_strip(
+ const HVX_Vector * restrict g0,
+ const HVX_Vector * restrict g1,
+ const HVX_Vector * restrict u0,
+ const HVX_Vector * restrict u1,
+ HVX_Vector * restrict g_hf,
+ HVX_Vector * restrict u_hf,
+ uint32_t n_pairs) {
+ #pragma clang loop unroll(disable)
+ for (uint32_t p = 0; p < n_pairs; ++p) {
+ const HVX_VectorPair gp = Q6_W_vdeal_VVR(g1[p], g0[p], -2);
+ const HVX_VectorPair up = Q6_W_vdeal_VVR(u1[p], u0[p], -2);
+ g_hf[2u * p + 0u] = Q6_V_lo_W(gp);
+ g_hf[2u * p + 1u] = Q6_V_hi_W(gp);
+ u_hf[2u * p + 0u] = Q6_V_lo_W(up);
+ u_hf[2u * p + 1u] = Q6_V_hi_W(up);
+ }
+}
+
+static __attribute__((noinline)) void fp16_glu_stage_strip_scaled(
+ const HVX_Vector * restrict s0,
+ const HVX_Vector * restrict s1,
+ const float * restrict ws,
+ size_t c_abs,
+ uint32_t c_mask,
+ HVX_Vector * restrict out_hf,
+ uint32_t n_pairs) {
+ if (!ws) {
+ #pragma clang loop unroll(disable)
+ for (uint32_t p = 0; p < n_pairs; ++p) {
+ const HVX_VectorPair vp = Q6_W_vdeal_VVR(s1[p], s0[p], -2);
+ out_hf[2u * p + 0u] = Q6_V_lo_W(vp);
+ out_hf[2u * p + 1u] = Q6_V_hi_W(vp);
+ }
+ return;
+ }
+ const HVX_Vector v_s0 = hvx_vmem(ws + ((c_abs + 0u) & c_mask));
+ const HVX_Vector v_s1 = hvx_vmem(ws + ((c_abs + 32u) & c_mask));
+ #pragma clang loop unroll(disable)
+ for (uint32_t p = 0; p < n_pairs; ++p) {
+ const HVX_VectorPair vp = Q6_W_vdeal_VVR(s1[p], s0[p], -2);
+ const HVX_VectorPair p0 = hvx_vec_f16_to_f32_shuff(Q6_V_lo_W(vp));
+ const HVX_VectorPair p1 = hvx_vec_f16_to_f32_shuff(Q6_V_hi_W(vp));
+ out_hf[2u * p + 0u] = hvx_vec_qf32_to_f16_clamped(
+ Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(p0), v_s0),
+ Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(p0), v_s1));
+ out_hf[2u * p + 1u] = hvx_vec_qf32_to_f16_clamped(
+ Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(p1), v_s0),
+ Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(p1), v_s1));
+ }
+}
+
static __attribute__((noinline)) void transfer_output_chunk_fp16_glu_fp16(
const void * st_void,
void * restrict dst_ptr,
uint32_t start_row,
uint32_t n_rows,
uint32_t c_first,
- uint32_t c_len_and_dst_cols) {
+ uint32_t c_len_and_dst_cols,
+ HVX_Vector * restrict spad) {
const hmx_output_glu_state_t * st = (const hmx_output_glu_state_t *) st_void;
__fp16 * restrict dst = (__fp16 *) dst_ptr;
const __fp16 * vtcm_gate = (const __fp16 *) st->vtcm_gate;
@@ -1660,8 +1757,9 @@ static __attribute__((noinline)) void transfer_output_chunk_fp16_glu_fp16(
const size_t limit_c_aligned = limit_c & ~63u;
const size_t dst_stride = st->dst_stride;
- HVX_Vector g_hf[4] __attribute__((aligned(128)));
- HVX_Vector u_hf[4] __attribute__((aligned(128)));
+ HVX_Vector * scratch = spad + 0u;
+ HVX_Vector * g_hf = spad + 32u;
+ HVX_Vector * u_hf = spad + 64u;
for (size_t r_base = 0; r_base < n_rows; ) {
const size_t r_idx_base = start_row + r_base;
@@ -1671,44 +1769,32 @@ static __attribute__((noinline)) void transfer_output_chunk_fp16_glu_fp16(
const __fp16 * g_row = vtcm_gate + tile_off;
const __fp16 * u_row = vtcm_up + tile_off;
- for (size_t r = r_base; r < r_end; r += 4) {
- const size_t r_idx0 = start_row + r;
- const size_t r1 = (r_idx0 & 31u) >> 1;
- const uint32_t valid_rows = (uint32_t) hex_smin((size_t) 4u, r_end - r);
- __fp16 * out_row0 = dst + r * dst_stride;
-
- #pragma clang loop unroll(disable)
+ if (limit_c_aligned > 0u) {
+ const size_t r1_base = (r_idx_base & 31u) >> 1;
+ const uint32_t n_pairs = (uint32_t) ((r_step + 1u) >> 1);
for (size_t c = 0; c < limit_c_aligned; c += 64) {
- const size_t c0 = c >> 5;
- const size_t off0 = c0 * HTP_MM_HMX_TILE_N_ELMS;
+ const size_t off0 = (c >> 5) * HTP_MM_HMX_TILE_N_ELMS;
const size_t off1 = off0 + HTP_MM_HMX_TILE_N_ELMS;
- const HVX_VectorPair gp0 = Q6_W_vdeal_VVR(((const HVX_Vector *) (g_row + off1))[r1],
- ((const HVX_Vector *) (g_row + off0))[r1], -2);
- const HVX_VectorPair up0 = Q6_W_vdeal_VVR(((const HVX_Vector *) (u_row + off1))[r1],
- ((const HVX_Vector *) (u_row + off0))[r1], -2);
- g_hf[0] = Q6_V_lo_W(gp0);
- g_hf[1] = Q6_V_hi_W(gp0);
- u_hf[0] = Q6_V_lo_W(up0);
- u_hf[1] = Q6_V_hi_W(up0);
- if (valid_rows > 2u) {
- const HVX_VectorPair gp1 = Q6_W_vdeal_VVR(((const HVX_Vector *) (g_row + off1))[r1 + 1],
- ((const HVX_Vector *) (g_row + off0))[r1 + 1], -2);
- const HVX_VectorPair up1 = Q6_W_vdeal_VVR(((const HVX_Vector *) (u_row + off1))[r1 + 1],
- ((const HVX_Vector *) (u_row + off0))[r1 + 1], -2);
- g_hf[2] = Q6_V_lo_W(gp1);
- g_hf[3] = Q6_V_hi_W(gp1);
- u_hf[2] = Q6_V_lo_W(up1);
- u_hf[3] = Q6_V_hi_W(up1);
- }
- st->store_quad_fn(out_row0 + c, dst_stride, g_hf, u_hf, valid_rows);
+ fp16_glu_stage_strip((const HVX_Vector *) (g_row + off0) + r1_base,
+ (const HVX_Vector *) (g_row + off1) + r1_base,
+ (const HVX_Vector *) (u_row + off0) + r1_base,
+ (const HVX_Vector *) (u_row + off1) + r1_base,
+ g_hf, u_hf, n_pairs);
+ st->store_strip_fn(dst + r_base * dst_stride + c, dst_stride, g_hf, u_hf, scratch, (uint32_t) r_step);
}
- if (limit_c_aligned < limit_c) {
- const size_t c = limit_c_aligned;
- const size_t c0 = c >> 5;
- const size_t off0 = c0 * HTP_MM_HMX_TILE_N_ELMS;
- const size_t off1 = off0 + HTP_MM_HMX_TILE_N_ELMS;
- const size_t valid_c = limit_c - c;
- const uint32_t valid_bytes = (uint32_t) (valid_c * sizeof(__fp16));
+ }
+ if (limit_c_aligned < limit_c) {
+ const size_t c = limit_c_aligned;
+ const size_t c0 = c >> 5;
+ const size_t off0 = c0 * HTP_MM_HMX_TILE_N_ELMS;
+ const size_t off1 = off0 + HTP_MM_HMX_TILE_N_ELMS;
+ const size_t valid_c = limit_c - c;
+ const uint32_t valid_bytes = (uint32_t) (valid_c * sizeof(__fp16));
+ for (size_t r = r_base; r < r_end; r += 4) {
+ const size_t r_idx0 = start_row + r;
+ const size_t r1 = (r_idx0 & 31u) >> 1;
+ const uint32_t valid_rows = (uint32_t) hex_smin((size_t) 4u, r_end - r);
+ __fp16 * out_row0 = dst + r * dst_stride;
const HVX_Vector g_t1_0 = (valid_c > 32) ? ((const HVX_Vector *) (g_row + off1))[r1] : Q6_V_vzero();
const HVX_Vector u_t1_0 = (valid_c > 32) ? ((const HVX_Vector *) (u_row + off1))[r1] : Q6_V_vzero();
const HVX_VectorPair gp0 = Q6_W_vdeal_VVR(g_t1_0, ((const HVX_Vector *) (g_row + off0))[r1], -2);
@@ -1740,7 +1826,9 @@ static __attribute__((noinline)) void transfer_output_chunk_fp16_glu_fp32(
uint32_t start_row,
uint32_t n_rows,
uint32_t c_first,
- uint32_t c_len_and_dst_cols) {
+ uint32_t c_len_and_dst_cols,
+ HVX_Vector * restrict spad) {
+ (void) spad;
const hmx_output_glu_state_t * st = (const hmx_output_glu_state_t *) st_void;
float * restrict dst = (float *) dst_ptr;
const __fp16 * vtcm_gate = (const __fp16 *) st->vtcm_gate;
@@ -1819,7 +1907,8 @@ static __attribute__((noinline)) void transfer_output_chunk_fp16_glu_fp16_scaled
uint32_t start_row,
uint32_t n_rows,
uint32_t c_first,
- uint32_t c_len_and_dst_cols) {
+ uint32_t c_len_and_dst_cols,
+ HVX_Vector * restrict spad) {
const hmx_output_glu_state_t * st = (const hmx_output_glu_state_t *) st_void;
__fp16 * restrict dst = (__fp16 *) dst_ptr;
const __fp16 * vtcm_gate = (const __fp16 *) st->vtcm_gate;
@@ -1834,8 +1923,9 @@ static __attribute__((noinline)) void transfer_output_chunk_fp16_glu_fp16_scaled
const size_t limit_c_aligned = limit_c & ~63u;
const size_t dst_stride = st->dst_stride;
- HVX_Vector g_hf[4] __attribute__((aligned(128)));
- HVX_Vector u_hf[4] __attribute__((aligned(128)));
+ HVX_Vector * scratch = spad + 0u;
+ HVX_Vector * g_hf = spad + 32u;
+ HVX_Vector * u_hf = spad + 64u;
for (size_t r_base = 0; r_base < n_rows; ) {
const size_t r_idx_base = start_row + r_base;
@@ -1845,46 +1935,35 @@ static __attribute__((noinline)) void transfer_output_chunk_fp16_glu_fp16_scaled
const __fp16 * g_row = vtcm_gate + tile_off;
const __fp16 * u_row = vtcm_up + tile_off;
- for (size_t r = r_base; r < r_end; r += 4) {
- const size_t r_idx0 = start_row + r;
- const size_t r1 = (r_idx0 & 31u) >> 1;
- const uint32_t valid_rows = (uint32_t) hex_smin((size_t) 4u, r_end - r);
- __fp16 * out_row0 = dst + r * dst_stride;
-
- #pragma clang loop unroll(disable)
+ if (limit_c_aligned > 0u) {
+ const size_t r1_base = (r_idx_base & 31u) >> 1;
+ const uint32_t n_pairs = (uint32_t) ((r_step + 1u) >> 1);
for (size_t c = 0; c < limit_c_aligned; c += 64) {
const size_t c_abs = c_first + c;
- const size_t c0 = c >> 5;
- const size_t off0 = c0 * HTP_MM_HMX_TILE_N_ELMS;
+ const size_t off0 = (c >> 5) * HTP_MM_HMX_TILE_N_ELMS;
const size_t off1 = off0 + HTP_MM_HMX_TILE_N_ELMS;
- const HVX_VectorPair gp0 = Q6_W_vdeal_VVR(((const HVX_Vector *) (g_row + off1))[r1],
- ((const HVX_Vector *) (g_row + off0))[r1], -2);
- const HVX_VectorPair up0 = Q6_W_vdeal_VVR(((const HVX_Vector *) (u_row + off1))[r1],
- ((const HVX_Vector *) (u_row + off0))[r1], -2);
- g_hf[0] = ws_gate ? hmx_scale_f16_vec64_f32(Q6_V_lo_W(gp0), ws_gate, c_abs, g_mask) : Q6_V_lo_W(gp0);
- g_hf[1] = ws_gate ? hmx_scale_f16_vec64_f32(Q6_V_hi_W(gp0), ws_gate, c_abs, g_mask) : Q6_V_hi_W(gp0);
- u_hf[0] = ws_up ? hmx_scale_f16_vec64_f32(Q6_V_lo_W(up0), ws_up, c_abs, u_mask) : Q6_V_lo_W(up0);
- u_hf[1] = ws_up ? hmx_scale_f16_vec64_f32(Q6_V_hi_W(up0), ws_up, c_abs, u_mask) : Q6_V_hi_W(up0);
- if (valid_rows > 2u) {
- const HVX_VectorPair gp1 = Q6_W_vdeal_VVR(((const HVX_Vector *) (g_row + off1))[r1 + 1],
- ((const HVX_Vector *) (g_row + off0))[r1 + 1], -2);
- const HVX_VectorPair up1 = Q6_W_vdeal_VVR(((const HVX_Vector *) (u_row + off1))[r1 + 1],
- ((const HVX_Vector *) (u_row + off0))[r1 + 1], -2);
- g_hf[2] = ws_gate ? hmx_scale_f16_vec64_f32(Q6_V_lo_W(gp1), ws_gate, c_abs, g_mask) : Q6_V_lo_W(gp1);
- g_hf[3] = ws_gate ? hmx_scale_f16_vec64_f32(Q6_V_hi_W(gp1), ws_gate, c_abs, g_mask) : Q6_V_hi_W(gp1);
- u_hf[2] = ws_up ? hmx_scale_f16_vec64_f32(Q6_V_lo_W(up1), ws_up, c_abs, u_mask) : Q6_V_lo_W(up1);
- u_hf[3] = ws_up ? hmx_scale_f16_vec64_f32(Q6_V_hi_W(up1), ws_up, c_abs, u_mask) : Q6_V_hi_W(up1);
- }
- st->store_quad_fn(out_row0 + c, dst_stride, g_hf, u_hf, valid_rows);
+ fp16_glu_stage_strip_scaled((const HVX_Vector *) (g_row + off0) + r1_base,
+ (const HVX_Vector *) (g_row + off1) + r1_base,
+ ws_gate, c_abs, g_mask, g_hf, n_pairs);
+ fp16_glu_stage_strip_scaled((const HVX_Vector *) (u_row + off0) + r1_base,
+ (const HVX_Vector *) (u_row + off1) + r1_base,
+ ws_up, c_abs, u_mask, u_hf, n_pairs);
+ st->store_strip_fn(dst + r_base * dst_stride + c, dst_stride, g_hf, u_hf, scratch, (uint32_t) r_step);
}
- if (limit_c_aligned < limit_c) {
- const size_t c = limit_c_aligned;
- const size_t c_abs = c_first + c;
- const size_t c0 = c >> 5;
- const size_t off0 = c0 * HTP_MM_HMX_TILE_N_ELMS;
- const size_t off1 = off0 + HTP_MM_HMX_TILE_N_ELMS;
- const size_t valid_c = limit_c - c;
- const uint32_t valid_bytes = (uint32_t) (valid_c * sizeof(__fp16));
+ }
+ if (limit_c_aligned < limit_c) {
+ const size_t c = limit_c_aligned;
+ const size_t c_abs = c_first + c;
+ const size_t c0 = c >> 5;
+ const size_t off0 = c0 * HTP_MM_HMX_TILE_N_ELMS;
+ const size_t off1 = off0 + HTP_MM_HMX_TILE_N_ELMS;
+ const size_t valid_c = limit_c - c;
+ const uint32_t valid_bytes = (uint32_t) (valid_c * sizeof(__fp16));
+ for (size_t r = r_base; r < r_end; r += 4) {
+ const size_t r_idx0 = start_row + r;
+ const size_t r1 = (r_idx0 & 31u) >> 1;
+ const uint32_t valid_rows = (uint32_t) hex_smin((size_t) 4u, r_end - r);
+ __fp16 * out_row0 = dst + r * dst_stride;
const HVX_Vector g_t1_0 = (valid_c > 32) ? ((const HVX_Vector *) (g_row + off1))[r1] : Q6_V_vzero();
const HVX_Vector u_t1_0 = (valid_c > 32) ? ((const HVX_Vector *) (u_row + off1))[r1] : Q6_V_vzero();
const HVX_VectorPair gp0 = Q6_W_vdeal_VVR(g_t1_0, ((const HVX_Vector *) (g_row + off0))[r1], -2);
@@ -1916,7 +1995,9 @@ static __attribute__((noinline)) void transfer_output_chunk_fp16_glu_fp32_scaled
uint32_t start_row,
uint32_t n_rows,
uint32_t c_first,
- uint32_t c_len_and_dst_cols) {
+ uint32_t c_len_and_dst_cols,
+ HVX_Vector * restrict spad) {
+ (void) spad;
const hmx_output_glu_state_t * st = (const hmx_output_glu_state_t *) st_void;
float * restrict dst = (float *) dst_ptr;
const __fp16 * vtcm_gate = (const __fp16 *) st->vtcm_gate;
diff --git a/ggml/src/ggml-hexagon/htp/hmx-mm-w8a8-kernels.h b/ggml/src/ggml-hexagon/htp/hmx-mm-w8a8-kernels.h
index c7e98a6a7..82522cd4e 100644
--- a/ggml/src/ggml-hexagon/htp/hmx-mm-w8a8-kernels.h
+++ b/ggml/src/ggml-hexagon/htp/hmx-mm-w8a8-kernels.h
@@ -932,50 +932,44 @@ static __attribute__((always_inline)) inline void rescale_f8_e4m3_weight_superbl
v_max_u8 = Q6_Vub_vmax_VubVub(v_max_u8, Q6_Vuw_vlsr_VuwR(v_max_u8, 8));
HVX_Vector v_max_sf = nvfp4_ue4m3_to_f32(v_max_u8);
- HVX_Vector v_inv_sf = Q6_Vsf_equals_Vqf32(hmx_w8a8_scale_qf_from_max_f32(v_max_sf, 127.0f));
+ HVX_Vector v_inv_sf = Q6_Vsf_equals_Vqf32(hmx_w8a8_scale_qf_from_max_f32(v_max_sf, use_fwht ? 1016.0f : 127.0f));
HVX_Vector v_sum_w = v_zero;
HVX_Vector v_l1_w = v_zero;
const HVX_Vector v_ones_s8 = Q6_Vb_vsplat_R(1);
- const HVX_Vector v_min_127 = Q6_Vh_vsplat_R(-127);
- const HVX_Vector v_max_127 = Q6_Vh_vsplat_R(127);
+ const HVX_Vector v_min_q = Q6_Vh_vsplat_R(use_fwht ? -1016 : -127);
+ const HVX_Vector v_max_q = Q6_Vh_vsplat_R(use_fwht ? 1016 : 127);
#pragma clang loop pipeline(disable) unroll(disable)
for (uint32_t kt = 0; kt < tiles_per_group; ++kt) {
const uint8_t * tile_src = src_base + (size_t) kt * tile_stride;
HVX_Vector * tile_dst = (HVX_Vector *) (dst_wt + (size_t) kt * dst_tile_stride);
+ HVX_Vector fwht_lo[8], fwht_hi[8];
for (int i = 0; i < 8; ++i) {
HVX_Vector v_h02, v_h13;
hmx_w8a8_f8_e4m3_vec_to_i16_pair(hvx_vmem(tile_src + i * 128), v_inv_sf, &v_h02, &v_h13);
- v_h02 = Q6_Vh_vmax_VhVh(v_min_127, Q6_Vh_vmin_VhVh(v_max_127, v_h02));
- v_h13 = Q6_Vh_vmax_VhVh(v_min_127, Q6_Vh_vmin_VhVh(v_max_127, v_h13));
- HVX_Vector v_W = Q6_Vb_vshuffe_VbVb(v_h13, v_h02);
- tile_dst[i] = v_W;
- if (!use_fwht) {
+ v_h02 = Q6_Vh_vmax_VhVh(v_min_q, Q6_Vh_vmin_VhVh(v_max_q, v_h02));
+ v_h13 = Q6_Vh_vmax_VhVh(v_min_q, Q6_Vh_vmin_VhVh(v_max_q, v_h13));
+ if (use_fwht) {
+ HVX_Vector v_xe = Q6_V_vsplat_R(hmx_w8a8_fwht32_rs_xor_h_even[i]);
+ HVX_Vector v_xo = Q6_V_vsplat_R(hmx_w8a8_fwht32_rs_xor_h_odd[i]);
+ v_h02 = Q6_Vh_vsub_VhVh(Q6_V_vxor_VV(v_h02, v_xe), v_xe);
+ v_h13 = Q6_Vh_vsub_VhVh(Q6_V_vxor_VV(v_h13, v_xo), v_xo);
+ hmx_w8a8_fwht32_s12_from_i16(v_h02, v_h13, &fwht_lo[i], &fwht_hi[i]);
+ } else {
+ HVX_Vector v_W = Q6_Vb_vshuffe_VbVb(v_h13, v_h02);
+ tile_dst[i] = v_W;
v_sum_w = Q6_Vw_vrmpyacc_VwVbVb(v_sum_w, v_W, v_ones_s8);
v_l1_w = Q6_Vw_vrmpyacc_VwVubVb(v_l1_w, Q6_Vb_vabs_Vb(v_W), v_ones_s8);
}
}
- }
-
- if (use_fwht) {
- #pragma clang loop pipeline(disable) unroll(disable)
- for (uint32_t kt = 0; kt < tiles_per_group; ++kt) {
- HVX_Vector * tile_dst = (HVX_Vector *) (dst_wt + (size_t) kt * dst_tile_stride);
- HVX_Vector fwht_lo[8], fwht_hi[8];
- #pragma unroll
- for (int i = 0; i < 8; ++i) {
- HVX_Vector v_W = tile_dst[i];
- HVX_Vector v_rs = Q6_V_vsplat_R(hmx_w8a8_fwht32_rs_xor[i]);
- v_W = Q6_Vb_vsub_VbVb(Q6_V_vxor_VV(v_W, v_rs), v_rs);
- hmx_w8a8_fwht32_s12_i16(v_W, v_ones_s8, &fwht_lo[i], &fwht_hi[i]);
- }
- hmx_w8a8_fwht32_s345_store(fwht_lo, fwht_hi, tile_dst, &v_sum_w, &v_l1_w, 4);
+ if (use_fwht) {
+ hmx_w8a8_fwht32_s345_store(fwht_lo, fwht_hi, tile_dst, &v_sum_w, &v_l1_w, 6);
}
}
- hmx_w8a8_emit_scales_and_bias(v_max_sf, use_fwht ? (0.5f / 127.0f) : (1.0f / 127.0f), v_sum_w, v_l1_w, dst_scales, bias_out);
+ hmx_w8a8_emit_scales_and_bias(v_max_sf, use_fwht ? (0.25f / 127.0f) : (1.0f / 127.0f), v_sum_w, v_l1_w, dst_scales, bias_out);
}
DEFINE_RESCALE_WEIGHT_SUPERBLOCK_BOTH_ENTRIES(f8_e4m3)
@@ -3617,6 +3611,170 @@ DEFINE_W8A8_EVAL_TILE_QUAD(f32, HMX_INT_FIN_ROW_F32)
} \
}
+static __attribute__((noinline)) void w8a8_sm_eval_1g_tile_qf32(
+ const uint8_t * restrict p0,
+ const float * restrict dwp,
+ const uint32_t * restrict da0,
+ HVX_Vector * restrict out_qf,
+ uint32_t n_quads) {
+ const HVX_Vector sign_80 = Q6_Vb_vsplat_R(0x80);
+ const HVX_Vector v_dw0 = hvx_vec_f32_to_qf32(*(const HVX_Vector *) dwp);
+
+ #pragma clang loop unroll(disable)
+ for (uint32_t q = 0u; q < n_quads; ++q) {
+ const HVX_VectorPair i16_0 = Q6_W_vshuff_VVR(
+ Q6_V_vxor_VV(*(const HVX_Vector *) p0, sign_80),
+ *(const HVX_Vector *) (p0 + HTP_MM_HMX_TILE_SIZE), -1);
+ const HVX_VectorPair p0_01 = Q6_Ww_vunpack_Vh(Q6_V_lo_W(i16_0));
+ const HVX_VectorPair p0_23 = Q6_Ww_vunpack_Vh(Q6_V_hi_W(i16_0));
+
+ out_qf[0] = Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p0_01)), da0[0]), v_dw0);
+ out_qf[1] = Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p0_01)), da0[1]), v_dw0);
+ out_qf[2] = Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p0_23)), da0[2]), v_dw0);
+ out_qf[3] = Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p0_23)), da0[3]), v_dw0);
+
+ p0 += 128u;
+ da0 += 4u;
+ out_qf += 4u;
+ }
+}
+
+static __attribute__((noinline)) void w8a8_sm_eval_1g_tile_pack_f16(
+ const uint8_t * restrict p0,
+ const float * restrict dwp,
+ const uint32_t * restrict da0,
+ const HVX_Vector * restrict lo_qf,
+ HVX_Vector * restrict out_f16,
+ size_t out_vec_stride,
+ uint32_t n_quads) {
+ const HVX_Vector sign_80 = Q6_Vb_vsplat_R(0x80);
+ const HVX_Vector v_dw0 = hvx_vec_f32_to_qf32(*(const HVX_Vector *) dwp);
+
+ #pragma clang loop unroll(disable)
+ for (uint32_t q = 0u; q < n_quads; ++q) {
+ const HVX_VectorPair i16_0 = Q6_W_vshuff_VVR(
+ Q6_V_vxor_VV(*(const HVX_Vector *) p0, sign_80),
+ *(const HVX_Vector *) (p0 + HTP_MM_HMX_TILE_SIZE), -1);
+ const HVX_VectorPair p0_01 = Q6_Ww_vunpack_Vh(Q6_V_lo_W(i16_0));
+ const HVX_VectorPair p0_23 = Q6_Ww_vunpack_Vh(Q6_V_hi_W(i16_0));
+
+ const HVX_Vector hi0 = Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p0_01)), da0[0]), v_dw0);
+ const HVX_Vector hi1 = Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p0_01)), da0[1]), v_dw0);
+ const HVX_Vector hi2 = Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p0_23)), da0[2]), v_dw0);
+ const HVX_Vector hi3 = Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p0_23)), da0[3]), v_dw0);
+
+ out_f16[0u * out_vec_stride] = hvx_vec_qf32_to_f16_clamped(lo_qf[0], hi0);
+ out_f16[1u * out_vec_stride] = hvx_vec_qf32_to_f16_clamped(lo_qf[1], hi1);
+ out_f16[2u * out_vec_stride] = hvx_vec_qf32_to_f16_clamped(lo_qf[2], hi2);
+ out_f16[3u * out_vec_stride] = hvx_vec_qf32_to_f16_clamped(lo_qf[3], hi3);
+
+ p0 += 128u;
+ da0 += 4u;
+ lo_qf += 4u;
+ out_f16 += 4u * out_vec_stride;
+ }
+}
+
+static __attribute__((noinline)) void w8a8_sm_eval_2g_tile_qf32(
+ const uint8_t * restrict p0,
+ const uint8_t * restrict p1,
+ const float * restrict dwp,
+ const uint32_t * restrict da0,
+ const uint32_t * restrict da1,
+ HVX_Vector * restrict out_qf,
+ uint32_t n_quads) {
+ const HVX_Vector sign_80 = Q6_Vb_vsplat_R(0x80);
+ const HVX_Vector v_dw0 = hvx_vec_f32_to_qf32(*(const HVX_Vector *) (dwp + 0u));
+ const HVX_Vector v_dw1 = hvx_vec_f32_to_qf32(*(const HVX_Vector *) (dwp + 32u));
+
+ #pragma clang loop unroll(disable)
+ for (uint32_t q = 0u; q < n_quads; ++q) {
+ const HVX_VectorPair i16_0 = Q6_W_vshuff_VVR(
+ Q6_V_vxor_VV(*(const HVX_Vector *) p0, sign_80),
+ *(const HVX_Vector *) (p0 + HTP_MM_HMX_TILE_SIZE), -1);
+ const HVX_VectorPair i16_1 = Q6_W_vshuff_VVR(
+ Q6_V_vxor_VV(*(const HVX_Vector *) p1, sign_80),
+ *(const HVX_Vector *) (p1 + HTP_MM_HMX_TILE_SIZE), -1);
+ const HVX_VectorPair p0_01 = Q6_Ww_vunpack_Vh(Q6_V_lo_W(i16_0));
+ const HVX_VectorPair p0_23 = Q6_Ww_vunpack_Vh(Q6_V_hi_W(i16_0));
+ const HVX_VectorPair p1_01 = Q6_Ww_vunpack_Vh(Q6_V_lo_W(i16_1));
+ const HVX_VectorPair p1_23 = Q6_Ww_vunpack_Vh(Q6_V_hi_W(i16_1));
+
+ out_qf[0] = Q6_Vqf32_vadd_Vqf32Vqf32(
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p0_01)), da0[0]), v_dw0),
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p1_01)), da1[0]), v_dw1));
+ out_qf[1] = Q6_Vqf32_vadd_Vqf32Vqf32(
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p0_01)), da0[1]), v_dw0),
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p1_01)), da1[1]), v_dw1));
+ out_qf[2] = Q6_Vqf32_vadd_Vqf32Vqf32(
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p0_23)), da0[2]), v_dw0),
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p1_23)), da1[2]), v_dw1));
+ out_qf[3] = Q6_Vqf32_vadd_Vqf32Vqf32(
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p0_23)), da0[3]), v_dw0),
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p1_23)), da1[3]), v_dw1));
+
+ p0 += 128u;
+ p1 += 128u;
+ da0 += 4u;
+ da1 += 4u;
+ out_qf += 4u;
+ }
+}
+
+static __attribute__((noinline)) void w8a8_sm_eval_2g_tile_pack_f16(
+ const uint8_t * restrict p0,
+ const uint8_t * restrict p1,
+ const float * restrict dwp,
+ const uint32_t * restrict da0,
+ const uint32_t * restrict da1,
+ const HVX_Vector * restrict lo_qf,
+ HVX_Vector * restrict out_f16,
+ size_t out_vec_stride,
+ uint32_t n_quads) {
+ const HVX_Vector sign_80 = Q6_Vb_vsplat_R(0x80);
+ const HVX_Vector v_dw0 = hvx_vec_f32_to_qf32(*(const HVX_Vector *) (dwp + 0u));
+ const HVX_Vector v_dw1 = hvx_vec_f32_to_qf32(*(const HVX_Vector *) (dwp + 32u));
+
+ #pragma clang loop unroll(disable)
+ for (uint32_t q = 0u; q < n_quads; ++q) {
+ const HVX_VectorPair i16_0 = Q6_W_vshuff_VVR(
+ Q6_V_vxor_VV(*(const HVX_Vector *) p0, sign_80),
+ *(const HVX_Vector *) (p0 + HTP_MM_HMX_TILE_SIZE), -1);
+ const HVX_VectorPair i16_1 = Q6_W_vshuff_VVR(
+ Q6_V_vxor_VV(*(const HVX_Vector *) p1, sign_80),
+ *(const HVX_Vector *) (p1 + HTP_MM_HMX_TILE_SIZE), -1);
+ const HVX_VectorPair p0_01 = Q6_Ww_vunpack_Vh(Q6_V_lo_W(i16_0));
+ const HVX_VectorPair p0_23 = Q6_Ww_vunpack_Vh(Q6_V_hi_W(i16_0));
+ const HVX_VectorPair p1_01 = Q6_Ww_vunpack_Vh(Q6_V_lo_W(i16_1));
+ const HVX_VectorPair p1_23 = Q6_Ww_vunpack_Vh(Q6_V_hi_W(i16_1));
+
+ const HVX_Vector hi0 = Q6_Vqf32_vadd_Vqf32Vqf32(
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p0_01)), da0[0]), v_dw0),
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p1_01)), da1[0]), v_dw1));
+ const HVX_Vector hi1 = Q6_Vqf32_vadd_Vqf32Vqf32(
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p0_01)), da0[1]), v_dw0),
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p1_01)), da1[1]), v_dw1));
+ const HVX_Vector hi2 = Q6_Vqf32_vadd_Vqf32Vqf32(
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p0_23)), da0[2]), v_dw0),
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(p1_23)), da1[2]), v_dw1));
+ const HVX_Vector hi3 = Q6_Vqf32_vadd_Vqf32Vqf32(
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p0_23)), da0[3]), v_dw0),
+ Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_vec_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(p1_23)), da1[3]), v_dw1));
+
+ out_f16[0u * out_vec_stride] = hvx_vec_qf32_to_f16_clamped(lo_qf[0], hi0);
+ out_f16[1u * out_vec_stride] = hvx_vec_qf32_to_f16_clamped(lo_qf[1], hi1);
+ out_f16[2u * out_vec_stride] = hvx_vec_qf32_to_f16_clamped(lo_qf[2], hi2);
+ out_f16[3u * out_vec_stride] = hvx_vec_qf32_to_f16_clamped(lo_qf[3], hi3);
+
+ p0 += 128u;
+ p1 += 128u;
+ da0 += 4u;
+ da1 += 4u;
+ lo_qf += 4u;
+ out_f16 += 4u * out_vec_stride;
+ }
+}
+
#define DEFINE_W8A8_COL_CHUNK_MODE(DST_SFX, DST_T, ALIGN_MASK, STEP_1G, STEP_2G, STEP_NG, STEP_CHUNKED, SFX, STORE_MODE) \
static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_to_##DST_SFX##_col_chunk_1g_##SFX##_##STORE_MODE( \
void * restrict dst_ptr, \
@@ -4129,40 +4287,6 @@ static __attribute__((noinline)) void w8a8_sm_glu_eval_1g_quad(
w8a8_sm_eval_tile_quad_1g_none(tp, dwp, 0, da0, NULL, 0, 4, 0, true, &out[0], &out[1], &out[2], &out[3]);
}
-static __attribute__((noinline)) void w8a8_sm_glu_eval_1g_pair_hf(
- const uint8_t * restrict tp,
- const float * restrict dwp,
- const uint32_t * restrict da0,
- HVX_Vector * restrict out_hf) {
- HVX_Vector lo0, lo1, lo2, lo3, hi0, hi1, hi2, hi3;
- w8a8_sm_eval_tile_quad_1g_none(tp, dwp, 0, da0, NULL, 0, 4, 0, true, &lo0, &lo1, &lo2, &lo3);
- w8a8_sm_eval_tile_quad_1g_none(tp, dwp, 32, da0, NULL, 0, 4, 0, true, &hi0, &hi1, &hi2, &hi3);
- out_hf[0] = hvx_vec_f32_to_f16_clamped(lo0, hi0);
- out_hf[1] = hvx_vec_f32_to_f16_clamped(lo1, hi1);
- out_hf[2] = hvx_vec_f32_to_f16_clamped(lo2, hi2);
- out_hf[3] = hvx_vec_f32_to_f16_clamped(lo3, hi3);
-}
-
-static __attribute__((noinline)) void w8a8_sm_glu_eval_1g_strip_hf(
- const uint8_t * restrict tp,
- const float * restrict dwp,
- const uint32_t * restrict da0,
- __fp16 * restrict dst,
- size_t dst_stride,
- size_t n_pairs) {
- _Pragma("clang loop unroll(disable)")
- for (size_t p = 0; p < n_pairs; ++p) {
- const size_t c = p << 6;
- HVX_Vector lo0, lo1, lo2, lo3, hi0, hi1, hi2, hi3;
- w8a8_sm_eval_tile_quad_1g_none(tp, dwp, c, da0, NULL, 0, 4, 0, true, &lo0, &lo1, &lo2, &lo3);
- w8a8_sm_eval_tile_quad_1g_none(tp, dwp, c + 32, da0, NULL, 0, 4, 0, true, &hi0, &hi1, &hi2, &hi3);
- hvx_vmem(dst + c + 0 * dst_stride) = hvx_vec_f32_to_f16_clamped(lo0, hi0);
- hvx_vmem(dst + c + 1 * dst_stride) = hvx_vec_f32_to_f16_clamped(lo1, hi1);
- hvx_vmem(dst + c + 2 * dst_stride) = hvx_vec_f32_to_f16_clamped(lo2, hi2);
- hvx_vmem(dst + c + 3 * dst_stride) = hvx_vec_f32_to_f16_clamped(lo3, hi3);
- }
-}
-
static __attribute__((noinline)) void w8a8_sm_glu_eval_2g_quad(
const uint8_t * restrict tp0,
const uint8_t * restrict tp1,
@@ -4173,44 +4297,6 @@ static __attribute__((noinline)) void w8a8_sm_glu_eval_2g_quad(
w8a8_sm_eval_tile_quad_2g_none(tp0, tp1, dwp, 0, da0, da1, NULL, 0, 4, 0, true, &out[0], &out[1], &out[2], &out[3]);
}
-static __attribute__((noinline)) void w8a8_sm_glu_eval_2g_pair_hf(
- const uint8_t * restrict tp0,
- const uint8_t * restrict tp1,
- const float * restrict dwp,
- const uint32_t * restrict da0,
- const uint32_t * restrict da1,
- HVX_Vector * restrict out_hf) {
- HVX_Vector lo0, lo1, lo2, lo3, hi0, hi1, hi2, hi3;
- w8a8_sm_eval_tile_quad_2g_none(tp0, tp1, dwp, 0, da0, da1, NULL, 0, 4, 0, true, &lo0, &lo1, &lo2, &lo3);
- w8a8_sm_eval_tile_quad_2g_none(tp0, tp1, dwp, 32, da0, da1, NULL, 0, 4, 0, true, &hi0, &hi1, &hi2, &hi3);
- out_hf[0] = hvx_vec_f32_to_f16_clamped(lo0, hi0);
- out_hf[1] = hvx_vec_f32_to_f16_clamped(lo1, hi1);
- out_hf[2] = hvx_vec_f32_to_f16_clamped(lo2, hi2);
- out_hf[3] = hvx_vec_f32_to_f16_clamped(lo3, hi3);
-}
-
-static __attribute__((noinline)) void w8a8_sm_glu_eval_2g_strip_hf(
- const uint8_t * restrict tp0,
- const uint8_t * restrict tp1,
- const float * restrict dwp,
- const uint32_t * restrict da0,
- const uint32_t * restrict da1,
- __fp16 * restrict dst,
- size_t dst_stride,
- size_t n_pairs) {
- _Pragma("clang loop unroll(disable)")
- for (size_t p = 0; p < n_pairs; ++p) {
- const size_t c = p << 6;
- HVX_Vector lo0, lo1, lo2, lo3, hi0, hi1, hi2, hi3;
- w8a8_sm_eval_tile_quad_2g_none(tp0, tp1, dwp, c, da0, da1, NULL, 0, 4, 0, true, &lo0, &lo1, &lo2, &lo3);
- w8a8_sm_eval_tile_quad_2g_none(tp0, tp1, dwp, c + 32, da0, da1, NULL, 0, 4, 0, true, &hi0, &hi1, &hi2, &hi3);
- hvx_vmem(dst + c + 0 * dst_stride) = hvx_vec_f32_to_f16_clamped(lo0, hi0);
- hvx_vmem(dst + c + 1 * dst_stride) = hvx_vec_f32_to_f16_clamped(lo1, hi1);
- hvx_vmem(dst + c + 2 * dst_stride) = hvx_vec_f32_to_f16_clamped(lo2, hi2);
- hvx_vmem(dst + c + 3 * dst_stride) = hvx_vec_f32_to_f16_clamped(lo3, hi3);
- }
-}
-
static __attribute__((noinline)) void w8a8_sm_glu_eval_ng_quad(
const uint8_t * restrict tp,
const float * restrict dw,
@@ -4291,26 +4377,10 @@ static __attribute__((noinline)) void w8a8_sm_glu_pack_and_store_tail_fp16(
w8a8_sm_glu_eval_chunked_quad((tp_g), (dw_g), &cargs, (out_g)); \
w8a8_sm_glu_eval_chunked_quad((tp_u), (dw_u), &cargs, (out_u));
-#define W8A8_GLU_EVAL_PAIR_1G(tp_g, tp_u, dw_g, dw_u, out_g, out_u) \
- w8a8_sm_glu_eval_1g_pair_hf((tp_g), (dw_g), da0, (out_g)); \
- w8a8_sm_glu_eval_1g_pair_hf((tp_u), (dw_u), da0, (out_u));
-
-#define W8A8_GLU_EVAL_PAIR_2G(tp_g, tp_u, dw_g, dw_u, out_g, out_u) \
- w8a8_sm_glu_eval_2g_pair_hf((tp_g), (tp_g) + group_stride, (dw_g), da0, da1, (out_g)); \
- w8a8_sm_glu_eval_2g_pair_hf((tp_u), (tp_u) + group_stride, (dw_u), da0, da1, (out_u));
-
-#define W8A8_GLU_EVAL_STRIP_1G(tp_g, tp_u, dw_g, dw_u, dst_g, dst_s, dst_u, u_s, n_p) \
- w8a8_sm_glu_eval_1g_strip_hf((tp_g), (dw_g), da0, (dst_g), (dst_s), (n_p)); \
- w8a8_sm_glu_eval_1g_strip_hf((tp_u), (dw_u), da0, (dst_u), (u_s), (n_p));
-
-#define W8A8_GLU_EVAL_STRIP_2G(tp_g, tp_u, dw_g, dw_u, dst_g, dst_s, dst_u, u_s, n_p) \
- w8a8_sm_glu_eval_2g_strip_hf((tp_g), (tp_g) + group_stride, (dw_g), da0, da1, (dst_g), (dst_s), (n_p)); \
- w8a8_sm_glu_eval_2g_strip_hf((tp_u), (tp_u) + group_stride, (dw_u), da0, da1, (dst_u), (u_s), (n_p));
-
#define W8A8_GLU_TAIL_FP16(EVAL_TILE, DW_STRIDE_ELEMS) \
if (limit_c_aligned < limit_c) { \
- HVX_Vector g_q[8] __attribute__((aligned(128))); \
- HVX_Vector u_q[8] __attribute__((aligned(128))); \
+ HVX_Vector * g_q = spad + 0u; \
+ HVX_Vector * u_q = spad + 8u; \
const size_t c = limit_c_aligned; \
const size_t c0 = c >> 5; \
const size_t off0 = c0 * 2 * HTP_MM_HMX_TILE_SIZE; \
@@ -4325,43 +4395,13 @@ static __attribute__((noinline)) void w8a8_sm_glu_pack_and_store_tail_fp16(
w8a8_sm_glu_pack_and_store_tail_fp16(st, out_row0 + c, dst_stride, g_q, u_q, valid_rows, valid_c); \
}
-#define W8A8_GLU_LOOP_FP16_PAIR(EVAL_STRIP, EVAL_PAIR, EVAL_TILE, DW_STRIDE_ELEMS) \
- { \
- const uint32_t valid_rows = (uint32_t) hex_smin((size_t) 4u, (size_t) n_rows - r); \
- if (valid_rows == 4u) { \
- __fp16 u_buf[4 * 256] __attribute__((aligned(128))); \
- _Pragma("clang loop unroll(disable)") \
- for (size_t c = 0; c < limit_c_aligned; c += 256) { \
- const size_t n_pairs = hex_smin((limit_c_aligned - c) >> 6, (size_t) 4u); \
- const size_t c0 = c >> 5; \
- const size_t off0 = c0 * 2 * HTP_MM_HMX_TILE_SIZE; \
- const size_t dw0 = c0 * (DW_STRIDE_ELEMS); \
- EVAL_STRIP(g_base + off0, u_base + off0, dw_g_base + dw0, dw_u_base + dw0, \
- out_row0 + c, dst_stride, u_buf, 256, n_pairs) \
- st->store_strip_fn(out_row0 + c, dst_stride, u_buf, 256, n_pairs); \
- } \
- } else { \
- HVX_Vector g_hf[4] __attribute__((aligned(128))); \
- HVX_Vector u_hf[4] __attribute__((aligned(128))); \
- _Pragma("clang loop unroll(disable)") \
- for (size_t c = 0; c < limit_c_aligned; c += 64) { \
- const size_t c0 = c >> 5; \
- const size_t off0 = c0 * 2 * HTP_MM_HMX_TILE_SIZE; \
- const size_t dw0 = c0 * (DW_STRIDE_ELEMS); \
- EVAL_PAIR(g_base + off0, u_base + off0, dw_g_base + dw0, dw_u_base + dw0, g_hf, u_hf) \
- st->store_quad_fn(out_row0 + c, dst_stride, g_hf, u_hf, valid_rows); \
- } \
- } \
- W8A8_GLU_TAIL_FP16(EVAL_TILE, DW_STRIDE_ELEMS) \
- }
-
#define W8A8_GLU_LOOP_FP16_GEN(EVAL_TILE, DW_STRIDE_ELEMS) \
{ \
const uint32_t valid_rows = (uint32_t) hex_smin((size_t) 4u, (size_t) n_rows - r); \
- HVX_Vector g_q[8] __attribute__((aligned(128))); \
- HVX_Vector u_q[8] __attribute__((aligned(128))); \
- HVX_Vector g_hf[4] __attribute__((aligned(128))); \
- HVX_Vector u_hf[4] __attribute__((aligned(128))); \
+ HVX_Vector * g_q = spad + 0u; \
+ HVX_Vector * u_q = spad + 8u; \
+ HVX_Vector * g_hf = spad + 16u; \
+ HVX_Vector * u_hf = spad + 20u; \
_Pragma("clang loop unroll(disable)") \
for (size_t c = 0; c < limit_c_aligned; c += 64) { \
const size_t c0 = c >> 5; \
@@ -4373,7 +4413,7 @@ static __attribute__((noinline)) void w8a8_sm_glu_pack_and_store_tail_fp16(
EVAL_TILE(g_base + off1, u_base + off1, dw_g_base + dw1, dw_u_base + dw1, &g_q[4], &u_q[4]) \
w8a8_sm_glu_pack_quad_hf(g_q, g_hf); \
w8a8_sm_glu_pack_quad_hf(u_q, u_hf); \
- st->store_quad_fn(out_row0 + c, dst_stride, g_hf, u_hf, valid_rows); \
+ st->store_strip_fn(out_row0 + c, dst_stride, g_hf, u_hf, g_q, valid_rows); \
} \
W8A8_GLU_TAIL_FP16(EVAL_TILE, DW_STRIDE_ELEMS) \
}
@@ -4381,8 +4421,8 @@ static __attribute__((noinline)) void w8a8_sm_glu_pack_and_store_tail_fp16(
#define W8A8_GLU_LOOP_FP32(EVAL_TILE, DW_STRIDE_ELEMS) \
{ \
const uint32_t valid_rows = (uint32_t) hex_smin((size_t) 4u, (size_t) n_rows - r); \
- HVX_Vector g_q[4] __attribute__((aligned(128))); \
- HVX_Vector u_q[4] __attribute__((aligned(128))); \
+ HVX_Vector * g_q = spad + 0u; \
+ HVX_Vector * u_q = spad + 4u; \
_Pragma("clang loop unroll(disable)") \
for (size_t c = 0; c < limit_c_aligned; c += 32) { \
const size_t c0 = c >> 5; \
@@ -4402,20 +4442,20 @@ static __attribute__((noinline)) void w8a8_sm_glu_pack_and_store_tail_fp16(
} \
}
-#define W8A8_GLU_LOOP_FP16_1G W8A8_GLU_LOOP_FP16_PAIR(W8A8_GLU_EVAL_STRIP_1G, W8A8_GLU_EVAL_PAIR_1G, W8A8_GLU_EVAL_TILE_1G, 32)
-#define W8A8_GLU_LOOP_FP16_2G W8A8_GLU_LOOP_FP16_PAIR(W8A8_GLU_EVAL_STRIP_2G, W8A8_GLU_EVAL_PAIR_2G, W8A8_GLU_EVAL_TILE_2G, 64)
-#define W8A8_GLU_LOOP_FP16_NG W8A8_GLU_LOOP_FP16_GEN(W8A8_GLU_EVAL_TILE_NG, dw_tile_elems)
-#define W8A8_GLU_LOOP_FP16_CHK W8A8_GLU_LOOP_FP16_GEN(W8A8_GLU_EVAL_TILE_CHK, dw_tile_elems)
+#define W8A8_GLU_LOOP_FP16_NG W8A8_GLU_LOOP_FP16_GEN(W8A8_GLU_EVAL_TILE_NG, dw_tile_elems)
+#define W8A8_GLU_LOOP_FP16_CHK W8A8_GLU_LOOP_FP16_GEN(W8A8_GLU_EVAL_TILE_CHK, dw_tile_elems)
#define W8A8_GLU_LOOP_FP32_1G W8A8_GLU_LOOP_FP32(W8A8_GLU_EVAL_TILE_1G, 32)
#define W8A8_GLU_LOOP_FP32_2G W8A8_GLU_LOOP_FP32(W8A8_GLU_EVAL_TILE_2G, 64)
#define W8A8_GLU_LOOP_FP32_NG W8A8_GLU_LOOP_FP32(W8A8_GLU_EVAL_TILE_NG, dw_tile_elems)
#define W8A8_GLU_LOOP_FP32_CHK W8A8_GLU_LOOP_FP32(W8A8_GLU_EVAL_TILE_CHK, dw_tile_elems)
-#define DEFINE_W8A8_GLU_COL_CHUNKS(DST_SFX, DST_T, ALIGN_MASK, LOOP_1G, LOOP_2G, LOOP_NG, LOOP_CHK) \
+#define DEFINE_W8A8_GLU_COL_CHUNKS_1G_2G(DST_SFX, DST_T, ALIGN_MASK, LOOP_1G, LOOP_2G) \
static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_glu_##DST_SFX##_1g( \
const void * st_void, void * restrict dst_ptr, \
- uint32_t start_row, uint32_t n_rows, uint32_t c_first, uint32_t c_len_and_dst_cols) { \
+ uint32_t start_row, uint32_t n_rows, uint32_t c_first, uint32_t c_len_and_dst_cols, \
+ HVX_Vector * restrict spad) { \
+ (void) spad; \
const hmx_output_glu_state_t * st = (const hmx_output_glu_state_t *) st_void; \
DST_T * restrict dst = (DST_T *) dst_ptr; \
const size_t total_col_tiles = hmx_ceil_div(st->n_cols, 32); \
@@ -4450,7 +4490,9 @@ static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_glu_##DST_SF
} \
static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_glu_##DST_SFX##_2g( \
const void * st_void, void * restrict dst_ptr, \
- uint32_t start_row, uint32_t n_rows, uint32_t c_first, uint32_t c_len_and_dst_cols) { \
+ uint32_t start_row, uint32_t n_rows, uint32_t c_first, uint32_t c_len_and_dst_cols, \
+ HVX_Vector * restrict spad) { \
+ (void) spad; \
const hmx_output_glu_state_t * st = (const hmx_output_glu_state_t *) st_void; \
DST_T * restrict dst = (DST_T *) dst_ptr; \
const size_t total_col_tiles = hmx_ceil_div(st->n_cols, 32); \
@@ -4486,10 +4528,14 @@ static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_glu_##DST_SF
} \
r_base = r_end; \
} \
-} \
+}
+
+#define DEFINE_W8A8_GLU_COL_CHUNKS_NG_CHK(DST_SFX, DST_T, ALIGN_MASK, LOOP_NG, LOOP_CHK) \
static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_glu_##DST_SFX##_ng( \
const void * st_void, void * restrict dst_ptr, \
- uint32_t start_row, uint32_t n_rows, uint32_t c_first, uint32_t c_len_and_dst_cols) { \
+ uint32_t start_row, uint32_t n_rows, uint32_t c_first, uint32_t c_len_and_dst_cols, \
+ HVX_Vector * restrict spad) { \
+ (void) spad; \
const hmx_output_glu_state_t * st = (const hmx_output_glu_state_t *) st_void; \
DST_T * restrict dst = (DST_T *) dst_ptr; \
const uint32_t n_groups = st->n_groups; \
@@ -4529,7 +4575,9 @@ static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_glu_##DST_SF
} \
static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_glu_##DST_SFX##_chunked( \
const void * st_void, void * restrict dst_ptr, \
- uint32_t start_row, uint32_t n_rows, uint32_t c_first, uint32_t c_len_and_dst_cols) { \
+ uint32_t start_row, uint32_t n_rows, uint32_t c_first, uint32_t c_len_and_dst_cols, \
+ HVX_Vector * restrict spad) { \
+ (void) spad; \
const hmx_output_glu_state_t * st = (const hmx_output_glu_state_t *) st_void; \
DST_T * restrict dst = (DST_T *) dst_ptr; \
const uint32_t n_groups = st->n_groups; \
@@ -4571,8 +4619,146 @@ static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_glu_##DST_SF
} \
}
-DEFINE_W8A8_GLU_COL_CHUNKS(fp16, __fp16, ~63u, W8A8_GLU_LOOP_FP16_1G, W8A8_GLU_LOOP_FP16_2G, W8A8_GLU_LOOP_FP16_NG, W8A8_GLU_LOOP_FP16_CHK)
-DEFINE_W8A8_GLU_COL_CHUNKS(fp32, float, ~31u, W8A8_GLU_LOOP_FP32_1G, W8A8_GLU_LOOP_FP32_2G, W8A8_GLU_LOOP_FP32_NG, W8A8_GLU_LOOP_FP32_CHK)
+DEFINE_W8A8_GLU_COL_CHUNKS_NG_CHK(fp16, __fp16, ~63u, W8A8_GLU_LOOP_FP16_NG, W8A8_GLU_LOOP_FP16_CHK)
+DEFINE_W8A8_GLU_COL_CHUNKS_1G_2G(fp32, float, ~31u, W8A8_GLU_LOOP_FP32_1G, W8A8_GLU_LOOP_FP32_2G)
+DEFINE_W8A8_GLU_COL_CHUNKS_NG_CHK(fp32, float, ~31u, W8A8_GLU_LOOP_FP32_NG, W8A8_GLU_LOOP_FP32_CHK)
+
+// L1D 32x64-chunked SW-pipelined FP16 GLU paths (1g/2g)
+static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_glu_fp16_blk_1g(
+ const void * st_void, void * restrict dst_ptr,
+ uint32_t start_row, uint32_t n_rows, uint32_t c_first, uint32_t c_len_and_dst_cols,
+ HVX_Vector * restrict spad) {
+ const hmx_output_glu_state_t * st = (const hmx_output_glu_state_t *) st_void;
+ __fp16 * restrict dst = (__fp16 *) dst_ptr;
+ const size_t total_col_tiles = hmx_ceil_div(st->n_cols, 32);
+ const size_t row_tile_stride = total_col_tiles * 2 * HTP_MM_HMX_TILE_SIZE;
+ const size_t c0_first = c_first >> 5;
+ const size_t limit_c = hex_smin(c_len_and_dst_cols >> 16, c_len_and_dst_cols & 0xffffu);
+ const size_t limit_c_aligned = limit_c & ~63u;
+ const size_t dst_stride = st->dst_stride;
+ const float * dw_g_base = st->vtcm_wt_scales_gate + c0_first * 32;
+ const float * dw_u_base = st->vtcm_wt_scales_up + c0_first * 32;
+ uint32_t da_buf[1][32] __attribute__((aligned(128)));
+ HVX_Vector * lo_qf_buf = spad + 0u;
+ HVX_Vector * g_f16_buf = spad + 32u;
+ HVX_Vector * u_f16_buf = spad + 64u;
+
+ for (size_t r_base = 0; r_base < n_rows; ) {
+ const size_t r_idx_base = start_row + r_base;
+ const size_t r_blk = r_idx_base >> 5;
+ const size_t r0 = r_idx_base >> 6;
+ const size_t r_step = hex_smin((size_t) n_rows - r_base, (size_t) (32u - (r_idx_base & 31u)));
+ const size_t r_end = r_base + r_step;
+ uint32_t (*da_ptr)[32] = stage_act_scales_to_l1d(da_buf, st->vtcm_act_scales + (r_blk << 5), 0, 1);
+
+ if (limit_c_aligned > 0u) {
+ const uint32_t n_quads = (uint32_t) ((r_step + 3u) >> 2);
+ const size_t rg = (r_idx_base & 63u) >> 2;
+ const size_t lane = r_idx_base & 31u;
+ const uint32_t * da0 = &da_ptr[0][lane];
+ const size_t blk_off = r0 * row_tile_stride + c0_first * 2 * HTP_MM_HMX_TILE_SIZE + rg * 128u;
+ const uint8_t * g_blk = st->vtcm_gate + blk_off;
+ const uint8_t * u_blk = st->vtcm_up + blk_off;
+ for (size_t c = 0; c < limit_c_aligned; c += 64) {
+ const size_t off0 = (c >> 5) * 2 * HTP_MM_HMX_TILE_SIZE;
+ const uint8_t * g_lo = g_blk + off0;
+ const uint8_t * u_lo = u_blk + off0;
+ w8a8_sm_eval_1g_tile_qf32(g_lo, dw_g_base + c, da0, lo_qf_buf, n_quads);
+ w8a8_sm_eval_1g_tile_pack_f16(g_lo + 2 * HTP_MM_HMX_TILE_SIZE, dw_g_base + c + 32, da0, lo_qf_buf, g_f16_buf, 1, n_quads);
+ w8a8_sm_eval_1g_tile_qf32(u_lo, dw_u_base + c, da0, lo_qf_buf, n_quads);
+ w8a8_sm_eval_1g_tile_pack_f16(u_lo + 2 * HTP_MM_HMX_TILE_SIZE, dw_u_base + c + 32, da0, lo_qf_buf, u_f16_buf, 1, n_quads);
+ st->store_strip_fn(dst + r_base * dst_stride + c, dst_stride, g_f16_buf, u_f16_buf, lo_qf_buf, (uint32_t) r_step);
+ }
+ }
+ if (limit_c_aligned < limit_c) {
+ for (size_t r = r_base; r < r_end; r += 4) {
+ const uint32_t valid_rows = (uint32_t) hex_smin((size_t) 4u, (size_t) n_rows - r);
+ const size_t r_idx0 = start_row + r;
+ const size_t rg = (r_idx0 & 63u) >> 2;
+ const size_t lane = r_idx0 & 31u;
+ const uint32_t * da0 = &da_ptr[0][lane];
+ const size_t tile_off = r0 * row_tile_stride + c0_first * 2 * HTP_MM_HMX_TILE_SIZE + rg * 128;
+ const uint8_t * g_base = st->vtcm_gate + tile_off;
+ const uint8_t * u_base = st->vtcm_up + tile_off;
+ __fp16 * out_row0 = dst + r * dst_stride;
+ W8A8_GLU_TAIL_FP16(W8A8_GLU_EVAL_TILE_1G, 32)
+ }
+ }
+ r_base = r_end;
+ }
+}
+
+static __attribute__((noinline)) void transfer_output_chunk_w8a8_sm_glu_fp16_blk_2g(
+ const void * st_void, void * restrict dst_ptr,
+ uint32_t start_row, uint32_t n_rows, uint32_t c_first, uint32_t c_len_and_dst_cols,
+ HVX_Vector * restrict spad) {
+ const hmx_output_glu_state_t * st = (const hmx_output_glu_state_t *) st_void;
+ __fp16 * restrict dst = (__fp16 *) dst_ptr;
+ const size_t total_col_tiles = hmx_ceil_div(st->n_cols, 32);
+ const size_t n_row_tiles_64 = st->mc_pad >> 6;
+ const size_t row_tile_stride = total_col_tiles * 2 * HTP_MM_HMX_TILE_SIZE;
+ const size_t group_stride = n_row_tiles_64 * row_tile_stride;
+ const size_t c0_first = c_first >> 5;
+ const size_t limit_c = hex_smin(c_len_and_dst_cols >> 16, c_len_and_dst_cols & 0xffffu);
+ const size_t limit_c_aligned = limit_c & ~63u;
+ const size_t dst_stride = st->dst_stride;
+ const float * dw_g_base = st->vtcm_wt_scales_gate + c0_first * 64;
+ const float * dw_u_base = st->vtcm_wt_scales_up + c0_first * 64;
+ uint32_t da_buf[2][32] __attribute__((aligned(128)));
+ HVX_Vector * lo_qf_buf = spad + 0u;
+ HVX_Vector * g_f16_buf = spad + 32u;
+ HVX_Vector * u_f16_buf = spad + 64u;
+
+ for (size_t r_base = 0; r_base < n_rows; ) {
+ const size_t r_idx_base = start_row + r_base;
+ const size_t r_blk = r_idx_base >> 5;
+ const size_t r0 = r_idx_base >> 6;
+ const size_t r_step = hex_smin((size_t) n_rows - r_base, (size_t) (32u - (r_idx_base & 31u)));
+ const size_t r_end = r_base + r_step;
+ uint32_t (*da_ptr)[32] = stage_act_scales_to_l1d(da_buf, st->vtcm_act_scales + (r_blk << 5), st->mc_pad, 2);
+
+ if (limit_c_aligned > 0u) {
+ const uint32_t n_quads = (uint32_t) ((r_step + 3u) >> 2);
+ const size_t rg = (r_idx_base & 63u) >> 2;
+ const size_t lane = r_idx_base & 31u;
+ const uint32_t * da0 = &da_ptr[0][lane];
+ const uint32_t * da1 = &da_ptr[1][lane];
+ const size_t blk_off = r0 * row_tile_stride + c0_first * 2 * HTP_MM_HMX_TILE_SIZE + rg * 128u;
+ const uint8_t * g_blk = st->vtcm_gate + blk_off;
+ const uint8_t * u_blk = st->vtcm_up + blk_off;
+ for (size_t c = 0; c < limit_c_aligned; c += 64) {
+ const size_t off0 = (c >> 5) * 2 * HTP_MM_HMX_TILE_SIZE;
+ const uint8_t * g_lo = g_blk + off0;
+ const uint8_t * u_lo = u_blk + off0;
+ const float * dwg = dw_g_base + (c << 1);
+ const float * dwu = dw_u_base + (c << 1);
+ w8a8_sm_eval_2g_tile_qf32(g_lo, g_lo + group_stride, dwg, da0, da1, lo_qf_buf, n_quads);
+ w8a8_sm_eval_2g_tile_pack_f16(g_lo + 2 * HTP_MM_HMX_TILE_SIZE, g_lo + group_stride + 2 * HTP_MM_HMX_TILE_SIZE,
+ dwg + 64, da0, da1, lo_qf_buf, g_f16_buf, 1, n_quads);
+ w8a8_sm_eval_2g_tile_qf32(u_lo, u_lo + group_stride, dwu, da0, da1, lo_qf_buf, n_quads);
+ w8a8_sm_eval_2g_tile_pack_f16(u_lo + 2 * HTP_MM_HMX_TILE_SIZE, u_lo + group_stride + 2 * HTP_MM_HMX_TILE_SIZE,
+ dwu + 64, da0, da1, lo_qf_buf, u_f16_buf, 1, n_quads);
+ st->store_strip_fn(dst + r_base * dst_stride + c, dst_stride, g_f16_buf, u_f16_buf, lo_qf_buf, (uint32_t) r_step);
+ }
+ }
+ if (limit_c_aligned < limit_c) {
+ for (size_t r = r_base; r < r_end; r += 4) {
+ const uint32_t valid_rows = (uint32_t) hex_smin((size_t) 4u, (size_t) n_rows - r);
+ const size_t r_idx0 = start_row + r;
+ const size_t rg = (r_idx0 & 63u) >> 2;
+ const size_t lane = r_idx0 & 31u;
+ const uint32_t * da0 = &da_ptr[0][lane];
+ const uint32_t * da1 = &da_ptr[1][lane];
+ const size_t tile_off = r0 * row_tile_stride + c0_first * 2 * HTP_MM_HMX_TILE_SIZE + rg * 128;
+ const uint8_t * g_base = st->vtcm_gate + tile_off;
+ const uint8_t * u_base = st->vtcm_up + tile_off;
+ __fp16 * out_row0 = dst + r * dst_stride;
+ W8A8_GLU_TAIL_FP16(W8A8_GLU_EVAL_TILE_2G, 64)
+ }
+ }
+ r_base = r_end;
+ }
+}
static inline bool resolve_w8a8_output_glu_fns(
uint32_t n_groups,
@@ -4586,8 +4772,8 @@ static inline bool resolve_w8a8_output_glu_fns(
return false;
}
if (dst_elem_size == sizeof(__fp16)) {
- if (n_groups <= 1u) *col_chunk_fn = transfer_output_chunk_w8a8_sm_glu_fp16_1g;
- else if (n_groups == 2u) *col_chunk_fn = transfer_output_chunk_w8a8_sm_glu_fp16_2g;
+ if (n_groups <= 1u) *col_chunk_fn = transfer_output_chunk_w8a8_sm_glu_fp16_blk_1g;
+ else if (n_groups == 2u) *col_chunk_fn = transfer_output_chunk_w8a8_sm_glu_fp16_blk_2g;
else if (n_groups <= 16u) *col_chunk_fn = transfer_output_chunk_w8a8_sm_glu_fp16_ng;
else *col_chunk_fn = transfer_output_chunk_w8a8_sm_glu_fp16_chunked;
} else {
diff --git a/ggml/src/ggml-hexagon/htp/hmx-w8a8-utils.h b/ggml/src/ggml-hexagon/htp/hmx-w8a8-utils.h
index a88e54001..4144967f0 100644
--- a/ggml/src/ggml-hexagon/htp/hmx-w8a8-utils.h
+++ b/ggml/src/ggml-hexagon/htp/hmx-w8a8-utils.h
@@ -23,8 +23,8 @@ static inline void hmx_w8a8_emit_scales_and_bias(
float * restrict dst_scales,
HVX_Vector * restrict bias_out) {
- // |sum_k (a - zp) * w_hat| < 128 * l1_w = (l1_w << 7) <= 32768 * 2^p
- // Add half-ULP (2^(p-1) for p > 0) so truncating the low p bits rounds to nearest instead of biasing negative.
+ // |sum_k (a - 128) * w_hat| <= 127 * l1_w = (l1_w << 7) - l1_w, with (l1_w << 7) <= 32768 * 2^p
+ // Slack is >= l1_w >= 2^(p+7) for p > 0, so adding half-ULP (2^(p-1)) cannot overflow.
HVX_Vector v_max_mag = Q6_Vw_vasl_VwR(v_l1_w, 7);
HVX_Vector v_p = Q6_Vw_vmax_VwVw(Q6_V_vzero(), Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(17), Q6_Vuw_vcl0_Vuw(v_max_mag)));
HVX_Vector v_rnd = Q6_Vw_vasr_VwR(Q6_Vw_vasl_VwVw(Q6_V_vsplat_R(1), v_p), 1);
@@ -90,9 +90,9 @@ static inline void hmx_w8a8_inv127_from_max_f16(
}
static inline HVX_Vector hmx_w8a8_m_b4_from_sc_qf(HVX_Vector v_sc_qf, HVX_Vector v_inv127_qf) {
- HVX_Vector v_m_biased = hvx_vec_i32_from_qf32_rnd(Q6_Vqf32_vmpy_Vqf32Vqf32(v_sc_qf, v_inv127_qf));
- HVX_Vector v_m_h = Q6_Vh_vpacke_VwVw(v_m_biased, v_m_biased);
- return Q6_Vh_vshuff_Vh(Q6_Vb_vshuff_Vb(Q6_Vb_vpacke_VhVh(v_m_h, v_m_h)));
+ HVX_Vector v_m_u8 = hmx_qf32_to_i32_zp128(Q6_Vqf32_vmpy_Vqf32Vqf32(v_sc_qf, v_inv127_qf));
+ HVX_Vector v_m_h = Q6_Vh_vpacke_VwVw(v_m_u8, v_m_u8);
+ return Q6_V_vxor_VV(Q6_Vh_vshuff_Vh(Q6_Vb_vshuff_Vb(Q6_Vb_vpacke_VhVh(v_m_h, v_m_h))), Q6_Vb_vsplat_R(0x80));
}
static inline HVX_Vector hmx_w8a8_m_b4_from_f16(HVX_Vector v_sc, HVX_Vector v_inv127_qf) {
diff --git a/ggml/src/ggml-hexagon/htp/hvx-base.h b/ggml/src/ggml-hexagon/htp/hvx-base.h
index fecabdfc3..653e75b28 100644
--- a/ggml/src/ggml-hexagon/htp/hvx-base.h
+++ b/ggml/src/ggml-hexagon/htp/hvx-base.h
@@ -184,6 +184,14 @@ static inline HVX_Vector hvx_vec_f32_to_f16_clamped(HVX_Vector v0, HVX_Vector v1
return hvx_vec_f32_to_f16(v0, v1);
}
+static inline HVX_Vector hvx_vec_qf32_to_f16_clamped(HVX_Vector q0, HVX_Vector q1) {
+ const HVX_Vector max_hf = Q6_Vh_vsplat_R(0x7bff);
+ const HVX_Vector min_hf = Q6_Vh_vsplat_R(0xfbff);
+ HVX_Vector vh = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(q1, q0));
+ vh = Q6_Vhf_vmax_VhfVhf(min_hf, Q6_Vhf_vmin_VhfVhf(max_hf, vh));
+ return Q6_Vh_vdeal_Vh(vh);
+}
+
// Multiply 64 F16 lanes to F32: lo = lanes 0..31, hi = lanes 32..63
static inline HVX_VectorPair hvx_vec_mul_f16_f16_to_f32_pair(HVX_Vector v1, HVX_Vector v2) {
#if __HVX_ARCH__ >= 79
diff --git a/ggml/src/ggml-hexagon/htp/matmul-hmx-f16.c b/ggml/src/ggml-hexagon/htp/matmul-hmx-f16.c
index bc2317071..725884156 100644
--- a/ggml/src/ggml-hexagon/htp/matmul-hmx-f16.c
+++ b/ggml/src/ggml-hexagon/htp/matmul-hmx-f16.c
@@ -420,7 +420,7 @@ static inline tiled_dequant_task_fn_t hmx_f16_get_dequant_worker_fn(int weight_t
}
static inline bool hmx_f16_scales_in_dequant(int weight_type) {
- return weight_type == HTP_TYPE_NVFP4;
+ return weight_type == HTP_TYPE_NVFP4 || weight_type == HTP_TYPE_F8_E4M3;
}
typedef struct {
@@ -463,7 +463,8 @@ static void hmx_f16_dequant_weight_chunk(
state.n_tiles_per_task = n_tiles_per_task;
state.dst = p->vtcm_dst;
state.src = (const uint8_t *) p->weight_src_ddr;
- state.task_fn = (p->weight_type == HTP_TYPE_NVFP4 && p->w_scale) ? dequantize_tiled_weight_to_fp16_task_nvfp4_scaled
+ state.task_fn = (p->weight_type == HTP_TYPE_NVFP4 && p->w_scale) ? dequantize_tiled_weight_to_fp16_task_nvfp4_scaled
+ : (p->weight_type == HTP_TYPE_F8_E4M3 && p->w_scale) ? dequantize_tiled_weight_to_fp16_task_f8_e4m3_scaled
: p->dequant_worker_fn;
state.n_cols = p->n_cols;
state.k_block = p->k_block;
diff --git a/ggml/src/ggml-hexagon/htp/matmul-internal.h b/ggml/src/ggml-hexagon/htp/matmul-internal.h
index 51d0697a5..17e12ad6c 100644
--- a/ggml/src/ggml-hexagon/htp/matmul-internal.h
+++ b/ggml/src/ggml-hexagon/htp/matmul-internal.h
@@ -423,9 +423,10 @@ typedef void (*hmx_glu_store_tail_fn_t)(
typedef void (*hmx_glu_store_strip_fn_t)(
void * restrict dst_ptr,
size_t dst_stride,
- const __fp16 * restrict u_buf,
- size_t u_stride,
- size_t n_pairs);
+ const HVX_Vector * restrict g_f16,
+ const HVX_Vector * restrict u_f16,
+ HVX_Vector * restrict scratch,
+ uint32_t n_rows);
typedef void (*hmx_output_glu_col_chunk_fn_t)(
const void * st_void,
@@ -433,10 +434,12 @@ typedef void (*hmx_output_glu_col_chunk_fn_t)(
uint32_t start_row,
uint32_t n_rows,
uint32_t c_first,
- uint32_t c_len_and_dst_cols);
+ uint32_t c_len_and_dst_cols,
+ HVX_Vector * restrict spad);
typedef struct {
void * dst;
+ HVX_Vector * ddr_spad;
const uint8_t * vtcm_gate;
const uint8_t * vtcm_up;
const float * vtcm_act_scales;
diff --git a/ggml/src/ggml-hexagon/htp/matmul-ops.c b/ggml/src/ggml-hexagon/htp/matmul-ops.c
index 289e14c01..ec64637c9 100644
--- a/ggml/src/ggml-hexagon/htp/matmul-ops.c
+++ b/ggml/src/ggml-hexagon/htp/matmul-ops.c
@@ -163,23 +163,25 @@ __attribute__((noinline)) void hvx_rms_norm_mul_rows_f16(
HVX_Vector mean_eps = Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_VsfVsf(sum_v, denom_v), epsilon_v);
HVX_Vector scale_v = hvx_vec_rsqrt_f32(Q6_Vsf_equals_Vqf32(mean_eps));
+ const HVX_Vector v_one_f16 = Q6_Vh_vsplat_R(0x3c00);
+ #pragma unroll(2)
for (int i = 0; i < nvec; ++i) {
- HVX_VectorPair p = hvx_vec_f16_to_f32_shuff(v_row[i]);
- HVX_Vector s0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(p), scale_v));
- HVX_Vector s1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(p), scale_v));
- HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(s0, v_w[2 * i]));
- HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(s1, v_w[2 * i + 1]));
- v_row[i] = hvx_vec_f32_to_f16_shuff(r0, r1);
+ HVX_VectorPair p = Q6_Wqf32_vmpy_VhfVhf(v_row[i], v_one_f16);
+ HVX_Vector ws0 = Q6_Vqf32_vmpy_VsfVsf(v_w[2 * i], scale_v);
+ HVX_Vector ws1 = Q6_Vqf32_vmpy_VsfVsf(v_w[2 * i + 1], scale_v);
+ HVX_Vector r0 = Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(p), ws0);
+ HVX_Vector r1 = Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_hi_W(p), ws1);
+ v_row[i] = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(r1, r0));
}
if (nloe > 0) {
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
- HVX_Vector v1 = Q6_V_vand_QV(bmask, v_row[nvec]);
- HVX_VectorPair p = hvx_vec_f16_to_f32_shuff(v1);
- HVX_Vector s0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(p), scale_v));
- HVX_Vector s1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(p), scale_v));
- HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(s0, v_w[2 * nvec]));
- HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(s1, v_w[2 * nvec + 1]));
- hvx_vec_store_a(&v_row[nvec], nloe * SIZEOF_FP16, hvx_vec_f32_to_f16_shuff(r0, r1));
+ HVX_Vector v1 = Q6_V_vand_QV(bmask, v_row[nvec]);
+ HVX_VectorPair p = Q6_Wqf32_vmpy_VhfVhf(v1, v_one_f16);
+ HVX_Vector ws0 = Q6_Vqf32_vmpy_VsfVsf(v_w[2 * nvec], scale_v);
+ HVX_Vector ws1 = Q6_Vqf32_vmpy_VsfVsf(v_w[2 * nvec + 1], scale_v);
+ HVX_Vector r0 = Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(p), ws0);
+ HVX_Vector r1 = Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_hi_W(p), ws1);
+ hvx_vec_store_a(&v_row[nvec], nloe * SIZEOF_FP16, Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(r1, r0)));
}
}
}
@@ -229,28 +231,6 @@ static __attribute__((noinline)) void hmx_glu_##NAME##_store_tail_f16(
hvx_vec_store_u(dst + k * dst_stride, valid_bytes, out); \
} \
} \
-static __attribute__((noinline)) void hmx_glu_##NAME##_store_quad_f16( \
- void * restrict dst_ptr, \
- size_t dst_stride, \
- const HVX_Vector * restrict g_hf, \
- const HVX_Vector * restrict u_hf, \
- uint32_t valid_rows) { \
- __fp16 * restrict dst = (__fp16 *) dst_ptr; \
- if (valid_rows == 4u) { \
- const HVX_Vector g0 = g_hf[0]; \
- const HVX_Vector g1 = g_hf[1]; \
- const HVX_VectorPair s01 = hvx_vec_fast_sigmoid_f16_pair(ARG_F16(g0), ARG_F16(g1)); \
- hvx_vmem(dst + 0 * dst_stride) = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(g0, Q6_V_lo_W(s01)), u_hf[0]); \
- hvx_vmem(dst + 1 * dst_stride) = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(g1, Q6_V_hi_W(s01)), u_hf[1]); \
- const HVX_Vector g2 = g_hf[2]; \
- const HVX_Vector g3 = g_hf[3]; \
- const HVX_VectorPair s23 = hvx_vec_fast_sigmoid_f16_pair(ARG_F16(g2), ARG_F16(g3)); \
- hvx_vmem(dst + 2 * dst_stride) = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(g2, Q6_V_lo_W(s23)), u_hf[2]); \
- hvx_vmem(dst + 3 * dst_stride) = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(g3, Q6_V_hi_W(s23)), u_hf[3]); \
- return; \
- } \
- hmx_glu_##NAME##_store_tail_f16(dst, dst_stride, g_hf, u_hf, (valid_rows << 16) | 128u); \
-} \
static __attribute__((noinline)) void hmx_glu_##NAME##_store_tail_f32( \
void * restrict dst_ptr, \
size_t dst_stride, \
@@ -303,36 +283,58 @@ DEFINE_HMX_SIG_GLU_STORE(swiglu, HMX_GLU_ARG_SWIGLU_F16, HMX_GLU_ARG_S
DEFINE_HMX_SIG_GLU_STORE(geglu, HMX_GLU_ARG_GEGLU_F16, HMX_GLU_ARG_GEGLU_F32)
DEFINE_HMX_SIG_GLU_STORE(geglu_quick, HMX_GLU_ARG_GEGLU_QUICK_F16, HMX_GLU_ARG_GEGLU_QUICK_F32)
-#define DEFINE_HMX_SIG_GLU_STRIP_F16(NAME, ARG_F16) \
-static __attribute__((noinline)) void hmx_glu_##NAME##_store_strip_f16( \
- void * restrict dst_ptr, \
- size_t dst_stride, \
- const __fp16 * restrict u_buf, \
- size_t u_stride, \
- size_t n_pairs) { \
- __fp16 * restrict dst = (__fp16 *) dst_ptr; \
- _Pragma("clang loop unroll(disable)") \
- for (size_t p = 0; p < n_pairs; ++p) { \
- __fp16 * d0 = dst + (p << 6); \
- const __fp16 * u0 = u_buf + (p << 6); \
- const HVX_Vector g0 = hvx_vmem(d0 + 0 * dst_stride); \
- const HVX_Vector g1 = hvx_vmem(d0 + 1 * dst_stride); \
- const HVX_VectorPair s01 = hvx_vec_fast_sigmoid_f16_pair(ARG_F16(g0), ARG_F16(g1)); \
- hvx_vmem(d0 + 0 * dst_stride) = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(g0, Q6_V_lo_W(s01)), \
- hvx_vmem(u0 + 0 * u_stride)); \
- hvx_vmem(d0 + 1 * dst_stride) = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(g1, Q6_V_hi_W(s01)), \
- hvx_vmem(u0 + 1 * u_stride)); \
- const HVX_Vector g2 = hvx_vmem(d0 + 2 * dst_stride); \
- const HVX_Vector g3 = hvx_vmem(d0 + 3 * dst_stride); \
- const HVX_VectorPair s23 = hvx_vec_fast_sigmoid_f16_pair(ARG_F16(g2), ARG_F16(g3)); \
- hvx_vmem(d0 + 2 * dst_stride) = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(g2, Q6_V_lo_W(s23)), \
- hvx_vmem(u0 + 2 * u_stride)); \
- hvx_vmem(d0 + 3 * dst_stride) = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(g3, Q6_V_hi_W(s23)), \
- hvx_vmem(u0 + 3 * u_stride)); \
- } \
+static __attribute__((noinline)) void hmx_glu_sig_apply_strip_f16(
+ void * restrict dst_ptr,
+ size_t dst_stride,
+ const HVX_Vector * restrict arg_f16,
+ const HVX_Vector * restrict g_f16,
+ const HVX_Vector * restrict u_f16,
+ uint32_t n_rows) {
+ __fp16 * restrict dst = (__fp16 *) dst_ptr;
+ #pragma clang loop unroll(disable)
+ for (uint32_t k = 0; k < n_rows; ++k) {
+ const HVX_Vector sig = hvx_vec_fast_sigmoid_f16(arg_f16[k]);
+ const HVX_Vector out = Q6_Vhf_equals_Vqf16(
+ Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(g_f16[k], sig), u_f16[k]));
+ hvx_vmem(dst + (size_t) k * dst_stride) = out;
+ }
+}
+
+static __attribute__((noinline)) void hmx_glu_swiglu_store_strip_f16(
+ void * restrict dst_ptr,
+ size_t dst_stride,
+ const HVX_Vector * restrict g_f16,
+ const HVX_Vector * restrict u_f16,
+ HVX_Vector * restrict scratch,
+ uint32_t n_rows) {
+ (void) scratch;
+ __fp16 * restrict dst = (__fp16 *) dst_ptr;
+ #pragma clang loop unroll(disable)
+ for (uint32_t k = 0; k < n_rows; ++k) {
+ const HVX_Vector g = g_f16[k];
+ const HVX_Vector sig = hvx_vec_fast_sigmoid_f16(g);
+ const HVX_Vector out = Q6_Vhf_equals_Vqf16(
+ Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(g, sig), u_f16[k]));
+ hvx_vmem(dst + (size_t) k * dst_stride) = out;
+ }
+}
+
+// Compute arg into caller scratch so the sigmoid loop stays below the 32-reg SW-pipeline limit
+#define DEFINE_HMX_SIG_GLU_STRIP_F16(NAME, ARG_F16) \
+static __attribute__((noinline)) void hmx_glu_##NAME##_store_strip_f16( \
+ void * restrict dst_ptr, \
+ size_t dst_stride, \
+ const HVX_Vector * restrict g_f16, \
+ const HVX_Vector * restrict u_f16, \
+ HVX_Vector * restrict scratch, \
+ uint32_t n_rows) { \
+ _Pragma("clang loop unroll(disable)") \
+ for (uint32_t k = 0; k < n_rows; ++k) { \
+ scratch[k] = ARG_F16(g_f16[k]); \
+ } \
+ hmx_glu_sig_apply_strip_f16(dst_ptr, dst_stride, scratch, g_f16, u_f16, n_rows); \
}
-DEFINE_HMX_SIG_GLU_STRIP_F16(swiglu, HMX_GLU_ARG_SWIGLU_F16)
DEFINE_HMX_SIG_GLU_STRIP_F16(geglu, HMX_GLU_ARG_GEGLU_F16)
DEFINE_HMX_SIG_GLU_STRIP_F16(geglu_quick, HMX_GLU_ARG_GEGLU_QUICK_F16)
@@ -352,35 +354,19 @@ static __attribute__((noinline)) void hmx_glu_reglu_store_tail_f16(
}
}
-static __attribute__((noinline)) void hmx_glu_reglu_store_quad_f16(
- void * restrict dst_ptr,
- size_t dst_stride,
- const HVX_Vector * restrict g_hf,
- const HVX_Vector * restrict u_hf,
- uint32_t valid_rows) {
- __fp16 * restrict dst = (__fp16 *) dst_ptr;
- const HVX_Vector v_zero = Q6_V_vzero();
- for (uint32_t k = 0; k < valid_rows; ++k) {
- hvx_vmem(dst + k * dst_stride) = hvx_vec_mul_f16_f16(Q6_Vhf_vmax_VhfVhf(g_hf[k], v_zero), u_hf[k]);
- }
-}
-
static __attribute__((noinline)) void hmx_glu_reglu_store_strip_f16(
void * restrict dst_ptr,
size_t dst_stride,
- const __fp16 * restrict u_buf,
- size_t u_stride,
- size_t n_pairs) {
+ const HVX_Vector * restrict g_f16,
+ const HVX_Vector * restrict u_f16,
+ HVX_Vector * restrict scratch,
+ uint32_t n_rows) {
+ (void) scratch;
__fp16 * restrict dst = (__fp16 *) dst_ptr;
const HVX_Vector v_zero = Q6_V_vzero();
_Pragma("clang loop unroll(disable)")
- for (size_t p = 0; p < n_pairs; ++p) {
- __fp16 * d0 = dst + (p << 6);
- const __fp16 * u0 = u_buf + (p << 6);
- hvx_vmem(d0 + 0 * dst_stride) = hvx_vec_mul_f16_f16(Q6_Vhf_vmax_VhfVhf(hvx_vmem(d0 + 0 * dst_stride), v_zero), hvx_vmem(u0 + 0 * u_stride));
- hvx_vmem(d0 + 1 * dst_stride) = hvx_vec_mul_f16_f16(Q6_Vhf_vmax_VhfVhf(hvx_vmem(d0 + 1 * dst_stride), v_zero), hvx_vmem(u0 + 1 * u_stride));
- hvx_vmem(d0 + 2 * dst_stride) = hvx_vec_mul_f16_f16(Q6_Vhf_vmax_VhfVhf(hvx_vmem(d0 + 2 * dst_stride), v_zero), hvx_vmem(u0 + 2 * u_stride));
- hvx_vmem(d0 + 3 * dst_stride) = hvx_vec_mul_f16_f16(Q6_Vhf_vmax_VhfVhf(hvx_vmem(d0 + 3 * dst_stride), v_zero), hvx_vmem(u0 + 3 * u_stride));
+ for (uint32_t k = 0; k < n_rows; ++k) {
+ hvx_vmem(dst + (size_t) k * dst_stride) = hvx_vec_mul_f16_f16(Q6_Vhf_vmax_VhfVhf(g_f16[k], v_zero), u_f16[k]);
}
}
@@ -420,24 +406,21 @@ bool resolve_hmx_glu_store_fns(
hmx_glu_store_tail_fn_t * store_tail_fn,
hmx_glu_store_strip_fn_t * store_strip_fn) {
if (dst_elem_size == sizeof(__fp16)) {
+ *store_quad_fn = NULL;
switch (glu_op) {
case HTP_OP_GLU_SWIGLU:
- *store_quad_fn = hmx_glu_swiglu_store_quad_f16;
*store_tail_fn = hmx_glu_swiglu_store_tail_f16;
*store_strip_fn = hmx_glu_swiglu_store_strip_f16;
return true;
case HTP_OP_GLU_GEGLU:
- *store_quad_fn = hmx_glu_geglu_store_quad_f16;
*store_tail_fn = hmx_glu_geglu_store_tail_f16;
*store_strip_fn = hmx_glu_geglu_store_strip_f16;
return true;
case HTP_OP_GLU_GEGLU_QUICK:
- *store_quad_fn = hmx_glu_geglu_quick_store_quad_f16;
*store_tail_fn = hmx_glu_geglu_quick_store_tail_f16;
*store_strip_fn = hmx_glu_geglu_quick_store_strip_f16;
return true;
case HTP_OP_GLU_REGLU:
- *store_quad_fn = hmx_glu_reglu_store_quad_f16;
*store_tail_fn = hmx_glu_reglu_store_tail_f16;
*store_strip_fn = hmx_glu_reglu_store_strip_f16;
return true;
@@ -497,7 +480,8 @@ static void hmx_transfer_output_glu_col_worker(const htp_thread_t * th, unsigned
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_O_PROC, c_first);
void * dst = (uint8_t *) st->dst + (size_t) c_first * st->dst_elem_size;
const uint32_t eff_dst_cols = (uint32_t) hex_smin((size_t) c_len, (size_t) chunk_dst_cols);
- st->col_chunk_fn(st, dst, 0, st->n_rows, c_first, (c_len << 16) | eff_dst_cols);
+ HVX_Vector * spad = st->ddr_spad + (size_t) i * (3u * 32u);
+ st->col_chunk_fn(st, dst, 0, st->n_rows, c_first, (c_len << 16) | eff_dst_cols, spad);
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_O_PROC, c_first);
}
@@ -519,7 +503,8 @@ static void hmx_transfer_output_glu_row_worker(const htp_thread_t * th, unsigned
const uint32_t c_packed = (st->n_cols << 16) | eff_dst_cols;
const uint32_t chunk_size = row_end - row_start;
void * dst = (uint8_t *) st->dst + (size_t) row_start * st->dst_stride * st->dst_elem_size;
- st->col_chunk_fn(st, dst, row_start, chunk_size, 0, c_packed);
+ HVX_Vector * spad = st->ddr_spad + (size_t) i * (3u * 32u);
+ st->col_chunk_fn(st, dst, row_start, chunk_size, 0, c_packed, spad);
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_O_PROC, row_start);
}
@@ -540,6 +525,7 @@ void hmx_transfer_output_glu(
const int n_hvx = (int) octx->n_hvx;
state->dst = dst;
+ state->ddr_spad = (HVX_Vector *) octx->ctx->ddr_spad_base;
state->vtcm_gate = (const uint8_t *) vtcm_gate;
state->vtcm_up = (const uint8_t *) vtcm_up;
state->vtcm_wt_scales_gate = vtcm_wt_scales_gate;
diff --git a/scripts/snapdragon/run.py b/scripts/snapdragon/run.py
index 2e6788807..e9bfe6c35 100755
--- a/scripts/snapdragon/run.py
+++ b/scripts/snapdragon/run.py
@@ -224,7 +224,7 @@ def main():
parser.add_argument("--hex-vmem", help="Maximum NPU VMEM size limit in MB to allocate (GGML_HEXAGON_VMEM)")
parser.add_argument("--hex-mbuf", help="Host buffer size limits in MB (supports K/M/G suffix): <dyn>[,<static>[,<total>]] (default: 512,1024,0) (GGML_HEXAGON_MBUF)")
parser.add_argument("--hex-mm-select", help="Select MUL_MAT and MUL_MAT_ID kernel (GGML_HEXAGON_MM_SELECT) 3:HMX-W8A8,2:HMX,1:HVX,0:disable")
- parser.add_argument("--hex-mm-fwht", help="Enable (1) or disable (0) online block-32 FWHT rotation in W8A8 HMX matmul (GGML_HEXAGON_MM_FWHT)")
+ parser.add_argument("--hex-mm-fwht", help="Online block-32 FWHT rotation in W8A8 HMX matmul: 0=disabled, 1=auto (default, Q6_K/Q8_0/F8_E4M3), 2=always (GGML_HEXAGON_MM_FWHT)")
parser.add_argument("--hex-fa-select", help="Select Flash Attention kernel (GGML_HEXAGON_FA_SELECT) 3:HMX-W8A8,2:HMX,1:HVX,0:disable")
parser.add_argument("--hex-fa-split", "--hex-fa-head-split", dest="hex_fa_split", help="Enable (1) or disable (0) Flash Attention head/team split (GGML_HEXAGON_FA_SPLIT)")
parser.add_argument("--hex-li-select", help="Select Lightning Indexer kernel (GGML_HEXAGON_LI_SELECT) 2:HMX,1:HVX,0:disable")