mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-11-30 14:47:40 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
779b413a67 | ||
|
|
7dfa241048 | ||
|
|
49f972e139 | ||
|
|
019951ec44 | ||
|
|
616c931e4b | ||
|
|
e1b9f46453 | ||
|
|
559afe384f | ||
|
|
2d48484127 | ||
|
|
22d68b8ac3 | ||
|
|
7778397892 | ||
|
|
8d0323c856 | ||
|
|
c2d2fc6a10 | ||
|
|
9bb00f98c4 | ||
|
|
3e23348afe | ||
|
|
0f72a8695a | ||
|
|
81a22ceb7b | ||
|
|
a346a49f88 | ||
|
|
2924a0e26b | ||
|
|
808d1fed52 | ||
|
|
89bfabaf81 | ||
|
|
f6621d0ad3 | ||
|
|
9fde456d34 | ||
|
|
aa631e031e | ||
|
|
bf062c6018 | ||
|
|
2f94007cc1 |
@@ -9,6 +9,7 @@
|
||||
# ggml-org/ggml-et : marty1885
|
||||
# ggml-org/ggml-hexagon : lhez, max-krasnyansky
|
||||
# ggml-org/ggml-hip : IMbackK, thelittlefireman
|
||||
# ggml-org/ggml-hrx : AaronStGeorge, Geramy, IMbackK, pwilkin
|
||||
# ggml-org/ggml-metal : forforever73, ggerganov, jhen0409, masterFoad
|
||||
# ggml-org/ggml-opencl : lhez, max-krasnyansky
|
||||
# ggml-org/ggml-openvino : ravi9, wine99
|
||||
@@ -56,6 +57,7 @@
|
||||
/ggml/src/ggml-cuda/vendors/hip.h @IMbackK
|
||||
/ggml/src/ggml-hexagon/ @ggml-org/ggml-hexagon
|
||||
/ggml/src/ggml-hip/ @ggml-org/ggml-hip
|
||||
/ggml/src/ggml-hrx/ @ggml-org/ggml-hrx @AaronStGeorge @Geramy @IMbackK @pwilkin
|
||||
/ggml/src/ggml-et/ @marty1885
|
||||
/ggml/src/ggml-impl.h @ggerganov
|
||||
/ggml/src/ggml-metal/ @ggml-org/ggml-metal
|
||||
|
||||
@@ -37,6 +37,9 @@
|
||||
# with OPENVINO support
|
||||
# GG_BUILD_OPENVINO=1 GG_BUILD_LOW_PERF=1 GGML_OPENVINO_DEVICE=CPU bash ./ci/run.sh ./tmp/results ./tmp/mnt
|
||||
#
|
||||
# with HRX support
|
||||
# GG_BUILD_HRX=1 HRX_SDK_PATH=/path/to/hrx-sdk bash ./ci/run.sh ./tmp/results ./tmp/mnt
|
||||
#
|
||||
|
||||
if [ -z "$2" ]; then
|
||||
echo "usage: $0 <output-dir> <mnt-dir>"
|
||||
@@ -99,6 +102,17 @@ if [ ! -z ${GG_BUILD_ROCM} ]; then
|
||||
CMAKE_EXTRA="${CMAKE_EXTRA} -DGPU_TARGETS=${GG_BUILD_AMDGPU_TARGETS}"
|
||||
fi
|
||||
|
||||
if [ ! -z ${GG_BUILD_HRX} ]; then
|
||||
CMAKE_EXTRA="${CMAKE_EXTRA} -DGGML_HRX=ON"
|
||||
|
||||
if [ ! -z "${HRX_SDK_PATH}" ]; then
|
||||
export CMAKE_PREFIX_PATH="${HRX_SDK_PATH}${CMAKE_PREFIX_PATH:+:${CMAKE_PREFIX_PATH}}"
|
||||
fi
|
||||
|
||||
# TODO: HRX has no quantized KV cache yet (SET_ROWS into q8_0)
|
||||
CTEST_EXTRA="-E test-llama-context"
|
||||
fi
|
||||
|
||||
if [ ! -z ${GG_BUILD_SYCL} ]; then
|
||||
if [ -z ${ONEAPI_ROOT} ]; then
|
||||
echo "Not detected ONEAPI_ROOT, please install oneAPI base toolkit and enable it by:"
|
||||
@@ -439,10 +453,13 @@ function gg_run_qwen3_0_6b {
|
||||
|
||||
(time ./bin/llama-imatrix --model ${model_f16} -f ${wiki_test} -ngl 99 -c 1024 -b 512 --chunks 2 ) 2>&1 | tee -a $OUT/${ci}-imatrix.log
|
||||
|
||||
(time ./bin/test-llama-context --model ${model_q4_0} -ngl 10 -c 1024 -fa off --no-op-offload) 2>&1 | tee -a $OUT/${ci}-test-llama-context.log
|
||||
(time ./bin/test-llama-context --model ${model_q4_0} -ngl 99 -c 1024 -fa off ) 2>&1 | tee -a $OUT/${ci}-test-llama-context.log
|
||||
(time ./bin/test-llama-context --model ${model_q4_0} -ngl 10 -c 1024 -fa on --no-op-offload) 2>&1 | tee -a $OUT/${ci}-test-llama-context.log
|
||||
(time ./bin/test-llama-context --model ${model_q4_0} -ngl 99 -c 1024 -fa on ) 2>&1 | tee -a $OUT/${ci}-test-llama-context.log
|
||||
# TODO: HRX has no quantized KV cache yet (SET_ROWS into q8_0)
|
||||
if [ -z ${GG_BUILD_HRX} ]; then
|
||||
(time ./bin/test-llama-context --model ${model_q4_0} -ngl 10 -c 1024 -fa off --no-op-offload) 2>&1 | tee -a $OUT/${ci}-test-llama-context.log
|
||||
(time ./bin/test-llama-context --model ${model_q4_0} -ngl 99 -c 1024 -fa off ) 2>&1 | tee -a $OUT/${ci}-test-llama-context.log
|
||||
(time ./bin/test-llama-context --model ${model_q4_0} -ngl 10 -c 1024 -fa on --no-op-offload) 2>&1 | tee -a $OUT/${ci}-test-llama-context.log
|
||||
(time ./bin/test-llama-context --model ${model_q4_0} -ngl 99 -c 1024 -fa on ) 2>&1 | tee -a $OUT/${ci}-test-llama-context.log
|
||||
fi
|
||||
|
||||
check_ppl "f16" "$(cat $OUT/${ci}-tg-f16.log | grep "^\[1\]")"
|
||||
if [ -z ${GG_BUILD_NO_BF16} ]; then
|
||||
|
||||
@@ -890,6 +890,11 @@ static bool is_vibevoice_asr_template(const std::string & src) {
|
||||
return src.find("transcribes audio input into text output in JSON format") != std::string::npos;
|
||||
}
|
||||
|
||||
// Qwen3-ASR: the template marks the transcript boundary with <asr_text>
|
||||
static bool is_qwen3_asr_template(const std::string & src) {
|
||||
return src.find("<asr_text>") != std::string::npos;
|
||||
}
|
||||
|
||||
// LFM3-Audio: language code -> name
|
||||
static std::string lfm3_language_name(const std::string & language) {
|
||||
static const std::map<std::string, std::string> names = {
|
||||
@@ -936,6 +941,13 @@ common_chat_prompt_preset common_chat_get_asr_prompt(const common_chat_templates
|
||||
}
|
||||
asr_preset.media_first = true;
|
||||
asr_preset.needs_audio_length = true;
|
||||
} else if (is_qwen3_asr_template(src)) {
|
||||
// Context belongs in the system turn and the user turn is audio only.
|
||||
// Language is detected from the audio, so do not append it to the user text.
|
||||
asr_preset.system = context;
|
||||
asr_preset.user = "";
|
||||
asr_preset.has_context = true;
|
||||
asr_preset.has_language = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -520,8 +520,8 @@ std::optional<common_chat_params> common_chat_try_specialized_template(
|
||||
struct common_chat_prompt_preset {
|
||||
std::string system;
|
||||
std::string user;
|
||||
bool has_language = false; // the preset already names the requested language
|
||||
bool has_context = false; // the user text already holds the context
|
||||
bool has_language = false; // the preset already handles the language, do not append it
|
||||
bool has_context = false; // the preset already handles the context, use preset.user
|
||||
bool media_first = false; // the media goes before the user text
|
||||
bool needs_audio_length = false; // the user text depends on the audio length, call again with it
|
||||
};
|
||||
|
||||
@@ -216,14 +216,33 @@ static value exec_binary_op(context & ctx, const token & op, value left_val, val
|
||||
}
|
||||
return mk_val<value_float>(std::floor(a / b));
|
||||
} else if (op.value == "%") {
|
||||
double rem = std::fmod(a, b);
|
||||
JJ_DEBUG("Modulo operation: %f %% %f = %f", a, b, rem);
|
||||
bool is_float = is_val<value_float>(left_val) || is_val<value_float>(right_val);
|
||||
if (is_float) {
|
||||
return mk_val<value_float>(rem);
|
||||
} else {
|
||||
return mk_val<value_int>(float_to_int(rem));
|
||||
// python semantics: result has the sign of the divisor
|
||||
if (is_val<value_int>(left_val) && is_val<value_int>(right_val)) {
|
||||
const int64_t x = left_val->as_int();
|
||||
const int64_t y = right_val->as_int();
|
||||
if (y == 0) {
|
||||
throw std::runtime_error("integer modulo by zero");
|
||||
}
|
||||
if (y == -1) {
|
||||
// INT64_MIN % -1 is UB
|
||||
return mk_val<value_int>(0);
|
||||
}
|
||||
int64_t r = x % y;
|
||||
if (r != 0 && (r < 0) != (y < 0)) {
|
||||
r += y;
|
||||
}
|
||||
return mk_val<value_int>(r);
|
||||
}
|
||||
double rem = std::fmod(a, b);
|
||||
if (rem != 0) {
|
||||
if ((rem < 0) != (b < 0)) {
|
||||
rem += b;
|
||||
}
|
||||
} else {
|
||||
rem = std::copysign(0.0, b);
|
||||
}
|
||||
JJ_DEBUG("Modulo operation: %f %% %f = %f", a, b, rem);
|
||||
return mk_val<value_float>(rem);
|
||||
} else if (op.value == "<") {
|
||||
JJ_DEBUG("Comparison operation: %f < %f is %d", a, b, a < b);
|
||||
return mk_val<value_bool>(a < b);
|
||||
@@ -951,8 +970,17 @@ value member_expression::execute_impl(context & ctx) const {
|
||||
val = arr->as_array().at(static_cast<size_t>(index));
|
||||
}
|
||||
} else { // value_string
|
||||
if (index >= 0) {
|
||||
const auto byte = cast_val<value_string>(object)->val_str.byte_at(static_cast<size_t>(index));
|
||||
const auto * str = cast_val<value_string>(object);
|
||||
const auto len = str->val_str.length();
|
||||
if (index < 0) {
|
||||
// Avoid negating INT64_MIN.
|
||||
const uint64_t distance = static_cast<uint64_t>(-(index + 1)) + 1;
|
||||
index = distance <= len
|
||||
? static_cast<int64_t>(len - distance)
|
||||
: -1;
|
||||
}
|
||||
if (index >= 0 && static_cast<uint64_t>(index) < len) {
|
||||
const auto byte = str->val_str.byte_at(static_cast<size_t>(index));
|
||||
if (byte) {
|
||||
val = mk_val<value_string>(std::string(1, *byte));
|
||||
}
|
||||
|
||||
@@ -883,7 +883,7 @@ const func_builtins & value_string_t::get_builtins() const {
|
||||
if (count > 0) {
|
||||
throw not_implemented_exception("String replace with count argument not implemented");
|
||||
}
|
||||
if (old_str != new_str) {
|
||||
if (count != 0 && old_str != new_str) {
|
||||
size_t pos = 0;
|
||||
if (old_str.empty()) {
|
||||
std::string new_res;
|
||||
|
||||
@@ -37,6 +37,23 @@ class BertModel(TextModel):
|
||||
if self.cls_out_labels:
|
||||
self.gguf_writer.add_classifier_output_labels([v for k, v in sorted(self.cls_out_labels.items())])
|
||||
|
||||
def generate_extra_tensors(self) -> Iterable[tuple[str, Tensor]]:
|
||||
dense_dir = self.dir_model / "2_Dense"
|
||||
if not self.sentence_transformers_dense_modules or self.model_arch != gguf.MODEL_ARCH.BERT or not dense_dir.is_dir():
|
||||
return
|
||||
|
||||
from safetensors.torch import load_file
|
||||
|
||||
config = json.loads((dense_dir / "config.json").read_text(encoding="utf-8"))
|
||||
if config.get("bias") is not False or config.get("activation_function") not in (
|
||||
"torch.nn.Identity", "torch.nn.modules.linear.Identity",
|
||||
):
|
||||
raise NotImplementedError("BERT dense export requires a bias-free projection with identity activation")
|
||||
|
||||
weight = load_file(str(dense_dir / "model.safetensors"))["linear.weight"]
|
||||
self.gguf_writer.add_embedding_length_out(config["out_features"])
|
||||
yield self.format_tensor_name(gguf.MODEL_TENSOR.DENSE_2_OUT), weight
|
||||
|
||||
def set_vocab(self):
|
||||
tokens, toktypes, tokpre = self.get_vocab_base()
|
||||
self.vocab_size = len(tokens)
|
||||
|
||||
@@ -309,16 +309,25 @@ on 4 physical NPUs, or `--devices 'HTP0[0-1:0],HTP1[0-1:1]'` on 2 physical NPUs
|
||||
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:
|
||||
`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`).
|
||||
|
||||
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
|
||||
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:
|
||||
|
||||
| MMSEL | FWHT | OPLWR | Matmul | Intermediates | Notes |
|
||||
| :---: | :--: | :---: | --------------- | :-----------: | ----------------------------------------------------------------------------------- |
|
||||
| 3 | 0 | 1 | `W8A8` HMX | `FP16` | Default; maximum prefill speed |
|
||||
| 3 | 1 | 1 | `W8A8` HMX+FWHT | `FP16` | Higher accuracy on models with activation outliers |
|
||||
| 3 | 1 | 0 | `W8A8` HMX+FWHT | `FP32` | Avoids `FP16` activation/residual overflow while keeping `W8A8` HMX (higher DDR BW) |
|
||||
| 2 | - | 1 | `FP16` HMX | `FP16` | Higher matmul precision without `W8A8` quantization |
|
||||
| 2 | - | 0 | `FP16` HMX | `FP32` | Maximum HMX precision (`FP16` HMX + `FP32` intermediates) |
|
||||
|
||||
Notes:
|
||||
- `MMSEL`: `GGML_HEXAGON_MM_SELECT` (`--hex-mm-select` in `run.py`); see `GGML_HEXAGON_MM_SELECT` below for `W8A8` integer HMX mode details
|
||||
- `FWHT`: `GGML_HEXAGON_MM_FWHT` (`--hex-mm-fwht` in `run.py`)
|
||||
- `OPLWR`: `GGML_HEXAGON_OPLOWER` (`--hex-oplower` in `run.py`)
|
||||
- Under `MMSEL=3`, projections tagged with `GGML_PREC_F32` automatically use `FP16` HMX.
|
||||
|
||||
## Environment variables
|
||||
|
||||
@@ -369,14 +378,8 @@ outliers or `FP16` residual overflow), try stepping down to higher-precision con
|
||||
|
||||
Unknown values are logged as a warning and fall back to `burst`.
|
||||
|
||||
This must be set in the environment before the process initializes the backend registry, so
|
||||
there is no `llama-*` CLI flag for it. The `run.py` runner exposes it as `--hex-power-mode`.
|
||||
|
||||
Examples:
|
||||
|
||||
`GGML_HEXAGON_POWER_MODE=balanced llama-completion ...`
|
||||
`./scripts/snapdragon/run.py --hex-power-mode sustained_high_performance ...`
|
||||
|
||||
This must be set in the environment before the process initializes the backend registry.
|
||||
The `run.py` runner exposes it as `--hex-power-mode`.
|
||||
|
||||
- `GGML_HEXAGON_VERBOSE=1`
|
||||
Enables verbose logging of Ops from the backend. Example output:
|
||||
@@ -426,9 +429,16 @@ outliers or `FP16` residual overflow), try stepping down to higher-precision con
|
||||
|
||||
- `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`)
|
||||
- `2`: `FP16` HMX matrix engine
|
||||
- `3`: `W8A8` integer HMX matrix engine (default)
|
||||
|
||||
All HMX modes fall back to HVX vector kernels for small batch sizes (`M <= 4`).
|
||||
|
||||
- `GGML_HEXAGON_MM_FWHT=0|1`
|
||||
Enables online block-32 Fast Walsh-Hadamard Transform (FWHT) rotation along `K` in `W8A8` HMX matmul (configurable via `--hex-mm-fwht` in `run.py`):
|
||||
|
||||
- `0`: Disabled (default)
|
||||
- `1`: Enabled - higher accuracy on models with activation outliers
|
||||
|
||||
- `GGML_HEXAGON_OPPOLL=N`
|
||||
Controls host polling while waiting for HTP opbatch completion (configurable via `--hex-oppoll` in `run.py`):
|
||||
@@ -437,4 +447,5 @@ outliers or `FP16` residual overflow), try stepping down to higher-precision con
|
||||
- `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.
|
||||
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.
|
||||
|
||||
@@ -119,6 +119,13 @@ columns `k = 0..31` are split into 8 groups `g = k / 4` of 4 consecutive columns
|
||||
- **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.
|
||||
|
||||
- **F8_E4M3** (1024 bytes per tile, `HTP_TENSOR_TILE_SIZE_F8_E4M3`, lossless tiling with same size as flat FP8):
|
||||
- **Values** (bytes 0..1023, 8 HVX vectors `p = 0..7`):
|
||||
Contains 32 rows x 32 columns of signed 8-bit E4M3 floating-point values (`S:1, E:4 bias 7, M:3`; `0x7F`/`0xFF` = NaN decoded as 0).
|
||||
Vector `p` holds column group `p` (`k = 4*p .. 4*p + 3`) in 4-K-grouped (`vrmpy`) byte order:
|
||||
`tile[p * 128 + row * 4 + j] = W[row][4*p + j]` for `row = 0..31`, `j = 0..3`.
|
||||
- Optional F32 weight scale (per-tensor, per-channel, or per-expert) is passed separately and folded in-kernel.
|
||||
|
||||
- **Q4_1** (640 bytes per tile, `HTP_TENSOR_TILE_SIZE_Q4_1`):
|
||||
- **Quants** (bytes 0..511, 512 bytes):
|
||||
Same 4-K-grouped nibble layout as `Q4_0`.
|
||||
|
||||
@@ -20,6 +20,7 @@ The following sections describe how to build with different backends and options
|
||||
* [CUDA](#cuda)
|
||||
* [MUSA](#musa)
|
||||
* [HIP](#hip)
|
||||
* [HRX](#hrx)
|
||||
* [Vulkan](#vulkan)
|
||||
* [CANN](#cann)
|
||||
* [ZenDNN](#zendnn)
|
||||
@@ -434,6 +435,39 @@ If your GPU is not officially supported you can use the environment variable [`H
|
||||
|
||||
On Linux it is possible to use unified memory architecture (UMA) to share main memory between the CPU and integrated GPU by setting environment variable `GGML_CUDA_ENABLE_UNIFIED_MEMORY=1`. However, this hurts performance for non-integrated GPUs (but enables working with integrated GPUs).
|
||||
|
||||
## HRX
|
||||
|
||||
This provides GPU acceleration on AMD RDNA3, RDNA3.5 and RDNA4 GPUs (gfx1100, gfx1101, gfx1102, gfx1103, gfx1150, gfx1151, gfx1152, gfx1200, gfx1201) through the HRX runtime. The backend is experimental. Kernels are written in Loom and compiled for the GPU at first use, so ROCm is not required.
|
||||
|
||||
Build and install the HRX SDK v0.2.0 from the [hrx-system release](https://github.com/ROCm/hrx-system/releases/tag/v0.2.0) source:
|
||||
|
||||
```bash
|
||||
curl -L https://github.com/ROCm/hrx-system/archive/refs/tags/v0.2.0.tar.gz | tar xz
|
||||
cd hrx-system-0.2.0
|
||||
python3 dev.py cmake setup
|
||||
source .venv/bin/activate
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_SHARED_LIBS=OFF -DIREE_ENABLE_WERROR_FLAG=OFF \
|
||||
-DLIBHRX_BUILD=ON -DLIBHRX_BUILD_HIP_BINDING=OFF -DLIBHRX_BUILD_PASSTHROUGH=OFF -DLIBHRX_BUILD_CTS=OFF
|
||||
cmake --build build --target hrx hrx-info loomc_shared
|
||||
cmake --install build --prefix /path/to/hrx-sdk --component HrxPublicDist
|
||||
```
|
||||
|
||||
Then build llama.cpp with:
|
||||
|
||||
```bash
|
||||
cmake -B build -DGGML_HRX=ON -DCMAKE_PREFIX_PATH=/path/to/hrx-sdk
|
||||
cmake --build build --config Release
|
||||
```
|
||||
|
||||
The devices are named `HRX0`, `HRX1`, ... Use `--list-devices` to show them and `--device HRX0` to select one.
|
||||
|
||||
This version of the backend targets TinyStories-15M (`stories15M-q4_0.gguf`, the model of the llama.cpp tests). It supports only these ops:
|
||||
|
||||
* `MUL_MAT` with Q4_0 weights and F32 activations
|
||||
* `SET_ROWS`, `CPY`, `DUP` and `CONT` (F32 to F32 or F16) and `FILL` (F32 and F16). llama.cpp runs these ops in place on the KV cache and the recurrent state in GPU memory. The CPU cannot run them there.
|
||||
|
||||
All other ops and types run on the CPU.
|
||||
|
||||
## Vulkan
|
||||
|
||||
### For Windows Users:
|
||||
|
||||
+126
-124
@@ -12,127 +12,129 @@ Legend:
|
||||
- 🟡 Partially supported by this backend
|
||||
- ❌ Not supported by this backend
|
||||
|
||||
| Operation | BLAS | CANN | CPU | CUDA | ET | HTP | MTL | OPENVINO | OpenCL | SYCL | Vulkan | WebGPU | ZenDNN | zDNN |
|
||||
|-----------|------|------|------|------|------|------|------|------|------|------|------|------|------|------|
|
||||
| ABS | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ACC | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| ADD | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ADD1 | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| ADD_ID | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ARANGE | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ARGMAX | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ARGSORT | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| CEIL | ❌ | ❌ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| CLAMP | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| COL2IM_1D | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| CONCAT | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| CONT | ❌ | 🟡 | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| CONV_1D_DW | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ |
|
||||
| CONV_2D | ❌ | ❌ | 🟡 | ✅ | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ❌ | ❌ |
|
||||
| CONV_2D_DW | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ❌ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| CONV_3D | ❌ | ❌ | ✅ | ✅ | ❌ | 🟡 | ✅ | 🟡 | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| CONV_TRANSPOSE_1D | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| CONV_TRANSPOSE_2D | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| COS | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| COUNT_EQUAL | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| CPY | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | ❌ |
|
||||
| CROSS_ENTROPY_LOSS | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| CROSS_ENTROPY_LOSS_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| CUMSUM | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| DIAG | ❌ | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| DIV | ❌ | ✅ | ✅ | ✅ | ❌ | 🟡 | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| DSV4_HC_COMB | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| DSV4_HC_POST | ❌ | ❌ | ✅ | ❌ | ❌ | 🟡 | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| DSV4_HC_PRE | ❌ | ❌ | ✅ | ❌ | ❌ | 🟡 | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| DUP | ❌ | ✅ | ✅ | ✅ | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| ELU | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| EXP | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| EXPM1 | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| FILL | ❌ | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| FLASH_ATTN_EXT | ❌ | 🟡 | ✅ | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | ❌ |
|
||||
| FLOOR | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GATED_DELTA_NET | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GATED_LINEAR_ATTN | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| GEGLU | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GEGLU_ERF | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GEGLU_QUICK | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GELU | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GELU_ERF | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GELU_QUICK | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GET_ROWS | ❌ | 🟡 | ✅ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | 🟡 | ❌ | ❌ |
|
||||
| GET_ROWS_BACK | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ❌ | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ❌ |
|
||||
| GROUP_NORM | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| HARDSIGMOID | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| HARDSWISH | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| IM2COL | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| IM2COL_3D | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| L2_NORM | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| LEAKY_RELU | ❌ | ✅ | ✅ | ✅ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| LIGHTNING_INDEXER | ❌ | ❌ | ✅ | ❌ | ❌ | 🟡 | ❌ | ❌ | 🟡 | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| LOG | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| MEAN | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | 🟡 | ✅ | 🟡 | ✅ | ✅ | ❌ | ❌ |
|
||||
| MUL | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| MUL_MAT | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 |
|
||||
| MUL_MAT_HADAMARD | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | 🟡 |
|
||||
| MUL_MAT_ID | ❌ | 🟡 | ✅ | ✅ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | 🟡 | 🟡 | ❌ |
|
||||
| MUL_MAT_ID_W4A4 | ❌ | ❌ | ✅ | ❌ | ❌ | 🟡 | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_ID_W4A8 | ❌ | ❌ | ✅ | ❌ | ❌ | 🟡 | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_PREC_SRC1_BF16 | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_PREC_SRC1_BF16_SRC0 | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_PREC_SRC1_F32 | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_W4A4 | ❌ | ❌ | ✅ | ❌ | ❌ | 🟡 | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_W4A8 | ❌ | ❌ | ✅ | ❌ | ❌ | 🟡 | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| NEG | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| NORM | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| OPT_STEP_ADAMW | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| OPT_STEP_SGD | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| OUT_PROD | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | ❌ | ❌ | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ❌ |
|
||||
| PAD | ❌ | 🟡 | ✅ | 🟡 | ❌ | ✅ | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | ❌ | ❌ |
|
||||
| PAD_REFLECT_1D | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ |
|
||||
| POOL_1D | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| POOL_2D | ❌ | 🟡 | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| REGLU | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| RELU | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| REPEAT | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| REPEAT_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| RMS_NORM | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| RMS_NORM_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| ROLL | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ROPE | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ROPE_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| ROUND | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| RWKV_WKV6 | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| RWKV_WKV7 | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| SCALE | ❌ | 🟡 | ✅ | ✅ | ✅ | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SET | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | 🟡 | ✅ | ✅ | ❌ | ❌ |
|
||||
| SET_ROWS | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | ❌ |
|
||||
| SGN | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SIGMOID | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SIGMOID_BACK | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| SILU | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SILU_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
|
||||
| SIN | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SLEEP | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| SOFTPLUS | ❌ | ❌ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SOFT_MAX | ❌ | 🟡 | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SOFT_MAX_BACK | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ❌ | ❌ | ❌ | 🟡 | ✅ | ❌ | ❌ | ❌ |
|
||||
| SOLVE_TRI | ❌ | ❌ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ❌ | ✅ | 🟡 | ✅ | ✅ | ❌ | ❌ |
|
||||
| SQR | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SQRT | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SSM_CONV | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SSM_SCAN | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | 🟡 | 🟡 | 🟡 | ✅ | ❌ | ❌ |
|
||||
| STEP | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SUB | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SUM | ❌ | 🟡 | ✅ | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | ❌ | ❌ |
|
||||
| SUM_ROWS | ❌ | ✅ | ✅ | 🟡 | ❌ | 🟡 | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ❌ | ❌ |
|
||||
| SWIGLU | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SWIGLU_CLAMP | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| SWIGLU_OAI | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| TANH | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| TIMESTEP_EMBEDDING | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| TOP_K | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | 🟡 | ✅ | ❌ | ❌ |
|
||||
| TOP_K_MASK | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
|
||||
| TRI | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| TRUNC | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | ✅ | ❌ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| UPSCALE | ❌ | 🟡 | ✅ | ✅ | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| XIELU | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| Operation | BLAS | CANN | CPU | CUDA | ET | HRX | HTP | MTL | OPENVINO | OpenCL | SYCL | Vulkan | WebGPU | ZenDNN | zDNN |
|
||||
|-----------|------|------|------|------|------|------|------|------|------|------|------|------|------|------|------|
|
||||
| ABS | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ACC | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| ADD | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ADD1 | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| ADD_ID | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ARANGE | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ARGMAX | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ARGSORT | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| CEIL | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| CLAMP | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| COL2IM_1D | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| CONCAT | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| CONT | ❌ | 🟡 | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| CONV_1D_DW | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ |
|
||||
| CONV_2D | ❌ | ❌ | 🟡 | ✅ | ❌ | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ❌ | ❌ |
|
||||
| CONV_2D_DW | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| CONV_3D | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | 🟡 | ✅ | 🟡 | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| CONV_TRANSPOSE_1D | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| CONV_TRANSPOSE_2D | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| COS | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| COUNT_EQUAL | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| CPY | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | ❌ |
|
||||
| CROSS_ENTROPY_LOSS | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| CROSS_ENTROPY_LOSS_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| CUMSUM | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| DIAG | ❌ | ❌ | ✅ | ✅ | 🟡 | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| DIV | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | 🟡 | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| DSV4_HC_COMB | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| DSV4_HC_POST | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | 🟡 | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| DSV4_HC_PRE | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | 🟡 | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| DUP | ❌ | ✅ | ✅ | ✅ | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| ELU | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| EXP | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| EXPM1 | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | ❌ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| FILL | ❌ | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| FLASH_ATTN_EXT | ❌ | 🟡 | ✅ | 🟡 | 🟡 | ❌ | 🟡 | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | ❌ |
|
||||
| FLOOR | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GATED_DELTA_NET | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GATED_LINEAR_ATTN | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| GEGLU | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GEGLU_ERF | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GEGLU_QUICK | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GELU | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GELU_ERF | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GELU_QUICK | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| GET_ROWS | ❌ | 🟡 | ✅ | 🟡 | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | 🟡 | ❌ | ❌ |
|
||||
| GET_ROWS_BACK | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ❌ |
|
||||
| GROUP_NORM | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| HARDSIGMOID | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| HARDSWISH | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| IM2COL | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| IM2COL_3D | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| L2_NORM | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| LEAKY_RELU | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| LIGHTNING_INDEXER | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | 🟡 | ❌ | ❌ | 🟡 | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| LOG | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| MEAN | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | 🟡 | ✅ | 🟡 | ✅ | ✅ | ❌ | ❌ |
|
||||
| MUL | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| MUL_MAT | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 |
|
||||
| MUL_MAT_HADAMARD | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | 🟡 |
|
||||
| MUL_MAT_ID | ❌ | 🟡 | ✅ | ✅ | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | 🟡 | 🟡 | ❌ |
|
||||
| MUL_MAT_ID_PREC_SRC1_BF16 | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_ID_PREC_SRC1_F32 | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_ID_W4A4 | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | 🟡 | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_ID_W4A8 | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | 🟡 | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_PREC_SRC1_BF16 | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_PREC_SRC1_BF16_SRC0 | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_PREC_SRC1_F32 | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_W4A4 | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | 🟡 | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| MUL_MAT_W4A8 | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | 🟡 | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| NEG | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| NORM | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | ✅ | ✅ | ✅ | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| OPT_STEP_ADAMW | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| OPT_STEP_SGD | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| OUT_PROD | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ❌ |
|
||||
| PAD | ❌ | 🟡 | ✅ | 🟡 | ❌ | ❌ | ✅ | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | ❌ | ❌ |
|
||||
| PAD_REFLECT_1D | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ |
|
||||
| POOL_1D | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| POOL_2D | ❌ | 🟡 | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| REGLU | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| RELU | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| REPEAT | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | 🟡 | ❌ | ❌ |
|
||||
| REPEAT_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| RMS_NORM | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| RMS_NORM_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| ROLL | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ROPE | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| ROPE_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| ROUND | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| RWKV_WKV6 | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| RWKV_WKV7 | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
|
||||
| SCALE | ❌ | 🟡 | ✅ | ✅ | ✅ | ❌ | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SET | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | 🟡 | ✅ | ✅ | ❌ | ❌ |
|
||||
| SET_ROWS | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ❌ | ❌ |
|
||||
| SGN | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SIGMOID | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SIGMOID_BACK | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| SILU | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SILU_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
|
||||
| SIN | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SLEEP | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
|
||||
| SOFTPLUS | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SOFT_MAX | ❌ | 🟡 | ✅ | ✅ | 🟡 | ❌ | 🟡 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SOFT_MAX_BACK | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | 🟡 | ✅ | ❌ | ❌ | ❌ |
|
||||
| SOLVE_TRI | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | ✅ | ✅ | ❌ | ✅ | 🟡 | ✅ | ✅ | ❌ | ❌ |
|
||||
| SQR | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SQRT | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SSM_CONV | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SSM_SCAN | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ❌ | 🟡 | 🟡 | 🟡 | ✅ | ❌ | ❌ |
|
||||
| STEP | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SUB | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SUM | ❌ | 🟡 | ✅ | 🟡 | ❌ | ❌ | 🟡 | 🟡 | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | ❌ | ❌ |
|
||||
| SUM_ROWS | ❌ | ✅ | ✅ | 🟡 | ❌ | ❌ | 🟡 | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ❌ | ❌ |
|
||||
| SWIGLU | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| SWIGLU_CLAMP | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| SWIGLU_OAI | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| TANH | ❌ | ✅ | ✅ | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| TIMESTEP_EMBEDDING | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| TOP_K | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | 🟡 | ✅ | ❌ | ❌ |
|
||||
| TOP_K_MASK | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
|
||||
| TRI | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| TRUNC | ❌ | ❌ | ✅ | 🟡 | 🟡 | ❌ | ❌ | ✅ | ❌ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| UPSCALE | ❌ | 🟡 | ✅ | ✅ | ❌ | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| XIELU | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
|
||||
+24215
File diff suppressed because it is too large
Load Diff
@@ -217,6 +217,7 @@ option(GGML_HIP "ggml: use HIP"
|
||||
option(GGML_HIP_GRAPHS "ggml: use HIP graph" ON)
|
||||
option(GGML_HIP_RCCL "ggml: use ROCm Collective Comm. Library" OFF)
|
||||
option(GGML_HIP_NO_VMM "ggml: do not try to use HIP VMM" ON)
|
||||
option(GGML_HRX "ggml: use HRX (experimental)" OFF)
|
||||
option(GGML_HIP_MMQ_MFMA "ggml: enable MFMA MMA for CDNA in MMQ" ON)
|
||||
option(GGML_HIP_EXPORT_METRICS "ggml: enable kernel perf metrics output" OFF)
|
||||
option(GGML_MUSA_GRAPHS "ggml: use MUSA graph, experimental, unstable" OFF)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "ggml-backend.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
#define GGML_HRX_NAME "HRX"
|
||||
|
||||
GGML_BACKEND_API ggml_backend_t ggml_backend_hrx_init(size_t device);
|
||||
GGML_BACKEND_API bool ggml_backend_is_hrx(ggml_backend_t backend);
|
||||
GGML_BACKEND_API int ggml_backend_hrx_get_device_count(void);
|
||||
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_hrx_buffer_type(size_t device);
|
||||
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_hrx_reg(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
+22
-8
@@ -2309,18 +2309,32 @@ extern "C" {
|
||||
struct ggml_tensor * a,
|
||||
int32_t n_kv_max);
|
||||
|
||||
GGML_API void ggml_flash_attn_ext_add_sinks(
|
||||
GGML_API void ggml_flash_attn_ext_set_sinks(
|
||||
struct ggml_tensor * a,
|
||||
struct ggml_tensor * sinks);
|
||||
|
||||
GGML_API bool ggml_flash_attn_ext_has_indexed_bias(const struct ggml_tensor * a);
|
||||
|
||||
// Add table[indices[key, query], head, query] to the mask, in F32.
|
||||
// Queries from all streams are flattened in stream order. Indices must be in [0, table->ne[0]).
|
||||
GGML_API void ggml_flash_attn_ext_add_indexed_bias(
|
||||
GGML_DEPRECATED(GGML_API void ggml_flash_attn_ext_add_sinks(
|
||||
struct ggml_tensor * a,
|
||||
struct ggml_tensor * table,
|
||||
struct ggml_tensor * indices);
|
||||
struct ggml_tensor * sinks),
|
||||
"use ggml_flash_attn_ext_set_sinks instead");
|
||||
|
||||
// Add bias[bias_idxs[key, query], head, query] to the mask, in F32.
|
||||
// The bias is added after the KQ scaling.
|
||||
// Queries from all streams are flattened in stream order. Indices must be in [0, bias->ne[0]).
|
||||
// If bias_idxs is NULL, bias is a dense K*Q bias with shape [n_kv, n_batch, n_head_kv, n_stream].
|
||||
GGML_API void ggml_flash_attn_ext_set_bias(
|
||||
struct ggml_tensor * a,
|
||||
struct ggml_tensor * bias,
|
||||
struct ggml_tensor * bias_idxs);
|
||||
|
||||
GGML_DEPRECATED(GGML_API bool ggml_flash_attn_ext_has_indexed_bias(const struct ggml_tensor * a),
|
||||
"use ggml_flash_attn_ext_bias_is_indexed instead");
|
||||
|
||||
GGML_DEPRECATED(GGML_API void ggml_flash_attn_ext_add_indexed_bias(
|
||||
struct ggml_tensor * a,
|
||||
struct ggml_tensor * bias,
|
||||
struct ggml_tensor * bias_idxs),
|
||||
"use ggml_flash_attn_ext_set_bias instead");
|
||||
|
||||
// TODO: needs to be adapted to ggml_flash_attn_ext
|
||||
GGML_API struct ggml_tensor * ggml_flash_attn_back(
|
||||
|
||||
@@ -601,6 +601,7 @@ ggml_add_backend(CANN)
|
||||
ggml_add_backend(CUDA)
|
||||
ggml_add_backend(ET)
|
||||
ggml_add_backend(HIP)
|
||||
ggml_add_backend(HRX)
|
||||
ggml_add_backend(METAL)
|
||||
ggml_add_backend(MUSA)
|
||||
ggml_add_backend(RPC)
|
||||
|
||||
@@ -153,7 +153,7 @@ static ggml_backend_buffer_type_t ggml_backend_meta_device_get_buffer_type(ggml_
|
||||
static ggml_backend_buffer_type_t ggml_backend_meta_device_get_host_buffer_type(ggml_backend_dev_t dev);
|
||||
|
||||
static bool ggml_backend_meta_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
|
||||
if (ggml_flash_attn_ext_has_indexed_bias(op)) {
|
||||
if (ggml_flash_attn_ext_has_bias(op)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,10 @@
|
||||
#include "ggml-metal.h"
|
||||
#endif
|
||||
|
||||
#ifdef GGML_USE_HRX
|
||||
#include "ggml-hrx.h"
|
||||
#endif // GGML_USE_HRX
|
||||
|
||||
#ifdef GGML_USE_SYCL
|
||||
#include "ggml-sycl.h"
|
||||
#endif
|
||||
@@ -123,6 +127,9 @@ struct ggml_backend_registry {
|
||||
#ifdef GGML_USE_METAL
|
||||
register_backend(ggml_backend_metal_reg());
|
||||
#endif
|
||||
#ifdef GGML_USE_HRX
|
||||
register_backend(ggml_backend_hrx_reg());
|
||||
#endif // GGML_USE_HRX
|
||||
#ifdef GGML_USE_SYCL
|
||||
register_backend(ggml_backend_sycl_reg());
|
||||
#endif
|
||||
@@ -581,6 +588,7 @@ void ggml_backend_load_all_from_path(const char * dir_path) {
|
||||
ggml_backend_load_best("cann", silent, dir_path);
|
||||
ggml_backend_load_best("cuda", silent, dir_path);
|
||||
ggml_backend_load_best("hip", silent, dir_path);
|
||||
ggml_backend_load_best("hrx", silent, dir_path);
|
||||
ggml_backend_load_best("metal", silent, dir_path);
|
||||
ggml_backend_load_best("rpc", silent, dir_path);
|
||||
ggml_backend_load_best("sycl", silent, dir_path);
|
||||
|
||||
@@ -2686,8 +2686,8 @@ static bool ggml_backend_cann_supports_op(ggml_backend_dev_t dev, const ggml_ten
|
||||
return true;
|
||||
case GGML_OP_FLASH_ATTN_EXT:
|
||||
{
|
||||
// K/V row lists and indexed bias are not supported
|
||||
if (op->src[5] != nullptr || ggml_flash_attn_ext_has_indexed_bias(op)) {
|
||||
// K/V row lists and bias are not supported
|
||||
if (op->src[5] != nullptr || ggml_flash_attn_ext_has_bias(op)) {
|
||||
return false;
|
||||
}
|
||||
#ifdef ASCEND_310P
|
||||
|
||||
@@ -442,7 +442,7 @@ static bool ggml_backend_cpu_device_supports_op(ggml_backend_dev_t dev, const st
|
||||
|
||||
switch (op->op) {
|
||||
case GGML_OP_FLASH_ATTN_EXT:
|
||||
return ggml_validate_flash_attn_ext_indexed_bias(op);
|
||||
return !ggml_flash_attn_ext_has_bias(op) || ggml_flash_attn_ext_bias_is_indexed(op);
|
||||
case GGML_OP_CPY:
|
||||
case GGML_OP_SET_ROWS:
|
||||
return
|
||||
|
||||
+15
-15
@@ -8981,8 +8981,8 @@ static void ggml_compute_forward_flash_attn_ext_f16_one_chunk(
|
||||
const ggml_tensor * mask = dst->src[3];
|
||||
const ggml_tensor * sinks = dst->src[4];
|
||||
const ggml_tensor * kv_rows = dst->src[5];
|
||||
const ggml_tensor * bias_table = dst->src[6];
|
||||
const ggml_tensor * bias_indices = dst->src[7];
|
||||
const ggml_tensor * bias = dst->src[6];
|
||||
const ggml_tensor * bias_idxs = dst->src[7];
|
||||
|
||||
GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
|
||||
GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
|
||||
@@ -9024,7 +9024,7 @@ static void ggml_compute_forward_flash_attn_ext_f16_one_chunk(
|
||||
const int64_t rv2 = neq2/nev2;
|
||||
const int64_t rv3 = neq3/nev3;
|
||||
|
||||
const int64_t n_bins = bias_table ? bias_table->ne[0] : 0;
|
||||
const int64_t n_bins = bias ? bias->ne[0] : 0;
|
||||
|
||||
// parallelize by q rows using ggml_vec_dot_f32
|
||||
|
||||
@@ -9090,8 +9090,8 @@ static void ggml_compute_forward_flash_attn_ext_f16_one_chunk(
|
||||
const int32_t * rows = kv_rows ? (const int32_t *) ((const char *) kv_rows->data + iq3*kv_rows->nb[1]) : nullptr;
|
||||
|
||||
// bias rows of this query and head, queries of all streams are flattened
|
||||
const float * bias = bias_table ? (const float *) ((const char *) bias_table->data + (iq3*neq1 + iq1)*bias_table->nb[2] + iq2*bias_table->nb[1]) : nullptr;
|
||||
const int32_t * bins = bias_indices ? (const int32_t *) ((const char *) bias_indices->data + (iq3*neq1 + iq1)*bias_indices->nb[1]) : nullptr;
|
||||
const float * bias_row = bias ? (const float *) ((const char *) bias->data + (iq3*neq1 + iq1)*bias->nb[2] + iq2*bias->nb[1]) : nullptr;
|
||||
const int32_t * bins = bias_idxs ? (const int32_t *) ((const char *) bias_idxs->data + (iq3*neq1 + iq1)*bias_idxs->nb[1]) : nullptr;
|
||||
|
||||
// online softmax / attention
|
||||
// loop over n_kv and n_head_kv
|
||||
@@ -9099,9 +9099,9 @@ static void ggml_compute_forward_flash_attn_ext_f16_one_chunk(
|
||||
|
||||
for (int64_t ic = ic_start; ic < ic_end; ++ic) {
|
||||
float mv = mp ? GGML_CPU_FP16_TO_FP32(mp[ic]) : 0.0f;
|
||||
if (bias) {
|
||||
if (bias_row) {
|
||||
GGML_ASSERT(bins[ic] >= 0 && bins[ic] < n_bins);
|
||||
mv += bias[bins[ic]];
|
||||
mv += bias_row[bins[ic]];
|
||||
}
|
||||
mv *= slope;
|
||||
if (mv == -INFINITY) {
|
||||
@@ -9281,9 +9281,9 @@ static void ggml_compute_forward_flash_attn_ext_tiled(
|
||||
const ggml_tensor * kv_rows = dst->src[5];
|
||||
const int64_t n_kv_cols = kv_rows ? kv_rows->ne[0] : nek1;
|
||||
|
||||
const ggml_tensor * bias_table = dst->src[6];
|
||||
const ggml_tensor * bias_indices = dst->src[7];
|
||||
const int64_t n_bins = bias_table ? bias_table->ne[0] : 0;
|
||||
const ggml_tensor * bias = dst->src[6];
|
||||
const ggml_tensor * bias_idxs = dst->src[7];
|
||||
const int64_t n_bins = bias ? bias->ne[0] : 0;
|
||||
|
||||
int ir = ir0;
|
||||
while (ir < ir1) {
|
||||
@@ -9363,13 +9363,13 @@ static void ggml_compute_forward_flash_attn_ext_tiled(
|
||||
bool can_skip = true;
|
||||
for (int tq = 0; tq < tile_rows; tq++) {
|
||||
const ggml_fp16_t * mp_row = (const ggml_fp16_t *)((const char *) mask->data + (iq1 + tq)*mask->nb[1] + (iq2%mask->ne[2])*mask->nb[2] + (iq3%mask->ne[3])*mask->nb[3]);
|
||||
const float * bias = bias_table ? (const float *) ((const char *) bias_table->data + (iq3*neq1 + iq1 + tq)*bias_table->nb[2] + iq2*bias_table->nb[1]) : nullptr;
|
||||
const int32_t * bins = bias_indices ? (const int32_t *) ((const char *) bias_indices->data + (iq3*neq1 + iq1 + tq)*bias_indices->nb[1]) : nullptr;
|
||||
const float * bias_row = bias ? (const float *) ((const char *) bias->data + (iq3*neq1 + iq1 + tq)*bias->nb[2] + iq2*bias->nb[1]) : nullptr;
|
||||
const int32_t * bins = bias_idxs ? (const int32_t *) ((const char *) bias_idxs->data + (iq3*neq1 + iq1 + tq)*bias_idxs->nb[1]) : nullptr;
|
||||
for (int tk = 0; tk < kv_tile; tk++) {
|
||||
float m = GGML_CPU_FP16_TO_FP32(mp_row[ic + tk]);
|
||||
if (bias) {
|
||||
if (bias_row) {
|
||||
GGML_ASSERT(bins[ic + tk] >= 0 && bins[ic + tk] < n_bins);
|
||||
m += bias[bins[ic + tk]];
|
||||
m += bias_row[bins[ic + tk]];
|
||||
}
|
||||
mask32[tq * KV_TILE_SZ + tk] = slope * m;
|
||||
if (mask32[tq * KV_TILE_SZ + tk] != -INFINITY) {
|
||||
@@ -9722,7 +9722,7 @@ static void ggml_compute_forward_flash_attn_ext_f16(
|
||||
void ggml_compute_forward_flash_attn_ext(
|
||||
const ggml_compute_params * params,
|
||||
ggml_tensor * dst) {
|
||||
GGML_ASSERT(ggml_validate_flash_attn_ext_indexed_bias(dst));
|
||||
GGML_ASSERT(!ggml_flash_attn_ext_has_bias(dst) || ggml_flash_attn_ext_bias_is_indexed(dst));
|
||||
switch (ggml_prec_get_acc(dst)) {
|
||||
case GGML_PREC_UNDEFINED:
|
||||
case GGML_PREC_F32:
|
||||
|
||||
@@ -1047,8 +1047,8 @@ class tensor_traits_common : public tensor_traits_base {
|
||||
return true;
|
||||
}
|
||||
case GGML_OP_FLASH_ATTN_EXT:
|
||||
// K/V row lists and the indexed bias are handled by the generic CPU path
|
||||
if (op->src[5] != nullptr || ggml_flash_attn_ext_has_indexed_bias(op)) {
|
||||
// K/V row lists and bias are handled by the generic CPU path
|
||||
if (op->src[5] != nullptr || ggml_flash_attn_ext_has_bias(op)) {
|
||||
return false;
|
||||
}
|
||||
forward_flash_attn_ext_f16(params, op);
|
||||
|
||||
@@ -20,20 +20,20 @@
|
||||
// The macro on the following line shifts it by a factor of 2**3=8, as was needed to fix https://github.com/ggml-org/llama.cpp/issues/18606 .
|
||||
#define FATTN_KQ_MAX_OFFSET (3.0f*0.6931f)
|
||||
|
||||
struct fattn_indexed_bias {
|
||||
const float * table;
|
||||
const int32_t * indices;
|
||||
struct fattn_bias_indexed {
|
||||
const float * bias;
|
||||
const int32_t * bias_idxs;
|
||||
int64_t n_bins;
|
||||
int64_t n_keys;
|
||||
int64_t query_stride;
|
||||
|
||||
__device__ fattn_indexed_bias for_head(int64_t first_query, int head) const {
|
||||
return {table + first_query*query_stride + head*n_bins, indices + first_query*n_keys, n_bins, n_keys, query_stride};
|
||||
__device__ fattn_bias_indexed for_head(int64_t first_query, int head) const {
|
||||
return {bias + first_query*query_stride + head*n_bins, bias_idxs + first_query*n_keys, n_bins, n_keys, query_stride};
|
||||
}
|
||||
|
||||
__device__ float add(float mask, int query, int key) const {
|
||||
const int bin = indices[query*n_keys + key];
|
||||
return bin >= 0 && bin < n_bins ? mask + table[query*query_stride + bin] : mask;
|
||||
const int bin = bias_idxs[query*n_keys + key];
|
||||
return bin >= 0 && bin < n_bins ? mask + bias[query*query_stride + bin] : mask;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -65,7 +65,7 @@ using fattn_kernel_t = void (*)(
|
||||
const int32_t s21, const int32_t s22, const int64_t s23,
|
||||
const int32_t ne31, const int32_t ne32, const int32_t ne33,
|
||||
const int32_t s31, const int64_t s32, const int64_t s33,
|
||||
const fattn_indexed_bias bias);
|
||||
const fattn_bias_indexed bias);
|
||||
|
||||
typedef float (*vec_dot_KQ_t)(
|
||||
const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8 , const void * __restrict__ Q_ds);
|
||||
@@ -1324,9 +1324,9 @@ void launch_fattn(
|
||||
const ggml_tensor * sinks = dst->src[4];
|
||||
const ggml_tensor * kv_rows = dst->src[5];
|
||||
|
||||
const bool has_bias = ggml_flash_attn_ext_has_indexed_bias(dst);
|
||||
const ggml_tensor * bias_table = has_bias ? dst->src[6] : nullptr;
|
||||
const ggml_tensor * bias_indices = has_bias ? dst->src[7] : nullptr;
|
||||
const bool bias_indexed = ggml_flash_attn_ext_bias_is_indexed(dst);
|
||||
const ggml_tensor * bias = bias_indexed ? dst->src[6] : nullptr;
|
||||
const ggml_tensor * bias_idxs = bias_indexed ? dst->src[7] : nullptr;
|
||||
|
||||
// with kv_rows the kernel iterates over the mask columns and reads K/V rows through kv_rows
|
||||
const int64_t n_kv_cols = kv_rows ? kv_rows->ne[0] : K->ne[1];
|
||||
@@ -1461,7 +1461,7 @@ void launch_fattn(
|
||||
// Only worth the overhead if there is at lease one FATTN_KQ_STRIDE x FATTN_KQ_STRIDE square to be skipped or
|
||||
// multiple sequences of possibly different lengths, or kv_rows lists that end with padding.
|
||||
// The scan reads ncols1 mask rows per tile, including the last tile.
|
||||
const bool scan_mask = !has_bias && !use_sparse && mask && mask->ne[2] == 1 && mask->ne[1]/ncols1 >= ntiles_x &&
|
||||
const bool scan_mask = !bias_indexed && !use_sparse && mask && mask->ne[2] == 1 && mask->ne[1]/ncols1 >= ntiles_x &&
|
||||
n_kv_cols % FATTN_KQ_STRIDE == 0 && (Q->ne[1] >= 1024 || Q->ne[3] > 1 || kv_rows);
|
||||
if (scan_mask) {
|
||||
const int64_t s31 = mask->nb[1] / sizeof(half2);
|
||||
@@ -1621,11 +1621,11 @@ void launch_fattn(
|
||||
mask ? int32_t(mask->nb[1] / sizeof(half)) : 0,
|
||||
mask ? int64_t(mask->nb[2] / sizeof(half)) : 0,
|
||||
mask ? int64_t(mask->nb[3] / sizeof(half)) : 0,
|
||||
fattn_indexed_bias{has_bias ? (const float *) bias_table->data : nullptr,
|
||||
has_bias ? (const int32_t *) bias_indices->data : nullptr,
|
||||
has_bias ? bias_table->ne[0] : 0,
|
||||
has_bias ? bias_indices->ne[0] : 0,
|
||||
has_bias ? bias_table->ne[0]*bias_table->ne[1] : 0}
|
||||
fattn_bias_indexed{bias_indexed ? (const float *) bias->data : nullptr,
|
||||
bias_indexed ? (const int32_t *) bias_idxs->data : nullptr,
|
||||
bias_indexed ? bias->ne[0] : 0,
|
||||
bias_indexed ? bias_idxs->ne[0] : 0,
|
||||
bias_indexed ? bias->ne[0]*bias->ne[1] : 0}
|
||||
);
|
||||
CUDA_CHECK(cudaGetLastError());
|
||||
|
||||
|
||||
@@ -1866,7 +1866,7 @@ static __global__ void flash_attn_ext_f16(
|
||||
const int32_t s21, const int32_t s22, const int64_t s23,
|
||||
const int32_t ne31, const int32_t ne32, const int32_t ne33,
|
||||
const int32_t s31, const int64_t s32, const int64_t s33,
|
||||
const fattn_indexed_bias bias) {
|
||||
const fattn_bias_indexed bias) {
|
||||
GGML_UNUSED(bias); // biased ops are never dispatched to the mma kernel
|
||||
ggml_cuda_pdl_sync(); // TODO optimize placement
|
||||
#if defined(FLASH_ATTN_AVAILABLE) && (defined(VOLTA_MMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE))
|
||||
|
||||
@@ -387,7 +387,7 @@ bool ggml_cuda_flash_attn_ext_tile_shall_use_sparse(const int cc, const ggml_ten
|
||||
|
||||
// Function that performs a single iteration in for the KQ matrix multiplication:
|
||||
template <int warp_size, int nwarps, int ncols1, int ncols2, int DKQ, int nbatch_fa, int nbatch_K,
|
||||
bool use_logit_softcap, bool oob_check, bool indexed_bias, bool use_sparse, typename T_vec_dot, typename T_K>
|
||||
bool use_logit_softcap, bool oob_check, bool bias_indexed, bool use_sparse, typename T_vec_dot, typename T_K>
|
||||
static __device__ __forceinline__ void flash_attn_tile_iter_KQ(
|
||||
T_vec_dot * const Q_tmp,
|
||||
const T_K * const __restrict__ K_h2,
|
||||
@@ -406,7 +406,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter_KQ(
|
||||
constexpr int np = nwarps > ncols ? nwarps/ncols : 1; // number of parallel warps per Q column
|
||||
|
||||
constexpr int stride_tile_K = std::is_same_v<T_vec_dot, half2> ? nbatch_K/2 + cpy_ne : nbatch_K + cpy_ne;
|
||||
flash_attn_load_tile<warp_size, nwarps, nbatch_fa, nbatch_K, stride_tile_K, false, oob_check, use_sparse, indexed_bias || use_sparse, false, T_K, T_vec_dot>
|
||||
flash_attn_load_tile<warp_size, nwarps, nbatch_fa, nbatch_K, stride_tile_K, false, oob_check, use_sparse, bias_indexed || use_sparse, false, T_K, T_vec_dot>
|
||||
(K_h2 + k_KQ_0/2, KV_tmp, nbatch_K/2, stride_K2, k_VKQ_0, k_VKQ_sup, rows);
|
||||
__syncthreads();
|
||||
|
||||
@@ -464,7 +464,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter_KQ(
|
||||
|
||||
// Function that performs a single iteration of the main loop over up to nbatch_fa tokens.
|
||||
template <int warp_size, int nwarps, int ncols1, int ncols2, int DKQ, int DV, int nbatch_fa, int nbatch_K,
|
||||
bool use_logit_softcap, bool oob_check, bool indexed_bias, bool use_sparse, typename T_vec_dot, typename T_KQ, typename T_acc, typename T_K, typename T_V>
|
||||
bool use_logit_softcap, bool oob_check, bool bias_indexed, bool use_sparse, typename T_vec_dot, typename T_KQ, typename T_acc, typename T_K, typename T_V>
|
||||
static __device__ __forceinline__ void flash_attn_tile_iter(
|
||||
T_vec_dot * const Q_tmp,
|
||||
const T_K * const __restrict__ K_h2,
|
||||
@@ -484,7 +484,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
|
||||
const int k_VKQ_0,
|
||||
const int k_VKQ_max,
|
||||
const int col_Q_0,
|
||||
const fattn_indexed_bias bias,
|
||||
const fattn_bias_indexed bias,
|
||||
const int32_t * const __restrict__ rows) {
|
||||
constexpr int cpy_nb = 16;
|
||||
constexpr int cpy_ne = cpy_nb / 4;
|
||||
@@ -517,12 +517,12 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
|
||||
constexpr int nbatch_K_last = DKQ % nbatch_K;
|
||||
#pragma unroll
|
||||
for (int k_KQ_0 = 0; k_KQ_0 < DKQ - nbatch_K_last; k_KQ_0 += nbatch_K) {
|
||||
flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>(
|
||||
flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, bias_indexed, use_sparse>(
|
||||
Q_tmp, K_h2, KV_tmp, stride_K2, k_VKQ_0, k_VKQ_sup, k_KQ_0, KQ_acc, rows);
|
||||
}
|
||||
if (nbatch_K_last > 0) {
|
||||
constexpr int k_KQ_0 = DKQ - nbatch_K_last;
|
||||
flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K_last, use_logit_softcap, oob_check, indexed_bias, use_sparse>(
|
||||
flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K_last, use_logit_softcap, oob_check, bias_indexed, use_sparse>(
|
||||
Q_tmp, K_h2, KV_tmp, stride_K2, k_VKQ_0, k_VKQ_sup, k_KQ_0, KQ_acc, rows);
|
||||
}
|
||||
|
||||
@@ -546,11 +546,11 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
|
||||
}
|
||||
|
||||
if (!oob_check || i_KQ < k_VKQ_sup) {
|
||||
if (indexed_bias || ncols2 > 1 || mask) {
|
||||
if (bias_indexed || ncols2 > 1 || mask) {
|
||||
const int k = use_sparse ? rows[k_VKQ_0 + i_KQ] : k_VKQ_0 + i_KQ;
|
||||
__builtin_assume(k >= 0); // avoid sign extension and register spills on RDNA
|
||||
const float m = __half2float(mask[int64_t(j)*stride_mask + k]);
|
||||
KQ_acc[(i_KQ_0/(np*warp_size))*cpw + jc0] += slope*(indexed_bias ? bias.add(m, j, k) : m);
|
||||
KQ_acc[(i_KQ_0/(np*warp_size))*cpw + jc0] += slope*(bias_indexed ? bias.add(m, j, k) : m);
|
||||
}
|
||||
|
||||
KQ_max_new[jc0] = fmaxf(KQ_max_new[jc0], KQ_acc[(i_KQ_0/(np*warp_size))*cpw + jc0] + FATTN_KQ_MAX_OFFSET);
|
||||
@@ -633,7 +633,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
|
||||
constexpr int stride_tile_V = std::is_same_v<T_vec_dot, half2> ? DV/2 : DV;
|
||||
#pragma unroll
|
||||
for (int k0 = 0; k0 < nbatch_fa; k0 += nbatch_V) {
|
||||
flash_attn_load_tile<warp_size, nwarps, nbatch_V, DV, stride_tile_V, false, oob_check, use_sparse, indexed_bias || use_sparse, false, T_V, T_vec_dot>
|
||||
flash_attn_load_tile<warp_size, nwarps, nbatch_V, DV, stride_tile_V, false, oob_check, use_sparse, bias_indexed || use_sparse, false, T_V, T_vec_dot>
|
||||
(V_h2, KV_tmp, DV/2, stride_V2, k_VKQ_0 + k0, k_VKQ_sup - k0, rows);
|
||||
__syncthreads();
|
||||
|
||||
@@ -703,7 +703,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
|
||||
}
|
||||
}
|
||||
|
||||
template<int DKQ, int DV, int ncols1, int ncols2, bool use_logit_softcap, bool indexed_bias, ggml_type type_K, ggml_type type_V, bool use_kv_rows, bool use_sparse = false> // D == head size
|
||||
template<int DKQ, int DV, int ncols1, int ncols2, bool use_logit_softcap, bool bias_indexed, ggml_type type_K, ggml_type type_V, bool use_kv_rows, bool use_sparse = false> // D == head size
|
||||
__launch_bounds__(ggml_cuda_fattn_tile_get_nthreads(DKQ, DV, ncols1*ncols2), ggml_cuda_fattn_tile_get_occupancy(DKQ, DV, ncols1*ncols2))
|
||||
static __global__ void flash_attn_tile(
|
||||
const float2 * Q_ptr,
|
||||
@@ -728,7 +728,7 @@ static __global__ void flash_attn_tile(
|
||||
const int32_t s21, const int32_t s22, const int64_t s23,
|
||||
const int32_t ne31, const int32_t ne32, const int32_t ne33,
|
||||
const int32_t s31, const int64_t s32, const int64_t s33,
|
||||
const fattn_indexed_bias bias) {
|
||||
const fattn_bias_indexed bias) {
|
||||
#ifdef FLASH_ATTN_AVAILABLE
|
||||
using T_K = decltype(ggml_cuda_fattn_kv_dtype<type_K>());
|
||||
using T_V = decltype(ggml_cuda_fattn_kv_dtype<type_V>());
|
||||
@@ -751,7 +751,7 @@ static __global__ void flash_attn_tile(
|
||||
}
|
||||
#endif // GCN
|
||||
|
||||
static_assert(!use_sparse || (ncols1 == 1 && !indexed_bias && !use_kv_rows), "bad sparse configuration");
|
||||
static_assert(!use_sparse || (ncols1 == 1 && !bias_indexed && !use_kv_rows), "bad sparse configuration");
|
||||
|
||||
if ((use_logit_softcap && !(DV == 128 || DV == 256 || DV == 512))) {
|
||||
GGML_UNUSED_VARS(Q, K, V, mask, sinks, KV_max, dst, dst_meta, scale,
|
||||
@@ -793,13 +793,13 @@ static __global__ void flash_attn_tile(
|
||||
const int32_t * rows = use_sparse ? KV_max + list*ne11 :
|
||||
use_kv_rows ? KV_rows_ptr + int64_t(sequence)*ne11 : nullptr;
|
||||
__builtin_assume(!use_kv_rows || rows != nullptr);
|
||||
const fattn_indexed_bias head_bias = indexed_bias ? bias.for_head(int64_t(sequence)*ne01.z, head0) : bias;
|
||||
const fattn_bias_indexed head_bias = bias_indexed ? bias.for_head(int64_t(sequence)*ne01.z, head0) : bias;
|
||||
const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix.
|
||||
const float2 * Q_f2 = Q + s03*sequence + s02* head0;
|
||||
const T_K * K_h2 = K + s13*sequence + s12*(head0 / gqa_ratio);
|
||||
const T_V * V_h2 = V + s23*sequence + s22*(head0 / gqa_ratio); // K and V have same shape
|
||||
|
||||
const half * maskh = indexed_bias || mask ? mask + s33*(sequence % ne33) + s32*(head0 % ne32) : nullptr;
|
||||
const half * maskh = bias_indexed || mask ? mask + s33*(sequence % ne33) + s32*(head0 % ne32) : nullptr;
|
||||
|
||||
const int stride_K2 = s11;
|
||||
const int stride_V2 = s21;
|
||||
@@ -899,14 +899,14 @@ static __global__ void flash_attn_tile(
|
||||
int k_VKQ_0 = blockIdx.y*nbatch_fa;
|
||||
while (k_VKQ_0 <= k_VKQ_max - nbatch_fa) {
|
||||
constexpr bool oob_check = false;
|
||||
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>
|
||||
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, bias_indexed, use_sparse>
|
||||
(Q_tmp, K_h2, V_h2, maskh, ne01, logit_softcap, slope, KQ, KV_tmp,
|
||||
stride_K2, stride_V2, stride_mask, KQ_max, KQ_sum, VKQ, k_VKQ_0, k_VKQ_max, col_Q_0, head_bias, rows);
|
||||
k_VKQ_0 += gridDim.y*nbatch_fa;
|
||||
}
|
||||
if (k_VKQ_0 < k_VKQ_max) {
|
||||
constexpr bool oob_check = true;
|
||||
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>
|
||||
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, bias_indexed, use_sparse>
|
||||
(Q_tmp, K_h2, V_h2, maskh, ne01, logit_softcap, slope, KQ, KV_tmp,
|
||||
stride_K2, stride_V2, stride_mask, KQ_max, KQ_sum, VKQ, k_VKQ_0, k_VKQ_max, col_Q_0, head_bias, rows);
|
||||
}
|
||||
@@ -914,7 +914,7 @@ static __global__ void flash_attn_tile(
|
||||
// Branch without out-of-bounds checks.
|
||||
for (int k_VKQ_0 = blockIdx.y*nbatch_fa; k_VKQ_0 < k_VKQ_max; k_VKQ_0 += gridDim.y*nbatch_fa) {
|
||||
constexpr bool oob_check = false;
|
||||
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>
|
||||
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, bias_indexed, use_sparse>
|
||||
(Q_tmp, K_h2, V_h2, maskh, ne01, logit_softcap, slope, KQ, KV_tmp,
|
||||
stride_K2, stride_V2, stride_mask, KQ_max, KQ_sum, VKQ, k_VKQ_0, k_VKQ_max, col_Q_0, head_bias, rows);
|
||||
}
|
||||
@@ -1088,7 +1088,7 @@ static __global__ void flash_attn_tile(
|
||||
#endif // FLASH_ATTN_AVAILABLE
|
||||
}
|
||||
|
||||
template <int DKQ, int DV, int ncols2, bool use_logit_softcap, ggml_type type_K, ggml_type type_V, bool indexed_bias, bool use_kv_rows>
|
||||
template <int DKQ, int DV, int ncols2, bool use_logit_softcap, ggml_type type_K, ggml_type type_V, bool bias_indexed, bool use_kv_rows>
|
||||
static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
||||
using T_K = decltype(ggml_cuda_fattn_kv_dtype<type_K>());
|
||||
using T_V = decltype(ggml_cuda_fattn_kv_dtype<type_V>());
|
||||
@@ -1102,7 +1102,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
|
||||
constexpr size_t nbytes_shared = 0;
|
||||
|
||||
#ifdef GGML_USE_HIP
|
||||
if constexpr (!indexed_bias && !use_kv_rows && ggml_cuda_flash_attn_ext_tile_may_use_sparse(DKQ, DV, ncols2, use_logit_softcap)) {
|
||||
if constexpr (!bias_indexed && !use_kv_rows && ggml_cuda_flash_attn_ext_tile_may_use_sparse(DKQ, DV, ncols2, use_logit_softcap)) {
|
||||
if (ggml_cuda_flash_attn_ext_tile_shall_use_sparse(cc, dst, DKQ, DV)) {
|
||||
const int nwarps = ggml_cuda_fattn_tile_get_nthreads(DKQ, DV, ncols2, cc) / warp_size;
|
||||
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, ncols2, cc);
|
||||
@@ -1115,13 +1115,13 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
|
||||
|
||||
// Limit indexed bias tile widths to avoid spilling registers on AMD.
|
||||
// TODO: select the tile width in one function.
|
||||
constexpr int max_cols = !indexed_bias ? 64 : DKQ == 64 ? 32 : use_kv_rows ? 16 : 8;
|
||||
constexpr int max_cols = !bias_indexed ? 64 : DKQ == 64 ? 32 : use_kv_rows ? 16 : 8;
|
||||
if constexpr (DKQ <= 128 && max_cols >= 64) {
|
||||
if (!(amd_mfma_available(cc) && ncols2 == 1) && Q->ne[1] > 32/ncols2) {
|
||||
constexpr int cols_per_block = 64;
|
||||
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
|
||||
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
|
||||
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
|
||||
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
|
||||
return;
|
||||
@@ -1136,7 +1136,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
|
||||
constexpr int cols_per_block = 32;
|
||||
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
|
||||
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
|
||||
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
|
||||
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
|
||||
return;
|
||||
@@ -1148,7 +1148,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
|
||||
constexpr int cols_per_block = 16;
|
||||
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
|
||||
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
|
||||
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
|
||||
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
|
||||
return;
|
||||
@@ -1160,7 +1160,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
|
||||
constexpr int cols_per_block = 8;
|
||||
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
|
||||
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
|
||||
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
|
||||
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
|
||||
return;
|
||||
@@ -1172,7 +1172,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
|
||||
constexpr int cols_per_block = 4;
|
||||
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
|
||||
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
|
||||
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
|
||||
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
|
||||
return;
|
||||
@@ -1183,7 +1183,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
|
||||
constexpr int cols_per_block = 2;
|
||||
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
|
||||
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
|
||||
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
|
||||
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
|
||||
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
|
||||
return;
|
||||
@@ -1213,7 +1213,7 @@ static void launch_fattn_tile_switch_ncols2(ggml_backend_cuda_context & ctx, ggm
|
||||
const bool use_gqa_opt = mask && mask->ne[2] == 1 && max_bias == 0.0f && Q->ne[1] <= gqa_limit && K->ne[1] % FATTN_KQ_STRIDE == 0;
|
||||
|
||||
if constexpr (DKQ == 64 || DKQ == 128) {
|
||||
if (ggml_flash_attn_ext_has_indexed_bias(dst)) {
|
||||
if (ggml_flash_attn_ext_bias_is_indexed(dst)) {
|
||||
if (dst->src[5]) {
|
||||
launch_fattn_tile_switch_ncols1<DKQ, DV, 1, use_logit_softcap, type_K, type_V, true, true>(ctx, dst);
|
||||
} else {
|
||||
|
||||
@@ -16,7 +16,7 @@ static constexpr __device__ int ggml_cuda_fattn_vec_get_nthreads_device() {
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wpass-failed"
|
||||
#endif // __clang__
|
||||
template<int D, int ncols, ggml_type type_K, ggml_type type_V, bool use_logit_softcap, bool indexed_bias, bool use_kv_rows> // D == head size
|
||||
template<int D, int ncols, ggml_type type_K, ggml_type type_V, bool use_logit_softcap, bool bias_indexed, bool use_kv_rows> // D == head size
|
||||
__launch_bounds__(ggml_cuda_fattn_vec_get_nthreads_device(), 1)
|
||||
static __global__ void flash_attn_ext_vec(
|
||||
const float2 * Q_ptr,
|
||||
@@ -41,7 +41,7 @@ static __global__ void flash_attn_ext_vec(
|
||||
const int32_t s21, const int32_t s22, const int64_t s23,
|
||||
const int32_t ne31, const int32_t ne32, const int32_t ne33,
|
||||
const int32_t s31, const int64_t s32, const int64_t s33,
|
||||
const fattn_indexed_bias bias) {
|
||||
const fattn_bias_indexed bias) {
|
||||
ggml_cuda_pdl_lc();
|
||||
#ifdef FLASH_ATTN_AVAILABLE
|
||||
const float2 * GGML_CUDA_RESTRICT Q = Q_ptr;
|
||||
@@ -75,7 +75,7 @@ static __global__ void flash_attn_ext_vec(
|
||||
|
||||
#ifdef GGML_USE_HIP
|
||||
#ifdef RDNA
|
||||
constexpr int nthreads_KQ_q = indexed_bias && type_K == GGML_TYPE_F8_E4M3 ? 16 : 2;
|
||||
constexpr int nthreads_KQ_q = bias_indexed && type_K == GGML_TYPE_F8_E4M3 ? 16 : 2;
|
||||
#else
|
||||
constexpr int nthreads_KQ_q = type_K == GGML_TYPE_F8_E4M3 && D == 256 && ncols == 2 ? 16 : 4;
|
||||
#endif // RDNA
|
||||
@@ -107,13 +107,13 @@ static __global__ void flash_attn_ext_vec(
|
||||
|
||||
const int sequence = blockIdx.z / ne02;
|
||||
const int head = blockIdx.z - sequence*ne02;
|
||||
const fattn_indexed_bias head_bias = indexed_bias ? bias.for_head(int64_t(sequence)*ne01.z, head) : bias;
|
||||
const fattn_bias_indexed head_bias = bias_indexed ? bias.for_head(int64_t(sequence)*ne01.z, head) : bias;
|
||||
const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix.
|
||||
Q += s03*sequence + s02* head + s01*ic0;
|
||||
K += s13*sequence + s12*(head / gqa_ratio);
|
||||
V += s23*sequence + s22*(head / gqa_ratio);
|
||||
|
||||
const half * maskh = indexed_bias || mask ? mask + s33*(sequence % ne33) + s32*(head % ne32) + s31*ic0 : nullptr;
|
||||
const half * maskh = bias_indexed || mask ? mask + s33*(sequence % ne33) + s32*(head % ne32) + s31*ic0 : nullptr;
|
||||
|
||||
const float slope = get_alibi_slope(max_bias, head, n_head_log2, m0, m1);
|
||||
|
||||
@@ -305,7 +305,7 @@ static __global__ void flash_attn_ext_vec(
|
||||
sum = logit_softcap*tanhf(sum);
|
||||
}
|
||||
|
||||
if constexpr (!indexed_bias) {
|
||||
if constexpr (!bias_indexed) {
|
||||
if (mask && (ncols == 1 || ic0 + j < int(ne01.z))) {
|
||||
sum += slope*__half2float(maskh[j*ne11 + i_KQ]);
|
||||
}
|
||||
@@ -321,14 +321,14 @@ static __global__ void flash_attn_ext_vec(
|
||||
|
||||
#pragma unroll
|
||||
for (int j = 0; j < ncols; ++j) {
|
||||
if constexpr (indexed_bias) {
|
||||
if constexpr (bias_indexed) {
|
||||
const int k = k_VKQ_0 + tid;
|
||||
const float m = head_bias.add(__half2float(maskh[j*ne11 + tid]), ic0 + j, k);
|
||||
KQ_reg[j] += slope*m;
|
||||
KQ_max_new[j] = fmaxf(KQ_max[j], KQ_reg[j] + FATTN_KQ_MAX_OFFSET);
|
||||
}
|
||||
#pragma unroll
|
||||
for (int offset = indexed_bias ? 1 : nthreads_KQ; offset < WARP_SIZE; offset <<= 1) {
|
||||
for (int offset = bias_indexed ? 1 : nthreads_KQ; offset < WARP_SIZE; offset <<= 1) {
|
||||
KQ_max_new[j] = fmaxf(KQ_max_new[j], ggml_cuda_shfl_xor(KQ_max_new[j], offset, WARP_SIZE));
|
||||
}
|
||||
const float KQ_max_scale = expf(KQ_max[j] - KQ_max_new[j]);
|
||||
@@ -575,7 +575,7 @@ void ggml_cuda_flash_attn_ext_vec_case_impl(ggml_backend_cuda_context & ctx, ggm
|
||||
const int nwarps = nthreads / WARP_SIZE;
|
||||
fattn_kernel_t fattn_kernel = flash_attn_ext_vec<D, cols_per_block, type_K, type_V, use_logit_softcap, false, false>;
|
||||
if constexpr (cols_per_block == 1 && (D == 64 || D == 128) && !use_logit_softcap) {
|
||||
if (ggml_flash_attn_ext_has_indexed_bias(dst)) {
|
||||
if (ggml_flash_attn_ext_bias_is_indexed(dst)) {
|
||||
fattn_kernel = dst->src[5] ? flash_attn_ext_vec<D, cols_per_block, type_K, type_V, use_logit_softcap, true, true>
|
||||
: flash_attn_ext_vec<D, cols_per_block, type_K, type_V, use_logit_softcap, true, false>;
|
||||
}
|
||||
|
||||
@@ -654,7 +654,7 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
|
||||
const ggml_tensor * mask = dst->src[3];
|
||||
const ggml_tensor * kv_rows = dst->src[5];
|
||||
|
||||
if (!ggml_validate_flash_attn_ext_indexed_bias(dst)) {
|
||||
if (ggml_flash_attn_ext_has_bias(dst) && !ggml_flash_attn_ext_bias_is_indexed(dst)) {
|
||||
return BEST_FATTN_KERNEL_NONE;
|
||||
}
|
||||
|
||||
@@ -739,7 +739,7 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
|
||||
}
|
||||
|
||||
// the tile and vec kernels implement the indexed bias, the mma kernel does not
|
||||
if (ggml_flash_attn_ext_has_indexed_bias(dst)) {
|
||||
if (ggml_flash_attn_ext_bias_is_indexed(dst)) {
|
||||
float logit_softcap;
|
||||
memcpy(&logit_softcap, (const float *) KQV->op_params + 2, sizeof(float));
|
||||
if (Q->ne[0] != 128 && !(Q->ne[0] == 64 && logit_softcap == 0.0f)) {
|
||||
|
||||
@@ -33,6 +33,27 @@ static __device__ __forceinline__ float reduce_row_f32(const float * x, const in
|
||||
return sum;
|
||||
}
|
||||
|
||||
#define CUDA_REDUCE_ROWS_SHORT_NCOLS 8
|
||||
|
||||
template <bool norm>
|
||||
static __global__ void reduce_rows_f32_short(const float * x_ptr, float * dst_ptr, const int ncols, const int64_t nrows) {
|
||||
const int64_t row = (int64_t) blockIdx.x*blockDim.x + threadIdx.x;
|
||||
|
||||
ggml_cuda_pdl_sync();
|
||||
if (row >= nrows) {
|
||||
return;
|
||||
}
|
||||
|
||||
const float * GGML_CUDA_RESTRICT x = x_ptr + row*ncols;
|
||||
|
||||
float sum = 0.0f;
|
||||
for (int i = 0; i < ncols; ++i) {
|
||||
sum += x[i];
|
||||
}
|
||||
|
||||
dst_ptr[row] = norm ? sum / ncols : sum;
|
||||
}
|
||||
|
||||
// Row reduction kernel template - compute sum (norm=false) or mean (norm=true)
|
||||
template <bool norm>
|
||||
static __global__ void reduce_rows_f32(const float * x_ptr, float * dst_ptr, const int ncols) {
|
||||
|
||||
@@ -142,6 +142,64 @@ static __global__ void soft_max_f32(
|
||||
}
|
||||
}
|
||||
|
||||
#define CUDA_SOFT_MAX_SHORT_NCOLS 8
|
||||
|
||||
template <typename T>
|
||||
static __global__ void soft_max_f32_short(
|
||||
const float * x, const T * mask, const float * sinks, float * dst, const soft_max_params p,
|
||||
const uint3 ne01_fdv, const uint3 ne02_fdv, const uint3 ne12_fdv, const uint3 ne13_fdv) {
|
||||
const uint32_t rowx = blockIdx.x*blockDim.x + threadIdx.x;
|
||||
if (rowx >= p.nrows_x) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint32_t i012 = fastdiv(rowx, ne01_fdv);
|
||||
const uint32_t i01 = rowx - i012*ne01_fdv.z;
|
||||
const uint32_t i03 = fastdiv(i012, ne02_fdv);
|
||||
const uint32_t i02 = i012 - i03*ne02_fdv.z;
|
||||
|
||||
const uint32_t i12 = fastmodulo(i02, ne12_fdv);
|
||||
const uint32_t i13 = fastmodulo(i03, ne13_fdv);
|
||||
|
||||
const int ncols = p.ncols;
|
||||
|
||||
x += (int64_t) rowx*ncols;
|
||||
mask += (i01*p.nb11 + i12*p.nb12 + i13*p.nb13) / sizeof(T) * (mask != nullptr);
|
||||
dst += (int64_t) rowx*ncols;
|
||||
|
||||
const float slope = get_alibi_slope(p.max_bias, i02, p.n_head_log2, p.m0, p.m1);
|
||||
|
||||
float vals[CUDA_SOFT_MAX_SHORT_NCOLS];
|
||||
float max_val = sinks ? sinks[i02] : -INFINITY;
|
||||
|
||||
#pragma unroll
|
||||
for (int col = 0; col < CUDA_SOFT_MAX_SHORT_NCOLS; ++col) {
|
||||
if (col < ncols) {
|
||||
vals[col] = x[col]*p.scale + (mask ? slope*t2f32(mask[col]) : 0.0f);
|
||||
max_val = max(max_val, vals[col]);
|
||||
}
|
||||
}
|
||||
|
||||
float sum = sinks ? expf(sinks[i02] - max_val) : 0.0f;
|
||||
|
||||
#pragma unroll
|
||||
for (int col = 0; col < CUDA_SOFT_MAX_SHORT_NCOLS; ++col) {
|
||||
if (col < ncols) {
|
||||
vals[col] = expf(vals[col] - max_val);
|
||||
sum += vals[col];
|
||||
}
|
||||
}
|
||||
|
||||
const float inv_sum = 1.0f / sum;
|
||||
|
||||
#pragma unroll
|
||||
for (int col = 0; col < CUDA_SOFT_MAX_SHORT_NCOLS; ++col) {
|
||||
if (col < ncols) {
|
||||
dst[col] = vals[col]*inv_sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <int n_elem_per_thread = 0>
|
||||
static __device__ void soft_max_f32_parallelize_cols_single_row(const float * __restrict__ x,
|
||||
float * __restrict__ dst,
|
||||
@@ -392,6 +450,15 @@ static void soft_max_f32_cuda(const float * x,
|
||||
int nth = WARP_SIZE;
|
||||
const int64_t ncols_x = params.ncols;
|
||||
|
||||
if (ncols_x <= CUDA_SOFT_MAX_SHORT_NCOLS && params.nrows_x <= INT_MAX) {
|
||||
const uint3 ne01_fdv = init_fastdiv_values(params.ne01);
|
||||
const uint3 ne02_fdv = init_fastdiv_values(params.ne02);
|
||||
const uint3 ne12_fdv = init_fastdiv_values(params.ne12);
|
||||
const uint3 ne13_fdv = init_fastdiv_values(params.ne13);
|
||||
soft_max_f32_short<<<(params.nrows_x + 255) / 256, 256, 0, stream>>>(x, mask, sinks, dst, params, ne01_fdv, ne02_fdv, ne12_fdv, ne13_fdv);
|
||||
return;
|
||||
}
|
||||
|
||||
while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
|
||||
const dim3 block_dims(nth, 1, 1);
|
||||
const dim3 block_nums(params.ne01, params.ne02, params.ne03);
|
||||
|
||||
@@ -4,6 +4,13 @@
|
||||
void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
||||
const int id = ggml_cuda_get_device();
|
||||
const int nsm = ggml_cuda_info().devices[id].nsm;
|
||||
if (ncols <= CUDA_REDUCE_ROWS_SHORT_NCOLS) {
|
||||
const dim3 block_dims(256, 1, 1);
|
||||
const dim3 block_nums((nrows + 255) / 256, 1, 1);
|
||||
const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
|
||||
ggml_cuda_kernel_launch(reduce_rows_f32_short</*norm=*/false>, launch_params, x, dst, ncols, (int64_t) nrows);
|
||||
return;
|
||||
}
|
||||
const dim3 block_nums(nrows, 1, 1);
|
||||
if ((nrows / nsm) < 2) {
|
||||
const dim3 block_dims(512, 1, 1);
|
||||
|
||||
@@ -1281,7 +1281,7 @@ static bool ggml_backend_et_device_supports_op(ggml_backend_dev_t dev, const ggm
|
||||
if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
|
||||
(op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) && op->src[2] &&
|
||||
(op->src[2]->type == GGML_TYPE_F32 || op->src[2]->type == GGML_TYPE_F16) && op->src[4] == nullptr && op->src[5] == nullptr &&
|
||||
!ggml_flash_attn_ext_has_indexed_bias(op) &&
|
||||
!ggml_flash_attn_ext_has_bias(op) &&
|
||||
ggml_is_contiguous_rows(op) && ggml_is_contiguous_rows(op->src[0])) {
|
||||
float max_bias = 0.0f;
|
||||
float logit_softcap = 0.0f;
|
||||
|
||||
@@ -135,6 +135,7 @@ size_t opt_mbuf_static = 1ul * 1024 * 1024 * 1024; // max static (weight/KV) buf
|
||||
size_t opt_mbuf_total = 0; // total buffer space limit (0 = unconstrained)
|
||||
|
||||
int opt_mm_select = 3; // 3 = W8A8 INT HMX -> FP16 HMX -> HVX -> CPU (default), 2 = FP16 HMX -> HVX -> CPU, 1 = HVX -> CPU, 0 = CPU
|
||||
int opt_mm_fwht = 0; // 1 = enable online block-32 FWHT rotation in W8A8 HMX matmul, 0 = off (default)
|
||||
int opt_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)
|
||||
@@ -365,7 +366,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, bool allow_fuse = true);
|
||||
void enqueue_op(const htp_opnode & node, bool fusable = false);
|
||||
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,
|
||||
@@ -1342,26 +1343,27 @@ struct ggml_hexagon_tensor_map {
|
||||
};
|
||||
|
||||
struct ggml_hexagon_opbatch {
|
||||
ggml_hexagon_session* sess;
|
||||
ggml_hexagon_session* sess;
|
||||
|
||||
std::vector<htp_opnode> ops; // htp_opnode of ops
|
||||
std::vector<htp_buf_desc> h_bufs; // htp buffer descriptors
|
||||
std::vector<htp_tensor> h_tens; // htp tensor descriptors
|
||||
std::vector<htp_op_desc> h_ops; // htp op descriptors
|
||||
std::vector<htp_opnode> ops; // htp_opnode of ops
|
||||
std::vector<htp_buf_desc> h_bufs; // htp buffer descriptors
|
||||
std::vector<htp_tensor> h_tens; // htp tensor descriptors
|
||||
std::vector<htp_op_desc> h_ops; // htp op descriptors
|
||||
|
||||
ggml_hexagon_tensor_map 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; // 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
|
||||
|
||||
unsigned int n_bufs_max;
|
||||
unsigned int n_tens_max;
|
||||
unsigned int n_ops_max;
|
||||
size_t b_vmem_max;
|
||||
|
||||
bool last_op_fusable;
|
||||
|
||||
void reset() {
|
||||
n_bufs = 0;
|
||||
n_tens = 0;
|
||||
@@ -1586,14 +1588,14 @@ struct ggml_hexagon_opbatch {
|
||||
}
|
||||
|
||||
// caller must ensure the op fits
|
||||
void add_op(const htp_opnode & node, bool save_op = true) {
|
||||
void add_op(const htp_opnode & node, bool save_op = true, bool fusable = false) {
|
||||
unsigned int n = n_ops++;
|
||||
GGML_ASSERT(n_ops <= n_ops_max);
|
||||
|
||||
if (save_op) {
|
||||
ops[n] = node;
|
||||
}
|
||||
last_op_fusable = save_op;
|
||||
last_op_fusable = fusable;
|
||||
h_ops[n].flags = 0;
|
||||
|
||||
if (opt_verbose) {
|
||||
@@ -1656,6 +1658,7 @@ struct ggml_hexagon_opbatch {
|
||||
|
||||
htp_lower_opnode(sess, last_node);
|
||||
sync_op_desc(n_ops - 1, last_node);
|
||||
last_op_fusable = false;
|
||||
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;
|
||||
@@ -1970,7 +1973,7 @@ void ggml_hexagon_session::flush_batch(size_t min_ops) {
|
||||
op_batch->reset();
|
||||
}
|
||||
|
||||
void ggml_hexagon_session::enqueue_op(const htp_opnode & node, bool allow_fuse) {
|
||||
void ggml_hexagon_session::enqueue_op(const htp_opnode & node, bool fusable) {
|
||||
auto register_tensor = [&](const ggml_tensor * t) {
|
||||
if (!t) return;
|
||||
auto * sbuf = static_cast<ggml_hexagon_shared_buffer *>(t->buffer->context);
|
||||
@@ -2000,7 +2003,7 @@ void ggml_hexagon_session::enqueue_op(const htp_opnode & node, bool allow_fuse)
|
||||
}
|
||||
|
||||
const bool fits_tensors = op_batch->fit_tensors(node);
|
||||
if (opt_opfusion && allow_fuse && fits_tensors && op_batch->try_fuse(node)) {
|
||||
if (opt_opfusion && fits_tensors && op_batch->try_fuse(node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2015,7 +2018,8 @@ void ggml_hexagon_session::enqueue_op(const htp_opnode & node, bool allow_fuse)
|
||||
}
|
||||
}
|
||||
|
||||
op_batch->add_op(node, opt_profile || (opt_opfusion && allow_fuse));
|
||||
const bool mark_fusable = opt_opfusion && fusable;
|
||||
op_batch->add_op(node, opt_profile || mark_fusable, mark_fusable);
|
||||
}
|
||||
|
||||
void ggml_hexagon_session::enqueue_mdev_group() {
|
||||
@@ -2104,7 +2108,7 @@ void ggml_hexagon_session::enqueue_allreduce(
|
||||
this, dst, rank, n_ranks, false, false, /*is_shard_ok=*/ false,
|
||||
ar_node.kparams<htp_allreduce_kernel_params>());
|
||||
|
||||
this->enqueue_op(ar_node);
|
||||
this->enqueue_op(ar_node, /*fusable=*/ true);
|
||||
}
|
||||
|
||||
void ggml_hexagon_session::mmap_buffers() {
|
||||
@@ -2629,6 +2633,30 @@ static void ggml_hexagon_graph_repack(const ggml_cgraph * graph, ggml_hexagon_se
|
||||
}
|
||||
}
|
||||
|
||||
static void ggml_hexagon_graph_append_opnode(
|
||||
const ggml_hexagon_session * sess,
|
||||
std::vector<htp_opnode> & nodes,
|
||||
htp_opnode && node
|
||||
) {
|
||||
const ggml_tensor * n = node.node;
|
||||
const ggml_tensor * scale = ggml_get_src_scale(n, 0);
|
||||
const bool fused_scale = scale != nullptr &&
|
||||
(node.opcode == HTP_OP_MUL_MAT || node.opcode == HTP_OP_MUL_MAT_ID) &&
|
||||
(node.kparams<htp_mm_kernel_params>()->scale_mask != 0);
|
||||
if (fused_scale) {
|
||||
node.inputs.push_back(scale);
|
||||
}
|
||||
if (nodes.empty() || !opt_opfusion || !htp_fusion_try_fuse(sess, nodes.back(), node)) {
|
||||
nodes.push_back(std::move(node));
|
||||
}
|
||||
|
||||
if (scale && !fused_scale) {
|
||||
htp_opnode scale_node = htp_mm_make_scale_opnode(n, scale);
|
||||
GGML_ASSERT(htp_kparams_solve(sess, scale_node));
|
||||
nodes.push_back(std::move(scale_node));
|
||||
}
|
||||
}
|
||||
|
||||
static ggml_status ggml_backend_hexagon_graph_compute(ggml_backend_t backend, ggml_cgraph * graph) {
|
||||
auto sess = static_cast<ggml_hexagon_session *>(backend->context);
|
||||
|
||||
@@ -2662,9 +2690,7 @@ 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));
|
||||
if (nodes.empty() || !opt_opfusion || !htp_fusion_try_fuse(sess, nodes.back(), node)) {
|
||||
nodes.push_back(std::move(node));
|
||||
}
|
||||
ggml_hexagon_graph_append_opnode(sess, nodes, std::move(node));
|
||||
}
|
||||
|
||||
if (opt_opfusion) {
|
||||
@@ -2677,7 +2703,7 @@ static ggml_status ggml_backend_hexagon_graph_compute(ggml_backend_t backend, gg
|
||||
}
|
||||
|
||||
for (const auto & node : sess->cached_nodes) {
|
||||
sess->enqueue_op(node, false);
|
||||
sess->enqueue_op(node);
|
||||
}
|
||||
|
||||
if (sess->last_error > HTP_STATUS_OK) {
|
||||
@@ -3233,10 +3259,7 @@ static bool ggml_backend_hexagon_device_supports_op(ggml_backend_dev_t dev, cons
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ggml_get_src_scale(op, 0)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// src_scale(op, 1) is an activation quantization hint for quantized-activation backends, ignored for F32/F16 activations
|
||||
bool supp = false;
|
||||
if (ggml_op_is_empty(op->op) || ggml_is_empty(op)) {
|
||||
supp = true;
|
||||
@@ -3636,6 +3659,8 @@ static void ggml_hexagon_init(ggml_backend_reg * reg) {
|
||||
"please update hexagon_type to match ggml_type");
|
||||
static_assert((unsigned int) HTP_TYPE_Q2_0 == (unsigned int) GGML_TYPE_Q2_0,
|
||||
"please update hexagon_type to match ggml_type");
|
||||
static_assert((unsigned int) HTP_TYPE_F8_E4M3 == (unsigned int) GGML_TYPE_F8_E4M3,
|
||||
"please update hexagon_type to match ggml_type");
|
||||
static_assert((unsigned int) HTP_TYPE_BF16 == (unsigned int) GGML_TYPE_BF16,
|
||||
"please update hexagon_type to match ggml_type");
|
||||
|
||||
@@ -3653,6 +3678,7 @@ static void ggml_hexagon_init(ggml_backend_reg * reg) {
|
||||
const char * str_nhmx = getenv("GGML_HEXAGON_NHMX");
|
||||
const char * str_vtcm_size = getenv("GGML_HEXAGON_VTCM_SIZE");
|
||||
const char * str_mm_select = getenv("GGML_HEXAGON_MM_SELECT");
|
||||
const char * str_mm_fwht = getenv("GGML_HEXAGON_MM_FWHT");
|
||||
const char * str_fa_select = getenv("GGML_HEXAGON_FA_SELECT");
|
||||
const char * str_fa_split = getenv("GGML_HEXAGON_FA_SPLIT");
|
||||
const char * str_li_select = getenv("GGML_HEXAGON_LI_SELECT");
|
||||
@@ -3727,6 +3753,7 @@ static void ggml_hexagon_init(ggml_backend_reg * reg) {
|
||||
opt_nhmx = str_nhmx ? atoi(str_nhmx) : opt_nhmx;
|
||||
opt_vtcm_size = str_vtcm_size ? parse_size(str_vtcm_size) : opt_vtcm_size;
|
||||
opt_mm_select = str_mm_select ? atoi(str_mm_select) : opt_mm_select;
|
||||
opt_mm_fwht = str_mm_fwht ? atoi(str_mm_fwht) : opt_mm_fwht;
|
||||
opt_fa_select = str_fa_select ? atoi(str_fa_select) : opt_fa_select;
|
||||
opt_fa_split = str_fa_split ? atoi(str_fa_split) : opt_fa_split;
|
||||
opt_li_select = str_li_select ? atoi(str_li_select) : opt_li_select;
|
||||
@@ -3954,10 +3981,6 @@ static void ggml_hexagon_init(ggml_backend_reg * reg) {
|
||||
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");
|
||||
|
||||
@@ -21,6 +21,7 @@ extern size_t opt_mbuf_static;
|
||||
extern size_t opt_mbuf_total;
|
||||
|
||||
extern int opt_mm_select;
|
||||
extern int opt_mm_fwht;
|
||||
extern int opt_fa_select;
|
||||
extern int opt_fa_split;
|
||||
extern int opt_li_select;
|
||||
@@ -154,4 +155,38 @@ uint32_t htp_session_mdev_count(const struct ggml_hexagon_session * sess);
|
||||
|
||||
htp_op_code htp_op_remap(const struct ggml_tensor * t);
|
||||
|
||||
enum htp_mm_scale_mode {
|
||||
HTP_MM_SCALE_NONE = 0,
|
||||
HTP_MM_SCALE_PER_TENSOR,
|
||||
HTP_MM_SCALE_PER_CHANNEL,
|
||||
HTP_MM_SCALE_PER_EXPERT,
|
||||
HTP_MM_SCALE_PER_CHANNEL_EXPERT,
|
||||
};
|
||||
|
||||
// classify weight scale (src_scale(op, 0)) of GGML_OP_MUL_MAT / GGML_OP_MUL_MAT_ID
|
||||
static inline enum htp_mm_scale_mode htp_get_mm_scale_mode(const struct ggml_tensor * op) {
|
||||
const struct ggml_tensor * scale = ggml_get_src_scale(op, 0);
|
||||
if (!scale) {
|
||||
return HTP_MM_SCALE_NONE;
|
||||
}
|
||||
if (ggml_is_scalar(scale)) {
|
||||
return HTP_MM_SCALE_PER_TENSOR;
|
||||
}
|
||||
if (op->op == GGML_OP_MUL_MAT_ID) {
|
||||
const int64_t n_out = op->src[0]->ne[1];
|
||||
const int64_t n_expert = op->src[0]->ne[2];
|
||||
if (scale->ne[0] == n_out && scale->ne[1] == n_expert && scale->ne[2] == 1 && scale->ne[3] == 1) {
|
||||
return HTP_MM_SCALE_PER_CHANNEL_EXPERT;
|
||||
}
|
||||
if (ggml_is_vector(scale) && scale->ne[0] == n_expert) {
|
||||
return HTP_MM_SCALE_PER_EXPERT;
|
||||
}
|
||||
GGML_ABORT("invalid MUL_MAT_ID weight scale shape");
|
||||
}
|
||||
if (ggml_is_vector(scale) && scale->ne[0] == op->src[0]->ne[1]) {
|
||||
return HTP_MM_SCALE_PER_CHANNEL;
|
||||
}
|
||||
GGML_ABORT("invalid MUL_MAT weight scale shape");
|
||||
}
|
||||
|
||||
#endif // HTP_COMMON_H
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -21,6 +21,7 @@ enum htp_fusion_flags {
|
||||
HTP_FUSE_GDN_STATE = (1 << 15), // 32768
|
||||
HTP_FUSE_CONCAT_SSM_CONV = (1 << 16), // 65536
|
||||
HTP_FUSE_MUL_MAT_NX_GLU = (1 << 17), // 131072
|
||||
HTP_FUSE_RMS_NORM_MUL_MM = (1 << 18), // 262144
|
||||
};
|
||||
|
||||
static inline bool htp_fusion_enabled(int flag) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -80,90 +80,9 @@ bool htp_kparams_solve_allreduce(
|
||||
struct htp_allreduce_kernel_params * kparams
|
||||
);
|
||||
|
||||
bool htp_kparams_solve_rms_norm_mul(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
const struct ggml_tensor * src0,
|
||||
const struct ggml_tensor * src1,
|
||||
const struct ggml_tensor * dst,
|
||||
struct htp_unary_kernel_params * kparams
|
||||
);
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
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 * bias,
|
||||
const struct ggml_tensor * dst,
|
||||
struct htp_mm_kernel_params * kparams
|
||||
);
|
||||
|
||||
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,
|
||||
const struct ggml_tensor * act,
|
||||
const struct ggml_tensor * dst,
|
||||
int32_t n_weights,
|
||||
struct htp_mm_kernel_params * kparams
|
||||
);
|
||||
|
||||
bool htp_kparams_solve_repeat_get_rows(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
const struct ggml_tensor * src0,
|
||||
const struct ggml_tensor * src1,
|
||||
const struct ggml_tensor * dst,
|
||||
struct htp_get_rows_kernel_params * kparams
|
||||
);
|
||||
|
||||
bool htp_kparams_solve_unary_mul_glu(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
uint32_t op,
|
||||
const struct ggml_tensor * src0,
|
||||
const struct ggml_tensor * src1,
|
||||
const struct ggml_tensor * dst,
|
||||
struct htp_glu_kernel_params * kparams
|
||||
);
|
||||
|
||||
bool htp_kparams_solve_upscale(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
const struct ggml_tensor * src0,
|
||||
const struct ggml_tensor * dst,
|
||||
struct htp_upscale_kernel_params * kparams
|
||||
);
|
||||
|
||||
static inline bool htp_kparams_solve_upscale(
|
||||
const struct ggml_hexagon_session * sess,
|
||||
struct htp_opnode htp_mm_make_scale_opnode(
|
||||
const struct ggml_tensor * op,
|
||||
struct htp_upscale_kernel_params * kparams
|
||||
) {
|
||||
return htp_kparams_solve_upscale(sess, op->src[0], op, kparams);
|
||||
}
|
||||
const struct ggml_tensor * scale
|
||||
);
|
||||
|
||||
#endif // HTP_KPARAMS_H
|
||||
|
||||
@@ -23,7 +23,8 @@ 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) {
|
||||
size_t s1, sd;
|
||||
return (dst->op == GGML_OP_MUL_MAT || dst->op == GGML_OP_ADD) &&
|
||||
return (dst->op == GGML_OP_MUL_MAT || dst->op == GGML_OP_ADD ||
|
||||
dst->op == GGML_OP_GLU || dst->op == GGML_OP_MUL) &&
|
||||
src0->ne[2] == 1 && src0->ne[3] == 1 && src1->ne[2] * src1->ne[3] > 1 &&
|
||||
src1->nb[0] == ggml_type_size(src1->type) && htp_rows_stride(src1->ne, src1->nb, &s1) &&
|
||||
dst->nb[0] == ggml_type_size(dst->type) && htp_rows_stride(dst->ne, dst->nb, &sd);
|
||||
@@ -427,26 +428,16 @@ void htp_lightning_indexer_collapse_tensors(
|
||||
}
|
||||
}
|
||||
|
||||
void htp_lower_opnode(const struct ggml_hexagon_session * sess, htp_opnode & node, struct ggml_tensor * scratch) {
|
||||
const uint32_t n_hvx = htp_session_n_hvx(sess);
|
||||
const size_t n_in = node.inputs.size();
|
||||
const size_t n_out = node.outputs.size();
|
||||
|
||||
ggml_tensor in_mat[HTP_OP_MAX_INPUTS] = {};
|
||||
ggml_tensor out_mat[HTP_OP_MAX_OUTPUTS] = {};
|
||||
bool in_dirty[HTP_OP_MAX_INPUTS] = {};
|
||||
bool out_dirty[HTP_OP_MAX_OUTPUTS] = {};
|
||||
|
||||
for (size_t i = 0; i < n_in; ++i) {
|
||||
if (node.inputs[i]) {
|
||||
in_dirty[i] = ggml_hexagon_tensor_is_lowered_f16(node.inputs[i]);
|
||||
in_mat[i] = htp_tensor_lower_f16(node.inputs[i]);
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i < n_out; ++i) {
|
||||
out_dirty[i] = ggml_hexagon_tensor_is_lowered_f16(node.outputs[i]);
|
||||
out_mat[i] = htp_tensor_lower_f16(node.outputs[i]);
|
||||
}
|
||||
static void htp_collapse_opnode_tensors(
|
||||
uint32_t n_hvx,
|
||||
const htp_opnode & node,
|
||||
ggml_tensor (& in_mat)[HTP_OP_MAX_INPUTS],
|
||||
ggml_tensor (& out_mat)[HTP_OP_MAX_OUTPUTS],
|
||||
bool (& in_dirty)[HTP_OP_MAX_INPUTS],
|
||||
bool (& out_dirty)[HTP_OP_MAX_OUTPUTS]
|
||||
) {
|
||||
const size_t n_in = node.inputs.size();
|
||||
const size_t n_out = node.outputs.size();
|
||||
|
||||
if (htp_op_is_unary(node.opcode) && htp_unary_is_elementwise(node.opcode)) {
|
||||
if (n_in >= 1 && n_out >= 1 &&
|
||||
@@ -497,8 +488,9 @@ void htp_lower_opnode(const struct ggml_hexagon_session * sess, htp_opnode & nod
|
||||
}
|
||||
}
|
||||
} else if (node.opcode == HTP_OP_MUL_MAT_NX) {
|
||||
const size_t n_w = n_out;
|
||||
if (n_w > 0 && n_in == n_w + 1) {
|
||||
const size_t n_w = n_out;
|
||||
const size_t norm_extra = (node.kparams<htp_mm_kernel_params>()->norm_w_idx != 0) ? 1 : 0;
|
||||
if (n_w > 0 && (n_in == n_w + 1 + norm_extra || n_in == 2 * n_w + 1 + norm_extra)) {
|
||||
bool can_collapse = true;
|
||||
for (size_t i = 0; i < n_w; ++i) {
|
||||
if (!htp_matmul_can_collapse(&in_mat[i], &in_mat[n_w], &out_mat[i])) {
|
||||
@@ -516,7 +508,8 @@ void htp_lower_opnode(const struct ggml_hexagon_session * sess, htp_opnode & nod
|
||||
}
|
||||
}
|
||||
} else if (node.opcode == HTP_OP_MUL_MAT_NX_GLU) {
|
||||
if (n_in == 3 && n_out == 1 &&
|
||||
const size_t norm_extra = (node.kparams<htp_mm_kernel_params>()->norm_w_idx != 0) ? 1 : 0;
|
||||
if ((n_in == 3 + norm_extra || n_in == 5 + norm_extra) && n_out == 1 &&
|
||||
htp_matmul_can_collapse(&in_mat[0], &in_mat[2], &out_mat[0]) &&
|
||||
htp_matmul_can_collapse(&in_mat[1], &in_mat[2], &out_mat[0])) {
|
||||
in_mat[2] = htp_tensor_collapse_rows(&in_mat[2]);
|
||||
@@ -591,6 +584,30 @@ void htp_lower_opnode(const struct ggml_hexagon_session * sess, htp_opnode & nod
|
||||
out_dirty[0] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void htp_lower_opnode(const struct ggml_hexagon_session * sess, htp_opnode & node, struct ggml_tensor * scratch) {
|
||||
const uint32_t n_hvx = htp_session_n_hvx(sess);
|
||||
const size_t n_in = node.inputs.size();
|
||||
const size_t n_out = node.outputs.size();
|
||||
|
||||
ggml_tensor in_mat[HTP_OP_MAX_INPUTS] = {};
|
||||
ggml_tensor out_mat[HTP_OP_MAX_OUTPUTS] = {};
|
||||
bool in_dirty[HTP_OP_MAX_INPUTS] = {};
|
||||
bool out_dirty[HTP_OP_MAX_OUTPUTS] = {};
|
||||
|
||||
for (size_t i = 0; i < n_in; ++i) {
|
||||
if (node.inputs[i]) {
|
||||
in_dirty[i] = ggml_hexagon_tensor_is_lowered_f16(node.inputs[i]);
|
||||
in_mat[i] = htp_tensor_lower_f16(node.inputs[i]);
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i < n_out; ++i) {
|
||||
out_dirty[i] = ggml_hexagon_tensor_is_lowered_f16(node.outputs[i]);
|
||||
out_mat[i] = htp_tensor_lower_f16(node.outputs[i]);
|
||||
}
|
||||
|
||||
htp_collapse_opnode_tensors(n_hvx, node, in_mat, out_mat, in_dirty, out_dirty);
|
||||
|
||||
size_t n_scratch = 0;
|
||||
auto commit_tensor = [&](const ggml_tensor & m) -> const ggml_tensor * {
|
||||
@@ -714,13 +731,19 @@ static bool htp_lower_is_mm_opcode(uint32_t opcode) {
|
||||
opcode == HTP_OP_MUL_MAT_NX_GLU || opcode == HTP_OP_MUL_MAT_ID_NX;
|
||||
}
|
||||
|
||||
static const ggml_tensor * htp_lower_orig_mm_for_output(const htp_opnode & node, const ggml_tensor * tensor) {
|
||||
static const ggml_tensor * htp_lower_orig_mm_for_output(const htp_opnode & node, const ggml_tensor * tensor, uint32_t * out_w_idx) {
|
||||
if (out_w_idx) {
|
||||
*out_w_idx = 0;
|
||||
}
|
||||
if (!htp_lower_is_mm_opcode(node.opcode)) {
|
||||
return nullptr;
|
||||
}
|
||||
if (node.opcode == HTP_OP_MUL_MAT_NX || node.opcode == HTP_OP_MUL_MAT_ID_NX) {
|
||||
for (size_t p = 0; p < node.outputs.size(); ++p) {
|
||||
if (node.outputs[p] == tensor) {
|
||||
if (out_w_idx) {
|
||||
*out_w_idx = (uint32_t) p;
|
||||
}
|
||||
if (p == 0) {
|
||||
return node.node;
|
||||
}
|
||||
@@ -732,10 +755,14 @@ static const ggml_tensor * htp_lower_orig_mm_for_output(const htp_opnode & node,
|
||||
return node.node;
|
||||
}
|
||||
|
||||
static bool htp_lower_mm_blocks_output_f16(const ggml_tensor * orig_mm) {
|
||||
static bool htp_lower_mm_blocks_output_f16(const htp_opnode & owner, const ggml_tensor * orig_mm, uint32_t w_idx) {
|
||||
if (!orig_mm) {
|
||||
return false;
|
||||
}
|
||||
const auto * kp = owner.kparams<htp_mm_kernel_params>();
|
||||
if (((kp->scale_mask >> w_idx) & 1u) == 0 && ggml_get_src_scale(orig_mm, 0) != nullptr) {
|
||||
return true;
|
||||
}
|
||||
if (ggml_prec_get_acc(orig_mm) == GGML_PREC_F32) {
|
||||
return true;
|
||||
}
|
||||
@@ -752,14 +779,21 @@ static bool htp_lower_respect_prec_hints(
|
||||
const std::unordered_map<const ggml_tensor *, std::vector<size_t>> & root_users
|
||||
) {
|
||||
if (htp_lower_is_mm_opcode(owner.opcode)) {
|
||||
const ggml_tensor * orig_mm = htp_lower_orig_mm_for_output(owner, r);
|
||||
if (htp_lower_mm_blocks_output_f16(orig_mm)) {
|
||||
uint32_t w_idx = 0;
|
||||
const ggml_tensor * orig_mm = htp_lower_orig_mm_for_output(owner, r, &w_idx);
|
||||
if (owner.opcode == HTP_OP_MUL_MAT_NX_GLU && orig_mm && owner.inputs.size() >= 2 && orig_mm->src[0] == owner.inputs[1]) {
|
||||
w_idx = 1;
|
||||
}
|
||||
if (htp_lower_mm_blocks_output_f16(owner, orig_mm, w_idx)) {
|
||||
return false;
|
||||
}
|
||||
if (owner.opcode == HTP_OP_MUL_MAT_NX_GLU) {
|
||||
for (const ggml_tensor * f : owner.fused) {
|
||||
if (f && f->op == GGML_OP_MUL_MAT && htp_lower_mm_blocks_output_f16(f)) {
|
||||
return false;
|
||||
if (f && f->op == GGML_OP_MUL_MAT) {
|
||||
const uint32_t f_idx = (owner.inputs.size() >= 2 && f->src[0] == owner.inputs[1]) ? 1u : 0u;
|
||||
if (htp_lower_mm_blocks_output_f16(owner, f, f_idx)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -769,9 +803,12 @@ static bool htp_lower_respect_prec_hints(
|
||||
auto oit = owner_idx.find(in_root);
|
||||
if (oit != owner_idx.end()) {
|
||||
const htp_opnode & node = nodes[oit->second];
|
||||
if (htp_lower_is_mm_opcode(node.opcode) &&
|
||||
htp_lower_mm_blocks_output_f16(htp_lower_orig_mm_for_output(node, in_root))) {
|
||||
return false;
|
||||
if (htp_lower_is_mm_opcode(node.opcode)) {
|
||||
uint32_t w_idx = 0;
|
||||
const ggml_tensor * orig_mm = htp_lower_orig_mm_for_output(node, in_root, &w_idx);
|
||||
if (htp_lower_mm_blocks_output_f16(node, orig_mm, w_idx)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1201,7 +1238,7 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
|
||||
}
|
||||
for (const ggml_tensor * inp : tnode->inputs) {
|
||||
const ggml_tensor * inp_root = inp ? htp_tensor_root(inp) : nullptr;
|
||||
if (inp_root && can_lower(inp_root) && is_selected_root(inp_root) && apply_root(inp_root)) {
|
||||
if (inp_root && can_lower(inp_root) && apply_root(inp_root)) {
|
||||
expanded = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "htp/allreduce-ops.h"
|
||||
#include "htp/argsort-ops.h"
|
||||
#include "htp/binary-ops.h"
|
||||
#include "htp/conv2d-ops.h"
|
||||
#include "htp/cumsum-ops.h"
|
||||
#include "htp/diag-ops.h"
|
||||
#include "htp/dsv4-hc-ops.h"
|
||||
@@ -11,11 +12,13 @@
|
||||
#include "htp/gated-delta-net-ops.h"
|
||||
#include "htp/get-rows-ops.h"
|
||||
#include "htp/glu-ops.h"
|
||||
#include "htp/im2col-ops.h"
|
||||
#include "htp/lightning-indexer-ops.h"
|
||||
#include "htp/matmul-ops.h"
|
||||
#include "htp/pad-ops.h"
|
||||
#include "htp/repeat-ops.h"
|
||||
#include "htp/roll-ops.h"
|
||||
#include "htp/set-rows-ops.h"
|
||||
#include "htp/softmax-ops.h"
|
||||
#include "htp/solve-tri-ops.h"
|
||||
#include "htp/ssm-conv.h"
|
||||
@@ -48,6 +51,7 @@ std::string htp_opnode::name() const {
|
||||
}
|
||||
}
|
||||
case HTP_OP_MUL_MAT_ID_NX: return "MUL_MAT_ID_NX";
|
||||
case HTP_OP_MUL_ID: return "MUL_ID";
|
||||
case HTP_OP_ADD:
|
||||
case HTP_OP_SUB:
|
||||
case HTP_OP_MUL:
|
||||
@@ -64,6 +68,15 @@ std::string htp_opnode::name() const {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!node) {
|
||||
switch (opcode) {
|
||||
case HTP_OP_ADD: return "ADD";
|
||||
case HTP_OP_SUB: return "SUB";
|
||||
case HTP_OP_MUL: return "MUL";
|
||||
case HTP_OP_DIV: return "DIV";
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -161,10 +174,11 @@ static std::string format_kernel_params(const htp_opnode & node) {
|
||||
const auto * kparams = node.kparams<htp_mm_kernel_params>();
|
||||
int32_t type = kparams->kernel_type;
|
||||
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);
|
||||
snprintf(str, max_size, "hmx-w8a8%s nhvx %u vtcm %u",
|
||||
kparams->norm_w_idx ? "+norm" : "", (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) {
|
||||
if (htp_mm_kernel_is_hmx_f16(type)) {
|
||||
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) {
|
||||
@@ -172,16 +186,17 @@ static std::string format_kernel_params(const htp_opnode & node) {
|
||||
} 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%s nhvx %u vtcm %u", path, kparams->norm_w_idx ? "+norm" : "",
|
||||
(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_F16) {
|
||||
path = kparams->u.hmx.pipeline ? "hmx-f16-pipe" : "hmx-f16-seq";
|
||||
path = (kparams->flags & HTP_FA_FLAG_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";
|
||||
path = (kparams->flags & HTP_FA_FLAG_PIPELINE) ? "hmx-w8a8-pipe" : "hmx-w8a8-seq";
|
||||
} else if (type == HTP_FA_KERNEL_HVX) {
|
||||
path = "hvx";
|
||||
}
|
||||
@@ -194,11 +209,14 @@ static std::string format_kernel_params(const htp_opnode & node) {
|
||||
snprintf(str, max_size, "idx %d count %d", (int) node.node->op_params[0], (int) node.dst()->ne[1]);
|
||||
} else if ((node.opcode == HTP_OP_FENCE || node.opcode == HTP_OP_CPY_FENCE) && node.node) {
|
||||
snprintf(str, max_size, "seq 0x%x", (uint32_t) node.node->op_params[0]);
|
||||
} else if (node.opcode == HTP_OP_ALLREDUCE && node.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_ALLREDUCE || node.opcode == HTP_OP_ALLREDUCE_ADD) && node.node) {
|
||||
const auto * kparams = node.kparams<htp_allreduce_kernel_params>();
|
||||
snprintf(str, max_size, "seq 0x%x -> 0x%x nhvx %u vtcm %u",
|
||||
(uint32_t) node.node->op_params[0], (uint32_t) node.node->op_params[1],
|
||||
(unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_SSM_CONV) {
|
||||
const auto * kparams = node.kparams<htp_ssm_conv_kernel_params>();
|
||||
const char * mode = kparams->token_major
|
||||
const char * mode = (kparams->flags & HTP_SSM_CONV_FLAG_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,
|
||||
@@ -236,7 +254,7 @@ static std::string format_kernel_params(const htp_opnode & node) {
|
||||
(unsigned int) kparams->n_hvx, (unsigned int) kparams->n_tokens_tile,
|
||||
(unsigned int) kparams->kv_block, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_MUL || node.opcode == HTP_OP_ADD || node.opcode == HTP_OP_ADD_ID ||
|
||||
node.opcode == HTP_OP_SUB || node.opcode == HTP_OP_DIV) {
|
||||
node.opcode == HTP_OP_MUL_ID || node.opcode == HTP_OP_SUB || node.opcode == HTP_OP_DIV) {
|
||||
const auto * kparams = node.kparams<htp_binary_kernel_params>();
|
||||
snprintf(str, max_size, "nhvx %u vtcm %u", (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_ARGSORT || node.opcode == HTP_OP_TOP_K) {
|
||||
@@ -273,16 +291,43 @@ static std::string format_kernel_params(const htp_opnode & node) {
|
||||
snprintf(str, max_size, "%s nhvx %u tile %u x%u vtcm %u", ktype_str, (unsigned int) kparams->n_hvx, (unsigned int) kparams->tile_cols, (unsigned int) kparams->n_col_tiles, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_FILL) {
|
||||
const auto * kparams = node.kparams<htp_fill_kernel_params>();
|
||||
snprintf(str, max_size, "%s nhvx %u", kparams->kernel_type == HTP_FILL_KERNEL_CONTIGUOUS ? "contig" : "strided", (unsigned int) kparams->n_hvx);
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u", kparams->kernel_type == HTP_FILL_KERNEL_CONTIGUOUS ? "contig" : "strided",
|
||||
(unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_ROLL) {
|
||||
const auto * kparams = node.kparams<htp_roll_kernel_params>();
|
||||
snprintf(str, max_size, "k%d nhvx %u", (int) kparams->kernel_type, (unsigned int) kparams->n_hvx);
|
||||
snprintf(str, max_size, "k%d nhvx %u vtcm %u", (int) kparams->kernel_type,
|
||||
(unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_DIAG) {
|
||||
const auto * kparams = node.kparams<htp_diag_kernel_params>();
|
||||
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);
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u", (kparams->flags & HTP_SOLVE_TRI_FLAG_BATCHED) ? "batched" : "chunked",
|
||||
(unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_SUM_ROWS || node.opcode == HTP_OP_SUM || node.opcode == HTP_OP_ARGMAX) {
|
||||
const auto * kparams = node.kparams<htp_sum_rows_kernel_params>();
|
||||
snprintf(str, max_size, "nhvx %u vtcm %u", (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_SET_ROWS) {
|
||||
const auto * kparams = node.kparams<htp_set_rows_kernel_params>();
|
||||
snprintf(str, max_size, "nhvx %u vtcm %u", (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_CONV_2D || node.opcode == HTP_OP_CONV_3D) {
|
||||
const auto * kparams = node.kparams<htp_conv2d_kernel_params>();
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u",
|
||||
kparams->kernel_type == HTP_CONV2D_KERNEL_HMX_WINDOW ? "window" : "tiled",
|
||||
(unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_CONV_2D_DW) {
|
||||
const auto * kparams = node.kparams<htp_conv2d_dw_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);
|
||||
} else if (node.opcode == HTP_OP_IM2COL || node.opcode == HTP_OP_IM2COL_3D) {
|
||||
const auto * kparams = node.kparams<htp_im2col_kernel_params>();
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u",
|
||||
kparams->kernel_type == HTP_IM2COL_KERNEL_PATCHEMBED_DMA ? "patchembed" : "blocked",
|
||||
(unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_REPEAT) {
|
||||
const auto * kparams = node.kparams<htp_repeat_kernel_params>();
|
||||
snprintf(str, max_size, "%s nhvx %u vtcm %u", kparams->pipelined ? "pipe" : "tiled",
|
||||
(unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
|
||||
} else if (node.opcode == HTP_OP_UPSCALE) {
|
||||
const auto * kparams = node.kparams<htp_upscale_kernel_params>();
|
||||
const char * ktype_str = "unknown";
|
||||
@@ -290,7 +335,7 @@ static std::string format_kernel_params(const htp_opnode & node) {
|
||||
case HTP_UPSCALE_KERNEL_NEAREST: ktype_str = "nearest"; break;
|
||||
case HTP_UPSCALE_KERNEL_NEAREST_2X: ktype_str = "nearest-2x"; break;
|
||||
case HTP_UPSCALE_KERNEL_NEAREST_4X: ktype_str = "nearest-4x"; break;
|
||||
case HTP_UPSCALE_KERNEL_BILINEAR: ktype_str = kparams->align_corners ? "bilinear-ac" : "bilinear"; break;
|
||||
case HTP_UPSCALE_KERNEL_BILINEAR: ktype_str = (kparams->flags & HTP_UPSCALE_FLAG_ALIGN_CORNERS) ? "bilinear-ac" : "bilinear"; break;
|
||||
case HTP_UPSCALE_KERNEL_BILINEAR_2X: ktype_str = "bilinear-2x"; break;
|
||||
case HTP_UPSCALE_KERNEL_BILINEAR_AA: ktype_str = "bilinear-aa"; break;
|
||||
case HTP_UPSCALE_KERNEL_BICUBIC: ktype_str = "bicubic"; break;
|
||||
|
||||
@@ -91,6 +91,13 @@ struct htp_opnode {
|
||||
// Build inputs (preserving optional nullptrs)
|
||||
if (htp_is_hadamard(this->node)) {
|
||||
this->inputs.push_back(this->node->src[1]);
|
||||
} else if (this->node->op == GGML_OP_MUL_MAT) {
|
||||
this->inputs.push_back(this->node->src[0]);
|
||||
this->inputs.push_back(this->node->src[1]);
|
||||
} else if (this->node->op == GGML_OP_MUL_MAT_ID) {
|
||||
this->inputs.push_back(this->node->src[0]);
|
||||
this->inputs.push_back(this->node->src[1]);
|
||||
this->inputs.push_back(this->node->src[2]);
|
||||
} else if (this->node->op != GGML_OP_FILL) {
|
||||
int n_inputs = n_active_src(this->node);
|
||||
for (int i = 0; i < n_inputs; i++) {
|
||||
|
||||
@@ -1602,6 +1602,54 @@ static void repack_tiled_nvfp4(void * data, const ggml_tensor * t, size_t offset
|
||||
}
|
||||
}
|
||||
|
||||
// --- F8_E4M3 (1024 B tile: 32x32 in 4-K-grouped vrmpy byte order, lossless) ---
|
||||
|
||||
static void repack_f8_e4m3_to_tiled(ggml_tensor * t, const void * data, size_t offset, size_t size) {
|
||||
repack_to_tiled_grid_32<uint8_t, 32>(t, data, offset, size, HTP_TENSOR_TILE_SIZE_F8_E4M3,
|
||||
[](uint8_t * tile_dst, const uint8_t * src_slice, int64_t ne0, int64_t ne1, int64_t row_stride, int ct, int kt) {
|
||||
for (int p = 0; p < 8; p++) {
|
||||
const int64_t c_base = (int64_t) kt * 32 + p * 4;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = (int64_t) ct * 32 + row;
|
||||
const uint8_t * r_ptr = (r < ne1) ? &src_slice[r * row_stride] : nullptr;
|
||||
uint8_t * dst = tile_dst + p * 128 + row * 4;
|
||||
if (r_ptr && c_base + 4 <= ne0) {
|
||||
memcpy(dst, r_ptr + c_base, 4);
|
||||
} else {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
dst[j] = (r_ptr && c_base + j < ne0) ? r_ptr[c_base + j] : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static void unpack_f8_e4m3_from_tiled(void * data, const ggml_tensor * t, size_t offset, size_t size) {
|
||||
unpack_from_tiled_grid_32<uint8_t, 32>(data, t, offset, size, HTP_TENSOR_TILE_SIZE_F8_E4M3,
|
||||
[](uint8_t * 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 p = 0; p < 8; p++) {
|
||||
const int64_t c_base = (int64_t) kt * 32 + p * 4;
|
||||
for (int row = 0; row < 32; row++) {
|
||||
const int64_t r = (int64_t) ct * 32 + row;
|
||||
if (r >= start_row && r < end_row) {
|
||||
uint8_t * r_ptr = dst_slice + (r - start_row) * row_stride;
|
||||
const uint8_t * src = tile_src + p * 128 + row * 4;
|
||||
if (c_base + 4 <= ne0) {
|
||||
memcpy(r_ptr + c_base, src, 4);
|
||||
} else {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
if (c_base + j < ne0) {
|
||||
r_ptr[c_base + j] = src[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// --- Top-level Repack / Unpack Dispatchers ---
|
||||
|
||||
void htp_repack_tensor_tiled(ggml_tensor * tensor, const void * data, size_t size) {
|
||||
@@ -1631,6 +1679,9 @@ void htp_repack_tensor_tiled(ggml_tensor * tensor, const void * data, size_t siz
|
||||
case GGML_TYPE_NVFP4:
|
||||
repack_nvfp4_tiled(tensor, data, 0, size);
|
||||
break;
|
||||
case GGML_TYPE_F8_E4M3:
|
||||
repack_f8_e4m3_to_tiled(tensor, data, 0, size);
|
||||
break;
|
||||
case GGML_TYPE_IQ2_XXS:
|
||||
repack_iq2_xxs_tiled(tensor, data, 0, size);
|
||||
break;
|
||||
@@ -1698,6 +1749,9 @@ void htp_unpack_tensor_tiled(void * data, const ggml_tensor * tensor, size_t off
|
||||
case GGML_TYPE_NVFP4:
|
||||
repack_tiled_nvfp4(data, tensor, offset, size);
|
||||
break;
|
||||
case GGML_TYPE_F8_E4M3:
|
||||
unpack_f8_e4m3_from_tiled(data, tensor, offset, size);
|
||||
break;
|
||||
case GGML_TYPE_IQ2_XXS:
|
||||
repack_tiled_iq2_xxs(data, tensor, offset, size);
|
||||
break;
|
||||
|
||||
@@ -17,7 +17,11 @@ static inline bool htp_is_tiled_weight_type(enum ggml_type type) {
|
||||
type == GGML_TYPE_Q2_K || type == GGML_TYPE_Q3_K ||
|
||||
type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K ||
|
||||
type == GGML_TYPE_F16 || type == GGML_TYPE_F32 ||
|
||||
type == GGML_TYPE_BF16;
|
||||
type == GGML_TYPE_BF16 || type == GGML_TYPE_F8_E4M3;
|
||||
}
|
||||
|
||||
static inline bool htp_is_quantized_weight_type(enum ggml_type type) {
|
||||
return ggml_is_quantized(type) || type == GGML_TYPE_F8_E4M3;
|
||||
}
|
||||
|
||||
static inline bool htp_is_tiled_weight_tensor(const struct ggml_tensor * t) {
|
||||
@@ -30,7 +34,7 @@ static inline bool htp_is_tiled_weight_tensor(const struct ggml_tensor * t) {
|
||||
if (t->ne[0] < 32 || (t->ne[0] % 32 != 0) || t->ne[3] != 1) {
|
||||
return false;
|
||||
}
|
||||
if (!ggml_is_quantized(t->type)) {
|
||||
if (!htp_is_quantized_weight_type(t->type)) {
|
||||
return t->ne[1] > 1;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -23,6 +23,9 @@ add_library(${HTP_LIB} SHARED
|
||||
hmx-queue.c
|
||||
htp-tensor.c
|
||||
matmul-ops.c
|
||||
matmul-hmx-w8a8.c
|
||||
matmul-hmx-f16.c
|
||||
matmul-hvx.c
|
||||
flash-attn-ops.c
|
||||
gated-delta-net-ops.c
|
||||
binary-ops.c
|
||||
|
||||
@@ -21,7 +21,6 @@ struct htp_allreduce_context {
|
||||
uint32_t n_dsts;
|
||||
uint32_t nelem;
|
||||
uint32_t ne0;
|
||||
uint32_t ne1;
|
||||
uint32_t row_size_aligned;
|
||||
uint32_t rank_elem_start;
|
||||
uint32_t rank_nelem;
|
||||
@@ -251,38 +250,6 @@ DEFINE_ALLREDUCE_THREAD_DMA_2D(add_f32, float, hvx_add_f32_aaa, 1, 0)
|
||||
DEFINE_ALLREDUCE_THREAD_DMA_2D(add_bcast_f16, __fp16, hvx_add_f16_aaa, 1, 1)
|
||||
DEFINE_ALLREDUCE_THREAD_DMA_2D(add_bcast_f32, float, hvx_add_f32_aaa, 1, 1)
|
||||
|
||||
static int validate_allreduce(
|
||||
struct htp_ops_context * octx,
|
||||
const struct htp_allreduce_kernel_params * kparams,
|
||||
uint32_t n_ranks
|
||||
) {
|
||||
if (kparams->n_hvx == 0 || (uint32_t) kparams->n_hvx > octx->ctx->n_hvx) {
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
octx->n_hvx = (uint32_t) kparams->n_hvx;
|
||||
|
||||
if (kparams->vtcm_size_per_hvx <= 0 || kparams->vtcm_size <= 0) {
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
|
||||
const bool has_add = (octx->op == HTP_OP_ALLREDUCE_ADD);
|
||||
const size_t n_vtcm_buffers = htp_allreduce_vtcm_buffer_count(
|
||||
n_ranks, octx->n_hvx, has_add, kparams->is_row_bcast != 0);
|
||||
const size_t vtcm_size = n_vtcm_buffers * (size_t) kparams->vtcm_size_per_hvx;
|
||||
if (vtcm_size != (size_t) kparams->vtcm_size) {
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
if (vtcm_size > octx->ctx->vtcm_size) {
|
||||
return HTP_STATUS_VTCM_TOO_SMALL;
|
||||
}
|
||||
|
||||
if (octx->dst->type != HTP_TYPE_F16 && octx->dst->type != HTP_TYPE_F32) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
int op_allreduce(struct htp_ops_context * octx) {
|
||||
if (octx->ctx->mdev.count > 1 && octx->ctx->mdev.idx > 0) {
|
||||
return HTP_STATUS_OK;
|
||||
@@ -294,28 +261,24 @@ int op_allreduce(struct htp_ops_context * octx) {
|
||||
const uint32_t rank = (uint32_t) kparams->rank;
|
||||
const uint32_t n_ranks = (uint32_t) kparams->n_ranks;
|
||||
|
||||
if (n_ranks < 2 || n_ranks > HTP_ALLREDUCE_MAX_RANKS || rank >= n_ranks) {
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
|
||||
const uint32_t fence_seq_entry = (uint32_t) octx->op_params[0];
|
||||
const uint32_t fence_seq_exit = (uint32_t) octx->op_params[1];
|
||||
|
||||
const struct htp_tensor * my_sync = octx->src[n_ranks + rank];
|
||||
atomic_uint * my_fence = (atomic_uint *) (uintptr_t) my_sync->data;
|
||||
|
||||
const int status = validate_allreduce(octx, kparams, n_ranks);
|
||||
if (status != HTP_STATUS_OK) {
|
||||
if (status == HTP_STATUS_NO_SUPPORT) {
|
||||
FARF(ERROR, "ggml-hex: allreduce unsupported type %d : rank %u\n", dst->type, rank);
|
||||
}
|
||||
htp_fence_write(my_fence, fence_seq_exit, status);
|
||||
return status;
|
||||
if (!htp_ops_context_set_n_hvx(octx, (uint32_t) kparams->n_hvx)) {
|
||||
htp_fence_write(my_fence, fence_seq_exit, HTP_STATUS_INVAL_PARAMS);
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
if ((size_t) kparams->vtcm_size > octx->ctx->vtcm_size) {
|
||||
htp_fence_write(my_fence, fence_seq_exit, HTP_STATUS_VTCM_TOO_SMALL);
|
||||
return HTP_STATUS_VTCM_TOO_SMALL;
|
||||
}
|
||||
|
||||
const bool has_add = (octx->op == HTP_OP_ALLREDUCE_ADD);
|
||||
const uint32_t nelem = dst->ne[0] * dst->ne[1] * dst->ne[2] * dst->ne[3];
|
||||
const int32_t mode = kparams->mode;
|
||||
const bool sharded_fanout = (kparams->flags & HTP_ALLREDUCE_FLAG_SHARDED_FANOUT) != 0;
|
||||
|
||||
// 1. Entry Barrier: Synchronize all ranks before reading
|
||||
struct htp_thread_trace * tr0 = &octx->ctx->trace[0];
|
||||
@@ -368,14 +331,13 @@ int op_allreduce(struct htp_ops_context * octx) {
|
||||
actx.n_dsts = (uint32_t) kparams->n_dsts ? (uint32_t) kparams->n_dsts : n_ranks;
|
||||
actx.nelem = nelem;
|
||||
actx.ne0 = (uint32_t) kparams->ne0;
|
||||
actx.ne1 = (uint32_t) kparams->ne1;
|
||||
actx.row_size_aligned = (uint32_t) kparams->row_size_aligned;
|
||||
actx.rank_elem_start = (uint32_t) kparams->rank_elem_start;
|
||||
actx.rank_nelem = (uint32_t) kparams->rank_nelem;
|
||||
actx.elems_per_hvx = elems_per_hvx;
|
||||
actx.block_elems = block_elems;
|
||||
actx.vtcm_size_per_hvx = vtcm_size_per_hvx;
|
||||
actx.is_row_bcast = (kparams->is_row_bcast != 0);
|
||||
actx.is_row_bcast = (kparams->flags & HTP_ALLREDUCE_FLAG_ROW_BCAST) != 0;
|
||||
|
||||
work_queue_func_t reduce_fun = NULL;
|
||||
switch (kparams->kernel_type) {
|
||||
@@ -388,7 +350,7 @@ int op_allreduce(struct htp_ops_context * octx) {
|
||||
break;
|
||||
case HTP_ALLREDUCE_KERNEL_DMA_2D:
|
||||
if (has_add) {
|
||||
if (kparams->is_row_bcast) {
|
||||
if (actx.is_row_bcast) {
|
||||
reduce_fun = (dst->type == HTP_TYPE_F16) ? allreduce_thread_dma_2d_add_bcast_f16 : allreduce_thread_dma_2d_add_bcast_f32;
|
||||
} else {
|
||||
reduce_fun = (dst->type == HTP_TYPE_F16) ? allreduce_thread_dma_2d_add_f16 : allreduce_thread_dma_2d_add_f32;
|
||||
@@ -430,7 +392,7 @@ int op_allreduce(struct htp_ops_context * octx) {
|
||||
htp_trace_event_start(tr0, HTP_TRACE_EVT_FENCE, (uint16_t) fence_seq_exit);
|
||||
|
||||
// drain fan-out DMA writes before exit barrier
|
||||
if (mode == HTP_ALLREDUCE_SHARDED_FANOUT) {
|
||||
if (sharded_fanout) {
|
||||
asm volatile ("syncht" : : : "memory");
|
||||
}
|
||||
|
||||
|
||||
@@ -17,9 +17,9 @@ enum htp_allreduce_kernel_type {
|
||||
HTP_ALLREDUCE_KERNEL_DMA_2D,
|
||||
};
|
||||
|
||||
enum htp_allreduce_mode {
|
||||
HTP_ALLREDUCE_FULL = 0, // all ranks reduce full tensor
|
||||
HTP_ALLREDUCE_SHARDED_FANOUT = 1, // each rank reduces 1/N shard and fans out to all buffers
|
||||
enum htp_allreduce_flags {
|
||||
HTP_ALLREDUCE_FLAG_ROW_BCAST = (1u << 0),
|
||||
HTP_ALLREDUCE_FLAG_SHARDED_FANOUT = (1u << 1), // each rank reduces 1/N shard and fans out to all buffers
|
||||
};
|
||||
|
||||
static inline size_t htp_allreduce_vtcm_buffer_count(
|
||||
@@ -33,6 +33,7 @@ static inline size_t htp_allreduce_vtcm_buffer_count(
|
||||
|
||||
struct htp_allreduce_kernel_params {
|
||||
uint8_t kernel_type;
|
||||
uint8_t flags; // bitmask of enum htp_allreduce_flags
|
||||
int32_t rank;
|
||||
int32_t n_ranks;
|
||||
int32_t n_hvx;
|
||||
@@ -41,13 +42,10 @@ struct htp_allreduce_kernel_params {
|
||||
int32_t vtcm_size_per_hvx;
|
||||
int32_t vtcm_size;
|
||||
int32_t ne0;
|
||||
int32_t ne1;
|
||||
int32_t row_size_aligned;
|
||||
int32_t rank_elem_start;
|
||||
int32_t rank_nelem;
|
||||
int32_t n_dsts;
|
||||
int32_t is_row_bcast;
|
||||
int32_t mode; // enum htp_allreduce_mode: FULL or SHARDED_FANOUT
|
||||
};
|
||||
|
||||
#if defined(__cplusplus)
|
||||
|
||||
@@ -1454,10 +1454,6 @@ static void htp_top_k_identity_job(const htp_thread_t * th, unsigned int n, unsi
|
||||
}
|
||||
|
||||
static int op_sort_common(struct htp_ops_context * octx, bool is_top_k) {
|
||||
if (octx->src[0]->type != HTP_TYPE_F32 && octx->src[0]->type != HTP_TYPE_F16) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ struct htp_binary_context {
|
||||
uint8_t * vtcm_base;
|
||||
|
||||
struct fastdiv_values src0_dim1_div; // ne01
|
||||
struct fastdiv_values src0_dim2_div; // ne02
|
||||
struct fastdiv_values src0_dim12_div;// ne03
|
||||
|
||||
struct fastdiv_values src1_dim1_div; // ne11
|
||||
@@ -479,6 +478,7 @@ DEFINE_BINARY_MIXED_OP(sub, BINARY_OP_SUB, _Pragma("unroll(2)"))
|
||||
DEFINE_BINARY_MIXED_OP(mul, BINARY_OP_MUL, _Pragma("unroll(2)"))
|
||||
DEFINE_BINARY_MIXED_OP(div, BINARY_OP_DIV, )
|
||||
DEFINE_BINARY_MIXED_F16_F16_F16(binary_add_f16_f16_f16, BINARY_OP_ADD)
|
||||
DEFINE_BINARY_MIXED_F16_F16_F16(binary_mul_f16_f16_f16, BINARY_OP_MUL)
|
||||
|
||||
#define DEFINE_COMPUTE_MIXED_SHAPES(OP_NAME, TYPES) \
|
||||
DEFINE_COMPUTE_SAME_SHAPE(OP_NAME##_##TYPES, binary_##OP_NAME##_##TYPES) \
|
||||
@@ -699,6 +699,60 @@ DEFINE_COMPUTE_ADD_ID(f32_f16_f16, binary_add_f32_f16_f16, add_id_copy_f32_f
|
||||
DEFINE_COMPUTE_ADD_ID(f32_f16_f32, binary_add_f32_f16_f32, add_id_copy_f32_f32)
|
||||
DEFINE_COMPUTE_ADD_ID(f32_f32_f16, binary_add_f32_f32_f16, add_id_copy_f32_f16)
|
||||
|
||||
#define DEFINE_COMPUTE_MUL_ID(NAME, MUL_FN, COPY_FN) \
|
||||
static void compute_mul_id_##NAME( \
|
||||
uint8_t * dst, const uint8_t * src0, const uint8_t * src1_data, const int32_t * src2_ids, \
|
||||
uint32_t i01, uint32_t i02, uint32_t ne01, uint32_t src1_stride, \
|
||||
uint32_t n_rows, size_t dst_stride, size_t src0_stride, uint32_t ne00) { \
|
||||
for (uint32_t r = 0; r < n_rows; r++) { \
|
||||
const uint32_t r_i01 = i01 + r; \
|
||||
const int32_t idx = src2_ids[i02 * ne01 + r_i01]; \
|
||||
const uint8_t * r_src0 = src0 + r * src0_stride; \
|
||||
uint8_t * r_dst = dst + r * dst_stride; \
|
||||
if (idx < 0) { \
|
||||
COPY_FN(r_dst, r_src0, ne00); \
|
||||
continue; \
|
||||
} \
|
||||
const uint8_t * r_src1 = src1_data + idx * src1_stride; \
|
||||
MUL_FN(r_dst, r_src0, r_src1, ne00); \
|
||||
} \
|
||||
}
|
||||
|
||||
DEFINE_COMPUTE_MUL_ID(f16_f16_f16, binary_mul_f16_f16_f16, add_id_copy_f16_f16)
|
||||
DEFINE_COMPUTE_MUL_ID(f32_f32_f32, hvx_mul_f32_aaa, add_id_copy_f32_f32)
|
||||
DEFINE_COMPUTE_MUL_ID(f16_f16_f32, binary_mul_f16_f16_f32, add_id_copy_f16_f32)
|
||||
DEFINE_COMPUTE_MUL_ID(f16_f32_f16, binary_mul_f16_f32_f16, add_id_copy_f16_f16)
|
||||
DEFINE_COMPUTE_MUL_ID(f16_f32_f32, binary_mul_f16_f32_f32, add_id_copy_f16_f32)
|
||||
DEFINE_COMPUTE_MUL_ID(f32_f16_f16, binary_mul_f32_f16_f16, add_id_copy_f32_f16)
|
||||
DEFINE_COMPUTE_MUL_ID(f32_f16_f32, binary_mul_f32_f16_f32, add_id_copy_f32_f32)
|
||||
DEFINE_COMPUTE_MUL_ID(f32_f32_f16, binary_mul_f32_f32_f16, add_id_copy_f32_f16)
|
||||
|
||||
#define DEFINE_COMPUTE_MUL_ID_SCALAR(NAME, TYPE, MUL_FN, COPY_FN) \
|
||||
static void compute_mul_id_scalar_##NAME( \
|
||||
uint8_t * dst, const uint8_t * src0, const uint8_t * src1_data, const int32_t * src2_ids, \
|
||||
uint32_t i01, uint32_t i02, uint32_t ne01, uint32_t src1_stride, \
|
||||
uint32_t n_rows, size_t dst_stride, size_t src0_stride, uint32_t ne00) { \
|
||||
(void) src1_stride; \
|
||||
const TYPE * table = (const TYPE *) src1_data; \
|
||||
for (uint32_t r = 0; r < n_rows; r++) { \
|
||||
const uint32_t r_i01 = i01 + r; \
|
||||
const int32_t idx = src2_ids[i02 * ne01 + r_i01]; \
|
||||
const uint8_t * r_src0 = src0 + r * src0_stride; \
|
||||
uint8_t * r_dst = dst + r * dst_stride; \
|
||||
if (idx < 0) { \
|
||||
COPY_FN(r_dst, r_src0, ne00); \
|
||||
continue; \
|
||||
} \
|
||||
MUL_FN(r_dst, r_src0, table[idx], ne00); \
|
||||
} \
|
||||
}
|
||||
|
||||
DEFINE_COMPUTE_MUL_ID_SCALAR(f16_f16_f16, _Float16, hvx_mul_scalar_f16_aa, add_id_copy_f16_f16)
|
||||
DEFINE_COMPUTE_MUL_ID_SCALAR(f16_f32_f16, float, binary_scalar_mul_f16_f16, add_id_copy_f16_f16)
|
||||
DEFINE_COMPUTE_MUL_ID_SCALAR(f16_f32_f32, float, binary_scalar_mul_f16_f32, add_id_copy_f16_f32)
|
||||
DEFINE_COMPUTE_MUL_ID_SCALAR(f32_f32_f16, float, binary_scalar_mul_f32_f16, add_id_copy_f32_f16)
|
||||
DEFINE_COMPUTE_MUL_ID_SCALAR(f32_f32_f32, float, hvx_mul_scalar_f32_aa, add_id_copy_f32_f32)
|
||||
|
||||
struct binary_io_pipe_state {
|
||||
const struct htp_binary_context * bctx;
|
||||
dma_addr_t src0_data;
|
||||
@@ -1378,7 +1432,7 @@ static int execute_op_binary(struct htp_ops_context * octx) {
|
||||
const uint32_t table_type_key = type_key == 0 ? 0 : (type_key | 2);
|
||||
|
||||
if (kparams->kernel_type == HTP_BINARY_KERNEL_CHUNKED) {
|
||||
const bool is_scalar = kparams->is_scalar;
|
||||
const bool is_scalar = (kparams->flags & HTP_BINARY_FLAG_SCALAR) != 0;
|
||||
|
||||
compute_binary_chunked_t compute = NULL;
|
||||
compute_binary_scalar_chunked_f32_t compute_scalar_f32 = NULL;
|
||||
@@ -1707,6 +1761,35 @@ static int execute_op_binary(struct htp_ops_context * octx) {
|
||||
}
|
||||
}
|
||||
break;
|
||||
case HTP_BINARY_KERNEL_MUL_ID:
|
||||
if (octx->op == HTP_OP_MUL_ID) {
|
||||
worker_func = binary_thread_add_id;
|
||||
switch (type_key) {
|
||||
case 0: compute_func = (void *) compute_mul_id_f16_f16_f16; break;
|
||||
case 1: compute_func = (void *) compute_mul_id_f16_f16_f32; break;
|
||||
case 2: compute_func = (void *) compute_mul_id_f16_f32_f16; break;
|
||||
case 3: compute_func = (void *) compute_mul_id_f16_f32_f32; break;
|
||||
case 4: compute_func = (void *) compute_mul_id_f32_f16_f16; break;
|
||||
case 5: compute_func = (void *) compute_mul_id_f32_f16_f32; break;
|
||||
case 6: compute_func = (void *) compute_mul_id_f32_f32_f16; break;
|
||||
case 7: compute_func = (void *) compute_mul_id_f32_f32_f32; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case HTP_BINARY_KERNEL_MUL_ID_SCALAR:
|
||||
if (octx->op == HTP_OP_MUL_ID) {
|
||||
worker_func = binary_thread_add_id;
|
||||
switch (table_type_key) {
|
||||
case 0: compute_func = (void *) compute_mul_id_scalar_f16_f16_f16; break;
|
||||
case 2: compute_func = (void *) compute_mul_id_scalar_f16_f32_f16; break;
|
||||
case 3: compute_func = (void *) compute_mul_id_scalar_f16_f32_f32; break;
|
||||
case 6: compute_func = (void *) compute_mul_id_scalar_f32_f32_f16; break;
|
||||
case 7: compute_func = (void *) compute_mul_id_scalar_f32_f32_f32; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
@@ -1717,7 +1800,7 @@ static int execute_op_binary(struct htp_ops_context * octx) {
|
||||
dma_queue * dma_q = octx->ctx->dma[0];
|
||||
uint8_t * vtcm_src1 = VTCM_LAYOUT_PTR(uint8_t, bctx.vtcm_base, bctx.vtcm_layout.off_src1);
|
||||
if (kparams->kernel_type == HTP_BINARY_KERNEL_ROW_BCAST) {
|
||||
if (!kparams->src1_strided) {
|
||||
if (!(kparams->flags & HTP_BINARY_FLAG_SRC1_STRIDED)) {
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data), bctx.vtcm_layout.src1_size, 0, src1->ne[0] * src1_elem_size, 1);
|
||||
} else {
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data), src1_elem_size, src1->nb[0], src1_elem_size, src1->ne[0]);
|
||||
@@ -1725,7 +1808,7 @@ static int execute_op_binary(struct htp_ops_context * octx) {
|
||||
} else if (kparams->kernel_type == HTP_BINARY_KERNEL_SCALAR ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_REPEAT ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_COMPLEX) {
|
||||
if (!kparams->src1_strided) {
|
||||
if (!(kparams->flags & HTP_BINARY_FLAG_SRC1_STRIDED)) {
|
||||
const size_t src1_bytes = (size_t) src1->ne[0] * src1->ne[1] * src1->ne[2] * src1->ne[3] * src1_elem_size;
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data), bctx.vtcm_layout.src1_size, 0, src1_bytes, 1);
|
||||
if (kparams->kernel_type == HTP_BINARY_KERNEL_SCALAR && octx->src[2] != NULL) {
|
||||
@@ -1763,12 +1846,41 @@ static int execute_op_binary(struct htp_ops_context * octx) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (kparams->kernel_type == HTP_BINARY_KERNEL_ADD_ID) {
|
||||
} else if (kparams->kernel_type == HTP_BINARY_KERNEL_ADD_ID ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_MUL_ID) {
|
||||
const struct htp_tensor * src2 = octx->src[2];
|
||||
uint8_t * vtcm_src2 = VTCM_LAYOUT_PTR(uint8_t, bctx.vtcm_base, bctx.vtcm_layout.off_src2);
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data),
|
||||
kparams->src1_row_size_aligned, src1->nb[1],
|
||||
src1->ne[0] * src1_elem_size, src1->ne[1]);
|
||||
if (!(kparams->flags & HTP_BINARY_FLAG_SRC1_STRIDED)) {
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data),
|
||||
kparams->src1_row_size_aligned, src1->nb[1],
|
||||
src1->ne[0] * src1_elem_size, src1->ne[1]);
|
||||
} else {
|
||||
for (uint32_t i11 = 0; i11 < src1->ne[1]; i11++) {
|
||||
dma_addr_t src_addr = src1->data + i11 * src1->nb[1];
|
||||
uint8_t * dst_ptr = vtcm_src1 + i11 * kparams->src1_row_size_aligned;
|
||||
while (!dma_queue_push(dma_q, dma_make_data(dst_ptr, src_addr),
|
||||
src1_elem_size, src1->nb[0], src1_elem_size, src1->ne[0])) {
|
||||
dma_queue_pop(dma_q);
|
||||
}
|
||||
}
|
||||
}
|
||||
const size_t src2_row_bytes = src2->ne[0] * sizeof(int32_t);
|
||||
while (!dma_queue_push(dma_q, dma_make_data(vtcm_src2, src2->data),
|
||||
src2_row_bytes, src2->nb[1],
|
||||
src2_row_bytes, src2->ne[1])) {
|
||||
dma_queue_pop(dma_q);
|
||||
}
|
||||
} else if (kparams->kernel_type == HTP_BINARY_KERNEL_MUL_ID_SCALAR) {
|
||||
const struct htp_tensor * src2 = octx->src[2];
|
||||
uint8_t * vtcm_src2 = VTCM_LAYOUT_PTR(uint8_t, bctx.vtcm_base, bctx.vtcm_layout.off_src2);
|
||||
if (!(kparams->flags & HTP_BINARY_FLAG_SRC1_STRIDED)) {
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data),
|
||||
bctx.vtcm_layout.src1_size, 0,
|
||||
src1->ne[0] * src1_elem_size, 1);
|
||||
} else {
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_src1, src1->data),
|
||||
src1_elem_size, src1->nb[0], src1_elem_size, src1->ne[0]);
|
||||
}
|
||||
const size_t src2_row_bytes = src2->ne[0] * sizeof(int32_t);
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_src2, src2->data),
|
||||
src2_row_bytes, src2->nb[1],
|
||||
@@ -1793,24 +1905,27 @@ static int execute_op_binary(struct htp_ops_context * octx) {
|
||||
bctx.src1_table_elem_size = table_type_key == 0 ? sizeof(_Float16) : sizeof(float);
|
||||
|
||||
bctx.src0_dim1_div = kparams->div_ne01;
|
||||
bctx.src0_dim2_div = kparams->div_ne02;
|
||||
bctx.src0_dim12_div = kparams->div_ne012;
|
||||
|
||||
bctx.src1_dim1_div = kparams->div_ne11;
|
||||
bctx.src1_dim2_div = kparams->div_ne12;
|
||||
bctx.src1_dim3_div = kparams->div_ne13;
|
||||
|
||||
bctx.split_at_ne01 = kparams->split_at_ne01;
|
||||
bctx.split_at_ne02 = kparams->split_at_ne02;
|
||||
bctx.split_at_ne01 = (kparams->flags & HTP_BINARY_FLAG_SPLIT_NE01) != 0;
|
||||
bctx.split_at_ne02 = (kparams->flags & HTP_BINARY_FLAG_SPLIT_NE02) != 0;
|
||||
bctx.compute = compute_func;
|
||||
|
||||
if (kparams->kernel_type == HTP_BINARY_KERNEL_ROW_BCAST ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_SCALAR ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_REPEAT ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_COMPLEX ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_ADD_ID) {
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_ADD_ID ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_MUL_ID ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_MUL_ID_SCALAR) {
|
||||
dma_queue_flush(dma_q);
|
||||
if ((kparams->kernel_type == HTP_BINARY_KERNEL_ROW_BCAST || kparams->kernel_type == HTP_BINARY_KERNEL_SCALAR) &&
|
||||
if ((kparams->kernel_type == HTP_BINARY_KERNEL_ROW_BCAST ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_SCALAR ||
|
||||
kparams->kernel_type == HTP_BINARY_KERNEL_MUL_ID_SCALAR) &&
|
||||
type_key != 0 && src1_type == HTP_TYPE_F16) {
|
||||
binary_expand_f16_to_f32(vtcm_src1, (uint32_t) (src1->ne[0] * src1->ne[1] * src1->ne[2] * src1->ne[3]));
|
||||
}
|
||||
|
||||
@@ -15,24 +15,28 @@ enum htp_binary_kernel_type {
|
||||
HTP_BINARY_KERNEL_ROW_BCAST,
|
||||
HTP_BINARY_KERNEL_SCALAR,
|
||||
HTP_BINARY_KERNEL_ADD_ID,
|
||||
HTP_BINARY_KERNEL_MUL_ID,
|
||||
HTP_BINARY_KERNEL_MUL_ID_SCALAR,
|
||||
HTP_BINARY_KERNEL_COMPLEX,
|
||||
HTP_BINARY_KERNEL_REPEAT,
|
||||
HTP_BINARY_KERNEL_CHUNKED,
|
||||
};
|
||||
|
||||
enum htp_binary_flags {
|
||||
HTP_BINARY_FLAG_SPLIT_NE01 = (1u << 0),
|
||||
HTP_BINARY_FLAG_SPLIT_NE02 = (1u << 1),
|
||||
HTP_BINARY_FLAG_SRC1_STRIDED = (1u << 2),
|
||||
HTP_BINARY_FLAG_SCALAR = (1u << 3),
|
||||
};
|
||||
|
||||
struct htp_binary_kernel_params {
|
||||
uint8_t kernel_type;
|
||||
uint8_t split_at_ne01;
|
||||
uint8_t split_at_ne02;
|
||||
uint8_t src1_strided;
|
||||
|
||||
uint8_t flags; // bitmask of enum htp_binary_flags
|
||||
uint8_t n_srcs;
|
||||
uint8_t is_scalar;
|
||||
uint8_t src0_elem_size;
|
||||
uint8_t src1_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;
|
||||
@@ -46,10 +50,8 @@ struct htp_binary_kernel_params {
|
||||
uint32_t vtcm_size;
|
||||
|
||||
uint32_t chunk_size;
|
||||
uint32_t chunk_bytes;
|
||||
|
||||
struct fastdiv_values div_ne01;
|
||||
struct fastdiv_values div_ne02;
|
||||
struct fastdiv_values div_ne012;
|
||||
|
||||
struct fastdiv_values div_ne11;
|
||||
@@ -127,19 +129,21 @@ static inline void htp_binary_vtcm_layout_build(
|
||||
return;
|
||||
}
|
||||
|
||||
const bool is_scalar = (kparams->flags & HTP_BINARY_FLAG_SCALAR) != 0;
|
||||
|
||||
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->src1_bytes_per_hvx = is_scalar ? 0 : (2 * (n_srcs - 1) * src1_chunk_bytes);
|
||||
L->dst_bytes_per_hvx = 2 * dst_chunk_bytes;
|
||||
|
||||
L->src0_spad_half_size = src0_chunk_bytes;
|
||||
L->src1_spad_half_size = kparams->is_scalar ? 0 : src1_chunk_bytes;
|
||||
L->src1_spad_half_size = is_scalar ? 0 : src1_chunk_bytes;
|
||||
L->dst_spad_half_size = dst_chunk_bytes;
|
||||
|
||||
L->rows_per_buffer = 1;
|
||||
L->src1_size = 0;
|
||||
|
||||
const size_t src0_total = n_hvx * L->src0_bytes_per_hvx;
|
||||
const size_t src1_total = kparams->is_scalar ? 128 : (n_hvx * L->src1_bytes_per_hvx);
|
||||
const size_t src1_total = is_scalar ? 128 : (n_hvx * L->src1_bytes_per_hvx);
|
||||
const size_t dst_total = n_hvx * L->dst_bytes_per_hvx;
|
||||
|
||||
size_t off = 0;
|
||||
@@ -234,7 +238,6 @@ static inline bool htp_binary_solve_chunked(
|
||||
const uint32_t chunk_size = (target_chunk_elems < min_chunk_elems) ? min_chunk_elems
|
||||
: (target_chunk_elems > max_chunk_elems) ? max_chunk_elems
|
||||
: target_chunk_elems;
|
||||
const uint32_t chunk_bytes = hex_round_up(chunk_size * elem_size, 128);
|
||||
const uint32_t total_chunks = (total_elems + chunk_size - 1) / chunk_size;
|
||||
const uint32_t n_hvx = (total_chunks >= 2) ? (max_hvx < total_chunks ? max_hvx : total_chunks) : 1;
|
||||
|
||||
@@ -242,14 +245,16 @@ static inline bool htp_binary_solve_chunked(
|
||||
kparams->n_hvx = n_hvx;
|
||||
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;
|
||||
kparams->is_scalar = is_scalar ? 1 : 0;
|
||||
if (is_scalar) {
|
||||
kparams->flags |= HTP_BINARY_FLAG_SCALAR;
|
||||
} else {
|
||||
kparams->flags &= ~HTP_BINARY_FLAG_SCALAR;
|
||||
}
|
||||
|
||||
htp_binary_vtcm_layout_build(L, kparams, vtcm_size);
|
||||
if (L->total_bytes == 0 || L->total_bytes > vtcm_size) {
|
||||
|
||||
@@ -779,9 +779,8 @@ static int concat_transposed(struct htp_ops_context * octx, const struct htp_con
|
||||
rstart1 = range1.start;
|
||||
nrows1 = range1.count;
|
||||
}
|
||||
const struct fastdiv_values div_ne11 = init_fastdiv_values(src1->ne[1]);
|
||||
const struct fastdiv_values div_ne12_ne11 = init_fastdiv_values(src1->ne[1] * src1->ne[2]);
|
||||
concat_regular_side(octx, &view1, src1, rstart1, nrows1, &div_ne11, &div_ne12_ne11);
|
||||
concat_regular_side(octx, &view1, src1, rstart1, nrows1,
|
||||
&kparams->u.transposed.div_ne11, &kparams->u.transposed.div_ne12_ne11);
|
||||
}
|
||||
|
||||
return HTP_STATUS_OK;
|
||||
|
||||
@@ -25,6 +25,8 @@ struct htp_concat_regular_params {
|
||||
|
||||
struct htp_concat_transposed_params {
|
||||
struct fastdiv_values div_ne2;
|
||||
struct fastdiv_values div_ne11;
|
||||
struct fastdiv_values div_ne12_ne11;
|
||||
};
|
||||
|
||||
struct htp_concat_kernel_params {
|
||||
|
||||
@@ -455,7 +455,7 @@ static uint32_t conv2d_hmx_issue_seg(const struct htp_ops_context * octx, const
|
||||
const uint32_t n = fastdiv(row, &kp->tiled.div_oh);
|
||||
const uint32_t oh = row - n * OH;
|
||||
|
||||
if (kp->cwhn) {
|
||||
if (kp->flags & HTP_CONV2D_FLAG_CWHN) {
|
||||
conv2d_hmx_stage_cwhn(octx, kp, dmaq, stage, n, oh, owt * 32, G);
|
||||
} else {
|
||||
conv2d_hmx_stage(octx, kp, g, dmaq, stage, n, oh, owt * 32, G);
|
||||
@@ -813,7 +813,7 @@ static void conv2d_hmx_pack(const struct htp_conv2d_hmx_context * hc, const htp_
|
||||
|
||||
// Reuse output staging after unpack has drained its DMA.
|
||||
HVX_Vector * gather_tmp = (HVX_Vector *) (thr + (size_t) n_stage * kp->stage_size);
|
||||
const HVX_Vector offsets = kp->cwhn || s0 <= 2 ? Q6_V_vzero() : hvx_vec_gather_offsets_w(s0 * sizeof(float));
|
||||
const HVX_Vector offsets = (kp->flags & HTP_CONV2D_FLAG_CWHN) || s0 <= 2 ? Q6_V_vzero() : hvx_vec_gather_offsets_w(s0 * sizeof(float));
|
||||
|
||||
uint32_t ng[2] = { 0, 0 };
|
||||
uint32_t tq = t0;
|
||||
@@ -832,7 +832,7 @@ static void conv2d_hmx_pack(const struct htp_conv2d_hmx_context * hc, const htp_
|
||||
}
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) t);
|
||||
if (kp->cwhn) {
|
||||
if (kp->flags & HTP_CONV2D_FLAG_CWHN) {
|
||||
const uint32_t Wb = 32 * G * s0 + (KW - 1) * d0;
|
||||
conv2d_hmx_cwhn_f16(stage16, stage, KH, kp->tiled.stage_row, Wb * Cin);
|
||||
for (uint32_t g = 0; g < G; g++) {
|
||||
@@ -1020,7 +1020,7 @@ static int conv2d_hmx_tiled(struct htp_ops_context * octx, const struct htp_conv
|
||||
for (uint32_t rt0 = 0; rt0 < kp->n_row_tiles; rt0 += kp->rt_block) {
|
||||
hc.rt0 = rt0;
|
||||
hc.n_rt = MIN(kp->rt_block, kp->n_row_tiles - rt0);
|
||||
work_queue_run(octx->ctx->hvx_queue, kp->w_kmajor ? conv2d_hmx_wpack_kmajor_thread : conv2d_hmx_wpack_thread, &hc, nth);
|
||||
work_queue_run(octx->ctx->hvx_queue, (kp->flags & HTP_CONV2D_FLAG_W_KMAJOR) ? conv2d_hmx_wpack_kmajor_thread : conv2d_hmx_wpack_thread, &hc, nth);
|
||||
|
||||
conv2d_hmx_run_phase(&hc, 0, -1, nth);
|
||||
|
||||
@@ -1550,7 +1550,7 @@ static void conv2d_hmx_win_wpack_rows(const struct htp_conv2d_win_context * wc,
|
||||
static void conv2d_hmx_win_wpack_thread(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
|
||||
const struct htp_conv2d_win_context * wc = (const struct htp_conv2d_win_context *) data;
|
||||
|
||||
if (wc->kp->w_kmajor) {
|
||||
if (wc->kp->flags & HTP_CONV2D_FLAG_W_KMAJOR) {
|
||||
conv2d_hmx_win_wpack_kmajor(wc, th, nth, ith);
|
||||
} else {
|
||||
conv2d_hmx_win_wpack_rows(wc, th, nth, ith);
|
||||
@@ -1573,7 +1573,7 @@ static uint32_t conv2d_hmx_win_stage(const struct htp_conv2d_win_context * wc, d
|
||||
const uint32_t p0 = octx->op_params[2];
|
||||
const uint32_t p1 = octx->op_params[3];
|
||||
const uint32_t span = kp->window.seg_b * 32;
|
||||
const bool cwhn = kp->cwhn;
|
||||
const bool cwhn = (kp->flags & HTP_CONV2D_FLAG_CWHN) != 0;
|
||||
|
||||
// rows of one image are contiguous when there is no column padding
|
||||
const bool rows_contig = p0 == 0 && input->nb[1] == (size_t) IW * input->nb[0];
|
||||
@@ -1693,7 +1693,7 @@ static uint32_t conv2d_hmx_win_stage_fold(const struct htp_conv2d_win_context *
|
||||
// rows of one image are contiguous in DDR
|
||||
const uint32_t nr = cont ? MIN(vr_b - vr, (uint32_t) (IH - ih)) : 1;
|
||||
const dma_addr_t src = input->data + (size_t) n * input->nb[3] + (size_t) ih * input->nb[1];
|
||||
if (kp->cwhn) {
|
||||
if (kp->flags & HTP_CONV2D_FLAG_CWHN) {
|
||||
// raw[vr - vr_a][iw][c]
|
||||
const size_t sz = (size_t) nr * IW * Cin * sizeof(float);
|
||||
dma_queue_push(dmaq, dma_make_data(raw + (size_t) (vr - vr_a) * IW * Cin, src), sz, sz, sz, 1);
|
||||
@@ -1734,7 +1734,7 @@ static void conv2d_hmx_win_croutons(const struct htp_conv2d_win_context * wc, co
|
||||
return;
|
||||
}
|
||||
|
||||
if (kp->cwhn) {
|
||||
if (kp->flags & HTP_CONV2D_FLAG_CWHN) {
|
||||
for (uint32_t cc = 0; cc < n_cc; cc++) {
|
||||
const uint32_t nc = MIN(32, Cin - cc * 32);
|
||||
const HVX_VectorPred qc = Q6_Q_vsetq2_R(nc * sizeof(float));
|
||||
@@ -2040,7 +2040,7 @@ static void conv2d_hmx_win_pack(const struct htp_conv2d_win_context * wc, const
|
||||
}
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) b);
|
||||
if (kp->window.fold && kp->cwhn) {
|
||||
if (kp->window.fold && (kp->flags & HTP_CONV2D_FLAG_CWHN)) {
|
||||
conv2d_hmx_win_croutons_fold_cwhn(wc, stages[si], act, b, nb, (ts + b) * 32);
|
||||
} else if (kp->window.fold) {
|
||||
conv2d_hmx_win_croutons_fold(wc, stages[si], act, b, nb, (ts + b) * 32);
|
||||
@@ -2287,7 +2287,7 @@ static int conv2d_hmx_win(struct htp_ops_context * octx, const struct htp_conv2d
|
||||
|
||||
// small Cin: croutons are built by scattering the staged values into zeroed croutons
|
||||
const uint32_t n_ch = octx->src[1]->ne[2] * (kp->window.fold ? KW : 1);
|
||||
if (!kp->cwhn && n_ch <= 2 * CONV2D_WIN_SCAT_MAX) {
|
||||
if (!(kp->flags & HTP_CONV2D_FLAG_CWHN) && n_ch <= 2 * CONV2D_WIN_SCAT_MAX) {
|
||||
wctx.n_scat = (n_ch + 1) / 2;
|
||||
work_queue_run(octx->ctx->hvx_queue, conv2d_hmx_win_zero_thread, &wctx, kp->n_hvx);
|
||||
}
|
||||
@@ -2395,7 +2395,7 @@ static inline void conv2d_dw_get_slice(
|
||||
uint32_t n;
|
||||
uint32_t c = 0;
|
||||
|
||||
if (kp->cwhn) {
|
||||
if (kp->flags & HTP_CONV2D_DW_FLAG_CWHN) {
|
||||
n = fastdiv(task, &kp->div_n_chunks);
|
||||
chunk_idx = task - n * kp->n_chunks;
|
||||
} else {
|
||||
@@ -2428,7 +2428,7 @@ static inline void conv2d_dw_get_slice(
|
||||
slice->ih_first = ih_first;
|
||||
slice->c = c;
|
||||
|
||||
if (kp->cwhn) {
|
||||
if (kp->flags & HTP_CONV2D_DW_FLAG_CWHN) {
|
||||
slice->src_addr = src1->data + (size_t) n * src1->nb[3] + (size_t) ih_first * src1->nb[1];
|
||||
slice->dst_addr = dst->data + (size_t) n * dst->nb[3] + (size_t) oh_start * dst->nb[1];
|
||||
} else {
|
||||
@@ -3201,7 +3201,7 @@ static void conv2d_dw_dma_thread(const htp_thread_t * th, unsigned int nth, unsi
|
||||
dma_queue_flush(dma_q);
|
||||
|
||||
FARF(HIGH, "conv2d_dw-f32-dma %d/%d: %ux%ux%ux%u -> %ux%ux%ux%u (%u:%u)\n",
|
||||
ith, nth, kp->w, kp->h, kp->c, kp->n, kp->ow, kp->oh, kp->c, kp->n, first, last);
|
||||
ith, nth, kp->w, kp->h, kp->c, dst->ne[3], kp->ow, kp->oh, kp->c, dst->ne[3], first, last);
|
||||
}
|
||||
|
||||
int op_conv_2d_dw(struct htp_ops_context * octx) {
|
||||
@@ -3223,7 +3223,8 @@ int op_conv_2d_dw(struct htp_ops_context * octx) {
|
||||
const uint32_t max_dst_bytes = kp->oh_chunk * kp->row_dst_bytes + 128;
|
||||
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const uint32_t elem_size = kp->is_f16 ? sizeof(__fp16) : sizeof(float);
|
||||
const bool is_f16 = (kp->flags & HTP_CONV2D_DW_FLAG_F16) != 0;
|
||||
const uint32_t elem_size = is_f16 ? sizeof(__fp16) : sizeof(float);
|
||||
const uint32_t w_size = src0->size;
|
||||
|
||||
struct htp_conv2d_dw_vtcm_layout layout;
|
||||
@@ -3262,7 +3263,7 @@ int op_conv_2d_dw(struct htp_ops_context * octx) {
|
||||
.kp = kp,
|
||||
.layout = layout,
|
||||
.w_vtcm = w_vtcm,
|
||||
.compute_fn = conv2d_dw_kernels[kp->kernel_type][kp->is_f16 ? 1 : 0],
|
||||
.compute_fn = conv2d_dw_kernels[kp->kernel_type][is_f16 ? 1 : 0],
|
||||
.task_start = task_start,
|
||||
.task_count = task_count,
|
||||
.tasks_per_hvx = tasks_per_hvx,
|
||||
|
||||
@@ -58,11 +58,15 @@ struct htp_conv2d_window_params {
|
||||
struct fastdiv_values div_cs;
|
||||
};
|
||||
|
||||
enum htp_conv2d_flags {
|
||||
HTP_CONV2D_FLAG_CWHN = (1u << 0),
|
||||
HTP_CONV2D_FLAG_W_KMAJOR = (1u << 1),
|
||||
};
|
||||
|
||||
struct htp_conv2d_kernel_params {
|
||||
uint8_t kernel_type;
|
||||
uint8_t flags; // bitmask of enum htp_conv2d_flags
|
||||
uint8_t n_hvx;
|
||||
uint8_t cwhn;
|
||||
uint8_t w_kmajor;
|
||||
|
||||
uint32_t total_rows;
|
||||
uint32_t n_row_tiles;
|
||||
@@ -96,18 +100,21 @@ enum htp_conv2d_dw_kernel_type {
|
||||
HTP_CONV2D_DW_KERNEL_COUNT,
|
||||
};
|
||||
|
||||
enum htp_conv2d_dw_flags {
|
||||
HTP_CONV2D_DW_FLAG_CWHN = (1u << 0),
|
||||
HTP_CONV2D_DW_FLAG_F16 = (1u << 1),
|
||||
};
|
||||
|
||||
struct htp_conv2d_dw_kernel_params {
|
||||
uint8_t kernel_type;
|
||||
uint8_t flags; // bitmask of enum htp_conv2d_dw_flags
|
||||
uint8_t n_hvx;
|
||||
uint8_t cwhn;
|
||||
uint8_t is_f16;
|
||||
|
||||
uint32_t total_tasks;
|
||||
uint32_t vtcm_size;
|
||||
uint32_t w;
|
||||
uint32_t h;
|
||||
uint32_t c;
|
||||
uint32_t n;
|
||||
uint32_t kw;
|
||||
uint32_t kh;
|
||||
uint32_t ow;
|
||||
|
||||
@@ -245,12 +245,7 @@ static int op_diag_impl(struct htp_ops_context * octx, size_t elem_size, dma_pip
|
||||
}
|
||||
|
||||
int op_diag(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
if (src0->type != dst->type) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
switch (dst->type) {
|
||||
case HTP_TYPE_F32:
|
||||
|
||||
@@ -569,6 +569,10 @@ int op_dsv4_hc_pre(struct htp_ops_context * octx) {
|
||||
const struct htp_dsv4_hc_pre_kernel_params * kparams =
|
||||
(const struct htp_dsv4_hc_pre_kernel_params *) octx->kernel_params;
|
||||
|
||||
if (kparams->total_jobs == 0) {
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
const uint32_t n_hvx = kparams->n_hvx;
|
||||
if (!htp_ops_context_set_n_hvx_div(octx, n_hvx, kparams->div_n_hvx)) {
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
@@ -1019,6 +1023,11 @@ int op_dsv4_hc_post(struct htp_ops_context * octx) {
|
||||
|
||||
const struct htp_dsv4_hc_post_kernel_params * kparams =
|
||||
(const struct htp_dsv4_hc_post_kernel_params *) octx->kernel_params;
|
||||
|
||||
if (kparams->total_jobs == 0) {
|
||||
return HTP_STATUS_OK;
|
||||
}
|
||||
|
||||
const struct htp_tensor * comb = kparams->has_comb ? octx->src[3] : NULL;
|
||||
|
||||
const uint32_t n_hvx = kparams->n_hvx;
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
#include "flash-attn-ops.h"
|
||||
#include "hvx-fa-kernels.h"
|
||||
#include "hmx-fa-kernels.h"
|
||||
#include "hmx-fa-fp16-kernels.h"
|
||||
#include "hmx-fa-w8a8-kernels.h"
|
||||
|
||||
// Must be multiple of 32
|
||||
@@ -3382,9 +3382,9 @@ int hmx_flash_attn_ext(struct htp_ops_context * octx) {
|
||||
factx.n_kv_blocks = kparams->n_kv_blocks;
|
||||
factx.is_q_fp32 = (q->type == HTP_TYPE_F32);
|
||||
factx.is_dst_fp32 = (dst->type == HTP_TYPE_F32);
|
||||
factx.pipeline = (kparams->u.hmx.pipeline != 0);
|
||||
factx.pipeline = (kparams->flags & HTP_FA_FLAG_PIPELINE) != 0;
|
||||
factx.is_w8a8 = (kparams->kernel_type == HTP_FA_KERNEL_HMX_W8A8);
|
||||
factx.mask_broadcast = (kparams->u.hmx.mask_broadcast != 0);
|
||||
factx.mask_broadcast = (kparams->flags & HTP_FA_FLAG_MASK_BCAST) != 0;
|
||||
if (mask) {
|
||||
factx.src3_div2 = kparams->src3_div2;
|
||||
factx.src3_div3 = kparams->src3_div3;
|
||||
@@ -3425,7 +3425,7 @@ int hmx_flash_attn_ext(struct htp_ops_context * octx) {
|
||||
if (kparams->flags & HTP_FA_FLAG_TEAM_SPLIT) {
|
||||
// KV heads split across teams, Q-blocks split within a team
|
||||
uint32_t n_teams, team_size;
|
||||
htp_fa_mdev_teams(n_kv_heads, mdev_count, kparams->head_split != 0, &n_teams, &team_size);
|
||||
htp_fa_mdev_teams(n_kv_heads, mdev_count, (kparams->flags & HTP_FA_FLAG_HEAD_SPLIT) != 0, &n_teams, &team_size);
|
||||
|
||||
const uint32_t team = mdev_idx / team_size;
|
||||
const uint32_t member = mdev_idx - team * team_size;
|
||||
@@ -3444,7 +3444,7 @@ int hmx_flash_attn_ext(struct htp_ops_context * octx) {
|
||||
|
||||
q_start_min = b0 * Br;
|
||||
q_start_max = MIN((b0 + nblk) * Br, neq1);
|
||||
} else if (kparams->head_split && n_kv_heads >= mdev_count && n_kv_heads % mdev_count == 0) {
|
||||
} else if ((kparams->flags & HTP_FA_FLAG_HEAD_SPLIT) && n_kv_heads >= mdev_count && n_kv_heads % mdev_count == 0) {
|
||||
const uint32_t kv_per_core = n_kv_heads / mdev_count;
|
||||
kv_head_min = mdev_idx * kv_per_core;
|
||||
kv_head_max = kv_head_min + kv_per_core;
|
||||
@@ -4097,19 +4097,8 @@ int op_flash_attn_ext(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * v = octx->src[2];
|
||||
const struct htp_tensor * mask = octx->src[3];
|
||||
|
||||
// Check support
|
||||
if ((q->type != HTP_TYPE_F16 && q->type != HTP_TYPE_F32) ||
|
||||
(k->type != HTP_TYPE_F16 && k->type != HTP_TYPE_Q8_0) ||
|
||||
(v->type != HTP_TYPE_F16 && v->type != HTP_TYPE_Q8_0)) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const struct htp_fa_kernel_params * kparams = (const struct htp_fa_kernel_params *) octx->kernel_params;
|
||||
|
||||
if (kparams->kernel_type == HTP_FA_KERNEL_UNSUPPORTED || kparams->n_kv_blocks > FA_MAX_KV_BLOCKS) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
if (!htp_ops_context_set_n_hvx(octx, kparams->n_hvx)) {
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
@@ -4156,9 +4145,6 @@ int op_flash_attn_ext(struct htp_ops_context * octx) {
|
||||
factx.m1 = kparams->m1;
|
||||
|
||||
const uint32_t n_head = q->ne[2];
|
||||
if (n_head > 512) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
factx.ddr = (struct hvx_fa_ddr_spad *) fa_get_ddr_spad(octx, sizeof(struct hvx_fa_ddr_spad));
|
||||
if (!factx.ddr) {
|
||||
@@ -4187,7 +4173,7 @@ int op_flash_attn_ext(struct htp_ops_context * octx) {
|
||||
const uint32_t n_kv_heads = k->ne[2];
|
||||
|
||||
// head range is contiguous in flat row space only when neq3 == 1
|
||||
if (kparams->head_split && neq3 == 1 && n_kv_heads >= mdev_count && n_kv_heads % mdev_count == 0) {
|
||||
if ((kparams->flags & HTP_FA_FLAG_HEAD_SPLIT) && neq3 == 1 && n_kv_heads >= mdev_count && n_kv_heads % mdev_count == 0) {
|
||||
const uint32_t G = kparams->G;
|
||||
const uint32_t kv_per_core = n_kv_heads / mdev_count;
|
||||
const uint32_t heads_per_core = kv_per_core * G;
|
||||
|
||||
@@ -40,8 +40,12 @@ enum htp_fa_kernel_type {
|
||||
HTP_FA_KERNEL_HMX_W8A8,
|
||||
};
|
||||
|
||||
// Multicore: gcd(n_kv_heads, N) teams own the KV heads, team members share the Q-blocks
|
||||
#define HTP_FA_FLAG_TEAM_SPLIT 0x01
|
||||
enum htp_fa_flags {
|
||||
HTP_FA_FLAG_TEAM_SPLIT = (1u << 0), // gcd(n_kv_heads, N) teams own the KV heads, team members share the Q-blocks
|
||||
HTP_FA_FLAG_HEAD_SPLIT = (1u << 1), // partition by KV heads in multicore (0 = token partition)
|
||||
HTP_FA_FLAG_MASK_BCAST = (1u << 2), // mask broadcast across heads
|
||||
HTP_FA_FLAG_PIPELINE = (1u << 3), // pipelined HMX execution
|
||||
};
|
||||
|
||||
static inline void htp_fa_mdev_teams(uint32_t n_kv_heads, uint32_t mdev_count, bool head_split,
|
||||
uint32_t * n_teams, uint32_t * team_size) {
|
||||
@@ -52,8 +56,7 @@ static inline void htp_fa_mdev_teams(uint32_t n_kv_heads, uint32_t mdev_count, b
|
||||
|
||||
struct htp_fa_kernel_params {
|
||||
uint8_t kernel_type; // enum htp_fa_kernel_type
|
||||
uint8_t head_split; // 1 = partition by KV heads in multicore, 0 = token partition
|
||||
uint8_t flags; // HTP_FA_FLAG_*
|
||||
uint8_t flags; // bitmask of enum htp_fa_flags
|
||||
uint8_t n_hvx; // Number of threads to run
|
||||
|
||||
// Common parameters
|
||||
@@ -82,8 +85,6 @@ struct htp_fa_kernel_params {
|
||||
union {
|
||||
struct {
|
||||
uint32_t g_br;
|
||||
int32_t mask_broadcast;
|
||||
int32_t pipeline;
|
||||
struct fastdiv_values div_G;
|
||||
} hmx;
|
||||
struct {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#include "htp-tensor.h"
|
||||
#include "htp-vtcm.h"
|
||||
#include "hmx-utils.h"
|
||||
#include "hmx-fa-kernels.h"
|
||||
#include "hmx-fa-fp16-kernels.h"
|
||||
#include "hmx-queue.h"
|
||||
#include "gated-delta-net-ops.h"
|
||||
|
||||
|
||||
@@ -464,22 +464,6 @@ 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);
|
||||
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;
|
||||
}
|
||||
|
||||
if (kparams->kernel_type == HTP_GET_ROWS_KERNEL_SAMETYPE) {
|
||||
if (octx->src[0]->type != octx->dst->type) {
|
||||
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 uint32_t total_tasks = kparams->total_tasks;
|
||||
|
||||
|
||||
@@ -58,12 +58,12 @@ static inline void htp_glu_vtcm_layout_build(
|
||||
const size_t row_size_aligned = hex_round_up(dst_row_size, 128);
|
||||
|
||||
L->row_size_aligned = row_size_aligned;
|
||||
L->vtcm_row_per_hvx = vtcm_row_per_hvx;
|
||||
L->block = vtcm_row_per_hvx >> 1;
|
||||
L->vtcm_row_per_hvx = 2 * L->block;
|
||||
|
||||
L->src0_bytes_per_hvx = row_size_aligned * vtcm_row_per_hvx;
|
||||
L->src1_bytes_per_hvx = row_size_aligned * vtcm_row_per_hvx;
|
||||
L->dst_bytes_per_hvx = row_size_aligned * vtcm_row_per_hvx;
|
||||
L->src0_bytes_per_hvx = 2 * L->block * row_size_aligned;
|
||||
L->src1_bytes_per_hvx = 2 * L->block * row_size_aligned;
|
||||
L->dst_bytes_per_hvx = 2 * L->block * row_size_aligned;
|
||||
|
||||
size_t off = 0;
|
||||
VTCM_LAYOUT_ALLOC(off, off_src0, L->src0_bytes_per_hvx * n_hvx);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "hex-fastdiv.h"
|
||||
#include "hmx-int-utils.h"
|
||||
#include "hmx-w8a8-utils.h"
|
||||
#include "hmx-utils.h"
|
||||
#include "hvx-utils.h"
|
||||
|
||||
@@ -406,7 +406,7 @@ static inline HVX_Vector hmx_fa_w8a8_k_pack4(const uint8_t * p, size_t stride) {
|
||||
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.
|
||||
// Same math as hmx_w8a8_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,
|
||||
@@ -431,7 +431,7 @@ static inline void hmx_fa_w8a8_k_rescale_pair(
|
||||
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);
|
||||
hmx_w8a8_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).
|
||||
@@ -469,10 +469,10 @@ static __attribute__((noinline)) void hmx_fa_w8a8_k_prep_tile(
|
||||
}
|
||||
|
||||
HVX_Vector v_max_sf, v_inv127_qf;
|
||||
int_hmx_inv127_from_max_f16(v_max_sc, &v_max_sf, &v_inv127_qf);
|
||||
hmx_w8a8_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);
|
||||
tile_dst_all[kt * 8 + 7] = hmx_w8a8_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
|
||||
@@ -527,7 +527,7 @@ static __attribute__((noinline)) void hmx_fa_w8a8_k_prep_tile(
|
||||
}
|
||||
|
||||
// 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);
|
||||
hmx_w8a8_emit_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.
|
||||
@@ -657,7 +657,7 @@ static __attribute__((noinline)) void hmx_fa_w8a8_v_prep_tile(
|
||||
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);
|
||||
hmx_w8a8_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);
|
||||
@@ -700,7 +700,7 @@ static __attribute__((noinline)) void hmx_fa_w8a8_v_prep_tile(
|
||||
}
|
||||
|
||||
// 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);
|
||||
hmx_w8a8_emit_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(
|
||||
|
||||
+453
-424
File diff suppressed because it is too large
Load Diff
+1467
-322
File diff suppressed because it is too large
Load Diff
+19
-19
@@ -1,5 +1,5 @@
|
||||
#ifndef HMX_INT_UTILS_H
|
||||
#define HMX_INT_UTILS_H
|
||||
#ifndef HMX_W8A8_UTILS_H
|
||||
#define HMX_W8A8_UTILS_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -31,7 +31,7 @@ static inline HVX_Vector hmx_qf32_to_i32_zp128(HVX_Vector v_qf) {
|
||||
return Q6_Vw_equals_Vsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(v_qf, v_128p5)));
|
||||
}
|
||||
|
||||
static inline void emit_int_hmx_scales_and_bias(
|
||||
static inline void hmx_w8a8_emit_scales_and_bias(
|
||||
HVX_Vector v_max_sf,
|
||||
float scale_const,
|
||||
HVX_Vector v_sum_w,
|
||||
@@ -58,7 +58,7 @@ static inline void emit_int_hmx_scales_and_bias(
|
||||
bias_out[3] = v_ib;
|
||||
}
|
||||
|
||||
static inline void emit_int_hmx_scales_and_bias_zp0(
|
||||
static inline void hmx_w8a8_emit_scales_and_bias_zp0(
|
||||
HVX_Vector v_max_sf,
|
||||
float scale_const,
|
||||
HVX_Vector v_l1_w,
|
||||
@@ -84,51 +84,51 @@ static inline void emit_int_hmx_scales_and_bias_zp0(
|
||||
bias_out[3] = v_ib;
|
||||
}
|
||||
|
||||
static inline HVX_Vector int_hmx_scale_qf_from_max_f32(HVX_Vector v_max_sf, float num) {
|
||||
static inline HVX_Vector hmx_w8a8_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 HVX_Vector hmx_w8a8_inv127_from_max_f32(HVX_Vector v_max_sf) {
|
||||
return hmx_w8a8_scale_qf_from_max_f32(v_max_sf, 127.0f);
|
||||
}
|
||||
|
||||
static inline void int_hmx_inv127_from_max_f16(
|
||||
static inline void hmx_w8a8_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);
|
||||
*v_inv127_qf_out = hmx_w8a8_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) {
|
||||
static inline HVX_Vector hmx_w8a8_m_b4_from_sc_qf(HVX_Vector v_sc_qf, HVX_Vector v_inv127_qf) {
|
||||
HVX_Vector v_m_biased = hvx_vec_i32_from_qf32_rnd(Q6_Vqf32_vmpy_Vqf32Vqf32(v_sc_qf, v_inv127_qf));
|
||||
HVX_Vector v_m_h = Q6_Vh_vpacke_VwVw(v_m_biased, v_m_biased);
|
||||
return Q6_Vh_vshuff_Vh(Q6_Vb_vshuff_Vb(Q6_Vb_vpacke_VhVh(v_m_h, v_m_h)));
|
||||
}
|
||||
|
||||
static inline HVX_Vector int_hmx_m_b4_from_f16(HVX_Vector v_sc, HVX_Vector v_inv127_qf) {
|
||||
static inline HVX_Vector hmx_w8a8_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);
|
||||
return hmx_w8a8_m_b4_from_sc_qf(v_sc_qf, v_inv127_qf);
|
||||
}
|
||||
|
||||
static inline void int_hmx_m_b4_pair_from_f16(
|
||||
static inline void hmx_w8a8_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);
|
||||
*v_m_b0_out = hmx_w8a8_m_b4_from_sc_qf(Q6_V_lo_W(vp_sc_qf), v_inv127_qf);
|
||||
*v_m_b1_out = hmx_w8a8_m_b4_from_sc_qf(Q6_V_hi_W(vp_sc_qf), v_inv127_qf);
|
||||
}
|
||||
|
||||
static inline void int_hmx_store_and_accum_pair(
|
||||
static inline void hmx_w8a8_store_and_accum_pair(
|
||||
HVX_Vector * restrict tile_dst,
|
||||
int pair_idx,
|
||||
HVX_Vector v_W0,
|
||||
@@ -144,7 +144,7 @@ static inline void int_hmx_store_and_accum_pair(
|
||||
*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(
|
||||
static inline void hmx_w8a8_rescale_pair_wb(
|
||||
HVX_Vector v_q0,
|
||||
HVX_Vector v_q1,
|
||||
HVX_Vector v_m_b4,
|
||||
@@ -167,7 +167,7 @@ static inline void int_hmx_rescale_pair_wb(
|
||||
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);
|
||||
hmx_w8a8_store_and_accum_pair(tile_dst, pair_idx, v_W0, v_W1, v_sum_w, v_l1_w);
|
||||
}
|
||||
|
||||
#if __HVX_ARCH__ >= 81
|
||||
@@ -214,4 +214,4 @@ static inline void hmx_pack_hf_quad_to_u8_cm_pair(
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HMX_INT_UTILS_H */
|
||||
#endif /* HMX_W8A8_UTILS_H */
|
||||
@@ -36,8 +36,9 @@ enum htp_data_type {
|
||||
HTP_TYPE_BF16 = 30,
|
||||
HTP_TYPE_MXFP4 = 39,
|
||||
HTP_TYPE_NVFP4 = 40,
|
||||
HTP_TYPE_Q1_0 = 41,
|
||||
HTP_TYPE_Q2_0 = 42,
|
||||
HTP_TYPE_Q1_0 = 41,
|
||||
HTP_TYPE_Q2_0 = 42,
|
||||
HTP_TYPE_F8_E4M3 = 43,
|
||||
|
||||
HTP_TYPE_INVALID
|
||||
};
|
||||
@@ -130,6 +131,7 @@ enum htp_op_code {
|
||||
HTP_OP_SSM_SCAN,
|
||||
HTP_OP_TIMESTEP_EMBEDDING,
|
||||
HTP_OP_MUL_MAT_NX_GLU,
|
||||
HTP_OP_MUL_ID,
|
||||
HTP_OP_INVALID
|
||||
};
|
||||
|
||||
|
||||
@@ -12,8 +12,9 @@
|
||||
#define HTP_TENSOR_TILE_N_COLS 32
|
||||
#define HTP_TENSOR_TILE_N_ROWS 32
|
||||
|
||||
#define HTP_TENSOR_TILE_SIZE_F16 2048
|
||||
#define HTP_TENSOR_TILE_SIZE_Q4_0 576
|
||||
#define HTP_TENSOR_TILE_SIZE_F16 2048
|
||||
#define HTP_TENSOR_TILE_SIZE_F8_E4M3 1024
|
||||
#define HTP_TENSOR_TILE_SIZE_Q4_0 576
|
||||
#define HTP_TENSOR_TILE_SIZE_Q4_1 640
|
||||
#define HTP_TENSOR_TILE_SIZE_Q5_0 704
|
||||
#define HTP_TENSOR_TILE_SIZE_Q5_1 768
|
||||
@@ -36,7 +37,7 @@
|
||||
#define HTP_TENSOR_TILE_K_Q2_0 64
|
||||
#define HTP_TENSOR_TILE_SIZE_Q2_0 576
|
||||
|
||||
// Tiled weight tensor layouts (Q4_0, Q5_0, Q5_1, Q8_0, IQ4_NL, MXFP4, NVFP4, IQ2_XXS, Q1_0, Q2_0, K-quants):
|
||||
// Tiled weight tensor layouts (Q4_0, Q5_0, Q5_1, Q8_0, IQ4_NL, MXFP4, NVFP4, F8_E4M3, IQ2_XXS, Q1_0, Q2_0, K-quants):
|
||||
// See docs/backend/snapdragon/developer.md ("Tiled Weight Formats").
|
||||
|
||||
struct htp_tensor_mdev_range {
|
||||
@@ -115,8 +116,9 @@ static inline uint32_t htp_tensor_get_tile_size(int type) {
|
||||
case HTP_TYPE_MXFP4: return HTP_TENSOR_TILE_SIZE_MXFP4;
|
||||
case HTP_TYPE_NVFP4: return HTP_TENSOR_TILE_SIZE_NVFP4;
|
||||
case HTP_TYPE_IQ2_XXS: return HTP_TENSOR_TILE_SIZE_IQ2_XXS;
|
||||
case HTP_TYPE_F16: return HTP_TENSOR_TILE_SIZE_F16;
|
||||
default: return 0;
|
||||
case HTP_TYPE_F16: return HTP_TENSOR_TILE_SIZE_F16;
|
||||
case HTP_TYPE_F8_E4M3: return HTP_TENSOR_TILE_SIZE_F8_E4M3;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,6 +145,7 @@ static inline uint32_t htp_tensor_get_row_size(int type, uint32_t ne00) {
|
||||
case HTP_TYPE_F32: return ne00 * 4;
|
||||
case HTP_TYPE_F16:
|
||||
case HTP_TYPE_BF16: return ne00 * 2;
|
||||
case HTP_TYPE_F8_E4M3: return ne00;
|
||||
case HTP_TYPE_Q8_0: return (ne00 / 32) * 34;
|
||||
case HTP_TYPE_Q4_0:
|
||||
case HTP_TYPE_IQ4_NL: return (ne00 / 32) * 18;
|
||||
|
||||
@@ -166,6 +166,31 @@ static inline HVX_Vector hvx_vec_f32_to_f16(HVX_Vector v0, HVX_Vector v1) {
|
||||
return Q6_Vh_vdeal_Vh(hvx_vec_f32_to_f16_shuff(v0, v1));
|
||||
}
|
||||
|
||||
static inline HVX_Vector hvx_vec_f32_to_f16_clamped(HVX_Vector v0, HVX_Vector v1) {
|
||||
const HVX_Vector max_f16 = hvx_vec_splat_f32(65504.0f);
|
||||
const HVX_Vector min_f16 = hvx_vec_splat_f32(-65504.0f);
|
||||
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));
|
||||
return hvx_vec_f32_to_f16(v0, v1);
|
||||
}
|
||||
|
||||
// Multiply 64 F16 lanes to F32: lo = lanes 0..31, hi = lanes 32..63
|
||||
static inline HVX_VectorPair hvx_vec_mul_f16_f16_to_f32_pair(HVX_Vector v1, HVX_Vector v2) {
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_VectorPair p = Q6_Wsf_vmpy_VhfVhf(v1, v2);
|
||||
return Q6_W_vshuff_VVR(Q6_V_hi_W(p), Q6_V_lo_W(p), -4);
|
||||
#else
|
||||
HVX_VectorPair p = Q6_Wqf32_vmpy_VhfVhf(v1, v2);
|
||||
HVX_Vector hi = Q6_Vsf_equals_Vqf32(Q6_V_hi_W(p));
|
||||
HVX_Vector lo = Q6_Vsf_equals_Vqf32(Q6_V_lo_W(p));
|
||||
return Q6_W_vshuff_VVR(hi, lo, -4);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline HVX_Vector hvx_vec_mul_f16_f16_to_f32_lower32(HVX_Vector v1, HVX_Vector v2) {
|
||||
return Q6_V_lo_W(hvx_vec_mul_f16_f16_to_f32_pair(v1, v2));
|
||||
}
|
||||
|
||||
#if __HVX_ARCH__ >= 79
|
||||
static inline HVX_VectorPair hvx_vec_f16_to_f32_shuff(HVX_Vector v) {
|
||||
const HVX_Vector one = hvx_vec_splat_f16(1.0);
|
||||
|
||||
@@ -265,18 +265,6 @@ static void quantize_row_f32_q8_1_s16_tiled(float * restrict x, uint8_t * restri
|
||||
}
|
||||
}
|
||||
|
||||
static inline HVX_Vector hvx_vec_reduce_abs_max_half_f16(HVX_Vector v) {
|
||||
HVX_Vector m = hvx_vec_abs_f16(v);
|
||||
m = Q6_Vhf_vmax_VhfVhf(m, Q6_V_vror_VR(m, 2));
|
||||
m = Q6_Vhf_vmax_VhfVhf(m, Q6_V_vror_VR(m, 4));
|
||||
m = Q6_Vhf_vmax_VhfVhf(m, Q6_V_vror_VR(m, 8));
|
||||
m = Q6_Vhf_vmax_VhfVhf(m, Q6_V_vror_VR(m, 16));
|
||||
m = Q6_Vhf_vmax_VhfVhf(m, Q6_V_vror_VR(m, 32));
|
||||
HVX_Vector m_lo = hvx_vec_repl_f16(m);
|
||||
HVX_Vector m_hi = hvx_vec_repl_f16(Q6_V_vror_VR(m, 64));
|
||||
return Q6_V_vmux_QVV(Q6_Q_vsetq_R(64), m_lo, m_hi);
|
||||
}
|
||||
|
||||
__attribute__((always_inline))
|
||||
static inline void quantize_block_f16_q8_1_tiled_impl(const __fp16 * restrict x, uint8_t * restrict y_block, const bool sums16,
|
||||
const __fp16 * restrict x_sb) {
|
||||
@@ -581,41 +569,6 @@ static void quantize_row_f16_f16_tiled(const __fp16 * restrict x, uint8_t * rest
|
||||
|
||||
// Dot kernels & helpers that consume tiled activations
|
||||
|
||||
static inline HVX_Vector hvx_vec_mul_f16_f16_to_f32_lower32(HVX_Vector v1, HVX_Vector v2) {
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_VectorPair p = Q6_Wsf_vmpy_VhfVhf(v1, v2);
|
||||
return Q6_V_lo_W(Q6_W_vshuff_VVR(Q6_V_hi_W(p), Q6_V_lo_W(p), -4));
|
||||
#else
|
||||
HVX_VectorPair p = Q6_Wqf32_vmpy_VhfVhf(v1, v2);
|
||||
HVX_Vector hi = Q6_Vsf_equals_Vqf32(Q6_V_hi_W(p));
|
||||
HVX_Vector lo = Q6_Vsf_equals_Vqf32(Q6_V_lo_W(p));
|
||||
return Q6_V_lo_W(Q6_W_vshuff_VVR(hi, lo, -4));
|
||||
#endif
|
||||
}
|
||||
|
||||
// both halves of hvx_vec_mul_f16_f16_to_f32_lower32: lo = products of lanes 0..31, hi = lanes 32..63
|
||||
static inline HVX_VectorPair hvx_vec_mul_f16_f16_to_f32_pair(HVX_Vector v1, HVX_Vector v2) {
|
||||
#if __HVX_ARCH__ >= 79
|
||||
HVX_VectorPair p = Q6_Wsf_vmpy_VhfVhf(v1, v2);
|
||||
return Q6_W_vshuff_VVR(Q6_V_hi_W(p), Q6_V_lo_W(p), -4);
|
||||
#else
|
||||
HVX_VectorPair p = Q6_Wqf32_vmpy_VhfVhf(v1, v2);
|
||||
HVX_Vector hi = Q6_Vsf_equals_Vqf32(Q6_V_hi_W(p));
|
||||
HVX_Vector lo = Q6_Vsf_equals_Vqf32(Q6_V_lo_W(p));
|
||||
return Q6_W_vshuff_VVR(hi, lo, -4);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline HVX_Vector unpack_q1_0_group(
|
||||
HVX_Vector v_plane,
|
||||
int g,
|
||||
HVX_Vector v_neg_one,
|
||||
HVX_Vector v_pos_one
|
||||
) {
|
||||
HVX_VectorPred q = Q6_Q_vand_VR(v_plane, 0x01010101u << g);
|
||||
return Q6_V_vmux_QVV(q, v_pos_one, v_neg_one);
|
||||
}
|
||||
|
||||
static inline HVX_Vector accum_q1_0_32x1(
|
||||
HVX_Vector v_plane,
|
||||
const HVX_Vector * restrict v_act,
|
||||
@@ -785,12 +738,6 @@ static inline HVX_VectorPair accum_q8_0_32x2(
|
||||
return Q6_W_vcombine_VV(v_sum1, v_sum0);
|
||||
}
|
||||
|
||||
// Q5_K: OR 0x10 into every lane of v whose flag j is set in the plane (see HTP_TENSOR_TILE_SIZE_Q5_K)
|
||||
static inline HVX_Vector hvx_q5k_or_hibit(HVX_Vector v, HVX_Vector v_plane, int j) {
|
||||
HVX_VectorPred q = Q6_Q_vand_VR(v_plane, 0x01010101u << j);
|
||||
return Q6_V_vandor_VQR(v, q, 0x10101010);
|
||||
}
|
||||
|
||||
static inline HVX_Vector accum_5bit_32x1(
|
||||
const HVX_Vector * restrict vptr,
|
||||
const HVX_Vector * restrict v_act
|
||||
@@ -838,12 +785,6 @@ static inline HVX_VectorPair accum_5bit_32x2(
|
||||
return Q6_W_vcombine_VV(v_sum1, v_sum0);
|
||||
}
|
||||
|
||||
// OR 0xF0 into every lane of v whose flag j is set in the plane, i.e. 4-bit value | 0xF0 = q - 16 where the 5th bit is clear (Q5_0)
|
||||
static inline HVX_Vector hvx_q5_0_or_neg(HVX_Vector v, HVX_Vector v_plane, int j) {
|
||||
HVX_VectorPred q = Q6_Q_vand_VR(v_plane, 0x01010101u << j);
|
||||
return Q6_V_vandor_VQR(v, q, 0xF0F0F0F0);
|
||||
}
|
||||
|
||||
static inline HVX_Vector accum_q5_0_32x1(
|
||||
const HVX_Vector * restrict vptr,
|
||||
const HVX_Vector * restrict v_act
|
||||
@@ -891,14 +832,6 @@ static inline HVX_VectorPair accum_q5_0_32x2(
|
||||
return Q6_W_vcombine_VV(v_sum1, v_sum0);
|
||||
}
|
||||
|
||||
// Q6_K weights are stored unsigned (0..63), see HTP_TENSOR_TILE_SIZE_Q6_K. Unpack k-group g of a tile to signed bytes (q - 32)
|
||||
static inline HVX_Vector unpack_q6_k_group(const HVX_Vector * restrict vptr, int g, HVX_Vector mask_0f, HVX_Vector mask_03, HVX_Vector i32) {
|
||||
HVX_Vector v_lo = (g & 1) ? Q6_Vub_vlsr_VubR(vptr[g >> 1], 4) : Q6_V_vand_VV(vptr[g >> 1], mask_0f);
|
||||
HVX_Vector v_hi = (g & 3) ? Q6_Vub_vlsr_VubR(vptr[4 + (g >> 2)], 2 * (g & 3)) : vptr[4 + (g >> 2)];
|
||||
HVX_Vector v_q = Q6_V_vor_VV(v_lo, Q6_Vw_vasl_VwR(Q6_V_vand_VV(v_hi, mask_03), 4));
|
||||
return Q6_Vb_vsub_VbVb(v_q, i32);
|
||||
}
|
||||
|
||||
// k 0..15 and k 16..31 of a Q6_K tile have different scales: lo half of the pair sums k 0..15, hi half sums k 16..31
|
||||
static inline HVX_VectorPair accum_q6_k_32x1(
|
||||
const HVX_Vector * restrict vptr,
|
||||
@@ -958,24 +891,6 @@ static inline HVX_Vector scale_q6_k_32x1(HVX_VectorPair v_sums, HVX_Vector v_sca
|
||||
return hvx_vec_add_f32_f32(v_lo, v_hi);
|
||||
}
|
||||
|
||||
// Q3_K / Q2_K: low 2 bits of k-group g, see HTP_TENSOR_TILE_SIZE_Q3_K
|
||||
static inline HVX_Vector unpack_q3_k_low2(const HVX_Vector * restrict vptr, int g, HVX_Vector mask_03) {
|
||||
HVX_Vector v = vptr[g >> 2];
|
||||
if ((g & 3) == 3) {
|
||||
return Q6_Vub_vlsr_VubR(v, 6);
|
||||
}
|
||||
if (g & 3) {
|
||||
v = Q6_Vub_vlsr_VubR(v, 2 * (g & 3));
|
||||
}
|
||||
return Q6_V_vand_VV(v, mask_03);
|
||||
}
|
||||
|
||||
// Q3_K k-group g as signed bytes: low2 | 0xFC (= low2 - 4) where bit g of vector 2 is set
|
||||
static inline HVX_Vector unpack_q3_k_group(const HVX_Vector * restrict vptr, int g, HVX_Vector mask_03) {
|
||||
HVX_VectorPred q_neg = Q6_Q_vand_VR(vptr[2], 0x01010101u << g);
|
||||
return Q6_V_vandor_VQR(unpack_q3_k_low2(vptr, g, mask_03), q_neg, 0xFCFCFCFC);
|
||||
}
|
||||
|
||||
// same half split as accum_q6_k_32x1
|
||||
static inline HVX_VectorPair accum_q3_k_32x1(
|
||||
const HVX_Vector * restrict vptr,
|
||||
@@ -1117,33 +1032,6 @@ static inline void accum_q2_0_32x2(
|
||||
*v_sum1 = Q6_Vw_vadd_VwVw(v_sum1_lo, v_sum1_hi);
|
||||
}
|
||||
|
||||
// Q2_K: fp16 d (*v_d) and -dmin (*v_m) of the 32 rows of a super-block (8 tiles, VTCM stride 384), each in both vector halves
|
||||
static inline void q2_k_load_dmin(const uint8_t * restrict sb, HVX_Vector * restrict v_d, HVX_Vector * restrict v_m) {
|
||||
HVX_Vector v_dd = Q6_V_vzero(); // lanes 0..31: d, lanes 32..63: -dmin
|
||||
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; j++) {
|
||||
HVX_Vector v = hvx_vmem(sb + j * 384 + 256); // the slice is at bytes 64..79
|
||||
v_dd = Q6_V_vmux_QVV(Q6_Q_vsetq_R(16 * j), v_dd, Q6_V_vror_VR(v, (64 - 16 * j) & 127));
|
||||
}
|
||||
|
||||
HVX_VectorPred q_lo = Q6_Q_vsetq_R(64);
|
||||
HVX_Vector v_r = Q6_V_vror_VR(v_dd, 64);
|
||||
*v_d = Q6_V_vmux_QVV(q_lo, v_dd, v_r);
|
||||
*v_m = Q6_V_vmux_QVV(q_lo, v_r, v_dd);
|
||||
}
|
||||
|
||||
// Q2_K: fp16 D = d * scale and M = -dmin * min of a tile from its scale bytes, in the lanes of the Q6_K scales
|
||||
static inline void q2_k_unpack_scales(const uint8_t * restrict tile, HVX_Vector v_d, HVX_Vector v_m,
|
||||
HVX_Vector * restrict v_D, HVX_Vector * restrict v_M) {
|
||||
HVX_Vector v_sc = hvx_vmem(tile + 256);
|
||||
HVX_Vector v_l = Q6_V_lo_W(Q6_Wuh_vunpack_Vub(Q6_V_vand_VV(v_sc, Q6_Vb_vsplat_R(0x0F))));
|
||||
HVX_Vector v_mn = Q6_V_lo_W(Q6_Wuh_vunpack_Vub(Q6_Vub_vlsr_VubR(v_sc, 4)));
|
||||
// qf32 product: rounds like GGML_FP32_TO_FP16(d * scale) (qf16 is off by 1 ulp for many values on v73)
|
||||
*v_D = Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(Q6_Vhf_equals_Vh(v_l), v_d));
|
||||
*v_M = Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(Q6_Vhf_equals_Vh(v_mn), v_m));
|
||||
}
|
||||
|
||||
// Q2_K scale and min nibbles of a tile as halfword pairs: word r = sub-block 1 << 16 | sub-block 0 of row r
|
||||
static inline void q2_k_scale_pairs(const uint8_t * restrict tile, HVX_Vector * restrict v_sc2, HVX_Vector * restrict v_mn2) {
|
||||
HVX_Vector v_h = Q6_Vh_vshuff_Vh(Q6_V_lo_W(Q6_Wuh_vunpack_Vub(hvx_vmem(tile + 256))));
|
||||
@@ -1213,6 +1101,40 @@ static __attribute__((noinline)) void hvx_tiled_store_row_f16_f16(void * restric
|
||||
hvx_vec_store_u(s, valid_rows * sizeof(__fp16), hvx_vec_f32_to_f16(v_sum, Q6_V_vzero()));
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_tiled_store_row_f32_scale(void * restrict s, uint32_t valid_rows, HVX_Vector v_sum, const void * restrict sz) {
|
||||
v_sum = hvx_vec_mul_f32_f32(v_sum, hvx_vmemu(sz));
|
||||
hvx_vec_store_u(s, valid_rows * sizeof(float), v_sum);
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_tiled_store_row_f16_scale(void * restrict s, uint32_t valid_rows, HVX_Vector v_sum, const void * restrict sz) {
|
||||
v_sum = hvx_vec_mul_f32_f32(v_sum, hvx_vmemu(sz));
|
||||
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, valid_rows * sizeof(__fp16), hvx_vec_f32_to_f16(v_sum, Q6_V_vzero()));
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_tiled_store_row_f32_scale_scalar(void * restrict s, uint32_t valid_rows, HVX_Vector v_sum, const void * restrict sz) {
|
||||
v_sum = hvx_vec_mul_f32_f32(v_sum, hvx_vec_repl4(hvx_vmemu(sz)));
|
||||
hvx_vec_store_u(s, valid_rows * sizeof(float), v_sum);
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void hvx_tiled_store_row_f16_scale_scalar(void * restrict s, uint32_t valid_rows, HVX_Vector v_sum, const void * restrict sz) {
|
||||
v_sum = hvx_vec_mul_f32_f32(v_sum, hvx_vec_repl4(hvx_vmemu(sz)));
|
||||
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, valid_rows * sizeof(__fp16), hvx_vec_f32_to_f16(v_sum, Q6_V_vzero()));
|
||||
}
|
||||
|
||||
static inline hvx_tiled_store_row_fn_t resolve_hvx_tiled_store_row_scaled_fn(uint32_t dst_elem_size, bool scalar_scale) {
|
||||
const bool dst_f16 = (dst_elem_size == sizeof(__fp16));
|
||||
if (dst_f16) {
|
||||
return scalar_scale ? hvx_tiled_store_row_f16_scale_scalar : hvx_tiled_store_row_f16_scale;
|
||||
}
|
||||
return scalar_scale ? hvx_tiled_store_row_f32_scale_scalar : hvx_tiled_store_row_f32_scale;
|
||||
}
|
||||
|
||||
static inline hvx_tiled_store_row_fn_t resolve_hvx_tiled_store_row_fn(uint32_t dst_elem_size, bool has_bias, uint32_t bias_elem_size) {
|
||||
const bool dst_f16 = (dst_elem_size == sizeof(__fp16));
|
||||
const bool bias_f16 = (bias_elem_size == sizeof(__fp16));
|
||||
@@ -1958,108 +1880,6 @@ static void tiled_vec_dot_iq4nl_32x2(const uint32_t n, void * restrict s0, void
|
||||
store_row(s1, valid_rows, v_sum_float_c1, sz1);
|
||||
}
|
||||
|
||||
#define IQ2XXS_GROUPS 4 // 8-element grid entries per tile row
|
||||
#define IQ2XXS_TILES_PER_SB 8 // 256 / 32
|
||||
#define IQ2XXS_CODE_SHIFT 1 // log2(2 B per grid code)
|
||||
#define IQ2XXS_WORD_BYTES 4
|
||||
#define IQ2XXS_IDX_BITS 8
|
||||
#define IQ2XXS_IDX_MASK 0xFF
|
||||
#define IQ2XXS_SIGN_BITS 7
|
||||
#define IQ2XXS_SIGN_MASK 0x7F
|
||||
#define IQ2XXS_PARITY_SHIFT 7
|
||||
#define IQ2XXS_SCALE_SHIFT 28
|
||||
#define IQ2XXS_D_SLICE_OFF 256 // 4 fp16 d of the tile, after the 32 rows x 8 B
|
||||
#define IQ2XXS_D_SCALE 0.125f // db = d * (2 * scale + 1) / 8
|
||||
#define IQ2XXS_D_SLICE_BYTES 8
|
||||
#define IQ2XXS_SIGN_PRED_LO 0x08040201 // sign bit of bytes 0..3 of an 8-element group
|
||||
#define IQ2XXS_SIGN_PRED_HI 0x80402010 // sign bit of bytes 4..7
|
||||
#define IQ2XXS_IDX_LO_MASK 0x1FE // index byte << 1, halfword 0 of a lane
|
||||
#define IQ2XXS_IDX_HI_MASK 0x1FE0000 // index byte << 1, halfword 1 of a lane
|
||||
#define IQ2XXS_NIB_LO_MASK 0x00000F0F // nibbles of the code bytes, lane bytes 0..1
|
||||
#define IQ2XXS_NIB_HI_MASK 0x0F0F0000 // nibbles of the code bytes, lane bytes 2..3
|
||||
|
||||
static inline HVX_Vector * iq2xxs_tile_scratch(const uint8_t * tile) {
|
||||
return (HVX_Vector *) (tile + htp_tensor_get_tile_padded_size(HTP_TYPE_IQ2_XXS) - HTP_TENSOR_TILE_SCRATCH_IQ2_XXS);
|
||||
}
|
||||
|
||||
// IQ2_XXS: start the 2 halfword grid-code gathers of a tile into its scratch, returns the sign/scale words
|
||||
static inline HVX_Vector iq2xxs_gather_issue(const uint8_t * restrict tile) {
|
||||
HVX_Vector * restrict v_scr = iq2xxs_tile_scratch(tile);
|
||||
const uint8_t * grid = iq2xxs_vtcm_lut + HTP_IQ2XXS_NIBBLE_BYTES;
|
||||
|
||||
// lo: 4 grid indices per row, hi: 4 sign indices + scale per row
|
||||
HVX_VectorPair p = Q6_W_vdeal_VVR(hvx_vmem(tile + VLEN), hvx_vmem(tile), -IQ2XXS_WORD_BYTES);
|
||||
HVX_Vector v_idx = Q6_V_lo_W(p);
|
||||
|
||||
// one halfword gather per 2 groups: lane i gets the codes of groups 2h and 2h+1 in halfwords 0 and 1
|
||||
HVX_Vector v_off0 = Q6_V_vor_VV(Q6_V_vand_VV(Q6_Vw_vasl_VwR(v_idx, IQ2XXS_CODE_SHIFT), Q6_V_vsplat_R(IQ2XXS_IDX_LO_MASK)),
|
||||
Q6_V_vand_VV(Q6_Vw_vasl_VwR(v_idx, IQ2XXS_CODE_SHIFT + IQ2XXS_IDX_BITS), Q6_V_vsplat_R(IQ2XXS_IDX_HI_MASK)));
|
||||
HVX_Vector v_off1 = Q6_V_vor_VV(Q6_V_vand_VV(Q6_Vuw_vlsr_VuwR(v_idx, 2 * IQ2XXS_IDX_BITS - IQ2XXS_CODE_SHIFT), Q6_V_vsplat_R(IQ2XXS_IDX_LO_MASK)),
|
||||
Q6_V_vand_VV(Q6_Vuw_vlsr_VuwR(v_idx, IQ2XXS_IDX_BITS - IQ2XXS_CODE_SHIFT), Q6_V_vsplat_R(IQ2XXS_IDX_HI_MASK)));
|
||||
Q6_vgather_ARMVh(&v_scr[0], (size_t) grid, HTP_IQ2XXS_GRID_BYTES - 1, v_off0);
|
||||
Q6_vgather_ARMVh(&v_scr[1], (size_t) grid, HTP_IQ2XXS_GRID_BYTES - 1, v_off1);
|
||||
|
||||
return Q6_V_hi_W(p);
|
||||
}
|
||||
|
||||
// IQ2_XXS: wait for the gathers of iq2xxs_gather_issue and read the grid codes
|
||||
static inline void iq2xxs_gather_wait(const uint8_t * restrict tile, HVX_Vector * restrict v_c) {
|
||||
HVX_Vector * restrict v_scr = iq2xxs_tile_scratch(tile);
|
||||
hvx_gather_sync(&v_scr[1]);
|
||||
v_c[0] = v_scr[0];
|
||||
v_c[1] = v_scr[1];
|
||||
}
|
||||
|
||||
// IQ2_XXS: 8 int8 weight words (k = 4g..4g+3 per row lane) and the odd group scale (2 * scale + 1) of a 32x32 tile
|
||||
static inline void iq2xxs_decode_expand(const HVX_Vector * restrict v_c, HVX_Vector v_sgn, HVX_Vector * restrict v_w, HVX_Vector * restrict v_ls) {
|
||||
const HVX_Vector v_zero = Q6_V_vzero();
|
||||
const HVX_Vector v_one = Q6_V_vsplat_R(1);
|
||||
const HVX_Vector v_sgn_mask = Q6_V_vsplat_R(IQ2XXS_SIGN_MASK);
|
||||
const HVX_Vector v_nib_lut = hvx_vmem(iq2xxs_vtcm_lut);
|
||||
|
||||
#pragma unroll
|
||||
for (int l = 0; l < IQ2XXS_GROUPS; l++) {
|
||||
// 7 stored sign bits + even-parity bit
|
||||
HVX_Vector v_s7 = Q6_V_vand_VV(Q6_Vuw_vlsr_VuwR(v_sgn, IQ2XXS_SIGN_BITS * l), v_sgn_mask);
|
||||
HVX_Vector v_t = Q6_Vh_vpopcount_Vh(v_s7);
|
||||
HVX_Vector v_s = Q6_V_vor_VV(v_s7, Q6_Vw_vasl_VwR(Q6_V_vand_VV(v_t, v_one), IQ2XXS_PARITY_SHIFT));
|
||||
HVX_Vector v_s16 = Q6_V_vor_VV(v_s, Q6_Vw_vasl_VwR(v_s, 8));
|
||||
HVX_Vector v_rep = Q6_V_vor_VV(v_s16, Q6_Vw_vasl_VwR(v_s16, 16));
|
||||
HVX_VectorPred q_lo = Q6_Q_vand_VR(v_rep, IQ2XXS_SIGN_PRED_LO);
|
||||
HVX_VectorPred q_hi = Q6_Q_vand_VR(v_rep, IQ2XXS_SIGN_PRED_HI);
|
||||
|
||||
HVX_Vector v_c2 = v_c[l >> 1];
|
||||
// code halfword duplicated in both halves of each lane
|
||||
HVX_Vector v_d = (l & 1) ? Q6_Vh_vshuffo_VhVh(v_c2, v_c2) : Q6_Vh_vshuffe_VhVh(v_c2, v_c2);
|
||||
// lane bytes: nibbles 0, 2, 1, 3 of the code, so vlut16 returns elements 0..3 / 4..7 as words
|
||||
HVX_Vector v_n = Q6_V_vor_VV(Q6_V_vand_VV(v_d, Q6_V_vsplat_R(IQ2XXS_NIB_LO_MASK)),
|
||||
Q6_V_vand_VV(Q6_Vuw_vlsr_VuwR(v_d, 4), Q6_V_vsplat_R(IQ2XXS_NIB_HI_MASK)));
|
||||
HVX_VectorPair p_g = Q6_Wh_vlut16_VbVhR(v_n, v_nib_lut, 0);
|
||||
HVX_Vector v_g0 = Q6_V_lo_W(p_g);
|
||||
HVX_Vector v_g1 = Q6_V_hi_W(p_g);
|
||||
|
||||
v_w[2 * l + 0] = Q6_V_vmux_QVV(q_lo, Q6_Vb_vsub_VbVb(v_zero, v_g0), v_g0);
|
||||
v_w[2 * l + 1] = Q6_V_vmux_QVV(q_hi, Q6_Vb_vsub_VbVb(v_zero, v_g1), v_g1);
|
||||
}
|
||||
|
||||
*v_ls = Q6_Vw_vadd_VwVw(Q6_Vw_vasl_VwR(Q6_Vuw_vlsr_VuwR(v_sgn, IQ2XXS_SCALE_SHIFT), 1), v_one);
|
||||
}
|
||||
|
||||
// IQ2_XXS: fp16 d of the 32 rows of a super-block (8 tiles) in halfword lanes 0..31
|
||||
static inline HVX_Vector iq2xxs_gather_d(const uint8_t * restrict sb) {
|
||||
const uint32_t padded = htp_tensor_get_tile_padded_size(HTP_TYPE_IQ2_XXS);
|
||||
HVX_Vector v_dd = Q6_V_vzero();
|
||||
|
||||
#pragma unroll
|
||||
for (int j = 0; j < IQ2XXS_TILES_PER_SB; j++) {
|
||||
HVX_Vector v = hvx_vmem(sb + j * padded + IQ2XXS_D_SLICE_OFF);
|
||||
v_dd = Q6_V_vmux_QVV(Q6_Q_vsetq_R(IQ2XXS_D_SLICE_BYTES * j), v_dd,
|
||||
Q6_V_vror_VR(v, (VLEN - IQ2XXS_D_SLICE_BYTES * j) & (VLEN - 1)));
|
||||
}
|
||||
|
||||
return v_dd;
|
||||
}
|
||||
|
||||
// IQ2_XXS: fp32 d / 8 of the 32 rows of a super-block (8 tiles) in lanes 0..31
|
||||
static inline HVX_Vector iq2xxs_load_d(const uint8_t * restrict sb) {
|
||||
// db = d * (0.5 + scale) * 0.25 = d * (2 * scale + 1) / 8
|
||||
@@ -2246,29 +2066,6 @@ static void tiled_vec_dot_mxfp4_32x2(const uint32_t n, void * restrict s0, void
|
||||
store_row(s1, valid_rows, v_sum_float_c1, sz1);
|
||||
}
|
||||
|
||||
#define NVFP4_SCALE_OFF 512 // UE4M3 scales after the 32 rows x 16 B of quants
|
||||
#define NVFP4_UE4M3_MASK 0x7F // code bits; 0x7F is the NaN code, decoded as 0
|
||||
#define NVFP4_UE4M3_MIN_NORMAL 0x08 // exponent field 1
|
||||
#define NVFP4_UE4M3_MAN_SHIFT 20 // 23 - 3 mantissa bits
|
||||
#define NVFP4_UE4M3_REBIAS 0x3C000000 // (127 - 7) << 23
|
||||
#define NVFP4_UE4M3_SUB_SCALE 0x1p-9f // subnormal: mantissa * 2^-9
|
||||
#define NVFP4_VECS_PER_GROUP 2 // quant vectors per scale group
|
||||
#define NVFP4_ACT_PER_GROUP 4 // activation vectors per scale group
|
||||
|
||||
// UE4M3 codes in the low byte of each word -> fp32 (without the 0.5 of kvalues_mxfp4)
|
||||
static inline HVX_Vector nvfp4_ue4m3_to_f32(HVX_Vector v_code) {
|
||||
HVX_Vector v_mask = Q6_V_vsplat_R(NVFP4_UE4M3_MASK);
|
||||
HVX_Vector v_x = Q6_V_vand_VV(v_code, v_mask);
|
||||
|
||||
HVX_Vector v_norm = Q6_Vw_vadd_VwVw(Q6_Vw_vasl_VwR(v_x, NVFP4_UE4M3_MAN_SHIFT), Q6_V_vsplat_R(NVFP4_UE4M3_REBIAS));
|
||||
HVX_Vector v_sub = hvx_vec_mul_f32_f32(Q6_Vsf_equals_Vw(v_x), hvx_vec_splat_f32(NVFP4_UE4M3_SUB_SCALE));
|
||||
|
||||
HVX_VectorPred q_sub = Q6_Q_vcmp_gt_VwVw(Q6_V_vsplat_R(NVFP4_UE4M3_MIN_NORMAL), v_x);
|
||||
HVX_VectorPred q_nan = Q6_Q_vcmp_eq_VwVw(v_x, v_mask);
|
||||
|
||||
return Q6_V_vmux_QVV(q_nan, Q6_V_vzero(), Q6_V_vmux_QVV(q_sub, v_sub, v_norm));
|
||||
}
|
||||
|
||||
// fp32 scales of the two 16-element sub-blocks of a tile, one lane per row
|
||||
static inline void nvfp4_load_scales(const uint8_t * restrict tile, HVX_Vector * restrict v_d0, HVX_Vector * restrict v_d1) {
|
||||
HVX_VectorPair v_code = Q6_Wuw_vunpack_Vuh(Q6_V_lo_W(Q6_Wuh_vunpack_Vub(hvx_vmem(tile + NVFP4_SCALE_OFF))));
|
||||
@@ -2391,6 +2188,54 @@ static void tiled_vec_dot_nvfp4_32x2(const uint32_t n, void * restrict s0, void
|
||||
store_row(s1, valid_rows, v_sum_float_c1, sz1);
|
||||
}
|
||||
|
||||
static void tiled_vec_dot_f8_e4m3_32x1(const uint32_t n, void * restrict s, const void * restrict vx, const void * restrict vy, uint32_t valid_rows, const void * restrict sz, hvx_tiled_store_row_fn_t store_row) {
|
||||
const HVX_Vector * restrict vptr = (const HVX_Vector *) vx;
|
||||
const HVX_Vector * restrict v_act = (const HVX_Vector *) vy;
|
||||
const uint32_t n_vecs = ((n + 31) / 32) * 8;
|
||||
|
||||
HVX_VectorPair acc = Q6_W_vzero();
|
||||
|
||||
for (uint32_t i = 0; i < n_vecs; i++) {
|
||||
HVX_VectorPair vp_u16 = Q6_Wuh_vunpack_Vub(vptr[i]);
|
||||
HVX_VectorPair vp_k = Q6_W_vdeal_VVR(Q6_V_hi_W(vp_u16), Q6_V_lo_W(vp_u16), -4);
|
||||
HVX_Vector v_w0 = f8_e4m3_to_f16(Q6_V_lo_W(vp_k));
|
||||
HVX_Vector v_w1 = f8_e4m3_to_f16(Q6_V_hi_W(vp_k));
|
||||
acc = hvx_vec_mpyacc_f32_f16(acc, v_w0, v_act[2 * i + 0]);
|
||||
acc = hvx_vec_mpyacc_f32_f16(acc, v_w1, v_act[2 * i + 1]);
|
||||
}
|
||||
|
||||
HVX_Vector v_sum = hvx_vec_add_f32_f32(Q6_V_lo_W(acc), Q6_V_hi_W(acc));
|
||||
|
||||
store_row(s, valid_rows, v_sum, sz);
|
||||
}
|
||||
|
||||
static void tiled_vec_dot_f8_e4m3_32x2(const uint32_t n, void * restrict s0, void * restrict s1, const void * restrict vx, const void * restrict vy0, const void * restrict vy1, uint32_t valid_rows, const void * restrict sz0, const void * restrict sz1, hvx_tiled_store_row_fn_t store_row) {
|
||||
const HVX_Vector * restrict vptr = (const HVX_Vector *) vx;
|
||||
const HVX_Vector * restrict v_act0 = (const HVX_Vector *) vy0;
|
||||
const HVX_Vector * restrict v_act1 = (const HVX_Vector *) vy1;
|
||||
const uint32_t n_vecs = ((n + 31) / 32) * 8;
|
||||
|
||||
HVX_VectorPair acc0 = Q6_W_vzero();
|
||||
HVX_VectorPair acc1 = Q6_W_vzero();
|
||||
|
||||
for (uint32_t i = 0; i < n_vecs; i++) {
|
||||
HVX_VectorPair vp_u16 = Q6_Wuh_vunpack_Vub(vptr[i]);
|
||||
HVX_VectorPair vp_k = Q6_W_vdeal_VVR(Q6_V_hi_W(vp_u16), Q6_V_lo_W(vp_u16), -4);
|
||||
HVX_Vector v_w0 = f8_e4m3_to_f16(Q6_V_lo_W(vp_k));
|
||||
HVX_Vector v_w1 = f8_e4m3_to_f16(Q6_V_hi_W(vp_k));
|
||||
acc0 = hvx_vec_mpyacc_f32_f16(acc0, v_w0, v_act0[2 * i + 0]);
|
||||
acc1 = hvx_vec_mpyacc_f32_f16(acc1, v_w0, v_act1[2 * i + 0]);
|
||||
acc0 = hvx_vec_mpyacc_f32_f16(acc0, v_w1, v_act0[2 * i + 1]);
|
||||
acc1 = hvx_vec_mpyacc_f32_f16(acc1, v_w1, v_act1[2 * i + 1]);
|
||||
}
|
||||
|
||||
HVX_Vector v_sum0 = hvx_vec_add_f32_f32(Q6_V_lo_W(acc0), Q6_V_hi_W(acc0));
|
||||
HVX_Vector v_sum1 = hvx_vec_add_f32_f32(Q6_V_lo_W(acc1), Q6_V_hi_W(acc1));
|
||||
|
||||
store_row(s0, valid_rows, v_sum0, sz0);
|
||||
store_row(s1, valid_rows, v_sum1, sz1);
|
||||
}
|
||||
|
||||
static void tiled_vec_dot_f16_32x1(const uint32_t n, void * restrict s, const void * restrict vx, const void * restrict vy, uint32_t valid_rows, const void * restrict sz, hvx_tiled_store_row_fn_t store_row) {
|
||||
const HVX_Vector * restrict vptr = (const HVX_Vector *) vx;
|
||||
const HVX_Vector * restrict v_act = (const HVX_Vector *) vy;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "hex-utils.h"
|
||||
#include "hvx-base.h"
|
||||
#include "hvx-repl.h"
|
||||
#include "hvx-types.h"
|
||||
|
||||
static inline HVX_Vector hvx_vec_reduce_sum_n_i32(HVX_Vector in, unsigned int n) {
|
||||
@@ -399,6 +400,19 @@ static inline void hvx_argmax_f32(
|
||||
*out_idx = best_idx;
|
||||
}
|
||||
|
||||
// Reduce abs-max independently across low 32 and high 32 F16 lanes
|
||||
static inline HVX_Vector hvx_vec_reduce_abs_max_half_f16(HVX_Vector v) {
|
||||
HVX_Vector m = hvx_vec_abs_f16(v);
|
||||
m = Q6_Vhf_vmax_VhfVhf(m, Q6_V_vror_VR(m, 2));
|
||||
m = Q6_Vhf_vmax_VhfVhf(m, Q6_V_vror_VR(m, 4));
|
||||
m = Q6_Vhf_vmax_VhfVhf(m, Q6_V_vror_VR(m, 8));
|
||||
m = Q6_Vhf_vmax_VhfVhf(m, Q6_V_vror_VR(m, 16));
|
||||
m = Q6_Vhf_vmax_VhfVhf(m, Q6_V_vror_VR(m, 32));
|
||||
HVX_Vector m_lo = hvx_vec_repl_f16(m);
|
||||
HVX_Vector m_hi = hvx_vec_repl_f16(Q6_V_vror_VR(m, 64));
|
||||
return Q6_V_vmux_QVV(Q6_Q_vsetq_R(64), m_lo, m_hi);
|
||||
}
|
||||
|
||||
#undef hvx_reduce_loop_body
|
||||
#undef HVX_REDUCE_MAX_OP
|
||||
#undef HVX_REDUCE_SUM_OP
|
||||
|
||||
@@ -38,7 +38,6 @@ struct htp_im2col_context {
|
||||
uint32_t pe_gather_nvec; // vectors per gathered patch (KH == 1 blocked path)
|
||||
|
||||
// Precomputed dimensions for worker threads
|
||||
uint32_t N;
|
||||
uint32_t IC;
|
||||
uint32_t ID, IH, IW;
|
||||
uint32_t KD, KH, KW;
|
||||
@@ -970,7 +969,6 @@ int op_im2col(struct htp_ops_context * octx) {
|
||||
ictx.pe_owb = kparams->owb;
|
||||
ictx.pe_wb = kparams->wb;
|
||||
ictx.pe_nbpr = kparams->nbpr;
|
||||
ictx.N = kparams->N;
|
||||
ictx.IC = kparams->IC;
|
||||
ictx.ID = kparams->ID;
|
||||
ictx.IH = kparams->IH;
|
||||
@@ -1069,7 +1067,6 @@ int op_im2col_3d(struct htp_ops_context * octx) {
|
||||
ictx.pe_owb = kparams->owb;
|
||||
ictx.pe_wb = kparams->wb;
|
||||
ictx.pe_nbpr = kparams->nbpr;
|
||||
ictx.N = kparams->N;
|
||||
ictx.IC = kparams->IC;
|
||||
ictx.ID = kparams->ID;
|
||||
ictx.IH = kparams->IH;
|
||||
|
||||
@@ -58,7 +58,6 @@ struct htp_im2col_kernel_params {
|
||||
uint32_t wb;
|
||||
uint32_t nbpr;
|
||||
|
||||
uint32_t N;
|
||||
uint32_t IC;
|
||||
uint32_t ID, IH, IW;
|
||||
uint32_t KD, KH, KW;
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#include "hvx-quant.h"
|
||||
#include "hvx-fa-kernels.h"
|
||||
#include "hmx-utils.h"
|
||||
#include "hmx-fa-kernels.h"
|
||||
#include "hmx-fa-fp16-kernels.h"
|
||||
#include "hmx-queue.h"
|
||||
|
||||
#include "lightning-indexer-ops.h"
|
||||
|
||||
@@ -966,6 +966,7 @@ static int execute_op(struct htp_ops_context * octx) {
|
||||
case HTP_OP_SUB:
|
||||
case HTP_OP_DIV:
|
||||
case HTP_OP_ADD_ID:
|
||||
case HTP_OP_MUL_ID:
|
||||
return op_binary(octx);
|
||||
|
||||
case HTP_OP_NORM:
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,25 @@
|
||||
#ifndef HTP_MATMUL_HMX_F16_H
|
||||
#define HTP_MATMUL_HMX_F16_H
|
||||
|
||||
#include "matmul-internal.h"
|
||||
|
||||
int hmx_f16_mm_2d(
|
||||
struct htp_ops_context * octx,
|
||||
const struct htp_mm_kernel_params * kparams,
|
||||
const hmx_mm_2d_params_t * p);
|
||||
|
||||
int hmx_f16_mm_batched(
|
||||
struct htp_ops_context * octx,
|
||||
const struct htp_mm_kernel_params * kparams,
|
||||
const hmx_mm_batched_params_t * params);
|
||||
|
||||
int hmx_f16_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_params * kparams);
|
||||
|
||||
int hmx_f16_mm_nx_glu_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_params * kparams);
|
||||
|
||||
int hmx_f16_mm_id_2d(
|
||||
struct htp_ops_context * octx,
|
||||
const struct htp_mm_kernel_params * kparams,
|
||||
const hmx_mm_id_2d_params_t * p);
|
||||
|
||||
#endif // HTP_MATMUL_HMX_F16_H
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,25 @@
|
||||
#ifndef HTP_MATMUL_HMX_W8A8_H
|
||||
#define HTP_MATMUL_HMX_W8A8_H
|
||||
|
||||
#include "matmul-internal.h"
|
||||
|
||||
int hmx_w8a8_mm_2d(
|
||||
struct htp_ops_context * octx,
|
||||
const struct htp_mm_kernel_params * kparams,
|
||||
const hmx_mm_2d_params_t * p);
|
||||
|
||||
int hmx_w8a8_mm_batched(
|
||||
struct htp_ops_context * octx,
|
||||
const struct htp_mm_kernel_params * kparams,
|
||||
const hmx_mm_batched_params_t * params);
|
||||
|
||||
int hmx_w8a8_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_params * kparams);
|
||||
|
||||
int hmx_w8a8_mm_nx_glu_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_params * kparams);
|
||||
|
||||
int hmx_w8a8_mm_id_2d(
|
||||
struct htp_ops_context * octx,
|
||||
const struct htp_mm_kernel_params * kparams,
|
||||
const hmx_mm_id_2d_params_t * p);
|
||||
|
||||
#endif // HTP_MATMUL_HMX_W8A8_H
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
||||
#ifndef HTP_MATMUL_HVX_H
|
||||
#define HTP_MATMUL_HVX_H
|
||||
|
||||
#include "matmul-internal.h"
|
||||
|
||||
int hvx_mm_matmul(struct htp_ops_context * octx);
|
||||
int hvx_mm_matmul_nx(struct htp_ops_context * octx);
|
||||
int hvx_mm_matmul_id(struct htp_ops_context * octx, const struct htp_mmid_mapping * map);
|
||||
int hvx_mm_matmul_id_nx(struct htp_ops_context * octx, const struct htp_mmid_mapping * map);
|
||||
|
||||
#endif // HTP_MATMUL_HVX_H
|
||||
@@ -0,0 +1,676 @@
|
||||
#ifndef HTP_MATMUL_INTERNAL_H
|
||||
#define HTP_MATMUL_INTERNAL_H
|
||||
|
||||
#include <HAP_farf.h>
|
||||
#include <HAP_perf.h>
|
||||
#include <HAP_compute_res.h>
|
||||
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
#include "dma-queue.h"
|
||||
#include "hvx-utils.h"
|
||||
#include "hvx-dump.h"
|
||||
#include "hvx-arith.h"
|
||||
#include "hvx-reduce.h"
|
||||
#include "hvx-copy.h"
|
||||
#include "hvx-norm.h"
|
||||
#include "hmx-utils.h"
|
||||
#include "hmx-queue.h"
|
||||
|
||||
#include "htp-ctx.h"
|
||||
#include "htp-ops.h"
|
||||
#include "htp-tensor.h"
|
||||
#include "matmul-ops.h"
|
||||
#include "htp-vtcm.h"
|
||||
|
||||
#define htp_matmul_tensors_preamble \
|
||||
const struct htp_tensor * restrict __attribute__((unused)) src0 = octx->src[0]; \
|
||||
const struct htp_tensor * restrict __attribute__((unused)) src1 = octx->src[1]; \
|
||||
const struct htp_tensor * restrict __attribute__((unused)) bias = \
|
||||
(octx->op == HTP_OP_MUL_MAT_ADD) ? octx->src[2] : NULL; \
|
||||
const struct htp_tensor * restrict __attribute__((unused)) dst = octx->dst; \
|
||||
const struct htp_tensor * restrict __attribute__((unused)) act = src1; \
|
||||
\
|
||||
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)) ne10 = src1->ne[0]; \
|
||||
const uint32_t __attribute__((unused)) ne11 = src1->ne[1]; \
|
||||
const uint32_t __attribute__((unused)) ne12 = src1->ne[2]; \
|
||||
const uint32_t __attribute__((unused)) ne13 = src1->ne[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)) 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)) nb10 = src1->nb[0]; \
|
||||
const uint32_t __attribute__((unused)) nb11 = src1->nb[1]; \
|
||||
const uint32_t __attribute__((unused)) nb12 = src1->nb[2]; \
|
||||
const uint32_t __attribute__((unused)) nb13 = src1->nb[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];
|
||||
|
||||
// --- Shared LUTs (defined in matmul-ops.c) ---
|
||||
|
||||
extern const uint8_t expand_x32_e8m0[128];
|
||||
extern const uint8_t kvalues_iq4nl_lut[];
|
||||
extern const uint8_t kvalues_mxfp4_lut[];
|
||||
extern const uint8_t * iq2xxs_vtcm_lut;
|
||||
|
||||
void iq2xxs_lut_copy(uint8_t * restrict lut);
|
||||
|
||||
static inline void hvx_mm_iq2xxs_lut_init(uint8_t * restrict base, const struct htp_mm_hvx_vtcm_layout * restrict L) {
|
||||
if (L->lut_bytes == 0) {
|
||||
return;
|
||||
}
|
||||
iq2xxs_lut_copy(base + L->off_lut);
|
||||
}
|
||||
|
||||
// --- Shared Quant Unpack Helpers ---
|
||||
|
||||
static inline HVX_Vector unpack_q1_0_group(
|
||||
HVX_Vector v_plane,
|
||||
int g,
|
||||
HVX_Vector v_neg_one,
|
||||
HVX_Vector v_pos_one
|
||||
) {
|
||||
HVX_VectorPred q = Q6_Q_vand_VR(v_plane, 0x01010101u << g);
|
||||
return Q6_V_vmux_QVV(q, v_pos_one, v_neg_one);
|
||||
}
|
||||
|
||||
// Q5_K: OR 0x10 into every lane of v whose flag j is set in the plane (see HTP_TENSOR_TILE_SIZE_Q5_K)
|
||||
static inline HVX_Vector hvx_q5k_or_hibit(HVX_Vector v, HVX_Vector v_plane, int j) {
|
||||
HVX_VectorPred q = Q6_Q_vand_VR(v_plane, 0x01010101u << j);
|
||||
return Q6_V_vandor_VQR(v, q, 0x10101010);
|
||||
}
|
||||
|
||||
// OR 0xF0 into every lane of v whose flag j is set in the plane, i.e. 4-bit value | 0xF0 = q - 16 where the 5th bit is clear (Q5_0)
|
||||
static inline HVX_Vector hvx_q5_0_or_neg(HVX_Vector v, HVX_Vector v_plane, int j) {
|
||||
HVX_VectorPred q = Q6_Q_vand_VR(v_plane, 0x01010101u << j);
|
||||
return Q6_V_vandor_VQR(v, q, 0xF0F0F0F0);
|
||||
}
|
||||
|
||||
// Q6_K weights are stored unsigned (0..63), see HTP_TENSOR_TILE_SIZE_Q6_K. Unpack k-group g of a tile to signed bytes (q - 32)
|
||||
static inline HVX_Vector unpack_q6_k_group(const HVX_Vector * restrict vptr, int g, HVX_Vector mask_0f, HVX_Vector mask_03, HVX_Vector i32) {
|
||||
HVX_Vector v_lo = (g & 1) ? Q6_Vub_vlsr_VubR(vptr[g >> 1], 4) : Q6_V_vand_VV(vptr[g >> 1], mask_0f);
|
||||
HVX_Vector v_hi = (g & 3) ? Q6_Vub_vlsr_VubR(vptr[4 + (g >> 2)], 2 * (g & 3)) : vptr[4 + (g >> 2)];
|
||||
HVX_Vector v_q = Q6_V_vor_VV(v_lo, Q6_Vw_vasl_VwR(Q6_V_vand_VV(v_hi, mask_03), 4));
|
||||
return Q6_Vb_vsub_VbVb(v_q, i32);
|
||||
}
|
||||
|
||||
// Q3_K / Q2_K: low 2 bits of k-group g, see HTP_TENSOR_TILE_SIZE_Q3_K
|
||||
static inline HVX_Vector unpack_q3_k_low2(const HVX_Vector * restrict vptr, int g, HVX_Vector mask_03) {
|
||||
HVX_Vector v = vptr[g >> 2];
|
||||
if ((g & 3) == 3) {
|
||||
return Q6_Vub_vlsr_VubR(v, 6);
|
||||
}
|
||||
if (g & 3) {
|
||||
v = Q6_Vub_vlsr_VubR(v, 2 * (g & 3));
|
||||
}
|
||||
return Q6_V_vand_VV(v, mask_03);
|
||||
}
|
||||
|
||||
// Q3_K k-group g as signed bytes: low2 | 0xFC (= low2 - 4) where bit g of vector 2 is set
|
||||
static inline HVX_Vector unpack_q3_k_group(const HVX_Vector * restrict vptr, int g, HVX_Vector mask_03) {
|
||||
HVX_VectorPred q_neg = Q6_Q_vand_VR(vptr[2], 0x01010101u << g);
|
||||
return Q6_V_vandor_VQR(unpack_q3_k_low2(vptr, g, mask_03), q_neg, 0xFCFCFCFC);
|
||||
}
|
||||
|
||||
// Q2_K: fp16 d (*v_d) and -dmin (*v_m) of the 32 rows of a super-block (8 tiles, VTCM stride 384), each in both vector halves
|
||||
static inline void q2_k_load_dmin(const uint8_t * restrict sb, HVX_Vector * restrict v_d, HVX_Vector * restrict v_m) {
|
||||
HVX_Vector v_dd = Q6_V_vzero(); // lanes 0..31: d, lanes 32..63: -dmin
|
||||
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; j++) {
|
||||
HVX_Vector v = hvx_vmem(sb + j * 384 + 256); // the slice is at bytes 64..79
|
||||
v_dd = Q6_V_vmux_QVV(Q6_Q_vsetq_R(16 * j), v_dd, Q6_V_vror_VR(v, (64 - 16 * j) & 127));
|
||||
}
|
||||
|
||||
HVX_VectorPred q_lo = Q6_Q_vsetq_R(64);
|
||||
HVX_Vector v_r = Q6_V_vror_VR(v_dd, 64);
|
||||
*v_d = Q6_V_vmux_QVV(q_lo, v_dd, v_r);
|
||||
*v_m = Q6_V_vmux_QVV(q_lo, v_r, v_dd);
|
||||
}
|
||||
|
||||
// Q2_K: fp16 D = d * scale and M = -dmin * min of a tile from its scale bytes, in the lanes of the Q6_K scales
|
||||
static inline void q2_k_unpack_scales(const uint8_t * restrict tile, HVX_Vector v_d, HVX_Vector v_m,
|
||||
HVX_Vector * restrict v_D, HVX_Vector * restrict v_M) {
|
||||
HVX_Vector v_sc = hvx_vmem(tile + 256);
|
||||
HVX_Vector v_l = Q6_V_lo_W(Q6_Wuh_vunpack_Vub(Q6_V_vand_VV(v_sc, Q6_Vb_vsplat_R(0x0F))));
|
||||
HVX_Vector v_mn = Q6_V_lo_W(Q6_Wuh_vunpack_Vub(Q6_Vub_vlsr_VubR(v_sc, 4)));
|
||||
// qf32 product: rounds like GGML_FP32_TO_FP16(d * scale) (qf16 is off by 1 ulp for many values on v73)
|
||||
*v_D = Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(Q6_Vhf_equals_Vh(v_l), v_d));
|
||||
*v_M = Q6_Vhf_equals_Wqf32(Q6_Wqf32_vmpy_VhfVhf(Q6_Vhf_equals_Vh(v_mn), v_m));
|
||||
}
|
||||
|
||||
#define IQ2XXS_GROUPS 4 // 8-element grid entries per tile row
|
||||
#define IQ2XXS_TILES_PER_SB 8 // 256 / 32
|
||||
#define IQ2XXS_CODE_SHIFT 1 // log2(2 B per grid code)
|
||||
#define IQ2XXS_WORD_BYTES 4
|
||||
#define IQ2XXS_IDX_BITS 8
|
||||
#define IQ2XXS_IDX_MASK 0xFF
|
||||
#define IQ2XXS_SIGN_BITS 7
|
||||
#define IQ2XXS_SIGN_MASK 0x7F
|
||||
#define IQ2XXS_PARITY_SHIFT 7
|
||||
#define IQ2XXS_SCALE_SHIFT 28
|
||||
#define IQ2XXS_D_SLICE_OFF 256 // 4 fp16 d of the tile, after the 32 rows x 8 B
|
||||
#define IQ2XXS_D_SCALE 0.125f // db = d * (2 * scale + 1) / 8
|
||||
#define IQ2XXS_D_SLICE_BYTES 8
|
||||
#define IQ2XXS_SIGN_PRED_LO 0x08040201 // sign bit of bytes 0..3 of an 8-element group
|
||||
#define IQ2XXS_SIGN_PRED_HI 0x80402010 // sign bit of bytes 4..7
|
||||
#define IQ2XXS_IDX_LO_MASK 0x1FE // index byte << 1, halfword 0 of a lane
|
||||
#define IQ2XXS_IDX_HI_MASK 0x1FE0000 // index byte << 1, halfword 1 of a lane
|
||||
#define IQ2XXS_NIB_LO_MASK 0x00000F0F // nibbles of the code bytes, lane bytes 0..1
|
||||
#define IQ2XXS_NIB_HI_MASK 0x0F0F0000 // nibbles of the code bytes, lane bytes 2..3
|
||||
|
||||
static inline HVX_Vector * iq2xxs_tile_scratch(const uint8_t * tile) {
|
||||
return (HVX_Vector *) (tile + htp_tensor_get_tile_padded_size(HTP_TYPE_IQ2_XXS) - HTP_TENSOR_TILE_SCRATCH_IQ2_XXS);
|
||||
}
|
||||
|
||||
// IQ2_XXS: start the 2 halfword grid-code gathers of a tile into its scratch, returns the sign/scale words
|
||||
static inline HVX_Vector iq2xxs_gather_issue(const uint8_t * restrict tile) {
|
||||
HVX_Vector * restrict v_scr = iq2xxs_tile_scratch(tile);
|
||||
const uint8_t * grid = iq2xxs_vtcm_lut + HTP_IQ2XXS_NIBBLE_BYTES;
|
||||
|
||||
// lo: 4 grid indices per row, hi: 4 sign indices + scale per row
|
||||
HVX_VectorPair p = Q6_W_vdeal_VVR(hvx_vmem(tile + VLEN), hvx_vmem(tile), -IQ2XXS_WORD_BYTES);
|
||||
HVX_Vector v_idx = Q6_V_lo_W(p);
|
||||
|
||||
// one halfword gather per 2 groups: lane i gets the codes of groups 2h and 2h+1 in halfwords 0 and 1
|
||||
HVX_Vector v_off0 = Q6_V_vor_VV(Q6_V_vand_VV(Q6_Vw_vasl_VwR(v_idx, IQ2XXS_CODE_SHIFT), Q6_V_vsplat_R(IQ2XXS_IDX_LO_MASK)),
|
||||
Q6_V_vand_VV(Q6_Vw_vasl_VwR(v_idx, IQ2XXS_CODE_SHIFT + IQ2XXS_IDX_BITS), Q6_V_vsplat_R(IQ2XXS_IDX_HI_MASK)));
|
||||
HVX_Vector v_off1 = Q6_V_vor_VV(Q6_V_vand_VV(Q6_Vuw_vlsr_VuwR(v_idx, 2 * IQ2XXS_IDX_BITS - IQ2XXS_CODE_SHIFT), Q6_V_vsplat_R(IQ2XXS_IDX_LO_MASK)),
|
||||
Q6_V_vand_VV(Q6_Vuw_vlsr_VuwR(v_idx, IQ2XXS_IDX_BITS - IQ2XXS_CODE_SHIFT), Q6_V_vsplat_R(IQ2XXS_IDX_HI_MASK)));
|
||||
Q6_vgather_ARMVh(&v_scr[0], (size_t) grid, HTP_IQ2XXS_GRID_BYTES - 1, v_off0);
|
||||
Q6_vgather_ARMVh(&v_scr[1], (size_t) grid, HTP_IQ2XXS_GRID_BYTES - 1, v_off1);
|
||||
|
||||
return Q6_V_hi_W(p);
|
||||
}
|
||||
|
||||
// IQ2_XXS: wait for the gathers of iq2xxs_gather_issue and read the grid codes
|
||||
static inline void iq2xxs_gather_wait(const uint8_t * restrict tile, HVX_Vector * restrict v_c) {
|
||||
HVX_Vector * restrict v_scr = iq2xxs_tile_scratch(tile);
|
||||
hvx_gather_sync(&v_scr[1]);
|
||||
v_c[0] = v_scr[0];
|
||||
v_c[1] = v_scr[1];
|
||||
}
|
||||
|
||||
// IQ2_XXS: 8 int8 weight words (k = 4g..4g+3 per row lane) and the odd group scale (2 * scale + 1) of a 32x32 tile
|
||||
static inline void iq2xxs_decode_expand(const HVX_Vector * restrict v_c, HVX_Vector v_sgn, HVX_Vector * restrict v_w, HVX_Vector * restrict v_ls) {
|
||||
const HVX_Vector v_zero = Q6_V_vzero();
|
||||
const HVX_Vector v_one = Q6_V_vsplat_R(1);
|
||||
const HVX_Vector v_sgn_mask = Q6_V_vsplat_R(IQ2XXS_SIGN_MASK);
|
||||
const HVX_Vector v_nib_lut = hvx_vmem(iq2xxs_vtcm_lut);
|
||||
|
||||
#pragma unroll
|
||||
for (int l = 0; l < IQ2XXS_GROUPS; l++) {
|
||||
// 7 stored sign bits + even-parity bit
|
||||
HVX_Vector v_s7 = Q6_V_vand_VV(Q6_Vuw_vlsr_VuwR(v_sgn, IQ2XXS_SIGN_BITS * l), v_sgn_mask);
|
||||
HVX_Vector v_t = Q6_Vh_vpopcount_Vh(v_s7);
|
||||
HVX_Vector v_s = Q6_V_vor_VV(v_s7, Q6_Vw_vasl_VwR(Q6_V_vand_VV(v_t, v_one), IQ2XXS_PARITY_SHIFT));
|
||||
HVX_Vector v_s16 = Q6_V_vor_VV(v_s, Q6_Vw_vasl_VwR(v_s, 8));
|
||||
HVX_Vector v_rep = Q6_V_vor_VV(v_s16, Q6_Vw_vasl_VwR(v_s16, 16));
|
||||
HVX_VectorPred q_lo = Q6_Q_vand_VR(v_rep, IQ2XXS_SIGN_PRED_LO);
|
||||
HVX_VectorPred q_hi = Q6_Q_vand_VR(v_rep, IQ2XXS_SIGN_PRED_HI);
|
||||
|
||||
HVX_Vector v_c2 = v_c[l >> 1];
|
||||
// code halfword duplicated in both halves of each lane
|
||||
HVX_Vector v_d = (l & 1) ? Q6_Vh_vshuffo_VhVh(v_c2, v_c2) : Q6_Vh_vshuffe_VhVh(v_c2, v_c2);
|
||||
// lane bytes: nibbles 0, 2, 1, 3 of the code, so vlut16 returns elements 0..3 / 4..7 as words
|
||||
HVX_Vector v_n = Q6_V_vor_VV(Q6_V_vand_VV(v_d, Q6_V_vsplat_R(IQ2XXS_NIB_LO_MASK)),
|
||||
Q6_V_vand_VV(Q6_Vuw_vlsr_VuwR(v_d, 4), Q6_V_vsplat_R(IQ2XXS_NIB_HI_MASK)));
|
||||
HVX_VectorPair p_g = Q6_Wh_vlut16_VbVhR(v_n, v_nib_lut, 0);
|
||||
HVX_Vector v_g0 = Q6_V_lo_W(p_g);
|
||||
HVX_Vector v_g1 = Q6_V_hi_W(p_g);
|
||||
|
||||
v_w[2 * l + 0] = Q6_V_vmux_QVV(q_lo, Q6_Vb_vsub_VbVb(v_zero, v_g0), v_g0);
|
||||
v_w[2 * l + 1] = Q6_V_vmux_QVV(q_hi, Q6_Vb_vsub_VbVb(v_zero, v_g1), v_g1);
|
||||
}
|
||||
|
||||
*v_ls = Q6_Vw_vadd_VwVw(Q6_Vw_vasl_VwR(Q6_Vuw_vlsr_VuwR(v_sgn, IQ2XXS_SCALE_SHIFT), 1), v_one);
|
||||
}
|
||||
|
||||
// IQ2_XXS: fp16 d of the 32 rows of a super-block (8 tiles) in halfword lanes 0..31
|
||||
static inline HVX_Vector iq2xxs_gather_d(const uint8_t * restrict sb) {
|
||||
const uint32_t padded = htp_tensor_get_tile_padded_size(HTP_TYPE_IQ2_XXS);
|
||||
HVX_Vector v_dd = Q6_V_vzero();
|
||||
|
||||
#pragma unroll
|
||||
for (int j = 0; j < IQ2XXS_TILES_PER_SB; j++) {
|
||||
HVX_Vector v = hvx_vmem(sb + j * padded + IQ2XXS_D_SLICE_OFF);
|
||||
v_dd = Q6_V_vmux_QVV(Q6_Q_vsetq_R(IQ2XXS_D_SLICE_BYTES * j), v_dd,
|
||||
Q6_V_vror_VR(v, (VLEN - IQ2XXS_D_SLICE_BYTES * j) & (VLEN - 1)));
|
||||
}
|
||||
|
||||
return v_dd;
|
||||
}
|
||||
|
||||
#define NVFP4_SCALE_OFF 512 // UE4M3 scales after the 32 rows x 16 B of quants
|
||||
#define NVFP4_UE4M3_MASK 0x7F // code bits; 0x7F is the NaN code, decoded as 0
|
||||
#define NVFP4_UE4M3_MIN_NORMAL 0x08 // exponent field 1
|
||||
#define NVFP4_UE4M3_MAN_SHIFT 20 // 23 - 3 mantissa bits
|
||||
#define NVFP4_UE4M3_REBIAS 0x3C000000 // (127 - 7) << 23
|
||||
#define NVFP4_UE4M3_SUB_SCALE 0x1p-9f // subnormal: mantissa * 2^-9
|
||||
#define NVFP4_VECS_PER_GROUP 2 // quant vectors per scale group
|
||||
#define NVFP4_ACT_PER_GROUP 4 // activation vectors per scale group
|
||||
#define NVFP4_UE4M3_F16_MAN_SHIFT 7 // 10 - 3 mantissa bits
|
||||
#define NVFP4_UE4M3_F16_REBIAS 0x2000 // (15 - 7) << 10
|
||||
#define NVFP4_UE4M3_F16_SUB_SCALE 0x1800 // 2^-9 as fp16 bits
|
||||
|
||||
// UE4M3 codes in the low byte of each word -> fp32 (without the 0.5 of kvalues_mxfp4)
|
||||
static inline HVX_Vector nvfp4_ue4m3_to_f32(HVX_Vector v_code) {
|
||||
HVX_Vector v_mask = Q6_V_vsplat_R(NVFP4_UE4M3_MASK);
|
||||
HVX_Vector v_x = Q6_V_vand_VV(v_code, v_mask);
|
||||
|
||||
HVX_Vector v_norm = Q6_Vw_vadd_VwVw(Q6_Vw_vasl_VwR(v_x, NVFP4_UE4M3_MAN_SHIFT), Q6_V_vsplat_R(NVFP4_UE4M3_REBIAS));
|
||||
HVX_Vector v_sub = hvx_vec_mul_f32_f32(Q6_Vsf_equals_Vw(v_x), hvx_vec_splat_f32(NVFP4_UE4M3_SUB_SCALE));
|
||||
|
||||
HVX_VectorPred q_sub = Q6_Q_vcmp_gt_VwVw(Q6_V_vsplat_R(NVFP4_UE4M3_MIN_NORMAL), v_x);
|
||||
HVX_VectorPred q_nan = Q6_Q_vcmp_eq_VwVw(v_x, v_mask);
|
||||
|
||||
return Q6_V_vmux_QVV(q_nan, Q6_V_vzero(), Q6_V_vmux_QVV(q_sub, v_sub, v_norm));
|
||||
}
|
||||
|
||||
// UE4M3 codes in the low byte of each halfword -> fp16
|
||||
static inline HVX_Vector nvfp4_ue4m3_to_f16(HVX_Vector v_code) {
|
||||
HVX_Vector v_mask = Q6_Vh_vsplat_R(NVFP4_UE4M3_MASK);
|
||||
HVX_Vector v_x = Q6_V_vand_VV(v_code, v_mask);
|
||||
|
||||
HVX_Vector v_norm = Q6_Vh_vadd_VhVh(Q6_Vh_vasl_VhR(v_x, NVFP4_UE4M3_F16_MAN_SHIFT), Q6_Vh_vsplat_R(NVFP4_UE4M3_F16_REBIAS));
|
||||
HVX_Vector v_sub = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(Q6_Vhf_equals_Vh(v_x), Q6_Vh_vsplat_R(NVFP4_UE4M3_F16_SUB_SCALE)));
|
||||
|
||||
HVX_VectorPred q_sub = Q6_Q_vcmp_gt_VhVh(Q6_Vh_vsplat_R(NVFP4_UE4M3_MIN_NORMAL), v_x);
|
||||
HVX_VectorPred q_nan = Q6_Q_vcmp_eq_VhVh(v_x, v_mask);
|
||||
|
||||
return Q6_V_vmux_QVV(q_nan, Q6_V_vzero(), Q6_V_vmux_QVV(q_sub, v_sub, v_norm));
|
||||
}
|
||||
|
||||
#define F8_E4M3_SIGN_MASK 0x80 // sign bit in FP8 E4M3
|
||||
#define F8_E4M3_F16_MAN_SHIFT 7 // 10 - 3 mantissa bits
|
||||
#define F8_E4M3_F16_REBIAS 0x2000 // (15 - 7) << 10
|
||||
#define F8_E4M3_F16_SUB_SCALE 0x1800 // 2^-9 as fp16 bits
|
||||
|
||||
// Signed F8_E4M3 codes in the low byte of each halfword -> fp16 (0x7F/0xFF NaN -> 0)
|
||||
static inline HVX_Vector f8_e4m3_to_f16(HVX_Vector v_code) {
|
||||
HVX_Vector v_mask = Q6_Vh_vsplat_R(NVFP4_UE4M3_MASK);
|
||||
HVX_Vector v_abs = Q6_V_vand_VV(v_code, v_mask);
|
||||
HVX_Vector v_sign = Q6_Vh_vasl_VhR(Q6_V_vand_VV(v_code, Q6_Vh_vsplat_R(F8_E4M3_SIGN_MASK)), 8);
|
||||
|
||||
HVX_Vector v_norm = Q6_Vh_vadd_VhVh(Q6_Vh_vasl_VhR(v_abs, F8_E4M3_F16_MAN_SHIFT), Q6_Vh_vsplat_R(F8_E4M3_F16_REBIAS));
|
||||
HVX_Vector v_sub = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(Q6_Vhf_equals_Vh(v_abs), Q6_Vh_vsplat_R(F8_E4M3_F16_SUB_SCALE)));
|
||||
|
||||
HVX_VectorPred q_sub = Q6_Q_vcmp_gt_VhVh(Q6_Vh_vsplat_R(NVFP4_UE4M3_MIN_NORMAL), v_abs);
|
||||
HVX_VectorPred q_nan = Q6_Q_vcmp_eq_VhVh(v_abs, v_mask);
|
||||
|
||||
return Q6_V_vmux_QVV(q_nan, Q6_V_vzero(), Q6_V_vor_VV(Q6_V_vmux_QVV(q_sub, v_sub, v_norm), v_sign));
|
||||
}
|
||||
|
||||
// --- Shared HMX Bias / Packing Helpers ---
|
||||
|
||||
static inline HVX_Vector hmx_load_bias_f32_32_f16(const void * bias_row_base, size_t c) {
|
||||
const __fp16 * p = (const __fp16 *) bias_row_base + c;
|
||||
return Q6_V_lo_W(hvx_vec_f16_to_f32(hvx_vmemu(p)));
|
||||
}
|
||||
|
||||
static inline HVX_Vector hmx_load_bias_f32_32_f32(const void * bias_row_base, size_t c) {
|
||||
const float * p = (const float *) bias_row_base + c;
|
||||
return hvx_vmemu(p);
|
||||
}
|
||||
|
||||
static inline HVX_VectorPair hmx_load_bias_f32_64_f16(const void * bias_row_base, size_t c) {
|
||||
const __fp16 * p = (const __fp16 *) bias_row_base + c;
|
||||
return hvx_vec_f16_to_f32(hvx_vmemu(p));
|
||||
}
|
||||
|
||||
static inline HVX_VectorPair hmx_load_bias_f32_64_f32(const void * bias_row_base, size_t c) {
|
||||
const float * p = (const float *) bias_row_base + c;
|
||||
return Q6_W_vcombine_VV(hvx_vmemu(p + 32), hvx_vmemu(p));
|
||||
}
|
||||
|
||||
// --- Shared HMX Scale / Norm / GLU / Batched / MMID Helpers ---
|
||||
|
||||
static inline void htp_mm_load_scale(
|
||||
dma_queue * dma_q,
|
||||
float * vtcm_scale,
|
||||
dma_addr_t scale_addr,
|
||||
size_t scale_bytes,
|
||||
bool splat_scalar) {
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_scale, scale_addr), hex_align_up(scale_bytes, 128), 0, scale_bytes, 1);
|
||||
dma_queue_pop(dma_q);
|
||||
if (splat_scalar) {
|
||||
hvx_vmem(vtcm_scale) = hvx_vec_repl4(hvx_vmem(vtcm_scale));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void htp_mm_load_norm(
|
||||
dma_queue * dma_q,
|
||||
float * vtcm_norm,
|
||||
void * vtcm_tmp,
|
||||
const struct htp_tensor * norm_w,
|
||||
uint32_t k_valid,
|
||||
bool deinterleave_f16) {
|
||||
if (norm_w->type == HTP_TYPE_F16) {
|
||||
const size_t nbytes = (size_t) k_valid * sizeof(__fp16);
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_tmp, norm_w->data), hex_align_up(nbytes, 128), 0, nbytes, 1);
|
||||
dma_queue_pop(dma_q);
|
||||
hvx_copy_f32_f16_aa((uint8_t *) vtcm_norm, (const uint8_t *) vtcm_tmp, k_valid);
|
||||
} else {
|
||||
const size_t nbytes = (size_t) k_valid * sizeof(float);
|
||||
dma_queue_push(dma_q, dma_make_data(vtcm_norm, norm_w->data), hex_align_up(nbytes, 128), 0, nbytes, 1);
|
||||
dma_queue_pop(dma_q);
|
||||
}
|
||||
if (deinterleave_f16) {
|
||||
const uint32_t k_pad = hex_align_up(k_valid, 64u);
|
||||
if (k_pad > k_valid) {
|
||||
memset(vtcm_norm + k_valid, 0, (size_t) (k_pad - k_valid) * sizeof(float));
|
||||
}
|
||||
HVX_Vector * vw = (HVX_Vector *) vtcm_norm;
|
||||
const uint32_t npairs = k_pad >> 6;
|
||||
for (uint32_t i = 0; i < npairs; ++i) {
|
||||
HVX_VectorPair wp = Q6_W_vdeal_VVR(vw[2 * i + 1], vw[2 * i], -4);
|
||||
vw[2 * i] = Q6_V_lo_W(wp);
|
||||
vw[2 * i + 1] = Q6_V_hi_W(wp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void hvx_rms_norm_mul_rows_f32(
|
||||
uint8_t * restrict curr_buf,
|
||||
size_t row_bytes,
|
||||
uint32_t valid_rows,
|
||||
uint32_t k_valid,
|
||||
const float * restrict vtcm_norm,
|
||||
float norm_eps);
|
||||
|
||||
void hvx_rms_norm_mul_rows_f16(
|
||||
uint8_t * restrict curr_buf,
|
||||
size_t row_bytes,
|
||||
uint32_t valid_rows,
|
||||
uint32_t k_valid,
|
||||
const float * restrict vtcm_norm,
|
||||
float norm_eps);
|
||||
|
||||
typedef void (*hmx_glu_store_quad_fn_t)(
|
||||
void * restrict dst_ptr,
|
||||
size_t dst_stride,
|
||||
const HVX_Vector * restrict g_vec,
|
||||
const HVX_Vector * restrict u_vec,
|
||||
uint32_t valid_rows);
|
||||
|
||||
typedef void (*hmx_glu_store_tail_fn_t)(
|
||||
void * restrict dst_ptr,
|
||||
size_t dst_stride,
|
||||
const HVX_Vector * restrict g_vec,
|
||||
const HVX_Vector * restrict u_vec,
|
||||
uint32_t rows_and_bytes);
|
||||
|
||||
typedef void (*hmx_glu_store_strip_fn_t)(
|
||||
void * restrict dst_ptr,
|
||||
size_t dst_stride,
|
||||
const __fp16 * restrict u_buf,
|
||||
size_t u_stride,
|
||||
size_t n_pairs);
|
||||
|
||||
typedef void (*hmx_output_glu_col_chunk_fn_t)(
|
||||
const void * st_void,
|
||||
void * restrict dst_ptr,
|
||||
uint32_t start_row,
|
||||
uint32_t n_rows,
|
||||
uint32_t c_first,
|
||||
uint32_t c_len_and_dst_cols);
|
||||
|
||||
typedef struct {
|
||||
void * dst;
|
||||
const uint8_t * vtcm_gate;
|
||||
const uint8_t * vtcm_up;
|
||||
const float * vtcm_act_scales;
|
||||
const float * vtcm_wt_scales_gate;
|
||||
const float * vtcm_wt_scales_up;
|
||||
uint32_t w_scale_gate_c_mask;
|
||||
uint32_t w_scale_up_c_mask;
|
||||
uint32_t n_rows;
|
||||
uint32_t mc_pad;
|
||||
uint32_t n_cols;
|
||||
uint32_t n_groups;
|
||||
uint32_t dst_stride;
|
||||
uint32_t dst_cols;
|
||||
size_t dst_elem_size;
|
||||
uint32_t n_tasks;
|
||||
uint32_t n_chunks_per_task;
|
||||
struct fastdiv_values n_hvx_div;
|
||||
hmx_output_glu_col_chunk_fn_t col_chunk_fn;
|
||||
hmx_glu_store_quad_fn_t store_quad_fn;
|
||||
hmx_glu_store_tail_fn_t store_tail_fn;
|
||||
hmx_glu_store_strip_fn_t store_strip_fn;
|
||||
} hmx_output_glu_state_t;
|
||||
|
||||
bool resolve_hmx_glu_store_fns(
|
||||
uint8_t glu_op,
|
||||
size_t dst_elem_size,
|
||||
hmx_glu_store_quad_fn_t * store_quad_fn,
|
||||
hmx_glu_store_tail_fn_t * store_tail_fn,
|
||||
hmx_glu_store_strip_fn_t * store_strip_fn);
|
||||
|
||||
void hmx_transfer_output_glu(
|
||||
struct htp_ops_context * octx,
|
||||
hmx_output_glu_state_t * state,
|
||||
void * dst,
|
||||
const void * vtcm_gate,
|
||||
const void * vtcm_up,
|
||||
const float * vtcm_wt_scales_gate,
|
||||
const float * vtcm_wt_scales_up,
|
||||
int n_rows,
|
||||
int n_cols,
|
||||
int dst_cols);
|
||||
|
||||
typedef struct {
|
||||
dma_queue * weight_dma;
|
||||
dma_addr_t dst_dma_addr;
|
||||
dma_addr_t bias_addr;
|
||||
size_t bias_bytes;
|
||||
dma_addr_t scale_addr;
|
||||
size_t scale_bytes;
|
||||
bool scale_per_channel;
|
||||
dma_addr_t act_dma_addr;
|
||||
dma_addr_t weight;
|
||||
int m;
|
||||
int k;
|
||||
int n;
|
||||
int act_stride;
|
||||
uint32_t act_elem_size; // 4=F32 src1, 2=F16 src1
|
||||
int weight_stride;
|
||||
int weight_type;
|
||||
int k_valid;
|
||||
int dst_stride;
|
||||
uint32_t bias_stride;
|
||||
int dst_cols;
|
||||
uint32_t dst_elem_size; // 4=F32 dst, 2=F16 dst
|
||||
uint32_t bias_elem_size; // 4=F32 bias, 2=F16 bias
|
||||
} hmx_mm_2d_params_t;
|
||||
|
||||
typedef struct {
|
||||
dma_queue * weight_dma;
|
||||
const dma_addr_t * dsts;
|
||||
dma_addr_t activation;
|
||||
size_t act_elem_size;
|
||||
size_t dst_elem_size;
|
||||
const dma_addr_t * weights;
|
||||
uint32_t n_weights;
|
||||
int m;
|
||||
int k;
|
||||
int n;
|
||||
int k_valid;
|
||||
int ne11;
|
||||
size_t act_nb1;
|
||||
size_t act_nb2;
|
||||
size_t dst_nb1;
|
||||
size_t dst_nb2;
|
||||
int weight_stride;
|
||||
int weight_type;
|
||||
const struct mmid_row_mapping * matrix_rows;
|
||||
int cur_a;
|
||||
int mapping_stride;
|
||||
int m_start;
|
||||
int m_end;
|
||||
const dma_addr_t * scale_addrs;
|
||||
const size_t * scale_bytes_arr;
|
||||
const uint8_t * scale_per_ch_arr;
|
||||
} hmx_mm_id_2d_params_t;
|
||||
|
||||
typedef struct {
|
||||
dma_addr_t dst_dma_addr;
|
||||
dma_addr_t bias_addr;
|
||||
size_t bias_bytes;
|
||||
dma_addr_t scale_addr;
|
||||
size_t scale_bytes;
|
||||
uint8_t scale_per_channel;
|
||||
dma_addr_t act_dma_addr;
|
||||
dma_addr_t weight;
|
||||
dma_queue * weight_dma;
|
||||
int m;
|
||||
int k;
|
||||
int n;
|
||||
int act_stride;
|
||||
uint32_t act_elem_size; // 4=F32 src1, 2=F16 src1
|
||||
uint32_t dst_elem_size; // 4=F32 dst, 2=F16 dst
|
||||
uint32_t bias_elem_size; // 4=F32 bias, 2=F16 bias
|
||||
int weight_stride;
|
||||
int weight_type;
|
||||
int dst_stride;
|
||||
int dst_cols;
|
||||
uint32_t bias_stride;
|
||||
int ne02;
|
||||
int ne03;
|
||||
int ne12;
|
||||
int ne13;
|
||||
size_t src0_nb2;
|
||||
size_t src0_nb3;
|
||||
size_t act_nb2;
|
||||
size_t act_nb3;
|
||||
size_t bias_nb2;
|
||||
size_t bias_nb3;
|
||||
size_t dst_nb2;
|
||||
size_t dst_nb3;
|
||||
int r2;
|
||||
int r3;
|
||||
struct fastdiv_values div_r2;
|
||||
struct fastdiv_values div_r3;
|
||||
} hmx_mm_batched_params_t;
|
||||
|
||||
static inline dma_addr_t hmx_mm_weight_batch_data(const hmx_mm_batched_params_t * params, int dst_b2, int dst_b3) {
|
||||
const size_t b2_idx = (params->r2 <= 1) ? (size_t) dst_b2 : (size_t) fastdiv((uint32_t) dst_b2, ¶ms->div_r2);
|
||||
const size_t b3_idx = (params->r3 <= 1) ? (size_t) dst_b3 : (size_t) fastdiv((uint32_t) dst_b3, ¶ms->div_r3);
|
||||
return params->weight + b2_idx * params->src0_nb2 + b3_idx * params->src0_nb3;
|
||||
}
|
||||
|
||||
static inline dma_addr_t hmx_mm_act_batch_addr(const hmx_mm_batched_params_t * params, int dst_b2, int dst_b3) {
|
||||
return params->act_dma_addr + dst_b2 * params->act_nb2 + dst_b3 * params->act_nb3;
|
||||
}
|
||||
|
||||
static inline dma_addr_t hmx_mm_dst_batch_addr(const hmx_mm_batched_params_t * params, int dst_b2, int dst_b3) {
|
||||
return params->dst_dma_addr + (size_t) dst_b2 * params->dst_nb2 + (size_t) dst_b3 * params->dst_nb3;
|
||||
}
|
||||
|
||||
static inline void push_gathered_act_rows(
|
||||
dma_queue * dma_q,
|
||||
uint8_t * slot_buf,
|
||||
dma_addr_t act_dma_addr,
|
||||
uint32_t valid_rows,
|
||||
uint32_t start_row,
|
||||
uint32_t r,
|
||||
size_t row_bytes,
|
||||
size_t valid_bytes,
|
||||
const struct mmid_row_mapping * matrix_rows,
|
||||
uint32_t cur_a,
|
||||
uint32_t mapping_stride,
|
||||
uint32_t ne11,
|
||||
const struct fastdiv_values * ne11_div,
|
||||
size_t nb11,
|
||||
size_t nb12) {
|
||||
const size_t base_idx = (size_t) cur_a * mapping_stride + start_row + r;
|
||||
if (ne11 == 1) {
|
||||
for (uint32_t s = 0; s < valid_rows; ++s) {
|
||||
const struct mmid_row_mapping mapping = matrix_rows[base_idx + s];
|
||||
const dma_addr_t src_row = act_dma_addr + (size_t) mapping.i2 * nb12;
|
||||
dma_queue_push(dma_q, dma_make_data(slot_buf + (size_t) s * row_bytes, src_row),
|
||||
valid_bytes, valid_bytes, valid_bytes, 1);
|
||||
}
|
||||
} else {
|
||||
for (uint32_t s = 0; s < valid_rows; ++s) {
|
||||
const struct mmid_row_mapping mapping = matrix_rows[base_idx + s];
|
||||
const uint32_t i11 = fastmodulo(mapping.i1, ne11, ne11_div);
|
||||
const dma_addr_t src_row = act_dma_addr + (size_t) i11 * nb11 + (size_t) mapping.i2 * nb12;
|
||||
dma_queue_push(dma_q, dma_make_data(slot_buf + (size_t) s * row_bytes, src_row),
|
||||
valid_bytes, valid_bytes, valid_bytes, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void push_scattered_dst_rows(
|
||||
dma_queue * dma_q,
|
||||
dma_addr_t dst_dma_addr,
|
||||
const uint8_t * vtcm_src,
|
||||
uint32_t start_row,
|
||||
uint32_t n_rows,
|
||||
size_t vtcm_row_bytes,
|
||||
size_t copy_bytes,
|
||||
const struct mmid_row_mapping * matrix_rows,
|
||||
uint32_t cur_a,
|
||||
uint32_t mapping_stride,
|
||||
size_t dst_nb1,
|
||||
size_t dst_nb2) {
|
||||
const size_t base_idx = (size_t) cur_a * mapping_stride + start_row;
|
||||
for (uint32_t r = 0; r < n_rows; ++r) {
|
||||
const struct mmid_row_mapping mapping = matrix_rows[base_idx + r];
|
||||
const dma_addr_t dst_row = dst_dma_addr + (size_t) mapping.i1 * dst_nb1 + (size_t) mapping.i2 * dst_nb2;
|
||||
dma_queue_push(dma_q, dma_make_data(dst_row, vtcm_src + (size_t) r * vtcm_row_bytes),
|
||||
copy_bytes, copy_bytes, copy_bytes, 1);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void sync_scattered_dst_rows(
|
||||
dma_queue * dma_q,
|
||||
dma_addr_t dst_dma_addr,
|
||||
const uint8_t * vtcm_src,
|
||||
uint32_t start_row,
|
||||
uint32_t n_rows,
|
||||
size_t vtcm_row_bytes,
|
||||
size_t copy_bytes,
|
||||
const struct mmid_row_mapping * matrix_rows,
|
||||
uint32_t cur_a,
|
||||
uint32_t mapping_stride,
|
||||
size_t dst_nb1,
|
||||
size_t dst_nb2) {
|
||||
for (uint32_t r = 0; r < n_rows; r += 128u) {
|
||||
const uint32_t batch = hex_smin(n_rows - r, 128u);
|
||||
push_scattered_dst_rows(dma_q, dst_dma_addr, vtcm_src + (size_t) r * vtcm_row_bytes,
|
||||
start_row + r, batch, vtcm_row_bytes, copy_bytes,
|
||||
matrix_rows, cur_a, mapping_stride, dst_nb1, dst_nb2);
|
||||
for (uint32_t s = 0; s < batch; ++s) {
|
||||
dma_queue_pop(dma_q);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct htp_mmid_mapping {
|
||||
const int32_t * ids_data;
|
||||
uint32_t * matrix_row_counts;
|
||||
struct mmid_row_mapping * matrix_rows;
|
||||
uint32_t mapping_stride;
|
||||
uint32_t n_skipped;
|
||||
};
|
||||
|
||||
#endif // HTP_MATMUL_INTERNAL_H
|
||||
+922
-6565
File diff suppressed because it is too large
Load Diff
@@ -48,8 +48,8 @@ enum htp_mm_kernel_type {
|
||||
HTP_MM_KERNEL_UNSUPPORTED = 0,
|
||||
|
||||
// HMX paths
|
||||
HTP_MM_KERNEL_HMX_2D,
|
||||
HTP_MM_KERNEL_HMX_BATCHED,
|
||||
HTP_MM_KERNEL_HMX_F16_2D,
|
||||
HTP_MM_KERNEL_HMX_F16_BATCHED,
|
||||
HTP_MM_KERNEL_HMX_W8A8_2D,
|
||||
HTP_MM_KERNEL_HMX_W8A8_BATCHED,
|
||||
|
||||
@@ -67,19 +67,19 @@ static inline bool htp_mm_kernel_is_hmx_w8a8(int32_t kt) {
|
||||
return kt == HTP_MM_KERNEL_HMX_W8A8_2D || kt == HTP_MM_KERNEL_HMX_W8A8_BATCHED;
|
||||
}
|
||||
|
||||
static inline bool htp_mm_kernel_is_hmx_int(int32_t kt) {
|
||||
return htp_mm_kernel_is_hmx_w8a8(kt);
|
||||
static inline bool htp_mm_kernel_is_hmx_f16(int32_t kt) {
|
||||
return kt == HTP_MM_KERNEL_HMX_F16_2D || kt == HTP_MM_KERNEL_HMX_F16_BATCHED;
|
||||
}
|
||||
|
||||
static inline bool htp_mm_kernel_is_hmx_2d(int32_t kt) {
|
||||
return kt == HTP_MM_KERNEL_HMX_2D || kt == HTP_MM_KERNEL_HMX_W8A8_2D;
|
||||
return kt == HTP_MM_KERNEL_HMX_F16_2D || kt == HTP_MM_KERNEL_HMX_W8A8_2D;
|
||||
}
|
||||
|
||||
static inline bool htp_mm_kernel_is_hmx_batched(int32_t kt) {
|
||||
return kt == HTP_MM_KERNEL_HMX_BATCHED || kt == HTP_MM_KERNEL_HMX_W8A8_BATCHED;
|
||||
return kt == HTP_MM_KERNEL_HMX_F16_BATCHED || kt == HTP_MM_KERNEL_HMX_W8A8_BATCHED;
|
||||
}
|
||||
|
||||
static inline bool htp_is_int_hmx_weight_type(int wtype) {
|
||||
static inline bool htp_is_hmx_w8a8_weight_type(int wtype) {
|
||||
return wtype == HTP_TYPE_Q4_0 || wtype == HTP_TYPE_Q8_0 ||
|
||||
wtype == HTP_TYPE_IQ4_NL || wtype == HTP_TYPE_MXFP4 ||
|
||||
wtype == HTP_TYPE_Q5_0 || wtype == HTP_TYPE_Q1_0 ||
|
||||
@@ -87,10 +87,11 @@ static inline bool htp_is_int_hmx_weight_type(int wtype) {
|
||||
wtype == HTP_TYPE_Q3_K || wtype == HTP_TYPE_Q6_K ||
|
||||
wtype == HTP_TYPE_IQ2_XXS || wtype == HTP_TYPE_Q4_1 ||
|
||||
wtype == HTP_TYPE_Q4_K || wtype == HTP_TYPE_Q5_1 ||
|
||||
wtype == HTP_TYPE_Q5_K || wtype == HTP_TYPE_Q2_K;
|
||||
wtype == HTP_TYPE_Q5_K || wtype == HTP_TYPE_Q2_K ||
|
||||
wtype == HTP_TYPE_F8_E4M3;
|
||||
}
|
||||
|
||||
static inline uint32_t htp_int_hmx_min_k(int wtype) {
|
||||
static inline uint32_t htp_hmx_w8a8_min_k(int wtype) {
|
||||
switch (wtype) {
|
||||
case HTP_TYPE_IQ2_XXS:
|
||||
case HTP_TYPE_Q2_K:
|
||||
@@ -106,26 +107,75 @@ static inline uint32_t htp_int_hmx_min_k(int wtype) {
|
||||
}
|
||||
}
|
||||
|
||||
enum htp_mm_fused_scale_mode {
|
||||
HTP_MM_FUSED_SCALE_PER_TENSOR = 0,
|
||||
HTP_MM_FUSED_SCALE_PER_CHANNEL = 1,
|
||||
HTP_MM_FUSED_SCALE_PER_EXPERT = 2,
|
||||
HTP_MM_FUSED_SCALE_PER_CHANNEL_EXPERT = 3,
|
||||
};
|
||||
|
||||
static inline uint32_t htp_mm_scale_mode_get(uint16_t packed_mode, uint32_t w_idx) {
|
||||
return (packed_mode >> (2u * w_idx)) & 3u;
|
||||
}
|
||||
|
||||
static inline bool htp_mm_scale_is_per_channel(uint32_t mode) {
|
||||
return mode == HTP_MM_FUSED_SCALE_PER_CHANNEL || mode == HTP_MM_FUSED_SCALE_PER_CHANNEL_EXPERT;
|
||||
}
|
||||
|
||||
static inline size_t htp_mm_fused_scale_vtcm_bytes(uint32_t ne01, uint32_t ne02, uint32_t smode, bool for_hmx) {
|
||||
switch (smode) {
|
||||
case HTP_MM_FUSED_SCALE_PER_TENSOR:
|
||||
return 128;
|
||||
case HTP_MM_FUSED_SCALE_PER_CHANNEL:
|
||||
return hex_round_up((size_t) ne01 * sizeof(float), 128);
|
||||
case HTP_MM_FUSED_SCALE_PER_EXPERT:
|
||||
return for_hmx ? 128 : hex_round_up((size_t) ne02 * sizeof(float), 128);
|
||||
case HTP_MM_FUSED_SCALE_PER_CHANNEL_EXPERT:
|
||||
return for_hmx
|
||||
? hex_round_up((size_t) ne01 * sizeof(float), 128)
|
||||
: hex_round_up((size_t) ne01 * (size_t) ne02 * sizeof(float), 128);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static inline size_t htp_mm_fused_norm_vtcm_bytes(uint32_t k) {
|
||||
return hex_align_up((size_t) k * sizeof(float), 128);
|
||||
}
|
||||
|
||||
enum htp_mm_flags {
|
||||
HTP_MM_FLAG_PIPELINE = (1u << 0), // Pipelined execution
|
||||
HTP_MM_FLAG_FWHT = (1u << 1), // Online block-32 FWHT rotation along K
|
||||
HTP_MM_FLAG_GROUPED = (1u << 2), // Grouped batched HMX-F16 layout
|
||||
};
|
||||
|
||||
// Op-specific struct for precomputed matmul params
|
||||
struct htp_mm_kernel_params {
|
||||
// Kernel selection and mode variants
|
||||
uint8_t kernel_type; // enum htp_mm_kernel_type
|
||||
uint8_t pipeline; // 1 = pipelined execution, 0 = standard
|
||||
uint8_t flags; // bitmask of enum htp_mm_flags
|
||||
uint8_t glu_op; // HTP_OP_GLU_* for fused MUL_MAT_NX_GLU (0 if none)
|
||||
uint8_t norm_w_idx; // Input index of norm weight in octx->src[] (0 if none)
|
||||
|
||||
// Execution resources and fusion counts
|
||||
uint8_t n_hmx; // 1 = use HMX, 0 = use HVX
|
||||
uint8_t n_hvx; // Number of HVX threads
|
||||
|
||||
uint8_t n_act_hvx; // Number of HVX for activation preparation
|
||||
uint8_t n_act_hvx; // Number of HVX threads for activation preparation
|
||||
uint8_t n_prefetch; // Prefetch lookahead buffers/rows in VTCM
|
||||
uint8_t n_weights; // Number of weights for fused NX
|
||||
uint8_t glu_op; // HTP_OP_GLU_* for fused MUL_MAT_NX_GLU (0 if none)
|
||||
uint8_t scale_mask; // Bitmask of weights 0..n_weights-1 with fused scale
|
||||
uint16_t scale_mode; // Packed 2-bit mode per weight (enum htp_mm_fused_scale_mode)
|
||||
|
||||
int32_t m_chunk; // Row chunk size (M chunk)
|
||||
int32_t n_chunk; // Col chunk size (N chunk)
|
||||
int32_t tile_size; // Weight tile size
|
||||
int32_t aligned_tile_size; // Aligned weight tile size (padded to 128)
|
||||
int32_t act_row_size; // Row size for activation scratchpad
|
||||
int32_t vtcm_size; // Total required scratchpad size in VTCM
|
||||
int32_t vtcm_bias_size; // bias scratchpad size in VTCM (fused only)
|
||||
int32_t hmx_n_groups; // Number of K superblocks for INT HMX kernels (1 for FP16)
|
||||
// Tiling and VTCM layout
|
||||
uint32_t m_chunk; // Row chunk size (M chunk)
|
||||
uint32_t n_chunk; // Col chunk size (N chunk)
|
||||
uint32_t tile_size; // Weight tile size
|
||||
uint32_t aligned_tile_size; // Aligned weight tile size (padded to 128)
|
||||
uint32_t act_row_size; // Row size for activation scratchpad
|
||||
uint32_t vtcm_size; // Total required scratchpad size in VTCM
|
||||
uint32_t vtcm_bias_size; // Bias scratchpad size in VTCM (fused only)
|
||||
uint32_t vtcm_scale_size; // Weight scale scratchpad size in VTCM (fused only)
|
||||
uint32_t hmx_n_groups; // Number of K superblocks for W8A8 HMX kernels (1 for FP16)
|
||||
|
||||
// Precomputed division values
|
||||
struct fastdiv_values div_ne12_ne1;
|
||||
@@ -137,12 +187,17 @@ struct htp_mm_kernel_params {
|
||||
struct fastdiv_values div_ne00_padded;
|
||||
struct fastdiv_values div_hmx_n_groups;
|
||||
struct fastdiv_values div_n_k_tiles;
|
||||
|
||||
// Fused RMS_NORM_MUL -> HMX activation prep
|
||||
float norm_eps; // RMSNorm epsilon
|
||||
};
|
||||
|
||||
#if defined(__cplusplus)
|
||||
static_assert(sizeof(struct htp_mm_kernel_params) <= 128, "htp_matmul_kernel_params is too large for kernel_params blob");
|
||||
static_assert(HTP_OP_MAX_OUTPUTS <= 8, "HTP_OP_MAX_OUTPUTS exceeds scale_mask (8 bits) and scale_mode (8 x 2 bits)");
|
||||
#else
|
||||
_Static_assert(sizeof(struct htp_mm_kernel_params) <= 128, "htp_matmul_kernel_params is too large for kernel_params blob");
|
||||
_Static_assert(HTP_OP_MAX_OUTPUTS <= 8, "HTP_OP_MAX_OUTPUTS exceeds scale_mask (8 bits) and scale_mode (8 x 2 bits)");
|
||||
#endif
|
||||
|
||||
struct mmid_row_mapping {
|
||||
@@ -261,7 +316,7 @@ static inline size_t htp_mm_f16_tiled_row_size(uint32_t ne) {
|
||||
}
|
||||
|
||||
static inline size_t htp_mm_tiled_act_row_size(int weight_type, uint32_t ne) {
|
||||
if (weight_type == HTP_TYPE_F16) {
|
||||
if (weight_type == HTP_TYPE_F16 || weight_type == HTP_TYPE_F8_E4M3) {
|
||||
return htp_mm_f16_tiled_row_size(ne);
|
||||
}
|
||||
return htp_mm_weight_has_offset(weight_type) ? htp_mm_q8_1_tiled_row_size(ne) : htp_mm_q8_0_tiled_row_size(ne);
|
||||
@@ -288,7 +343,7 @@ static inline void htp_mm_hmx_get_2d_chunk_costs_ex(
|
||||
const uint32_t n_weight_tiles = k / htp_tensor_get_tile_k(wtype);
|
||||
const size_t qweight_row_stride = is_quant ? (size_t)(n_weight_tiles * aligned_tile_size) / 32 : 0;
|
||||
|
||||
if (htp_mm_kernel_is_hmx_int(kernel_type)) {
|
||||
if (htp_mm_kernel_is_hmx_w8a8(kernel_type)) {
|
||||
const size_t n_groups = (k + HTP_MM_HMX_W8A8_KGROUP - 1u) / HTP_MM_HMX_W8A8_KGROUP;
|
||||
const size_t w_unpacked_per_n = (size_t) k * sizeof(uint8_t) + n_groups * 20;
|
||||
|
||||
@@ -309,7 +364,7 @@ static inline void htp_mm_hmx_get_2d_chunk_costs(
|
||||
int wtype, uint32_t k, bool pipeline, uint32_t aligned_tile_size,
|
||||
size_t * size_per_n_out, size_t * size_per_m_out, size_t * size_per_mn_out
|
||||
) {
|
||||
htp_mm_hmx_get_2d_chunk_costs_ex(HTP_MM_KERNEL_HMX_2D, wtype, k, pipeline, aligned_tile_size,
|
||||
htp_mm_hmx_get_2d_chunk_costs_ex(HTP_MM_KERNEL_HMX_F16_2D, wtype, k, pipeline, aligned_tile_size,
|
||||
size_per_n_out, size_per_m_out, size_per_mn_out);
|
||||
}
|
||||
|
||||
@@ -351,6 +406,8 @@ struct htp_mm_hmx_vtcm_layout {
|
||||
size_t off_scratch[2]; // dequantization scratch pads
|
||||
size_t off_scales; // HMX scales (256 bytes)
|
||||
size_t off_bias; // bias in VTCM
|
||||
size_t off_scale; // weight scale in VTCM
|
||||
size_t off_norm; // fused RMSNorm weight in VTCM (F32)
|
||||
size_t off_lut; // IQ2_XXS grid + sign tables (2D only)
|
||||
|
||||
// Cached sizes of regions for HMX kernel use
|
||||
@@ -368,6 +425,8 @@ struct htp_mm_hmx_vtcm_layout {
|
||||
size_t weight_scales_offset;
|
||||
size_t act_head_stride;
|
||||
size_t bias_bytes;
|
||||
size_t scale_bytes;
|
||||
size_t norm_bytes;
|
||||
size_t lut_bytes;
|
||||
|
||||
size_t total_bytes;
|
||||
@@ -379,6 +438,7 @@ struct htp_mm_hvx_vtcm_layout {
|
||||
size_t off_act; // vtcm_act (activation)
|
||||
size_t off_src0; // vtcm_src0 (weight/Wk)
|
||||
size_t off_bias; // vtcm_bias (bias / fused add only)
|
||||
size_t off_scale; // vtcm_scale (fused weight scale)
|
||||
size_t off_dst; // vtcm_dst (output scratch)
|
||||
size_t off_act_raw; // vtcm_act_raw (raw activation DMA staging)
|
||||
size_t off_lut; // vtcm_lut (IQ2_XXS grid + sign tables)
|
||||
@@ -387,6 +447,7 @@ struct htp_mm_hvx_vtcm_layout {
|
||||
size_t src0_bytes;
|
||||
size_t act_bytes;
|
||||
size_t bias_bytes;
|
||||
size_t scale_bytes;
|
||||
size_t dst_bytes;
|
||||
size_t act_raw_bytes;
|
||||
size_t lut_bytes;
|
||||
@@ -408,11 +469,17 @@ static inline void htp_mm_hmx_vtcm_layout_build_core(
|
||||
size_t bias_size,
|
||||
size_t res_in_elem_size,
|
||||
size_t res_out_elem_size,
|
||||
bool is_glu
|
||||
bool is_glu,
|
||||
size_t scale_size,
|
||||
size_t norm_size
|
||||
) {
|
||||
size_t off = 0;
|
||||
L->lut_bytes = 0;
|
||||
L->off_lut = 0;
|
||||
L->off_scale = 0;
|
||||
L->scale_bytes = 0;
|
||||
L->off_norm = 0;
|
||||
L->norm_bytes = 0;
|
||||
L->off_act_scales = 0;
|
||||
L->can_preissue_weight = false;
|
||||
L->act_scales_bytes = 0;
|
||||
@@ -433,7 +500,7 @@ static inline void htp_mm_hmx_vtcm_layout_build_core(
|
||||
L->weight_bias_offset = 0;
|
||||
L->weight_scales_offset = 0;
|
||||
|
||||
if (kernel_type == HTP_MM_KERNEL_HMX_BATCHED) {
|
||||
if (kernel_type == HTP_MM_KERNEL_HMX_F16_BATCHED) {
|
||||
const size_t vec_dot_size = k * sizeof(uint16_t);
|
||||
const size_t act_head_stride = mc * k;
|
||||
const size_t weight_area_size = hex_align_up(nc * vec_dot_size, HTP_MM_HMX_TILE_SIZE);
|
||||
@@ -482,13 +549,13 @@ static inline void htp_mm_hmx_vtcm_layout_build_core(
|
||||
|
||||
off = off_group_a + hex_smax(group_b_size, group_c_size);
|
||||
} else {
|
||||
// HTP_MM_KERNEL_HMX_2D / W8A8
|
||||
const bool is_int_hmx = htp_mm_kernel_is_hmx_int(kernel_type);
|
||||
// HTP_MM_KERNEL_HMX_F16_2D / W8A8
|
||||
const bool is_w8a8 = htp_mm_kernel_is_hmx_w8a8(kernel_type);
|
||||
const bool is_quant = (wtype != HTP_TYPE_F16 && wtype != HTP_TYPE_F32 && wtype != HTP_TYPE_BF16);
|
||||
const size_t row_stride = htp_tensor_get_row_size(wtype, k);
|
||||
const uint32_t n_weight_tiles = k / htp_tensor_get_tile_k(wtype);
|
||||
|
||||
const uint32_t dma_act_mult = is_int_hmx ? 8u : HTP_MM_DMA_ACT_MULTIPLIER;
|
||||
const uint32_t dma_act_mult = is_w8a8 ? 8u : HTP_MM_DMA_ACT_MULTIPLIER;
|
||||
const size_t min_f32_size = hex_align_up((size_t) act_hvx * dma_act_mult * k * sizeof(float), 128);
|
||||
const size_t weight_area_size = is_quant
|
||||
? hex_align_up((nc / 32) * n_weight_tiles * aligned_tile_size, HTP_MM_HMX_TILE_SIZE)
|
||||
@@ -499,7 +566,7 @@ static inline void htp_mm_hmx_vtcm_layout_build_core(
|
||||
size_t output_area_size = 0;
|
||||
size_t scratch0_size = 0;
|
||||
|
||||
if (is_int_hmx) {
|
||||
if (is_w8a8) {
|
||||
const size_t n_groups = (k + HTP_MM_HMX_W8A8_KGROUP - 1u) / HTP_MM_HMX_W8A8_KGROUP;
|
||||
const size_t mc_pad = hex_align_up(mc, 64);
|
||||
const size_t nc_pad = hex_align_up(nc, 32);
|
||||
@@ -532,10 +599,12 @@ static inline void htp_mm_hmx_vtcm_layout_build_core(
|
||||
|
||||
// Group A: Scales and activation tiles (must not overlap with Group B or C)
|
||||
size_t off_group_a = 0;
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_scales, HTP_MM_HMX_TILE_SIZE, !is_int_hmx);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_scales, HTP_MM_HMX_TILE_SIZE, !is_w8a8);
|
||||
VTCM_LAYOUT_ALLOC(off_group_a, off_act, act_area_size);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_act_scales, act_scales_size, act_scales_size > 0);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_bias, hex_align_up(bias_size, HTP_MM_HMX_TILE_SIZE), bias_size > 0);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_scale, hex_align_up(scale_size, HTP_MM_HMX_TILE_SIZE), scale_size > 0);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_norm, hex_align_up(norm_size, HTP_MM_HMX_TILE_SIZE), norm_size > 0);
|
||||
|
||||
// Group B: Compute-only buffers (starts at off_group_a)
|
||||
size_t off_group_b = off_group_a;
|
||||
@@ -565,7 +634,7 @@ static inline void htp_mm_hmx_vtcm_layout_build_core(
|
||||
const size_t avail_after_w = off_group_b - off_after_weights;
|
||||
size_t act_f32_size;
|
||||
size_t group_c_size;
|
||||
if (is_int_hmx && avail_after_w >= min_f32_size) {
|
||||
if (is_w8a8 && avail_after_w >= min_f32_size) {
|
||||
act_f32_size = hex_align_down(hex_smin(max_f32_size, avail_after_w), 128);
|
||||
L->off_act_f32 = off_after_weights;
|
||||
group_c_size = group_b_size;
|
||||
@@ -588,6 +657,8 @@ static inline void htp_mm_hmx_vtcm_layout_build_core(
|
||||
L->scratch_bytes[1] = scratch1_size;
|
||||
L->act_head_stride = 0;
|
||||
L->bias_bytes = bias_size;
|
||||
L->scale_bytes = scale_size;
|
||||
L->norm_bytes = norm_size;
|
||||
|
||||
off = off_group_a + hex_smax(group_b_size, group_c_size);
|
||||
|
||||
@@ -598,6 +669,26 @@ static inline void htp_mm_hmx_vtcm_layout_build_core(
|
||||
L->total_bytes = off;
|
||||
}
|
||||
|
||||
static inline void htp_mm_hmx_vtcm_layout_build_ex_scaled(
|
||||
struct htp_mm_hmx_vtcm_layout * L,
|
||||
int kernel_type,
|
||||
int wtype,
|
||||
uint32_t k,
|
||||
size_t mc,
|
||||
size_t nc,
|
||||
uint32_t group_size,
|
||||
bool pipeline,
|
||||
uint32_t act_hvx,
|
||||
uint32_t aligned_tile_size,
|
||||
size_t bias_size,
|
||||
size_t scale_size,
|
||||
size_t res_in_elem_size,
|
||||
size_t res_out_elem_size,
|
||||
size_t norm_size
|
||||
) {
|
||||
htp_mm_hmx_vtcm_layout_build_core(L, kernel_type, wtype, k, mc, nc, group_size, pipeline, act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, false, scale_size, norm_size);
|
||||
}
|
||||
|
||||
static inline void htp_mm_hmx_vtcm_layout_build_ex(
|
||||
struct htp_mm_hmx_vtcm_layout * L,
|
||||
int kernel_type,
|
||||
@@ -613,7 +704,7 @@ static inline void htp_mm_hmx_vtcm_layout_build_ex(
|
||||
size_t res_in_elem_size,
|
||||
size_t res_out_elem_size
|
||||
) {
|
||||
htp_mm_hmx_vtcm_layout_build_core(L, kernel_type, wtype, k, mc, nc, group_size, pipeline, act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, false);
|
||||
htp_mm_hmx_vtcm_layout_build_core(L, kernel_type, wtype, k, mc, nc, group_size, pipeline, act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, false, 0, 0);
|
||||
}
|
||||
|
||||
static inline void htp_mm_hmx_vtcm_layout_build_glu(
|
||||
@@ -626,9 +717,11 @@ static inline void htp_mm_hmx_vtcm_layout_build_glu(
|
||||
bool pipeline,
|
||||
uint32_t act_hvx,
|
||||
uint32_t aligned_tile_size,
|
||||
size_t res_out_elem_size
|
||||
size_t res_out_elem_size,
|
||||
size_t scale_size,
|
||||
size_t norm_size
|
||||
) {
|
||||
htp_mm_hmx_vtcm_layout_build_core(L, kernel_type, wtype, k, mc, nc, 1, pipeline, act_hvx, aligned_tile_size, 0, 0, res_out_elem_size, true);
|
||||
htp_mm_hmx_vtcm_layout_build_core(L, kernel_type, wtype, k, mc, nc, 1, pipeline, act_hvx, aligned_tile_size, 0, 0, res_out_elem_size, true, scale_size, norm_size);
|
||||
}
|
||||
|
||||
static inline void htp_mm_hmx_vtcm_layout_build(
|
||||
@@ -647,7 +740,7 @@ static inline void htp_mm_hmx_vtcm_layout_build(
|
||||
htp_mm_hmx_vtcm_layout_build_ex(L, kernel_type, wtype, k, mc, nc, group_size, pipeline, act_hvx, aligned_tile_size, bias_size, 0, 0);
|
||||
}
|
||||
|
||||
static inline void htp_mm_hvx_vtcm_layout_build(
|
||||
static inline void htp_mm_hvx_vtcm_layout_build_ex(
|
||||
struct htp_mm_hvx_vtcm_layout * L,
|
||||
int kernel_type,
|
||||
int wtype,
|
||||
@@ -658,6 +751,7 @@ static inline void htp_mm_hvx_vtcm_layout_build(
|
||||
size_t src0_row_size,
|
||||
size_t act_elem_size,
|
||||
size_t bias_row_size,
|
||||
size_t scale_size,
|
||||
uint32_t n_prefetch,
|
||||
bool is_matmul_id,
|
||||
bool is_fused_nx
|
||||
@@ -667,6 +761,7 @@ static inline void htp_mm_hvx_vtcm_layout_build(
|
||||
size_t src0_sz = 0;
|
||||
size_t act_sz = 0;
|
||||
size_t bias_sz = bias_row_size > 0 ? htp_mm_round_up(bias_row_size, 128) : 0;
|
||||
const size_t scale_sz = scale_size > 0 ? htp_mm_round_up(scale_size, 128) : 0;
|
||||
size_t dst_sz = 0;
|
||||
size_t act_raw_sz = 0;
|
||||
const size_t lut_sz = (wtype == HTP_TYPE_IQ2_XXS) ? HTP_IQ2XXS_LUT_BYTES : 0;
|
||||
@@ -759,6 +854,7 @@ static inline void htp_mm_hvx_vtcm_layout_build(
|
||||
size_t off_group_a = 0;
|
||||
VTCM_LAYOUT_ALLOC(off_group_a, off_act, act_sz);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_bias, bias_sz, bias_sz > 0);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_scale, scale_sz, scale_sz > 0);
|
||||
VTCM_LAYOUT_ALLOC_OPTIONAL(off_group_a, off_lut, lut_sz, lut_sz > 0);
|
||||
|
||||
// Group B: Compute-only buffers (starts at off_group_a)
|
||||
@@ -775,13 +871,35 @@ static inline void htp_mm_hvx_vtcm_layout_build(
|
||||
L->src0_bytes = src0_sz;
|
||||
L->act_bytes = act_sz;
|
||||
L->bias_bytes = bias_sz;
|
||||
L->scale_bytes = scale_sz;
|
||||
L->dst_bytes = dst_sz;
|
||||
L->act_raw_bytes = act_raw_sz;
|
||||
L->lut_bytes = lut_sz;
|
||||
L->total_bytes = off_group_a + hex_smax(group_b_size, group_c_size);
|
||||
}
|
||||
|
||||
static inline bool htp_mm_hvx_solve_vtcm_params(
|
||||
static inline void htp_mm_hvx_vtcm_layout_build(
|
||||
struct htp_mm_hvx_vtcm_layout * L,
|
||||
int kernel_type,
|
||||
int wtype,
|
||||
uint32_t ne10, // k
|
||||
uint32_t act_nrows, // m_total
|
||||
uint32_t n_hvx,
|
||||
size_t dst_row_size,
|
||||
size_t src0_row_size,
|
||||
size_t act_elem_size,
|
||||
size_t bias_row_size,
|
||||
uint32_t n_prefetch,
|
||||
bool is_matmul_id,
|
||||
bool is_fused_nx
|
||||
) {
|
||||
htp_mm_hvx_vtcm_layout_build_ex(
|
||||
L, kernel_type, wtype, ne10, act_nrows, n_hvx,
|
||||
dst_row_size, src0_row_size, act_elem_size, bias_row_size, 0, n_prefetch, is_matmul_id, is_fused_nx
|
||||
);
|
||||
}
|
||||
|
||||
static inline bool htp_mm_hvx_solve_vtcm_params_ex(
|
||||
int kernel_type,
|
||||
int wtype,
|
||||
uint32_t ne10,
|
||||
@@ -791,6 +909,7 @@ static inline bool htp_mm_hvx_solve_vtcm_params(
|
||||
size_t src0_row_size,
|
||||
size_t act_elem_size,
|
||||
size_t bias_row_size,
|
||||
size_t scale_size,
|
||||
uint32_t n_prefetch,
|
||||
size_t vtcm_budget,
|
||||
struct htp_mm_hvx_vtcm_layout * L_out,
|
||||
@@ -798,9 +917,9 @@ static inline bool htp_mm_hvx_solve_vtcm_params(
|
||||
) {
|
||||
const size_t raw_elem_size = (act_elem_size == sizeof(__fp16)) ? sizeof(__fp16) : sizeof(float);
|
||||
struct htp_mm_hvx_vtcm_layout L;
|
||||
htp_mm_hvx_vtcm_layout_build(
|
||||
htp_mm_hvx_vtcm_layout_build_ex(
|
||||
&L, kernel_type, wtype, ne10, act_nrows, n_hvx,
|
||||
dst_row_size, src0_row_size, raw_elem_size, bias_row_size, n_prefetch, false, false
|
||||
dst_row_size, src0_row_size, raw_elem_size, bias_row_size, scale_size, n_prefetch, false, false
|
||||
);
|
||||
|
||||
if (L.total_bytes <= vtcm_budget) {
|
||||
@@ -809,7 +928,7 @@ static inline bool htp_mm_hvx_solve_vtcm_params(
|
||||
return true;
|
||||
}
|
||||
|
||||
const size_t fixed_bytes = L.src0_bytes + L.bias_bytes + L.dst_bytes + L.lut_bytes;
|
||||
const size_t fixed_bytes = L.src0_bytes + L.bias_bytes + L.scale_bytes + L.dst_bytes + L.lut_bytes;
|
||||
if (vtcm_budget <= fixed_bytes) {
|
||||
return false;
|
||||
}
|
||||
@@ -843,24 +962,24 @@ static inline bool htp_mm_hvx_solve_vtcm_params(
|
||||
return false;
|
||||
}
|
||||
|
||||
htp_mm_hvx_vtcm_layout_build(
|
||||
htp_mm_hvx_vtcm_layout_build_ex(
|
||||
&L, kernel_type, wtype, ne10, m_chunk, n_hvx,
|
||||
dst_row_size, src0_row_size, raw_elem_size, bias_row_size, n_prefetch, false, false
|
||||
dst_row_size, src0_row_size, raw_elem_size, bias_row_size, scale_size, n_prefetch, false, false
|
||||
);
|
||||
|
||||
while (m_chunk > 2 && L.total_bytes > vtcm_budget) {
|
||||
m_chunk -= 2;
|
||||
htp_mm_hvx_vtcm_layout_build(
|
||||
htp_mm_hvx_vtcm_layout_build_ex(
|
||||
&L, kernel_type, wtype, ne10, m_chunk, n_hvx,
|
||||
dst_row_size, src0_row_size, raw_elem_size, bias_row_size, n_prefetch, false, false
|
||||
dst_row_size, src0_row_size, raw_elem_size, bias_row_size, scale_size, n_prefetch, false, false
|
||||
);
|
||||
}
|
||||
|
||||
if (m_chunk > 1 && L.total_bytes > vtcm_budget) {
|
||||
m_chunk = 1;
|
||||
htp_mm_hvx_vtcm_layout_build(
|
||||
htp_mm_hvx_vtcm_layout_build_ex(
|
||||
&L, kernel_type, wtype, ne10, m_chunk, n_hvx,
|
||||
dst_row_size, src0_row_size, raw_elem_size, bias_row_size, n_prefetch, false, false
|
||||
dst_row_size, src0_row_size, raw_elem_size, bias_row_size, scale_size, n_prefetch, false, false
|
||||
);
|
||||
}
|
||||
|
||||
@@ -873,12 +992,34 @@ static inline bool htp_mm_hvx_solve_vtcm_params(
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline bool htp_mm_hvx_solve_vtcm_params(
|
||||
int kernel_type,
|
||||
int wtype,
|
||||
uint32_t ne10,
|
||||
uint32_t act_nrows,
|
||||
uint32_t n_hvx,
|
||||
size_t dst_row_size,
|
||||
size_t src0_row_size,
|
||||
size_t act_elem_size,
|
||||
size_t bias_row_size,
|
||||
uint32_t n_prefetch,
|
||||
size_t vtcm_budget,
|
||||
struct htp_mm_hvx_vtcm_layout * L_out,
|
||||
uint32_t * m_chunk_out
|
||||
) {
|
||||
return htp_mm_hvx_solve_vtcm_params_ex(
|
||||
kernel_type, wtype, ne10, act_nrows, n_hvx,
|
||||
dst_row_size, src0_row_size, act_elem_size, bias_row_size, 0, n_prefetch, vtcm_budget,
|
||||
L_out, m_chunk_out
|
||||
);
|
||||
}
|
||||
|
||||
static inline size_t htp_mm_hmx_get_2d_vtcm_size_core(
|
||||
int kernel_type, int wtype, uint32_t k, size_t mc, size_t nc, bool pipeline, uint32_t act_hvx, uint32_t aligned_tile_size, size_t bias_size,
|
||||
size_t res_in_elem_size, size_t res_out_elem_size, bool is_glu
|
||||
size_t res_in_elem_size, size_t res_out_elem_size, bool is_glu, size_t scale_size, size_t norm_size
|
||||
) {
|
||||
struct htp_mm_hmx_vtcm_layout L;
|
||||
htp_mm_hmx_vtcm_layout_build_core(&L, kernel_type, wtype, k, mc, nc, 1, pipeline, act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, is_glu);
|
||||
htp_mm_hmx_vtcm_layout_build_core(&L, kernel_type, wtype, k, mc, nc, 1, pipeline, act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, is_glu, scale_size, norm_size);
|
||||
return L.total_bytes;
|
||||
}
|
||||
|
||||
@@ -886,20 +1027,20 @@ static inline size_t htp_mm_hmx_get_2d_vtcm_size_ex(
|
||||
int kernel_type, int wtype, uint32_t k, size_t mc, size_t nc, bool pipeline, uint32_t act_hvx, uint32_t aligned_tile_size, size_t bias_size,
|
||||
size_t res_in_elem_size, size_t res_out_elem_size
|
||||
) {
|
||||
return htp_mm_hmx_get_2d_vtcm_size_core(kernel_type, wtype, k, mc, nc, pipeline, act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, false);
|
||||
return htp_mm_hmx_get_2d_vtcm_size_core(kernel_type, wtype, k, mc, nc, pipeline, act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, false, 0, 0);
|
||||
}
|
||||
|
||||
static inline size_t htp_mm_hmx_get_2d_vtcm_size(
|
||||
int wtype, uint32_t k, size_t mc, size_t nc, bool pipeline, uint32_t act_hvx, uint32_t aligned_tile_size, size_t bias_size
|
||||
) {
|
||||
return htp_mm_hmx_get_2d_vtcm_size_ex(HTP_MM_KERNEL_HMX_2D, wtype, k, mc, nc, pipeline, act_hvx, aligned_tile_size, bias_size, 0, 0);
|
||||
return htp_mm_hmx_get_2d_vtcm_size_ex(HTP_MM_KERNEL_HMX_F16_2D, wtype, k, mc, nc, pipeline, act_hvx, aligned_tile_size, bias_size, 0, 0);
|
||||
}
|
||||
|
||||
static inline size_t htp_mm_hmx_get_batched_vtcm_size(
|
||||
int wtype, uint32_t k, size_t mc, size_t nc, uint32_t group_size, bool pipeline, uint32_t act_hvx, size_t bias_size, size_t res_out_elem_size) {
|
||||
(void)pipeline;
|
||||
struct htp_mm_hmx_vtcm_layout L;
|
||||
htp_mm_hmx_vtcm_layout_build_ex(&L, HTP_MM_KERNEL_HMX_BATCHED, wtype, k, mc, nc, group_size, false, act_hvx, 0, bias_size, 0, res_out_elem_size);
|
||||
htp_mm_hmx_vtcm_layout_build_ex(&L, HTP_MM_KERNEL_HMX_F16_BATCHED, wtype, k, mc, nc, group_size, false, act_hvx, 0, bias_size, 0, res_out_elem_size);
|
||||
return L.total_bytes;
|
||||
}
|
||||
|
||||
@@ -981,6 +1122,8 @@ static inline bool htp_mm_hmx_solve_2d_params_core(
|
||||
bool is_matmul_id,
|
||||
uint32_t aligned_tile_size,
|
||||
size_t bias_size,
|
||||
size_t scale_size,
|
||||
size_t norm_size,
|
||||
size_t res_in_elem_size,
|
||||
size_t res_out_elem_size,
|
||||
bool is_glu,
|
||||
@@ -990,8 +1133,8 @@ static inline bool htp_mm_hmx_solve_2d_params_core(
|
||||
int * act_hvx_out,
|
||||
size_t * vtcm_size_out
|
||||
) {
|
||||
const bool is_int_hmx = htp_mm_kernel_is_hmx_int(kernel_type);
|
||||
const size_t m_align = is_int_hmx ? 64u : HTP_MM_HMX_TILE_N_ROWS;
|
||||
const bool is_w8a8 = htp_mm_kernel_is_hmx_w8a8(kernel_type);
|
||||
const size_t m_align = is_w8a8 ? 64u : HTP_MM_HMX_TILE_N_ROWS;
|
||||
const size_t n_total = hex_align_up((size_t) ne01_padded, HTP_MM_HMX_TILE_N_COLS);
|
||||
const size_t m_target = is_matmul_id ? hex_align_up((size_t) m_id_rows, m_align)
|
||||
: hex_align_up((size_t) ne11_padded, m_align);
|
||||
@@ -1005,13 +1148,15 @@ static inline bool htp_mm_hmx_solve_2d_params_core(
|
||||
&per_n_approx, &per_m_approx, &per_mn_approx);
|
||||
if (is_glu) {
|
||||
const size_t n_groups = (k + HTP_MM_HMX_W8A8_KGROUP - 1u) / HTP_MM_HMX_W8A8_KGROUP;
|
||||
per_mn_approx += (pipeline ? 2u : 1u) * (is_int_hmx ? (n_groups * sizeof(uint16_t)) : sizeof(__fp16));
|
||||
per_mn_approx += (pipeline ? 2u : 1u) * (is_w8a8 ? (n_groups * sizeof(uint16_t)) : sizeof(__fp16));
|
||||
}
|
||||
per_mn_approx += (pipeline ? 2u : 1u) * res_out_elem_size + (pipeline ? 2u * res_in_elem_size : 0u);
|
||||
const size_t base_overhead = htp_mm_hmx_get_2d_overhead(wtype, pipeline, is_matmul_id) +
|
||||
(is_int_hmx ? (HTP_MM_HMX_TILE_SIZE + (pipeline ? 2u : 1u) * 768u) : 0u) +
|
||||
((is_glu && is_int_hmx) ? ((pipeline ? 4u : 2u) * HTP_MM_HMX_TILE_SIZE) : 0u) +
|
||||
(bias_size > 0 ? hex_align_up(bias_size, HTP_MM_HMX_TILE_SIZE) : 0u);
|
||||
(is_w8a8 ? (HTP_MM_HMX_TILE_SIZE + (pipeline ? 2u : 1u) * 768u) : 0u) +
|
||||
((is_glu && is_w8a8) ? ((pipeline ? 4u : 2u) * HTP_MM_HMX_TILE_SIZE) : 0u) +
|
||||
(bias_size > 0 ? hex_align_up(bias_size, HTP_MM_HMX_TILE_SIZE) : 0u) +
|
||||
(scale_size > 0 ? hex_align_up(scale_size, HTP_MM_HMX_TILE_SIZE) : 0u) +
|
||||
(norm_size > 0 ? hex_align_up(norm_size, HTP_MM_HMX_TILE_SIZE) : 0u);
|
||||
if (vtcm_budget <= base_overhead || per_n_approx == 0 || per_m_approx == 0 || per_mn_approx == 0) {
|
||||
return false;
|
||||
}
|
||||
@@ -1067,7 +1212,7 @@ static inline bool htp_mm_hmx_solve_2d_params_core(
|
||||
mc = hex_smin(mc, m_target);
|
||||
while (mc >= m_align) {
|
||||
size_t exact = htp_mm_hmx_get_2d_vtcm_size_core(
|
||||
kernel_type, wtype, k, mc, nc, pipeline, layout_act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, is_glu);
|
||||
kernel_type, wtype, k, mc, nc, pipeline, layout_act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, is_glu, scale_size, norm_size);
|
||||
if (exact <= vtcm_budget) {
|
||||
const size_t mblocks = (m_rows + mc - 1u) / mc;
|
||||
size_t mc_tight = (mblocks > 1u)
|
||||
@@ -1075,7 +1220,7 @@ static inline bool htp_mm_hmx_solve_2d_params_core(
|
||||
: mc;
|
||||
if (mc_tight != mc) {
|
||||
size_t exact_tight = htp_mm_hmx_get_2d_vtcm_size_core(
|
||||
kernel_type, wtype, k, mc_tight, nc, pipeline, layout_act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, is_glu);
|
||||
kernel_type, wtype, k, mc_tight, nc, pipeline, layout_act_hvx, aligned_tile_size, bias_size, res_in_elem_size, res_out_elem_size, is_glu, scale_size, norm_size);
|
||||
if (exact_tight <= vtcm_budget) {
|
||||
mc = mc_tight;
|
||||
exact = exact_tight;
|
||||
@@ -1125,6 +1270,34 @@ static inline bool htp_mm_hmx_solve_2d_params_core(
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline bool htp_mm_hmx_solve_2d_params_ex_scaled(
|
||||
int kernel_type,
|
||||
int wtype,
|
||||
uint32_t k,
|
||||
uint32_t m_id_rows,
|
||||
uint32_t ne01_padded,
|
||||
uint32_t ne11_padded,
|
||||
uint32_t m_for_cost,
|
||||
int n_hvx,
|
||||
bool pipeline,
|
||||
bool is_matmul_id,
|
||||
uint32_t aligned_tile_size,
|
||||
size_t bias_size,
|
||||
size_t scale_size,
|
||||
size_t norm_size,
|
||||
size_t res_in_elem_size,
|
||||
size_t res_out_elem_size,
|
||||
size_t vtcm_budget,
|
||||
size_t * m_chunk_out,
|
||||
size_t * n_chunk_out,
|
||||
int * act_hvx_out,
|
||||
size_t * vtcm_size_out
|
||||
) {
|
||||
return htp_mm_hmx_solve_2d_params_core(kernel_type, wtype, k, m_id_rows, ne01_padded, ne11_padded, m_for_cost,
|
||||
n_hvx, pipeline, is_matmul_id, aligned_tile_size, bias_size, scale_size, norm_size, res_in_elem_size,
|
||||
res_out_elem_size, false, vtcm_budget, m_chunk_out, n_chunk_out, act_hvx_out, vtcm_size_out);
|
||||
}
|
||||
|
||||
static inline bool htp_mm_hmx_solve_2d_params_ex(
|
||||
int kernel_type,
|
||||
int wtype,
|
||||
@@ -1147,7 +1320,7 @@ static inline bool htp_mm_hmx_solve_2d_params_ex(
|
||||
size_t * vtcm_size_out
|
||||
) {
|
||||
return htp_mm_hmx_solve_2d_params_core(kernel_type, wtype, k, m_id_rows, ne01_padded, ne11_padded, m_for_cost,
|
||||
n_hvx, pipeline, is_matmul_id, aligned_tile_size, bias_size, res_in_elem_size,
|
||||
n_hvx, pipeline, is_matmul_id, aligned_tile_size, bias_size, 0, 0, res_in_elem_size,
|
||||
res_out_elem_size, false, vtcm_budget, m_chunk_out, n_chunk_out, act_hvx_out, vtcm_size_out);
|
||||
}
|
||||
|
||||
@@ -1162,6 +1335,8 @@ static inline bool htp_mm_hmx_solve_2d_params_glu(
|
||||
bool pipeline,
|
||||
uint32_t aligned_tile_size,
|
||||
size_t res_out_elem_size,
|
||||
size_t scale_size,
|
||||
size_t norm_size,
|
||||
size_t vtcm_budget,
|
||||
size_t * m_chunk_out,
|
||||
size_t * n_chunk_out,
|
||||
@@ -1169,35 +1344,10 @@ static inline bool htp_mm_hmx_solve_2d_params_glu(
|
||||
size_t * vtcm_size_out
|
||||
) {
|
||||
return htp_mm_hmx_solve_2d_params_core(kernel_type, wtype, k, 0, ne01_padded, ne11_padded, m_for_cost,
|
||||
n_hvx, pipeline, false, aligned_tile_size, 0, 0,
|
||||
n_hvx, pipeline, false, aligned_tile_size, 0, scale_size, norm_size, 0,
|
||||
res_out_elem_size, true, vtcm_budget, m_chunk_out, n_chunk_out, act_hvx_out, vtcm_size_out);
|
||||
}
|
||||
|
||||
static inline bool htp_mm_int_hmx_solve_2d_params(
|
||||
int kernel_type,
|
||||
int wtype,
|
||||
uint32_t k,
|
||||
uint32_t m_id_rows,
|
||||
uint32_t ne01_padded,
|
||||
uint32_t ne11_padded,
|
||||
uint32_t m_for_cost,
|
||||
int n_hvx,
|
||||
bool pipeline,
|
||||
bool is_matmul_id,
|
||||
uint32_t aligned_tile_size,
|
||||
size_t bias_size,
|
||||
size_t res_out_elem_size,
|
||||
size_t vtcm_budget,
|
||||
size_t * m_chunk_out,
|
||||
size_t * n_chunk_out,
|
||||
int * act_hvx_out,
|
||||
size_t * vtcm_size_out
|
||||
) {
|
||||
return htp_mm_hmx_solve_2d_params_ex(kernel_type, wtype, k, m_id_rows, ne01_padded, ne11_padded, m_for_cost,
|
||||
n_hvx, pipeline, is_matmul_id, aligned_tile_size, bias_size, 0, res_out_elem_size, vtcm_budget,
|
||||
m_chunk_out, n_chunk_out, act_hvx_out, vtcm_size_out);
|
||||
}
|
||||
|
||||
static inline bool htp_mm_hmx_solve_2d_params(
|
||||
int wtype,
|
||||
uint32_t k,
|
||||
@@ -1217,7 +1367,7 @@ static inline bool htp_mm_hmx_solve_2d_params(
|
||||
int * act_hvx_out,
|
||||
size_t * vtcm_size_out
|
||||
) {
|
||||
return htp_mm_hmx_solve_2d_params_ex(HTP_MM_KERNEL_HMX_2D, wtype, k, m_id_rows, ne01_padded, ne11_padded, m_for_cost,
|
||||
return htp_mm_hmx_solve_2d_params_ex(HTP_MM_KERNEL_HMX_F16_2D, wtype, k, m_id_rows, ne01_padded, ne11_padded, m_for_cost,
|
||||
n_hvx, pipeline, is_matmul_id, aligned_tile_size, bias_size, 0, res_out_elem_size, vtcm_budget,
|
||||
m_chunk_out, n_chunk_out, act_hvx_out, vtcm_size_out);
|
||||
}
|
||||
|
||||
@@ -378,15 +378,9 @@ static void pad_thread_circular(const htp_thread_t * th, unsigned int nth, unsig
|
||||
}
|
||||
|
||||
int op_pad(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
const struct htp_pad_kernel_params * kparams =
|
||||
(const struct htp_pad_kernel_params *) octx->kernel_params;
|
||||
|
||||
if ((src0->type != HTP_TYPE_F32 && src0->type != HTP_TYPE_F16) || dst->type != src0->type) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const int32_t lp0 = octx->op_params[0];
|
||||
const int32_t rp0 = octx->op_params[1];
|
||||
const int32_t lp1 = octx->op_params[2];
|
||||
@@ -441,9 +435,9 @@ int op_pad(struct htp_ops_context * octx) {
|
||||
};
|
||||
|
||||
FARF(HIGH, "pad-hvx%s: (%ux%ux%ux%u) -> (%ux%ux%ux%u) pads=(%d,%d,%d,%d,%d,%d,%d,%d)\n",
|
||||
circular ? "-circ" : "",
|
||||
src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
|
||||
dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3],
|
||||
(kparams->kernel_type == HTP_PAD_KERNEL_HVX_CIRCULAR) ? "-circ" : "",
|
||||
octx->src[0]->ne[0], octx->src[0]->ne[1], octx->src[0]->ne[2], octx->src[0]->ne[3],
|
||||
octx->dst->ne[0], octx->dst->ne[1], octx->dst->ne[2], octx->dst->ne[3],
|
||||
lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3);
|
||||
|
||||
switch (kparams->kernel_type) {
|
||||
|
||||
@@ -321,12 +321,6 @@ int op_repeat(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
if ((src0->type != HTP_TYPE_F32 && src0->type != HTP_TYPE_F16) ||
|
||||
(dst->type != src0->type && !(src0->type == HTP_TYPE_F32 && dst->type == HTP_TYPE_F16))) {
|
||||
FARF(ERROR, "repeat: unsupported types %u -> %u\n", src0->type, dst->type);
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const uint32_t src_elem_size = (src0->type == HTP_TYPE_F16) ? 2 : 4;
|
||||
const uint32_t dst_elem_size = (dst->type == HTP_TYPE_F16) ? 2 : 4;
|
||||
|
||||
@@ -390,10 +384,10 @@ int op_repeat(struct htp_ops_context * octx) {
|
||||
.dst_reps = kparams->dst_reps,
|
||||
};
|
||||
|
||||
FARF(HIGH, "repeat: (%ux%ux%ux%u) -> (%ux%ux%ux%u) nr=(%u,%u,%u,%u) pipe=%u\n",
|
||||
FARF(HIGH, "repeat: (%ux%ux%ux%u) -> (%ux%ux%ux%u) nr0=%u pipe=%u\n",
|
||||
src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
|
||||
dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3],
|
||||
kparams->nr0, kparams->nr1, kparams->nr2, kparams->nr3, kparams->pipelined);
|
||||
kparams->nr0, kparams->pipelined);
|
||||
|
||||
work_queue_func_t worker = kparams->pipelined ? repeat_thread : repeat_thread_tiled;
|
||||
work_queue_run(octx->ctx->hvx_queue, worker, &rctx, n_hvx);
|
||||
|
||||
@@ -39,9 +39,6 @@ static inline void htp_repeat_vtcm_layout_build(
|
||||
struct htp_repeat_kernel_params {
|
||||
uint32_t n_hvx;
|
||||
uint32_t nr0;
|
||||
uint32_t nr1;
|
||||
uint32_t nr2;
|
||||
uint32_t nr3;
|
||||
uint32_t tile_elems;
|
||||
uint32_t dst_reps;
|
||||
uint32_t pipelined;
|
||||
|
||||
@@ -422,15 +422,5 @@ static int execute_op_roll(struct htp_ops_context * octx) {
|
||||
}
|
||||
|
||||
int op_roll(struct htp_ops_context * octx) {
|
||||
if (octx->src[0]->type != octx->dst->type) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
switch (octx->src[0]->type) {
|
||||
case HTP_TYPE_F32:
|
||||
case HTP_TYPE_F16:
|
||||
return execute_op_roll(octx);
|
||||
|
||||
default:
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
return execute_op_roll(octx);
|
||||
}
|
||||
|
||||
@@ -56,7 +56,6 @@ typedef void (*set_rows_compute_fn_t)(void * restrict dst, const void * restrict
|
||||
|
||||
struct set_rows_context {
|
||||
struct htp_ops_context * octx;
|
||||
const struct htp_set_rows_kernel_params * kparams;
|
||||
struct htp_set_rows_vtcm_layout vtcm_layout;
|
||||
set_rows_compute_fn_t compute;
|
||||
uint8_t * vtcm_base;
|
||||
@@ -267,7 +266,6 @@ int op_set_rows(struct htp_ops_context * octx) {
|
||||
|
||||
struct set_rows_context srctx;
|
||||
srctx.octx = octx;
|
||||
srctx.kparams = kparams;
|
||||
srctx.task_start = task_start;
|
||||
srctx.tasks = tasks;
|
||||
srctx.tasks_per_hvx = fastdiv(tasks + n_hvx - 1, &octx->n_hvx_div);
|
||||
|
||||
@@ -729,10 +729,6 @@ static int execute_op_softmax_f32(struct htp_ops_context * octx) {
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
|
||||
if (kparams->kernel_type >= HTP_SOFTMAX_KERNEL_COUNT) {
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
|
||||
if (octx->ctx->vtcm_size < (size_t) kparams->vtcm_size) {
|
||||
FARF(ERROR, "%s : current VTCM reservation %zu is too small, needed %u\n",
|
||||
op_type, octx->ctx->vtcm_size, kparams->vtcm_size);
|
||||
@@ -761,33 +757,39 @@ static int execute_op_softmax_f32(struct htp_ops_context * octx) {
|
||||
const uint32_t n_hvx = octx->n_hvx;
|
||||
uint8_t * const vtcm_base = (uint8_t *) octx->ctx->vtcm_base;
|
||||
|
||||
const bool src0_f16 = (kparams->flags & HTP_SOFTMAX_FLAG_SRC0_F16) != 0;
|
||||
const bool dst_f16 = (kparams->flags & HTP_SOFTMAX_FLAG_DST_F16) != 0;
|
||||
const bool use_src1 = (kparams->flags & HTP_SOFTMAX_FLAG_USE_SRC1) != 0;
|
||||
const bool use_f16 = (kparams->flags & HTP_SOFTMAX_FLAG_SRC1_F16) != 0;
|
||||
const bool use_src2 = (kparams->flags & HTP_SOFTMAX_FLAG_USE_SRC2) != 0;
|
||||
|
||||
struct htp_softmax_vtcm_layout layout;
|
||||
htp_softmax_vtcm_layout_build(&layout,
|
||||
src0->ne[0],
|
||||
kparams->use_src1 ? octx->src[1]->ne[0] : 0,
|
||||
use_src1 ? octx->src[1]->ne[0] : 0,
|
||||
kparams->n_head,
|
||||
kparams->src0_f16 != 0,
|
||||
kparams->dst_f16 != 0,
|
||||
kparams->use_src1 != 0,
|
||||
kparams->use_f16 != 0,
|
||||
kparams->use_src2 != 0,
|
||||
src0_f16,
|
||||
dst_f16,
|
||||
use_src1,
|
||||
use_f16,
|
||||
use_src2,
|
||||
kparams->n_hvx);
|
||||
|
||||
struct htp_softmax_context smctx = {
|
||||
.octx = octx,
|
||||
.kparams = kparams,
|
||||
.prep = (void *) softmax_prep_kernels[kparams->kernel_type],
|
||||
.softmax = (void *) softmax_kernels[kparams->dst_f16 != 0][kparams->use_src2 != 0],
|
||||
.softmax = (void *) softmax_kernels[dst_f16][use_src2],
|
||||
|
||||
.data_src0 = src0->data,
|
||||
.data_src1 = kparams->use_src1 ? octx->src[1]->data : 0,
|
||||
.data_src1 = use_src1 ? octx->src[1]->data : 0,
|
||||
.data_dst = dst->data,
|
||||
|
||||
.vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, layout.off_src0),
|
||||
.vtcm_dst = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, layout.off_dst),
|
||||
.vtcm_src1 = VTCM_LAYOUT_PTR_OPTIONAL(uint8_t, vtcm_base, layout.off_src1, kparams->use_src1),
|
||||
.vtcm_work = VTCM_LAYOUT_PTR_OPTIONAL(uint8_t, vtcm_base, layout.off_work, kparams->dst_f16),
|
||||
.vtcm_src2 = VTCM_LAYOUT_PTR_OPTIONAL(float, vtcm_base, layout.off_src2, kparams->use_src2),
|
||||
.vtcm_src1 = VTCM_LAYOUT_PTR_OPTIONAL(uint8_t, vtcm_base, layout.off_src1, use_src1),
|
||||
.vtcm_work = VTCM_LAYOUT_PTR_OPTIONAL(uint8_t, vtcm_base, layout.off_work, dst_f16),
|
||||
.vtcm_src2 = VTCM_LAYOUT_PTR_OPTIONAL(float, vtcm_base, layout.off_src2, use_src2),
|
||||
|
||||
.vtcm_src0_size_per_hvx = (uint32_t) layout.src0_bytes_per_hvx,
|
||||
.vtcm_src1_size_per_hvx = (uint32_t) layout.src1_bytes_per_hvx,
|
||||
@@ -802,10 +804,10 @@ static int execute_op_softmax_f32(struct htp_ops_context * octx) {
|
||||
.src1_row_size_aligned = (uint32_t) layout.src1_spad_half_size,
|
||||
.dst_row_size_aligned = (uint32_t) layout.dst_spad_half_size,
|
||||
|
||||
.src0_f16 = kparams->src0_f16 != 0,
|
||||
.dst_f16 = kparams->dst_f16 != 0,
|
||||
.use_f16 = kparams->use_f16 != 0,
|
||||
.use_src2 = kparams->use_src2 != 0,
|
||||
.src0_f16 = src0_f16,
|
||||
.dst_f16 = dst_f16,
|
||||
.use_f16 = use_f16,
|
||||
.use_src2 = use_src2,
|
||||
|
||||
.n_head = kparams->n_head,
|
||||
.n_head_log2 = kparams->n_head_log2,
|
||||
@@ -825,21 +827,18 @@ static int execute_op_softmax_f32(struct htp_ops_context * octx) {
|
||||
.nrows = nrows,
|
||||
};
|
||||
|
||||
if (kparams->max_bias > 0.0f && kparams->use_src1) {
|
||||
if (kparams->n_head > 512) {
|
||||
return HTP_STATUS_INVAL_PARAMS;
|
||||
}
|
||||
if (kparams->max_bias > 0.0f && use_src1) {
|
||||
softmax_init_alibi_slopes(smctx.slopes, kparams->n_head, kparams->n_head_log2, kparams->m0, kparams->m1);
|
||||
}
|
||||
|
||||
if (kparams->use_src2) {
|
||||
if (use_src2) {
|
||||
const struct htp_tensor * src2 = octx->src[2];
|
||||
dma_queue * dma_q = octx->ctx->dma[0];
|
||||
dma_queue_push(dma_q, dma_make_data(smctx.vtcm_src2, src2->data), layout.src2_bytes, 0, src2->size, 1);
|
||||
dma_queue_pop(dma_q);
|
||||
}
|
||||
|
||||
work_queue_func_t task_func = kparams->use_src1 ? softmax_thread_mask : softmax_thread;
|
||||
work_queue_func_t task_func = use_src1 ? softmax_thread_mask : softmax_thread;
|
||||
work_queue_run(octx->ctx->hvx_queue, task_func, &smctx, n_hvx);
|
||||
|
||||
return HTP_STATUS_OK;
|
||||
|
||||
@@ -19,18 +19,22 @@ enum htp_softmax_kernel_type {
|
||||
HTP_SOFTMAX_KERNEL_COUNT,
|
||||
};
|
||||
|
||||
enum htp_softmax_flags {
|
||||
HTP_SOFTMAX_FLAG_SRC0_F16 = (1u << 0),
|
||||
HTP_SOFTMAX_FLAG_DST_F16 = (1u << 1),
|
||||
HTP_SOFTMAX_FLAG_USE_SRC1 = (1u << 2),
|
||||
HTP_SOFTMAX_FLAG_SRC1_F16 = (1u << 3),
|
||||
HTP_SOFTMAX_FLAG_USE_SRC2 = (1u << 4),
|
||||
};
|
||||
|
||||
struct htp_softmax_kernel_params {
|
||||
uint8_t kernel_type;
|
||||
uint8_t src0_f16;
|
||||
uint8_t dst_f16;
|
||||
uint8_t use_src2;
|
||||
uint8_t flags; // bitmask of enum htp_softmax_flags
|
||||
uint32_t n_hvx;
|
||||
uint32_t vtcm_size;
|
||||
|
||||
uint32_t n_head;
|
||||
uint32_t n_head_log2;
|
||||
uint32_t use_src1;
|
||||
uint32_t use_f16;
|
||||
|
||||
float scale;
|
||||
float max_bias;
|
||||
|
||||
@@ -356,19 +356,14 @@ int op_solve_tri(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * src1 = octx->src[1];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
if ((src0->type != HTP_TYPE_F32 && src0->type != HTP_TYPE_F16) ||
|
||||
(src1->type != HTP_TYPE_F32 && src1->type != HTP_TYPE_F16) ||
|
||||
(dst->type != HTP_TYPE_F32 && dst->type != HTP_TYPE_F16)) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const struct htp_solve_tri_kernel_params * kparams =
|
||||
(const struct htp_solve_tri_kernel_params *) octx->kernel_params;
|
||||
|
||||
const uint32_t col_block = kparams->col_block;
|
||||
const uint32_t k_chunks = kparams->k_chunks;
|
||||
const uint32_t total_batches = kparams->total_batches;
|
||||
const bool batched = (kparams->batched != 0);
|
||||
const bool batched = (kparams->flags & HTP_SOLVE_TRI_FLAG_BATCHED) != 0;
|
||||
const uint32_t a_full = (kparams->flags & HTP_SOLVE_TRI_FLAG_A_FULL) ? 1 : 0;
|
||||
const uint8_t a_f16 = (src0->type == HTP_TYPE_F16) ? 1 : 0;
|
||||
const uint8_t b_f16 = (src1->type == HTP_TYPE_F16) ? 1 : 0;
|
||||
const uint8_t x_f16 = (dst->type == HTP_TYPE_F16) ? 1 : 0;
|
||||
@@ -400,7 +395,7 @@ int op_solve_tri(struct htp_ops_context * octx) {
|
||||
const uint32_t n_hvx = octx->n_hvx;
|
||||
|
||||
struct htp_solve_tri_vtcm_layout layout;
|
||||
htp_solve_tri_vtcm_layout_build(&layout, src0->ne[0], kparams->a_full, n_hvx);
|
||||
htp_solve_tri_vtcm_layout_build(&layout, src0->ne[0], a_full, n_hvx);
|
||||
if (octx->ctx->vtcm_size < layout.total_bytes) {
|
||||
FARF(ERROR, "solve-tri: VTCM size %zu is too small, needed %zu\n", octx->ctx->vtcm_size, layout.total_bytes);
|
||||
return HTP_STATUS_VTCM_TOO_SMALL;
|
||||
@@ -421,7 +416,7 @@ int op_solve_tri(struct htp_ops_context * octx) {
|
||||
.job_start = job_start,
|
||||
.k_chunks = k_chunks,
|
||||
.col_block = col_block,
|
||||
.a_full = kparams->a_full,
|
||||
.a_full = a_full,
|
||||
.a_f16 = a_f16,
|
||||
.b_f16 = b_f16,
|
||||
.x_f16 = x_f16,
|
||||
|
||||
@@ -33,14 +33,18 @@ static inline void htp_solve_tri_vtcm_layout_build(
|
||||
L->total_bytes = off;
|
||||
}
|
||||
|
||||
enum htp_solve_tri_flags {
|
||||
HTP_SOLVE_TRI_FLAG_BATCHED = (1u << 0),
|
||||
HTP_SOLVE_TRI_FLAG_A_FULL = (1u << 1),
|
||||
};
|
||||
|
||||
struct htp_solve_tri_kernel_params {
|
||||
uint32_t n_hvx;
|
||||
uint32_t total_batches;
|
||||
uint32_t total_jobs;
|
||||
uint32_t k_chunks;
|
||||
uint32_t col_block;
|
||||
uint32_t batched;
|
||||
uint32_t a_full;
|
||||
uint32_t flags; // bitmask of enum htp_solve_tri_flags
|
||||
uint32_t vtcm_size;
|
||||
|
||||
struct fastdiv_values div_ne02;
|
||||
|
||||
@@ -1550,8 +1550,10 @@ int op_ssm_conv_f32(struct htp_ops_context * octx) {
|
||||
const size_t src0_elem_size = (src0->type == HTP_TYPE_F16) ? sizeof(__fp16) : sizeof(float);
|
||||
const size_t dst_elem_size = (dst->type == HTP_TYPE_F16) ? sizeof(__fp16) : sizeof(float);
|
||||
|
||||
const bool token_major = (kparams->flags & HTP_SSM_CONV_FLAG_TOKEN_MAJOR) != 0;
|
||||
|
||||
struct htp_ssm_conv_vtcm_layout L;
|
||||
if (kparams->token_major) {
|
||||
if (token_major) {
|
||||
const size_t x_elem_size = (src2->type == HTP_TYPE_F16) ? sizeof(__fp16) : sizeof(float);
|
||||
htp_ssm_conv_token_vtcm_layout_build(&L, d_inner_per_hvx, kparams->d_conv, kparams->n_t_tile,
|
||||
kparams->d_inner_tile, n_hvx, x_elem_size, dst_elem_size);
|
||||
@@ -1564,9 +1566,9 @@ int op_ssm_conv_f32(struct htp_ops_context * octx) {
|
||||
|
||||
const bool src0_f16 = (src0->type == HTP_TYPE_F16);
|
||||
const bool dst_f16 = (dst->type == HTP_TYPE_F16);
|
||||
const bool apply_silu = kparams->silu != 0;
|
||||
const bool apply_silu = (kparams->flags & HTP_SSM_CONV_FLAG_SILU) != 0;
|
||||
|
||||
if (kparams->token_major) {
|
||||
if (token_major) {
|
||||
const bool x_f16 = (src2->type == HTP_TYPE_F16);
|
||||
const bool dst_st_f16 = (dst_state && dst_state->type == HTP_TYPE_F16);
|
||||
const bool is_d4 = (kparams->d_conv == 4);
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
#include "htp-ops.h"
|
||||
#include "htp-vtcm.h"
|
||||
|
||||
enum htp_ssm_conv_flags {
|
||||
HTP_SSM_CONV_FLAG_SILU = (1u << 0),
|
||||
HTP_SSM_CONV_FLAG_TOKEN_MAJOR = (1u << 1),
|
||||
};
|
||||
|
||||
struct htp_ssm_conv_kernel_params {
|
||||
uint32_t n_hvx;
|
||||
uint32_t d_conv;
|
||||
@@ -17,8 +22,7 @@ struct htp_ssm_conv_kernel_params {
|
||||
uint32_t d_inner_tile;
|
||||
|
||||
uint32_t vtcm_size;
|
||||
uint32_t silu;
|
||||
uint32_t token_major;
|
||||
uint32_t flags; // bitmask of enum htp_ssm_conv_flags
|
||||
uint32_t n_t_tile;
|
||||
uint32_t n_t_blocks;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#include "hvx-exp.h"
|
||||
#include "hvx-utils.h"
|
||||
#include "hmx-fa-kernels.h"
|
||||
#include "hmx-fa-fp16-kernels.h"
|
||||
#include "hmx-queue.h"
|
||||
#include "hmx-utils.h"
|
||||
|
||||
|
||||
@@ -338,11 +338,6 @@ int op_sum_rows(struct htp_ops_context * octx) {
|
||||
const bool src0_f16 = (src0->type == HTP_TYPE_F16);
|
||||
const bool dst_f16 = (dst->type == HTP_TYPE_F16);
|
||||
|
||||
if ((src0->type != HTP_TYPE_F32 && src0->type != HTP_TYPE_F16) ||
|
||||
(dst->type != HTP_TYPE_F32 && dst->type != HTP_TYPE_F16)) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const struct htp_sum_rows_kernel_params * kparams =
|
||||
(const struct htp_sum_rows_kernel_params *) octx->kernel_params;
|
||||
|
||||
@@ -531,11 +526,6 @@ int op_sum(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
if ((src0->type != HTP_TYPE_F32 && src0->type != HTP_TYPE_F16) ||
|
||||
(dst->type != HTP_TYPE_F32 && dst->type != HTP_TYPE_F16)) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const struct htp_sum_rows_kernel_params * kparams =
|
||||
(const struct htp_sum_rows_kernel_params *) octx->kernel_params;
|
||||
|
||||
@@ -932,10 +922,6 @@ int op_argmax(struct htp_ops_context * octx) {
|
||||
const struct htp_tensor * src0 = octx->src[0];
|
||||
const struct htp_tensor * dst = octx->dst;
|
||||
|
||||
if ((src0->type != HTP_TYPE_F32 && src0->type != HTP_TYPE_F16) || dst->type != HTP_TYPE_I32) {
|
||||
return HTP_STATUS_NO_SUPPORT;
|
||||
}
|
||||
|
||||
const struct htp_sum_rows_kernel_params * kparams =
|
||||
(const struct htp_sum_rows_kernel_params *) octx->kernel_params;
|
||||
|
||||
|
||||
@@ -203,7 +203,7 @@ int op_timestep_embedding(struct htp_ops_context * octx) {
|
||||
}
|
||||
|
||||
FARF(HIGH, "timestep-embedding: %u timesteps dim %u max-period %d split-%s n-hvx %u chunk-rows %u\n",
|
||||
kparams->n_rows, kparams->dim, max_period, kparams->split_cols ? "cols" : "rows", kparams->n_hvx, kparams->chunk_rows);
|
||||
kparams->n_rows, kparams->dim, octx->op_params[1], kparams->split_cols ? "cols" : "rows", kparams->n_hvx, kparams->chunk_rows);
|
||||
|
||||
work_queue_run(octx->ctx->hvx_queue, timestep_thread, &tctx, kparams->n_hvx);
|
||||
|
||||
|
||||
@@ -3250,9 +3250,9 @@ static void unary_thread_row_block(const htp_thread_t * th, unsigned int nth, un
|
||||
.nb01 = nb01, .nb02 = nb02, .nb03 = nb03,
|
||||
.nb1 = nb1,
|
||||
.block = uctx->block,
|
||||
.src0_contig = uctx->kparams->src0_contig,
|
||||
.block_contig = uctx->kparams->src0_contig,
|
||||
.src0_slice_dma = uctx->kparams->src0_slice_dma,
|
||||
.src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
|
||||
.block_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
|
||||
.src0_slice_dma = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_SLICE_DMA) != 0,
|
||||
.ir = src0_start_row,
|
||||
.ir_end = src0_end_row,
|
||||
};
|
||||
@@ -3297,9 +3297,9 @@ static void unary_thread_rms_norm_mul_bc(const htp_thread_t * th, unsigned int n
|
||||
.nb01 = nb01, .nb02 = nb02, .nb03 = nb03,
|
||||
.nb1 = nb1,
|
||||
.block = uctx->block,
|
||||
.src0_contig = uctx->kparams->src0_contig,
|
||||
.block_contig = uctx->kparams->src0_contig,
|
||||
.src0_slice_dma = uctx->kparams->src0_slice_dma,
|
||||
.src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
|
||||
.block_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
|
||||
.src0_slice_dma = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_SLICE_DMA) != 0,
|
||||
.ir = src0_start_row,
|
||||
.ir_end = src0_end_row,
|
||||
};
|
||||
@@ -3341,9 +3341,9 @@ static void unary_thread_rms_norm_mul(const htp_thread_t * th, unsigned int nth,
|
||||
.nb11 = src1->nb[1], .nb12 = src1->nb[2], .nb13 = src1->nb[3],
|
||||
.nb1 = nb1,
|
||||
.block = uctx->block,
|
||||
.src0_contig = uctx->kparams->src0_contig,
|
||||
.src1_contig = uctx->kparams->src1_contig,
|
||||
.block_contig = uctx->kparams->src0_contig && uctx->kparams->src1_contig,
|
||||
.src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
|
||||
.src1_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC1_CONTIG) != 0,
|
||||
.block_contig = (uctx->kparams->flags & (HTP_UNARY_FLAG_SRC0_CONTIG | HTP_UNARY_FLAG_SRC1_CONTIG)) == (HTP_UNARY_FLAG_SRC0_CONTIG | HTP_UNARY_FLAG_SRC1_CONTIG),
|
||||
.src0_slice_dma = false,
|
||||
.ir = src0_start_row,
|
||||
.ir_end = src0_end_row,
|
||||
@@ -3394,8 +3394,8 @@ static void unary_thread_rms_norm_mul_add(const htp_thread_t * th, unsigned int
|
||||
.nb21 = src2->nb[1], .nb22 = src2->nb[2], .nb23 = src2->nb[3],
|
||||
.nb1 = nb1,
|
||||
.block = uctx->block,
|
||||
.src0_contig = uctx->kparams->src0_contig,
|
||||
.block_contig = uctx->kparams->src0_contig,
|
||||
.src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
|
||||
.block_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
|
||||
.src0_slice_dma = false,
|
||||
.ir = src0_start_row,
|
||||
.ir_end = src0_end_row,
|
||||
@@ -3463,8 +3463,8 @@ static void unary_thread_tiled_rms_norm(const htp_thread_t * th, unsigned int nt
|
||||
const struct fastdiv_values * div_ne012 = &uctx->kparams->div_ne012;
|
||||
|
||||
const uint32_t tiles_per_row = uctx->kparams->tiles_per_row;
|
||||
const bool src0_contig = uctx->kparams->src0_contig;
|
||||
const bool src1_contig = uctx->kparams->src1_contig;
|
||||
const bool src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0;
|
||||
const bool src1_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC1_CONTIG) != 0;
|
||||
|
||||
float epsilon = 0.f;
|
||||
float scale = 0.f;
|
||||
@@ -3741,7 +3741,7 @@ static void unary_thread_tiled(const htp_thread_t * th, unsigned int nth, unsign
|
||||
.tile_in_row = 0,
|
||||
.i01 = 0,
|
||||
.tri_ttype = 0,
|
||||
.src0_contig = uctx->kparams->src0_contig,
|
||||
.src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
|
||||
};
|
||||
|
||||
dma_pipe_io_run(th->dma_q, th->trace, &s, in_buf, out_buf,
|
||||
@@ -3873,7 +3873,7 @@ static int execute_op_unary(struct htp_ops_context * octx) {
|
||||
|
||||
const uint32_t col_tile = kparams->col_tile;
|
||||
|
||||
bool broadcast_src1 = kparams->broadcast_src1;
|
||||
bool broadcast_src1 = (kparams->flags & HTP_UNARY_FLAG_BCAST_SRC1) != 0;
|
||||
const uint32_t src1_nrows = broadcast_src1 ? (uint32_t) src1->ne[1] : 0;
|
||||
|
||||
if ((!col_tile && kparams->block == 0) || octx->ctx->vtcm_size < (size_t) kparams->vtcm_size) {
|
||||
@@ -3976,7 +3976,7 @@ static int execute_op_unary(struct htp_ops_context * octx) {
|
||||
break;
|
||||
}
|
||||
|
||||
const bool has_scale_bias = (kparams->has_scale_bias != 0);
|
||||
const bool has_scale_bias = (kparams->flags & HTP_UNARY_FLAG_SCALE_BIAS) != 0;
|
||||
|
||||
if (!compute_func && is_f16) {
|
||||
switch (octx->op) {
|
||||
|
||||
@@ -6,21 +6,24 @@
|
||||
#include "htp-ops.h"
|
||||
#include "htp-vtcm.h"
|
||||
|
||||
enum htp_unary_flags {
|
||||
HTP_UNARY_FLAG_BCAST_SRC1 = (1u << 0),
|
||||
HTP_UNARY_FLAG_SRC0_CONTIG = (1u << 1),
|
||||
HTP_UNARY_FLAG_SRC1_CONTIG = (1u << 2),
|
||||
HTP_UNARY_FLAG_SRC0_SLICE_DMA = (1u << 3),
|
||||
HTP_UNARY_FLAG_SCALE_BIAS = (1u << 4),
|
||||
};
|
||||
|
||||
// Op-specific struct for precomputed unary params
|
||||
struct htp_unary_kernel_params {
|
||||
uint32_t n_hvx;
|
||||
uint32_t col_tile;
|
||||
uint32_t block;
|
||||
uint32_t broadcast_src1;
|
||||
|
||||
uint32_t src0_contig;
|
||||
uint32_t src1_contig;
|
||||
uint32_t src0_slice_dma;
|
||||
uint32_t flags; // bitmask of enum htp_unary_flags
|
||||
|
||||
uint32_t vtcm_size;
|
||||
uint32_t tiles_per_row;
|
||||
|
||||
uint32_t has_scale_bias;
|
||||
float in_scale;
|
||||
float in_bias;
|
||||
float out_scale;
|
||||
@@ -30,7 +33,6 @@ struct htp_unary_kernel_params {
|
||||
struct fastdiv_values div_ne01;
|
||||
struct fastdiv_values div_ne02;
|
||||
struct fastdiv_values div_ne012;
|
||||
struct fastdiv_values div_tpr;
|
||||
};
|
||||
|
||||
#if defined(__cplusplus)
|
||||
|
||||
@@ -221,7 +221,7 @@ static inline void get_src_y_window(
|
||||
const int32_t y1 = ly[last_oy].y[3];
|
||||
*out_src_y_start = (uint32_t) y0;
|
||||
*out_src_y_count = (uint32_t) (y1 - y0 + 1);
|
||||
} else if (p->antialias) {
|
||||
} else if (p->flags & HTP_UPSCALE_FLAG_ANTIALIAS) {
|
||||
const struct upscale_antialias_y * ly = (const struct upscale_antialias_y *) lut_y;
|
||||
const int32_t y0 = ly[dst_y_start].y_min;
|
||||
const int32_t y1 = ly[last_oy].y_max;
|
||||
@@ -892,7 +892,7 @@ int op_upscale(struct htp_ops_context * octx) {
|
||||
if (p->mode == HTP_UPSCALE_MODE_BICUBIC) {
|
||||
lut_x_size = hex_round_up(dst_x * sizeof(struct upscale_bicubic_x), 128);
|
||||
elem_y_sz = sizeof(struct upscale_bicubic_y);
|
||||
} else if (p->antialias) {
|
||||
} else if (p->flags & HTP_UPSCALE_FLAG_ANTIALIAS) {
|
||||
lut_x_size = hex_round_up(dst_x * sizeof(struct upscale_antialias_x), 128);
|
||||
elem_y_sz = sizeof(struct upscale_antialias_y);
|
||||
} else if (p->mode == HTP_UPSCALE_MODE_BILINEAR) {
|
||||
@@ -918,7 +918,7 @@ int op_upscale(struct htp_ops_context * octx) {
|
||||
upscale_init_luts_nearest((uint32_t *) lut_x, (uint32_t *) lut_y, src0, dst, p);
|
||||
} else if (p->mode == HTP_UPSCALE_MODE_BICUBIC) {
|
||||
upscale_init_luts_bicubic((struct upscale_bicubic_x *) lut_x, (struct upscale_bicubic_y *) lut_y, src0, dst, p);
|
||||
} else if (p->antialias) {
|
||||
} else if (p->flags & HTP_UPSCALE_FLAG_ANTIALIAS) {
|
||||
upscale_init_luts_antialias((struct upscale_antialias_x *) lut_x, (struct upscale_antialias_y *) lut_y, src0, dst, p);
|
||||
} else {
|
||||
upscale_init_luts_bilinear((struct upscale_bilinear_x_chunk *) lut_x, (struct upscale_bilinear_y *) lut_y, src0, dst, p);
|
||||
|
||||
@@ -26,11 +26,15 @@ enum htp_upscale_kernel_type {
|
||||
HTP_UPSCALE_KERNEL_BICUBIC = 6,
|
||||
};
|
||||
|
||||
enum htp_upscale_flags {
|
||||
HTP_UPSCALE_FLAG_ALIGN_CORNERS = (1u << 0),
|
||||
HTP_UPSCALE_FLAG_ANTIALIAS = (1u << 1),
|
||||
};
|
||||
|
||||
struct htp_upscale_kernel_params {
|
||||
uint8_t kernel_type;
|
||||
uint8_t mode;
|
||||
uint8_t align_corners;
|
||||
uint8_t antialias;
|
||||
uint8_t flags; // bitmask of enum htp_upscale_flags
|
||||
|
||||
float inv_sf0;
|
||||
float inv_sf1;
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user