mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-11-26 04:37:40 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09c1410642 | ||
|
|
28148b5f06 | ||
|
|
51ab45e3c5 | ||
|
|
caea914e9d | ||
|
|
e38ea719bd | ||
|
|
074f1f3f73 | ||
|
|
1715275226 | ||
|
|
2a59da5bf2 | ||
|
|
07ba74babd | ||
|
|
267dda8180 | ||
|
|
e0f884d405 | ||
|
|
f8b7ed5250 | ||
|
|
55b2238be8 | ||
|
|
a8ad690ca9 | ||
|
|
aee03fbe80 | ||
|
|
9c003a97ab | ||
|
|
7135e2fd69 | ||
|
|
5917b39ff7 | ||
|
|
8d35ba4f7b | ||
|
|
acb269e511 |
+1
-14
@@ -13,7 +13,6 @@
|
||||
blas,
|
||||
cudaPackages,
|
||||
autoAddDriverRunpath,
|
||||
darwin,
|
||||
rocmPackages,
|
||||
vulkan-headers,
|
||||
vulkan-loader,
|
||||
@@ -104,17 +103,6 @@ let
|
||||
ln -s /usr/bin/xcrun $out/bin
|
||||
'';
|
||||
|
||||
# apple_sdk is supposed to choose sane defaults, no need to handle isAarch64
|
||||
# separately
|
||||
darwinBuildInputs =
|
||||
with darwin.apple_sdk.frameworks;
|
||||
[
|
||||
Accelerate
|
||||
CoreVideo
|
||||
CoreGraphics
|
||||
]
|
||||
++ optionals useMetalKit [ MetalKit ];
|
||||
|
||||
cudaBuildInputs = with cudaPackages; [
|
||||
cuda_cudart
|
||||
cuda_nvml_dev
|
||||
@@ -244,8 +232,7 @@ effectiveStdenv.mkDerivation (finalAttrs: {
|
||||
++ optionals syclAot [ oclocAot ];
|
||||
|
||||
buildInputs =
|
||||
optionals effectiveStdenv.hostPlatform.isDarwin darwinBuildInputs
|
||||
++ optionals useCuda cudaBuildInputs
|
||||
optionals useCuda cudaBuildInputs
|
||||
++ optionals useMpi [ mpi ]
|
||||
++ optionals useRocm rocmBuildInputs
|
||||
++ optionals useBlas [ blas ]
|
||||
|
||||
@@ -248,6 +248,12 @@ class Inkling2Model(TextModel):
|
||||
return super().tensor_force_quant(name, new_name, bid, n_dims)
|
||||
|
||||
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
|
||||
# PEFT names the wrapped projections (self_attn.k_proj.linear.weight) and the attention
|
||||
# output gate (self_attn.o_proj.gate_proj.weight); neither is in the tensor map.
|
||||
name = name.replace(".linear.", ".")
|
||||
if name.endswith(".o_proj.gate_proj.weight"):
|
||||
yield self.format_tensor_name(gguf.MODEL_TENSOR.ATTN_GATE, bid), data_torch
|
||||
return
|
||||
if name in self._packed_experts:
|
||||
target = name.replace(".w13_weight", ".gate_up_proj").replace(".w2_weight", ".down_proj")
|
||||
yield self.map_tensor_name(target, try_suffixes=()) + ".weight", cast("Tensor", self._packed_experts[name])
|
||||
|
||||
@@ -44,7 +44,7 @@ The following releases are verified and recommended:
|
||||
|
||||
### Ubuntu 24.04
|
||||
|
||||
The release packages for Ubuntu 24.04 x64 (FP32/FP16) only include the binary files of the llama.cpp SYCL backend. They require the target machine to have pre-installed Intel GPU drivers and oneAPI packages that are the same version as the build package. To get the version and installation info, refer to [.github/workflows/release.yml#L713](../../.github/workflows/release.yml#L713): ubuntu-24-sycl -> Download & Install oneAPI.
|
||||
The release packages for Ubuntu 24.04 x64 (FP32/FP16) only include the binary files of the llama.cpp SYCL backend. They require the target machine to have pre-installed Intel GPU drivers and oneAPI packages that are the same version as the build package. To get the version and installation info, refer to [.github/workflows/release.yml#1447](../../.github/workflows/release.yml#L1447): `ubuntu-24-sycl` -> `Download & Install oneAPI`.
|
||||
|
||||
It is recommended to use them with [Intel Docker](https://hub.docker.com/r/intel/deep-learning-essentials).
|
||||
|
||||
@@ -822,6 +822,7 @@ User can use the device management in [docs/multi-gpu.md](https://github.com/ggm
|
||||
| GGML_SYCL_MEMTRACE_STEP | 64 (default) or positive integer | With GGML_SYCL_MEMTRACE=1, the minimum growth in memory usage to trigger another log record. |
|
||||
| GGML_SYCL_MKL_FA_DIAG | 0 (default) or 1 | Enable output fingerprinting for MKL flash attention. Dumps the first 64 float output values for the first 6 FA calls with n_kv ≥ 1024, labeled with kernel type (MKL/TILE/VEC) for cross-kernel comparison. |
|
||||
| GGML_SYCL_ENABLE_FUSION | 0 or 1 (default) | Enable fused-kernel dispatch in graph compute. Unsupported types and layouts fall back to the standalone op kernels. See `ggml_sycl_can_fuse()`. |
|
||||
| GGML_SYCL_FUSE_CONV_WINDOW | 0 or 1 (default) | Fuse four-tap recurrent convolution windows, rollback cache updates and SiLU into one kernel. Other shapes use the existing kernels. |
|
||||
| GGML_SYCL_ENABLE_ESIMD | 0 or 1 (default)| Enable ESIMD kernels when available. |
|
||||
| GGML_SYCL_ENABLE_XMX_ESIMD | 0 or 1 (default) | Use the XMX (DPAS) multi-column mat-vec kernels for reordered Q2_K to Q6_K and Q8_0 weights on Xe2 GPUs. Speeds up speculative decoding and short prompt batches. |
|
||||
| GGML_SYCL_XMX_MAX_COLS | non-negative integer, 80 (default) | Most activation columns (batch size) the `GGML_SYCL_ENABLE_XMX_ESIMD` kernels take; wider batches use the other mat-mul kernels. A larger value keeps XMX for bigger batches (as repeated launches), `0` never uses it. |
|
||||
|
||||
@@ -304,6 +304,22 @@ Unlike host-level tensor-splitting, row-splitting is executed entirely inside th
|
||||
You can also combine row-splitting with layer-splitting across multiple grouped devices (e.g. `--devices 'HTP0[0-1],HTP1[2-3]'`
|
||||
on 4 physical NPUs, or `--devices 'HTP0[0-1:0],HTP1[0-1:1]'` on 2 physical NPUs using virtual sessions 0 and 1).
|
||||
|
||||
## Numerical Precision and End-to-End Accuracy
|
||||
|
||||
By default, the Hexagon backend is tuned for higher NPU throughput and lower memory bandwidth while preserving strong
|
||||
end-to-end accuracy across typical models. Specifically, batched matrix multiplications default to `W8A8` integer HMX
|
||||
(`--hex-mm-select 3`), which dynamically quantizes activations into 8-bit (`uint8` x `int8`) blocks with a 2048-element
|
||||
`K`-group scale (using HVX when `M <= 4`),
|
||||
and internal `FP32` activation tensors within each HTP subgraph are automatically lowered in-place to `FP16`
|
||||
(`--hex-oplower 1`). If a particular model or workload exhibits accuracy degradation (for example due to large activation
|
||||
outliers or `FP16` residual overflow), try stepping down to higher-precision configurations in the following order:
|
||||
|
||||
1. `--hex-mm-select 4 --hex-oplower 1`: pure `W8A8` INT HMX everywhere + internal `FP16` lowering (maximum prefill speed)
|
||||
2. `--hex-mm-select 3 --hex-oplower 1` (default): hybrid `W8A8` INT HMX (FP16 HMX on post-GLU / `GGML_PREC_F32` projections) + internal `FP16` lowering
|
||||
3. `--hex-mm-select 3 --hex-oplower 0`: hybrid `W8A8` INT HMX + `FP32` intermediate activations
|
||||
4. `--hex-mm-select 2 --hex-oplower 1`: `FP16` HMX everywhere + internal `FP16` lowering
|
||||
5. `--hex-mm-select 2 --hex-oplower 0`: `FP16` HMX everywhere + `FP32` intermediate activations
|
||||
|
||||
## Environment variables
|
||||
|
||||
- `GGML_HEXAGON_DEVICES` (default: not set, defaults to HTP0 session)
|
||||
@@ -398,3 +414,27 @@ on 4 physical NPUs, or `--devices 'HTP0[0-1:0],HTP1[0-1:1]'` on 2 physical NPUs
|
||||
# Disable ADD and SUB on Hexagon (fall back to CPU or GPU)
|
||||
./scripts/snapdragon/run.py --target adb --hex-opfilter "ADD|SUB" -- llama-cli ...
|
||||
```
|
||||
|
||||
- `GGML_HEXAGON_OPLOWER=N`
|
||||
Controls internal FP32-to-FP16 activation tensor lowering inside HTP opbatches (configurable via `--hex-oplower` in `run.py`):
|
||||
|
||||
- `0`: Disabled - keep internal activations in FP32
|
||||
- `1`: Enabled (default) - lower internal `FP32` activation tensors across the opbatch to `FP16` while respecting `GGML_PREC_F32` hints
|
||||
|
||||
- `GGML_HEXAGON_MM_SELECT=N`
|
||||
Selects the matrix multiplication engine on HTP (configurable via `--hex-mm-select` in `run.py`):
|
||||
|
||||
- `0`: Disabled (falls back to CPU or GPU)
|
||||
- `1`: HVX vector kernels only
|
||||
- `2`: FP16 HMX matrix engine (`M > 4`) with HVX decode (`M <= 4`)
|
||||
- `3`: Hybrid INT HMX `W8A8` (`uint8` activations x `int8` weights) with FP16 HMX fallback on post-GLU / `GGML_PREC_F32` projections and HVX decode (`M <= 4`) (default; automatically defaults to 2 on multi-session v73 hardware unless overridden)
|
||||
- `4`: Pure INT HMX `W8A8` everywhere (`M > 4`) with HVX decode (`M <= 4`)
|
||||
|
||||
- `GGML_HEXAGON_OPPOLL=N`
|
||||
Controls host polling while waiting for HTP opbatch completion (configurable via `--hex-oppoll` in `run.py`):
|
||||
|
||||
- `0`: Blocking wait (lowest host CPU usage)
|
||||
- `1`: Full polling (keeps the host CPU awake for minimum wakeup latency; best for peak benchmarking and profiling)
|
||||
- `> 1`: Hybrid polling budget in milliseconds - polls for up to `N` ms per opbatch before falling back to a blocking wait (default: `50` when supported by the FastRPC driver, `0` otherwise)
|
||||
|
||||
The default (`50`) is tuned for general performance and thermal/power balance (covers decode batches while letting the CPU sleep during longer prefills). For benchmarking, consider more aggressive settings like `10`-`20` or `1` for full polling.
|
||||
|
||||
@@ -30,6 +30,10 @@ The Hexagon NPU backend takes advantage of Snapdragon unified memory where all D
|
||||
The NPU has dedicated tightly-coupled memory called VTCM (Vector Tightly-Coupled Memory). VTCM is used for intermediate data (such as
|
||||
dynamically quantized activations) and streaming buffers (chunks of weight and activation tensors fetched via DMA).
|
||||
|
||||
Both DDR and VTCM reserve trailing guard space so HVX kernels can issue full-width vector loads (`vmem` / `vmemu`) on tail elements without boundary branches:
|
||||
- Shared DDR buffers pad allocations to 4 KB and append a 4 KB guard page.
|
||||
- Usable VTCM (`htp_vtcm_usable_size`) reserves `HTP_VTCM_GUARD_SIZE` (128 bytes) at the end of the physical VTCM page.
|
||||
|
||||
## Large model handling
|
||||
|
||||
Hexagon NPU sessions have a 32-bit virtual address space window of around 3.5GB.
|
||||
@@ -72,34 +76,29 @@ For a weight matrix of logical shape `[ne0, ne1]` (where `ne0 = K`, `ne1 = N`):
|
||||
|
||||
### Block-32 Quantized Formats
|
||||
|
||||
Block-32 formats use a tile size of 32 rows x 32 columns (`K_cols = 32`):
|
||||
Block-32 formats use a tile size of 32 rows x 32 columns (`K_cols = 32`).
|
||||
Quants are stored in 4-K-grouped (`vrmpy`) byte order across 128-byte HVX vectors:
|
||||
columns `k = 0..31` are split into 8 groups `g = k / 4` of 4 consecutive columns (`j = k % 4`), stored at `4 * row + j` within each vector.
|
||||
|
||||
- **Q4_0** (576 bytes per tile, `HTP_TENSOR_TILE_SIZE_Q4_0`):
|
||||
- **Quants** (bytes 0..511, 512 bytes):
|
||||
- **Quants** (bytes 0..511, 512 bytes, 4 HVX vectors `p = 0..3`):
|
||||
Contains 32 rows x 32 columns of 4-bit quants.
|
||||
Divided into 16 column pairs `cp = 0..15` covering columns `2*cp` and `2*cp + 1`.
|
||||
For each column pair `cp`, 32 bytes are stored consecutively for rows `row = 0..31`:
|
||||
`tile[cp * 32 + row]` holds `(quant[row][2*cp + 1] << 4) | quant[row][2*cp]`.
|
||||
Low nibble holds the even column; high nibble holds the odd column.
|
||||
Vector `p` packs column group `2*p` in the low nibble and group `2*p + 1` in the high nibble:
|
||||
`tile[p * 128 + row * 4 + j]` holds `(quant[row][8*p + 4 + j] << 4) | quant[row][8*p + j]` for `row = 0..31`, `j = 0..3`.
|
||||
- **Scales** (bytes 512..575, 64 bytes):
|
||||
32 FP16 values (`ggml_half`), storing scale `d` for each row `row = 0..31`.
|
||||
|
||||
- **Q8_0** (1088 bytes per tile, `HTP_TENSOR_TILE_SIZE_Q8_0`):
|
||||
- **Quants** (bytes 0..1023, 1024 bytes):
|
||||
- **Quants** (bytes 0..1023, 1024 bytes, 8 HVX vectors `p = 0..7`):
|
||||
Contains 32 rows x 32 columns of signed 8-bit quants (`int8_t`).
|
||||
Divided into 16 column pairs `cp = 0..15` covering columns `col0 = 2*cp` and `col1 = 2*cp + 1`.
|
||||
For each column pair `cp`, 64 bytes store interleaved column elements for each row `row = 0..31`:
|
||||
`tile[cp * 64 + 2 * row + 0] = quant[row][col0]`
|
||||
`tile[cp * 64 + 2 * row + 1] = quant[row][col1]`
|
||||
Vector `p` holds column group `p` (`k = 4*p .. 4*p + 3`):
|
||||
`tile[p * 128 + row * 4 + j] = quant[row][4*p + j]` for `row = 0..31`, `j = 0..3`.
|
||||
- **Scales** (bytes 1024..1087, 64 bytes):
|
||||
32 FP16 values (`ggml_half`), storing scale `d` for each row `row = 0..31`.
|
||||
|
||||
- **IQ4_NL** (576 bytes per tile, `HTP_TENSOR_TILE_SIZE_IQ4_NL`):
|
||||
- **Quants** (bytes 0..511, 512 bytes):
|
||||
Same nibble transposition and storage layout as `Q4_0`.
|
||||
Contains 32 rows x 32 columns of non-linear 4-bit indices (values 0..15).
|
||||
Divided into 16 column pairs `cp = 0..15`:
|
||||
`tile[cp * 32 + row]` holds `(quant[row][2*cp + 1] << 4) | quant[row][2*cp]`.
|
||||
Same 4-K-grouped nibble layout as `Q4_0`, storing 4-bit non-linear indices (values 0..15).
|
||||
- **Scales** (bytes 512..575, 64 bytes):
|
||||
32 FP16 values (`ggml_half`), storing scale `d` for each row `row = 0..31`.
|
||||
- **Dequantization**:
|
||||
@@ -107,10 +106,7 @@ Block-32 formats use a tile size of 32 rows x 32 columns (`K_cols = 32`):
|
||||
|
||||
- **MXFP4** (544 bytes per tile, `HTP_TENSOR_TILE_SIZE_MXFP4`):
|
||||
- **Quants** (bytes 0..511, 512 bytes):
|
||||
Same nibble transposition and storage layout as `Q4_0`.
|
||||
Contains 32 rows x 32 columns of 4-bit OCP Microscaling FP4 (E2M1) values.
|
||||
Divided into 16 column pairs `cp = 0..15`:
|
||||
`tile[cp * 32 + row]` holds `(quant[row][2*cp + 1] << 4) | quant[row][2*cp]`.
|
||||
Same 4-K-grouped nibble layout as `Q4_0`, storing 4-bit OCP Microscaling FP4 (E2M1) values.
|
||||
- **Scales** (bytes 512..543, 32 bytes):
|
||||
32 unsigned 8-bit values (`uint8_t`), storing one 8-bit biased exponent (E8M0, bias 127) scale `e` per row `row = 0..31`.
|
||||
- **Dequantization**:
|
||||
@@ -119,26 +115,25 @@ Block-32 formats use a tile size of 32 rows x 32 columns (`K_cols = 32`):
|
||||
|
||||
- **NVFP4** (576 bytes per tile, `HTP_TENSOR_TILE_SIZE_NVFP4`, lossless tiling with same size as `block_nvfp4`):
|
||||
- A 64-element block spans 2 consecutive k-tiles; k-tile `h` holds sub-blocks `2h` and `2h+1`.
|
||||
- **Quants** (bytes 0..511): same nibble transposition as `Q4_0`, holding E2M1 values in element order
|
||||
(within a 16-element sub-block, `qs` byte `j` holds elements `j` and `j+8`).
|
||||
- **Quants** (bytes 0..511, 4 HVX vectors): 4-K-grouped nibble layout per 16-element sub-block `u = 0..1` (`tile[(2*u + 0)*128 + row*4 + j]` holds low nibbles of `qs_sub[j]` and `qs_sub[4+j]`; `tile[(2*u + 1)*128 + row*4 + j]` holds high nibbles).
|
||||
- **Scales** (bytes 512..575): UE4M3 bytes, `tile[512 + 32*u + row]` for sub-block `u = 0..1` of the k-tile.
|
||||
- The per-tensor F32 scale is not part of the tile.
|
||||
|
||||
- **Q4_1** (640 bytes per tile, `HTP_TENSOR_TILE_SIZE_Q4_1`):
|
||||
- **Quants** (bytes 0..511, 512 bytes):
|
||||
Same 4-bit nibble transposition and storage layout as `Q4_0`.
|
||||
Same 4-K-grouped nibble layout as `Q4_0`.
|
||||
- **Scales** (bytes 512..639, 128 bytes):
|
||||
32 pairs of FP16 values (`ggml_half`): `scale[2 * row + 0] = d` (scale) and `scale[2 * row + 1] = m` (min) for each row `row = 0..31`.
|
||||
|
||||
- **Q5_0** (704 bytes per tile, `HTP_TENSOR_TILE_SIZE_Q5_0`):
|
||||
- **Quants** (bytes 0..511): same 4-bit nibble transposition and storage layout as `Q4_0`, holding the low 4 bits.
|
||||
- **5th-bit plane** (bytes 512..639): byte `32*c + row` (`c = 0..3`) holds a flag for `k = 2*(4i + c)` at bit `2i` and for `k = 2*(4i + c) + 1` at bit `2i + 1`, for `i = 0..3`. The flag is set when the 5th bit of the quant is clear.
|
||||
- **Quants** (bytes 0..511): same 4-K-grouped nibble layout as `Q4_0`, holding the low 4 bits.
|
||||
- **5th-bit plane** (bytes 512..639, 1 HVX vector): 4-K-grouped; `plane[row * 4 + j]` (`j = 0..3`) holds a flag at bit `g` (`0..7`) for `k = 4*g + j`. The flag is set when the 5th bit of the quant is clear.
|
||||
- **Scales** (bytes 640..703, 64 bytes): 32 FP16 values, storing scale `d` for each row `row = 0..31`.
|
||||
- **Dequantization**: `0xF0` is OR-ed into the nibble where the flag is set, giving the signed value `q - 16` (-16..15), scaled by `d`.
|
||||
|
||||
- **Q5_1** (768 bytes per tile, `HTP_TENSOR_TILE_SIZE_Q5_1`, same layout as the Q5_K tile):
|
||||
- **Quants** (bytes 0..511) and **scales** (bytes 512..639): same as `Q4_1`, holding the low 4 bits and `(d, m)` pairs.
|
||||
- **5th-bit plane** (bytes 640..767): same as `Q5_K`; a bit is set when the 5th bit of the quant is set.
|
||||
- **5th-bit plane** (bytes 640..767, 1 HVX vector): 4-K-grouped; `plane[row * 4 + j]` (`j = 0..3`) holds bit `g` (`0..7`) for `k = 4*g + j`, set when the 5th bit of the quant is set.
|
||||
- Uses the `Q5_K` HVX/HMX kernels (Q8_1 activations with block sums).
|
||||
|
||||
### Q1_0 Format (1-Bit Quantization)
|
||||
@@ -209,12 +204,12 @@ Byte ordering: `Q2_K`, `Q3_K`, and `Q6_K` use `vrmpy` byte order within each 128
|
||||
- `bytes 384..511` (vec 3): FP16 scales `d * (scales[] - 32)`: `fp16[0..31]` for k 0..15, `fp16[32..63]` for k 16..31.
|
||||
|
||||
- **Q4_K** (640 bytes per tile, `HTP_TENSOR_TILE_SIZE_Q4_K`, same layout as Q4_1 tile; each 256-k super-block splits into 8 tiles of 32 k):
|
||||
- `bytes 0..511` (vec 0..3): 4-bit quants; byte `32*cp + r` holds `k = 2*cp` (low nibble) and `2*cp + 1` (high nibble).
|
||||
- `bytes 0..511` (vec 0..3): 4-bit quants in 4-K-grouped order; `tile[p * 128 + row * 4 + j]` holds `k = 8*p + j` (low nibble) and `k = 8*p + 4 + j` (high nibble).
|
||||
- `bytes 512..639` (vec 4): 32x2 FP16 per row: `(d * sc, -dmin * m)`.
|
||||
|
||||
- **Q5_K** (768 bytes per tile, `HTP_TENSOR_TILE_SIZE_Q5_K`):
|
||||
- `bytes 0..639` (vec 0..4): Q4_K tile (low 4 bits + FP16 scale/min pairs).
|
||||
- `bytes 640..767` (vec 5): 5th-bit plane; byte `l` holds the 8 high bits for lane `l` across vec `i = 0..3` (`bit 2i = lo, 2i+1 = hi`).
|
||||
- `bytes 640..767` (vec 5): 5th-bit plane in 4-K-grouped order; `plane[row * 4 + j]` holds bit `g` (`0..7`) for `k = 4*g + j`.
|
||||
|
||||
- **Q6_K** (896 bytes per tile, `HTP_TENSOR_TILE_SIZE_Q6_K`):
|
||||
- `bytes 0..511` (vec 0..3): low 4 bits; vec `i` holds group `2i` (low nibble) and `2i+1` (high nibble).
|
||||
@@ -227,8 +222,9 @@ Writing high-performance operators for Hexagon requires following specific guide
|
||||
|
||||
### DDR -> DMA -> VTCM Execution Pipeline
|
||||
|
||||
- Strongly prefer the `DDR -> DMA -> VTCM -> compute (HVX/HMX) -> VTCM -> DMA -> DDR` data flow.
|
||||
- Direct HVX reads/writes from/to DDR are less efficient and should only be used as a fallback.
|
||||
- All operators must use the `DDR -> DMA -> VTCM -> compute (HVX/HMX) -> VTCM -> DMA -> DDR` data flow.
|
||||
- **No direct reads or writes to `tensor->data`**: Do not dereference `tensor->data` pointers directly (scalar or HVX). All tensor transfers must go through DMA, because bypass DMA writes are not L1D/L2 coherent and tensors may reside in 40-bit extended address space.
|
||||
- **Rare exception (small index/position tensors)**: For small metadata tensors that require scalar indexing (such as `ids`, `pos`, or row indices), use [`htp_tensor_read()`](../../../ggml/src/ggml-hexagon/htp/htp-tensor.h) to copy into a 128-byte aligned local DDR buffer (`ddr_spad_base` or stack). It handles extended addresses, arbitrary strides, and `dccleaninva` + `syncht` cache maintenance in one place.
|
||||
- Use `dma_addr_t` only for DMA base and final addresses. Form a final address by adding a 32-bit byte offset to a
|
||||
`dma_addr_t` tensor base address. This permits a 64-bit mapped base address on newer platforms while retaining 32-bit
|
||||
relative addressing.
|
||||
@@ -244,8 +240,12 @@ Writing high-performance operators for Hexagon requires following specific guide
|
||||
properly. Flushing is only used in rare exceptions where a batch of operations is pushed without individual pops.
|
||||
- Use the DMA queue interface from [`dma-queue.h`](../../../ggml/src/ggml-hexagon/htp/dma-queue.h)
|
||||
(`dma_queue_push()`, `dma_queue_pop()`, and `dma_queue_flush()`).
|
||||
See [`cumsum-ops.c`](../../../ggml/src/ggml-hexagon/htp/cumsum-ops.c) and
|
||||
[`act-ops.c`](../../../ggml/src/ggml-hexagon/htp/act-ops.c) for reference implementations.
|
||||
- For new simple ops, prefer the generic double-buffered pipeline drivers in
|
||||
[`dma-pipeline.h`](../../../ggml/src/ggml-hexagon/htp/dma-pipeline.h) (`dma_pipe_io_run`, `dma_pipe_in_run`,
|
||||
`dma_pipe_out_run`), which use dummy pushes, `dma_queue_pop().src`/`.dst` slot recycling, and `dma_queue_flush()`
|
||||
at the end of the op. See [`cumsum-ops.c`](../../../ggml/src/ggml-hexagon/htp/cumsum-ops.c),
|
||||
[`diag-ops.c`](../../../ggml/src/ggml-hexagon/htp/diag-ops.c), and
|
||||
[`pad-ops.c`](../../../ggml/src/ggml-hexagon/htp/pad-ops.c) for reference implementations.
|
||||
|
||||
### Avoid Scalar Reads and Writes to VTCM
|
||||
|
||||
@@ -256,22 +256,22 @@ Writing high-performance operators for Hexagon requires following specific guide
|
||||
- Hexagon cores do not have hardware division instructions.
|
||||
- For recurring divisions across iterations or threads, use `fastdiv` from
|
||||
[`hex-fastdiv.h`](../../../ggml/src/ggml-hexagon/htp/hex-fastdiv.h) with precomputed divisors (such as
|
||||
`octx->ctx->mdev.count_div` or `octx->n_threads_div`).
|
||||
`octx->ctx->mdev.count_div` or `octx->n_hvx_div`).
|
||||
- Do not call `init_fastdiv_values()` for single-use divisions; use standard compiler division (`/`) instead.
|
||||
|
||||
### Host-Side Precomputation via `kernel_params`
|
||||
|
||||
- Precompute tensor shapes, strides, scale conversions, tiling layouts, and validation checks on the host CPU during graph
|
||||
preparation in [`ggml-hexagon.cpp`](../../../ggml/src/ggml-hexagon/ggml-hexagon.cpp).
|
||||
preparation in [`htp-kparams.cpp`](../../../ggml/src/ggml-hexagon/htp-kparams.cpp).
|
||||
- Pack precomputed parameters into the operator's fixed `kernel_params` structure in `htp_op_node` (such as
|
||||
`htp_mm_kernel_params`, `htp_unary_kernel_params`, `htp_fa_kernel_params`, `htp_get_rows_kernel_params`).
|
||||
- The NPU executes directly using `octx->kernel_params` without redundant runtime metadata extraction or validation.
|
||||
- **Strict Host-Kernel Alignment**:
|
||||
- Verify that parameters calculated by the host CPU are strictly honored by the NPU kernel.
|
||||
- Ensure the kernel does not ignore host-computed fields (for example, falling back to `octx->n_threads` instead of
|
||||
using `kparams->n_threads`, or ignoring precomputed `tasks_per_thread` and chunk counts).
|
||||
- Ensure the kernel does not ignore host-computed fields (for example, falling back to `octx->n_hvx` instead of
|
||||
using `kparams->n_hvx`, or ignoring precomputed `tasks_per_hvx` and chunk counts).
|
||||
- Both human developers and coding agents must audit both sides of the interface: ensure fields populated in `kernel_params`
|
||||
in [`ggml-hexagon.cpp`](../../../ggml/src/ggml-hexagon/ggml-hexagon.cpp) are actively and consistently utilized by the
|
||||
in [`htp-kparams.cpp`](../../../ggml/src/ggml-hexagon/htp-kparams.cpp) are actively and consistently utilized by the
|
||||
corresponding operator entry point and worker threads in `htp/*-ops.c`.
|
||||
|
||||
### Tracing Instrumentation
|
||||
@@ -292,7 +292,7 @@ Writing high-performance operators for Hexagon requires following specific guide
|
||||
- Distribute parallel work across NPU worker threads using the thread pool work queue:
|
||||
|
||||
```c
|
||||
work_queue_run(ctx->work_queue, worker_func, &op_ctx, n_threads);
|
||||
work_queue_run(ctx->hvx_queue, worker_func, &op_ctx, n_hvx);
|
||||
```
|
||||
|
||||
- Keep worker functions independent and re-entrant. Worker threads should only operate on their designated chunk of rows or elements.
|
||||
@@ -307,6 +307,12 @@ Writing high-performance operators for Hexagon requires following specific guide
|
||||
- **Distinction**: `octx->src[N]` pointers *can* be NULL by design and must be checked when optional.
|
||||
Examples include attention masks, optional bias or weights in fused kernels, and frequency factors.
|
||||
|
||||
### Prefer Early Dispatch and Specialized Micro-Kernels
|
||||
|
||||
- Avoid peppering micro-kernels, row or tile compute loops, or DMA worker loops with runtime checks for operand types, optional inputs, broadcast modes, or layout variants.
|
||||
- Resolve execution variants early during operator setup (`op_*` / `execute_op_*`) via function pointers or dispatch tables so each micro-kernel and inner loop handles a single clean path that the compiler can unroll and software-pipeline without extra branch or register pressure.
|
||||
- Prefer clean standalone specialized functions; use macros only to avoid genuine code duplication across type or mode instantiations.
|
||||
|
||||
### `supports_op` Independence from Buffer and Repack State
|
||||
|
||||
- `supports_op` (and op-specific checkers such as `supported_mul_mat()`, `supported_get_rows()`) runs during GGML graph scheduling **before** buffers are allocated and before tensor data is populated.
|
||||
@@ -319,6 +325,10 @@ Writing high-performance operators for Hexagon requires following specific guide
|
||||
- Repacking (converting weights to tiled layout) is an internal optimization applied during `set_tensor()` or rt-repack. Non-weight tensors or test harness tensors with the same type remain in flat layout.
|
||||
- Therefore, any supported type (such as BF16, F16, quantized types) must be supported both when repacked into tiles and when stored flat in memory.
|
||||
|
||||
### FP16 and Mixed-Precision Operator Support
|
||||
|
||||
- New and updated Hexagon compute operators must support `F16` (and mixed `F16`/`F32` where applicable for graph lowering) across activation inputs and outputs, even when `ggml-cpu` only implements `F32`.
|
||||
|
||||
### Multiline Macro Formatting
|
||||
|
||||
- Keep trailing backslashes in multiline `#define` macros cleanly aligned to a consistent column.
|
||||
@@ -361,56 +371,37 @@ python3 scripts/snapdragon/ggml-hexagon-inspect.py --addr2line 0x51a30 0x5ba54
|
||||
Multi-device (mdev) mode enables row-level tensor parallel execution across multiple physical NPU cores or virtual NPU
|
||||
sessions.
|
||||
|
||||
### 128-Byte Cache Line Alignment
|
||||
### Non-Overlapping Output Ranges
|
||||
|
||||
- Shared tensor buffers reside in DDR (RPCMEM) with a 128-byte cache line granularity
|
||||
(`HEX_L2_LINE_SIZE` = 128 bytes, `HTP_TENSOR_MDEV_LINE_SIZE`).
|
||||
- **Rule**: Multi-device work partitions that write directly to DDR through HVX/L2 must align destination write regions to
|
||||
128-byte cache line boundaries so distinct devices never share or overwrite the same cache line.
|
||||
- DMA writes to DDR are not subject to this cache-line ownership rule. They may use smaller non-overlapping destination
|
||||
ranges when the operator only writes through DMA.
|
||||
- Because all DDR writes go through bypass DMA, multi-device partitions are **not** subject to 128-byte cache-line
|
||||
alignment rules.
|
||||
- Devices only need non-overlapping destination output ranges and any kernel-specific tile or vector granularity
|
||||
(for example, 32 rows for tiled matmul or 128-byte vector chunks for flat 1D element-wise ops).
|
||||
|
||||
### Partitioning Helpers in `htp-tensor.h`
|
||||
### Partitioning Helper in `htp-tensor.h`
|
||||
|
||||
Common partitioning logic is factored into reusable inline helpers in
|
||||
[`htp-tensor.h`](../../../ggml/src/ggml-hexagon/htp/htp-tensor.h):
|
||||
Common partitioning logic is factored into [`htp_tensor_mdev_partition`](../../../ggml/src/ggml-hexagon/htp/htp-tensor.h)
|
||||
in [`htp-tensor.h`](../../../ggml/src/ggml-hexagon/htp/htp-tensor.h):
|
||||
|
||||
1. [`htp_tensor_mdev_rows_per_chunk`](../../../ggml/src/ggml-hexagon/htp/htp-tensor.h#L104):
|
||||
Determines the minimum number of rows per chunk so that the chunk byte size is a multiple of 128 bytes:
|
||||
|
||||
```
|
||||
rows_per_chunk = 128 / hex_gcd_u32(row_size, 128)
|
||||
```
|
||||
|
||||
If the active row and outer strides are already multiples of 128 bytes, `rows_per_chunk = 1`.
|
||||
Returns `false` if the tensor cannot be safely row-partitioned (such as unaligned base pointer, permuted layout,
|
||||
or non-128-byte aligned outer strides).
|
||||
|
||||
2. [`htp_tensor_mdev_partition`](../../../ggml/src/ggml-hexagon/htp/htp-tensor.h#L131):
|
||||
Calculates the per-device work range `struct htp_tensor_mdev_range { uint32_t start; uint32_t count; }` given
|
||||
`total_units`, `units_per_chunk`, `mdev_idx`, `mdev_count`, and the precomputed `mdev_count_div`.
|
||||
Handles chunk distribution across devices, assigns remainder units to the last device, and automatically triggers
|
||||
single-device fallback when partitioning is unsafe.
|
||||
- Calculates the per-device work range `struct htp_tensor_mdev_range { uint32_t start; uint32_t count; }` given
|
||||
`total_units`, `units_per_chunk`, `mdev_idx`, `mdev_count`, and the precomputed `mdev_count_div`.
|
||||
- Handles chunk distribution across devices, assigns remainder units to the last device, and automatically falls back
|
||||
to Device 0 when `units_per_chunk == 0` or `total_units / units_per_chunk < mdev_count`.
|
||||
|
||||
### Row-Partitioned Operators
|
||||
|
||||
For row-wise operators that write directly to DDR
|
||||
(such as activations in [`act-ops.c`](../../../ggml/src/ggml-hexagon/htp/act-ops.c),
|
||||
binary ops in [`binary-ops.c`](../../../ggml/src/ggml-hexagon/htp/binary-ops.c),
|
||||
For row-wise operators (such as binary ops in [`binary-ops.c`](../../../ggml/src/ggml-hexagon/htp/binary-ops.c)
|
||||
and unary ops in [`unary-ops.c`](../../../ggml/src/ggml-hexagon/htp/unary-ops.c)):
|
||||
|
||||
```c
|
||||
const uint32_t total_rows = ne01 * ne02 * ne03;
|
||||
const size_t dst_row_size = dst->ne[0] * elem_size;
|
||||
const uint32_t total_rows = ne01 * ne02 * ne03;
|
||||
|
||||
uint32_t row_start = 0;
|
||||
uint32_t nrows = total_rows;
|
||||
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
uint32_t rows_per_chunk = 0;
|
||||
htp_tensor_mdev_rows_per_chunk(dst, elem_size, (uint32_t) dst_row_size, &rows_per_chunk);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(
|
||||
total_rows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
total_rows, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
row_start = range.start;
|
||||
nrows = range.count;
|
||||
}
|
||||
@@ -422,24 +413,16 @@ if (nrows == 0) {
|
||||
|
||||
### Element-Partitioned Operators
|
||||
|
||||
For flat element-wise operations that write directly to DDR:
|
||||
- Partition total linear elements N = ne0 * ne1 * ne2 * ne3 in 128-byte cache line chunks (`elems_per_line = (elem_size == 4) ? 32 : 64`).
|
||||
- Requires strict 1D contiguity:
|
||||
[`htp_tensor_is_contiguous(dst, elem_size)`](../../../ggml/src/ggml-hexagon/htp/htp-tensor.h#L65)
|
||||
and 128-byte aligned destination pointer
|
||||
[`htp_tensor_mdev_data_aligned(dst)`](../../../ggml/src/ggml-hexagon/htp/htp-tensor.h#L84).
|
||||
- If contiguous and aligned, pass `elems_per_line` to
|
||||
[`htp_tensor_mdev_partition`](../../../ggml/src/ggml-hexagon/htp/htp-tensor.h#L131);
|
||||
otherwise pass 0 to trigger Device 0 fallback.
|
||||
For flat 1D element-wise operations:
|
||||
- Partition total linear elements `N = ne0 * ne1 * ne2 * ne3` in HVX vector-sized chunks (`elems_per_line = 128 / elem_size`).
|
||||
- Requires 1D contiguity via [`htp_tensor_is_contiguous`](../../../ggml/src/ggml-hexagon/htp/htp-tensor.h); otherwise pass
|
||||
`0` to trigger Device 0 fallback.
|
||||
|
||||
### Single-Device Fallback (Device 0)
|
||||
|
||||
- Fallback to Device 0 (`mdev.idx == 0`) when partitioning would cause cache line tearing or there are too few aligned chunks.
|
||||
- Triggers:
|
||||
1. Destination tensor cannot be safely partitioned (`rows_per_chunk == 0` or non-contiguous/unaligned buffer).
|
||||
2. Total aligned chunks < `mdev_count`.
|
||||
- Fallback to Device 0 (`mdev.idx == 0`) happens when `units_per_chunk == 0` or `total_chunks < mdev_count`.
|
||||
- Device 0 processes the entire tensor `[0, total_units)`.
|
||||
- Devices 1 ... N-1 receive `count = 0` and return `HTP_STATUS_OK` immediately.
|
||||
- Devices `1 ... N-1` receive `count = 0` and return `HTP_STATUS_OK` immediately.
|
||||
|
||||
### Flatten Outer Dimensions Globally
|
||||
|
||||
@@ -494,10 +477,7 @@ Multi-device execution synchronizes worker sessions through atomic fence slots a
|
||||
(htp_mdev_group_barrier) (htp_mdev_group_barrier)
|
||||
| |
|
||||
Kernel Execution Kernel Execution
|
||||
(Output Slice 0) (Output Slice 1)
|
||||
| |
|
||||
Tensor Cache Flush Tensor Cache Flush
|
||||
(htp_dcache_flush_dirty_tensors) (htp_dcache_flush_dirty_tensors)
|
||||
(Output Slice 0, via bypass DMA) (Output Slice 1, via bypass DMA)
|
||||
| |
|
||||
Post-Op/Batch Barrier ---------------------- Post-Op/Batch Barrier
|
||||
(htp_mdev_group_barrier) (htp_mdev_group_barrier)
|
||||
@@ -532,11 +512,8 @@ Multi-device execution synchronizes worker sessions through atomic fence slots a
|
||||
misaligned barrier arrivals across iterations.
|
||||
- If any peer reports an error status (`peer_status > HTP_STATUS_OK`), the barrier propagates the error and unblocks immediately.
|
||||
|
||||
### Tensor Cache Flush and Pipeline Completion
|
||||
### Pipeline Completion
|
||||
|
||||
- In the kernel, ensure all pushed DMA operations have been popped in strict FIFO order to drain the queue.
|
||||
- Use [`htp_dcache_flush_dirty_tensors()`](../../../ggml/src/ggml-hexagon/htp/htp-dcache.h) to flush specific dirty tensors back to DDR:
|
||||
- [`htp_dcache_flush_dirty_tensors()`](../../../ggml/src/ggml-hexagon/htp/htp-dcache.h) flushes modified tensor address ranges, or the
|
||||
full D-cache when their total size exceeds the flush threshold, ensuring peer devices and the host CPU observe consistent
|
||||
data in DDR.
|
||||
- Never signal completion before all DMA transfers are drained and dirty tensor flushes have completed.
|
||||
- In the kernel, ensure all pushed DMA operations have been popped in strict FIFO order (or flushed via `dma_queue_flush()`) to drain the queue.
|
||||
- Because all DDR writes use bypass DMA, no per-tensor output cache flush is needed.
|
||||
- Never signal completion before all DMA transfers are drained.
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ project("ggml" C CXX ASM)
|
||||
### GGML Version
|
||||
set(GGML_VERSION_MAJOR 1)
|
||||
set(GGML_VERSION_MINOR 1)
|
||||
set(GGML_VERSION_PATCH 0)
|
||||
set(GGML_VERSION_PATCH 1)
|
||||
set(GGML_VERSION_BASE "${GGML_VERSION_MAJOR}.${GGML_VERSION_MINOR}.${GGML_VERSION_PATCH}")
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
|
||||
|
||||
@@ -2449,10 +2449,12 @@ static bool ggml_backend_cann_supports_op(ggml_backend_dev_t dev, const ggml_ten
|
||||
switch (op->src[0]->type) {
|
||||
#ifndef ASCEND_310P
|
||||
case GGML_TYPE_BF16:
|
||||
return true;
|
||||
#endif
|
||||
case GGML_TYPE_F16:
|
||||
case GGML_TYPE_F32:
|
||||
return true;
|
||||
// src1 goes to aclnn as is: a BF16 src1 with F16/F32 weights is not verified
|
||||
return op->src[1]->type != GGML_TYPE_BF16;
|
||||
case GGML_TYPE_Q8_0:
|
||||
case GGML_TYPE_Q4_0:
|
||||
#ifdef ASCEND_310P
|
||||
@@ -2470,7 +2472,8 @@ static bool ggml_backend_cann_supports_op(ggml_backend_dev_t dev, const ggml_ten
|
||||
switch (op->src[0]->type) {
|
||||
case GGML_TYPE_F16:
|
||||
case GGML_TYPE_F32:
|
||||
return true;
|
||||
// src1 goes to aclnn as is: a BF16 src1 with F16/F32 weights is not verified
|
||||
return op->src[1]->type != GGML_TYPE_BF16;
|
||||
case GGML_TYPE_Q8_0:
|
||||
case GGML_TYPE_Q4_0:
|
||||
#ifdef ASCEND_310P
|
||||
|
||||
@@ -1287,6 +1287,25 @@ static void ggml_compute_forward_mul_mat_one_chunk(
|
||||
}
|
||||
}
|
||||
|
||||
// converts n BF16 values (whole blocks of vec_dot_type) to vec_dot_type: widened directly to F32, otherwise via F32 in chunks
|
||||
static void ggml_cpu_bf16_to_vec_dot_type(const ggml_bf16_t * x, void * y, int64_t n, enum ggml_type vec_dot_type) {
|
||||
if (vec_dot_type == GGML_TYPE_F32) {
|
||||
ggml_cpu_bf16_to_fp32(x, (float *) y, n);
|
||||
return;
|
||||
}
|
||||
|
||||
ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
|
||||
const int64_t bs = ggml_blck_size(vec_dot_type);
|
||||
|
||||
float tmp[1024];
|
||||
const int64_t chunk = (1024/bs)*bs;
|
||||
for (int64_t i = 0; i < n; i += chunk) {
|
||||
const int64_t nc = MIN(chunk, n - i);
|
||||
ggml_cpu_bf16_to_fp32(x + i, tmp, nc);
|
||||
from_float(tmp, (char *) y + (i/bs)*ggml_type_size(vec_dot_type), nc);
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_compute_forward_mul_mat(
|
||||
const struct ggml_compute_params * params,
|
||||
struct ggml_tensor * dst) {
|
||||
@@ -1372,11 +1391,9 @@ UseGgmlGemm1:;
|
||||
const size_t nbw3 = nbw2*ne12;
|
||||
|
||||
assert(params->wsize >= ne13*nbw3);
|
||||
// src1 is either packed from F32 into vec_dot_type, or widened from F16 or BF16 into the F32 work buffer
|
||||
const bool widen = src1->type != GGML_TYPE_F32;
|
||||
|
||||
GGML_ASSERT(!widen || vec_dot_type == GGML_TYPE_F32);
|
||||
GGML_ASSERT(!widen || src1->type == GGML_TYPE_F16 || src1->type == GGML_TYPE_BF16);
|
||||
// src1 is either packed from F32 or BF16 into vec_dot_type, or widened from F16 or BF16 into the F32 work buffer
|
||||
GGML_ASSERT(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16 ||
|
||||
(src1->type == GGML_TYPE_F16 && vec_dot_type == GGML_TYPE_F32));
|
||||
|
||||
#if 0
|
||||
for (int64_t i13 = 0; i13 < ne13; ++i13) {
|
||||
@@ -1404,7 +1421,7 @@ UseGgmlGemm1:;
|
||||
if (src1->type == GGML_TYPE_F16) {
|
||||
ggml_cpu_fp16_to_fp32((const ggml_fp16_t *) src1_row, (float *) wdata_row, ne10_block_size);
|
||||
} else if (src1->type == GGML_TYPE_BF16) {
|
||||
ggml_cpu_bf16_to_fp32((const ggml_bf16_t *) src1_row, (float *) wdata_row, ne10_block_size);
|
||||
ggml_cpu_bf16_to_vec_dot_type((const ggml_bf16_t *) src1_row, wdata_row, ne10_block_size, vec_dot_type);
|
||||
} else {
|
||||
from_float((const float *) src1_row, wdata_row, ne10_block_size);
|
||||
}
|
||||
@@ -1656,7 +1673,7 @@ static void ggml_compute_forward_mul_mat_id(
|
||||
const size_t nbw3 = nbw2*ne12;
|
||||
|
||||
assert(params->wsize >= ne13*nbw3);
|
||||
GGML_ASSERT(src1->type == GGML_TYPE_F32);
|
||||
GGML_ASSERT(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16);
|
||||
|
||||
#if 0
|
||||
for (int64_t i13 = 0; i13 < ne13; ++i13) {
|
||||
@@ -1675,9 +1692,13 @@ static void ggml_compute_forward_mul_mat_id(
|
||||
size_t bs = ggml_blck_size(vec_dot_type);
|
||||
int64_t ne10_block_start = (ith * ne10/bs) / nth;
|
||||
int64_t ne10_block_end = ((ith + 1) * ne10/bs) / nth;
|
||||
from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + ne10_block_start*bs*nb10),
|
||||
(void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1 + ne10_block_start*nbw0),
|
||||
(ne10_block_end - ne10_block_start) * bs);
|
||||
const void * src1_row = (const char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + ne10_block_start*bs*nb10;
|
||||
void * wdata_row = wdata + i13*nbw3 + i12*nbw2 + i11*nbw1 + ne10_block_start*nbw0;
|
||||
if (src1->type == GGML_TYPE_BF16) {
|
||||
ggml_cpu_bf16_to_vec_dot_type((const ggml_bf16_t *) src1_row, wdata_row, (ne10_block_end - ne10_block_start) * bs, vec_dot_type);
|
||||
} else {
|
||||
from_float((const float *) src1_row, wdata_row, (ne10_block_end - ne10_block_start) * bs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -453,14 +453,19 @@ static bool ggml_backend_cpu_device_supports_op(ggml_backend_dev_t dev, const st
|
||||
op->type != GGML_TYPE_IQ1_M; // missing type_traits.from_float
|
||||
case GGML_OP_MUL_MAT:
|
||||
if (ggml_get_op_params_i32(op, 1) == GGML_HINT_SRC0_IS_HADAMARD &&
|
||||
src0->type == GGML_TYPE_F32 && op->type == GGML_TYPE_F32) {
|
||||
return src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16;
|
||||
src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) {
|
||||
// the hinted matmul is an FWHT of src1 for every src0 type, which reads F32 or F16 only;
|
||||
// with use_ref it takes the general path instead, so src1 has to pass the check below too
|
||||
return false;
|
||||
}
|
||||
// F16 or BF16 in src1 is widened into the F32 work buffer
|
||||
return src1->type == GGML_TYPE_F32 ||
|
||||
// F16 in src1 is widened into the F32 work buffer, BF16 is widened or converted to vec_dot_type
|
||||
return src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16 ||
|
||||
src1->type == ggml_get_type_traits_cpu(src0->type)->vec_dot_type ||
|
||||
((src1->type == GGML_TYPE_F16 || src1->type == GGML_TYPE_BF16) &&
|
||||
ggml_get_type_traits_cpu(src0->type)->vec_dot_type == GGML_TYPE_F32);
|
||||
(src1->type == GGML_TYPE_F16 && ggml_get_type_traits_cpu(src0->type)->vec_dot_type == GGML_TYPE_F32);
|
||||
case GGML_OP_MUL_MAT_ID:
|
||||
// src1 is converted to vec_dot_type from F32 or BF16 only
|
||||
return src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16 ||
|
||||
src1->type == ggml_get_type_traits_cpu(src0->type)->vec_dot_type;
|
||||
case GGML_OP_SOFT_MAX_BACK: {
|
||||
if (op->src[0]->type != GGML_TYPE_F32 || op->src[1]->type != GGML_TYPE_F32) {
|
||||
return false;
|
||||
|
||||
@@ -627,7 +627,7 @@ static bool ggml_tiled_supported(const struct ggml_tensor * src0,
|
||||
case GGML_TYPE_IQ3_S:
|
||||
case GGML_TYPE_IQ1_S:
|
||||
case GGML_TYPE_IQ1_M:
|
||||
return true;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -104,38 +104,41 @@ static __global__ void __launch_bounds__(CUDA_CONCAT_BLOCK_SIZE)
|
||||
uint64_t nb13,
|
||||
int64_t ne0,
|
||||
int64_t /*ne1*/,
|
||||
int64_t /*ne2*/,
|
||||
int64_t /*ne3*/,
|
||||
int64_t ne2,
|
||||
int64_t ne3,
|
||||
uint64_t nb0,
|
||||
uint64_t nb1,
|
||||
uint64_t nb2,
|
||||
uint64_t nb3) {
|
||||
static_assert(dim >= 0 && dim <= 3, "dim must be in [0, 3]");
|
||||
|
||||
const int64_t i3 = blockIdx.z;
|
||||
const int64_t i2 = blockIdx.y;
|
||||
const int64_t i1 = blockIdx.x;
|
||||
|
||||
const T * x;
|
||||
|
||||
for (int64_t i0 = threadIdx.x; i0 < ne0; i0 += blockDim.x) {
|
||||
if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
|
||||
x = (const T *)(src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
|
||||
} else {
|
||||
if constexpr (dim == 0) {
|
||||
x = (const T *)(src1 + i3*nb13 + i2*nb12 + i1*nb11 + (i0 - ne00)*nb10);
|
||||
} else if constexpr (dim == 1) {
|
||||
x = (const T *)(src1 + i3*nb13 + i2*nb12 + (i1 - ne01)*nb11 + i0*nb10);
|
||||
} else if constexpr (dim == 2) {
|
||||
x = (const T *)(src1 + i3*nb13 + (i2 - ne02)*nb12 + i1*nb11 + i0*nb10);
|
||||
} else if constexpr (dim == 3) {
|
||||
x = (const T *)(src1 + (i3 - ne03)*nb13 + i2*nb12 + i1*nb11 + i0*nb10);
|
||||
// gridDim.y and gridDim.z are capped at 65535, loop over the rest of ne2 and ne3
|
||||
for (int64_t i3 = blockIdx.z; i3 < ne3; i3 += gridDim.z) {
|
||||
for (int64_t i2 = blockIdx.y; i2 < ne2; i2 += gridDim.y) {
|
||||
for (int64_t i0 = threadIdx.x; i0 < ne0; i0 += blockDim.x) {
|
||||
if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
|
||||
x = (const T *)(src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
|
||||
} else {
|
||||
if constexpr (dim == 0) {
|
||||
x = (const T *)(src1 + i3*nb13 + i2*nb12 + i1*nb11 + (i0 - ne00)*nb10);
|
||||
} else if constexpr (dim == 1) {
|
||||
x = (const T *)(src1 + i3*nb13 + i2*nb12 + (i1 - ne01)*nb11 + i0*nb10);
|
||||
} else if constexpr (dim == 2) {
|
||||
x = (const T *)(src1 + i3*nb13 + (i2 - ne02)*nb12 + i1*nb11 + i0*nb10);
|
||||
} else if constexpr (dim == 3) {
|
||||
x = (const T *)(src1 + (i3 - ne03)*nb13 + i2*nb12 + i1*nb11 + i0*nb10);
|
||||
}
|
||||
}
|
||||
|
||||
T * y = (T *)(dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
|
||||
|
||||
*y = *x;
|
||||
}
|
||||
}
|
||||
|
||||
T * y = (T *)(dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
|
||||
|
||||
*y = *x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,7 +166,7 @@ static void concat_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml
|
||||
} else {
|
||||
GGML_ASSERT(!ggml_is_quantized(src0->type));
|
||||
|
||||
dim3 grid_dim(dst->ne[1], dst->ne[2], dst->ne[3]);
|
||||
dim3 grid_dim(dst->ne[1], std::min<int64_t>(dst->ne[2], 65535), std::min<int64_t>(dst->ne[3], 65535));
|
||||
auto launch_kernel = [&](auto dim) {
|
||||
concat_non_cont<T, dim><<<grid_dim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(
|
||||
(const char *) src0->data, (const char *) src1->data, (char *) dst->data,
|
||||
|
||||
@@ -749,12 +749,15 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
|
||||
gqa_ratio_eff *= 2;
|
||||
}
|
||||
|
||||
// the tile kernel for DKQ=576 only has the ncols2 = 4 and 16 variants
|
||||
const best_fattn_kernel tile_kernel = Q->ne[0] == 576 && gqa_ratio % 4 != 0 ? BEST_FATTN_KERNEL_NONE : BEST_FATTN_KERNEL_TILE;
|
||||
|
||||
if (volta_mma_available(cc) && Q->ne[0] != 40 && Q->ne[0] != 72) {
|
||||
if (can_use_vector_kernel && Q->ne[1] * gqa_ratio_eff <= 2) {
|
||||
return BEST_FATTN_KERNEL_VEC;
|
||||
}
|
||||
if (Q->ne[1] * gqa_ratio_eff <= 16) {
|
||||
return BEST_FATTN_KERNEL_TILE; // On Volta tensor cores are only faster for sufficiently large matrices.
|
||||
return tile_kernel; // On Volta tensor cores are only faster for sufficiently large matrices.
|
||||
}
|
||||
return BEST_FATTN_KERNEL_MMA_F16;
|
||||
}
|
||||
@@ -800,7 +803,7 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
|
||||
}
|
||||
}
|
||||
}
|
||||
return BEST_FATTN_KERNEL_TILE;
|
||||
return tile_kernel;
|
||||
}
|
||||
|
||||
size_t ggml_cuda_flash_attn_ext_get_alloc_size(int device, const ggml_tensor * dst) {
|
||||
|
||||
+36
-19
@@ -1,9 +1,11 @@
|
||||
#include "convert.cuh"
|
||||
#include "fp8.cuh"
|
||||
|
||||
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA) && CUDART_VERSION >= 11080
|
||||
|
||||
template <typename src_t>
|
||||
struct fp8_abs_src {
|
||||
const float * x;
|
||||
const src_t * x;
|
||||
int64_t ne0;
|
||||
int64_t ne1;
|
||||
int64_t ne2;
|
||||
@@ -18,12 +20,13 @@ struct fp8_abs_src {
|
||||
i /= ne1;
|
||||
const int64_t i2 = i % ne2;
|
||||
const int64_t i3 = i / ne2;
|
||||
const float value = fabsf(x[i0 + i1*s1 + i2*s2 + i3*s3]);
|
||||
const float value = fabsf(ggml_cuda_cast<float>(x[i0 + i1*s1 + i2*s2 + i3*s3]));
|
||||
return isfinite(value) ? value : 448.0f;
|
||||
}
|
||||
};
|
||||
|
||||
static __global__ void fp8_amax_partials(fp8_abs_src src, int64_t ne, float * partials) {
|
||||
template <typename src_t>
|
||||
static __global__ void fp8_amax_partials(fp8_abs_src<src_t> src, int64_t ne, float * partials) {
|
||||
float amax = 0.0f;
|
||||
for (int64_t i = (int64_t) blockIdx.x*blockDim.x + threadIdx.x; i < ne; i += (int64_t) blockDim.x*gridDim.x) {
|
||||
amax = fmaxf(amax, src(i));
|
||||
@@ -49,8 +52,9 @@ static __global__ void fp8_amax_final(const float * partials, int n, float * ama
|
||||
}
|
||||
}
|
||||
|
||||
template <typename src_t>
|
||||
static __global__ void quantize_fp8_e4m3(
|
||||
const float * __restrict__ x, uint8_t * __restrict__ y, const float * __restrict__ amax,
|
||||
const src_t * __restrict__ x, uint8_t * __restrict__ y, const float * __restrict__ amax,
|
||||
float * __restrict__ scale, int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne, int64_t s1, int64_t s2, int64_t s3) {
|
||||
const int64_t i = (int64_t) blockIdx.x*blockDim.x + threadIdx.x;
|
||||
if (i >= ne) {
|
||||
@@ -65,12 +69,26 @@ static __global__ void quantize_fp8_e4m3(
|
||||
const int64_t i3 = tmp / ne2;
|
||||
|
||||
const float d = *amax > 0.0f ? *amax / 448.0f : 1.0f;
|
||||
y[i] = ggml_cuda_fp32_to_f8_e4m3(x[i0 + i1*s1 + i2*s2 + i3*s3] / d);
|
||||
y[i] = ggml_cuda_fp32_to_f8_e4m3(ggml_cuda_cast<float>(x[i0 + i1*s1 + i2*s2 + i3*s3]) / d);
|
||||
if (i == 0) {
|
||||
*scale = d;
|
||||
}
|
||||
}
|
||||
|
||||
// src1 (F32 or BF16) to FP8 with one scale for the whole tensor: amax / 448
|
||||
template <typename src_t>
|
||||
static void fp8_quantize_src1(
|
||||
ggml_backend_cuda_context & ctx, const src_t * x, uint8_t * y, float * amax, float * scale,
|
||||
int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne, int64_t s1, int64_t s2, int64_t s3, cudaStream_t stream) {
|
||||
const fp8_abs_src<src_t> abs_src = { x, ne0, ne1, ne2, s1, s2, s3 };
|
||||
const int reduce_blocks = std::min<int64_t>((ne + 255)/256, 1024);
|
||||
ggml_cuda_pool_alloc<float> reduce_tmp(ctx.pool(), reduce_blocks);
|
||||
fp8_amax_partials<<<reduce_blocks, 256, 0, stream>>>(abs_src, ne, reduce_tmp.ptr);
|
||||
fp8_amax_final<<<1, 256, 0, stream>>>(reduce_tmp.ptr, reduce_blocks, amax);
|
||||
|
||||
quantize_fp8_e4m3<<<(ne + 255)/256, 256, 0, stream>>>(x, y, amax, scale, ne0, ne1, ne2, ne, s1, s2, s3);
|
||||
}
|
||||
|
||||
static void fp8_set_batch(cublasLtMatrixLayout_t desc, int32_t count, int64_t stride) {
|
||||
CUBLAS_CHECK(cublasLtMatrixLayoutSetAttribute(desc, CUBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &count, sizeof(count)));
|
||||
CUBLAS_CHECK(cublasLtMatrixLayoutSetAttribute(desc, CUBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &stride, sizeof(stride)));
|
||||
@@ -89,9 +107,9 @@ bool ggml_cuda_mul_mat_fp8(
|
||||
ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
|
||||
const ggml_tensor * weight_scale) {
|
||||
const int cc = ggml_cuda_info().devices[ctx.device].cc;
|
||||
if (!fp8_mma_hardware_available(cc) || src0->type != GGML_TYPE_F8_E4M3 || src1->type != GGML_TYPE_F32 ||
|
||||
if (!fp8_mma_hardware_available(cc) || src0->type != GGML_TYPE_F8_E4M3 || (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_BF16) ||
|
||||
dst->type != GGML_TYPE_F32 || !ggml_is_contiguous(dst) || src0->ne[0] % 16 != 0 || src0->ne[1] % 16 != 0 ||
|
||||
src0->nb[0] != sizeof(uint8_t) || src0->nb[1] != (size_t) src0->ne[0] || src1->nb[0] != sizeof(float) ||
|
||||
src0->nb[0] != sizeof(uint8_t) || src0->nb[1] != (size_t) src0->ne[0] || src1->nb[0] != ggml_type_size(src1->type) ||
|
||||
!ggml_cuda_is_aligned(src0, 16) || !ggml_cuda_is_aligned(dst, 16)) {
|
||||
return false;
|
||||
}
|
||||
@@ -110,18 +128,17 @@ bool ggml_cuda_mul_mat_fp8(
|
||||
ggml_cuda_pool_alloc<float> src1_scale(ctx.pool(), 1);
|
||||
ggml_cuda_pool_alloc<float> src1_amax(ctx.pool(), 1);
|
||||
|
||||
const fp8_abs_src abs_src = {
|
||||
(const float *) src1->data, ne10, ne11, ne12,
|
||||
(int64_t) (nb11 / sizeof(float)), (int64_t) (nb12 / sizeof(float)), (int64_t) (nb13 / sizeof(float))
|
||||
};
|
||||
const int reduce_blocks = std::min<int64_t>((ne_src1 + 255)/256, 1024);
|
||||
ggml_cuda_pool_alloc<float> reduce_tmp(ctx.pool(), reduce_blocks);
|
||||
fp8_amax_partials<<<reduce_blocks, 256, 0, stream>>>(abs_src, ne_src1, reduce_tmp.ptr);
|
||||
fp8_amax_final<<<1, 256, 0, stream>>>(reduce_tmp.ptr, reduce_blocks, src1_amax.ptr);
|
||||
|
||||
quantize_fp8_e4m3<<<(ne_src1 + 255)/256, 256, 0, stream>>>(
|
||||
(const float *) src1->data, src1_fp8.ptr, src1_amax.ptr, src1_scale.ptr, ne10, ne11, ne12, ne_src1,
|
||||
nb11 / sizeof(float), nb12 / sizeof(float), nb13 / sizeof(float));
|
||||
const int64_t s11 = nb11 / nb10;
|
||||
const int64_t s12 = nb12 / nb10;
|
||||
const int64_t s13 = nb13 / nb10;
|
||||
if (src1->type == GGML_TYPE_BF16) {
|
||||
fp8_quantize_src1(ctx, (const nv_bfloat16 *) src1->data, src1_fp8.ptr, src1_amax.ptr, src1_scale.ptr,
|
||||
ne10, ne11, ne12, ne_src1, s11, s12, s13, stream);
|
||||
} else {
|
||||
GGML_ASSERT(src1->type == GGML_TYPE_F32);
|
||||
fp8_quantize_src1(ctx, (const float *) src1->data, src1_fp8.ptr, src1_amax.ptr, src1_scale.ptr,
|
||||
ne10, ne11, ne12, ne_src1, s11, s12, s13, stream);
|
||||
}
|
||||
CUDA_CHECK(cudaGetLastError());
|
||||
|
||||
cublasLtMatmulDesc_t op_desc;
|
||||
|
||||
@@ -2310,13 +2310,17 @@ static bool ggml_cuda_should_fuse_mul_mat_vec_f(const ggml_tensor * tensor, cons
|
||||
bool use_mul_mat_vec_f =
|
||||
(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 ||
|
||||
src0->type == GGML_TYPE_F8_E4M3) &&
|
||||
src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
|
||||
(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16) && dst->type == GGML_TYPE_F32;
|
||||
|
||||
const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc;
|
||||
const int warp_size = ggml_cuda_info().devices[ggml_cuda_get_device()].warp_size;
|
||||
use_mul_mat_vec_f = use_mul_mat_vec_f && ggml_cuda_should_use_mmvf(src0, cc, warp_size, is_mul_mat_id ? src1->ne[2] : src1->ne[1]);
|
||||
const int64_t ne11 = is_mul_mat_id ? src1->ne[2] : src1->ne[1];
|
||||
// a MUL_MAT_ID with an F32 src1 keeps the MUL_MAT rule here, with a BF16 src1 it takes MMVF where
|
||||
// ggml_cuda_mul_mat_id does, since MMF can not read a BF16 src1
|
||||
const bool mul_mat_id_rule = is_mul_mat_id && src1->type == GGML_TYPE_BF16;
|
||||
use_mul_mat_vec_f = use_mul_mat_vec_f && ggml_cuda_should_use_mmvf(src0, src1->type, cc, warp_size, ne11, mul_mat_id_rule);
|
||||
|
||||
if (gate && !ggml_cuda_should_use_mmvf(gate->src[0], cc, warp_size, is_mul_mat_id ? src1->ne[2] : src1->ne[1])) {
|
||||
if (gate && !ggml_cuda_should_use_mmvf(gate->src[0], src1->type, cc, warp_size, ne11, mul_mat_id_rule)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2344,7 +2348,7 @@ static bool ggml_cuda_should_fuse_mul_mat_vec_q(const ggml_tensor * tensor) {
|
||||
|
||||
const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc;
|
||||
bool use_mul_mat_vec_q = ggml_cuda_should_use_mmvq(src0->type, cc, src1->ne[1]) && !bad_padding_clear &&
|
||||
src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32;
|
||||
(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16) && dst->type == GGML_TYPE_F32;
|
||||
|
||||
// fusion is not universally faster on Pascal
|
||||
if (cc < GGML_CUDA_CC_VOLTA) {
|
||||
@@ -2466,7 +2470,8 @@ static bool ggml_cuda_mul_mat_q_fusion_matches(const ggml_tensor * up, const ggm
|
||||
const ggml_tensor * x_up = up->src[0];
|
||||
const ggml_tensor * y = up->src[1];
|
||||
|
||||
if ((up->op != GGML_OP_MUL_MAT && up->op != GGML_OP_MUL_MAT_ID) || !ggml_cuda_mmq_fusion_supported(x_up->type) || y->type != GGML_TYPE_F32) {
|
||||
if ((up->op != GGML_OP_MUL_MAT && up->op != GGML_OP_MUL_MAT_ID) || !ggml_cuda_mmq_fusion_supported(x_up->type) ||
|
||||
(y->type != GGML_TYPE_F32 && y->type != GGML_TYPE_BF16)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2544,7 +2549,8 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
|
||||
// Therefore, in such cases use cuBLAS.
|
||||
const bool bad_padding_clear = ggml_backend_buffer_get_usage(src0->buffer) == GGML_BACKEND_BUFFER_USAGE_COMPUTE
|
||||
&& ggml_nbytes(src0) != ggml_backend_buffer_get_alloc_size(src0->buffer, src0) && src0->view_src;
|
||||
if (bad_padding_clear || src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
|
||||
// MMVQ, MMQ and MMVF (BF16 and FP8 weights) also read a BF16 src1, MMF only an F32 src1
|
||||
if (bad_padding_clear || (src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_BF16) || dst->type != GGML_TYPE_F32) {
|
||||
ggml_cuda_mul_mat_cublas(ctx, src0, src1, dst);
|
||||
return;
|
||||
}
|
||||
@@ -2552,7 +2558,7 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
|
||||
const int cc = ggml_cuda_info().devices[ctx.device].cc;
|
||||
const int warp_size = ggml_cuda_info().devices[ctx.device].warp_size;
|
||||
|
||||
if (ggml_cuda_should_use_mmvf(src0, cc, warp_size, ne11)) {
|
||||
if (ggml_cuda_should_use_mmvf(src0, src1->type, cc, warp_size, ne11, /*mul_mat_id =*/ false)) {
|
||||
// The custom vector kernel can be used over batched cuBLAS GEMM.
|
||||
// But this is only faster for GPUs without tensor cores or with a thin src0 matrix (particularly KQV in attention)
|
||||
ggml_cuda_mul_mat_vec_f(ctx, src0, src1, nullptr, dst);
|
||||
@@ -2562,7 +2568,7 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
|
||||
if (ne01 == 1 && ne11 > MMVF_MAX_BATCH_SIZE && ne2 == 1 && ne3 == 1
|
||||
&& src0->type == GGML_TYPE_F32
|
||||
&& ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst)
|
||||
&& ggml_cuda_should_use_mmvf(src1, cc, warp_size, /*ne11 =*/ 1)) {
|
||||
&& ggml_cuda_should_use_mmvf(src1, src0->type, cc, warp_size, /*ne11 =*/ 1, /*mul_mat_id =*/ false)) {
|
||||
ggml_tensor dst_vec = *dst;
|
||||
dst_vec.ne[0] = ne11;
|
||||
dst_vec.ne[1] = 1;
|
||||
@@ -2572,7 +2578,7 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
|
||||
ggml_cuda_mul_mat_vec_f(ctx, src1, src0, nullptr, &dst_vec);
|
||||
return;
|
||||
}
|
||||
if (ggml_cuda_should_use_mmf(src0->type, cc, warp_size, src0->ne, src0->nb, ne11, /*mul_mat_id =*/ false)) {
|
||||
if (src1->type == GGML_TYPE_F32 && ggml_cuda_should_use_mmf(src0->type, cc, warp_size, src0->ne, src0->nb, ne11, /*mul_mat_id =*/ false)) {
|
||||
ggml_cuda_mul_mat_f(ctx, src0, src1, nullptr, dst);
|
||||
return;
|
||||
}
|
||||
@@ -2603,7 +2609,7 @@ static bool ggml_cuda_mul_mat_id_needs_sync(const ggml_tensor * dst, const int c
|
||||
const ggml_tensor * src1 = dst->src[1];
|
||||
const int warp_size = ggml_cuda_info().devices[ggml_cuda_get_device()].warp_size;
|
||||
|
||||
if (src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
|
||||
if ((src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_BF16) || dst->type != GGML_TYPE_F32) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2612,10 +2618,7 @@ static bool ggml_cuda_mul_mat_id_needs_sync(const ggml_tensor * dst, const int c
|
||||
if (dst->ne[2] <= get_mmvq_mmid_max_batch(src0->type, cc)) {
|
||||
return false;
|
||||
}
|
||||
} else if (src0->type == GGML_TYPE_F8_E4M3 &&
|
||||
ggml_cuda_should_use_mmvf(src0, cc, warp_size, dst->ne[2])) {
|
||||
return false;
|
||||
} else if (src0->type != GGML_TYPE_F8_E4M3 && GGML_CUDA_CC_IS_AMD(cc) && src0->ne[0] % 2 == 0) {
|
||||
} else if (ggml_cuda_should_use_mmvf(src0, src1->type, cc, warp_size, dst->ne[2], /*mul_mat_id =*/ true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -2628,7 +2631,7 @@ static bool ggml_cuda_mul_mat_id_needs_sync(const ggml_tensor * dst, const int c
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ggml_cuda_should_use_mmf(src0->type, cc, warp_size, src0->ne, src0->nb, src1->ne[2], /*mul_mat_id=*/true)) {
|
||||
if (src1->type == GGML_TYPE_F32 && ggml_cuda_should_use_mmf(src0->type, cc, warp_size, src0->ne, src0->nb, src1->ne[2], /*mul_mat_id=*/true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2640,7 +2643,7 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
|
||||
const ggml_tensor * src1 = dst->src[1];
|
||||
const ggml_tensor * ids = dst->src[2];
|
||||
|
||||
GGML_ASSERT(src1->type == GGML_TYPE_F32);
|
||||
GGML_ASSERT(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16);
|
||||
GGML_ASSERT(dst->type == GGML_TYPE_F32);
|
||||
|
||||
GGML_TENSOR_BINARY_OP_LOCALS
|
||||
@@ -2649,7 +2652,7 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
|
||||
const int warp_size = ggml_cuda_info().devices[ggml_cuda_get_device()].warp_size;
|
||||
|
||||
// [TAG_MUL_MAT_ID_CUDA_GRAPHS]
|
||||
if (src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
|
||||
if ((src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16) && dst->type == GGML_TYPE_F32) {
|
||||
// up to 24 tokens few-row MMQ is faster than MMQ, on DGX Spark also faster than MMVQ
|
||||
const bool use_mmq_few = ggml_cuda_should_use_mmq_few(src0, src1, ids, cc);
|
||||
|
||||
@@ -2661,16 +2664,9 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
|
||||
ggml_cuda_mul_mat_vec_q(ctx, src0, src1, ids, dst);
|
||||
return;
|
||||
}
|
||||
} else if (src0->type == GGML_TYPE_F8_E4M3 &&
|
||||
ggml_cuda_should_use_mmvf(src0, cc, warp_size, ne2)) {
|
||||
} else if (ggml_cuda_should_use_mmvf(src0, src1->type, cc, warp_size, ne2, /*mul_mat_id =*/ true)) {
|
||||
ggml_cuda_mul_mat_vec_f(ctx, src0, src1, ids, dst);
|
||||
return;
|
||||
} else {
|
||||
// MMVF reads src0 in pairs of values, so it needs an even number of columns
|
||||
if (src0->type != GGML_TYPE_F8_E4M3 && GGML_CUDA_CC_IS_AMD(cc) && ne00 % 2 == 0) {
|
||||
ggml_cuda_mul_mat_vec_f(ctx, src0, src1, ids, dst);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2684,7 +2680,7 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
|
||||
return;
|
||||
}
|
||||
|
||||
if (ggml_cuda_should_use_mmf(src0->type, cc, warp_size, src0->ne, src0->nb, src1->ne[2], /*mul_mat_id=*/true)) {
|
||||
if (src1->type == GGML_TYPE_F32 && ggml_cuda_should_use_mmf(src0->type, cc, warp_size, src0->ne, src0->nb, src1->ne[2], /*mul_mat_id=*/true)) {
|
||||
ggml_cuda_mul_mat_f(ctx, src0, src1, ids, dst);
|
||||
return;
|
||||
}
|
||||
@@ -3842,6 +3838,7 @@ static bool ggml_cuda_topk_moe_fusion(const struct ggml_cgraph * cgraph, int nod
|
||||
args.delayed_softmax = false;
|
||||
args.prob_bias = false;
|
||||
args.norm = false;
|
||||
args.norm_bias = false;
|
||||
args.n_groups = 0;
|
||||
args.n_group_used = 0;
|
||||
|
||||
@@ -3962,8 +3959,8 @@ static bool ggml_cuda_topk_moe_fusion(const struct ggml_cgraph * cgraph, int nod
|
||||
}
|
||||
|
||||
if (nodes[node_idx]->op == GGML_OP_RESHAPE) {
|
||||
//check RESHAPE->SUM_ROWS->CLAMP->DIV->RESHAPE
|
||||
static const std::vector<ggml_op> norm_ops = { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP };
|
||||
//check RESHAPE->SUM_ROWS->CLAMP|SCALE->DIV->RESHAPE
|
||||
static const std::vector<ggml_op> norm_ops = { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS };
|
||||
|
||||
args.norm = true;
|
||||
for (const ggml_op op : norm_ops) {
|
||||
@@ -3975,7 +3972,18 @@ static bool ggml_cuda_topk_moe_fusion(const struct ggml_cgraph * cgraph, int nod
|
||||
}
|
||||
}
|
||||
|
||||
// DIV <- CLAMP, RESHAPE
|
||||
// the sum is clamped from below, or a SCALE with scale 1 adds a bias (DeepSeek-V4.1, Inkling 2)
|
||||
const ggml_tensor * denom = node_idx < n_nodes ? nodes[node_idx] : nullptr;
|
||||
const bool is_clamp = denom && denom->op == GGML_OP_CLAMP;
|
||||
const bool is_bias = denom && denom->op == GGML_OP_SCALE && ggml_get_op_params_f32(denom, 0) == 1.0f;
|
||||
if ((!is_clamp && !is_bias) || denom->src[0] != nodes[node_idx - 1]) {
|
||||
args.norm = false;
|
||||
return true;
|
||||
}
|
||||
args.norm_bias = is_bias;
|
||||
node_idx++;
|
||||
|
||||
// DIV <- CLAMP|SCALE, RESHAPE
|
||||
if (nodes[node_idx]->op != GGML_OP_DIV || nodes[node_idx]->src[1] != nodes[node_idx - 1] ||
|
||||
nodes[node_idx]->src[0] != nodes[node_idx - 3]) {
|
||||
args.norm = false;
|
||||
@@ -4605,7 +4613,7 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph
|
||||
ggml_tensor * weights = nullptr;
|
||||
ggml_tensor * ids = nullptr;
|
||||
const ggml_tensor * bias = nullptr;
|
||||
const ggml_tensor * clamp = nullptr;
|
||||
const ggml_tensor * denom = nullptr;
|
||||
const ggml_tensor * scale = nullptr;
|
||||
|
||||
if (!args.delayed_softmax) {
|
||||
@@ -4633,9 +4641,9 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph
|
||||
ids = cgraph->nodes[out_nodes[0]];
|
||||
|
||||
if (args.norm) {
|
||||
ops.insert(ops.end(),
|
||||
{ GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE });
|
||||
clamp = cgraph->nodes[i + ops.size() - 3];
|
||||
ops.insert(ops.end(), { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, args.norm_bias ? GGML_OP_SCALE : GGML_OP_CLAMP,
|
||||
GGML_OP_DIV, GGML_OP_RESHAPE });
|
||||
denom = cgraph->nodes[i + ops.size() - 3];
|
||||
}
|
||||
if (args.scale) {
|
||||
ops.insert(ops.end(), { GGML_OP_SCALE });
|
||||
@@ -4648,7 +4656,7 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph
|
||||
if (ggml_can_fuse_subgraph(cgraph, i, ops.size(), ops.data(), out_nodes, 2) &&
|
||||
ggml_cuda_should_use_topk_moe(node, logits, weights, ids) &&
|
||||
ggml_cuda_check_fusion_memory_ranges(cgraph, i, ops.size(), out_nodes, 2, /*is_topk_moe=*/true)) {
|
||||
ggml_cuda_op_topk_moe(*cuda_ctx, logits, weights, ids, clamp, scale, bias, args);
|
||||
ggml_cuda_op_topk_moe(*cuda_ctx, logits, weights, ids, denom, scale, bias, args);
|
||||
return ops.size() - 1;
|
||||
}
|
||||
} else if (!args.norm && !args.prob_bias) {
|
||||
@@ -4663,7 +4671,7 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph
|
||||
if (ggml_can_fuse_subgraph(cgraph, i, ops.size(), ops.data(), out_nodes, 2) &&
|
||||
ggml_cuda_should_use_topk_moe(softmax, logits, weights, ids) &&
|
||||
ggml_cuda_check_fusion_memory_ranges(cgraph, i, ops.size(), out_nodes, 2, /*is_topk_moe=*/true)) {
|
||||
ggml_cuda_op_topk_moe(*cuda_ctx, logits, weights, ids, clamp, scale, bias, args);
|
||||
ggml_cuda_op_topk_moe(*cuda_ctx, logits, weights, ids, denom, scale, bias, args);
|
||||
return ops.size() - 1;
|
||||
}
|
||||
}
|
||||
@@ -5204,7 +5212,8 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!with_bias && src0->type == GGML_TYPE_NVFP4 && src1->type == GGML_TYPE_F32 && mm_node->type == GGML_TYPE_F32) {
|
||||
if (!with_bias && src0->type == GGML_TYPE_NVFP4 && (src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16) &&
|
||||
mm_node->type == GGML_TYPE_F32) {
|
||||
const int cc = ggml_cuda_info().devices[cuda_ctx->device].cc;
|
||||
const bool is_id = op == GGML_OP_MUL_MAT_ID;
|
||||
const int64_t ncols = is_id ? src1->ne[2] : src1->ne[1];
|
||||
@@ -5835,8 +5844,9 @@ static void ggml_backend_cuda_graph_optimize(ggml_backend_t backend, ggml_cgraph
|
||||
ids = cgraph->nodes[out_nodes[0]];
|
||||
|
||||
if (args.norm) {
|
||||
ops.insert(ops.end(),
|
||||
{ GGML_OP_RESHAPE, GGML_OP_SUM_ROWS, GGML_OP_CLAMP, GGML_OP_DIV, GGML_OP_RESHAPE });
|
||||
ops.insert(ops.end(), { GGML_OP_RESHAPE, GGML_OP_SUM_ROWS,
|
||||
args.norm_bias ? GGML_OP_SCALE : GGML_OP_CLAMP, GGML_OP_DIV,
|
||||
GGML_OP_RESHAPE });
|
||||
}
|
||||
if (args.scale) {
|
||||
ops.insert(ops.end(), { GGML_OP_SCALE });
|
||||
@@ -6468,12 +6478,16 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
|
||||
if (b->type == GGML_TYPE_F16 && a->type != GGML_TYPE_F16 && !ggml_cuda_op_mul_mat_use_fwht(op)) {
|
||||
return false;
|
||||
}
|
||||
if (op->op == GGML_OP_MUL_MAT_ID && b->type != GGML_TYPE_F32 && b->type != GGML_TYPE_BF16) {
|
||||
return false;
|
||||
}
|
||||
const int cc_dev = ggml_cuda_info().devices[dev_ctx->device].cc;
|
||||
const bool f32_src1_ok = GGML_CUDA_CC_IS_NVIDIA(cc_dev) && cc_dev < GGML_CUDA_CC_AMPERE;
|
||||
const enum ggml_prec prec_src1 = ggml_prec_get_src(op, 1);
|
||||
const bool fp8_id_mmvf = op->op == GGML_OP_MUL_MAT_ID &&
|
||||
a->type == GGML_TYPE_F8_E4M3 && b->type == GGML_TYPE_F32 && op->type == GGML_TYPE_F32 &&
|
||||
ggml_cuda_should_use_mmvf(a, cc_dev, ggml_cuda_info().devices[dev_ctx->device].warp_size, b->ne[2]);
|
||||
a->type == GGML_TYPE_F8_E4M3 && (b->type == GGML_TYPE_F32 || b->type == GGML_TYPE_BF16) && op->type == GGML_TYPE_F32 &&
|
||||
ggml_cuda_should_use_mmvf(a, b->type, cc_dev, ggml_cuda_info().devices[dev_ctx->device].warp_size, b->ne[2],
|
||||
/*mul_mat_id =*/ true);
|
||||
if (prec_src1 == GGML_PREC_F32 && !fp8_id_mmvf && (op->op == GGML_OP_MUL_MAT_ID || !f32_src1_ok || a->type != GGML_TYPE_F32)) {
|
||||
return false;
|
||||
}
|
||||
@@ -6488,7 +6502,7 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
|
||||
return false;
|
||||
}
|
||||
if (GGML_CUDA_CC_IS_QY2(cc) && op->op == GGML_OP_MUL_MAT_ID &&
|
||||
a->type == GGML_TYPE_Q2_K && b->type == GGML_TYPE_F32) {
|
||||
a->type == GGML_TYPE_Q2_K && (b->type == GGML_TYPE_F32 || b->type == GGML_TYPE_BF16)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,9 +98,9 @@ static __global__ void mul_mat_f(
|
||||
const int sample_x = sample_dst / sample_ratio;
|
||||
const int sample_y = sample_dst;
|
||||
|
||||
x += int64_t(sample_x) *stride_sample_x + channel_x *stride_channel_x + row0*stride_row ;
|
||||
y += int64_t(sample_y) *stride_sample_y + channel_y *stride_channel_y;
|
||||
dst += int64_t(sample_dst)*stride_sample_dst + channel_dst*stride_channel_dst;
|
||||
x += int64_t(sample_x) *stride_sample_x + int64_t(channel_x) *stride_channel_x + int64_t(row0)*stride_row;
|
||||
y += int64_t(sample_y) *stride_sample_y + int64_t(channel_y) *stride_channel_y;
|
||||
dst += int64_t(sample_dst)*stride_sample_dst + int64_t(channel_dst)*stride_channel_dst;
|
||||
|
||||
[[maybe_unused]] const float2 * y2 = (const float2 *) y;
|
||||
|
||||
@@ -290,7 +290,7 @@ static __global__ void mul_mat_f_ids(
|
||||
const int sample_x = sample_dst / sample_ratio;
|
||||
const int sample_y = sample_dst;
|
||||
|
||||
x += int64_t(sample_x) *stride_sample_x + channel_x *stride_channel_x + row0*stride_row;
|
||||
x += int64_t(sample_x) *stride_sample_x + int64_t(channel_x) *stride_channel_x + int64_t(row0)*stride_row;
|
||||
y += int64_t(sample_y) *stride_sample_y;
|
||||
dst += int64_t(sample_dst)*stride_sample_dst;
|
||||
|
||||
|
||||
@@ -799,10 +799,10 @@ static void launch_mul_mat_q_few_switch_ncg(const ggml_tensor * src0, const ggml
|
||||
void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst) {
|
||||
GGML_TENSOR_BINARY_OP_LOCALS;
|
||||
|
||||
GGML_ASSERT(src1->type == GGML_TYPE_F32);
|
||||
GGML_ASSERT(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16);
|
||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
||||
GGML_ASSERT(nb00 == ggml_type_size(src0->type));
|
||||
GGML_ASSERT(nb10 == sizeof(float));
|
||||
GGML_ASSERT(nb10 == ggml_type_size(src1->type));
|
||||
GGML_ASSERT(nb0 == sizeof(float));
|
||||
GGML_ASSERT(ne00 % QK_K == 0);
|
||||
|
||||
@@ -828,14 +828,14 @@ void ggml_cuda_mul_mat_q_few(ggml_backend_cuda_context & ctx, const ggml_tensor
|
||||
|
||||
pairs.alloc(int64_t(npairs_max)*MMQ_FEW_PAIR_INTS);
|
||||
src1_q.alloc(int64_t(npairs_max)*nunits*MMQ_FEW_Q8_1_INT4);
|
||||
quantize_mmq_few_id_q8_1_cuda((const float *) src1->data, (const int32_t *) ids->data, src1_q.get(), pairs.get(), mmq_few_ds_layout(src0->type),
|
||||
ne00, nb11/sizeof(float), nb12/sizeof(float), ne11, ne02, n_expert_used, n_tokens, ids->nb[1]/sizeof(int32_t), npairs_max, stream);
|
||||
quantize_mmq_few_id_q8_1_cuda(src1->data, src1->type, (const int32_t *) ids->data, src1_q.get(), pairs.get(), mmq_few_ds_layout(src0->type),
|
||||
ne00, nb11/nb10, nb12/nb10, ne11, ne02, n_expert_used, n_tokens, ids->nb[1]/sizeof(int32_t), npairs_max, stream);
|
||||
} else {
|
||||
GGML_ASSERT(ne11 <= MMQ_FEW_MAX_NCG*MMQ_FEW_NCOLS);
|
||||
GGML_ASSERT(ne12 % ne02 == 0 && ne13 % ne03 == 0);
|
||||
|
||||
src1_q.alloc(int64_t(ne12)*ne13*nunits*ncg*MMQ_FEW_Q8_1_INT4);
|
||||
quantize_mmq_few_q8_1_cuda((const float *) src1->data, src1_q.get(), mmq_few_ds_layout(src0->type), ne10, nb11/sizeof(float), nb12/sizeof(float), nb13/sizeof(float),
|
||||
quantize_mmq_few_q8_1_cuda(src1->data, src1->type, src1_q.get(), mmq_few_ds_layout(src0->type), ne10, nb11/nb10, nb12/nb10, nb13/nb10,
|
||||
ne11, ne12, ne13, stream);
|
||||
}
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ static int ggml_cuda_mmq_get_J_best(
|
||||
void ggml_cuda_mul_mat_q(
|
||||
ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst,
|
||||
const ggml_cuda_mm_fusion_args_host * fusion) {
|
||||
GGML_ASSERT( src1->type == GGML_TYPE_F32);
|
||||
GGML_ASSERT( src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16);
|
||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
||||
GGML_ASSERT(!ids || ids->type == GGML_TYPE_I32); // Optional, used for batched GGML_MUL_MAT_ID.
|
||||
|
||||
@@ -210,7 +210,6 @@ void ggml_cuda_mul_mat_q(
|
||||
GGML_ASSERT(!ids || ids->nb[0] == ggml_type_size(ids->type));
|
||||
|
||||
const char * src0_d = (const char *) src0->data;
|
||||
const float * src1_d = (const float *) src1->data;
|
||||
float * dst_d = (float *) dst->data;
|
||||
|
||||
// If src0 (or the fused gate weight) is a temporary compute buffer, clear any potential padding.
|
||||
@@ -284,11 +283,11 @@ void ggml_cuda_mul_mat_q(
|
||||
static constexpr size_t align_float8 = 32;
|
||||
const bool use_aligned_float8 = ggml_cuda_is_aligned(src1, align_float8);
|
||||
static_assert(sizeof(block_fp4_mmq) == 4 * sizeof(block_q8_1));
|
||||
quantize_mmq_fp4_cuda(src1_d, nullptr, src1_q8_1.get(), src1_scale.ptr, src0->type, use_aligned_float8, ne10, s11, s12, s13, ne10_padded,
|
||||
quantize_mmq_fp4_cuda(src1->data, src1->type, nullptr, src1_q8_1.get(), src1_scale.ptr, src0->type, use_aligned_float8, ne10, s11, s12, s13, ne10_padded,
|
||||
ne11, ne12, ne13, stream);
|
||||
|
||||
} else {
|
||||
quantize_mmq_q8_1_cuda(src1_d, nullptr, src1_q8_1.get(), src0->type, ne10, s11, s12, s13, ne10_padded,
|
||||
quantize_mmq_q8_1_cuda(src1->data, src1->type, nullptr, src1_q8_1.get(), src0->type, ne10, s11, s12, s13, ne10_padded,
|
||||
ne11, ne12, ne13, stream);
|
||||
}
|
||||
CUDA_CHECK(cudaGetLastError());
|
||||
@@ -359,17 +358,17 @@ void ggml_cuda_mul_mat_q(
|
||||
static constexpr size_t align_float8 = 32;
|
||||
const bool use_aligned_float8 = ggml_cuda_is_aligned(src1, align_float8);
|
||||
if (dedup_bcast) {
|
||||
quantize_scatter_mmq_fp4_cuda(src1_d, ids_src1.get(), src1_q8_1.get(), src1_scale.ptr, src0->type, use_aligned_float8, ne10,
|
||||
quantize_scatter_mmq_fp4_cuda(src1->data, src1->type, ids_src1.get(), src1_q8_1.get(), src1_scale.ptr, src0->type, use_aligned_float8, ne10,
|
||||
/*stride_token=*/s12, ne10_padded, ne12, ne11_flat, n_expert_used, stream);
|
||||
} else {
|
||||
quantize_mmq_fp4_cuda(src1_d, ids_src1.get(), src1_q8_1.get(), src1_scale.ptr, src0->type, use_aligned_float8, ne10, s11, s12, s13,
|
||||
quantize_mmq_fp4_cuda(src1->data, src1->type, ids_src1.get(), src1_q8_1.get(), src1_scale.ptr, src0->type, use_aligned_float8, ne10, s11, s12, s13,
|
||||
ne10_padded, ne11_flat, ne12_flat, ne13_flat, stream);
|
||||
}
|
||||
} else if (dedup_bcast) {
|
||||
quantize_scatter_mmq_q8_1_cuda(src1_d, ids_src1.get(), src1_q8_1.get(), src0->type, ne10,
|
||||
quantize_scatter_mmq_q8_1_cuda(src1->data, src1->type, ids_src1.get(), src1_q8_1.get(), src0->type, ne10,
|
||||
/*stride_token=*/s12, ne10_padded, ne12, ne11_flat, n_expert_used, stream);
|
||||
} else {
|
||||
quantize_mmq_q8_1_cuda(src1_d, ids_src1.get(), src1_q8_1.get(), src0->type, ne10, s11, s12, s13,
|
||||
quantize_mmq_q8_1_cuda(src1->data, src1->type, ids_src1.get(), src1_q8_1.get(), src0->type, ne10, s11, s12, s13,
|
||||
ne10_padded, ne11_flat, ne12_flat, ne13_flat, stream);
|
||||
}
|
||||
CUDA_CHECK(cudaGetLastError());
|
||||
|
||||
+66
-27
@@ -13,15 +13,20 @@ static __device__ __forceinline__ float2 mmvf_load_fp8x2(const ggml_fp8_e4m3_t *
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T, typename type_acc, int ncols_dst, int block_size, bool has_fusion = false, bool is_multi_token_id = false>
|
||||
template <typename T, typename type_acc, int ncols_dst, int block_size, bool has_fusion = false, bool is_multi_token_id = false,
|
||||
typename T_y = float>
|
||||
static __global__ void mul_mat_vec_f(
|
||||
const T * x_ptr, const float * y_ptr, const int32_t * ids_ptr, const ggml_cuda_mm_fusion_args_device fusion, float * dst_ptr,
|
||||
const T * x_ptr, const T_y * y_ptr, const int32_t * ids_ptr, const ggml_cuda_mm_fusion_args_device fusion, float * dst_ptr,
|
||||
const int ncols2, const uint3 nchannels_y, const int stride_row, const int stride_col_y2, const int stride_col_dst,
|
||||
const uint3 channel_ratio, const int stride_channel_x, const int stride_channel_y, const int stride_channel_dst,
|
||||
const uint3 sample_ratio, const int stride_sample_x, const int stride_sample_y, const int stride_sample_dst,
|
||||
const int ids_stride) {
|
||||
static_assert(std::is_same_v<T_y, float> || (std::is_same_v<T_y, nv_bfloat16> &&
|
||||
(std::is_same_v<T, nv_bfloat16> || std::is_same_v<T, ggml_fp8_e4m3_t>)), "a BF16 src1 needs BF16 or FP8 weights");
|
||||
using T_y2 = std::conditional_t<std::is_same_v<T_y, nv_bfloat16>, nv_bfloat162, float2>; // a pair of src1 values
|
||||
|
||||
const T * GGML_CUDA_RESTRICT x = x_ptr;
|
||||
const float * GGML_CUDA_RESTRICT y = y_ptr;
|
||||
const T_y * GGML_CUDA_RESTRICT y = y_ptr;
|
||||
const int32_t * GGML_CUDA_RESTRICT ids = ids_ptr;
|
||||
float * GGML_CUDA_RESTRICT dst = dst_ptr;
|
||||
const int row = blockIdx.x;
|
||||
@@ -257,7 +262,7 @@ static __global__ void mul_mat_vec_f(
|
||||
#endif // FP16_AVAILABLE
|
||||
}
|
||||
} else if constexpr (std::is_same_v<T, nv_bfloat16>) {
|
||||
const float2 * y2 = (const float2 *) y;
|
||||
const T_y2 * y2 = (const T_y2 *) y;
|
||||
//TODO: add support for ggml_cuda_mad for hip_bfloat162
|
||||
#if defined(GGML_USE_HIP)
|
||||
const int * x2 = (const int *) x;
|
||||
@@ -277,7 +282,7 @@ static __global__ void mul_mat_vec_f(
|
||||
}
|
||||
#pragma unroll
|
||||
for (int j = 0; j < ncols_dst; ++j) {
|
||||
const float2 tmpy = y2[j*stride_col_y2 + col2];
|
||||
const float2 tmpy = ggml_cuda_cast<float2>(y2[j*stride_col_y2 + col2]);
|
||||
const float tmpx0 = ggml_cuda_cast<float>(reinterpret_cast<const nv_bfloat16 *>(&tmpx)[0]);
|
||||
const float tmpx1 = ggml_cuda_cast<float>(reinterpret_cast<const nv_bfloat16 *>(&tmpx)[1]);
|
||||
ggml_cuda_mad(sumf[j], tmpx0, tmpy.x);
|
||||
@@ -311,7 +316,7 @@ static __global__ void mul_mat_vec_f(
|
||||
}
|
||||
#pragma unroll
|
||||
for (int j = 0; j < ncols_dst; ++j) {
|
||||
const float2 tmpy = y2[j*stride_col_y2 + col2];
|
||||
const float2 tmpy = ggml_cuda_cast<float2>(y2[j*stride_col_y2 + col2]);
|
||||
ggml_cuda_mad(sumf[j], tmpx.x, tmpy.x);
|
||||
ggml_cuda_mad(sumf[j], tmpx.y, tmpy.y);
|
||||
|
||||
@@ -325,7 +330,7 @@ static __global__ void mul_mat_vec_f(
|
||||
}
|
||||
#endif
|
||||
} else if constexpr (std::is_same_v<T, ggml_fp8_e4m3_t>) {
|
||||
const float2 * y2 = (const float2 *) y;
|
||||
const T_y2 * y2 = (const T_y2 *) y;
|
||||
for (int col2 = tid; col2 < ncols2; col2 += block_size) {
|
||||
const float2 tmpx = mmvf_load_fp8x2(x, col2);
|
||||
float2 tmpx_gate = {};
|
||||
@@ -336,7 +341,7 @@ static __global__ void mul_mat_vec_f(
|
||||
}
|
||||
#pragma unroll
|
||||
for (int j = 0; j < ncols_dst; ++j) {
|
||||
const float2 tmpy = y2[j*stride_col_y2 + col2];
|
||||
const float2 tmpy = ggml_cuda_cast<float2>(y2[j*stride_col_y2 + col2]);
|
||||
ggml_cuda_mad(sumf[j], tmpx, tmpy);
|
||||
|
||||
if constexpr (has_fusion) {
|
||||
@@ -439,9 +444,9 @@ static __global__ void mul_mat_vec_f(
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, typename type_acc, int ncols_dst, int block_size, bool is_multi_token_id = false>
|
||||
template<typename T, typename type_acc, int ncols_dst, int block_size, bool is_multi_token_id = false, typename T_y = float>
|
||||
static void mul_mat_vec_f_switch_fusion(
|
||||
const T * x, const float * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
|
||||
const T * x, const T_y * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
|
||||
const int64_t ncols, const uint3 nchannels_y,
|
||||
const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst,
|
||||
const uint3 channel_ratio, const int stride_channel_x, const int stride_channel_y, const int stride_channel_dst,
|
||||
@@ -454,7 +459,7 @@ static void mul_mat_vec_f_switch_fusion(
|
||||
fusion.x_scale != nullptr || fusion.gate_scale != nullptr;
|
||||
if constexpr (ncols_dst == 1) {
|
||||
if (has_fusion) {
|
||||
ggml_cuda_kernel_launch(mul_mat_vec_f<T, type_acc, ncols_dst, block_size, true, is_multi_token_id>, launch_params,
|
||||
ggml_cuda_kernel_launch(mul_mat_vec_f<T, type_acc, ncols_dst, block_size, true, is_multi_token_id, T_y>, launch_params,
|
||||
x, y, ids, fusion, dst, ncols, nchannels_y, stride_row, stride_col_y, stride_col_dst,
|
||||
channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
|
||||
sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride);
|
||||
@@ -464,16 +469,16 @@ static void mul_mat_vec_f_switch_fusion(
|
||||
|
||||
GGML_ASSERT(!has_fusion && "fusion only supported for ncols_dst=1");
|
||||
|
||||
ggml_cuda_kernel_launch(mul_mat_vec_f<T, type_acc, ncols_dst, block_size, false, is_multi_token_id>, launch_params,
|
||||
ggml_cuda_kernel_launch(mul_mat_vec_f<T, type_acc, ncols_dst, block_size, false, is_multi_token_id, T_y>, launch_params,
|
||||
x, y, ids, fusion, dst, ncols, nchannels_y, stride_row, stride_col_y, stride_col_dst,
|
||||
channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
|
||||
sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride);
|
||||
|
||||
}
|
||||
|
||||
template <typename T, typename type_acc, int ncols_dst, bool is_multi_token_id = false>
|
||||
template <typename T, typename type_acc, int ncols_dst, bool is_multi_token_id = false, typename T_y = float>
|
||||
void launch_mul_mat_vec_f_cuda(
|
||||
const T * x, const float * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
|
||||
const T * x, const T_y * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
|
||||
const int64_t ncols, const int64_t nrows,
|
||||
const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst,
|
||||
const int64_t nchannels_x, const int64_t nchannels_y, const int64_t nchannels_dst,
|
||||
@@ -565,9 +570,9 @@ void launch_mul_mat_vec_f_cuda(
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename type_acc>
|
||||
template <typename T, typename type_acc, typename T_y>
|
||||
static void mul_mat_vec_f_cuda_switch_ncols_dst(
|
||||
const T * x, const float * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
|
||||
const T * x, const T_y * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
|
||||
const int64_t ncols, const int64_t nrows, const int64_t ncols_dst,
|
||||
const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst,
|
||||
const int64_t nchannels_x, const int64_t nchannels_y, const int64_t nchannels_dst,
|
||||
@@ -661,9 +666,9 @@ static void mul_mat_vec_f_cuda_switch_ncols_dst(
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename T, typename T_y>
|
||||
static void mul_mat_vec_f_cuda(
|
||||
const T * x, const float * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
|
||||
const T * x, const T_y * y, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst,
|
||||
const int64_t ncols, const int64_t nrows, const int64_t ncols_dst,
|
||||
const int64_t stride_row, const int64_t stride_col_y, const int stride_col_dst,
|
||||
const int64_t nchannels_x, const int64_t nchannels_y, const int64_t nchannels_dst,
|
||||
@@ -688,7 +693,9 @@ static void mul_mat_vec_f_cuda(
|
||||
|
||||
void ggml_cuda_mul_mat_vec_f(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst,
|
||||
const ggml_cuda_mm_fusion_args_host * fusion) {
|
||||
GGML_ASSERT( src1->type == GGML_TYPE_F32);
|
||||
// a BF16 src1 only for BF16 and FP8 weights
|
||||
GGML_ASSERT( src1->type == GGML_TYPE_F32 ||
|
||||
(src1->type == GGML_TYPE_BF16 && (src0->type == GGML_TYPE_BF16 || src0->type == GGML_TYPE_F8_E4M3)));
|
||||
GGML_ASSERT(!ids || ids->type == GGML_TYPE_I32);
|
||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
||||
|
||||
@@ -790,12 +797,24 @@ void ggml_cuda_mul_mat_vec_f(ggml_backend_cuda_context & ctx, const ggml_tensor
|
||||
} break;
|
||||
case GGML_TYPE_BF16: {
|
||||
const nv_bfloat16 * src0_d = (const nv_bfloat16 *) src0->data;
|
||||
if (src1->type == GGML_TYPE_BF16) {
|
||||
mul_mat_vec_f_cuda(src0_d, (const nv_bfloat16 *) src1->data, ids_d, fusion_local, dst_d, ne00, ne01, ncols_dst, s01,
|
||||
stride_col_y, stride_col_dst, ne02, nchannels_y, nchannels_dst, s02, stride_channel_y, stride_channel_dst,
|
||||
ne03, ne3, s03, s13, s3, ids_stride, prec, ctx.stream());
|
||||
break;
|
||||
}
|
||||
mul_mat_vec_f_cuda(src0_d, src1_d, ids_d, fusion_local, dst_d, ne00, ne01, ncols_dst, s01, stride_col_y, stride_col_dst,
|
||||
ne02, nchannels_y, nchannels_dst, s02, stride_channel_y, stride_channel_dst,
|
||||
ne03, ne3, s03, s13, s3, ids_stride, prec, ctx.stream());
|
||||
} break;
|
||||
case GGML_TYPE_F8_E4M3: {
|
||||
const ggml_fp8_e4m3_t * src0_d = (const ggml_fp8_e4m3_t *) src0->data;
|
||||
if (src1->type == GGML_TYPE_BF16) {
|
||||
mul_mat_vec_f_cuda(src0_d, (const nv_bfloat16 *) src1->data, ids_d, fusion_local, dst_d, ne00, ne01, ncols_dst, s01,
|
||||
stride_col_y, stride_col_dst, ne02, nchannels_y, nchannels_dst, s02, stride_channel_y, stride_channel_dst,
|
||||
ne03, ne3, s03, s13, s3, ids_stride, prec, ctx.stream());
|
||||
break;
|
||||
}
|
||||
mul_mat_vec_f_cuda(src0_d, src1_d, ids_d, fusion_local, dst_d, ne00, ne01, ncols_dst, s01, stride_col_y, stride_col_dst,
|
||||
ne02, nchannels_y, nchannels_dst, s02, stride_channel_y, stride_channel_dst,
|
||||
ne03, ne3, s03, s13, s3, ids_stride, prec, ctx.stream());
|
||||
@@ -866,8 +885,8 @@ void ggml_cuda_op_mul_mat_vec_f(
|
||||
GGML_UNUSED_VARS(ctx, src1, dst, src1_ddq_i, src1_ncols, src1_padded_row_size);
|
||||
}
|
||||
|
||||
bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, int cc, int warp_size, int64_t ne11) {
|
||||
const ggml_type type = src0->type;
|
||||
bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, ggml_type type_src1, int cc, int warp_size, int64_t ne11, bool mul_mat_id) {
|
||||
const ggml_type type_src0 = src0->type;
|
||||
const int64_t * src0_ne = src0->ne;
|
||||
const size_t * src0_nb = src0->nb;
|
||||
|
||||
@@ -875,7 +894,7 @@ bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, int cc, int warp_size,
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t ts = ggml_type_size(type);
|
||||
const size_t ts = ggml_type_size(type_src0);
|
||||
if (src0_nb[0] != ts) {
|
||||
return false;
|
||||
}
|
||||
@@ -884,7 +903,27 @@ bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, int cc, int warp_size,
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
// MMVF reads a BF16 src1 only for BF16 and FP8 weights
|
||||
if (type_src1 == GGML_TYPE_BF16 && type_src0 != GGML_TYPE_BF16 && type_src0 != GGML_TYPE_F8_E4M3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mul_mat_id) {
|
||||
// FP8 experts on all GPUs, F32/F16/BF16 experts on AMD, and BF16 experts with a BF16 src1, which MMF can not read
|
||||
// (with an F32 src1 NVIDIA takes MMF where it supports the experts, otherwise the sorted fallback)
|
||||
switch (type_src0) {
|
||||
case GGML_TYPE_F32:
|
||||
case GGML_TYPE_F16:
|
||||
case GGML_TYPE_BF16:
|
||||
return ne11 <= MMVF_MAX_BATCH_SIZE && (GGML_CUDA_CC_IS_AMD(cc) || type_src1 == GGML_TYPE_BF16);
|
||||
case GGML_TYPE_F8_E4M3:
|
||||
return ne11 <= MMVF_MAX_BATCH_SIZE;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
switch (type_src0) {
|
||||
case GGML_TYPE_F32:
|
||||
if (GGML_CUDA_CC_IS_NVIDIA(cc)) {
|
||||
if (ampere_mma_available(cc)) {
|
||||
@@ -905,7 +944,7 @@ bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, int cc, int warp_size,
|
||||
if (GGML_CUDA_CC_IS_NVIDIA(cc)) {
|
||||
const bool src0_small = (src0_ne[1] <= 512 || src0_ne[2]*src0_ne[3] == 1);
|
||||
// MMF needs full row tiles, for other row counts MMVF still beats cuBLAS at small batch size
|
||||
if (src0_small && !ggml_cuda_should_use_mmf(type, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
|
||||
if (src0_small && !ggml_cuda_should_use_mmf(type_src0, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
|
||||
return ne11 <= MMVF_MAX_BATCH_SIZE;
|
||||
}
|
||||
if (ampere_mma_available(cc)) {
|
||||
@@ -919,7 +958,7 @@ bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, int cc, int warp_size,
|
||||
}
|
||||
return ne11 <= MMVF_MAX_BATCH_SIZE;
|
||||
} else if (GGML_CUDA_CC_IS_AMD(cc)) {
|
||||
if (GGML_CUDA_CC_IS_RDNA(cc) && !ggml_cuda_should_use_mmf(type, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
|
||||
if (GGML_CUDA_CC_IS_RDNA(cc) && !ggml_cuda_should_use_mmf(type_src0, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
|
||||
return ne11 <= MMVF_MAX_BATCH_SIZE;
|
||||
}
|
||||
if (fp16_mma_hardware_available(cc)) {
|
||||
@@ -938,7 +977,7 @@ bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, int cc, int warp_size,
|
||||
if (GGML_CUDA_CC_IS_NVIDIA(cc)) {
|
||||
const bool src0_small = (src0_ne[1] <= 512 || src0_ne[2]*src0_ne[3] == 1);
|
||||
// MMF needs full row tiles, for other row counts MMVF still beats cuBLAS at small batch size
|
||||
if (src0_small && !ggml_cuda_should_use_mmf(type, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
|
||||
if (src0_small && !ggml_cuda_should_use_mmf(type_src0, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
|
||||
return ne11 <= MMVF_MAX_BATCH_SIZE;
|
||||
}
|
||||
// MMF tiles 32 rows per block, so a matrix of few rows leaves most SMs idle while MMVF runs a block
|
||||
@@ -964,7 +1003,7 @@ bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, int cc, int warp_size,
|
||||
}
|
||||
return ne11 <= MMVF_MAX_BATCH_SIZE;
|
||||
} else if (GGML_CUDA_CC_IS_AMD(cc)) {
|
||||
if (GGML_CUDA_CC_IS_RDNA(cc) && !ggml_cuda_should_use_mmf(type, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
|
||||
if (GGML_CUDA_CC_IS_RDNA(cc) && !ggml_cuda_should_use_mmf(type_src0, cc, warp_size, src0_ne, src0_nb, ne11, /*mul_mat_id =*/ false)) {
|
||||
return ne11 <= MMVF_MAX_BATCH_SIZE;
|
||||
}
|
||||
if (bf16_mma_hardware_available(cc)) {
|
||||
|
||||
@@ -11,4 +11,4 @@ void ggml_cuda_op_mul_mat_vec_f(
|
||||
const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
|
||||
const int64_t src1_padded_row_size, cudaStream_t stream);
|
||||
|
||||
bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, int cc, int warp_size, int64_t ne11);
|
||||
bool ggml_cuda_should_use_mmvf(const ggml_tensor * src0, ggml_type type_src1, int cc, int warp_size, int64_t ne11, bool mul_mat_id);
|
||||
|
||||
@@ -1538,7 +1538,7 @@ static void mul_mat_vec_q_switch_type(
|
||||
void ggml_cuda_mul_mat_vec_q(
|
||||
ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst,
|
||||
const ggml_cuda_mm_fusion_args_host * fusion) {
|
||||
GGML_ASSERT( src1->type == GGML_TYPE_F32);
|
||||
GGML_ASSERT( src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_BF16);
|
||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
||||
GGML_ASSERT(!ids || ids->type == GGML_TYPE_I32); // Optional, used for batched GGML_MUL_MAT_ID.
|
||||
|
||||
@@ -1557,7 +1557,6 @@ void ggml_cuda_mul_mat_vec_q(
|
||||
|
||||
GGML_ASSERT(!ids || ne12 <= MMVQ_MAX_BATCH_SIZE);
|
||||
|
||||
const float * src1_d = (const float *) src1->data;
|
||||
const int32_t * ids_d = ids ? (const int32_t *) ids->data : nullptr;
|
||||
float * dst_d = (float *) dst->data;
|
||||
|
||||
@@ -1637,7 +1636,7 @@ void ggml_cuda_mul_mat_vec_q(
|
||||
const int64_t s11 = src1->nb[1] / ts_src1;
|
||||
const int64_t s12 = src1->nb[2] / ts_src1;
|
||||
const int64_t s13 = src1->nb[3] / ts_src1;
|
||||
quantize_row_q8_1_cuda(src1_d, src1_q8_1.get(), ne10, s11, s12, s13, ne10_padded, ne11, ne12, ne13, stream);
|
||||
quantize_row_q8_1_cuda(src1->data, src1->type, src1_q8_1.get(), ne10, s11, s12, s13, ne10_padded, ne11, ne12, ne13, stream);
|
||||
}
|
||||
|
||||
const int64_t s01 = src0->nb[1] / ts_src0;
|
||||
|
||||
+170
-59
@@ -1,6 +1,21 @@
|
||||
#include "quantize.cuh"
|
||||
#include "convert.cuh"
|
||||
#include <cstdint>
|
||||
|
||||
// 4 consecutive values of x as float4, x + 4*i is aligned to 4 values
|
||||
template <typename src_t>
|
||||
static __device__ __forceinline__ float4 quantize_load4(const src_t * x, const int64_t i) {
|
||||
if constexpr (std::is_same_v<src_t, nv_bfloat16>) {
|
||||
__align__(8) nv_bfloat162 v[2];
|
||||
ggml_cuda_memcpy_1<sizeof(v)>(v, x + 4*i);
|
||||
const float2 lo = ggml_cuda_cast<float2>(v[0]);
|
||||
const float2 hi = ggml_cuda_cast<float2>(v[1]);
|
||||
return make_float4(lo.x, lo.y, hi.x, hi.y);
|
||||
} else {
|
||||
return ((const float4 *) x)[i];
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(BLACKWELL_MMA_AVAILABLE)
|
||||
// this maps to 256-bit loads in PTX on supported devices,
|
||||
// and otherwise falls back to 2 128-bit loads
|
||||
@@ -9,6 +24,23 @@ struct __builtin_align__(32) float8 {
|
||||
float p; float q; float r; float s;
|
||||
};
|
||||
|
||||
// 8 consecutive values of x as float8, x is aligned to 8 values
|
||||
// for float it returns a reference like a direct load, a copy changes the register allocation of the kernels
|
||||
template <typename src_t>
|
||||
static __device__ __forceinline__ decltype(auto) quantize_load8(const src_t * x) {
|
||||
if constexpr (std::is_same_v<src_t, nv_bfloat16>) {
|
||||
__align__(16) nv_bfloat162 v[4];
|
||||
ggml_cuda_memcpy_1<sizeof(v)>(v, x);
|
||||
const float2 a = ggml_cuda_cast<float2>(v[0]);
|
||||
const float2 b = ggml_cuda_cast<float2>(v[1]);
|
||||
const float2 c = ggml_cuda_cast<float2>(v[2]);
|
||||
const float2 d = ggml_cuda_cast<float2>(v[3]);
|
||||
return float8{a.x, a.y, b.x, b.y, c.x, c.y, d.x, d.y};
|
||||
} else {
|
||||
return reinterpret_cast<const float8 *>(x)[0];
|
||||
}
|
||||
}
|
||||
|
||||
#if CUDART_VERSION >= 12080
|
||||
static __device__ __forceinline__ float nvfp4_native_scale_error(
|
||||
const float vals[QK_NVFP4_SUB], const float inv_col_scale, const float inv_scale, const float scale) {
|
||||
@@ -50,13 +82,14 @@ static __device__ __forceinline__ float nvfp4_native_scale_error(
|
||||
#endif // CUDART_VERSION >= 12080
|
||||
#endif // defined(BLACKWELL_MMA_AVAILABLE)
|
||||
|
||||
template <typename src_t>
|
||||
__launch_bounds__(CUDA_QUANTIZE_BLOCK_SIZE, 1)
|
||||
static __global__ void quantize_q8_1(
|
||||
const float * x_ptr, void * vy_ptr,
|
||||
const src_t * x_ptr, void * vy_ptr,
|
||||
const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne0, const uint32_t ne1, const uint3 ne2) {
|
||||
ggml_cuda_pdl_lc();
|
||||
const float * GGML_CUDA_RESTRICT x = x_ptr;
|
||||
const src_t * GGML_CUDA_RESTRICT x = x_ptr;
|
||||
void * GGML_CUDA_RESTRICT vy = vy_ptr;
|
||||
const int64_t i0 = (int64_t)blockDim.x*blockIdx.x + threadIdx.x;
|
||||
|
||||
@@ -81,7 +114,7 @@ static __global__ void quantize_q8_1(
|
||||
const int64_t iqs = i_cont % QK8_1; // quant index
|
||||
|
||||
ggml_cuda_pdl_sync();
|
||||
const float xi = i0 < ne00 ? x[i03*s03 + i02*s02 + i01*s01 + i00] : 0.0f;
|
||||
const float xi = i0 < ne00 ? ggml_cuda_cast<float>(x[i03*s03 + i02*s02 + i01*s01 + i00]) : 0.0f;
|
||||
float amax = fabsf(xi);
|
||||
|
||||
amax = warp_reduce_max<QK8_1>(amax);
|
||||
@@ -124,9 +157,9 @@ __device__ __forceinline__ uint8_t compute_e8m0_scale(float amax) {
|
||||
}
|
||||
|
||||
// scatter: grid over tokens, quantize once, write to all the token's compact rows
|
||||
template <bool scatter, bool use_aligned_float8>
|
||||
template <typename src_t, bool scatter, bool use_aligned_float8>
|
||||
static __global__ void quantize_mmq_nvfp4(
|
||||
const float * __restrict__ x, const int32_t * __restrict__ ids, void * __restrict__ vy, float * __restrict__ scale,
|
||||
const src_t * __restrict__ x, const int32_t * __restrict__ ids, void * __restrict__ vy, float * __restrict__ scale,
|
||||
const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne0, const int64_t ne1, const int64_t ne2, const int n_expert_used) {
|
||||
#if defined(BLACKWELL_MMA_AVAILABLE)
|
||||
@@ -140,13 +173,12 @@ static __global__ void quantize_mmq_nvfp4(
|
||||
const int64_t i01 = ids ? ids[blockIdx.x] : blockIdx.x;
|
||||
base_idx = i3 * s03 + i2 * s02 + i01 * s01;
|
||||
}
|
||||
const float * __restrict__ x_row = x + base_idx;
|
||||
const src_t * __restrict__ x_row = x + base_idx;
|
||||
|
||||
float amax = 0.0f;
|
||||
if constexpr (use_aligned_float8) {
|
||||
for (int64_t i0 = 8 * threadIdx.x; i0 < ne00; i0 += 8 * blockDim.x) {
|
||||
const float * x_base = x_row + i0;
|
||||
const float8 v = reinterpret_cast<const float8 *>(x_base)[0];
|
||||
const float8 v = quantize_load8(x_row + i0);
|
||||
amax = fmaxf(amax, fabsf(v.x));
|
||||
amax = fmaxf(amax, fabsf(v.y));
|
||||
amax = fmaxf(amax, fabsf(v.z));
|
||||
@@ -158,7 +190,7 @@ static __global__ void quantize_mmq_nvfp4(
|
||||
}
|
||||
} else {
|
||||
for (int64_t i0 = threadIdx.x; i0 < ne00; i0 += blockDim.x) {
|
||||
amax = fmaxf(amax, fabsf(x_row[i0]));
|
||||
amax = fmaxf(amax, fabsf(ggml_cuda_cast<float>(x_row[i0])));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,9 +236,9 @@ static __global__ void quantize_mmq_nvfp4(
|
||||
|
||||
float vals[QK_NVFP4_SUB];
|
||||
if constexpr (use_aligned_float8) {
|
||||
const float * x_base = x_row + i0_base;
|
||||
const float8 v0 = i0_base + 7 < ne00 ? reinterpret_cast<const float8 *>(x_base)[0] : float8{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
|
||||
const float8 v1 = i0_base + 15 < ne00 ? reinterpret_cast<const float8 *>(x_base + 8)[0] : float8{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
|
||||
const src_t * x_base = x_row + i0_base;
|
||||
const float8 v0 = i0_base + 7 < ne00 ? quantize_load8(x_base) : float8{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
|
||||
const float8 v1 = i0_base + 15 < ne00 ? quantize_load8(x_base + 8) : float8{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
|
||||
vals[0] = v0.x; vals[1] = v0.y; vals[2] = v0.z; vals[3] = v0.w;
|
||||
vals[4] = v0.p; vals[5] = v0.q; vals[6] = v0.r; vals[7] = v0.s;
|
||||
vals[8] = v1.x; vals[9] = v1.y; vals[10] = v1.z; vals[11] = v1.w;
|
||||
@@ -215,7 +247,7 @@ static __global__ void quantize_mmq_nvfp4(
|
||||
#pragma unroll
|
||||
for (int k = 0; k < QK_NVFP4_SUB; ++k) {
|
||||
const int64_t i00 = i0_base + k;
|
||||
vals[k] = i00 < ne00 ? x_row[i00] : 0.0f;
|
||||
vals[k] = i00 < ne00 ? ggml_cuda_cast<float>(x_row[i00]) : 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -333,8 +365,8 @@ static __global__ void quantize_mmq_nvfp4(
|
||||
// quantize values in the format mxfp4 is stored which is interleaved nibbles
|
||||
// i.e. a block a0-a31 is represented as a0a16,a1a17 ...a15a31
|
||||
// scatter: grid over tokens, quantize once, write to all the token's compact rows
|
||||
template <bool scatter>
|
||||
static __global__ void quantize_mmq_mxfp4(const float * __restrict__ x,
|
||||
template <typename src_t, bool scatter>
|
||||
static __global__ void quantize_mmq_mxfp4(const src_t * __restrict__ x,
|
||||
const int32_t * __restrict__ ids,
|
||||
void * __restrict__ vy,
|
||||
const int64_t ne00,
|
||||
@@ -384,7 +416,7 @@ static __global__ void quantize_mmq_mxfp4(const float * __restrict__ x,
|
||||
#pragma unroll
|
||||
for (int b = 0; b < 2; ++b) {
|
||||
const int64_t i0 = warp_start_offset + b * vals_per_scale + lane_id_32;
|
||||
const float xi = (i0 < ne00) ? x[base_pos + i0] : 0.0f;
|
||||
const float xi = (i0 < ne00) ? ggml_cuda_cast<float>(x[base_pos + i0]) : 0.0f;
|
||||
|
||||
float amax = fabsf(xi);
|
||||
#pragma unroll
|
||||
@@ -453,9 +485,9 @@ static __global__ void quantize_mmq_mxfp4(const float * __restrict__ x,
|
||||
}
|
||||
|
||||
// scatter: grid over tokens, quantize once, write to all the token's compact rows
|
||||
template <mmq_q8_1_ds_layout ds_layout, bool scatter>
|
||||
template <typename src_t, mmq_q8_1_ds_layout ds_layout, bool scatter>
|
||||
static __global__ void quantize_mmq_q8_1(
|
||||
const float * __restrict__ x, const int32_t * __restrict__ ids, void * __restrict__ vy,
|
||||
const src_t * __restrict__ x, const int32_t * __restrict__ ids, void * __restrict__ vy,
|
||||
const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne0, const int ne1, const int ne2, const int n_expert_used) {
|
||||
|
||||
@@ -481,14 +513,13 @@ static __global__ void quantize_mmq_q8_1(
|
||||
base_idx = i3*s03 + i2*s02 + i01*s01;
|
||||
}
|
||||
|
||||
const float4 * x4 = (const float4 *) x;
|
||||
block_q8_1_mmq * y = (block_q8_1_mmq *) vy;
|
||||
|
||||
const int64_t k_block = i0 / QK8_1_MMQ; // column block in the channel
|
||||
const int64_t iqs = i0 % QK8_1_MMQ; // quant index in block
|
||||
|
||||
// Load 4 floats per thread and calculate max. abs. value between them:
|
||||
const float4 xi = i0 < ne00 ? x4[(base_idx + i00)/4] : make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
const float4 xi = i0 < ne00 ? quantize_load4(x, (base_idx + i00)/4) : make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
float amax = fabsf(xi.x);
|
||||
amax = fmaxf(amax, fabsf(xi.y));
|
||||
amax = fmaxf(amax, fabsf(xi.z));
|
||||
@@ -556,8 +587,20 @@ static __global__ void quantize_mmq_q8_1(
|
||||
GGML_UNUSED(n_expert_used);
|
||||
}
|
||||
|
||||
void quantize_row_q8_1_cuda(
|
||||
const float * x, void * vy,
|
||||
// calls launch with x as const float * or const nv_bfloat16 *, the src1 types that the quantize kernels read
|
||||
template <typename F>
|
||||
static void quantize_launch_typed(const void * x, const ggml_type type_src1, const F & launch) {
|
||||
if (type_src1 == GGML_TYPE_BF16) {
|
||||
launch((const nv_bfloat16 *) x);
|
||||
} else {
|
||||
GGML_ASSERT(type_src1 == GGML_TYPE_F32);
|
||||
launch((const float *) x);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename src_t>
|
||||
static void quantize_row_q8_1_cuda(
|
||||
const src_t * x, void * vy,
|
||||
const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
|
||||
GGML_ASSERT(ne0 % QK8_1 == 0);
|
||||
@@ -568,11 +611,21 @@ void quantize_row_q8_1_cuda(
|
||||
const dim3 num_blocks(block_num_x, ne1, ne2*ne3);
|
||||
const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE, 1, 1);
|
||||
const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(num_blocks, block_size, 0, stream);
|
||||
ggml_cuda_kernel_launch(quantize_q8_1, launch_params, x, vy, ne00, s01, s02, s03, ne0, ne1, ne2_fastdiv);
|
||||
ggml_cuda_kernel_launch(quantize_q8_1<src_t>, launch_params, x, vy, ne00, s01, s02, s03, ne0, ne1, ne2_fastdiv);
|
||||
}
|
||||
|
||||
void quantize_mmq_q8_1_cuda(
|
||||
const float * x, const int32_t * ids, void * vy, const ggml_type type_src0,
|
||||
void quantize_row_q8_1_cuda(
|
||||
const void * x, const ggml_type type_src1, void * vy,
|
||||
const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
|
||||
quantize_launch_typed(x, type_src1, [&](auto * x_t) {
|
||||
quantize_row_q8_1_cuda(x_t, vy, ne00, s01, s02, s03, ne0, ne1, ne2, ne3, stream);
|
||||
});
|
||||
}
|
||||
|
||||
template <typename src_t>
|
||||
static void quantize_mmq_q8_1_cuda(
|
||||
const src_t * x, const int32_t * ids, void * vy, const ggml_type type_src0,
|
||||
const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
|
||||
GGML_ASSERT(ne00 % 4 == 0);
|
||||
@@ -584,15 +637,15 @@ void quantize_mmq_q8_1_cuda(
|
||||
const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1);
|
||||
switch (mmq_get_q8_1_ds_layout(type_src0)) {
|
||||
case MMQ_Q8_1_DS_LAYOUT_D4:
|
||||
quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D4, false>
|
||||
quantize_mmq_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D4, false>
|
||||
<<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0);
|
||||
break;
|
||||
case MMQ_Q8_1_DS_LAYOUT_DS4:
|
||||
quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_DS4, false>
|
||||
quantize_mmq_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_DS4, false>
|
||||
<<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0);
|
||||
break;
|
||||
case MMQ_Q8_1_DS_LAYOUT_D2S6:
|
||||
quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D2S6, false>
|
||||
quantize_mmq_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D2S6, false>
|
||||
<<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0);
|
||||
break;
|
||||
default:
|
||||
@@ -600,6 +653,15 @@ void quantize_mmq_q8_1_cuda(
|
||||
}
|
||||
}
|
||||
|
||||
void quantize_mmq_q8_1_cuda(
|
||||
const void * x, const ggml_type type_src1, const int32_t * ids, void * vy, const ggml_type type_src0,
|
||||
const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
|
||||
quantize_launch_typed(x, type_src1, [&](auto * x_t) {
|
||||
quantize_mmq_q8_1_cuda(x_t, ids, vy, type_src0, ne00, s01, s02, s03, ne0, ne1, ne2, ne3, stream);
|
||||
});
|
||||
}
|
||||
|
||||
// Stores the 4 values xi of a thread, values [iu, iu + 4) of column g in a unit of 256 values, in the column group yg of the unit, see quantize.cuh for the layout.
|
||||
template <mmq_q8_1_ds_layout ds_layout>
|
||||
static __device__ __forceinline__ void quantize_mmq_few_store(const float4 xi, int4 * yg, const int iu, const int g) {
|
||||
@@ -635,12 +697,12 @@ static __device__ __forceinline__ void quantize_mmq_few_store(const float4 xi, i
|
||||
}
|
||||
|
||||
// Each thread quantizes 4 values of one column, see quantize.cuh for the layout.
|
||||
template <mmq_q8_1_ds_layout ds_layout>
|
||||
template <typename src_t, mmq_q8_1_ds_layout ds_layout>
|
||||
static __global__ void quantize_mmq_few_q8_1(
|
||||
const float * x_ptr, void * vy_ptr, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const src_t * x_ptr, void * vy_ptr, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int ne1, const int ne2) {
|
||||
ggml_cuda_pdl_lc();
|
||||
const float * GGML_CUDA_RESTRICT x = x_ptr;
|
||||
const src_t * GGML_CUDA_RESTRICT x = x_ptr;
|
||||
int4 * GGML_CUDA_RESTRICT vy = (int4 *) vy_ptr;
|
||||
const int64_t i0 = ((int64_t)blockDim.x*blockIdx.y + threadIdx.x)*4;
|
||||
|
||||
@@ -651,7 +713,7 @@ static __global__ void quantize_mmq_few_q8_1(
|
||||
|
||||
const int64_t i2 = blockIdx.z % ne2;
|
||||
const int64_t i3 = blockIdx.z / ne2;
|
||||
const float4 xi = (int) blockIdx.x < ne1 ? *(const float4 *) (x + i3*s03 + i2*s02 + blockIdx.x*s01 + i0) : make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
const float4 xi = (int) blockIdx.x < ne1 ? quantize_load4(x + i3*s03 + i2*s02 + blockIdx.x*s01 + i0, 0) : make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
const int g = blockIdx.x % MMQ_FEW_NCOLS; // column in group
|
||||
int4 * yg = vy + ((blockIdx.z*(ne00/QK_K) + i0/QK_K)*(gridDim.x/MMQ_FEW_NCOLS) + blockIdx.x/MMQ_FEW_NCOLS)*MMQ_FEW_Q8_1_INT4;
|
||||
@@ -660,9 +722,9 @@ static __global__ void quantize_mmq_few_q8_1(
|
||||
|
||||
// MUL_MAT_ID: block x is the (token, slot) entry, ids are as in mm_ids_helper. Each block finds the pair of its entry in shared memory, block (0, 0) also writes all pairs.
|
||||
// Entry rank r = number of earlier tokens with the same expert, pair = first pair of the expert + r/MMQ_FEW_NCOLS, column = r % MMQ_FEW_NCOLS.
|
||||
template <mmq_q8_1_ds_layout ds_layout>
|
||||
template <typename src_t, mmq_q8_1_ds_layout ds_layout>
|
||||
static __global__ void quantize_mmq_few_id_q8_1(
|
||||
const float * x_ptr, const int32_t * ids, void * vy_ptr, int * pairs, const int64_t ne00, const int64_t s11, const int64_t s12,
|
||||
const src_t * x_ptr, const int32_t * ids, void * vy_ptr, int * pairs, const int64_t ne00, const int64_t s11, const int64_t s12,
|
||||
const int ne11, const int n_experts, const int n_expert_used, const int64_t si1, const int npairs_max) {
|
||||
// per expert: bit t set if token t uses it and the index of its first pair; then the total number of pairs
|
||||
extern __shared__ unsigned int data_mmq_few_id[];
|
||||
@@ -671,7 +733,7 @@ static __global__ void quantize_mmq_few_id_q8_1(
|
||||
int & npairs = pair0[n_experts];
|
||||
|
||||
ggml_cuda_pdl_lc();
|
||||
const float * GGML_CUDA_RESTRICT x = x_ptr;
|
||||
const src_t * GGML_CUDA_RESTRICT x = x_ptr;
|
||||
int4 * GGML_CUDA_RESTRICT vy = (int4 *) vy_ptr;
|
||||
ggml_cuda_pdl_sync();
|
||||
|
||||
@@ -741,15 +803,16 @@ static __global__ void quantize_mmq_few_id_q8_1(
|
||||
}
|
||||
const int it = blockIdx.x / n_expert_used;
|
||||
const int is = blockIdx.x % n_expert_used;
|
||||
const float4 xi = *(const float4 *) (x + it*s12 + (is % ne11)*s11 + i0);
|
||||
const float4 xi = quantize_load4(x + it*s12 + (is % ne11)*s11 + i0, 0);
|
||||
|
||||
const int r = rank(blockIdx.x, e);
|
||||
int4 * yg = vy + (int64_t(pair0[e] + r/MMQ_FEW_NCOLS)*(ne00/QK_K) + i0/QK_K)*MMQ_FEW_Q8_1_INT4;
|
||||
quantize_mmq_few_store<ds_layout>(xi, yg, i0 % QK_K, r % MMQ_FEW_NCOLS);
|
||||
}
|
||||
|
||||
void quantize_mmq_few_q8_1_cuda(
|
||||
const float * x, void * vy, const mmq_q8_1_ds_layout ds_layout, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
template <typename src_t>
|
||||
static void quantize_mmq_few_q8_1_cuda(
|
||||
const src_t * x, void * vy, const mmq_q8_1_ds_layout ds_layout, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
|
||||
GGML_ASSERT(ne00 % QK_K == 0);
|
||||
|
||||
@@ -759,11 +822,11 @@ void quantize_mmq_few_q8_1_cuda(
|
||||
const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(num_blocks, block_size, 0, stream);
|
||||
switch (ds_layout) {
|
||||
case MMQ_Q8_1_DS_LAYOUT_D4:
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<MMQ_Q8_1_DS_LAYOUT_D4>, launch_params,
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D4>, launch_params,
|
||||
x, vy, ne00, s01, s02, s03, (int) ne1, (int) ne2);
|
||||
break;
|
||||
case MMQ_Q8_1_DS_LAYOUT_DS4:
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<MMQ_Q8_1_DS_LAYOUT_DS4>, launch_params,
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_DS4>, launch_params,
|
||||
x, vy, ne00, s01, s02, s03, (int) ne1, (int) ne2);
|
||||
break;
|
||||
default:
|
||||
@@ -771,8 +834,17 @@ void quantize_mmq_few_q8_1_cuda(
|
||||
}
|
||||
}
|
||||
|
||||
void quantize_mmq_few_id_q8_1_cuda(
|
||||
const float * x, const int32_t * ids, void * vy, int * pairs, const mmq_q8_1_ds_layout ds_layout, const int64_t ne00, const int64_t s11, const int64_t s12,
|
||||
void quantize_mmq_few_q8_1_cuda(
|
||||
const void * x, const ggml_type type_src1, void * vy, const mmq_q8_1_ds_layout ds_layout, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
|
||||
quantize_launch_typed(x, type_src1, [&](auto * x_t) {
|
||||
quantize_mmq_few_q8_1_cuda(x_t, vy, ds_layout, ne00, s01, s02, s03, ne1, ne2, ne3, stream);
|
||||
});
|
||||
}
|
||||
|
||||
template <typename src_t>
|
||||
static void quantize_mmq_few_id_q8_1_cuda(
|
||||
const src_t * x, const int32_t * ids, void * vy, int * pairs, const mmq_q8_1_ds_layout ds_layout, const int64_t ne00, const int64_t s11, const int64_t s12,
|
||||
const int64_t ne11, const int64_t n_experts, const int64_t n_expert_used, const int64_t n_tokens, const int64_t si1, const int64_t npairs_max, cudaStream_t stream) {
|
||||
GGML_ASSERT(ne00 % QK_K == 0);
|
||||
GGML_ASSERT(n_tokens <= 32); // one bit per token
|
||||
@@ -785,11 +857,11 @@ void quantize_mmq_few_id_q8_1_cuda(
|
||||
const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(num_blocks, block_size, nbytes_shared, stream);
|
||||
switch (ds_layout) {
|
||||
case MMQ_Q8_1_DS_LAYOUT_D4:
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_id_q8_1<MMQ_Q8_1_DS_LAYOUT_D4>, launch_params,
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_id_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D4>, launch_params,
|
||||
x, ids, vy, pairs, ne00, s11, s12, (int) ne11, (int) n_experts, (int) n_expert_used, si1, (int) npairs_max);
|
||||
break;
|
||||
case MMQ_Q8_1_DS_LAYOUT_DS4:
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_id_q8_1<MMQ_Q8_1_DS_LAYOUT_DS4>, launch_params,
|
||||
ggml_cuda_kernel_launch(quantize_mmq_few_id_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_DS4>, launch_params,
|
||||
x, ids, vy, pairs, ne00, s11, s12, (int) ne11, (int) n_experts, (int) n_expert_used, si1, (int) npairs_max);
|
||||
break;
|
||||
default:
|
||||
@@ -797,9 +869,18 @@ void quantize_mmq_few_id_q8_1_cuda(
|
||||
}
|
||||
}
|
||||
|
||||
void quantize_mmq_few_id_q8_1_cuda(
|
||||
const void * x, const ggml_type type_src1, const int32_t * ids, void * vy, int * pairs, const mmq_q8_1_ds_layout ds_layout, const int64_t ne00, const int64_t s11, const int64_t s12,
|
||||
const int64_t ne11, const int64_t n_experts, const int64_t n_expert_used, const int64_t n_tokens, const int64_t si1, const int64_t npairs_max, cudaStream_t stream) {
|
||||
quantize_launch_typed(x, type_src1, [&](auto * x_t) {
|
||||
quantize_mmq_few_id_q8_1_cuda(x_t, ids, vy, pairs, ds_layout, ne00, s11, s12, ne11, n_experts, n_expert_used, n_tokens, si1, npairs_max, stream);
|
||||
});
|
||||
}
|
||||
|
||||
// scatter=true reuses the quant kernel: grid over tokens, ids = inverse map (token slot -> compact row)
|
||||
void quantize_scatter_mmq_q8_1_cuda(
|
||||
const float * x, const int32_t * ids_src1_inv, void * vy, const ggml_type type_src0,
|
||||
template <typename src_t>
|
||||
static void quantize_scatter_mmq_q8_1_cuda(
|
||||
const src_t * x, const int32_t * ids_src1_inv, void * vy, const ggml_type type_src0,
|
||||
const int64_t ne00, const int64_t stride_token, const int64_t ne0,
|
||||
const int64_t n_tokens, const int64_t nrows_dst, const int n_expert_used, cudaStream_t stream) {
|
||||
GGML_ASSERT(ne00 % 4 == 0);
|
||||
@@ -810,15 +891,15 @@ void quantize_scatter_mmq_q8_1_cuda(
|
||||
const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1);
|
||||
switch (mmq_get_q8_1_ds_layout(type_src0)) {
|
||||
case MMQ_Q8_1_DS_LAYOUT_D4:
|
||||
quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D4, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
quantize_mmq_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D4, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
x, ids_src1_inv, vy, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/(int) nrows_dst, /*ne2=*/1, n_expert_used);
|
||||
break;
|
||||
case MMQ_Q8_1_DS_LAYOUT_DS4:
|
||||
quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_DS4, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
quantize_mmq_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_DS4, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
x, ids_src1_inv, vy, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/(int) nrows_dst, /*ne2=*/1, n_expert_used);
|
||||
break;
|
||||
case MMQ_Q8_1_DS_LAYOUT_D2S6:
|
||||
quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D2S6, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
quantize_mmq_q8_1<src_t, MMQ_Q8_1_DS_LAYOUT_D2S6, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
x, ids_src1_inv, vy, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/(int) nrows_dst, /*ne2=*/1, n_expert_used);
|
||||
break;
|
||||
default:
|
||||
@@ -826,9 +907,19 @@ void quantize_scatter_mmq_q8_1_cuda(
|
||||
}
|
||||
}
|
||||
|
||||
void quantize_scatter_mmq_q8_1_cuda(
|
||||
const void * x, const ggml_type type_src1, const int32_t * ids_src1_inv, void * vy, const ggml_type type_src0,
|
||||
const int64_t ne00, const int64_t stride_token, const int64_t ne0,
|
||||
const int64_t n_tokens, const int64_t nrows_dst, const int n_expert_used, cudaStream_t stream) {
|
||||
quantize_launch_typed(x, type_src1, [&](auto * x_t) {
|
||||
quantize_scatter_mmq_q8_1_cuda(x_t, ids_src1_inv, vy, type_src0, ne00, stride_token, ne0, n_tokens, nrows_dst, n_expert_used, stream);
|
||||
});
|
||||
}
|
||||
|
||||
// scatter=true reuses the quant kernels: grid over tokens, ids = inverse map (token slot -> compact row)
|
||||
void quantize_scatter_mmq_fp4_cuda(
|
||||
const float * x, const int32_t * ids_src1_inv, void * vy, float * scale, const ggml_type type_src0, const bool use_aligned_float8,
|
||||
template <typename src_t>
|
||||
static void quantize_scatter_mmq_fp4_cuda(
|
||||
const src_t * x, const int32_t * ids_src1_inv, void * vy, float * scale, const ggml_type type_src0, const bool use_aligned_float8,
|
||||
const int64_t ne00, const int64_t stride_token, const int64_t ne0,
|
||||
const int64_t n_tokens, const int64_t nrows_dst, const int n_expert_used, cudaStream_t stream) {
|
||||
GGML_ASSERT(ne0 > 0);
|
||||
@@ -838,10 +929,10 @@ void quantize_scatter_mmq_fp4_cuda(
|
||||
const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1);
|
||||
const dim3 num_blocks(n_tokens, 1, 1);
|
||||
if (use_aligned_float8) {
|
||||
quantize_mmq_nvfp4<true, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
quantize_mmq_nvfp4<src_t, true, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
x, ids_src1_inv, vy, scale, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/nrows_dst, /*ne2=*/1, n_expert_used);
|
||||
} else {
|
||||
quantize_mmq_nvfp4<true, false><<<num_blocks, block_size, 0, stream>>>(
|
||||
quantize_mmq_nvfp4<src_t, true, false><<<num_blocks, block_size, 0, stream>>>(
|
||||
x, ids_src1_inv, vy, scale, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/nrows_dst, /*ne2=*/1, n_expert_used);
|
||||
}
|
||||
} else {
|
||||
@@ -851,13 +942,24 @@ void quantize_scatter_mmq_fp4_cuda(
|
||||
const int64_t block_num_y = (ne0 + vals_per_block - 1) / vals_per_block;
|
||||
const dim3 block_size(WARP_SIZE, nwarps, 1);
|
||||
const dim3 num_blocks(n_tokens, block_num_y, 1);
|
||||
quantize_mmq_mxfp4<true><<<num_blocks, block_size, 0, stream>>>(
|
||||
quantize_mmq_mxfp4<src_t, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
x, ids_src1_inv, vy, ne00, /*s01=*/0, /*s02=*/stride_token, /*s03=*/0, ne0, /*ne1=*/(int) nrows_dst, /*ne2=*/1, n_expert_used);
|
||||
}
|
||||
}
|
||||
|
||||
void quantize_mmq_fp4_cuda(
|
||||
const float * x, const int32_t * ids, void * vy, float * scale, const ggml_type type_src0, const bool use_aligned_float8,
|
||||
void quantize_scatter_mmq_fp4_cuda(
|
||||
const void * x, const ggml_type type_src1, const int32_t * ids_src1_inv, void * vy, float * scale, const ggml_type type_src0,
|
||||
const bool use_aligned_float8, const int64_t ne00, const int64_t stride_token, const int64_t ne0,
|
||||
const int64_t n_tokens, const int64_t nrows_dst, const int n_expert_used, cudaStream_t stream) {
|
||||
quantize_launch_typed(x, type_src1, [&](auto * x_t) {
|
||||
quantize_scatter_mmq_fp4_cuda(x_t, ids_src1_inv, vy, scale, type_src0, use_aligned_float8, ne00, stride_token, ne0,
|
||||
n_tokens, nrows_dst, n_expert_used, stream);
|
||||
});
|
||||
}
|
||||
|
||||
template <typename src_t>
|
||||
static void quantize_mmq_fp4_cuda(
|
||||
const src_t * x, const int32_t * ids, void * vy, float * scale, const ggml_type type_src0, const bool use_aligned_float8,
|
||||
const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
|
||||
GGML_ASSERT(type_src0 == GGML_TYPE_MXFP4 || type_src0 == GGML_TYPE_NVFP4);
|
||||
@@ -869,10 +971,10 @@ void quantize_mmq_fp4_cuda(
|
||||
const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1);
|
||||
const dim3 num_blocks(ne1, ne2 * ne3, 1);
|
||||
if (use_aligned_float8) {
|
||||
quantize_mmq_nvfp4<false, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
quantize_mmq_nvfp4<src_t, false, true><<<num_blocks, block_size, 0, stream>>>(
|
||||
x, ids, vy, scale, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0);
|
||||
} else {
|
||||
quantize_mmq_nvfp4<false, false><<<num_blocks, block_size, 0, stream>>>(
|
||||
quantize_mmq_nvfp4<src_t, false, false><<<num_blocks, block_size, 0, stream>>>(
|
||||
x, ids, vy, scale, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0);
|
||||
}
|
||||
} else {
|
||||
@@ -886,6 +988,15 @@ void quantize_mmq_fp4_cuda(
|
||||
const dim3 num_blocks(ne1, block_num_y, ne2 * ne3);
|
||||
const dim3 block_size(WARP_SIZE, nwarps, 1);
|
||||
|
||||
quantize_mmq_mxfp4<false><<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0);
|
||||
quantize_mmq_mxfp4<src_t, false><<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2, /*n_expert_used=*/0);
|
||||
}
|
||||
}
|
||||
|
||||
void quantize_mmq_fp4_cuda(
|
||||
const void * x, const ggml_type type_src1, const int32_t * ids, void * vy, float * scale, const ggml_type type_src0,
|
||||
const bool use_aligned_float8, const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,
|
||||
const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, cudaStream_t stream) {
|
||||
quantize_launch_typed(x, type_src1, [&](auto * x_t) {
|
||||
quantize_mmq_fp4_cuda(x_t, ids, vy, scale, type_src0, use_aligned_float8, ne00, s01, s02, s03, ne0, ne1, ne2, ne3, stream);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -11,13 +11,15 @@
|
||||
static_assert(MATRIX_ROW_PADDING % CUDA_QUANTIZE_BLOCK_SIZE == 0, "Risk of out-of-bounds access.");
|
||||
static_assert(MATRIX_ROW_PADDING % (4*CUDA_QUANTIZE_BLOCK_SIZE_MMQ) == 0, "Risk of out-of-bounds access.");
|
||||
|
||||
// x holds the src1 values, type_src1 is GGML_TYPE_F32 or GGML_TYPE_BF16
|
||||
|
||||
void quantize_row_q8_1_cuda(
|
||||
const float * x, void * vy,
|
||||
const void * x, ggml_type type_src1, void * vy,
|
||||
int64_t ne00, int64_t s01, int64_t s02, int64_t s03,
|
||||
int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3, cudaStream_t stream);
|
||||
|
||||
void quantize_mmq_q8_1_cuda(
|
||||
const float * x, const int32_t * ids, void * vy,
|
||||
const void * x, ggml_type type_src1, const int32_t * ids, void * vy,
|
||||
ggml_type type_src0, int64_t ne00, int64_t s01, int64_t s02, int64_t s03,
|
||||
int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3, cudaStream_t stream);
|
||||
|
||||
@@ -29,7 +31,7 @@ static constexpr int MMQ_FEW_NCOLS = 8; // src1 columns per column group
|
||||
static constexpr int MMQ_FEW_Q8_1_INT4 = 4*WARP_SIZE + 4*4; // int4 per 256 values and column group
|
||||
|
||||
void quantize_mmq_few_q8_1_cuda(
|
||||
const float * x, void * vy, mmq_q8_1_ds_layout ds_layout, int64_t ne00, int64_t s01, int64_t s02, int64_t s03,
|
||||
const void * x, ggml_type type_src1, void * vy, mmq_q8_1_ds_layout ds_layout, int64_t ne00, int64_t s01, int64_t s02, int64_t s03,
|
||||
int64_t ne1, int64_t ne2, int64_t ne3, cudaStream_t stream);
|
||||
|
||||
// MUL_MAT_ID: a pair is an expert and one column group of up to MMQ_FEW_NCOLS of its (token, slot) entries. The src1 of pair p is channel p above, its columns past the entries are not written.
|
||||
@@ -37,10 +39,11 @@ void quantize_mmq_few_q8_1_cuda(
|
||||
static constexpr int MMQ_FEW_PAIR_INTS = 2 + MMQ_FEW_NCOLS;
|
||||
|
||||
void quantize_mmq_few_id_q8_1_cuda(
|
||||
const float * x, const int32_t * ids, void * vy, int * pairs, mmq_q8_1_ds_layout ds_layout, int64_t ne00, int64_t s11, int64_t s12,
|
||||
const void * x, ggml_type type_src1, const int32_t * ids, void * vy, int * pairs, mmq_q8_1_ds_layout ds_layout, int64_t ne00, int64_t s11, int64_t s12,
|
||||
int64_t ne11, int64_t n_experts, int64_t n_expert_used, int64_t n_tokens, int64_t si1, int64_t npairs_max, cudaStream_t stream);
|
||||
|
||||
void quantize_mmq_fp4_cuda(const float * x,
|
||||
void quantize_mmq_fp4_cuda(const void * x,
|
||||
ggml_type type_src1,
|
||||
const int32_t * ids,
|
||||
void * vy,
|
||||
float * scale,
|
||||
@@ -57,7 +60,8 @@ void quantize_mmq_fp4_cuda(const float * x,
|
||||
cudaStream_t stream);
|
||||
|
||||
// quantize each token once and scatter the block to its compact rows (via the inverse map)
|
||||
void quantize_scatter_mmq_fp4_cuda(const float * x,
|
||||
void quantize_scatter_mmq_fp4_cuda(const void * x,
|
||||
ggml_type type_src1,
|
||||
const int32_t * ids_src1_inv,
|
||||
void * vy,
|
||||
float * scale,
|
||||
@@ -71,7 +75,8 @@ void quantize_scatter_mmq_fp4_cuda(const float * x,
|
||||
int n_expert_used,
|
||||
cudaStream_t stream);
|
||||
|
||||
void quantize_scatter_mmq_q8_1_cuda(const float * x,
|
||||
void quantize_scatter_mmq_q8_1_cuda(const void * x,
|
||||
ggml_type type_src1,
|
||||
const int32_t * ids_src1_inv,
|
||||
void * vy,
|
||||
ggml_type type_src0,
|
||||
|
||||
@@ -7,12 +7,13 @@
|
||||
|
||||
// Kernel config struct - passed by value to CUDA kernel
|
||||
struct topk_moe_config {
|
||||
bool use_sigmoid;
|
||||
bool use_sqrt_softplus;
|
||||
bool with_norm;
|
||||
bool delayed_softmax;
|
||||
int n_groups;
|
||||
int n_group_used;
|
||||
bool use_sigmoid;
|
||||
bool use_sqrt_softplus;
|
||||
bool with_norm;
|
||||
bool delayed_softmax;
|
||||
int n_groups;
|
||||
int n_group_used;
|
||||
float norm_bias;
|
||||
};
|
||||
|
||||
// Warp-local softmax used for both the pre-top-k logits and the post-top-k delayed path.
|
||||
@@ -334,7 +335,7 @@ __global__ void topk_moe_cuda(const float * logits,
|
||||
|
||||
if (config.with_norm) {
|
||||
wt_sum = warp_reduce_sum<warp_size>(wt_sum);
|
||||
wt_sum = max(wt_sum, clamp_val);
|
||||
wt_sum = max(wt_sum, clamp_val) + config.norm_bias;
|
||||
const float inv_sum = 1.0f / wt_sum;
|
||||
|
||||
for (int i = 0; i < experts_per_thread; i++) {
|
||||
@@ -438,7 +439,7 @@ void ggml_cuda_op_topk_moe(ggml_backend_cuda_context & ctx,
|
||||
const ggml_tensor * logits,
|
||||
ggml_tensor * weights,
|
||||
ggml_tensor * ids,
|
||||
const ggml_tensor * clamp,
|
||||
const ggml_tensor * denom,
|
||||
const ggml_tensor * scale,
|
||||
const ggml_tensor * bias,
|
||||
const ggml_cuda_topk_moe_args & args) {
|
||||
@@ -460,11 +461,15 @@ void ggml_cuda_op_topk_moe(ggml_backend_cuda_context & ctx,
|
||||
|
||||
const int n_expert_used = weights->ne[1];
|
||||
|
||||
const bool with_norm = clamp != nullptr;
|
||||
const bool with_norm = denom != nullptr;
|
||||
|
||||
// the sum is clamped from below (CLAMP) or gets an additive bias (SCALE with scale 1)
|
||||
float clamp_val = -INFINITY;
|
||||
if (clamp) {
|
||||
clamp_val = ggml_get_op_params_f32(clamp, 0);
|
||||
float norm_bias = 0.0f;
|
||||
if (denom && denom->op == GGML_OP_SCALE) {
|
||||
norm_bias = ggml_get_op_params_f32(denom, 1);
|
||||
} else if (denom) {
|
||||
clamp_val = ggml_get_op_params_f32(denom, 0);
|
||||
}
|
||||
|
||||
topk_moe_config config;
|
||||
@@ -474,6 +479,7 @@ void ggml_cuda_op_topk_moe(ggml_backend_cuda_context & ctx,
|
||||
config.delayed_softmax = args.delayed_softmax;
|
||||
config.n_groups = args.n_groups;
|
||||
config.n_group_used = args.n_group_used;
|
||||
config.norm_bias = norm_bias;
|
||||
|
||||
if (bias) {
|
||||
launch_topk_moe_cuda<true>(ctx, logits_d, weights_d, ids_d, bias_d, n_rows, n_experts, n_expert_used, clamp_val,
|
||||
|
||||
@@ -13,6 +13,7 @@ struct ggml_cuda_topk_moe_args {
|
||||
bool delayed_softmax{};
|
||||
bool prob_bias{};
|
||||
bool norm{};
|
||||
bool norm_bias{}; // the weight sum gets SCALE (sum + bias) instead of CLAMP
|
||||
bool scale{};
|
||||
int n_groups{}; // > 0: pick experts only from the top n_group_used expert groups
|
||||
int n_group_used{};
|
||||
@@ -22,7 +23,7 @@ void ggml_cuda_op_topk_moe(ggml_backend_cuda_context & ctx,
|
||||
const ggml_tensor * logits,
|
||||
ggml_tensor * weights,
|
||||
ggml_tensor * ids,
|
||||
const ggml_tensor * clamp,
|
||||
const ggml_tensor * denom,
|
||||
const ggml_tensor * scale,
|
||||
const ggml_tensor * bias,
|
||||
const ggml_cuda_topk_moe_args & args);
|
||||
|
||||
@@ -59,6 +59,8 @@ ggml_add_backend_library(${TARGET_NAME}
|
||||
htp-opnode.h
|
||||
htp-fusion.cpp
|
||||
htp-fusion.h
|
||||
htp-lower.cpp
|
||||
htp-lower.h
|
||||
htp-repack.cpp
|
||||
htp-repack.h
|
||||
htp-drv.cpp
|
||||
|
||||
@@ -56,12 +56,14 @@
|
||||
#include "ggml-quants.h"
|
||||
#include "htp-opnode.h"
|
||||
#include "htp/htp-ops.h"
|
||||
#include "htp/htp-vtcm.h"
|
||||
#include "htp_iface.h"
|
||||
#include "htp-drv.h"
|
||||
#include "htp-repack.h"
|
||||
#include "htp-common.h"
|
||||
#include "htp-kparams.h"
|
||||
#include "htp-fusion.h"
|
||||
#include "htp-lower.h"
|
||||
|
||||
using intvec = std::vector<int>;
|
||||
using uintvec = std::vector<unsigned int>;
|
||||
@@ -132,8 +134,8 @@ size_t opt_mbuf_dyn = 512ul * 1024 * 1024; // max dynamic (compute) buff
|
||||
size_t opt_mbuf_static = 1ul * 1024 * 1024 * 1024; // max static (weight/KV) buffer size
|
||||
size_t opt_mbuf_total = 0; // total buffer space limit (0 = unconstrained)
|
||||
|
||||
int opt_mm_select = 2; // 2 = HMX -> HVX -> CPU, 1 = HVX -> CPU, 0 = CPU (unsupported)
|
||||
int opt_fa_select = 2; // 2 = HMX -> HVX -> CPU, 1 = HVX -> CPU, 0 = CPU (unsupported)
|
||||
int opt_mm_select = 3; // 4 = pure W8A8, 3 = hybrid W8A8 + FP16 post-GLU (default), 2 = FP16 HMX -> HVX -> CPU, 1 = HVX -> CPU, 0 = CPU
|
||||
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)
|
||||
int opt_gdn_select = 2; // 2 = HMX -> HVX, 1 = HVX, 0 = CPU (unsupported)
|
||||
@@ -151,6 +153,7 @@ int opt_opqueue = 8; // max number of pending batches
|
||||
int opt_optrace = 0; // trace buffer size per thread (0 means default)
|
||||
int opt_oppoll = 0; // polling for batch completions
|
||||
int opt_opfusion = 1; // enable/disable op fusion
|
||||
int opt_oplower = 1; // lower internal F32 ops to F16 (0: off, 1: on)
|
||||
int opt_rt_repack = 1; // runtime repacking: 0 = disabled, 1 = all tileable weights (default)
|
||||
|
||||
static std::regex* opt_opfilter = NULL; // regex of ops to not claim
|
||||
@@ -362,7 +365,7 @@ struct ggml_hexagon_session {
|
||||
std::unordered_map<uint64_t, volatile uint32_t *> cpy_fence_slots;
|
||||
|
||||
void enqueue_mdev_group();
|
||||
void enqueue_op(const htp_opnode & node);
|
||||
void enqueue_op(const htp_opnode & node, bool allow_fuse = true);
|
||||
void enqueue_cpy(const ggml_tensor * src, ggml_tensor * dst, const ggml_tensor * sync_tensor = nullptr, uint32_t fence_seq = 0);
|
||||
void enqueue_fence(const ggml_tensor * sync_tensor, uint32_t fence_seq = 0, bool wait = true);
|
||||
void enqueue_allreduce(const ggml_tensor * dst, const std::vector<const ggml_tensor *> & src_tensors,
|
||||
@@ -391,7 +394,7 @@ struct ggml_hexagon_session {
|
||||
};
|
||||
|
||||
const char * htp_session_name(const struct ggml_hexagon_session * sess) { return sess->c_name(); }
|
||||
size_t htp_session_vtcm_size(const struct ggml_hexagon_session * sess) { return sess->vtcm_size; }
|
||||
size_t htp_session_vtcm_size(const struct ggml_hexagon_session * sess) { return htp_vtcm_usable_size(sess->vtcm_size); }
|
||||
uint32_t htp_session_n_hvx(const struct ggml_hexagon_session * sess) { return sess->n_hvx; }
|
||||
uint32_t htp_session_n_hmx(const struct ggml_hexagon_session * sess) { return sess->n_hmx; }
|
||||
uint32_t htp_session_mdev_count(const struct ggml_hexagon_session * sess) { return sess->mdev.count; }
|
||||
@@ -697,6 +700,11 @@ static void ggml_backend_hexagon_buffer_set_tensor(ggml_backend_buffer_t buffer,
|
||||
HEX_VERBOSE("ggml-hex: %s set-tensor %s : data %p offset %zu size %zu usage %d flags 0x%x\n",
|
||||
sess->c_name(), tensor->name, data, offset, size, (int) buffer->usage, ggml_hexagon_tensor_get_flags(tensor));
|
||||
|
||||
if (htp_tensor_is_stored_f16(tensor)) {
|
||||
htp_pack_tensor_lowered_f16(tensor, data, offset, size);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy((char *) tensor->data + offset, data, size);
|
||||
}
|
||||
|
||||
@@ -712,12 +720,16 @@ static void ggml_backend_hexagon_buffer_get_tensor(ggml_backend_buffer_t buffer,
|
||||
HEX_VERBOSE("ggml-hex: %s get-tensor %s : data %p offset %zu size %zu usage %d flags 0x%x\n",
|
||||
sess->c_name(), tensor->name, data, offset, size, (int) buffer->usage, flags);
|
||||
|
||||
if ((flags & HTP_TENSOR_TILED) == 0) {
|
||||
memcpy(data, (const char *) tensor->data + offset, size);
|
||||
if (flags & HTP_TENSOR_TILED) {
|
||||
htp_unpack_tensor_tiled(data, tensor, offset, size);
|
||||
return;
|
||||
}
|
||||
if (htp_tensor_is_stored_f16(tensor)) {
|
||||
htp_unpack_tensor_lowered_f16(data, tensor, offset, size);
|
||||
return;
|
||||
}
|
||||
|
||||
htp_unpack_tensor_tiled(data, tensor, offset, size);
|
||||
memcpy(data, (const char *) tensor->data + offset, size);
|
||||
}
|
||||
|
||||
static bool ggml_backend_hexagon_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
|
||||
@@ -751,6 +763,13 @@ static void ggml_backend_hexagon_buffer_set_tensor_2d(ggml_backend_buffer_t buff
|
||||
HEX_VERBOSE("ggml-hex: %s set-tensor-2d %s : data %p offset %zu size %zu n_copies %zu stride_tensor %zu stride_data %zu usage %d flags 0x%x\n",
|
||||
sess->c_name(), tensor->name, data, offset, size, n_copies, stride_tensor, stride_data, (int) buffer->usage, ggml_hexagon_tensor_get_flags(tensor));
|
||||
|
||||
if (htp_tensor_is_stored_f16(tensor)) {
|
||||
for (size_t i = 0; i < n_copies; i++) {
|
||||
htp_pack_tensor_lowered_f16(tensor, (const uint8_t *) data + i * stride_data, offset + i * stride_tensor, size);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < n_copies; i++) {
|
||||
memcpy((uint8_t *) tensor->data + offset + i * stride_tensor, (const uint8_t *) data + i * stride_data, size);
|
||||
}
|
||||
@@ -772,6 +791,12 @@ static void ggml_backend_hexagon_buffer_get_tensor_2d(ggml_backend_buffer_t buff
|
||||
sess->c_name(), tensor->name, data, offset, size, n_copies, stride_tensor, stride_data, (int) buffer->usage);
|
||||
|
||||
if ((flags & HTP_TENSOR_TILED) == 0) {
|
||||
if (htp_tensor_is_stored_f16(tensor)) {
|
||||
for (size_t i = 0; i < n_copies; i++) {
|
||||
htp_unpack_tensor_lowered_f16((uint8_t *) data + i * stride_data, tensor, offset + i * stride_tensor, size);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (size_t i = 0; i < n_copies; i++) {
|
||||
memcpy((uint8_t *)data + i * stride_data, (const uint8_t *)tensor->data + offset + i * stride_tensor, size);
|
||||
}
|
||||
@@ -814,6 +839,10 @@ static void ggml_backend_hexagon_buffer_set_tensor_n(ggml_backend_buffer_t buffe
|
||||
if (is_weights) {
|
||||
ggml_hexagon_tensor_set_flags(tensors[i], HTP_TENSOR_WEIGHT);
|
||||
}
|
||||
if (htp_tensor_is_stored_f16(tensors[i])) {
|
||||
htp_pack_tensor_lowered_f16(tensors[i], data[i], offsets[i], sizes[i]);
|
||||
continue;
|
||||
}
|
||||
memcpy((char *) tensors[i]->data + offsets[i], data[i], sizes[i]);
|
||||
}
|
||||
}
|
||||
@@ -854,6 +883,11 @@ static void ggml_backend_hexagon_host_buffer_set_tensor(ggml_backend_buffer_t bu
|
||||
|
||||
sess->flush_sync();
|
||||
|
||||
if (htp_tensor_is_stored_f16(tensor)) {
|
||||
htp_pack_tensor_lowered_f16(tensor, data, offset, size);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy((char *) tensor->data + offset, data, size);
|
||||
}
|
||||
|
||||
@@ -862,6 +896,10 @@ static void ggml_backend_hexagon_host_buffer_get_tensor(ggml_backend_buffer_t bu
|
||||
void * data,
|
||||
size_t offset,
|
||||
size_t size) {
|
||||
if (htp_tensor_is_stored_f16(tensor)) {
|
||||
htp_unpack_tensor_lowered_f16(data, tensor, offset, size);
|
||||
return;
|
||||
}
|
||||
memcpy(data, (const char *) tensor->data + offset, size);
|
||||
GGML_UNUSED(buffer);
|
||||
}
|
||||
@@ -881,6 +919,10 @@ static void ggml_backend_hexagon_host_buffer_set_tensor_n(ggml_backend_buffer_t
|
||||
if (sizes[i] == 0) {
|
||||
continue;
|
||||
}
|
||||
if (htp_tensor_is_stored_f16(tensors[i])) {
|
||||
htp_pack_tensor_lowered_f16(tensors[i], data[i], offsets[i], sizes[i]);
|
||||
continue;
|
||||
}
|
||||
memcpy((char *) tensors[i]->data + offsets[i], data[i], sizes[i]);
|
||||
}
|
||||
}
|
||||
@@ -1247,6 +1289,58 @@ static bool ggml_backend_buffer_is_hexagon(const struct ggml_backend_buffer * b)
|
||||
return b->buft->iface.get_alignment == ggml_backend_hexagon_buffer_type_get_alignment;
|
||||
}
|
||||
|
||||
struct ggml_hexagon_tensor_map {
|
||||
struct slot {
|
||||
uint64_t key;
|
||||
uint32_t gen;
|
||||
int32_t val;
|
||||
};
|
||||
|
||||
std::vector<slot> slots;
|
||||
uint32_t mask = 0;
|
||||
uint32_t gen = 1;
|
||||
|
||||
void init(size_t max_entries) {
|
||||
size_t cap = 64;
|
||||
while (cap < max_entries * 2) {
|
||||
cap <<= 1;
|
||||
}
|
||||
slots.assign(cap, { 0, 0, 0 });
|
||||
mask = (uint32_t) (cap - 1);
|
||||
gen = 1;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
if (++gen == 0) {
|
||||
for (auto & s : slots) { s.gen = 0; }
|
||||
gen = 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool contains(uint64_t key) const {
|
||||
uint32_t i = (uint32_t) key & mask;
|
||||
while (slots[i].gen == gen) {
|
||||
if (slots[i].key == key) { return true; }
|
||||
i = (i + 1) & mask;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int find_or_insert(uint64_t key, int new_val, bool & inserted) {
|
||||
uint32_t i = (uint32_t) key & mask;
|
||||
while (slots[i].gen == gen) {
|
||||
if (slots[i].key == key) {
|
||||
inserted = false;
|
||||
return slots[i].val;
|
||||
}
|
||||
i = (i + 1) & mask;
|
||||
}
|
||||
slots[i] = { key, gen, new_val };
|
||||
inserted = true;
|
||||
return new_val;
|
||||
}
|
||||
};
|
||||
|
||||
struct ggml_hexagon_opbatch {
|
||||
ggml_hexagon_session* sess;
|
||||
|
||||
@@ -1255,12 +1349,13 @@ struct ggml_hexagon_opbatch {
|
||||
std::vector<htp_tensor> h_tens; // htp tensor descriptors
|
||||
std::vector<htp_op_desc> h_ops; // htp op descriptors
|
||||
|
||||
std::unordered_map<uint64_t, int> t_map; // tensor hash to index
|
||||
ggml_hexagon_tensor_map t_map; // tensor hash to index
|
||||
|
||||
unsigned int n_bufs; // num buffers in the batch
|
||||
unsigned int n_tens; // num tensors ...
|
||||
unsigned int n_ops; // num ops ...
|
||||
size_t b_vmem; // sum of non-extended buffer sizes
|
||||
bool last_op_fusable;
|
||||
|
||||
unsigned int n_bufs_max;
|
||||
unsigned int n_tens_max;
|
||||
@@ -1272,9 +1367,9 @@ struct ggml_hexagon_opbatch {
|
||||
n_tens = 0;
|
||||
n_ops = 0;
|
||||
b_vmem = 0;
|
||||
last_op_fusable = false;
|
||||
|
||||
t_map.clear();
|
||||
ops.resize(n_ops_max);
|
||||
}
|
||||
|
||||
ggml_hexagon_opbatch(ggml_hexagon_session *sess, size_t batch_size, size_t max_vmem) {
|
||||
@@ -1292,7 +1387,7 @@ struct ggml_hexagon_opbatch {
|
||||
h_tens.resize(n_tens_max);
|
||||
h_ops.resize(n_ops_max);
|
||||
|
||||
t_map.reserve(n_tens_max);
|
||||
t_map.init(n_tens_max);
|
||||
|
||||
GGML_LOG_INFO("ggml-hex: %s op batching: n-bufs %u n-tensors %u n-ops %u vmem %zu\n",
|
||||
sess->c_name(), n_bufs_max, n_tens_max, n_ops_max, b_vmem_max);
|
||||
@@ -1371,20 +1466,16 @@ struct ggml_hexagon_opbatch {
|
||||
}
|
||||
|
||||
static uint64_t hash_tensor(const ggml_tensor * t, const htp_tensor & shape) {
|
||||
uint64_t hash = 1469598103934665603ull;
|
||||
auto hash_value = [&hash](uint64_t value) {
|
||||
hash ^= value;
|
||||
hash *= 1099511628211ull;
|
||||
};
|
||||
|
||||
hash_value((uintptr_t) t->data);
|
||||
hash_value(shape.type);
|
||||
hash_value(shape.size);
|
||||
hash_value(shape.flags);
|
||||
for (int i = 0; i < GGML_MAX_DIMS; ++i) {
|
||||
hash_value(shape.ne[i]);
|
||||
hash_value(shape.nb[i]);
|
||||
}
|
||||
uint64_t h1 = (uintptr_t) t->data ^ ((uint64_t) shape.type << 48) ^ ((uint64_t) shape.flags << 32) ^ shape.size;
|
||||
uint64_t h2 = (uint64_t) shape.ne[0]
|
||||
^ ((uint64_t) shape.ne[1] * 0x9e3779b97f4a7c15ull)
|
||||
^ ((uint64_t) shape.ne[2] * 0xbf58476d1ce4e5b9ull)
|
||||
^ ((uint64_t) shape.ne[3] * 0x94d049bb133111ebull);
|
||||
uint64_t h3 = (uint64_t) shape.nb[0]
|
||||
^ ((uint64_t) shape.nb[1] * 0x9e3779b97f4a7c15ull)
|
||||
^ ((uint64_t) shape.nb[2] * 0xbf58476d1ce4e5b9ull)
|
||||
^ ((uint64_t) shape.nb[3] * 0x94d049bb133111ebull);
|
||||
uint64_t hash = (h1 * 0xff51afd7ed558ccdull) ^ (h2 * 0xc4ceb9fe1a85ec53ull) ^ h3;
|
||||
|
||||
// avalanche high bits down to low bits for power-of-two table indexing
|
||||
hash ^= hash >> 33;
|
||||
@@ -1400,19 +1491,16 @@ struct ggml_hexagon_opbatch {
|
||||
init_tensor_shape(shape, t);
|
||||
|
||||
const uint64_t hash = hash_tensor(t, shape);
|
||||
auto it = t_map.find(hash);
|
||||
if (it != t_map.end()) {
|
||||
return it->second;
|
||||
bool inserted = false;
|
||||
int ti = t_map.find_or_insert(hash, (int) n_tens, inserted);
|
||||
if (!inserted) {
|
||||
return ti;
|
||||
}
|
||||
|
||||
auto sbuf = static_cast<ggml_hexagon_shared_buffer *>(t->buffer->context);
|
||||
|
||||
// Add new tensor to the batch
|
||||
int ti = n_tens++;
|
||||
n_tens++;
|
||||
GGML_ASSERT(n_tens <= n_tens_max);
|
||||
|
||||
t_map[hash] = ti;
|
||||
|
||||
auto sbuf = static_cast<ggml_hexagon_shared_buffer *>(t->buffer->context);
|
||||
uint64_t t_offset = (uint8_t *) t->data - sbuf->base();
|
||||
|
||||
htp_tensor & h = h_tens[ti];
|
||||
@@ -1445,7 +1533,7 @@ struct ggml_hexagon_opbatch {
|
||||
if (check_tens) {
|
||||
htp_tensor shape {};
|
||||
init_tensor_shape(shape, t);
|
||||
if (t_map.count(hash_tensor(t, shape))) return;
|
||||
if (t_map.contains(hash_tensor(t, shape))) return;
|
||||
extra_tens++;
|
||||
}
|
||||
|
||||
@@ -1479,8 +1567,7 @@ struct ggml_hexagon_opbatch {
|
||||
return true;
|
||||
}
|
||||
|
||||
void sync_op_desc(unsigned int n) {
|
||||
const htp_opnode & node = ops[n];
|
||||
void sync_op_desc(unsigned int n, const htp_opnode & node) {
|
||||
htp_op_desc & o = h_ops[n];
|
||||
|
||||
memcpy(o.params, node.params, sizeof(o.params));
|
||||
@@ -1499,24 +1586,29 @@ struct ggml_hexagon_opbatch {
|
||||
}
|
||||
|
||||
// caller must ensure the op fits
|
||||
void add_op(const htp_opnode & node) {
|
||||
void add_op(const htp_opnode & node, bool save_op = true) {
|
||||
unsigned int n = n_ops++;
|
||||
GGML_ASSERT(n_ops <= n_ops_max);
|
||||
|
||||
ops[n] = node;
|
||||
if (save_op) {
|
||||
ops[n] = node;
|
||||
}
|
||||
last_op_fusable = save_op;
|
||||
h_ops[n].flags = 0;
|
||||
|
||||
ggml_hexagon_dump_op_exec(sess->c_name(), ops[n], h_ops[n].flags);
|
||||
sync_op_desc(n);
|
||||
if (opt_verbose) {
|
||||
ggml_hexagon_dump_op_exec(sess->c_name(), node, h_ops[n].flags);
|
||||
}
|
||||
sync_op_desc(n, node);
|
||||
}
|
||||
|
||||
void sort_buffers() {
|
||||
if (n_bufs <= 1) return;
|
||||
|
||||
std::vector<int> order(n_bufs);
|
||||
int order[HTP_OP_MAX_BUFS];
|
||||
for (unsigned int i = 0; i < n_bufs; i++) { order[i] = (int) i; }
|
||||
|
||||
std::stable_sort(order.begin(), order.end(), [&](int a, int b) {
|
||||
std::stable_sort(order, order + n_bufs, [&](int a, int b) {
|
||||
return h_bufs[a].size > h_bufs[b].size;
|
||||
});
|
||||
|
||||
@@ -1529,8 +1621,8 @@ struct ggml_hexagon_opbatch {
|
||||
}
|
||||
if (already_sorted) return;
|
||||
|
||||
std::vector<uint16_t> remap(n_bufs);
|
||||
std::vector<htp_buf_desc> sorted_bufs(n_bufs);
|
||||
uint16_t remap[HTP_OP_MAX_BUFS];
|
||||
htp_buf_desc sorted_bufs[HTP_OP_MAX_BUFS];
|
||||
for (unsigned int new_bi = 0; new_bi < n_bufs; new_bi++) {
|
||||
int old_bi = order[new_bi];
|
||||
remap[old_bi] = (uint16_t) new_bi;
|
||||
@@ -1553,7 +1645,7 @@ struct ggml_hexagon_opbatch {
|
||||
}
|
||||
|
||||
bool try_fuse(const htp_opnode & node) {
|
||||
if (n_ops == 0) {
|
||||
if (n_ops == 0 || !last_op_fusable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1562,7 +1654,8 @@ struct ggml_hexagon_opbatch {
|
||||
return false;
|
||||
}
|
||||
|
||||
sync_op_desc(n_ops - 1);
|
||||
htp_lower_opnode(sess, last_node);
|
||||
sync_op_desc(n_ops - 1, last_node);
|
||||
HEX_VERBOSE("ggml-hex: %s fused %s (N=%zu, #%u)\n",
|
||||
sess->c_name(), last_node.name().c_str(), last_node.fused.size() + 1, n_ops - 1);
|
||||
return true;
|
||||
@@ -1776,12 +1869,20 @@ void ggml_hexagon_session::flush_pending(bool all) {
|
||||
struct dspqueue_buffer dbuf;
|
||||
uint32_t n_dbufs;
|
||||
|
||||
// Read response packet from queue
|
||||
const uint32_t timeo = opt_oppoll ? 0 : DSPQUEUE_TIMEOUT;
|
||||
// opt_oppoll: 0 = block, 1 = poll, >1 = hybrid poll budget in ms
|
||||
const auto poll_deadline = opt_oppoll > 1
|
||||
? std::chrono::steady_clock::now() + std::chrono::milliseconds(opt_oppoll)
|
||||
: std::chrono::steady_clock::time_point::max();
|
||||
|
||||
int err = dspqueue_read(this->queue, &flags, 1, &n_dbufs, &dbuf, sizeof(rsp), &rsp_size, (uint8_t *) &rsp, timeo);
|
||||
if (err == AEE_EEXPIRED || err == AEE_EWOULDBLOCK) {
|
||||
continue;
|
||||
int err = 0;
|
||||
for (;;) {
|
||||
const uint32_t timeo = (opt_oppoll == 1 || (opt_oppoll > 1 && std::chrono::steady_clock::now() < poll_deadline))
|
||||
? 0 : DSPQUEUE_TIMEOUT;
|
||||
|
||||
err = dspqueue_read(this->queue, &flags, 1, &n_dbufs, &dbuf, sizeof(rsp), &rsp_size, (uint8_t *) &rsp, timeo);
|
||||
if (err != AEE_EEXPIRED && err != AEE_EWOULDBLOCK) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (err != 0) {
|
||||
@@ -1869,10 +1970,13 @@ void ggml_hexagon_session::flush_batch(size_t min_ops) {
|
||||
op_batch->reset();
|
||||
}
|
||||
|
||||
void ggml_hexagon_session::enqueue_op(const htp_opnode & node) {
|
||||
void ggml_hexagon_session::enqueue_op(const htp_opnode & node, bool allow_fuse) {
|
||||
auto register_tensor = [&](const ggml_tensor * t) {
|
||||
if (!t) return;
|
||||
auto * sbuf = static_cast<ggml_hexagon_shared_buffer *>(t->buffer->context);
|
||||
if (sbuf->sess == this && sbuf->mapped && this->mdev.sessions.empty()) {
|
||||
return;
|
||||
}
|
||||
if (ggml_backend_buffer_get_usage(t->buffer) == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
|
||||
sbuf->extended = opt_dma64;
|
||||
}
|
||||
@@ -1896,7 +2000,7 @@ void ggml_hexagon_session::enqueue_op(const htp_opnode & node) {
|
||||
}
|
||||
|
||||
const bool fits_tensors = op_batch->fit_tensors(node);
|
||||
if (opt_opfusion && fits_tensors && op_batch->try_fuse(node)) {
|
||||
if (opt_opfusion && allow_fuse && fits_tensors && op_batch->try_fuse(node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1911,7 +2015,7 @@ void ggml_hexagon_session::enqueue_op(const htp_opnode & node) {
|
||||
}
|
||||
}
|
||||
|
||||
op_batch->add_op(node);
|
||||
op_batch->add_op(node, opt_profile || (opt_opfusion && allow_fuse));
|
||||
}
|
||||
|
||||
void ggml_hexagon_session::enqueue_mdev_group() {
|
||||
@@ -2299,6 +2403,7 @@ void ggml_hexagon_session::allocate(const ggml_hexagon_device_config & config) n
|
||||
this->n_hmx = (opt_nhmx != 0) ? 1 : 0;
|
||||
this->vtcm_size = opt_vtcm_size > 0 ? opt_vtcm_size : 8 * 1024 * 1024;
|
||||
}
|
||||
GGML_ASSERT(this->n_hvx > 0 && this->vtcm_size >= 2 * HTP_VTCM_GUARD_SIZE);
|
||||
}
|
||||
|
||||
// Enable FastRPC QoS mode
|
||||
@@ -2533,18 +2638,16 @@ static ggml_status ggml_backend_hexagon_graph_compute(ggml_backend_t backend, gg
|
||||
|
||||
HEX_VERBOSE("ggml-hex: %s graph-compute n_nodes %d\n", sess->c_name(), graph->n_nodes);
|
||||
|
||||
const std::vector<htp_opnode> * nodes_ptr = nullptr;
|
||||
std::vector<htp_opnode> computed_nodes;
|
||||
|
||||
bool cache_hit = (graph->uid != 0 && sess->cached_uid == graph->uid);
|
||||
if (cache_hit) {
|
||||
nodes_ptr = &sess->cached_nodes;
|
||||
} else {
|
||||
const bool cache_hit = graph->uid != 0 && sess->cached_uid == graph->uid;
|
||||
if (!cache_hit) {
|
||||
ggml_hexagon_graph_repack(graph, sess);
|
||||
|
||||
htp_lower_reset_graph(graph);
|
||||
htp_fusion_tag_graph(graph);
|
||||
|
||||
computed_nodes.reserve(graph->n_nodes);
|
||||
auto & nodes = sess->cached_nodes;
|
||||
nodes.clear();
|
||||
nodes.reserve(graph->n_nodes);
|
||||
|
||||
for (int i = 0; i < graph->n_nodes; ++i) {
|
||||
ggml_tensor * n = graph->nodes[i];
|
||||
@@ -2559,21 +2662,22 @@ static ggml_status ggml_backend_hexagon_graph_compute(ggml_backend_t backend, gg
|
||||
|
||||
htp_opnode node(opcode, n);
|
||||
GGML_ASSERT(htp_kparams_solve(sess, node));
|
||||
|
||||
computed_nodes.push_back(std::move(node));
|
||||
if (nodes.empty() || !opt_opfusion || !htp_fusion_try_fuse(sess, nodes.back(), node)) {
|
||||
nodes.push_back(std::move(node));
|
||||
}
|
||||
}
|
||||
|
||||
if (graph->uid != 0) {
|
||||
sess->cached_uid = graph->uid;
|
||||
sess->cached_nodes = std::move(computed_nodes);
|
||||
nodes_ptr = &sess->cached_nodes;
|
||||
} else {
|
||||
nodes_ptr = &computed_nodes;
|
||||
if (opt_opfusion) {
|
||||
htp_fusion_optimize_nodes(sess, graph, nodes);
|
||||
}
|
||||
|
||||
htp_lower_graph(sess, graph, nodes);
|
||||
|
||||
sess->cached_uid = graph->uid;
|
||||
}
|
||||
|
||||
for (const auto & node : *nodes_ptr) {
|
||||
sess->enqueue_op(node);
|
||||
for (const auto & node : sess->cached_nodes) {
|
||||
sess->enqueue_op(node, false);
|
||||
}
|
||||
|
||||
if (sess->last_error > HTP_STATUS_OK) {
|
||||
@@ -2737,7 +2841,7 @@ static std::vector<int> ggml_hexagon_graph_optimize_reorder(const std::vector<no
|
||||
}
|
||||
|
||||
// that many nodes forward to search for stackable nodes that can reuse VTCM
|
||||
constexpr int N_FORWARD = 64;
|
||||
constexpr int N_FORWARD = 128;
|
||||
|
||||
htp_opvec<int, N_FORWARD> stack;
|
||||
stack.push_back(i0);
|
||||
@@ -3536,6 +3640,7 @@ static void ggml_hexagon_init(ggml_backend_reg * reg) {
|
||||
const char * str_opqueue = getenv("GGML_HEXAGON_OPQUEUE");
|
||||
const char * str_oppoll = getenv("GGML_HEXAGON_OPPOLL");
|
||||
const char * str_opfusion = getenv("GGML_HEXAGON_OPFUSION");
|
||||
const char * str_oplower = getenv("GGML_HEXAGON_OPLOWER");
|
||||
const char * str_rt_repack = getenv("GGML_HEXAGON_RT_REPACK");
|
||||
const char * str_opfilter = getenv("GGML_HEXAGON_OPFILTER");
|
||||
const char * str_profile = getenv("GGML_HEXAGON_PROFILE");
|
||||
@@ -3561,12 +3666,14 @@ static void ggml_hexagon_init(ggml_backend_reg * reg) {
|
||||
const char * str_power_mode = getenv("GGML_HEXAGON_POWER_MODE");
|
||||
|
||||
// Init Arch first since it affects other defaults
|
||||
int hw_arch = 0;
|
||||
int arch_err = htpdrv_get_arch(CDSP_DOMAIN_ID, &hw_arch);
|
||||
if (!str_arch) {
|
||||
int err = htpdrv_get_arch(CDSP_DOMAIN_ID, &opt_arch);
|
||||
if (err != 0) {
|
||||
GGML_LOG_ERROR("ggml-hex: failed to query HTP version (err %d) defaulting to v73\n", err);
|
||||
if (arch_err != 0) {
|
||||
GGML_LOG_ERROR("ggml-hex: failed to query HTP version (err %d) defaulting to v73\n", arch_err);
|
||||
opt_arch = 73;
|
||||
} else {
|
||||
opt_arch = hw_arch;
|
||||
if (opt_arch < 73) {
|
||||
GGML_LOG_WARN("ggml-hex: Hexagon arch v%d is under supported range, capping at v73\n", opt_arch);
|
||||
opt_arch = 73;
|
||||
@@ -3581,6 +3688,9 @@ static void ggml_hexagon_init(ggml_backend_reg * reg) {
|
||||
}
|
||||
opt_arch = strtoul(str_arch, NULL, 0);
|
||||
}
|
||||
if (arch_err != 0) {
|
||||
hw_arch = opt_arch;
|
||||
}
|
||||
|
||||
size_t MiB = 1024 * 1024;
|
||||
|
||||
@@ -3595,8 +3705,9 @@ static void ggml_hexagon_init(ggml_backend_reg * reg) {
|
||||
opt_opbatch = str_opbatch ? strtoul(str_opbatch, NULL, 0) : opt_opbatch;
|
||||
opt_opqueue = str_opqueue ? strtoul(str_opqueue, NULL, 0) : opt_opqueue;
|
||||
opt_optrace = str_optrace ? strtoul(str_optrace, NULL, 0) : (opt_opbatch * HTP_TRACE_MAX_EVENTS + 131072);
|
||||
opt_oppoll = str_oppoll ? strtoul(str_oppoll, NULL, 0) : opt_oppoll;
|
||||
opt_oppoll = str_oppoll ? strtoul(str_oppoll, NULL, 0) : (htpdrv_has_read_noblock() ? 50 : 0);
|
||||
opt_opfusion = str_opfusion ? atoi(str_opfusion) : opt_opfusion;
|
||||
opt_oplower = str_oplower ? (atoi(str_oplower) != 0 ? 1 : 0) : opt_oplower;
|
||||
opt_rt_repack = str_rt_repack ? atoi(str_rt_repack) : opt_rt_repack;
|
||||
opt_profile = str_profile ? atoi(str_profile) : 0;
|
||||
opt_etm = str_etm ? atoi(str_etm) : 0;
|
||||
@@ -3835,6 +3946,19 @@ static void ggml_hexagon_init(ggml_backend_reg * reg) {
|
||||
// Resolve domain info for all configured devices
|
||||
ggml_hexagon_discover_devices();
|
||||
|
||||
size_t total_sessions = opt_ndev;
|
||||
for (size_t i = 0; i < opt_ndev; i++) {
|
||||
total_sessions += opt_device_configs[i].mdev_group.size();
|
||||
}
|
||||
if (!str_mm_select && hw_arch == 73 && total_sessions > 1) {
|
||||
opt_mm_select = 2;
|
||||
GGML_LOG_INFO("ggml-hex: defaulting MM_SELECT=2 on multi-session v73 hardware\n");
|
||||
}
|
||||
if (!str_fa_select && hw_arch == 73 && total_sessions > 1) {
|
||||
opt_fa_select = 2;
|
||||
GGML_LOG_INFO("ggml-hex: defaulting FA_SELECT=2 on multi-session v73 hardware\n");
|
||||
}
|
||||
|
||||
if (str_profile) {
|
||||
opt_pmu_evt = [&]() -> std::vector<uint32_t> {
|
||||
auto v = str_to_vec<uint32_t>(str_profile);
|
||||
|
||||
@@ -34,6 +34,7 @@ extern int opt_opqueue;
|
||||
extern int opt_optrace;
|
||||
extern int opt_oppoll;
|
||||
extern int opt_opfusion;
|
||||
extern int opt_oplower;
|
||||
extern int opt_rt_repack;
|
||||
|
||||
#ifndef HEX_VERBOSE
|
||||
@@ -61,10 +62,66 @@ static inline bool ggml_hexagon_tensor_is_tiled(const struct ggml_tensor * t) {
|
||||
return (ggml_hexagon_tensor_get_flags(t) & HTP_TENSOR_TILED) != 0;
|
||||
}
|
||||
|
||||
static inline const struct ggml_tensor * htp_tensor_root(const struct ggml_tensor * t) {
|
||||
while (t && t->view_src) {
|
||||
t = t->view_src;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
static inline void ggml_hexagon_tensor_clear_lowered(struct ggml_tensor * t) {
|
||||
for (; t != nullptr; t = t->view_src) {
|
||||
ggml_hexagon_tensor_clear_flags(t, HTP_TENSOR_LOWERED_F16);
|
||||
}
|
||||
}
|
||||
|
||||
static inline bool ggml_hexagon_tensor_is_lowered_f16(const struct ggml_tensor * t) {
|
||||
return t && (ggml_hexagon_tensor_get_flags(t) & HTP_TENSOR_LOWERED_F16) != 0;
|
||||
}
|
||||
|
||||
static inline bool htp_tensor_is_stored_f16(const struct ggml_tensor * t) {
|
||||
return t && t->type == GGML_TYPE_F32 &&
|
||||
(ggml_hexagon_tensor_is_lowered_f16(t) || ggml_hexagon_tensor_is_lowered_f16(htp_tensor_root(t)));
|
||||
}
|
||||
|
||||
static inline enum ggml_type htp_effective_type(const struct ggml_tensor * t) {
|
||||
return ggml_hexagon_tensor_is_lowered_f16(t) ? GGML_TYPE_F16 : t->type;
|
||||
}
|
||||
|
||||
static inline uint8_t * htp_tensor_data_ptr(const struct ggml_tensor * t) {
|
||||
if (!ggml_hexagon_tensor_is_lowered_f16(t) || !t->view_src) {
|
||||
return (uint8_t *) t->data;
|
||||
}
|
||||
const struct ggml_tensor * root = htp_tensor_root(t);
|
||||
return (uint8_t *) root->data + ((const uint8_t *) t->data - (const uint8_t *) root->data) / 2;
|
||||
}
|
||||
|
||||
static inline size_t htp_tensor_nbytes(const struct ggml_tensor * t) {
|
||||
return ggml_hexagon_tensor_is_lowered_f16(t) ? (ggml_nbytes(t) / 2) : ggml_nbytes(t);
|
||||
}
|
||||
|
||||
// Lower a tagged F32 tensor to F16 on the stack and clear HTP_TENSOR_LOWERED_F16 so lowering is idempotent.
|
||||
static inline struct ggml_tensor htp_tensor_lower_f16(const struct ggml_tensor * t) {
|
||||
struct ggml_tensor m = *t;
|
||||
if (ggml_hexagon_tensor_is_lowered_f16(t)) {
|
||||
m.type = GGML_TYPE_F16;
|
||||
for (int d = 0; d < GGML_MAX_DIMS; ++d) {
|
||||
m.nb[d] /= 2;
|
||||
}
|
||||
m.view_offs /= 2;
|
||||
m.data = htp_tensor_data_ptr(t);
|
||||
m.extra = (void *) (uintptr_t) (ggml_hexagon_tensor_get_flags(t) & ~HTP_TENSOR_LOWERED_F16);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
static inline bool ggml_hexagon_tensor_can_tile(const struct ggml_tensor * t) {
|
||||
if (opt_rt_repack == 0) {
|
||||
return false;
|
||||
}
|
||||
if (!ggml_op_is_empty(t->op)) {
|
||||
return false;
|
||||
}
|
||||
if (t->buffer) {
|
||||
if (ggml_backend_buft_is_host(t->buffer->buft) ||
|
||||
ggml_backend_buffer_get_usage(t->buffer) == GGML_BACKEND_BUFFER_USAGE_COMPUTE) {
|
||||
@@ -79,38 +136,14 @@ static inline bool ggml_hexagon_tensor_will_tile(const struct ggml_tensor * t) {
|
||||
}
|
||||
|
||||
static inline bool htp_tensors_overlap(const struct ggml_tensor * a, const struct ggml_tensor * b) {
|
||||
const uintptr_t a0 = (uintptr_t) a->data;
|
||||
const uintptr_t b0 = (uintptr_t) b->data;
|
||||
const uintptr_t a1 = a0 + ggml_nbytes(a);
|
||||
const uintptr_t b1 = b0 + ggml_nbytes(b);
|
||||
const uintptr_t a0 = (uintptr_t) htp_tensor_data_ptr(a);
|
||||
const uintptr_t b0 = (uintptr_t) htp_tensor_data_ptr(b);
|
||||
const uintptr_t a1 = a0 + htp_tensor_nbytes(a);
|
||||
const uintptr_t b1 = b0 + htp_tensor_nbytes(b);
|
||||
|
||||
return a0 < b1 && b0 < a1;
|
||||
}
|
||||
|
||||
static inline bool htp_can_row_partition(const struct ggml_tensor * t) {
|
||||
if (t->ne[1] > 1 && (t->nb[1] & 127) != 0) return false;
|
||||
if (t->ne[2] > 1 && (t->nb[2] & 127) != 0) return false;
|
||||
if (t->ne[3] > 1 && (t->nb[3] & 127) != 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline uint32_t htp_mdev_rows_per_chunk(const struct ggml_tensor * t) {
|
||||
if ((t->data && ((uintptr_t) t->data & 127) != 0) || (t->view_offs & 127) != 0) return 0;
|
||||
const size_t elem_size = ggml_type_size(t->type);
|
||||
if (t->ne[0] != 1 && t->nb[0] != elem_size) return 0;
|
||||
if (ggml_is_permuted(t)) return 0;
|
||||
if (t->ne[2] > 1 && (t->nb[2] & 127) != 0) return 0;
|
||||
if (t->ne[3] > 1 && (t->nb[3] & 127) != 0) return 0;
|
||||
if (t->ne[1] > 1 && (t->nb[1] & 127) == 0) return 1;
|
||||
const size_t row_size = (size_t) t->ne[0] * elem_size;
|
||||
if (t->nb[1] == row_size &&
|
||||
(t->ne[2] <= 1 || t->nb[2] == t->nb[1] * (size_t) t->ne[1]) &&
|
||||
(t->ne[3] <= 1 || t->nb[3] == t->nb[2] * (size_t) t->ne[2])) {
|
||||
return (row_size > 0) ? (HEX_L2_LINE_SIZE / hex_gcd_u32((uint32_t) row_size, HEX_L2_LINE_SIZE)) : 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ggml_hexagon_session;
|
||||
|
||||
const char * htp_session_name(const struct ggml_hexagon_session * sess);
|
||||
|
||||
@@ -174,6 +174,10 @@ AEEResult dspqueue_write(dspqueue_t queue,
|
||||
return dspqueue_write_pfn(queue, flags, num_buffers, buffers, message_length, message, timeout_us);
|
||||
}
|
||||
|
||||
bool htpdrv_has_read_noblock(void) {
|
||||
return dspqueue_read_noblock_pfn != nullptr;
|
||||
}
|
||||
|
||||
AEEResult dspqueue_read(dspqueue_t queue,
|
||||
uint32_t * flags,
|
||||
uint32_t max_buffers,
|
||||
@@ -183,12 +187,10 @@ AEEResult dspqueue_read(dspqueue_t queue,
|
||||
uint32_t * message_length,
|
||||
uint8_t * message,
|
||||
uint32_t timeout_us) {
|
||||
#ifdef _WIN32
|
||||
if (timeout_us == 0) {
|
||||
if (timeout_us == 0 && dspqueue_read_noblock_pfn != nullptr) {
|
||||
return dspqueue_read_noblock_pfn(queue, flags, max_buffers, num_buffers, buffers, max_message_length,
|
||||
message_length, message);
|
||||
message_length, message);
|
||||
}
|
||||
#endif
|
||||
return dspqueue_read_pfn(queue, flags, max_buffers, num_buffers, buffers, max_message_length, message_length,
|
||||
message, timeout_us);
|
||||
}
|
||||
@@ -378,7 +380,7 @@ int htpdrv_init() {
|
||||
dlsym(handle.get(), dspqueue_export_pfn_t, dspqueue_export_pfn, dspqueue_export, false);
|
||||
dlsym(handle.get(), dspqueue_write_pfn_t, dspqueue_write_pfn, dspqueue_write, false);
|
||||
dlsym(handle.get(), dspqueue_read_pfn_t, dspqueue_read_pfn, dspqueue_read, false);
|
||||
dlsym(handle.get(), dspqueue_read_noblock_pfn_t, dspqueue_read_noblock_pfn, dspqueue_read_noblock, false);
|
||||
dlsym(handle.get(), dspqueue_read_noblock_pfn_t, dspqueue_read_noblock_pfn, dspqueue_read_noblock, true);
|
||||
dlsym(handle.get(), remote_handle64_open_pfn_t, remote_handle64_open_pfn, remote_handle64_open, false);
|
||||
dlsym(handle.get(), remote_handle64_invoke_pfn_t, remote_handle64_invoke_pfn, remote_handle64_invoke, false);
|
||||
dlsym(handle.get(), remote_handle_control_pfn_t, remote_handle_control_pfn, remote_handle_control, false);
|
||||
|
||||
@@ -117,6 +117,7 @@ HTPDRV_API domain * htpdrv_get_domain(int domain_id);
|
||||
HTPDRV_API int htpdrv_get_arch(int domain, int * arch);
|
||||
|
||||
HTPDRV_API int remote_system_request(system_req_payload * req);
|
||||
HTPDRV_API bool htpdrv_has_read_noblock(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -51,14 +51,15 @@ static inline bool htp_match_consumer(
|
||||
|
||||
static bool is_supported_mul_mat_nx_kernel(const ggml_tensor * src0, const struct htp_mm_kernel_params * kparams) {
|
||||
if (kparams->n_hmx) {
|
||||
return kparams->kernel_type == HTP_MM_KERNEL_HMX_2D;
|
||||
return htp_mm_kernel_is_hmx_2d(kparams->kernel_type);
|
||||
}
|
||||
return ggml_hexagon_tensor_is_tiled(src0) && (kparams->kernel_type == HTP_MM_KERNEL_HVX_QUANT_ROW);
|
||||
return ggml_hexagon_tensor_is_tiled(src0) &&
|
||||
(kparams->kernel_type == HTP_MM_KERNEL_HVX_QUANT_ROW || kparams->kernel_type == HTP_MM_KERNEL_HVX_QUANT_BLOCK);
|
||||
}
|
||||
|
||||
static bool is_supported_mul_mat_id_nx_kernel(const ggml_tensor * src0, const struct htp_mm_kernel_params * kparams) {
|
||||
if (kparams->n_hmx) {
|
||||
return kparams->kernel_type == HTP_MM_KERNEL_HMX_2D;
|
||||
return htp_mm_kernel_is_hmx_2d(kparams->kernel_type);
|
||||
}
|
||||
return ggml_hexagon_tensor_is_tiled(src0) &&
|
||||
(kparams->kernel_type == HTP_MM_KERNEL_HVX_QUANT_ROW || kparams->kernel_type == HTP_MM_KERNEL_HVX_QUANT_BLOCK);
|
||||
@@ -76,21 +77,23 @@ static bool fuse_allreduce_add(const ggml_hexagon_session * sess, htp_opnode & p
|
||||
const ggml_tensor * ar_local = prev.inputs[rank];
|
||||
if (!ggml_hexagon_tensor_is_fuseable(ar_local)) return false;
|
||||
|
||||
if (cur.inputs.size() < 2) return false;
|
||||
const ggml_tensor * cur_src0 = cur.inputs[0];
|
||||
const ggml_tensor * cur_src1 = cur.inputs[1];
|
||||
const ggml_tensor * add_dst = cur.dst();
|
||||
const ggml_tensor * res_tensor = nullptr;
|
||||
if (htp_same_tensor_data(cur.src0(), ar_local)) {
|
||||
res_tensor = cur.src1();
|
||||
} else if (htp_same_tensor_data(cur.src1(), ar_local)) {
|
||||
res_tensor = cur.src0();
|
||||
if (htp_same_tensor_data(cur_src0, ar_local)) {
|
||||
res_tensor = cur_src1;
|
||||
} else if (htp_same_tensor_data(cur_src1, ar_local)) {
|
||||
res_tensor = cur_src0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ar_local->type != res_tensor->type) return false;
|
||||
if (ar_local->type != res_tensor->type || ar_local->type != add_dst->type) return false;
|
||||
|
||||
const bool is_same_shape = ggml_are_same_shape(ar_local, res_tensor);
|
||||
const bool is_row_bcast = !is_same_shape && (ar_local->ne[0] == res_tensor->ne[0] &&
|
||||
res_tensor->ne[1] == 1 && res_tensor->ne[2] == 1 && res_tensor->ne[3] == 1);
|
||||
const bool is_row_bcast = !is_same_shape && (ar_local->ne[0] == res_tensor->ne[0] && ggml_is_vector(res_tensor));
|
||||
if (!is_same_shape && !is_row_bcast) return false;
|
||||
|
||||
if (is_same_shape && !ggml_are_same_stride(ar_local, res_tensor)) {
|
||||
@@ -135,12 +138,15 @@ static bool fuse_rms_norm_mul(const ggml_hexagon_session * sess, htp_opnode & pr
|
||||
|
||||
const ggml_tensor * src0 = prev.src0();
|
||||
const ggml_tensor * dst = cur.dst();
|
||||
if (src0->type != GGML_TYPE_F32 || src0->ne[0] != weight->ne[0] || !htp_same_shape_and_contiguity(prev.dst(), dst)) return false;
|
||||
if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
|
||||
(weight->type != GGML_TYPE_F32 && weight->type != GGML_TYPE_F16) ||
|
||||
src0->ne[0] != weight->ne[0] || !htp_same_shape_and_contiguity(prev.dst(), dst)) return false;
|
||||
if (htp_inputs_overlap_unsafe(prev, dst, weight)) return false;
|
||||
|
||||
const bool is_row_bcast = (weight->ne[1] == 1 && weight->ne[2] == 1 && weight->ne[3] == 1);
|
||||
const bool is_row_bcast = ggml_is_vector(weight);
|
||||
const bool is_dim1_bcast = (weight->ne[1] == src0->ne[1] && ggml_is_matrix(weight));
|
||||
const bool is_same_shape = ggml_are_same_shape(src0, weight);
|
||||
if (!is_row_bcast && !is_same_shape) return false;
|
||||
if (!is_row_bcast && !is_dim1_bcast && !is_same_shape) return false;
|
||||
|
||||
struct htp_unary_kernel_params kparams;
|
||||
if (!htp_kparams_solve_rms_norm_mul(sess, src0, weight, dst, &kparams)) {
|
||||
@@ -167,11 +173,13 @@ static bool fuse_rms_norm_mul_add(const ggml_hexagon_session * sess, htp_opnode
|
||||
const ggml_tensor * src0 = prev.src0();
|
||||
const ggml_tensor * weight = prev.inputs[1];
|
||||
const ggml_tensor * dst = cur.dst();
|
||||
if (!ggml_are_same_layout(src0, r)) return false;
|
||||
if ((r->type != GGML_TYPE_F32 && r->type != GGML_TYPE_F16) ||
|
||||
(weight->type != GGML_TYPE_F32 && weight->type != GGML_TYPE_F16)) return false;
|
||||
if (!ggml_are_same_shape(src0, r) || (ggml_is_contiguous(src0) != ggml_is_contiguous(r))) return false;
|
||||
if (htp_inputs_overlap_unsafe(prev, dst, r)) return false;
|
||||
|
||||
struct htp_unary_kernel_params kparams;
|
||||
if (!htp_kparams_solve_rms_norm_mul_add(sess, src0, weight, dst, &kparams)) return false;
|
||||
if (!htp_kparams_solve_rms_norm_mul_add(sess, src0, weight, r, dst, &kparams)) return false;
|
||||
|
||||
prev.opcode = HTP_OP_RMS_NORM_MUL_ADD;
|
||||
prev.inputs.push_back(r);
|
||||
@@ -188,7 +196,7 @@ static bool fuse_rms_norm_scale(const ggml_hexagon_session * sess, htp_opnode &
|
||||
|
||||
const ggml_tensor * src0 = prev.src0();
|
||||
const ggml_tensor * dst = cur.dst();
|
||||
if (src0->type != GGML_TYPE_F32 || !htp_same_shape_and_contiguity(prev.dst(), dst)) return false;
|
||||
if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) || !htp_same_shape_and_contiguity(prev.dst(), dst)) return false;
|
||||
if (htp_inputs_overlap_unsafe(prev, dst)) return false;
|
||||
|
||||
float scale = 0.0f;
|
||||
@@ -214,13 +222,27 @@ static bool fuse_mul_mat_add(const ggml_hexagon_session * sess, htp_opnode & pre
|
||||
|
||||
const ggml_tensor * src2 = nullptr;
|
||||
if (!htp_match_consumer(prev, cur, /*commutative=*/ true, &src2)) return false;
|
||||
if (src2->type != GGML_TYPE_F32) return false;
|
||||
if ((src2->type != GGML_TYPE_F32 && src2->type != GGML_TYPE_F16) ||
|
||||
!ggml_is_contiguous_to_2(src2)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ggml_tensor * src0 = prev.node->src[0];
|
||||
const ggml_tensor * src1 = prev.node->src[1];
|
||||
const ggml_tensor * dst = cur.node;
|
||||
if (!ggml_are_same_shape(prev.node, dst)) return false;
|
||||
|
||||
// Require matching ne[0], valid broadcast dims (1 or equal), and no unsafe overlap.
|
||||
if (src2->ne[0] != dst->ne[0] ||
|
||||
(src2->ne[1] != 1 && src2->ne[1] != dst->ne[1]) ||
|
||||
(src2->ne[2] != 1 && src2->ne[2] != dst->ne[2]) ||
|
||||
(src2->ne[3] != 1 && src2->ne[3] != dst->ne[3])) {
|
||||
return false;
|
||||
}
|
||||
if (htp_tensors_overlap(dst, src0) || htp_tensors_overlap(dst, src1) || htp_inplace_overlap_unsafe(dst, src2)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool prev_collapsed = htp_matmul_can_collapse(src0, src1, prev.node);
|
||||
const bool cur_collapsed = htp_matmul_add_can_collapse(src0, src1, src2, dst);
|
||||
if (prev_collapsed != cur_collapsed) {
|
||||
@@ -233,12 +255,12 @@ static bool fuse_mul_mat_add(const ggml_hexagon_session * sess, htp_opnode & pre
|
||||
return false;
|
||||
}
|
||||
|
||||
const int src1_nrows = src1->ne[1] * src1->ne[2] * src1->ne[3];
|
||||
if (kparams.n_hmx == 0 && src1_nrows != 1) return false;
|
||||
const int src1_nrows = (int) ggml_nrows(src1);
|
||||
const bool is_batched = (!ggml_is_matrix(src0) || !ggml_is_matrix(src1));
|
||||
if (kparams.n_hmx == 0 && (src1_nrows != 1 || is_batched)) return false;
|
||||
|
||||
if (kparams.n_hmx > 0 && orig_kparams->n_hmx > 0) {
|
||||
if (kparams.m_chunk < orig_kparams->m_chunk ||
|
||||
kparams.n_chunk < orig_kparams->n_chunk ||
|
||||
kparams.n_act_hvx < orig_kparams->n_act_hvx) {
|
||||
HEX_VERBOSE("ggml-hex: %s skip MUL_MAT_ADD fusion: HMX efficiency reduced (m %d->%d, n %d->%d, hvx %d->%d)\n",
|
||||
htp_session_name(sess), orig_kparams->m_chunk, kparams.m_chunk,
|
||||
@@ -251,7 +273,6 @@ static bool fuse_mul_mat_add(const ggml_hexagon_session * sess, htp_opnode & pre
|
||||
prev.opcode = HTP_OP_MUL_MAT_ADD;
|
||||
prev.inputs = { src0, src1, src2 };
|
||||
prev.outputs = { dst };
|
||||
htp_opnode_collapse(sess, prev);
|
||||
prev.fused.push_back(cur.node);
|
||||
prev.set_kparams(kparams);
|
||||
return true;
|
||||
@@ -273,21 +294,36 @@ static bool fuse_mul_mat_nx(const ggml_hexagon_session * sess, htp_opnode & prev
|
||||
const auto * cur_kparams = cur.kparams<htp_mm_kernel_params>();
|
||||
|
||||
if (x_in != x_orig || w_in->type != w0->type || w_in->ne[0] != w0->ne[0] ||
|
||||
w0->ne[2] != 1 || w0->ne[3] != 1 || w_in->ne[2] != 1 || w_in->ne[3] != 1 ||
|
||||
!ggml_is_matrix(w0) || !ggml_is_matrix(w_in) ||
|
||||
ggml_hexagon_tensor_is_tiled(w_in) != ggml_hexagon_tensor_is_tiled(w0) ||
|
||||
orig_kparams->n_hmx != cur_kparams->n_hmx) {
|
||||
orig_kparams->n_hmx != cur_kparams->n_hmx ||
|
||||
orig_kparams->kernel_type != cur_kparams->kernel_type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool collapse = htp_matmul_can_collapse(w0, x_orig, prev.node) && htp_matmul_can_collapse(w_in, x_in, d_in);
|
||||
if ((x_orig->ne[2] > 1 || x_orig->ne[3] > 1) && !collapse) return false;
|
||||
if (!ggml_is_matrix(x_orig) && !collapse) return false;
|
||||
|
||||
if (prev.opcode == HTP_OP_MUL_MAT_NX) {
|
||||
const ggml_tensor * prev_w = prev.inputs[0];
|
||||
ggml_type prev_dst_type = prev.outputs[0]->type;
|
||||
for (size_t i = 1; i < curr_n; ++i) {
|
||||
if (prev.inputs[i]->ne[1] > prev_w->ne[1]) prev_w = prev.inputs[i];
|
||||
if (ggml_type_size(prev.outputs[i]->type) > ggml_type_size(prev_dst_type)) {
|
||||
prev_dst_type = prev.outputs[i]->type;
|
||||
}
|
||||
}
|
||||
const ggml_tensor * w_for_solve = (w_in->ne[1] > prev_w->ne[1]) ? w_in : prev_w;
|
||||
const ggml_type max_dst_type = (ggml_type_size(d_in->type) > ggml_type_size(prev_dst_type)) ? d_in->type : prev_dst_type;
|
||||
|
||||
if (prev.opcode == HTP_OP_MUL_MAT_NX && w_for_solve == prev_w &&
|
||||
ggml_type_size(max_dst_type) == ggml_type_size(prev_dst_type)) {
|
||||
orig_kparams->n_weights = curr_n + 1;
|
||||
} else {
|
||||
struct htp_mm_kernel_params kparams;
|
||||
const ggml_tensor x_collapsed = collapse ? htp_tensor_collapse_rows(x_orig) : *x_orig;
|
||||
if (!htp_kparams_solve_mmnx(sess, w0, &x_collapsed, curr_n + 1, &kparams)) return false;
|
||||
ggml_tensor d_collapsed = collapse ? htp_tensor_collapse_rows(prev.outputs[0]) : *prev.outputs[0];
|
||||
d_collapsed.type = max_dst_type;
|
||||
if (!htp_kparams_solve_mmnx(sess, w_for_solve, &x_collapsed, &d_collapsed, curr_n + 1, &kparams)) return false;
|
||||
if (!is_supported_mul_mat_nx_kernel(w0, &kparams)) return false;
|
||||
|
||||
prev.set_kparams(kparams);
|
||||
@@ -295,11 +331,101 @@ static bool fuse_mul_mat_nx(const ggml_hexagon_session * sess, htp_opnode & prev
|
||||
}
|
||||
|
||||
prev.inputs.insert(prev.inputs.begin() + curr_n, w_in);
|
||||
prev.outputs.push_back(collapse ? prev.add_dummy(htp_tensor_collapse_rows(d_in)) : d_in);
|
||||
prev.outputs.push_back(d_in);
|
||||
prev.fused.push_back(cur.node);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool htp_is_fuseable_mmnx_glu_op(uint32_t op) {
|
||||
return op == HTP_OP_GLU_SWIGLU || op == HTP_OP_GLU_GEGLU ||
|
||||
op == HTP_OP_GLU_GEGLU_QUICK || op == HTP_OP_GLU_REGLU;
|
||||
}
|
||||
|
||||
static bool fuse_mul_mat_nx_glu(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
|
||||
if (prev.opcode != HTP_OP_MUL_MAT_NX || prev.outputs.size() != 2 || prev.inputs.size() != 3) {
|
||||
return false;
|
||||
}
|
||||
if (!htp_is_fuseable_mmnx_glu_op(cur.opcode) || cur.inputs.size() != 2 || cur.outputs.size() != 1 || !cur.src1()) {
|
||||
return false;
|
||||
}
|
||||
if (prev.fused.size() + 1 + cur.fused.size() > HTP_OP_MAX_FUSED) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto * orig_kparams = prev.kparams<htp_mm_kernel_params>();
|
||||
if (!orig_kparams->n_hmx || !htp_mm_kernel_is_hmx_2d(orig_kparams->kernel_type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ggml_tensor * out0 = prev.outputs[0];
|
||||
const ggml_tensor * out1 = prev.outputs[1];
|
||||
if (!ggml_hexagon_tensor_is_fuseable(out0) || !ggml_hexagon_tensor_is_fuseable(out1) ||
|
||||
(out0->flags & GGML_TENSOR_FLAG_OUTPUT) || (out1->flags & GGML_TENSOR_FLAG_OUTPUT)) {
|
||||
return false;
|
||||
}
|
||||
if (ggml_prec_get_acc(out0) == GGML_PREC_F32 || ggml_prec_get_acc(out1) == GGML_PREC_F32) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ggml_tensor * w_gate = nullptr;
|
||||
const ggml_tensor * w_up = nullptr;
|
||||
const ggml_tensor * out_gate = nullptr;
|
||||
const ggml_tensor * out_up = nullptr;
|
||||
if (htp_same_tensor_data(cur.src0(), out0) && htp_same_tensor_data(cur.src1(), out1)) {
|
||||
w_gate = prev.inputs[0];
|
||||
w_up = prev.inputs[1];
|
||||
out_gate = out0;
|
||||
out_up = out1;
|
||||
} else if (htp_same_tensor_data(cur.src0(), out1) && htp_same_tensor_data(cur.src1(), out0)) {
|
||||
w_gate = prev.inputs[1];
|
||||
w_up = prev.inputs[0];
|
||||
out_gate = out1;
|
||||
out_up = out0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ggml_tensor * act = prev.inputs[2];
|
||||
const ggml_tensor * dst = cur.dst();
|
||||
if (w_gate->ne[1] != w_up->ne[1] || w_gate->ne[0] != w_up->ne[0] ||
|
||||
!ggml_is_contiguous(out_gate) || !ggml_is_contiguous(out_up) || !ggml_is_contiguous(dst) ||
|
||||
out_gate->type != cur.src0()->type || out_up->type != cur.src1()->type ||
|
||||
!ggml_are_same_shape(out_gate, cur.src0()) || !ggml_are_same_stride(out_gate, cur.src0()) ||
|
||||
!ggml_are_same_shape(out_up, cur.src1()) || !ggml_are_same_stride(out_up, cur.src1()) ||
|
||||
!ggml_are_same_shape(out_gate, dst)) {
|
||||
return false;
|
||||
}
|
||||
if (dst->type != GGML_TYPE_F16 && dst->type != GGML_TYPE_F32) {
|
||||
return false;
|
||||
}
|
||||
if (htp_tensors_overlap(dst, w_gate) || htp_tensors_overlap(dst, w_up) || htp_tensors_overlap(dst, act)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool collapse = htp_matmul_can_collapse(w_gate, act, dst) && htp_matmul_can_collapse(w_up, act, dst);
|
||||
if (!ggml_is_matrix(act) && !collapse) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ggml_tensor x_collapsed = collapse ? htp_tensor_collapse_rows(act) : *act;
|
||||
const ggml_tensor d_collapsed = collapse ? htp_tensor_collapse_rows(dst) : *dst;
|
||||
|
||||
struct htp_mm_kernel_params kparams;
|
||||
if (!htp_kparams_solve_mmnx_glu(sess, w_gate, w_up, &x_collapsed, &d_collapsed, (uint8_t) cur.opcode, &kparams)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
prev.opcode = HTP_OP_MUL_MAT_NX_GLU;
|
||||
prev.inputs = { w_gate, w_up, act };
|
||||
prev.outputs = { dst };
|
||||
prev.fused.push_back(cur.node);
|
||||
for (const ggml_tensor * f : cur.fused) {
|
||||
prev.fused.push_back(f);
|
||||
}
|
||||
prev.set_kparams(kparams);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool fuse_mul_mat_id_nx(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
|
||||
if (prev.opcode != HTP_OP_MUL_MAT_ID && prev.opcode != HTP_OP_MUL_MAT_ID_NX) return false;
|
||||
|
||||
@@ -380,7 +506,6 @@ static bool fuse_gdn_cpy(const ggml_hexagon_session * sess, htp_opnode & prev, c
|
||||
}
|
||||
|
||||
static bool fuse_ssm_conv_silu(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
|
||||
GGML_UNUSED(sess);
|
||||
if (prev.opcode != HTP_OP_SSM_CONV) return false;
|
||||
|
||||
auto * kparams = prev.kparams<htp_ssm_conv_kernel_params>();
|
||||
@@ -389,12 +514,17 @@ static bool fuse_ssm_conv_silu(const ggml_hexagon_session * sess, htp_opnode & p
|
||||
|
||||
const ggml_tensor * conv_dst = prev.dst();
|
||||
const ggml_tensor * dst = cur.dst();
|
||||
if (!ggml_are_same_layout(conv_dst, dst)) return false;
|
||||
if (!ggml_are_same_shape(conv_dst, cur.src0()) || !ggml_are_same_stride(conv_dst, cur.src0())) return false;
|
||||
if (!ggml_are_same_shape(conv_dst, dst)) return false;
|
||||
if (htp_tensors_overlap(dst, prev.src0()) || htp_tensors_overlap(dst, prev.src1())) return false;
|
||||
|
||||
kparams->silu = 1;
|
||||
prev.outputs[0] = dst;
|
||||
prev.fused.push_back(cur.node);
|
||||
htp_opnode cand = prev;
|
||||
cand.outputs[0] = dst;
|
||||
cand.fused.push_back(cur.node);
|
||||
cand.kparams<htp_ssm_conv_kernel_params>()->silu = 1;
|
||||
if (!htp_kparams_solve(sess, cand)) return false;
|
||||
|
||||
prev = std::move(cand);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -421,6 +551,97 @@ static bool fuse_repeat_get_rows(const ggml_hexagon_session * sess, htp_opnode &
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool htp_is_scale_bias_unary(uint32_t opcode) {
|
||||
switch (opcode) {
|
||||
case HTP_OP_UNARY_EXP:
|
||||
case HTP_OP_UNARY_SIGMOID:
|
||||
case HTP_OP_UNARY_SILU:
|
||||
case HTP_OP_UNARY_GELU:
|
||||
case HTP_OP_UNARY_GELU_QUICK:
|
||||
case HTP_OP_UNARY_GELU_ERF:
|
||||
case HTP_OP_UNARY_SOFTPLUS:
|
||||
case HTP_OP_UNARY_TANH:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool fuse_scale_unary(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
|
||||
if (prev.opcode != HTP_OP_SCALE || !prev.fused.empty() || !htp_is_scale_bias_unary(cur.opcode)) return false;
|
||||
if (cur.kparams<htp_unary_kernel_params>()->has_scale_bias) return false;
|
||||
if (!htp_match_consumer(prev, cur, /*commutative=*/ false)) return false;
|
||||
|
||||
const ggml_tensor * src0 = prev.src0();
|
||||
const ggml_tensor * dst = cur.dst();
|
||||
if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
|
||||
dst->type != src0->type || !htp_same_shape_and_contiguity(prev.dst(), dst)) {
|
||||
return false;
|
||||
}
|
||||
if (htp_inputs_overlap_unsafe(prev, dst)) return false;
|
||||
|
||||
float in_scale = 0.0f;
|
||||
float in_bias = 0.0f;
|
||||
memcpy(&in_scale, (const float *) prev.params + 0, sizeof(float));
|
||||
memcpy(&in_bias, (const float *) prev.params + 1, sizeof(float));
|
||||
|
||||
htp_opnode cand = prev;
|
||||
cand.opcode = cur.opcode;
|
||||
cand.outputs = { dst };
|
||||
memcpy(cand.params, cur.params, sizeof(cand.params));
|
||||
|
||||
auto * kp = cand.kparams<htp_unary_kernel_params>();
|
||||
kp->has_scale_bias = 1;
|
||||
kp->in_scale = in_scale;
|
||||
kp->in_bias = in_bias;
|
||||
kp->out_scale = 1.0f;
|
||||
kp->out_bias = 0.0f;
|
||||
if (!htp_kparams_solve(sess, cand)) return false;
|
||||
|
||||
prev = std::move(cand);
|
||||
prev.fused.push_back(cur.node);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool fuse_unary_scale(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
|
||||
if (!htp_is_scale_bias_unary(prev.opcode) || prev.fused.size() >= HTP_OP_MAX_FUSED) return false;
|
||||
if (!htp_match_consumer(prev, cur, /*commutative=*/ false)) return false;
|
||||
|
||||
const ggml_tensor * src0 = prev.src0();
|
||||
const ggml_tensor * dst = cur.dst();
|
||||
if ((src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16) ||
|
||||
dst->type != src0->type || !htp_same_shape_and_contiguity(prev.dst(), dst)) {
|
||||
return false;
|
||||
}
|
||||
if (htp_inputs_overlap_unsafe(prev, dst)) return false;
|
||||
|
||||
float s_out = 0.0f;
|
||||
float b_out = 0.0f;
|
||||
memcpy(&s_out, (const float *) cur.params + 0, sizeof(float));
|
||||
memcpy(&b_out, (const float *) cur.params + 1, sizeof(float));
|
||||
|
||||
const auto * prev_kp = prev.kparams<htp_unary_kernel_params>();
|
||||
const float in_scale = prev_kp->has_scale_bias ? prev_kp->in_scale : 1.0f;
|
||||
const float in_bias = prev_kp->has_scale_bias ? prev_kp->in_bias : 0.0f;
|
||||
const float out_scale = (prev_kp->has_scale_bias ? prev_kp->out_scale : 1.0f) * s_out;
|
||||
const float out_bias = (prev_kp->has_scale_bias ? prev_kp->out_bias : 0.0f) * s_out + b_out;
|
||||
|
||||
htp_opnode cand = prev;
|
||||
cand.outputs = { dst };
|
||||
|
||||
auto * kp = cand.kparams<htp_unary_kernel_params>();
|
||||
kp->has_scale_bias = 1;
|
||||
kp->in_scale = in_scale;
|
||||
kp->in_bias = in_bias;
|
||||
kp->out_scale = out_scale;
|
||||
kp->out_bias = out_bias;
|
||||
if (!htp_kparams_solve(sess, cand)) return false;
|
||||
|
||||
prev = std::move(cand);
|
||||
prev.fused.push_back(cur.node);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool fuse_unary_mul_glu(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
|
||||
htp_op_code glu_op = HTP_OP_INVALID;
|
||||
switch (prev.opcode) {
|
||||
@@ -432,6 +653,7 @@ static bool fuse_unary_mul_glu(const ggml_hexagon_session * sess, htp_opnode & p
|
||||
case HTP_OP_UNARY_SIGMOID: glu_op = HTP_OP_GLU_SIGMOID; break;
|
||||
default: return false;
|
||||
}
|
||||
if (prev.kparams<htp_unary_kernel_params>()->has_scale_bias) return false;
|
||||
|
||||
const ggml_tensor * up = nullptr;
|
||||
if (!htp_match_consumer(prev, cur, /*commutative=*/ true, &up)) return false;
|
||||
@@ -440,11 +662,13 @@ static bool fuse_unary_mul_glu(const ggml_hexagon_session * sess, htp_opnode & p
|
||||
const ggml_tensor * dst = cur.dst();
|
||||
if (htp_inputs_overlap_unsafe(prev, dst, up)) return false;
|
||||
|
||||
prev.opcode = glu_op;
|
||||
prev.inputs = { gate, up };
|
||||
prev.outputs = { dst };
|
||||
if (!htp_kparams_solve(sess, prev)) return false;
|
||||
htp_opnode cand = prev;
|
||||
cand.opcode = glu_op;
|
||||
cand.inputs = { gate, up };
|
||||
cand.outputs = { dst };
|
||||
if (!htp_kparams_solve(sess, cand)) return false;
|
||||
|
||||
prev = std::move(cand);
|
||||
prev.fused.push_back(cur.node);
|
||||
memset(prev.params, 0, sizeof(prev.params));
|
||||
return true;
|
||||
@@ -463,6 +687,7 @@ static bool fuse_mul_mul_scalar(const ggml_hexagon_session * sess, htp_opnode &
|
||||
|
||||
const ggml_tensor * src1 = prev.src1();
|
||||
const ggml_tensor * dst = cur.dst();
|
||||
if (prev.src0()->type != src1->type || prev.dst()->type != src1->type || src2->type != src1->type || dst->type != src1->type) return false;
|
||||
if (!ggml_are_same_shape(src1, src2) || !htp_same_shape_and_contiguity(prev.dst(), dst)) return false;
|
||||
if (htp_inputs_overlap_unsafe(prev, dst, src2)) return false;
|
||||
|
||||
@@ -506,6 +731,10 @@ static bool fuse_binary_nx(const ggml_hexagon_session * sess, htp_opnode & prev,
|
||||
|
||||
const ggml_tensor * src0 = prev.inputs[0];
|
||||
const ggml_tensor * dst = cur.dst();
|
||||
if (prev.dst()->type != src0->type || new_src->type != src0->type || dst->type != src0->type) return false;
|
||||
for (const auto * in : prev.inputs) {
|
||||
if (in->type != src0->type) return false;
|
||||
}
|
||||
if (!ggml_are_same_shape(src0, prev.inputs[1]) ||
|
||||
!ggml_are_same_shape(src0, new_src) ||
|
||||
!htp_same_shape_and_contiguity(prev.dst(), dst)) {
|
||||
@@ -547,7 +776,7 @@ static bool fuse_binary_nx(const ggml_hexagon_session * sess, htp_opnode & prev,
|
||||
const uint32_t total_elems = (uint32_t) ggml_nelements(src0);
|
||||
const uint32_t elem_size = (uint32_t) ggml_type_size(src0->type);
|
||||
const uint32_t max_hvx = htp_session_n_hvx(sess);
|
||||
if (!htp_binary_solve_chunked(&L, &kparams, total_elems, elem_size, max_hvx, n_srcs, false, vtcm_size)) {
|
||||
if (!htp_binary_solve_chunked(&L, &kparams, total_elems, elem_size, elem_size, elem_size, max_hvx, n_srcs, false, vtcm_size)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -569,7 +798,8 @@ bool htp_fusion_can_stack(const struct ggml_tensor * a) {
|
||||
if (!htp_fusion_enabled(HTP_FUSE_MUL_MAT_NX)) return false;
|
||||
if (htp_is_hadamard(a)) return false;
|
||||
if (a->src[0]->ne[2] != 1 || a->src[0]->ne[3] != 1) return false;
|
||||
return ggml_is_quantized(a->src[0]->type);
|
||||
return ggml_is_quantized(a->src[0]->type) ||
|
||||
(a->src[0]->type == GGML_TYPE_F16 && a->src[0]->ne[0] % 32 == 0);
|
||||
case GGML_OP_MUL_MAT_ID:
|
||||
if (!htp_fusion_enabled(HTP_FUSE_MUL_MAT_ID_NX)) return false;
|
||||
return a->src[2] != nullptr && ggml_is_quantized(a->src[0]->type);
|
||||
@@ -639,6 +869,7 @@ void htp_fusion_tag_graph(struct ggml_cgraph * graph) {
|
||||
|
||||
bool htp_fusion_try_fuse(const struct ggml_hexagon_session * sess, struct htp_opnode & prev, const struct htp_opnode & cur) {
|
||||
if (!opt_opfusion) return false;
|
||||
if (!cur.fused.empty()) return false;
|
||||
|
||||
switch (cur.opcode) {
|
||||
case HTP_OP_ADD:
|
||||
@@ -655,9 +886,19 @@ bool htp_fusion_try_fuse(const struct ggml_hexagon_session * sess, struct htp_op
|
||||
case HTP_OP_DIV:
|
||||
return htp_fusion_enabled(HTP_FUSE_BINARY_NX) && fuse_binary_nx(sess, prev, cur);
|
||||
case HTP_OP_SCALE:
|
||||
return htp_fusion_enabled(HTP_FUSE_RMS_NORM_SCALE) && fuse_rms_norm_scale(sess, prev, cur);
|
||||
return (htp_fusion_enabled(HTP_FUSE_RMS_NORM_SCALE) && fuse_rms_norm_scale(sess, prev, cur)) ||
|
||||
(htp_fusion_enabled(HTP_FUSE_UNARY_SCALE) && fuse_unary_scale(sess, prev, cur));
|
||||
case HTP_OP_UNARY_SILU:
|
||||
return htp_fusion_enabled(HTP_FUSE_SSM_CONV_SILU) && fuse_ssm_conv_silu(sess, prev, cur);
|
||||
return (htp_fusion_enabled(HTP_FUSE_SSM_CONV_SILU) && fuse_ssm_conv_silu(sess, prev, cur)) ||
|
||||
(htp_fusion_enabled(HTP_FUSE_UNARY_SCALE) && fuse_scale_unary(sess, prev, cur));
|
||||
case HTP_OP_UNARY_EXP:
|
||||
case HTP_OP_UNARY_SIGMOID:
|
||||
case HTP_OP_UNARY_GELU:
|
||||
case HTP_OP_UNARY_GELU_QUICK:
|
||||
case HTP_OP_UNARY_GELU_ERF:
|
||||
case HTP_OP_UNARY_SOFTPLUS:
|
||||
case HTP_OP_UNARY_TANH:
|
||||
return htp_fusion_enabled(HTP_FUSE_UNARY_SCALE) && fuse_scale_unary(sess, prev, cur);
|
||||
case HTP_OP_GET_ROWS:
|
||||
return htp_fusion_enabled(HTP_FUSE_REPEAT_GET_ROWS) && fuse_repeat_get_rows(sess, prev, cur);
|
||||
case HTP_OP_CPY:
|
||||
@@ -666,7 +907,396 @@ bool htp_fusion_try_fuse(const struct ggml_hexagon_session * sess, struct htp_op
|
||||
return htp_fusion_enabled(HTP_FUSE_MUL_MAT_NX) && fuse_mul_mat_nx(sess, prev, cur);
|
||||
case HTP_OP_MUL_MAT_ID:
|
||||
return htp_fusion_enabled(HTP_FUSE_MUL_MAT_ID_NX) && fuse_mul_mat_id_nx(sess, prev, cur);
|
||||
case HTP_OP_GLU_SWIGLU:
|
||||
case HTP_OP_GLU_GEGLU:
|
||||
case HTP_OP_GLU_GEGLU_QUICK:
|
||||
case HTP_OP_GLU_REGLU:
|
||||
return htp_fusion_enabled(HTP_FUSE_MUL_MAT_NX_GLU) && fuse_mul_mat_nx_glu(sess, prev, cur);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void htp_fuse_gdn_state(const struct ggml_hexagon_session * sess, std::vector<struct htp_opnode> & nodes) {
|
||||
for (size_t c = 0; c < nodes.size(); ++c) {
|
||||
if (nodes[c].opcode != HTP_OP_GATED_DELTA_NET || nodes[c].inputs.size() != 6 ||
|
||||
nodes[c].fused.size() >= HTP_OP_MAX_FUSED) {
|
||||
continue;
|
||||
}
|
||||
const ggml_tensor * v = nodes[c].inputs[2];
|
||||
const ggml_tensor * s = nodes[c].inputs[5];
|
||||
|
||||
const ggml_tensor * base_s = s;
|
||||
while (base_s->view_src &&
|
||||
(base_s->op == GGML_OP_RESHAPE || (base_s->op == GGML_OP_VIEW && base_s->view_offs == 0)) &&
|
||||
ggml_is_contiguous(base_s)) {
|
||||
base_s = base_s->view_src;
|
||||
}
|
||||
if (base_s->flags & GGML_TENSOR_FLAG_OUTPUT) continue;
|
||||
|
||||
size_t p = c;
|
||||
for (size_t i = 0; i < c; ++i) {
|
||||
if (nodes[i].opcode == HTP_OP_GET_ROWS && nodes[i].fused.empty() &&
|
||||
nodes[i].outputs.size() == 1 && nodes[i].dst() == base_s) {
|
||||
p = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (p == c) continue;
|
||||
|
||||
const ggml_tensor * gr_src0 = nodes[p].src0();
|
||||
const ggml_tensor * gr_src1 = nodes[p].src1();
|
||||
const int64_t n_seqs = v->ne[3];
|
||||
if (htp_tensors_overlap(nodes[c].outputs[0], gr_src0) || htp_tensors_overlap(nodes[c].outputs[0], gr_src1)) {
|
||||
continue;
|
||||
}
|
||||
if (nodes[c].outputs.size() > 1 && nodes[c].outputs[1]) {
|
||||
const ggml_tensor * dst_cache = nodes[c].outputs[1];
|
||||
if (htp_tensors_overlap(dst_cache, gr_src1)) continue;
|
||||
if (htp_tensors_overlap(gr_src0, dst_cache)) {
|
||||
const uintptr_t src0_ptr = (uintptr_t) htp_tensor_data_ptr(gr_src0);
|
||||
const uintptr_t dst_ptr = (uintptr_t) htp_tensor_data_ptr(dst_cache);
|
||||
const bool slot_aligned = (n_seqs == 1) &&
|
||||
(dst_cache->type == gr_src0->type) &&
|
||||
(dst_ptr >= src0_ptr) &&
|
||||
(gr_src0->nb[1] > 0) &&
|
||||
((dst_ptr - src0_ptr) % gr_src0->nb[1] == 0) &&
|
||||
(dst_cache->ne[2] == 1 || gr_src0->nb[1] == dst_cache->nb[2]);
|
||||
if (!slot_aligned) continue;
|
||||
}
|
||||
}
|
||||
|
||||
bool safe = true;
|
||||
for (size_t k = 0; k < nodes.size() && safe; ++k) {
|
||||
if (k == p) continue;
|
||||
if (k != c) {
|
||||
for (const ggml_tensor * in : nodes[k].inputs) {
|
||||
for (const ggml_tensor * t = in; t != nullptr; t = t->view_src) {
|
||||
if (t == base_s) { safe = false; break; }
|
||||
}
|
||||
if (!safe) break;
|
||||
}
|
||||
}
|
||||
if (k > p && k < c) {
|
||||
for (const ggml_tensor * out : nodes[k].outputs) {
|
||||
if (out && (htp_tensors_overlap(out, gr_src0) || htp_tensors_overlap(out, gr_src1) ||
|
||||
htp_tensors_overlap(out, base_s))) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!safe) continue;
|
||||
|
||||
htp_opnode cand = nodes[c];
|
||||
cand.inputs[5] = gr_src0;
|
||||
cand.inputs.push_back(gr_src1);
|
||||
cand.fused.insert(cand.fused.begin(), nodes[p].node);
|
||||
if (!htp_kparams_solve(sess, cand)) continue;
|
||||
|
||||
nodes[c] = std::move(cand);
|
||||
nodes.erase(nodes.begin() + (ptrdiff_t) p);
|
||||
--c;
|
||||
}
|
||||
}
|
||||
|
||||
static bool htp_unwrap_transpose_01(const ggml_tensor * t, const ggml_tensor ** out_src) {
|
||||
if (!t || !t->view_src || t->view_offs != 0) {
|
||||
return false;
|
||||
}
|
||||
if (t->op == GGML_OP_TRANSPOSE) {
|
||||
*out_src = t->view_src;
|
||||
return true;
|
||||
}
|
||||
if (t->op == GGML_OP_PERMUTE) {
|
||||
if (ggml_get_op_params_i32(t, 0) == 1 &&
|
||||
ggml_get_op_params_i32(t, 1) == 0 &&
|
||||
ggml_get_op_params_i32(t, 2) == 2 &&
|
||||
ggml_get_op_params_i32(t, 3) == 3) {
|
||||
*out_src = t->view_src;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool htp_tensor_is_view_of(const ggml_tensor * in, const ggml_tensor * target) {
|
||||
if (!in || !target) {
|
||||
return false;
|
||||
}
|
||||
for (const ggml_tensor * t = in; t != nullptr; t = t->view_src) {
|
||||
if (t == target) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool htp_tensor_reads_from(const ggml_tensor * in, const ggml_tensor * target) {
|
||||
if (!in || !target) {
|
||||
return false;
|
||||
}
|
||||
if (htp_tensor_is_view_of(in, target)) {
|
||||
return true;
|
||||
}
|
||||
return htp_tensors_overlap(in, target);
|
||||
}
|
||||
|
||||
static void htp_fuse_concat_ssm_conv(const struct ggml_hexagon_session * sess, std::vector<struct htp_opnode> & nodes) {
|
||||
for (size_t c = 0; c < nodes.size(); ++c) {
|
||||
if (nodes[c].opcode != HTP_OP_SSM_CONV || nodes[c].inputs.size() != 2 ||
|
||||
nodes[c].outputs.size() != 1 || nodes[c].fused.size() >= HTP_OP_MAX_FUSED) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const ggml_tensor * conv_x = nodes[c].src0();
|
||||
const ggml_tensor * conv_weight = nodes[c].src1();
|
||||
const ggml_tensor * conv_dst = nodes[c].dst();
|
||||
if (conv_x->flags & GGML_TENSOR_FLAG_OUTPUT) {
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t p = c;
|
||||
for (size_t i = 0; i < c; ++i) {
|
||||
if (nodes[i].opcode == HTP_OP_CONCAT && nodes[i].fused.empty() &&
|
||||
nodes[i].outputs.size() == 1 && nodes[i].dst() == conv_x &&
|
||||
ggml_get_op_params_i32(nodes[i].node, 0) == 0) {
|
||||
p = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (p == c) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const ggml_tensor * cat_state = nodes[p].src0();
|
||||
const ggml_tensor * cat_xT = nodes[p].src1();
|
||||
const ggml_tensor * cat_x = nullptr;
|
||||
if (!htp_unwrap_transpose_01(cat_xT, &cat_x)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const int64_t d_conv = conv_weight->ne[0];
|
||||
const int64_t d_inner = conv_weight->ne[1];
|
||||
const int64_t n_t = conv_dst->ne[1];
|
||||
const int64_t n_s = conv_dst->ne[2];
|
||||
const int64_t S = d_conv - 1;
|
||||
|
||||
size_t q = nodes.size();
|
||||
size_t n_other = 0;
|
||||
for (size_t k = 0; k < nodes.size(); ++k) {
|
||||
if (k == p || k == c) {
|
||||
continue;
|
||||
}
|
||||
bool uses_conv_x = false;
|
||||
for (const ggml_tensor * in : nodes[k].inputs) {
|
||||
if (htp_tensor_is_view_of(in, conv_x)) {
|
||||
uses_conv_x = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (uses_conv_x) {
|
||||
q = k;
|
||||
++n_other;
|
||||
}
|
||||
}
|
||||
if (n_other > 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const bool has_cpy = (n_other == 1);
|
||||
const ggml_tensor * cpy_dst = nullptr;
|
||||
if (has_cpy) {
|
||||
if (q <= p || nodes[q].opcode != HTP_OP_CPY || !nodes[q].fused.empty() ||
|
||||
nodes[q].outputs.size() != 1 || (nodes[q].node->flags & GGML_TENSOR_FLAG_OUTPUT)) {
|
||||
continue;
|
||||
}
|
||||
if (nodes[c].fused.size() + 2 > HTP_OP_MAX_FUSED) {
|
||||
continue;
|
||||
}
|
||||
const ggml_tensor * cpy_src = nodes[q].src0();
|
||||
cpy_dst = nodes[q].dst();
|
||||
const size_t conv_x_es = ggml_type_size(conv_x->type);
|
||||
if (cpy_src->op != GGML_OP_VIEW ||
|
||||
!htp_same_tensor_data(cpy_src->view_src, conv_x) ||
|
||||
cpy_src->ne[0] != S || cpy_src->ne[1] != d_inner || cpy_src->ne[2] != n_s || cpy_src->ne[3] != 1 ||
|
||||
cpy_src->nb[0] != conv_x_es || cpy_src->nb[1] != conv_x->nb[1] ||
|
||||
(n_s > 1 && cpy_src->nb[2] != conv_x->nb[2]) ||
|
||||
cpy_src->view_offs != (size_t) n_t * conv_x_es) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const bool inplace_x_dst = htp_same_tensor_data(conv_dst, cat_x) &&
|
||||
conv_dst->type == cat_x->type &&
|
||||
ggml_are_same_shape(conv_dst, cat_x) &&
|
||||
ggml_are_same_stride(conv_dst, cat_x);
|
||||
if (htp_tensors_overlap(conv_dst, cat_state) ||
|
||||
(htp_tensors_overlap(conv_dst, cat_x) && !inplace_x_dst) ||
|
||||
htp_tensors_overlap(conv_dst, conv_weight)) {
|
||||
continue;
|
||||
}
|
||||
if (has_cpy) {
|
||||
if (htp_tensors_overlap(conv_dst, cpy_dst) ||
|
||||
htp_tensors_overlap(cpy_dst, cat_x) ||
|
||||
htp_tensors_overlap(cpy_dst, conv_weight)) {
|
||||
continue;
|
||||
}
|
||||
if (htp_tensors_overlap(cpy_dst, cat_state)) {
|
||||
const bool inplace_state = (n_s == 1) &&
|
||||
htp_same_tensor_data(cpy_dst, cat_state) &&
|
||||
cpy_dst->type == cat_state->type;
|
||||
if (!inplace_state) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool safe = true;
|
||||
for (size_t k = p + 1; k < c && safe; ++k) {
|
||||
if (has_cpy && k == q) {
|
||||
continue;
|
||||
}
|
||||
for (const ggml_tensor * out : nodes[k].outputs) {
|
||||
if (!out) {
|
||||
continue;
|
||||
}
|
||||
if (htp_tensors_overlap(out, cat_state) ||
|
||||
htp_tensors_overlap(out, cat_x) ||
|
||||
htp_tensors_overlap(out, conv_x) ||
|
||||
htp_tensors_overlap(out, conv_weight) ||
|
||||
(has_cpy && htp_tensors_overlap(out, cpy_dst))) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (has_cpy && safe) {
|
||||
const size_t lo = (q < c) ? (q + 1) : (c + 1);
|
||||
const size_t hi = (q < c) ? c : q;
|
||||
for (size_t k = lo; k < hi && safe; ++k) {
|
||||
for (const ggml_tensor * in : nodes[k].inputs) {
|
||||
if (htp_tensor_reads_from(in, cpy_dst)) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (const ggml_tensor * out : nodes[k].outputs) {
|
||||
if (out && (htp_tensors_overlap(out, cpy_dst) || (q > c && htp_tensors_overlap(out, conv_x)))) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!safe) {
|
||||
continue;
|
||||
}
|
||||
|
||||
htp_opnode cand = nodes[c];
|
||||
cand.inputs = { cat_state, conv_weight, cat_x };
|
||||
if (has_cpy) {
|
||||
cand.outputs = { conv_dst, cpy_dst };
|
||||
cand.fused.insert(cand.fused.begin(), nodes[q].node);
|
||||
}
|
||||
cand.fused.insert(cand.fused.begin(), nodes[p].node);
|
||||
if (!htp_kparams_solve(sess, cand)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
nodes[c] = std::move(cand);
|
||||
if (has_cpy) {
|
||||
nodes.erase(nodes.begin() + (ptrdiff_t) q);
|
||||
if (q < c) {
|
||||
--c;
|
||||
}
|
||||
}
|
||||
nodes.erase(nodes.begin() + (ptrdiff_t) p);
|
||||
--c;
|
||||
}
|
||||
}
|
||||
|
||||
static void htp_fuse_mul_mat_nx_glu(const struct ggml_hexagon_session * sess, std::vector<struct htp_opnode> & nodes) {
|
||||
for (size_t c = 0; c < nodes.size(); ++c) {
|
||||
if (!htp_is_fuseable_mmnx_glu_op(nodes[c].opcode) || nodes[c].inputs.size() != 2 ||
|
||||
nodes[c].outputs.size() != 1 || !nodes[c].src1()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const ggml_tensor * s0 = nodes[c].src0();
|
||||
const ggml_tensor * s1 = nodes[c].src1();
|
||||
|
||||
size_t p = c;
|
||||
for (size_t i = 0; i < c; ++i) {
|
||||
if (nodes[i].opcode != HTP_OP_MUL_MAT_NX || nodes[i].outputs.size() != 2 || nodes[i].inputs.size() != 3) {
|
||||
continue;
|
||||
}
|
||||
const ggml_tensor * o0 = nodes[i].outputs[0];
|
||||
const ggml_tensor * o1 = nodes[i].outputs[1];
|
||||
if ((htp_same_tensor_data(s0, o0) && htp_same_tensor_data(s1, o1)) ||
|
||||
(htp_same_tensor_data(s0, o1) && htp_same_tensor_data(s1, o0))) {
|
||||
p = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (p == c) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const ggml_tensor * w0 = nodes[p].inputs[0];
|
||||
const ggml_tensor * w1 = nodes[p].inputs[1];
|
||||
const ggml_tensor * act = nodes[p].inputs[2];
|
||||
const ggml_tensor * o0 = nodes[p].outputs[0];
|
||||
const ggml_tensor * o1 = nodes[p].outputs[1];
|
||||
|
||||
bool safe = true;
|
||||
for (size_t k = 0; k < nodes.size() && safe; ++k) {
|
||||
if (k == p || k == c) {
|
||||
continue;
|
||||
}
|
||||
for (const ggml_tensor * in : nodes[k].inputs) {
|
||||
if (htp_tensor_reads_from(in, o0) || htp_tensor_reads_from(in, o1)) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (safe && k > p && k < c) {
|
||||
for (const ggml_tensor * out : nodes[k].outputs) {
|
||||
if (out && (htp_tensors_overlap(out, w0) || htp_tensors_overlap(out, w1) ||
|
||||
htp_tensors_overlap(out, act) || htp_tensors_overlap(out, o0) ||
|
||||
htp_tensors_overlap(out, o1))) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!safe) {
|
||||
continue;
|
||||
}
|
||||
|
||||
htp_opnode cand = nodes[p];
|
||||
if (!fuse_mul_mat_nx_glu(sess, cand, nodes[c])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
nodes[c] = std::move(cand);
|
||||
nodes.erase(nodes.begin() + (ptrdiff_t) p);
|
||||
--c;
|
||||
}
|
||||
}
|
||||
|
||||
void htp_fusion_optimize_nodes(const struct ggml_hexagon_session * sess, const struct ggml_cgraph * graph, std::vector<struct htp_opnode> & nodes) {
|
||||
GGML_UNUSED(graph);
|
||||
|
||||
if (htp_fusion_enabled(HTP_FUSE_MUL_MAT_NX_GLU)) {
|
||||
htp_fuse_mul_mat_nx_glu(sess, nodes);
|
||||
}
|
||||
if (htp_fusion_enabled(HTP_FUSE_GDN_STATE)) {
|
||||
htp_fuse_gdn_state(sess, nodes);
|
||||
}
|
||||
if (htp_fusion_enabled(HTP_FUSE_CONCAT_SSM_CONV)) {
|
||||
htp_fuse_concat_ssm_conv(sess, nodes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,12 @@ enum htp_fusion_flags {
|
||||
HTP_FUSE_MUL_MUL_SCALAR = (1 << 9), // 512
|
||||
HTP_FUSE_BINARY_NX = (1 << 10), // 1024
|
||||
HTP_FUSE_RMS_NORM_SCALE = (1 << 11), // 2048
|
||||
HTP_FUSE_SSM_CONV_SILU = (1 << 12), // 4096
|
||||
HTP_FUSE_SSM_CONV_SILU = (1 << 12), // 4096
|
||||
HTP_FUSE_RMS_NORM_MUL_ADD = (1 << 13), // 8192
|
||||
HTP_FUSE_UNARY_SCALE = (1 << 14), // 16384
|
||||
HTP_FUSE_GDN_STATE = (1 << 15), // 32768
|
||||
HTP_FUSE_CONCAT_SSM_CONV = (1 << 16), // 65536
|
||||
HTP_FUSE_MUL_MAT_NX_GLU = (1 << 17), // 131072
|
||||
};
|
||||
|
||||
static inline bool htp_fusion_enabled(int flag) {
|
||||
@@ -30,5 +34,6 @@ bool htp_fusion_can_stack(const struct ggml_tensor * a, const struct ggml_tensor
|
||||
int htp_fusion_max(const struct ggml_cgraph * graph, int idx);
|
||||
void htp_fusion_tag_graph(struct ggml_cgraph * graph);
|
||||
bool htp_fusion_try_fuse(const struct ggml_hexagon_session * sess, struct htp_opnode & prev, const struct htp_opnode & cur);
|
||||
void htp_fusion_optimize_nodes(const struct ggml_hexagon_session * sess, const struct ggml_cgraph * graph, std::vector<struct htp_opnode> & nodes);
|
||||
|
||||
#endif // HTP_FUSION_H
|
||||
|
||||
+1438
-1081
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,7 @@
|
||||
#define HTP_KPARAMS_H
|
||||
|
||||
#include "htp-common.h"
|
||||
#include "htp-lower.h"
|
||||
|
||||
#include "htp/allreduce-ops.h"
|
||||
#include "htp/argsort-ops.h"
|
||||
@@ -39,11 +40,6 @@
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
bool htp_rows_stride(const int64_t * ne, const size_t * nb, size_t * stride);
|
||||
bool htp_matmul_can_collapse(const struct ggml_tensor * src0, const struct ggml_tensor * src1, const struct ggml_tensor * dst);
|
||||
bool htp_matmul_add_can_collapse(const struct ggml_tensor * src0, const struct ggml_tensor * src1, const struct ggml_tensor * src2, const struct ggml_tensor * dst);
|
||||
ggml_tensor htp_tensor_collapse_rows(const struct ggml_tensor * t);
|
||||
|
||||
template <typename... Args>
|
||||
auto htp_kparams_type(bool (*)(Args...)) -> std::remove_pointer_t<std::remove_reference_t<decltype((std::declval<Args>(), ...))>>;
|
||||
|
||||
@@ -60,11 +56,6 @@ bool htp_kparams_solve(
|
||||
struct htp_opnode & node
|
||||
);
|
||||
|
||||
void htp_opnode_collapse(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
struct htp_opnode & node
|
||||
);
|
||||
|
||||
bool htp_kparams_solve(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
const struct ggml_tensor * op,
|
||||
@@ -101,6 +92,7 @@ bool htp_kparams_solve_rms_norm_mul_add(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
const struct ggml_tensor * src0,
|
||||
const struct ggml_tensor * src1,
|
||||
const struct ggml_tensor * src2,
|
||||
const struct ggml_tensor * dst,
|
||||
struct htp_unary_kernel_params * kparams
|
||||
);
|
||||
@@ -109,7 +101,7 @@ bool htp_kparams_solve_matmul_add(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
const struct ggml_tensor * src0,
|
||||
const struct ggml_tensor * src1,
|
||||
const struct ggml_tensor * src2,
|
||||
const struct ggml_tensor * bias,
|
||||
const struct ggml_tensor * dst,
|
||||
struct htp_mm_kernel_params * kparams
|
||||
);
|
||||
@@ -118,10 +110,21 @@ bool htp_kparams_solve_mmnx(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
const struct ggml_tensor * src0,
|
||||
const struct ggml_tensor * act,
|
||||
const struct ggml_tensor * dst,
|
||||
int32_t n_weights,
|
||||
struct htp_mm_kernel_params * kparams
|
||||
);
|
||||
|
||||
bool htp_kparams_solve_mmnx_glu(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
const struct ggml_tensor * w_gate,
|
||||
const struct ggml_tensor * w_up,
|
||||
const struct ggml_tensor * act,
|
||||
const struct ggml_tensor * dst,
|
||||
uint8_t glu_op,
|
||||
struct htp_mm_kernel_params * kparams
|
||||
);
|
||||
|
||||
bool htp_kparams_solve_mmidnx(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
const struct ggml_tensor * src0,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,37 @@
|
||||
#ifndef HTP_LOWER_H
|
||||
#define HTP_LOWER_H
|
||||
|
||||
#include "ggml.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
struct ggml_cgraph;
|
||||
struct ggml_hexagon_session;
|
||||
struct htp_opnode;
|
||||
|
||||
bool htp_rows_stride(const int64_t * ne, const size_t * nb, size_t * stride);
|
||||
bool htp_matmul_can_collapse(const struct ggml_tensor * src0, const struct ggml_tensor * src1, const struct ggml_tensor * dst);
|
||||
bool htp_matmul_add_can_collapse(const struct ggml_tensor * src0, const struct ggml_tensor * src1, const struct ggml_tensor * src2, const struct ggml_tensor * dst);
|
||||
ggml_tensor htp_tensor_collapse_rows(const struct ggml_tensor * t);
|
||||
|
||||
bool htp_dsv4_hc_pre_can_collapse(const struct ggml_tensor * x, const struct ggml_tensor * weights, const struct ggml_tensor * dst);
|
||||
void htp_dsv4_hc_pre_collapse_tensors(const struct ggml_tensor * x, const struct ggml_tensor * weights, const struct ggml_tensor * dst,
|
||||
struct ggml_tensor * x_c, struct ggml_tensor * w_c, struct ggml_tensor * d_c);
|
||||
|
||||
bool htp_dsv4_hc_post_can_collapse(const struct ggml_tensor * x, const struct ggml_tensor * residual, const struct ggml_tensor * post, const struct ggml_tensor * comb, const struct ggml_tensor * dst);
|
||||
void htp_dsv4_hc_post_collapse_tensors(const struct ggml_tensor * x, const struct ggml_tensor * residual, const struct ggml_tensor * post, const struct ggml_tensor * comb, const struct ggml_tensor * dst,
|
||||
struct ggml_tensor * x_c, struct ggml_tensor * r_c, struct ggml_tensor * p_c, struct ggml_tensor * c_c, struct ggml_tensor * d_c);
|
||||
|
||||
bool htp_lightning_indexer_can_collapse(const struct ggml_tensor * q, const struct ggml_tensor * k, const struct ggml_tensor * w, const struct ggml_tensor * m, const struct ggml_tensor * dst);
|
||||
void htp_lightning_indexer_collapse_tensors(const struct ggml_tensor * q, const struct ggml_tensor * k, const struct ggml_tensor * w, const struct ggml_tensor * m, const struct ggml_tensor * dst,
|
||||
struct ggml_tensor * q_c, struct ggml_tensor * k_c, struct ggml_tensor * w_c, struct ggml_tensor * m_c, struct ggml_tensor * dst_c);
|
||||
|
||||
void htp_lower_opnode(const struct ggml_hexagon_session * sess, htp_opnode & node, struct ggml_tensor * scratch = nullptr);
|
||||
void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml_cgraph * graph, std::vector<htp_opnode> & nodes);
|
||||
void htp_lower_reset_graph(const struct ggml_cgraph * graph);
|
||||
void htp_pack_tensor_lowered_f16(struct ggml_tensor * t, const void * src, size_t offset, size_t size);
|
||||
void htp_unpack_tensor_lowered_f16(void * dst, const struct ggml_tensor * t, size_t offset, size_t size);
|
||||
|
||||
#endif // HTP_LOWER_H
|
||||
@@ -36,8 +36,18 @@ std::string htp_opnode::name() const {
|
||||
case HTP_OP_ALLREDUCE: return "ALLREDUCE";
|
||||
case HTP_OP_ALLREDUCE_ADD: return "ALLREDUCE+ADD";
|
||||
case HTP_OP_HADAMARD: return "MUL_MAT_HADAMARD";
|
||||
case HTP_OP_MUL_MAT_NX: return "MUL_MAT_NX";
|
||||
case HTP_OP_MUL_MAT_ID_NX: return "MUL_MAT_ID_NX";
|
||||
case HTP_OP_MUL_MAT_NX: return "MUL_MAT_NX";
|
||||
case HTP_OP_MUL_MAT_NX_GLU: {
|
||||
const auto * kp = kparams<htp_mm_kernel_params>();
|
||||
switch (kp->glu_op) {
|
||||
case HTP_OP_GLU_SWIGLU: return "MUL_MAT_NX_SWIGLU";
|
||||
case HTP_OP_GLU_GEGLU: return "MUL_MAT_NX_GEGLU";
|
||||
case HTP_OP_GLU_GEGLU_QUICK: return "MUL_MAT_NX_GEGLU_QUICK";
|
||||
case HTP_OP_GLU_REGLU: return "MUL_MAT_NX_REGLU";
|
||||
default: return "MUL_MAT_NX_GLU";
|
||||
}
|
||||
}
|
||||
case HTP_OP_MUL_MAT_ID_NX: return "MUL_MAT_ID_NX";
|
||||
case HTP_OP_ADD:
|
||||
case HTP_OP_SUB:
|
||||
case HTP_OP_MUL:
|
||||
@@ -146,26 +156,32 @@ static std::string format_kernel_params(const htp_opnode & node) {
|
||||
const size_t max_size = sizeof(str);
|
||||
|
||||
if (node.opcode == HTP_OP_MUL_MAT || node.opcode == HTP_OP_MUL_MAT_ID ||
|
||||
node.opcode == HTP_OP_MUL_MAT_NX || node.opcode == HTP_OP_MUL_MAT_ID_NX ||
|
||||
node.opcode == HTP_OP_MUL_MAT_ADD) {
|
||||
node.opcode == HTP_OP_MUL_MAT_NX || node.opcode == HTP_OP_MUL_MAT_NX_GLU ||
|
||||
node.opcode == HTP_OP_MUL_MAT_ID_NX || node.opcode == HTP_OP_MUL_MAT_ADD) {
|
||||
const auto * kparams = node.kparams<htp_mm_kernel_params>();
|
||||
const char * path = "unknown";
|
||||
int32_t type = kparams->kernel_type;
|
||||
if (type == HTP_MM_KERNEL_HMX_2D || type == HTP_MM_KERNEL_HMX_BATCHED) {
|
||||
path = (kparams->tile_size > 0) ? "hmx-tiled" : "hmx-flat";
|
||||
} else if (type == HTP_MM_KERNEL_HVX_F16_F16_VTCM || type == HTP_MM_KERNEL_HVX_F32_F32_VTCM ||
|
||||
type == HTP_MM_KERNEL_HVX_BF16_F32_VTCM) {
|
||||
path = "hvx-flat";
|
||||
} else if (type == HTP_MM_KERNEL_HVX_QUANT_ROW || type == HTP_MM_KERNEL_HVX_QUANT_BLOCK) {
|
||||
path = "hvx-tiled";
|
||||
if (htp_mm_kernel_is_hmx_w8a8(type)) {
|
||||
snprintf(str, max_size, "hmx-w8a8 nhvx %u vtcm %u", (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else {
|
||||
const char * path = "unknown";
|
||||
if (type == HTP_MM_KERNEL_HMX_2D || type == HTP_MM_KERNEL_HMX_BATCHED) {
|
||||
path = (kparams->tile_size > 0) ? "hmx-tiled" : "hmx-flat";
|
||||
} else if (type == HTP_MM_KERNEL_HVX_F16_F16_VTCM || type == HTP_MM_KERNEL_HVX_F32_F32_VTCM ||
|
||||
type == HTP_MM_KERNEL_HVX_BF16_F32_VTCM) {
|
||||
path = "hvx-flat";
|
||||
} else if (type == HTP_MM_KERNEL_HVX_QUANT_ROW || type == HTP_MM_KERNEL_HVX_QUANT_BLOCK) {
|
||||
path = "hvx-tiled";
|
||||
}
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u", path, (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
}
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u", path, (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_FLASH_ATTN_EXT) {
|
||||
const auto * kparams = node.kparams<htp_fa_kernel_params>();
|
||||
const char * path = "unknown";
|
||||
int32_t type = kparams->kernel_type;
|
||||
if (type == HTP_FA_KERNEL_HMX) {
|
||||
path = kparams->u.hmx.pipeline ? "hmx-pipe" : "hmx-seq";
|
||||
if (type == HTP_FA_KERNEL_HMX_F16) {
|
||||
path = kparams->u.hmx.pipeline ? "hmx-f16-pipe" : "hmx-f16-seq";
|
||||
} else if (type == HTP_FA_KERNEL_HMX_W8A8) {
|
||||
path = kparams->u.hmx.pipeline ? "hmx-w8a8-pipe" : "hmx-w8a8-seq";
|
||||
} else if (type == HTP_FA_KERNEL_HVX) {
|
||||
path = "hvx";
|
||||
}
|
||||
@@ -182,7 +198,10 @@ static std::string format_kernel_params(const htp_opnode & node) {
|
||||
snprintf(str, max_size, "seq 0x%x -> 0x%x", (uint32_t) node.node->op_params[0], (uint32_t) node.node->op_params[1]);
|
||||
} else if (node.opcode == HTP_OP_SSM_CONV) {
|
||||
const auto * kparams = node.kparams<htp_ssm_conv_kernel_params>();
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u", kparams->n_t == 1 ? "decode" : "prefill",
|
||||
const char * mode = kparams->token_major
|
||||
? (kparams->n_t == 1 ? "tok-decode" : "tok-prefill")
|
||||
: (kparams->n_t == 1 ? "decode" : "prefill");
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u", mode,
|
||||
(unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_SSM_SCAN) {
|
||||
const auto * kparams = node.kparams<htp_ssm_scan_kernel_params>();
|
||||
@@ -240,10 +259,10 @@ static std::string format_kernel_params(const htp_opnode & node) {
|
||||
snprintf(str, max_size, "nhvx %u vtcm %u", (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_PAD) {
|
||||
const auto * kparams = node.kparams<htp_pad_kernel_params>();
|
||||
snprintf(str, max_size, "k%d nhvx %u vtcm %u", (int) kparams->kernel_type, (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
snprintf(str, max_size, "k%d nhvx %u chunk %u vtcm %u", (int) kparams->kernel_type, (unsigned int) kparams->n_hvx, (unsigned int) kparams->chunk_elems, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_CUMSUM) {
|
||||
const auto * kparams = node.kparams<htp_cumsum_kernel_params>();
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u", kparams->use_dma ? "dma" : "hvx", (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
snprintf(str, max_size, "nhvx %u chunk %u vtcm %u", (unsigned int) kparams->n_hvx, (unsigned int) kparams->chunk_elems, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_TOP_K_MASK) {
|
||||
const auto * kparams = node.kparams<htp_top_k_mask_kernel_params>();
|
||||
const char * ktype_str = "?";
|
||||
@@ -260,7 +279,7 @@ static std::string format_kernel_params(const htp_opnode & node) {
|
||||
snprintf(str, max_size, "k%d nhvx %u", (int) kparams->kernel_type, (unsigned int) kparams->n_hvx);
|
||||
} else if (node.opcode == HTP_OP_DIAG) {
|
||||
const auto * kparams = node.kparams<htp_diag_kernel_params>();
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u", kparams->use_dma ? "dma" : "hvx", (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
snprintf(str, max_size, "nhvx %u chunk %u vtcm %u", (unsigned int) kparams->n_hvx, (unsigned int) kparams->chunk_elems, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_SOLVE_TRI) {
|
||||
const auto * kparams = node.kparams<htp_solve_tri_kernel_params>();
|
||||
snprintf(str, max_size, "%s nhvx %u", kparams->batched ? "batched" : "chunked", (unsigned int) kparams->n_hvx);
|
||||
|
||||
@@ -91,7 +91,7 @@ struct htp_opnode {
|
||||
// Build inputs (preserving optional nullptrs)
|
||||
if (htp_is_hadamard(this->node)) {
|
||||
this->inputs.push_back(this->node->src[1]);
|
||||
} else {
|
||||
} else if (this->node->op != GGML_OP_FILL) {
|
||||
int n_inputs = n_active_src(this->node);
|
||||
for (int i = 0; i < n_inputs; i++) {
|
||||
this->inputs.push_back(this->node->src[i]);
|
||||
|
||||
@@ -15,169 +15,67 @@
|
||||
# include <arm_neon.h>
|
||||
#endif
|
||||
|
||||
// --- 4-bit block-32 nibble helpers (shared by Q4_0, IQ4_NL, Q4_1, Q5_0, Q5_1, MXFP4) ---
|
||||
|
||||
static inline void unpack_nibbles_32(uint8_t * dst, const uint8_t * qs) {
|
||||
for (unsigned int i = 0; i < 16; ++i) {
|
||||
dst[i + 0] = qs[i] & 0x0F;
|
||||
dst[i + 16] = qs[i] >> 4;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void pack_nibbles_32(uint8_t * qs, const uint8_t * src) {
|
||||
for (unsigned int i = 0; i < 16; ++i) {
|
||||
qs[i] = src[i + 0] | (src[i + 16] << 4);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void transpose_nibbles_to_tile(uint8_t * tile_dst, const uint8_t tile_quants[32][32]) {
|
||||
for (int cp = 0; cp < 16; cp++) {
|
||||
for (int row = 0; row < 32; row++) {
|
||||
tile_dst[cp * 32 + row] = (tile_quants[row][2 * cp + 1] << 4) | tile_quants[row][2 * cp];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void transpose_tile_to_nibbles(uint8_t tile_quants[32][32], const uint8_t * tile_src) {
|
||||
for (int cp = 0; cp < 16; cp++) {
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const uint8_t val = tile_src[cp * 32 + row];
|
||||
tile_quants[row][2 * cp + 0] = val & 0x0F;
|
||||
tile_quants[row][2 * cp + 1] = val >> 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 5th-bit plane of one row (qh bit k = quant k), same bit layout as the Q5_K plane
|
||||
// 5th-bit plane of one row (qh bit k = quant k), 4-K-grouped layout matching wb planes
|
||||
static inline void pack_q5_plane(uint8_t * plane, int row, uint32_t qh) {
|
||||
for (int c = 0; c < 4; c++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
uint32_t v = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
v |= ((qh >> (8 * i + 2 * c)) & 3) << (2 * i);
|
||||
for (int g = 0; g < 8; g++) {
|
||||
v |= ((qh >> (4 * g + j)) & 1u) << g;
|
||||
}
|
||||
plane[c * 32 + row] = (uint8_t) v;
|
||||
plane[row * 4 + j] = (uint8_t) v;
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t unpack_q5_plane(const uint8_t * plane, int row) {
|
||||
uint32_t qh = 0;
|
||||
for (int c = 0; c < 4; c++) {
|
||||
const uint32_t v = plane[c * 32 + row];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
qh |= ((v >> (2 * i)) & 3) << (8 * i + 2 * c);
|
||||
for (int j = 0; j < 4; j++) {
|
||||
const uint32_t v = plane[row * 4 + j];
|
||||
for (int g = 0; g < 8; g++) {
|
||||
qh |= ((v >> g) & 1u) << (4 * g + j);
|
||||
}
|
||||
}
|
||||
return qh;
|
||||
}
|
||||
|
||||
#if defined(__ARM_NEON) || defined(__aarch64__)
|
||||
static void repack_neon_transpose8x8(const uint8x8_t rows[8], uint8x8_t columns[8]) {
|
||||
const uint8x8x2_t t0 = vtrn_u8(rows[0], rows[1]);
|
||||
const uint8x8x2_t t1 = vtrn_u8(rows[2], rows[3]);
|
||||
const uint8x8x2_t t2 = vtrn_u8(rows[4], rows[5]);
|
||||
const uint8x8x2_t t3 = vtrn_u8(rows[6], rows[7]);
|
||||
const uint16x4x2_t t4 = vtrn_u16(vreinterpret_u16_u8(t0.val[0]), vreinterpret_u16_u8(t1.val[0]));
|
||||
const uint16x4x2_t t5 = vtrn_u16(vreinterpret_u16_u8(t0.val[1]), vreinterpret_u16_u8(t1.val[1]));
|
||||
const uint16x4x2_t t6 = vtrn_u16(vreinterpret_u16_u8(t2.val[0]), vreinterpret_u16_u8(t3.val[0]));
|
||||
const uint16x4x2_t t7 = vtrn_u16(vreinterpret_u16_u8(t2.val[1]), vreinterpret_u16_u8(t3.val[1]));
|
||||
const uint32x2x2_t t8 = vtrn_u32(vreinterpret_u32_u16(t4.val[0]), vreinterpret_u32_u16(t6.val[0]));
|
||||
const uint32x2x2_t t9 = vtrn_u32(vreinterpret_u32_u16(t4.val[1]), vreinterpret_u32_u16(t6.val[1]));
|
||||
const uint32x2x2_t ta = vtrn_u32(vreinterpret_u32_u16(t5.val[0]), vreinterpret_u32_u16(t7.val[0]));
|
||||
const uint32x2x2_t tb = vtrn_u32(vreinterpret_u32_u16(t5.val[1]), vreinterpret_u32_u16(t7.val[1]));
|
||||
columns[0] = vreinterpret_u8_u32(t8.val[0]);
|
||||
columns[1] = vreinterpret_u8_u32(ta.val[0]);
|
||||
columns[2] = vreinterpret_u8_u32(t9.val[0]);
|
||||
columns[3] = vreinterpret_u8_u32(tb.val[0]);
|
||||
columns[4] = vreinterpret_u8_u32(t8.val[1]);
|
||||
columns[5] = vreinterpret_u8_u32(ta.val[1]);
|
||||
columns[6] = vreinterpret_u8_u32(t9.val[1]);
|
||||
columns[7] = vreinterpret_u8_u32(tb.val[1]);
|
||||
}
|
||||
|
||||
static uint8x8_t repack_neon_pack_4bit_pairs(uint8x8_t first, uint8x8_t second, bool high) {
|
||||
const uint8x8x2_t separated = vuzp_u8(first, second);
|
||||
const uint8x8_t mask = vdup_n_u8(0x0F);
|
||||
uint8x8_t even = separated.val[0];
|
||||
uint8x8_t odd = separated.val[1];
|
||||
|
||||
if (high) {
|
||||
even = vshr_n_u8(even, 4);
|
||||
odd = vshr_n_u8(odd, 4);
|
||||
}
|
||||
|
||||
even = vand_u8(even, mask);
|
||||
odd = vand_u8(odd, mask);
|
||||
return vorr_u8(vshl_n_u8(odd, 4), even);
|
||||
}
|
||||
|
||||
static void repack_neon_4bit_rows(const uint8_t * qs[8], uint8_t * tile_dst, int row0) {
|
||||
uint8x8_t low_rows[8];
|
||||
uint8x8_t high_rows[8];
|
||||
uint8x8_t columns[8];
|
||||
|
||||
for (int rr = 0; rr < 8; ++rr) {
|
||||
const uint8x16_t packed = vld1q_u8(qs[rr]);
|
||||
const uint8x8_t first = vget_low_u8(packed);
|
||||
const uint8x8_t second = vget_high_u8(packed);
|
||||
low_rows[rr] = repack_neon_pack_4bit_pairs(first, second, false);
|
||||
high_rows[rr] = repack_neon_pack_4bit_pairs(first, second, true);
|
||||
}
|
||||
|
||||
repack_neon_transpose8x8(low_rows, columns);
|
||||
for (int cp = 0; cp < 8; ++cp) {
|
||||
vst1_u8(tile_dst + cp * 32 + row0, columns[cp]);
|
||||
}
|
||||
|
||||
repack_neon_transpose8x8(high_rows, columns);
|
||||
for (int cp = 0; cp < 8; ++cp) {
|
||||
vst1_u8(tile_dst + (cp + 8) * 32 + row0, columns[cp]);
|
||||
}
|
||||
}
|
||||
|
||||
static void repack_neon_k4_rows(const uint8_t * qs[8], uint8_t * tile_dst, int row0, bool high) {
|
||||
uint8x8_t first_rows[8];
|
||||
uint8x8_t second_rows[8];
|
||||
uint8x8_t columns[8];
|
||||
|
||||
for (int rr = 0; rr < 8; ++rr) {
|
||||
first_rows[rr] = repack_neon_pack_4bit_pairs(vld1_u8(qs[rr]), vld1_u8(qs[rr] + 8), high);
|
||||
second_rows[rr] = repack_neon_pack_4bit_pairs(vld1_u8(qs[rr] + 16), vld1_u8(qs[rr] + 24), high);
|
||||
}
|
||||
|
||||
repack_neon_transpose8x8(first_rows, columns);
|
||||
for (int cp = 0; cp < 8; ++cp) {
|
||||
vst1_u8(tile_dst + cp * 32 + row0, columns[cp]);
|
||||
}
|
||||
|
||||
repack_neon_transpose8x8(second_rows, columns);
|
||||
for (int cp = 0; cp < 8; ++cp) {
|
||||
vst1_u8(tile_dst + (cp + 8) * 32 + row0, columns[cp]);
|
||||
}
|
||||
}
|
||||
|
||||
static void repack_neon_q8_rows(const uint8_t * qs[8], uint8_t * tile_dst, int row0) {
|
||||
uint8x8_t rows[8];
|
||||
uint8x8_t columns[8];
|
||||
|
||||
for (int cp = 0; cp < 16; cp += 4) {
|
||||
for (int rr = 0; rr < 8; ++rr) {
|
||||
rows[rr] = vld1_u8(qs[rr] + 2 * cp);
|
||||
}
|
||||
repack_neon_transpose8x8(rows, columns);
|
||||
|
||||
for (int pair = 0; pair < 4; ++pair) {
|
||||
const uint8x8x2_t interleaved = vzip_u8(columns[2 * pair], columns[2 * pair + 1]);
|
||||
uint8_t * dst = tile_dst + (cp + pair) * 64 + 2 * row0;
|
||||
vst1_u8(dst, interleaved.val[0]);
|
||||
vst1_u8(dst + 8, interleaved.val[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void repack_store_u32(uint8_t * dst, uint32_t v) {
|
||||
memcpy(dst, &v, sizeof(v));
|
||||
}
|
||||
|
||||
static inline void repack_neon_4bit_wb_row(const uint8_t * qs, uint8_t * tile_dst, int row) {
|
||||
const uint32x4_t w = vreinterpretq_u32_u8(vld1q_u8(qs));
|
||||
const uint32x2x2_t u = vuzp_u32(vget_low_u32(w), vget_high_u32(w));
|
||||
const uint8x8_t even = vreinterpret_u8_u32(u.val[0]);
|
||||
const uint8x8_t odd = vreinterpret_u8_u32(u.val[1]);
|
||||
const uint32x2_t lo = vreinterpret_u32_u8(vorr_u8(vand_u8(even, vdup_n_u8(0x0F)), vshl_n_u8(odd, 4)));
|
||||
const uint32x2_t hi = vreinterpret_u32_u8(vorr_u8(vshr_n_u8(even, 4), vand_u8(odd, vdup_n_u8(0xF0))));
|
||||
repack_store_u32(tile_dst + 0 * 128 + row * 4, vget_lane_u32(lo, 0));
|
||||
repack_store_u32(tile_dst + 1 * 128 + row * 4, vget_lane_u32(lo, 1));
|
||||
repack_store_u32(tile_dst + 2 * 128 + row * 4, vget_lane_u32(hi, 0));
|
||||
repack_store_u32(tile_dst + 3 * 128 + row * 4, vget_lane_u32(hi, 1));
|
||||
}
|
||||
|
||||
static inline void repack_neon_k4_wb_row(const uint8_t * qs_sub, uint8_t * tile_dst, int row, bool high) {
|
||||
const uint32x4_t w0 = vreinterpretq_u32_u8(vld1q_u8(qs_sub));
|
||||
const uint32x4_t w1 = vreinterpretq_u32_u8(vld1q_u8(qs_sub + 16));
|
||||
const uint32x2x2_t u0 = vuzp_u32(vget_low_u32(w0), vget_high_u32(w0));
|
||||
const uint32x2x2_t u1 = vuzp_u32(vget_low_u32(w1), vget_high_u32(w1));
|
||||
const uint8x8_t even0 = vreinterpret_u8_u32(u0.val[0]);
|
||||
const uint8x8_t odd0 = vreinterpret_u8_u32(u0.val[1]);
|
||||
const uint8x8_t even1 = vreinterpret_u8_u32(u1.val[0]);
|
||||
const uint8x8_t odd1 = vreinterpret_u8_u32(u1.val[1]);
|
||||
const uint32x2_t p01 = vreinterpret_u32_u8(high
|
||||
? vorr_u8(vshr_n_u8(even0, 4), vand_u8(odd0, vdup_n_u8(0xF0)))
|
||||
: vorr_u8(vand_u8(even0, vdup_n_u8(0x0F)), vshl_n_u8(odd0, 4)));
|
||||
const uint32x2_t p23 = vreinterpret_u32_u8(high
|
||||
? vorr_u8(vshr_n_u8(even1, 4), vand_u8(odd1, vdup_n_u8(0xF0)))
|
||||
: vorr_u8(vand_u8(even1, vdup_n_u8(0x0F)), vshl_n_u8(odd1, 4)));
|
||||
repack_store_u32(tile_dst + 0 * 128 + row * 4, vget_lane_u32(p01, 0));
|
||||
repack_store_u32(tile_dst + 1 * 128 + row * 4, vget_lane_u32(p01, 1));
|
||||
repack_store_u32(tile_dst + 2 * 128 + row * 4, vget_lane_u32(p23, 0));
|
||||
repack_store_u32(tile_dst + 3 * 128 + row * 4, vget_lane_u32(p23, 1));
|
||||
}
|
||||
|
||||
static inline uint32x4_t repack_neon_transpose_2bit_4x4(uint32x4_t w) {
|
||||
uint32x4_t t = vandq_u32(veorq_u32(w, vshrq_n_u32(w, 6)), vdupq_n_u32(0x00CC00CCu));
|
||||
w = veorq_u32(w, veorq_u32(t, vshlq_n_u32(t, 6)));
|
||||
@@ -262,19 +160,16 @@ static inline void repack_neon_q6_k(const block_q6_K * b, int kt_local, uint8_t
|
||||
}
|
||||
|
||||
static inline void repack_neon_q5_k_qh(const uint8_t * qh, uint8_t hbit, uint8_t * plane, int row) {
|
||||
static const uint8_t mask_bytes[16] = { 1, 2, 1, 2, 1, 2, 1, 2, 4, 8, 4, 8, 4, 8, 4, 8 };
|
||||
const uint8x16_t m0 = vld1q_u8(mask_bytes);
|
||||
static const uint32_t mask_words[4] = { 0x01010101u, 0x02020202u, 0x04040404u, 0x08080808u };
|
||||
const uint8x16_t m0 = vreinterpretq_u8_u32(vld1q_u32(mask_words));
|
||||
const uint8x16_t m1 = vshlq_n_u8(m0, 4);
|
||||
const uint8x16_t vbit = vdupq_n_u8(hbit);
|
||||
const uint8x16_t b0 = vandq_u8(vtstq_u8(vld1q_u8(qh + 0), vbit), m0);
|
||||
const uint8x16_t b1 = vandq_u8(vtstq_u8(vld1q_u8(qh + 16), vbit), m1);
|
||||
const uint8x16_t b01 = vorrq_u8(b0, b1);
|
||||
const uint8x8_t fold = vorr_u8(vget_low_u8(b01), vget_high_u8(b01));
|
||||
const uint8x8_t out = vpadd_u8(fold, fold);
|
||||
plane[0 * 32 + row] = vget_lane_u8(out, 0);
|
||||
plane[1 * 32 + row] = vget_lane_u8(out, 1);
|
||||
plane[2 * 32 + row] = vget_lane_u8(out, 2);
|
||||
plane[3 * 32 + row] = vget_lane_u8(out, 3);
|
||||
const uint32x4_t b01 = vreinterpretq_u32_u8(vorrq_u8(b0, b1));
|
||||
const uint32x2_t f0 = vorr_u32(vget_low_u32(b01), vget_high_u32(b01));
|
||||
const uint32x2_t f1 = vpadd_u32(f0, f0);
|
||||
repack_store_u32(plane + row * 4, vget_lane_u32(f1, 0));
|
||||
}
|
||||
|
||||
static inline uint16x8_t repack_neon_load_f16x8(const uint16_t * src) {
|
||||
@@ -457,53 +352,62 @@ static void unpack_from_tiled_grid_32(void * data, const ggml_tensor * t, size_t
|
||||
|
||||
// --- Block-32 Quantized Types (Q4_0, IQ4_NL, Q4_1, Q5_0, Q5_1, Q8_0, MXFP4) ---
|
||||
|
||||
static inline void repack_4bit_wb_row(const uint8_t * qs, uint8_t * tile_dst, int row) {
|
||||
#if defined(__ARM_NEON) || defined(__aarch64__)
|
||||
repack_neon_4bit_wb_row(qs, tile_dst, row);
|
||||
#else
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
tile_dst[0 * 128 + row * 4 + j] = (uint8_t) (((qs[4 + j] & 0x0F) << 4) | (qs[0 + j] & 0x0F));
|
||||
tile_dst[1 * 128 + row * 4 + j] = (uint8_t) (((qs[12 + j] & 0x0F) << 4) | (qs[8 + j] & 0x0F));
|
||||
tile_dst[2 * 128 + row * 4 + j] = (uint8_t) ((qs[4 + j] & 0xF0) | (qs[0 + j] >> 4));
|
||||
tile_dst[3 * 128 + row * 4 + j] = (uint8_t) ((qs[12 + j] & 0xF0) | (qs[8 + j] >> 4));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void unpack_4bit_wb_row(uint8_t * qs, const uint8_t * tile_src, int row) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
const uint8_t p0 = tile_src[0 * 128 + row * 4 + j];
|
||||
const uint8_t p1 = tile_src[1 * 128 + row * 4 + j];
|
||||
const uint8_t p2 = tile_src[2 * 128 + row * 4 + j];
|
||||
const uint8_t p3 = tile_src[3 * 128 + row * 4 + j];
|
||||
qs[0 + j] = (uint8_t) ((p0 & 0x0F) | ((p2 & 0x0F) << 4));
|
||||
qs[4 + j] = (uint8_t) ((p0 >> 4) | (p2 & 0xF0));
|
||||
qs[8 + j] = (uint8_t) ((p1 & 0x0F) | ((p3 & 0x0F) << 4));
|
||||
qs[12 + j] = (uint8_t) ((p1 >> 4) | (p3 & 0xF0));
|
||||
}
|
||||
}
|
||||
|
||||
static void repack_q4_0_tiled(ggml_tensor * t, const void * data, size_t offset, size_t size) {
|
||||
repack_to_tiled_grid_32<block_q4_0, 1>(t, data, offset, size, HTP_TENSOR_TILE_SIZE_Q4_0,
|
||||
[](uint8_t * tile_dst, const block_q4_0 * src_slice, int64_t ne0, int64_t ne1, int64_t row_stride, int ct, int kt) {
|
||||
ggml_half * scale_dst = (ggml_half *) (tile_dst + 512);
|
||||
#if defined(__ARM_NEON) || defined(__aarch64__)
|
||||
if (ct * 32 + 32 <= ne1 && kt < ne0 / 32) {
|
||||
for (int row0 = 0; row0 < 32; row0 += 8) {
|
||||
const uint8_t * qs[8];
|
||||
for (int rr = 0; rr < 8; ++rr) {
|
||||
qs[rr] = src_slice[(ct * 32 + row0 + rr) * row_stride + kt].qs;
|
||||
}
|
||||
repack_neon_4bit_rows(qs, tile_dst, row0);
|
||||
}
|
||||
|
||||
for (int row = 0; row < 32; ++row) {
|
||||
scale_dst[row] = src_slice[(ct * 32 + row) * row_stride + kt].d;
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
uint8_t tile_quants[32][32];
|
||||
const bool valid_kt = kt < ne0 / 32;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r < ne1 && kt < ne0 / 32) {
|
||||
if (r < ne1 && valid_kt) {
|
||||
const block_q4_0 & b = src_slice[r * row_stride + kt];
|
||||
unpack_nibbles_32(tile_quants[row], b.qs);
|
||||
repack_4bit_wb_row(b.qs, tile_dst, row);
|
||||
scale_dst[row] = b.d;
|
||||
} else {
|
||||
memset(tile_quants[row], 8, 32);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
memset(tile_dst + i * 128 + row * 4, 0x88, 4);
|
||||
}
|
||||
scale_dst[row] = 0;
|
||||
}
|
||||
}
|
||||
transpose_nibbles_to_tile(tile_dst, tile_quants);
|
||||
});
|
||||
}
|
||||
|
||||
static void repack_tiled_q4_0(void * data, const ggml_tensor * t, size_t offset, size_t size) {
|
||||
unpack_from_tiled_grid_32<block_q4_0, 1>(data, t, offset, size, HTP_TENSOR_TILE_SIZE_Q4_0,
|
||||
[](block_q4_0 * dst_slice, const uint8_t * tile_src, int64_t ne0, int64_t start_row, int64_t end_row, int64_t row_stride, int ct, int kt) {
|
||||
uint8_t tile_quants[32][32];
|
||||
transpose_tile_to_nibbles(tile_quants, tile_src);
|
||||
const ggml_half * scale_src = (const ggml_half *) (tile_src + 512);
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r >= start_row && r < end_row && kt < ne0 / 32) {
|
||||
block_q4_0 & b = dst_slice[(r - start_row) * row_stride + kt];
|
||||
pack_nibbles_32(b.qs, tile_quants[row]);
|
||||
unpack_4bit_wb_row(b.qs, tile_src, row);
|
||||
b.d = scale_src[row];
|
||||
}
|
||||
}
|
||||
@@ -514,53 +418,34 @@ static void repack_q4_1_tiled(ggml_tensor * t, const void * data, size_t offset,
|
||||
repack_to_tiled_grid_32<block_q4_1, 1>(t, data, offset, size, HTP_TENSOR_TILE_SIZE_Q4_1,
|
||||
[](uint8_t * tile_dst, const block_q4_1 * src_slice, int64_t ne0, int64_t ne1, int64_t row_stride, int ct, int kt) {
|
||||
ggml_half * scale_dst = (ggml_half *) (tile_dst + 512);
|
||||
#if defined(__ARM_NEON) || defined(__aarch64__)
|
||||
if (ct * 32 + 32 <= ne1 && kt < ne0 / 32) {
|
||||
for (int row0 = 0; row0 < 32; row0 += 8) {
|
||||
const uint8_t * qs[8];
|
||||
for (int rr = 0; rr < 8; ++rr) {
|
||||
qs[rr] = src_slice[(ct * 32 + row0 + rr) * row_stride + kt].qs;
|
||||
}
|
||||
repack_neon_4bit_rows(qs, tile_dst, row0);
|
||||
}
|
||||
|
||||
for (int row = 0; row < 32; ++row) {
|
||||
const block_q4_1 & src_block = src_slice[(ct * 32 + row) * row_stride + kt];
|
||||
scale_dst[2 * row + 0] = src_block.d;
|
||||
scale_dst[2 * row + 1] = src_block.m;
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
uint8_t tile_quants[32][32];
|
||||
const bool valid_kt = kt < ne0 / 32;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r < ne1 && kt < ne0 / 32) {
|
||||
if (r < ne1 && valid_kt) {
|
||||
const block_q4_1 & b = src_slice[r * row_stride + kt];
|
||||
unpack_nibbles_32(tile_quants[row], b.qs);
|
||||
repack_4bit_wb_row(b.qs, tile_dst, row);
|
||||
scale_dst[2 * row + 0] = b.d;
|
||||
scale_dst[2 * row + 1] = b.m;
|
||||
} else {
|
||||
memset(tile_quants[row], 0, 32);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
memset(tile_dst + i * 128 + row * 4, 0, 4);
|
||||
}
|
||||
scale_dst[2 * row + 0] = 0;
|
||||
scale_dst[2 * row + 1] = 0;
|
||||
}
|
||||
}
|
||||
transpose_nibbles_to_tile(tile_dst, tile_quants);
|
||||
});
|
||||
}
|
||||
|
||||
static void repack_tiled_q4_1(void * data, const ggml_tensor * t, size_t offset, size_t size) {
|
||||
unpack_from_tiled_grid_32<block_q4_1, 1>(data, t, offset, size, HTP_TENSOR_TILE_SIZE_Q4_1,
|
||||
[](block_q4_1 * dst_slice, const uint8_t * tile_src, int64_t ne0, int64_t start_row, int64_t end_row, int64_t row_stride, int ct, int kt) {
|
||||
uint8_t tile_quants[32][32];
|
||||
transpose_tile_to_nibbles(tile_quants, tile_src);
|
||||
const ggml_half * scale_src = (const ggml_half *) (tile_src + 512);
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r >= start_row && r < end_row && kt < ne0 / 32) {
|
||||
block_q4_1 & b = dst_slice[(r - start_row) * row_stride + kt];
|
||||
pack_nibbles_32(b.qs, tile_quants[row]);
|
||||
unpack_4bit_wb_row(b.qs, tile_src, row);
|
||||
b.d = scale_src[2 * row + 0];
|
||||
b.m = scale_src[2 * row + 1];
|
||||
}
|
||||
@@ -573,58 +458,37 @@ static void repack_q5_0_tiled(ggml_tensor * t, const void * data, size_t offset,
|
||||
[](uint8_t * tile_dst, const block_q5_0 * src_slice, int64_t ne0, int64_t ne1, int64_t row_stride, int ct, int kt) {
|
||||
uint8_t * plane_dst = tile_dst + 512;
|
||||
ggml_half * scale_dst = (ggml_half *) (tile_dst + 640);
|
||||
#if defined(__ARM_NEON) || defined(__aarch64__)
|
||||
if (ct * 32 + 32 <= ne1 && kt < ne0 / 32) {
|
||||
for (int row0 = 0; row0 < 32; row0 += 8) {
|
||||
const uint8_t * qs[8];
|
||||
for (int rr = 0; rr < 8; ++rr) {
|
||||
qs[rr] = src_slice[(ct * 32 + row0 + rr) * row_stride + kt].qs;
|
||||
}
|
||||
repack_neon_4bit_rows(qs, tile_dst, row0);
|
||||
}
|
||||
|
||||
for (int row = 0; row < 32; ++row) {
|
||||
const block_q5_0 & src_block = src_slice[(ct * 32 + row) * row_stride + kt];
|
||||
uint32_t qh;
|
||||
memcpy(&qh, src_block.qh, sizeof(qh));
|
||||
pack_q5_plane(plane_dst, row, ~qh);
|
||||
scale_dst[row] = src_block.d;
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
uint8_t tile_quants[32][32];
|
||||
const bool valid_kt = kt < ne0 / 32;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r < ne1 && kt < ne0 / 32) {
|
||||
if (r < ne1 && valid_kt) {
|
||||
const block_q5_0 & b = src_slice[r * row_stride + kt];
|
||||
unpack_nibbles_32(tile_quants[row], b.qs);
|
||||
repack_4bit_wb_row(b.qs, tile_dst, row);
|
||||
uint32_t qh;
|
||||
memcpy(&qh, b.qh, sizeof(qh));
|
||||
pack_q5_plane(plane_dst, row, ~qh);
|
||||
scale_dst[row] = b.d;
|
||||
} else {
|
||||
memset(tile_quants[row], 0, 32);
|
||||
pack_q5_plane(plane_dst, row, 0);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
memset(tile_dst + i * 128 + row * 4, 0, 4);
|
||||
}
|
||||
memset(plane_dst + row * 4, 0, 4);
|
||||
scale_dst[row] = 0;
|
||||
}
|
||||
}
|
||||
transpose_nibbles_to_tile(tile_dst, tile_quants);
|
||||
});
|
||||
}
|
||||
|
||||
static void repack_tiled_q5_0(void * data, const ggml_tensor * t, size_t offset, size_t size) {
|
||||
unpack_from_tiled_grid_32<block_q5_0, 1>(data, t, offset, size, HTP_TENSOR_TILE_SIZE_Q5_0,
|
||||
[](block_q5_0 * dst_slice, const uint8_t * tile_src, int64_t ne0, int64_t start_row, int64_t end_row, int64_t row_stride, int ct, int kt) {
|
||||
uint8_t tile_quants[32][32];
|
||||
transpose_tile_to_nibbles(tile_quants, tile_src);
|
||||
const uint8_t * plane_src = tile_src + 512;
|
||||
const ggml_half * scale_src = (const ggml_half *) (tile_src + 640);
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r >= start_row && r < end_row && kt < ne0 / 32) {
|
||||
block_q5_0 & b = dst_slice[(r - start_row) * row_stride + kt];
|
||||
pack_nibbles_32(b.qs, tile_quants[row]);
|
||||
unpack_4bit_wb_row(b.qs, tile_src, row);
|
||||
const uint32_t qh = ~unpack_q5_plane(plane_src, row);
|
||||
memcpy(b.qh, &qh, sizeof(qh));
|
||||
b.d = scale_src[row];
|
||||
@@ -639,61 +503,39 @@ static void repack_q5_1_tiled(ggml_tensor * t, const void * data, size_t offset,
|
||||
[](uint8_t * tile_dst, const block_q5_1 * src_slice, int64_t ne0, int64_t ne1, int64_t row_stride, int ct, int kt) {
|
||||
ggml_half * scale_dst = (ggml_half *) (tile_dst + 512);
|
||||
uint8_t * plane_dst = tile_dst + 640;
|
||||
#if defined(__ARM_NEON) || defined(__aarch64__)
|
||||
if (ct * 32 + 32 <= ne1 && kt < ne0 / 32) {
|
||||
for (int row0 = 0; row0 < 32; row0 += 8) {
|
||||
const uint8_t * qs[8];
|
||||
for (int rr = 0; rr < 8; ++rr) {
|
||||
qs[rr] = src_slice[(ct * 32 + row0 + rr) * row_stride + kt].qs;
|
||||
}
|
||||
repack_neon_4bit_rows(qs, tile_dst, row0);
|
||||
}
|
||||
|
||||
for (int row = 0; row < 32; ++row) {
|
||||
const block_q5_1 & src_block = src_slice[(ct * 32 + row) * row_stride + kt];
|
||||
uint32_t qh;
|
||||
memcpy(&qh, src_block.qh, sizeof(qh));
|
||||
pack_q5_plane(plane_dst, row, qh);
|
||||
scale_dst[2 * row + 0] = src_block.d;
|
||||
scale_dst[2 * row + 1] = src_block.m;
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
uint8_t tile_quants[32][32];
|
||||
const bool valid_kt = kt < ne0 / 32;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r < ne1 && kt < ne0 / 32) {
|
||||
if (r < ne1 && valid_kt) {
|
||||
const block_q5_1 & b = src_slice[r * row_stride + kt];
|
||||
unpack_nibbles_32(tile_quants[row], b.qs);
|
||||
repack_4bit_wb_row(b.qs, tile_dst, row);
|
||||
uint32_t qh;
|
||||
memcpy(&qh, b.qh, sizeof(qh));
|
||||
pack_q5_plane(plane_dst, row, qh);
|
||||
scale_dst[2 * row + 0] = b.d;
|
||||
scale_dst[2 * row + 1] = b.m;
|
||||
} else {
|
||||
memset(tile_quants[row], 0, 32);
|
||||
pack_q5_plane(plane_dst, row, 0);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
memset(tile_dst + i * 128 + row * 4, 0, 4);
|
||||
}
|
||||
memset(plane_dst + row * 4, 0, 4);
|
||||
scale_dst[2 * row + 0] = 0;
|
||||
scale_dst[2 * row + 1] = 0;
|
||||
}
|
||||
}
|
||||
transpose_nibbles_to_tile(tile_dst, tile_quants);
|
||||
});
|
||||
}
|
||||
|
||||
static void repack_tiled_q5_1(void * data, const ggml_tensor * t, size_t offset, size_t size) {
|
||||
unpack_from_tiled_grid_32<block_q5_1, 1>(data, t, offset, size, HTP_TENSOR_TILE_SIZE_Q5_1,
|
||||
[](block_q5_1 * dst_slice, const uint8_t * tile_src, int64_t ne0, int64_t start_row, int64_t end_row, int64_t row_stride, int ct, int kt) {
|
||||
uint8_t tile_quants[32][32];
|
||||
transpose_tile_to_nibbles(tile_quants, tile_src);
|
||||
const ggml_half * scale_src = (const ggml_half *) (tile_src + 512);
|
||||
const uint8_t * plane_src = tile_src + 640;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r >= start_row && r < end_row && kt < ne0 / 32) {
|
||||
block_q5_1 & b = dst_slice[(r - start_row) * row_stride + kt];
|
||||
pack_nibbles_32(b.qs, tile_quants[row]);
|
||||
unpack_4bit_wb_row(b.qs, tile_src, row);
|
||||
const uint32_t qh = unpack_q5_plane(plane_src, row);
|
||||
memcpy(b.qh, &qh, sizeof(qh));
|
||||
b.d = scale_src[2 * row + 0];
|
||||
@@ -706,50 +548,33 @@ static void repack_tiled_q5_1(void * data, const ggml_tensor * t, size_t offset,
|
||||
static void repack_mxfp4_tiled(ggml_tensor * t, const void * data, size_t offset, size_t size) {
|
||||
repack_to_tiled_grid_32<block_mxfp4, 1>(t, data, offset, size, HTP_TENSOR_TILE_SIZE_MXFP4,
|
||||
[](uint8_t * tile_dst, const block_mxfp4 * src_slice, int64_t ne0, int64_t ne1, int64_t row_stride, int ct, int kt) {
|
||||
uint8_t * scale_dst = tile_dst + 512;
|
||||
#if defined(__ARM_NEON) || defined(__aarch64__)
|
||||
if (ct * 32 + 32 <= ne1 && kt < ne0 / 32) {
|
||||
for (int row0 = 0; row0 < 32; row0 += 8) {
|
||||
const uint8_t * qs[8];
|
||||
for (int rr = 0; rr < 8; ++rr) {
|
||||
qs[rr] = src_slice[(ct * 32 + row0 + rr) * row_stride + kt].qs;
|
||||
}
|
||||
repack_neon_4bit_rows(qs, tile_dst, row0);
|
||||
}
|
||||
|
||||
for (int row = 0; row < 32; ++row) {
|
||||
scale_dst[row] = src_slice[(ct * 32 + row) * row_stride + kt].e;
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
uint8_t tile_quants[32][32];
|
||||
uint8_t * scale_dst = tile_dst + 512;
|
||||
const bool valid_kt = kt < ne0 / 32;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r < ne1 && kt < ne0 / 32) {
|
||||
if (r < ne1 && valid_kt) {
|
||||
const block_mxfp4 & b = src_slice[r * row_stride + kt];
|
||||
unpack_nibbles_32(tile_quants[row], b.qs);
|
||||
repack_4bit_wb_row(b.qs, tile_dst, row);
|
||||
scale_dst[row] = b.e;
|
||||
} else {
|
||||
memset(tile_quants[row], 0, 32);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
memset(tile_dst + i * 128 + row * 4, 0, 4);
|
||||
}
|
||||
scale_dst[row] = 0;
|
||||
}
|
||||
}
|
||||
transpose_nibbles_to_tile(tile_dst, tile_quants);
|
||||
});
|
||||
}
|
||||
|
||||
static void repack_tiled_mxfp4(void * data, const ggml_tensor * t, size_t offset, size_t size) {
|
||||
unpack_from_tiled_grid_32<block_mxfp4, 1>(data, t, offset, size, HTP_TENSOR_TILE_SIZE_MXFP4,
|
||||
[](block_mxfp4 * dst_slice, const uint8_t * tile_src, int64_t ne0, int64_t start_row, int64_t end_row, int64_t row_stride, int ct, int kt) {
|
||||
uint8_t tile_quants[32][32];
|
||||
transpose_tile_to_nibbles(tile_quants, tile_src);
|
||||
const uint8_t * scale_src = tile_src + 512;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r >= start_row && r < end_row && kt < ne0 / 32) {
|
||||
block_mxfp4 & b = dst_slice[(r - start_row) * row_stride + kt];
|
||||
pack_nibbles_32(b.qs, tile_quants[row]);
|
||||
unpack_4bit_wb_row(b.qs, tile_src, row);
|
||||
b.e = scale_src[row];
|
||||
}
|
||||
}
|
||||
@@ -760,35 +585,21 @@ static void repack_q8_0_tiled(ggml_tensor * t, const void * data, size_t offset,
|
||||
repack_to_tiled_grid_32<block_q8_0, 1>(t, data, offset, size, HTP_TENSOR_TILE_SIZE_Q8_0,
|
||||
[](uint8_t * tile_dst, const block_q8_0 * src_slice, int64_t ne0, int64_t ne1, int64_t row_stride, int ct, int kt) {
|
||||
ggml_half * scale_dst = (ggml_half *) (tile_dst + 1024);
|
||||
#if defined(__ARM_NEON) || defined(__aarch64__)
|
||||
if (ct * 32 + 32 <= ne1 && kt < ne0 / 32) {
|
||||
for (int row0 = 0; row0 < 32; row0 += 8) {
|
||||
const uint8_t * qs[8];
|
||||
for (int rr = 0; rr < 8; ++rr) {
|
||||
qs[rr] = (const uint8_t *) src_slice[(ct * 32 + row0 + rr) * row_stride + kt].qs;
|
||||
}
|
||||
repack_neon_q8_rows(qs, tile_dst, row0);
|
||||
}
|
||||
|
||||
for (int row = 0; row < 32; ++row) {
|
||||
scale_dst[row] = src_slice[(ct * 32 + row) * row_stride + kt].d;
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
for (int cp = 0; cp < 16; cp++) {
|
||||
const int col0 = cp * 2;
|
||||
const int col1 = col0 + 1;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
const block_q8_0 * b = (r < ne1 && kt < ne0 / 32) ? &src_slice[r * row_stride + kt] : nullptr;
|
||||
tile_dst[cp * 64 + 2 * row + 0] = b ? b->qs[col0] : 0;
|
||||
tile_dst[cp * 64 + 2 * row + 1] = b ? b->qs[col1] : 0;
|
||||
}
|
||||
}
|
||||
const bool valid_kt = kt < ne0 / 32;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
scale_dst[row] = (r < ne1 && kt < ne0 / 32) ? src_slice[r * row_stride + kt].d : 0;
|
||||
if (r < ne1 && valid_kt) {
|
||||
const block_q8_0 & b = src_slice[r * row_stride + kt];
|
||||
for (int p = 0; p < 8; ++p) {
|
||||
memcpy(tile_dst + p * 128 + row * 4, b.qs + p * 4, 4);
|
||||
}
|
||||
scale_dst[row] = b.d;
|
||||
} else {
|
||||
for (int p = 0; p < 8; ++p) {
|
||||
memset(tile_dst + p * 128 + row * 4, 0, 4);
|
||||
}
|
||||
scale_dst[row] = 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -796,23 +607,15 @@ static void repack_q8_0_tiled(ggml_tensor * t, const void * data, size_t offset,
|
||||
static void repack_tiled_q8_0(void * data, const ggml_tensor * t, size_t offset, size_t size) {
|
||||
unpack_from_tiled_grid_32<block_q8_0, 1>(data, t, offset, size, HTP_TENSOR_TILE_SIZE_Q8_0,
|
||||
[](block_q8_0 * dst_slice, const uint8_t * tile_src, int64_t ne0, int64_t start_row, int64_t end_row, int64_t row_stride, int ct, int kt) {
|
||||
for (int cp = 0; cp < 16; cp++) {
|
||||
const int col0 = cp * 2;
|
||||
const int col1 = col0 + 1;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r >= start_row && r < end_row && kt < ne0 / 32) {
|
||||
block_q8_0 & b = dst_slice[(r - start_row) * row_stride + kt];
|
||||
b.qs[col0] = tile_src[cp * 64 + 2 * row + 0];
|
||||
b.qs[col1] = tile_src[cp * 64 + 2 * row + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
const ggml_half * scale_src = (const ggml_half *) (tile_src + 1024);
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = ct * 32 + row;
|
||||
if (r >= start_row && r < end_row && kt < ne0 / 32) {
|
||||
dst_slice[(r - start_row) * row_stride + kt].d = scale_src[row];
|
||||
block_q8_0 & b = dst_slice[(r - start_row) * row_stride + kt];
|
||||
for (int p = 0; p < 8; ++p) {
|
||||
memcpy(b.qs + p * 4, tile_src + p * 128 + row * 4, 4);
|
||||
}
|
||||
b.d = scale_src[row];
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -1414,46 +1217,42 @@ static inline void pack_k4_scales_mins(uint8_t * scales, ggml_half * d, ggml_hal
|
||||
}
|
||||
}
|
||||
|
||||
template <typename BlockT>
|
||||
static inline void repack_k4_quants(uint8_t * tile_dst, const BlockT * b, int row, int kt_local,
|
||||
int64_t sb_per_row, bool use_neon) {
|
||||
static inline void repack_k4_wb_row(const uint8_t * qs_sub, uint8_t * tile_dst, int row, bool high) {
|
||||
#if defined(__ARM_NEON) || defined(__aarch64__)
|
||||
if (use_neon) {
|
||||
if ((row & 7) == 0) {
|
||||
const uint8_t * qs[8];
|
||||
for (int rr = 0; rr < 8; ++rr) {
|
||||
qs[rr] = b[rr * sb_per_row].qs + (kt_local / 2) * 32;
|
||||
}
|
||||
repack_neon_k4_rows(qs, tile_dst, row, (kt_local & 1) != 0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
repack_neon_k4_wb_row(qs_sub, tile_dst, row, high);
|
||||
#else
|
||||
GGML_UNUSED(sb_per_row);
|
||||
GGML_UNUSED(use_neon);
|
||||
const int shift = high ? 4 : 0;
|
||||
for (int p = 0; p < 4; ++p) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
const uint8_t lo = (qs_sub[p * 8 + j] >> shift) & 0x0F;
|
||||
const uint8_t hi = (qs_sub[p * 8 + 4 + j] >> shift) & 0x0F;
|
||||
tile_dst[p * 128 + row * 4 + j] = (uint8_t) ((hi << 4) | lo);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
const uint8_t * qs_sub = b->qs + (kt_local / 2) * 32;
|
||||
const int shift = (kt_local & 1) ? 4 : 0;
|
||||
}
|
||||
|
||||
for (int cp = 0; cp < 16; cp++) {
|
||||
const uint8_t q0 = (qs_sub[2 * cp + 0] >> shift) & 0x0F;
|
||||
const uint8_t q1 = (qs_sub[2 * cp + 1] >> shift) & 0x0F;
|
||||
tile_dst[cp * 32 + row] = (uint8_t) ((q1 << 4) | q0);
|
||||
static inline void unpack_k4_wb_row(uint8_t * qs_sub, const uint8_t * tile_src, int row, bool high) {
|
||||
const int shift = high ? 4 : 0;
|
||||
for (int p = 0; p < 4; ++p) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
const uint8_t val = tile_src[p * 128 + row * 4 + j];
|
||||
qs_sub[p * 8 + j] |= (uint8_t) ((val & 0x0F) << shift);
|
||||
qs_sub[p * 8 + 4 + j] |= (uint8_t) ((val >> 4) << shift);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void repack_q4_K_tiled(ggml_tensor * t, const void * data, size_t offset, size_t size) {
|
||||
const int64_t sb_per_row = t->ne[0] / QK_K;
|
||||
const bool use_neon = (t->ne[1] % 8 == 0);
|
||||
repack_kquant_to_tiled_grid<block_q4_K>(t, data, offset, size, HTP_TENSOR_TILE_SIZE_Q4_K,
|
||||
[sb_per_row, use_neon](uint8_t * tile_dst, const block_q4_K * b, int row, int kt_local) {
|
||||
[](uint8_t * tile_dst, const block_q4_K * b, int row, int kt_local) {
|
||||
const float d = GGML_FP16_TO_FP32(b->d);
|
||||
const float dmin = GGML_FP16_TO_FP32(b->dmin);
|
||||
|
||||
uint8_t sc, m;
|
||||
get_scale_min_k4(kt_local, b->scales, &sc, &m);
|
||||
|
||||
repack_k4_quants(tile_dst, b, row, kt_local, sb_per_row, use_neon);
|
||||
repack_k4_wb_row(b->qs + (kt_local / 2) * 32, tile_dst, row, (kt_local & 1) != 0);
|
||||
|
||||
ggml_half * scale_dst = (ggml_half *) (tile_dst + 512);
|
||||
scale_dst[2 * row + 0] = GGML_FP32_TO_FP16(d * (float) sc);
|
||||
@@ -1471,14 +1270,7 @@ static void repack_tiled_q4_K(void * data, const ggml_tensor * t, size_t offset,
|
||||
const uint8_t * tile_src = sb_tiles + kt_local * tile_size;
|
||||
const ggml_half * scale_src = (const ggml_half *) (tile_src + 512);
|
||||
|
||||
uint8_t * qs_sub = b->qs + (kt_local / 2) * 32;
|
||||
const int shift = (kt_local & 1) ? 4 : 0;
|
||||
|
||||
for (int cp = 0; cp < 16; cp++) {
|
||||
const uint8_t val = tile_src[cp * 32 + row];
|
||||
qs_sub[2 * cp + 0] |= (uint8_t) ((val & 0x0F) << shift);
|
||||
qs_sub[2 * cp + 1] |= (uint8_t) ((val >> 4) << shift);
|
||||
}
|
||||
unpack_k4_wb_row(b->qs + (kt_local / 2) * 32, tile_src, row, (kt_local & 1) != 0);
|
||||
|
||||
const float D = GGML_FP16_TO_FP32(scale_src[2 * row + 0]);
|
||||
const float M = GGML_FP16_TO_FP32(scale_src[2 * row + 1]);
|
||||
@@ -1491,10 +1283,8 @@ static void repack_tiled_q4_K(void * data, const ggml_tensor * t, size_t offset,
|
||||
}
|
||||
|
||||
static void repack_q5_K_tiled(ggml_tensor * t, const void * data, size_t offset, size_t size) {
|
||||
const int64_t sb_per_row = t->ne[0] / QK_K;
|
||||
const bool use_neon = (t->ne[1] % 8 == 0);
|
||||
repack_kquant_to_tiled_grid<block_q5_K>(t, data, offset, size, HTP_TENSOR_TILE_SIZE_Q5_K,
|
||||
[sb_per_row, use_neon](uint8_t * tile_dst, const block_q5_K * b, int row, int kt_local) {
|
||||
[](uint8_t * tile_dst, const block_q5_K * b, int row, int kt_local) {
|
||||
const float d = GGML_FP16_TO_FP32(b->d);
|
||||
const float dmin = GGML_FP16_TO_FP32(b->dmin);
|
||||
uint8_t * plane = tile_dst + 640;
|
||||
@@ -1502,21 +1292,20 @@ static void repack_q5_K_tiled(ggml_tensor * t, const void * data, size_t offset,
|
||||
uint8_t sc, m;
|
||||
get_scale_min_k4(kt_local, b->scales, &sc, &m);
|
||||
|
||||
repack_k4_quants(tile_dst, b, row, kt_local, sb_per_row, use_neon);
|
||||
repack_k4_wb_row(b->qs + (kt_local / 2) * 32, tile_dst, row, (kt_local & 1) != 0);
|
||||
|
||||
const uint8_t hbit = (uint8_t) (1 << kt_local);
|
||||
#if defined(__ARM_NEON) || defined(__aarch64__)
|
||||
repack_neon_q5_k_qh(b->qh, hbit, plane, row);
|
||||
#else
|
||||
for (int cp = 0; cp < 16; cp++) {
|
||||
const int i = cp / 4;
|
||||
const int lane = (cp % 4) * 32 + row;
|
||||
if (b->qh[2 * cp + 0] & hbit) {
|
||||
plane[lane] |= (uint8_t) (1 << (2 * i));
|
||||
}
|
||||
if (b->qh[2 * cp + 1] & hbit) {
|
||||
plane[lane] |= (uint8_t) (1 << (2 * i + 1));
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
uint8_t v = 0;
|
||||
for (int g = 0; g < 8; ++g) {
|
||||
if (b->qh[4 * g + j] & hbit) {
|
||||
v |= (uint8_t) (1u << g);
|
||||
}
|
||||
}
|
||||
plane[row * 4 + j] = v;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1537,22 +1326,15 @@ static void repack_tiled_q5_K(void * data, const ggml_tensor * t, size_t offset,
|
||||
const uint8_t * plane = tile_src + 640;
|
||||
const ggml_half * scale_src = (const ggml_half *) (tile_src + 512);
|
||||
|
||||
uint8_t * qs_sub = b->qs + (kt_local / 2) * 32;
|
||||
const int shift = (kt_local & 1) ? 4 : 0;
|
||||
unpack_k4_wb_row(b->qs + (kt_local / 2) * 32, tile_src, row, (kt_local & 1) != 0);
|
||||
|
||||
const uint8_t hbit = (uint8_t) (1 << kt_local);
|
||||
|
||||
for (int cp = 0; cp < 16; cp++) {
|
||||
const uint8_t val = tile_src[cp * 32 + row];
|
||||
qs_sub[2 * cp + 0] |= (uint8_t) ((val & 0x0F) << shift);
|
||||
qs_sub[2 * cp + 1] |= (uint8_t) ((val >> 4) << shift);
|
||||
|
||||
const int i = cp / 4;
|
||||
const int lane = (cp % 4) * 32 + row;
|
||||
if (plane[lane] & (1 << (2 * i))) {
|
||||
b->qh[2 * cp + 0] |= hbit;
|
||||
}
|
||||
if (plane[lane] & (1 << (2 * i + 1))) {
|
||||
b->qh[2 * cp + 1] |= hbit;
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
const uint8_t v = plane[row * 4 + j];
|
||||
for (int g = 0; g < 8; ++g) {
|
||||
if (v & (1u << g)) {
|
||||
b->qh[4 * g + j] |= hbit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1714,25 +1496,32 @@ static void repack_tiled_iq2_xxs(void * data, const ggml_tensor * t, size_t offs
|
||||
});
|
||||
}
|
||||
|
||||
// --- NVFP4 (576 B tile: Q4_0-style nibble transpose + 2 UE4M3 scales per row, one block-64 = 2 k-tiles) ---
|
||||
// --- NVFP4 (576 B tile: 4-K-grouped wb nibbles + 2 UE4M3 scales per row, one block-64 = 2 k-tiles) ---
|
||||
|
||||
#define NVFP4_TILE_NIBBLE_BYTES 512
|
||||
#define NVFP4_KT_PER_BLOCK (QK_NVFP4 / 32)
|
||||
#define NVFP4_SUB_PER_KT (32 / QK_NVFP4_SUB)
|
||||
|
||||
// element e (0..31) of k-tile kt_half lives in sub-block 2*kt_half + e/16, low nibble if (e%16) < 8
|
||||
static inline uint8_t nvfp4_get_nibble(const block_nvfp4 & b, int kt_half, int e) {
|
||||
const int sub = kt_half * NVFP4_SUB_PER_KT + e / QK_NVFP4_SUB;
|
||||
const int j = e % (QK_NVFP4_SUB / 2);
|
||||
const uint8_t q = b.qs[sub * (QK_NVFP4_SUB / 2) + j];
|
||||
return (e % QK_NVFP4_SUB) < QK_NVFP4_SUB / 2 ? (q & 0x0F) : (q >> 4);
|
||||
static inline void repack_nvfp4_wb_row(const uint8_t * qs, uint8_t * tile_dst, int row) {
|
||||
for (int u = 0; u < NVFP4_SUB_PER_KT; ++u) {
|
||||
const uint8_t * qs_sub = qs + u * (QK_NVFP4_SUB / 2);
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
tile_dst[(2 * u + 0) * 128 + row * 4 + j] = (uint8_t) (((qs_sub[4 + j] & 0x0F) << 4) | (qs_sub[j] & 0x0F));
|
||||
tile_dst[(2 * u + 1) * 128 + row * 4 + j] = (uint8_t) ((qs_sub[4 + j] & 0xF0) | (qs_sub[j] >> 4));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void nvfp4_set_nibble(block_nvfp4 & b, int kt_half, int e, uint8_t v) {
|
||||
const int sub = kt_half * NVFP4_SUB_PER_KT + e / QK_NVFP4_SUB;
|
||||
const int j = e % (QK_NVFP4_SUB / 2);
|
||||
uint8_t & q = b.qs[sub * (QK_NVFP4_SUB / 2) + j];
|
||||
q |= (e % QK_NVFP4_SUB) < QK_NVFP4_SUB / 2 ? v : (uint8_t) (v << 4);
|
||||
static inline void unpack_nvfp4_wb_row(uint8_t * qs, const uint8_t * tile_src, int row) {
|
||||
for (int u = 0; u < NVFP4_SUB_PER_KT; ++u) {
|
||||
uint8_t * qs_sub = qs + u * (QK_NVFP4_SUB / 2);
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
const uint8_t p0 = tile_src[(2 * u + 0) * 128 + row * 4 + j];
|
||||
const uint8_t p1 = tile_src[(2 * u + 1) * 128 + row * 4 + j];
|
||||
qs_sub[j] = (uint8_t) ((p0 & 0x0F) | ((p1 & 0x0F) << 4));
|
||||
qs_sub[4 + j] = (uint8_t) ((p0 >> 4) | (p1 & 0xF0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void repack_nvfp4_tiled(ggml_tensor * t, const void * data, size_t offset, size_t size) {
|
||||
@@ -1752,7 +1541,7 @@ static void repack_nvfp4_tiled(ggml_tensor * t, const void * data, size_t offset
|
||||
uint8_t * dst_slice = (uint8_t *) t->data + slice * matrix_size;
|
||||
|
||||
repack_parallel_for_ct(matrix_size, (int) n_col_tiles, [&](int ct0, int ct1) {
|
||||
// Padding rows and OR-ed nibbles require zeroed tiles
|
||||
// Padding rows require zeroed tiles
|
||||
memset(dst_slice + (size_t) ct0 * ct_bytes, 0, (size_t) (ct1 - ct0) * ct_bytes);
|
||||
|
||||
const int64_t r0 = (int64_t) ct0 * 32;
|
||||
@@ -1764,9 +1553,7 @@ static void repack_nvfp4_tiled(ggml_tensor * t, const void * data, size_t offset
|
||||
const block_nvfp4 & b = src_slice[r * blocks_per_row + blk];
|
||||
for (int h = 0; h < NVFP4_KT_PER_BLOCK; ++h) {
|
||||
uint8_t * tile = dst_slice + (ct * n_k_tiles + blk * NVFP4_KT_PER_BLOCK + h) * HTP_TENSOR_TILE_SIZE_NVFP4;
|
||||
for (int e = 0; e < 32; ++e) {
|
||||
tile[(e / 2) * 32 + row] |= (uint8_t) (nvfp4_get_nibble(b, h, e) << (4 * (e & 1)));
|
||||
}
|
||||
repack_nvfp4_wb_row(b.qs + h * 16, tile, row);
|
||||
for (int u = 0; u < NVFP4_SUB_PER_KT; ++u) {
|
||||
tile[NVFP4_TILE_NIBBLE_BYTES + u * 32 + row] = b.d[h * NVFP4_SUB_PER_KT + u];
|
||||
}
|
||||
@@ -1802,9 +1589,7 @@ static void repack_tiled_nvfp4(void * data, const ggml_tensor * t, size_t offset
|
||||
for (int h = 0; h < NVFP4_KT_PER_BLOCK; ++h) {
|
||||
const uint8_t * tile = (const uint8_t *) t->data + slice * matrix_size +
|
||||
(ct * n_k_tiles + blk * NVFP4_KT_PER_BLOCK + h) * HTP_TENSOR_TILE_SIZE_NVFP4;
|
||||
for (int e = 0; e < 32; ++e) {
|
||||
nvfp4_set_nibble(b, h, e, (tile[(e / 2) * 32 + row] >> (4 * (e & 1))) & 0x0F);
|
||||
}
|
||||
unpack_nvfp4_wb_row(b.qs + h * 16, tile, row);
|
||||
for (int u = 0; u < NVFP4_SUB_PER_KT; ++u) {
|
||||
b.d[h * NVFP4_SUB_PER_KT + u] = tile[NVFP4_TILE_NIBBLE_BYTES + u * 32 + row];
|
||||
}
|
||||
|
||||
@@ -19,8 +19,9 @@ add_library(${HTP_LIB} SHARED
|
||||
htp_iface_skel.c
|
||||
work-queue.c
|
||||
dma-queue.c
|
||||
dma-proxy.c
|
||||
hmx-queue.c
|
||||
htp-dcache.c
|
||||
htp-tensor.c
|
||||
matmul-ops.c
|
||||
flash-attn-ops.c
|
||||
gated-delta-net-ops.c
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
#include <HAP_perf.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
+1029
-380
File diff suppressed because it is too large
Load Diff
@@ -25,6 +25,9 @@ struct htp_sort_kernel_params {
|
||||
struct htp_sort_vtcm_layout {
|
||||
size_t total_bytes;
|
||||
size_t phase1_slot_size;
|
||||
size_t row_values_size;
|
||||
size_t row_indices_size;
|
||||
size_t off_cand;
|
||||
size_t off_phase1;
|
||||
size_t off_merge_values;
|
||||
size_t off_merge_indices;
|
||||
@@ -33,16 +36,20 @@ struct htp_sort_vtcm_layout {
|
||||
uint32_t merge_elems;
|
||||
uint32_t n_hvx;
|
||||
uint32_t n_slots;
|
||||
uint32_t batch_rows;
|
||||
};
|
||||
|
||||
static inline void htp_sort_vtcm_layout_build(
|
||||
struct htp_sort_vtcm_layout * L,
|
||||
uint32_t ne00,
|
||||
uint32_t total_rows,
|
||||
uint32_t chunk_elems,
|
||||
uint32_t n_chunks,
|
||||
uint32_t merge_elems,
|
||||
uint32_t n_hvx,
|
||||
uint32_t n_slots) {
|
||||
uint32_t n_slots,
|
||||
uint32_t k,
|
||||
bool is_top_k) {
|
||||
|
||||
memset(L, 0, sizeof(*L));
|
||||
L->chunk_elems = chunk_elems;
|
||||
@@ -51,19 +58,65 @@ static inline void htp_sort_vtcm_layout_build(
|
||||
L->n_hvx = n_hvx;
|
||||
L->n_slots = n_slots;
|
||||
|
||||
const size_t values_size = ((chunk_elems * sizeof(float)) + 127) & ~127;
|
||||
const size_t indices_size = ((chunk_elems * sizeof(int32_t)) + 127) & ~127;
|
||||
L->phase1_slot_size = ((values_size + indices_size) + 255) & ~255;
|
||||
if (is_top_k && k >= ne00) {
|
||||
const size_t idx_size = ((k * sizeof(int32_t)) + 127u) & ~(size_t) 127u;
|
||||
uint32_t batch_rows = 1u;
|
||||
if ((idx_size << 4) <= 65536u) {
|
||||
batch_rows = 16u;
|
||||
} else if ((idx_size << 2) <= 65536u) {
|
||||
batch_rows = 4u;
|
||||
}
|
||||
if (batch_rows > total_rows && total_rows > 0u) {
|
||||
batch_rows = total_rows;
|
||||
}
|
||||
L->row_indices_size = idx_size;
|
||||
L->batch_rows = batch_rows;
|
||||
L->phase1_slot_size = ((idx_size * batch_rows) + 255u) & ~(size_t) 255u;
|
||||
|
||||
size_t off = 0;
|
||||
VTCM_LAYOUT_ALLOC(off, off_phase1, L->phase1_slot_size);
|
||||
L->total_bytes = off;
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t indices_elems = chunk_elems;
|
||||
if (is_top_k) {
|
||||
if (total_rows == 1u && n_chunks > 1u) {
|
||||
indices_elems = 0u;
|
||||
} else if (k < chunk_elems) {
|
||||
indices_elems = k;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t batch_rows = 1u;
|
||||
if (total_rows > 1u && chunk_elems <= 256u) {
|
||||
if (total_rows > 7u * n_hvx) {
|
||||
batch_rows = 8u;
|
||||
} else if (total_rows > 3u * n_hvx) {
|
||||
batch_rows = 4u;
|
||||
}
|
||||
}
|
||||
|
||||
const size_t row_values_size = ((chunk_elems * sizeof(float)) + 127u) & ~(size_t) 127u;
|
||||
const size_t row_indices_size = ((indices_elems * sizeof(int32_t)) + 127u) & ~(size_t) 127u;
|
||||
const size_t off_cand = (row_values_size + row_indices_size) * batch_rows;
|
||||
const size_t cand_size = (is_top_k && chunk_elems > 256u) ? 512u : 0u;
|
||||
|
||||
L->row_values_size = row_values_size;
|
||||
L->row_indices_size = row_indices_size;
|
||||
L->off_cand = off_cand;
|
||||
L->batch_rows = batch_rows;
|
||||
L->phase1_slot_size = ((off_cand + cand_size) + 255u) & ~(size_t) 255u;
|
||||
|
||||
size_t off = 0;
|
||||
if (total_rows > 1) {
|
||||
if (total_rows > 1u) {
|
||||
VTCM_LAYOUT_ALLOC(off, off_phase1, L->phase1_slot_size * n_slots * n_hvx);
|
||||
} else {
|
||||
VTCM_LAYOUT_ALLOC(off, off_phase1, L->phase1_slot_size * n_chunks * n_slots);
|
||||
const size_t merge_values_size = ((merge_elems * sizeof(float)) + 127) & ~127;
|
||||
const size_t merge_indices_size = ((merge_elems * sizeof(int32_t)) + 127) & ~127;
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_merge_values, merge_values_size, n_chunks > 1);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_merge_indices, merge_indices_size, n_chunks > 1);
|
||||
const size_t merge_values_size = ((merge_elems * sizeof(float)) + 127u) & ~(size_t) 127u;
|
||||
const size_t merge_indices_size = ((merge_elems * sizeof(int32_t)) + 127u) & ~(size_t) 127u;
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_merge_values, merge_values_size, n_chunks > 1u);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_merge_indices, merge_indices_size, n_chunks > 1u);
|
||||
}
|
||||
L->total_bytes = off;
|
||||
}
|
||||
@@ -79,79 +132,111 @@ static inline bool htp_sort_solve_layout(
|
||||
|
||||
memset(layout, 0, sizeof(*layout));
|
||||
|
||||
if (total_rows > 1) {
|
||||
uint32_t n_hvx = total_rows < n_hvx_max ? total_rows : n_hvx_max;
|
||||
|
||||
uint32_t n_vec = (ne00 + 31) / 32;
|
||||
uint32_t n_vec_pow2 = 1;
|
||||
while (n_vec_pow2 < n_vec) n_vec_pow2 <<= 1;
|
||||
uint32_t ne00_padded = n_vec_pow2 * 32;
|
||||
|
||||
size_t values_size = ((ne00_padded * sizeof(float)) + 127) & ~127;
|
||||
size_t indices_size = ((ne00_padded * sizeof(int32_t)) + 127) & ~127;
|
||||
size_t spad_per_slot = ((values_size + indices_size) + 255) & ~255;
|
||||
|
||||
uint32_t n_slots = 2;
|
||||
if (spad_per_slot * 2 > vtcm_budget) {
|
||||
n_slots = 1;
|
||||
if (is_top_k && k >= ne00) {
|
||||
htp_sort_vtcm_layout_build(layout, ne00, total_rows, ne00, 1, 0, 1, 1, k, true);
|
||||
if (layout->total_bytes > vtcm_budget) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t spad_per_hvx = spad_per_slot * n_slots;
|
||||
|
||||
while (n_hvx > 1 && (spad_per_hvx * n_hvx) > vtcm_budget) {
|
||||
n_hvx--;
|
||||
uint32_t n_hvx = 1u;
|
||||
if (total_rows > 1u && (size_t) total_rows * k * sizeof(int32_t) > 16384u) {
|
||||
const uint32_t b = layout->batch_rows > 0u ? layout->batch_rows : 1u;
|
||||
const uint32_t max_w = (total_rows + b - 1u) / b;
|
||||
n_hvx = max_w < n_hvx_max ? max_w : n_hvx_max;
|
||||
}
|
||||
|
||||
htp_sort_vtcm_layout_build(layout, total_rows, ne00_padded, 1, 0, n_hvx, n_slots);
|
||||
htp_sort_vtcm_layout_build(layout, ne00, total_rows, ne00, 1, 0, n_hvx, 1, k, true);
|
||||
return layout->total_bytes <= vtcm_budget;
|
||||
}
|
||||
|
||||
uint32_t n_vec = (ne00 + 31) / 32;
|
||||
uint32_t n_vec_pow2 = 1;
|
||||
while (n_vec_pow2 < n_vec) n_vec_pow2 <<= 1;
|
||||
|
||||
uint32_t n_chunks = 1;
|
||||
if (ne00 > 1024) {
|
||||
while (n_chunks * 2 <= n_hvx_max && n_chunks * 2 <= n_vec_pow2) {
|
||||
n_chunks *= 2;
|
||||
if (total_rows > 1u) {
|
||||
uint32_t n_hvx = total_rows < n_hvx_max ? total_rows : n_hvx_max;
|
||||
if (ne00 <= 128u) {
|
||||
const uint32_t cap = (total_rows >= 4u) ? ((total_rows + 3u) >> 2) : 1u;
|
||||
if (n_hvx > cap) {
|
||||
n_hvx = cap;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t chunk_n_vec = n_vec_pow2 / n_chunks;
|
||||
uint32_t chunk_elems = chunk_n_vec * 32;
|
||||
uint32_t merge_elems = 0;
|
||||
|
||||
if (n_chunks > 1) {
|
||||
if (is_top_k) {
|
||||
uint32_t local_k = k < chunk_elems ? k : chunk_elems;
|
||||
uint32_t total_candidates = n_chunks * local_k;
|
||||
uint32_t merge_n_vec = (total_candidates + 31) / 32;
|
||||
uint32_t merge_n_vec_pow2 = 1;
|
||||
while (merge_n_vec_pow2 < merge_n_vec) merge_n_vec_pow2 <<= 1;
|
||||
merge_elems = merge_n_vec_pow2 * 32;
|
||||
uint32_t ne00_padded = 0u;
|
||||
if (is_top_k && ne00 > 256u) {
|
||||
ne00_padded = (ne00 + 31u) & ~31u;
|
||||
} else {
|
||||
merge_elems = n_vec_pow2 * 32;
|
||||
const uint32_t n_vec = (ne00 + 31u) >> 5;
|
||||
uint32_t n_vec_pow2 = 1u;
|
||||
while (n_vec_pow2 < n_vec) {
|
||||
n_vec_pow2 <<= 1;
|
||||
}
|
||||
ne00_padded = n_vec_pow2 * 32u;
|
||||
}
|
||||
|
||||
uint32_t n_slots = 2u;
|
||||
htp_sort_vtcm_layout_build(layout, ne00, total_rows, ne00_padded, 1, 0, 1, n_slots, k, is_top_k);
|
||||
if (layout->total_bytes > vtcm_budget) {
|
||||
n_slots = 1u;
|
||||
}
|
||||
|
||||
htp_sort_vtcm_layout_build(layout, ne00, total_rows, ne00_padded, 1, 0, n_hvx, n_slots, k, is_top_k);
|
||||
while (n_hvx > 1u && layout->total_bytes > vtcm_budget) {
|
||||
n_hvx--;
|
||||
htp_sort_vtcm_layout_build(layout, ne00, total_rows, ne00_padded, 1, 0, n_hvx, n_slots, k, is_top_k);
|
||||
}
|
||||
if (layout->total_bytes > vtcm_budget && n_slots > 1u) {
|
||||
n_slots = 1u;
|
||||
htp_sort_vtcm_layout_build(layout, ne00, total_rows, ne00_padded, 1, 0, n_hvx, n_slots, k, is_top_k);
|
||||
}
|
||||
return layout->total_bytes <= vtcm_budget;
|
||||
}
|
||||
|
||||
const uint32_t n_vec = (ne00 + 31u) >> 5;
|
||||
uint32_t n_vec_pow2 = 1u;
|
||||
while (n_vec_pow2 < n_vec) {
|
||||
n_vec_pow2 <<= 1;
|
||||
}
|
||||
|
||||
if (is_top_k) {
|
||||
uint32_t n_chunks = 1u;
|
||||
if (ne00 > 2048u) {
|
||||
while (n_chunks * 2u <= n_hvx_max && (n_chunks * 2u) * 32u <= n_vec) {
|
||||
n_chunks *= 2u;
|
||||
}
|
||||
}
|
||||
|
||||
while (n_chunks > 1u) {
|
||||
const uint32_t chunk_n_vec = (n_vec + n_chunks - 1u) / n_chunks;
|
||||
const uint32_t chunk_elems = chunk_n_vec * 32u;
|
||||
const uint32_t local_k = k < chunk_elems ? k : chunk_elems;
|
||||
const uint32_t merge_elems = ((n_chunks * local_k) + 31u) & ~31u;
|
||||
|
||||
htp_sort_vtcm_layout_build(layout, ne00, 1, chunk_elems, n_chunks, merge_elems, n_chunks, 1, k, true);
|
||||
if (layout->total_bytes <= vtcm_budget) {
|
||||
return true;
|
||||
}
|
||||
n_chunks >>= 1;
|
||||
}
|
||||
|
||||
const uint32_t chunk_elems = (ne00 > 256u) ? (n_vec * 32u) : (n_vec_pow2 * 32u);
|
||||
htp_sort_vtcm_layout_build(layout, ne00, 1, chunk_elems, 1, 0, 1, 1, k, true);
|
||||
return layout->total_bytes <= vtcm_budget;
|
||||
}
|
||||
|
||||
uint32_t n_chunks = 1u;
|
||||
if (ne00 > 1024u) {
|
||||
while (n_chunks * 2u <= n_hvx_max && n_chunks * 2u <= n_vec_pow2) {
|
||||
n_chunks *= 2u;
|
||||
}
|
||||
}
|
||||
|
||||
htp_sort_vtcm_layout_build(layout, 1, chunk_elems, n_chunks, merge_elems, n_chunks, 1);
|
||||
|
||||
if (layout->total_bytes > vtcm_budget && n_chunks > 1) {
|
||||
n_chunks = 1;
|
||||
chunk_elems = n_vec_pow2 * 32;
|
||||
merge_elems = 0;
|
||||
htp_sort_vtcm_layout_build(layout, 1, chunk_elems, n_chunks, merge_elems, 1, 1);
|
||||
while (n_chunks > 1u) {
|
||||
const uint32_t chunk_elems = (n_vec_pow2 / n_chunks) * 32u;
|
||||
const uint32_t merge_elems = n_vec_pow2 * 32u;
|
||||
htp_sort_vtcm_layout_build(layout, ne00, 1, chunk_elems, n_chunks, merge_elems, n_chunks, 1, k, false);
|
||||
if (layout->total_bytes <= vtcm_budget) {
|
||||
return true;
|
||||
}
|
||||
n_chunks >>= 1;
|
||||
}
|
||||
|
||||
if (layout->total_bytes > vtcm_budget) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (n_chunks == 1 && layout->phase1_slot_size * 2 <= vtcm_budget) {
|
||||
htp_sort_vtcm_layout_build(layout, 1, chunk_elems, 1, 0, 1, 2);
|
||||
}
|
||||
|
||||
return true;
|
||||
htp_sort_vtcm_layout_build(layout, ne00, 1, n_vec_pow2 * 32u, 1, 0, 1, 1, k, false);
|
||||
return layout->total_bytes <= vtcm_budget;
|
||||
}
|
||||
|
||||
#if defined(__cplusplus)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -28,7 +28,11 @@ struct htp_binary_kernel_params {
|
||||
|
||||
uint8_t n_srcs;
|
||||
uint8_t is_scalar;
|
||||
uint8_t pad[2];
|
||||
uint8_t src0_elem_size;
|
||||
uint8_t src1_elem_size;
|
||||
|
||||
uint8_t dst_elem_size;
|
||||
uint8_t pad[3];
|
||||
|
||||
uint32_t n_hvx;
|
||||
uint32_t rows_per_buffer;
|
||||
@@ -61,16 +65,18 @@ static inline bool htp_binary_strides_safe(
|
||||
const struct ggml_tensor * src0,
|
||||
const struct ggml_tensor * src1,
|
||||
const struct ggml_tensor * dst,
|
||||
size_t elem_size
|
||||
size_t src0_elem_size,
|
||||
size_t src1_elem_size,
|
||||
size_t dst_elem_size
|
||||
) {
|
||||
if ((src0->ne[0] != 1 && src0->nb[0] != elem_size) ||
|
||||
(src1->ne[0] != 1 && src1->nb[0] != elem_size) ||
|
||||
(dst->ne[0] != 1 && dst->nb[0] != elem_size)) {
|
||||
(void) src1;
|
||||
(void) src1_elem_size;
|
||||
if ((src0->ne[0] != 1 && src0->nb[0] != src0_elem_size) ||
|
||||
(dst->ne[0] != 1 && dst->nb[0] != dst_elem_size)) {
|
||||
return false;
|
||||
}
|
||||
if ((src0->ne[1] > 1 && src0->nb[1] < src0->ne[0] * elem_size) ||
|
||||
(src1->ne[1] > 1 && src1->nb[1] < src1->ne[0] * elem_size) ||
|
||||
(dst->ne[1] > 1 && dst->nb[1] < dst->ne[0] * elem_size)) {
|
||||
if ((src0->ne[1] > 1 && src0->nb[1] < src0->ne[0] * src0_elem_size) ||
|
||||
(dst->ne[1] > 1 && dst->nb[1] < dst->ne[0] * dst_elem_size)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -114,18 +120,20 @@ static inline void htp_binary_vtcm_layout_build(
|
||||
const uint32_t n_srcs = kparams->n_srcs > 2 ? kparams->n_srcs : 2;
|
||||
|
||||
if (kparams->kernel_type == HTP_BINARY_KERNEL_CHUNKED) {
|
||||
const size_t chunk_bytes = kparams->chunk_bytes;
|
||||
if (chunk_bytes == 0) {
|
||||
const size_t src0_chunk_bytes = hex_round_up((size_t) kparams->chunk_size * kparams->src0_elem_size, 128);
|
||||
const size_t src1_chunk_bytes = hex_round_up((size_t) kparams->chunk_size * kparams->src1_elem_size, 128);
|
||||
const size_t dst_chunk_bytes = hex_round_up((size_t) kparams->chunk_size * kparams->dst_elem_size, 128);
|
||||
if (src0_chunk_bytes == 0 || src1_chunk_bytes == 0 || dst_chunk_bytes == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
L->src0_bytes_per_hvx = 2 * chunk_bytes;
|
||||
L->src1_bytes_per_hvx = kparams->is_scalar ? 0 : (2 * (n_srcs - 1) * chunk_bytes);
|
||||
L->dst_bytes_per_hvx = 2 * chunk_bytes;
|
||||
L->src0_bytes_per_hvx = 2 * src0_chunk_bytes;
|
||||
L->src1_bytes_per_hvx = kparams->is_scalar ? 0 : (2 * (n_srcs - 1) * src1_chunk_bytes);
|
||||
L->dst_bytes_per_hvx = 2 * dst_chunk_bytes;
|
||||
|
||||
L->src0_spad_half_size = chunk_bytes;
|
||||
L->src1_spad_half_size = kparams->is_scalar ? 0 : chunk_bytes;
|
||||
L->dst_spad_half_size = chunk_bytes;
|
||||
L->src0_spad_half_size = src0_chunk_bytes;
|
||||
L->src1_spad_half_size = kparams->is_scalar ? 0 : src1_chunk_bytes;
|
||||
L->dst_spad_half_size = dst_chunk_bytes;
|
||||
|
||||
L->rows_per_buffer = 1;
|
||||
L->src1_size = 0;
|
||||
@@ -199,13 +207,18 @@ static inline bool htp_binary_solve_chunked(
|
||||
struct htp_binary_vtcm_layout * L,
|
||||
struct htp_binary_kernel_params * kparams,
|
||||
uint32_t total_elems,
|
||||
uint32_t elem_size,
|
||||
uint32_t src0_elem_size,
|
||||
uint32_t src1_elem_size,
|
||||
uint32_t dst_elem_size,
|
||||
uint32_t max_hvx,
|
||||
uint32_t n_srcs,
|
||||
bool is_scalar,
|
||||
size_t vtcm_size
|
||||
) {
|
||||
const size_t n_bufs = is_scalar ? 2 : (size_t) n_srcs + 1;
|
||||
const uint32_t elem_size = src0_elem_size > src1_elem_size ?
|
||||
(src0_elem_size > dst_elem_size ? src0_elem_size : dst_elem_size) :
|
||||
(src1_elem_size > dst_elem_size ? src1_elem_size : dst_elem_size);
|
||||
const size_t scalar_bytes = is_scalar ? 128 : 0;
|
||||
const size_t budget_per_buf = (max_hvx > 0 && vtcm_size > scalar_bytes)
|
||||
? hex_align_down((vtcm_size - scalar_bytes) / (2 * max_hvx * n_bufs), 128)
|
||||
@@ -230,6 +243,9 @@ static inline bool htp_binary_solve_chunked(
|
||||
kparams->n_srcs = (uint8_t) n_srcs;
|
||||
kparams->rows_per_buffer = 1;
|
||||
kparams->src1_row_size_aligned = is_scalar ? 0 : kparams->src0_row_size_aligned;
|
||||
kparams->src0_elem_size = (uint8_t) src0_elem_size;
|
||||
kparams->src1_elem_size = (uint8_t) src1_elem_size;
|
||||
kparams->dst_elem_size = (uint8_t) dst_elem_size;
|
||||
kparams->src1_size = 0;
|
||||
kparams->chunk_size = chunk_size;
|
||||
kparams->chunk_bytes = chunk_bytes;
|
||||
|
||||
+1009
-199
File diff suppressed because it is too large
Load Diff
@@ -14,6 +14,15 @@ enum htp_concat_kernel_type {
|
||||
HTP_CONCAT_KERNEL_GATHER_ROWS = 3,
|
||||
};
|
||||
|
||||
#define HTP_CONCAT_REG_BLK_BYTES (32 * 1024)
|
||||
|
||||
struct htp_concat_regular_params {
|
||||
struct fastdiv_values div_ne01;
|
||||
struct fastdiv_values div_ne02_ne01;
|
||||
struct fastdiv_values div_ne11;
|
||||
struct fastdiv_values div_ne12_ne11;
|
||||
};
|
||||
|
||||
struct htp_concat_transposed_params {
|
||||
struct fastdiv_values div_ne2;
|
||||
};
|
||||
@@ -27,6 +36,7 @@ struct htp_concat_kernel_params {
|
||||
uint32_t vtcm_size;
|
||||
|
||||
union {
|
||||
struct htp_concat_regular_params regular;
|
||||
struct htp_concat_transposed_params transposed;
|
||||
struct htp_gather_rows_params gather;
|
||||
} u;
|
||||
@@ -38,11 +48,198 @@ static_assert(sizeof(struct htp_concat_kernel_params) <= 128, "htp_concat_kernel
|
||||
_Static_assert(sizeof(struct htp_concat_kernel_params) <= 128, "htp_concat_kernel_params is too large for kernel_params blob");
|
||||
#endif
|
||||
|
||||
struct htp_concat_gather_rows_vtcm_layout {
|
||||
size_t off_tab;
|
||||
size_t off_inc;
|
||||
size_t off_cur;
|
||||
size_t off_region0;
|
||||
size_t off_out0;
|
||||
size_t off_region1;
|
||||
size_t off_out1;
|
||||
size_t off_raw_a0;
|
||||
size_t off_raw_a1;
|
||||
size_t off_raw_b0;
|
||||
size_t off_raw_b1;
|
||||
size_t total_per_hvx;
|
||||
};
|
||||
|
||||
static inline void htp_concat_gather_rows_vtcm_layout_build(
|
||||
struct htp_concat_gather_rows_vtcm_layout * L,
|
||||
uint32_t vg,
|
||||
uint32_t region_size,
|
||||
uint32_t nr_max,
|
||||
uint32_t na, uint32_t a_nb1,
|
||||
uint32_t nb, uint32_t b_nb1, bool b_dense, uint32_t span1,
|
||||
uint32_t ne,
|
||||
uint32_t src0_es, uint32_t src1_es, uint32_t dst_es) {
|
||||
|
||||
const size_t vec_bytes = (size_t) vg * 128;
|
||||
const size_t region_aligned = hex_round_up(region_size, 128);
|
||||
const size_t out_aligned = hex_round_up(region_aligned + (size_t) nr_max * ne * dst_es, 128) - region_aligned;
|
||||
|
||||
size_t raw_a_aligned = 0;
|
||||
if (src0_es != dst_es && na > 0) {
|
||||
raw_a_aligned = hex_round_up((size_t) (nr_max - 1) * a_nb1 + (size_t) na * src0_es, 128);
|
||||
}
|
||||
|
||||
size_t raw_b_aligned = 0;
|
||||
if (src1_es != dst_es && nb > 0) {
|
||||
const size_t raw_span1 = ((size_t) span1 / dst_es) * src1_es;
|
||||
const size_t raw_b_bytes = b_dense
|
||||
? ((size_t) (nr_max - 1) * b_nb1 + (size_t) nb * src1_es)
|
||||
: ((size_t) nb * raw_span1);
|
||||
raw_b_aligned = hex_round_up(raw_b_bytes, 128);
|
||||
}
|
||||
|
||||
size_t off = 0;
|
||||
VTCM_LAYOUT_ALLOC(off, off_tab, vec_bytes);
|
||||
VTCM_LAYOUT_ALLOC(off, off_inc, vec_bytes);
|
||||
VTCM_LAYOUT_ALLOC(off, off_cur, vec_bytes);
|
||||
VTCM_LAYOUT_ALLOC(off, off_region0, region_aligned);
|
||||
VTCM_LAYOUT_ALLOC(off, off_out0, out_aligned);
|
||||
VTCM_LAYOUT_ALLOC(off, off_region1, region_aligned);
|
||||
VTCM_LAYOUT_ALLOC(off, off_out1, out_aligned);
|
||||
if (raw_a_aligned > 0) {
|
||||
VTCM_LAYOUT_ALLOC(off, off_raw_a0, raw_a_aligned);
|
||||
VTCM_LAYOUT_ALLOC(off, off_raw_a1, raw_a_aligned);
|
||||
} else {
|
||||
L->off_raw_a0 = 0;
|
||||
L->off_raw_a1 = 0;
|
||||
}
|
||||
if (raw_b_aligned > 0) {
|
||||
VTCM_LAYOUT_ALLOC(off, off_raw_b0, raw_b_aligned);
|
||||
VTCM_LAYOUT_ALLOC(off, off_raw_b1, raw_b_aligned);
|
||||
} else {
|
||||
L->off_raw_b0 = 0;
|
||||
L->off_raw_b1 = 0;
|
||||
}
|
||||
L->total_per_hvx = off;
|
||||
}
|
||||
|
||||
static inline bool htp_concat_gather_rows_solve_layout(
|
||||
struct htp_gather_rows_params * kparams,
|
||||
uint32_t na, uint32_t a_nb1,
|
||||
uint32_t nb, uint32_t b_nb1, bool b_dense,
|
||||
uint32_t src0_es, uint32_t src1_es, uint32_t dst_es,
|
||||
uint32_t total_rows,
|
||||
uint32_t n_hvx,
|
||||
uint32_t mdev_count,
|
||||
uint32_t vtcm_size) {
|
||||
|
||||
if (src0_es == dst_es && src1_es == dst_es) {
|
||||
return htp_gather_rows_solve_layout(kparams, na, a_nb1, nb, b_nb1, b_dense, dst_es, dst_es, total_rows, n_hvx, mdev_count, vtcm_size);
|
||||
}
|
||||
|
||||
const uint32_t max_ne0 = 128 / dst_es;
|
||||
const uint32_t ne = na + nb;
|
||||
if (ne == 0 || ne > max_ne0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((dst_es != 4 && dst_es != 2) || (src0_es != 4 && src0_es != 2) || (src1_es != 4 && src1_es != 2)) {
|
||||
return false;
|
||||
}
|
||||
if ((a_nb1 % src0_es) != 0 || (b_nb1 % src1_es) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t a_reg_nb1 = (a_nb1 / src0_es) * dst_es;
|
||||
const uint32_t b_reg_nb1 = (b_nb1 / src1_es) * dst_es;
|
||||
|
||||
const uint32_t v_lanes = 128 / dst_es;
|
||||
const uint32_t g = hex_gcd_u32(ne, v_lanes);
|
||||
const uint32_t rg = v_lanes / g;
|
||||
const uint32_t vg = ne / g;
|
||||
|
||||
const uint32_t slice = (vtcm_size / n_hvx) & ~127u;
|
||||
const uint32_t total_chunks = total_rows / rg;
|
||||
const uint32_t chunks_per_dev = (total_chunks + mdev_count - 1) / mdev_count;
|
||||
const uint32_t max_rows_per_dev = (mdev_count > 1 && total_chunks >= mdev_count)
|
||||
? (chunks_per_dev * rg + (total_rows % rg))
|
||||
: total_rows;
|
||||
const uint32_t rows_per_hvx = hex_round_up((max_rows_per_dev + n_hvx - 1) / n_hvx, rg);
|
||||
|
||||
const uint32_t fixed = 3 * vg * 128;
|
||||
const uint32_t per_row_reg_a = na > 0 ? a_reg_nb1 : 0;
|
||||
const uint32_t per_row_reg_b = nb > 0 ? (b_dense ? b_reg_nb1 : nb * b_reg_nb1) : 0;
|
||||
const uint32_t per_row_raw_a = (src0_es != dst_es && na > 0) ? a_nb1 : 0;
|
||||
const uint32_t per_row_raw_b = (src1_es != dst_es && nb > 0) ? (b_dense ? b_nb1 : nb * b_nb1) : 0;
|
||||
const uint32_t per_row = per_row_reg_a + per_row_reg_b + per_row_raw_a + per_row_raw_b + ne * dst_es;
|
||||
if (slice <= fixed + 2 * per_row * rg) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t avail_per_buf = (slice - fixed) / 2;
|
||||
uint32_t nr_max = MIN(avail_per_buf / per_row, rows_per_hvx);
|
||||
if (dst_es == 2) {
|
||||
const uint32_t per_row_region = per_row_reg_a + per_row_reg_b;
|
||||
if (per_row_region > 0) {
|
||||
nr_max = MIN(nr_max, (32768u - 256u) / per_row_region);
|
||||
}
|
||||
}
|
||||
nr_max = (nr_max / rg) * rg;
|
||||
|
||||
uint32_t v1 = 0;
|
||||
uint32_t span1 = 0;
|
||||
uint32_t region_size = 0;
|
||||
|
||||
while (nr_max > 0) {
|
||||
v1 = na > 0 ? hex_round_up((nr_max - 1) * a_reg_nb1 + na * dst_es, 128) : 0;
|
||||
if (nb > 0) {
|
||||
if (b_dense) {
|
||||
span1 = dst_es;
|
||||
region_size = v1 + (nr_max - 1) * b_reg_nb1 + nb * dst_es;
|
||||
} else {
|
||||
span1 = (nr_max - 1) * b_reg_nb1 + dst_es;
|
||||
region_size = v1 + nb * span1;
|
||||
}
|
||||
} else {
|
||||
span1 = 0;
|
||||
region_size = v1;
|
||||
}
|
||||
|
||||
struct htp_concat_gather_rows_vtcm_layout layout;
|
||||
htp_concat_gather_rows_vtcm_layout_build(&layout, vg, region_size, nr_max,
|
||||
na, a_nb1, nb, b_nb1, b_dense, span1, ne,
|
||||
src0_es, src1_es, dst_es);
|
||||
|
||||
if ((dst_es != 2 || region_size <= 32768) && layout.total_per_hvx <= slice) {
|
||||
break;
|
||||
}
|
||||
|
||||
nr_max -= rg;
|
||||
}
|
||||
|
||||
if (nr_max == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
kparams->na = na;
|
||||
kparams->nb = nb;
|
||||
kparams->ne = ne;
|
||||
kparams->rg = rg;
|
||||
kparams->vg = vg;
|
||||
kparams->nr_max = nr_max;
|
||||
kparams->v1 = v1;
|
||||
kparams->span1 = span1;
|
||||
kparams->region_size = region_size;
|
||||
kparams->slice = slice;
|
||||
kparams->total_rows = total_rows;
|
||||
kparams->rows_per_hvx = rows_per_hvx;
|
||||
kparams->b_dense = b_dense ? 1 : 0;
|
||||
kparams->pad[0] = 0;
|
||||
kparams->pad[1] = 0;
|
||||
kparams->pad[2] = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
struct htp_concat_transposed_vtcm_layout {
|
||||
uint32_t src0_spad_size_per_hvx;
|
||||
uint32_t src1_spad_size_per_hvx;
|
||||
uint32_t cvt_spad_size_per_hvx;
|
||||
size_t off_spad0;
|
||||
size_t off_spad1;
|
||||
size_t off_spad2;
|
||||
uint32_t total_bytes;
|
||||
};
|
||||
|
||||
@@ -50,24 +247,39 @@ static inline void htp_concat_transposed_vtcm_layout_build(
|
||||
struct htp_concat_transposed_vtcm_layout * L,
|
||||
uint32_t src0_ne0,
|
||||
uint32_t src1_ne0,
|
||||
uint32_t type_size,
|
||||
uint32_t src0_es,
|
||||
uint32_t src1_es,
|
||||
uint32_t dst_es,
|
||||
uint32_t n_hvx) {
|
||||
|
||||
// src1_ne0 is processed in chunks of block_cols tokens, so VTCM does not grow with the batch
|
||||
uint32_t block_rows = 128;
|
||||
uint32_t block_cols = (type_size == 4) ? 256 : 512;
|
||||
uint32_t block_cols = (src1_es == 2 && dst_es == 2) ? 512 : 256;
|
||||
uint32_t col_align = (src1_es == 2 || dst_es == 2) ? 64 : 32;
|
||||
// +1 element spreads the gather lanes across VTCM, so they do not serialize on one path
|
||||
uint32_t spad1_stride = block_rows * type_size + type_size;
|
||||
uint32_t block_cols_padded = MIN(hex_round_up(src1_ne0, (type_size == 4) ? 32 : 64), block_cols);
|
||||
uint32_t spad0_row_bytes = hex_round_up(src0_ne0 * type_size, 128) + block_cols_padded * type_size;
|
||||
uint32_t spad1_stride = block_rows * src1_es + src1_es;
|
||||
uint32_t block_cols_padded = MIN(hex_round_up(src1_ne0, col_align), block_cols);
|
||||
|
||||
uint32_t dst_src0_row_padded = hex_round_up(src0_ne0 * dst_es, 128);
|
||||
uint32_t raw_src0_row_padded = (src0_es != dst_es) ? hex_round_up(src0_ne0 * src0_es, 128) : 0;
|
||||
uint32_t spad0_row_bytes = MAX(dst_src0_row_padded + block_cols_padded * dst_es, raw_src0_row_padded);
|
||||
|
||||
uint32_t cvt1_bytes = (src1_es != dst_es) ? (block_rows * 64 * src1_es) : 0;
|
||||
uint32_t cvt0_bytes = (src0_es != dst_es) ? dst_src0_row_padded : 0;
|
||||
|
||||
// double-buffered: two slots per thread
|
||||
L->src0_spad_size_per_hvx = 2 * block_rows * spad0_row_bytes;
|
||||
L->src1_spad_size_per_hvx = 2 * block_cols_padded * spad1_stride;
|
||||
L->cvt_spad_size_per_hvx = hex_round_up(MAX(cvt1_bytes, cvt0_bytes), 128);
|
||||
|
||||
size_t off = 0;
|
||||
VTCM_LAYOUT_ALLOC(off, off_spad0, (size_t) n_hvx * L->src0_spad_size_per_hvx);
|
||||
VTCM_LAYOUT_ALLOC(off, off_spad1, (size_t) n_hvx * L->src1_spad_size_per_hvx);
|
||||
if (L->cvt_spad_size_per_hvx > 0) {
|
||||
VTCM_LAYOUT_ALLOC(off, off_spad2, (size_t) n_hvx * L->cvt_spad_size_per_hvx);
|
||||
} else {
|
||||
L->off_spad2 = 0;
|
||||
}
|
||||
L->total_bytes = (uint32_t) off;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
@@ -1406,9 +1403,8 @@ static void conv2d_hmx_win_wrows_dma(const struct htp_conv2d_win_context * wc, d
|
||||
}
|
||||
|
||||
// K-contiguous kernel (k = cin * T + t): transpose the 32 cout rows of a packing unit to k rows of 32 cout, then into tiles
|
||||
__attribute__((noinline))
|
||||
static void conv2d_hmx_win_wrows_pack(const struct htp_conv2d_win_context * wc, const uint8_t * raw, uint8_t * xr, HVX_Vector * vin,
|
||||
HVX_Vector * scratch, uint32_t unit, uint32_t n_cs) {
|
||||
static inline void conv2d_hmx_win_wrows_pack_impl(const struct htp_conv2d_win_context * wc, const uint8_t * raw, uint8_t * xr, HVX_Vector * vin,
|
||||
HVX_Vector * scratch, uint32_t unit, uint32_t n_cs, const bool is_f16) {
|
||||
const struct htp_conv2d_kernel_params * kp = wc->kp;
|
||||
const struct htp_tensor * kernel = wc->octx->src[0];
|
||||
|
||||
@@ -1419,7 +1415,6 @@ static void conv2d_hmx_win_wrows_pack(const struct htp_conv2d_win_context * wc,
|
||||
const uint32_t T = KW * KH;
|
||||
const uint32_t n_cc = kp->window.n_cc;
|
||||
const uint32_t CS = kp->window.w_cs;
|
||||
const bool is_f16 = kernel->type == HTP_TYPE_F16;
|
||||
const uint32_t kpw = is_f16 ? 64 : 32; // k per vector
|
||||
const uint32_t rstep = is_f16 ? 64 : 128; // bytes per transposed k row
|
||||
|
||||
@@ -1491,6 +1486,27 @@ static void conv2d_hmx_win_wrows_pack(const struct htp_conv2d_win_context * wc,
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((noinline))
|
||||
static void conv2d_hmx_win_wrows_pack_f16(const struct htp_conv2d_win_context * wc, const uint8_t * raw, uint8_t * xr, HVX_Vector * vin,
|
||||
HVX_Vector * scratch, uint32_t unit, uint32_t n_cs) {
|
||||
conv2d_hmx_win_wrows_pack_impl(wc, raw, xr, vin, scratch, unit, n_cs, true);
|
||||
}
|
||||
|
||||
__attribute__((noinline))
|
||||
static void conv2d_hmx_win_wrows_pack_f32(const struct htp_conv2d_win_context * wc, const uint8_t * raw, uint8_t * xr, HVX_Vector * vin,
|
||||
HVX_Vector * scratch, uint32_t unit, uint32_t n_cs) {
|
||||
conv2d_hmx_win_wrows_pack_impl(wc, raw, xr, vin, scratch, unit, n_cs, false);
|
||||
}
|
||||
|
||||
static inline void conv2d_hmx_win_wrows_pack(const struct htp_conv2d_win_context * wc, const uint8_t * raw, uint8_t * xr, HVX_Vector * vin,
|
||||
HVX_Vector * scratch, uint32_t unit, uint32_t n_cs) {
|
||||
if (wc->octx->src[0]->type == HTP_TYPE_F16) {
|
||||
conv2d_hmx_win_wrows_pack_f16(wc, raw, xr, vin, scratch, unit, n_cs);
|
||||
} else {
|
||||
conv2d_hmx_win_wrows_pack_f32(wc, raw, xr, vin, scratch, unit, n_cs);
|
||||
}
|
||||
}
|
||||
|
||||
static void conv2d_hmx_win_wpack_rows(const struct htp_conv2d_win_context * wc, const htp_thread_t * th, unsigned int nth, unsigned int ith) {
|
||||
struct htp_ops_context * octx = wc->octx;
|
||||
const struct htp_conv2d_kernel_params * kp = wc->kp;
|
||||
@@ -1807,7 +1823,6 @@ static void conv2d_hmx_win_croutons_fold(const struct htp_conv2d_win_context * w
|
||||
const uint32_t Cin = input->ne[2];
|
||||
const uint32_t N = input->ne[3];
|
||||
const uint32_t KW = octx->src[0]->ne[0];
|
||||
const uint32_t nch = KW * Cin;
|
||||
const uint32_t SR = kp->window.sr;
|
||||
const uint32_t IHS = kp->window.ihs;
|
||||
const int32_t p0 = octx->op_params[2];
|
||||
@@ -1984,7 +1999,6 @@ static void conv2d_hmx_win_croutons_fold_cwhn(const struct htp_conv2d_win_contex
|
||||
}
|
||||
|
||||
static void conv2d_hmx_win_pack(const struct htp_conv2d_win_context * wc, const htp_thread_t * th, unsigned int nth, unsigned int ith) {
|
||||
struct htp_ops_context * octx = wc->octx;
|
||||
const struct htp_conv2d_kernel_params * kp = wc->kp;
|
||||
|
||||
uint32_t ts;
|
||||
|
||||
+151
-111
@@ -1,7 +1,3 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
#include <HAP_perf.h>
|
||||
#include <qurt_memory.h>
|
||||
@@ -13,6 +9,7 @@
|
||||
#include "ggml-common.h"
|
||||
#include "cpy-ops.h"
|
||||
#include "dma-copy.h"
|
||||
#include "dma-pipeline.h"
|
||||
#include "htp-ctx.h"
|
||||
#include "htp-fence.h"
|
||||
#include "htp-ops.h"
|
||||
@@ -20,9 +17,19 @@
|
||||
#include "hvx-utils.h"
|
||||
#include "gather-rows.h"
|
||||
|
||||
typedef void (*cpy_convert_block_fn_t)(
|
||||
uint8_t * restrict dst_spad,
|
||||
const uint8_t * restrict src_spad,
|
||||
uint32_t nr,
|
||||
uint32_t ne00,
|
||||
uint32_t dst_row_stride,
|
||||
uint32_t src0_row_stride
|
||||
);
|
||||
|
||||
struct htp_copy_context {
|
||||
struct htp_ops_context * octx;
|
||||
const struct htp_copy_kernel_params * kparams;
|
||||
cpy_convert_block_fn_t convert_block;
|
||||
|
||||
uint32_t row_start;
|
||||
uint32_t nrows;
|
||||
@@ -41,29 +48,29 @@ struct htp_copy_context {
|
||||
uint32_t tiles_per_hvx;
|
||||
};
|
||||
|
||||
#define cpy_preamble \
|
||||
const struct htp_tensor *src0 = octx->src[0]; \
|
||||
const struct htp_tensor *dst = octx->dst; \
|
||||
\
|
||||
const uint32_t ne00 = src0->ne[0]; \
|
||||
const uint32_t ne01 = src0->ne[1]; \
|
||||
const uint32_t ne02 = src0->ne[2]; \
|
||||
const uint32_t ne03 = src0->ne[3]; \
|
||||
\
|
||||
const uint32_t nb00 = src0->nb[0]; \
|
||||
const uint32_t nb01 = src0->nb[1]; \
|
||||
const uint32_t nb02 = src0->nb[2]; \
|
||||
const uint32_t nb03 = src0->nb[3]; \
|
||||
\
|
||||
const uint32_t ne0 = dst->ne[0]; \
|
||||
const uint32_t ne1 = dst->ne[1]; \
|
||||
const uint32_t ne2 = dst->ne[2]; \
|
||||
const uint32_t ne3 = dst->ne[3]; \
|
||||
\
|
||||
const uint32_t nb0 = dst->nb[0]; \
|
||||
const uint32_t nb1 = dst->nb[1]; \
|
||||
const uint32_t nb2 = dst->nb[2]; \
|
||||
const uint32_t nb3 = dst->nb[3];
|
||||
#define cpy_preamble \
|
||||
const struct htp_tensor *src0 = octx->src[0]; \
|
||||
const struct htp_tensor *dst = octx->dst; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) ne00 = src0->ne[0]; \
|
||||
const uint32_t __attribute__((unused)) ne01 = src0->ne[1]; \
|
||||
const uint32_t __attribute__((unused)) ne02 = src0->ne[2]; \
|
||||
const uint32_t __attribute__((unused)) ne03 = src0->ne[3]; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) nb00 = src0->nb[0]; \
|
||||
const uint32_t __attribute__((unused)) nb01 = src0->nb[1]; \
|
||||
const uint32_t __attribute__((unused)) nb02 = src0->nb[2]; \
|
||||
const uint32_t __attribute__((unused)) nb03 = src0->nb[3]; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) ne0 = dst->ne[0]; \
|
||||
const uint32_t __attribute__((unused)) ne1 = dst->ne[1]; \
|
||||
const uint32_t __attribute__((unused)) ne2 = dst->ne[2]; \
|
||||
const uint32_t __attribute__((unused)) ne3 = dst->ne[3]; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) nb0 = dst->nb[0]; \
|
||||
const uint32_t __attribute__((unused)) nb1 = dst->nb[1]; \
|
||||
const uint32_t __attribute__((unused)) nb2 = dst->nb[2]; \
|
||||
const uint32_t __attribute__((unused)) nb3 = dst->nb[3];
|
||||
|
||||
#define DEFINE_CPY_RESHAPE(NAME, ELEM_TYPE, ELEM_SIZE) \
|
||||
static void cpy_thread_##NAME##_reshape(const htp_thread_t * th, unsigned int nth, unsigned int ith, \
|
||||
@@ -332,85 +339,120 @@ static void cpy_thread_transpose(const htp_thread_t * th, unsigned int nth, unsi
|
||||
dma_queue_flush(dma_q);
|
||||
}
|
||||
|
||||
#define DEFINE_CPY_CONVERT_SAMESHAPE(NAME, CONV_FUNC) \
|
||||
static void cpy_thread_##NAME##_sameshape(const htp_thread_t * th, unsigned int nth, \
|
||||
unsigned int ith, void * data) { \
|
||||
struct htp_copy_context * ct = (struct htp_copy_context *) data; \
|
||||
struct htp_ops_context * octx = ct->octx; \
|
||||
cpy_preamble; \
|
||||
\
|
||||
const uint32_t dr = ct->src0_nrows_per_hvx; \
|
||||
const uint32_t ir0 = ct->row_start + dr * ith; \
|
||||
const uint32_t ir1 = MIN(ir0 + dr, ct->row_start + ct->nrows); \
|
||||
if (ir0 >= ir1) return; \
|
||||
\
|
||||
dma_queue * dma_q = th->dma_q; \
|
||||
struct htp_thread_trace * tr = th->trace; \
|
||||
\
|
||||
const struct htp_copy_convert_params * cvt = &ct->kparams->u.convert; \
|
||||
const struct htp_copy_convert_vtcm_layout * L = &ct->layout; \
|
||||
const uint32_t src0_buf_size = L->src0_buf_size; \
|
||||
const uint32_t dst_buf_size = L->dst_buf_size; \
|
||||
const uint32_t src0_row_stride = L->src0_row_stride; \
|
||||
const uint32_t dst_row_stride = L->dst_row_stride; \
|
||||
const uint32_t blk_rows = L->blk_rows; \
|
||||
uint8_t * vtcm_src0_base = ct->vtcm_src0 + ith * L->spad0_size_per_hvx; \
|
||||
uint8_t * vtcm_dst_base = ct->vtcm_dst + ith * L->spad1_size_per_hvx; \
|
||||
const uint32_t src0_row_size = ne00 * ct->kparams->src0_type_size; \
|
||||
const uint32_t dst_row_size = ne00 * ct->kparams->dst_type_size; \
|
||||
\
|
||||
const uint32_t ne02_ne01 = ne02 * ne01; \
|
||||
uint32_t i03 = fastdiv(ir0, &cvt->div_ne02_ne01); \
|
||||
uint32_t rem = ir0 - i03 * ne02_ne01; \
|
||||
uint32_t i02 = fastdiv(rem, &cvt->div_ne01); \
|
||||
uint32_t i01 = rem - i02 * ne01; \
|
||||
\
|
||||
struct cpy_row_cursor f = { ir0, i01, i02, i03 }; \
|
||||
struct cpy_row_cursor c = f; \
|
||||
\
|
||||
for (uint32_t b = 0; b < 2 && f.r < ir1; ++b) { \
|
||||
uint8_t * src_spad = vtcm_src0_base + b * src0_buf_size; \
|
||||
uint8_t * dst_spad = vtcm_dst_base + b * dst_buf_size; \
|
||||
const uint32_t nr = MIN(blk_rows, MIN(ne01 - f.i01, ir1 - f.r)); \
|
||||
dma_addr_t f_src0_addr = src0->data + f.i01*nb01 + f.i02*nb02 + f.i03*nb03; \
|
||||
dma_queue_push(dma_q, dma_make_data(dst->data, dst_spad), \
|
||||
dst_row_size, dst_buf_size, dst_row_size, 0); \
|
||||
dma_queue_push(dma_q, dma_make_data(src_spad, f_src0_addr), \
|
||||
src0_row_stride, nb01, src0_row_size, nr); \
|
||||
cpy_cursor_advance(&f, nr, ne01, ne02); \
|
||||
} \
|
||||
\
|
||||
for (uint32_t blk = 0; c.r < ir1; ++blk) { \
|
||||
const uint32_t nr = MIN(blk_rows, MIN(ne01 - c.i01, ir1 - c.r)); \
|
||||
uint8_t * dst_spad = (uint8_t *) (uintptr_t) dma_queue_pop(dma_q).src; \
|
||||
uint8_t * src_spad = (uint8_t *) (uintptr_t) dma_queue_pop(dma_q).dst; \
|
||||
\
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) blk); \
|
||||
for (uint32_t r = 0; r < nr; ++r) { \
|
||||
CONV_FUNC(dst_spad + r * dst_row_stride, src_spad + r * src0_row_stride, ne00); \
|
||||
} \
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) blk); \
|
||||
\
|
||||
dma_addr_t c_dst_addr = dst->data + c.i01*nb1 + c.i02*nb2 + c.i03*nb3; \
|
||||
dma_queue_push(dma_q, dma_make_data(c_dst_addr, dst_spad), \
|
||||
nb1, dst_row_stride, dst_row_size, nr); \
|
||||
cpy_cursor_advance(&c, nr, ne01, ne02); \
|
||||
\
|
||||
if (f.r < ir1) { \
|
||||
const uint32_t fnr = MIN(blk_rows, MIN(ne01 - f.i01, ir1 - f.r)); \
|
||||
dma_addr_t f_src0_addr = src0->data + f.i01*nb01 + f.i02*nb02 + f.i03*nb03; \
|
||||
dma_queue_push(dma_q, dma_make_data(src_spad, f_src0_addr), \
|
||||
src0_row_stride, nb01, src0_row_size, fnr); \
|
||||
cpy_cursor_advance(&f, fnr, ne01, ne02); \
|
||||
} \
|
||||
} \
|
||||
dma_queue_flush(dma_q); \
|
||||
#define DEFINE_CPY_CONVERT_BLOCK(NAME, CONV_FUNC) \
|
||||
static void __attribute__((noinline)) cpy_convert_block_##NAME( \
|
||||
uint8_t * restrict dst_spad, \
|
||||
const uint8_t * restrict src_spad, \
|
||||
uint32_t nr, \
|
||||
uint32_t ne00, \
|
||||
uint32_t dst_row_stride, \
|
||||
uint32_t src0_row_stride \
|
||||
) { \
|
||||
for (uint32_t r = 0; r < nr; ++r) { \
|
||||
CONV_FUNC(dst_spad + r * dst_row_stride, src_spad + r * src0_row_stride, ne00); \
|
||||
} \
|
||||
}
|
||||
|
||||
DEFINE_CPY_CONVERT_SAMESHAPE(f16_f32, hvx_copy_f16_f32_aa)
|
||||
DEFINE_CPY_CONVERT_SAMESHAPE(f32_f16, hvx_copy_f32_f16_aa)
|
||||
DEFINE_CPY_CONVERT_SAMESHAPE(i32_f32, hvx_copy_i32_f32_aa)
|
||||
DEFINE_CPY_CONVERT_SAMESHAPE(f32_i32, hvx_copy_f32_i32_aa)
|
||||
DEFINE_CPY_CONVERT_BLOCK(f16_f32, hvx_copy_f16_f32_aa)
|
||||
DEFINE_CPY_CONVERT_BLOCK(f32_f16, hvx_copy_f32_f16_aa)
|
||||
DEFINE_CPY_CONVERT_BLOCK(i32_f32, hvx_copy_i32_f32_aa)
|
||||
DEFINE_CPY_CONVERT_BLOCK(f32_i32, hvx_copy_f32_i32_aa)
|
||||
|
||||
struct cpy_convert_pipe_state {
|
||||
dma_addr_t src0_data;
|
||||
dma_addr_t dst_data;
|
||||
cpy_convert_block_fn_t convert_block;
|
||||
uint32_t ne00, ne01, ne02;
|
||||
uint32_t nb01, nb02, nb03;
|
||||
uint32_t nb1, nb2, nb3;
|
||||
uint32_t src0_row_size;
|
||||
uint32_t dst_row_size;
|
||||
uint32_t src0_row_stride;
|
||||
uint32_t dst_row_stride;
|
||||
uint32_t blk_rows;
|
||||
uint32_t ir1;
|
||||
struct cpy_row_cursor cur;
|
||||
};
|
||||
|
||||
static bool cpy_convert_next_task(void * ctx, struct dma_pipe_task * task) {
|
||||
struct cpy_convert_pipe_state * s = (struct cpy_convert_pipe_state *) ctx;
|
||||
if (s->cur.r >= s->ir1) {
|
||||
return false;
|
||||
}
|
||||
const uint32_t nr = MIN(s->blk_rows, MIN(s->ne01 - s->cur.i01, s->ir1 - s->cur.r));
|
||||
task->row = s->cur.i01;
|
||||
task->off = s->cur.i02;
|
||||
task->len = nr;
|
||||
task->aux = s->cur.i03;
|
||||
cpy_cursor_advance(&s->cur, nr, s->ne01, s->ne02);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void cpy_convert_push_in(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad) {
|
||||
struct cpy_convert_pipe_state * s = (struct cpy_convert_pipe_state *) ctx;
|
||||
const dma_addr_t src0_addr = s->src0_data + (size_t) task->row * s->nb01 + (size_t) task->off * s->nb02 + (size_t) task->aux * s->nb03;
|
||||
dma_queue_push(dma_q, dma_make_data(in_spad, src0_addr),
|
||||
s->src0_row_stride, s->nb01, s->src0_row_size, task->len);
|
||||
}
|
||||
|
||||
static void cpy_convert_compute(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad) {
|
||||
struct cpy_convert_pipe_state * s = (struct cpy_convert_pipe_state *) ctx;
|
||||
s->convert_block((uint8_t *) out_spad, (const uint8_t *) in_spad, task->len, s->ne00, s->dst_row_stride, s->src0_row_stride);
|
||||
}
|
||||
|
||||
static void cpy_convert_push_out(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad) {
|
||||
struct cpy_convert_pipe_state * s = (struct cpy_convert_pipe_state *) ctx;
|
||||
const dma_addr_t dst_addr = s->dst_data + (size_t) task->row * s->nb1 + (size_t) task->off * s->nb2 + (size_t) task->aux * s->nb3;
|
||||
dma_queue_push(dma_q, dma_make_data(dst_addr, out_spad),
|
||||
s->nb1, s->dst_row_stride, s->dst_row_size, task->len);
|
||||
}
|
||||
|
||||
static void cpy_thread_convert_sameshape(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
(void) nth;
|
||||
struct htp_copy_context * ct = (struct htp_copy_context *) data;
|
||||
struct htp_ops_context * octx = ct->octx;
|
||||
cpy_preamble;
|
||||
|
||||
const uint32_t dr = ct->src0_nrows_per_hvx;
|
||||
const uint32_t ir0 = ct->row_start + dr * ith;
|
||||
const uint32_t ir1 = MIN(ir0 + dr, ct->row_start + ct->nrows);
|
||||
if (ir0 >= ir1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const struct htp_copy_convert_params * cvt = &ct->kparams->u.convert;
|
||||
const struct htp_copy_convert_vtcm_layout * L = &ct->layout;
|
||||
|
||||
uint8_t * vtcm_src0_base = ct->vtcm_src0 + ith * L->spad0_size_per_hvx;
|
||||
uint8_t * vtcm_dst_base = ct->vtcm_dst + ith * L->spad1_size_per_hvx;
|
||||
void * in_buf[2] = { vtcm_src0_base, vtcm_src0_base + L->src0_buf_size };
|
||||
void * out_buf[2] = { vtcm_dst_base, vtcm_dst_base + L->dst_buf_size };
|
||||
|
||||
const uint32_t ne02_ne01 = ne02 * ne01;
|
||||
uint32_t i03 = fastdiv(ir0, &cvt->div_ne02_ne01);
|
||||
uint32_t rem = ir0 - i03 * ne02_ne01;
|
||||
uint32_t i02 = fastdiv(rem, &cvt->div_ne01);
|
||||
uint32_t i01 = rem - i02 * ne01;
|
||||
|
||||
struct cpy_convert_pipe_state s = {
|
||||
.src0_data = src0->data,
|
||||
.dst_data = dst->data,
|
||||
.convert_block = ct->convert_block,
|
||||
.ne00 = ne00, .ne01 = ne01, .ne02 = ne02,
|
||||
.nb01 = nb01, .nb02 = nb02, .nb03 = nb03,
|
||||
.nb1 = nb1, .nb2 = nb2, .nb3 = nb3,
|
||||
.src0_row_size = ne00 * ct->kparams->src0_type_size,
|
||||
.dst_row_size = ne00 * ct->kparams->dst_type_size,
|
||||
.src0_row_stride = L->src0_row_stride,
|
||||
.dst_row_stride = L->dst_row_stride,
|
||||
.blk_rows = L->blk_rows,
|
||||
.ir1 = ir1,
|
||||
.cur = { ir0, i01, i02, i03 },
|
||||
};
|
||||
|
||||
dma_pipe_io_run(th->dma_q, th->trace, &s, in_buf, out_buf,
|
||||
cpy_convert_next_task, cpy_convert_push_in, cpy_convert_compute, cpy_convert_push_out);
|
||||
}
|
||||
|
||||
static int cpy_scalar(struct htp_ops_context * octx, const struct htp_copy_kernel_params * kparams) {
|
||||
if (octx->ctx->mdev.count > 1 && octx->ctx->mdev.idx > 0) {
|
||||
@@ -550,26 +592,24 @@ static int cpy_sameshape_convert(struct htp_ops_context * octx, const struct htp
|
||||
ct.vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, ct.layout.off_src0);
|
||||
ct.vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, ct.layout.off_dst);
|
||||
|
||||
work_queue_func_t copy_fun = NULL;
|
||||
if (dst->type == HTP_TYPE_F16 && src0->type == HTP_TYPE_F32) {
|
||||
copy_fun = cpy_thread_f16_f32_sameshape;
|
||||
ct.convert_block = cpy_convert_block_f16_f32;
|
||||
} else if (dst->type == HTP_TYPE_F32 && src0->type == HTP_TYPE_F16) {
|
||||
copy_fun = cpy_thread_f32_f16_sameshape;
|
||||
ct.convert_block = cpy_convert_block_f32_f16;
|
||||
} else if (dst->type == HTP_TYPE_I32 && src0->type == HTP_TYPE_F32) {
|
||||
copy_fun = cpy_thread_i32_f32_sameshape;
|
||||
ct.convert_block = cpy_convert_block_i32_f32;
|
||||
} else if (dst->type == HTP_TYPE_F32 && src0->type == HTP_TYPE_I32) {
|
||||
copy_fun = cpy_thread_f32_i32_sameshape;
|
||||
ct.convert_block = cpy_convert_block_f32_i32;
|
||||
} else {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
work_queue_run(octx->ctx->hvx_queue, copy_fun, &ct, n_hvx);
|
||||
work_queue_run(octx->ctx->hvx_queue, cpy_thread_convert_sameshape, &ct, n_hvx);
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
static int cpy_reshape(struct htp_ops_context * octx, const struct htp_copy_kernel_params * kparams) {
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
if (!htp_ops_context_set_n_hvx(octx, kparams->n_hvx)) {
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
#include <HAP_perf.h>
|
||||
|
||||
@@ -16,48 +12,44 @@
|
||||
#include "hvx-types.h"
|
||||
#include "hvx-utils.h"
|
||||
#include "dma-queue.h"
|
||||
#include "dma-pipeline.h"
|
||||
#include "cumsum-ops.h"
|
||||
|
||||
#define htp_cumsum_tensors_preamble \
|
||||
const struct htp_tensor * restrict src0 = octx->src[0]; \
|
||||
const struct htp_tensor * restrict dst = octx->dst; \
|
||||
\
|
||||
const uint32_t ne00 = src0->ne[0]; \
|
||||
const uint32_t ne01 = src0->ne[1]; \
|
||||
const uint32_t ne02 = src0->ne[2]; \
|
||||
const uint32_t ne03 = src0->ne[3]; \
|
||||
\
|
||||
const uint32_t ne0 = dst->ne[0]; \
|
||||
const uint32_t ne1 = dst->ne[1]; \
|
||||
const uint32_t ne2 = dst->ne[2]; \
|
||||
const uint32_t ne3 = dst->ne[3]; \
|
||||
\
|
||||
const uint32_t nb00 = src0->nb[0]; \
|
||||
const uint32_t nb01 = src0->nb[1]; \
|
||||
const uint32_t nb02 = src0->nb[2]; \
|
||||
const uint32_t nb03 = src0->nb[3]; \
|
||||
\
|
||||
const uint32_t nb0 = dst->nb[0]; \
|
||||
const uint32_t nb1 = dst->nb[1]; \
|
||||
const uint32_t nb2 = dst->nb[2]; \
|
||||
const uint32_t nb3 = dst->nb[3];
|
||||
|
||||
struct htp_cumsum_context {
|
||||
struct htp_ops_context * octx;
|
||||
size_t src_row_size;
|
||||
size_t dst_row_size;
|
||||
size_t src_row_size_aligned;
|
||||
size_t dst_row_size_aligned;
|
||||
uint32_t rows_per_hvx;
|
||||
uint32_t total_rows;
|
||||
uint32_t row_start;
|
||||
dma_pipe_compute_io_fn compute;
|
||||
uint8_t * vtcm_src0;
|
||||
uint8_t * vtcm_dst;
|
||||
size_t src0_bytes_per_hvx;
|
||||
size_t dst_bytes_per_hvx;
|
||||
size_t src_elem_size;
|
||||
size_t dst_elem_size;
|
||||
size_t src_row_size;
|
||||
size_t dst_row_size;
|
||||
size_t src_chunk_bytes_aligned;
|
||||
size_t dst_chunk_bytes_aligned;
|
||||
uint32_t chunk_elems;
|
||||
uint32_t rows_per_hvx;
|
||||
uint32_t total_rows;
|
||||
uint32_t row_start;
|
||||
};
|
||||
|
||||
#define htp_cumsum_preamble \
|
||||
struct htp_cumsum_context * cctx = (struct htp_cumsum_context *) data; \
|
||||
struct htp_ops_context * octx = cctx->octx; \
|
||||
htp_cumsum_tensors_preamble; \
|
||||
dma_queue * dma_q = th->dma_q;
|
||||
struct cumsum_pipe_state {
|
||||
dma_addr_t src_data;
|
||||
dma_addr_t dst_data;
|
||||
size_t src_elem_size;
|
||||
size_t dst_elem_size;
|
||||
size_t src_row_size;
|
||||
size_t dst_row_size;
|
||||
size_t src_chunk_bytes_aligned;
|
||||
size_t dst_chunk_bytes_aligned;
|
||||
uint32_t ne00;
|
||||
uint32_t chunk_elems;
|
||||
uint32_t row;
|
||||
uint32_t row_end;
|
||||
uint32_t off;
|
||||
uint32_t carry_bits;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HVX prefix scan helpers
|
||||
@@ -71,7 +63,7 @@ static inline HVX_Vector hvx_cumsum_vadd(HVX_Vector a, HVX_Vector b) {
|
||||
static inline HVX_Vector hvx_cumsum_vadd(HVX_Vector a, HVX_Vector b) {
|
||||
return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_VsfVsf(a, b));
|
||||
}
|
||||
#endif // __HVX_ARCH__ > 75
|
||||
#endif // __HVX_ARCH__ > 75
|
||||
|
||||
static inline HVX_Vector hvx_prefix_scan_f32(HVX_Vector v, HVX_Vector carry_in) {
|
||||
const HVX_Vector zero = Q6_V_vsplat_R(0);
|
||||
@@ -90,36 +82,191 @@ static inline HVX_Vector hvx_splat_last_f32(HVX_Vector v) {
|
||||
return hvx_vec_repl4(Q6_V_vror_VR(v, 124));
|
||||
}
|
||||
|
||||
static inline void hvx_cumsum_row_f32(const float * restrict src, float * restrict dst, uint32_t n) {
|
||||
static inline HVX_Vector hvx_cumsum_clamp_f16(HVX_Vector v) {
|
||||
const HVX_Vector max_f16 = hvx_vec_splat_f32(65504.0f);
|
||||
const HVX_Vector min_f16 = hvx_vec_splat_f32(-65504.0f);
|
||||
return Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v));
|
||||
}
|
||||
|
||||
static void __attribute__((noinline)) cumsum_compute_f32_f32(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad) {
|
||||
struct cumsum_pipe_state * s = (struct cumsum_pipe_state *) ctx;
|
||||
|
||||
const float * restrict src = (const float *) in_spad;
|
||||
float * restrict dst = (float *) out_spad;
|
||||
|
||||
const uint32_t n = task->len;
|
||||
const uint32_t nvec = n >> 5;
|
||||
const uint32_t nloe = n & (VLEN_FP32 - 1);
|
||||
|
||||
HVX_Vector carry = Q6_V_vsplat_R(0);
|
||||
HVX_Vector carry = (task->off == 0) ? Q6_V_vsplat_R(0) : Q6_V_vsplat_R(s->carry_bits);
|
||||
|
||||
for (uint32_t i = 0; i < nvec; i++) {
|
||||
HVX_Vector v = *((const HVX_UVector *) (src + i * VLEN_FP32));
|
||||
HVX_Vector v = *((const HVX_Vector *) (src + i * VLEN_FP32));
|
||||
v = hvx_prefix_scan_f32(v, carry);
|
||||
hvx_vec_store_u(dst + i * VLEN_FP32, VLEN, v);
|
||||
*((HVX_Vector *) (dst + i * VLEN_FP32)) = v;
|
||||
carry = hvx_splat_last_f32(v);
|
||||
}
|
||||
|
||||
if (nloe) {
|
||||
float acc = hvx_vec_get_f32(carry);
|
||||
const float * src_tail = src + nvec * VLEN_FP32;
|
||||
float * dst_tail = dst + nvec * VLEN_FP32;
|
||||
for (uint32_t i = 0; i < nloe; i++) {
|
||||
acc += src_tail[i];
|
||||
dst_tail[i] = acc;
|
||||
HVX_Vector v = *((const HVX_Vector *) (src + nvec * VLEN_FP32));
|
||||
v = hvx_prefix_scan_f32(v, carry);
|
||||
hvx_vec_store_a(dst + nvec * VLEN_FP32, nloe * sizeof(float), v);
|
||||
}
|
||||
|
||||
s->carry_bits = (uint32_t) Q6_R_vextract_VR(carry, 0);
|
||||
}
|
||||
|
||||
static void __attribute__((noinline)) cumsum_compute_f16_f16(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad) {
|
||||
struct cumsum_pipe_state * s = (struct cumsum_pipe_state *) ctx;
|
||||
|
||||
const HVX_Vector * restrict vsrc = (const HVX_Vector *) in_spad;
|
||||
HVX_Vector * restrict vdst = (HVX_Vector *) out_spad;
|
||||
|
||||
const uint32_t n = task->len;
|
||||
const uint32_t nvec = n >> 6;
|
||||
const uint32_t nloe = n & (VLEN_FP16 - 1);
|
||||
|
||||
HVX_Vector carry = (task->off == 0) ? Q6_V_vsplat_R(0) : Q6_V_vsplat_R(s->carry_bits);
|
||||
|
||||
for (uint32_t i = 0; i < nvec; i++) {
|
||||
const HVX_VectorPair p = hvx_vec_f16_to_f32(vsrc[i]);
|
||||
const HVX_Vector v0 = hvx_prefix_scan_f32(Q6_V_lo_W(p), carry);
|
||||
carry = hvx_splat_last_f32(v0);
|
||||
const HVX_Vector v1 = hvx_prefix_scan_f32(Q6_V_hi_W(p), carry);
|
||||
carry = hvx_splat_last_f32(v1);
|
||||
vdst[i] = hvx_vec_f32_to_f16(hvx_cumsum_clamp_f16(v0), hvx_cumsum_clamp_f16(v1));
|
||||
}
|
||||
|
||||
if (nloe) {
|
||||
const HVX_VectorPair p = hvx_vec_f16_to_f32(vsrc[nvec]);
|
||||
const HVX_Vector v0 = hvx_prefix_scan_f32(Q6_V_lo_W(p), carry);
|
||||
carry = hvx_splat_last_f32(v0);
|
||||
const HVX_Vector v1 = hvx_prefix_scan_f32(Q6_V_hi_W(p), carry);
|
||||
const HVX_Vector out = hvx_vec_f32_to_f16(hvx_cumsum_clamp_f16(v0), hvx_cumsum_clamp_f16(v1));
|
||||
hvx_vec_store_a(&vdst[nvec], nloe * sizeof(__fp16), out);
|
||||
}
|
||||
|
||||
s->carry_bits = (uint32_t) Q6_R_vextract_VR(carry, 0);
|
||||
}
|
||||
|
||||
static void __attribute__((noinline)) cumsum_compute_f16_f32(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad) {
|
||||
struct cumsum_pipe_state * s = (struct cumsum_pipe_state *) ctx;
|
||||
|
||||
const HVX_Vector * restrict vsrc = (const HVX_Vector *) in_spad;
|
||||
HVX_Vector * restrict vdst = (HVX_Vector *) out_spad;
|
||||
|
||||
const uint32_t n = task->len;
|
||||
const uint32_t nvec = n >> 6;
|
||||
const uint32_t nloe = n & (VLEN_FP16 - 1);
|
||||
|
||||
HVX_Vector carry = (task->off == 0) ? Q6_V_vsplat_R(0) : Q6_V_vsplat_R(s->carry_bits);
|
||||
|
||||
for (uint32_t i = 0; i < nvec; i++) {
|
||||
const HVX_VectorPair p = hvx_vec_f16_to_f32(vsrc[i]);
|
||||
const HVX_Vector v0 = hvx_prefix_scan_f32(Q6_V_lo_W(p), carry);
|
||||
carry = hvx_splat_last_f32(v0);
|
||||
const HVX_Vector v1 = hvx_prefix_scan_f32(Q6_V_hi_W(p), carry);
|
||||
carry = hvx_splat_last_f32(v1);
|
||||
vdst[2 * i] = v0;
|
||||
vdst[2 * i + 1] = v1;
|
||||
}
|
||||
|
||||
if (nloe) {
|
||||
const HVX_VectorPair p = hvx_vec_f16_to_f32(vsrc[nvec]);
|
||||
const HVX_Vector v0 = hvx_prefix_scan_f32(Q6_V_lo_W(p), carry);
|
||||
if (nloe > VLEN_FP32) {
|
||||
vdst[2 * nvec] = v0;
|
||||
carry = hvx_splat_last_f32(v0);
|
||||
const HVX_Vector v1 = hvx_prefix_scan_f32(Q6_V_hi_W(p), carry);
|
||||
hvx_vec_store_a(&vdst[2 * nvec + 1], (nloe - VLEN_FP32) * sizeof(float), v1);
|
||||
} else {
|
||||
hvx_vec_store_a(&vdst[2 * nvec], nloe * sizeof(float), v0);
|
||||
}
|
||||
}
|
||||
|
||||
s->carry_bits = (uint32_t) Q6_R_vextract_VR(carry, 0);
|
||||
}
|
||||
|
||||
static void __attribute__((noinline)) cumsum_compute_f32_f16(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad) {
|
||||
struct cumsum_pipe_state * s = (struct cumsum_pipe_state *) ctx;
|
||||
|
||||
const HVX_Vector * restrict vsrc = (const HVX_Vector *) in_spad;
|
||||
HVX_Vector * restrict vdst = (HVX_Vector *) out_spad;
|
||||
|
||||
const uint32_t n = task->len;
|
||||
const uint32_t nvec = n >> 6;
|
||||
const uint32_t nloe = n & (VLEN_FP16 - 1);
|
||||
|
||||
HVX_Vector carry = (task->off == 0) ? Q6_V_vsplat_R(0) : Q6_V_vsplat_R(s->carry_bits);
|
||||
|
||||
for (uint32_t i = 0; i < nvec; i++) {
|
||||
const HVX_Vector v0 = hvx_prefix_scan_f32(vsrc[2 * i], carry);
|
||||
carry = hvx_splat_last_f32(v0);
|
||||
const HVX_Vector v1 = hvx_prefix_scan_f32(vsrc[2 * i + 1], carry);
|
||||
carry = hvx_splat_last_f32(v1);
|
||||
vdst[i] = hvx_vec_f32_to_f16(hvx_cumsum_clamp_f16(v0), hvx_cumsum_clamp_f16(v1));
|
||||
}
|
||||
|
||||
if (nloe) {
|
||||
const HVX_Vector v0 = hvx_prefix_scan_f32(vsrc[2 * nvec], carry);
|
||||
carry = hvx_splat_last_f32(v0);
|
||||
const HVX_Vector v1 = (nloe > VLEN_FP32) ? hvx_prefix_scan_f32(vsrc[2 * nvec + 1], carry) : Q6_V_vzero();
|
||||
const HVX_Vector out = hvx_vec_f32_to_f16(hvx_cumsum_clamp_f16(v0), hvx_cumsum_clamp_f16(v1));
|
||||
hvx_vec_store_a(&vdst[nvec], nloe * sizeof(__fp16), out);
|
||||
}
|
||||
|
||||
s->carry_bits = (uint32_t) Q6_R_vextract_VR(carry, 0);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per thread worker: Double-buffered DMA
|
||||
// Per thread worker: Double-buffered DMA pipeline
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static void cumsum_thread_f32_dma(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
htp_cumsum_preamble;
|
||||
static bool cumsum_next_task(void * ctx, struct dma_pipe_task * task) {
|
||||
struct cumsum_pipe_state * s = (struct cumsum_pipe_state *) ctx;
|
||||
if (s->row >= s->row_end) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t rem = s->ne00 - s->off;
|
||||
const uint32_t len = MIN(s->chunk_elems, rem);
|
||||
|
||||
task->row = s->row;
|
||||
task->off = s->off;
|
||||
task->len = len;
|
||||
task->aux = 0;
|
||||
|
||||
s->off += len;
|
||||
if (s->off >= s->ne00) {
|
||||
s->off = 0;
|
||||
s->row++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void cumsum_push_in(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad) {
|
||||
struct cumsum_pipe_state * s = (struct cumsum_pipe_state *) ctx;
|
||||
|
||||
const size_t bytes = (size_t) task->len * s->src_elem_size;
|
||||
const dma_addr_t src = s->src_data + (size_t) task->row * s->src_row_size + (size_t) task->off * s->src_elem_size;
|
||||
|
||||
dma_queue_push(dma_q, dma_make_data(in_spad, src), s->src_chunk_bytes_aligned, bytes, bytes, 1);
|
||||
}
|
||||
|
||||
static void cumsum_push_out(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad) {
|
||||
struct cumsum_pipe_state * s = (struct cumsum_pipe_state *) ctx;
|
||||
|
||||
const size_t bytes = (size_t) task->len * s->dst_elem_size;
|
||||
const dma_addr_t dst = s->dst_data + (size_t) task->row * s->dst_row_size + (size_t) task->off * s->dst_elem_size;
|
||||
|
||||
dma_queue_push(dma_q, dma_make_data(dst, out_spad), bytes, s->dst_chunk_bytes_aligned, bytes, 1);
|
||||
}
|
||||
|
||||
static void cumsum_thread(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
struct htp_cumsum_context * cctx = (struct htp_cumsum_context *) data;
|
||||
struct htp_ops_context * octx = cctx->octx;
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
const uint32_t ir0 = cctx->row_start + cctx->rows_per_hvx * ith;
|
||||
const uint32_t ir1 = MIN(ir0 + cctx->rows_per_hvx, cctx->row_start + cctx->total_rows);
|
||||
@@ -128,88 +275,36 @@ static void cumsum_thread_f32_dma(const htp_thread_t * th, unsigned int nth, uns
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t src_row_size = cctx->src_row_size;
|
||||
const size_t dst_row_size = cctx->dst_row_size;
|
||||
const size_t src_row_size_aligned = cctx->src_row_size_aligned;
|
||||
const size_t dst_row_size_aligned = cctx->dst_row_size_aligned;
|
||||
uint8_t * src_spad = cctx->vtcm_src0 + ith * cctx->src0_bytes_per_hvx;
|
||||
uint8_t * dst_spad = cctx->vtcm_dst + ith * cctx->dst_bytes_per_hvx;
|
||||
void * in_buf[2] = { src_spad, src_spad + cctx->src_chunk_bytes_aligned };
|
||||
void * out_buf[2] = { dst_spad, dst_spad + cctx->dst_chunk_bytes_aligned };
|
||||
|
||||
const dma_addr_t src_data = src0->data;
|
||||
const dma_addr_t dst_data = dst->data;
|
||||
struct cumsum_pipe_state s = {
|
||||
.src_data = src0->data,
|
||||
.dst_data = dst->data,
|
||||
.src_elem_size = cctx->src_elem_size,
|
||||
.dst_elem_size = cctx->dst_elem_size,
|
||||
.src_row_size = cctx->src_row_size,
|
||||
.dst_row_size = cctx->dst_row_size,
|
||||
.src_chunk_bytes_aligned = cctx->src_chunk_bytes_aligned,
|
||||
.dst_chunk_bytes_aligned = cctx->dst_chunk_bytes_aligned,
|
||||
.ne00 = src0->ne[0],
|
||||
.chunk_elems = cctx->chunk_elems,
|
||||
.row = ir0,
|
||||
.row_end = ir1,
|
||||
.off = 0,
|
||||
.carry_bits = 0,
|
||||
};
|
||||
|
||||
uint8_t * src_spad = octx->src0_spad.data + (ith * src_row_size_aligned * 2);
|
||||
uint8_t * dst_spad = octx->dst_spad.data + (ith * dst_row_size_aligned * 2);
|
||||
dma_pipe_io_run(th->dma_q, th->trace, &s, in_buf, out_buf, cumsum_next_task, cumsum_push_in, cctx->compute, cumsum_push_out);
|
||||
|
||||
for (uint32_t ir = ir0, spad_idx = 0; ir < ir1 && spad_idx < 2; ir++, spad_idx++) {
|
||||
// Dummy dst writeback to establish queue ordering
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(dst_data, dst_spad + (spad_idx * dst_row_size_aligned)),
|
||||
dst_row_size, dst_row_size_aligned, dst_row_size, 0);
|
||||
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(src_spad + (spad_idx * src_row_size_aligned),
|
||||
src_data + (ir * src_row_size)),
|
||||
src_row_size_aligned, src_row_size, src_row_size, 1);
|
||||
}
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
|
||||
for (uint32_t ir = ir0; ir < ir1; ir++) {
|
||||
float * dst_spad_row = (float *) dma_queue_pop(dma_q).src;
|
||||
float * src_spad_row = (float *) dma_queue_pop(dma_q).dst;
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir);
|
||||
hvx_cumsum_row_f32(src_spad_row, dst_spad_row, ne00);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir);
|
||||
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(dst_data + (ir * dst_row_size), dst_spad_row),
|
||||
dst_row_size, dst_row_size_aligned, dst_row_size, 1);
|
||||
|
||||
const uint32_t next_row = ir + 2;
|
||||
if (next_row < ir1) {
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(src_spad_row, src_data + (next_row * src_row_size)),
|
||||
src_row_size_aligned, src_row_size, src_row_size, 1);
|
||||
}
|
||||
}
|
||||
|
||||
dma_queue_flush(dma_q);
|
||||
|
||||
FARF(HIGH, "cumsum-f32-dma %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n",
|
||||
FARF(HIGH, "cumsum %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n",
|
||||
ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ir0, ir1,
|
||||
dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3]);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per thread worker: Direct HVX (no DMA)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static void cumsum_thread_f32(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
htp_cumsum_preamble;
|
||||
|
||||
const uint8_t * src_data = (const uint8_t *) src0->data;
|
||||
uint8_t * dst_data = (uint8_t *) dst->data;
|
||||
|
||||
const uint32_t ir0 = cctx->row_start + cctx->rows_per_hvx * ith;
|
||||
const uint32_t ir1 = MIN(ir0 + cctx->rows_per_hvx, cctx->row_start + cctx->total_rows);
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir0);
|
||||
|
||||
for (uint32_t ir = ir0; ir < ir1; ir++) {
|
||||
const float * restrict src_row = (const float *) (src_data + ir * cctx->src_row_size);
|
||||
float * restrict dst_row = (float *) (dst_data + ir * cctx->dst_row_size);
|
||||
hvx_cumsum_row_f32(src_row, dst_row, ne00);
|
||||
}
|
||||
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir0);
|
||||
|
||||
FARF(HIGH, "cumsum-f32 %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n",
|
||||
ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ir0, ir1,
|
||||
dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3]);
|
||||
}
|
||||
|
||||
int op_cumsum_f32(struct htp_ops_context * octx) {
|
||||
static int op_cumsum_impl(struct htp_ops_context * octx, size_t src_elem_size, size_t dst_elem_size, dma_pipe_compute_io_fn compute) {
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
const struct htp_cumsum_kernel_params * kparams =
|
||||
@@ -223,9 +318,7 @@ int op_cumsum_f32(struct htp_ops_context * octx) {
|
||||
uint32_t nrows = total_rows;
|
||||
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
uint32_t rows_per_chunk = 0;
|
||||
htp_tensor_mdev_rows_per_chunk(dst, sizeof(float), (uint32_t) dst_row_size, &rows_per_chunk);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_rows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_rows, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
row_start = range.start;
|
||||
nrows = range.count;
|
||||
}
|
||||
@@ -240,51 +333,48 @@ int op_cumsum_f32(struct htp_ops_context * octx) {
|
||||
|
||||
const uint32_t n_hvx = octx->n_hvx;
|
||||
|
||||
if (!kparams->use_dma && (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst))) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
struct htp_cumsum_vtcm_layout layout = { 0 };
|
||||
if (kparams->use_dma) {
|
||||
htp_cumsum_vtcm_layout_build(&layout, src_row_size, dst_row_size, n_hvx);
|
||||
|
||||
octx->src0_spad.size_per_hvx = layout.src0_bytes_per_hvx;
|
||||
octx->dst_spad.size_per_hvx = layout.dst_bytes_per_hvx;
|
||||
octx->src0_spad.size = n_hvx * layout.src0_bytes_per_hvx;
|
||||
octx->dst_spad.size = n_hvx * layout.dst_bytes_per_hvx;
|
||||
octx->src0_spad.data = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_src0);
|
||||
octx->dst_spad.data = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_dst);
|
||||
octx->src0_spad.src = NULL;
|
||||
octx->dst_spad.src = NULL;
|
||||
}
|
||||
htp_cumsum_vtcm_layout_build(&layout, (size_t) kparams->chunk_elems * src_elem_size, (size_t) kparams->chunk_elems * dst_elem_size, n_hvx);
|
||||
|
||||
struct htp_cumsum_context cctx = {
|
||||
.octx = octx,
|
||||
.src_row_size = src_row_size,
|
||||
.dst_row_size = dst_row_size,
|
||||
.src_row_size_aligned = layout.src_row_size_aligned,
|
||||
.dst_row_size_aligned = layout.dst_row_size_aligned,
|
||||
.rows_per_hvx = fastdiv(nrows + n_hvx - 1, &octx->n_hvx_div),
|
||||
.total_rows = nrows,
|
||||
.row_start = row_start,
|
||||
.octx = octx,
|
||||
.compute = compute,
|
||||
.vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_src0),
|
||||
.vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_dst),
|
||||
.src0_bytes_per_hvx = layout.src0_bytes_per_hvx,
|
||||
.dst_bytes_per_hvx = layout.dst_bytes_per_hvx,
|
||||
.src_elem_size = src_elem_size,
|
||||
.dst_elem_size = dst_elem_size,
|
||||
.src_row_size = src_row_size,
|
||||
.dst_row_size = dst_row_size,
|
||||
.src_chunk_bytes_aligned = layout.src_chunk_bytes_aligned,
|
||||
.dst_chunk_bytes_aligned = layout.dst_chunk_bytes_aligned,
|
||||
.chunk_elems = kparams->chunk_elems,
|
||||
.rows_per_hvx = fastdiv(nrows + n_hvx - 1, &octx->n_hvx_div),
|
||||
.total_rows = nrows,
|
||||
.row_start = row_start,
|
||||
};
|
||||
|
||||
if (kparams->use_dma) {
|
||||
work_queue_run(octx->ctx->hvx_queue, cumsum_thread_f32_dma, &cctx, n_hvx);
|
||||
} else {
|
||||
work_queue_run(octx->ctx->hvx_queue, cumsum_thread_f32, &cctx, n_hvx);
|
||||
}
|
||||
work_queue_run(octx->ctx->hvx_queue, cumsum_thread, &cctx, n_hvx);
|
||||
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
int op_cumsum(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
switch (dst->type) {
|
||||
case HTP_TYPE_F32:
|
||||
return op_cumsum_f32(octx);
|
||||
default:
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
if (src0->type == HTP_TYPE_F32 && dst->type == HTP_TYPE_F32) {
|
||||
return op_cumsum_impl(octx, sizeof(float), sizeof(float), cumsum_compute_f32_f32);
|
||||
}
|
||||
if (src0->type == HTP_TYPE_F16 && dst->type == HTP_TYPE_F16) {
|
||||
return op_cumsum_impl(octx, sizeof(__fp16), sizeof(__fp16), cumsum_compute_f16_f16);
|
||||
}
|
||||
if (src0->type == HTP_TYPE_F16 && dst->type == HTP_TYPE_F32) {
|
||||
return op_cumsum_impl(octx, sizeof(__fp16), sizeof(float), cumsum_compute_f16_f32);
|
||||
}
|
||||
if (src0->type == HTP_TYPE_F32 && dst->type == HTP_TYPE_F16) {
|
||||
return op_cumsum_impl(octx, sizeof(float), sizeof(__fp16), cumsum_compute_f32_f16);
|
||||
}
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
@@ -9,22 +9,22 @@ struct htp_cumsum_vtcm_layout {
|
||||
size_t total_bytes;
|
||||
size_t off_src0;
|
||||
size_t off_dst;
|
||||
size_t src_row_size_aligned;
|
||||
size_t dst_row_size_aligned;
|
||||
size_t src_chunk_bytes_aligned;
|
||||
size_t dst_chunk_bytes_aligned;
|
||||
size_t src0_bytes_per_hvx;
|
||||
size_t dst_bytes_per_hvx;
|
||||
};
|
||||
|
||||
static inline void htp_cumsum_vtcm_layout_build(
|
||||
struct htp_cumsum_vtcm_layout * L,
|
||||
size_t src_row_size,
|
||||
size_t dst_row_size,
|
||||
size_t src_chunk_bytes,
|
||||
size_t dst_chunk_bytes,
|
||||
uint32_t n_hvx
|
||||
) {
|
||||
L->src_row_size_aligned = hex_round_up(src_row_size, 128);
|
||||
L->dst_row_size_aligned = hex_round_up(dst_row_size, 128);
|
||||
L->src0_bytes_per_hvx = 2 * L->src_row_size_aligned;
|
||||
L->dst_bytes_per_hvx = 2 * L->dst_row_size_aligned;
|
||||
L->src_chunk_bytes_aligned = hex_round_up(src_chunk_bytes, 128);
|
||||
L->dst_chunk_bytes_aligned = hex_round_up(dst_chunk_bytes, 128);
|
||||
L->src0_bytes_per_hvx = 2 * L->src_chunk_bytes_aligned;
|
||||
L->dst_bytes_per_hvx = 2 * L->dst_chunk_bytes_aligned;
|
||||
|
||||
size_t off = 0;
|
||||
VTCM_LAYOUT_ALLOC(off, off_src0, L->src0_bytes_per_hvx * n_hvx);
|
||||
@@ -36,7 +36,7 @@ static inline void htp_cumsum_vtcm_layout_build(
|
||||
struct htp_cumsum_kernel_params {
|
||||
uint32_t n_hvx;
|
||||
uint32_t total_rows;
|
||||
uint32_t use_dma;
|
||||
uint32_t chunk_elems;
|
||||
uint32_t vtcm_size;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
#include <HAP_perf.h>
|
||||
|
||||
@@ -15,54 +13,147 @@
|
||||
#include "hex-utils.h"
|
||||
#include "hvx-copy.h"
|
||||
#include "dma-queue.h"
|
||||
#include "dma-pipeline.h"
|
||||
#include "diag-ops.h"
|
||||
|
||||
#define htp_diag_tensors_preamble \
|
||||
const struct htp_tensor * restrict src0 = octx->src[0]; \
|
||||
const struct htp_tensor * restrict dst = octx->dst; \
|
||||
\
|
||||
const uint32_t ne02 = src0->ne[2]; \
|
||||
\
|
||||
const uint32_t ne0 = dst->ne[0]; \
|
||||
const uint32_t ne1 = dst->ne[1]; \
|
||||
\
|
||||
const uint32_t nb02 = src0->nb[2]; \
|
||||
const uint32_t nb03 = src0->nb[3]; \
|
||||
\
|
||||
const uint32_t nb1 = dst->nb[1]; \
|
||||
const uint32_t nb2 = dst->nb[2]; \
|
||||
const uint32_t nb3 = dst->nb[3];
|
||||
|
||||
struct htp_diag_context {
|
||||
struct htp_ops_context * octx;
|
||||
struct htp_ops_context * octx;
|
||||
const struct htp_diag_kernel_params * kparams;
|
||||
size_t src_batch_size;
|
||||
size_t dst_row_size;
|
||||
size_t src_batch_size_aligned;
|
||||
size_t dst_row_size_aligned;
|
||||
uint32_t batches_per_hvx;
|
||||
uint32_t total_batches;
|
||||
uint32_t batch_start;
|
||||
dma_pipe_compute_io_fn compute;
|
||||
uint8_t * vtcm_src0;
|
||||
uint8_t * vtcm_dst;
|
||||
size_t src0_bytes_per_hvx;
|
||||
size_t dst_bytes_per_hvx;
|
||||
size_t elem_size;
|
||||
size_t dst_chunk_bytes_aligned;
|
||||
uint32_t chunk_elems;
|
||||
uint32_t batches_per_hvx;
|
||||
uint32_t total_batches;
|
||||
uint32_t batch_start;
|
||||
};
|
||||
|
||||
#define htp_diag_preamble \
|
||||
struct htp_diag_context * dctx = (struct htp_diag_context *) data; \
|
||||
struct htp_ops_context * octx = dctx->octx; \
|
||||
htp_diag_tensors_preamble;
|
||||
struct diag_pipe_state {
|
||||
dma_addr_t src_data;
|
||||
dma_addr_t dst_data;
|
||||
size_t elem_size;
|
||||
size_t dst_chunk_bytes_aligned;
|
||||
uint32_t ne0;
|
||||
uint32_t ne1;
|
||||
uint32_t ne02;
|
||||
uint32_t nb02;
|
||||
uint32_t nb03;
|
||||
uint32_t nb1;
|
||||
uint32_t nb2;
|
||||
uint32_t nb3;
|
||||
uint32_t chunk_elems;
|
||||
uint32_t ib;
|
||||
uint32_t ib_end;
|
||||
uint32_t i1;
|
||||
uint32_t off;
|
||||
const struct fastdiv_values * div_ne02;
|
||||
};
|
||||
|
||||
static inline void hvx_diag_row_f32(const float * restrict src, float * restrict dst,
|
||||
uint32_t row_idx, uint32_t n) {
|
||||
hvx_splat_f32_a((uint8_t *) dst, 0.0f, n);
|
||||
dst[row_idx] = src[row_idx];
|
||||
static void __attribute__((noinline)) diag_compute_f32(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad) {
|
||||
(void) ctx;
|
||||
|
||||
uint8_t * restrict dst = (uint8_t *) out_spad;
|
||||
const uint8_t * restrict src = (const uint8_t *) in_spad;
|
||||
|
||||
hvx_splat_f32_a(dst, 0.0f, task->len);
|
||||
|
||||
if (task->row >= task->off && task->row < task->off + task->len) {
|
||||
const uint32_t local_idx = task->row - task->off;
|
||||
const uint32_t vec_idx = local_idx >> 5;
|
||||
const uint32_t b_off = (local_idx & 31u) * sizeof(float);
|
||||
const HVX_Vector src_v = *((const HVX_Vector *) src);
|
||||
const HVX_Vector val_v = Q6_V_vsplat_R(Q6_R_vextract_VR(src_v, 0));
|
||||
const HVX_VectorPred b_mask = Q6_Q_vsetq_R(b_off);
|
||||
const HVX_VectorPred e_mask = Q6_Q_vsetq2_R(b_off + sizeof(float));
|
||||
Q6_vmem_QnRIV(Q6_Q_or_QQn(b_mask, e_mask), &((HVX_Vector *) dst)[vec_idx], val_v);
|
||||
}
|
||||
}
|
||||
|
||||
static void __attribute__((noinline)) diag_compute_f16(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad) {
|
||||
(void) ctx;
|
||||
|
||||
uint8_t * restrict dst = (uint8_t *) out_spad;
|
||||
const uint8_t * restrict src = (const uint8_t *) in_spad;
|
||||
|
||||
hvx_splat_u16_a(dst, 0, task->len);
|
||||
|
||||
if (task->row >= task->off && task->row < task->off + task->len) {
|
||||
const uint32_t local_idx = task->row - task->off;
|
||||
const uint32_t vec_idx = local_idx >> 6;
|
||||
const uint32_t b_off = (local_idx & 63u) * sizeof(__fp16);
|
||||
const HVX_Vector src_v = *((const HVX_Vector *) src);
|
||||
const HVX_Vector val_v = Q6_Vh_vsplat_R(Q6_R_vextract_VR(src_v, 0));
|
||||
const HVX_VectorPred b_mask = Q6_Q_vsetq_R(b_off);
|
||||
const HVX_VectorPred e_mask = Q6_Q_vsetq2_R(b_off + sizeof(__fp16));
|
||||
Q6_vmem_QnRIV(Q6_Q_or_QQn(b_mask, e_mask), &((HVX_Vector *) dst)[vec_idx], val_v);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per thread worker: DMA src fetch, compute in VTCM, DMA dst writeback
|
||||
// Per thread worker: Double-buffered DMA pipeline
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static void diag_thread_f32_dma(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
htp_diag_preamble;
|
||||
dma_queue * dma_q = th->dma_q;
|
||||
static bool diag_next_task(void * ctx, struct dma_pipe_task * task) {
|
||||
struct diag_pipe_state * s = (struct diag_pipe_state *) ctx;
|
||||
if (s->ib >= s->ib_end) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t rem = s->ne0 - s->off;
|
||||
const uint32_t len = MIN(s->chunk_elems, rem);
|
||||
|
||||
task->row = s->i1;
|
||||
task->off = s->off;
|
||||
task->len = len;
|
||||
task->aux = s->ib;
|
||||
|
||||
s->off += len;
|
||||
if (s->off >= s->ne0) {
|
||||
s->off = 0;
|
||||
s->i1++;
|
||||
if (s->i1 >= s->ne1) {
|
||||
s->i1 = 0;
|
||||
s->ib++;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void diag_push_in(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad) {
|
||||
struct diag_pipe_state * s = (struct diag_pipe_state *) ctx;
|
||||
|
||||
if (task->row < task->off || task->row >= task->off + task->len) {
|
||||
dma_queue_push(dma_q, dma_make_data(in_spad, 0), 0, 0, 0, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
const uint32_t i3 = fastdiv(task->aux, s->div_ne02);
|
||||
const uint32_t i2 = task->aux - i3 * s->ne02;
|
||||
const dma_addr_t src_elem = s->src_data + (size_t) i3 * s->nb03 + (size_t) i2 * s->nb02 + (size_t) task->row * s->elem_size;
|
||||
|
||||
dma_queue_push(dma_q, dma_make_data(in_spad, src_elem), s->elem_size, s->elem_size, s->elem_size, 1);
|
||||
}
|
||||
|
||||
static void diag_push_out(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad) {
|
||||
struct diag_pipe_state * s = (struct diag_pipe_state *) ctx;
|
||||
|
||||
const uint32_t i3 = fastdiv(task->aux, s->div_ne02);
|
||||
const uint32_t i2 = task->aux - i3 * s->ne02;
|
||||
const size_t bytes = (size_t) task->len * s->elem_size;
|
||||
const dma_addr_t dst = s->dst_data + (size_t) i3 * s->nb3 + (size_t) i2 * s->nb2 + (size_t) task->row * s->nb1 + (size_t) task->off * s->elem_size;
|
||||
|
||||
dma_queue_push(dma_q, dma_make_data(dst, out_spad), bytes, s->dst_chunk_bytes_aligned, bytes, 1);
|
||||
}
|
||||
|
||||
static void diag_thread(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
struct htp_diag_context * dctx = (struct htp_diag_context *) data;
|
||||
struct htp_ops_context * octx = dctx->octx;
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
const uint32_t ib0 = dctx->batch_start + dctx->batches_per_hvx * ith;
|
||||
const uint32_t ib1 = MIN(ib0 + dctx->batches_per_hvx, dctx->batch_start + dctx->total_batches);
|
||||
@@ -71,93 +162,40 @@ static void diag_thread_f32_dma(const htp_thread_t * th, unsigned int nth, unsig
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t src_batch_size = dctx->src_batch_size;
|
||||
const size_t dst_row_size = dctx->dst_row_size;
|
||||
const size_t src_batch_size_aligned = dctx->src_batch_size_aligned;
|
||||
const size_t dst_row_size_aligned = dctx->dst_row_size_aligned;
|
||||
uint8_t * src_spad = dctx->vtcm_src0 + ith * dctx->src0_bytes_per_hvx;
|
||||
uint8_t * dst_spad = dctx->vtcm_dst + ith * dctx->dst_bytes_per_hvx;
|
||||
void * in_buf[2] = { src_spad, src_spad + 128 };
|
||||
void * out_buf[2] = { dst_spad, dst_spad + dctx->dst_chunk_bytes_aligned };
|
||||
|
||||
const dma_addr_t src_data = src0->data;
|
||||
const dma_addr_t dst_data = dst->data;
|
||||
struct diag_pipe_state s = {
|
||||
.src_data = src0->data,
|
||||
.dst_data = dst->data,
|
||||
.elem_size = dctx->elem_size,
|
||||
.dst_chunk_bytes_aligned = dctx->dst_chunk_bytes_aligned,
|
||||
.ne0 = dst->ne[0],
|
||||
.ne1 = dst->ne[1],
|
||||
.ne02 = src0->ne[2],
|
||||
.nb02 = src0->nb[2],
|
||||
.nb03 = src0->nb[3],
|
||||
.nb1 = dst->nb[1],
|
||||
.nb2 = dst->nb[2],
|
||||
.nb3 = dst->nb[3],
|
||||
.chunk_elems = dctx->chunk_elems,
|
||||
.ib = ib0,
|
||||
.ib_end = ib1,
|
||||
.i1 = 0,
|
||||
.off = 0,
|
||||
.div_ne02 = &dctx->kparams->div_ne02,
|
||||
};
|
||||
|
||||
// 1 src buffer + 1 dst row buffer per thread in VTCM
|
||||
uint8_t * src_spad = octx->src0_spad.data + (ith * src_batch_size_aligned);
|
||||
uint8_t * dst_spad = octx->dst_spad.data + (ith * dst_row_size_aligned);
|
||||
dma_pipe_io_run(th->dma_q, th->trace, &s, in_buf, out_buf, diag_next_task, diag_push_in, dctx->compute, diag_push_out);
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
|
||||
for (uint32_t ib = ib0; ib < ib1; ib++) {
|
||||
const uint32_t i3 = fastdiv(ib, &dctx->kparams->div_ne02);
|
||||
const uint32_t i2 = ib - i3 * ne02;
|
||||
|
||||
const dma_addr_t src_batch = src_data + i3 * nb03 + i2 * nb02;
|
||||
|
||||
// Fetch source vector into VTCM
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(src_spad, src_batch),
|
||||
src_batch_size_aligned, src_batch_size, src_batch_size, 1);
|
||||
dma_queue_flush(dma_q);
|
||||
|
||||
const float * src_spad_f32 = (const float *) src_spad;
|
||||
float * dst_spad_f32 = (float *) dst_spad;
|
||||
|
||||
for (uint32_t i1 = 0; i1 < ne1; i1++) {
|
||||
// Compute row in VTCM
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) (ib * ne1 + i1));
|
||||
hvx_diag_row_f32(src_spad_f32, dst_spad_f32, i1, ne0);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) (ib * ne1 + i1));
|
||||
|
||||
// Write completed row back to DDR
|
||||
const dma_addr_t dst_row = dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1;
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(dst_row, dst_spad),
|
||||
dst_row_size, dst_row_size_aligned, dst_row_size, 1);
|
||||
dma_queue_flush(dma_q);
|
||||
}
|
||||
}
|
||||
|
||||
FARF(HIGH, "diag-f32-dma %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n",
|
||||
FARF(HIGH, "diag %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n",
|
||||
ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ib0, ib1,
|
||||
dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3]);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per thread worker: Direct HVX (no DMA)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static void diag_thread_f32(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
htp_diag_preamble;
|
||||
|
||||
const uint8_t * src_data = (const uint8_t *) src0->data;
|
||||
uint8_t * dst_data = (uint8_t *) dst->data;
|
||||
|
||||
const uint32_t ib0 = dctx->batch_start + dctx->batches_per_hvx * ith;
|
||||
const uint32_t ib1 = MIN(ib0 + dctx->batches_per_hvx, dctx->batch_start + dctx->total_batches);
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ib0);
|
||||
|
||||
for (uint32_t ib = ib0; ib < ib1; ib++) {
|
||||
const uint32_t i3 = fastdiv(ib, &dctx->kparams->div_ne02);
|
||||
const uint32_t i2 = ib - i3 * ne02;
|
||||
|
||||
const float * restrict src_batch = (const float *)(src_data + i3 * nb03 + i2 * nb02);
|
||||
|
||||
for (uint32_t i1 = 0; i1 < ne1; i1++) {
|
||||
float * restrict dst_row = (float *)(dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
|
||||
hvx_diag_row_f32(src_batch, dst_row, i1, ne0);
|
||||
}
|
||||
}
|
||||
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ib0);
|
||||
|
||||
FARF(HIGH, "diag-f32 %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n",
|
||||
ith, nth, src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], ib0, ib1,
|
||||
dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3]);
|
||||
}
|
||||
|
||||
int op_diag_f32(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
static int op_diag_impl(struct htp_ops_context * octx, size_t elem_size, dma_pipe_compute_io_fn compute) {
|
||||
const struct htp_diag_kernel_params * kparams =
|
||||
(const struct htp_diag_kernel_params *) octx->kernel_params;
|
||||
|
||||
@@ -167,8 +205,7 @@ int op_diag_f32(struct htp_ops_context * octx) {
|
||||
uint32_t nbatches = total_batches;
|
||||
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
const bool can_split = (kparams->can_split != 0) && htp_tensor_mdev_data_aligned(dst);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_batches, can_split ? kparams->batches_per_chunk : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_batches, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
batch_start = range.start;
|
||||
nbatches = range.count;
|
||||
}
|
||||
@@ -183,61 +220,44 @@ int op_diag_f32(struct htp_ops_context * octx) {
|
||||
|
||||
const uint32_t n_hvx = octx->n_hvx;
|
||||
|
||||
if (!kparams->use_dma && (htp_tensor_is_extended(src0) || htp_tensor_is_extended(dst))) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const size_t src_batch_size = src0->ne[0] * sizeof(float);
|
||||
const size_t dst_row_size = dst->ne[0] * sizeof(float);
|
||||
|
||||
struct htp_diag_vtcm_layout layout = { 0 };
|
||||
if (kparams->use_dma) {
|
||||
htp_diag_vtcm_layout_build(&layout, src_batch_size, dst_row_size, n_hvx);
|
||||
|
||||
octx->src0_spad.size_per_hvx = layout.src0_bytes_per_hvx;
|
||||
octx->dst_spad.size_per_hvx = layout.dst_bytes_per_hvx;
|
||||
octx->src0_spad.size = n_hvx * layout.src0_bytes_per_hvx;
|
||||
octx->dst_spad.size = n_hvx * layout.dst_bytes_per_hvx;
|
||||
octx->src0_spad.data = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_src0);
|
||||
octx->dst_spad.data = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_dst);
|
||||
octx->src0_spad.src = NULL;
|
||||
octx->dst_spad.src = NULL;
|
||||
}
|
||||
htp_diag_vtcm_layout_build(&layout, (size_t) kparams->chunk_elems * elem_size, n_hvx);
|
||||
|
||||
struct htp_diag_context dctx = {
|
||||
.octx = octx,
|
||||
.kparams = kparams,
|
||||
.src_batch_size = src_batch_size,
|
||||
.dst_row_size = dst_row_size,
|
||||
.src_batch_size_aligned = layout.src_batch_size_aligned,
|
||||
.dst_row_size_aligned = layout.dst_row_size_aligned,
|
||||
.batches_per_hvx = fastdiv(nbatches + n_hvx - 1, &octx->n_hvx_div),
|
||||
.total_batches = nbatches,
|
||||
.batch_start = batch_start,
|
||||
.octx = octx,
|
||||
.kparams = kparams,
|
||||
.compute = compute,
|
||||
.vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_src0),
|
||||
.vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_dst),
|
||||
.src0_bytes_per_hvx = layout.src0_bytes_per_hvx,
|
||||
.dst_bytes_per_hvx = layout.dst_bytes_per_hvx,
|
||||
.elem_size = elem_size,
|
||||
.dst_chunk_bytes_aligned = layout.dst_chunk_bytes_aligned,
|
||||
.chunk_elems = kparams->chunk_elems,
|
||||
.batches_per_hvx = fastdiv(nbatches + n_hvx - 1, &octx->n_hvx_div),
|
||||
.total_batches = nbatches,
|
||||
.batch_start = batch_start,
|
||||
};
|
||||
|
||||
if (kparams->use_dma) {
|
||||
work_queue_run(octx->ctx->hvx_queue, diag_thread_f32_dma, &dctx, n_hvx);
|
||||
} else {
|
||||
work_queue_run(octx->ctx->hvx_queue, diag_thread_f32, &dctx, n_hvx);
|
||||
}
|
||||
work_queue_run(octx->ctx->hvx_queue, diag_thread, &dctx, n_hvx);
|
||||
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
int op_diag(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
int err = HTP_STATUS_OK;
|
||||
if (src0->type != dst->type) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
switch (dst->type) {
|
||||
case HTP_TYPE_F32:
|
||||
err = op_diag_f32(octx);
|
||||
break;
|
||||
return op_diag_impl(octx, sizeof(float), diag_compute_f32);
|
||||
case HTP_TYPE_F16:
|
||||
return op_diag_impl(octx, sizeof(__fp16), diag_compute_f16);
|
||||
default:
|
||||
err = HTP_STATUS_NO_SUPPORT;
|
||||
break;
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -10,22 +10,19 @@ struct htp_diag_vtcm_layout {
|
||||
size_t total_bytes;
|
||||
size_t off_src0;
|
||||
size_t off_dst;
|
||||
size_t src_batch_size_aligned;
|
||||
size_t dst_row_size_aligned;
|
||||
size_t dst_chunk_bytes_aligned;
|
||||
size_t src0_bytes_per_hvx;
|
||||
size_t dst_bytes_per_hvx;
|
||||
};
|
||||
|
||||
static inline void htp_diag_vtcm_layout_build(
|
||||
struct htp_diag_vtcm_layout * L,
|
||||
size_t src_batch_size,
|
||||
size_t dst_row_size,
|
||||
size_t dst_chunk_bytes,
|
||||
uint32_t n_hvx
|
||||
) {
|
||||
L->src_batch_size_aligned = hex_round_up(src_batch_size, 128);
|
||||
L->dst_row_size_aligned = hex_round_up(dst_row_size, 128);
|
||||
L->src0_bytes_per_hvx = L->src_batch_size_aligned;
|
||||
L->dst_bytes_per_hvx = L->dst_row_size_aligned;
|
||||
L->dst_chunk_bytes_aligned = hex_round_up(dst_chunk_bytes, 128);
|
||||
L->src0_bytes_per_hvx = 2 * 128;
|
||||
L->dst_bytes_per_hvx = 2 * L->dst_chunk_bytes_aligned;
|
||||
|
||||
size_t off = 0;
|
||||
VTCM_LAYOUT_ALLOC(off, off_src0, L->src0_bytes_per_hvx * n_hvx);
|
||||
@@ -37,9 +34,7 @@ static inline void htp_diag_vtcm_layout_build(
|
||||
struct htp_diag_kernel_params {
|
||||
uint32_t n_hvx;
|
||||
uint32_t total_batches;
|
||||
uint32_t use_dma;
|
||||
uint32_t can_split;
|
||||
uint32_t batches_per_chunk;
|
||||
uint32_t chunk_elems;
|
||||
uint32_t vtcm_size;
|
||||
|
||||
struct fastdiv_values div_ne02;
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
#ifndef HTP_DMA_PIPELINE_H
|
||||
#define HTP_DMA_PIPELINE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "dma-queue.h"
|
||||
#include "htp-trace.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct dma_pipe_task {
|
||||
uint32_t row;
|
||||
uint32_t off;
|
||||
uint32_t len;
|
||||
uint32_t aux;
|
||||
};
|
||||
|
||||
typedef bool (*dma_pipe_next_task_fn)(void * ctx, struct dma_pipe_task * task);
|
||||
typedef void (*dma_pipe_push_in_fn)(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad);
|
||||
typedef void (*dma_pipe_push_in2_fn)(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in0_spad, void * in1_spad);
|
||||
typedef void (*dma_pipe_compute_io_fn)(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad);
|
||||
typedef void (*dma_pipe_compute_io2_fn)(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in0_spad, void * in1_spad);
|
||||
typedef void (*dma_pipe_compute_1_fn)(void * ctx, const struct dma_pipe_task * task, void * spad);
|
||||
typedef void (*dma_pipe_push_out_fn)(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad);
|
||||
|
||||
// Double-buffered DDR -> VTCM -> HVX -> VTCM -> DDR pipeline.
|
||||
// Uses dummy output pushes in the prologue and dma_queue_pop().src/.dst to
|
||||
// recycle VTCM slot pointers directly through the DMA ring.
|
||||
// push_in/push_out must always push exactly one descriptor per task (use a
|
||||
// zero-nrows dummy push carrying the VTCM pointer when skipping a transfer).
|
||||
static inline void dma_pipe_io_run(
|
||||
dma_queue * dma_q,
|
||||
struct htp_thread_trace * tr,
|
||||
void * ctx,
|
||||
void * in_buf[2],
|
||||
void * out_buf[2],
|
||||
dma_pipe_next_task_fn next_task,
|
||||
dma_pipe_push_in_fn push_in,
|
||||
dma_pipe_compute_io_fn compute,
|
||||
dma_pipe_push_out_fn push_out
|
||||
) {
|
||||
struct dma_pipe_task t[2];
|
||||
uint32_t head = 0, tail = 0;
|
||||
|
||||
for (uint32_t b = 0; b < 2; ++b) {
|
||||
if (!next_task(ctx, &t[tail & 1])) {
|
||||
break;
|
||||
}
|
||||
dma_queue_push(dma_q, dma_make_data(0, out_buf[b]), 0, 0, 0, 0);
|
||||
push_in(dma_q, ctx, &t[tail & 1], in_buf[b]);
|
||||
tail++;
|
||||
}
|
||||
|
||||
while (head != tail) {
|
||||
const struct dma_pipe_task cur = t[head & 1];
|
||||
head++;
|
||||
|
||||
void * out_spad = (void *) (uintptr_t) dma_queue_pop(dma_q).src;
|
||||
void * in_spad = (void *) (uintptr_t) dma_queue_pop(dma_q).dst;
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) cur.row);
|
||||
compute(ctx, &cur, out_spad, in_spad);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) cur.row);
|
||||
|
||||
push_out(dma_q, ctx, &cur, out_spad);
|
||||
|
||||
if (next_task(ctx, &t[tail & 1])) {
|
||||
push_in(dma_q, ctx, &t[tail & 1], in_spad);
|
||||
tail++;
|
||||
}
|
||||
}
|
||||
|
||||
dma_queue_flush(dma_q);
|
||||
}
|
||||
|
||||
// Double-buffered 2-input DDR -> VTCM -> HVX -> VTCM -> DDR pipeline.
|
||||
static inline void dma_pipe_io2_run(
|
||||
dma_queue * dma_q,
|
||||
struct htp_thread_trace * tr,
|
||||
void * ctx,
|
||||
void * in0_buf[2],
|
||||
void * in1_buf[2],
|
||||
void * out_buf[2],
|
||||
dma_pipe_next_task_fn next_task,
|
||||
dma_pipe_push_in2_fn push_in,
|
||||
dma_pipe_compute_io2_fn compute,
|
||||
dma_pipe_push_out_fn push_out
|
||||
) {
|
||||
struct dma_pipe_task t[2];
|
||||
uint32_t head = 0, tail = 0;
|
||||
|
||||
for (uint32_t b = 0; b < 2; ++b) {
|
||||
if (!next_task(ctx, &t[tail & 1])) {
|
||||
break;
|
||||
}
|
||||
dma_queue_push(dma_q, dma_make_data(0, out_buf[b]), 0, 0, 0, 0);
|
||||
push_in(dma_q, ctx, &t[tail & 1], in0_buf[b], in1_buf[b]);
|
||||
tail++;
|
||||
}
|
||||
|
||||
while (head != tail) {
|
||||
const struct dma_pipe_task cur = t[head & 1];
|
||||
head++;
|
||||
|
||||
void * out_spad = (void *) (uintptr_t) dma_queue_pop(dma_q).src;
|
||||
void * in0_spad = (void *) (uintptr_t) dma_queue_pop(dma_q).dst;
|
||||
void * in1_spad = (void *) (uintptr_t) dma_queue_pop(dma_q).dst;
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) cur.row);
|
||||
compute(ctx, &cur, out_spad, in0_spad, in1_spad);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) cur.row);
|
||||
|
||||
push_out(dma_q, ctx, &cur, out_spad);
|
||||
|
||||
if (next_task(ctx, &t[tail & 1])) {
|
||||
push_in(dma_q, ctx, &t[tail & 1], in0_spad, in1_spad);
|
||||
tail++;
|
||||
}
|
||||
}
|
||||
|
||||
dma_queue_flush(dma_q);
|
||||
}
|
||||
|
||||
// Double-buffered DDR -> VTCM -> HVX pipeline for input-only reductions.
|
||||
static inline void dma_pipe_in_run(
|
||||
dma_queue * dma_q,
|
||||
struct htp_thread_trace * tr,
|
||||
void * ctx,
|
||||
void * in_buf[2],
|
||||
dma_pipe_next_task_fn next_task,
|
||||
dma_pipe_push_in_fn push_in,
|
||||
dma_pipe_compute_1_fn compute
|
||||
) {
|
||||
struct dma_pipe_task t[2];
|
||||
uint32_t head = 0, tail = 0;
|
||||
|
||||
for (uint32_t b = 0; b < 2; ++b) {
|
||||
if (!next_task(ctx, &t[tail & 1])) {
|
||||
break;
|
||||
}
|
||||
push_in(dma_q, ctx, &t[tail & 1], in_buf[b]);
|
||||
tail++;
|
||||
}
|
||||
|
||||
while (head != tail) {
|
||||
const struct dma_pipe_task cur = t[head & 1];
|
||||
head++;
|
||||
|
||||
void * in_spad = (void *) (uintptr_t) dma_queue_pop(dma_q).dst;
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) cur.row);
|
||||
compute(ctx, &cur, in_spad);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) cur.row);
|
||||
|
||||
if (next_task(ctx, &t[tail & 1])) {
|
||||
push_in(dma_q, ctx, &t[tail & 1], in_spad);
|
||||
tail++;
|
||||
}
|
||||
}
|
||||
|
||||
dma_queue_flush(dma_q);
|
||||
}
|
||||
|
||||
// Bounded in-flight HVX -> VTCM -> DDR pipeline for output-only generators.
|
||||
static inline void dma_pipe_out_run(
|
||||
dma_queue * dma_q,
|
||||
struct htp_thread_trace * tr,
|
||||
void * ctx,
|
||||
void * const * out_bufs,
|
||||
uint32_t n_bufs,
|
||||
dma_pipe_next_task_fn next_task,
|
||||
dma_pipe_compute_1_fn compute,
|
||||
dma_pipe_push_out_fn push_out
|
||||
) {
|
||||
for (uint32_t b = 0; b < n_bufs; ++b) {
|
||||
dma_queue_push(dma_q, dma_make_data(0, out_bufs[b]), 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
struct dma_pipe_task cur;
|
||||
while (next_task(ctx, &cur)) {
|
||||
void * out_spad = (void *) (uintptr_t) dma_queue_pop(dma_q).src;
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) cur.row);
|
||||
compute(ctx, &cur, out_spad);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) cur.row);
|
||||
|
||||
push_out(dma_q, ctx, &cur, out_spad);
|
||||
}
|
||||
|
||||
dma_queue_flush(dma_q);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif // HTP_DMA_PIPELINE_H
|
||||
@@ -0,0 +1,188 @@
|
||||
#include "dma-proxy.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <qurt_thread.h>
|
||||
#include <qurt_futex.h>
|
||||
|
||||
#define QURT_LOWEST_PRIO (254)
|
||||
|
||||
static bool __attribute__((noinline)) dma_proxy_reap_completed(dma_proxy_t p, bool wait_one) {
|
||||
dma_ring * r = p->th.dma_q->ring0;
|
||||
uint32_t id = atomic_load_explicit(&p->idx_done, memory_order_relaxed);
|
||||
if (id == p->idx_submit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool reaped = false;
|
||||
dmpoll();
|
||||
#pragma clang loop vectorize(disable) interleave(disable)
|
||||
while (id != p->idx_submit) {
|
||||
volatile dma_descriptor_2d * hw_desc = &r->desc[r->pop_idx];
|
||||
if (!hw_desc->done) {
|
||||
if (!wait_one) {
|
||||
break;
|
||||
}
|
||||
while (!hw_desc->done) {
|
||||
dmpoll();
|
||||
}
|
||||
}
|
||||
dma_ring_pop_nowait(r);
|
||||
id++;
|
||||
atomic_store_explicit(&p->idx_done, id, memory_order_release);
|
||||
wait_one = false;
|
||||
reaped = true;
|
||||
}
|
||||
return reaped;
|
||||
}
|
||||
|
||||
static void dma_proxy_thread(void * arg) {
|
||||
dma_proxy_t p = (dma_proxy_t) arg;
|
||||
|
||||
FARF(HIGH, "dma-proxy-thread (tid %u): started", p->th.tid);
|
||||
|
||||
const uint32_t max_inflight = dma_queue_capacity(p->th.dma_q) - 1;
|
||||
unsigned int poll_cnt = DMA_PROXY_POLL_COUNT;
|
||||
unsigned int prev_seqn = 0;
|
||||
|
||||
while (1) {
|
||||
bool did_work = false;
|
||||
|
||||
uint32_t iw = atomic_load_explicit(&p->idx_write, memory_order_acquire);
|
||||
#pragma clang loop vectorize(disable) interleave(disable)
|
||||
while (p->idx_submit != iw) {
|
||||
uint32_t id = atomic_load_explicit(&p->idx_done, memory_order_relaxed);
|
||||
if ((p->idx_submit - id) >= max_inflight) {
|
||||
dma_proxy_reap_completed(p, true);
|
||||
}
|
||||
const struct dma_proxy_desc * d = &p->desc[p->idx_submit & p->idx_mask];
|
||||
const size_t nrows = (d->nrows != 0 && d->row_size != 0) ? d->nrows : 0;
|
||||
dma_queue_push(p->th.dma_q, d->ddata, d->dst_stride, d->src_stride, d->row_size, nrows);
|
||||
p->idx_submit++;
|
||||
did_work = true;
|
||||
}
|
||||
|
||||
if (atomic_load_explicit(&p->idx_done, memory_order_relaxed) != p->idx_submit) {
|
||||
if (!dma_proxy_reap_completed(p, false) && !did_work) {
|
||||
hex_pause();
|
||||
}
|
||||
poll_cnt = DMA_PROXY_POLL_COUNT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (did_work) {
|
||||
poll_cnt = DMA_PROXY_POLL_COUNT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (atomic_load_explicit(&p->killed, memory_order_acquire)) {
|
||||
break;
|
||||
}
|
||||
|
||||
unsigned int seqn = atomic_load_explicit(&p->seqn, memory_order_acquire);
|
||||
if (seqn != prev_seqn) {
|
||||
prev_seqn = seqn;
|
||||
poll_cnt = DMA_PROXY_POLL_COUNT;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (--poll_cnt) {
|
||||
hex_pause();
|
||||
continue;
|
||||
}
|
||||
|
||||
FARF(HIGH, "dma-proxy-thread: sleeping");
|
||||
qurt_futex_wait(&p->seqn, prev_seqn);
|
||||
poll_cnt = DMA_PROXY_POLL_COUNT;
|
||||
}
|
||||
|
||||
FARF(HIGH, "dma-proxy-thread (tid %u): stopped", p->th.tid);
|
||||
}
|
||||
|
||||
size_t dma_proxy_sizeof(size_t capacity, uint32_t stack_size) {
|
||||
capacity = hex_ceil_pow2(capacity > 0 ? capacity : 1);
|
||||
size_t size_p = hex_align_up(sizeof(struct dma_proxy_s), HEX_L2_LINE_SIZE);
|
||||
size_t size_desc = hex_align_up(capacity * sizeof(struct dma_proxy_desc), HEX_L2_LINE_SIZE);
|
||||
size_t size_stack = stack_size;
|
||||
return size_p + size_desc + size_stack;
|
||||
}
|
||||
|
||||
size_t dma_proxy_alignof(void) {
|
||||
return HEX_L2_LINE_SIZE;
|
||||
}
|
||||
|
||||
dma_proxy_t dma_proxy_init(void * ptr, size_t capacity, uint32_t stack_size, dma_queue_t * dma, struct htp_thread_trace * trace) {
|
||||
capacity = hex_ceil_pow2(capacity > 0 ? capacity : 1);
|
||||
size_t size_p = hex_align_up(sizeof(struct dma_proxy_s), HEX_L2_LINE_SIZE);
|
||||
size_t size_desc = hex_align_up(capacity * sizeof(struct dma_proxy_desc), HEX_L2_LINE_SIZE);
|
||||
|
||||
uint8_t * block = (uint8_t *) ptr;
|
||||
|
||||
dma_proxy_t p = (dma_proxy_t) block;
|
||||
block += size_p;
|
||||
memset(p, 0, sizeof(struct dma_proxy_s));
|
||||
|
||||
p->capacity = (uint32_t) capacity;
|
||||
p->idx_mask = (uint32_t) (capacity - 1);
|
||||
p->idx_pop = 0;
|
||||
p->idx_submit = 0;
|
||||
atomic_init(&p->idx_write, 0);
|
||||
atomic_init(&p->idx_done, 0);
|
||||
atomic_init(&p->seqn, 0);
|
||||
atomic_init(&p->killed, false);
|
||||
|
||||
p->desc = (struct dma_proxy_desc *) block;
|
||||
block += size_desc;
|
||||
memset(p->desc, 0, capacity * sizeof(struct dma_proxy_desc));
|
||||
|
||||
p->stack = block;
|
||||
memset(p->stack, 0, stack_size);
|
||||
|
||||
htp_thread_init(&p->th, dma, trace);
|
||||
|
||||
int prio = qurt_thread_get_priority(qurt_thread_get_id());
|
||||
if (prio < 1) {
|
||||
prio = 1;
|
||||
}
|
||||
if (prio > QURT_LOWEST_PRIO) {
|
||||
prio = QURT_LOWEST_PRIO;
|
||||
}
|
||||
|
||||
qurt_thread_attr_t attr;
|
||||
qurt_thread_attr_init(&attr);
|
||||
qurt_thread_attr_set_stack_addr(&attr, p->stack);
|
||||
qurt_thread_attr_set_stack_size(&attr, stack_size);
|
||||
qurt_thread_attr_set_priority(&attr, prio);
|
||||
qurt_thread_attr_set_name(&attr, "dma-proxy");
|
||||
|
||||
int err = qurt_thread_create(&p->thread, &attr, dma_proxy_thread, p);
|
||||
if (err) {
|
||||
FARF(ERROR, "dma-proxy: thread create failed (%d)", err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
FARF(HIGH, "dma-proxy: capacity %u\n", (unsigned) capacity);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void dma_proxy_free(dma_proxy_t p) {
|
||||
if (!p) {
|
||||
return;
|
||||
}
|
||||
|
||||
dma_proxy_flush(p);
|
||||
atomic_store_explicit(&p->killed, true, memory_order_release);
|
||||
|
||||
unsigned int seqn = atomic_load_explicit(&p->seqn, memory_order_relaxed) + 1;
|
||||
atomic_store_explicit(&p->seqn, seqn, memory_order_release);
|
||||
qurt_futex_wake(&p->seqn, 1);
|
||||
|
||||
if (p->thread) {
|
||||
int status = 0;
|
||||
(void) qurt_thread_join(p->thread, &status);
|
||||
p->thread = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
#ifndef HTP_DMA_PROXY_H
|
||||
#define HTP_DMA_PROXY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
#include <qurt_thread.h>
|
||||
#include <qurt_futex.h>
|
||||
#include <HAP_farf.h>
|
||||
|
||||
#include "dma-queue.h"
|
||||
#include "hex-utils.h"
|
||||
#include "htp-thread.h"
|
||||
#include "htp-trace.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if __HVX_ARCH__ > 79
|
||||
#define DMA_PROXY_POLL_COUNT 2000
|
||||
#else
|
||||
#define DMA_PROXY_POLL_COUNT 1
|
||||
#endif
|
||||
|
||||
struct dma_proxy_desc {
|
||||
dma_data ddata;
|
||||
uint32_t dst_stride;
|
||||
uint32_t src_stride;
|
||||
uint32_t row_size;
|
||||
uint32_t nrows;
|
||||
};
|
||||
|
||||
struct dma_proxy_s {
|
||||
struct dma_proxy_desc * desc;
|
||||
atomic_uint idx_write; // monotonic, updated by producer (push)
|
||||
uint32_t idx_pop; // monotonic, updated by producer (pop)
|
||||
uint32_t idx_mask;
|
||||
uint32_t capacity;
|
||||
atomic_uint seqn; // incremented on push/kill, used with futex
|
||||
atomic_bool killed;
|
||||
|
||||
atomic_uint idx_done __attribute__((aligned(HEX_L2_LINE_SIZE))); // monotonic, updated by consumer
|
||||
uint32_t idx_submit; // monotonic, private to consumer
|
||||
|
||||
qurt_thread_t thread;
|
||||
htp_thread_t th;
|
||||
void * stack;
|
||||
};
|
||||
|
||||
typedef struct dma_proxy_s * dma_proxy_t;
|
||||
|
||||
size_t dma_proxy_sizeof(size_t capacity, uint32_t stack_size);
|
||||
size_t dma_proxy_alignof(void);
|
||||
dma_proxy_t dma_proxy_init(void * ptr, size_t capacity, uint32_t stack_size, dma_queue_t * dma, struct htp_thread_trace * trace);
|
||||
void dma_proxy_free(dma_proxy_t p);
|
||||
|
||||
static inline bool dma_proxy_empty(dma_proxy_t p) {
|
||||
return p->idx_pop == atomic_load_explicit(&p->idx_write, memory_order_relaxed);
|
||||
}
|
||||
|
||||
static inline bool dma_proxy_push(dma_proxy_t p, dma_data ddata, size_t dst_stride, size_t src_stride, size_t row_size, size_t nrows) {
|
||||
uint32_t iw = atomic_load_explicit(&p->idx_write, memory_order_relaxed);
|
||||
if ((iw - p->idx_pop) >= p->capacity) {
|
||||
FARF(HIGH, "dma-proxy-push: queue is full\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
struct dma_proxy_desc * d = &p->desc[iw & p->idx_mask];
|
||||
d->ddata = ddata;
|
||||
d->dst_stride = (uint32_t) dst_stride;
|
||||
d->src_stride = (uint32_t) src_stride;
|
||||
d->row_size = (uint32_t) row_size;
|
||||
d->nrows = (uint32_t) nrows;
|
||||
|
||||
atomic_store_explicit(&p->idx_write, iw + 1, memory_order_release);
|
||||
|
||||
// wake up proxy thread
|
||||
unsigned int seqn = atomic_load_explicit(&p->seqn, memory_order_relaxed) + 1;
|
||||
atomic_store_explicit(&p->seqn, seqn, memory_order_release);
|
||||
qurt_futex_wake(&p->seqn, 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline dma_data dma_proxy_pop(dma_proxy_t p) {
|
||||
uint32_t ip = p->idx_pop;
|
||||
uint32_t iw = atomic_load_explicit(&p->idx_write, memory_order_relaxed);
|
||||
|
||||
dma_data ddata = { 0 };
|
||||
if (ip == iw) {
|
||||
return ddata;
|
||||
}
|
||||
|
||||
while ((int32_t) (atomic_load_explicit(&p->idx_done, memory_order_relaxed) - ip) <= 0) {
|
||||
hex_pause();
|
||||
}
|
||||
atomic_thread_fence(memory_order_acquire);
|
||||
|
||||
ddata = p->desc[ip & p->idx_mask].ddata;
|
||||
p->idx_pop = ip + 1;
|
||||
return ddata;
|
||||
}
|
||||
|
||||
static inline void dma_proxy_flush(dma_proxy_t p) {
|
||||
while (!dma_proxy_empty(p)) {
|
||||
dma_proxy_pop(p);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif /* HTP_DMA_PROXY_H */
|
||||
@@ -4,8 +4,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
|
||||
static inline uint32_t pow2_ceil(uint32_t x) {
|
||||
if (x <= 1) {
|
||||
return 1;
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
|
||||
#include <stdint.h>
|
||||
@@ -51,6 +48,117 @@ static inline HVX_Vector dsv4_hc_sigmoid_f32(HVX_Vector v, HVX_Vector max_exp, H
|
||||
return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(v3, r));
|
||||
}
|
||||
|
||||
typedef void (*dsv4_hc_unpack_fn_t)(uint8_t * p);
|
||||
|
||||
static void dsv4_hc_unpack_noop(uint8_t * p) {
|
||||
(void) p;
|
||||
}
|
||||
|
||||
static void dsv4_hc_unpack_f16_0(uint8_t * p) {
|
||||
*(HVX_Vector *) p = Q6_V_lo_W(hvx_vec_f16_to_f32(*(const HVX_Vector *) p));
|
||||
}
|
||||
|
||||
static void dsv4_hc_unpack_f16_128(uint8_t * p) {
|
||||
*(HVX_Vector *) (p + 128) = Q6_V_lo_W(hvx_vec_f16_to_f32(*(const HVX_Vector *) (p + 128)));
|
||||
}
|
||||
|
||||
static void dsv4_hc_unpack_f16_0_128(uint8_t * p) {
|
||||
*(HVX_Vector *) p = Q6_V_lo_W(hvx_vec_f16_to_f32(*(const HVX_Vector *) p));
|
||||
*(HVX_Vector *) (p + 128) = Q6_V_lo_W(hvx_vec_f16_to_f32(*(const HVX_Vector *) (p + 128)));
|
||||
}
|
||||
|
||||
#define DSV4_HC_LOAD2_f32(lo, hi, ptr) \
|
||||
do { \
|
||||
lo = *(const HVX_Vector *) (ptr); \
|
||||
hi = *(const HVX_Vector *) ((ptr) + VLEN); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_LOAD2_f16(lo, hi, ptr) \
|
||||
do { \
|
||||
const HVX_VectorPair _p = hvx_vec_f16_to_f32(*(const HVX_Vector *) (ptr)); \
|
||||
lo = Q6_V_lo_W(_p); \
|
||||
hi = Q6_V_hi_W(_p); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_LOAD2_f16s(lo, hi, ptr) \
|
||||
do { \
|
||||
const HVX_VectorPair _p = hvx_vec_f16_to_f32_shuff(*(const HVX_Vector *) (ptr)); \
|
||||
lo = Q6_V_lo_W(_p); \
|
||||
hi = Q6_V_hi_W(_p); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_STORE2_f32(ptr, lo, hi) \
|
||||
do { \
|
||||
*(HVX_Vector *) (ptr) = (lo); \
|
||||
*(HVX_Vector *) ((ptr) + VLEN) = (hi); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_STORE2_f16(ptr, lo, hi) \
|
||||
do { \
|
||||
*(HVX_Vector *) (ptr) = hvx_vec_f32_to_f16((lo), (hi)); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_STORE2_f16s(ptr, lo, hi) \
|
||||
do { \
|
||||
*(HVX_Vector *) (ptr) = hvx_vec_f32_to_f16_shuff((lo), (hi)); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_STORE2_TAIL_f16s(ptr, lo, hi, has_hi) \
|
||||
do { \
|
||||
(void) (has_hi); \
|
||||
*(HVX_Vector *) (ptr) = hvx_vec_f32_to_f16_shuff((lo), (hi)); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_STORE2_TAIL_f32s(ptr, lo, hi, has_hi) \
|
||||
do { \
|
||||
const HVX_VectorPair _p = Q6_W_vshuff_VVR((hi), (lo), -4); \
|
||||
*(HVX_Vector *) (ptr) = Q6_V_lo_W(_p); \
|
||||
if (has_hi) { \
|
||||
*(HVX_Vector *) ((ptr) + VLEN) = Q6_V_hi_W(_p); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_LOAD1_f32(v, ptr) \
|
||||
do { \
|
||||
v = *(const HVX_Vector *) (ptr); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_LOAD1_f16(v, ptr) \
|
||||
do { \
|
||||
v = Q6_V_lo_W(hvx_vec_f16_to_f32(*(const HVX_Vector *) (ptr))); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_LOAD1_f16s(v, ptr) DSV4_HC_LOAD1_f16(v, ptr)
|
||||
|
||||
#define DSV4_HC_STORE1_f32(ptr, v) \
|
||||
do { \
|
||||
*(HVX_Vector *) (ptr) = (v); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_STORE1_f16(ptr, v) \
|
||||
do { \
|
||||
*(HVX_Vector *) (ptr) = hvx_vec_f32_to_f16((v), (v)); \
|
||||
} while (0)
|
||||
|
||||
#define DSV4_HC_STORE1_f16s(ptr, v) DSV4_HC_STORE1_f16(ptr, v)
|
||||
|
||||
struct dsv4_hc_pre_context;
|
||||
struct dsv4_hc_block;
|
||||
|
||||
typedef void (*dsv4_hc_pre_plain_fn_t)(
|
||||
const struct dsv4_hc_pre_context * dctx,
|
||||
const struct dsv4_hc_block * block,
|
||||
const uint8_t * x_spad,
|
||||
uint8_t * d_spad,
|
||||
const float * wb);
|
||||
|
||||
typedef void (*dsv4_hc_pre_gated_fn_t)(
|
||||
const struct dsv4_hc_pre_context * dctx,
|
||||
const struct dsv4_hc_block * block,
|
||||
const uint8_t * x_spad,
|
||||
const uint8_t * g_spad,
|
||||
uint8_t * d_spad);
|
||||
|
||||
struct dsv4_hc_pre_context {
|
||||
struct htp_ops_context * octx;
|
||||
const struct htp_dsv4_hc_pre_kernel_params * kparams;
|
||||
@@ -63,6 +171,13 @@ struct dsv4_hc_pre_context {
|
||||
uint32_t dma_block_chunks;
|
||||
uint32_t dma_row_stride;
|
||||
uint32_t dma_buffer_size;
|
||||
uint32_t x_chunk_bytes;
|
||||
uint32_t w_chunk_bytes;
|
||||
uint32_t w_row_bytes;
|
||||
uint32_t dst_chunk_bytes;
|
||||
dsv4_hc_unpack_fn_t unpack_weights;
|
||||
dsv4_hc_pre_plain_fn_t compute_plain;
|
||||
dsv4_hc_pre_gated_fn_t compute_gated;
|
||||
};
|
||||
|
||||
struct dsv4_hc_block {
|
||||
@@ -93,10 +208,12 @@ static inline void dsv4_hc_pre_push_plain(
|
||||
const struct htp_tensor * x = dctx->octx->src[0];
|
||||
const struct htp_tensor * weights = dctx->octx->src[1];
|
||||
const uint32_t row_stride = dctx->dma_row_stride;
|
||||
const uint32_t row_size = block->count * VLEN;
|
||||
const uint32_t x_cb = dctx->x_chunk_bytes;
|
||||
const uint32_t w_bytes = dctx->w_row_bytes;
|
||||
const uint32_t x_size = block->count * x_cb;
|
||||
|
||||
dma_queue_push(dma_q, dma_make_data(spad, x->data + (block->it*x->nb[2] + block->chunk*VLEN)), row_stride, x->nb[1], row_size, 4);
|
||||
dma_queue_push(dma_q, dma_make_data(spad + 5*row_stride, weights->data + (block->it*weights->nb[1])), 16, 16, 16, 1);
|
||||
dma_queue_push(dma_q, dma_make_data(spad, x->data + (block->it*x->nb[2] + block->chunk*x_cb)), row_stride, x->nb[1], x_size, 4);
|
||||
dma_queue_push(dma_q, dma_make_data(spad + 5*row_stride, weights->data + (block->it*weights->nb[1])), w_bytes, w_bytes, w_bytes, 1);
|
||||
}
|
||||
|
||||
static inline void dsv4_hc_pre_push_gated(
|
||||
@@ -107,78 +224,203 @@ static inline void dsv4_hc_pre_push_gated(
|
||||
const struct htp_tensor * x = dctx->octx->src[0];
|
||||
const struct htp_tensor * g = dctx->octx->src[1];
|
||||
const uint32_t row_stride = dctx->dma_row_stride;
|
||||
const uint32_t row_size = block->count * VLEN;
|
||||
const uint32_t x_cb = dctx->x_chunk_bytes;
|
||||
const uint32_t g_cb = dctx->w_chunk_bytes;
|
||||
const uint32_t x_size = block->count * x_cb;
|
||||
const uint32_t g_size = block->count * g_cb;
|
||||
|
||||
dma_queue_push(dma_q, dma_make_data(spad, x->data + (block->it*x->nb[2] + block->chunk*VLEN)), row_stride, x->nb[1], row_size, 4);
|
||||
dma_queue_push(dma_q, dma_make_data(spad + 4*row_stride, g->data + (block->it*g->nb[2] + block->chunk*VLEN)), row_stride, g->nb[1], row_size, 4);
|
||||
dma_queue_push(dma_q, dma_make_data(spad, x->data + (block->it*x->nb[2] + block->chunk*x_cb)), row_stride, x->nb[1], x_size, 4);
|
||||
dma_queue_push(dma_q, dma_make_data(spad + 4*row_stride, g->data + (block->it*g->nb[2] + block->chunk*g_cb)), row_stride, g->nb[1], g_size, 4);
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void dsv4_hc_pre_plain_block_f32(
|
||||
const struct dsv4_hc_pre_context * dctx,
|
||||
const struct dsv4_hc_block * block,
|
||||
const uint8_t * x_spad,
|
||||
uint8_t * d_spad,
|
||||
const float * wb) {
|
||||
const uint32_t row_stride = dctx->dma_row_stride;
|
||||
const HVX_Vector scale_v = hvx_vec_splat_f32(dctx->scale);
|
||||
const HVX_Vector w0 = hvx_vec_splat_f32(wb[0]);
|
||||
const HVX_Vector w1 = hvx_vec_splat_f32(wb[1]);
|
||||
const HVX_Vector w2 = hvx_vec_splat_f32(wb[2]);
|
||||
const HVX_Vector w3 = hvx_vec_splat_f32(wb[3]);
|
||||
|
||||
for (uint32_t c = 0; c < block->count; ++c) {
|
||||
const uint8_t * xb = x_spad + c*VLEN;
|
||||
|
||||
HVX_Vector x0 = *(const HVX_Vector *) xb;
|
||||
HVX_Vector x1 = *(const HVX_Vector *) (xb + row_stride);
|
||||
HVX_Vector x2 = *(const HVX_Vector *) (xb + 2*row_stride);
|
||||
HVX_Vector x3 = *(const HVX_Vector *) (xb + 3*row_stride);
|
||||
|
||||
HVX_Vector acc = hvx_vec_mul_f32_f32(x0, w0);
|
||||
acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(x1, w1));
|
||||
acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(x2, w2));
|
||||
acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(x3, w3));
|
||||
|
||||
*(HVX_Vector *) (d_spad + c*VLEN) = hvx_vec_mul_f32_f32(acc, scale_v);
|
||||
}
|
||||
#define DEFINE_DSV4_HC_PRE_PLAIN_BLOCK(SUFFIX, TX, TDST, X_CB, D_CB) \
|
||||
static __attribute__((noinline)) void dsv4_hc_pre_plain_block_##SUFFIX( \
|
||||
const struct dsv4_hc_pre_context * dctx, \
|
||||
const struct dsv4_hc_block * block, \
|
||||
const uint8_t * x_spad, \
|
||||
uint8_t * d_spad, \
|
||||
const float * wb) { \
|
||||
const uint32_t row_stride = dctx->dma_row_stride; \
|
||||
const float scale = dctx->scale; \
|
||||
const HVX_Vector w0 = hvx_vec_splat_f32(wb[0] * scale); \
|
||||
const HVX_Vector w1 = hvx_vec_splat_f32(wb[1] * scale); \
|
||||
const HVX_Vector w2 = hvx_vec_splat_f32(wb[2] * scale); \
|
||||
const HVX_Vector w3 = hvx_vec_splat_f32(wb[3] * scale); \
|
||||
const uint32_t count = block->count; \
|
||||
uint32_t c = 0; \
|
||||
for (; c + 2 <= count; c += 2) { \
|
||||
const uint8_t * xb = x_spad + c * (X_CB); \
|
||||
HVX_Vector x0, x1; \
|
||||
DSV4_HC_LOAD2_##TX(x0, x1, xb); \
|
||||
HVX_Vector acc0 = hvx_vec_mul_f32_f32(x0, w0); \
|
||||
HVX_Vector acc1 = hvx_vec_mul_f32_f32(x1, w0); \
|
||||
DSV4_HC_LOAD2_##TX(x0, x1, xb + row_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(x0, w1)); \
|
||||
acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(x1, w1)); \
|
||||
DSV4_HC_LOAD2_##TX(x0, x1, xb + 2 * row_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(x0, w2)); \
|
||||
acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(x1, w2)); \
|
||||
DSV4_HC_LOAD2_##TX(x0, x1, xb + 3 * row_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(x0, w3)); \
|
||||
acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(x1, w3)); \
|
||||
DSV4_HC_STORE2_##TDST(d_spad + c * (D_CB), acc0, acc1); \
|
||||
} \
|
||||
if (c < count) { \
|
||||
const uint8_t * xb = x_spad + c * (X_CB); \
|
||||
HVX_Vector x0; \
|
||||
DSV4_HC_LOAD1_##TX(x0, xb); \
|
||||
HVX_Vector acc0 = hvx_vec_mul_f32_f32(x0, w0); \
|
||||
DSV4_HC_LOAD1_##TX(x0, xb + row_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(x0, w1)); \
|
||||
DSV4_HC_LOAD1_##TX(x0, xb + 2 * row_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(x0, w2)); \
|
||||
DSV4_HC_LOAD1_##TX(x0, xb + 3 * row_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(x0, w3)); \
|
||||
DSV4_HC_STORE1_##TDST(d_spad + c * (D_CB), acc0); \
|
||||
} \
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void dsv4_hc_pre_gated_block_f32(
|
||||
const struct dsv4_hc_pre_context * dctx,
|
||||
const struct dsv4_hc_block * block,
|
||||
const uint8_t * x_spad,
|
||||
const uint8_t * g_spad,
|
||||
uint8_t * d_spad) {
|
||||
const uint32_t row_stride = dctx->dma_row_stride;
|
||||
const HVX_Vector scale_v = hvx_vec_splat_f32(dctx->scale);
|
||||
const HVX_Vector max_exp = hvx_vec_splat_f32(87.0f);
|
||||
const HVX_Vector min_exp = hvx_vec_splat_f32(-87.0f);
|
||||
DEFINE_DSV4_HC_PRE_PLAIN_BLOCK(f32_f32, f32, f32, 128, 128)
|
||||
DEFINE_DSV4_HC_PRE_PLAIN_BLOCK(f32_f16, f32, f16, 128, 64)
|
||||
DEFINE_DSV4_HC_PRE_PLAIN_BLOCK(f16_f32, f16, f32, 64, 128)
|
||||
DEFINE_DSV4_HC_PRE_PLAIN_BLOCK(f16_f16, f16s, f16s, 64, 64)
|
||||
|
||||
for (uint32_t c = 0; c < block->count; ++c) {
|
||||
const uint8_t * xb = x_spad + c*VLEN;
|
||||
const uint8_t * gb = g_spad + c*VLEN;
|
||||
#define DSV4_HC_SIGMOID2_f32(lo, hi, ptr, max_exp, min_exp) \
|
||||
do { \
|
||||
lo = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) (ptr), max_exp, min_exp); \
|
||||
hi = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) ((ptr) + VLEN), max_exp, min_exp); \
|
||||
} while (0)
|
||||
|
||||
HVX_Vector xv = *(const HVX_Vector *) xb;
|
||||
HVX_Vector gv = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) gb, max_exp, min_exp);
|
||||
HVX_Vector acc = hvx_vec_mul_f32_f32(xv, gv);
|
||||
#define DSV4_HC_SIGMOID2_f16(lo, hi, ptr, max_exp, min_exp) \
|
||||
do { \
|
||||
(void) (max_exp); \
|
||||
(void) (min_exp); \
|
||||
const HVX_VectorPair _p = hvx_vec_f16_to_f32(hvx_vec_fast_sigmoid_f16(*(const HVX_Vector *) (ptr))); \
|
||||
lo = Q6_V_lo_W(_p); \
|
||||
hi = Q6_V_hi_W(_p); \
|
||||
} while (0)
|
||||
|
||||
xv = *(const HVX_Vector *) (xb + row_stride);
|
||||
gv = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) (gb + row_stride), max_exp, min_exp);
|
||||
acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(xv, gv));
|
||||
#define DSV4_HC_SIGMOID1_f32(v, ptr, max_exp, min_exp) \
|
||||
do { \
|
||||
v = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) (ptr), max_exp, min_exp); \
|
||||
} while (0)
|
||||
|
||||
xv = *(const HVX_Vector *) (xb + 2*row_stride);
|
||||
gv = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) (gb + 2*row_stride), max_exp, min_exp);
|
||||
acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(xv, gv));
|
||||
#define DSV4_HC_SIGMOID1_f16(v, ptr, max_exp, min_exp) \
|
||||
do { \
|
||||
(void) (max_exp); \
|
||||
(void) (min_exp); \
|
||||
v = Q6_V_lo_W(hvx_vec_f16_to_f32(hvx_vec_fast_sigmoid_f16(*(const HVX_Vector *) (ptr)))); \
|
||||
} while (0)
|
||||
|
||||
xv = *(const HVX_Vector *) (xb + 3*row_stride);
|
||||
gv = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) (gb + 3*row_stride), max_exp, min_exp);
|
||||
acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(xv, gv));
|
||||
#define DSV4_HC_PRE_GATED_STREAM2(TX, TG, xb, gb, off, acc0, acc1, max_exp, min_exp) \
|
||||
do { \
|
||||
const uint8_t * _xp = (xb) + (off); \
|
||||
const uint8_t * _gp = (gb) + (off); \
|
||||
asm volatile("" : "+v"(acc0), "+v"(acc1), "+r"(_xp), "+r"(_gp)); \
|
||||
HVX_Vector _x0, _x1, _g0, _g1; \
|
||||
DSV4_HC_LOAD2_##TX(_x0, _x1, _xp); \
|
||||
DSV4_HC_SIGMOID2_##TG(_g0, _g1, _gp, max_exp, min_exp); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(_x0, _g0)); \
|
||||
acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(_x1, _g1)); \
|
||||
} while (0)
|
||||
|
||||
*(HVX_Vector *) (d_spad + c*VLEN) = hvx_vec_mul_f32_f32(acc, scale_v);
|
||||
}
|
||||
#define DEFINE_DSV4_HC_PRE_GATED_BLOCK(SUFFIX, TX, TG, TDST, X_CB, G_CB, D_CB) \
|
||||
static __attribute__((noinline)) void dsv4_hc_pre_gated_block_##SUFFIX( \
|
||||
const struct dsv4_hc_pre_context * dctx, \
|
||||
const struct dsv4_hc_block * block, \
|
||||
const uint8_t * x_spad, \
|
||||
const uint8_t * g_spad, \
|
||||
uint8_t * d_spad) { \
|
||||
const uint32_t row_stride = dctx->dma_row_stride; \
|
||||
const HVX_Vector scale_v = hvx_vec_splat_f32(dctx->scale); \
|
||||
const HVX_Vector max_exp = hvx_vec_splat_f32(87.0f); \
|
||||
const HVX_Vector min_exp = hvx_vec_splat_f32(-87.0f); \
|
||||
const uint32_t count = block->count; \
|
||||
uint32_t c = 0; \
|
||||
for (; c + 2 <= count; c += 2) { \
|
||||
const uint8_t * xb = x_spad + c * (X_CB); \
|
||||
const uint8_t * gb = g_spad + c * (G_CB); \
|
||||
HVX_Vector x0, x1, g0, g1; \
|
||||
DSV4_HC_LOAD2_##TX(x0, x1, xb); \
|
||||
DSV4_HC_SIGMOID2_##TG(g0, g1, gb, max_exp, min_exp); \
|
||||
HVX_Vector acc0 = hvx_vec_mul_f32_f32(x0, g0); \
|
||||
HVX_Vector acc1 = hvx_vec_mul_f32_f32(x1, g1); \
|
||||
DSV4_HC_PRE_GATED_STREAM2(TX, TG, xb, gb, row_stride, acc0, acc1, max_exp, \
|
||||
min_exp); \
|
||||
DSV4_HC_PRE_GATED_STREAM2(TX, TG, xb, gb, 2 * row_stride, acc0, acc1, max_exp, \
|
||||
min_exp); \
|
||||
DSV4_HC_PRE_GATED_STREAM2(TX, TG, xb, gb, 3 * row_stride, acc0, acc1, max_exp, \
|
||||
min_exp); \
|
||||
acc0 = hvx_vec_mul_f32_f32(acc0, scale_v); \
|
||||
acc1 = hvx_vec_mul_f32_f32(acc1, scale_v); \
|
||||
DSV4_HC_STORE2_##TDST(d_spad + c * (D_CB), acc0, acc1); \
|
||||
} \
|
||||
if (c < count) { \
|
||||
const uint8_t * xb = x_spad + c * (X_CB); \
|
||||
const uint8_t * gb = g_spad + c * (G_CB); \
|
||||
HVX_Vector x0, g0; \
|
||||
DSV4_HC_LOAD1_##TX(x0, xb); \
|
||||
DSV4_HC_SIGMOID1_##TG(g0, gb, max_exp, min_exp); \
|
||||
HVX_Vector acc0 = hvx_vec_mul_f32_f32(x0, g0); \
|
||||
DSV4_HC_LOAD1_##TX(x0, xb + row_stride); \
|
||||
DSV4_HC_SIGMOID1_##TG(g0, gb + row_stride, max_exp, min_exp); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(x0, g0)); \
|
||||
DSV4_HC_LOAD1_##TX(x0, xb + 2 * row_stride); \
|
||||
DSV4_HC_SIGMOID1_##TG(g0, gb + 2 * row_stride, max_exp, min_exp); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(x0, g0)); \
|
||||
DSV4_HC_LOAD1_##TX(x0, xb + 3 * row_stride); \
|
||||
DSV4_HC_SIGMOID1_##TG(g0, gb + 3 * row_stride, max_exp, min_exp); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(x0, g0)); \
|
||||
acc0 = hvx_vec_mul_f32_f32(acc0, scale_v); \
|
||||
DSV4_HC_STORE1_##TDST(d_spad + c * (D_CB), acc0); \
|
||||
} \
|
||||
}
|
||||
|
||||
static void dsv4_hc_pre_thread_plain_f32(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
#define DSV4_HC_PRE_GATED_EVAL_F16(xb, gb, row_stride, scale_v, acc0, acc1) \
|
||||
do { \
|
||||
const uint8_t * _xb = (xb); \
|
||||
const uint8_t * _gb = (gb); \
|
||||
const HVX_Vector _s0 = hvx_vec_fast_sigmoid_f16(*(const HVX_Vector *) _gb); \
|
||||
const HVX_Vector _s1 = hvx_vec_fast_sigmoid_f16(*(const HVX_Vector *) (_gb + (row_stride))); \
|
||||
const HVX_Vector _s2 = hvx_vec_fast_sigmoid_f16(*(const HVX_Vector *) (_gb + 2 * (row_stride))); \
|
||||
const HVX_Vector _s3 = hvx_vec_fast_sigmoid_f16(*(const HVX_Vector *) (_gb + 3 * (row_stride))); \
|
||||
asm volatile("" : "+r"(_xb) : "v"(_s0), "v"(_s1), "v"(_s2), "v"(_s3)); \
|
||||
HVX_VectorPair _acc = hvx_vec_mpy_f32_f16(*(const HVX_Vector *) _xb, _s0); \
|
||||
_acc = hvx_vec_mpyacc_f32_f16(_acc, *(const HVX_Vector *) (_xb + (row_stride)), _s1); \
|
||||
_acc = hvx_vec_mpyacc_f32_f16(_acc, *(const HVX_Vector *) (_xb + 2 * (row_stride)), _s2); \
|
||||
_acc = hvx_vec_mpyacc_f32_f16(_acc, *(const HVX_Vector *) (_xb + 3 * (row_stride)), _s3); \
|
||||
acc0 = hvx_vec_mul_f32_f32(Q6_V_lo_W(_acc), scale_v); \
|
||||
acc1 = hvx_vec_mul_f32_f32(Q6_V_hi_W(_acc), scale_v); \
|
||||
} while (0)
|
||||
|
||||
#define DEFINE_DSV4_HC_PRE_GATED_F16_BLOCK(SUFFIX, TDST, D_CB) \
|
||||
static __attribute__((noinline)) void dsv4_hc_pre_gated_block_##SUFFIX( \
|
||||
const struct dsv4_hc_pre_context * dctx, \
|
||||
const struct dsv4_hc_block * block, \
|
||||
const uint8_t * x_spad, \
|
||||
const uint8_t * g_spad, \
|
||||
uint8_t * d_spad) { \
|
||||
const uint32_t row_stride = dctx->dma_row_stride; \
|
||||
const HVX_Vector scale_v = hvx_vec_splat_f32(dctx->scale); \
|
||||
const uint32_t count = block->count; \
|
||||
for (uint32_t c = 0; c < count; c += 2) { \
|
||||
HVX_Vector acc0, acc1; \
|
||||
DSV4_HC_PRE_GATED_EVAL_F16(x_spad + c * 64, g_spad + c * 64, row_stride, \
|
||||
scale_v, acc0, acc1); \
|
||||
DSV4_HC_STORE2_TAIL_##TDST(d_spad + c * (D_CB), acc0, acc1, c + 1 < count); \
|
||||
} \
|
||||
}
|
||||
|
||||
DEFINE_DSV4_HC_PRE_GATED_BLOCK(f32_f32_f32, f32, f32, f32, 128, 128, 128)
|
||||
DEFINE_DSV4_HC_PRE_GATED_BLOCK(f32_f32_f16, f32, f32, f16, 128, 128, 64)
|
||||
DEFINE_DSV4_HC_PRE_GATED_BLOCK(f32_f16_f32, f32, f16, f32, 128, 64, 128)
|
||||
DEFINE_DSV4_HC_PRE_GATED_BLOCK(f32_f16_f16, f32, f16, f16, 128, 64, 64)
|
||||
DEFINE_DSV4_HC_PRE_GATED_BLOCK(f16_f32_f32, f16, f32, f32, 64, 128, 128)
|
||||
DEFINE_DSV4_HC_PRE_GATED_BLOCK(f16_f32_f16, f16, f32, f16, 64, 128, 64)
|
||||
DEFINE_DSV4_HC_PRE_GATED_F16_BLOCK(f16_f16_f32, f32s, 128)
|
||||
DEFINE_DSV4_HC_PRE_GATED_F16_BLOCK(f16_f16_f16, f16s, 64)
|
||||
|
||||
static void dsv4_hc_pre_thread_plain(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
struct dsv4_hc_pre_context * dctx = (struct dsv4_hc_pre_context *) data;
|
||||
struct htp_ops_context * octx = dctx->octx;
|
||||
|
||||
@@ -187,6 +429,7 @@ static void dsv4_hc_pre_thread_plain_f32(const htp_thread_t * th, unsigned int n
|
||||
|
||||
const uint32_t row_stride = dctx->dma_row_stride;
|
||||
const uint32_t buffer_size = dctx->dma_buffer_size;
|
||||
const uint32_t dst_cb = dctx->dst_chunk_bytes;
|
||||
const uint32_t start = dctx->jobs_per_hvx * ith;
|
||||
const uint32_t end = hex_smin(start + dctx->jobs_per_hvx, dctx->total_jobs);
|
||||
if (start >= end) {
|
||||
@@ -202,13 +445,16 @@ static void dsv4_hc_pre_thread_plain_f32(const htp_thread_t * th, unsigned int n
|
||||
uint32_t prefetch_job = start;
|
||||
for (uint32_t i = 0; i < 2 && prefetch_job < end; ++i) {
|
||||
const struct dsv4_hc_block block = dsv4_hc_pre_block_make(dctx, prefetch_job, end);
|
||||
const uint32_t d_bytes = block.count * dst_cb;
|
||||
// empty dst writeback keeps pop order dst, x, weights in loop
|
||||
dma_queue_push(dma_q, dma_make_data(d_data, spad[i] + 4*row_stride), block.count*VLEN, block.count*VLEN, 0, 0);
|
||||
dma_queue_push(dma_q, dma_make_data(d_data, spad[i] + 4*row_stride), d_bytes, d_bytes, 0, 0);
|
||||
dsv4_hc_pre_push_plain(dma_q, dctx, &block, spad[i]);
|
||||
prefetch_job += block.count;
|
||||
}
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
const dsv4_hc_unpack_fn_t unpack = dctx->unpack_weights;
|
||||
const dsv4_hc_pre_plain_fn_t compute = dctx->compute_plain;
|
||||
|
||||
uint32_t job = start;
|
||||
uint32_t curr = 0;
|
||||
@@ -219,15 +465,17 @@ static void dsv4_hc_pre_thread_plain_f32(const htp_thread_t * th, unsigned int n
|
||||
dma_queue_pop(dma_q);
|
||||
dma_queue_pop(dma_q);
|
||||
|
||||
const float * wb = (const float *) (spad[curr] + 5*row_stride);
|
||||
uint8_t * w_spad = spad[curr] + 5*row_stride;
|
||||
uint8_t * d_spad = spad[curr] + 4*row_stride;
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it);
|
||||
dsv4_hc_pre_plain_block_f32(dctx, &block, spad[curr], d_spad, wb);
|
||||
unpack(w_spad);
|
||||
compute(dctx, &block, spad[curr], d_spad, (const float *) w_spad);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it);
|
||||
|
||||
const uint64_t d_dst = d_data + (block.it*dst->nb[1] + block.chunk*VLEN);
|
||||
dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), block.count*VLEN, block.count*VLEN, block.count*VLEN, 1);
|
||||
const uint32_t d_bytes = block.count * dst_cb;
|
||||
const uint64_t d_dst = d_data + (block.it*dst->nb[1] + block.chunk*dst_cb);
|
||||
dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), d_bytes, d_bytes, d_bytes, 1);
|
||||
|
||||
if (prefetch_job < end) {
|
||||
const struct dsv4_hc_block next = dsv4_hc_pre_block_make(dctx, prefetch_job, end);
|
||||
@@ -244,7 +492,7 @@ static void dsv4_hc_pre_thread_plain_f32(const htp_thread_t * th, unsigned int n
|
||||
(void) nth;
|
||||
}
|
||||
|
||||
static void dsv4_hc_pre_thread_gated_f32(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
static void dsv4_hc_pre_thread_gated(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
struct dsv4_hc_pre_context * dctx = (struct dsv4_hc_pre_context *) data;
|
||||
struct htp_ops_context * octx = dctx->octx;
|
||||
|
||||
@@ -253,6 +501,7 @@ static void dsv4_hc_pre_thread_gated_f32(const htp_thread_t * th, unsigned int n
|
||||
|
||||
const uint32_t row_stride = dctx->dma_row_stride;
|
||||
const uint32_t buffer_size = dctx->dma_buffer_size;
|
||||
const uint32_t dst_cb = dctx->dst_chunk_bytes;
|
||||
const uint32_t start = dctx->jobs_per_hvx * ith;
|
||||
const uint32_t end = hex_smin(start + dctx->jobs_per_hvx, dctx->total_jobs);
|
||||
if (start >= end) {
|
||||
@@ -268,13 +517,15 @@ static void dsv4_hc_pre_thread_gated_f32(const htp_thread_t * th, unsigned int n
|
||||
uint32_t prefetch_job = start;
|
||||
for (uint32_t i = 0; i < 2 && prefetch_job < end; ++i) {
|
||||
const struct dsv4_hc_block block = dsv4_hc_pre_block_make(dctx, prefetch_job, end);
|
||||
const uint32_t d_bytes = block.count * dst_cb;
|
||||
// empty dst writeback keeps pop order dst, x, g in loop
|
||||
dma_queue_push(dma_q, dma_make_data(d_data, spad[i] + 8*row_stride), block.count*VLEN, block.count*VLEN, 0, 0);
|
||||
dma_queue_push(dma_q, dma_make_data(d_data, spad[i] + 8*row_stride), d_bytes, d_bytes, 0, 0);
|
||||
dsv4_hc_pre_push_gated(dma_q, dctx, &block, spad[i]);
|
||||
prefetch_job += block.count;
|
||||
}
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
const dsv4_hc_pre_gated_fn_t compute = dctx->compute_gated;
|
||||
|
||||
uint32_t job = start;
|
||||
uint32_t curr = 0;
|
||||
@@ -288,11 +539,12 @@ static void dsv4_hc_pre_thread_gated_f32(const htp_thread_t * th, unsigned int n
|
||||
uint8_t * d_spad = spad[curr] + 8*row_stride;
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it);
|
||||
dsv4_hc_pre_gated_block_f32(dctx, &block, spad[curr], spad[curr] + 4*row_stride, d_spad);
|
||||
compute(dctx, &block, spad[curr], spad[curr] + 4*row_stride, d_spad);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it);
|
||||
|
||||
const uint64_t d_dst = d_data + (block.it*dst->nb[1] + block.chunk*VLEN);
|
||||
dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), block.count*VLEN, block.count*VLEN, block.count*VLEN, 1);
|
||||
const uint32_t d_bytes = block.count * dst_cb;
|
||||
const uint64_t d_dst = d_data + (block.it*dst->nb[1] + block.chunk*dst_cb);
|
||||
dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), d_bytes, d_bytes, d_bytes, 1);
|
||||
|
||||
if (prefetch_job < end) {
|
||||
const struct dsv4_hc_block next = dsv4_hc_pre_block_make(dctx, prefetch_job, end);
|
||||
@@ -327,8 +579,7 @@ int op_dsv4_hc_pre(struct htp_ops_context * octx) {
|
||||
uint32_t total_jobs = kparams->total_jobs;
|
||||
uint32_t jobs_per_hvx = kparams->jobs_per_hvx;
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
const bool can_split = htp_tensor_mdev_data_aligned(dst) && ((dst->nb[1] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(n_tokens, can_split ? 1 : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(n_tokens, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
token_start = range.start;
|
||||
n_tokens = range.count;
|
||||
total_jobs = n_tokens * kparams->n_chunks;
|
||||
@@ -342,6 +593,28 @@ int op_dsv4_hc_pre(struct htp_ops_context * octx) {
|
||||
float scale;
|
||||
memcpy(&scale, octx->op_params, sizeof(scale));
|
||||
|
||||
const uint32_t x_f16 = (x->type == HTP_TYPE_F16) ? 1u : 0u;
|
||||
const uint32_t w_f16 = (weights->type == HTP_TYPE_F16) ? 1u : 0u;
|
||||
const uint32_t d_f16 = (dst->type == HTP_TYPE_F16) ? 1u : 0u;
|
||||
|
||||
static const dsv4_hc_pre_plain_fn_t plain_fns[4] = {
|
||||
dsv4_hc_pre_plain_block_f32_f32,
|
||||
dsv4_hc_pre_plain_block_f32_f16,
|
||||
dsv4_hc_pre_plain_block_f16_f32,
|
||||
dsv4_hc_pre_plain_block_f16_f16,
|
||||
};
|
||||
|
||||
static const dsv4_hc_pre_gated_fn_t gated_fns[8] = {
|
||||
dsv4_hc_pre_gated_block_f32_f32_f32,
|
||||
dsv4_hc_pre_gated_block_f32_f32_f16,
|
||||
dsv4_hc_pre_gated_block_f32_f16_f32,
|
||||
dsv4_hc_pre_gated_block_f32_f16_f16,
|
||||
dsv4_hc_pre_gated_block_f16_f32_f32,
|
||||
dsv4_hc_pre_gated_block_f16_f32_f16,
|
||||
dsv4_hc_pre_gated_block_f16_f16_f32,
|
||||
dsv4_hc_pre_gated_block_f16_f16_f16,
|
||||
};
|
||||
|
||||
struct dsv4_hc_pre_context dctx = {
|
||||
.octx = octx,
|
||||
.kparams = kparams,
|
||||
@@ -354,12 +627,19 @@ int op_dsv4_hc_pre(struct htp_ops_context * octx) {
|
||||
.dma_block_chunks = kparams->dma_block_chunks,
|
||||
.dma_row_stride = kparams->dma_row_stride,
|
||||
.dma_buffer_size = kparams->dma_buffer_size,
|
||||
.x_chunk_bytes = x_f16 ? 64u : 128u,
|
||||
.w_chunk_bytes = w_f16 ? 64u : 128u,
|
||||
.w_row_bytes = w_f16 ? 8u : 16u,
|
||||
.dst_chunk_bytes = d_f16 ? 64u : 128u,
|
||||
.unpack_weights = w_f16 ? dsv4_hc_unpack_f16_0 : dsv4_hc_unpack_noop,
|
||||
.compute_plain = plain_fns[(x_f16 << 1) | d_f16],
|
||||
.compute_gated = gated_fns[(x_f16 << 2) | (w_f16 << 1) | d_f16],
|
||||
};
|
||||
|
||||
work_queue_func_t worker = NULL;
|
||||
switch (kparams->kernel_type) {
|
||||
case HTP_DSV4_HC_PRE_KERNEL_PLAIN: worker = dsv4_hc_pre_thread_plain_f32; break;
|
||||
case HTP_DSV4_HC_PRE_KERNEL_GATED: worker = dsv4_hc_pre_thread_gated_f32; break;
|
||||
case HTP_DSV4_HC_PRE_KERNEL_PLAIN: worker = dsv4_hc_pre_thread_plain; break;
|
||||
case HTP_DSV4_HC_PRE_KERNEL_GATED: worker = dsv4_hc_pre_thread_gated; break;
|
||||
default: return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
|
||||
@@ -375,6 +655,16 @@ int op_dsv4_hc_pre(struct htp_ops_context * octx) {
|
||||
// DSV4_HC_POST
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
typedef void (*dsv4_hc_post_compute_fn_t)(
|
||||
const uint8_t * xb,
|
||||
const uint8_t * rb,
|
||||
size_t r_stride,
|
||||
uint8_t * db,
|
||||
size_t d_stride,
|
||||
uint32_t count,
|
||||
const float * p,
|
||||
const float * c);
|
||||
|
||||
struct dsv4_hc_post_context {
|
||||
struct htp_ops_context * octx;
|
||||
const struct htp_dsv4_hc_post_kernel_params * kparams;
|
||||
@@ -382,6 +672,13 @@ struct dsv4_hc_post_context {
|
||||
uint32_t total_jobs;
|
||||
uint32_t jobs_per_hvx;
|
||||
uint8_t * vtcm;
|
||||
uint32_t x_chunk_bytes;
|
||||
uint32_t r_chunk_bytes;
|
||||
uint32_t dst_chunk_bytes;
|
||||
uint32_t post_bytes;
|
||||
uint32_t comb_row_bytes;
|
||||
dsv4_hc_unpack_fn_t unpack_params;
|
||||
dsv4_hc_post_compute_fn_t compute;
|
||||
};
|
||||
|
||||
static inline struct dsv4_hc_block dsv4_hc_post_block_make(
|
||||
@@ -399,56 +696,138 @@ static inline struct dsv4_hc_block dsv4_hc_post_block_make(
|
||||
};
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void dsv4_hc_post_identity_block_f32(
|
||||
const uint8_t * xb,
|
||||
const uint8_t * rb,
|
||||
size_t r_stride,
|
||||
uint8_t * db,
|
||||
size_t d_stride,
|
||||
uint32_t count,
|
||||
const float * p) {
|
||||
for (uint32_t id = 0; id < 4; ++id) {
|
||||
const HVX_Vector pv = hvx_vec_splat_f32(p[id]);
|
||||
const uint8_t * rr = rb + id*r_stride;
|
||||
uint8_t * dd = db + id*d_stride;
|
||||
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
const HVX_Vector xv = *(const HVX_Vector *) (xb + i*VLEN);
|
||||
const HVX_Vector rv = *(const HVX_Vector *) (rr + i*VLEN);
|
||||
*(HVX_Vector *) (dd + i*VLEN) = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(xv, pv), rv);
|
||||
}
|
||||
}
|
||||
#define DEFINE_DSV4_HC_POST_IDENTITY_BLOCK(SUFFIX, TX, TR, TDST, X_CB, R_CB, D_CB) \
|
||||
static __attribute__((noinline)) void dsv4_hc_post_identity_block_##SUFFIX( \
|
||||
const uint8_t * xb, \
|
||||
const uint8_t * rb, \
|
||||
size_t r_stride, \
|
||||
uint8_t * db, \
|
||||
size_t d_stride, \
|
||||
uint32_t count, \
|
||||
const float * p, \
|
||||
const float * c_unused) { \
|
||||
(void) c_unused; \
|
||||
const HVX_Vector pv0 = hvx_vec_splat_f32(p[0]); \
|
||||
const HVX_Vector pv1 = hvx_vec_splat_f32(p[1]); \
|
||||
const HVX_Vector pv2 = hvx_vec_splat_f32(p[2]); \
|
||||
const HVX_Vector pv3 = hvx_vec_splat_f32(p[3]); \
|
||||
uint32_t i = 0; \
|
||||
for (; i + 2 <= count; i += 2) { \
|
||||
HVX_Vector x0, x1, r0, r1; \
|
||||
DSV4_HC_LOAD2_##TX(x0, x1, xb + i * (X_CB)); \
|
||||
const uint8_t * rr = rb + i * (R_CB); \
|
||||
uint8_t * dd = db + i * (D_CB); \
|
||||
DSV4_HC_LOAD2_##TR(r0, r1, rr); \
|
||||
DSV4_HC_STORE2_##TDST(dd, \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x0, pv0), r0), \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x1, pv0), r1)); \
|
||||
DSV4_HC_LOAD2_##TR(r0, r1, rr + r_stride); \
|
||||
DSV4_HC_STORE2_##TDST(dd + d_stride, \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x0, pv1), r0), \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x1, pv1), r1)); \
|
||||
DSV4_HC_LOAD2_##TR(r0, r1, rr + 2 * r_stride); \
|
||||
DSV4_HC_STORE2_##TDST(dd + 2 * d_stride, \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x0, pv2), r0), \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x1, pv2), r1)); \
|
||||
DSV4_HC_LOAD2_##TR(r0, r1, rr + 3 * r_stride); \
|
||||
DSV4_HC_STORE2_##TDST(dd + 3 * d_stride, \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x0, pv3), r0), \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x1, pv3), r1)); \
|
||||
} \
|
||||
if (i < count) { \
|
||||
HVX_Vector x0, r0; \
|
||||
DSV4_HC_LOAD1_##TX(x0, xb + i * (X_CB)); \
|
||||
const uint8_t * rr = rb + i * (R_CB); \
|
||||
uint8_t * dd = db + i * (D_CB); \
|
||||
DSV4_HC_LOAD1_##TR(r0, rr); \
|
||||
DSV4_HC_STORE1_##TDST(dd, \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x0, pv0), r0)); \
|
||||
DSV4_HC_LOAD1_##TR(r0, rr + r_stride); \
|
||||
DSV4_HC_STORE1_##TDST(dd + d_stride, \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x0, pv1), r0)); \
|
||||
DSV4_HC_LOAD1_##TR(r0, rr + 2 * r_stride); \
|
||||
DSV4_HC_STORE1_##TDST(dd + 2 * d_stride, \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x0, pv2), r0)); \
|
||||
DSV4_HC_LOAD1_##TR(r0, rr + 3 * r_stride); \
|
||||
DSV4_HC_STORE1_##TDST(dd + 3 * d_stride, \
|
||||
hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x0, pv3), r0)); \
|
||||
} \
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void dsv4_hc_post_comb_block_f32(
|
||||
const uint8_t * xb,
|
||||
const uint8_t * rb,
|
||||
size_t r_stride,
|
||||
uint8_t * db,
|
||||
size_t d_stride,
|
||||
uint32_t count,
|
||||
const float * p,
|
||||
const float * c) {
|
||||
for (uint32_t id = 0; id < 4; ++id) {
|
||||
const HVX_Vector pv = hvx_vec_splat_f32(p[id]);
|
||||
const HVX_Vector c0 = hvx_vec_splat_f32(c[id]);
|
||||
const HVX_Vector c1 = hvx_vec_splat_f32(c[4 + id]);
|
||||
const HVX_Vector c2 = hvx_vec_splat_f32(c[8 + id]);
|
||||
const HVX_Vector c3 = hvx_vec_splat_f32(c[12 + id]);
|
||||
uint8_t * dd = db + id*d_stride;
|
||||
DEFINE_DSV4_HC_POST_IDENTITY_BLOCK(f32_f32_f32, f32, f32, f32, 128, 128, 128)
|
||||
DEFINE_DSV4_HC_POST_IDENTITY_BLOCK(f32_f32_f16, f32, f32, f16, 128, 128, 64)
|
||||
DEFINE_DSV4_HC_POST_IDENTITY_BLOCK(f32_f16_f32, f32, f16, f32, 128, 64, 128)
|
||||
DEFINE_DSV4_HC_POST_IDENTITY_BLOCK(f32_f16_f16, f32, f16, f16, 128, 64, 64)
|
||||
DEFINE_DSV4_HC_POST_IDENTITY_BLOCK(f16_f32_f32, f16, f32, f32, 64, 128, 128)
|
||||
DEFINE_DSV4_HC_POST_IDENTITY_BLOCK(f16_f32_f16, f16, f32, f16, 64, 128, 64)
|
||||
DEFINE_DSV4_HC_POST_IDENTITY_BLOCK(f16_f16_f32, f16, f16, f32, 64, 64, 128)
|
||||
DEFINE_DSV4_HC_POST_IDENTITY_BLOCK(f16_f16_f16, f16s, f16s, f16s, 64, 64, 64)
|
||||
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
const uint8_t * r = rb + i*VLEN;
|
||||
HVX_Vector acc = hvx_vec_mul_f32_f32(*(const HVX_Vector *) (xb + i*VLEN), pv);
|
||||
acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(*(const HVX_Vector *) r, c0));
|
||||
acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(*(const HVX_Vector *) (r + r_stride), c1));
|
||||
acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(*(const HVX_Vector *) (r + 2*r_stride), c2));
|
||||
acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(*(const HVX_Vector *) (r + 3*r_stride), c3));
|
||||
*(HVX_Vector *) (dd + i*VLEN) = acc;
|
||||
}
|
||||
}
|
||||
#define DEFINE_DSV4_HC_POST_COMB_BLOCK(SUFFIX, TX, TR, TDST, X_CB, R_CB, D_CB) \
|
||||
static __attribute__((noinline)) void dsv4_hc_post_comb_block_##SUFFIX( \
|
||||
const uint8_t * xb, \
|
||||
const uint8_t * rb, \
|
||||
size_t r_stride, \
|
||||
uint8_t * db, \
|
||||
size_t d_stride, \
|
||||
uint32_t count, \
|
||||
const float * p, \
|
||||
const float * c) { \
|
||||
for (uint32_t id = 0; id < 4; ++id) { \
|
||||
const HVX_Vector pv = hvx_vec_splat_f32(p[id]); \
|
||||
const HVX_Vector c0 = hvx_vec_splat_f32(c[id]); \
|
||||
const HVX_Vector c1 = hvx_vec_splat_f32(c[4 + id]); \
|
||||
const HVX_Vector c2 = hvx_vec_splat_f32(c[8 + id]); \
|
||||
const HVX_Vector c3 = hvx_vec_splat_f32(c[12 + id]); \
|
||||
uint8_t * dd = db + id * d_stride; \
|
||||
uint32_t i = 0; \
|
||||
for (; i + 2 <= count; i += 2) { \
|
||||
HVX_Vector v0, v1; \
|
||||
DSV4_HC_LOAD2_##TX(v0, v1, xb + i * (X_CB)); \
|
||||
HVX_Vector acc0 = hvx_vec_mul_f32_f32(v0, pv); \
|
||||
HVX_Vector acc1 = hvx_vec_mul_f32_f32(v1, pv); \
|
||||
const uint8_t * r = rb + i * (R_CB); \
|
||||
DSV4_HC_LOAD2_##TR(v0, v1, r); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(v0, c0)); \
|
||||
acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(v1, c0)); \
|
||||
DSV4_HC_LOAD2_##TR(v0, v1, r + r_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(v0, c1)); \
|
||||
acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(v1, c1)); \
|
||||
DSV4_HC_LOAD2_##TR(v0, v1, r + 2 * r_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(v0, c2)); \
|
||||
acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(v1, c2)); \
|
||||
DSV4_HC_LOAD2_##TR(v0, v1, r + 3 * r_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(v0, c3)); \
|
||||
acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(v1, c3)); \
|
||||
DSV4_HC_STORE2_##TDST(dd + i * (D_CB), acc0, acc1); \
|
||||
} \
|
||||
if (i < count) { \
|
||||
HVX_Vector v0; \
|
||||
DSV4_HC_LOAD1_##TX(v0, xb + i * (X_CB)); \
|
||||
HVX_Vector acc0 = hvx_vec_mul_f32_f32(v0, pv); \
|
||||
const uint8_t * r = rb + i * (R_CB); \
|
||||
DSV4_HC_LOAD1_##TR(v0, r); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(v0, c0)); \
|
||||
DSV4_HC_LOAD1_##TR(v0, r + r_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(v0, c1)); \
|
||||
DSV4_HC_LOAD1_##TR(v0, r + 2 * r_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(v0, c2)); \
|
||||
DSV4_HC_LOAD1_##TR(v0, r + 3 * r_stride); \
|
||||
acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(v0, c3)); \
|
||||
DSV4_HC_STORE1_##TDST(dd + i * (D_CB), acc0); \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
DEFINE_DSV4_HC_POST_COMB_BLOCK(f32_f32_f32, f32, f32, f32, 128, 128, 128)
|
||||
DEFINE_DSV4_HC_POST_COMB_BLOCK(f32_f32_f16, f32, f32, f16, 128, 128, 64)
|
||||
DEFINE_DSV4_HC_POST_COMB_BLOCK(f32_f16_f32, f32, f16, f32, 128, 64, 128)
|
||||
DEFINE_DSV4_HC_POST_COMB_BLOCK(f32_f16_f16, f32, f16, f16, 128, 64, 64)
|
||||
DEFINE_DSV4_HC_POST_COMB_BLOCK(f16_f32_f32, f16, f32, f32, 64, 128, 128)
|
||||
DEFINE_DSV4_HC_POST_COMB_BLOCK(f16_f32_f16, f16, f32, f16, 64, 128, 64)
|
||||
DEFINE_DSV4_HC_POST_COMB_BLOCK(f16_f16_f32, f16, f16, f32, 64, 64, 128)
|
||||
DEFINE_DSV4_HC_POST_COMB_BLOCK(f16_f16_f16, f16s, f16s, f16s, 64, 64, 64)
|
||||
|
||||
static inline void dsv4_hc_post_push_identity(
|
||||
dma_queue * dma_q,
|
||||
const struct dsv4_hc_post_context * dctx,
|
||||
@@ -458,14 +837,18 @@ static inline void dsv4_hc_post_push_identity(
|
||||
const struct htp_tensor * residual = dctx->octx->src[1];
|
||||
const struct htp_tensor * post = dctx->octx->src[2];
|
||||
const uint32_t row_stride = dctx->kparams->dma_row_stride;
|
||||
const uint32_t row_size = block->count * VLEN;
|
||||
const uint32_t x_cb = dctx->x_chunk_bytes;
|
||||
const uint32_t r_cb = dctx->r_chunk_bytes;
|
||||
const uint32_t p_bytes = dctx->post_bytes;
|
||||
const uint32_t x_size = block->count * x_cb;
|
||||
const uint32_t r_size = block->count * r_cb;
|
||||
|
||||
dma_queue_push(dma_q, dma_make_data(spad, residual->data + (block->it*residual->nb[2] + block->chunk*VLEN)),
|
||||
row_stride, residual->nb[1], row_size, 4);
|
||||
dma_queue_push(dma_q, dma_make_data(spad + 4*row_stride, x->data + (block->it*x->nb[1] + block->chunk*VLEN)),
|
||||
row_stride, row_size, row_size, 1);
|
||||
dma_queue_push(dma_q, dma_make_data(spad, residual->data + (block->it*residual->nb[2] + block->chunk*r_cb)),
|
||||
row_stride, residual->nb[1], r_size, 4);
|
||||
dma_queue_push(dma_q, dma_make_data(spad + 4*row_stride, x->data + (block->it*x->nb[1] + block->chunk*x_cb)),
|
||||
row_stride, x_size, x_size, 1);
|
||||
dma_queue_push(dma_q, dma_make_data(spad + 9*row_stride, post->data + (block->it*post->nb[1])),
|
||||
16, 16, 16, 1);
|
||||
p_bytes, p_bytes, p_bytes, 1);
|
||||
}
|
||||
|
||||
static inline void dsv4_hc_post_push_comb(
|
||||
@@ -475,13 +858,14 @@ static inline void dsv4_hc_post_push_comb(
|
||||
uint8_t * spad) {
|
||||
const struct htp_tensor * comb = dctx->octx->src[3];
|
||||
const uint32_t row_stride = dctx->kparams->dma_row_stride;
|
||||
const uint32_t c_bytes = dctx->comb_row_bytes;
|
||||
|
||||
dsv4_hc_post_push_identity(dma_q, dctx, block, spad);
|
||||
dma_queue_push(dma_q, dma_make_data(spad + 9*row_stride + 64, comb->data + (block->it*comb->nb[2])),
|
||||
16, comb->nb[1], 16, 4);
|
||||
dma_queue_push(dma_q, dma_make_data(spad + 9*row_stride + 128, comb->data + (block->it*comb->nb[2])),
|
||||
c_bytes, comb->nb[1], c_bytes, 4);
|
||||
}
|
||||
|
||||
static void dsv4_hc_post_thread_identity_f32(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
static void dsv4_hc_post_thread_identity(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
struct dsv4_hc_post_context * dctx = (struct dsv4_hc_post_context *) data;
|
||||
struct htp_ops_context * octx = dctx->octx;
|
||||
|
||||
@@ -491,6 +875,7 @@ static void dsv4_hc_post_thread_identity_f32(const htp_thread_t * th, unsigned i
|
||||
const uint32_t block_chunks = dctx->kparams->dma_block_chunks;
|
||||
const uint32_t row_stride = dctx->kparams->dma_row_stride;
|
||||
const uint32_t buffer_size = dctx->kparams->dma_buffer_size;
|
||||
const uint32_t dst_cb = dctx->dst_chunk_bytes;
|
||||
const uint32_t start = dctx->jobs_per_hvx * ith;
|
||||
const uint32_t end = hex_smin(start + dctx->jobs_per_hvx, dctx->total_jobs);
|
||||
if (start >= end) {
|
||||
@@ -512,7 +897,9 @@ static void dsv4_hc_post_thread_identity_f32(const htp_thread_t * th, unsigned i
|
||||
prefetch_job += block.count;
|
||||
}
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
const dsv4_hc_unpack_fn_t unpack = dctx->unpack_params;
|
||||
const dsv4_hc_post_compute_fn_t compute = dctx->compute;
|
||||
|
||||
uint32_t job = start;
|
||||
uint32_t curr = 0;
|
||||
@@ -524,15 +911,16 @@ static void dsv4_hc_post_thread_identity_f32(const htp_thread_t * th, unsigned i
|
||||
dma_queue_pop(dma_q);
|
||||
dma_queue_pop(dma_q);
|
||||
|
||||
const float * p = (const float *) (spad[curr] + 9*row_stride);
|
||||
uint8_t * d_spad = spad[curr] + 5*row_stride;
|
||||
uint8_t * row9 = spad[curr] + 9*row_stride;
|
||||
uint8_t * d_spad = spad[curr] + 5*row_stride;
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it);
|
||||
dsv4_hc_post_identity_block_f32(spad[curr] + 4*row_stride, spad[curr], row_stride, d_spad, row_stride, block.count, p);
|
||||
unpack(row9);
|
||||
compute(spad[curr] + 4*row_stride, spad[curr], row_stride, d_spad, row_stride, block.count, (const float *) row9, NULL);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it);
|
||||
|
||||
const uint64_t d_dst = d_data + (block.it*dst->nb[2] + block.chunk*VLEN);
|
||||
dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), dst->nb[1], row_stride, block.count*VLEN, 4);
|
||||
const uint64_t d_dst = d_data + (block.it*dst->nb[2] + block.chunk*dst_cb);
|
||||
dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), dst->nb[1], row_stride, block.count*dst_cb, 4);
|
||||
|
||||
if (prefetch_job < end) {
|
||||
const struct dsv4_hc_block next = dsv4_hc_post_block_make(dctx, prefetch_job, end, block_chunks);
|
||||
@@ -549,7 +937,7 @@ static void dsv4_hc_post_thread_identity_f32(const htp_thread_t * th, unsigned i
|
||||
(void) nth;
|
||||
}
|
||||
|
||||
static void dsv4_hc_post_thread_comb_f32(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
static void dsv4_hc_post_thread_comb(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
struct dsv4_hc_post_context * dctx = (struct dsv4_hc_post_context *) data;
|
||||
struct htp_ops_context * octx = dctx->octx;
|
||||
|
||||
@@ -559,6 +947,7 @@ static void dsv4_hc_post_thread_comb_f32(const htp_thread_t * th, unsigned int n
|
||||
const uint32_t block_chunks = dctx->kparams->dma_block_chunks;
|
||||
const uint32_t row_stride = dctx->kparams->dma_row_stride;
|
||||
const uint32_t buffer_size = dctx->kparams->dma_buffer_size;
|
||||
const uint32_t dst_cb = dctx->dst_chunk_bytes;
|
||||
const uint32_t start = dctx->jobs_per_hvx * ith;
|
||||
const uint32_t end = hex_smin(start + dctx->jobs_per_hvx, dctx->total_jobs);
|
||||
if (start >= end) {
|
||||
@@ -580,7 +969,9 @@ static void dsv4_hc_post_thread_comb_f32(const htp_thread_t * th, unsigned int n
|
||||
prefetch_job += block.count;
|
||||
}
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
const dsv4_hc_unpack_fn_t unpack = dctx->unpack_params;
|
||||
const dsv4_hc_post_compute_fn_t compute = dctx->compute;
|
||||
|
||||
uint32_t job = start;
|
||||
uint32_t curr = 0;
|
||||
@@ -593,16 +984,17 @@ static void dsv4_hc_post_thread_comb_f32(const htp_thread_t * th, unsigned int n
|
||||
dma_queue_pop(dma_q);
|
||||
dma_queue_pop(dma_q);
|
||||
|
||||
const float * p = (const float *) (spad[curr] + 9*row_stride);
|
||||
const float * c = (const float *) (spad[curr] + 9*row_stride + 64);
|
||||
uint8_t * d_spad = spad[curr] + 5*row_stride;
|
||||
uint8_t * row9 = spad[curr] + 9*row_stride;
|
||||
uint8_t * d_spad = spad[curr] + 5*row_stride;
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it);
|
||||
dsv4_hc_post_comb_block_f32(spad[curr] + 4*row_stride, spad[curr], row_stride, d_spad, row_stride, block.count, p, c);
|
||||
unpack(row9);
|
||||
compute(spad[curr] + 4*row_stride, spad[curr], row_stride, d_spad, row_stride, block.count,
|
||||
(const float *) row9, (const float *) (row9 + 128));
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it);
|
||||
|
||||
const uint64_t d_dst = d_data + (block.it*dst->nb[2] + block.chunk*VLEN);
|
||||
dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), dst->nb[1], row_stride, block.count*VLEN, 4);
|
||||
const uint64_t d_dst = d_data + (block.it*dst->nb[2] + block.chunk*dst_cb);
|
||||
dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), dst->nb[1], row_stride, block.count*dst_cb, 4);
|
||||
|
||||
if (prefetch_job < end) {
|
||||
const struct dsv4_hc_block next = dsv4_hc_post_block_make(dctx, prefetch_job, end, block_chunks);
|
||||
@@ -622,10 +1014,12 @@ static void dsv4_hc_post_thread_comb_f32(const htp_thread_t * th, unsigned int n
|
||||
int op_dsv4_hc_post(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * x = octx->src[0];
|
||||
const struct htp_tensor * residual = octx->src[1];
|
||||
const struct htp_tensor * post = octx->src[2];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
const struct htp_dsv4_hc_post_kernel_params * kparams =
|
||||
(const struct htp_dsv4_hc_post_kernel_params *) octx->kernel_params;
|
||||
const struct htp_tensor * comb = kparams->has_comb ? octx->src[3] : NULL;
|
||||
|
||||
const uint32_t n_hvx = kparams->n_hvx;
|
||||
if (!htp_ops_context_set_n_hvx_div(octx, n_hvx, kparams->div_n_hvx)) {
|
||||
@@ -637,8 +1031,7 @@ int op_dsv4_hc_post(struct htp_ops_context * octx) {
|
||||
uint32_t total_jobs = kparams->total_jobs;
|
||||
uint32_t jobs_per_hvx = kparams->jobs_per_hvx;
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
const bool can_split = htp_tensor_mdev_data_aligned(dst) && ((dst->nb[2] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(n_tokens, can_split ? 1 : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(n_tokens, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
token_start = range.start;
|
||||
n_tokens = range.count;
|
||||
total_jobs = n_tokens * kparams->n_chunks;
|
||||
@@ -649,6 +1042,43 @@ int op_dsv4_hc_post(struct htp_ops_context * octx) {
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
const uint32_t x_f16 = (x->type == HTP_TYPE_F16) ? 1u : 0u;
|
||||
const uint32_t r_f16 = (residual->type == HTP_TYPE_F16) ? 1u : 0u;
|
||||
const uint32_t d_f16 = (dst->type == HTP_TYPE_F16) ? 1u : 0u;
|
||||
const uint32_t p_f16 = (post->type == HTP_TYPE_F16) ? 1u : 0u;
|
||||
const uint32_t c_f16 = (comb && comb->type == HTP_TYPE_F16) ? 1u : 0u;
|
||||
|
||||
static const dsv4_hc_unpack_fn_t unpack_fns[4] = {
|
||||
dsv4_hc_unpack_noop,
|
||||
dsv4_hc_unpack_f16_0,
|
||||
dsv4_hc_unpack_f16_128,
|
||||
dsv4_hc_unpack_f16_0_128,
|
||||
};
|
||||
|
||||
static const dsv4_hc_post_compute_fn_t identity_fns[8] = {
|
||||
dsv4_hc_post_identity_block_f32_f32_f32,
|
||||
dsv4_hc_post_identity_block_f32_f32_f16,
|
||||
dsv4_hc_post_identity_block_f32_f16_f32,
|
||||
dsv4_hc_post_identity_block_f32_f16_f16,
|
||||
dsv4_hc_post_identity_block_f16_f32_f32,
|
||||
dsv4_hc_post_identity_block_f16_f32_f16,
|
||||
dsv4_hc_post_identity_block_f16_f16_f32,
|
||||
dsv4_hc_post_identity_block_f16_f16_f16,
|
||||
};
|
||||
|
||||
static const dsv4_hc_post_compute_fn_t comb_fns[8] = {
|
||||
dsv4_hc_post_comb_block_f32_f32_f32,
|
||||
dsv4_hc_post_comb_block_f32_f32_f16,
|
||||
dsv4_hc_post_comb_block_f32_f16_f32,
|
||||
dsv4_hc_post_comb_block_f32_f16_f16,
|
||||
dsv4_hc_post_comb_block_f16_f32_f32,
|
||||
dsv4_hc_post_comb_block_f16_f32_f16,
|
||||
dsv4_hc_post_comb_block_f16_f16_f32,
|
||||
dsv4_hc_post_comb_block_f16_f16_f16,
|
||||
};
|
||||
|
||||
const uint32_t fn_idx = (x_f16 << 2) | (r_f16 << 1) | d_f16;
|
||||
|
||||
struct dsv4_hc_post_context dctx = {
|
||||
.octx = octx,
|
||||
.kparams = kparams,
|
||||
@@ -656,12 +1086,19 @@ int op_dsv4_hc_post(struct htp_ops_context * octx) {
|
||||
.total_jobs = total_jobs,
|
||||
.jobs_per_hvx = jobs_per_hvx,
|
||||
.vtcm = (uint8_t *) octx->ctx->vtcm_base,
|
||||
.x_chunk_bytes = x_f16 ? 64u : 128u,
|
||||
.r_chunk_bytes = r_f16 ? 64u : 128u,
|
||||
.dst_chunk_bytes = d_f16 ? 64u : 128u,
|
||||
.post_bytes = p_f16 ? 8u : 16u,
|
||||
.comb_row_bytes = c_f16 ? 8u : 16u,
|
||||
.unpack_params = unpack_fns[p_f16 | (c_f16 << 1)],
|
||||
.compute = (kparams->kernel_type == HTP_DSV4_HC_POST_KERNEL_COMB) ? comb_fns[fn_idx] : identity_fns[fn_idx],
|
||||
};
|
||||
|
||||
work_queue_func_t worker = NULL;
|
||||
switch (kparams->kernel_type) {
|
||||
case HTP_DSV4_HC_POST_KERNEL_IDENTITY: worker = dsv4_hc_post_thread_identity_f32; break;
|
||||
case HTP_DSV4_HC_POST_KERNEL_COMB: worker = dsv4_hc_post_thread_comb_f32; break;
|
||||
case HTP_DSV4_HC_POST_KERNEL_IDENTITY: worker = dsv4_hc_post_thread_identity; break;
|
||||
case HTP_DSV4_HC_POST_KERNEL_COMB: worker = dsv4_hc_post_thread_comb; break;
|
||||
default: return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dma-queue.h"
|
||||
#include "hex-common.h"
|
||||
#include "htp-trace.h"
|
||||
|
||||
@@ -16,39 +13,67 @@
|
||||
#include "htp-ctx.h"
|
||||
#include "htp-ops.h"
|
||||
#include "htp-tensor.h"
|
||||
#include "htp-vtcm.h"
|
||||
#include "fill-ops.h"
|
||||
|
||||
// ggml op_params layout for FILL:
|
||||
// op_params[0] (as float) - the scalar fill value
|
||||
|
||||
#define fill_preamble \
|
||||
const struct htp_tensor * dst = octx->dst; \
|
||||
\
|
||||
const uint32_t ne0 = dst->ne[0]; \
|
||||
const uint32_t ne1 = dst->ne[1]; \
|
||||
const uint32_t ne2 = dst->ne[2]; \
|
||||
const uint32_t ne3 = dst->ne[3]; \
|
||||
\
|
||||
const uint32_t nb1 = dst->nb[1]; \
|
||||
const uint32_t nb2 = dst->nb[2]; \
|
||||
const uint32_t nb3 = dst->nb[3];
|
||||
#define fill_preamble \
|
||||
const struct htp_tensor * dst = octx->dst; \
|
||||
\
|
||||
const uint32_t ne0 = dst->ne[0]; \
|
||||
const uint32_t __attribute__((unused)) ne1 = dst->ne[1]; \
|
||||
const uint32_t __attribute__((unused)) ne2 = dst->ne[2]; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) nb1 = dst->nb[1]; \
|
||||
const uint32_t __attribute__((unused)) nb2 = dst->nb[2]; \
|
||||
const uint32_t __attribute__((unused)) nb3 = dst->nb[3];
|
||||
|
||||
struct htp_fill_context {
|
||||
struct htp_ops_context * octx;
|
||||
struct htp_ops_context * octx;
|
||||
const struct htp_fill_kernel_params * kparams;
|
||||
uint32_t nrows_per_hvx;
|
||||
uint32_t total_rows; // ne1 * ne2 * ne3
|
||||
uint32_t row_start;
|
||||
HVX_Vector splat_vec;
|
||||
uint32_t elem_size;
|
||||
dma_addr_t data_dst;
|
||||
uint8_t * vtcm_tile;
|
||||
size_t tile_bytes;
|
||||
uint32_t nrows_per_hvx;
|
||||
uint32_t total_rows;
|
||||
uint32_t row_start;
|
||||
uint32_t splat_bits;
|
||||
uint32_t elem_size;
|
||||
};
|
||||
|
||||
static void __attribute__((noinline)) fill_splat_vtcm(
|
||||
uint8_t * vtcm_tile,
|
||||
uint32_t splat_bits,
|
||||
uint32_t n_bytes,
|
||||
uint32_t elem_size
|
||||
) {
|
||||
const HVX_Vector sv = (elem_size == 2) ? Q6_Vh_vsplat_R((int32_t) splat_bits) : Q6_V_vsplat_R((int32_t) splat_bits);
|
||||
hvx_splat_a(vtcm_tile, sv, n_bytes, 1);
|
||||
}
|
||||
|
||||
static inline void fill_dma_write_capped(
|
||||
dma_queue * dma_q,
|
||||
uint32_t * in_flight,
|
||||
uint32_t max_in_flight,
|
||||
dma_addr_t dst_addr,
|
||||
const uint8_t * src_vtcm,
|
||||
size_t nbytes
|
||||
) {
|
||||
if (*in_flight >= max_in_flight) {
|
||||
dma_queue_pop(dma_q);
|
||||
(*in_flight)--;
|
||||
}
|
||||
dma_queue_push(dma_q, dma_make_data(dst_addr, src_vtcm), nbytes, nbytes, nbytes, 1);
|
||||
(*in_flight)++;
|
||||
}
|
||||
|
||||
static void fill_thread_contiguous(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
const struct htp_fill_context * fctx = (const struct htp_fill_context *) data;
|
||||
struct htp_ops_context * octx = fctx->octx;
|
||||
struct htp_ops_context * octx = fctx->octx;
|
||||
fill_preamble;
|
||||
|
||||
// Parallelise over the flat row index spanning ne1*ne2*ne3
|
||||
const uint32_t ir0 = fctx->row_start + fctx->nrows_per_hvx * ith;
|
||||
const uint32_t ir1 = MIN(ir0 + fctx->nrows_per_hvx, fctx->row_start + fctx->total_rows);
|
||||
|
||||
@@ -56,25 +81,40 @@ static void fill_thread_contiguous(const htp_thread_t * th, unsigned int nth, un
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t tile_bytes = fctx->tile_bytes;
|
||||
const uint32_t elem_size = fctx->elem_size;
|
||||
const size_t total_bytes = (size_t) (ir1 - ir0) * ne0 * elem_size;
|
||||
const size_t splat_bytes = MIN(tile_bytes, hex_round_up(total_bytes, 128));
|
||||
uint8_t * vtcm_tile = fctx->vtcm_tile + ith * tile_bytes;
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir0);
|
||||
|
||||
const uint32_t elem_start = ir0 * ne0;
|
||||
const uint32_t elem_end = ir1 * ne0;
|
||||
uint8_t * dst_ptr = (uint8_t *) dst->data + elem_start * fctx->elem_size;
|
||||
hvx_splat_u(dst_ptr, fctx->splat_vec, elem_end - elem_start, fctx->elem_size);
|
||||
|
||||
fill_splat_vtcm(vtcm_tile, fctx->splat_bits, (uint32_t) splat_bytes, elem_size);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir1);
|
||||
|
||||
dma_queue * dma_q = th->dma_q;
|
||||
const uint32_t max_in_flight = MIN(8u, dma_queue_capacity(dma_q) - 1);
|
||||
uint32_t in_flight = 0;
|
||||
const dma_addr_t dst_base = fctx->data_dst + (size_t) ir0 * ne0 * elem_size;
|
||||
|
||||
for (size_t off = 0; off < total_bytes; off += tile_bytes) {
|
||||
const size_t chunk = MIN(tile_bytes, total_bytes - off);
|
||||
fill_dma_write_capped(dma_q, &in_flight, max_in_flight, dst_base + off, vtcm_tile, chunk);
|
||||
}
|
||||
|
||||
while (in_flight--) {
|
||||
dma_queue_pop(dma_q);
|
||||
}
|
||||
|
||||
FARF(HIGH, "fill-contig %u/%u: rows %u:%u\n", ith, nth, ir0, ir1);
|
||||
}
|
||||
|
||||
static void fill_thread_strided(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
const struct htp_fill_context * fctx = (const struct htp_fill_context *) data;
|
||||
struct htp_ops_context * octx = fctx->octx;
|
||||
const struct htp_fill_context * fctx = (const struct htp_fill_context *) data;
|
||||
struct htp_ops_context * octx = fctx->octx;
|
||||
const struct htp_fill_kernel_params * kparams = fctx->kparams;
|
||||
fill_preamble;
|
||||
|
||||
// Parallelise over the flat row index spanning ne1*ne2*ne3
|
||||
const uint32_t ir0 = fctx->row_start + fctx->nrows_per_hvx * ith;
|
||||
const uint32_t ir1 = MIN(ir0 + fctx->nrows_per_hvx, fctx->row_start + fctx->total_rows);
|
||||
|
||||
@@ -82,19 +122,38 @@ static void fill_thread_strided(const htp_thread_t * th, unsigned int nth, unsig
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t tile_bytes = fctx->tile_bytes;
|
||||
const uint32_t elem_size = fctx->elem_size;
|
||||
const size_t row_bytes = (size_t) ne0 * elem_size;
|
||||
const size_t splat_bytes = MIN(tile_bytes, hex_round_up(row_bytes, 128));
|
||||
uint8_t * vtcm_tile = fctx->vtcm_tile + ith * tile_bytes;
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir0);
|
||||
fill_splat_vtcm(vtcm_tile, fctx->splat_bits, (uint32_t) splat_bytes, elem_size);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir1);
|
||||
|
||||
dma_queue * dma_q = th->dma_q;
|
||||
const uint32_t max_in_flight = MIN(8u, dma_queue_capacity(dma_q) - 1);
|
||||
uint32_t in_flight = 0;
|
||||
|
||||
for (uint32_t ir = ir0; ir < ir1; ++ir) {
|
||||
const uint32_t i3 = fastdiv(ir, &kparams->div_ne1_ne2);
|
||||
const uint32_t rem = ir - i3 * (ne1 * ne2);
|
||||
const uint32_t i2 = fastdiv(rem, &kparams->div_ne1);
|
||||
const uint32_t i1 = rem - i2 * ne1;
|
||||
uint8_t * dst_ptr = (uint8_t *) dst->data + i1*nb1 + i2*nb2 + i3*nb3;
|
||||
hvx_splat_u(dst_ptr, fctx->splat_vec, ne0, fctx->elem_size);
|
||||
const uint32_t i3 = fastdiv(ir, &kparams->div_ne1_ne2);
|
||||
const uint32_t rem = ir - i3 * (ne1 * ne2);
|
||||
const uint32_t i2 = fastdiv(rem, &kparams->div_ne1);
|
||||
const uint32_t i1 = rem - i2 * ne1;
|
||||
const dma_addr_t dst_row = fctx->data_dst + (size_t) i1 * nb1 + (size_t) i2 * nb2 + (size_t) i3 * nb3;
|
||||
|
||||
for (size_t off = 0; off < row_bytes; off += tile_bytes) {
|
||||
const size_t chunk = MIN(tile_bytes, row_bytes - off);
|
||||
fill_dma_write_capped(dma_q, &in_flight, max_in_flight, dst_row + off, vtcm_tile, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
while (in_flight--) {
|
||||
dma_queue_pop(dma_q);
|
||||
}
|
||||
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir1);
|
||||
FARF(HIGH, "fill-strided %u/%u: rows %u:%u\n", ith, nth, ir0, ir1);
|
||||
}
|
||||
|
||||
@@ -103,15 +162,13 @@ int op_fill(struct htp_ops_context * octx) {
|
||||
const struct htp_fill_kernel_params * kparams =
|
||||
(const struct htp_fill_kernel_params *) octx->kernel_params;
|
||||
|
||||
if (htp_tensor_is_extended(dst)) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
uint32_t row_start = 0;
|
||||
uint32_t nrows = kparams->total_rows;
|
||||
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(kparams->total_rows, htp_tensor_mdev_data_aligned(dst) ? kparams->rows_per_chunk : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(
|
||||
kparams->total_rows, 1,
|
||||
octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
row_start = range.start;
|
||||
nrows = range.count;
|
||||
}
|
||||
@@ -126,6 +183,13 @@ int op_fill(struct htp_ops_context * octx) {
|
||||
|
||||
const uint32_t n_hvx = octx->n_hvx;
|
||||
|
||||
struct htp_fill_vtcm_layout layout;
|
||||
htp_fill_vtcm_layout_build(&layout, kparams->tile_bytes, n_hvx);
|
||||
if (octx->ctx->vtcm_size < layout.total_bytes) {
|
||||
FARF(ERROR, "fill: VTCM size %zu is too small, needed %zu\n", octx->ctx->vtcm_size, layout.total_bytes);
|
||||
return HTP_STATUS_VTCM_TOO_SMALL;
|
||||
}
|
||||
|
||||
FARF(HIGH, "fill: (%ux%ux%ux%u) type=%u ktype=%u\n",
|
||||
dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], dst->type, kparams->kernel_type);
|
||||
|
||||
@@ -133,21 +197,28 @@ int op_fill(struct htp_ops_context * octx) {
|
||||
memcpy(&val_f32, &octx->op_params[0], sizeof(float));
|
||||
|
||||
struct htp_fill_context fctx = {
|
||||
.octx = octx,
|
||||
.kparams = kparams,
|
||||
.nrows_per_hvx = fastdiv(nrows + n_hvx - 1, &octx->n_hvx_div),
|
||||
.total_rows = nrows,
|
||||
.row_start = row_start,
|
||||
.elem_size = kparams->elem_size,
|
||||
.octx = octx,
|
||||
.kparams = kparams,
|
||||
.data_dst = (dma_addr_t) dst->data,
|
||||
.vtcm_tile = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, layout.off_tile),
|
||||
.tile_bytes = layout.tile_bytes,
|
||||
.nrows_per_hvx = fastdiv(nrows + n_hvx - 1, &octx->n_hvx_div),
|
||||
.total_rows = nrows,
|
||||
.row_start = row_start,
|
||||
.elem_size = kparams->elem_size,
|
||||
};
|
||||
|
||||
switch (dst->type) {
|
||||
case HTP_TYPE_F32:
|
||||
fctx.splat_vec = hvx_vec_splat_f32(val_f32);
|
||||
memcpy(&fctx.splat_bits, &val_f32, sizeof(uint32_t));
|
||||
break;
|
||||
case HTP_TYPE_F16:
|
||||
fctx.splat_vec = hvx_vec_splat_f16((_Float16) val_f32);
|
||||
case HTP_TYPE_F16: {
|
||||
const _Float16 val_f16 = (_Float16) val_f32;
|
||||
uint16_t bits16 = 0;
|
||||
memcpy(&bits16, &val_f16, sizeof(uint16_t));
|
||||
fctx.splat_bits = bits16;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#ifndef HTP_FILL_OPS_H
|
||||
#define HTP_FILL_OPS_H
|
||||
|
||||
#include "hex-common.h"
|
||||
#include "hex-fastdiv.h"
|
||||
#include "htp-vtcm.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
enum htp_fill_kernel_type {
|
||||
@@ -9,12 +12,31 @@ enum htp_fill_kernel_type {
|
||||
HTP_FILL_KERNEL_STRIDED = 1,
|
||||
};
|
||||
|
||||
struct htp_fill_vtcm_layout {
|
||||
size_t tile_bytes;
|
||||
size_t off_tile;
|
||||
size_t total_bytes;
|
||||
};
|
||||
|
||||
static inline void htp_fill_vtcm_layout_build(
|
||||
struct htp_fill_vtcm_layout * L,
|
||||
size_t tile_bytes,
|
||||
uint32_t n_hvx
|
||||
) {
|
||||
size_t off = 0;
|
||||
|
||||
L->tile_bytes = hex_round_up(tile_bytes, 128);
|
||||
VTCM_LAYOUT_ALLOC(off, off_tile, (size_t) n_hvx * L->tile_bytes);
|
||||
L->total_bytes = off;
|
||||
}
|
||||
|
||||
struct htp_fill_kernel_params {
|
||||
uint8_t kernel_type;
|
||||
uint32_t n_hvx;
|
||||
uint32_t total_rows;
|
||||
uint32_t elem_size;
|
||||
uint32_t rows_per_chunk;
|
||||
uint32_t tile_bytes;
|
||||
uint32_t vtcm_size;
|
||||
|
||||
struct fastdiv_values div_ne1;
|
||||
struct fastdiv_values div_ne1_ne2;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "hex-fastdiv.h"
|
||||
#include "hex-common.h"
|
||||
#include "htp-tensor.h"
|
||||
#include "htp-vtcm.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -14,11 +15,17 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
// Tile constants (mirrored from hmx-utils.h for use on host side if needed)
|
||||
#define HTP_FA_HMX_TILE_SIZE 2048
|
||||
#define HMX_FP16_TILE_SIZE 2048
|
||||
#define HMX_FP16_TILE_N_ROWS 32
|
||||
#define HMX_FP16_TILE_N_COLS 32
|
||||
#define HMX_FP16_TILE_N_ELMS 1024
|
||||
#define HTP_FA_HMX_TILE_SIZE 2048
|
||||
#define HMX_FP16_TILE_SIZE 2048
|
||||
#define HMX_FP16_TILE_N_ROWS 32
|
||||
#define HMX_FP16_TILE_N_COLS 32
|
||||
#define HMX_FP16_TILE_N_ELMS 1024
|
||||
|
||||
#define HMX_W8A8_ACT_TILE_N_ROWS 64
|
||||
#define HMX_W8A8_TILE_N_COLS 32
|
||||
#define HMX_W8A8_ACT_TILE_SIZE 2048
|
||||
#define HMX_W8A8_WT_TILE_SIZE 1024
|
||||
#define HMX_W8A8_BIAS_BANK_SIZE 256
|
||||
|
||||
#define HVX_FA_DMA_CACHE_SIZE 128
|
||||
#define HMX_FA_DMA_CACHE_SIZE 4
|
||||
@@ -29,7 +36,8 @@ extern "C" {
|
||||
enum htp_fa_kernel_type {
|
||||
HTP_FA_KERNEL_UNSUPPORTED = 0,
|
||||
HTP_FA_KERNEL_HVX,
|
||||
HTP_FA_KERNEL_HMX
|
||||
HTP_FA_KERNEL_HMX_F16,
|
||||
HTP_FA_KERNEL_HMX_W8A8,
|
||||
};
|
||||
|
||||
// Multicore: gcd(n_kv_heads, N) teams own the KV heads, team members share the Q-blocks
|
||||
@@ -129,68 +137,143 @@ struct hmx_fa_vtcm_layout {
|
||||
size_t off_mask_buf;
|
||||
size_t off_slopes;
|
||||
size_t off_sinks;
|
||||
size_t off_q_scales;
|
||||
size_t off_k_scales[2];
|
||||
size_t off_k_bias[2];
|
||||
size_t off_o_f32;
|
||||
size_t off_do_tiles;
|
||||
size_t off_v_scales[2];
|
||||
size_t off_v_bias[2];
|
||||
size_t off_p_scales[2];
|
||||
size_t off_exp_mdiff[2];
|
||||
|
||||
// Region byte sizes reused by the device at runtime (not just for allocation).
|
||||
size_t q_tile_bytes;
|
||||
size_t o_tile_bytes;
|
||||
size_t k_tile_bytes;
|
||||
size_t s_tile_bytes; // S and P tiles (same size)
|
||||
size_t s_tile_bytes; // S tiles (2x in W8A8 for hi+lo)
|
||||
size_t p_tile_bytes; // P tiles
|
||||
size_t d_tile_bytes; // d_tiles[0..1] + d_inv_l, allocated back to back
|
||||
size_t o_f32_bytes; // W8A8 FP32 O accumulator
|
||||
size_t m_line_bytes; // one mask row
|
||||
size_t m_buf_slot_bytes; // one dma_cache slot = align_up(Br * m_line_bytes, 4096)
|
||||
size_t col_vec_bytes;
|
||||
size_t sinks_bytes;
|
||||
size_t k_raw_row_padded;
|
||||
size_t v_raw_row_padded;
|
||||
|
||||
// Derived strides.
|
||||
size_t row_buf_stride; // HVX vectors (128B) per row buffer
|
||||
size_t mask_buf_row_stride; // __fp16 elements per row in the mask buffer
|
||||
|
||||
bool pipeline;
|
||||
bool is_w8a8;
|
||||
size_t total_bytes;
|
||||
};
|
||||
|
||||
#define FA_MAX_KV_BLOCKS 1024
|
||||
|
||||
struct hmx_fa_layout_params {
|
||||
int kernel_type;
|
||||
int q_type;
|
||||
size_t gqa_factor;
|
||||
size_t DK;
|
||||
size_t DV;
|
||||
size_t Br;
|
||||
size_t Bc;
|
||||
size_t n_hvx;
|
||||
size_t n_heads;
|
||||
bool pipeline;
|
||||
bool has_mask;
|
||||
bool mask_broadcast;
|
||||
bool has_sinks;
|
||||
};
|
||||
|
||||
// Build the VTCM layout.
|
||||
|
||||
static inline void hmx_fa_vtcm_layout_build(struct hmx_fa_vtcm_layout * L,
|
||||
size_t gqa_factor, size_t DK, size_t DV,
|
||||
size_t Br, size_t Bc, size_t n_hvx,
|
||||
bool pipeline, bool is_q_fp32,
|
||||
bool has_sinks, size_t n_heads) {
|
||||
const size_t g_br = hex_align_up(gqa_factor * Br, HMX_FP16_TILE_N_ROWS);
|
||||
const size_t q_tile_size = hex_align_up(g_br * DK * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
const size_t o_tile_size = hex_align_up(g_br * DV * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
const size_t k_tile_size = hex_align_up(Bc * DK * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
const size_t v_tile_size = hex_align_up(Bc * DV * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
const size_t s_tile_size = hex_align_up(g_br * Bc * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
const struct hmx_fa_layout_params * p) {
|
||||
const bool is_w8a8 = (p->kernel_type == HTP_FA_KERNEL_HMX_W8A8);
|
||||
const size_t gqa_factor = p->gqa_factor;
|
||||
const size_t DK = p->DK;
|
||||
const size_t DV = p->DV;
|
||||
const size_t Br = p->Br;
|
||||
const size_t Bc = p->Bc;
|
||||
const size_t n_hvx = p->n_hvx;
|
||||
const size_t n_heads = p->n_heads;
|
||||
const bool pipeline = p->pipeline;
|
||||
const bool has_mask = p->has_mask;
|
||||
const bool mask_broadcast = p->mask_broadcast;
|
||||
const bool has_sinks = p->has_sinks;
|
||||
|
||||
const size_t g_br = hex_align_up(gqa_factor * Br, is_w8a8 ? HMX_W8A8_ACT_TILE_N_ROWS : HMX_FP16_TILE_N_ROWS);
|
||||
const size_t n_row_tiles_64 = g_br / HMX_W8A8_ACT_TILE_N_ROWS;
|
||||
const size_t n_col_tiles = Bc / HMX_W8A8_TILE_N_COLS;
|
||||
const size_t n_k_tiles = DK / HMX_W8A8_TILE_N_COLS;
|
||||
const size_t n_v_tiles = DV / HMX_W8A8_TILE_N_COLS;
|
||||
|
||||
const size_t q_tile_size = is_w8a8
|
||||
? (n_row_tiles_64 * n_k_tiles * HMX_W8A8_ACT_TILE_SIZE)
|
||||
: hex_align_up(g_br * DK * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
const size_t o_tile_size = is_w8a8
|
||||
? 0
|
||||
: hex_align_up(g_br * DV * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
const size_t k_tile_size = is_w8a8
|
||||
? hex_align_up(n_col_tiles * n_k_tiles * HMX_W8A8_WT_TILE_SIZE, HTP_FA_HMX_TILE_SIZE)
|
||||
: hex_align_up(Bc * DK * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
const size_t v_tile_size = is_w8a8
|
||||
? hex_align_up(n_v_tiles * n_col_tiles * HMX_W8A8_WT_TILE_SIZE, HTP_FA_HMX_TILE_SIZE)
|
||||
: hex_align_up(Bc * DV * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
const size_t s_tile_size = is_w8a8
|
||||
? (n_row_tiles_64 * n_col_tiles * 2 * HMX_W8A8_ACT_TILE_SIZE)
|
||||
: hex_align_up(g_br * Bc * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
const size_t p_tile_size = is_w8a8
|
||||
? (n_row_tiles_64 * n_col_tiles * HMX_W8A8_ACT_TILE_SIZE)
|
||||
: hex_align_up(g_br * Bc * sizeof(__fp16), HTP_FA_HMX_TILE_SIZE);
|
||||
|
||||
// The rescale matrices are diagonal: the HMX kernels only ever load the g_br/32
|
||||
// tiles that sit on the diagonal, so store just those, packed back to back with
|
||||
// a stride of one tile. The old [g_br, g_br] square layout allocated g_br/32
|
||||
// times more than it used, which is also why a second D buffer was unaffordable.
|
||||
const size_t d_tile_size = (g_br / HMX_FP16_TILE_N_ROWS) * HTP_FA_HMX_TILE_SIZE;
|
||||
const size_t d_tile_size = is_w8a8 ? 0 : (g_br / HMX_FP16_TILE_N_ROWS) * HTP_FA_HMX_TILE_SIZE;
|
||||
const size_t o_f32_size = hex_align_up(g_br * DV * sizeof(float), 128);
|
||||
const size_t do_tile_size = n_row_tiles_64 * n_v_tiles * 2 * HMX_W8A8_ACT_TILE_SIZE;
|
||||
|
||||
const size_t q_dma_size = hex_align_up(g_br * DK * (is_q_fp32 ? sizeof(float) : sizeof(__fp16)), 128);
|
||||
const size_t k_dma_size = hex_align_up(Bc * hex_round_up(DK * sizeof(__fp16), 128), 128);
|
||||
const size_t v_dma_size = hex_align_up(Bc * hex_round_up(DV * sizeof(__fp16), 128), 128);
|
||||
const size_t col_vec_size = hex_align_up(g_br * sizeof(float), 256);
|
||||
const size_t row_vec_size = hex_align_up(Bc * sizeof(__fp16), 256);
|
||||
const size_t m_line_size = hex_align_up(Bc * sizeof(__fp16), 128);
|
||||
const size_t m_buf_slot = hex_align_up(Br * m_line_size, 256);
|
||||
const size_t m_buf_size = m_buf_slot * HMX_FA_DMA_CACHE_SIZE;
|
||||
const size_t slopes_size = hex_align_up(g_br * sizeof(__fp16), 128);
|
||||
const size_t sinks_size = hex_round_up(n_heads * sizeof(float), 128);
|
||||
const size_t k_raw_row_padded = is_w8a8
|
||||
? hex_round_up((DK / 32) * 34, 128)
|
||||
: hex_round_up(DK * sizeof(__fp16), 128);
|
||||
const size_t v_raw_row_padded = is_w8a8
|
||||
? hex_round_up((DV / 32) * 34, 128)
|
||||
: hex_round_up(DV * sizeof(__fp16), 128);
|
||||
|
||||
const size_t q_dma_size = hex_align_up(g_br * htp_tensor_get_row_size(p->q_type, DK), 128);
|
||||
const size_t k_dma_size = hex_align_up(Bc * k_raw_row_padded, 128);
|
||||
const size_t v_dma_size = hex_align_up(Bc * v_raw_row_padded, 128);
|
||||
const size_t col_vec_size = hex_align_up(g_br * sizeof(float), 256);
|
||||
const size_t row_vec_size = hex_align_up(Bc * sizeof(__fp16), 256);
|
||||
const size_t m_line_size = hex_align_up(Bc * sizeof(__fp16), 128);
|
||||
const size_t m_buf_slot = hex_align_up(Br * m_line_size, 256);
|
||||
const size_t m_buf_slots = mask_broadcast ? HMX_FA_DMA_CACHE_SIZE : gqa_factor;
|
||||
const size_t m_buf_size = m_buf_slot * m_buf_slots;
|
||||
const size_t slopes_size = hex_align_up(g_br * sizeof(__fp16), 128);
|
||||
const size_t sinks_size = hex_round_up(n_heads * sizeof(float), 128);
|
||||
const size_t q_scales_size = hex_align_up(g_br * sizeof(float), 128);
|
||||
const size_t k_scales_size = hex_align_up(Bc * sizeof(float), 128);
|
||||
const size_t k_bias_size = hex_align_up(n_col_tiles * 2 * HMX_W8A8_BIAS_BANK_SIZE, HMX_W8A8_BIAS_BANK_SIZE);
|
||||
const size_t v_scales_size = hex_align_up(DV * sizeof(float), 128);
|
||||
const size_t v_bias_size = hex_align_up(n_v_tiles * 2 * HMX_W8A8_BIAS_BANK_SIZE, HMX_W8A8_BIAS_BANK_SIZE);
|
||||
const size_t p_scales_size = hex_align_up(g_br * sizeof(float), 128);
|
||||
const size_t exp_mdiff_size = hex_align_up(g_br * sizeof(float), 128);
|
||||
|
||||
size_t off = 0;
|
||||
|
||||
// Group A (Part 1 - HMX Tiled buffers)
|
||||
VTCM_LAYOUT_ALLOC(off, off_q_tiles, q_tile_size);
|
||||
VTCM_LAYOUT_ALLOC(off, off_o_tiles[0], o_tile_size);
|
||||
VTCM_LAYOUT_ALLOC(off, off_o_tiles[1], o_tile_size);
|
||||
VTCM_LAYOUT_ALLOC(off, off_d_tiles[0], d_tile_size);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_d_tiles[1], d_tile_size, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off, off_d_inv_l, d_tile_size);
|
||||
VTCM_LAYOUT_ALLOC(off, off_q_tiles, q_tile_size);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_o_tiles[0], o_tile_size, !is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_o_tiles[1], o_tile_size, !is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_d_tiles[0], d_tile_size, !is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_d_tiles[1], d_tile_size, !is_w8a8 && pipeline);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_d_inv_l, d_tile_size, !is_w8a8);
|
||||
|
||||
// Group B & C share start offset (Group B tiles must be 2KB aligned)
|
||||
size_t off_group_b_c = hex_align_up(off, HTP_FA_HMX_TILE_SIZE);
|
||||
@@ -203,11 +286,27 @@ static inline void hmx_fa_vtcm_layout_build(struct hmx_fa_vtcm_layout * L,
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_v_tiles[1], v_tile_size, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off_group_b, off_s_tiles[0], s_tile_size);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_s_tiles[1], s_tile_size, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off_group_b, off_p_tiles[0], s_tile_size);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_p_tiles[1], s_tile_size, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off_group_b, off_p_tiles[0], p_tile_size);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_p_tiles[1], p_tile_size, pipeline);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_do_tiles, do_tile_size, is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC(off_group_b, off_s_rowmax, col_vec_size);
|
||||
VTCM_LAYOUT_ALLOC(off_group_b, off_p_rowsum, col_vec_size);
|
||||
VTCM_LAYOUT_ALLOC(off_group_b, off_row_bufs, row_vec_size * 2 * n_hvx);
|
||||
VTCM_LAYOUT_ALLOC(off_group_b, off_row_bufs, row_vec_size * (is_w8a8 ? 4 : 2) * n_hvx);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_k_scales[0], k_scales_size, is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_k_scales[1], k_scales_size, is_w8a8 && pipeline);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_v_scales[0], v_scales_size, is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_v_scales[1], v_scales_size, is_w8a8 && pipeline);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_p_scales[0], p_scales_size, is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_p_scales[1], p_scales_size, is_w8a8 && pipeline);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_exp_mdiff[0], exp_mdiff_size, is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_exp_mdiff[1], exp_mdiff_size, is_w8a8 && pipeline);
|
||||
if (is_w8a8) {
|
||||
off_group_b = hex_align_up(off_group_b, HMX_W8A8_BIAS_BANK_SIZE);
|
||||
}
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_k_bias[0], k_bias_size, is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_k_bias[1], k_bias_size, is_w8a8 && pipeline);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_v_bias[0], v_bias_size, is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_b, off_v_bias[1], v_bias_size, is_w8a8 && pipeline);
|
||||
|
||||
const size_t group_b_size = off_group_b - off_group_b_c;
|
||||
|
||||
@@ -220,6 +319,7 @@ static inline void hmx_fa_vtcm_layout_build(struct hmx_fa_vtcm_layout * L,
|
||||
off = off_group_b_c + hex_smax(group_b_size, group_c_size);
|
||||
|
||||
// Group A (Part 2 - remaining non-HMX buffers)
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_o_f32, o_f32_size, is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_fp16[0], k_dma_size);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_fp16[1], k_dma_size);
|
||||
VTCM_LAYOUT_ALLOC(off, off_v_fp16[0], v_dma_size);
|
||||
@@ -228,8 +328,9 @@ static inline void hmx_fa_vtcm_layout_build(struct hmx_fa_vtcm_layout * L,
|
||||
VTCM_LAYOUT_ALLOC(off, off_l_vec, col_vec_size);
|
||||
VTCM_LAYOUT_ALLOC(off, off_hmx_scales_id, 256);
|
||||
VTCM_LAYOUT_ALLOC(off, off_hmx_scales_qk, 256);
|
||||
VTCM_LAYOUT_ALLOC(off, off_mask_buf, m_buf_size);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_mask_buf, m_buf_size, has_mask);
|
||||
VTCM_LAYOUT_ALLOC(off, off_slopes, slopes_size);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_q_scales, q_scales_size, is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_sinks, sinks_size, has_sinks);
|
||||
|
||||
L->q_tile_bytes = q_tile_size;
|
||||
@@ -237,29 +338,44 @@ static inline void hmx_fa_vtcm_layout_build(struct hmx_fa_vtcm_layout * L,
|
||||
L->k_tile_bytes = k_tile_size;
|
||||
L->col_vec_bytes = col_vec_size;
|
||||
L->s_tile_bytes = s_tile_size;
|
||||
L->p_tile_bytes = p_tile_size;
|
||||
// Measured from the actual offsets rather than assumed to be N * d_tile_size, so
|
||||
// that inserting a region between them (or adding padding to VTCM_LAYOUT_ALLOC)
|
||||
// cannot silently leave the tail of the run unzeroed.
|
||||
L->d_tile_bytes = (L->off_d_inv_l + d_tile_size) - L->off_d_tiles[0];
|
||||
L->d_tile_bytes = is_w8a8 ? 0 : ((L->off_d_inv_l + d_tile_size) - L->off_d_tiles[0]);
|
||||
L->o_f32_bytes = is_w8a8 ? o_f32_size : 0;
|
||||
L->m_line_bytes = m_line_size;
|
||||
L->m_buf_slot_bytes = m_buf_slot;
|
||||
L->k_raw_row_padded = k_raw_row_padded;
|
||||
L->v_raw_row_padded = v_raw_row_padded;
|
||||
L->row_buf_stride = row_vec_size / 128;
|
||||
L->mask_buf_row_stride = m_line_size / sizeof(__fp16);
|
||||
L->sinks_bytes = has_sinks ? sinks_size : 0;
|
||||
L->pipeline = pipeline;
|
||||
L->is_w8a8 = is_w8a8;
|
||||
L->total_bytes = off;
|
||||
}
|
||||
|
||||
// Exact VTCM usage for a given (gqa_factor, DK, DV, Br, Bc) configuration.
|
||||
static inline size_t hmx_fa_compute_vtcm_usage(size_t gqa_factor, size_t DK, size_t DV, size_t Br, size_t Bc, size_t n_hvx, bool pipeline, bool is_q_fp32, bool has_sinks, size_t n_heads) {
|
||||
// Exact VTCM usage for a given HMX layout configuration.
|
||||
static inline size_t hmx_fa_compute_vtcm_usage(const struct hmx_fa_layout_params * p) {
|
||||
struct hmx_fa_vtcm_layout L;
|
||||
hmx_fa_vtcm_layout_build(&L, gqa_factor, DK, DV, Br, Bc, n_hvx, pipeline, is_q_fp32, has_sinks, n_heads);
|
||||
hmx_fa_vtcm_layout_build(&L, p);
|
||||
return L.total_bytes;
|
||||
}
|
||||
|
||||
#define FA_HVX_BLOCK_SIZE 64
|
||||
#define FA_HVX_G_MAX 8
|
||||
|
||||
struct hvx_fa_layout_params {
|
||||
int q_type;
|
||||
size_t DK;
|
||||
size_t DV;
|
||||
size_t n_heads;
|
||||
size_t n_hvx;
|
||||
bool has_mask;
|
||||
bool has_sinks;
|
||||
};
|
||||
|
||||
struct hvx_fa_vtcm_layout {
|
||||
size_t off_q;
|
||||
size_t off_k;
|
||||
@@ -279,65 +395,57 @@ struct hvx_fa_vtcm_layout {
|
||||
};
|
||||
|
||||
static inline void hvx_fa_vtcm_layout_build(struct hvx_fa_vtcm_layout * L,
|
||||
size_t DK, size_t DV,
|
||||
bool is_q_fp32, bool has_mask,
|
||||
bool has_sinks, size_t n_heads,
|
||||
size_t n_hvx) {
|
||||
const size_t size_q_row_padded = hex_round_up(DK * (is_q_fp32 ? 4 : 2), 128);
|
||||
const size_t size_k_row_padded = hex_round_up(DK * sizeof(__fp16), 128);
|
||||
const size_t size_v_row_padded = hex_round_up(DV * sizeof(__fp16), 128);
|
||||
const struct hvx_fa_layout_params * p) {
|
||||
const size_t size_q_row_padded = hex_round_up(htp_tensor_get_row_size(p->q_type, p->DK), 128);
|
||||
const size_t size_k_row_padded = hex_round_up(p->DK * sizeof(__fp16), 128);
|
||||
const size_t size_v_row_padded = hex_round_up(p->DV * sizeof(__fp16), 128);
|
||||
|
||||
const size_t size_q_block = size_q_row_padded * FA_HVX_G_MAX;
|
||||
const size_t size_k_block = size_k_row_padded * FA_HVX_BLOCK_SIZE;
|
||||
const size_t size_v_block = size_v_row_padded * FA_HVX_BLOCK_SIZE;
|
||||
const size_t size_m_block = hex_round_up(FA_HVX_BLOCK_SIZE * sizeof(__fp16), 128);
|
||||
const size_t size_vkq_acc = hex_round_up(DV * sizeof(float), 128) * FA_HVX_G_MAX;
|
||||
const size_t size_sinks = hex_round_up(n_heads * sizeof(float), 128);
|
||||
const size_t size_vkq_acc = hex_round_up(p->DV * sizeof(float), 256) * FA_HVX_G_MAX;
|
||||
const size_t size_sinks = hex_round_up(p->n_heads * sizeof(float), 128);
|
||||
|
||||
size_t off = 0;
|
||||
|
||||
VTCM_LAYOUT_ALLOC(off, off_q, size_q_block * n_hvx);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k, size_k_block * 2 * n_hvx);
|
||||
VTCM_LAYOUT_ALLOC(off, off_v, size_v_block * 2 * n_hvx);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_m, size_m_block * HVX_FA_DMA_CACHE_SIZE * n_hvx, has_mask);
|
||||
VTCM_LAYOUT_ALLOC(off, off_a, size_vkq_acc * n_hvx);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_sinks, size_sinks, has_sinks);
|
||||
VTCM_LAYOUT_ALLOC(off, off_q, size_q_block * p->n_hvx);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k, size_k_block * 2 * p->n_hvx);
|
||||
VTCM_LAYOUT_ALLOC(off, off_v, size_v_block * 2 * p->n_hvx);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_m, size_m_block * HVX_FA_DMA_CACHE_SIZE * p->n_hvx, p->has_mask);
|
||||
VTCM_LAYOUT_ALLOC(off, off_a, size_vkq_acc * p->n_hvx);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_sinks, size_sinks, p->has_sinks);
|
||||
|
||||
L->size_q_block = size_q_block;
|
||||
L->size_k_block = size_k_block;
|
||||
L->size_v_block = size_v_block;
|
||||
L->size_m_block = size_m_block;
|
||||
L->size_vkq_acc = size_vkq_acc;
|
||||
L->size_sinks = has_sinks ? size_sinks : 0;
|
||||
L->size_sinks = p->has_sinks ? size_sinks : 0;
|
||||
L->total_bytes = off;
|
||||
}
|
||||
|
||||
static inline size_t hvx_fa_compute_vtcm_usage(size_t DK, size_t DV, bool is_q_fp32, bool has_mask, bool has_sinks, size_t n_heads, size_t n_hvx) {
|
||||
static inline size_t hvx_fa_compute_vtcm_usage(const struct hvx_fa_layout_params * p) {
|
||||
struct hvx_fa_vtcm_layout L;
|
||||
hvx_fa_vtcm_layout_build(&L, DK, DV, is_q_fp32, has_mask, has_sinks, n_heads, n_hvx);
|
||||
hvx_fa_vtcm_layout_build(&L, p);
|
||||
return L.total_bytes;
|
||||
}
|
||||
|
||||
#define FA_MIN_KV_BLOCKS 3
|
||||
|
||||
// Cost-based (Br, Bc) search for flash attention with pipeline constraint.
|
||||
static inline int hmx_fa_find_chunk_size(size_t * Br_out,
|
||||
size_t * Bc_out,
|
||||
size_t gqa_factor,
|
||||
size_t DK,
|
||||
size_t DV,
|
||||
size_t qo_len,
|
||||
size_t kv_len,
|
||||
size_t vtcm_budget,
|
||||
size_t n_hvx,
|
||||
bool is_q_fp32,
|
||||
bool has_sinks,
|
||||
size_t n_heads,
|
||||
size_t q_split) {
|
||||
const size_t T = HMX_FP16_TILE_N_ROWS; // 32
|
||||
const size_t br_unit = hmx_ceil_div(T, gqa_factor);
|
||||
const size_t bc_unit = HMX_FP16_TILE_N_COLS * 2; // 64
|
||||
const bool can_pipeline = (kv_len >= FA_MIN_KV_BLOCKS * bc_unit && n_hvx >= 2);
|
||||
static inline int hmx_fa_find_chunk_size(struct hmx_fa_layout_params * p,
|
||||
size_t qo_len,
|
||||
size_t kv_len,
|
||||
size_t vtcm_budget,
|
||||
size_t q_split) {
|
||||
const size_t gqa_factor = p->gqa_factor;
|
||||
const size_t n_hvx = p->n_hvx;
|
||||
const bool is_w8a8 = (p->kernel_type == HTP_FA_KERNEL_HMX_W8A8);
|
||||
const size_t T = HMX_FP16_TILE_N_ROWS; // 32
|
||||
const size_t br_unit = hmx_ceil_div(T, gqa_factor);
|
||||
const size_t bc_unit = HMX_FP16_TILE_N_COLS * 2; // 64
|
||||
const bool can_pipeline = (kv_len >= FA_MIN_KV_BLOCKS * bc_unit && n_hvx >= 2 && (!p->has_mask || p->mask_broadcast));
|
||||
|
||||
// Br_max: largest Br aligned to br_unit that does not exceed qo_len.
|
||||
const size_t Br_max = qo_len >= br_unit ? hex_align_down(qo_len, br_unit) : br_unit;
|
||||
@@ -350,6 +458,10 @@ static inline int hmx_fa_find_chunk_size(size_t * Br_out,
|
||||
if (qo_len <= 16 && kv_len <= 2048 && Bc_limit > 256) {
|
||||
Bc_limit = 256;
|
||||
}
|
||||
// Cap Bc at 2048 for W8A8 so P * V always runs in 1 superblock (<= 64 tiles of 32)
|
||||
if (is_w8a8 && Bc_limit > 2048) {
|
||||
Bc_limit = 2048;
|
||||
}
|
||||
// Cost coefficients calibrated from profiling
|
||||
const size_t c_q_fixed = 800; // per-Q-block: q_load + epilogue o_update + o_norm + o_store
|
||||
const size_t c_iter_base = 200; // per-KV-iter base (HMX dot/update + DMA)
|
||||
@@ -358,10 +470,15 @@ static inline int hmx_fa_find_chunk_size(size_t * Br_out,
|
||||
size_t best_cost = SIZE_MAX, best_mn = 0;
|
||||
size_t best_Br = 0, best_Bc = 0;
|
||||
|
||||
struct hmx_fa_layout_params cand = *p;
|
||||
cand.pipeline = can_pipeline;
|
||||
|
||||
for (size_t Br = Br_max; Br >= br_unit; Br -= br_unit) {
|
||||
cand.Br = Br;
|
||||
// Try all Bc candidates from Bc_limit down to bc_unit
|
||||
for (size_t Bc = Bc_limit; Bc >= bc_unit; Bc -= bc_unit) {
|
||||
size_t vtcm_needed = hmx_fa_compute_vtcm_usage(gqa_factor, DK, DV, Br, Bc, n_hvx, can_pipeline, is_q_fp32, has_sinks, n_heads);
|
||||
cand.Bc = Bc;
|
||||
size_t vtcm_needed = hmx_fa_compute_vtcm_usage(&cand);
|
||||
if (vtcm_needed <= vtcm_budget) {
|
||||
// This Bc fits for this Br!
|
||||
const size_t q_blocks = (qo_len + Br - 1) / Br;
|
||||
@@ -398,8 +515,8 @@ static inline int hmx_fa_find_chunk_size(size_t * Br_out,
|
||||
return -1;
|
||||
}
|
||||
|
||||
*Br_out = best_Br;
|
||||
*Bc_out = best_Bc;
|
||||
p->Br = best_Br;
|
||||
p->Bc = best_Bc;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -12,6 +12,7 @@
|
||||
#define HTP_GDN_MAX_SV 128
|
||||
#define HTP_GDN_CHUNK_SIZE 64
|
||||
#define HTP_GDN_MIN_TOKENS 8
|
||||
#define HTP_GDN_TOKEN_TILE 16
|
||||
|
||||
#ifndef HMX_FP16_TILE_SIZE
|
||||
#define HMX_FP16_TILE_SIZE 2048
|
||||
@@ -24,7 +25,7 @@ enum htp_gdn_kernel_type {
|
||||
|
||||
struct htp_gdn_kernel_params {
|
||||
uint8_t kernel_type;
|
||||
uint8_t pipeline;
|
||||
uint8_t reserved0;
|
||||
uint16_t chunk_size;
|
||||
uint16_t n_chunks;
|
||||
uint16_t n_heads_batch;
|
||||
@@ -59,6 +60,12 @@ _Static_assert(sizeof(struct htp_gdn_kernel_params) <= 128, "htp_gdn_kernel_para
|
||||
|
||||
struct htp_gdn_vtcm_layout {
|
||||
size_t state_aligned;
|
||||
size_t scratch_bytes;
|
||||
size_t tile_vec_bytes;
|
||||
size_t tile_scalar_bytes;
|
||||
size_t spad_stride;
|
||||
size_t off_scratch;
|
||||
size_t off_spad;
|
||||
size_t bytes_per_hvx;
|
||||
size_t off_state;
|
||||
size_t total_bytes;
|
||||
@@ -69,9 +76,16 @@ static inline void htp_gdn_vtcm_layout_build(
|
||||
uint32_t S_v,
|
||||
uint32_t n_hvx
|
||||
) {
|
||||
size_t state_bytes = (size_t) S_v * S_v * sizeof(float);
|
||||
L->state_aligned = hex_round_up(state_bytes, 128);
|
||||
L->bytes_per_hvx = 2 * L->state_aligned;
|
||||
const size_t state_bytes = (size_t) S_v * S_v * sizeof(float);
|
||||
const size_t vec_aligned = hex_round_up((size_t) S_v * sizeof(float), 128);
|
||||
L->state_aligned = hex_round_up(state_bytes, 128);
|
||||
L->scratch_bytes = 24 * 128;
|
||||
L->tile_vec_bytes = HTP_GDN_TOKEN_TILE * vec_aligned;
|
||||
L->tile_scalar_bytes = hex_round_up(HTP_GDN_TOKEN_TILE * sizeof(float), 128);
|
||||
L->spad_stride = 5 * L->tile_vec_bytes + L->tile_scalar_bytes;
|
||||
L->off_scratch = 2 * L->state_aligned;
|
||||
L->off_spad = L->off_scratch + L->scratch_bytes;
|
||||
L->bytes_per_hvx = L->off_spad + 2 * L->spad_stride + 128;
|
||||
size_t off = 0;
|
||||
VTCM_LAYOUT_ALLOC(off, off_state, L->bytes_per_hvx * n_hvx);
|
||||
L->total_bytes = off;
|
||||
@@ -79,9 +93,7 @@ static inline void htp_gdn_vtcm_layout_build(
|
||||
|
||||
struct htp_gdn_hmx_vtcm_layout {
|
||||
size_t off_s_state;
|
||||
size_t off_s_f16;
|
||||
size_t off_s_col_tiles;
|
||||
size_t off_s_update_f32;
|
||||
size_t off_s_update_tiles;
|
||||
|
||||
size_t off_q_f32[2];
|
||||
@@ -93,20 +105,14 @@ struct htp_gdn_hmx_vtcm_layout {
|
||||
size_t off_b_raw[2];
|
||||
size_t off_o_f32[2];
|
||||
|
||||
size_t off_v_inter_f32;
|
||||
size_t off_o_inter_f32;
|
||||
size_t off_o_intra_f32;
|
||||
size_t off_k_f16;
|
||||
size_t off_v_prime_f16;
|
||||
size_t off_delta_f16;
|
||||
size_t off_d_f16;
|
||||
|
||||
size_t off_q_row_tiles;
|
||||
size_t off_q_prime_row_tiles;
|
||||
size_t off_k_row_tiles;
|
||||
size_t off_k_col_tiles;
|
||||
size_t off_k_prime_row_tiles;
|
||||
size_t off_k_col_tiles_64x128;
|
||||
size_t off_k_col_tiles_64x128[2];
|
||||
size_t off_kk_tiles;
|
||||
size_t off_qk_tiles;
|
||||
size_t off_v_inter_tiles;
|
||||
@@ -115,12 +121,11 @@ struct htp_gdn_hmx_vtcm_layout {
|
||||
size_t off_a_row_tiles;
|
||||
size_t off_v_prime_col_tiles;
|
||||
size_t off_delta_tiles;
|
||||
size_t off_delta_col_tiles;
|
||||
size_t off_o_intra_tiles;
|
||||
size_t off_d_row_tiles;
|
||||
|
||||
size_t off_gamma;
|
||||
size_t off_lambda_init;
|
||||
size_t off_lambda_init[2];
|
||||
size_t off_decay_m;
|
||||
size_t off_decay_a;
|
||||
size_t off_rows_kk;
|
||||
@@ -132,7 +137,6 @@ struct htp_gdn_hmx_vtcm_layout {
|
||||
size_t off_scales_1;
|
||||
|
||||
size_t state_f32_bytes;
|
||||
size_t state_f16_bytes;
|
||||
size_t state_tiles_bytes;
|
||||
size_t dma_chunk_bytes;
|
||||
size_t act_f16_bytes;
|
||||
@@ -141,7 +145,6 @@ struct htp_gdn_hmx_vtcm_layout {
|
||||
|
||||
uint32_t n_heads_batch;
|
||||
uint32_t n_hvx;
|
||||
bool pipeline;
|
||||
size_t total_bytes;
|
||||
};
|
||||
|
||||
@@ -150,19 +153,16 @@ static inline void htp_gdn_hmx_vtcm_layout_build(
|
||||
uint32_t S_v,
|
||||
uint32_t chunk_size,
|
||||
uint32_t n_heads_batch,
|
||||
uint32_t n_hvx,
|
||||
bool pipeline
|
||||
uint32_t n_hvx
|
||||
) {
|
||||
memset(L, 0, sizeof(*L));
|
||||
L->n_heads_batch = n_heads_batch;
|
||||
L->n_hvx = n_hvx;
|
||||
L->pipeline = pipeline;
|
||||
|
||||
const size_t bh = (size_t) n_heads_batch;
|
||||
const size_t nth = (size_t) (n_hvx > 0 ? n_hvx : 1);
|
||||
|
||||
const size_t state_f32_sz = hex_round_up(S_v * S_v * sizeof(float), 2048);
|
||||
const size_t state_f16_sz = hex_round_up(S_v * S_v * sizeof(__fp16), 2048);
|
||||
const size_t n_sv_tiles = S_v / 32;
|
||||
const size_t state_tiles_sz = n_sv_tiles * n_sv_tiles * 2048;
|
||||
|
||||
@@ -177,7 +177,6 @@ static inline void htp_gdn_hmx_vtcm_layout_build(
|
||||
const size_t row_vecs_sz = 64 * 128;
|
||||
|
||||
L->state_f32_bytes = state_f32_sz;
|
||||
L->state_f16_bytes = state_f16_sz;
|
||||
L->state_tiles_bytes = state_tiles_sz;
|
||||
L->dma_chunk_bytes = dma_chunk_sz;
|
||||
L->act_f16_bytes = act_f16_sz;
|
||||
@@ -187,60 +186,52 @@ static inline void htp_gdn_hmx_vtcm_layout_build(
|
||||
size_t off = 0;
|
||||
|
||||
VTCM_LAYOUT_ALLOC(off, off_s_state, bh * state_f32_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_s_f16, bh * state_f16_sz);
|
||||
off = hex_align_up(off, HMX_FP16_TILE_SIZE);
|
||||
VTCM_LAYOUT_ALLOC(off, off_s_col_tiles, bh * state_tiles_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_s_update_f32, bh * state_f32_sz);
|
||||
off = hex_align_up(off, HMX_FP16_TILE_SIZE);
|
||||
VTCM_LAYOUT_ALLOC(off, off_s_update_tiles, bh * state_tiles_sz);
|
||||
|
||||
VTCM_LAYOUT_ALLOC(off, off_q_f32[0], bh * dma_chunk_sz);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_q_f32[1], bh * dma_chunk_sz, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off, off_q_f32[1], bh * dma_chunk_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_f32[0], bh * dma_chunk_sz);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_k_f32[1], bh * dma_chunk_sz, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_f32[1], bh * dma_chunk_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_v_f32[0], bh * dma_chunk_sz);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_v_f32[1], bh * dma_chunk_sz, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off, off_v_f32[1], bh * dma_chunk_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_g_f32[0], bh * dma_scalar_sz);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_g_f32[1], bh * dma_scalar_sz, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off, off_g_f32[1], bh * dma_scalar_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_b_f32[0], bh * dma_scalar_sz);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_b_f32[1], bh * dma_scalar_sz, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off, off_b_f32[1], bh * dma_scalar_sz);
|
||||
const size_t raw_gb_sz = hex_round_up((bh + 1) * chunk_size * sizeof(float), 128);
|
||||
VTCM_LAYOUT_ALLOC(off, off_g_raw[0], raw_gb_sz);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_g_raw[1], raw_gb_sz, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off, off_g_raw[1], raw_gb_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_b_raw[0], raw_gb_sz);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_b_raw[1], raw_gb_sz, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off, off_b_raw[1], raw_gb_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_o_f32[0], bh * dma_chunk_sz);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_o_f32[1], bh * dma_chunk_sz, pipeline);
|
||||
VTCM_LAYOUT_ALLOC(off, off_o_f32[1], bh * dma_chunk_sz);
|
||||
|
||||
VTCM_LAYOUT_ALLOC(off, off_v_inter_f32, bh * dma_chunk_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_o_inter_f32, bh * dma_chunk_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_o_intra_f32, bh * dma_chunk_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_f16, bh * act_f16_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_v_prime_f16, bh * act_f16_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_delta_f16, bh * act_f16_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_d_f16, bh * act_f16_sz);
|
||||
|
||||
off = hex_align_up(off, HMX_FP16_TILE_SIZE);
|
||||
VTCM_LAYOUT_ALLOC(off, off_q_row_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_q_prime_row_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_row_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_col_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_prime_row_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_col_tiles_64x128, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_kk_tiles, bh * tile_64x64_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_qk_tiles, bh * tile_64x64_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_v_inter_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_o_inter_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_inv_row_tiles, bh * tile_64x64_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_a_row_tiles, bh * tile_64x64_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_v_prime_col_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_delta_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_delta_col_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_o_intra_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_d_row_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_prime_row_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_col_tiles_64x128[0], bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_col_tiles_64x128[1], bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_kk_tiles, bh * tile_64x64_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_qk_tiles, bh * tile_64x64_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_v_inter_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_o_inter_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_inv_row_tiles, bh * tile_64x64_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_a_row_tiles, bh * tile_64x64_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_v_prime_col_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_delta_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_o_intra_tiles, bh * tile_64xSv_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_d_row_tiles, bh * tile_64xSv_sz);
|
||||
|
||||
VTCM_LAYOUT_ALLOC(off, off_gamma, bh * hex_round_up(chunk_size * sizeof(float), 128));
|
||||
VTCM_LAYOUT_ALLOC(off, off_lambda_init, bh * hex_round_up(chunk_size * sizeof(float), 128));
|
||||
VTCM_LAYOUT_ALLOC(off, off_gamma, bh * hex_round_up(chunk_size * sizeof(float), 128));
|
||||
VTCM_LAYOUT_ALLOC(off, off_lambda_init[0], bh * hex_round_up(chunk_size * sizeof(float), 128));
|
||||
VTCM_LAYOUT_ALLOC(off, off_lambda_init[1], bh * hex_round_up(chunk_size * sizeof(float), 128));
|
||||
VTCM_LAYOUT_ALLOC(off, off_decay_m, bh * decay_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_decay_a, bh * decay_sz);
|
||||
VTCM_LAYOUT_ALLOC(off, off_rows_kk, bh * row_vecs_sz);
|
||||
@@ -264,7 +255,6 @@ static inline bool htp_gdn_hmx_solve_layout(
|
||||
uint32_t total_rows,
|
||||
size_t vtcm_budget,
|
||||
uint32_t n_hvx,
|
||||
bool pipeline,
|
||||
uint32_t * n_heads_batch_out
|
||||
) {
|
||||
uint32_t max_batch = 8;
|
||||
@@ -281,16 +271,13 @@ static inline bool htp_gdn_hmx_solve_layout(
|
||||
continue;
|
||||
}
|
||||
struct htp_gdn_hmx_vtcm_layout L;
|
||||
htp_gdn_hmx_vtcm_layout_build(&L, S_v, chunk_size, bh, n_hvx, pipeline);
|
||||
htp_gdn_hmx_vtcm_layout_build(&L, S_v, chunk_size, bh, n_hvx);
|
||||
if (L.total_bytes <= vtcm_budget) {
|
||||
*layout_out = L;
|
||||
*n_heads_batch_out = bh;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (pipeline) {
|
||||
return htp_gdn_hmx_solve_layout(layout_out, S_v, chunk_size, total_rows, vtcm_budget, n_hvx, false, n_heads_batch_out);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ struct gather_rows_task {
|
||||
uint32_t r_start;
|
||||
uint32_t rows;
|
||||
uint32_t rows_per_hvx;
|
||||
uint32_t dst_es;
|
||||
const struct htp_gather_rows_params * kparams;
|
||||
gather_rows_compute_fn_t compute;
|
||||
};
|
||||
@@ -72,7 +73,10 @@ static __attribute__((noinline)) void gather_rows_init_tables_f32(
|
||||
int32_t s_buf[32] __attribute__((aligned(128)));
|
||||
for (uint32_t k = 0; k < kparams->vg; k++) {
|
||||
for (uint32_t lane = 0; lane < 32; lane++) {
|
||||
if (col < kparams->na) {
|
||||
if (row >= kparams->nr_max) {
|
||||
t_buf[lane] = 0;
|
||||
s_buf[lane] = 0;
|
||||
} else if (col < kparams->na) {
|
||||
t_buf[lane] = row * a->nb[1] + col * 4;
|
||||
s_buf[lane] = s_a;
|
||||
} else {
|
||||
@@ -103,7 +107,10 @@ static __attribute__((noinline)) void gather_rows_init_tables_f16(
|
||||
int16_t s_buf[64] __attribute__((aligned(128)));
|
||||
for (uint32_t k = 0; k < kparams->vg; k++) {
|
||||
for (uint32_t lane = 0; lane < 64; lane++) {
|
||||
if (col < kparams->na) {
|
||||
if (row >= kparams->nr_max) {
|
||||
t_buf[lane] = 0;
|
||||
s_buf[lane] = 0;
|
||||
} else if (col < kparams->na) {
|
||||
t_buf[lane] = (int16_t) (row * a->nb[1] + col * 2);
|
||||
s_buf[lane] = s_a;
|
||||
} else {
|
||||
@@ -120,6 +127,26 @@ static __attribute__((noinline)) void gather_rows_init_tables_f16(
|
||||
}
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void gather_rows_widen_f16_to_f32(uint8_t * restrict buf, uint32_t nvec) {
|
||||
HVX_Vector * restrict vbuf = (HVX_Vector *) buf;
|
||||
for (int32_t v = (int32_t) nvec - 1; v >= 0; v--) {
|
||||
const HVX_VectorPair vp = hvx_vec_f16_to_f32(vbuf[v]);
|
||||
vbuf[2 * v + 0] = Q6_V_lo_W(vp);
|
||||
vbuf[2 * v + 1] = Q6_V_hi_W(vp);
|
||||
}
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void gather_rows_pack_f32_to_f16(uint8_t * restrict buf, uint32_t nvec) {
|
||||
HVX_Vector * restrict vbuf = (HVX_Vector *) buf;
|
||||
uint32_t v = 0;
|
||||
for (; v + 1 < nvec; v += 2) {
|
||||
vbuf[v >> 1] = hvx_vec_f32_to_f16(vbuf[v], vbuf[v + 1]);
|
||||
}
|
||||
if (v < nvec) {
|
||||
vbuf[v >> 1] = hvx_vec_f32_to_f16(vbuf[v], Q6_V_vzero());
|
||||
}
|
||||
}
|
||||
|
||||
static void gather_rows_compute_f32_vg1(
|
||||
uint8_t * restrict out, const uint8_t * restrict region, uint32_t nvec,
|
||||
const struct htp_gather_rows_params * restrict kparams,
|
||||
@@ -133,6 +160,7 @@ static void gather_rows_compute_f32_vg1(
|
||||
Q6_vgather_ARMVw((HVX_Vector *) (out + v * 128), (size_t) region, region_size, vcur);
|
||||
vcur = Q6_Vw_vadd_VwVw(vcur, vinc);
|
||||
}
|
||||
hvx_gather_sync(out);
|
||||
}
|
||||
|
||||
static void gather_rows_compute_f32_vgn(
|
||||
@@ -153,6 +181,7 @@ static void gather_rows_compute_f32_vgn(
|
||||
k = 0;
|
||||
}
|
||||
}
|
||||
hvx_gather_sync(out);
|
||||
}
|
||||
|
||||
static void gather_rows_compute_f16_vg1(
|
||||
@@ -168,6 +197,7 @@ static void gather_rows_compute_f16_vg1(
|
||||
Q6_vgather_ARMVh((HVX_Vector *) (out + v * 128), (size_t) region, region_size, vcur);
|
||||
vcur = Q6_Vh_vadd_VhVh(vcur, vinc);
|
||||
}
|
||||
hvx_gather_sync(out);
|
||||
}
|
||||
|
||||
static void gather_rows_compute_f16_vgn(
|
||||
@@ -188,6 +218,7 @@ static void gather_rows_compute_f16_vgn(
|
||||
k = 0;
|
||||
}
|
||||
}
|
||||
hvx_gather_sync(out);
|
||||
}
|
||||
|
||||
static void gather_rows_thread_f32(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
@@ -207,7 +238,7 @@ static void gather_rows_thread_f32(const htp_thread_t * th, unsigned int nth, un
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
|
||||
struct htp_gather_rows_vtcm_layout layout;
|
||||
htp_gather_rows_vtcm_layout_build(&layout, kparams->vg, kparams->region_size, kparams->nr_max, kparams->ne, 4);
|
||||
htp_gather_rows_vtcm_layout_build(&layout, kparams->vg, kparams->region_size, kparams->nr_max, kparams->ne, 4, task->dst_es);
|
||||
|
||||
uint8_t * base = VTCM_LAYOUT_PTR(uint8_t, task->octx->ctx->vtcm_base, (size_t) ith * kparams->slice);
|
||||
HVX_Vector * tab = VTCM_LAYOUT_PTR(HVX_Vector, base, layout.off_tab);
|
||||
@@ -255,10 +286,12 @@ static void gather_rows_thread_f32(const htp_thread_t * th, unsigned int nth, un
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r);
|
||||
const uint32_t nvec = (nr * kparams->ne + 31) >> 5;
|
||||
task->compute(out[buf], region[buf], nvec, kparams, tab, inc, cur);
|
||||
hvx_gather_sync(out[buf]);
|
||||
if (task->dst_es == 2) {
|
||||
gather_rows_pack_f32_to_f16(out[buf], nvec);
|
||||
}
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r);
|
||||
|
||||
gather_rows_dma_out(q, task, out[buf], r, nr, 4);
|
||||
gather_rows_dma_out(q, task, out[buf], r, nr, task->dst_es);
|
||||
|
||||
const uint32_t r_prefetch = r + 2 * nr_max;
|
||||
if (r_prefetch < r_end) {
|
||||
@@ -288,7 +321,7 @@ static void gather_rows_thread_f16(const htp_thread_t * th, unsigned int nth, un
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
|
||||
struct htp_gather_rows_vtcm_layout layout;
|
||||
htp_gather_rows_vtcm_layout_build(&layout, kparams->vg, kparams->region_size, kparams->nr_max, kparams->ne, 2);
|
||||
htp_gather_rows_vtcm_layout_build(&layout, kparams->vg, kparams->region_size, kparams->nr_max, kparams->ne, 2, task->dst_es);
|
||||
|
||||
uint8_t * base = VTCM_LAYOUT_PTR(uint8_t, task->octx->ctx->vtcm_base, (size_t) ith * kparams->slice);
|
||||
HVX_Vector * tab = VTCM_LAYOUT_PTR(HVX_Vector, base, layout.off_tab);
|
||||
@@ -336,10 +369,12 @@ static void gather_rows_thread_f16(const htp_thread_t * th, unsigned int nth, un
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r);
|
||||
const uint32_t nvec = (nr * kparams->ne + 63) >> 6;
|
||||
task->compute(out[buf], region[buf], nvec, kparams, tab, inc, cur);
|
||||
hvx_gather_sync(out[buf]);
|
||||
if (task->dst_es == 4) {
|
||||
gather_rows_widen_f16_to_f32(out[buf], nvec);
|
||||
}
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) r);
|
||||
|
||||
gather_rows_dma_out(q, task, out[buf], r, nr, 2);
|
||||
gather_rows_dma_out(q, task, out[buf], r, nr, task->dst_es);
|
||||
|
||||
const uint32_t r_prefetch = r + 2 * nr_max;
|
||||
if (r_prefetch < r_end) {
|
||||
@@ -379,25 +414,27 @@ int htp_gather_rows(
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
const uint32_t elem_size = (a->type == HTP_TYPE_F16) ? 2 : 4;
|
||||
const uint32_t src_es = (a->type == HTP_TYPE_F16) ? 2 : 4;
|
||||
const uint32_t dst_es = (octx->dst->type == HTP_TYPE_F16) ? 2 : 4;
|
||||
|
||||
struct gather_rows_task task;
|
||||
task.octx = octx;
|
||||
task.a = a;
|
||||
task.b = b;
|
||||
task.dst = octx->dst->data;
|
||||
task.r_start = r_start;
|
||||
task.rows = r_count;
|
||||
task.kparams = kparams;
|
||||
task.rows_per_hvx = kparams->rows_per_hvx;
|
||||
task.octx = octx;
|
||||
task.a = a;
|
||||
task.b = b;
|
||||
task.dst = octx->dst->data;
|
||||
task.r_start = r_start;
|
||||
task.rows = r_count;
|
||||
task.rows_per_hvx = kparams->rows_per_hvx;
|
||||
task.dst_es = dst_es;
|
||||
task.kparams = kparams;
|
||||
|
||||
if (elem_size == 2) {
|
||||
if (src_es == 2) {
|
||||
task.compute = (kparams->vg == 1) ? gather_rows_compute_f16_vg1 : gather_rows_compute_f16_vgn;
|
||||
} else {
|
||||
task.compute = (kparams->vg == 1) ? gather_rows_compute_f32_vg1 : gather_rows_compute_f32_vgn;
|
||||
}
|
||||
|
||||
work_queue_func_t worker_func = (elem_size == 2) ? gather_rows_thread_f16 : gather_rows_thread_f32;
|
||||
work_queue_func_t worker_func = (src_es == 2) ? gather_rows_thread_f16 : gather_rows_thread_f32;
|
||||
work_queue_run(octx->ctx->hvx_queue, worker_func, &task, octx->n_hvx);
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
@@ -44,11 +44,13 @@ static inline void htp_gather_rows_vtcm_layout_build(
|
||||
uint32_t region_size,
|
||||
uint32_t nr_max,
|
||||
uint32_t ne,
|
||||
uint32_t elem_size) {
|
||||
uint32_t src_es,
|
||||
uint32_t dst_es) {
|
||||
|
||||
const size_t vec_bytes = (size_t) vg * 128;
|
||||
const size_t region_aligned = hex_round_up(region_size, 128);
|
||||
const size_t out_aligned = hex_round_up(region_aligned + (size_t) nr_max * ne * elem_size, 128) - region_aligned;
|
||||
const size_t gather_bytes = hex_round_up((size_t) nr_max * ne * src_es, 128);
|
||||
const size_t out_aligned = (dst_es > src_es) ? (2 * gather_bytes) : gather_bytes;
|
||||
|
||||
size_t off = 0;
|
||||
VTCM_LAYOUT_ALLOC(off, off_tab, vec_bytes);
|
||||
@@ -65,23 +67,23 @@ static inline bool htp_gather_rows_solve_layout(
|
||||
struct htp_gather_rows_params * kparams,
|
||||
uint32_t na, uint32_t a_nb1,
|
||||
uint32_t nb, uint32_t b_nb1, bool b_dense,
|
||||
uint32_t elem_size,
|
||||
uint32_t src_es, uint32_t dst_es,
|
||||
uint32_t total_rows,
|
||||
uint32_t n_hvx,
|
||||
uint32_t mdev_count,
|
||||
uint32_t vtcm_size) {
|
||||
|
||||
const uint32_t max_ne0 = 128 / elem_size;
|
||||
const uint32_t max_ne0 = 128 / src_es;
|
||||
const uint32_t ne = na + nb;
|
||||
if (ne == 0 || ne > max_ne0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (elem_size != 4 && elem_size != 2) {
|
||||
if ((src_es != 4 && src_es != 2) || (dst_es != 4 && dst_es != 2)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t v_lanes = 128 / elem_size;
|
||||
const uint32_t v_lanes = 128 / src_es;
|
||||
const uint32_t g = hex_gcd_u32(ne, v_lanes);
|
||||
const uint32_t rg = v_lanes / g;
|
||||
const uint32_t vg = ne / g;
|
||||
@@ -94,34 +96,37 @@ static inline bool htp_gather_rows_solve_layout(
|
||||
: total_rows;
|
||||
const uint32_t rows_per_hvx = hex_round_up((max_rows_per_dev + n_hvx - 1) / n_hvx, rg);
|
||||
|
||||
const uint32_t fixed = 3 * vg * 128;
|
||||
const uint32_t per_row = a_nb1 + (nb > 0 ? (b_dense ? b_nb1 : nb * b_nb1) : 0) + ne * elem_size;
|
||||
if (slice <= fixed + 2 * per_row * rg) {
|
||||
const uint32_t max_es = MAX(src_es, dst_es);
|
||||
const uint32_t fixed = 3 * vg * 128;
|
||||
const uint32_t per_row = a_nb1 + (nb > 0 ? (b_dense ? b_nb1 : nb * b_nb1) : 0) + ne * max_es;
|
||||
if (slice <= fixed + 2 * per_row) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t avail_per_buf = (slice - fixed) / 2;
|
||||
uint32_t nr_max = MIN(avail_per_buf / per_row, rows_per_hvx);
|
||||
if (elem_size == 2) {
|
||||
if (src_es == 2) {
|
||||
const uint32_t per_row_region = a_nb1 + (nb > 0 ? (b_dense ? b_nb1 : nb * b_nb1) : 0);
|
||||
if (per_row_region > 0) {
|
||||
nr_max = MIN(nr_max, (32768u - 256u) / per_row_region);
|
||||
nr_max = MIN(nr_max, 1 + 32768u / per_row_region);
|
||||
}
|
||||
}
|
||||
nr_max = (nr_max / rg) * rg;
|
||||
if (nr_max >= rg) {
|
||||
nr_max = (nr_max / rg) * rg;
|
||||
}
|
||||
|
||||
uint32_t v1 = 0;
|
||||
uint32_t span1 = 0;
|
||||
uint32_t region_size = 0;
|
||||
|
||||
while (nr_max > 0) {
|
||||
v1 = hex_round_up((nr_max - 1) * a_nb1 + na * elem_size, 128);
|
||||
v1 = hex_round_up((nr_max - 1) * a_nb1 + na * src_es, 128);
|
||||
if (nb > 0) {
|
||||
if (b_dense) {
|
||||
span1 = elem_size;
|
||||
region_size = v1 + (nr_max - 1) * b_nb1 + nb * elem_size;
|
||||
span1 = src_es;
|
||||
region_size = v1 + (nr_max - 1) * b_nb1 + nb * src_es;
|
||||
} else {
|
||||
span1 = (nr_max - 1) * b_nb1 + elem_size;
|
||||
span1 = (nr_max - 1) * b_nb1 + src_es;
|
||||
region_size = v1 + nb * span1;
|
||||
}
|
||||
} else {
|
||||
@@ -130,13 +135,13 @@ static inline bool htp_gather_rows_solve_layout(
|
||||
}
|
||||
|
||||
struct htp_gather_rows_vtcm_layout layout;
|
||||
htp_gather_rows_vtcm_layout_build(&layout, vg, region_size, nr_max, ne, elem_size);
|
||||
htp_gather_rows_vtcm_layout_build(&layout, vg, region_size, nr_max, ne, src_es, dst_es);
|
||||
|
||||
if ((elem_size != 2 || region_size <= 32768) && layout.total_per_hvx <= slice) {
|
||||
if ((src_es != 2 || region_size <= 32768) && layout.total_per_hvx <= slice) {
|
||||
break;
|
||||
}
|
||||
|
||||
nr_max -= rg;
|
||||
nr_max = (nr_max > rg) ? (nr_max - rg) : (nr_max - 1);
|
||||
}
|
||||
|
||||
if (nr_max == 0) {
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
#include <HAP_perf.h>
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define GGML_COMMON_DECL_C
|
||||
#include "ggml-common.h"
|
||||
#include "hex-common.h"
|
||||
#include "dma-queue.h"
|
||||
#include "dma-pipeline.h"
|
||||
#include "htp-ctx.h"
|
||||
#include "htp-ops.h"
|
||||
#include "htp-tensor.h"
|
||||
@@ -22,9 +20,8 @@
|
||||
|
||||
struct get_rows_context;
|
||||
|
||||
typedef uint32_t (*get_rows_read_index_fn_t)(const void * src1, uint32_t idx, int idx_type);
|
||||
typedef void (*get_rows_flat_compute_fn_t)(float * dst_spad, const void * src_spad, uint32_t cur_elems);
|
||||
typedef void (*get_rows_tiled_compute_fn_t)(float * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row);
|
||||
typedef void (*get_rows_tiled_compute_fn_t)(void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row);
|
||||
|
||||
struct get_rows_context {
|
||||
struct htp_ops_context * octx;
|
||||
@@ -32,7 +29,6 @@ struct get_rows_context {
|
||||
struct htp_get_rows_vtcm_layout vtcm_layout;
|
||||
uint8_t * vtcm_base;
|
||||
const void * src1_data;
|
||||
get_rows_read_index_fn_t read_index;
|
||||
get_rows_flat_compute_fn_t compute_flat;
|
||||
get_rows_tiled_compute_fn_t compute_tiled;
|
||||
uint32_t task_start;
|
||||
@@ -45,37 +41,37 @@ struct get_rows_context {
|
||||
int idx_type;
|
||||
};
|
||||
|
||||
#define get_rows_preamble \
|
||||
const uint32_t ne00 = octx->src[0]->ne[0]; \
|
||||
const uint32_t ne01 = octx->src[0]->ne[1]; \
|
||||
const uint32_t ne02 = octx->src[0]->ne[2]; \
|
||||
const uint32_t ne03 = octx->src[0]->ne[3]; \
|
||||
\
|
||||
const uint32_t ne10 = octx->src[1]->ne[0]; \
|
||||
const uint32_t ne11 = octx->src[1]->ne[1]; \
|
||||
const uint32_t ne12 = octx->src[1]->ne[2]; \
|
||||
const uint32_t ne13 = octx->src[1]->ne[3]; \
|
||||
\
|
||||
const uint32_t ne0 = octx->dst->ne[0]; \
|
||||
const uint32_t ne1 = octx->dst->ne[1]; \
|
||||
const uint32_t ne2 = octx->dst->ne[2]; \
|
||||
const uint32_t ne3 = octx->dst->ne[3]; \
|
||||
\
|
||||
const uint32_t nb01 = octx->src[0]->nb[1]; \
|
||||
const uint32_t nb02 = octx->src[0]->nb[2]; \
|
||||
const uint32_t nb03 = octx->src[0]->nb[3]; \
|
||||
\
|
||||
const uint32_t nb10 = octx->src[1]->nb[0]; \
|
||||
const uint32_t nb11 = octx->src[1]->nb[1]; \
|
||||
const uint32_t nb12 = octx->src[1]->nb[2]; \
|
||||
\
|
||||
const uint32_t nb1 = octx->dst->nb[1]; \
|
||||
const uint32_t nb2 = octx->dst->nb[2]; \
|
||||
const uint32_t nb3 = octx->dst->nb[3]; \
|
||||
\
|
||||
const uint32_t nr = ne10 * ne11 * ne12;
|
||||
#define get_rows_preamble \
|
||||
const uint32_t __attribute__((unused)) ne00 = octx->src[0]->ne[0]; \
|
||||
const uint32_t __attribute__((unused)) ne01 = octx->src[0]->ne[1]; \
|
||||
const uint32_t __attribute__((unused)) ne02 = octx->src[0]->ne[2]; \
|
||||
const uint32_t __attribute__((unused)) ne03 = octx->src[0]->ne[3]; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) ne10 = octx->src[1]->ne[0]; \
|
||||
const uint32_t __attribute__((unused)) ne11 = octx->src[1]->ne[1]; \
|
||||
const uint32_t __attribute__((unused)) ne12 = octx->src[1]->ne[2]; \
|
||||
const uint32_t __attribute__((unused)) ne13 = octx->src[1]->ne[3]; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) ne0 = octx->dst->ne[0]; \
|
||||
const uint32_t __attribute__((unused)) ne1 = octx->dst->ne[1]; \
|
||||
const uint32_t __attribute__((unused)) ne2 = octx->dst->ne[2]; \
|
||||
const uint32_t __attribute__((unused)) ne3 = octx->dst->ne[3]; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) nb01 = octx->src[0]->nb[1]; \
|
||||
const uint32_t __attribute__((unused)) nb02 = octx->src[0]->nb[2]; \
|
||||
const uint32_t __attribute__((unused)) nb03 = octx->src[0]->nb[3]; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) nb10 = octx->src[1]->nb[0]; \
|
||||
const uint32_t __attribute__((unused)) nb11 = octx->src[1]->nb[1]; \
|
||||
const uint32_t __attribute__((unused)) nb12 = octx->src[1]->nb[2]; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) nb1 = octx->dst->nb[1]; \
|
||||
const uint32_t __attribute__((unused)) nb2 = octx->dst->nb[2]; \
|
||||
const uint32_t __attribute__((unused)) nb3 = octx->dst->nb[3]; \
|
||||
\
|
||||
const uint32_t __attribute__((unused)) nr = ne10 * ne11 * ne12;
|
||||
|
||||
static uint32_t get_rows_read_index_ddr(
|
||||
static inline uint32_t get_rows_read_index(
|
||||
const void * src1,
|
||||
uint32_t idx,
|
||||
int idx_type
|
||||
@@ -84,17 +80,6 @@ static uint32_t get_rows_read_index_ddr(
|
||||
return *(const uint32_t *) ((const uint8_t *) src1 + off);
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) uint32_t get_rows_read_index_vtcm(
|
||||
const void * src1,
|
||||
uint32_t idx,
|
||||
int idx_type
|
||||
) {
|
||||
const uint32_t off = idx * ((idx_type == HTP_TYPE_I64) ? sizeof(int64_t) : sizeof(int32_t));
|
||||
const HVX_Vector v = hvx_vmem((const uint8_t *) src1 + off); // off & ~127u in HW
|
||||
// vextract is normally slow, but here it avoids an HVX->L2 store and L1D miss
|
||||
return (uint32_t) Q6_R_vextract_VR(v, off);
|
||||
}
|
||||
|
||||
static void get_rows_thread_sametype(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
struct get_rows_context * grctx = (struct get_rows_context *) data;
|
||||
struct htp_ops_context * octx = grctx->octx;
|
||||
@@ -116,7 +101,7 @@ static void get_rows_thread_sametype(const htp_thread_t * th, unsigned int nth,
|
||||
const uint32_t i11 = fastdiv(rem, &kparams->div_ne10);
|
||||
const uint32_t i10 = rem - i11 * ne10;
|
||||
|
||||
const uint32_t i01 = grctx->read_index(grctx->src1_data, i, grctx->idx_type);
|
||||
const uint32_t i01 = get_rows_read_index(grctx->src1_data, i, grctx->idx_type);
|
||||
assert(i01 < ne01);
|
||||
|
||||
const uint32_t q02 = fastdiv(i11, &kparams->div_ne02);
|
||||
@@ -136,6 +121,102 @@ static void get_rows_thread_sametype(const htp_thread_t * th, unsigned int nth,
|
||||
|
||||
|
||||
|
||||
static inline HVX_Vector get_rows_clamp_f16_finite(HVX_Vector v) {
|
||||
const HVX_Vector max_f16 = hvx_vec_splat_f32(65504.0f);
|
||||
const HVX_Vector min_f16 = hvx_vec_splat_f32(-65504.0f);
|
||||
const HVX_Vector inf = hvx_vec_splat_f32(INFINITY);
|
||||
const HVX_VectorPred finite = Q6_Q_vcmp_gt_VsfVsf(inf, hvx_vec_abs_f32(v));
|
||||
const HVX_VectorPred high = Q6_Q_and_QQ(finite, Q6_Q_vcmp_gt_VsfVsf(v, max_f16));
|
||||
const HVX_VectorPred low = Q6_Q_and_QQ(finite, Q6_Q_vcmp_gt_VsfVsf(min_f16, v));
|
||||
v = Q6_V_vmux_QVV(high, max_f16, v);
|
||||
return Q6_V_vmux_QVV(low, min_f16, v);
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void get_rows_flat_f32_to_f16(
|
||||
float * restrict dst_ptr,
|
||||
const void * restrict src_ptr,
|
||||
uint32_t n
|
||||
) {
|
||||
const HVX_Vector * restrict vsrc = (const HVX_Vector *) src_ptr;
|
||||
HVX_Vector * restrict vdst = (HVX_Vector *) dst_ptr;
|
||||
|
||||
const uint32_t n_vec = n / 64;
|
||||
const uint32_t n_rem = n % 64;
|
||||
|
||||
for (uint32_t i = 0; i < n_vec; i++) {
|
||||
const HVX_Vector v0 = get_rows_clamp_f16_finite(vsrc[2 * i + 0]);
|
||||
const HVX_Vector v1 = get_rows_clamp_f16_finite(vsrc[2 * i + 1]);
|
||||
vdst[i] = hvx_vec_f32_to_f16(v0, v1);
|
||||
}
|
||||
|
||||
if (n_rem > 0) {
|
||||
const HVX_Vector v0 = get_rows_clamp_f16_finite(vsrc[2 * n_vec + 0]);
|
||||
const HVX_Vector v1 = (n_rem > 32) ? get_rows_clamp_f16_finite(vsrc[2 * n_vec + 1]) : Q6_V_vzero();
|
||||
const HVX_Vector v = hvx_vec_f32_to_f16(v0, v1);
|
||||
hvx_vec_store_a(&vdst[n_vec], n_rem * sizeof(__fp16), v);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void get_rows_pack_f32_to_f16_inplace(
|
||||
void * spad_ptr,
|
||||
uint32_t n
|
||||
) {
|
||||
HVX_Vector * vec = (HVX_Vector *) spad_ptr;
|
||||
|
||||
const uint32_t n_vec = n / 64;
|
||||
const uint32_t n_rem = n % 64;
|
||||
|
||||
for (uint32_t i = 0; i < n_vec; i++) {
|
||||
const HVX_Vector v0 = get_rows_clamp_f16_finite(vec[2 * i + 0]);
|
||||
const HVX_Vector v1 = get_rows_clamp_f16_finite(vec[2 * i + 1]);
|
||||
vec[i] = hvx_vec_f32_to_f16(v0, v1);
|
||||
}
|
||||
|
||||
if (n_rem > 0) {
|
||||
const HVX_Vector v0 = get_rows_clamp_f16_finite(vec[2 * n_vec + 0]);
|
||||
const HVX_Vector v1 = (n_rem > 32) ? get_rows_clamp_f16_finite(vec[2 * n_vec + 1]) : Q6_V_vzero();
|
||||
const HVX_Vector v = hvx_vec_f32_to_f16(v0, v1);
|
||||
hvx_vec_store_a(&vec[n_vec], n_rem * sizeof(__fp16), v);
|
||||
}
|
||||
}
|
||||
|
||||
#define DEFINE_GET_ROWS_FLAT_F16(type) \
|
||||
static void get_rows_flat_##type##_f16( \
|
||||
float * dst_spad, const void * src_spad, uint32_t n) { \
|
||||
hvx_dequantize_row_flat_##type##_f32(dst_spad, src_spad, n); \
|
||||
get_rows_pack_f32_to_f16_inplace(dst_spad, n); \
|
||||
}
|
||||
|
||||
DEFINE_GET_ROWS_FLAT_F16(bf16)
|
||||
DEFINE_GET_ROWS_FLAT_F16(q1_0)
|
||||
DEFINE_GET_ROWS_FLAT_F16(q2_0)
|
||||
DEFINE_GET_ROWS_FLAT_F16(q8_0)
|
||||
DEFINE_GET_ROWS_FLAT_F16(q4_0)
|
||||
DEFINE_GET_ROWS_FLAT_F16(q4_k)
|
||||
DEFINE_GET_ROWS_FLAT_F16(q5_k)
|
||||
DEFINE_GET_ROWS_FLAT_F16(q6_k)
|
||||
|
||||
#undef DEFINE_GET_ROWS_FLAT_F16
|
||||
|
||||
#define DEFINE_GET_ROWS_TILED_F16(type, tile_k) \
|
||||
static void get_rows_tiled_##type##_f16( \
|
||||
void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, \
|
||||
uint32_t tile_stride, uint32_t row) { \
|
||||
hvx_dequantize_row_tiled_##type##_f32( \
|
||||
dst_spad, src_spad, n_k_tiles, tile_stride, row); \
|
||||
get_rows_pack_f32_to_f16_inplace(dst_spad, n_k_tiles * (tile_k)); \
|
||||
}
|
||||
|
||||
DEFINE_GET_ROWS_TILED_F16(q1_0, HTP_TENSOR_TILE_K_Q1_0)
|
||||
DEFINE_GET_ROWS_TILED_F16(q2_0, HTP_TENSOR_TILE_K_Q2_0)
|
||||
DEFINE_GET_ROWS_TILED_F16(q8_0, HTP_TENSOR_TILE_N_COLS)
|
||||
DEFINE_GET_ROWS_TILED_F16(q4_0, HTP_TENSOR_TILE_N_COLS)
|
||||
DEFINE_GET_ROWS_TILED_F16(q4_k, HTP_TENSOR_TILE_N_COLS)
|
||||
DEFINE_GET_ROWS_TILED_F16(q5_k, HTP_TENSOR_TILE_N_COLS)
|
||||
DEFINE_GET_ROWS_TILED_F16(q6_k, HTP_TENSOR_TILE_N_COLS)
|
||||
|
||||
#undef DEFINE_GET_ROWS_TILED_F16
|
||||
|
||||
struct get_rows_flat_task {
|
||||
dma_addr_t src0_data;
|
||||
dma_addr_t dst_data;
|
||||
@@ -160,7 +241,7 @@ static inline struct get_rows_flat_task get_rows_flat_calc_task(
|
||||
task.dst_data = octx->dst->data + i10*nb1 + i11*nb2 + i12*nb3;
|
||||
|
||||
if (need_src0) {
|
||||
const uint32_t i01 = grctx->read_index(grctx->src1_data, i, grctx->idx_type);
|
||||
const uint32_t i01 = get_rows_read_index(grctx->src1_data, i, grctx->idx_type);
|
||||
assert(i01 < ne01);
|
||||
|
||||
const uint32_t q02 = fastdiv(i11, &kparams->div_ne02);
|
||||
@@ -174,7 +255,53 @@ static inline struct get_rows_flat_task get_rows_flat_calc_task(
|
||||
return task;
|
||||
}
|
||||
|
||||
struct get_rows_flat_pipe_state {
|
||||
const struct htp_ops_context * octx;
|
||||
const struct get_rows_context * grctx;
|
||||
get_rows_flat_compute_fn_t compute;
|
||||
uint32_t ne00;
|
||||
uint32_t src0_bytes;
|
||||
uint32_t dst_bytes;
|
||||
size_t src0_spad_half;
|
||||
size_t dst_spad_half;
|
||||
uint32_t ir;
|
||||
uint32_t ir_end;
|
||||
};
|
||||
|
||||
static bool get_rows_flat_next_task(void * ctx, struct dma_pipe_task * task) {
|
||||
struct get_rows_flat_pipe_state * s = (struct get_rows_flat_pipe_state *) ctx;
|
||||
if (s->ir >= s->ir_end) {
|
||||
return false;
|
||||
}
|
||||
task->row = s->ir++;
|
||||
task->off = 0;
|
||||
task->len = 0;
|
||||
task->aux = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void get_rows_flat_push_in(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad) {
|
||||
struct get_rows_flat_pipe_state * s = (struct get_rows_flat_pipe_state *) ctx;
|
||||
struct get_rows_flat_task t = get_rows_flat_calc_task(s->octx, s->grctx, task->row, true);
|
||||
dma_queue_push(dma_q, dma_make_data(in_spad, t.src0_data),
|
||||
s->src0_spad_half, s->src0_bytes, s->src0_bytes, 1);
|
||||
}
|
||||
|
||||
static void get_rows_flat_compute(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad) {
|
||||
(void) task;
|
||||
struct get_rows_flat_pipe_state * s = (struct get_rows_flat_pipe_state *) ctx;
|
||||
s->compute((float *) out_spad, in_spad, s->ne00);
|
||||
}
|
||||
|
||||
static void get_rows_flat_push_out(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad) {
|
||||
struct get_rows_flat_pipe_state * s = (struct get_rows_flat_pipe_state *) ctx;
|
||||
struct get_rows_flat_task t = get_rows_flat_calc_task(s->octx, s->grctx, task->row, false);
|
||||
dma_queue_push(dma_q, dma_make_data(t.dst_data, out_spad),
|
||||
s->dst_bytes, s->dst_spad_half, s->dst_bytes, 1);
|
||||
}
|
||||
|
||||
static void get_rows_thread_flat(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
(void) nth;
|
||||
struct get_rows_context * grctx = (struct get_rows_context *) data;
|
||||
struct htp_ops_context * octx = grctx->octx;
|
||||
get_rows_preamble;
|
||||
@@ -186,54 +313,27 @@ static void get_rows_thread_flat(const htp_thread_t * th, unsigned int nth, unsi
|
||||
}
|
||||
const uint32_t ir1 = MIN(ir0 + dr, grctx->task_start + grctx->tasks);
|
||||
|
||||
dma_queue * dma_q = th->dma_q;
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
const struct htp_get_rows_vtcm_layout * vtcm_layout = &grctx->vtcm_layout;
|
||||
uint8_t * vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, grctx->vtcm_base, vtcm_layout->off_src0) + ith * vtcm_layout->src0_bytes_per_hvx;
|
||||
uint8_t * vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, grctx->vtcm_base, vtcm_layout->off_dst) + ith * vtcm_layout->dst_bytes_per_hvx;
|
||||
const uint32_t src0_bytes = grctx->src0_row_size;
|
||||
const uint32_t dst_bytes = grctx->dst_row_size;
|
||||
get_rows_flat_compute_fn_t compute = grctx->compute_flat;
|
||||
void * in_buf[2] = { vtcm_src0, vtcm_src0 + vtcm_layout->src0_spad_half_size };
|
||||
void * out_buf[2] = { vtcm_dst, vtcm_dst + vtcm_layout->dst_spad_half_size };
|
||||
|
||||
for (uint32_t step = 0, spad_idx = 0; step < ir1 - ir0 && spad_idx < 2; ++step, spad_idx++) {
|
||||
const uint32_t i = ir0 + step;
|
||||
struct get_rows_flat_task task = get_rows_flat_calc_task(octx, grctx, i, true);
|
||||
struct get_rows_flat_pipe_state s = {
|
||||
.octx = octx,
|
||||
.grctx = grctx,
|
||||
.compute = grctx->compute_flat,
|
||||
.ne00 = ne00,
|
||||
.src0_bytes = grctx->src0_row_size,
|
||||
.dst_bytes = grctx->dst_row_size,
|
||||
.src0_spad_half = vtcm_layout->src0_spad_half_size,
|
||||
.dst_spad_half = vtcm_layout->dst_spad_half_size,
|
||||
.ir = ir0,
|
||||
.ir_end = ir1,
|
||||
};
|
||||
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(octx->dst->data,
|
||||
vtcm_dst + spad_idx * vtcm_layout->dst_spad_half_size),
|
||||
dst_bytes, vtcm_layout->dst_spad_half_size, dst_bytes, 0);
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(vtcm_src0 + spad_idx * vtcm_layout->src0_spad_half_size, task.src0_data),
|
||||
vtcm_layout->src0_spad_half_size, src0_bytes, src0_bytes, 1);
|
||||
}
|
||||
|
||||
for (uint32_t step = 0; step < ir1 - ir0; ++step) {
|
||||
const uint32_t i = ir0 + step;
|
||||
void * dst_spad = (void *) dma_queue_pop(dma_q).src;
|
||||
void * src_spad = (void *) dma_queue_pop(dma_q).dst;
|
||||
|
||||
struct get_rows_flat_task task = get_rows_flat_calc_task(octx, grctx, i, false);
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i);
|
||||
compute((float *) dst_spad, src_spad, ne00);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i);
|
||||
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(task.dst_data, dst_spad),
|
||||
dst_bytes, vtcm_layout->dst_spad_half_size, dst_bytes, 1);
|
||||
|
||||
const uint32_t next_step = step + 2;
|
||||
if (next_step < ir1 - ir0) {
|
||||
const uint32_t pi = ir0 + next_step;
|
||||
struct get_rows_flat_task next_task = get_rows_flat_calc_task(octx, grctx, pi, true);
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(src_spad, next_task.src0_data),
|
||||
vtcm_layout->src0_spad_half_size, src0_bytes, src0_bytes, 1);
|
||||
}
|
||||
}
|
||||
|
||||
dma_queue_flush(dma_q);
|
||||
dma_pipe_io_run(th->dma_q, th->trace, &s, in_buf, out_buf,
|
||||
get_rows_flat_next_task, get_rows_flat_push_in, get_rows_flat_compute, get_rows_flat_push_out);
|
||||
}
|
||||
|
||||
|
||||
@@ -258,7 +358,7 @@ static inline struct get_rows_tiled_task get_rows_tiled_calc_task(
|
||||
const uint32_t rem = i - i12 * ne11 * ne10;
|
||||
const uint32_t i11 = fastdiv(rem, &kparams->div_ne10);
|
||||
const uint32_t i10 = rem - i11 * ne10;
|
||||
const uint32_t i01 = grctx->read_index(grctx->src1_data, i, grctx->idx_type);
|
||||
const uint32_t i01 = get_rows_read_index(grctx->src1_data, i, grctx->idx_type);
|
||||
assert(i01 < ne01);
|
||||
|
||||
const uint32_t q02 = fastdiv(i11, &kparams->div_ne02);
|
||||
@@ -276,7 +376,53 @@ static inline struct get_rows_tiled_task get_rows_tiled_calc_task(
|
||||
return task;
|
||||
}
|
||||
|
||||
struct get_rows_tiled_pipe_state {
|
||||
const struct htp_ops_context * octx;
|
||||
const struct get_rows_context * grctx;
|
||||
get_rows_tiled_compute_fn_t compute_tiled;
|
||||
uint32_t n_k_tiles;
|
||||
uint32_t tile_size;
|
||||
uint32_t tile_stride;
|
||||
uint32_t dst_bytes;
|
||||
size_t dst_spad_half;
|
||||
uint32_t ir;
|
||||
uint32_t ir_end;
|
||||
};
|
||||
|
||||
static bool get_rows_tiled_next_task(void * ctx, struct dma_pipe_task * task) {
|
||||
struct get_rows_tiled_pipe_state * s = (struct get_rows_tiled_pipe_state *) ctx;
|
||||
if (s->ir >= s->ir_end) {
|
||||
return false;
|
||||
}
|
||||
task->row = s->ir++;
|
||||
task->off = 0;
|
||||
task->len = 0;
|
||||
task->aux = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void get_rows_tiled_push_in(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad) {
|
||||
struct get_rows_tiled_pipe_state * s = (struct get_rows_tiled_pipe_state *) ctx;
|
||||
struct get_rows_tiled_task t = get_rows_tiled_calc_task(s->octx, s->grctx, task->row, s->n_k_tiles, s->tile_size);
|
||||
dma_queue_push(dma_q, dma_make_data(in_spad, t.tile_src_base),
|
||||
s->tile_stride, s->tile_size, s->tile_size, s->n_k_tiles);
|
||||
}
|
||||
|
||||
static void get_rows_tiled_compute(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad) {
|
||||
struct get_rows_tiled_pipe_state * s = (struct get_rows_tiled_pipe_state *) ctx;
|
||||
struct get_rows_tiled_task t = get_rows_tiled_calc_task(s->octx, s->grctx, task->row, s->n_k_tiles, s->tile_size);
|
||||
s->compute_tiled(out_spad, (const uint8_t *) in_spad, s->n_k_tiles, s->tile_stride, t.row);
|
||||
}
|
||||
|
||||
static void get_rows_tiled_push_out(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad) {
|
||||
struct get_rows_tiled_pipe_state * s = (struct get_rows_tiled_pipe_state *) ctx;
|
||||
struct get_rows_tiled_task t = get_rows_tiled_calc_task(s->octx, s->grctx, task->row, s->n_k_tiles, s->tile_size);
|
||||
dma_queue_push(dma_q, dma_make_data(t.dst_data, out_spad),
|
||||
s->dst_bytes, s->dst_spad_half, s->dst_bytes, 1);
|
||||
}
|
||||
|
||||
static void get_rows_thread_tiled(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
(void) nth;
|
||||
struct get_rows_context * grctx = (struct get_rows_context *) data;
|
||||
struct htp_ops_context * octx = grctx->octx;
|
||||
get_rows_preamble;
|
||||
@@ -292,84 +438,24 @@ static void get_rows_thread_tiled(const htp_thread_t * th, unsigned int nth, uns
|
||||
const struct htp_get_rows_vtcm_layout * vtcm_layout = &grctx->vtcm_layout;
|
||||
uint8_t * src_spad_base = VTCM_LAYOUT_PTR(uint8_t, grctx->vtcm_base, vtcm_layout->off_src0) + ith * vtcm_layout->src0_bytes_per_hvx;
|
||||
uint8_t * dst_spad_base = VTCM_LAYOUT_PTR(uint8_t, grctx->vtcm_base, vtcm_layout->off_dst) + ith * vtcm_layout->dst_bytes_per_hvx;
|
||||
dma_queue * dma_q = th->dma_q;
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
void * in_buf[2] = { src_spad_base, src_spad_base + vtcm_layout->src0_spad_half_size };
|
||||
void * out_buf[2] = { dst_spad_base, dst_spad_base + vtcm_layout->dst_spad_half_size };
|
||||
|
||||
const uint32_t tile_size = grctx->tile_size;
|
||||
const uint32_t tile_stride = grctx->tile_stride;
|
||||
const uint32_t dst_bytes = grctx->dst_row_size;
|
||||
get_rows_tiled_compute_fn_t compute_tiled = grctx->compute_tiled;
|
||||
struct get_rows_tiled_pipe_state s = {
|
||||
.octx = octx,
|
||||
.grctx = grctx,
|
||||
.compute_tiled = grctx->compute_tiled,
|
||||
.n_k_tiles = n_k_tiles,
|
||||
.tile_size = grctx->tile_size,
|
||||
.tile_stride = grctx->tile_stride,
|
||||
.dst_bytes = grctx->dst_row_size,
|
||||
.dst_spad_half = vtcm_layout->dst_spad_half_size,
|
||||
.ir = ir0,
|
||||
.ir_end = ir1,
|
||||
};
|
||||
|
||||
for (uint32_t step = 0, spad_idx = 0; step < ir1 - ir0 && spad_idx < 2; ++step, ++spad_idx) {
|
||||
const uint32_t i = ir0 + step;
|
||||
struct get_rows_tiled_task task = get_rows_tiled_calc_task(octx, grctx, i, n_k_tiles, tile_size);
|
||||
|
||||
// Dummy writeback to prime the queue with dst descriptor
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(task.dst_data, dst_spad_base + spad_idx * vtcm_layout->dst_spad_half_size),
|
||||
dst_bytes, vtcm_layout->dst_spad_half_size, dst_bytes, 0);
|
||||
|
||||
// Prefetch row tiles
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(src_spad_base + spad_idx * vtcm_layout->src0_spad_half_size, task.tile_src_base),
|
||||
tile_stride, tile_size, tile_size, n_k_tiles);
|
||||
}
|
||||
|
||||
for (uint32_t step = 0; step < ir1 - ir0; ++step) {
|
||||
const uint32_t i = ir0 + step;
|
||||
float * dst_spad = (float *) dma_queue_pop(dma_q).src;
|
||||
uint8_t * src_spad = (uint8_t *) dma_queue_pop(dma_q).dst;
|
||||
|
||||
struct get_rows_tiled_task task = get_rows_tiled_calc_task(octx, grctx, i, n_k_tiles, tile_size);
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i);
|
||||
compute_tiled(dst_spad, src_spad, n_k_tiles, tile_stride, task.row);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) i);
|
||||
|
||||
// Real writeback of dst_spad
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(task.dst_data, dst_spad),
|
||||
dst_bytes, vtcm_layout->dst_spad_half_size, dst_bytes, 1);
|
||||
|
||||
const uint32_t next_step = step + 2;
|
||||
if (next_step < ir1 - ir0) {
|
||||
const uint32_t ni = ir0 + next_step;
|
||||
struct get_rows_tiled_task next_task = get_rows_tiled_calc_task(octx, grctx, ni, n_k_tiles, tile_size);
|
||||
dma_queue_push(dma_q,
|
||||
dma_make_data(src_spad, next_task.tile_src_base),
|
||||
tile_stride, tile_size, tile_size, n_k_tiles);
|
||||
}
|
||||
}
|
||||
|
||||
dma_queue_flush(dma_q);
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void get_rows_dma_src1(
|
||||
struct htp_ops_context * octx,
|
||||
uint8_t * vtcm_src1,
|
||||
uint32_t elem_size
|
||||
) {
|
||||
const uint32_t ne10 = octx->src[1]->ne[0];
|
||||
const uint32_t ne11 = octx->src[1]->ne[1];
|
||||
const uint32_t ne12 = octx->src[1]->ne[2];
|
||||
const uint32_t ne13 = octx->src[1]->ne[3];
|
||||
const uint32_t nb11 = octx->src[1]->nb[1];
|
||||
const uint32_t nb12 = octx->src[1]->nb[2];
|
||||
const uint32_t nb13 = octx->src[1]->nb[3];
|
||||
|
||||
dma_queue * dma_q = octx->ctx->dma[0];
|
||||
uint8_t * cur_dst = vtcm_src1;
|
||||
for (uint32_t i13 = 0; i13 < ne13; ++i13) {
|
||||
for (uint32_t i12 = 0; i12 < ne12; ++i12) {
|
||||
const dma_addr_t src_plane = octx->src[1]->data + i12 * nb12 + i13 * nb13;
|
||||
while (!dma_queue_push(dma_q, dma_make_data(cur_dst, src_plane),
|
||||
ne10 * elem_size, nb11, ne10 * elem_size, ne11)) {
|
||||
dma_queue_pop(dma_q);
|
||||
}
|
||||
cur_dst += ne10 * ne11 * elem_size;
|
||||
}
|
||||
}
|
||||
dma_queue_flush(dma_q);
|
||||
dma_pipe_io_run(th->dma_q, th->trace, &s, in_buf, out_buf,
|
||||
get_rows_tiled_next_task, get_rows_tiled_push_in, get_rows_tiled_compute, get_rows_tiled_push_out);
|
||||
}
|
||||
|
||||
int op_get_rows(struct htp_ops_context * octx) {
|
||||
@@ -377,7 +463,8 @@ int op_get_rows(struct htp_ops_context * octx) {
|
||||
|
||||
const uint32_t ne00 = octx->src[0]->ne[0];
|
||||
const uint32_t src0_row_size = htp_tensor_get_row_size(octx->src[0]->type, ne00);
|
||||
if (src0_row_size == 0) {
|
||||
const uint32_t dst_row_size = htp_tensor_get_row_size(octx->dst->type, ne00);
|
||||
if (src0_row_size == 0 || dst_row_size == 0) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
@@ -385,27 +472,22 @@ int op_get_rows(struct htp_ops_context * octx) {
|
||||
if (octx->src[0]->type != octx->dst->type) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
} else {
|
||||
if (octx->dst->type != HTP_TYPE_F32) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
} else if ((octx->dst->type != HTP_TYPE_F32 && octx->dst->type != HTP_TYPE_F16) ||
|
||||
(octx->src[0]->type == HTP_TYPE_F32 && octx->dst->type == HTP_TYPE_F32)) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
if (octx->src[1]->type != HTP_TYPE_I32 && octx->src[1]->type != HTP_TYPE_I64) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
const uint32_t total_tasks = kparams->total_tasks;
|
||||
const uint32_t dst_row_size = (kparams->kernel_type == HTP_GET_ROWS_KERNEL_SAMETYPE) ? src0_row_size : ne00 * sizeof(float);
|
||||
|
||||
uint32_t task_start = 0;
|
||||
uint32_t tasks = total_tasks;
|
||||
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
uint32_t tasks_per_chunk = 1;
|
||||
htp_tensor_mdev_rows_per_chunk(dst, (uint32_t) dst->nb[0], dst_row_size, &tasks_per_chunk);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_tasks, tasks_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(total_tasks, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
task_start = range.start;
|
||||
tasks = range.count;
|
||||
}
|
||||
@@ -435,7 +517,7 @@ int op_get_rows(struct htp_ops_context * octx) {
|
||||
grctx.dst_row_size = dst_row_size;
|
||||
grctx.idx_type = octx->src[1]->type;
|
||||
|
||||
htp_get_rows_vtcm_layout_build(&grctx.vtcm_layout, kparams->kernel_type, octx->src[0]->type, ne00, total_tasks, grctx.idx_type, n_hvx);
|
||||
htp_get_rows_vtcm_layout_build(&grctx.vtcm_layout, kparams->kernel_type, octx->src[0]->type, octx->dst->type, ne00, n_hvx);
|
||||
|
||||
if (grctx.vtcm_layout.total_bytes > octx->ctx->vtcm_size) {
|
||||
FARF(ERROR, "get-rows: VTCM reservation %zu is too small, needed %zu\n",
|
||||
@@ -449,33 +531,62 @@ int op_get_rows(struct htp_ops_context * octx) {
|
||||
q_func = get_rows_thread_sametype;
|
||||
break;
|
||||
case HTP_GET_ROWS_KERNEL_TILED:
|
||||
switch (octx->src[0]->type) {
|
||||
case HTP_TYPE_F16: grctx.compute_tiled = hvx_dequantize_row_tiled_f16_f32; break;
|
||||
case HTP_TYPE_Q1_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q1_0_f32; break;
|
||||
case HTP_TYPE_Q2_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q2_0_f32; break;
|
||||
case HTP_TYPE_Q8_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q8_0_f32; break;
|
||||
case HTP_TYPE_Q4_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q4_0_f32; break;
|
||||
case HTP_TYPE_Q4_K: grctx.compute_tiled = hvx_dequantize_row_tiled_q4_k_f32; break;
|
||||
case HTP_TYPE_Q5_K: grctx.compute_tiled = hvx_dequantize_row_tiled_q5_k_f32; break;
|
||||
case HTP_TYPE_Q6_K: grctx.compute_tiled = hvx_dequantize_row_tiled_q6_k_f32; break;
|
||||
default: return HTP_STATUS_NO_SUPPORT;
|
||||
if (octx->dst->type == HTP_TYPE_F16) {
|
||||
switch (octx->src[0]->type) {
|
||||
case HTP_TYPE_F16: grctx.compute_tiled = hvx_dequantize_row_tiled_f16_f16; break;
|
||||
case HTP_TYPE_Q1_0: grctx.compute_tiled = get_rows_tiled_q1_0_f16; break;
|
||||
case HTP_TYPE_Q2_0: grctx.compute_tiled = get_rows_tiled_q2_0_f16; break;
|
||||
case HTP_TYPE_Q8_0: grctx.compute_tiled = get_rows_tiled_q8_0_f16; break;
|
||||
case HTP_TYPE_Q4_0: grctx.compute_tiled = get_rows_tiled_q4_0_f16; break;
|
||||
case HTP_TYPE_Q4_K: grctx.compute_tiled = get_rows_tiled_q4_k_f16; break;
|
||||
case HTP_TYPE_Q5_K: grctx.compute_tiled = get_rows_tiled_q5_k_f16; break;
|
||||
case HTP_TYPE_Q6_K: grctx.compute_tiled = get_rows_tiled_q6_k_f16; break;
|
||||
default: return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
} else {
|
||||
switch (octx->src[0]->type) {
|
||||
case HTP_TYPE_F16: grctx.compute_tiled = hvx_dequantize_row_tiled_f16_f32; break;
|
||||
case HTP_TYPE_Q1_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q1_0_f32; break;
|
||||
case HTP_TYPE_Q2_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q2_0_f32; break;
|
||||
case HTP_TYPE_Q8_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q8_0_f32; break;
|
||||
case HTP_TYPE_Q4_0: grctx.compute_tiled = hvx_dequantize_row_tiled_q4_0_f32; break;
|
||||
case HTP_TYPE_Q4_K: grctx.compute_tiled = hvx_dequantize_row_tiled_q4_k_f32; break;
|
||||
case HTP_TYPE_Q5_K: grctx.compute_tiled = hvx_dequantize_row_tiled_q5_k_f32; break;
|
||||
case HTP_TYPE_Q6_K: grctx.compute_tiled = hvx_dequantize_row_tiled_q6_k_f32; break;
|
||||
default: return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
}
|
||||
grctx.tile_size = htp_tensor_get_tile_size(octx->src[0]->type);
|
||||
grctx.tile_stride = htp_tensor_get_tile_padded_size(octx->src[0]->type);
|
||||
q_func = get_rows_thread_tiled;
|
||||
break;
|
||||
case HTP_GET_ROWS_KERNEL_FLAT:
|
||||
switch (octx->src[0]->type) {
|
||||
case HTP_TYPE_F16: grctx.compute_flat = hvx_dequantize_row_flat_f16_f32; break;
|
||||
case HTP_TYPE_BF16: grctx.compute_flat = hvx_dequantize_row_flat_bf16_f32; break;
|
||||
case HTP_TYPE_Q1_0: grctx.compute_flat = hvx_dequantize_row_flat_q1_0_f32; break;
|
||||
case HTP_TYPE_Q2_0: grctx.compute_flat = hvx_dequantize_row_flat_q2_0_f32; break;
|
||||
case HTP_TYPE_Q8_0: grctx.compute_flat = hvx_dequantize_row_flat_q8_0_f32; break;
|
||||
case HTP_TYPE_Q4_0: grctx.compute_flat = hvx_dequantize_row_flat_q4_0_f32; break;
|
||||
case HTP_TYPE_Q4_K: grctx.compute_flat = hvx_dequantize_row_flat_q4_k_f32; break;
|
||||
case HTP_TYPE_Q5_K: grctx.compute_flat = hvx_dequantize_row_flat_q5_k_f32; break;
|
||||
case HTP_TYPE_Q6_K: grctx.compute_flat = hvx_dequantize_row_flat_q6_k_f32; break;
|
||||
default: return HTP_STATUS_NO_SUPPORT;
|
||||
if (octx->dst->type == HTP_TYPE_F16) {
|
||||
switch (octx->src[0]->type) {
|
||||
case HTP_TYPE_F32: grctx.compute_flat = get_rows_flat_f32_to_f16; break;
|
||||
case HTP_TYPE_BF16: grctx.compute_flat = get_rows_flat_bf16_f16; break;
|
||||
case HTP_TYPE_Q1_0: grctx.compute_flat = get_rows_flat_q1_0_f16; break;
|
||||
case HTP_TYPE_Q2_0: grctx.compute_flat = get_rows_flat_q2_0_f16; break;
|
||||
case HTP_TYPE_Q8_0: grctx.compute_flat = get_rows_flat_q8_0_f16; break;
|
||||
case HTP_TYPE_Q4_0: grctx.compute_flat = get_rows_flat_q4_0_f16; break;
|
||||
case HTP_TYPE_Q4_K: grctx.compute_flat = get_rows_flat_q4_k_f16; break;
|
||||
case HTP_TYPE_Q5_K: grctx.compute_flat = get_rows_flat_q5_k_f16; break;
|
||||
case HTP_TYPE_Q6_K: grctx.compute_flat = get_rows_flat_q6_k_f16; break;
|
||||
default: return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
} else {
|
||||
switch (octx->src[0]->type) {
|
||||
case HTP_TYPE_F16: grctx.compute_flat = hvx_dequantize_row_flat_f16_f32; break;
|
||||
case HTP_TYPE_BF16: grctx.compute_flat = hvx_dequantize_row_flat_bf16_f32; break;
|
||||
case HTP_TYPE_Q1_0: grctx.compute_flat = hvx_dequantize_row_flat_q1_0_f32; break;
|
||||
case HTP_TYPE_Q2_0: grctx.compute_flat = hvx_dequantize_row_flat_q2_0_f32; break;
|
||||
case HTP_TYPE_Q8_0: grctx.compute_flat = hvx_dequantize_row_flat_q8_0_f32; break;
|
||||
case HTP_TYPE_Q4_0: grctx.compute_flat = hvx_dequantize_row_flat_q4_0_f32; break;
|
||||
case HTP_TYPE_Q4_K: grctx.compute_flat = hvx_dequantize_row_flat_q4_k_f32; break;
|
||||
case HTP_TYPE_Q5_K: grctx.compute_flat = hvx_dequantize_row_flat_q5_k_f32; break;
|
||||
case HTP_TYPE_Q6_K: grctx.compute_flat = hvx_dequantize_row_flat_q6_k_f32; break;
|
||||
default: return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
}
|
||||
q_func = get_rows_thread_flat;
|
||||
break;
|
||||
@@ -483,19 +594,18 @@ int op_get_rows(struct htp_ops_context * octx) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const uint32_t elem_size = (grctx.idx_type == HTP_TYPE_I64) ? sizeof(int64_t) : sizeof(int32_t);
|
||||
const size_t src1_bytes = (size_t) total_tasks * elem_size;
|
||||
|
||||
if (!htp_tensor_is_extended(octx->src[1]) && src1_bytes <= 128 &&
|
||||
htp_tensor_is_contiguous(octx->src[1], elem_size)) {
|
||||
grctx.src1_data = (const void *) (uintptr_t) octx->src[1]->data;
|
||||
grctx.read_index = get_rows_read_index_ddr;
|
||||
} else {
|
||||
uint8_t * vtcm_src1 = VTCM_LAYOUT_PTR(uint8_t, grctx.vtcm_base, grctx.vtcm_layout.off_src1);
|
||||
get_rows_dma_src1(octx, vtcm_src1, elem_size);
|
||||
grctx.src1_data = vtcm_src1;
|
||||
grctx.read_index = get_rows_read_index_vtcm;
|
||||
const size_t src1_bytes = hex_round_up(htp_tensor_nbytes(octx->src[1]), 128);
|
||||
void * src1_buf = (src1_bytes <= octx->ctx->ddr_spad_size) ? octx->ctx->ddr_spad_base : memalign(128, src1_bytes);
|
||||
if (!src1_buf) {
|
||||
return HTP_STATUS_INTERNAL_ERR;
|
||||
}
|
||||
if (htp_tensor_read(&octx->ctx->main_th, src1_buf, src1_bytes, octx->src[1]) == 0) {
|
||||
if (src1_buf != octx->ctx->ddr_spad_base) {
|
||||
free(src1_buf);
|
||||
}
|
||||
return HTP_STATUS_INTERNAL_ERR;
|
||||
}
|
||||
grctx.src1_data = src1_buf;
|
||||
|
||||
FARF(HIGH, "get-rows: (%ux%ux%ux%u) x (%ux%ux%ux%u) -> (%ux%ux%ux%u) : src0-vtcm-size %zu dst-vtcm-size %zu kernel-type %d n-hvx %d\n",
|
||||
octx->src[0]->ne[0], octx->src[0]->ne[1], octx->src[0]->ne[2], octx->src[0]->ne[3],
|
||||
@@ -506,5 +616,8 @@ int op_get_rows(struct htp_ops_context * octx) {
|
||||
kparams->kernel_type, n_hvx);
|
||||
|
||||
work_queue_run(octx->ctx->hvx_queue, q_func, &grctx, n_hvx);
|
||||
if (src1_buf != octx->ctx->ddr_spad_base) {
|
||||
free(src1_buf);
|
||||
}
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
@@ -29,8 +29,6 @@ struct htp_get_rows_kernel_params {
|
||||
|
||||
struct htp_get_rows_vtcm_layout {
|
||||
size_t total_bytes;
|
||||
size_t off_src1;
|
||||
size_t src1_bytes;
|
||||
size_t off_src0;
|
||||
size_t off_dst;
|
||||
|
||||
@@ -44,39 +42,35 @@ struct htp_get_rows_vtcm_layout {
|
||||
static inline void htp_get_rows_vtcm_layout_build(
|
||||
struct htp_get_rows_vtcm_layout * L,
|
||||
int kernel_type,
|
||||
int type,
|
||||
int src0_type,
|
||||
int dst_type,
|
||||
uint32_t ne00,
|
||||
uint32_t total_tasks,
|
||||
int idx_type,
|
||||
uint32_t n_hvx) {
|
||||
|
||||
memset(L, 0, sizeof(*L));
|
||||
|
||||
const size_t elem_size = (idx_type == HTP_TYPE_I64) ? sizeof(int64_t) : sizeof(int32_t);
|
||||
const size_t src1_raw_bytes = total_tasks * elem_size;
|
||||
const size_t src1_bytes = ((src1_raw_bytes + 127) & ~127) + 128;
|
||||
|
||||
L->src1_bytes = src1_bytes;
|
||||
const size_t src1_alloc_bytes = (src1_bytes + 255) & ~255;
|
||||
|
||||
if (kernel_type != HTP_GET_ROWS_KERNEL_SAMETYPE) {
|
||||
size_t src0_row_size = 0;
|
||||
if (kernel_type == HTP_GET_ROWS_KERNEL_TILED) {
|
||||
const size_t tile_stride = htp_tensor_get_tile_padded_size(type);
|
||||
const uint32_t n_k_tiles = ne00 / htp_tensor_get_tile_k(type);
|
||||
const size_t tile_stride = htp_tensor_get_tile_padded_size(src0_type);
|
||||
const uint32_t n_k_tiles = ne00 / htp_tensor_get_tile_k(src0_type);
|
||||
src0_row_size = n_k_tiles > 0 ? (n_k_tiles * tile_stride) : tile_stride;
|
||||
} else {
|
||||
src0_row_size = htp_tensor_get_row_size(type, ne00);
|
||||
src0_row_size = htp_tensor_get_row_size(src0_type, ne00);
|
||||
}
|
||||
|
||||
const bool direct_f16_spad =
|
||||
(kernel_type == HTP_GET_ROWS_KERNEL_TILED && src0_type == HTP_TYPE_F16 && dst_type == HTP_TYPE_F16) ||
|
||||
(kernel_type == HTP_GET_ROWS_KERNEL_FLAT && src0_type == HTP_TYPE_F32 && dst_type == HTP_TYPE_F16);
|
||||
const size_t dst_spad_row_size = htp_tensor_get_row_size(direct_f16_spad ? dst_type : HTP_TYPE_F32, ne00);
|
||||
|
||||
L->src0_spad_half_size = (src0_row_size + 255) & ~255;
|
||||
L->dst_spad_half_size = (ne00 * sizeof(float) + 255) & ~255;
|
||||
L->dst_spad_half_size = (dst_spad_row_size + 255) & ~255;
|
||||
L->src0_bytes_per_hvx = 2 * L->src0_spad_half_size;
|
||||
L->dst_bytes_per_hvx = 2 * L->dst_spad_half_size;
|
||||
}
|
||||
|
||||
size_t off = 0;
|
||||
VTCM_LAYOUT_ALLOC(off, off_src1, src1_alloc_bytes);
|
||||
VTCM_LAYOUT_ALLOC(off, off_src0, L->src0_bytes_per_hvx * n_hvx);
|
||||
VTCM_LAYOUT_ALLOC(off, off_dst, L->dst_bytes_per_hvx * n_hvx);
|
||||
L->total_bytes = off;
|
||||
|
||||
+166
-111
@@ -1,13 +1,10 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dma-queue.h"
|
||||
#include "dma-pipeline.h"
|
||||
#include "hvx-utils.h"
|
||||
|
||||
#define GGML_COMMON_DECL_C
|
||||
@@ -475,6 +472,7 @@ static __attribute__((noinline)) void hvx_sigglu_f32_vec(HVX_Vector * restrict v
|
||||
const HVX_Vector v_max_exp = hvx_vec_splat_f32(87.0f);
|
||||
const HVX_Vector v_min_exp = hvx_vec_splat_f32(-87.0f);
|
||||
|
||||
_Pragma("unroll(2)")
|
||||
for (uint32_t i = 0; i < nvec; i++) {
|
||||
HVX_Vector sig = hvx_vec_fast_sigmoid_f32_guard(vsrc0[i], v_one, v_max_exp, v_min_exp);
|
||||
vdst[i] = hvx_vec_mul_f32_f32(sig, vsrc1[i]);
|
||||
@@ -523,6 +521,11 @@ static void sigglu_f32(const float * restrict src0,
|
||||
const struct htp_glu_context * gctx) {
|
||||
htp_glu_op_preamble;
|
||||
|
||||
if ((size_t) nc * sizeof(float) == src0_row_size_aligned) {
|
||||
hvx_sigglu_f32_aa((uint8_t *) dst, (const uint8_t *) src0, (const uint8_t *) src1, num_rows * (uint32_t) nc);
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint32_t ib = 0; ib < num_rows; ib++) {
|
||||
const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
|
||||
const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
|
||||
@@ -532,7 +535,58 @@ static void sigglu_f32(const float * restrict src0,
|
||||
}
|
||||
}
|
||||
|
||||
// swiglu(x) = x1 * sigmoid(x0)
|
||||
static __attribute__((noinline)) void hvx_swiglu_f16_vec(HVX_Vector * restrict vdst,
|
||||
const HVX_Vector * restrict vsrc0,
|
||||
const HVX_Vector * restrict vsrc1,
|
||||
uint32_t nvec) {
|
||||
_Pragma("clang loop pipeline(disable) unroll(disable)")
|
||||
for (uint32_t i = 0; i + 1 < nvec; i += 2) {
|
||||
HVX_Vector x0 = vsrc0[i + 0];
|
||||
HVX_Vector x1 = vsrc0[i + 1];
|
||||
HVX_VectorPair sigp = hvx_vec_fast_sigmoid_f16_pair(x0, x1);
|
||||
vdst[i + 0] = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(x0, Q6_V_lo_W(sigp)), vsrc1[i + 0]);
|
||||
vdst[i + 1] = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(x1, Q6_V_hi_W(sigp)), vsrc1[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_swiglu_f16_tail(HVX_Vector * restrict vdst,
|
||||
const HVX_Vector * restrict vsrc0,
|
||||
const HVX_Vector * restrict vsrc1,
|
||||
uint32_t nloe) {
|
||||
HVX_Vector x = vsrc0[0];
|
||||
HVX_Vector sig = hvx_vec_fast_sigmoid_f16(x);
|
||||
HVX_Vector res = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(x, sig), vsrc1[0]);
|
||||
hvx_vec_store_a((void *) vdst, nloe * sizeof(_Float16), res);
|
||||
}
|
||||
|
||||
static inline void hvx_swiglu_f16_aa(uint8_t * restrict dst, const uint8_t * restrict src0, const uint8_t * restrict src1, uint32_t n) {
|
||||
assert((unsigned long) dst % 128 == 0);
|
||||
assert((unsigned long) src0 % 128 == 0);
|
||||
assert((unsigned long) src1 % 128 == 0);
|
||||
|
||||
HVX_Vector * restrict vdst = (HVX_Vector *) dst;
|
||||
const HVX_Vector * restrict vsrc0 = (const HVX_Vector *) src0;
|
||||
const HVX_Vector * restrict vsrc1 = (const HVX_Vector *) src1;
|
||||
|
||||
const uint32_t epv = 128 / sizeof(_Float16);
|
||||
const uint32_t nvec = n / epv;
|
||||
const uint32_t nloe = n % epv;
|
||||
uint32_t i = 0;
|
||||
|
||||
if (nvec >= 2) {
|
||||
const uint32_t nvec2 = nvec & ~1u;
|
||||
hvx_swiglu_f16_vec(vdst, vsrc0, vsrc1, nvec2);
|
||||
i = nvec2;
|
||||
}
|
||||
if (i < nvec) {
|
||||
hvx_swiglu_f16_tail(vdst + i, vsrc0 + i, vsrc1 + i, epv);
|
||||
}
|
||||
if (nloe) {
|
||||
hvx_swiglu_f16_tail(vdst + nvec, vsrc0 + nvec, vsrc1 + nvec, nloe);
|
||||
}
|
||||
}
|
||||
|
||||
// swiglu(x, g) = x * sigmoid(x) * g
|
||||
static void swiglu_f16(const _Float16 * restrict src0,
|
||||
const _Float16 * restrict src1,
|
||||
_Float16 * restrict dst,
|
||||
@@ -540,13 +594,17 @@ static void swiglu_f16(const _Float16 * restrict src0,
|
||||
const struct htp_glu_context * gctx) {
|
||||
htp_glu_op_preamble;
|
||||
|
||||
if ((size_t) nc * sizeof(_Float16) == src0_row_size_aligned) {
|
||||
hvx_swiglu_f16_aa((uint8_t *) dst, (const uint8_t *) src0, (const uint8_t *) src1, num_rows * (uint32_t) nc);
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint32_t ib = 0; ib < num_rows; ib++) {
|
||||
const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
|
||||
const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
|
||||
uint8_t * restrict dst_ptr = (uint8_t *) dst + (ib * dst_row_size_aligned);
|
||||
|
||||
hvx_sigmoid_f16_aa(dst_ptr, src0_ptr, nc);
|
||||
hvx_mul_mul_f16_aa(dst_ptr, src0_ptr, dst_ptr, src1_ptr, nc);
|
||||
hvx_swiglu_f16_aa(dst_ptr, src0_ptr, src1_ptr, nc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -668,9 +726,13 @@ static __attribute__((noinline)) void hvx_sigglu_f16_vec(HVX_Vector * restrict v
|
||||
const HVX_Vector * restrict vsrc0,
|
||||
const HVX_Vector * restrict vsrc1,
|
||||
uint32_t nvec) {
|
||||
for (uint32_t i = 0; i < nvec; i++) {
|
||||
HVX_Vector sig = hvx_vec_fast_sigmoid_f16(vsrc0[i]);
|
||||
vdst[i] = hvx_vec_mul_f16_f16(sig, vsrc1[i]);
|
||||
for (uint32_t i = 0; i + 2 < nvec; i += 3) {
|
||||
HVX_Vector sig0 = hvx_vec_fast_sigmoid_f16(vsrc0[i + 0]);
|
||||
HVX_Vector sig1 = hvx_vec_fast_sigmoid_f16(vsrc0[i + 1]);
|
||||
HVX_Vector sig2 = hvx_vec_fast_sigmoid_f16(vsrc0[i + 2]);
|
||||
vdst[i + 0] = hvx_vec_mul_f16_f16(sig0, vsrc1[i + 0]);
|
||||
vdst[i + 1] = hvx_vec_mul_f16_f16(sig1, vsrc1[i + 1]);
|
||||
vdst[i + 2] = hvx_vec_mul_f16_f16(sig2, vsrc1[i + 2]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -695,9 +757,15 @@ static inline void hvx_sigglu_f16_aa(uint8_t * restrict dst, const uint8_t * res
|
||||
const uint32_t epv = 128 / sizeof(_Float16);
|
||||
const uint32_t nvec = n / epv;
|
||||
const uint32_t nloe = n % epv;
|
||||
uint32_t i = 0;
|
||||
|
||||
if (nvec) {
|
||||
hvx_sigglu_f16_vec(vdst, vsrc0, vsrc1, nvec);
|
||||
if (nvec >= 3) {
|
||||
const uint32_t nvec3 = nvec - (nvec % 3);
|
||||
hvx_sigglu_f16_vec(vdst, vsrc0, vsrc1, nvec3);
|
||||
i = nvec3;
|
||||
}
|
||||
for (; i < nvec; i++) {
|
||||
hvx_sigglu_f16_tail(vdst + i, vsrc0 + i, vsrc1 + i, epv);
|
||||
}
|
||||
if (nloe) {
|
||||
hvx_sigglu_f16_tail(vdst + nvec, vsrc0 + nvec, vsrc1 + nvec, nloe);
|
||||
@@ -712,6 +780,11 @@ static void sigglu_f16(const _Float16 * restrict src0,
|
||||
const struct htp_glu_context * gctx) {
|
||||
htp_glu_op_preamble;
|
||||
|
||||
if ((size_t) nc * sizeof(_Float16) == src0_row_size_aligned) {
|
||||
hvx_sigglu_f16_aa((uint8_t *) dst, (const uint8_t *) src0, (const uint8_t *) src1, num_rows * (uint32_t) nc);
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint32_t ib = 0; ib < num_rows; ib++) {
|
||||
const uint8_t * restrict src0_ptr = (const uint8_t *) src0 + (ib * src0_row_size_aligned);
|
||||
const uint8_t * restrict src1_ptr = (const uint8_t *) src1 + (ib * src1_row_size_aligned);
|
||||
@@ -721,99 +794,85 @@ static void sigglu_f16(const _Float16 * restrict src0,
|
||||
}
|
||||
}
|
||||
|
||||
#define DEFINE_GLU_PER_THREAD(TYPE, SUFFIX) \
|
||||
static void glu_##SUFFIX##_per_hvx(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) { \
|
||||
struct htp_glu_context * gctx = (struct htp_glu_context *) data; \
|
||||
htp_glu_preamble; \
|
||||
\
|
||||
struct htp_thread_trace * tr = th->trace; \
|
||||
\
|
||||
size_t src0_row_size = gctx->src0_row_size; \
|
||||
size_t src1_row_size = gctx->src1_row_size; \
|
||||
size_t dst_row_size = gctx->dst_row_size; \
|
||||
\
|
||||
size_t src0_row_stride = gctx->src0_row_stride; \
|
||||
size_t src1_row_stride = gctx->src1_row_stride; \
|
||||
\
|
||||
const uint32_t src0_nrows = gctx->src0_nrows; \
|
||||
const uint32_t src0_nrows_per_hvx = gctx->src0_nrows_per_hvx; \
|
||||
\
|
||||
const uint32_t src0_start_row = gctx->row_start + src0_nrows_per_hvx * ith; \
|
||||
const uint32_t src0_end_row = MIN(src0_start_row + src0_nrows_per_hvx, gctx->row_start + src0_nrows); \
|
||||
\
|
||||
/* no work for this thread */ \
|
||||
if (src0_start_row >= src0_end_row) { \
|
||||
return; \
|
||||
} \
|
||||
\
|
||||
const dma_addr_t data_src0 = gctx->data_src0; \
|
||||
const dma_addr_t data_src1 = gctx->data_src1; \
|
||||
const dma_addr_t data_dst = gctx->data_dst; \
|
||||
\
|
||||
const size_t src0_row_size_aligned = gctx->src0_row_size_aligned; \
|
||||
const size_t src1_row_size_aligned = gctx->src1_row_size_aligned; \
|
||||
const size_t dst_row_size_aligned = gctx->dst_row_size_aligned; \
|
||||
\
|
||||
uint8_t * restrict src0_spad_data = gctx->vtcm_src0 + (ith * gctx->vtcm_src0_size_per_hvx); \
|
||||
uint8_t * restrict src1_spad_data = gctx->vtcm_src1 + (ith * gctx->vtcm_src1_size_per_hvx); \
|
||||
uint8_t * restrict dst_spad_data = gctx->vtcm_dst + (ith * gctx->vtcm_dst_size_per_hvx); \
|
||||
\
|
||||
size_t src0_spad_half_size = gctx->src0_spad_half_size; \
|
||||
size_t src1_spad_half_size = gctx->src1_spad_half_size; \
|
||||
size_t dst_spad_half_size = gctx->dst_spad_half_size; \
|
||||
\
|
||||
const int BLOCK = gctx->block; \
|
||||
\
|
||||
dma_queue * dma_q = th->dma_q; \
|
||||
glu_compute_##SUFFIX##_fn_t compute = (glu_compute_##SUFFIX##_fn_t) gctx->compute; \
|
||||
\
|
||||
for (uint32_t ir = src0_start_row, spad_idx = 0; ir < src0_end_row && spad_idx < 2; ir += BLOCK, spad_idx++) { \
|
||||
const uint32_t block_size = MIN(BLOCK, src0_end_row - ir); \
|
||||
\
|
||||
/* Dummy DMA transation for sequencing (interleaving dst,src,dst,...) */ \
|
||||
dma_queue_push(dma_q, \
|
||||
dma_make_data(data_dst, dst_spad_data + (spad_idx * dst_spad_half_size)), \
|
||||
dst_row_size, dst_row_size_aligned, dst_row_size, 0); \
|
||||
\
|
||||
dma_queue_push(dma_q, \
|
||||
dma_make_data(src0_spad_data + (spad_idx * src0_spad_half_size), data_src0 + (ir * src0_row_stride)), \
|
||||
src0_row_size_aligned, src0_row_stride, src0_row_size, block_size); \
|
||||
\
|
||||
dma_queue_push(dma_q, \
|
||||
dma_make_data(src1_spad_data + (spad_idx * src1_spad_half_size), data_src1 + (ir * src1_row_stride)), \
|
||||
src1_row_size_aligned, src1_row_stride, src1_row_size, block_size); \
|
||||
} \
|
||||
\
|
||||
for (uint32_t ir = src0_start_row; ir < src0_end_row; ir += BLOCK) { \
|
||||
const uint32_t block_size = MIN(BLOCK, src0_end_row - ir); \
|
||||
\
|
||||
TYPE * dst_spad = (TYPE *) dma_queue_pop(dma_q).src; \
|
||||
TYPE * src0_spad = (TYPE *) dma_queue_pop(dma_q).dst; \
|
||||
TYPE * src1_spad = (TYPE *) dma_queue_pop(dma_q).dst; \
|
||||
\
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, ir); \
|
||||
compute(src0_spad, src1_spad, dst_spad, block_size, gctx); \
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, ir); \
|
||||
\
|
||||
dma_queue_push(dma_q, dma_make_data(data_dst + (ir * dst_row_size), dst_spad), \
|
||||
dst_row_size, dst_row_size_aligned, dst_row_size, block_size); \
|
||||
\
|
||||
/* prefetch N+2 loop iteration if any */ \
|
||||
const uint32_t pref_block = (ir + BLOCK * 2); \
|
||||
if (pref_block < src0_end_row) { \
|
||||
const uint32_t pref_block_size = MIN(BLOCK, src0_end_row - pref_block); \
|
||||
dma_queue_push(dma_q, dma_make_data(src0_spad, data_src0 + (pref_block * src0_row_stride)), \
|
||||
src0_row_size_aligned, src0_row_stride, src0_row_size, pref_block_size); \
|
||||
dma_queue_push(dma_q, dma_make_data(src1_spad, data_src1 + (pref_block * src1_row_stride)), \
|
||||
src1_row_size_aligned, src1_row_stride, src1_row_size, pref_block_size); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
dma_queue_flush(dma_q); \
|
||||
struct glu_pipe_state {
|
||||
const struct htp_glu_context * gctx;
|
||||
uint32_t ir;
|
||||
uint32_t ir_end;
|
||||
};
|
||||
|
||||
static bool glu_next_task(void * ctx, struct dma_pipe_task * task) {
|
||||
struct glu_pipe_state * s = (struct glu_pipe_state *) ctx;
|
||||
if (s->ir >= s->ir_end) {
|
||||
return false;
|
||||
}
|
||||
const uint32_t block_size = MIN((uint32_t) s->gctx->block, s->ir_end - s->ir);
|
||||
task->row = s->ir;
|
||||
task->off = 0;
|
||||
task->len = block_size;
|
||||
task->aux = 0;
|
||||
s->ir += block_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void glu_push_in(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in0_spad, void * in1_spad) {
|
||||
const struct htp_glu_context * gctx = ((struct glu_pipe_state *) ctx)->gctx;
|
||||
dma_queue_push(dma_q, dma_make_data(in0_spad, gctx->data_src0 + (size_t) task->row * gctx->src0_row_stride),
|
||||
gctx->src0_row_size_aligned, gctx->src0_row_stride, gctx->src0_row_size, task->len);
|
||||
dma_queue_push(dma_q, dma_make_data(in1_spad, gctx->data_src1 + (size_t) task->row * gctx->src1_row_stride),
|
||||
gctx->src1_row_size_aligned, gctx->src1_row_stride, gctx->src1_row_size, task->len);
|
||||
}
|
||||
|
||||
static void glu_push_out(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad) {
|
||||
const struct htp_glu_context * gctx = ((struct glu_pipe_state *) ctx)->gctx;
|
||||
dma_queue_push(dma_q, dma_make_data(gctx->data_dst + (size_t) task->row * gctx->dst_row_size, out_spad),
|
||||
gctx->dst_row_size, gctx->dst_row_size_aligned, gctx->dst_row_size, task->len);
|
||||
}
|
||||
|
||||
static void glu_compute_f32(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in0_spad, void * in1_spad) {
|
||||
const struct htp_glu_context * gctx = ((struct glu_pipe_state *) ctx)->gctx;
|
||||
glu_compute_f32_fn_t compute = (glu_compute_f32_fn_t) gctx->compute;
|
||||
compute((const float *) in0_spad, (const float *) in1_spad, (float *) out_spad, task->len, gctx);
|
||||
}
|
||||
|
||||
static void glu_compute_f16(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in0_spad, void * in1_spad) {
|
||||
const struct htp_glu_context * gctx = ((struct glu_pipe_state *) ctx)->gctx;
|
||||
glu_compute_f16_fn_t compute = (glu_compute_f16_fn_t) gctx->compute;
|
||||
compute((const _Float16 *) in0_spad, (const _Float16 *) in1_spad, (_Float16 *) out_spad, task->len, gctx);
|
||||
}
|
||||
|
||||
static void glu_thread_run(const htp_thread_t * th, unsigned int ith, const struct htp_glu_context * gctx, dma_pipe_compute_io2_fn compute) {
|
||||
const uint32_t src0_start_row = gctx->row_start + gctx->src0_nrows_per_hvx * ith;
|
||||
const uint32_t src0_end_row = MIN(src0_start_row + gctx->src0_nrows_per_hvx, gctx->row_start + gctx->src0_nrows);
|
||||
if (src0_start_row >= src0_end_row) {
|
||||
return;
|
||||
}
|
||||
|
||||
DEFINE_GLU_PER_THREAD(float, f32)
|
||||
DEFINE_GLU_PER_THREAD(_Float16, f16)
|
||||
uint8_t * src0_spad_data = gctx->vtcm_src0 + (ith * gctx->vtcm_src0_size_per_hvx);
|
||||
uint8_t * src1_spad_data = gctx->vtcm_src1 + (ith * gctx->vtcm_src1_size_per_hvx);
|
||||
uint8_t * dst_spad_data = gctx->vtcm_dst + (ith * gctx->vtcm_dst_size_per_hvx);
|
||||
void * in0_buf[2] = { src0_spad_data, src0_spad_data + gctx->src0_spad_half_size };
|
||||
void * in1_buf[2] = { src1_spad_data, src1_spad_data + gctx->src1_spad_half_size };
|
||||
void * out_buf[2] = { dst_spad_data, dst_spad_data + gctx->dst_spad_half_size };
|
||||
|
||||
struct glu_pipe_state s = {
|
||||
.gctx = gctx,
|
||||
.ir = src0_start_row,
|
||||
.ir_end = src0_end_row,
|
||||
};
|
||||
|
||||
dma_pipe_io2_run(th->dma_q, th->trace, &s, in0_buf, in1_buf, out_buf,
|
||||
glu_next_task, glu_push_in, compute, glu_push_out);
|
||||
}
|
||||
|
||||
static void glu_f32_thread(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
(void) nth;
|
||||
glu_thread_run(th, ith, (const struct htp_glu_context *) data, glu_compute_f32);
|
||||
}
|
||||
|
||||
static void glu_f16_thread(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
(void) nth;
|
||||
glu_thread_run(th, ith, (const struct htp_glu_context *) data, glu_compute_f16);
|
||||
}
|
||||
|
||||
static int execute_op_glu_f32(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
@@ -882,9 +941,7 @@ static int execute_op_glu_f32(struct htp_ops_context * octx) {
|
||||
uint32_t nrows = src0_nrows;
|
||||
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
uint32_t rows_per_chunk = 0;
|
||||
htp_tensor_mdev_rows_per_chunk(dst, sizeof(float), (uint32_t) dst_row_size, &rows_per_chunk);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(src0_nrows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(src0_nrows, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
row_start = range.start;
|
||||
nrows = range.count;
|
||||
}
|
||||
@@ -987,7 +1044,7 @@ static int execute_op_glu_f32(struct htp_ops_context * octx) {
|
||||
gctx.data_src1 = data_src1;
|
||||
gctx.data_dst = dst->data;
|
||||
|
||||
work_queue_run(octx->ctx->hvx_queue, (work_queue_func_t)glu_f32_per_hvx, &gctx, n_hvx);
|
||||
work_queue_run(octx->ctx->hvx_queue, glu_f32_thread, &gctx, n_hvx);
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
@@ -1048,9 +1105,7 @@ static int execute_op_glu_f16(struct htp_ops_context * octx) {
|
||||
uint32_t nrows = src0_nrows;
|
||||
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
uint32_t rows_per_chunk = 0;
|
||||
htp_tensor_mdev_rows_per_chunk(dst, sizeof(_Float16), (uint32_t) dst_row_size, &rows_per_chunk);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(src0_nrows, rows_per_chunk, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(src0_nrows, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
row_start = range.start;
|
||||
nrows = range.count;
|
||||
}
|
||||
@@ -1153,7 +1208,7 @@ static int execute_op_glu_f16(struct htp_ops_context * octx) {
|
||||
gctx.data_src1 = data_src1;
|
||||
gctx.data_dst = dst->data;
|
||||
|
||||
work_queue_run(octx->ctx->hvx_queue, (work_queue_func_t)glu_f16_per_hvx, &gctx, n_hvx);
|
||||
work_queue_run(octx->ctx->hvx_queue, glu_f16_thread, &gctx, n_hvx);
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -65,11 +65,14 @@ static inline void hex_l2fetch_block(const void * addr, size_t size) {
|
||||
|
||||
static inline void hex_l2flush(void * addr, size_t size) {
|
||||
if (size == 0) return;
|
||||
const uint32_t s = ((uint32_t) addr) & ~(HEX_L2_LINE_SIZE - 1);
|
||||
const uint32_t e = (((uint32_t) addr) + size + HEX_L2_LINE_SIZE - 1) & ~(HEX_L2_LINE_SIZE - 1);
|
||||
for (uint32_t i = s; i < e; i += HEX_L2_LINE_SIZE) {
|
||||
// Step by 64B (L1D line size on v73+ and L2 line size on v73) so no L1D lines are skipped.
|
||||
const uint32_t line = 64;
|
||||
const uint32_t s = ((uint32_t) addr) & ~(line - 1);
|
||||
const uint32_t e = (((uint32_t) addr) + size + line - 1) & ~(line - 1);
|
||||
for (uint32_t i = s; i < e; i += line) {
|
||||
Q6_dccleaninva_A((void *) i);
|
||||
}
|
||||
asm volatile(" syncht\n" : : : "memory");
|
||||
}
|
||||
|
||||
static inline void hex_pause() {
|
||||
|
||||
@@ -623,83 +623,4 @@ static inline void hmx_fa_q_prep_fp16_pad(__fp16 * vtcm_q_t
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hmx_fa_q_prep_fallback(
|
||||
__fp16 * vtcm_q_tiles, uintptr_t q_data,
|
||||
size_t q_nb1, size_t q_nb2, size_t q_nb3,
|
||||
uint32_t q_start, uint32_t kv_head, uint32_t ib3,
|
||||
size_t start, size_t end, size_t n_rows_g,
|
||||
size_t G, size_t dk_in, size_t dk_out, bool is_q_fp32,
|
||||
const struct fastdiv_values * div_G
|
||||
) {
|
||||
for (size_t r = start; r < end; r += 2) {
|
||||
const size_t q_idx0 = fastdiv(r + 0, div_G);
|
||||
const size_t h_idx0 = fastmodulo(r + 0, G, div_G);
|
||||
const size_t q_idx1 = fastdiv(r + 1, div_G);
|
||||
const size_t h_idx1 = fastmodulo(r + 1, G, div_G);
|
||||
|
||||
const uint8_t * q_ptr0 = (r + 0 < n_rows_g) ? ((const uint8_t *) q_data + (q_start + q_idx0) * q_nb1 +
|
||||
(kv_head * G + h_idx0) * q_nb2 + ib3 * q_nb3) :
|
||||
NULL;
|
||||
const uint8_t * q_ptr1 = (r + 1 < n_rows_g) ? ((const uint8_t *) q_data + (q_start + q_idx1) * q_nb1 +
|
||||
(kv_head * G + h_idx1) * q_nb2 + ib3 * q_nb3) :
|
||||
NULL;
|
||||
|
||||
size_t r0 = r / HMX_FP16_TILE_N_ROWS;
|
||||
size_t r1 = r % HMX_FP16_TILE_N_ROWS;
|
||||
__fp16 * out_base = vtcm_q_tiles + r0 * HMX_FP16_TILE_N_ROWS * dk_out;
|
||||
|
||||
if (is_q_fp32) {
|
||||
const HVX_UVector * pv_in0 = q_ptr0 ? (const HVX_UVector *) q_ptr0 : NULL;
|
||||
const HVX_UVector * pv_in1 = q_ptr1 ? (const HVX_UVector *) q_ptr1 : NULL;
|
||||
|
||||
for (uint32_t d = 0; d < dk_out / 32; ++d) {
|
||||
HVX_Vector * out_tile = (HVX_Vector *) (out_base + d * HMX_FP16_TILE_N_ELMS);
|
||||
const size_t base_lane = (size_t) d * 32;
|
||||
const size_t real_lanes = (base_lane < dk_in) ? hex_smin(32, dk_in - base_lane) : 0;
|
||||
|
||||
if (real_lanes == 0) {
|
||||
out_tile[r1 / 2] = Q6_V_vzero();
|
||||
continue;
|
||||
}
|
||||
HVX_Vector v0 = pv_in0 ? pv_in0[d] : Q6_V_vzero();
|
||||
HVX_Vector v1 = pv_in1 ? pv_in1[d] : Q6_V_vzero();
|
||||
if (real_lanes < 32) {
|
||||
const HVX_VectorPred keep = Q6_Q_vsetq_R((uint32_t) (real_lanes * sizeof(float)));
|
||||
v0 = Q6_V_vmux_QVV(keep, v0, Q6_V_vzero());
|
||||
v1 = Q6_V_vmux_QVV(keep, v1, Q6_V_vzero());
|
||||
}
|
||||
out_tile[r1 / 2] = hvx_vec_f32_to_f16_shuff(v0, v1);
|
||||
}
|
||||
} else {
|
||||
const HVX_UVector * pv_in0 = q_ptr0 ? (const HVX_UVector *) q_ptr0 : NULL;
|
||||
const HVX_UVector * pv_in1 = q_ptr1 ? (const HVX_UVector *) q_ptr1 : NULL;
|
||||
|
||||
for (uint32_t d = 0; d < dk_out / 64; ++d) {
|
||||
__fp16 * out_dtile = out_base + d * HMX_FP16_TILE_N_ELMS * 2;
|
||||
HVX_Vector * pv_out0 = ((HVX_Vector *) out_dtile) + r1 / 2;
|
||||
HVX_Vector * pv_out1 = pv_out0 + 16;
|
||||
|
||||
const size_t base_lane = (size_t) d * 64;
|
||||
const size_t real_lanes = (base_lane < dk_in) ? hex_smin(64, dk_in - base_lane) : 0;
|
||||
|
||||
if (real_lanes == 0) {
|
||||
*pv_out0 = Q6_V_vzero();
|
||||
*pv_out1 = Q6_V_vzero();
|
||||
continue;
|
||||
}
|
||||
HVX_Vector v0 = pv_in0 ? pv_in0[d] : Q6_V_vzero();
|
||||
HVX_Vector v1 = pv_in1 ? pv_in1[d] : Q6_V_vzero();
|
||||
if (real_lanes < 64) {
|
||||
const HVX_VectorPred keep = Q6_Q_vsetq_R((uint32_t) (real_lanes * sizeof(__fp16)));
|
||||
v0 = Q6_V_vmux_QVV(keep, v0, Q6_V_vzero());
|
||||
v1 = Q6_V_vmux_QVV(keep, v1, Q6_V_vzero());
|
||||
}
|
||||
HVX_VectorPair vp = Q6_W_vshuff_VVR(v1, v0, -2);
|
||||
*pv_out0 = Q6_V_lo_W(vp);
|
||||
*pv_out1 = Q6_V_hi_W(vp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* HMX_FA_KERNELS_H */
|
||||
|
||||
@@ -0,0 +1,801 @@
|
||||
#ifndef HMX_FA_W8A8_KERNELS_H
|
||||
#define HMX_FA_W8A8_KERNELS_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "hex-fastdiv.h"
|
||||
#include "hmx-int-utils.h"
|
||||
#include "hmx-utils.h"
|
||||
#include "hvx-utils.h"
|
||||
|
||||
// Quantize one 4-row quad of FP32 Q into W8A8 U8 SM 64x32 tiles and emit d_q (with scale folded).
|
||||
static __attribute__((noinline)) void hmx_fa_w8a8_q_prep_quad_f32(
|
||||
uint8_t * restrict tile_row_base,
|
||||
float * restrict d_q_out,
|
||||
const float * restrict p0,
|
||||
const float * restrict p1,
|
||||
const float * restrict p2,
|
||||
const float * restrict p3,
|
||||
uint32_t rg,
|
||||
uint32_t n_dot,
|
||||
uint32_t valid_rows,
|
||||
float scale_f32) {
|
||||
|
||||
HVX_Vector vmax0 = Q6_V_vzero();
|
||||
HVX_Vector vmax1 = Q6_V_vzero();
|
||||
HVX_Vector vmax2 = Q6_V_vzero();
|
||||
HVX_Vector vmax3 = Q6_V_vzero();
|
||||
|
||||
for (uint32_t kt = 0; kt < n_dot; ++kt) {
|
||||
const uint32_t off = kt * 32;
|
||||
HVX_Vector vx0 = *(const HVX_UVector *) (p0 + off);
|
||||
HVX_Vector vx1 = *(const HVX_UVector *) (p1 + off);
|
||||
HVX_Vector vx2 = *(const HVX_UVector *) (p2 + off);
|
||||
HVX_Vector vx3 = *(const HVX_UVector *) (p3 + off);
|
||||
vmax0 = Q6_Vsf_vmax_VsfVsf(vmax0, hvx_vec_abs_f32(vx0));
|
||||
vmax1 = Q6_Vsf_vmax_VsfVsf(vmax1, hvx_vec_abs_f32(vx1));
|
||||
vmax2 = Q6_Vsf_vmax_VsfVsf(vmax2, hvx_vec_abs_f32(vx2));
|
||||
vmax3 = Q6_Vsf_vmax_VsfVsf(vmax3, hvx_vec_abs_f32(vx3));
|
||||
}
|
||||
|
||||
vmax0 = (valid_rows > 0) ? hvx_vec_reduce_max_f32(vmax0) : Q6_V_vzero();
|
||||
vmax1 = (valid_rows > 1) ? hvx_vec_reduce_max_f32(vmax1) : Q6_V_vzero();
|
||||
vmax2 = (valid_rows > 2) ? hvx_vec_reduce_max_f32(vmax2) : Q6_V_vzero();
|
||||
vmax3 = (valid_rows > 3) ? hvx_vec_reduce_max_f32(vmax3) : Q6_V_vzero();
|
||||
|
||||
const HVX_Vector v_sc_inv127 = hvx_vec_splat_f32(scale_f32 * (1.0f / 127.0f));
|
||||
const HVX_Vector v_127 = hvx_vec_splat_f32(127.0f);
|
||||
const HVX_Vector v_eps = hvx_vec_splat_f32(1e-30f);
|
||||
|
||||
HVX_Vector vd0 = hvx_vec_mul_f32_f32(vmax0, v_sc_inv127);
|
||||
HVX_Vector vd1 = hvx_vec_mul_f32_f32(vmax1, v_sc_inv127);
|
||||
HVX_Vector vd2 = hvx_vec_mul_f32_f32(vmax2, v_sc_inv127);
|
||||
HVX_Vector vd3 = hvx_vec_mul_f32_f32(vmax3, v_sc_inv127);
|
||||
HVX_Vector v_d02 = Q6_V_lo_W(Q6_W_vshuff_VVR(vd2, vd0, -4));
|
||||
HVX_Vector v_d13 = Q6_V_lo_W(Q6_W_vshuff_VVR(vd3, vd1, -4));
|
||||
HVX_Vector v_d = Q6_V_lo_W(Q6_W_vshuff_VVR(v_d13, v_d02, -4));
|
||||
hvx_vec_store_u(d_q_out, 4 * sizeof(float), v_d);
|
||||
|
||||
HVX_Vector v_inv0 = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(vmax0, v_eps), hvx_vec_mul_f32_f32(hvx_vec_inverse_f32(vmax0), v_127), Q6_V_vzero());
|
||||
HVX_Vector v_inv1 = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(vmax1, v_eps), hvx_vec_mul_f32_f32(hvx_vec_inverse_f32(vmax1), v_127), Q6_V_vzero());
|
||||
HVX_Vector v_inv2 = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(vmax2, v_eps), hvx_vec_mul_f32_f32(hvx_vec_inverse_f32(vmax2), v_127), Q6_V_vzero());
|
||||
HVX_Vector v_inv3 = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(vmax3, v_eps), hvx_vec_mul_f32_f32(hvx_vec_inverse_f32(vmax3), v_127), Q6_V_vzero());
|
||||
|
||||
#if __HVX_ARCH__ >= 81
|
||||
const uint32_t n_pairs = n_dot >> 1;
|
||||
for (uint32_t step = 0; step < n_pairs; ++step) {
|
||||
const uint32_t off = step * 64;
|
||||
HVX_Vector hf0 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p0 + off + 32), v_inv0),
|
||||
Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p0 + off + 0), v_inv0))));
|
||||
HVX_Vector hf1 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p1 + off + 32), v_inv1),
|
||||
Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p1 + off + 0), v_inv1))));
|
||||
HVX_Vector hf2 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p2 + off + 32), v_inv2),
|
||||
Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p2 + off + 0), v_inv2))));
|
||||
HVX_Vector hf3 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p3 + off + 32), v_inv3),
|
||||
Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p3 + off + 0), v_inv3))));
|
||||
|
||||
pack_hf_quad_to_u8_sm_pair(tile_row_base, step * 2, rg, hf0, hf1, hf2, hf3);
|
||||
}
|
||||
if (n_dot & 1u) {
|
||||
const uint32_t kt = n_dot - 1u;
|
||||
const uint32_t off = kt * 32;
|
||||
const HVX_Vector v_zp_u8 = Q6_Vb_vsplat_R(0x80);
|
||||
const HVX_Vector v_min_u8b = Q6_Vb_vsplat_R(1);
|
||||
HVX_Vector hf0 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_V_vzero(), Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p0 + off), v_inv0))));
|
||||
HVX_Vector hf1 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_V_vzero(), Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p1 + off), v_inv1))));
|
||||
HVX_Vector hf2 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_V_vzero(), Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p2 + off), v_inv2))));
|
||||
HVX_Vector hf3 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_V_vzero(), Q6_Vqf32_vmpy_VsfVsf(*(const HVX_UVector *) (p3 + off), v_inv3))));
|
||||
|
||||
HVX_Vector vb02 = Q6_Vb_vshuff_Vb(Q6_Vb_vpack_VhVh_sat(Q6_Vh_equals_Vhf_rnd(hf2), Q6_Vh_equals_Vhf_rnd(hf0)));
|
||||
HVX_Vector vb13 = Q6_Vb_vshuff_Vb(Q6_Vb_vpack_VhVh_sat(Q6_Vh_equals_Vhf_rnd(hf3), Q6_Vh_equals_Vhf_rnd(hf1)));
|
||||
HVX_VectorPair w_u8 = Q6_W_vshuff_VVR(vb13, vb02, -1);
|
||||
((HVX_Vector *) (tile_row_base + (size_t) kt * HMX_W8A8_ACT_TILE_SIZE))[rg] =
|
||||
Q6_Vub_vmax_VubVub(Q6_V_vxor_VV(Q6_V_lo_W(w_u8), v_zp_u8), v_min_u8b);
|
||||
}
|
||||
#else
|
||||
const HVX_Vector v_bias_u8 = Q6_V_vsplat_R(0x4B400080); // 12582912.0f + 128.0f
|
||||
const HVX_Vector v_min_u8 = Q6_Vh_vsplat_R(1);
|
||||
const HVX_Vector v_max_u8 = Q6_Vh_vsplat_R(255);
|
||||
|
||||
for (uint32_t kt = 0; kt < n_dot; ++kt) {
|
||||
const uint32_t off = kt * 32;
|
||||
HVX_Vector vx0 = *(const HVX_UVector *) (p0 + off);
|
||||
HVX_Vector vx1 = *(const HVX_UVector *) (p1 + off);
|
||||
HVX_Vector vx2 = *(const HVX_UVector *) (p2 + off);
|
||||
HVX_Vector vx3 = *(const HVX_UVector *) (p3 + off);
|
||||
|
||||
HVX_Vector y0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_VsfVsf(vx0, v_inv0), v_bias_u8));
|
||||
HVX_Vector y1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_VsfVsf(vx1, v_inv1), v_bias_u8));
|
||||
HVX_Vector y2 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_VsfVsf(vx2, v_inv2), v_bias_u8));
|
||||
HVX_Vector y3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_VsfVsf(vx3, v_inv3), v_bias_u8));
|
||||
|
||||
HVX_Vector vh02 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y2, y0), v_min_u8), v_max_u8);
|
||||
HVX_Vector vh13 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y3, y1), v_min_u8), v_max_u8);
|
||||
((HVX_Vector *) (tile_row_base + (size_t) kt * HMX_W8A8_ACT_TILE_SIZE))[rg] = Q6_Vb_vshuffe_VbVb(vh13, vh02);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Quantize one 4-row quad of FP16 Q into W8A8 U8 SM 64x32 tiles and emit d_q (with scale folded).
|
||||
static __attribute__((noinline)) void hmx_fa_w8a8_q_prep_quad_f16(
|
||||
uint8_t * restrict tile_row_base,
|
||||
float * restrict d_q_out,
|
||||
const __fp16 * restrict p0,
|
||||
const __fp16 * restrict p1,
|
||||
const __fp16 * restrict p2,
|
||||
const __fp16 * restrict p3,
|
||||
uint32_t rg,
|
||||
uint32_t n_dot,
|
||||
uint32_t valid_rows,
|
||||
float scale_f32) {
|
||||
|
||||
const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f);
|
||||
const HVX_Vector v_abs_f16 = Q6_Vh_vsplat_R(0x7fff);
|
||||
HVX_Vector vmax_hf0 = Q6_V_vzero();
|
||||
HVX_Vector vmax_hf1 = Q6_V_vzero();
|
||||
HVX_Vector vmax_hf2 = Q6_V_vzero();
|
||||
HVX_Vector vmax_hf3 = Q6_V_vzero();
|
||||
|
||||
const uint32_t n_pairs = n_dot >> 1;
|
||||
for (uint32_t step = 0; step < n_pairs; ++step) {
|
||||
const uint32_t off = step * 64;
|
||||
HVX_Vector vh0 = *(const HVX_UVector *) (p0 + off);
|
||||
HVX_Vector vh1 = *(const HVX_UVector *) (p1 + off);
|
||||
HVX_Vector vh2 = *(const HVX_UVector *) (p2 + off);
|
||||
HVX_Vector vh3 = *(const HVX_UVector *) (p3 + off);
|
||||
vmax_hf0 = Q6_Vhf_vmax_VhfVhf(vmax_hf0, Q6_V_vand_VV(vh0, v_abs_f16));
|
||||
vmax_hf1 = Q6_Vhf_vmax_VhfVhf(vmax_hf1, Q6_V_vand_VV(vh1, v_abs_f16));
|
||||
vmax_hf2 = Q6_Vhf_vmax_VhfVhf(vmax_hf2, Q6_V_vand_VV(vh2, v_abs_f16));
|
||||
vmax_hf3 = Q6_Vhf_vmax_VhfVhf(vmax_hf3, Q6_V_vand_VV(vh3, v_abs_f16));
|
||||
}
|
||||
if (n_dot & 1u) {
|
||||
const uint32_t off = (n_dot - 1u) * 32;
|
||||
const HVX_VectorPred q_32h = Q6_Q_vsetq2_R(32 * sizeof(__fp16));
|
||||
HVX_Vector vh0 = Q6_V_vmux_QVV(q_32h, *(const HVX_UVector *) (p0 + off), Q6_V_vzero());
|
||||
HVX_Vector vh1 = Q6_V_vmux_QVV(q_32h, *(const HVX_UVector *) (p1 + off), Q6_V_vzero());
|
||||
HVX_Vector vh2 = Q6_V_vmux_QVV(q_32h, *(const HVX_UVector *) (p2 + off), Q6_V_vzero());
|
||||
HVX_Vector vh3 = Q6_V_vmux_QVV(q_32h, *(const HVX_UVector *) (p3 + off), Q6_V_vzero());
|
||||
vmax_hf0 = Q6_Vhf_vmax_VhfVhf(vmax_hf0, Q6_V_vand_VV(vh0, v_abs_f16));
|
||||
vmax_hf1 = Q6_Vhf_vmax_VhfVhf(vmax_hf1, Q6_V_vand_VV(vh1, v_abs_f16));
|
||||
vmax_hf2 = Q6_Vhf_vmax_VhfVhf(vmax_hf2, Q6_V_vand_VV(vh2, v_abs_f16));
|
||||
vmax_hf3 = Q6_Vhf_vmax_VhfVhf(vmax_hf3, Q6_V_vand_VV(vh3, v_abs_f16));
|
||||
}
|
||||
|
||||
vmax_hf0 = Q6_Vhf_vmax_VhfVhf(vmax_hf0, Q6_V_vror_VR(vmax_hf0, 64));
|
||||
vmax_hf1 = Q6_Vhf_vmax_VhfVhf(vmax_hf1, Q6_V_vror_VR(vmax_hf1, 64));
|
||||
vmax_hf2 = Q6_Vhf_vmax_VhfVhf(vmax_hf2, Q6_V_vror_VR(vmax_hf2, 64));
|
||||
vmax_hf3 = Q6_Vhf_vmax_VhfVhf(vmax_hf3, Q6_V_vror_VR(vmax_hf3, 64));
|
||||
|
||||
HVX_Vector vmax0 = (valid_rows > 0) ? hvx_vec_reduce_max_f32(Q6_Vsf_equals_Vqf32(Q6_V_lo_W(Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(vmax_hf0), v_one_f16)))) : Q6_V_vzero();
|
||||
HVX_Vector vmax1 = (valid_rows > 1) ? hvx_vec_reduce_max_f32(Q6_Vsf_equals_Vqf32(Q6_V_lo_W(Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(vmax_hf1), v_one_f16)))) : Q6_V_vzero();
|
||||
HVX_Vector vmax2 = (valid_rows > 2) ? hvx_vec_reduce_max_f32(Q6_Vsf_equals_Vqf32(Q6_V_lo_W(Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(vmax_hf2), v_one_f16)))) : Q6_V_vzero();
|
||||
HVX_Vector vmax3 = (valid_rows > 3) ? hvx_vec_reduce_max_f32(Q6_Vsf_equals_Vqf32(Q6_V_lo_W(Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(vmax_hf3), v_one_f16)))) : Q6_V_vzero();
|
||||
|
||||
const HVX_Vector v_sc_inv127 = hvx_vec_splat_f32(scale_f32 * (1.0f / 127.0f));
|
||||
const HVX_Vector v_127 = hvx_vec_splat_f32(127.0f);
|
||||
const HVX_Vector v_eps = hvx_vec_splat_f32(1e-30f);
|
||||
|
||||
HVX_Vector vd0 = hvx_vec_mul_f32_f32(vmax0, v_sc_inv127);
|
||||
HVX_Vector vd1 = hvx_vec_mul_f32_f32(vmax1, v_sc_inv127);
|
||||
HVX_Vector vd2 = hvx_vec_mul_f32_f32(vmax2, v_sc_inv127);
|
||||
HVX_Vector vd3 = hvx_vec_mul_f32_f32(vmax3, v_sc_inv127);
|
||||
HVX_Vector v_d02 = Q6_V_lo_W(Q6_W_vshuff_VVR(vd2, vd0, -4));
|
||||
HVX_Vector v_d13 = Q6_V_lo_W(Q6_W_vshuff_VVR(vd3, vd1, -4));
|
||||
HVX_Vector v_d = Q6_V_lo_W(Q6_W_vshuff_VVR(v_d13, v_d02, -4));
|
||||
hvx_vec_store_u(d_q_out, 4 * sizeof(float), v_d);
|
||||
|
||||
HVX_Vector v_inv0_qf = hvx_vec_f32_to_qf32(Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(vmax0, v_eps), hvx_vec_mul_f32_f32(hvx_vec_inverse_f32(vmax0), v_127), Q6_V_vzero()));
|
||||
HVX_Vector v_inv1_qf = hvx_vec_f32_to_qf32(Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(vmax1, v_eps), hvx_vec_mul_f32_f32(hvx_vec_inverse_f32(vmax1), v_127), Q6_V_vzero()));
|
||||
HVX_Vector v_inv2_qf = hvx_vec_f32_to_qf32(Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(vmax2, v_eps), hvx_vec_mul_f32_f32(hvx_vec_inverse_f32(vmax2), v_127), Q6_V_vzero()));
|
||||
HVX_Vector v_inv3_qf = hvx_vec_f32_to_qf32(Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VsfVsf(vmax3, v_eps), hvx_vec_mul_f32_f32(hvx_vec_inverse_f32(vmax3), v_127), Q6_V_vzero()));
|
||||
|
||||
#if __HVX_ARCH__ >= 81
|
||||
for (uint32_t step = 0; step < n_pairs; ++step) {
|
||||
const uint32_t off = step * 64;
|
||||
HVX_VectorPair vp0 = Q6_Wqf32_vmpy_VhfVhf(*(const HVX_UVector *) (p0 + off), v_one_f16);
|
||||
HVX_VectorPair vp1 = Q6_Wqf32_vmpy_VhfVhf(*(const HVX_UVector *) (p1 + off), v_one_f16);
|
||||
HVX_VectorPair vp2 = Q6_Wqf32_vmpy_VhfVhf(*(const HVX_UVector *) (p2 + off), v_one_f16);
|
||||
HVX_VectorPair vp3 = Q6_Wqf32_vmpy_VhfVhf(*(const HVX_UVector *) (p3 + off), v_one_f16);
|
||||
|
||||
HVX_Vector hf0 = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_hi_W(vp0), v_inv0_qf),
|
||||
Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp0), v_inv0_qf)));
|
||||
HVX_Vector hf1 = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_hi_W(vp1), v_inv1_qf),
|
||||
Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp1), v_inv1_qf)));
|
||||
HVX_Vector hf2 = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_hi_W(vp2), v_inv2_qf),
|
||||
Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp2), v_inv2_qf)));
|
||||
HVX_Vector hf3 = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_hi_W(vp3), v_inv3_qf),
|
||||
Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp3), v_inv3_qf)));
|
||||
|
||||
pack_hf_quad_to_u8_sm_pair(tile_row_base, step * 2, rg, hf0, hf1, hf2, hf3);
|
||||
}
|
||||
if (n_dot & 1u) {
|
||||
const uint32_t kt = n_dot - 1u;
|
||||
const uint32_t off = kt * 32;
|
||||
const HVX_Vector v_zp_u8 = Q6_Vb_vsplat_R(0x80);
|
||||
const HVX_Vector v_min_u8b = Q6_Vb_vsplat_R(1);
|
||||
HVX_VectorPair vp0 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p0 + off)), v_one_f16);
|
||||
HVX_VectorPair vp1 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p1 + off)), v_one_f16);
|
||||
HVX_VectorPair vp2 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p2 + off)), v_one_f16);
|
||||
HVX_VectorPair vp3 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p3 + off)), v_one_f16);
|
||||
|
||||
HVX_Vector hf0 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_V_vzero(), Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp0), v_inv0_qf))));
|
||||
HVX_Vector hf1 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_V_vzero(), Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp1), v_inv1_qf))));
|
||||
HVX_Vector hf2 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_V_vzero(), Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp2), v_inv2_qf))));
|
||||
HVX_Vector hf3 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(
|
||||
Q6_V_vzero(), Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp3), v_inv3_qf))));
|
||||
|
||||
HVX_Vector vb02 = Q6_Vb_vshuff_Vb(Q6_Vb_vpack_VhVh_sat(Q6_Vh_equals_Vhf_rnd(hf2), Q6_Vh_equals_Vhf_rnd(hf0)));
|
||||
HVX_Vector vb13 = Q6_Vb_vshuff_Vb(Q6_Vb_vpack_VhVh_sat(Q6_Vh_equals_Vhf_rnd(hf3), Q6_Vh_equals_Vhf_rnd(hf1)));
|
||||
HVX_VectorPair w_u8 = Q6_W_vshuff_VVR(vb13, vb02, -1);
|
||||
((HVX_Vector *) (tile_row_base + (size_t) kt * HMX_W8A8_ACT_TILE_SIZE))[rg] =
|
||||
Q6_Vub_vmax_VubVub(Q6_V_vxor_VV(Q6_V_lo_W(w_u8), v_zp_u8), v_min_u8b);
|
||||
}
|
||||
#else
|
||||
const HVX_Vector v_bias_u8 = Q6_V_vsplat_R(0x4B400080);
|
||||
const HVX_Vector v_min_u8 = Q6_Vh_vsplat_R(1);
|
||||
const HVX_Vector v_max_u8 = Q6_Vh_vsplat_R(255);
|
||||
|
||||
for (uint32_t step = 0; step < n_pairs; ++step) {
|
||||
const uint32_t off = step * 64;
|
||||
const uint32_t kt = step * 2;
|
||||
HVX_VectorPair vp0 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p0 + off)), v_one_f16);
|
||||
HVX_VectorPair vp1 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p1 + off)), v_one_f16);
|
||||
HVX_VectorPair vp2 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p2 + off)), v_one_f16);
|
||||
HVX_VectorPair vp3 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p3 + off)), v_one_f16);
|
||||
|
||||
{
|
||||
HVX_Vector y0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp0), v_inv0_qf), v_bias_u8));
|
||||
HVX_Vector y1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp1), v_inv1_qf), v_bias_u8));
|
||||
HVX_Vector y2 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp2), v_inv2_qf), v_bias_u8));
|
||||
HVX_Vector y3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp3), v_inv3_qf), v_bias_u8));
|
||||
HVX_Vector vh02 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y2, y0), v_min_u8), v_max_u8);
|
||||
HVX_Vector vh13 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y3, y1), v_min_u8), v_max_u8);
|
||||
((HVX_Vector *) (tile_row_base + (size_t) (kt + 0) * HMX_W8A8_ACT_TILE_SIZE))[rg] = Q6_Vb_vshuffe_VbVb(vh13, vh02);
|
||||
}
|
||||
{
|
||||
HVX_Vector y0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_hi_W(vp0), v_inv0_qf), v_bias_u8));
|
||||
HVX_Vector y1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_hi_W(vp1), v_inv1_qf), v_bias_u8));
|
||||
HVX_Vector y2 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_hi_W(vp2), v_inv2_qf), v_bias_u8));
|
||||
HVX_Vector y3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_hi_W(vp3), v_inv3_qf), v_bias_u8));
|
||||
HVX_Vector vh02 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y2, y0), v_min_u8), v_max_u8);
|
||||
HVX_Vector vh13 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y3, y1), v_min_u8), v_max_u8);
|
||||
((HVX_Vector *) (tile_row_base + (size_t) (kt + 1) * HMX_W8A8_ACT_TILE_SIZE))[rg] = Q6_Vb_vshuffe_VbVb(vh13, vh02);
|
||||
}
|
||||
}
|
||||
if (n_dot & 1u) {
|
||||
const uint32_t kt = n_dot - 1u;
|
||||
const uint32_t off = kt * 32;
|
||||
HVX_VectorPair vp0 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p0 + off)), v_one_f16);
|
||||
HVX_VectorPair vp1 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p1 + off)), v_one_f16);
|
||||
HVX_VectorPair vp2 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p2 + off)), v_one_f16);
|
||||
HVX_VectorPair vp3 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(*(const HVX_UVector *) (p3 + off)), v_one_f16);
|
||||
|
||||
HVX_Vector y0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp0), v_inv0_qf), v_bias_u8));
|
||||
HVX_Vector y1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp1), v_inv1_qf), v_bias_u8));
|
||||
HVX_Vector y2 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp2), v_inv2_qf), v_bias_u8));
|
||||
HVX_Vector y3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(Q6_V_lo_W(vp3), v_inv3_qf), v_bias_u8));
|
||||
HVX_Vector vh02 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y2, y0), v_min_u8), v_max_u8);
|
||||
HVX_Vector vh13 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y3, y1), v_min_u8), v_max_u8);
|
||||
((HVX_Vector *) (tile_row_base + (size_t) kt * HMX_W8A8_ACT_TILE_SIZE))[rg] = Q6_Vb_vshuffe_VbVb(vh13, vh02);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void hmx_fa_w8a8_q_prep_f32(
|
||||
uint8_t * restrict vtcm_q_tiles,
|
||||
float * restrict vtcm_q_scales,
|
||||
const uint8_t * restrict temp_q_vtcm,
|
||||
size_t start,
|
||||
size_t end,
|
||||
size_t g_rows_end,
|
||||
size_t DK,
|
||||
size_t G,
|
||||
size_t n_rows_q,
|
||||
const struct fastdiv_values * div_G,
|
||||
bool q_transposed,
|
||||
float scale_f32) {
|
||||
|
||||
const uint32_t n_dot = (uint32_t) (DK / 32);
|
||||
const HVX_Vector v_zp_u8 = Q6_Vb_vsplat_R(0x80);
|
||||
|
||||
for (size_t r = start; r < end; r += 4) {
|
||||
const size_t r0 = r >> 6;
|
||||
const uint32_t rg = (uint32_t) ((r & 63) >> 2);
|
||||
uint8_t * tile_row_base = vtcm_q_tiles + r0 * n_dot * HMX_W8A8_ACT_TILE_SIZE;
|
||||
|
||||
if (r >= g_rows_end) {
|
||||
for (uint32_t kt = 0; kt < n_dot; ++kt) {
|
||||
((HVX_Vector *) (tile_row_base + (size_t) kt * HMX_W8A8_ACT_TILE_SIZE))[rg] = v_zp_u8;
|
||||
}
|
||||
hvx_vec_store_u(vtcm_q_scales + r, 4 * sizeof(float), Q6_V_vzero());
|
||||
continue;
|
||||
}
|
||||
|
||||
const uint32_t valid_rows = (uint32_t) hex_smin(4, g_rows_end - r);
|
||||
const float * ptrs[4];
|
||||
for (uint32_t k = 0; k < 4; ++k) {
|
||||
if (k < valid_rows) {
|
||||
const size_t q_idx = fastdiv(r + k, div_G);
|
||||
const size_t h_idx = fastmodulo(r + k, G, div_G);
|
||||
const size_t offset = q_transposed ? (h_idx * n_rows_q + q_idx) : (q_idx * G + h_idx);
|
||||
ptrs[k] = (const float *) (temp_q_vtcm + offset * DK * sizeof(float));
|
||||
} else {
|
||||
ptrs[k] = ptrs[0];
|
||||
}
|
||||
}
|
||||
|
||||
hmx_fa_w8a8_q_prep_quad_f32(
|
||||
tile_row_base, vtcm_q_scales + r,
|
||||
ptrs[0], ptrs[1], ptrs[2], ptrs[3],
|
||||
rg, n_dot, valid_rows, scale_f32);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hmx_fa_w8a8_q_prep_f16(
|
||||
uint8_t * restrict vtcm_q_tiles,
|
||||
float * restrict vtcm_q_scales,
|
||||
const uint8_t * restrict temp_q_vtcm,
|
||||
size_t start,
|
||||
size_t end,
|
||||
size_t g_rows_end,
|
||||
size_t DK,
|
||||
size_t G,
|
||||
size_t n_rows_q,
|
||||
const struct fastdiv_values * div_G,
|
||||
bool q_transposed,
|
||||
float scale_f32) {
|
||||
|
||||
const uint32_t n_dot = (uint32_t) (DK / 32);
|
||||
const HVX_Vector v_zp_u8 = Q6_Vb_vsplat_R(0x80);
|
||||
|
||||
for (size_t r = start; r < end; r += 4) {
|
||||
const size_t r0 = r >> 6;
|
||||
const uint32_t rg = (uint32_t) ((r & 63) >> 2);
|
||||
uint8_t * tile_row_base = vtcm_q_tiles + r0 * n_dot * HMX_W8A8_ACT_TILE_SIZE;
|
||||
|
||||
if (r >= g_rows_end) {
|
||||
for (uint32_t kt = 0; kt < n_dot; ++kt) {
|
||||
((HVX_Vector *) (tile_row_base + (size_t) kt * HMX_W8A8_ACT_TILE_SIZE))[rg] = v_zp_u8;
|
||||
}
|
||||
hvx_vec_store_u(vtcm_q_scales + r, 4 * sizeof(float), Q6_V_vzero());
|
||||
continue;
|
||||
}
|
||||
|
||||
const uint32_t valid_rows = (uint32_t) hex_smin(4, g_rows_end - r);
|
||||
const __fp16 * ptrs[4];
|
||||
for (uint32_t k = 0; k < 4; ++k) {
|
||||
if (k < valid_rows) {
|
||||
const size_t q_idx = fastdiv(r + k, div_G);
|
||||
const size_t h_idx = fastmodulo(r + k, G, div_G);
|
||||
const size_t offset = q_transposed ? (h_idx * n_rows_q + q_idx) : (q_idx * G + h_idx);
|
||||
ptrs[k] = (const __fp16 *) (temp_q_vtcm + offset * DK * sizeof(__fp16));
|
||||
} else {
|
||||
ptrs[k] = ptrs[0];
|
||||
}
|
||||
}
|
||||
|
||||
hmx_fa_w8a8_q_prep_quad_f16(
|
||||
tile_row_base, vtcm_q_scales + r,
|
||||
ptrs[0], ptrs[1], ptrs[2], ptrs[3],
|
||||
rg, n_dot, valid_rows, scale_f32);
|
||||
}
|
||||
}
|
||||
|
||||
static inline HVX_Vector hmx_fa_w8a8_k_pack4(const uint8_t * p, size_t stride) {
|
||||
HVX_Vector v0 = *(const HVX_UVector *) (p + 0 * stride);
|
||||
HVX_Vector v1 = *(const HVX_UVector *) (p + 1 * stride);
|
||||
HVX_Vector v2 = *(const HVX_UVector *) (p + 2 * stride);
|
||||
HVX_Vector v3 = *(const HVX_UVector *) (p + 3 * stride);
|
||||
HVX_Vector p01 = Q6_V_lo_W(Q6_W_vshuff_VVR(v1, v0, -4));
|
||||
HVX_Vector p23 = Q6_V_lo_W(Q6_W_vshuff_VVR(v3, v2, -4));
|
||||
return Q6_V_lo_W(Q6_W_vshuff_VVR(p23, p01, -8));
|
||||
}
|
||||
|
||||
// Same math as int_hmx_rescale_pair_wb, using vadd(-1) instead of vcombine+vmpyacc to avoid Vdd pair spills.
|
||||
static inline void hmx_fa_w8a8_k_rescale_pair(
|
||||
HVX_Vector v_q0,
|
||||
HVX_Vector v_q1,
|
||||
HVX_Vector v_m_b4,
|
||||
HVX_Vector * restrict tile_dst,
|
||||
int pair_idx,
|
||||
HVX_Vector * restrict v_sum_w,
|
||||
HVX_Vector * restrict v_l1_w) {
|
||||
|
||||
const HVX_Vector v_neg1_h = Q6_Vh_vsplat_R(-1);
|
||||
HVX_VectorPair vp_p01 = Q6_Wh_vmpy_VbVb(v_q0, v_m_b4);
|
||||
HVX_VectorPair vp_p23 = Q6_Wh_vmpy_VbVb(v_q1, v_m_b4);
|
||||
HVX_Vector v_p23_lo = Q6_Vh_vadd_VhVh(Q6_V_lo_W(vp_p23), v_neg1_h);
|
||||
HVX_Vector v_p23_hi = Q6_Vh_vadd_VhVh(Q6_V_hi_W(vp_p23), v_neg1_h);
|
||||
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_Vector v_W0 = Q6_Vb_vasr_VhVhR_rnd_sat(Q6_V_hi_W(vp_p01), Q6_V_lo_W(vp_p01), 7);
|
||||
HVX_Vector v_W1 = Q6_Vb_vasr_VhVhR_rnd_sat(v_p23_hi, v_p23_lo, 7);
|
||||
#else
|
||||
HVX_Vector v_W0 = Q6_Vb_vround_VhVh_sat(Q6_Vh_vasl_VhR(Q6_V_hi_W(vp_p01), 1),
|
||||
Q6_Vh_vasl_VhR(Q6_V_lo_W(vp_p01), 1));
|
||||
HVX_Vector v_W1 = Q6_Vb_vround_VhVh_sat(Q6_Vh_vasl_VhR(v_p23_hi, 1),
|
||||
Q6_Vh_vasl_VhR(v_p23_lo, 1));
|
||||
#endif
|
||||
int_hmx_store_and_accum_pair(tile_dst, pair_idx, v_W0, v_W1, v_sum_w, v_l1_w);
|
||||
}
|
||||
|
||||
// Q8_0 K -> S8 weight.b 32x32 tiles for one column tile (32 KV rows).
|
||||
static __attribute__((noinline)) void hmx_fa_w8a8_k_prep_tile(
|
||||
const uint8_t * restrict k_raw,
|
||||
uint8_t * restrict dst_wt,
|
||||
float * restrict dst_scales,
|
||||
HVX_Vector * restrict bias_out,
|
||||
uint32_t valid_rows,
|
||||
uint32_t n_dot,
|
||||
size_t k_raw_row_padded) {
|
||||
|
||||
uint16_t off_l1[64] __attribute__((aligned(128)));
|
||||
for (uint32_t c = 0; c < 32; ++c) {
|
||||
off_l1[c] = (c < valid_rows) ? (uint16_t) (c * k_raw_row_padded) : 0;
|
||||
off_l1[c + 32] = 0;
|
||||
}
|
||||
const HVX_Vector v_off_h = *(const HVX_Vector *) off_l1;
|
||||
const uint32_t mu = (uint32_t) (32 * k_raw_row_padded - 1);
|
||||
const HVX_VectorPred q_valid_h = Q6_Q_vsetq2_R(valid_rows * sizeof(uint16_t));
|
||||
|
||||
// Step 1: gather Q8_0 block scales into tile_dst[kt * 8 + 7] and compute per-channel max
|
||||
HVX_Vector * tile_dst_all = (HVX_Vector *) dst_wt;
|
||||
for (uint32_t kt = 0; kt < n_dot; ++kt) {
|
||||
Q6_vgather_ARMVh(&tile_dst_all[kt * 8 + 7], (size_t) (k_raw + kt * 34), mu, v_off_h);
|
||||
}
|
||||
hvx_gather_sync(&tile_dst_all[(n_dot - 1) * 8 + 7]);
|
||||
|
||||
const HVX_Vector v_abs_mask = Q6_Vh_vsplat_R(0x7fff);
|
||||
HVX_Vector v_max_sc = Q6_V_vzero();
|
||||
for (uint32_t kt = 0; kt < n_dot; ++kt) {
|
||||
HVX_Vector v_sc = Q6_V_vmux_QVV(q_valid_h, tile_dst_all[kt * 8 + 7], Q6_V_vzero());
|
||||
tile_dst_all[kt * 8 + 7] = v_sc;
|
||||
v_max_sc = Q6_Vhf_vmax_VhfVhf(v_max_sc, Q6_V_vand_VV(v_sc, v_abs_mask));
|
||||
}
|
||||
|
||||
HVX_Vector v_max_sf, v_inv127_qf;
|
||||
int_hmx_inv127_from_max_f16(v_max_sc, &v_max_sf, &v_inv127_qf);
|
||||
|
||||
for (uint32_t kt = 0; kt < n_dot; ++kt) {
|
||||
tile_dst_all[kt * 8 + 7] = int_hmx_m_b4_from_f16(tile_dst_all[kt * 8 + 7], v_inv127_qf);
|
||||
}
|
||||
|
||||
// Step 2: 32x32-byte vshuffvdd transpose of int8 quants and rescale in-place to weight.b
|
||||
// Invalid rows (r >= valid_rows) have zeroed scales so v_m_b4 == 0 zeroes their weights.
|
||||
HVX_Vector v_sum_w = Q6_V_vzero();
|
||||
HVX_Vector v_l1_w = Q6_V_vzero();
|
||||
|
||||
#pragma clang loop pipeline(disable) unroll(disable)
|
||||
for (uint32_t kt = 0; kt < n_dot; ++kt) {
|
||||
HVX_Vector * tile_dst = tile_dst_all + kt * 8;
|
||||
const uint8_t * qs_base = k_raw + kt * 34 + 2;
|
||||
|
||||
HVX_Vector b0, b1, b4, b5;
|
||||
{
|
||||
HVX_Vector t0 = hmx_fa_w8a8_k_pack4(qs_base + 0 * k_raw_row_padded, k_raw_row_padded);
|
||||
HVX_Vector t1 = hmx_fa_w8a8_k_pack4(qs_base + 4 * k_raw_row_padded, k_raw_row_padded);
|
||||
HVX_VectorPair w01 = Q6_W_vshuff_VVR(t1, t0, -16);
|
||||
HVX_Vector t2 = hmx_fa_w8a8_k_pack4(qs_base + 8 * k_raw_row_padded, k_raw_row_padded);
|
||||
HVX_Vector t3 = hmx_fa_w8a8_k_pack4(qs_base + 12 * k_raw_row_padded, k_raw_row_padded);
|
||||
HVX_VectorPair w23 = Q6_W_vshuff_VVR(t3, t2, -16);
|
||||
|
||||
HVX_VectorPair u0 = Q6_W_vshuff_VVR(Q6_V_lo_W(w23), Q6_V_lo_W(w01), -32);
|
||||
HVX_VectorPair u2 = Q6_W_vshuff_VVR(Q6_V_hi_W(w23), Q6_V_hi_W(w01), -32);
|
||||
b0 = Q6_V_lo_W(u0);
|
||||
b1 = Q6_V_hi_W(u0);
|
||||
b4 = Q6_V_lo_W(u2);
|
||||
b5 = Q6_V_hi_W(u2);
|
||||
}
|
||||
|
||||
{
|
||||
HVX_Vector t4 = hmx_fa_w8a8_k_pack4(qs_base + 16 * k_raw_row_padded, k_raw_row_padded);
|
||||
HVX_Vector t5 = hmx_fa_w8a8_k_pack4(qs_base + 20 * k_raw_row_padded, k_raw_row_padded);
|
||||
HVX_VectorPair w45 = Q6_W_vshuff_VVR(t5, t4, -16);
|
||||
HVX_Vector t6 = hmx_fa_w8a8_k_pack4(qs_base + 24 * k_raw_row_padded, k_raw_row_padded);
|
||||
HVX_Vector t7 = hmx_fa_w8a8_k_pack4(qs_base + 28 * k_raw_row_padded, k_raw_row_padded);
|
||||
HVX_VectorPair w67 = Q6_W_vshuff_VVR(t7, t6, -16);
|
||||
|
||||
const HVX_Vector v_m_b4 = tile_dst[7];
|
||||
|
||||
HVX_VectorPair u1 = Q6_W_vshuff_VVR(Q6_V_lo_W(w67), Q6_V_lo_W(w45), -32);
|
||||
HVX_VectorPair q01 = Q6_W_vshuff_VVR(Q6_V_lo_W(u1), b0, -64);
|
||||
hmx_fa_w8a8_k_rescale_pair(Q6_V_lo_W(q01), Q6_V_hi_W(q01), v_m_b4, tile_dst, 0, &v_sum_w, &v_l1_w);
|
||||
HVX_VectorPair q23 = Q6_W_vshuff_VVR(Q6_V_hi_W(u1), b1, -64);
|
||||
hmx_fa_w8a8_k_rescale_pair(Q6_V_lo_W(q23), Q6_V_hi_W(q23), v_m_b4, tile_dst, 1, &v_sum_w, &v_l1_w);
|
||||
|
||||
HVX_VectorPair u3 = Q6_W_vshuff_VVR(Q6_V_hi_W(w67), Q6_V_hi_W(w45), -32);
|
||||
HVX_VectorPair q45 = Q6_W_vshuff_VVR(Q6_V_lo_W(u3), b4, -64);
|
||||
hmx_fa_w8a8_k_rescale_pair(Q6_V_lo_W(q45), Q6_V_hi_W(q45), v_m_b4, tile_dst, 2, &v_sum_w, &v_l1_w);
|
||||
HVX_VectorPair q67 = Q6_W_vshuff_VVR(Q6_V_hi_W(u3), b5, -64);
|
||||
hmx_fa_w8a8_k_rescale_pair(Q6_V_lo_W(q67), Q6_V_hi_W(q67), v_m_b4, tile_dst, 3, &v_sum_w, &v_l1_w);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3: emit per-channel scales and HMX bias banks
|
||||
emit_int_hmx_scales_and_bias(v_max_sf, 128.0f / 127.0f, v_sum_w, v_l1_w, dst_scales, bias_out);
|
||||
}
|
||||
|
||||
// Decode one 4-row x 32-col W8A8 S tile (out_hi + out_lo) into 4 FP32 vectors.
|
||||
static inline void hmx_fa_w8a8_eval_s_tile_quad(
|
||||
const uint8_t * restrict tile_base,
|
||||
const float * restrict dw_base,
|
||||
size_t c,
|
||||
uint32_t da0,
|
||||
uint32_t da1,
|
||||
uint32_t da2,
|
||||
uint32_t da3,
|
||||
HVX_Vector sign_80,
|
||||
HVX_Vector * v_out0,
|
||||
HVX_Vector * v_out1,
|
||||
HVX_Vector * v_out2,
|
||||
HVX_Vector * v_out3) {
|
||||
|
||||
const size_t c0 = c >> 5;
|
||||
const uint8_t * tp = tile_base + c0 * 2 * HMX_W8A8_ACT_TILE_SIZE;
|
||||
const HVX_Vector v_hi_s8 = Q6_V_vxor_VV(*(const HVX_Vector *) tp, sign_80);
|
||||
const HVX_Vector v_lo_u8 = *(const HVX_Vector *) (tp + HMX_W8A8_ACT_TILE_SIZE);
|
||||
const HVX_Vector v_dw_qf = hvx_vec_f32_to_qf32(*(const HVX_Vector *) (dw_base + c));
|
||||
HVX_VectorPair vp_i16 = Q6_W_vshuff_VVR(v_hi_s8, v_lo_u8, -1);
|
||||
HVX_VectorPair vp_i32_01 = Q6_Ww_vunpack_Vh(Q6_V_lo_W(vp_i16));
|
||||
HVX_VectorPair vp_i32_23 = Q6_Ww_vunpack_Vh(Q6_V_hi_W(vp_i16));
|
||||
|
||||
*v_out0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(vp_i32_01)), da0), v_dw_qf));
|
||||
*v_out1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(vp_i32_01)), da1), v_dw_qf));
|
||||
*v_out2 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_lo_W(vp_i32_23)), da2), v_dw_qf));
|
||||
*v_out3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_Vqf32Vqf32(hvx_qf32_mul_sf_scalar(Q6_Vsf_equals_Vw(Q6_V_hi_W(vp_i32_23)), da3), v_dw_qf));
|
||||
}
|
||||
|
||||
// Pack 4 rows x 64 FP16 values in [0, 255] (zp = 0) into 1 or 2 U8 SM 64x32 tiles.
|
||||
static inline void hmx_fa_w8a8_pack_p_quad_u8_sm(
|
||||
uint8_t * restrict tile_base,
|
||||
uint32_t kt,
|
||||
uint32_t rg,
|
||||
bool write_hi,
|
||||
HVX_Vector hf0,
|
||||
HVX_Vector hf1,
|
||||
HVX_Vector hf2,
|
||||
HVX_Vector hf3) {
|
||||
#if __HVX_ARCH__ >= 81
|
||||
HVX_Vector vb02 = Q6_Vb_vshuff_Vb(Q6_Vub_vpack_VhVh_sat(Q6_Vh_equals_Vhf_rnd(hf2), Q6_Vh_equals_Vhf_rnd(hf0)));
|
||||
HVX_Vector vb13 = Q6_Vb_vshuff_Vb(Q6_Vub_vpack_VhVh_sat(Q6_Vh_equals_Vhf_rnd(hf3), Q6_Vh_equals_Vhf_rnd(hf1)));
|
||||
HVX_VectorPair w_u8 = Q6_W_vshuff_VVR(vb13, vb02, -1);
|
||||
|
||||
((HVX_Vector *) (tile_base + (size_t) (kt + 0) * HMX_W8A8_ACT_TILE_SIZE))[rg] = Q6_V_lo_W(w_u8);
|
||||
if (write_hi) {
|
||||
((HVX_Vector *) (tile_base + (size_t) (kt + 1) * HMX_W8A8_ACT_TILE_SIZE))[rg] = Q6_V_hi_W(w_u8);
|
||||
}
|
||||
#else
|
||||
const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f);
|
||||
const HVX_Vector v_bias_zp0 = Q6_V_vsplat_R(0x4B400000); // 12582912.0f
|
||||
const HVX_Vector v_zero = Q6_V_vzero();
|
||||
const HVX_Vector v_max_u8 = Q6_Vh_vsplat_R(255);
|
||||
|
||||
HVX_VectorPair vp0 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(hf0), v_one_f16);
|
||||
HVX_VectorPair vp1 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(hf1), v_one_f16);
|
||||
HVX_VectorPair vp2 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(hf2), v_one_f16);
|
||||
HVX_VectorPair vp3 = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(hf3), v_one_f16);
|
||||
|
||||
{
|
||||
HVX_Vector y0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_lo_W(vp0), v_bias_zp0));
|
||||
HVX_Vector y1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_lo_W(vp1), v_bias_zp0));
|
||||
HVX_Vector y2 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_lo_W(vp2), v_bias_zp0));
|
||||
HVX_Vector y3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_lo_W(vp3), v_bias_zp0));
|
||||
HVX_Vector vh02 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y2, y0), v_zero), v_max_u8);
|
||||
HVX_Vector vh13 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y3, y1), v_zero), v_max_u8);
|
||||
((HVX_Vector *) (tile_base + (size_t) (kt + 0) * HMX_W8A8_ACT_TILE_SIZE))[rg] = Q6_Vb_vshuffe_VbVb(vh13, vh02);
|
||||
}
|
||||
if (write_hi) {
|
||||
HVX_Vector y0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_hi_W(vp0), v_bias_zp0));
|
||||
HVX_Vector y1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_hi_W(vp1), v_bias_zp0));
|
||||
HVX_Vector y2 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_hi_W(vp2), v_bias_zp0));
|
||||
HVX_Vector y3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_V_hi_W(vp3), v_bias_zp0));
|
||||
HVX_Vector vh02 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y2, y0), v_zero), v_max_u8);
|
||||
HVX_Vector vh13 = Q6_Vh_vmin_VhVh(Q6_Vh_vmax_VhVh(Q6_Vh_vshuffe_VhVh(y3, y1), v_zero), v_max_u8);
|
||||
((HVX_Vector *) (tile_base + (size_t) (kt + 1) * HMX_W8A8_ACT_TILE_SIZE))[rg] = Q6_Vb_vshuffe_VbVb(vh13, vh02);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Zero-gather Q8_0 V -> S8 weight.b 32x32 tiles for one DV tile (32 output cols) across kv_rows.
|
||||
static __attribute__((noinline)) void hmx_fa_w8a8_v_prep_tile(
|
||||
const uint8_t * restrict v_base,
|
||||
uint8_t * restrict dst_wt,
|
||||
float * restrict dst_scales,
|
||||
HVX_Vector * restrict bias_out,
|
||||
uint32_t kv_rows,
|
||||
uint32_t n_col_tiles,
|
||||
size_t v_raw_row_padded) {
|
||||
|
||||
uint16_t off_full[64] __attribute__((aligned(128)));
|
||||
for (uint32_t c = 0; c < 32; ++c) {
|
||||
off_full[c] = (uint16_t) (c * v_raw_row_padded);
|
||||
off_full[c + 32] = 0;
|
||||
}
|
||||
const HVX_Vector v_off_full = *(const HVX_Vector *) off_full;
|
||||
const uint32_t mu = (uint32_t) (32 * v_raw_row_padded - 1);
|
||||
|
||||
// Pass 1: gather Q8_0 block scales into tile_dst[kt * 8 + 7] and reduce max across kv_rows
|
||||
HVX_Vector * tile_dst_all = (HVX_Vector *) dst_wt;
|
||||
for (uint32_t kt = 0; kt < n_col_tiles; ++kt) {
|
||||
const uint32_t valid = (uint32_t) hex_smin(32, kv_rows - kt * 32);
|
||||
HVX_Vector v_off = v_off_full;
|
||||
if (valid < 32) {
|
||||
uint16_t off_tail[64] __attribute__((aligned(128)));
|
||||
for (uint32_t c = 0; c < 32; ++c) {
|
||||
off_tail[c] = (c < valid) ? (uint16_t) (c * v_raw_row_padded) : 0;
|
||||
off_tail[c + 32] = 0;
|
||||
}
|
||||
v_off = *(const HVX_Vector *) off_tail;
|
||||
}
|
||||
Q6_vgather_ARMVh(&tile_dst_all[kt * 8 + 7], (size_t) (v_base + (size_t) kt * 32 * v_raw_row_padded), mu, v_off);
|
||||
}
|
||||
hvx_gather_sync(&tile_dst_all[(n_col_tiles - 1) * 8 + 7]);
|
||||
|
||||
const HVX_Vector v_abs_mask = Q6_Vh_vsplat_R(0x7fff);
|
||||
HVX_Vector v_max_sc = Q6_V_vzero();
|
||||
for (uint32_t kt = 0; kt < n_col_tiles; ++kt) {
|
||||
const uint32_t valid = (uint32_t) hex_smin(32, kv_rows - kt * 32);
|
||||
const HVX_VectorPred q_valid = Q6_Q_vsetq2_R(valid * sizeof(uint16_t));
|
||||
HVX_Vector v_sc = Q6_V_vmux_QVV(q_valid, tile_dst_all[kt * 8 + 7], Q6_V_vzero());
|
||||
tile_dst_all[kt * 8 + 7] = v_sc;
|
||||
v_max_sc = Q6_Vhf_vmax_VhfVhf(v_max_sc, Q6_V_vand_VV(v_sc, v_abs_mask));
|
||||
}
|
||||
v_max_sc = hvx_vec_reduce_max_f16(v_max_sc);
|
||||
|
||||
HVX_Vector v_max_sf, v_inv127_qf;
|
||||
int_hmx_inv127_from_max_f16(v_max_sc, &v_max_sf, &v_inv127_qf);
|
||||
|
||||
// Pass 2: 4-row vshuff interleave + per-KV-row int8 scale multiply -> S8 weight.b
|
||||
const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f);
|
||||
const HVX_Vector v12M_sf = Q6_V_vsplat_R(0x4B400000);
|
||||
const HVX_Vector v_ones_s8 = Q6_Vb_vsplat_R(1);
|
||||
HVX_Vector v_l1_w = Q6_V_vzero();
|
||||
|
||||
#pragma clang loop pipeline(disable) unroll(disable)
|
||||
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 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(v_sc_qf, v_inv127_qf), v12M_sf));
|
||||
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);
|
||||
|
||||
#pragma clang loop pipeline(disable) unroll(disable)
|
||||
for (uint32_t g = 0; g < 8; ++g) {
|
||||
const uint32_t k0 = kt * 32 + g * 4;
|
||||
const uint8_t * p0 = v_base + (size_t) k0 * v_raw_row_padded + 2;
|
||||
HVX_Vector r0 = (k0 + 0 < kv_rows) ? *(const HVX_UVector *) (p0 + 0 * v_raw_row_padded) : Q6_V_vzero();
|
||||
HVX_Vector r1 = (k0 + 1 < kv_rows) ? *(const HVX_UVector *) (p0 + 1 * v_raw_row_padded) : Q6_V_vzero();
|
||||
HVX_Vector r2 = (k0 + 2 < kv_rows) ? *(const HVX_UVector *) (p0 + 2 * v_raw_row_padded) : Q6_V_vzero();
|
||||
HVX_Vector r3 = (k0 + 3 < kv_rows) ? *(const HVX_UVector *) (p0 + 3 * v_raw_row_padded) : Q6_V_vzero();
|
||||
|
||||
HVX_Vector v02 = Q6_V_lo_W(Q6_W_vshuff_VVR(r2, r0, -1));
|
||||
HVX_Vector v13 = Q6_V_lo_W(Q6_W_vshuff_VVR(r3, r1, -1));
|
||||
HVX_Vector v_q = Q6_V_lo_W(Q6_W_vshuff_VVR(v13, v02, -1));
|
||||
|
||||
HVX_Vector v_m = Q6_V_vsplat_R(Q6_R_vextract_VR(v_m_b, (int) (g * 4)));
|
||||
HVX_VectorPair vp = Q6_Wh_vmpy_VbVb(v_q, v_m);
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_Vector v_W = Q6_Vb_vasr_VhVhR_rnd_sat(Q6_V_hi_W(vp), Q6_V_lo_W(vp), 7);
|
||||
#else
|
||||
HVX_Vector v_W = Q6_Vb_vround_VhVh_sat(Q6_Vh_vasl_VhR(Q6_V_hi_W(vp), 1),
|
||||
Q6_Vh_vasl_VhR(Q6_V_lo_W(vp), 1));
|
||||
#endif
|
||||
tile_dst[g] = v_W;
|
||||
v_l1_w = Q6_Vw_vrmpyacc_VwVubVb(v_l1_w, Q6_Vb_vabs_Vb(v_W), v_ones_s8);
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 3: emit zp=0 per-channel scales and HMX bias banks
|
||||
emit_int_hmx_scales_and_bias_zp0(v_max_sf, 128.0f / 127.0f, v_l1_w, dst_scales, bias_out);
|
||||
}
|
||||
|
||||
static inline void hmx_fa_w8a8_gemm_run(
|
||||
const uint8_t * restrict act_tiles,
|
||||
const uint8_t * restrict wt_tiles,
|
||||
const uint8_t * restrict wt_bias,
|
||||
uint8_t * restrict out_tiles,
|
||||
uint32_t n_row_tiles_64,
|
||||
uint32_t n_col_tiles,
|
||||
uint32_t n_dot_tiles) {
|
||||
|
||||
__builtin_assume(n_row_tiles_64 > 0);
|
||||
__builtin_assume(n_col_tiles > 0);
|
||||
__builtin_assume(n_dot_tiles > 0);
|
||||
|
||||
const uint32_t cvt_ctrl_hi = hmx_cvt_ctrl(0, 0, 0, 1);
|
||||
#if __HVX_ARCH__ >= 79
|
||||
const uint32_t cvt_ctrl_lo = hmx_cvt_ctrl(1, 0, 1, 0);
|
||||
#else
|
||||
const uint32_t cvt_ctrl_lo = hmx_cvt_ctrl(0, 0, 1, 0);
|
||||
#endif
|
||||
const uint32_t a_rng = hmx_act_sm_range(n_dot_tiles);
|
||||
const uint32_t w_rng = hmx_wt_b_range(n_dot_tiles * HMX_W8A8_WT_TILE_SIZE);
|
||||
const uint32_t out_cm_rng = hmx_out_cm_range();
|
||||
|
||||
for (uint32_t j = 0; j < n_col_tiles; ++j) {
|
||||
const uint8_t * wt_base = wt_tiles + (size_t) j * n_dot_tiles * HMX_W8A8_WT_TILE_SIZE;
|
||||
const uint8_t * bias_hi = wt_bias + ((size_t) j * 2 + 0) * HMX_W8A8_BIAS_BANK_SIZE;
|
||||
const uint8_t * bias_lo = wt_bias + ((size_t) j * 2 + 1) * HMX_W8A8_BIAS_BANK_SIZE;
|
||||
#if __HVX_ARCH__ >= 79
|
||||
hmx_load_bias_bank(bias_hi, 0);
|
||||
hmx_load_bias_bank(bias_lo, 1);
|
||||
#endif
|
||||
|
||||
for (uint32_t i = 0; i < n_row_tiles_64; ++i) {
|
||||
const uint8_t * act_base = act_tiles + (size_t) i * n_dot_tiles * HMX_W8A8_ACT_TILE_SIZE;
|
||||
const size_t out_idx = ((size_t) i * n_col_tiles + j) * 2;
|
||||
uint8_t * out_hi = out_tiles + (out_idx + 0) * HMX_W8A8_ACT_TILE_SIZE;
|
||||
uint8_t * out_lo = out_tiles + (out_idx + 1) * HMX_W8A8_ACT_TILE_SIZE;
|
||||
|
||||
asm volatile(HMX_CLRACC_INT());
|
||||
asm volatile(HMX_LOAD_MPY_DEEP_U8_S8_SM("%0", "%1", "%2", "%3")
|
||||
: : "r"(act_base), "r"(a_rng), "r"(wt_base), "r"(w_rng) : "memory");
|
||||
|
||||
#if __HVX_ARCH__ < 79
|
||||
hmx_load_bias_bank(bias_hi, 0);
|
||||
#endif
|
||||
asm volatile(HMX_CVT_UB_ACC("%0") : : "r"(cvt_ctrl_hi) : "memory");
|
||||
asm volatile(HMX_STORE_CVT_CM("%0", "%1") : : "r"(out_hi), "r"(out_cm_rng) : "memory");
|
||||
|
||||
#if __HVX_ARCH__ < 79
|
||||
hmx_load_bias_bank(bias_lo, 0);
|
||||
#endif
|
||||
asm volatile(HMX_CVT_UB_ACC("%0") : : "r"(cvt_ctrl_lo) : "memory");
|
||||
asm volatile(HMX_STORE_CVT_CM("%0", "%1") : : "r"(out_lo), "r"(out_cm_rng) : "memory");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
const uint8_t * q_tiles;
|
||||
const uint8_t * k_tiles;
|
||||
const uint8_t * k_bias;
|
||||
uint8_t * s_tiles;
|
||||
uint32_t n_row_tiles_64;
|
||||
uint32_t n_col_tiles;
|
||||
uint32_t n_dot_tiles; // DK / 32
|
||||
} hmx_fa_w8a8_qk_job_t;
|
||||
|
||||
static void hmx_fa_w8a8_qk_dot_worker(const htp_thread_t * th, void * data) {
|
||||
(void) th;
|
||||
hmx_fa_w8a8_qk_job_t * job = (hmx_fa_w8a8_qk_job_t *) data;
|
||||
hmx_fa_w8a8_gemm_run(job->q_tiles, job->k_tiles, job->k_bias, job->s_tiles,
|
||||
job->n_row_tiles_64, job->n_col_tiles, job->n_dot_tiles);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
const uint8_t * p_tiles;
|
||||
const uint8_t * v_tiles;
|
||||
const uint8_t * v_bias;
|
||||
uint8_t * do_tiles;
|
||||
uint32_t n_row_tiles_64;
|
||||
uint32_t n_v_tiles; // DV / 32
|
||||
uint32_t n_dot_tiles; // ceil(kv_rows / 32)
|
||||
} hmx_fa_w8a8_pv_job_t;
|
||||
|
||||
static void hmx_fa_w8a8_pv_worker(const htp_thread_t * th, void * data) {
|
||||
(void) th;
|
||||
hmx_fa_w8a8_pv_job_t * job = (hmx_fa_w8a8_pv_job_t *) data;
|
||||
hmx_fa_w8a8_gemm_run(job->p_tiles, job->v_tiles, job->v_bias, job->do_tiles,
|
||||
job->n_row_tiles_64, job->n_v_tiles, job->n_dot_tiles);
|
||||
}
|
||||
|
||||
#endif /* HMX_FA_W8A8_KERNELS_H */
|
||||
@@ -0,0 +1,209 @@
|
||||
#ifndef HMX_INT_UTILS_H
|
||||
#define HMX_INT_UTILS_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "hvx-utils.h"
|
||||
|
||||
#ifndef HMX_W8A8_ACT_TILE_SIZE
|
||||
#define HMX_W8A8_ACT_TILE_SIZE 2048
|
||||
#endif
|
||||
|
||||
static inline HVX_Vector hvx_vec_f32_to_qf32(HVX_Vector v) {
|
||||
#if __HVX_ARCH__ >= 81
|
||||
return Q6_Vqf32_equals_Vsf(v);
|
||||
#else
|
||||
return Q6_Vqf32_vadd_VsfVsf(v, Q6_V_vzero());
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline HVX_Vector hvx_qf32_mul_sf_scalar(HVX_Vector v_sf, uint32_t scalar_sf_bits) {
|
||||
#if __HVX_ARCH__ >= 79
|
||||
return Q6_Vqf32_vmpy_VsfRsf(v_sf, (int32_t) scalar_sf_bits);
|
||||
#else
|
||||
return Q6_Vqf32_vmpy_VsfVsf(v_sf, Q6_V_vsplat_R((int32_t) scalar_sf_bits));
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void emit_int_hmx_scales_and_bias(
|
||||
HVX_Vector v_max_sf,
|
||||
float scale_const,
|
||||
HVX_Vector v_sum_w,
|
||||
HVX_Vector v_l1_w,
|
||||
float * restrict dst_scales,
|
||||
HVX_Vector * restrict bias_out) {
|
||||
|
||||
// |sum_k (a - zp) * w_hat| < 128 * l1_w = (l1_w << 7) <= 32768 * 2^p
|
||||
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_ib = Q6_Vw_vsub_VwVw(Q6_Vw_vasl_VwVw(Q6_V_vsplat_R(32768), v_p), Q6_Vw_vasl_VwR(v_sum_w, 7));
|
||||
|
||||
HVX_Vector v_dw_base = hvx_vec_mul_f32_f32(v_max_sf, hvx_vec_splat_f32(scale_const));
|
||||
HVX_Vector v_pow2_sf = Q6_Vsf_equals_Vw(Q6_Vw_vasl_VwVw(Q6_V_vsplat_R(1), v_p));
|
||||
*(HVX_Vector *) dst_scales = hvx_vec_mul_f32_f32(v_dw_base, v_pow2_sf);
|
||||
|
||||
HVX_Vector v_exp_hi = Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(16), v_p);
|
||||
HVX_Vector v_exp_lo = Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(24), v_p);
|
||||
bias_out[0] = Q6_Vw_vasl_VwR(v_exp_hi, 10);
|
||||
bias_out[1] = v_ib;
|
||||
bias_out[2] = Q6_Vw_vasl_VwR(v_exp_lo, 10);
|
||||
bias_out[3] = v_ib;
|
||||
}
|
||||
|
||||
static inline void emit_int_hmx_scales_and_bias_zp0(
|
||||
HVX_Vector v_max_sf,
|
||||
float scale_const,
|
||||
HVX_Vector v_l1_w,
|
||||
float * restrict dst_scales,
|
||||
HVX_Vector * restrict bias_out) {
|
||||
|
||||
// Unsigned a_u8 in [0, 255] (zp = 0): |sum_k a_u8 * w_hat| <= 255 * l1_w < (l1_w << 8) <= 32768 * 2^p
|
||||
HVX_Vector v_max_mag = Q6_Vw_vasl_VwR(v_l1_w, 8);
|
||||
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_ib = Q6_Vw_vasl_VwVw(Q6_V_vsplat_R(32768), v_p);
|
||||
|
||||
HVX_Vector v_dw_base = hvx_vec_mul_f32_f32(v_max_sf, hvx_vec_splat_f32(scale_const));
|
||||
HVX_Vector v_pow2_sf = Q6_Vsf_equals_Vw(Q6_Vw_vasl_VwVw(Q6_V_vsplat_R(1), v_p));
|
||||
*(HVX_Vector *) dst_scales = hvx_vec_mul_f32_f32(v_dw_base, v_pow2_sf);
|
||||
|
||||
HVX_Vector v_exp_hi = Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(16), v_p);
|
||||
HVX_Vector v_exp_lo = Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(24), v_p);
|
||||
bias_out[0] = Q6_Vw_vasl_VwR(v_exp_hi, 10);
|
||||
bias_out[1] = v_ib;
|
||||
bias_out[2] = Q6_Vw_vasl_VwR(v_exp_lo, 10);
|
||||
bias_out[3] = v_ib;
|
||||
}
|
||||
|
||||
static inline HVX_Vector int_hmx_scale_qf_from_max_f32(HVX_Vector v_max_sf, float num) {
|
||||
HVX_VectorPred q_pos = Q6_Q_vcmp_gt_VsfVsf(v_max_sf, hvx_vec_splat_f32(1e-30f));
|
||||
HVX_Vector v_inv_raw = hvx_vec_inverse_f32(v_max_sf);
|
||||
HVX_Vector v_scale_sf = Q6_V_vmux_QVV(q_pos, hvx_vec_mul_f32_f32(v_inv_raw, hvx_vec_splat_f32(num)), Q6_V_vzero());
|
||||
return hvx_vec_f32_to_qf32(v_scale_sf);
|
||||
}
|
||||
|
||||
static inline HVX_Vector int_hmx_inv127_from_max_f32(HVX_Vector v_max_sf) {
|
||||
return int_hmx_scale_qf_from_max_f32(v_max_sf, 127.0f);
|
||||
}
|
||||
|
||||
static inline void int_hmx_inv127_from_max_f16(
|
||||
HVX_Vector v_max_sc,
|
||||
HVX_Vector * restrict v_max_sf_out,
|
||||
HVX_Vector * restrict v_inv127_qf_out) {
|
||||
const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f);
|
||||
HVX_Vector v_max_sf = Q6_Vsf_equals_Vqf32(Q6_V_lo_W(Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(v_max_sc), v_one_f16)));
|
||||
*v_max_sf_out = v_max_sf;
|
||||
*v_inv127_qf_out = int_hmx_inv127_from_max_f32(v_max_sf);
|
||||
}
|
||||
|
||||
static inline HVX_Vector int_hmx_m_b4_from_sc_qf(HVX_Vector v_sc_qf, HVX_Vector v_inv127_qf) {
|
||||
const HVX_Vector v12M_sf = Q6_V_vsplat_R(0x4B400000);
|
||||
HVX_Vector v_m_biased = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(v_sc_qf, v_inv127_qf), v12M_sf));
|
||||
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)));
|
||||
}
|
||||
|
||||
static inline HVX_Vector int_hmx_m_b4_from_f16(HVX_Vector v_sc, HVX_Vector v_inv127_qf) {
|
||||
const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f);
|
||||
HVX_Vector v_sc_qf = Q6_V_lo_W(Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(v_sc), v_one_f16));
|
||||
return int_hmx_m_b4_from_sc_qf(v_sc_qf, v_inv127_qf);
|
||||
}
|
||||
|
||||
static inline void int_hmx_m_b4_pair_from_f16(
|
||||
HVX_Vector v_sc,
|
||||
HVX_Vector v_inv127_qf,
|
||||
HVX_Vector * restrict v_m_b0_out,
|
||||
HVX_Vector * restrict v_m_b1_out) {
|
||||
const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f);
|
||||
HVX_VectorPair vp_sc_qf = Q6_Wqf32_vmpy_VhfVhf(Q6_Vh_vshuff_Vh(v_sc), v_one_f16);
|
||||
*v_m_b0_out = int_hmx_m_b4_from_sc_qf(Q6_V_lo_W(vp_sc_qf), v_inv127_qf);
|
||||
*v_m_b1_out = int_hmx_m_b4_from_sc_qf(Q6_V_hi_W(vp_sc_qf), v_inv127_qf);
|
||||
}
|
||||
|
||||
static inline void int_hmx_store_and_accum_pair(
|
||||
HVX_Vector * restrict tile_dst,
|
||||
int pair_idx,
|
||||
HVX_Vector v_W0,
|
||||
HVX_Vector v_W1,
|
||||
HVX_Vector * restrict v_sum_w,
|
||||
HVX_Vector * restrict v_l1_w) {
|
||||
|
||||
const HVX_Vector v_ones_s8 = Q6_Vb_vsplat_R(1);
|
||||
tile_dst[2 * pair_idx + 0] = v_W0;
|
||||
tile_dst[2 * pair_idx + 1] = v_W1;
|
||||
|
||||
*v_sum_w = Q6_Vw_vrmpyacc_VwVbVb(Q6_Vw_vrmpyacc_VwVbVb(*v_sum_w, v_W0, v_ones_s8), v_W1, v_ones_s8);
|
||||
*v_l1_w = Q6_Vw_vrmpyacc_VwVubVb(Q6_Vw_vrmpyacc_VwVubVb(*v_l1_w, Q6_Vb_vabs_Vb(v_W0), v_ones_s8), Q6_Vb_vabs_Vb(v_W1), v_ones_s8);
|
||||
}
|
||||
|
||||
static inline void int_hmx_rescale_pair_wb(
|
||||
HVX_Vector v_q0,
|
||||
HVX_Vector v_q1,
|
||||
HVX_Vector v_m_b4,
|
||||
HVX_Vector * restrict tile_dst,
|
||||
int pair_idx,
|
||||
HVX_Vector * restrict v_sum_w,
|
||||
HVX_Vector * restrict v_l1_w) {
|
||||
|
||||
HVX_Vector v_neg1_h = Q6_Vh_vsplat_R(-1);
|
||||
HVX_VectorPair vp_tie_adj = Q6_W_vcombine_VV(v_neg1_h, v_neg1_h);
|
||||
HVX_VectorPair vp_p01 = Q6_Wh_vmpy_VbVb(v_q0, v_m_b4);
|
||||
HVX_VectorPair vp_p23 = Q6_Wh_vmpyacc_WhVbVb(vp_tie_adj, v_q1, v_m_b4);
|
||||
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_Vector v_W0 = Q6_Vb_vasr_VhVhR_rnd_sat(Q6_V_hi_W(vp_p01), Q6_V_lo_W(vp_p01), 7);
|
||||
HVX_Vector v_W1 = Q6_Vb_vasr_VhVhR_rnd_sat(Q6_V_hi_W(vp_p23), Q6_V_lo_W(vp_p23), 7);
|
||||
#else
|
||||
HVX_Vector v_W0 = Q6_Vb_vround_VhVh_sat(Q6_Vh_vasl_VhR(Q6_V_hi_W(vp_p01), 1),
|
||||
Q6_Vh_vasl_VhR(Q6_V_lo_W(vp_p01), 1));
|
||||
HVX_Vector v_W1 = Q6_Vb_vround_VhVh_sat(Q6_Vh_vasl_VhR(Q6_V_hi_W(vp_p23), 1),
|
||||
Q6_Vh_vasl_VhR(Q6_V_lo_W(vp_p23), 1));
|
||||
#endif
|
||||
int_hmx_store_and_accum_pair(tile_dst, pair_idx, v_W0, v_W1, v_sum_w, v_l1_w);
|
||||
}
|
||||
|
||||
#if __HVX_ARCH__ >= 81
|
||||
static inline void pack_hf_quad_to_u8_sm_pair(
|
||||
uint8_t * restrict tile_base,
|
||||
uint32_t kt,
|
||||
uint32_t rg,
|
||||
HVX_Vector hf0,
|
||||
HVX_Vector hf1,
|
||||
HVX_Vector hf2,
|
||||
HVX_Vector hf3) {
|
||||
const HVX_Vector v_zp_u8 = Q6_Vb_vsplat_R(0x80);
|
||||
const HVX_Vector v_min_u8b = Q6_Vb_vsplat_R(1);
|
||||
|
||||
HVX_Vector vb02 = Q6_Vb_vshuff_Vb(Q6_Vb_vpack_VhVh_sat(Q6_Vh_equals_Vhf_rnd(hf2), Q6_Vh_equals_Vhf_rnd(hf0)));
|
||||
HVX_Vector vb13 = Q6_Vb_vshuff_Vb(Q6_Vb_vpack_VhVh_sat(Q6_Vh_equals_Vhf_rnd(hf3), Q6_Vh_equals_Vhf_rnd(hf1)));
|
||||
HVX_VectorPair w_u8 = Q6_W_vshuff_VVR(vb13, vb02, -1);
|
||||
|
||||
((HVX_Vector *) (tile_base + (size_t) (kt + 0) * HMX_W8A8_ACT_TILE_SIZE))[rg] =
|
||||
Q6_Vub_vmax_VubVub(Q6_V_vxor_VV(Q6_V_lo_W(w_u8), v_zp_u8), v_min_u8b);
|
||||
((HVX_Vector *) (tile_base + (size_t) (kt + 1) * HMX_W8A8_ACT_TILE_SIZE))[rg] =
|
||||
Q6_Vub_vmax_VubVub(Q6_V_vxor_VV(Q6_V_hi_W(w_u8), v_zp_u8), v_min_u8b);
|
||||
}
|
||||
|
||||
static inline void hmx_pack_hf_quad_to_u8_cm_pair(
|
||||
uint8_t * restrict tile_base,
|
||||
uint32_t kt,
|
||||
uint32_t rg,
|
||||
HVX_Vector hf0,
|
||||
HVX_Vector hf1,
|
||||
HVX_Vector hf2,
|
||||
HVX_Vector hf3) {
|
||||
const HVX_Vector v_zp_u8 = Q6_Vb_vsplat_R(0x80);
|
||||
const HVX_Vector v_min_u8b = Q6_Vb_vsplat_R(1);
|
||||
|
||||
HVX_Vector vb01 = Q6_Vb_vpack_VhVh_sat(Q6_Vh_equals_Vhf_rnd(hf1), Q6_Vh_equals_Vhf_rnd(hf0));
|
||||
HVX_Vector vb23 = Q6_Vb_vpack_VhVh_sat(Q6_Vh_equals_Vhf_rnd(hf3), Q6_Vh_equals_Vhf_rnd(hf2));
|
||||
HVX_VectorPair w_u8 = Q6_W_vdeal_VVR(vb23, vb01, -32);
|
||||
|
||||
((HVX_Vector *) (tile_base + (size_t) (kt + 0) * HMX_W8A8_ACT_TILE_SIZE))[rg] =
|
||||
Q6_Vub_vmax_VubVub(Q6_V_vxor_VV(Q6_V_lo_W(w_u8), v_zp_u8), v_min_u8b);
|
||||
((HVX_Vector *) (tile_base + (size_t) (kt + 1) * HMX_W8A8_ACT_TILE_SIZE))[rg] =
|
||||
Q6_Vub_vmax_VubVub(Q6_V_vxor_VV(Q6_V_hi_W(w_u8), v_zp_u8), v_min_u8b);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HMX_INT_UTILS_H */
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,3 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -14,49 +12,59 @@
|
||||
|
||||
#define QURT_LOWEST_PRIO (254)
|
||||
|
||||
static inline void hmx_lock(hmx_queue_t q)
|
||||
{
|
||||
static inline void hmx_lock(hmx_queue_t q) {
|
||||
if (!q->hmx_locked) {
|
||||
HAP_compute_res_hmx_lock(q->hap_rctx);
|
||||
q->hmx_locked = true;
|
||||
if (HAP_compute_res_hmx_lock(q->hap_rctx) == 0) {
|
||||
q->hmx_locked = true;
|
||||
} else {
|
||||
FARF(ERROR, "hmx-queue: HAP_compute_res_hmx_lock failed\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hmx_unlock(hmx_queue_t q)
|
||||
{
|
||||
static inline void hmx_unlock(hmx_queue_t q) {
|
||||
if (q->hmx_locked) {
|
||||
HAP_compute_res_hmx_unlock(q->hap_rctx);
|
||||
q->hmx_locked = false;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hmx_queue_process(hmx_queue_t q, bool* killed) {
|
||||
unsigned int ir = atomic_load(&q->idx_read);
|
||||
static inline void hmx_queue_process(hmx_queue_t q, bool * killed) {
|
||||
uint32_t ir = atomic_load_explicit(&q->idx_read, memory_order_relaxed);
|
||||
|
||||
while (ir != atomic_load(&q->idx_write)) {
|
||||
struct hmx_queue_desc *d = &q->desc[ir];
|
||||
if (!d->done) {
|
||||
FARF(HIGH, "hmx-queue-process: ir %u func %p data %p", ir, d->func, d->data);
|
||||
while (ir != atomic_load_explicit(&q->idx_write, memory_order_acquire)) {
|
||||
struct hmx_queue_desc * d = &q->desc[ir & q->idx_mask];
|
||||
|
||||
uintptr_t sig = (uintptr_t) d->func;
|
||||
switch (sig) {
|
||||
case HMX_QUEUE_NOOP: /* noop */; break;
|
||||
case HMX_QUEUE_KILL: *killed = true; break;
|
||||
case HMX_QUEUE_SUSPEND: hmx_unlock(q); break;
|
||||
case HMX_QUEUE_WAKEUP: hmx_lock(q); break;
|
||||
default:
|
||||
hmx_lock(q);
|
||||
htp_trace_event_start(q->th.trace, HTP_TRACE_EVT_HMX_COMP, ir);
|
||||
d->func(&q->th, d->data);
|
||||
htp_trace_event_stop(q->th.trace, HTP_TRACE_EVT_HMX_COMP, ir);
|
||||
break;
|
||||
}
|
||||
FARF(HIGH, "hmx-queue-process: ir %u func %p data %p", ir, d->func, d->data);
|
||||
|
||||
atomic_fetch_add(&d->done, 1);
|
||||
uintptr_t sig = (uintptr_t) d->func;
|
||||
switch (sig) {
|
||||
case HMX_QUEUE_KILL:
|
||||
hmx_unlock(q);
|
||||
*killed = true;
|
||||
break;
|
||||
case HMX_QUEUE_SUSPEND:
|
||||
hmx_unlock(q);
|
||||
break;
|
||||
case HMX_QUEUE_WAKEUP:
|
||||
hmx_lock(q);
|
||||
break;
|
||||
default:
|
||||
hmx_lock(q);
|
||||
htp_trace_event_start(q->th.trace, HTP_TRACE_EVT_HMX_COMP, ir);
|
||||
d->func(&q->th, d->data);
|
||||
// drain HMX VTCM stores before signaling completion to HVX workers
|
||||
asm volatile("syncht" ::: "memory");
|
||||
htp_trace_event_stop(q->th.trace, HTP_TRACE_EVT_HMX_COMP, ir);
|
||||
break;
|
||||
}
|
||||
|
||||
ir = (ir + 1) & q->idx_mask;
|
||||
atomic_store(&q->idx_read, ir);
|
||||
ir++;
|
||||
atomic_store_explicit(&q->idx_read, ir, memory_order_release);
|
||||
|
||||
if (*killed) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,13 +78,16 @@ static void hmx_queue_thread(void * arg) {
|
||||
unsigned int poll_cnt = HMX_QUEUE_POLL_COUNT;
|
||||
unsigned int prev_seqn = 0;
|
||||
while (!killed) {
|
||||
unsigned int seqn = atomic_load(&q->seqn);
|
||||
unsigned int seqn = atomic_load_explicit(&q->seqn, memory_order_acquire);
|
||||
if (seqn == prev_seqn) {
|
||||
// drop HVX context while spinning
|
||||
if (poll_cnt > 1 && poll_cnt == HMX_QUEUE_POLL_COUNT) {
|
||||
// drop HVX context before spinning/sleeping
|
||||
if (poll_cnt == HMX_QUEUE_POLL_COUNT) {
|
||||
qurt_hvx_unlock();
|
||||
}
|
||||
if (--poll_cnt) { hex_pause(); continue; }
|
||||
if (--poll_cnt) {
|
||||
hex_pause();
|
||||
continue;
|
||||
}
|
||||
FARF(HIGH, "hmx-queue-thread: sleeping");
|
||||
qurt_futex_wait(&q->seqn, prev_seqn);
|
||||
poll_cnt = HMX_QUEUE_POLL_COUNT;
|
||||
@@ -94,9 +105,9 @@ static void hmx_queue_thread(void * arg) {
|
||||
}
|
||||
|
||||
size_t hmx_queue_sizeof(size_t capacity, uint32_t stack_size) {
|
||||
capacity = hex_ceil_pow2(capacity);
|
||||
size_t size_q = hex_align_up(sizeof(struct hmx_queue_s), HEX_L2_LINE_SIZE);
|
||||
size_t size_desc = hex_align_up(capacity * sizeof(struct hmx_queue_desc), HEX_L2_LINE_SIZE);
|
||||
capacity = hex_ceil_pow2(capacity > 0 ? capacity : 1);
|
||||
size_t size_q = hex_align_up(sizeof(struct hmx_queue_s), HEX_L2_LINE_SIZE);
|
||||
size_t size_desc = hex_align_up(capacity * sizeof(struct hmx_queue_desc), HEX_L2_LINE_SIZE);
|
||||
size_t size_stack = stack_size;
|
||||
return size_q + size_desc + size_stack;
|
||||
}
|
||||
@@ -106,21 +117,26 @@ size_t hmx_queue_alignof(void) {
|
||||
}
|
||||
|
||||
hmx_queue_t hmx_queue_init(void * ptr, size_t capacity, uint32_t stack_size, uint32_t hap_rctx, dma_queue_t * dma, struct htp_thread_trace * trace) {
|
||||
capacity = hex_ceil_pow2(capacity);
|
||||
size_t size_q = hex_align_up(sizeof(struct hmx_queue_s), HEX_L2_LINE_SIZE);
|
||||
capacity = hex_ceil_pow2(capacity > 0 ? capacity : 1);
|
||||
size_t size_q = hex_align_up(sizeof(struct hmx_queue_s), HEX_L2_LINE_SIZE);
|
||||
size_t size_desc = hex_align_up(capacity * sizeof(struct hmx_queue_desc), HEX_L2_LINE_SIZE);
|
||||
|
||||
uint8_t * block = (uint8_t *) ptr;
|
||||
|
||||
hmx_queue_t q = (hmx_queue_t) block; block += size_q;
|
||||
hmx_queue_t q = (hmx_queue_t) block;
|
||||
block += size_q;
|
||||
memset(q, 0, sizeof(struct hmx_queue_s));
|
||||
|
||||
q->capacity = capacity;
|
||||
q->idx_mask = capacity - 1;
|
||||
q->idx_pop = 0;
|
||||
q->hap_rctx = hap_rctx;
|
||||
q->external_mem = true;
|
||||
atomic_init(&q->idx_write, 0);
|
||||
atomic_init(&q->idx_read, 0);
|
||||
atomic_init(&q->seqn, 0);
|
||||
|
||||
q->desc = (struct hmx_queue_desc *) block; block += size_desc;
|
||||
q->desc = (struct hmx_queue_desc *) block;
|
||||
block += size_desc;
|
||||
memset(q->desc, 0, capacity * sizeof(struct hmx_queue_desc));
|
||||
|
||||
q->stack = block;
|
||||
@@ -128,7 +144,7 @@ hmx_queue_t hmx_queue_init(void * ptr, size_t capacity, uint32_t stack_size, uin
|
||||
|
||||
htp_thread_init(&q->th, dma, trace);
|
||||
|
||||
// Match caller thread priority (same pattern as worker-pool.c).
|
||||
// Match caller thread priority (same pattern as work-queue.c).
|
||||
int prio = qurt_thread_get_priority(qurt_thread_get_id());
|
||||
if (prio < 1) {
|
||||
prio = 1;
|
||||
|
||||
@@ -26,35 +26,33 @@ extern "C" {
|
||||
|
||||
typedef void (*hmx_queue_func)(const htp_thread_t * th, void *);
|
||||
|
||||
// Dummy funcs used as signals
|
||||
// Dummy funcs used as signals (non-zero so NULL remains the empty sentinel)
|
||||
enum hmx_queue_signal {
|
||||
HMX_QUEUE_NOOP = 0, // aka NULL
|
||||
HMX_QUEUE_WAKEUP,
|
||||
HMX_QUEUE_SUSPEND,
|
||||
HMX_QUEUE_KILL
|
||||
HMX_QUEUE_WAKEUP = 1,
|
||||
HMX_QUEUE_SUSPEND = 2,
|
||||
HMX_QUEUE_KILL = 3,
|
||||
};
|
||||
|
||||
struct hmx_queue_desc {
|
||||
hmx_queue_func func;
|
||||
void * data;
|
||||
atomic_uint done;
|
||||
hmx_queue_func func;
|
||||
void * data;
|
||||
};
|
||||
|
||||
struct hmx_queue_s {
|
||||
struct hmx_queue_desc * desc;
|
||||
atomic_uint idx_write; // updated by producer (push)
|
||||
atomic_uint idx_read; // updated by consumer (process)
|
||||
unsigned int idx_pop; // updated by producer (pop)
|
||||
uint32_t idx_mask;
|
||||
uint32_t capacity;
|
||||
atomic_uint idx_write; // monotonic, updated by producer (push)
|
||||
uint32_t idx_pop; // monotonic, updated by producer (pop)
|
||||
uint32_t idx_mask;
|
||||
uint32_t capacity;
|
||||
atomic_uint seqn; // incremented for all pushes, used with futex
|
||||
|
||||
atomic_uint seqn; // incremented for all pushes, used with futex
|
||||
qurt_thread_t thread;
|
||||
htp_thread_t th;
|
||||
void * stack;
|
||||
uint32_t hap_rctx;
|
||||
bool hmx_locked;
|
||||
bool external_mem; // memory owned externally
|
||||
atomic_uint idx_read __attribute__((aligned(HEX_L2_LINE_SIZE))); // monotonic, updated by consumer
|
||||
bool hmx_locked;
|
||||
|
||||
qurt_thread_t thread;
|
||||
htp_thread_t th;
|
||||
void * stack;
|
||||
uint32_t hap_rctx;
|
||||
};
|
||||
|
||||
typedef struct hmx_queue_s * hmx_queue_t;
|
||||
@@ -70,37 +68,37 @@ static inline struct hmx_queue_desc hmx_queue_make_desc(hmx_queue_func func, voi
|
||||
}
|
||||
|
||||
static inline bool hmx_queue_push(hmx_queue_t q, struct hmx_queue_desc d) {
|
||||
unsigned int ir = atomic_load(&q->idx_read);
|
||||
unsigned int iw = atomic_load(&q->idx_write);
|
||||
uint32_t iw = atomic_load_explicit(&q->idx_write, memory_order_relaxed);
|
||||
|
||||
if (((iw + 1) & q->idx_mask) == ir) {
|
||||
if ((iw - q->idx_pop) >= q->capacity) {
|
||||
FARF(HIGH, "hmx-queue-push: queue is full\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
atomic_store(&d.done, 0);
|
||||
|
||||
FARF(HIGH, "hmx-queue-push: iw %u func %p data %p\n", iw, d.func, d.data);
|
||||
|
||||
q->desc[iw] = d;
|
||||
atomic_store(&q->idx_write, (iw + 1) & q->idx_mask);
|
||||
q->desc[iw & q->idx_mask] = d;
|
||||
atomic_store_explicit(&q->idx_write, iw + 1, memory_order_release);
|
||||
|
||||
// wake up our thread
|
||||
atomic_fetch_add(&q->seqn, 1);
|
||||
unsigned int seqn = atomic_load_explicit(&q->seqn, memory_order_relaxed) + 1;
|
||||
atomic_store_explicit(&q->seqn, seqn, memory_order_release);
|
||||
qurt_futex_wake(&q->seqn, 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool hmx_queue_signal(hmx_queue_t q, enum hmx_queue_signal sig) {
|
||||
return hmx_queue_push(q, hmx_queue_make_desc((hmx_queue_func) sig, NULL));
|
||||
return hmx_queue_push(q, hmx_queue_make_desc((hmx_queue_func) (uintptr_t) sig, NULL));
|
||||
}
|
||||
|
||||
static inline bool hmx_queue_empty(hmx_queue_t q) {
|
||||
return q->idx_pop == atomic_load(&q->idx_write);
|
||||
return q->idx_pop == atomic_load_explicit(&q->idx_write, memory_order_relaxed);
|
||||
}
|
||||
|
||||
static inline uint32_t hmx_queue_depth(hmx_queue_t q) {
|
||||
return (atomic_load(&q->idx_write) - atomic_load(&q->idx_read)) & q->idx_mask;
|
||||
return atomic_load_explicit(&q->idx_write, memory_order_relaxed) -
|
||||
atomic_load_explicit(&q->idx_read, memory_order_relaxed);
|
||||
}
|
||||
|
||||
static inline uint32_t hmx_queue_capacity(hmx_queue_t q) {
|
||||
@@ -108,23 +106,23 @@ static inline uint32_t hmx_queue_capacity(hmx_queue_t q) {
|
||||
}
|
||||
|
||||
static inline struct hmx_queue_desc hmx_queue_pop_one(hmx_queue_t q) {
|
||||
unsigned int ip = q->idx_pop;
|
||||
unsigned int iw = atomic_load(&q->idx_write);
|
||||
uint32_t ip = q->idx_pop;
|
||||
uint32_t iw = atomic_load_explicit(&q->idx_write, memory_order_relaxed);
|
||||
|
||||
struct hmx_queue_desc rd = { NULL, NULL };
|
||||
if (ip == iw) {
|
||||
return rd;
|
||||
}
|
||||
|
||||
// Wait for desc to complete
|
||||
struct hmx_queue_desc * d = &q->desc[ip];
|
||||
while (!atomic_load(&d->done)) {
|
||||
// Wait for consumer to finish slot ip
|
||||
while ((int32_t) (atomic_load_explicit(&q->idx_read, memory_order_relaxed) - ip) <= 0) {
|
||||
FARF(HIGH, "hmx-queue-pop: waiting for HMX queue : %u\n", ip);
|
||||
hex_pause();
|
||||
}
|
||||
atomic_thread_fence(memory_order_acquire);
|
||||
|
||||
rd = *d;
|
||||
q->idx_pop = (ip + 1) & q->idx_mask;
|
||||
rd = q->desc[ip & q->idx_mask];
|
||||
q->idx_pop = ip + 1;
|
||||
|
||||
FARF(HIGH, "hmx-queue-pop: ip %u func %p data %p\n", ip, rd.func, rd.data);
|
||||
return rd;
|
||||
@@ -134,9 +132,10 @@ static inline struct hmx_queue_desc hmx_queue_pop(hmx_queue_t q) {
|
||||
while (1) {
|
||||
struct hmx_queue_desc d = hmx_queue_pop_one(q);
|
||||
|
||||
uint32_t sig = (uint32_t) d.func;
|
||||
if (sig && sig <= HMX_QUEUE_KILL)
|
||||
uintptr_t sig = (uintptr_t) d.func;
|
||||
if (sig && sig <= HMX_QUEUE_KILL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,120 @@ static const int32_t hmx_transpose_scatter_offsets[32] __attribute__((aligned(VL
|
||||
22 * 128, 23 * 128, 24 * 128, 25 * 128, 26 * 128, 27 * 128, 28 * 128, 29 * 128, 30 * 128, 31 * 128,
|
||||
};
|
||||
|
||||
// 5-stage vshuffvdd transpose of a 32x64 FP16 block into two contiguous 32x32 HMX tiles.
|
||||
// Stages 1-3 (-4, -8, -16) mix groups of 8 rows into a 4KB stack scratch; stages 4-5 (-32, -64)
|
||||
// Stage in-place in dst_tile0/dst_tile1 (each 4-vector slot holds g=0..3), then
|
||||
// combine across the 4 groups in-place with zero stack scratch.
|
||||
static inline void hmx_transpose_k_pair_tiles_vshuff(HVX_Vector * restrict dst_tile0,
|
||||
HVX_Vector * restrict dst_tile1,
|
||||
const uint8_t * restrict src_base,
|
||||
size_t row_byte_stride,
|
||||
uint32_t valid_rows) {
|
||||
if (valid_rows >= HMX_FP16_TILE_N_ROWS) {
|
||||
for (uint32_t g = 0; g < 4; ++g) {
|
||||
const uint8_t * p = src_base + (size_t) (8 * g) * row_byte_stride;
|
||||
HVX_Vector r0 = hvx_vmem(p); p += row_byte_stride;
|
||||
HVX_Vector r1 = hvx_vmem(p); p += row_byte_stride;
|
||||
HVX_Vector r2 = hvx_vmem(p); p += row_byte_stride;
|
||||
HVX_Vector r3 = hvx_vmem(p); p += row_byte_stride;
|
||||
HVX_Vector r4 = hvx_vmem(p); p += row_byte_stride;
|
||||
HVX_Vector r5 = hvx_vmem(p); p += row_byte_stride;
|
||||
HVX_Vector r6 = hvx_vmem(p); p += row_byte_stride;
|
||||
HVX_Vector r7 = hvx_vmem(p);
|
||||
|
||||
HVX_VectorPair p01 = Q6_W_vshuff_VVR(r1, r0, -4);
|
||||
HVX_VectorPair p23 = Q6_W_vshuff_VVR(r3, r2, -4);
|
||||
HVX_VectorPair p45 = Q6_W_vshuff_VVR(r5, r4, -4);
|
||||
HVX_VectorPair p67 = Q6_W_vshuff_VVR(r7, r6, -4);
|
||||
r0 = Q6_V_lo_W(p01); r1 = Q6_V_hi_W(p01);
|
||||
r2 = Q6_V_lo_W(p23); r3 = Q6_V_hi_W(p23);
|
||||
r4 = Q6_V_lo_W(p45); r5 = Q6_V_hi_W(p45);
|
||||
r6 = Q6_V_lo_W(p67); r7 = Q6_V_hi_W(p67);
|
||||
|
||||
HVX_VectorPair p02 = Q6_W_vshuff_VVR(r2, r0, -8);
|
||||
HVX_VectorPair p13 = Q6_W_vshuff_VVR(r3, r1, -8);
|
||||
HVX_VectorPair p46 = Q6_W_vshuff_VVR(r6, r4, -8);
|
||||
HVX_VectorPair p57 = Q6_W_vshuff_VVR(r7, r5, -8);
|
||||
r0 = Q6_V_lo_W(p02); r2 = Q6_V_hi_W(p02);
|
||||
r1 = Q6_V_lo_W(p13); r3 = Q6_V_hi_W(p13);
|
||||
r4 = Q6_V_lo_W(p46); r6 = Q6_V_hi_W(p46);
|
||||
r5 = Q6_V_lo_W(p57); r7 = Q6_V_hi_W(p57);
|
||||
|
||||
HVX_VectorPair p04 = Q6_W_vshuff_VVR(r4, r0, -16);
|
||||
HVX_VectorPair p15 = Q6_W_vshuff_VVR(r5, r1, -16);
|
||||
HVX_VectorPair p26 = Q6_W_vshuff_VVR(r6, r2, -16);
|
||||
HVX_VectorPair p37 = Q6_W_vshuff_VVR(r7, r3, -16);
|
||||
|
||||
dst_tile0[ 0 + g] = Q6_V_lo_W(p04); dst_tile0[ 4 + g] = Q6_V_hi_W(p04);
|
||||
dst_tile1[ 0 + g] = Q6_V_lo_W(p15); dst_tile1[ 4 + g] = Q6_V_hi_W(p15);
|
||||
dst_tile0[ 8 + g] = Q6_V_lo_W(p26); dst_tile0[12 + g] = Q6_V_hi_W(p26);
|
||||
dst_tile1[ 8 + g] = Q6_V_lo_W(p37); dst_tile1[12 + g] = Q6_V_hi_W(p37);
|
||||
}
|
||||
} else {
|
||||
const HVX_Vector vzero = Q6_V_vzero();
|
||||
for (uint32_t g = 0; g < 4; ++g) {
|
||||
const uint32_t base_r = 8 * g;
|
||||
const uint8_t * p = src_base + (size_t) base_r * row_byte_stride;
|
||||
HVX_Vector r0 = (base_r + 0 < valid_rows) ? hvx_vmem(p + 0 * row_byte_stride) : vzero;
|
||||
HVX_Vector r1 = (base_r + 1 < valid_rows) ? hvx_vmem(p + 1 * row_byte_stride) : vzero;
|
||||
HVX_Vector r2 = (base_r + 2 < valid_rows) ? hvx_vmem(p + 2 * row_byte_stride) : vzero;
|
||||
HVX_Vector r3 = (base_r + 3 < valid_rows) ? hvx_vmem(p + 3 * row_byte_stride) : vzero;
|
||||
HVX_Vector r4 = (base_r + 4 < valid_rows) ? hvx_vmem(p + 4 * row_byte_stride) : vzero;
|
||||
HVX_Vector r5 = (base_r + 5 < valid_rows) ? hvx_vmem(p + 5 * row_byte_stride) : vzero;
|
||||
HVX_Vector r6 = (base_r + 6 < valid_rows) ? hvx_vmem(p + 6 * row_byte_stride) : vzero;
|
||||
HVX_Vector r7 = (base_r + 7 < valid_rows) ? hvx_vmem(p + 7 * row_byte_stride) : vzero;
|
||||
|
||||
HVX_VectorPair p01 = Q6_W_vshuff_VVR(r1, r0, -4);
|
||||
HVX_VectorPair p23 = Q6_W_vshuff_VVR(r3, r2, -4);
|
||||
HVX_VectorPair p45 = Q6_W_vshuff_VVR(r5, r4, -4);
|
||||
HVX_VectorPair p67 = Q6_W_vshuff_VVR(r7, r6, -4);
|
||||
r0 = Q6_V_lo_W(p01); r1 = Q6_V_hi_W(p01);
|
||||
r2 = Q6_V_lo_W(p23); r3 = Q6_V_hi_W(p23);
|
||||
r4 = Q6_V_lo_W(p45); r5 = Q6_V_hi_W(p45);
|
||||
r6 = Q6_V_lo_W(p67); r7 = Q6_V_hi_W(p67);
|
||||
|
||||
HVX_VectorPair p02 = Q6_W_vshuff_VVR(r2, r0, -8);
|
||||
HVX_VectorPair p13 = Q6_W_vshuff_VVR(r3, r1, -8);
|
||||
HVX_VectorPair p46 = Q6_W_vshuff_VVR(r6, r4, -8);
|
||||
HVX_VectorPair p57 = Q6_W_vshuff_VVR(r7, r5, -8);
|
||||
r0 = Q6_V_lo_W(p02); r2 = Q6_V_hi_W(p02);
|
||||
r1 = Q6_V_lo_W(p13); r3 = Q6_V_hi_W(p13);
|
||||
r4 = Q6_V_lo_W(p46); r6 = Q6_V_hi_W(p46);
|
||||
r5 = Q6_V_lo_W(p57); r7 = Q6_V_hi_W(p57);
|
||||
|
||||
HVX_VectorPair p04 = Q6_W_vshuff_VVR(r4, r0, -16);
|
||||
HVX_VectorPair p15 = Q6_W_vshuff_VVR(r5, r1, -16);
|
||||
HVX_VectorPair p26 = Q6_W_vshuff_VVR(r6, r2, -16);
|
||||
HVX_VectorPair p37 = Q6_W_vshuff_VVR(r7, r3, -16);
|
||||
|
||||
dst_tile0[ 0 + g] = Q6_V_lo_W(p04); dst_tile0[ 4 + g] = Q6_V_hi_W(p04);
|
||||
dst_tile1[ 0 + g] = Q6_V_lo_W(p15); dst_tile1[ 4 + g] = Q6_V_hi_W(p15);
|
||||
dst_tile0[ 8 + g] = Q6_V_lo_W(p26); dst_tile0[12 + g] = Q6_V_hi_W(p26);
|
||||
dst_tile1[ 8 + g] = Q6_V_lo_W(p37); dst_tile1[12 + g] = Q6_V_hi_W(p37);
|
||||
}
|
||||
}
|
||||
|
||||
for (uint32_t t = 0; t < 2; ++t) {
|
||||
HVX_Vector * dst = t ? dst_tile1 : dst_tile0;
|
||||
for (uint32_t p0 = 0; p0 < 16; p0 += 4) {
|
||||
HVX_Vector a = dst[p0 + 0];
|
||||
HVX_Vector b = dst[p0 + 1];
|
||||
HVX_Vector c = dst[p0 + 2];
|
||||
HVX_Vector d = dst[p0 + 3];
|
||||
|
||||
HVX_VectorPair pab = Q6_W_vshuff_VVR(b, a, -32);
|
||||
HVX_VectorPair pcd = Q6_W_vshuff_VVR(d, c, -32);
|
||||
HVX_VectorPair out0 = Q6_W_vshuff_VVR(Q6_V_lo_W(pcd), Q6_V_lo_W(pab), -64);
|
||||
HVX_VectorPair out1 = Q6_W_vshuff_VVR(Q6_V_hi_W(pcd), Q6_V_hi_W(pab), -64);
|
||||
|
||||
dst[p0 + 0] = Q6_V_lo_W(out0);
|
||||
dst[p0 + 1] = Q6_V_hi_W(out0);
|
||||
dst[p0 + 2] = Q6_V_lo_W(out1);
|
||||
dst[p0 + 3] = Q6_V_hi_W(out1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Scatter row-major FP16 data (in VTCM scratch) into transposed [K][N] tiles.
|
||||
// vtcm_src: [n_cols][src_stride] row-major fp16 (only first k elements per row are used)
|
||||
// vtcm_dst: [n_col_tiles][n_k_tiles][HMX_FP16_TILE_N_ELMS] tile-major interleaved fp16
|
||||
@@ -50,18 +164,36 @@ static inline void hmx_interleave_rows_to_tiles(__fp16 * restrict vtcm_dst,
|
||||
assert(k % HMX_FP16_TILE_N_COLS == 0);
|
||||
|
||||
const uint32_t n_k_tiles = k / HMX_FP16_TILE_N_COLS;
|
||||
const bool pair_scatter = (n_k_tiles & 1) == 0;
|
||||
__builtin_assume(k > 0);
|
||||
__builtin_assume(end_row > start_row);
|
||||
|
||||
if (pair_scatter && ((start_row & 31) == 0) && ((end_row & 31) == 0) &&
|
||||
hex_is_aligned(vtcm_src, 128) && ((src_stride & 63) == 0)) {
|
||||
const uint32_t n_c_iters = n_k_tiles >> 1;
|
||||
const size_t row_byte_stride = src_stride * sizeof(__fp16);
|
||||
const size_t tile_pair_vecs = 2 * (HMX_FP16_TILE_SIZE / sizeof(HVX_Vector));
|
||||
|
||||
for (uint32_t r_base = start_row; r_base < end_row; r_base += HMX_FP16_TILE_N_ROWS) {
|
||||
const uint32_t ct = r_base / HMX_FP16_TILE_N_ROWS;
|
||||
const uint32_t valid_rows = (r_base < n_cols) ? (n_cols - r_base) : 0;
|
||||
HVX_Vector * tile_base = (HVX_Vector *) (vtcm_dst + (size_t) ct * n_k_tiles * HMX_FP16_TILE_N_ELMS);
|
||||
const uint8_t * row_src = (const uint8_t *) (vtcm_src + (size_t) r_base * src_stride);
|
||||
|
||||
for (uint32_t i = 0; i < n_c_iters; ++i) {
|
||||
hmx_transpose_k_pair_tiles_vshuff(tile_base, tile_base + 16, row_src + (size_t) i * 128,
|
||||
row_byte_stride, valid_rows);
|
||||
tile_base += tile_pair_vecs;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const HVX_Vector v_scat_base = hvx_vmem(hmx_transpose_scatter_offsets);
|
||||
const HVX_Vector v_scat_step = Q6_V_vsplat_R(4);
|
||||
const HVX_VectorPred q_mask64 = Q6_Q_vsetq_R(64);
|
||||
// Each hvx_vmemu load brings 64 fp16 = 128 bytes covering 2 adjacent K-tiles.
|
||||
// When n_k_tiles is even, scatter into 2 K-tiles per call (region 4095, no mask)
|
||||
// using the upper half of hmx_transpose_scatter_offsets. Tail one K-tile (when
|
||||
// n_k_tiles is odd) falls back to single-tile masked scatter.
|
||||
const bool pair_scatter = (n_k_tiles & 1) == 0;
|
||||
const size_t pair_region = (size_t) (2 * HMX_FP16_TILE_SIZE - 1);
|
||||
const size_t single_region = (size_t) (HMX_FP16_TILE_SIZE - 1);
|
||||
__builtin_assume(k > 0);
|
||||
__builtin_assume(end_row > start_row);
|
||||
|
||||
if (pair_scatter) {
|
||||
// Step c by 64 fp16 (two K-tiles per scatter), advance dst by 2 tiles per iter.
|
||||
@@ -148,6 +280,7 @@ static inline void hmx_interleave_rows_to_tiles(__fp16 * restrict vtcm_dst,
|
||||
}
|
||||
}
|
||||
}
|
||||
hvx_gather_sync(vtcm_dst);
|
||||
}
|
||||
|
||||
// Interleave row-major FP16 data into column-major tile format.
|
||||
@@ -227,4 +360,69 @@ static inline void hmx_interleave_cols_to_tiles(__fp16 * restrict tiles_out,
|
||||
#define HMX_CLRACC_F16() \
|
||||
"mxclracc.hf\n"
|
||||
|
||||
// --- Fixed-point INT HMX inline asm macros and helpers (v73+) ---
|
||||
|
||||
#define HMX_CLRACC_INT() \
|
||||
"mxclracc\n"
|
||||
|
||||
#define HMX_SWAPACC() \
|
||||
"mxswapacc\n"
|
||||
|
||||
#define HMX_LOAD_MPY_DEEP_U8_S8_SM(act, act_rt, wt, wt_rt) \
|
||||
"{\n" \
|
||||
" activation.ub = mxmem(" act ", " act_rt "):deep\n" \
|
||||
" weight.b = mxmem(" wt ", " wt_rt ")\n" \
|
||||
"}\n"
|
||||
|
||||
#define HMX_LOAD_MPY_DEEP_U8_S8_SM_64C(act, act_rt, wt, wt_rt) \
|
||||
"{\n" \
|
||||
" activation.ub = mxmem(" act ", " act_rt "):deep\n" \
|
||||
" weight.b = mxmem(" wt ", " wt_rt "):deep\n" \
|
||||
"}\n"
|
||||
|
||||
#define HMX_LOAD_MPY_DEEP_U8_S8_CM(act, act_rt, wt, wt_rt) \
|
||||
"{\n" \
|
||||
" activation.ub = mxmem(" act ", " act_rt "):deep:cm\n" \
|
||||
" weight.b = mxmem(" wt ", " wt_rt ")\n" \
|
||||
"}\n"
|
||||
|
||||
#define HMX_LOAD_MPY_DEEP_U8_S8_CM_64C(act, act_rt, wt, wt_rt) \
|
||||
"{\n" \
|
||||
" activation.ub = mxmem(" act ", " act_rt "):deep:cm\n" \
|
||||
" weight.b = mxmem(" wt ", " wt_rt "):deep\n" \
|
||||
"}\n"
|
||||
|
||||
#define HMX_CVT_UB_ACC(ctrl) \
|
||||
"cvt.ub = acc(" ctrl ")\n"
|
||||
|
||||
#define HMX_CVT_UH_ACC_2X1(ctrl) \
|
||||
"cvt.uh = acc(" ctrl "):2x1\n"
|
||||
|
||||
#define HMX_STORE_CVT_SM(out, range) \
|
||||
"mxmem(" out ", " range ") = cvt\n"
|
||||
|
||||
#define HMX_STORE_CVT_CM(out, range) \
|
||||
"mxmem(" out ", " range "):cm = cvt\n"
|
||||
|
||||
static inline uint32_t hmx_act_sm_range(uint32_t n_tiles) {
|
||||
return 2048u * n_tiles - 1u;
|
||||
}
|
||||
|
||||
static inline uint32_t hmx_out_cm_range(void) {
|
||||
return 0x3Fu << 5;
|
||||
}
|
||||
|
||||
static inline uint32_t hmx_wt_b_range(uint32_t n_bytes) {
|
||||
return ((n_bytes - 128u) & ~127u) | 0x7Fu;
|
||||
}
|
||||
|
||||
static inline uint32_t hmx_cvt_ctrl(uint32_t bank, uint32_t frac_only, uint32_t sat_mode, uint32_t retain_acc) {
|
||||
return (1u << 15) | ((frac_only & 1u) << 14) | ((bank & 3u) << 12) | ((sat_mode & 1u) << 1) | (retain_acc & 1u);
|
||||
}
|
||||
|
||||
static inline void hmx_load_bias_bank(const void * addr, uint32_t bank) {
|
||||
uint32_t rs = ((uint32_t) (uintptr_t) addr & ~255u) | (bank & 3u);
|
||||
asm volatile("bias = mxmem2(%0)\n" :: "r"(rs) : "memory");
|
||||
}
|
||||
|
||||
#endif // HMX_UTILS_H
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#ifndef HTP_CTX_H
|
||||
#define HTP_CTX_H
|
||||
|
||||
#include "dma-proxy.h"
|
||||
#include "dma-queue.h"
|
||||
#include "hmx-queue.h"
|
||||
#include "htp-dcache.h"
|
||||
#include "htp-ops.h"
|
||||
#include "htp-thread.h"
|
||||
#include "htp-trace.h"
|
||||
#include "work-queue.h"
|
||||
#include "hex-fastdiv.h"
|
||||
@@ -27,15 +28,6 @@ struct htp_mmap {
|
||||
uint32_t flags;
|
||||
};
|
||||
|
||||
// Scratchpad state
|
||||
struct htp_spad {
|
||||
const struct htp_tensor * src; // original src of the data (for reuse)
|
||||
uint8_t * data; // pointer to an area in vtcm
|
||||
uint32_t stride; // stride used inside this spad
|
||||
uint32_t size; // total size
|
||||
uint32_t size_per_hvx; // size per hvx
|
||||
};
|
||||
|
||||
struct htp_mdev_group {
|
||||
uint16_t idx;
|
||||
uint16_t count;
|
||||
@@ -61,11 +53,6 @@ struct htp_ops_context {
|
||||
const struct htp_tensor * dsts[HTP_OP_MAX_OUTPUTS];
|
||||
};
|
||||
|
||||
// TODO convert these to an array
|
||||
struct htp_spad src0_spad;
|
||||
struct htp_spad src1_spad;
|
||||
struct htp_spad dst_spad;
|
||||
|
||||
uint32_t flags;
|
||||
uint32_t n_hvx;
|
||||
struct fastdiv_values n_hvx_div;
|
||||
@@ -86,8 +73,10 @@ struct htp_context {
|
||||
struct htp_mmap mmap[HTP_MAX_MMAPS];
|
||||
dma_queue_t dma[HTP_MAX_NTHREADS];
|
||||
struct htp_thread_trace trace[HTP_MAX_NTHREADS];
|
||||
htp_thread_t main_th;
|
||||
work_queue_t hvx_queue;
|
||||
hmx_queue_t hmx_queue;
|
||||
dma_proxy_t dma_proxy;
|
||||
|
||||
uint32_t n_threads;
|
||||
uint32_t n_hvx;
|
||||
@@ -107,7 +96,6 @@ struct htp_context {
|
||||
atomic_bool vtcm_needs_release;
|
||||
|
||||
uint64_t max_vmem;
|
||||
struct htp_dirty_range dirty_ranges[HTP_MAX_DIRTY_RANGES];
|
||||
|
||||
// Persistent DDR scratchpad for MUL_MAT_ID mappings
|
||||
void * ddr_spad_base;
|
||||
|
||||
@@ -1,357 +0,0 @@
|
||||
#include "htp-dcache.h"
|
||||
|
||||
#include <qurt.h>
|
||||
#include <qurt_memory.h>
|
||||
#include <HAP_farf.h>
|
||||
|
||||
#include "hex-common.h"
|
||||
#include "hex-utils.h"
|
||||
#include "hex-fastdiv.h"
|
||||
#include "htp-trace.h"
|
||||
#include "htp-ctx.h"
|
||||
#include "work-queue.h"
|
||||
|
||||
struct l2flush_range {
|
||||
uint32_t start; // line-aligned start address
|
||||
uint32_t end; // line-aligned end address
|
||||
uint32_t block_first; // global block index of this range's first block
|
||||
uint32_t n_blocks; // number of HTP_DCACHE_BLOCK_SIZE chunks (last may be partial)
|
||||
};
|
||||
|
||||
struct l2flush_multi_task {
|
||||
struct l2flush_range ranges[HTP_MAX_DIRTY_RANGES];
|
||||
uint32_t n_ranges;
|
||||
uint32_t total_blocks;
|
||||
uint32_t blocks_per_hvx;
|
||||
};
|
||||
|
||||
static void flush_all_dcache(struct htp_context * ctx) {
|
||||
struct htp_thread_trace * tr = &ctx->trace[0];
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_L2FLUSH, 0);
|
||||
qurt_mem_cache_clean((qurt_addr_t) 0, 0, QURT_MEM_CACHE_FLUSH_INVALIDATE_ALL, QURT_MEM_DCACHE);
|
||||
hex_l2fetch_block(ctx, ctx->footprint);
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_L2FLUSH, 0);
|
||||
memset(ctx->dirty_ranges, 0, sizeof(ctx->dirty_ranges));
|
||||
}
|
||||
|
||||
static void l2flush_multi_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
|
||||
struct l2flush_multi_task * task = (struct l2flush_multi_task *) data;
|
||||
(void) n;
|
||||
|
||||
const uint32_t gb_first = i * task->blocks_per_hvx;
|
||||
uint32_t gb_last = gb_first + task->blocks_per_hvx;
|
||||
if (gb_last > task->total_blocks) {
|
||||
gb_last = task->total_blocks;
|
||||
}
|
||||
if (gb_first >= gb_last) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_L2FLUSH, gb_first);
|
||||
|
||||
for (uint32_t r = 0; r < task->n_ranges; r++) {
|
||||
const struct l2flush_range * rg = &task->ranges[r];
|
||||
const uint32_t rb_first = rg->block_first;
|
||||
const uint32_t rb_last = rg->block_first + rg->n_blocks;
|
||||
|
||||
const uint32_t lo = gb_first > rb_first ? gb_first : rb_first;
|
||||
const uint32_t hi = gb_last < rb_last ? gb_last : rb_last;
|
||||
if (lo >= hi) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const uint32_t s = rg->start + (lo - rb_first) * HTP_DCACHE_BLOCK_SIZE;
|
||||
uint32_t e = rg->start + (hi - rb_first) * HTP_DCACHE_BLOCK_SIZE;
|
||||
if (e > rg->end) {
|
||||
e = rg->end;
|
||||
}
|
||||
hex_l2flush((void *) (uintptr_t) s, e - s);
|
||||
}
|
||||
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_L2FLUSH, gb_first);
|
||||
}
|
||||
|
||||
static void merge_dirty_ranges(struct htp_context * ctx) {
|
||||
for (uint32_t i = 0; i < HTP_MAX_DIRTY_RANGES; i++) {
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[i];
|
||||
if (!r->start) continue;
|
||||
|
||||
for (uint32_t j = 0; j < HTP_MAX_DIRTY_RANGES;) {
|
||||
struct htp_dirty_range * s = &ctx->dirty_ranges[j];
|
||||
if (i == j || !s->start || r->end < s->start || s->end < r->start) {
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
|
||||
r->start = MIN(r->start, s->start);
|
||||
r->end = MAX(r->end, s->end);
|
||||
s->start = 0;
|
||||
s->end = 0;
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void htp_dcache_mark_dirty_tensors(struct htp_context * ctx, const struct htp_tensor * const * tensors, uint32_t n) {
|
||||
const struct htp_tensor * pending[HTP_OP_MAX_OUTPUTS];
|
||||
uint32_t n_pending = 0;
|
||||
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
const struct htp_tensor * t = tensors[i];
|
||||
if (!t || (t->flags & (HTP_TENSOR_WEIGHT | HTP_TENSOR_FENCE))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
uint32_t t_start = t->data;
|
||||
uint32_t t_end = t_start + t->size;
|
||||
|
||||
bool merged = false;
|
||||
for (uint32_t j = 0; j < HTP_MAX_DIRTY_RANGES; j++) {
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[j];
|
||||
if (!r->start) continue;
|
||||
|
||||
if (r->start <= t_end && t_start <= r->end) {
|
||||
uint32_t new_start = (t_start < r->start) ? t_start : r->start;
|
||||
uint32_t new_end = (t_end > r->end) ? t_end : r->end;
|
||||
r->start = new_start;
|
||||
r->end = new_end;
|
||||
merged = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!merged) {
|
||||
pending[n_pending++] = t;
|
||||
}
|
||||
}
|
||||
|
||||
merge_dirty_ranges(ctx);
|
||||
|
||||
if (n_pending == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t empty_indices[HTP_MAX_DIRTY_RANGES];
|
||||
uint32_t active_indices[HTP_MAX_DIRTY_RANGES];
|
||||
uint32_t n_active = 0;
|
||||
uint32_t n_empty = 0;
|
||||
for (uint32_t j = 0; j < HTP_MAX_DIRTY_RANGES; j++) {
|
||||
if (ctx->dirty_ranges[j].start) {
|
||||
active_indices[n_active++] = j;
|
||||
} else {
|
||||
empty_indices[n_empty++] = j;
|
||||
}
|
||||
}
|
||||
|
||||
if (n_pending <= n_empty) {
|
||||
for (uint32_t i = 0; i < n_pending; i++) {
|
||||
uint32_t idx = empty_indices[i];
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[idx];
|
||||
r->start = pending[i]->data;
|
||||
r->end = pending[i]->data + pending[i]->size;
|
||||
}
|
||||
merge_dirty_ranges(ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t n_evict = n_pending - n_empty;
|
||||
uint32_t total_evict_size = 0;
|
||||
for (uint32_t i = 0; i < n_evict; i++) {
|
||||
uint32_t idx = active_indices[i];
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[idx];
|
||||
total_evict_size += r->end - r->start;
|
||||
}
|
||||
|
||||
if (total_evict_size > HTP_DCACHE_FLUSH_ALL_THRESHOLD) {
|
||||
flush_all_dcache(ctx);
|
||||
for (uint32_t i = 0; i < n_pending; i++) {
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[i];
|
||||
r->start = pending[i]->data;
|
||||
r->end = pending[i]->data + pending[i]->size;
|
||||
}
|
||||
merge_dirty_ranges(ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
if (total_evict_size > HTP_DCACHE_FLUSH_WQ_THRESHOLD && ctx->n_hvx > 1 && n_evict <= HTP_MAX_DIRTY_RANGES) {
|
||||
struct l2flush_multi_task task;
|
||||
task.n_ranges = n_evict;
|
||||
|
||||
uint32_t block_acc = 0;
|
||||
for (uint32_t i = 0; i < n_evict; i++) {
|
||||
uint32_t idx = active_indices[i];
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[idx];
|
||||
|
||||
struct l2flush_range * rg = &task.ranges[i];
|
||||
rg->start = hex_align_down((size_t) r->start, HEX_L2_LINE_SIZE);
|
||||
rg->end = hex_align_up((size_t) r->end, HEX_L2_LINE_SIZE);
|
||||
rg->block_first = block_acc;
|
||||
rg->n_blocks = (rg->end - rg->start + HTP_DCACHE_BLOCK_SIZE - 1) / HTP_DCACHE_BLOCK_SIZE;
|
||||
block_acc += rg->n_blocks;
|
||||
}
|
||||
|
||||
task.total_blocks = block_acc;
|
||||
task.blocks_per_hvx = fastdiv(block_acc + ctx->n_hvx - 1, &ctx->n_hvx_div);
|
||||
|
||||
work_queue_run(ctx->hvx_queue, l2flush_multi_worker, &task, ctx->n_hvx);
|
||||
} else {
|
||||
struct htp_thread_trace * tr = &ctx->trace[0];
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_L2FLUSH, 0);
|
||||
for (uint32_t i = 0; i < n_evict; i++) {
|
||||
uint32_t idx = active_indices[i];
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[idx];
|
||||
uint32_t size = r->end - r->start;
|
||||
hex_l2flush((void *) (uintptr_t) r->start, size);
|
||||
}
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_L2FLUSH, 0);
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < n_evict; i++) {
|
||||
uint32_t idx = active_indices[i];
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[idx];
|
||||
r->start = pending[i]->data;
|
||||
r->end = pending[i]->data + pending[i]->size;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < n_empty; i++) {
|
||||
uint32_t idx = empty_indices[i];
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[idx];
|
||||
r->start = pending[n_evict + i]->data;
|
||||
r->end = pending[n_evict + i]->data + pending[n_evict + i]->size;
|
||||
}
|
||||
|
||||
merge_dirty_ranges(ctx);
|
||||
}
|
||||
|
||||
static void make_tensor_clean(struct htp_context * ctx, const struct htp_tensor * t) {
|
||||
if (!t || (t->flags & (HTP_TENSOR_WEIGHT | HTP_TENSOR_FENCE))) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t t_start = t->data;
|
||||
uint32_t t_end = t_start + t->size;
|
||||
|
||||
for (uint32_t i = 0; i < HTP_MAX_DIRTY_RANGES; i++) {
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[i];
|
||||
if (!r->start) continue;
|
||||
|
||||
if (r->start < t_end && t_start < r->end) {
|
||||
if (t_start <= r->start && r->end <= t_end) {
|
||||
r->start = 0;
|
||||
r->end = 0;
|
||||
} else if (t_start <= r->start) {
|
||||
r->start = t_end;
|
||||
} else if (r->end <= t_end) {
|
||||
r->end = t_start;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline bool is_tensor_dirty(struct htp_context * ctx, const struct htp_tensor * t) {
|
||||
if (!t || (t->flags & (HTP_TENSOR_WEIGHT | HTP_TENSOR_FENCE))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t t_start = t->data;
|
||||
uint32_t t_end = t_start + t->size;
|
||||
|
||||
for (uint32_t i = 0; i < HTP_MAX_DIRTY_RANGES; i++) {
|
||||
struct htp_dirty_range * r = &ctx->dirty_ranges[i];
|
||||
if (!r->start) continue;
|
||||
|
||||
if (r->start < t_end && t_start < r->end) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void flush_dirty_ranges(struct htp_context * ctx, const struct htp_dirty_range * ranges, uint32_t n_ranges, uint64_t total_dirty) {
|
||||
if (total_dirty >= HTP_DCACHE_FLUSH_WQ_THRESHOLD && ctx->n_hvx > 1) {
|
||||
struct l2flush_multi_task task;
|
||||
task.n_ranges = n_ranges;
|
||||
|
||||
uint32_t block_acc = 0;
|
||||
for (uint32_t i = 0; i < n_ranges; i++) {
|
||||
const struct htp_dirty_range * r = &ranges[i];
|
||||
struct l2flush_range * rg = &task.ranges[i];
|
||||
rg->start = hex_align_down((size_t) r->start, HEX_L2_LINE_SIZE);
|
||||
rg->end = hex_align_up((size_t) r->end, HEX_L2_LINE_SIZE);
|
||||
rg->block_first = block_acc;
|
||||
rg->n_blocks = (rg->end - rg->start + HTP_DCACHE_BLOCK_SIZE - 1) / HTP_DCACHE_BLOCK_SIZE;
|
||||
block_acc += rg->n_blocks;
|
||||
}
|
||||
|
||||
task.total_blocks = block_acc;
|
||||
task.blocks_per_hvx = fastdiv(block_acc + ctx->n_hvx - 1, &ctx->n_hvx_div);
|
||||
|
||||
work_queue_run(ctx->hvx_queue, l2flush_multi_worker, &task, ctx->n_hvx);
|
||||
} else {
|
||||
struct htp_thread_trace * tr = &ctx->trace[0];
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_L2FLUSH, 0);
|
||||
for (uint32_t i = 0; i < n_ranges; i++) {
|
||||
const struct htp_dirty_range * r = &ranges[i];
|
||||
hex_l2flush((void *) (uintptr_t) r->start, r->end - r->start);
|
||||
}
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_L2FLUSH, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void htp_dcache_flush_dirty_ranges(struct htp_context * ctx) {
|
||||
struct htp_dirty_range ranges[HTP_MAX_DIRTY_RANGES];
|
||||
uint32_t n_ranges = 0;
|
||||
uint64_t total_dirty = 0;
|
||||
|
||||
for (uint32_t i = 0; i < HTP_MAX_DIRTY_RANGES; i++) {
|
||||
const struct htp_dirty_range * r = &ctx->dirty_ranges[i];
|
||||
if (!r->start) {
|
||||
continue;
|
||||
}
|
||||
ranges[n_ranges++] = *r;
|
||||
total_dirty += r->end - r->start;
|
||||
}
|
||||
|
||||
if (total_dirty == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (total_dirty > HTP_DCACHE_FLUSH_ALL_THRESHOLD) {
|
||||
flush_all_dcache(ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
flush_dirty_ranges(ctx, ranges, n_ranges, total_dirty);
|
||||
memset(ctx->dirty_ranges, 0, sizeof(ctx->dirty_ranges));
|
||||
}
|
||||
|
||||
void htp_dcache_flush_dirty_tensors(struct htp_context * ctx, const struct htp_tensor * const * tensors, uint32_t n) {
|
||||
const struct htp_tensor * dirty_tensors[HTP_OP_MAX_INPUTS];
|
||||
struct htp_dirty_range ranges[HTP_OP_MAX_INPUTS];
|
||||
uint32_t n_dirty = 0;
|
||||
uint64_t total_dirty = 0;
|
||||
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
const struct htp_tensor * t = tensors[i];
|
||||
if (is_tensor_dirty(ctx, t)) {
|
||||
dirty_tensors[n_dirty++] = t;
|
||||
ranges[n_dirty - 1].start = t->data;
|
||||
ranges[n_dirty - 1].end = t->data + t->size;
|
||||
total_dirty += t->size;
|
||||
}
|
||||
}
|
||||
|
||||
if (total_dirty == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (total_dirty > HTP_DCACHE_FLUSH_ALL_THRESHOLD) {
|
||||
flush_all_dcache(ctx);
|
||||
return;
|
||||
}
|
||||
|
||||
flush_dirty_ranges(ctx, ranges, n_dirty, total_dirty);
|
||||
for (uint32_t i = 0; i < n_dirty; i++) {
|
||||
make_tensor_clean(ctx, dirty_tensors[i]);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef HTP_DCACHE_H
|
||||
#define HTP_DCACHE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "hex-common.h"
|
||||
#include "htp-ops.h"
|
||||
|
||||
#define HTP_MAX_DIRTY_RANGES 64
|
||||
#define HTP_DCACHE_BLOCK_SIZE (HEX_L2_LINE_SIZE * 4) // flush granularity (chunks per thread)
|
||||
#define HTP_DCACHE_FLUSH_WQ_THRESHOLD (4 * 1024)
|
||||
#define HTP_DCACHE_FLUSH_ALL_THRESHOLD (4 * 1024 * 1024)
|
||||
|
||||
struct htp_dirty_range {
|
||||
uint32_t start;
|
||||
uint32_t end;
|
||||
};
|
||||
|
||||
struct htp_context;
|
||||
|
||||
void htp_dcache_flush_dirty_ranges(struct htp_context * ctx);
|
||||
void htp_dcache_flush_dirty_tensors(struct htp_context * ctx, const struct htp_tensor * const * tensors, uint32_t n);
|
||||
void htp_dcache_mark_dirty_tensors(struct htp_context * ctx, const struct htp_tensor * const * tensors, uint32_t n);
|
||||
|
||||
#endif // HTP_DCACHE_H
|
||||
@@ -129,6 +129,7 @@ enum htp_op_code {
|
||||
HTP_OP_LIGHTNING_INDEXER,
|
||||
HTP_OP_SSM_SCAN,
|
||||
HTP_OP_TIMESTEP_EMBEDDING,
|
||||
HTP_OP_MUL_MAT_NX_GLU,
|
||||
HTP_OP_INVALID
|
||||
};
|
||||
|
||||
@@ -154,10 +155,11 @@ enum htp_op_code {
|
||||
#define HTP_DDR_SPAD_SIZE (512 * 1024)
|
||||
|
||||
enum htp_tensor_flags {
|
||||
HTP_TENSOR_WEIGHT = (1U << 0), // Tensor buffer model weight data (not compute)
|
||||
HTP_TENSOR_TILED = (1U << 1), // Tensor is in tiled format
|
||||
HTP_TENSOR_FENCE = (1U << 2), // Tensor is synchronization fence (explicitly managed)
|
||||
HTP_TENSOR_FUSEABLE = (1U << 3), // Tensor can be fused (host-only)
|
||||
HTP_TENSOR_WEIGHT = (1U << 0), // Tensor buffer model weight data (not compute)
|
||||
HTP_TENSOR_TILED = (1U << 1), // Tensor is in tiled format
|
||||
HTP_TENSOR_FENCE = (1U << 2), // Tensor is synchronization fence (explicitly managed)
|
||||
HTP_TENSOR_FUSEABLE = (1U << 3), // Tensor can be fused (host-only)
|
||||
HTP_TENSOR_LOWERED_F16 = (1U << 4), // F32 tensor lowered in-place to F16 (host-only)
|
||||
};
|
||||
|
||||
enum htp_buf_flags {
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
#include "htp-tensor.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "dma-copy.h"
|
||||
#include "hex-utils.h"
|
||||
|
||||
#define HTP_TENSOR_READ_SMALL_BYTES 256
|
||||
|
||||
static inline void htp_tensor_read_l2flush(const htp_thread_t * th, uintptr_t start, size_t len) {
|
||||
const uintptr_t s = hex_align_down(start, HEX_L2_LINE_SIZE);
|
||||
const uintptr_t e = hex_align_up(start + len, HEX_L2_LINE_SIZE);
|
||||
if (e <= s) {
|
||||
return;
|
||||
}
|
||||
htp_trace_event_start(th->trace, HTP_TRACE_EVT_L2FLUSH, 0);
|
||||
hex_l2flush((void *) s, e - s);
|
||||
htp_trace_event_stop(th->trace, HTP_TRACE_EVT_L2FLUSH, 0);
|
||||
}
|
||||
|
||||
size_t htp_tensor_read(const htp_thread_t * th, void * dst, size_t dst_size, const struct htp_tensor * src) {
|
||||
if (!th || !dst || !src) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const uint32_t ne0 = src->ne[0];
|
||||
const uint32_t ne1 = src->ne[1];
|
||||
const uint32_t ne2 = src->ne[2];
|
||||
const uint32_t ne3 = src->ne[3];
|
||||
if (ne0 == 0 || ne1 == 0 || ne2 == 0 || ne3 == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const uint32_t row_bytes = htp_tensor_get_row_size(src->type, ne0);
|
||||
if (row_bytes == 0 || (row_bytes % ne0) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const uint32_t elem_size = row_bytes / ne0;
|
||||
const size_t nrows = (size_t) ne1 * ne2 * ne3;
|
||||
const size_t total_bytes = (size_t) row_bytes * nrows;
|
||||
if (total_bytes > dst_size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const uintptr_t dst_addr = (uintptr_t) dst;
|
||||
const bool is_contig = htp_tensor_is_contiguous(src, elem_size);
|
||||
|
||||
if (!htp_tensor_is_extended(src) && is_contig && total_bytes <= HTP_TENSOR_READ_SMALL_BYTES) {
|
||||
if (!(src->flags & (HTP_TENSOR_WEIGHT | HTP_TENSOR_FENCE))) {
|
||||
htp_tensor_read_l2flush(th, (uintptr_t) src->data, total_bytes);
|
||||
}
|
||||
memcpy(dst, (const void *) (uintptr_t) src->data, total_bytes);
|
||||
return total_bytes;
|
||||
}
|
||||
|
||||
htp_tensor_read_l2flush(th, dst_addr, total_bytes);
|
||||
|
||||
dma_queue * dma_q = th->dma_q;
|
||||
if (ne0 > 1 && src->nb[0] != elem_size) {
|
||||
dma_addr_t cur_dst = (dma_addr_t) dst_addr;
|
||||
for (uint32_t i3 = 0; i3 < ne3; ++i3) {
|
||||
for (uint32_t i2 = 0; i2 < ne2; ++i2) {
|
||||
for (uint32_t i1 = 0; i1 < ne1; ++i1) {
|
||||
const dma_addr_t cur_src = src->data +
|
||||
(dma_addr_t) i1 * src->nb[1] +
|
||||
(dma_addr_t) i2 * src->nb[2] +
|
||||
(dma_addr_t) i3 * src->nb[3];
|
||||
dma_cpy_push_2d_chunked(dma_q, cur_dst, cur_src, elem_size, src->nb[0], elem_size, ne0);
|
||||
cur_dst += row_bytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
struct htp_tensor dst_t = *src;
|
||||
dst_t.data = (dma_addr_t) dst_addr;
|
||||
dst_t.nb[0] = elem_size;
|
||||
dst_t.nb[1] = row_bytes;
|
||||
dst_t.nb[2] = row_bytes * ne1;
|
||||
dst_t.nb[3] = dst_t.nb[2] * ne2;
|
||||
dma_cpy_sametype_sameshape(dma_q, &dst_t, src, elem_size);
|
||||
}
|
||||
dma_queue_flush(dma_q);
|
||||
|
||||
return total_bytes;
|
||||
}
|
||||
@@ -8,8 +8,6 @@
|
||||
#include "hex-common.h"
|
||||
#include "hex-fastdiv.h"
|
||||
|
||||
#define HTP_TENSOR_MDEV_LINE_SIZE 128
|
||||
|
||||
// Currently HMX requires 32x32 tile formats (do not change)
|
||||
#define HTP_TENSOR_TILE_N_COLS 32
|
||||
#define HTP_TENSOR_TILE_N_ROWS 32
|
||||
@@ -69,53 +67,6 @@ static inline bool htp_tensor_is_permuted(const struct htp_tensor * t) {
|
||||
return t->nb[0] > t->nb[1] || t->nb[1] > t->nb[2] || t->nb[2] > t->nb[3];
|
||||
}
|
||||
|
||||
static inline bool htp_tensor_mdev_data_aligned(const struct htp_tensor * t) {
|
||||
return ((uintptr_t) t->data & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0;
|
||||
}
|
||||
|
||||
static inline bool htp_tensor_can_row_partition(const struct htp_tensor * t, uint32_t elem_size) {
|
||||
if (!htp_tensor_mdev_data_aligned(t)) {
|
||||
return false;
|
||||
}
|
||||
if (t->ne[0] != 1 && t->nb[0] != elem_size) {
|
||||
return false;
|
||||
}
|
||||
if (htp_tensor_is_permuted(t)) {
|
||||
return false;
|
||||
}
|
||||
if (t->ne[1] > 1 && (t->nb[1] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) != 0) return false;
|
||||
if (t->ne[2] > 1 && (t->nb[2] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) != 0) return false;
|
||||
if (t->ne[3] > 1 && (t->nb[3] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) != 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool htp_tensor_mdev_rows_per_chunk(const struct htp_tensor * t, uint32_t elem_size, uint32_t row_size, uint32_t * rows_per_chunk) {
|
||||
*rows_per_chunk = 0;
|
||||
|
||||
if (!htp_tensor_mdev_data_aligned(t)) {
|
||||
return false;
|
||||
}
|
||||
if (t->ne[0] != 1 && t->nb[0] != elem_size) {
|
||||
return false;
|
||||
}
|
||||
if (htp_tensor_is_permuted(t)) {
|
||||
return false;
|
||||
}
|
||||
if (t->ne[1] > 1 && (t->nb[1] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0 &&
|
||||
(t->ne[2] <= 1 || (t->nb[2] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0) &&
|
||||
(t->ne[3] <= 1 || (t->nb[3] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0)) {
|
||||
*rows_per_chunk = 1;
|
||||
return true;
|
||||
}
|
||||
if (t->nb[1] == row_size &&
|
||||
(t->ne[2] <= 1 || t->nb[2] == t->nb[1] * t->ne[1]) &&
|
||||
(t->ne[3] <= 1 || t->nb[3] == t->nb[2] * t->ne[2])) {
|
||||
*rows_per_chunk = (row_size > 0) ? (HTP_TENSOR_MDEV_LINE_SIZE / hex_gcd_u32(row_size, HTP_TENSOR_MDEV_LINE_SIZE)) : 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline struct htp_tensor_mdev_range htp_tensor_mdev_partition(uint32_t total_units, uint32_t units_per_chunk, uint32_t mdev_idx, uint32_t mdev_count, const struct fastdiv_values * mdev_count_div) {
|
||||
struct htp_tensor_mdev_range range = { 0, total_units };
|
||||
|
||||
@@ -214,4 +165,16 @@ static inline uint32_t htp_tensor_get_row_size(int type, uint32_t ne00) {
|
||||
}
|
||||
}
|
||||
|
||||
static inline size_t htp_tensor_nbytes(const struct htp_tensor * t) {
|
||||
return (size_t) htp_tensor_get_row_size(t->type, t->ne[0]) * t->ne[1] * t->ne[2] * t->ne[3];
|
||||
}
|
||||
|
||||
#ifdef __HVX_ARCH__
|
||||
#include "htp-thread.h"
|
||||
|
||||
// Read tensor data contiguously into a 128-byte aligned cached DDR buffer.
|
||||
// [dst, hex_align_up(dst + bytes, 128)) must be exclusively owned by the caller.
|
||||
size_t htp_tensor_read(const htp_thread_t * th, void * dst, size_t dst_size, const struct htp_tensor * src);
|
||||
#endif
|
||||
|
||||
#endif // HTP_TENSOR_H
|
||||
|
||||
@@ -4,6 +4,13 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Reserve one HVX vector at the end of physical VTCM so tail hvx_vmemu loads cannot fault past the page end
|
||||
#define HTP_VTCM_GUARD_SIZE 128
|
||||
|
||||
static inline size_t htp_vtcm_usable_size(size_t raw_size) {
|
||||
return (raw_size - HTP_VTCM_GUARD_SIZE) & ~(size_t) 127;
|
||||
}
|
||||
|
||||
static inline uint8_t *vtcm_seq_alloc(uint8_t **vtcm_ptr, size_t size) {
|
||||
uint8_t *p = *vtcm_ptr;
|
||||
*vtcm_ptr += size;
|
||||
|
||||
@@ -12,6 +12,10 @@
|
||||
#define hvx_vmem(A) *((HVX_Vector *)(A))
|
||||
#define hvx_vmemu(A) *((HVX_UVector *)(A))
|
||||
|
||||
static inline void hvx_vmem_store_nt(void * restrict dst, HVX_Vector v) {
|
||||
asm("vmem(%1+#0):nt = %2" : "=m"(*(HVX_Vector *) dst) : "r"(dst), "v"(v));
|
||||
}
|
||||
|
||||
static inline void hvx_vec_store_u(void * restrict dst, uint32_t n, HVX_Vector v) {
|
||||
// Rotate as needed.
|
||||
v = Q6_V_vlalign_VVR(v, v, (size_t) dst);
|
||||
@@ -187,6 +191,9 @@ static inline HVX_VectorPair hvx_vec_f16_to_f32(HVX_Vector v) {
|
||||
#endif
|
||||
|
||||
static inline HVX_Vector hvx_vec_i16_from_hf_rnd_sat(HVX_Vector vin) {
|
||||
#if __HVX_ARCH__ >= 81
|
||||
return Q6_Vh_equals_Vhf_rnd(vin);
|
||||
#else
|
||||
// This looks complicated.
|
||||
// Ideally should just be Q6_Vh_equals_Vhf(vin)
|
||||
// but that instruction does not do proper rounding.
|
||||
@@ -208,10 +215,17 @@ static inline HVX_Vector hvx_vec_i16_from_hf_rnd_sat(HVX_Vector vin) {
|
||||
|
||||
// now round down to 16
|
||||
return Q6_Vh_vround_VwVw_sat(vsf_1, vsf_0);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if __HVX_ARCH__ < 79
|
||||
|
||||
static inline HVX_VectorPair hvx_vec_mpy_f32_f16(HVX_Vector x, HVX_Vector y)
|
||||
{
|
||||
HVX_VectorPair m = Q6_Wqf32_vmpy_VhfVhf(x, y);
|
||||
return Q6_W_vcombine_VV(Q6_Vsf_equals_Vqf32(Q6_V_hi_W(m)), Q6_Vsf_equals_Vqf32(Q6_V_lo_W(m)));
|
||||
}
|
||||
|
||||
static inline HVX_VectorPair hvx_vec_mpyacc_f32_f16(HVX_VectorPair acc, HVX_Vector x, HVX_Vector y)
|
||||
{
|
||||
HVX_VectorPair m = Q6_Wqf32_vmpy_VhfVhf(x, y);
|
||||
@@ -222,6 +236,11 @@ static inline HVX_VectorPair hvx_vec_mpyacc_f32_f16(HVX_VectorPair acc, HVX_Vect
|
||||
|
||||
#else
|
||||
|
||||
static inline HVX_VectorPair hvx_vec_mpy_f32_f16(HVX_Vector x, HVX_Vector y)
|
||||
{
|
||||
return Q6_Wsf_vmpy_VhfVhf(x, y);
|
||||
}
|
||||
|
||||
static inline HVX_VectorPair hvx_vec_mpyacc_f32_f16(HVX_VectorPair acc, HVX_Vector x, HVX_Vector y)
|
||||
{
|
||||
return Q6_Wsf_vmpyacc_WsfVhfVhf(acc, x, y);
|
||||
@@ -400,7 +419,7 @@ static inline void hvx_transpose_32x32_w_shuff(uint8_t * restrict dst, uint32_t
|
||||
}
|
||||
}
|
||||
|
||||
// Specialized store that will follow outstanding scatters or gathers to make sure they complete
|
||||
// Drain outstanding vscatter/vgather before DMA, HMX, or scalar access to the VTCM region.
|
||||
static inline void hvx_gather_sync(void * vtcm_addr) {
|
||||
asm volatile("vmem(%0+#0):scatter_release\n"
|
||||
"v0 = vmem(%0+#0)\n" :: "r"(vtcm_addr) : "v0", "memory");
|
||||
@@ -424,7 +443,7 @@ static inline HVX_Vector hvx_vec_gather_offsets_h(uint32_t stride) {
|
||||
}
|
||||
|
||||
// Rows [0, nrows) of the transpose of ncols src vectors (src_stride apart) to dst.
|
||||
// src, dst in VTCM. Async: hvx_gather_sync() before use.
|
||||
// src, dst in VTCM. Async: call hvx_gather_sync() before DMA, HMX, or scalar access.
|
||||
// Use for partial tiles (small ncols). Fewest packets.
|
||||
static inline void hvx_transpose_32x32_w_gather(uint8_t * restrict dst, uint32_t dst_stride, const uint8_t * restrict src,
|
||||
uint32_t src_stride, HVX_Vector offsets, uint32_t nrows, uint32_t ncols) {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "hex-utils.h"
|
||||
#include "hvx-types.h"
|
||||
|
||||
static void hvx_vec_dump_f16_n(char * pref, HVX_Vector v, uint32_t n) {
|
||||
static inline void hvx_vec_dump_f16_n(char * pref, HVX_Vector v, uint32_t n) {
|
||||
HVX_VectorAlias u = { .v = v };
|
||||
|
||||
const uint32_t n0 = n / 16;
|
||||
@@ -23,11 +23,11 @@ static void hvx_vec_dump_f16_n(char * pref, HVX_Vector v, uint32_t n) {
|
||||
}
|
||||
}
|
||||
|
||||
static void hvx_vec_dump_f16(char * pref, HVX_Vector v) {
|
||||
static inline void hvx_vec_dump_f16(char * pref, HVX_Vector v) {
|
||||
hvx_vec_dump_f16_n(pref, v, 64);
|
||||
}
|
||||
|
||||
static void hvx_vec_dump_f32_n(char * pref, HVX_Vector v, uint32_t n) {
|
||||
static inline void hvx_vec_dump_f32_n(char * pref, HVX_Vector v, uint32_t n) {
|
||||
HVX_VectorAlias u = { .v = v };
|
||||
|
||||
const uint32_t n0 = n / 16;
|
||||
@@ -41,21 +41,22 @@ static void hvx_vec_dump_f32_n(char * pref, HVX_Vector v, uint32_t n) {
|
||||
}
|
||||
}
|
||||
|
||||
static void hvx_vec_dump_f32_hmt(char * pref, HVX_Vector v) {
|
||||
static inline void hvx_vec_dump_f32_hmt(char * pref, HVX_Vector v) {
|
||||
union {
|
||||
HVX_Vector v;
|
||||
float d[32];
|
||||
} u = { .v = v };
|
||||
(void) u;
|
||||
|
||||
FARF(HIGH, "%s: %.6f %.6f %.6f %.6f ... %.6f %.6f %.6f %.6f ... %.6f %.6f %.6f %.6f\n", pref, u.d[0], u.d[1],
|
||||
u.d[2], u.d[3], u.d[12], u.d[13], u.d[14], u.d[15], u.d[28], u.d[29], u.d[30], u.d[31]);
|
||||
}
|
||||
|
||||
static void hvx_vec_dump_f32(char * pref, HVX_Vector v) {
|
||||
static inline void hvx_vec_dump_f32(char * pref, HVX_Vector v) {
|
||||
hvx_vec_dump_f32_n(pref, v, 32);
|
||||
}
|
||||
|
||||
static void hvx_vec_dump_int32(char * pref, HVX_Vector v) {
|
||||
static inline void hvx_vec_dump_int32(char * pref, HVX_Vector v) {
|
||||
union {
|
||||
HVX_Vector v;
|
||||
int32_t d[32];
|
||||
@@ -66,27 +67,29 @@ static void hvx_vec_dump_int32(char * pref, HVX_Vector v) {
|
||||
}
|
||||
}
|
||||
|
||||
static void hvx_vec_dump_int32_hmt(char * pref, HVX_Vector v) {
|
||||
static inline void hvx_vec_dump_int32_hmt(char * pref, HVX_Vector v) {
|
||||
union {
|
||||
HVX_Vector v;
|
||||
int32_t d[32];
|
||||
} u = { .v = v };
|
||||
(void) u;
|
||||
|
||||
FARF(HIGH, "%s: %d %d %d %d ... %d %d %d %d ... %d %d %d %d\n", pref, u.d[0], u.d[1], u.d[2], u.d[3], u.d[12],
|
||||
u.d[13], u.d[14], u.d[15], u.d[28], u.d[29], u.d[30], u.d[31]);
|
||||
}
|
||||
|
||||
static void hvx_vec_dump_int8_hmt(char * pref, HVX_Vector v) {
|
||||
static inline void hvx_vec_dump_int8_hmt(char * pref, HVX_Vector v) {
|
||||
union {
|
||||
HVX_Vector v;
|
||||
int8_t d[128];
|
||||
} u = { .v = v };
|
||||
(void) u;
|
||||
|
||||
FARF(HIGH, "%s: %d %d %d %d ... %d %d %d %d ... %d %d %d %d\n", pref, u.d[0], u.d[1], u.d[2], u.d[3], u.d[60],
|
||||
u.d[61], u.d[62], u.d[63], u.d[124], u.d[125], u.d[126], u.d[127]);
|
||||
}
|
||||
|
||||
static void hvx_vec_dump_int8(char * pref, HVX_Vector v) {
|
||||
static inline void hvx_vec_dump_int8(char * pref, HVX_Vector v) {
|
||||
union {
|
||||
HVX_Vector v;
|
||||
int8_t d[128];
|
||||
@@ -97,7 +100,7 @@ static void hvx_vec_dump_int8(char * pref, HVX_Vector v) {
|
||||
}
|
||||
}
|
||||
|
||||
static void hvx_vec_dump_uint8(char * pref, HVX_Vector v) {
|
||||
static inline void hvx_vec_dump_uint8(char * pref, HVX_Vector v) {
|
||||
union {
|
||||
HVX_Vector v;
|
||||
uint8_t d[128];
|
||||
@@ -108,7 +111,7 @@ static void hvx_vec_dump_uint8(char * pref, HVX_Vector v) {
|
||||
}
|
||||
}
|
||||
|
||||
static bool hvx_vec_eq(HVX_Vector v0, HVX_Vector v1, size_t n) {
|
||||
static inline bool hvx_vec_eq(HVX_Vector v0, HVX_Vector v1, size_t n) {
|
||||
typedef union {
|
||||
HVX_Vector v;
|
||||
int8_t d[128];
|
||||
|
||||
@@ -214,7 +214,25 @@ static inline void hvx_exp_f32(uint8_t * restrict dst, const uint8_t * restrict
|
||||
}
|
||||
}
|
||||
|
||||
static inline HVX_Vector hvx_vec_exp2_f16(HVX_Vector x_v) {
|
||||
// 2^x on 64 fp16 lanes, requires x <= 0
|
||||
#if __HVX_ARCH__ >= 81
|
||||
#define HVX_EXP2_FIXED_G0 (-12573)
|
||||
#define HVX_EXP2_FIXED_G1 (15210)
|
||||
#define HVX_EXP2_FIXED_G2 (-2645)
|
||||
#define HVX_EXP2_FIXED_G2_PAIR ((int32_t) ((uint32_t) (uint16_t) HVX_EXP2_FIXED_G2 * 0x00010001u))
|
||||
|
||||
static inline HVX_Vector hvx_vec_exp2_neg_f16(HVX_Vector x_v) {
|
||||
const HVX_Vector z = Q6_Vuh_vmin_VuhVuh(Q6_V_vand_VV(x_v, Q6_Vh_vsplat_R(0x7FFF)), Q6_Vh_vsplat_R(0x4E40));
|
||||
const HVX_Vector t = Q6_Vh_equals_Vhf_rnd(Q6_Vh_vadd_VhVh(z, Q6_Vh_vsplat_R(0x2800)));
|
||||
const HVX_Vector rho = Q6_V_vand_VV(t, Q6_Vh_vsplat_R(0x03FF));
|
||||
const HVX_Vector r15 = Q6_Vh_vasl_VhR(rho, 5);
|
||||
HVX_Vector r = Q6_Vh_vadd_VhVh(Q6_Vh_vmpy_VhRh_s1_rnd_sat(r15, HVX_EXP2_FIXED_G2_PAIR), Q6_Vh_vsplat_R(HVX_EXP2_FIXED_G1));
|
||||
r = Q6_Vh_vadd_VhVh(Q6_Vh_vmpy_VhVh_s1_rnd_sat(r, r15), Q6_Vh_vsplat_R(HVX_EXP2_FIXED_G0));
|
||||
const HVX_Vector b = Q6_Vh_vadd_VhVh(Q6_Vh_vsub_VhVh(Q6_Vh_vsplat_R(0x6400), t), Q6_Vh_vmpy_VhVh_s1_rnd_sat(r, rho));
|
||||
return Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(b, Q6_Vh_vsplat_R(0x1400)));
|
||||
}
|
||||
#else
|
||||
static inline HVX_Vector hvx_vec_exp2_neg_f16(HVX_Vector x_v) {
|
||||
const HVX_Vector zero_v = Q6_V_vzero();
|
||||
const HVX_Vector half_hf_v = Q6_Vh_vsplat_R(0x3800); // fp16 0.5
|
||||
|
||||
@@ -230,7 +248,7 @@ static inline HVX_Vector hvx_vec_exp2_f16(HVX_Vector x_v) {
|
||||
HVX_Vector x_qf16 = Q6_Vqf16_vsub_VhfVhf(x_v, f_v); // fractional part in qf16
|
||||
|
||||
// Horner: y = ((((E5*x + E4)*x + E3)*x + E2)*x + E1)*x + E0
|
||||
HVX_Vector y = Q6_Vqf16_vmpy_Vqf16Vqf16(Q6_Vh_vsplat_R(0x5082), x_qf16); // E5*x
|
||||
HVX_Vector y = Q6_Vqf16_vmpy_Vqf16Vqf16(Q6_Vqf16_vadd_VhfVhf(Q6_Vh_vsplat_R(0x090c), zero_v), x_qf16); // E5*x
|
||||
y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x157d)); // + E4
|
||||
y = Q6_Vqf16_vmpy_Vqf16Vqf16(y, x_qf16);
|
||||
y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x20ed)); // + E3
|
||||
@@ -251,5 +269,6 @@ static inline HVX_Vector hvx_vec_exp2_f16(HVX_Vector x_v) {
|
||||
y = Q6_Vh_vaslacc_VhVhR(y, k_v, 10);
|
||||
return Q6_V_vmux_QVV(q_underflow, zero_v, y);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HVX_EXP_H */
|
||||
|
||||
@@ -70,12 +70,9 @@ static inline HVX_Vector hvx_vec_inverse_f16(HVX_Vector vals) {
|
||||
// Get mantissa for 16-bit representation
|
||||
HVX_Vector mant_recip = Q6_V_vand_VV(Q6_Vh_vasr_VhR(Q6_Vh_vasl_VhVh(rm, vcl0), 5), Q6_Vh_vsplat_R(0x03FF));
|
||||
|
||||
//Compute Reciprocal Exponent
|
||||
HVX_Vector exp_recip =
|
||||
Q6_Vh_vsub_VhVh(Q6_Vh_vsub_VhVh(Q6_Vh_vsplat_R(254), exp_u16), Q6_Vh_vsub_VhVh(vcl0, Q6_Vh_vsplat_R(1)));
|
||||
//Convert it for 16-bit representation
|
||||
exp_recip = Q6_Vh_vadd_VhVh_sat(Q6_Vh_vsub_VhVh(exp_recip, Q6_Vh_vsplat_R(127)), Q6_Vh_vsplat_R(15));
|
||||
exp_recip = Q6_Vh_vasl_VhR(exp_recip, 10);
|
||||
// Compute reciprocal exponent for 16-bit representation: (254 - exp_u16) - (vcl0 - 1) - 127 + 15
|
||||
HVX_Vector exp_recip = Q6_Vh_vsub_VhVh(Q6_Vh_vsub_VhVh(Q6_Vh_vsplat_R(143), exp_u16), vcl0);
|
||||
exp_recip = Q6_Vh_vasl_VhR(exp_recip, 10);
|
||||
|
||||
//Merge exponent and mantissa for reciprocal
|
||||
HVX_Vector recip = Q6_V_vor_VV(exp_recip, mant_recip);
|
||||
@@ -86,6 +83,21 @@ static inline HVX_Vector hvx_vec_inverse_f16(HVX_Vector vals) {
|
||||
return recip;
|
||||
}
|
||||
|
||||
// Reciprocal of positive normal fp16 in [1.0f, 16384.0f] (e.g. 1 + exp(-|x|) in sigmoid).
|
||||
static inline HVX_Vector hvx_vec_inverse_pos_f16(HVX_Vector avals) {
|
||||
HVX_Vector mant_u16 = Q6_Vh_vasl_VhR(avals, 6);
|
||||
HVX_Vector exp_u16 = Q6_Vuh_vlsr_VuhR(avals, 10);
|
||||
|
||||
HVX_Vector rm = hvx_vec_recip_xp1_O3_unsigned(mant_u16);
|
||||
HVX_Vector vcl0 = Q6_Vuh_vcl0_Vuh(rm);
|
||||
|
||||
HVX_Vector mant_recip = Q6_V_vand_VV(Q6_Vh_vasr_VhR(Q6_Vh_vasl_VhVh(rm, vcl0), 5), Q6_Vh_vsplat_R(0x03FF));
|
||||
HVX_Vector exp_recip = Q6_Vh_vsub_VhVh(Q6_Vh_vsub_VhVh(Q6_Vh_vsplat_R(31), exp_u16), vcl0);
|
||||
exp_recip = Q6_Vh_vasl_VhR(exp_recip, 10);
|
||||
|
||||
return Q6_V_vor_VV(exp_recip, mant_recip);
|
||||
}
|
||||
|
||||
static inline HVX_Vector hvx_vec_inverse_f32(HVX_Vector v_sf) {
|
||||
HVX_Vector inv_aprox_sf = Q6_V_vsplat_R(0x7EEEEBB3);
|
||||
HVX_Vector two_sf = hvx_vec_splat_f32(2.0);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "hvx-utils.h"
|
||||
#include "htp-tensor.h"
|
||||
|
||||
// Float activation copy/quantization kernels (DDR -> VTCM)
|
||||
// Float activation copy/quantization kernels (VTCM -> VTCM)
|
||||
|
||||
static inline void quantize_f32_f32_kernel(
|
||||
const uint8_t * restrict src_data,
|
||||
@@ -16,9 +16,7 @@ static inline void quantize_f32_f32_kernel(
|
||||
size_t dst_stride
|
||||
) {
|
||||
(void) tmp_data;
|
||||
const size_t src_row_size = ne0 * sizeof(float);
|
||||
for (uint32_t i = 0; i < nrows; ++i) {
|
||||
hex_l2fetch(src_data, src_row_size, src_stride, 2);
|
||||
hvx_copy_f32_au(dst_data, src_data, ne0);
|
||||
|
||||
dst_data += dst_stride;
|
||||
@@ -36,9 +34,7 @@ static inline void quantize_f32_f16_kernel(
|
||||
size_t dst_stride
|
||||
) {
|
||||
(void) tmp_data;
|
||||
const size_t src_row_size = ne0 * sizeof(float);
|
||||
for (uint32_t i = 0; i < nrows; ++i) {
|
||||
hex_l2fetch(src_data, src_row_size, src_stride, 2);
|
||||
hvx_copy_f16_f32_au(dst_data, src_data, ne0);
|
||||
|
||||
dst_data += dst_stride;
|
||||
@@ -56,9 +52,7 @@ static inline void quantize_f16_f16_kernel(
|
||||
size_t dst_stride
|
||||
) {
|
||||
(void) tmp_data;
|
||||
const size_t src_row_size = ne0 * sizeof(float);
|
||||
for (uint32_t i = 0; i < nrows; ++i) {
|
||||
hex_l2fetch(src_data, src_row_size, src_stride, 2);
|
||||
hvx_copy_f16_au(dst_data, src_data, ne0);
|
||||
|
||||
dst_data += dst_stride;
|
||||
@@ -66,8 +60,65 @@ static inline void quantize_f16_f16_kernel(
|
||||
}
|
||||
}
|
||||
|
||||
static inline void quantize_f16_f32_kernel(
|
||||
const uint8_t * restrict src_data,
|
||||
uint8_t * restrict dst_data,
|
||||
uint8_t * restrict tmp_data,
|
||||
uint32_t ne0,
|
||||
uint32_t nrows,
|
||||
size_t src_stride,
|
||||
size_t dst_stride
|
||||
) {
|
||||
(void) tmp_data;
|
||||
const size_t dst_valid_bytes = ne0 * sizeof(float);
|
||||
for (uint32_t i = 0; i < nrows; ++i) {
|
||||
hvx_copy_f32_f16_au(dst_data, src_data, ne0);
|
||||
if (dst_stride > dst_valid_bytes) {
|
||||
hvx_splat_f32_u(dst_data + dst_valid_bytes, 0.0f, (dst_stride - dst_valid_bytes) / sizeof(float));
|
||||
}
|
||||
|
||||
dst_data += dst_stride;
|
||||
src_data += src_stride;
|
||||
}
|
||||
}
|
||||
|
||||
// Float dot product kernels (HVX)
|
||||
|
||||
typedef void (*hvx_flat_store_1x1_fn_t)(void * restrict s, HVX_Vector rsum);
|
||||
typedef void (*hvx_flat_store_2x1_fn_t)(void * restrict s, HVX_Vector rsum0, HVX_Vector rsum1);
|
||||
|
||||
static __attribute__((noinline)) void hvx_flat_store_1x1_f32(void * restrict s, HVX_Vector rsum) {
|
||||
hvx_vec_store_u(s, 4, hvx_vec_reduce_sum_f32(rsum));
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_flat_store_1x1_f16(void * restrict s, HVX_Vector rsum) {
|
||||
HVX_Vector v_sum = hvx_vec_reduce_sum_f32(rsum);
|
||||
const HVX_Vector max_f16 = hvx_vec_splat_f32(65504.0f);
|
||||
const HVX_Vector min_f16 = hvx_vec_splat_f32(-65504.0f);
|
||||
v_sum = Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v_sum));
|
||||
hvx_vec_store_u(s, 2, hvx_vec_f32_to_f16(v_sum, Q6_V_vzero()));
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_flat_store_2x1_f32(void * restrict s, HVX_Vector rsum0, HVX_Vector rsum1) {
|
||||
hvx_vec_store_u(s, 8, hvx_vec_reduce_sum_f32x2(rsum0, rsum1));
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_flat_store_2x1_f16(void * restrict s, HVX_Vector rsum0, HVX_Vector rsum1) {
|
||||
HVX_Vector v_sum = hvx_vec_reduce_sum_f32x2(rsum0, rsum1);
|
||||
const HVX_Vector max_f16 = hvx_vec_splat_f32(65504.0f);
|
||||
const HVX_Vector min_f16 = hvx_vec_splat_f32(-65504.0f);
|
||||
v_sum = Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v_sum));
|
||||
hvx_vec_store_u(s, 4, hvx_vec_f32_to_f16(v_sum, Q6_V_vzero()));
|
||||
}
|
||||
|
||||
static inline hvx_flat_store_1x1_fn_t resolve_hvx_flat_store_1x1_fn(uint32_t dst_elem_size) {
|
||||
return (dst_elem_size == sizeof(__fp16)) ? hvx_flat_store_1x1_f16 : hvx_flat_store_1x1_f32;
|
||||
}
|
||||
|
||||
static inline hvx_flat_store_2x1_fn_t resolve_hvx_flat_store_2x1_fn(uint32_t dst_elem_size) {
|
||||
return (dst_elem_size == sizeof(__fp16)) ? hvx_flat_store_2x1_f16 : hvx_flat_store_2x1_f32;
|
||||
}
|
||||
|
||||
#if __HVX_ARCH__ < 79
|
||||
#define HVX_OP_ADD_F32(a, b) Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_VsfVsf(a, b))
|
||||
#define HVX_OP_MUL_F32(a, b) Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(a, b))
|
||||
@@ -76,7 +127,7 @@ static inline void quantize_f16_f16_kernel(
|
||||
#define HVX_OP_MUL_F32(a, b) Q6_Vsf_vmpy_VsfVsf(a, b)
|
||||
#endif
|
||||
|
||||
static inline void flat_vec_dot_f32_f32_aa_1x1(const uint32_t n, float * restrict s, const void * restrict vx, const void * restrict vy) {
|
||||
static inline void flat_vec_dot_f32_f32_aa_1x1(const uint32_t n, void * restrict s, const void * restrict vx, const void * restrict vy, hvx_flat_store_1x1_fn_t store_1x1) {
|
||||
const HVX_Vector * restrict x = (const HVX_Vector *) vx;
|
||||
const HVX_Vector * restrict y = (const HVX_Vector *) vy;
|
||||
|
||||
@@ -101,12 +152,12 @@ static inline void flat_vec_dot_f32_f32_aa_1x1(const uint32_t n, float * restric
|
||||
rsum = HVX_OP_ADD_F32(rsum, prod);
|
||||
}
|
||||
|
||||
*s = hvx_vec_get_f32(hvx_vec_reduce_sum_f32(rsum));
|
||||
store_1x1(s, rsum);
|
||||
}
|
||||
|
||||
static inline void flat_vec_dot_f32_f32_aa_2x1(const uint32_t n, float * restrict s0,
|
||||
static inline void flat_vec_dot_f32_f32_aa_2x1(const uint32_t n, void * restrict s0,
|
||||
const void * restrict vx0, const void * restrict vx1,
|
||||
const void * restrict vy0) {
|
||||
const void * restrict vy0, hvx_flat_store_2x1_fn_t store_2x1) {
|
||||
const HVX_Vector * restrict x0 = (const HVX_Vector *) vx0;
|
||||
const HVX_Vector * restrict x1 = (const HVX_Vector *) vx1;
|
||||
const HVX_Vector * restrict y = (const HVX_Vector *) vy0;
|
||||
@@ -139,13 +190,12 @@ static inline void flat_vec_dot_f32_f32_aa_2x1(const uint32_t n, float * restric
|
||||
rsum1 = HVX_OP_ADD_F32(rsum1, prod1);
|
||||
}
|
||||
|
||||
HVX_Vector rsum = hvx_vec_reduce_sum_f32x2(rsum0, rsum1);
|
||||
hvx_vec_store_u(s0, 8, rsum);
|
||||
store_2x1(s0, rsum0, rsum1);
|
||||
}
|
||||
|
||||
static inline void flat_vec_dot_f32_f32_aa_2x2(const uint32_t n, float * restrict s0, float * restrict s1,
|
||||
static inline void flat_vec_dot_f32_f32_aa_2x2(const uint32_t n, void * restrict s0, void * restrict s1,
|
||||
const void * restrict vx0, const void * restrict vx1,
|
||||
const void * restrict vy0, const void * restrict vy1) {
|
||||
const void * restrict vy0, const void * restrict vy1, hvx_flat_store_2x1_fn_t store_2x1) {
|
||||
const HVX_Vector * restrict x0 = (const HVX_Vector *) vx0;
|
||||
const HVX_Vector * restrict x1 = (const HVX_Vector *) vx1;
|
||||
const HVX_Vector * restrict y0 = (const HVX_Vector *) vy0;
|
||||
@@ -188,15 +238,11 @@ static inline void flat_vec_dot_f32_f32_aa_2x2(const uint32_t n, float * restric
|
||||
r1_c1_sum = HVX_OP_ADD_F32(r1_c1_sum, HVX_OP_MUL_F32(r1_sf, c1_sf));
|
||||
}
|
||||
|
||||
// Reduce and store results
|
||||
HVX_Vector r0_r1_c0_sum = hvx_vec_reduce_sum_f32x2(r0_c0_sum, r1_c0_sum);
|
||||
HVX_Vector r0_r1_c1_sum = hvx_vec_reduce_sum_f32x2(r0_c1_sum, r1_c1_sum);
|
||||
|
||||
hvx_vec_store_u(s0, 8, r0_r1_c0_sum);
|
||||
hvx_vec_store_u(s1, 8, r0_r1_c1_sum);
|
||||
store_2x1(s0, r0_c0_sum, r1_c0_sum);
|
||||
store_2x1(s1, r0_c1_sum, r1_c1_sum);
|
||||
}
|
||||
|
||||
static inline void flat_vec_dot_bf16_f32_aa_1x1(const uint32_t n, float * restrict s, const void * restrict vx, const void * restrict vy) {
|
||||
static inline void flat_vec_dot_bf16_f32_aa_1x1(const uint32_t n, void * restrict s, const void * restrict vx, const void * restrict vy, hvx_flat_store_1x1_fn_t store_1x1) {
|
||||
const HVX_Vector * restrict x = (const HVX_Vector *) vx;
|
||||
const HVX_Vector * restrict y = (const HVX_Vector *) vy;
|
||||
|
||||
@@ -240,12 +286,12 @@ static inline void flat_vec_dot_bf16_f32_aa_1x1(const uint32_t n, float * restri
|
||||
}
|
||||
|
||||
HVX_Vector rsum = HVX_OP_ADD_F32(rsum0, rsum1);
|
||||
*s = hvx_vec_get_f32(hvx_vec_reduce_sum_f32(rsum));
|
||||
store_1x1(s, rsum);
|
||||
}
|
||||
|
||||
static inline void flat_vec_dot_bf16_f32_aa_2x1(const uint32_t n, float * restrict s0,
|
||||
static inline void flat_vec_dot_bf16_f32_aa_2x1(const uint32_t n, void * restrict s0,
|
||||
const void * restrict vx0, const void * restrict vx1,
|
||||
const void * restrict vy0) {
|
||||
const void * restrict vy0, hvx_flat_store_2x1_fn_t store_2x1) {
|
||||
const HVX_Vector * restrict x0 = (const HVX_Vector *) vx0;
|
||||
const HVX_Vector * restrict x1 = (const HVX_Vector *) vx1;
|
||||
const HVX_Vector * restrict y = (const HVX_Vector *) vy0;
|
||||
@@ -301,13 +347,12 @@ static inline void flat_vec_dot_bf16_f32_aa_2x1(const uint32_t n, float * restri
|
||||
}
|
||||
}
|
||||
|
||||
HVX_Vector rsum = hvx_vec_reduce_sum_f32x2(rsum0, rsum1);
|
||||
hvx_vec_store_u(s0, 8, rsum);
|
||||
store_2x1(s0, rsum0, rsum1);
|
||||
}
|
||||
|
||||
static inline void flat_vec_dot_bf16_f32_aa_2x2(const uint32_t n, float * restrict s0, float * restrict s1,
|
||||
static inline void flat_vec_dot_bf16_f32_aa_2x2(const uint32_t n, void * restrict s0, void * restrict s1,
|
||||
const void * restrict vx0, const void * restrict vx1,
|
||||
const void * restrict vy0, const void * restrict vy1) {
|
||||
const void * restrict vy0, const void * restrict vy1, hvx_flat_store_2x1_fn_t store_2x1) {
|
||||
const HVX_Vector * restrict x0 = (const HVX_Vector *) vx0;
|
||||
const HVX_Vector * restrict x1 = (const HVX_Vector *) vx1;
|
||||
const HVX_Vector * restrict y0 = (const HVX_Vector *) vy0;
|
||||
@@ -382,17 +427,14 @@ static inline void flat_vec_dot_bf16_f32_aa_2x2(const uint32_t n, float * restri
|
||||
}
|
||||
}
|
||||
|
||||
HVX_Vector r0_r1_c0_sum = hvx_vec_reduce_sum_f32x2(r0_c0_sum, r1_c0_sum);
|
||||
HVX_Vector r0_r1_c1_sum = hvx_vec_reduce_sum_f32x2(r0_c1_sum, r1_c1_sum);
|
||||
|
||||
hvx_vec_store_u(s0, 8, r0_r1_c0_sum);
|
||||
hvx_vec_store_u(s1, 8, r0_r1_c1_sum);
|
||||
store_2x1(s0, r0_c0_sum, r1_c0_sum);
|
||||
store_2x1(s1, r0_c1_sum, r1_c1_sum);
|
||||
}
|
||||
|
||||
#undef HVX_OP_ADD_F32
|
||||
#undef HVX_OP_MUL_F32
|
||||
|
||||
static inline void flat_vec_dot_f16_f16_aa_1x1(const uint32_t n, float * restrict s, const void * restrict vx, const void * restrict vy) {
|
||||
static inline void flat_vec_dot_f16_f16_aa_1x1(const uint32_t n, void * restrict s, const void * restrict vx, const void * restrict vy, hvx_flat_store_1x1_fn_t store_1x1) {
|
||||
const HVX_Vector * restrict x = (const HVX_Vector *) vx;
|
||||
const HVX_Vector * restrict y = (const HVX_Vector *) vy;
|
||||
|
||||
@@ -416,12 +458,12 @@ static inline void flat_vec_dot_f16_f16_aa_1x1(const uint32_t n, float * restric
|
||||
}
|
||||
|
||||
HVX_Vector rsum = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_VsfVsf(Q6_V_lo_W(rsum_p), Q6_V_hi_W(rsum_p)));
|
||||
hvx_vec_store_u(s, 4, hvx_vec_reduce_sum_f32(rsum));
|
||||
store_1x1(s, rsum);
|
||||
}
|
||||
|
||||
static inline void flat_vec_dot_f16_f16_aa_2x1(const uint32_t n, float * restrict s0,
|
||||
static inline void flat_vec_dot_f16_f16_aa_2x1(const uint32_t n, void * restrict s0,
|
||||
const void * restrict vx0, const void * restrict vx1,
|
||||
const void * restrict vy0) {
|
||||
const void * restrict vy0, hvx_flat_store_2x1_fn_t store_2x1) {
|
||||
const HVX_Vector * restrict x0 = (const HVX_Vector *) vx0;
|
||||
const HVX_Vector * restrict x1 = (const HVX_Vector *) vx1;
|
||||
const HVX_Vector * restrict y = (const HVX_Vector *) vy0;
|
||||
@@ -452,13 +494,12 @@ static inline void flat_vec_dot_f16_f16_aa_2x1(const uint32_t n, float * restric
|
||||
|
||||
HVX_Vector rsum0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_VsfVsf(Q6_V_lo_W(rsum0_p), Q6_V_hi_W(rsum0_p)));
|
||||
HVX_Vector rsum1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_VsfVsf(Q6_V_lo_W(rsum1_p), Q6_V_hi_W(rsum1_p)));
|
||||
HVX_Vector rsum = hvx_vec_reduce_sum_f32x2(rsum0, rsum1);
|
||||
hvx_vec_store_u(s0, 8, rsum);
|
||||
store_2x1(s0, rsum0, rsum1);
|
||||
}
|
||||
|
||||
static inline void flat_vec_dot_f16_f16_aa_2x2(const uint32_t n, float * restrict s0, float * restrict s1,
|
||||
static inline void flat_vec_dot_f16_f16_aa_2x2(const uint32_t n, void * restrict s0, void * restrict s1,
|
||||
const void * restrict vx0, const void * restrict vx1,
|
||||
const void * restrict vy0, const void * restrict vy1) {
|
||||
const void * restrict vy0, const void * restrict vy1, hvx_flat_store_2x1_fn_t store_2x1) {
|
||||
const HVX_Vector * restrict x0 = (const HVX_Vector *) vx0;
|
||||
const HVX_Vector * restrict x1 = (const HVX_Vector *) vx1;
|
||||
const HVX_Vector * restrict y0 = (const HVX_Vector *) vy0;
|
||||
@@ -508,19 +549,110 @@ static inline void flat_vec_dot_f16_f16_aa_2x2(const uint32_t n, float * restric
|
||||
HVX_Vector r1_c0_sum = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_VsfVsf(Q6_V_lo_W(r1_c0_sum_p), Q6_V_hi_W(r1_c0_sum_p)));
|
||||
HVX_Vector r1_c1_sum = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_VsfVsf(Q6_V_lo_W(r1_c1_sum_p), Q6_V_hi_W(r1_c1_sum_p)));
|
||||
|
||||
// Reduce and store results
|
||||
HVX_Vector r0_r1_c0_sum = hvx_vec_reduce_sum_f32x2(r0_c0_sum, r1_c0_sum);
|
||||
HVX_Vector r0_r1_c1_sum = hvx_vec_reduce_sum_f32x2(r0_c1_sum, r1_c1_sum);
|
||||
|
||||
hvx_vec_store_u(&s0[0], 8, r0_r1_c0_sum); // row0,col0 row1,col0
|
||||
hvx_vec_store_u(&s1[0], 8, r0_r1_c1_sum); // row0,col1 row1,col1
|
||||
store_2x1(s0, r0_c0_sum, r1_c0_sum);
|
||||
store_2x1(s1, r0_c1_sum, r1_c1_sum);
|
||||
}
|
||||
|
||||
typedef void (*hvx_tensor_add_row_fn_t)(void * restrict dst_ptr, const void * restrict bias_ptr, uint32_t n);
|
||||
|
||||
static __attribute__((noinline)) void hvx_tensor_add_row_f32_f32(void * restrict dst_ptr, const void * restrict bias_ptr, uint32_t n) {
|
||||
float * restrict d = (float *) dst_ptr;
|
||||
const float * restrict b = (const float *) bias_ptr;
|
||||
uint32_t c = 0;
|
||||
for (; c + 32 <= n; c += 32) {
|
||||
hvx_vmemu(d + c) = hvx_vec_add_f32_f32(hvx_vmemu(d + c), hvx_vmemu(b + c));
|
||||
}
|
||||
if (c < n) {
|
||||
const uint32_t rem = n - c;
|
||||
HVX_Vector v_sum = hvx_vec_add_f32_f32(hvx_vmemu(d + c), hvx_vmemu(b + c));
|
||||
hvx_vec_store_u(d + c, rem * sizeof(float), v_sum);
|
||||
}
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_tensor_add_row_f32_f16(void * restrict dst_ptr, const void * restrict bias_ptr, uint32_t n) {
|
||||
float * restrict d = (float *) dst_ptr;
|
||||
const __fp16 * restrict b = (const __fp16 *) bias_ptr;
|
||||
uint32_t c = 0;
|
||||
for (; c + 64 <= n; c += 64) {
|
||||
HVX_VectorPair vp_b = hvx_vec_f16_to_f32(hvx_vmemu(b + c));
|
||||
hvx_vmemu(d + c + 0) = hvx_vec_add_f32_f32(hvx_vmemu(d + c + 0), Q6_V_lo_W(vp_b));
|
||||
hvx_vmemu(d + c + 32) = hvx_vec_add_f32_f32(hvx_vmemu(d + c + 32), Q6_V_hi_W(vp_b));
|
||||
}
|
||||
if (c + 32 <= n) {
|
||||
HVX_Vector v_b = Q6_V_lo_W(hvx_vec_f16_to_f32(hvx_vmemu(b + c)));
|
||||
hvx_vmemu(d + c) = hvx_vec_add_f32_f32(hvx_vmemu(d + c), v_b);
|
||||
c += 32;
|
||||
}
|
||||
if (c < n) {
|
||||
const uint32_t rem = n - c;
|
||||
HVX_Vector v_b = Q6_V_lo_W(hvx_vec_f16_to_f32(hvx_vmemu(b + c)));
|
||||
HVX_Vector v_sum = hvx_vec_add_f32_f32(hvx_vmemu(d + c), v_b);
|
||||
hvx_vec_store_u(d + c, rem * sizeof(float), v_sum);
|
||||
}
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_tensor_add_row_f16_f32(void * restrict dst_ptr, const void * restrict bias_ptr, uint32_t n) {
|
||||
__fp16 * restrict d = (__fp16 *) dst_ptr;
|
||||
const float * restrict b = (const float *) bias_ptr;
|
||||
const HVX_Vector max_f16 = hvx_vec_splat_f32(65504.0f);
|
||||
const HVX_Vector min_f16 = hvx_vec_splat_f32(-65504.0f);
|
||||
uint32_t c = 0;
|
||||
for (; c + 64 <= n; c += 64) {
|
||||
HVX_VectorPair vp_d = hvx_vec_f16_to_f32(hvx_vmemu(d + c));
|
||||
HVX_Vector v0 = hvx_vec_add_f32_f32(Q6_V_lo_W(vp_d), hvx_vmemu(b + c + 0));
|
||||
HVX_Vector v1 = hvx_vec_add_f32_f32(Q6_V_hi_W(vp_d), hvx_vmemu(b + c + 32));
|
||||
v0 = Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v0));
|
||||
v1 = Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v1));
|
||||
hvx_vmemu(d + c) = hvx_vec_f32_to_f16(v0, v1);
|
||||
}
|
||||
if (c < n) {
|
||||
const uint32_t rem = n - c;
|
||||
HVX_VectorPair vp_d = hvx_vec_f16_to_f32(hvx_vmemu(d + c));
|
||||
HVX_Vector v0 = hvx_vec_add_f32_f32(Q6_V_lo_W(vp_d), hvx_vmemu(b + c));
|
||||
HVX_Vector v1 = (rem > 32) ? hvx_vec_add_f32_f32(Q6_V_hi_W(vp_d), hvx_vmemu(b + c + 32)) : Q6_V_vzero();
|
||||
v0 = Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v0));
|
||||
v1 = Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v1));
|
||||
hvx_vec_store_u(d + c, rem * sizeof(__fp16), hvx_vec_f32_to_f16(v0, v1));
|
||||
}
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_tensor_add_row_f16_f16(void * restrict dst_ptr, const void * restrict bias_ptr, uint32_t n) {
|
||||
__fp16 * restrict d = (__fp16 *) dst_ptr;
|
||||
const __fp16 * restrict b = (const __fp16 *) bias_ptr;
|
||||
const HVX_Vector max_f16 = hvx_vec_splat_f32(65504.0f);
|
||||
const HVX_Vector min_f16 = hvx_vec_splat_f32(-65504.0f);
|
||||
uint32_t c = 0;
|
||||
for (; c + 64 <= n; c += 64) {
|
||||
HVX_VectorPair vp_d = hvx_vec_f16_to_f32(hvx_vmemu(d + c));
|
||||
HVX_VectorPair vp_b = hvx_vec_f16_to_f32(hvx_vmemu(b + c));
|
||||
HVX_Vector v0 = hvx_vec_add_f32_f32(Q6_V_lo_W(vp_d), Q6_V_lo_W(vp_b));
|
||||
HVX_Vector v1 = hvx_vec_add_f32_f32(Q6_V_hi_W(vp_d), Q6_V_hi_W(vp_b));
|
||||
v0 = Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v0));
|
||||
v1 = Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v1));
|
||||
hvx_vmemu(d + c) = hvx_vec_f32_to_f16(v0, v1);
|
||||
}
|
||||
if (c < n) {
|
||||
const uint32_t rem = n - c;
|
||||
HVX_VectorPair vp_d = hvx_vec_f16_to_f32(hvx_vmemu(d + c));
|
||||
HVX_VectorPair vp_b = hvx_vec_f16_to_f32(hvx_vmemu(b + c));
|
||||
HVX_Vector v0 = hvx_vec_add_f32_f32(Q6_V_lo_W(vp_d), Q6_V_lo_W(vp_b));
|
||||
HVX_Vector v1 = (rem > 32) ? hvx_vec_add_f32_f32(Q6_V_hi_W(vp_d), Q6_V_hi_W(vp_b)) : Q6_V_vzero();
|
||||
v0 = Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v0));
|
||||
v1 = Q6_Vsf_vmax_VsfVsf(min_f16, Q6_Vsf_vmin_VsfVsf(max_f16, v1));
|
||||
hvx_vec_store_u(d + c, rem * sizeof(__fp16), hvx_vec_f32_to_f16(v0, v1));
|
||||
}
|
||||
}
|
||||
|
||||
static inline hvx_tensor_add_row_fn_t resolve_hvx_tensor_add_row_fn(uint32_t dst_elem_size, uint32_t bias_elem_size) {
|
||||
if (dst_elem_size == sizeof(__fp16)) {
|
||||
return (bias_elem_size == sizeof(__fp16)) ? hvx_tensor_add_row_f16_f16 : hvx_tensor_add_row_f16_f32;
|
||||
}
|
||||
return (bias_elem_size == sizeof(__fp16)) ? hvx_tensor_add_row_f32_f16 : hvx_tensor_add_row_f32_f32;
|
||||
}
|
||||
|
||||
static inline void hvx_tensor_add_f32_grid(
|
||||
const struct htp_tensor * restrict dst,
|
||||
const struct htp_tensor * restrict src2,
|
||||
const struct htp_tensor * restrict bias,
|
||||
uint32_t start_row,
|
||||
uint32_t end_row,
|
||||
uint32_t start_col,
|
||||
@@ -529,46 +661,33 @@ static inline void hvx_tensor_add_f32_grid(
|
||||
const struct fastdiv_values * div_ne11
|
||||
) {
|
||||
if (start_row >= end_row || start_col >= end_col) return;
|
||||
const uint32_t nb1 = dst->nb[1]; // row stride in bytes
|
||||
const uint32_t nb1 = dst->nb[1];
|
||||
const uint32_t dst_elem_size = (dst->type == HTP_TYPE_F16) ? sizeof(__fp16) : sizeof(float);
|
||||
const uint32_t bias_elem_size = (bias->type == HTP_TYPE_F16) ? sizeof(__fp16) : sizeof(float);
|
||||
const hvx_tensor_add_row_fn_t add_row_fn = resolve_hvx_tensor_add_row_fn(dst_elem_size, bias_elem_size);
|
||||
const uint32_t n_cols = end_col - start_col;
|
||||
const size_t dst_col_off = (size_t) start_col * dst_elem_size;
|
||||
const size_t bias_col_off = (size_t) start_col * bias_elem_size;
|
||||
|
||||
const uint32_t ne11 = dst->ne[1];
|
||||
const uint32_t ne12 = dst->ne[2];
|
||||
const uint32_t ne11_12 = ne11 * ne12;
|
||||
|
||||
const bool is_broadcast1 = (src2->ne[1] == 1);
|
||||
const bool is_broadcast2 = (src2->ne[2] == 1);
|
||||
const bool is_broadcast3 = (src2->ne[3] == 1);
|
||||
const size_t bias_nb1 = (bias->ne[1] == 1) ? 0 : bias->nb[1];
|
||||
const size_t bias_nb2 = (bias->ne[2] == 1) ? 0 : bias->nb[2];
|
||||
const size_t bias_nb3 = (bias->ne[3] == 1) ? 0 : bias->nb[3];
|
||||
|
||||
for (uint32_t r = start_row; r < end_row; r++) {
|
||||
float * dst_row = (float *) ((uint8_t *) dst->data + (size_t) r * nb1);
|
||||
|
||||
uint32_t i13 = fastdiv(r, div_ne11_12);
|
||||
uint32_t i12 = fastdiv(r - i13 * ne11_12, div_ne11);
|
||||
uint32_t i11 = r - i13 * ne11_12 - i12 * ne11;
|
||||
|
||||
uint32_t i23 = is_broadcast3 ? 0 : i13;
|
||||
uint32_t i22 = is_broadcast2 ? 0 : i12;
|
||||
uint32_t i21 = is_broadcast1 ? 0 : i11;
|
||||
uint8_t * dst_ptr = (uint8_t *) dst->data +
|
||||
(size_t) i11 * nb1 + (size_t) i12 * dst->nb[2] + (size_t) i13 * dst->nb[3] + dst_col_off;
|
||||
const uint8_t * bias_ptr = (const uint8_t *) bias->data +
|
||||
(size_t) i11 * bias_nb1 + (size_t) i12 * bias_nb2 + (size_t) i13 * bias_nb3 + bias_col_off;
|
||||
|
||||
const float * src2_row = (const float *) ((const uint8_t *) src2->data +
|
||||
(size_t) i21 * src2->nb[1] + (size_t) i22 * src2->nb[2] + (size_t) i23 * src2->nb[3]);
|
||||
|
||||
float * dst_ptr = &dst_row[start_col];
|
||||
const float * src2_ptr = &src2_row[start_col];
|
||||
int remaining = end_col - start_col;
|
||||
while (remaining >= 32) {
|
||||
HVX_Vector v_out = hvx_vmemu(dst_ptr);
|
||||
HVX_Vector v_z = hvx_vmemu(src2_ptr);
|
||||
hvx_vmemu(dst_ptr) = hvx_vec_add_f32_f32(v_out, v_z);
|
||||
dst_ptr += 32;
|
||||
src2_ptr += 32;
|
||||
remaining -= 32;
|
||||
}
|
||||
if (remaining > 0) {
|
||||
HVX_Vector v_out = hvx_vmemu(dst_ptr);
|
||||
HVX_Vector v_z = hvx_vmemu(src2_ptr);
|
||||
hvx_vec_store_u(dst_ptr, remaining * sizeof(float), hvx_vec_add_f32_f32(v_out, v_z));
|
||||
}
|
||||
add_row_fn(dst_ptr, bias_ptr, n_cols);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -341,29 +341,48 @@ static inline void hvx_fast_rms_norm_f16(const uint8_t * restrict src,
|
||||
const int nvec = num_elems / VLEN_FP16; // number of full f16 vectors
|
||||
const int nloe = num_elems % VLEN_FP16; // leftover elements
|
||||
|
||||
HVX_Vector sum_v = Q6_V_vsplat_R(0x00000000);
|
||||
HVX_Vector epsilon_v = hvx_vec_splat_f32(epsilon);
|
||||
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_VectorPair sum_p = Q6_W_vzero();
|
||||
|
||||
#pragma unroll(4)
|
||||
for (int i = 0; i < nvec; i++) {
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v_src[i]);
|
||||
HVX_Vector p0 = Q6_V_lo_W(p);
|
||||
HVX_Vector p1 = Q6_V_hi_W(p);
|
||||
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, Q6_Vqf32_vmpy_VsfVsf(p0, p0));
|
||||
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, Q6_Vqf32_vmpy_VsfVsf(p1, p1));
|
||||
HVX_Vector v = v_src[i];
|
||||
sum_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_p, v, v);
|
||||
}
|
||||
|
||||
if (nloe > 0) {
|
||||
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
|
||||
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v1);
|
||||
HVX_Vector p0 = Q6_V_lo_W(p);
|
||||
HVX_Vector p1 = Q6_V_hi_W(p);
|
||||
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, Q6_Vqf32_vmpy_VsfVsf(p0, p0));
|
||||
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, Q6_Vqf32_vmpy_VsfVsf(p1, p1));
|
||||
HVX_Vector v = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
sum_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_p, v, v);
|
||||
}
|
||||
|
||||
sum_v = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_v));
|
||||
HVX_Vector sum_v = Q6_Vsf_vadd_VsfVsf(Q6_V_lo_W(sum_p), Q6_V_hi_W(sum_p));
|
||||
#else
|
||||
HVX_Vector sum0 = Q6_V_vzero();
|
||||
HVX_Vector sum1 = Q6_V_vzero();
|
||||
|
||||
#pragma unroll(4)
|
||||
for (int i = 0; i < nvec; i++) {
|
||||
HVX_Vector v = v_src[i];
|
||||
HVX_VectorPair sq = Q6_Wqf32_vmpy_VhfVhf(v, v);
|
||||
sum0 = Q6_Vqf32_vadd_Vqf32Vqf32(sum0, Q6_V_lo_W(sq));
|
||||
sum1 = Q6_Vqf32_vadd_Vqf32Vqf32(sum1, Q6_V_hi_W(sq));
|
||||
}
|
||||
|
||||
if (nloe > 0) {
|
||||
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
|
||||
HVX_Vector v = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
HVX_VectorPair sq = Q6_Wqf32_vmpy_VhfVhf(v, v);
|
||||
sum0 = Q6_Vqf32_vadd_Vqf32Vqf32(sum0, Q6_V_lo_W(sq));
|
||||
sum1 = Q6_Vqf32_vadd_Vqf32Vqf32(sum1, Q6_V_hi_W(sq));
|
||||
}
|
||||
|
||||
HVX_Vector sum_v = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(sum0, sum1));
|
||||
#endif
|
||||
|
||||
sum_v = hvx_vec_reduce_sum_f32(sum_v);
|
||||
|
||||
HVX_Vector t_v = hvx_vec_splat_f32((float) num_elems);
|
||||
HVX_Vector denom_v = hvx_vec_inverse_f32(t_v);
|
||||
@@ -374,19 +393,19 @@ static inline void hvx_fast_rms_norm_f16(const uint8_t * restrict src,
|
||||
|
||||
#pragma unroll(4)
|
||||
for (int i = 0; i < nvec; i++) {
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v_src[i]);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32_shuff(v_src[i]);
|
||||
HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(p), scale_v));
|
||||
HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(p), scale_v));
|
||||
v_dst[i] = hvx_vec_f32_to_f16(r0, r1);
|
||||
v_dst[i] = hvx_vec_f32_to_f16_shuff(r0, r1);
|
||||
}
|
||||
|
||||
if (nloe > 0) {
|
||||
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
|
||||
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v1);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32_shuff(v1);
|
||||
HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(p), scale_v));
|
||||
HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(p), scale_v));
|
||||
HVX_Vector result = hvx_vec_f32_to_f16(r0, r1);
|
||||
HVX_Vector result = hvx_vec_f32_to_f16_shuff(r0, r1);
|
||||
hvx_vec_store_a(&v_dst[nvec], nloe * SIZEOF_FP16, result);
|
||||
}
|
||||
}
|
||||
@@ -402,35 +421,63 @@ static inline void hvx_fast_norm_f16(const uint8_t * restrict src,
|
||||
const int nvec = num_elems / VLEN_FP16;
|
||||
const int nloe = num_elems % VLEN_FP16;
|
||||
|
||||
HVX_Vector sum_sq_v = Q6_V_vsplat_R(0x00000000);
|
||||
HVX_Vector sum_x_v = Q6_V_vsplat_R(0x00000000);
|
||||
const HVX_Vector one_hf = hvx_vec_splat_f16(1.0);
|
||||
HVX_Vector epsilon_v = hvx_vec_splat_f32(epsilon);
|
||||
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_VectorPair sum_sq_p = Q6_W_vzero();
|
||||
HVX_VectorPair sum_x_p = Q6_W_vzero();
|
||||
|
||||
#pragma unroll(4)
|
||||
for (int i = 0; i < nvec; i++) {
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v_src[i]);
|
||||
HVX_Vector p0 = Q6_V_lo_W(p);
|
||||
HVX_Vector p1 = Q6_V_hi_W(p);
|
||||
sum_sq_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq_v, Q6_Vqf32_vmpy_VsfVsf(p0, p0));
|
||||
sum_sq_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq_v, Q6_Vqf32_vmpy_VsfVsf(p1, p1));
|
||||
sum_x_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_x_v, Q6_Vqf32_vadd_VsfVsf(p0, Q6_V_vzero()));
|
||||
sum_x_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_x_v, Q6_Vqf32_vadd_VsfVsf(p1, Q6_V_vzero()));
|
||||
HVX_Vector v = v_src[i];
|
||||
sum_sq_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_sq_p, v, v);
|
||||
sum_x_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_x_p, v, one_hf);
|
||||
}
|
||||
|
||||
if (nloe > 0) {
|
||||
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
|
||||
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v1);
|
||||
HVX_Vector p0 = Q6_V_lo_W(p);
|
||||
HVX_Vector p1 = Q6_V_hi_W(p);
|
||||
sum_sq_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq_v, Q6_Vqf32_vmpy_VsfVsf(p0, p0));
|
||||
sum_sq_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq_v, Q6_Vqf32_vmpy_VsfVsf(p1, p1));
|
||||
sum_x_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_x_v, Q6_Vqf32_vadd_VsfVsf(p0, Q6_V_vzero()));
|
||||
sum_x_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_x_v, Q6_Vqf32_vadd_VsfVsf(p1, Q6_V_vzero()));
|
||||
HVX_Vector v = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
sum_sq_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_sq_p, v, v);
|
||||
sum_x_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_x_p, v, one_hf);
|
||||
}
|
||||
|
||||
sum_sq_v = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_sq_v));
|
||||
sum_x_v = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_x_v));
|
||||
HVX_Vector sum_sq_v = Q6_Vsf_vadd_VsfVsf(Q6_V_lo_W(sum_sq_p), Q6_V_hi_W(sum_sq_p));
|
||||
HVX_Vector sum_x_v = Q6_Vsf_vadd_VsfVsf(Q6_V_lo_W(sum_x_p), Q6_V_hi_W(sum_x_p));
|
||||
#else
|
||||
HVX_Vector sum_sq0 = Q6_V_vzero();
|
||||
HVX_Vector sum_sq1 = Q6_V_vzero();
|
||||
HVX_Vector sum_x0 = Q6_V_vzero();
|
||||
HVX_Vector sum_x1 = Q6_V_vzero();
|
||||
|
||||
#pragma unroll(4)
|
||||
for (int i = 0; i < nvec; i++) {
|
||||
HVX_Vector v = v_src[i];
|
||||
HVX_VectorPair sq = Q6_Wqf32_vmpy_VhfVhf(v, v);
|
||||
HVX_VectorPair xp = Q6_Wqf32_vmpy_VhfVhf(v, one_hf);
|
||||
sum_sq0 = Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq0, Q6_V_lo_W(sq));
|
||||
sum_sq1 = Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq1, Q6_V_hi_W(sq));
|
||||
sum_x0 = Q6_Vqf32_vadd_Vqf32Vqf32(sum_x0, Q6_V_lo_W(xp));
|
||||
sum_x1 = Q6_Vqf32_vadd_Vqf32Vqf32(sum_x1, Q6_V_hi_W(xp));
|
||||
}
|
||||
|
||||
if (nloe > 0) {
|
||||
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
|
||||
HVX_Vector v = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
HVX_VectorPair sq = Q6_Wqf32_vmpy_VhfVhf(v, v);
|
||||
HVX_VectorPair xp = Q6_Wqf32_vmpy_VhfVhf(v, one_hf);
|
||||
sum_sq0 = Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq0, Q6_V_lo_W(sq));
|
||||
sum_sq1 = Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq1, Q6_V_hi_W(sq));
|
||||
sum_x0 = Q6_Vqf32_vadd_Vqf32Vqf32(sum_x0, Q6_V_lo_W(xp));
|
||||
sum_x1 = Q6_Vqf32_vadd_Vqf32Vqf32(sum_x1, Q6_V_hi_W(xp));
|
||||
}
|
||||
|
||||
HVX_Vector sum_sq_v = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq0, sum_sq1));
|
||||
HVX_Vector sum_x_v = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(sum_x0, sum_x1));
|
||||
#endif
|
||||
|
||||
sum_sq_v = hvx_vec_reduce_sum_f32(sum_sq_v);
|
||||
sum_x_v = hvx_vec_reduce_sum_f32(sum_x_v);
|
||||
|
||||
HVX_Vector t_v = hvx_vec_splat_f32((float) num_elems);
|
||||
HVX_Vector denom_v = hvx_vec_inverse_f32(t_v);
|
||||
@@ -445,23 +492,23 @@ static inline void hvx_fast_norm_f16(const uint8_t * restrict src,
|
||||
|
||||
#pragma unroll(4)
|
||||
for (int i = 0; i < nvec; i++) {
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v_src[i]);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32_shuff(v_src[i]);
|
||||
HVX_Vector d0 = Q6_Vqf32_vsub_VsfVsf(Q6_V_lo_W(p), mean_x_b);
|
||||
HVX_Vector d1 = Q6_Vqf32_vsub_VsfVsf(Q6_V_hi_W(p), mean_x_b);
|
||||
HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(d0), scale_v));
|
||||
HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(d1), scale_v));
|
||||
v_dst[i] = hvx_vec_f32_to_f16(r0, r1);
|
||||
v_dst[i] = hvx_vec_f32_to_f16_shuff(r0, r1);
|
||||
}
|
||||
|
||||
if (nloe > 0) {
|
||||
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
|
||||
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v1);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32_shuff(v1);
|
||||
HVX_Vector d0 = Q6_Vqf32_vsub_VsfVsf(Q6_V_lo_W(p), mean_x_b);
|
||||
HVX_Vector d1 = Q6_Vqf32_vsub_VsfVsf(Q6_V_hi_W(p), mean_x_b);
|
||||
HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(d0), scale_v));
|
||||
HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(d1), scale_v));
|
||||
HVX_Vector result = hvx_vec_f32_to_f16(r0, r1);
|
||||
HVX_Vector result = hvx_vec_f32_to_f16_shuff(r0, r1);
|
||||
hvx_vec_store_a(&v_dst[nvec], nloe * SIZEOF_FP16, result);
|
||||
}
|
||||
}
|
||||
@@ -477,51 +524,224 @@ static inline void hvx_fast_l2_norm_f16(const uint8_t * restrict src,
|
||||
const int nvec = num_elems / VLEN_FP16;
|
||||
const int nloe = num_elems % VLEN_FP16;
|
||||
|
||||
HVX_Vector sum_v = hvx_vec_splat_f32(0.0f);
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_VectorPair sum_p = Q6_W_vzero();
|
||||
|
||||
#pragma unroll(4)
|
||||
for (int i = 0; i < nvec; i++) {
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v_src[i]);
|
||||
HVX_Vector p0 = Q6_V_lo_W(p);
|
||||
HVX_Vector p1 = Q6_V_hi_W(p);
|
||||
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, Q6_Vqf32_vmpy_VsfVsf(p0, p0));
|
||||
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, Q6_Vqf32_vmpy_VsfVsf(p1, p1));
|
||||
HVX_Vector v = v_src[i];
|
||||
sum_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_p, v, v);
|
||||
}
|
||||
|
||||
if (nloe > 0) {
|
||||
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
|
||||
HVX_Vector v = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
sum_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_p, v, v);
|
||||
}
|
||||
|
||||
HVX_Vector sum_sf = Q6_Vsf_vadd_VsfVsf(Q6_V_lo_W(sum_p), Q6_V_hi_W(sum_p));
|
||||
#else
|
||||
HVX_Vector sum0 = Q6_V_vzero();
|
||||
HVX_Vector sum1 = Q6_V_vzero();
|
||||
|
||||
#pragma unroll(4)
|
||||
for (int i = 0; i < nvec; i++) {
|
||||
HVX_Vector v = v_src[i];
|
||||
HVX_VectorPair sq = Q6_Wqf32_vmpy_VhfVhf(v, v);
|
||||
sum0 = Q6_Vqf32_vadd_Vqf32Vqf32(sum0, Q6_V_lo_W(sq));
|
||||
sum1 = Q6_Vqf32_vadd_Vqf32Vqf32(sum1, Q6_V_hi_W(sq));
|
||||
}
|
||||
|
||||
if (nloe > 0) {
|
||||
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
|
||||
HVX_Vector v = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
HVX_VectorPair sq = Q6_Wqf32_vmpy_VhfVhf(v, v);
|
||||
sum0 = Q6_Vqf32_vadd_Vqf32Vqf32(sum0, Q6_V_lo_W(sq));
|
||||
sum1 = Q6_Vqf32_vadd_Vqf32Vqf32(sum1, Q6_V_hi_W(sq));
|
||||
}
|
||||
|
||||
HVX_Vector sum_sf = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(sum0, sum1));
|
||||
#endif
|
||||
|
||||
sum_sf = hvx_vec_reduce_sum_f32(sum_sf);
|
||||
HVX_Vector eps2_v = hvx_vec_splat_f32(epsilon * epsilon);
|
||||
HVX_Vector scale_v = hvx_vec_rsqrt_f32(Q6_Vsf_vmax_VsfVsf(sum_sf, eps2_v));
|
||||
|
||||
#pragma unroll(4)
|
||||
for (int i = 0; i < nvec; i++) {
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32_shuff(v_src[i]);
|
||||
HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(p), scale_v));
|
||||
HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(p), scale_v));
|
||||
v_dst[i] = hvx_vec_f32_to_f16_shuff(r0, r1);
|
||||
}
|
||||
|
||||
if (nloe > 0) {
|
||||
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
|
||||
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v1);
|
||||
HVX_Vector p0 = Q6_V_lo_W(p);
|
||||
HVX_Vector p1 = Q6_V_hi_W(p);
|
||||
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, Q6_Vqf32_vmpy_VsfVsf(p0, p0));
|
||||
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, Q6_Vqf32_vmpy_VsfVsf(p1, p1));
|
||||
}
|
||||
|
||||
HVX_Vector sum_sf = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_v));
|
||||
HVX_Vector rsqrt_v = hvx_vec_rsqrt_f32(sum_sf);
|
||||
HVX_Vector sqrt_v = hvx_vec_inverse_f32(rsqrt_v);
|
||||
HVX_Vector epsilon_v = hvx_vec_splat_f32(epsilon);
|
||||
HVX_Vector denom_v = Q6_Vsf_vmax_VsfVsf(sqrt_v, epsilon_v);
|
||||
HVX_Vector scale_v = hvx_vec_inverse_f32(denom_v);
|
||||
|
||||
#pragma unroll(4)
|
||||
for (int i = 0; i < nvec; i++) {
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v_src[i]);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32_shuff(v1);
|
||||
HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(p), scale_v));
|
||||
HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(p), scale_v));
|
||||
v_dst[i] = hvx_vec_f32_to_f16(r0, r1);
|
||||
}
|
||||
|
||||
if (nloe > 0) {
|
||||
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * SIZEOF_FP16);
|
||||
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
|
||||
HVX_VectorPair p = hvx_vec_f16_to_f32(v1);
|
||||
HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_lo_W(p), scale_v));
|
||||
HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_hi_W(p), scale_v));
|
||||
HVX_Vector result = hvx_vec_f32_to_f16(r0, r1);
|
||||
HVX_Vector result = hvx_vec_f32_to_f16_shuff(r0, r1);
|
||||
hvx_vec_store_a(&v_dst[nvec], nloe * SIZEOF_FP16, result);
|
||||
}
|
||||
}
|
||||
|
||||
static __attribute__((noinline, unused)) void hvx_rms_norm_mul_f16_w32_ne128_bcast_rows(
|
||||
const uint8_t * restrict src,
|
||||
const uint8_t * restrict weight,
|
||||
uint8_t * restrict dst,
|
||||
uint32_t num_rows,
|
||||
size_t src_stride,
|
||||
size_t dst_stride,
|
||||
float epsilon,
|
||||
float scale) {
|
||||
const HVX_Vector * restrict v_w = (const HVX_Vector *) weight;
|
||||
const HVX_VectorPair wp0 = Q6_W_vdeal_VVR(v_w[1], v_w[0], -4);
|
||||
const HVX_VectorPair wp1 = Q6_W_vdeal_VVR(v_w[3], v_w[2], -4);
|
||||
const HVX_Vector w0 = Q6_V_lo_W(wp0);
|
||||
const HVX_Vector w1 = Q6_V_hi_W(wp0);
|
||||
const HVX_Vector w2 = Q6_V_lo_W(wp1);
|
||||
const HVX_Vector w3 = Q6_V_hi_W(wp1);
|
||||
|
||||
const HVX_Vector v_one_f16 = Q6_Vh_vsplat_R(0x3c00);
|
||||
const HVX_Vector denom_v = hvx_vec_splat_f32(1.0f / 128.0f);
|
||||
const HVX_Vector epsilon_v = hvx_vec_splat_f32(epsilon);
|
||||
const HVX_Vector scale_opt = hvx_vec_splat_f32(scale);
|
||||
const bool has_scale = (scale != 0.0f);
|
||||
|
||||
#pragma nounroll
|
||||
for (uint32_t ir = 0; ir < num_rows; ++ir) {
|
||||
const HVX_Vector * restrict v_src = (const HVX_Vector *) (src + ir * src_stride);
|
||||
HVX_Vector * restrict v_dst = (HVX_Vector *) (dst + ir * dst_stride);
|
||||
|
||||
const HVX_Vector s0_hf = v_src[0];
|
||||
const HVX_Vector s1_hf = v_src[1];
|
||||
const HVX_VectorPair p0 = hvx_vec_mpy_f32_f16(s0_hf, v_one_f16);
|
||||
const HVX_VectorPair p1 = hvx_vec_mpy_f32_f16(s1_hf, v_one_f16);
|
||||
const HVX_Vector x0 = Q6_V_lo_W(p0);
|
||||
const HVX_Vector x1 = Q6_V_hi_W(p0);
|
||||
const HVX_Vector x2 = Q6_V_lo_W(p1);
|
||||
const HVX_Vector x3 = Q6_V_hi_W(p1);
|
||||
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_VectorPair sum_p = Q6_Wsf_vmpy_VhfVhf(s0_hf, s0_hf);
|
||||
sum_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_p, s1_hf, s1_hf);
|
||||
HVX_Vector sum_v = hvx_vec_reduce_sum_f32(Q6_Vsf_vadd_VsfVsf(Q6_V_lo_W(sum_p), Q6_V_hi_W(sum_p)));
|
||||
#else
|
||||
HVX_Vector sum01 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(x0, x0), Q6_Vqf32_vmpy_VsfVsf(x1, x1));
|
||||
HVX_Vector sum23 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(x2, x2), Q6_Vqf32_vmpy_VsfVsf(x3, x3));
|
||||
HVX_Vector sum_v = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(sum01, sum23)));
|
||||
#endif
|
||||
|
||||
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));
|
||||
if (has_scale) {
|
||||
scale_v = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(scale_v, scale_opt));
|
||||
}
|
||||
|
||||
HVX_Vector s0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x0, scale_v));
|
||||
HVX_Vector s1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x1, scale_v));
|
||||
HVX_Vector s2 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x2, scale_v));
|
||||
HVX_Vector s3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x3, scale_v));
|
||||
|
||||
HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(s0, w0));
|
||||
HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(s1, w1));
|
||||
HVX_Vector r2 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(s2, w2));
|
||||
HVX_Vector r3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(s3, w3));
|
||||
|
||||
v_dst[0] = hvx_vec_f32_to_f16_shuff(r0, r1);
|
||||
v_dst[1] = hvx_vec_f32_to_f16_shuff(r2, r3);
|
||||
}
|
||||
}
|
||||
|
||||
static __attribute__((noinline, unused)) void hvx_rms_norm_mul_f16_w32_ne256_bcast_rows(
|
||||
const uint8_t * restrict src,
|
||||
const uint8_t * restrict weight,
|
||||
uint8_t * restrict dst,
|
||||
uint32_t num_rows,
|
||||
size_t src_stride,
|
||||
size_t dst_stride,
|
||||
float epsilon,
|
||||
float scale) {
|
||||
const HVX_Vector * restrict v_w = (const HVX_Vector *) weight;
|
||||
const HVX_VectorPair wp0 = Q6_W_vdeal_VVR(v_w[1], v_w[0], -4);
|
||||
const HVX_VectorPair wp1 = Q6_W_vdeal_VVR(v_w[3], v_w[2], -4);
|
||||
const HVX_VectorPair wp2 = Q6_W_vdeal_VVR(v_w[5], v_w[4], -4);
|
||||
const HVX_VectorPair wp3 = Q6_W_vdeal_VVR(v_w[7], v_w[6], -4);
|
||||
const HVX_Vector w0 = Q6_V_lo_W(wp0);
|
||||
const HVX_Vector w1 = Q6_V_hi_W(wp0);
|
||||
const HVX_Vector w2 = Q6_V_lo_W(wp1);
|
||||
const HVX_Vector w3 = Q6_V_hi_W(wp1);
|
||||
const HVX_Vector w4 = Q6_V_lo_W(wp2);
|
||||
const HVX_Vector w5 = Q6_V_hi_W(wp2);
|
||||
const HVX_Vector w6 = Q6_V_lo_W(wp3);
|
||||
const HVX_Vector w7 = Q6_V_hi_W(wp3);
|
||||
|
||||
const HVX_Vector v_one_f16 = Q6_Vh_vsplat_R(0x3c00);
|
||||
const HVX_Vector denom_v = hvx_vec_splat_f32(1.0f / 256.0f);
|
||||
const HVX_Vector epsilon_v = hvx_vec_splat_f32(epsilon);
|
||||
const HVX_Vector scale_opt = hvx_vec_splat_f32(scale);
|
||||
const bool has_scale = (scale != 0.0f);
|
||||
|
||||
#pragma nounroll
|
||||
for (uint32_t ir = 0; ir < num_rows; ++ir) {
|
||||
const HVX_Vector * restrict v_src = (const HVX_Vector *) (src + ir * src_stride);
|
||||
HVX_Vector * restrict v_dst = (HVX_Vector *) (dst + ir * dst_stride);
|
||||
|
||||
const HVX_Vector s0_hf = v_src[0];
|
||||
const HVX_Vector s1_hf = v_src[1];
|
||||
const HVX_Vector s2_hf = v_src[2];
|
||||
const HVX_Vector s3_hf = v_src[3];
|
||||
const HVX_VectorPair p0 = hvx_vec_mpy_f32_f16(s0_hf, v_one_f16);
|
||||
const HVX_VectorPair p1 = hvx_vec_mpy_f32_f16(s1_hf, v_one_f16);
|
||||
const HVX_VectorPair p2 = hvx_vec_mpy_f32_f16(s2_hf, v_one_f16);
|
||||
const HVX_VectorPair p3 = hvx_vec_mpy_f32_f16(s3_hf, v_one_f16);
|
||||
|
||||
const HVX_Vector x0 = Q6_V_lo_W(p0);
|
||||
const HVX_Vector x1 = Q6_V_hi_W(p0);
|
||||
const HVX_Vector x2 = Q6_V_lo_W(p1);
|
||||
const HVX_Vector x3 = Q6_V_hi_W(p1);
|
||||
const HVX_Vector x4 = Q6_V_lo_W(p2);
|
||||
const HVX_Vector x5 = Q6_V_hi_W(p2);
|
||||
const HVX_Vector x6 = Q6_V_lo_W(p3);
|
||||
const HVX_Vector x7 = Q6_V_hi_W(p3);
|
||||
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_VectorPair sum_p = Q6_Wsf_vmpy_VhfVhf(s0_hf, s0_hf);
|
||||
sum_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_p, s1_hf, s1_hf);
|
||||
sum_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_p, s2_hf, s2_hf);
|
||||
sum_p = Q6_Wsf_vmpyacc_WsfVhfVhf(sum_p, s3_hf, s3_hf);
|
||||
HVX_Vector sum_v = hvx_vec_reduce_sum_f32(Q6_Vsf_vadd_VsfVsf(Q6_V_lo_W(sum_p), Q6_V_hi_W(sum_p)));
|
||||
#else
|
||||
HVX_Vector s01 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(x0, x0), Q6_Vqf32_vmpy_VsfVsf(x1, x1));
|
||||
HVX_Vector s23 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(x2, x2), Q6_Vqf32_vmpy_VsfVsf(x3, x3));
|
||||
HVX_Vector s45 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(x4, x4), Q6_Vqf32_vmpy_VsfVsf(x5, x5));
|
||||
HVX_Vector s67 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(x6, x6), Q6_Vqf32_vmpy_VsfVsf(x7, x7));
|
||||
HVX_Vector sum_qf = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(s01, s23),
|
||||
Q6_Vqf32_vadd_Vqf32Vqf32(s45, s67));
|
||||
HVX_Vector sum_v = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_qf));
|
||||
#endif
|
||||
|
||||
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));
|
||||
if (has_scale) {
|
||||
scale_v = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(scale_v, scale_opt));
|
||||
}
|
||||
|
||||
HVX_Vector r0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x0, scale_v)), w0));
|
||||
HVX_Vector r1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x1, scale_v)), w1));
|
||||
HVX_Vector r2 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x2, scale_v)), w2));
|
||||
HVX_Vector r3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x3, scale_v)), w3));
|
||||
HVX_Vector r4 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x4, scale_v)), w4));
|
||||
HVX_Vector r5 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x5, scale_v)), w5));
|
||||
HVX_Vector r6 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x6, scale_v)), w6));
|
||||
HVX_Vector r7 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x7, scale_v)), w7));
|
||||
|
||||
v_dst[0] = hvx_vec_f32_to_f16_shuff(r0, r1);
|
||||
v_dst[1] = hvx_vec_f32_to_f16_shuff(r2, r3);
|
||||
v_dst[2] = hvx_vec_f32_to_f16_shuff(r4, r5);
|
||||
v_dst[3] = hvx_vec_f32_to_f16_shuff(r6, r7);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // HVX_NORM_H
|
||||
|
||||
@@ -16,12 +16,28 @@
|
||||
#define GGML_COMMON_DECL_C
|
||||
#endif
|
||||
#include "ggml-common.h"
|
||||
#include "ggml-impl.h"
|
||||
|
||||
static inline float hvx_fp16_to_fp32(uint16_t h) {
|
||||
__fp16 f;
|
||||
memcpy(&f, &h, sizeof(f));
|
||||
return (float) f;
|
||||
}
|
||||
|
||||
static inline HVX_Vector hvx_vec_reduce_max_32_f16(HVX_Vector v) {
|
||||
v = Q6_Vhf_vmax_VhfVhf(v, Q6_V_vror_VR(v, 32));
|
||||
v = Q6_Vhf_vmax_VhfVhf(v, Q6_V_vror_VR(v, 16));
|
||||
v = Q6_Vhf_vmax_VhfVhf(v, Q6_V_vror_VR(v, 8));
|
||||
v = Q6_Vhf_vmax_VhfVhf(v, Q6_V_vror_VR(v, 4));
|
||||
v = Q6_Vhf_vmax_VhfVhf(v, Q6_V_vror_VR(v, 2));
|
||||
HVX_Vector pair = Q6_V_vmux_QVV(Q6_Q_vsetq_R(2), v, Q6_V_vror_VR(v, 62));
|
||||
return hvx_vec_repl_2x_f16(pair);
|
||||
}
|
||||
|
||||
static inline void hvx_quantize_row_q8_0_f32(void * restrict dst_ptr, const float * restrict src_ptr, int n) {
|
||||
const int nb = n / QK8_0;
|
||||
block_q8_0 * dst = (block_q8_0 *) dst_ptr;
|
||||
HVX_Vector zero = Q6_V_vzero();
|
||||
const HVX_Vector inv127_hf = Q6_Vh_vsplat_R(0x2008); // 1.0 / 127.0
|
||||
const HVX_Vector zero = Q6_V_vzero();
|
||||
|
||||
int i = 0;
|
||||
for (; i + 3 < nb; i += 4) {
|
||||
@@ -32,31 +48,19 @@ static inline void hvx_quantize_row_q8_0_f32(void * restrict dst_ptr, const floa
|
||||
HVX_Vector vmax2_sf = hvx_vec_reduce_max_f32(hvx_vec_abs_f32(vx[2]));
|
||||
HVX_Vector vmax3_sf = hvx_vec_reduce_max_f32(hvx_vec_abs_f32(vx[3]));
|
||||
|
||||
HVX_Vector vx0_qf = Q6_Vqf32_vsub_VsfVsf(vx[0], zero);
|
||||
HVX_Vector vx1_qf = Q6_Vqf32_vsub_VsfVsf(vx[1], zero);
|
||||
HVX_Vector vx2_qf = Q6_Vqf32_vsub_VsfVsf(vx[2], zero);
|
||||
HVX_Vector vx3_qf = Q6_Vqf32_vsub_VsfVsf(vx[3], zero);
|
||||
HVX_Vector vmax01_hf = hvx_vec_f32_to_f16(vmax0_sf, vmax1_sf);
|
||||
HVX_Vector vmax23_hf = hvx_vec_f32_to_f16(vmax2_sf, vmax3_sf);
|
||||
|
||||
HVX_Vector vmax0_qf = Q6_Vqf32_vsub_VsfVsf(vmax0_sf, zero);
|
||||
HVX_Vector vmax1_qf = Q6_Vqf32_vsub_VsfVsf(vmax1_sf, zero);
|
||||
HVX_Vector vmax2_qf = Q6_Vqf32_vsub_VsfVsf(vmax2_sf, zero);
|
||||
HVX_Vector vmax3_qf = Q6_Vqf32_vsub_VsfVsf(vmax3_sf, zero);
|
||||
HVX_Vector vx01_hf = hvx_vec_f32_to_f16(vx[0], vx[1]);
|
||||
HVX_Vector vx23_hf = hvx_vec_f32_to_f16(vx[2], vx[3]);
|
||||
|
||||
HVX_Vector vmax01_hf = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(vmax1_qf, vmax0_qf)));
|
||||
HVX_Vector vmax23_hf = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(vmax3_qf, vmax2_qf)));
|
||||
|
||||
HVX_Vector vx01_hf = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(vx1_qf, vx0_qf)));
|
||||
HVX_Vector vx23_hf = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(vx3_qf, vx2_qf)));
|
||||
|
||||
HVX_Vector vd01_qf16 = Q6_Vqf16_vmpy_VhfVhf(vmax01_hf, Q6_Vh_vsplat_R(0x2008)); // 1.0 / 127.0
|
||||
HVX_Vector vd23_qf16 = Q6_Vqf16_vmpy_VhfVhf(vmax23_hf, Q6_Vh_vsplat_R(0x2008)); // 1.0 / 127.0
|
||||
HVX_Vector vd01_hf = Q6_Vhf_equals_Vqf16(vd01_qf16);
|
||||
HVX_Vector vd23_hf = Q6_Vhf_equals_Vqf16(vd23_qf16);
|
||||
HVX_Vector vd01_hf = hvx_vec_mul_f16_f16(vmax01_hf, inv127_hf);
|
||||
HVX_Vector vd23_hf = hvx_vec_mul_f16_f16(vmax23_hf, inv127_hf);
|
||||
|
||||
HVX_Vector vd01_inv_hf = hvx_vec_inverse_f16(vd01_hf);
|
||||
HVX_Vector vd23_inv_hf = hvx_vec_inverse_f16(vd23_hf);
|
||||
vx01_hf = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(vx01_hf, vd01_inv_hf));
|
||||
vx23_hf = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(vx23_hf, vd23_inv_hf));
|
||||
vx01_hf = hvx_vec_mul_f16_f16(vx01_hf, vd01_inv_hf);
|
||||
vx23_hf = hvx_vec_mul_f16_f16(vx23_hf, vd23_inv_hf);
|
||||
|
||||
HVX_Vector vx01_i16 = hvx_vec_i16_from_hf_rnd_sat(vx01_hf);
|
||||
HVX_Vector vx23_i16 = hvx_vec_i16_from_hf_rnd_sat(vx23_hf);
|
||||
@@ -77,22 +81,77 @@ static inline void hvx_quantize_row_q8_0_f32(void * restrict dst_ptr, const floa
|
||||
|
||||
for (; i < nb; i++) {
|
||||
const float * block_src = src_ptr + i * QK8_0;
|
||||
HVX_Vector vx = *(const HVX_UVector *) block_src;
|
||||
HVX_Vector v_abs = hvx_vec_abs_f32(vx);
|
||||
HVX_Vector v_max = hvx_vec_reduce_max_f32(v_abs);
|
||||
float amax = hvx_vec_get_f32(v_max);
|
||||
HVX_Vector vx = *(const HVX_UVector *) block_src;
|
||||
HVX_Vector vmax_sf = hvx_vec_reduce_max_f32(hvx_vec_abs_f32(vx));
|
||||
|
||||
const float d = amax / 127.0f;
|
||||
const float id = d ? (1.0f / d) : 0.0f;
|
||||
dst[i].d = GGML_FP32_TO_FP16(d);
|
||||
HVX_Vector vmax_hf = hvx_vec_f32_to_f16(vmax_sf, zero);
|
||||
HVX_Vector vx_hf = hvx_vec_f32_to_f16(vx, zero);
|
||||
|
||||
HVX_Vector vid = hvx_vec_splat_f32(id);
|
||||
HVX_Vector v_scaled = hvx_vec_mul_f32_f32(vx, vid);
|
||||
HVX_Vector v_scaled_qf = Q6_Vqf32_vsub_VsfVsf(v_scaled, zero);
|
||||
HVX_Vector v_scaled_hf = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(zero, v_scaled_qf)));
|
||||
HVX_Vector v_i16 = hvx_vec_i16_from_hf_rnd_sat(v_scaled_hf);
|
||||
HVX_Vector vd_hf = hvx_vec_mul_f16_f16(vmax_hf, inv127_hf);
|
||||
HVX_Vector vd_inv_hf = hvx_vec_inverse_f16(vd_hf);
|
||||
vx_hf = hvx_vec_mul_f16_f16(vx_hf, vd_inv_hf);
|
||||
|
||||
HVX_Vector v_i16 = hvx_vec_i16_from_hf_rnd_sat(vx_hf);
|
||||
HVX_Vector v_i8 = Q6_Vb_vpack_VhVh_sat(zero, v_i16);
|
||||
|
||||
hvx_vec_store_u(&dst[i].d, 2, vd_hf);
|
||||
hvx_vec_store_u(dst[i].qs, 32, v_i8);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hvx_quantize_row_q8_0_f16(void * restrict dst_ptr, const __fp16 * restrict src_ptr, int n) {
|
||||
const int nb = n / QK8_0;
|
||||
block_q8_0 * dst = (block_q8_0 *) dst_ptr;
|
||||
const HVX_Vector inv127_hf = Q6_Vh_vsplat_R(0x2008); // 1.0 / 127.0
|
||||
|
||||
int i = 0;
|
||||
for (; i + 3 < nb; i += 4) {
|
||||
const HVX_UVector * vsrc = (const HVX_UVector *) (src_ptr + i * QK8_0);
|
||||
HVX_Vector vx01_hf = vsrc[0];
|
||||
HVX_Vector vx23_hf = vsrc[1];
|
||||
|
||||
HVX_Vector vmax01_hf = hvx_vec_reduce_max_32_f16(hvx_vec_abs_f16(vx01_hf));
|
||||
HVX_Vector vmax23_hf = hvx_vec_reduce_max_32_f16(hvx_vec_abs_f16(vx23_hf));
|
||||
|
||||
HVX_Vector vd01_hf = hvx_vec_mul_f16_f16(vmax01_hf, inv127_hf);
|
||||
HVX_Vector vd23_hf = hvx_vec_mul_f16_f16(vmax23_hf, inv127_hf);
|
||||
|
||||
HVX_Vector vd01_inv_hf = hvx_vec_inverse_f16(vd01_hf);
|
||||
HVX_Vector vd23_inv_hf = hvx_vec_inverse_f16(vd23_hf);
|
||||
vx01_hf = hvx_vec_mul_f16_f16(vx01_hf, vd01_inv_hf);
|
||||
vx23_hf = hvx_vec_mul_f16_f16(vx23_hf, vd23_inv_hf);
|
||||
|
||||
HVX_Vector vx01_i16 = hvx_vec_i16_from_hf_rnd_sat(vx01_hf);
|
||||
HVX_Vector vx23_i16 = hvx_vec_i16_from_hf_rnd_sat(vx23_hf);
|
||||
HVX_Vector vx_i8 = Q6_Vb_vpack_VhVh_sat(vx23_i16, vx01_i16);
|
||||
|
||||
hvx_vec_store_u(&dst[i + 0].d, 2, vd01_hf);
|
||||
hvx_vec_store_u(dst[i + 0].qs, 32, vx_i8);
|
||||
|
||||
hvx_vec_store_u(&dst[i + 1].d, 2, Q6_V_vror_VR(vd01_hf, 64));
|
||||
hvx_vec_store_u(dst[i + 1].qs, 32, Q6_V_vror_VR(vx_i8, 32));
|
||||
|
||||
hvx_vec_store_u(&dst[i + 2].d, 2, vd23_hf);
|
||||
hvx_vec_store_u(dst[i + 2].qs, 32, Q6_V_vror_VR(vx_i8, 64));
|
||||
|
||||
hvx_vec_store_u(&dst[i + 3].d, 2, Q6_V_vror_VR(vd23_hf, 64));
|
||||
hvx_vec_store_u(dst[i + 3].qs, 32, Q6_V_vror_VR(vx_i8, 96));
|
||||
}
|
||||
|
||||
const HVX_Vector zero = Q6_V_vzero();
|
||||
for (; i < nb; i++) {
|
||||
const __fp16 * block_src = src_ptr + i * QK8_0;
|
||||
HVX_Vector vx_hf = *(const HVX_UVector *) block_src;
|
||||
HVX_Vector vmax_hf = hvx_vec_reduce_max_32_f16(hvx_vec_abs_f16(vx_hf));
|
||||
|
||||
HVX_Vector vd_hf = hvx_vec_mul_f16_f16(vmax_hf, inv127_hf);
|
||||
HVX_Vector vd_inv_hf = hvx_vec_inverse_f16(vd_hf);
|
||||
vx_hf = hvx_vec_mul_f16_f16(vx_hf, vd_inv_hf);
|
||||
|
||||
HVX_Vector v_i16 = hvx_vec_i16_from_hf_rnd_sat(vx_hf);
|
||||
HVX_Vector v_i8 = Q6_Vb_vpack_VhVh_sat(zero, v_i16);
|
||||
|
||||
hvx_vec_store_u(&dst[i].d, 2, vd_hf);
|
||||
hvx_vec_store_u(dst[i].qs, 32, v_i8);
|
||||
}
|
||||
}
|
||||
@@ -121,29 +180,99 @@ static inline void hvx_dequantize_row_flat_q8_0_f32(float * restrict dst_ptr, co
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_flat_q8_0_f16(__fp16 * restrict dst_ptr, const void * restrict src_ptr, uint32_t n) {
|
||||
const int nb = n / QK8_0;
|
||||
const block_q8_0 * src = (const block_q8_0 *) src_ptr;
|
||||
static inline HVX_VectorPair hvx_dequant_q8_0_x4_f16(HVX_Vector v0, HVX_Vector v1,
|
||||
HVX_VectorPred q32, HVX_VectorPred q64, HVX_VectorPred q96) {
|
||||
HVX_Vector qs = Q6_V_vmux_QVV(q96, Q6_V_valign_VVR(v1, v0, 6), Q6_V_valign_VVR(v1, v0, 8));
|
||||
qs = Q6_V_vmux_QVV(q64, Q6_V_valign_VVR(v1, v0, 4), qs);
|
||||
qs = Q6_V_vmux_QVV(q32, Q6_V_valign_VVR(v1, v0, 2), qs);
|
||||
|
||||
for (int i = nb - 1; i >= 0; i--) {
|
||||
HVX_Vector vd_f16 = Q6_Vh_vsplat_R(*(const int16_t *) &src[i].d);
|
||||
HVX_VectorPair vp_f32 = hvx_vec_f16_to_f32(vd_f16);
|
||||
HVX_Vector vd = Q6_V_lo_W(vp_f32);
|
||||
const HVX_Vector s01 = Q6_V_vmux_QVV(q64, hvx_vec_repl_f16(v0), hvx_vec_repl_f16(Q6_V_valign_VVR(v1, v0, 34)));
|
||||
const HVX_Vector s23 = Q6_V_vmux_QVV(q64, hvx_vec_repl_f16(Q6_V_valign_VVR(v1, v0, 68)), hvx_vec_repl_f16(Q6_V_valign_VVR(v1, v0, 102)));
|
||||
|
||||
HVX_Vector vq_i8 = *(const HVX_UVector *) src[i].qs;
|
||||
const HVX_VectorPair qh = Q6_Wh_vunpack_Vb(qs);
|
||||
const HVX_Vector res01 = hvx_vec_mul_f16_f16(Q6_Vhf_equals_Vh(Q6_V_lo_W(qh)), s01);
|
||||
const HVX_Vector res23 = hvx_vec_mul_f16_f16(Q6_Vhf_equals_Vh(Q6_V_hi_W(qh)), s23);
|
||||
return Q6_W_vcombine_VV(res23, res01);
|
||||
}
|
||||
|
||||
HVX_VectorPair p16 = Q6_Wh_vunpack_Vb(vq_i8);
|
||||
HVX_Vector v_i16 = Q6_V_lo_W(p16);
|
||||
HVX_VectorPair p32 = Q6_Ww_vunpack_Vh(v_i16);
|
||||
HVX_Vector v_i32 = Q6_V_lo_W(p32);
|
||||
static __attribute__((noinline)) void hvx_dequantize_row_flat_q8_0_f16_u(__fp16 * dst_ptr, const void * src_ptr, int32_t nb) {
|
||||
const uint8_t * src_base = (const uint8_t *) src_ptr;
|
||||
const HVX_VectorPred q32 = Q6_Q_vsetq_R(32);
|
||||
const HVX_VectorPred q64 = Q6_Q_vsetq_R(64);
|
||||
const HVX_VectorPred q96 = Q6_Q_vsetq_R(96);
|
||||
|
||||
HVX_Vector v_f32 = Q6_Vsf_equals_Vw(v_i32);
|
||||
HVX_Vector res_f32 = hvx_vec_mul_f32_f32(v_f32, vd);
|
||||
const int32_t n_full = nb / 4;
|
||||
const int32_t rem = nb & 3;
|
||||
|
||||
HVX_Vector res_f16 = hvx_vec_f32_to_f16(res_f32, Q6_V_vzero());
|
||||
if (rem > 0) {
|
||||
const uint8_t * s = src_base + n_full * 4 * sizeof(block_q8_0);
|
||||
__fp16 * d = dst_ptr + n_full * 4 * QK8_0;
|
||||
const HVX_Vector v0 = *(const HVX_UVector *) s;
|
||||
const HVX_VectorPair res = hvx_dequant_q8_0_x4_f16(v0, Q6_V_vzero(), q32, q64, q96);
|
||||
if (rem == 3) {
|
||||
hvx_vec_store_u(d + 64, 64, Q6_V_hi_W(res));
|
||||
hvx_vmemu(d + 0) = Q6_V_lo_W(res);
|
||||
} else if (rem == 2) {
|
||||
hvx_vmemu(d + 0) = Q6_V_lo_W(res);
|
||||
} else {
|
||||
hvx_vec_store_u(d + 0, 64, Q6_V_lo_W(res));
|
||||
}
|
||||
}
|
||||
|
||||
__fp16 * block_dst = dst_ptr + i * QK8_0;
|
||||
hvx_vec_store_u(block_dst, QK8_0 * sizeof(__fp16), res_f16);
|
||||
for (int32_t g = n_full - 1; g >= 0; --g) {
|
||||
const uint8_t * s = src_base + g * 4 * sizeof(block_q8_0);
|
||||
__fp16 * d = dst_ptr + g * 4 * QK8_0;
|
||||
const HVX_Vector v0 = *(const HVX_UVector *) s;
|
||||
const HVX_Vector v1 = *(const HVX_UVector *) (s + VLEN);
|
||||
const HVX_VectorPair res = hvx_dequant_q8_0_x4_f16(v0, v1, q32, q64, q96);
|
||||
hvx_vmemu(d + 64) = Q6_V_hi_W(res);
|
||||
hvx_vmemu(d + 0) = Q6_V_lo_W(res);
|
||||
}
|
||||
}
|
||||
|
||||
// Q8_0 row to f16 (supports in-place dst_ptr == src_ptr), 4 blocks (128 values) per step backwards.
|
||||
static inline void hvx_dequantize_row_flat_q8_0_f16(__fp16 * dst_ptr, const void * src_ptr, uint32_t n) {
|
||||
const int32_t nb = (int32_t) (n / QK8_0);
|
||||
if (nb <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hex_is_aligned(dst_ptr, 128)) {
|
||||
hvx_dequantize_row_flat_q8_0_f16_u(dst_ptr, src_ptr, nb);
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t * src_base = (const uint8_t *) src_ptr;
|
||||
const HVX_VectorPred q32 = Q6_Q_vsetq_R(32);
|
||||
const HVX_VectorPred q64 = Q6_Q_vsetq_R(64);
|
||||
const HVX_VectorPred q96 = Q6_Q_vsetq_R(96);
|
||||
|
||||
const int32_t n_full = nb / 4;
|
||||
const int32_t rem = nb & 3;
|
||||
|
||||
if (rem > 0) {
|
||||
const uint8_t * s = src_base + n_full * 4 * sizeof(block_q8_0);
|
||||
__fp16 * d = dst_ptr + n_full * 4 * QK8_0;
|
||||
const HVX_Vector v0 = *(const HVX_UVector *) s;
|
||||
const HVX_VectorPair res = hvx_dequant_q8_0_x4_f16(v0, Q6_V_vzero(), q32, q64, q96);
|
||||
if (rem == 3) {
|
||||
Q6_vmem_QRIV(q64, (HVX_Vector *) (d + 64), Q6_V_hi_W(res));
|
||||
hvx_vmem(d + 0) = Q6_V_lo_W(res);
|
||||
} else if (rem == 2) {
|
||||
hvx_vmem(d + 0) = Q6_V_lo_W(res);
|
||||
} else {
|
||||
Q6_vmem_QRIV(q64, (HVX_Vector *) (d + 0), Q6_V_lo_W(res));
|
||||
}
|
||||
}
|
||||
|
||||
for (int32_t g = n_full - 1; g >= 0; --g) {
|
||||
const uint8_t * s = src_base + g * 4 * sizeof(block_q8_0);
|
||||
__fp16 * d = dst_ptr + g * 4 * QK8_0;
|
||||
const HVX_Vector v0 = *(const HVX_UVector *) s;
|
||||
const HVX_Vector v1 = *(const HVX_UVector *) (s + VLEN);
|
||||
const HVX_VectorPair res = hvx_dequant_q8_0_x4_f16(v0, v1, q32, q64, q96);
|
||||
hvx_vmem(d + 64) = Q6_V_hi_W(res);
|
||||
hvx_vmem(d + 0) = Q6_V_lo_W(res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,8 +417,8 @@ static inline void hvx_dequantize_row_flat_q4_k_f32(float * restrict dst_ptr, co
|
||||
const block_q4_K * src = (const block_q4_K *) src_ptr;
|
||||
|
||||
for (int i = 0; i < nb; i++) {
|
||||
const float d = GGML_FP16_TO_FP32(src[i].d);
|
||||
const float min = GGML_FP16_TO_FP32(src[i].dmin);
|
||||
const float d = hvx_fp16_to_fp32(src[i].d);
|
||||
const float min = hvx_fp16_to_fp32(src[i].dmin);
|
||||
const uint8_t * sc = src[i].scales;
|
||||
|
||||
float * y = dst_ptr + i * QK_K;
|
||||
@@ -343,8 +472,8 @@ static inline void hvx_dequantize_row_flat_q5_k_f32(float * restrict dst_ptr, co
|
||||
const block_q5_K * src = (const block_q5_K *) src_ptr;
|
||||
|
||||
for (int i = 0; i < nb; i++) {
|
||||
const float d = GGML_FP16_TO_FP32(src[i].d);
|
||||
const float min = GGML_FP16_TO_FP32(src[i].dmin);
|
||||
const float d = hvx_fp16_to_fp32(src[i].d);
|
||||
const float min = hvx_fp16_to_fp32(src[i].dmin);
|
||||
const uint8_t * sc = src[i].scales;
|
||||
|
||||
HVX_Vector v_qh = *(const HVX_UVector *) src[i].qh;
|
||||
@@ -410,7 +539,7 @@ static inline void hvx_dequantize_row_flat_q6_k_f32(float * restrict dst_ptr, co
|
||||
const HVX_Vector mask_03 = Q6_Vb_vsplat_R(0x03);
|
||||
|
||||
for (int i = 0; i < nb; i++) {
|
||||
const float d = GGML_FP16_TO_FP32(src[i].d);
|
||||
const float d = hvx_fp16_to_fp32(src[i].d);
|
||||
|
||||
const uint8_t * restrict ql = src[i].ql;
|
||||
const uint8_t * restrict qh = src[i].qh;
|
||||
@@ -478,26 +607,17 @@ static inline void hvx_dequantize_row_flat_q6_k_f32(float * restrict dst_ptr, co
|
||||
}
|
||||
|
||||
// bytes row, row + 32, row + 64 and row + 96 of v, to bytes 0..3
|
||||
static inline HVX_Vector hvx_dequantize_gather4(HVX_Vector v, uint32_t row) {
|
||||
v = Q6_V_vror_VR(v, row);
|
||||
HVX_Vector packed = Q6_V_vmux_QVV(Q6_Q_vsetq_R(1), v, Q6_V_vror_VR(v, 31));
|
||||
packed = Q6_V_vmux_QVV(Q6_Q_vsetq_R(2), packed, Q6_V_vror_VR(v, 62));
|
||||
return Q6_V_vmux_QVV(Q6_Q_vsetq_R(3), packed, Q6_V_vror_VR(v, 93));
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_tiled_q8_0_f32(float * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const HVX_VectorPred first2 = Q6_Q_vsetq_R(2);
|
||||
static inline void hvx_dequantize_row_tiled_q8_0_f32(void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const HVX_VectorPred first4 = Q6_Q_vsetq_R(4);
|
||||
|
||||
for (uint32_t k_tile = 0; k_tile < n_k_tiles; ++k_tile) {
|
||||
const uint8_t * tile = src_spad + k_tile * tile_stride;
|
||||
float * dst = dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
float * dst = (float *) dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
|
||||
HVX_Vector vq = Q6_V_vzero();
|
||||
for (int group = 7; group >= 0; --group) {
|
||||
const HVX_Vector v = Q6_V_vror_VR(hvx_vmem(tile + group * VLEN), 2 * row);
|
||||
const HVX_Vector packed = Q6_V_vmux_QVV(first2, v, Q6_V_vror_VR(v, 62));
|
||||
vq = Q6_V_vmux_QVV(first4, packed, Q6_V_vror_VR(vq, VLEN - 4));
|
||||
const HVX_Vector v = Q6_V_vror_VR(hvx_vmem(tile + group * VLEN), 4 * row);
|
||||
vq = Q6_V_vmux_QVV(first4, v, Q6_V_vror_VR(vq, VLEN - 4));
|
||||
}
|
||||
|
||||
const HVX_Vector scales = hvx_vmem(tile + 1024);
|
||||
@@ -510,22 +630,24 @@ static inline void hvx_dequantize_row_tiled_q8_0_f32(float * dst_spad, const uin
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_tiled_q4_0_f32(float * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const HVX_VectorPred first4 = Q6_Q_vsetq_R(4);
|
||||
static inline void hvx_dequantize_row_tiled_q4_0_f32(void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const HVX_VectorPred first4 = Q6_Q_vsetq_R(4);
|
||||
const HVX_Vector mask_0f = Q6_Vb_vsplat_R(0x0F);
|
||||
const HVX_Vector i8 = Q6_Vb_vsplat_R(8);
|
||||
|
||||
for (uint32_t k_tile = 0; k_tile < n_k_tiles; ++k_tile) {
|
||||
const uint8_t * tile = src_spad + k_tile * tile_stride;
|
||||
float * dst = dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
float * dst = (float *) dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
|
||||
HVX_Vector vq = Q6_V_vzero();
|
||||
for (int group = 3; group >= 0; --group) {
|
||||
vq = Q6_V_vmux_QVV(first4, hvx_dequantize_gather4(hvx_vmem(tile + group * VLEN), row), Q6_V_vror_VR(vq, VLEN - 4));
|
||||
for (int i = 3; i >= 0; --i) {
|
||||
const HVX_Vector plane = Q6_V_vror_VR(hvx_vmem(tile + i * VLEN), 4 * row);
|
||||
const HVX_Vector hi = Q6_Vub_vlsr_VubR(plane, 4);
|
||||
const HVX_Vector lo = Q6_V_vand_VV(plane, mask_0f);
|
||||
vq = Q6_V_vmux_QVV(first4, hi, Q6_V_vror_VR(vq, VLEN - 4));
|
||||
vq = Q6_V_vmux_QVV(first4, lo, Q6_V_vror_VR(vq, VLEN - 4));
|
||||
}
|
||||
|
||||
const HVX_Vector lo = Q6_V_vand_VV(vq, Q6_Vb_vsplat_R(0x0F));
|
||||
const HVX_Vector hi = Q6_Vub_vlsr_VubR(vq, 4);
|
||||
vq = Q6_V_lo_W(Q6_W_vshuff_VVR(hi, lo, -1));
|
||||
vq = Q6_Vb_vsub_VbVb(vq, Q6_Vb_vsplat_R(8));
|
||||
vq = Q6_Vb_vsub_VbVb(vq, i8);
|
||||
|
||||
const HVX_Vector scales = hvx_vmem(tile + 512);
|
||||
const HVX_Vector scale_hf = hvx_vec_repl_f16(Q6_V_vror_VR(scales, 2 * row));
|
||||
@@ -537,22 +659,23 @@ static inline void hvx_dequantize_row_tiled_q4_0_f32(float * dst_spad, const uin
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_tiled_q4_k_f32(float * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const HVX_VectorPred first4 = Q6_Q_vsetq_R(4);
|
||||
static inline void hvx_dequantize_row_tiled_q4_k_f32(void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const HVX_VectorPred first4 = Q6_Q_vsetq_R(4);
|
||||
const HVX_Vector mask_0f = Q6_Vb_vsplat_R(0x0F);
|
||||
|
||||
for (uint32_t k_tile = 0; k_tile < n_k_tiles; ++k_tile) {
|
||||
const uint8_t * tile = src_spad + k_tile * tile_stride;
|
||||
float * dst = dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
float * dst = (float *) dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
|
||||
HVX_Vector vq = Q6_V_vzero();
|
||||
for (int group = 3; group >= 0; --group) {
|
||||
vq = Q6_V_vmux_QVV(first4, hvx_dequantize_gather4(hvx_vmem(tile + group * VLEN), row), Q6_V_vror_VR(vq, VLEN - 4));
|
||||
for (int i = 3; i >= 0; --i) {
|
||||
const HVX_Vector plane = Q6_V_vror_VR(hvx_vmem(tile + i * VLEN), 4 * row);
|
||||
const HVX_Vector hi = Q6_Vub_vlsr_VubR(plane, 4);
|
||||
const HVX_Vector lo = Q6_V_vand_VV(plane, mask_0f);
|
||||
vq = Q6_V_vmux_QVV(first4, hi, Q6_V_vror_VR(vq, VLEN - 4));
|
||||
vq = Q6_V_vmux_QVV(first4, lo, Q6_V_vror_VR(vq, VLEN - 4));
|
||||
}
|
||||
|
||||
const HVX_Vector lo = Q6_V_vand_VV(vq, Q6_Vb_vsplat_R(0x0F));
|
||||
const HVX_Vector hi = Q6_Vub_vlsr_VubR(vq, 4);
|
||||
vq = Q6_V_lo_W(Q6_W_vshuff_VVR(hi, lo, -1));
|
||||
|
||||
const HVX_Vector scales = hvx_vmem(tile + 512);
|
||||
const HVX_Vector scale_hf = hvx_vec_repl_f16(Q6_V_vror_VR(scales, 4 * row));
|
||||
const HVX_Vector scale = Q6_V_lo_W(hvx_vec_f16_to_f32(scale_hf));
|
||||
@@ -566,26 +689,24 @@ static inline void hvx_dequantize_row_tiled_q4_k_f32(float * dst_spad, const uin
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_tiled_q5_k_f32(float * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const HVX_VectorPred first4 = Q6_Q_vsetq_R(4);
|
||||
const HVX_Vector one = Q6_Vb_vsplat_R(1);
|
||||
static inline void hvx_dequantize_row_tiled_q5_k_f32(void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const HVX_VectorPred first4 = Q6_Q_vsetq_R(4);
|
||||
const HVX_Vector mask_0f = Q6_Vb_vsplat_R(0x0F);
|
||||
|
||||
for (uint32_t k_tile = 0; k_tile < n_k_tiles; ++k_tile) {
|
||||
const uint8_t * tile = src_spad + k_tile * tile_stride;
|
||||
float * dst = dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
float * dst = (float *) dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
|
||||
const HVX_Vector p5 = hvx_dequantize_gather4(hvx_vmem(tile + 640), row);
|
||||
const HVX_Vector p5 = Q6_V_vror_VR(hvx_vmem(tile + 640), 4 * row);
|
||||
HVX_Vector vq = Q6_V_vzero();
|
||||
HVX_Vector vh = Q6_V_vzero();
|
||||
for (int group = 3; group >= 0; --group) {
|
||||
vq = Q6_V_vmux_QVV(first4, hvx_dequantize_gather4(hvx_vmem(tile + group * VLEN), row), Q6_V_vror_VR(vq, VLEN - 4));
|
||||
vh = Q6_V_vmux_QVV(first4, Q6_Vub_vlsr_VubR(p5, 2 * group), Q6_V_vror_VR(vh, VLEN - 4));
|
||||
for (int i = 3; i >= 0; --i) {
|
||||
const HVX_Vector plane = Q6_V_vror_VR(hvx_vmem(tile + i * VLEN), 4 * row);
|
||||
const HVX_Vector hi = Q6_V_vandor_VQR(Q6_Vub_vlsr_VubR(plane, 4), Q6_Q_vand_VR(p5, 0x01010101u << (2 * i + 1)), 0x10101010);
|
||||
const HVX_Vector lo = Q6_V_vandor_VQR(Q6_V_vand_VV(plane, mask_0f), Q6_Q_vand_VR(p5, 0x01010101u << (2 * i + 0)), 0x10101010);
|
||||
vq = Q6_V_vmux_QVV(first4, hi, Q6_V_vror_VR(vq, VLEN - 4));
|
||||
vq = Q6_V_vmux_QVV(first4, lo, Q6_V_vror_VR(vq, VLEN - 4));
|
||||
}
|
||||
|
||||
const HVX_Vector lo = Q6_V_vor_VV(Q6_V_vand_VV(vq, Q6_Vb_vsplat_R(0x0F)), Q6_Vw_vasl_VwR(Q6_V_vand_VV(vh, one), 4));
|
||||
const HVX_Vector hi = Q6_V_vor_VV(Q6_Vub_vlsr_VubR(vq, 4), Q6_Vw_vasl_VwR(Q6_V_vand_VV(Q6_Vub_vlsr_VubR(vh, 1), one), 4));
|
||||
vq = Q6_V_lo_W(Q6_W_vshuff_VVR(hi, lo, -1));
|
||||
|
||||
const HVX_Vector scales = hvx_vmem(tile + 512);
|
||||
const HVX_Vector scale_hf = hvx_vec_repl_f16(Q6_V_vror_VR(scales, 4 * row));
|
||||
const HVX_Vector scale = Q6_V_lo_W(hvx_vec_f16_to_f32(scale_hf));
|
||||
@@ -599,7 +720,7 @@ static inline void hvx_dequantize_row_tiled_q5_k_f32(float * dst_spad, const uin
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_tiled_q6_k_f32(float * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
static inline void hvx_dequantize_row_tiled_q6_k_f32(void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const HVX_VectorPred first4 = Q6_Q_vsetq_R(4);
|
||||
const HVX_VectorPred first16 = Q6_Q_vsetq_R(16 * sizeof(float));
|
||||
const HVX_Vector mask_0f = Q6_Vb_vsplat_R(0x0F);
|
||||
@@ -607,7 +728,7 @@ static inline void hvx_dequantize_row_tiled_q6_k_f32(float * dst_spad, const uin
|
||||
|
||||
for (uint32_t k_tile = 0; k_tile < n_k_tiles; ++k_tile) {
|
||||
const uint8_t * tile = src_spad + k_tile * tile_stride;
|
||||
float * dst = dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
float * dst = (float *) dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
|
||||
HVX_Vector vq = Q6_V_vzero();
|
||||
for (int group = 7; group >= 0; --group) {
|
||||
@@ -641,27 +762,53 @@ static inline HVX_Vector hvx_dequantize_tiled_f16_gather4(const uint8_t * p, uin
|
||||
return Q6_V_lo_W(Q6_W_vshuff_VVR(w23, w01, 8));
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_tiled_f16_f32(float * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
static inline HVX_Vector hvx_gather_tiled_f16_row_k(const uint8_t * tile, uint32_t r_byte) {
|
||||
HVX_Vector g0 = hvx_dequantize_tiled_f16_gather4(tile + 0 * VLEN, r_byte);
|
||||
HVX_Vector g1 = hvx_dequantize_tiled_f16_gather4(tile + 4 * VLEN, r_byte);
|
||||
HVX_Vector g01 = Q6_V_lo_W(Q6_W_vshuff_VVR(g1, g0, 16));
|
||||
asm volatile("" : "+v"(g01));
|
||||
|
||||
HVX_Vector g2 = hvx_dequantize_tiled_f16_gather4(tile + 8 * VLEN, r_byte);
|
||||
HVX_Vector g3 = hvx_dequantize_tiled_f16_gather4(tile + 12 * VLEN, r_byte);
|
||||
HVX_Vector g23 = Q6_V_lo_W(Q6_W_vshuff_VVR(g3, g2, 16));
|
||||
|
||||
return Q6_V_lo_W(Q6_W_vshuff_VVR(g23, g01, 32));
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_tiled_f16_f32(void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const uint32_t r_byte = 4 * row;
|
||||
|
||||
for (uint32_t k_tile = 0; k_tile < n_k_tiles; ++k_tile) {
|
||||
const uint8_t * tile = src_spad + k_tile * tile_stride;
|
||||
float * dst = dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
float * dst = (float *) dst_spad + k_tile * HTP_TENSOR_TILE_N_COLS;
|
||||
|
||||
HVX_Vector g0 = hvx_dequantize_tiled_f16_gather4(tile + 0 * VLEN, r_byte);
|
||||
HVX_Vector g1 = hvx_dequantize_tiled_f16_gather4(tile + 4 * VLEN, r_byte);
|
||||
HVX_Vector g01 = Q6_V_lo_W(Q6_W_vshuff_VVR(g1, g0, 16));
|
||||
|
||||
HVX_Vector g2 = hvx_dequantize_tiled_f16_gather4(tile + 8 * VLEN, r_byte);
|
||||
HVX_Vector g3 = hvx_dequantize_tiled_f16_gather4(tile + 12 * VLEN, r_byte);
|
||||
HVX_Vector g23 = Q6_V_lo_W(Q6_W_vshuff_VVR(g3, g2, 16));
|
||||
|
||||
HVX_Vector g = Q6_V_lo_W(Q6_W_vshuff_VVR(g23, g01, 32));
|
||||
HVX_Vector g = hvx_gather_tiled_f16_row_k(tile, r_byte);
|
||||
*(HVX_Vector *) dst = Q6_V_lo_W(hvx_vec_f16_to_f32(g));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_tiled_q1_0_f32(float * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
static inline void hvx_dequantize_row_tiled_f16_f16(void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
HVX_Vector * vdst = (HVX_Vector *) dst_spad;
|
||||
const uint32_t r_byte = 4 * row;
|
||||
const uint32_t n_pairs = n_k_tiles / 2;
|
||||
|
||||
for (uint32_t pair = 0; pair < n_pairs; ++pair) {
|
||||
const uint8_t * tile0 = src_spad + (2 * pair + 0) * tile_stride;
|
||||
const uint8_t * tile1 = src_spad + (2 * pair + 1) * tile_stride;
|
||||
HVX_Vector g0 = hvx_gather_tiled_f16_row_k(tile0, r_byte);
|
||||
asm volatile("" : "+v"(g0));
|
||||
HVX_Vector g1 = hvx_gather_tiled_f16_row_k(tile1, r_byte);
|
||||
vdst[pair] = Q6_V_lo_W(Q6_W_vshuff_VVR(g1, g0, 64));
|
||||
}
|
||||
|
||||
if (n_k_tiles & 1) {
|
||||
const uint8_t * tile = src_spad + (2 * n_pairs) * tile_stride;
|
||||
HVX_Vector g = hvx_gather_tiled_f16_row_k(tile, r_byte);
|
||||
hvx_vec_store_a(&vdst[n_pairs], HTP_TENSOR_TILE_N_COLS * sizeof(__fp16), g);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_tiled_q1_0_f32(void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
static const uint32_t __attribute__((aligned(128))) bit_mask[32] = {
|
||||
1u << 0, 1u << 8, 1u << 16, 1u << 24,
|
||||
1u << 1, 1u << 9, 1u << 17, 1u << 25,
|
||||
@@ -678,7 +825,7 @@ static inline void hvx_dequantize_row_tiled_q1_0_f32(float * dst_spad, const uin
|
||||
|
||||
for (uint32_t k_tile = 0; k_tile < n_k_tiles; ++k_tile) {
|
||||
const uint8_t * tile = src_spad + k_tile * tile_stride;
|
||||
float * dst = dst_spad + k_tile * HTP_TENSOR_TILE_K_Q1_0;
|
||||
float * dst = (float *) dst_spad + k_tile * HTP_TENSOR_TILE_K_Q1_0;
|
||||
|
||||
const HVX_Vector scales = hvx_vmem(tile + 512);
|
||||
const HVX_Vector scale_hf = hvx_vec_repl_f16(Q6_V_vror_VR(scales, 2 * row));
|
||||
@@ -694,13 +841,13 @@ static inline void hvx_dequantize_row_tiled_q1_0_f32(float * dst_spad, const uin
|
||||
}
|
||||
}
|
||||
|
||||
static inline void hvx_dequantize_row_tiled_q2_0_f32(float * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
static inline void hvx_dequantize_row_tiled_q2_0_f32(void * dst_spad, const uint8_t * src_spad, uint32_t n_k_tiles, uint32_t tile_stride, uint32_t row) {
|
||||
const HVX_Vector mask_03 = Q6_Vb_vsplat_R(0x03);
|
||||
const HVX_Vector v_one = Q6_Vb_vsplat_R(1);
|
||||
|
||||
for (uint32_t k_tile = 0; k_tile < n_k_tiles; ++k_tile) {
|
||||
const uint8_t * tile = src_spad + k_tile * tile_stride;
|
||||
float * dst = dst_spad + k_tile * HTP_TENSOR_TILE_K_Q2_0;
|
||||
float * dst = (float *) dst_spad + k_tile * HTP_TENSOR_TILE_K_Q2_0;
|
||||
|
||||
const HVX_Vector v_packed = hvx_dequantize_tiled_f16_gather4(tile, 4 * row);
|
||||
const HVX_Vector p0 = Q6_V_vand_VV(v_packed, mask_03);
|
||||
|
||||
@@ -52,6 +52,85 @@ static inline HVX_Vector hvx_vec_fast_sigmoid_f32_guard(HVX_Vector v,
|
||||
return Q6_V_vmux_QVV(pred_min, out, Q6_V_vzero());
|
||||
}
|
||||
|
||||
static inline HVX_VectorPair hvx_vec_fast_sigmoid_f32_guard_pair(HVX_Vector x0,
|
||||
HVX_Vector x1,
|
||||
HVX_Vector one,
|
||||
HVX_Vector max_exp,
|
||||
HVX_Vector min_exp) {
|
||||
const HVX_Vector c_log2f = Q6_V_vsplat_R(FAST_SIGMOID_LOG2F);
|
||||
const HVX_Vector c1 = Q6_V_vsplat_R(FAST_SIGMOID_C1);
|
||||
const HVX_Vector c2 = Q6_V_vsplat_R(FAST_SIGMOID_C2);
|
||||
const HVX_Vector c3 = Q6_V_vsplat_R(FAST_SIGMOID_C3);
|
||||
const HVX_Vector inv_aprox = Q6_V_vsplat_R(0x7EEEEBB3);
|
||||
const HVX_Vector two_sf = hvx_vec_splat_f32(2.0f);
|
||||
const HVX_Vector zero = Q6_V_vzero();
|
||||
|
||||
HVX_Vector v0 = Q6_Vqf32_vmpy_VsfVsf(x0, c_log2f);
|
||||
HVX_Vector v1 = Q6_Vqf32_vmpy_VsfVsf(x1, c_log2f);
|
||||
v0 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(v0), c3);
|
||||
v1 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(v1), c3);
|
||||
|
||||
HVX_Vector in_int0 = hvx_vec_truncate_f32(Q6_Vsf_equals_Vqf32(v0));
|
||||
HVX_Vector in_int1 = hvx_vec_truncate_f32(Q6_Vsf_equals_Vqf32(v1));
|
||||
HVX_Vector f0 = Q6_Vqf32_vsub_Vqf32Vsf(v0, Q6_Vsf_equals_Vw(in_int0));
|
||||
HVX_Vector f1 = Q6_Vqf32_vsub_Vqf32Vsf(v1, Q6_Vsf_equals_Vw(in_int1));
|
||||
HVX_Vector ff0 = Q6_Vqf32_vmpy_Vqf32Vqf32(f0, f0);
|
||||
HVX_Vector ff1 = Q6_Vqf32_vmpy_Vqf32Vqf32(f1, f1);
|
||||
|
||||
HVX_Vector p1_0 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(ff0), c2);
|
||||
HVX_Vector p1_1 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(ff1), c2);
|
||||
p1_0 = Q6_Vqf32_vadd_Vqf32Vsf(p1_0, c_log2f);
|
||||
p1_1 = Q6_Vqf32_vadd_Vqf32Vsf(p1_1, c_log2f);
|
||||
|
||||
HVX_Vector p2_0 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(f0), c1);
|
||||
HVX_Vector p2_1 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(f1), c1);
|
||||
p2_0 = Q6_Vqf32_vmpy_Vqf32Vqf32(p2_0, ff0);
|
||||
p2_1 = Q6_Vqf32_vmpy_Vqf32Vqf32(p2_1, ff1);
|
||||
p2_0 = Q6_Vqf32_vadd_Vqf32Vqf32(p2_0, f0);
|
||||
p2_1 = Q6_Vqf32_vadd_Vqf32Vqf32(p2_1, f1);
|
||||
|
||||
HVX_Vector v3_0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(p2_0, p1_0));
|
||||
HVX_Vector v3_1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(p2_1, p1_1));
|
||||
v3_0 = Q6_Vw_vaslacc_VwVwR(v3_0, in_int0, 24);
|
||||
v3_1 = Q6_Vw_vaslacc_VwVwR(v3_1, in_int1, 24);
|
||||
|
||||
HVX_Vector v4_0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(p2_0, p1_0));
|
||||
HVX_Vector v4_1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(p2_1, p1_1));
|
||||
HVX_Vector v5_0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(v3_0, v4_0));
|
||||
HVX_Vector v5_1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(v3_1, v4_1));
|
||||
|
||||
HVX_Vector i0 = Q6_Vw_vsub_VwVw(inv_aprox, v5_0);
|
||||
HVX_Vector i1 = Q6_Vw_vsub_VwVw(inv_aprox, v5_1);
|
||||
|
||||
HVX_Vector r0 = Q6_Vqf32_vmpy_VsfVsf(
|
||||
i0, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(two_sf, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(i0, v5_0)))));
|
||||
HVX_Vector r1 = Q6_Vqf32_vmpy_VsfVsf(
|
||||
i1, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(two_sf, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(i1, v5_1)))));
|
||||
|
||||
r0 = Q6_Vqf32_vmpy_Vqf32Vqf32(
|
||||
r0, Q6_Vqf32_vsub_VsfVsf(two_sf, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(r0), v5_0))));
|
||||
r1 = Q6_Vqf32_vmpy_Vqf32Vqf32(
|
||||
r1, Q6_Vqf32_vsub_VsfVsf(two_sf, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(r1), v5_1))));
|
||||
|
||||
r0 = Q6_Vqf32_vmpy_Vqf32Vqf32(
|
||||
r0, Q6_Vqf32_vsub_VsfVsf(two_sf, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(r0), v5_0))));
|
||||
r1 = Q6_Vqf32_vmpy_Vqf32Vqf32(
|
||||
r1, Q6_Vqf32_vsub_VsfVsf(two_sf, Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(r1), v5_1))));
|
||||
|
||||
HVX_Vector out0 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(v3_0, Q6_Vsf_equals_Vqf32(r0)));
|
||||
HVX_Vector out1 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(v3_1, Q6_Vsf_equals_Vqf32(r1)));
|
||||
|
||||
const HVX_VectorPred pmax0 = Q6_Q_vcmp_gt_VsfVsf(max_exp, x0);
|
||||
const HVX_VectorPred pmin0 = Q6_Q_vcmp_gt_VsfVsf(x0, min_exp);
|
||||
const HVX_VectorPred pmax1 = Q6_Q_vcmp_gt_VsfVsf(max_exp, x1);
|
||||
const HVX_VectorPred pmin1 = Q6_Q_vcmp_gt_VsfVsf(x1, min_exp);
|
||||
|
||||
out0 = Q6_V_vmux_QVV(pmin0, Q6_V_vmux_QVV(pmax0, out0, one), zero);
|
||||
out1 = Q6_V_vmux_QVV(pmin1, Q6_V_vmux_QVV(pmax1, out1, one), zero);
|
||||
|
||||
return Q6_W_vcombine_VV(out1, out0);
|
||||
}
|
||||
|
||||
static inline HVX_Vector hvx_vec_fast_sigmoid_f32_2it(HVX_Vector v) {
|
||||
v = Q6_Vqf32_vmpy_VsfVsf(v, Q6_V_vsplat_R(FAST_SIGMOID_LOG2F));
|
||||
v = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(v), Q6_V_vsplat_R(FAST_SIGMOID_C3));
|
||||
@@ -115,11 +194,15 @@ static inline HVX_Vector hvx_vec_tanh_f32(HVX_Vector x) {
|
||||
return Q6_Vsf_equals_Vqf32(res);
|
||||
}
|
||||
|
||||
static inline HVX_Vector hvx_vec_silu_f32(HVX_Vector x) {
|
||||
static inline HVX_Vector hvx_vec_sigmoid_f32(HVX_Vector x) {
|
||||
HVX_Vector one = hvx_vec_splat_f32(1.0f);
|
||||
HVX_Vector max_exp = hvx_vec_splat_f32(87.f);
|
||||
HVX_Vector min_exp = hvx_vec_splat_f32(-87.f);
|
||||
HVX_Vector sig = hvx_vec_fast_sigmoid_f32_guard(x, one, max_exp, min_exp);
|
||||
return hvx_vec_fast_sigmoid_f32_guard(x, one, max_exp, min_exp);
|
||||
}
|
||||
|
||||
static inline HVX_Vector hvx_vec_silu_f32(HVX_Vector x) {
|
||||
HVX_Vector sig = hvx_vec_sigmoid_f32(x);
|
||||
return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(x, sig));
|
||||
}
|
||||
|
||||
@@ -195,33 +278,185 @@ static inline void hvx_tanh_f32_aa(uint8_t * restrict dst, const uint8_t * restr
|
||||
hvx_tanh_loop_body(HVX_Vector, HVX_Vector, hvx_vec_store_a);
|
||||
}
|
||||
|
||||
#if __HVX_ARCH__ >= 81
|
||||
// Centered degree-4 minimax polynomial for 1 / (1 + e) in t = e - 0.5 on [-0.5, 0.5]
|
||||
#define HVX_SIGMOID_RECIP_C0 (0x3956)
|
||||
#define HVX_SIGMOID_RECIP_C1 (0xB711)
|
||||
#define HVX_SIGMOID_RECIP_C2 (0x34AF)
|
||||
#define HVX_SIGMOID_RECIP_C3 (0xB365)
|
||||
#define HVX_SIGMOID_RECIP_C4 (0x3113)
|
||||
|
||||
static inline HVX_Vector hvx_vec_fast_sigmoid_f16(HVX_Vector x_v) {
|
||||
const HVX_Vector a = Q6_V_vand_VV(x_v, Q6_Vh_vsplat_R(0x7FFF));
|
||||
const HVX_Vector z = Q6_Vuh_vmin_VuhVuh(a, Q6_Vh_vsplat_R(0x4C50)); // min(|x|, 17.25) so e underflows to exact 0 in fp16
|
||||
const HVX_Vector y = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(z, Q6_Vh_vsplat_R(0x65C5)));
|
||||
const HVX_Vector t = Q6_Vh_equals_Vhf_rnd(y);
|
||||
const HVX_Vector rho = Q6_V_vand_VV(t, Q6_Vh_vsplat_R(0x03FF));
|
||||
const HVX_Vector r15 = Q6_Vh_vasl_VhR(rho, 5);
|
||||
HVX_Vector rp = Q6_Vh_vadd_VhVh(Q6_Vh_vmpy_VhRh_s1_rnd_sat(r15, HVX_EXP2_FIXED_G2_PAIR), Q6_Vh_vsplat_R(HVX_EXP2_FIXED_G1));
|
||||
rp = Q6_Vh_vadd_VhVh(Q6_Vh_vmpy_VhVh_s1_rnd_sat(rp, r15), Q6_Vh_vsplat_R(HVX_EXP2_FIXED_G0));
|
||||
const HVX_Vector b = Q6_Vh_vadd_VhVh(Q6_Vh_vsub_VhVh(Q6_Vh_vsplat_R(0x6400), t), Q6_Vh_vmpy_VhVh_s1_rnd_sat(rp, rho));
|
||||
const HVX_Vector e = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(b, Q6_Vh_vsplat_R(0x1400)));
|
||||
const HVX_Vector tc = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vadd_VhfVhf(e, Q6_Vh_vsplat_R(0xB800)));
|
||||
|
||||
HVX_Vector p = Q6_Vqf16_vmpy_VhfVhf(tc, Q6_Vh_vsplat_R(HVX_SIGMOID_RECIP_C4));
|
||||
p = Q6_Vqf16_vadd_Vqf16Vhf(p, Q6_Vh_vsplat_R(HVX_SIGMOID_RECIP_C3));
|
||||
p = Q6_Vqf16_vmpy_Vqf16Vhf(p, tc);
|
||||
p = Q6_Vqf16_vadd_Vqf16Vhf(p, Q6_Vh_vsplat_R(HVX_SIGMOID_RECIP_C2));
|
||||
p = Q6_Vqf16_vmpy_Vqf16Vhf(p, tc);
|
||||
p = Q6_Vqf16_vadd_Vqf16Vhf(p, Q6_Vh_vsplat_R(HVX_SIGMOID_RECIP_C1));
|
||||
p = Q6_Vqf16_vmpy_Vqf16Vhf(p, tc);
|
||||
const HVX_Vector r_q = Q6_Vqf16_vadd_Vqf16Vhf(p, Q6_Vh_vsplat_R(HVX_SIGMOID_RECIP_C0));
|
||||
|
||||
const HVX_Vector r = Q6_Vhf_equals_Vqf16(r_q);
|
||||
const HVX_Vector er = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(r_q, e));
|
||||
const HVX_VectorPred is_neg = Q6_Q_vcmp_gt_VhVh(Q6_V_vzero(), x_v);
|
||||
return Q6_V_vmux_QVV(is_neg, er, r);
|
||||
}
|
||||
|
||||
static inline HVX_VectorPair hvx_vec_fast_sigmoid_f16_pair(HVX_Vector x0, HVX_Vector x1) {
|
||||
const HVX_Vector em_mask = Q6_Vh_vsplat_R(0x7FFF);
|
||||
const HVX_Vector v_clamp = Q6_Vh_vsplat_R(0x4C50); // min(|x|, 17.25) so e underflows to exact 0 in fp16
|
||||
const HVX_Vector v_scale = Q6_Vh_vsplat_R(0x65C5);
|
||||
const HVX_Vector v_frac = Q6_Vh_vsplat_R(0x03FF);
|
||||
const HVX_Vector v_g1 = Q6_Vh_vsplat_R(HVX_EXP2_FIXED_G1);
|
||||
const HVX_Vector v_g0 = Q6_Vh_vsplat_R(HVX_EXP2_FIXED_G0);
|
||||
const HVX_Vector v_base = Q6_Vh_vsplat_R(0x6400);
|
||||
const HVX_Vector v_last = Q6_Vh_vsplat_R(0x1400);
|
||||
const HVX_Vector v_nhalf = Q6_Vh_vsplat_R(0xB800);
|
||||
|
||||
const HVX_Vector z0 = Q6_Vuh_vmin_VuhVuh(Q6_V_vand_VV(x0, em_mask), v_clamp);
|
||||
const HVX_Vector z1 = Q6_Vuh_vmin_VuhVuh(Q6_V_vand_VV(x1, em_mask), v_clamp);
|
||||
const HVX_Vector y0 = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(z0, v_scale));
|
||||
const HVX_Vector y1 = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(z1, v_scale));
|
||||
const HVX_Vector t0 = Q6_Vh_equals_Vhf_rnd(y0);
|
||||
const HVX_Vector t1 = Q6_Vh_equals_Vhf_rnd(y1);
|
||||
const HVX_Vector rho0 = Q6_V_vand_VV(t0, v_frac);
|
||||
const HVX_Vector rho1 = Q6_V_vand_VV(t1, v_frac);
|
||||
const HVX_Vector r15_0 = Q6_Vh_vasl_VhR(rho0, 5);
|
||||
const HVX_Vector r15_1 = Q6_Vh_vasl_VhR(rho1, 5);
|
||||
|
||||
HVX_Vector rp0 = Q6_Vh_vadd_VhVh(Q6_Vh_vmpy_VhRh_s1_rnd_sat(r15_0, HVX_EXP2_FIXED_G2_PAIR), v_g1);
|
||||
HVX_Vector rp1 = Q6_Vh_vadd_VhVh(Q6_Vh_vmpy_VhRh_s1_rnd_sat(r15_1, HVX_EXP2_FIXED_G2_PAIR), v_g1);
|
||||
rp0 = Q6_Vh_vadd_VhVh(Q6_Vh_vmpy_VhVh_s1_rnd_sat(rp0, r15_0), v_g0);
|
||||
rp1 = Q6_Vh_vadd_VhVh(Q6_Vh_vmpy_VhVh_s1_rnd_sat(rp1, r15_1), v_g0);
|
||||
|
||||
const HVX_Vector b0 = Q6_Vh_vadd_VhVh(Q6_Vh_vsub_VhVh(v_base, t0), Q6_Vh_vmpy_VhVh_s1_rnd_sat(rp0, rho0));
|
||||
const HVX_Vector b1 = Q6_Vh_vadd_VhVh(Q6_Vh_vsub_VhVh(v_base, t1), Q6_Vh_vmpy_VhVh_s1_rnd_sat(rp1, rho1));
|
||||
const HVX_Vector e0 = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(b0, v_last));
|
||||
const HVX_Vector e1 = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(b1, v_last));
|
||||
const HVX_Vector tc0 = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vadd_VhfVhf(e0, v_nhalf));
|
||||
const HVX_Vector tc1 = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vadd_VhfVhf(e1, v_nhalf));
|
||||
|
||||
const HVX_Vector c4 = Q6_Vh_vsplat_R(HVX_SIGMOID_RECIP_C4);
|
||||
const HVX_Vector c3 = Q6_Vh_vsplat_R(HVX_SIGMOID_RECIP_C3);
|
||||
const HVX_Vector c2 = Q6_Vh_vsplat_R(HVX_SIGMOID_RECIP_C2);
|
||||
const HVX_Vector c1 = Q6_Vh_vsplat_R(HVX_SIGMOID_RECIP_C1);
|
||||
const HVX_Vector c0 = Q6_Vh_vsplat_R(HVX_SIGMOID_RECIP_C0);
|
||||
|
||||
HVX_Vector p0 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(tc0, c4), c3);
|
||||
HVX_Vector p1 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(tc1, c4), c3);
|
||||
p0 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vhf(p0, tc0), c2);
|
||||
p1 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vhf(p1, tc1), c2);
|
||||
p0 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vhf(p0, tc0), c1);
|
||||
p1 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vhf(p1, tc1), c1);
|
||||
const HVX_Vector rq0 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vhf(p0, tc0), c0);
|
||||
const HVX_Vector rq1 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vhf(p1, tc1), c0);
|
||||
|
||||
const HVX_Vector zero = Q6_V_vzero();
|
||||
const HVX_Vector r0 = Q6_Vhf_equals_Vqf16(rq0);
|
||||
const HVX_Vector r1 = Q6_Vhf_equals_Vqf16(rq1);
|
||||
const HVX_Vector er0 = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(rq0, e0));
|
||||
const HVX_Vector er1 = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(rq1, e1));
|
||||
const HVX_Vector out0 = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VhVh(zero, x0), er0, r0);
|
||||
const HVX_Vector out1 = Q6_V_vmux_QVV(Q6_Q_vcmp_gt_VhVh(zero, x1), er1, r1);
|
||||
return Q6_W_vcombine_VV(out1, out0);
|
||||
}
|
||||
#else
|
||||
// Clamping u >= -14.0f keeps 2^u in normal fp16 range while 1.0f + 2^-14 == 1.0f in fp16
|
||||
static inline HVX_Vector hvx_vec_fast_sigmoid_f16(HVX_Vector x_v) {
|
||||
const HVX_Vector v_one = hvx_vec_splat_f16(1.0f);
|
||||
const HVX_Vector v_neg_log2e = hvx_vec_splat_f16(-EXP_LOG2E_F);
|
||||
const HVX_Vector em_mask = Q6_Vh_vsplat_R(0x7FFF);
|
||||
const HVX_Vector half_hf_v = Q6_Vh_vsplat_R(0x3800);
|
||||
const HVX_Vector v_clamp_min = hvx_vec_splat_f16(-14.0f);
|
||||
|
||||
// Compute absolute value of x_v
|
||||
HVX_Vector abs_x = Q6_V_vand_VV(x_v, em_mask);
|
||||
|
||||
// Compute u = -abs_x * log2(e) <= 0.
|
||||
HVX_Vector u = hvx_vec_mul_f16_f16(abs_x, v_neg_log2e);
|
||||
|
||||
// Clamp input to prevent underflow in exp2
|
||||
const HVX_Vector v_clamp_min = hvx_vec_splat_f16(-24.0f);
|
||||
u = Q6_Vhf_vmax_VhfVhf(v_clamp_min, u);
|
||||
|
||||
HVX_Vector exp_val = hvx_vec_exp2_f16(u);
|
||||
HVX_Vector denom = hvx_vec_add_f16_f16(v_one, exp_val);
|
||||
HVX_Vector sig_abs = hvx_vec_inverse_f16(denom);
|
||||
|
||||
// check if x_v < 0 (using integer comparison on absolute value)
|
||||
HVX_Vector abs_x = Q6_V_vand_VV(x_v, em_mask);
|
||||
HVX_VectorPred is_neg = Q6_Q_vcmp_gt_VhVh(abs_x, x_v);
|
||||
HVX_Vector u = hvx_vec_mul_f16_f16(abs_x, v_neg_log2e);
|
||||
u = Q6_Vhf_vmax_VhfVhf(v_clamp_min, u);
|
||||
|
||||
// If x_v < 0, return 1.0f - sig_abs
|
||||
HVX_Vector x_minus_half = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vsub_VhfVhf(u, half_hf_v));
|
||||
HVX_Vector k_v = Q6_Vh_equals_Vhf(x_minus_half);
|
||||
HVX_Vector f_v = Q6_Vhf_equals_Vh(k_v);
|
||||
HVX_Vector x_qf16 = Q6_Vqf16_vsub_VhfVhf(u, f_v);
|
||||
|
||||
// Degree-4 polynomial for 2^f on [-0.5, 0.5] (max error < 4.2e-5, well below fp16 0.5 ULP)
|
||||
HVX_Vector y = Q6_Vqf16_vmpy_Vqf16Vqf16(Q6_Vqf16_vadd_VhfVhf(Q6_Vh_vsplat_R(0x20ed), Q6_V_vzero()), x_qf16);
|
||||
y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x2b1b));
|
||||
y = Q6_Vqf16_vmpy_Vqf16Vqf16(y, x_qf16);
|
||||
y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x33b0));
|
||||
y = Q6_Vqf16_vmpy_Vqf16Vqf16(y, x_qf16);
|
||||
y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x398c));
|
||||
y = Q6_Vqf16_vmpy_Vqf16Vqf16(y, x_qf16);
|
||||
y = Q6_Vqf16_vadd_Vqf16Vhf(y, v_one);
|
||||
|
||||
HVX_Vector exp_val = Q6_Vh_vaslacc_VhVhR(Q6_Vhf_equals_Vqf16(y), k_v, 10);
|
||||
HVX_Vector denom = hvx_vec_add_f16_f16(v_one, exp_val);
|
||||
HVX_Vector sig_abs = hvx_vec_inverse_pos_f16(denom);
|
||||
HVX_Vector sig_neg = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vsub_VhfVhf(v_one, sig_abs));
|
||||
return Q6_V_vmux_QVV(is_neg, sig_neg, sig_abs);
|
||||
}
|
||||
|
||||
static inline HVX_VectorPair hvx_vec_fast_sigmoid_f16_pair(HVX_Vector x0, HVX_Vector x1) {
|
||||
const HVX_Vector v_one = hvx_vec_splat_f16(1.0f);
|
||||
const HVX_Vector v_neg_log2e = hvx_vec_splat_f16(-EXP_LOG2E_F);
|
||||
const HVX_Vector em_mask = Q6_Vh_vsplat_R(0x7FFF);
|
||||
const HVX_Vector half_hf_v = Q6_Vh_vsplat_R(0x3800);
|
||||
const HVX_Vector v_clamp_min = hvx_vec_splat_f16(-14.0f);
|
||||
|
||||
const HVX_Vector abs0 = Q6_V_vand_VV(x0, em_mask);
|
||||
const HVX_Vector abs1 = Q6_V_vand_VV(x1, em_mask);
|
||||
const HVX_VectorPred is_neg0 = Q6_Q_vcmp_gt_VhVh(abs0, x0);
|
||||
const HVX_VectorPred is_neg1 = Q6_Q_vcmp_gt_VhVh(abs1, x1);
|
||||
|
||||
HVX_Vector u0 = Q6_Vhf_vmax_VhfVhf(v_clamp_min, hvx_vec_mul_f16_f16(abs0, v_neg_log2e));
|
||||
HVX_Vector u1 = Q6_Vhf_vmax_VhfVhf(v_clamp_min, hvx_vec_mul_f16_f16(abs1, v_neg_log2e));
|
||||
|
||||
HVX_Vector k0 = Q6_Vh_equals_Vhf(Q6_Vhf_equals_Vqf16(Q6_Vqf16_vsub_VhfVhf(u0, half_hf_v)));
|
||||
HVX_Vector k1 = Q6_Vh_equals_Vhf(Q6_Vhf_equals_Vqf16(Q6_Vqf16_vsub_VhfVhf(u1, half_hf_v)));
|
||||
HVX_Vector q0 = Q6_Vqf16_vsub_VhfVhf(u0, Q6_Vhf_equals_Vh(k0));
|
||||
HVX_Vector q1 = Q6_Vqf16_vsub_VhfVhf(u1, Q6_Vhf_equals_Vh(k1));
|
||||
|
||||
// Degree-4 polynomial for 2^f on [-0.5, 0.5] (max error < 4.2e-5, well below fp16 0.5 ULP)
|
||||
const HVX_Vector c3 = Q6_Vqf16_vadd_VhfVhf(Q6_Vh_vsplat_R(0x20ed), Q6_V_vzero());
|
||||
const HVX_Vector c2 = Q6_Vh_vsplat_R(0x2b1b);
|
||||
const HVX_Vector c1 = Q6_Vh_vsplat_R(0x33b0);
|
||||
const HVX_Vector c0 = Q6_Vh_vsplat_R(0x398c);
|
||||
|
||||
HVX_Vector y0 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vqf16(c3, q0), c2);
|
||||
HVX_Vector y1 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vqf16(c3, q1), c2);
|
||||
y0 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vqf16(y0, q0), c1);
|
||||
y1 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vqf16(y1, q1), c1);
|
||||
y0 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vqf16(y0, q0), c0);
|
||||
y1 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vqf16(y1, q1), c0);
|
||||
y0 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vqf16(y0, q0), v_one);
|
||||
y1 = Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_Vqf16Vqf16(y1, q1), v_one);
|
||||
|
||||
HVX_Vector e0 = Q6_Vh_vaslacc_VhVhR(Q6_Vhf_equals_Vqf16(y0), k0, 10);
|
||||
HVX_Vector e1 = Q6_Vh_vaslacc_VhVhR(Q6_Vhf_equals_Vqf16(y1), k1, 10);
|
||||
HVX_Vector d0 = hvx_vec_add_f16_f16(v_one, e0);
|
||||
HVX_Vector d1 = hvx_vec_add_f16_f16(v_one, e1);
|
||||
HVX_Vector sa0 = hvx_vec_inverse_pos_f16(d0);
|
||||
HVX_Vector sa1 = hvx_vec_inverse_pos_f16(d1);
|
||||
HVX_Vector sn0 = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vsub_VhfVhf(v_one, sa0));
|
||||
HVX_Vector sn1 = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vsub_VhfVhf(v_one, sa1));
|
||||
HVX_Vector out0 = Q6_V_vmux_QVV(is_neg0, sn0, sa0);
|
||||
HVX_Vector out1 = Q6_V_vmux_QVV(is_neg1, sn1, sa1);
|
||||
return Q6_W_vcombine_VV(out1, out0);
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline HVX_Vector hvx_vec_tanh_f16(HVX_Vector x) {
|
||||
// tanh(x) = 2 * sigmoid(2x) - 1
|
||||
const HVX_Vector v_two = hvx_vec_splat_f16(2.0f);
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
#include <hexagon_protos.h>
|
||||
#include <hexagon_types.h>
|
||||
@@ -935,7 +931,7 @@ int op_im2col(struct htp_ops_context * octx) {
|
||||
uint32_t nrows = kparams->total_rows;
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
const struct htp_tensor_mdev_range range =
|
||||
htp_tensor_mdev_partition(kparams->total_rows, htp_tensor_mdev_data_aligned(dst) ? kparams->rows_per_chunk : 0,
|
||||
htp_tensor_mdev_partition(kparams->total_rows, 1,
|
||||
octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
row_base = range.start;
|
||||
nrows = range.count;
|
||||
@@ -1039,7 +1035,7 @@ int op_im2col_3d(struct htp_ops_context * octx) {
|
||||
uint32_t nrows = kparams->total_rows;
|
||||
if (octx->ctx->mdev.count > 1) {
|
||||
const struct htp_tensor_mdev_range range =
|
||||
htp_tensor_mdev_partition(kparams->total_rows, htp_tensor_mdev_data_aligned(dst) ? kparams->rows_per_chunk : 0,
|
||||
htp_tensor_mdev_partition(kparams->total_rows, 1,
|
||||
octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
|
||||
row_base = range.start;
|
||||
nrows = range.count;
|
||||
|
||||
@@ -65,7 +65,6 @@ struct htp_im2col_kernel_params {
|
||||
uint32_t OD, OH, OW;
|
||||
uint32_t patch_stride;
|
||||
uint32_t total_rows;
|
||||
uint32_t rows_per_chunk;
|
||||
|
||||
struct fastdiv_values div_OH;
|
||||
struct fastdiv_values div_OD;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -18,7 +18,7 @@ extern "C" {
|
||||
#define HTP_LID_HMX_TOKENS_MAX 512 // HMX can keep more tokens; VTCM decides
|
||||
#define HTP_LID_HMX_KV_BLOCK_MAX 2048
|
||||
#define HTP_LID_HMX_MAX_QSETS 8 // streams in one HMX pipeline run
|
||||
#define HTP_LID_N_BUF 4 // K / mask / output buffers per thread (two blocks computed, two in flight)
|
||||
#define HTP_LID_N_BUF 4 // K / mask / output buffers per HVX (two blocks computed, two in flight)
|
||||
|
||||
#ifndef HMX_FP16_TILE_N_COLS
|
||||
#define HMX_FP16_TILE_N_COLS 32
|
||||
@@ -72,15 +72,15 @@ struct htp_lid_vtcm_layout {
|
||||
size_t off_o;
|
||||
|
||||
size_t size_q_row; // f16 Q row, padded
|
||||
size_t size_qs_row; // f32 Q staging row, padded
|
||||
size_t size_qs_row; // f32/f16 Q staging row, padded
|
||||
size_t size_k_row; // f16 K row, padded
|
||||
size_t size_k_raw_row;
|
||||
size_t size_m_row; // f16 mask row of one kv block, padded
|
||||
size_t size_o_row; // f32 output row of one kv block, padded
|
||||
|
||||
size_t size_qs; // per thread
|
||||
size_t size_qs; // per HVX
|
||||
size_t size_k_buf; // one K buffer
|
||||
size_t size_kt; // per thread: two transposed K blocks, two 64x64 scratch tiles, Q8_0 block scales
|
||||
size_t size_kt; // per HVX: two transposed K blocks, two 64x64 scratch tiles, Q8_0 block scales
|
||||
size_t size_m_buf; // one mask buffer
|
||||
size_t size_o_buf; // one output buffer
|
||||
|
||||
@@ -122,29 +122,29 @@ static inline size_t htp_lid_compute_vtcm_usage(size_t D, size_t n_head, size_t
|
||||
// HMX path. K is the activation (kv rows), Q the weights (one output channel per (token, head)).
|
||||
// mm1: S = relu(K Q^T) * |w| per channel (HMX convert scale + pos clamp)
|
||||
// mm2: O = S G, G[(t,h), t] = sign(w[t,h]). O is [kv, token], f16.
|
||||
// Q weights, G, scales and w are per Q set (one per stream when all tokens fit one tile).
|
||||
// Q weights, G and scales are per Q set (one per stream when all tokens fit one tile).
|
||||
struct htp_lid_hmx_vtcm_layout {
|
||||
size_t off_qw; // Q weight tiles [n_qsets][n_ch][n_dot]
|
||||
size_t off_g; // G weight tiles [n_qsets][n_ch]
|
||||
size_t off_sc; // per channel scales, 256 bytes per channel tile, [n_qsets][n_ch]
|
||||
size_t off_sc_id; // identity scales
|
||||
size_t off_s; // S tiles of one block and token tile, [n_rt][n_sg]
|
||||
size_t off_w; // w of the token tile, [n_qsets][n_tok][n_head] f32
|
||||
size_t off_qs; // per thread: f32 and f16 staging for 32 Q rows, gather scratch
|
||||
size_t off_w; // per HVX: w staging for its channel tiles
|
||||
size_t off_qs; // per HVX: f32/f16 and f16 staging for 32 Q rows, gather scratch
|
||||
size_t off_k_raw[2]; // K rows, [Bc][size_k_row]
|
||||
size_t off_k_t[2]; // K tiles [n_rt][n_dot]
|
||||
size_t off_o[2]; // O tiles [n_rt][n_u]
|
||||
size_t off_m[4]; // mask rows [n_tok][size_m_row]
|
||||
size_t off_out[2]; // f32 output rows [n_tok][size_o_row]
|
||||
|
||||
size_t size_qs_row; // f32 Q row, padded
|
||||
size_t size_qs_row; // f32/f16 Q row, padded
|
||||
size_t size_qh_row; // f16 Q row, padded
|
||||
size_t size_k_row; // f16 K row, padded
|
||||
size_t size_k_raw_row;
|
||||
size_t size_m_row;
|
||||
size_t size_o_row;
|
||||
size_t size_qs; // per thread
|
||||
size_t size_w; // per Q set
|
||||
size_t size_qs; // per HVX
|
||||
size_t size_w; // per HVX
|
||||
|
||||
uint32_t n_ch; // channel tiles: ceil(n_tok * n_head / 32)
|
||||
uint32_t n_u; // token tiles: ceil(n_tok / 32)
|
||||
@@ -169,8 +169,9 @@ static inline void htp_lid_hmx_vtcm_layout_build(struct htp_lid_hmx_vtcm_layout
|
||||
L->size_k_raw_row = L->size_k_row > hex_round_up(k_row_size, 128) ? L->size_k_row : hex_round_up(k_row_size, 128);
|
||||
L->size_m_row = hex_round_up(Bc * sizeof(uint16_t), 128);
|
||||
L->size_o_row = hex_round_up(Bc * sizeof(float), 128);
|
||||
const size_t w_tiles = (n_qsets * L->n_ch + n_hvx - 1) / n_hvx;
|
||||
L->size_qs = 2 * 32 * L->size_qs_row + 32 * L->size_qh_row + hex_round_up(Bc / 64 * 128, 128);
|
||||
L->size_w = hex_round_up(n_tok * n_head * sizeof(float), 128);
|
||||
L->size_w = w_tiles * 128 + 2 * hex_round_up(n_head * sizeof(float), 128) + 256;
|
||||
|
||||
const size_t tile = HMX_FP16_TILE_SIZE;
|
||||
|
||||
@@ -185,7 +186,7 @@ static inline void htp_lid_hmx_vtcm_layout_build(struct htp_lid_hmx_vtcm_layout
|
||||
VTCM_LAYOUT_ALLOC(off, off_o[1], (size_t) L->n_rt * L->n_u * tile);
|
||||
VTCM_LAYOUT_ALLOC(off, off_sc, n_qsets * L->n_ch * 256);
|
||||
VTCM_LAYOUT_ALLOC(off, off_sc_id, 256);
|
||||
VTCM_LAYOUT_ALLOC(off, off_w, n_qsets * L->size_w);
|
||||
VTCM_LAYOUT_ALLOC(off, off_w, n_hvx * L->size_w);
|
||||
VTCM_LAYOUT_ALLOC(off, off_qs, n_hvx * L->size_qs);
|
||||
for (int i = 0; i < 2; i++) {
|
||||
VTCM_LAYOUT_ALLOC(off, off_k_raw[i], Bc * L->size_k_raw_row);
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
#pragma clang diagnostic ignored "-Wunused-variable"
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
|
||||
#include <HAP_farf.h>
|
||||
#include <HAP_perf.h>
|
||||
#include <AEEStdErr.h>
|
||||
@@ -22,6 +17,7 @@
|
||||
|
||||
#include "htp-thread.h"
|
||||
#include "hex-utils.h"
|
||||
#include "dma-proxy.h"
|
||||
#include "dma-queue.h"
|
||||
#include "hmx-queue.h"
|
||||
|
||||
@@ -29,8 +25,8 @@
|
||||
#include "ggml-common.h"
|
||||
#include "hex-bitmap.h"
|
||||
#include "htp-ctx.h"
|
||||
#include "htp-dcache.h"
|
||||
#include "htp-ops.h"
|
||||
#include "htp-vtcm.h"
|
||||
#include "htp_iface.h"
|
||||
#include "work-queue.h"
|
||||
#include "htp-trace.h"
|
||||
@@ -38,9 +34,10 @@
|
||||
#include "dsv4-hc-ops.h"
|
||||
#include "htp-fence.h"
|
||||
|
||||
#define DMA_PROXY_CAPACITY 64
|
||||
#define DMA_PROXY_STACK_SIZE 16384
|
||||
#define HMX_QUEUE_CAPACITY 128
|
||||
#define HMX_QUEUE_STACK_SIZE 16384
|
||||
#define WORK_QUEUE_CAPACITY 16
|
||||
#define WORK_QUEUE_STACK_SIZE 16384
|
||||
#define MAIN_THREAD_STACK_SIZE 32768
|
||||
|
||||
@@ -348,14 +345,14 @@ static int vtcm_alloc(struct htp_context * ctx, uint64_t size_override) {
|
||||
}
|
||||
|
||||
void * vtcm_ptr;
|
||||
if (HAP_compute_res_attr_get_vtcm_ptr_v2(&attr, &vtcm_ptr, &vtcm_size) != 0) {
|
||||
if (HAP_compute_res_attr_get_vtcm_ptr_v2(&attr, &vtcm_ptr, &vtcm_size) != 0 || vtcm_size < 2 * HTP_VTCM_GUARD_SIZE) {
|
||||
HAP_compute_res_release(rctx);
|
||||
FARF(ERROR, "failed to allocate %zu bytes VTCM (new)\n", ctx->vtcm_size);
|
||||
return AEE_ENOMEMORY;
|
||||
}
|
||||
|
||||
ctx->vtcm_base = (uint8_t *) vtcm_ptr;
|
||||
ctx->vtcm_size = vtcm_size;
|
||||
ctx->vtcm_size = htp_vtcm_usable_size(vtcm_size);
|
||||
ctx->vtcm_rctx = rctx;
|
||||
atomic_store(&ctx->vtcm_valid, false);
|
||||
atomic_store(&ctx->vtcm_needs_release, false);
|
||||
@@ -502,8 +499,8 @@ AEEResult htp_iface_start(remote_handle64 handle, uint32_t sess_id, uint64_t dsp
|
||||
if (n_hvx > HTP_MAX_NHVX) {
|
||||
n_hvx = HTP_MAX_NHVX;
|
||||
}
|
||||
if (n_hvx > hw_nth - 1) {
|
||||
n_hvx = hw_nth - 1;
|
||||
if (n_hvx > hw_nth - 2) {
|
||||
n_hvx = hw_nth - 2;
|
||||
}
|
||||
|
||||
uint32_t n_threads = hw_nth;
|
||||
@@ -525,7 +522,7 @@ AEEResult htp_iface_start(remote_handle64 handle, uint32_t sess_id, uint64_t dsp
|
||||
// 3. work_queue
|
||||
size_t wq_align = work_queue_alignof();
|
||||
size_t offset_wq = hex_align_up(offset, wq_align);
|
||||
size_t size_wq = work_queue_sizeof(n_hvx, WORK_QUEUE_CAPACITY, WORK_QUEUE_STACK_SIZE);
|
||||
size_t size_wq = work_queue_sizeof(n_hvx, WORK_QUEUE_STACK_SIZE);
|
||||
offset = offset_wq + size_wq;
|
||||
|
||||
// 4. dma_queue
|
||||
@@ -548,6 +545,12 @@ AEEResult htp_iface_start(remote_handle64 handle, uint32_t sess_id, uint64_t dsp
|
||||
offset = offset_hmx + size_hmx;
|
||||
}
|
||||
|
||||
// 6. dma_proxy
|
||||
size_t dp_align = dma_proxy_alignof();
|
||||
size_t offset_dma_proxy = hex_align_up(offset, dp_align);
|
||||
size_t size_dma_proxy = dma_proxy_sizeof(DMA_PROXY_CAPACITY, DMA_PROXY_STACK_SIZE);
|
||||
offset = offset_dma_proxy + size_dma_proxy;
|
||||
|
||||
size_t footprint = hex_align_up(offset, 128);
|
||||
|
||||
void * block = memalign(4096, footprint);
|
||||
@@ -610,6 +613,9 @@ AEEResult htp_iface_start(remote_handle64 handle, uint32_t sess_id, uint64_t dsp
|
||||
|
||||
#if (__HEXAGON_ARCH__ >= 79)
|
||||
HAP_set_dcvs_v3_protected_bus_corners(&request, 1);
|
||||
if (power_mode >= HTP_POWER_MODE_HIGH_PERFORMANCE) {
|
||||
HAP_set_ddr_perf_mode(&request, 1);
|
||||
}
|
||||
#endif
|
||||
if ((err = HAP_power_set((void *) ctx, &request)) != 0) {
|
||||
htp_iface_stop(handle);
|
||||
@@ -677,13 +683,15 @@ AEEResult htp_iface_start(remote_handle64 handle, uint32_t sess_id, uint64_t dsp
|
||||
dma_ptr_curr += size_dma_q;
|
||||
}
|
||||
|
||||
ctx->main_th = (htp_thread_t) { 0, ctx->dma[0], &ctx->trace[0] };
|
||||
|
||||
ctx->ddr_spad_size = HTP_DDR_SPAD_SIZE;
|
||||
ctx->ddr_spad_base = memalign(128, ctx->ddr_spad_size);
|
||||
|
||||
htp_thread_reset();
|
||||
|
||||
void * wq_ptr = (void *) ((uintptr_t) block + offset_wq);
|
||||
ctx->hvx_queue = work_queue_init(wq_ptr, n_hvx, WORK_QUEUE_CAPACITY, WORK_QUEUE_STACK_SIZE, ctx->dma, ctx->trace);
|
||||
ctx->hvx_queue = work_queue_init(wq_ptr, n_hvx, WORK_QUEUE_STACK_SIZE, ctx->dma, ctx->trace);
|
||||
|
||||
ctx->hmx_enabled = n_hmx;
|
||||
ctx->hmx_queue = NULL;
|
||||
@@ -693,6 +701,9 @@ AEEResult htp_iface_start(remote_handle64 handle, uint32_t sess_id, uint64_t dsp
|
||||
}
|
||||
FARF(HIGH, "HMX %s (n_hmx=%d)", ctx->hmx_enabled ? "enabled" : "disabled", n_hmx);
|
||||
|
||||
void * dp_ptr = (void *) ((uintptr_t) block + offset_dma_proxy);
|
||||
ctx->dma_proxy = dma_proxy_init(dp_ptr, DMA_PROXY_CAPACITY, DMA_PROXY_STACK_SIZE, ctx->dma, ctx->trace);
|
||||
|
||||
ctx->main_stack = NULL;
|
||||
ctx->main_thread = 0;
|
||||
atomic_store(&ctx->killed, false);
|
||||
@@ -744,16 +755,21 @@ AEEResult htp_iface_stop(remote_handle64 handle) {
|
||||
|
||||
work_queue_free(ctx->hvx_queue);
|
||||
|
||||
for (int i = 0; i < ctx->n_threads; i++) {
|
||||
dma_queue_free(ctx->dma[i]);
|
||||
}
|
||||
|
||||
if (ctx->hmx_queue) {
|
||||
hmx_queue_free(ctx->hmx_queue);
|
||||
ctx->hmx_queue = NULL;
|
||||
}
|
||||
ctx->hmx_enabled = false;
|
||||
|
||||
if (ctx->dma_proxy) {
|
||||
dma_proxy_free(ctx->dma_proxy);
|
||||
ctx->dma_proxy = NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < ctx->n_threads; i++) {
|
||||
dma_queue_free(ctx->dma[i]);
|
||||
}
|
||||
|
||||
vtcm_free(ctx);
|
||||
|
||||
if (ctx->ddr_spad_base) {
|
||||
@@ -762,6 +778,18 @@ AEEResult htp_iface_stop(remote_handle64 handle) {
|
||||
ctx->ddr_spad_size = 0;
|
||||
}
|
||||
|
||||
#if (__HEXAGON_ARCH__ >= 79)
|
||||
{
|
||||
HAP_power_request_t request;
|
||||
memset(&request, 0, sizeof(request));
|
||||
request.type = HAP_power_set_DCVS_v3;
|
||||
request.dcvs_v3.set_dcvs_enable = TRUE;
|
||||
request.dcvs_v3.dcvs_enable = TRUE;
|
||||
HAP_set_ddr_perf_mode(&request, 0);
|
||||
HAP_power_set((void *) ctx, &request);
|
||||
}
|
||||
#endif
|
||||
|
||||
free(ctx);
|
||||
h->ctx = NULL;
|
||||
|
||||
@@ -787,8 +815,8 @@ AEEResult htp_iface_hwinfo(remote_handle64 handle, uint32_t * n_threads, uint32_
|
||||
if (n_hvx_val > HTP_MAX_NHVX) {
|
||||
n_hvx_val = HTP_MAX_NHVX;
|
||||
}
|
||||
if (n_hvx_val > n_threads_val - 1) {
|
||||
n_hvx_val = n_threads_val - 1;
|
||||
if (n_hvx_val > n_threads_val - 2) {
|
||||
n_hvx_val = n_threads_val - 2;
|
||||
}
|
||||
|
||||
*n_threads = n_threads_val;
|
||||
@@ -858,8 +886,6 @@ static int op_fence(struct htp_ops_context * octx) {
|
||||
atomic_uint * sync_fence = (atomic_uint *) (uintptr_t) sync->data;
|
||||
|
||||
if (mode == 1) {
|
||||
htp_dcache_flush_dirty_ranges(ctx);
|
||||
|
||||
htp_mdev_group_barrier(octx);
|
||||
|
||||
if (ctx->mdev.idx == 0) {
|
||||
@@ -932,6 +958,7 @@ static int execute_op(struct htp_ops_context * octx) {
|
||||
return op_matmul_id_nx(octx);
|
||||
|
||||
case HTP_OP_MUL_MAT_NX:
|
||||
case HTP_OP_MUL_MAT_NX_GLU:
|
||||
return op_matmul_nx(octx);
|
||||
|
||||
case HTP_OP_MUL:
|
||||
@@ -1273,8 +1300,6 @@ static int proc_op_req(struct htp_ops_context * octx, struct htp_buf_desc * bufs
|
||||
src->ne[0], src->ne[1], src->ne[2], src->ne[3]);
|
||||
}
|
||||
|
||||
htp_dcache_flush_dirty_tensors(octx->ctx, octx->src, HTP_OP_MAX_INPUTS);
|
||||
|
||||
// Prep output tensors
|
||||
for (uint32_t i = 0; i < HTP_OP_MAX_OUTPUTS; i++) {
|
||||
uint16_t dst_idx = op->dst[i];
|
||||
@@ -1289,17 +1314,12 @@ static int proc_op_req(struct htp_ops_context * octx, struct htp_buf_desc * bufs
|
||||
dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3]);
|
||||
}
|
||||
|
||||
htp_dcache_mark_dirty_tensors(octx->ctx, octx->dsts, HTP_OP_MAX_OUTPUTS);
|
||||
|
||||
htp_mdev_group_barrier(octx);
|
||||
|
||||
int status = execute_op(octx);
|
||||
|
||||
htp_ops_context_set_status(octx, status);
|
||||
|
||||
octx->src0_spad.src = NULL;
|
||||
octx->src1_spad.src = NULL;
|
||||
octx->dst_spad.src = NULL;
|
||||
|
||||
return octx->status;
|
||||
}
|
||||
@@ -1352,7 +1372,6 @@ static void process_opbatch(struct htp_context * ctx, const struct htp_opbatch_r
|
||||
htp_trace_event_start(&ctx->trace[0], HTP_TRACE_EVT_L2FLUSH, 0);
|
||||
qurt_mem_cache_clean((qurt_addr_t) 0, 0, QURT_MEM_CACHE_FLUSH_INVALIDATE_ALL, QURT_MEM_DCACHE);
|
||||
hex_l2fetch_block(ctx, ctx->footprint);
|
||||
memset(ctx->dirty_ranges, 0, sizeof(ctx->dirty_ranges));
|
||||
htp_trace_event_stop(&ctx->trace[0], HTP_TRACE_EVT_L2FLUSH, 0);
|
||||
|
||||
htp_trace_event_start(&ctx->trace[0], HTP_TRACE_EVT_BUFF, 0);
|
||||
@@ -1395,6 +1414,7 @@ static void process_opbatch(struct htp_context * ctx, const struct htp_opbatch_r
|
||||
}
|
||||
}
|
||||
|
||||
dma_proxy_flush(ctx->dma_proxy);
|
||||
if (ctx->hmx_queue) {
|
||||
hmx_queue_suspend(ctx->hmx_queue);
|
||||
hmx_queue_flush(ctx->hmx_queue);
|
||||
|
||||
+3692
-1325
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user