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 |
@@ -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
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
find_package(hrx CONFIG REQUIRED)
|
||||
find_package(loomc CONFIG REQUIRED)
|
||||
|
||||
set(GGML_HRX_KERNEL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/kernels")
|
||||
set(GGML_HRX_KERNELS_CPP "${CMAKE_CURRENT_BINARY_DIR}/ggml-hrx-kernels.cpp")
|
||||
|
||||
file(GLOB_RECURSE GGML_HRX_KERNEL_FILES CONFIGURE_DEPENDS "${GGML_HRX_KERNEL_DIR}/*.loom")
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT "${GGML_HRX_KERNELS_CPP}"
|
||||
COMMAND "${CMAKE_COMMAND}"
|
||||
-DKERNEL_DIR=${GGML_HRX_KERNEL_DIR}
|
||||
-DOUTPUT=${GGML_HRX_KERNELS_CPP}
|
||||
-P "${CMAKE_CURRENT_SOURCE_DIR}/embed-kernels.cmake"
|
||||
DEPENDS ${GGML_HRX_KERNEL_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/embed-kernels.cmake"
|
||||
COMMENT "Embedding HRX Loom kernels"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
file(GLOB GGML_HRX_SOURCES CONFIGURE_DEPENDS "*.cpp" "ops/*.cpp")
|
||||
file(GLOB GGML_HRX_HEADERS CONFIGURE_DEPENDS "*.h")
|
||||
|
||||
ggml_add_backend_library(ggml-hrx
|
||||
${GGML_HRX_HEADERS}
|
||||
${GGML_HRX_SOURCES}
|
||||
"${GGML_HRX_KERNELS_CPP}"
|
||||
../../include/ggml-hrx.h
|
||||
)
|
||||
|
||||
target_link_libraries(ggml-hrx PRIVATE hrx::hrx loomc::loomc)
|
||||
target_include_directories(ggml-hrx PRIVATE .)
|
||||
target_compile_features(ggml-hrx PRIVATE cxx_std_17)
|
||||
@@ -0,0 +1,27 @@
|
||||
# Usage: cmake -DKERNEL_DIR=<dir> -DOUTPUT=<file.cpp> -P embed-kernels.cmake
|
||||
|
||||
file(GLOB_RECURSE kernel_files RELATIVE "${KERNEL_DIR}" "${KERNEL_DIR}/*.loom")
|
||||
list(SORT kernel_files)
|
||||
|
||||
set(arrays "")
|
||||
set(table "")
|
||||
set(index 0)
|
||||
foreach(file IN LISTS kernel_files)
|
||||
file(READ "${KERNEL_DIR}/${file}" hex HEX)
|
||||
string(LENGTH "${hex}" hex_len)
|
||||
math(EXPR size "${hex_len} / 2")
|
||||
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," bytes "${hex}")
|
||||
string(APPEND arrays "static const unsigned char ggml_hrx_kernel_${index}[] = { ${bytes}0x00 };\n")
|
||||
string(APPEND table " { \"${file}\", ggml_hrx_kernel_${index}, ${size} },\n")
|
||||
math(EXPR index "${index} + 1")
|
||||
endforeach()
|
||||
|
||||
set(content "// generated by embed-kernels.cmake, do not edit\n")
|
||||
string(APPEND content "#include \"ggml-hrx-common.h\"\n\n")
|
||||
string(APPEND content "${arrays}\n")
|
||||
string(APPEND content "const ggml_hrx_kernel_source ggml_hrx_kernel_sources[] = {\n${table} { nullptr, nullptr, 0 },\n};\n\n")
|
||||
string(APPEND content "const size_t ggml_hrx_kernel_source_count = ${index};\n")
|
||||
|
||||
file(WRITE "${OUTPUT}.tmp" "${content}")
|
||||
execute_process(COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${OUTPUT}.tmp" "${OUTPUT}")
|
||||
file(REMOVE "${OUTPUT}.tmp")
|
||||
@@ -0,0 +1,93 @@
|
||||
#include "ggml-hrx-arch.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
// The limits follow from the kernel layouts.
|
||||
static constexpr bool ggml_hrx_tune_valid(const ggml_hrx_tune & t) {
|
||||
for (int i = 0; i < GGML_TYPE_COUNT; ++i) {
|
||||
const ggml_hrx_mul_mat_vec_tune & mv = t.mul_mat_vec[i];
|
||||
if (mv.max_cols < 1 || mv.max_cols > 8 || mv.xs_max_k < 256 || mv.xs_max_k > 4096 || mv.xs_wg_per_cu < 1 ||
|
||||
mv.xs_depth < 1 || mv.xs_depth > 4) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const bool wg_size = t.elementwise_wg_size >= 64 && t.elementwise_wg_size <= 1024 && t.elementwise_wg_size % 64 == 0;
|
||||
const bool stream = t.stream_wg_per_cu >= 0 && t.stream_wg_per_cu <= 16;
|
||||
return wg_size && stream;
|
||||
}
|
||||
|
||||
// Tuned on gfx1151.
|
||||
static constexpr ggml_hrx_tune ggml_hrx_tune_rdna3_5 = [] {
|
||||
ggml_hrx_tune t = {};
|
||||
t.nodes_per_submit = 64;
|
||||
t.nodes_first_submit = 8;
|
||||
t.mapped_readback = false;
|
||||
for (int i = 0; i < GGML_TYPE_COUNT; ++i) {
|
||||
t.mul_mat_vec[i] = {
|
||||
/* .max_cols = */ 8,
|
||||
/* .xs_max_k = */ 4096,
|
||||
/* .xs_wg_per_cu = */ 4,
|
||||
/* .xs_depth = */ 2,
|
||||
};
|
||||
}
|
||||
t.elementwise_wg_size = 256;
|
||||
t.stream_wg_per_cu = 0;
|
||||
return t;
|
||||
}();
|
||||
|
||||
// Untuned. These families use the RDNA3.5 values.
|
||||
static constexpr ggml_hrx_tune ggml_hrx_tune_rdna3 = ggml_hrx_tune_rdna3_5;
|
||||
static constexpr ggml_hrx_tune ggml_hrx_tune_rdna4 = ggml_hrx_tune_rdna3_5;
|
||||
|
||||
// Tuned on gfx1201.
|
||||
static constexpr ggml_hrx_tune ggml_hrx_tune_gfx1201 = [] {
|
||||
ggml_hrx_tune t = ggml_hrx_tune_rdna4;
|
||||
t.nodes_per_submit = 256;
|
||||
// hrx_stream_copy_d2h over the host link is slow. A device copy to mapped host memory is faster.
|
||||
t.mapped_readback = true;
|
||||
for (int i = 0; i < GGML_TYPE_COUNT; ++i) {
|
||||
t.mul_mat_vec[i].xs_wg_per_cu = 2;
|
||||
}
|
||||
// A dispatch that fills all wave slots of the device costs approximately 30 us more on gfx1201.
|
||||
// 4 workgroups of 256 lanes for each CU use half of the slots.
|
||||
t.stream_wg_per_cu = 4;
|
||||
return t;
|
||||
}();
|
||||
|
||||
static constexpr uint32_t GGML_HRX_WG_32BIT = UINT32_MAX;
|
||||
// Architected workgroup ids pack y and z into one 32-bit register.
|
||||
static constexpr uint32_t GGML_HRX_WG_16BIT = 65536;
|
||||
|
||||
static constexpr ggml_hrx_arch ggml_hrx_archs[] = {
|
||||
// RDNA3. Untuned.
|
||||
{ "gfx1100", "gfx1100", true, GGML_HRX_MATRIX_OP_WMMA_GFX11, GGML_HRX_WG_32BIT, &ggml_hrx_tune_rdna3 },
|
||||
{ "gfx1101", "gfx1101", true, GGML_HRX_MATRIX_OP_WMMA_GFX11, GGML_HRX_WG_32BIT, &ggml_hrx_tune_rdna3 },
|
||||
{ "gfx1102", "gfx1102", true, GGML_HRX_MATRIX_OP_WMMA_GFX11, GGML_HRX_WG_32BIT, &ggml_hrx_tune_rdna3 },
|
||||
{ "gfx1103", "gfx1103", true, GGML_HRX_MATRIX_OP_WMMA_GFX11, GGML_HRX_WG_32BIT, &ggml_hrx_tune_rdna3 },
|
||||
// RDNA3.5. Only gfx1151 is tuned.
|
||||
{ "gfx1150", "gfx1150", true, GGML_HRX_MATRIX_OP_WMMA_GFX11, GGML_HRX_WG_32BIT, &ggml_hrx_tune_rdna3_5 },
|
||||
{ "gfx1151", "gfx1151", true, GGML_HRX_MATRIX_OP_WMMA_GFX11, GGML_HRX_WG_32BIT, &ggml_hrx_tune_rdna3_5 },
|
||||
{ "gfx1152", "gfx1152", true, GGML_HRX_MATRIX_OP_WMMA_GFX11, GGML_HRX_WG_32BIT, &ggml_hrx_tune_rdna3_5 },
|
||||
// RDNA4. Only gfx1201 is tuned.
|
||||
{ "gfx1200", "gfx1200", true, GGML_HRX_MATRIX_OP_WMMA_GFX12, GGML_HRX_WG_16BIT, &ggml_hrx_tune_rdna4 },
|
||||
{ "gfx1201", "gfx1201", true, GGML_HRX_MATRIX_OP_WMMA_GFX12, GGML_HRX_WG_16BIT, &ggml_hrx_tune_gfx1201 },
|
||||
};
|
||||
|
||||
static constexpr bool ggml_hrx_archs_valid() {
|
||||
for (const ggml_hrx_arch & arch : ggml_hrx_archs) {
|
||||
if (!ggml_hrx_tune_valid(*arch.tune)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
static_assert(ggml_hrx_archs_valid(), "ggml_hrx_tune value out of range");
|
||||
|
||||
const ggml_hrx_arch * ggml_hrx_arch_find(const char * name) {
|
||||
for (const ggml_hrx_arch & arch : ggml_hrx_archs) {
|
||||
if (std::strcmp(arch.name, name) == 0) {
|
||||
return &arch;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "ggml.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
enum ggml_hrx_matrix_op {
|
||||
GGML_HRX_MATRIX_OP_NONE,
|
||||
GGML_HRX_MATRIX_OP_WMMA_GFX11,
|
||||
GGML_HRX_MATRIX_OP_WMMA_GFX12, // The operand layout is different from gfx11. The WMMA motifs have gfx12 providers.
|
||||
};
|
||||
|
||||
// ggml_mul_mat_vec_xs.
|
||||
struct ggml_hrx_mul_mat_vec_tune {
|
||||
int64_t max_cols; // At most 8. More input columns use the tiled kernels.
|
||||
int64_t xs_max_k; // At most 4096. The vec_xs kernel keeps 16 superblocks of activations in registers.
|
||||
int64_t xs_wg_per_cu;
|
||||
int64_t xs_depth; // At most 4. Pipeline depth of the vec_xs row passes.
|
||||
};
|
||||
|
||||
// Op dispatch values for one architecture.
|
||||
struct ggml_hrx_tune {
|
||||
int nodes_per_submit; // Nodes between stream flushes.
|
||||
int nodes_first_submit; // The interval starts here and doubles up to nodes_per_submit.
|
||||
// get_tensor_async copies through mapped host memory instead of hrx_stream_copy_d2h.
|
||||
bool mapped_readback;
|
||||
|
||||
// Index: the weight tensor type (src0).
|
||||
ggml_hrx_mul_mat_vec_tune mul_mat_vec[GGML_TYPE_COUNT];
|
||||
|
||||
int64_t elementwise_wg_size; // Multiple of 64, at most 1024.
|
||||
// At most 16. Workgroup limit for each CU of the grid-stride copy kernel. 0 disables this kernel.
|
||||
int64_t stream_wg_per_cu;
|
||||
};
|
||||
|
||||
struct ggml_hrx_arch {
|
||||
const char * name; // Architecture that the device reports.
|
||||
const char * loom_target; // Loom AMDGPU target of the kernels.
|
||||
bool wave32;
|
||||
ggml_hrx_matrix_op matrix_op;
|
||||
uint32_t max_workgroups_yz; // Workgroup count limit in y and z.
|
||||
const ggml_hrx_tune * tune;
|
||||
};
|
||||
|
||||
// Returns nullptr if the backend does not support the architecture.
|
||||
const ggml_hrx_arch * ggml_hrx_arch_find(const char * name);
|
||||
@@ -0,0 +1,135 @@
|
||||
#pragma once
|
||||
|
||||
#include "ggml-backend-impl.h"
|
||||
#include "ggml-impl.h"
|
||||
#include "ggml-hrx-arch.h"
|
||||
#include "hrx_runtime.h"
|
||||
#include "loom-jit.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// embed-kernels.cmake generates these sources.
|
||||
struct ggml_hrx_kernel_source {
|
||||
const char * path;
|
||||
const unsigned char * data;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
extern const ggml_hrx_kernel_source ggml_hrx_kernel_sources[];
|
||||
extern const size_t ggml_hrx_kernel_source_count;
|
||||
|
||||
struct ggml_hrx_kernel {
|
||||
hrx_executable_t executable = nullptr;
|
||||
uint32_t export_ordinal = 0;
|
||||
hrx_executable_export_info_t export_info = {};
|
||||
ggml_hrx_loom_launch_program * launch = nullptr;
|
||||
size_t n_scalars = 0;
|
||||
size_t scalar_size = 0;
|
||||
|
||||
~ggml_hrx_kernel();
|
||||
};
|
||||
|
||||
struct ggml_hrx_kernel_desc {
|
||||
const char * file = nullptr; // Path relative to kernels/.
|
||||
const char * symbol = nullptr;
|
||||
std::vector<const char *> libs;
|
||||
std::vector<std::pair<std::string, std::string>> config;
|
||||
};
|
||||
|
||||
struct ggml_hrx_device_context {
|
||||
int index = 0;
|
||||
std::string name;
|
||||
std::string description;
|
||||
size_t memory_total = 0;
|
||||
uint32_t n_cu = 0;
|
||||
|
||||
const ggml_hrx_arch * arch = nullptr;
|
||||
const ggml_hrx_tune * tune = nullptr;
|
||||
|
||||
hrx_device_t device = nullptr;
|
||||
hrx_queue_t transfer_queue = nullptr;
|
||||
hrx_queue_t stream_queue = nullptr;
|
||||
|
||||
// Locks buffer fills, copies and uploads that run outside a backend stream.
|
||||
std::mutex buffer_mutex;
|
||||
hrx_stream_t buffer_stream = nullptr;
|
||||
// True until the host waits for buffer_stream. A flush is not enough: transfer_queue reads do not wait for buffer_stream.
|
||||
bool upload_pending = false;
|
||||
|
||||
ggml_backend_buffer_type buft = {};
|
||||
|
||||
std::mutex kernel_mutex;
|
||||
ggml_hrx_loom_jit * jit = nullptr;
|
||||
std::map<std::string, std::unique_ptr<ggml_hrx_kernel>> kernels;
|
||||
};
|
||||
|
||||
struct ggml_backend_hrx_context {
|
||||
ggml_hrx_device_context * device = nullptr;
|
||||
hrx_stream_t stream = nullptr;
|
||||
std::string name;
|
||||
uint64_t upload_wait = 0; // Last buffer_stream timeline value that this stream waits on.
|
||||
|
||||
// Mapped host memory for get_tensor_async. The stream copies into it. synchronize copies it to the caller memory.
|
||||
struct readback_copy {
|
||||
void * dst;
|
||||
size_t offset;
|
||||
size_t size;
|
||||
};
|
||||
hrx_buffer_t readback = nullptr;
|
||||
const uint8_t * readback_host = nullptr;
|
||||
size_t readback_size = 0;
|
||||
size_t readback_used = 0;
|
||||
bool readback_failed = false;
|
||||
std::vector<readback_copy> readback_copies;
|
||||
};
|
||||
|
||||
// Logs a failed status and returns false. It frees the status.
|
||||
bool ggml_hrx_check(hrx_status_t status, const char * expr, const char * file, int line);
|
||||
#define GGML_HRX_CHECK(expr) ggml_hrx_check((expr), #expr, __FILE__, __LINE__)
|
||||
|
||||
// Compiles each kernel one time for each device. Returns nullptr on failure.
|
||||
ggml_hrx_kernel * ggml_hrx_get_kernel(ggml_hrx_device_context * dev, const ggml_hrx_kernel_desc & desc);
|
||||
|
||||
// workload: the root kernel.def arguments. They set the launch config.
|
||||
// scalars: the launch() index arguments, in order. Each value must be >= 0.
|
||||
// buffers: the launch() buffer arguments, in order. Each binds the data range of its tensor, with the view offset.
|
||||
bool ggml_hrx_dispatch(ggml_backend_hrx_context * ctx,
|
||||
ggml_hrx_kernel * kernel,
|
||||
const std::vector<int64_t> & workload,
|
||||
const std::vector<int64_t> & scalars,
|
||||
const std::vector<const ggml_tensor *> & buffers);
|
||||
|
||||
inline bool ggml_hrx_elem_strides(const ggml_tensor * t, int64_t s[GGML_MAX_DIMS]) {
|
||||
const size_t ts = ggml_type_size(t->type);
|
||||
if (ggml_blck_size(t->type) != 1) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < GGML_MAX_DIMS; ++i) {
|
||||
if (t->nb[i] % ts != 0) {
|
||||
return false;
|
||||
}
|
||||
s[i] = (int64_t) (t->nb[i] / ts);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Config of the kernels that use @ggml.elementwise.wg_size.
|
||||
inline std::pair<std::string, std::string> ggml_hrx_elementwise_wg_config(const ggml_hrx_device_context * dev) {
|
||||
return { "ggml.elementwise.wg_size", std::to_string(dev->tune->elementwise_wg_size) };
|
||||
}
|
||||
|
||||
// Workgroups of a grid-stride kernel that moves n f32 values, 4 for each lane in one pass.
|
||||
// Returns 0 if the tune disables the grid-stride kernels.
|
||||
inline int64_t ggml_hrx_stream_groups(const ggml_hrx_device_context * dev, int64_t n) {
|
||||
const int64_t per_group = 4 * dev->tune->elementwise_wg_size;
|
||||
const int64_t limit = dev->tune->stream_wg_per_cu * dev->n_cu;
|
||||
return limit > 0 ? std::min((n + per_group - 1) / per_group, limit) : 0;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "ggml-hrx-common.h"
|
||||
|
||||
// supports_X must return true only for nodes that op_X can run. op_X returns false on failure.
|
||||
|
||||
bool ggml_hrx_supports_cpy(const ggml_hrx_device_context * dev, const ggml_tensor * op); // CPY DUP CONT
|
||||
bool ggml_hrx_op_cpy(ggml_backend_hrx_context * ctx, const ggml_tensor * node);
|
||||
|
||||
bool ggml_hrx_supports_set_rows(const ggml_hrx_device_context * dev, const ggml_tensor * op);
|
||||
bool ggml_hrx_op_set_rows(ggml_backend_hrx_context * ctx, const ggml_tensor * node);
|
||||
|
||||
bool ggml_hrx_supports_mul_mat(const ggml_hrx_device_context * dev, const ggml_tensor * op);
|
||||
bool ggml_hrx_op_mul_mat(ggml_backend_hrx_context * ctx, const ggml_tensor * node);
|
||||
@@ -0,0 +1,962 @@
|
||||
#include "ggml-hrx.h"
|
||||
|
||||
#include "ggml-hrx-common.h"
|
||||
#include "ggml-hrx-ops.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cinttypes>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
|
||||
static constexpr size_t GGML_HRX_ALIGNMENT = 256;
|
||||
|
||||
// Device memory has no host address. Tensor data pointers are offsets from this fake base. The Vulkan backend uses the same method.
|
||||
static uint8_t * const GGML_HRX_BASE = (uint8_t *) (uintptr_t) 0x1000;
|
||||
|
||||
bool ggml_hrx_check(hrx_status_t status, const char * expr, const char * file, int line) {
|
||||
if (hrx_status_is_ok(status)) {
|
||||
return true;
|
||||
}
|
||||
char * message = nullptr;
|
||||
size_t length = 0;
|
||||
hrx_status_t format_status = hrx_status_to_string(status, &message, &length);
|
||||
if (!hrx_status_is_ok(format_status)) {
|
||||
hrx_status_ignore(format_status);
|
||||
}
|
||||
GGML_LOG_ERROR("%s:%d: %s failed: %s\n", file, line, expr, message != nullptr ? message : "unknown HRX error");
|
||||
hrx_status_free_message(message);
|
||||
hrx_status_ignore(status);
|
||||
return false;
|
||||
}
|
||||
|
||||
struct ggml_hrx_buffer_context {
|
||||
ggml_hrx_device_context * dev = nullptr;
|
||||
hrx_buffer_t buffer = nullptr;
|
||||
};
|
||||
|
||||
static void * ggml_backend_hrx_buffer_get_base(ggml_backend_buffer_t buffer) {
|
||||
GGML_UNUSED(buffer);
|
||||
return GGML_HRX_BASE;
|
||||
}
|
||||
|
||||
static size_t ggml_hrx_tensor_offset(const ggml_tensor * t) {
|
||||
return (size_t) ((const uint8_t *) t->data - GGML_HRX_BASE);
|
||||
}
|
||||
|
||||
static ggml_backend_buffer_t ggml_hrx_tensor_buffer(const ggml_tensor * t) {
|
||||
return t->view_src != nullptr ? t->view_src->buffer : t->buffer;
|
||||
}
|
||||
|
||||
static bool ggml_hrx_tensor_on_device(const ggml_tensor * t) {
|
||||
ggml_backend_buffer_t buffer = ggml_hrx_tensor_buffer(t);
|
||||
return buffer != nullptr && buffer->iface.get_base == ggml_backend_hrx_buffer_get_base;
|
||||
}
|
||||
|
||||
// buffer_stream records uploads up to this size. They run before the next graph or read.
|
||||
static constexpr size_t GGML_HRX_MAX_RECORDED_UPLOAD = 1024 * 1024;
|
||||
// This is the HAL limit for one update_buffer command.
|
||||
static constexpr size_t GGML_HRX_UPDATE_CHUNK = 64 * 1024;
|
||||
|
||||
// Hold buffer_mutex when you call this function.
|
||||
static bool ggml_hrx_sync_buffer_stream(ggml_hrx_device_context * dev) {
|
||||
dev->upload_pending = false;
|
||||
return GGML_HRX_CHECK(hrx_stream_synchronize(dev->buffer_stream));
|
||||
}
|
||||
|
||||
static bool ggml_hrx_finish_uploads(ggml_hrx_device_context * dev) {
|
||||
std::lock_guard<std::mutex> lock(dev->buffer_mutex);
|
||||
return !dev->upload_pending || ggml_hrx_sync_buffer_stream(dev);
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_buffer_free_buffer(ggml_backend_buffer_t buffer) {
|
||||
auto * ctx = (ggml_hrx_buffer_context *) buffer->context;
|
||||
if (ctx->buffer != nullptr) {
|
||||
ggml_hrx_finish_uploads(ctx->dev);
|
||||
hrx_buffer_release(ctx->buffer);
|
||||
}
|
||||
delete ctx;
|
||||
}
|
||||
|
||||
struct ggml_hrx_fill_args {
|
||||
hrx_buffer_t buffer;
|
||||
size_t offset;
|
||||
size_t size;
|
||||
uint8_t value;
|
||||
};
|
||||
|
||||
static bool ggml_hrx_fill(ggml_hrx_device_context * dev, const ggml_hrx_fill_args & args) {
|
||||
std::lock_guard<std::mutex> lock(dev->buffer_mutex);
|
||||
if (!GGML_HRX_CHECK(hrx_stream_fill_buffer(dev->buffer_stream, args.buffer, args.offset, args.size, &args.value, sizeof(args.value)))) {
|
||||
return false;
|
||||
}
|
||||
return ggml_hrx_sync_buffer_stream(dev);
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_buffer_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value,
|
||||
size_t offset, size_t size) {
|
||||
auto * ctx = (ggml_hrx_buffer_context *) buffer->context;
|
||||
const size_t dst = ggml_hrx_tensor_offset(tensor) + offset;
|
||||
GGML_ASSERT(dst <= buffer->size && size <= buffer->size - dst);
|
||||
if (!ggml_hrx_fill(ctx->dev, { ctx->buffer, dst, size, value })) {
|
||||
GGML_ABORT("%s: HRX buffer fill failed", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_buffer_set_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, const void * data,
|
||||
size_t offset, size_t size) {
|
||||
auto * ctx = (ggml_hrx_buffer_context *) buffer->context;
|
||||
const size_t dst = ggml_hrx_tensor_offset(tensor) + offset;
|
||||
GGML_ASSERT(dst <= buffer->size && size <= buffer->size - dst);
|
||||
ggml_hrx_device_context * dev = ctx->dev;
|
||||
if (size <= GGML_HRX_MAX_RECORDED_UPLOAD) {
|
||||
std::lock_guard<std::mutex> lock(dev->buffer_mutex);
|
||||
// The stream copies the data now. The caller can reuse it.
|
||||
for (size_t i = 0; i < size; i += GGML_HRX_UPDATE_CHUNK) {
|
||||
const size_t n = std::min(GGML_HRX_UPDATE_CHUNK, size - i);
|
||||
if (!GGML_HRX_CHECK(hrx_stream_update_buffer(dev->buffer_stream, (const uint8_t *) data + i, n, ctx->buffer, dst + i))) {
|
||||
GGML_ABORT("%s: HRX upload failed", __func__);
|
||||
}
|
||||
}
|
||||
dev->upload_pending = true;
|
||||
return;
|
||||
}
|
||||
if (!ggml_hrx_finish_uploads(dev) || !GGML_HRX_CHECK(hrx_synchronous_h2d(dev->transfer_queue, data, ctx->buffer, dst, size))) {
|
||||
GGML_ABORT("%s: HRX upload failed", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_buffer_get_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * tensor, void * data,
|
||||
size_t offset, size_t size) {
|
||||
auto * ctx = (ggml_hrx_buffer_context *) buffer->context;
|
||||
const size_t src = ggml_hrx_tensor_offset(tensor) + offset;
|
||||
GGML_ASSERT(src <= buffer->size && size <= buffer->size - src);
|
||||
if (!ggml_hrx_finish_uploads(ctx->dev) ||
|
||||
!GGML_HRX_CHECK(hrx_synchronous_d2h(ctx->dev->transfer_queue, ctx->buffer, src, data, size))) {
|
||||
GGML_ABORT("%s: HRX download failed", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
static bool ggml_backend_hrx_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const ggml_tensor * src, ggml_tensor * dst) {
|
||||
if (!ggml_hrx_tensor_on_device(src)) {
|
||||
return false;
|
||||
}
|
||||
auto * src_ctx = (ggml_hrx_buffer_context *) ggml_hrx_tensor_buffer(src)->context;
|
||||
auto * dst_ctx = (ggml_hrx_buffer_context *) buffer->context;
|
||||
if (src_ctx->dev != dst_ctx->dev) {
|
||||
return false;
|
||||
}
|
||||
const size_t size = ggml_nbytes(src);
|
||||
if (size == 0) {
|
||||
return true;
|
||||
}
|
||||
ggml_hrx_device_context * dev = dst_ctx->dev;
|
||||
std::lock_guard<std::mutex> lock(dev->buffer_mutex);
|
||||
if (!GGML_HRX_CHECK(hrx_stream_copy_buffer(dev->buffer_stream, src_ctx->buffer, ggml_hrx_tensor_offset(src),
|
||||
dst_ctx->buffer, ggml_hrx_tensor_offset(dst), size))) {
|
||||
return false;
|
||||
}
|
||||
return ggml_hrx_sync_buffer_stream(dev);
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
|
||||
auto * ctx = (ggml_hrx_buffer_context *) buffer->context;
|
||||
if (ctx->buffer == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (!ggml_hrx_fill(ctx->dev, { ctx->buffer, 0, buffer->size, value })) {
|
||||
GGML_ABORT("%s: HRX buffer clear failed", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
static const ggml_backend_buffer_i ggml_backend_hrx_buffer_interface = {
|
||||
/* .free_buffer = */ ggml_backend_hrx_buffer_free_buffer,
|
||||
/* .get_base = */ ggml_backend_hrx_buffer_get_base,
|
||||
/* .init_tensor = */ nullptr,
|
||||
/* .memset_tensor = */ ggml_backend_hrx_buffer_memset_tensor,
|
||||
/* .set_tensor = */ ggml_backend_hrx_buffer_set_tensor,
|
||||
/* .get_tensor = */ ggml_backend_hrx_buffer_get_tensor,
|
||||
/* .set_tensor_2d = */ nullptr,
|
||||
/* .get_tensor_2d = */ nullptr,
|
||||
/* .cpy_tensor = */ ggml_backend_hrx_buffer_cpy_tensor,
|
||||
/* .clear = */ ggml_backend_hrx_buffer_clear,
|
||||
/* .reset = */ nullptr,
|
||||
/* .set_tensor_n = */ nullptr,
|
||||
/* .get_tensor_n = */ nullptr,
|
||||
/* .cpy_tensor_n = */ nullptr,
|
||||
};
|
||||
|
||||
static const char * ggml_backend_hrx_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
|
||||
return ((ggml_hrx_device_context *) buft->context)->name.c_str();
|
||||
}
|
||||
|
||||
static ggml_backend_buffer_t ggml_backend_hrx_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
|
||||
auto * dev = (ggml_hrx_device_context *) buft->context;
|
||||
|
||||
hrx_buffer_t allocation = nullptr;
|
||||
if (size > 0) {
|
||||
const hrx_buffer_params_t params = {
|
||||
HRX_MEMORY_TYPE_DEVICE_LOCAL,
|
||||
HRX_MEMORY_ACCESS_ALL,
|
||||
HRX_BUFFER_USAGE_DEFAULT,
|
||||
};
|
||||
if (!GGML_HRX_CHECK(hrx_allocator_allocate_buffer(hrx_device_allocator(dev->device), params, size, &allocation))) {
|
||||
GGML_LOG_ERROR("%s: failed to allocate %.2f MiB on %s\n", __func__, size / 1024.0 / 1024.0, dev->name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
auto * ctx = new ggml_hrx_buffer_context;
|
||||
ctx->dev = dev;
|
||||
ctx->buffer = allocation;
|
||||
return ggml_backend_buffer_init(buft, ggml_backend_hrx_buffer_interface, ctx, size);
|
||||
}
|
||||
|
||||
static size_t ggml_backend_hrx_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
|
||||
GGML_UNUSED(buft);
|
||||
return GGML_HRX_ALIGNMENT;
|
||||
}
|
||||
|
||||
static size_t ggml_backend_hrx_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
|
||||
return ((ggml_hrx_device_context *) buft->context)->memory_total;
|
||||
}
|
||||
|
||||
static const ggml_backend_buffer_type_i ggml_backend_hrx_buffer_type_interface = {
|
||||
/* .get_name = */ ggml_backend_hrx_buffer_type_get_name,
|
||||
/* .alloc_buffer = */ ggml_backend_hrx_buffer_type_alloc_buffer,
|
||||
/* .alloc_buffer_n = */ nullptr,
|
||||
/* .get_alignment = */ ggml_backend_hrx_buffer_type_get_alignment,
|
||||
/* .get_max_size = */ ggml_backend_hrx_buffer_type_get_max_size,
|
||||
/* .get_alloc_size = */ nullptr,
|
||||
/* .get_alloc_size_n = */ nullptr,
|
||||
/* .is_host = */ nullptr,
|
||||
};
|
||||
|
||||
ggml_hrx_kernel::~ggml_hrx_kernel() {
|
||||
ggml_hrx_loom_launch_program_free(launch);
|
||||
if (executable != nullptr) {
|
||||
hrx_executable_release(executable);
|
||||
}
|
||||
}
|
||||
|
||||
static const ggml_hrx_kernel_source * ggml_hrx_find_source(const char * path) {
|
||||
for (size_t i = 0; i < ggml_hrx_kernel_source_count; ++i) {
|
||||
if (std::strcmp(ggml_hrx_kernel_sources[i].path, path) == 0) {
|
||||
return &ggml_hrx_kernel_sources[i];
|
||||
}
|
||||
}
|
||||
GGML_LOG_ERROR("%s: kernel source %s is not embedded\n", __func__, path);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static std::unique_ptr<ggml_hrx_kernel> ggml_hrx_build_kernel(ggml_hrx_device_context * dev, const ggml_hrx_kernel_desc & desc) {
|
||||
if (dev->jit == nullptr && !GGML_HRX_CHECK(ggml_hrx_loom_jit_create(dev->arch->name, dev->arch->loom_target, &dev->jit))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const ggml_hrx_kernel_source * source = ggml_hrx_find_source(desc.file);
|
||||
if (source == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
std::vector<ggml_hrx_loom_source> deps;
|
||||
for (const char * lib : desc.libs) {
|
||||
const ggml_hrx_kernel_source * dep = ggml_hrx_find_source(lib);
|
||||
if (dep == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
deps.push_back({ dep->data, dep->size, dep->path });
|
||||
}
|
||||
std::vector<ggml_hrx_loom_config_binding> config;
|
||||
for (const auto & kv : desc.config) {
|
||||
config.push_back({ kv.first.c_str(), kv.second.c_str() });
|
||||
}
|
||||
|
||||
ggml_hrx_loom_compile_options options;
|
||||
options.source = { source->data, source->size, source->path };
|
||||
options.deps = deps.data();
|
||||
options.n_deps = deps.size();
|
||||
options.config = config.data();
|
||||
options.n_config = config.size();
|
||||
options.symbol = desc.symbol;
|
||||
|
||||
ggml_hrx_loom_compile_result compiled;
|
||||
if (!GGML_HRX_CHECK(ggml_hrx_loom_compile(dev->jit, &options, &compiled))) {
|
||||
GGML_LOG_ERROR("%s: failed to compile %s from %s\n", __func__, desc.symbol, desc.file);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto kernel = std::make_unique<ggml_hrx_kernel>();
|
||||
kernel->launch = compiled.launch;
|
||||
if (!GGML_HRX_CHECK(hrx_executable_load_data(dev->device, compiled.hsaco.data(), compiled.hsaco.size(), "amdgpu",
|
||||
dev->arch->name, &kernel->executable)) ||
|
||||
!GGML_HRX_CHECK(hrx_executable_lookup_export_by_name(kernel->executable, desc.symbol, &kernel->export_ordinal)) ||
|
||||
!GGML_HRX_CHECK(hrx_executable_export_info(kernel->executable, kernel->export_ordinal, &kernel->export_info))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const hrx_executable_export_info_t & info = kernel->export_info;
|
||||
if (info.parameter_count < info.binding_count) {
|
||||
GGML_LOG_ERROR("%s: %s has an invalid parameter count\n", __func__, desc.symbol);
|
||||
return nullptr;
|
||||
}
|
||||
kernel->n_scalars = info.parameter_count - info.binding_count;
|
||||
if (kernel->n_scalars == 0) {
|
||||
if (info.constant_byte_length != 0) {
|
||||
GGML_LOG_ERROR("%s: %s has constants but no scalar parameters\n", __func__, desc.symbol);
|
||||
return nullptr;
|
||||
}
|
||||
} else {
|
||||
kernel->scalar_size = info.constant_byte_length / kernel->n_scalars;
|
||||
if (info.constant_byte_length % kernel->n_scalars != 0 || (kernel->scalar_size != 4 && kernel->scalar_size != 8)) {
|
||||
GGML_LOG_ERROR("%s: %s has an unsupported scalar ABI\n", __func__, desc.symbol);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return kernel;
|
||||
}
|
||||
|
||||
ggml_hrx_kernel * ggml_hrx_get_kernel(ggml_hrx_device_context * dev, const ggml_hrx_kernel_desc & desc) {
|
||||
std::string key = std::string(desc.file) + "|" + desc.symbol;
|
||||
for (const auto & kv : desc.config) {
|
||||
key += "|" + kv.first + "=" + kv.second;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(dev->kernel_mutex);
|
||||
auto it = dev->kernels.find(key);
|
||||
if (it != dev->kernels.end()) {
|
||||
return it->second.get();
|
||||
}
|
||||
// Cache a failed build as nullptr. This prevents a retry for each node.
|
||||
auto & slot = dev->kernels[key];
|
||||
slot = ggml_hrx_build_kernel(dev, desc);
|
||||
return slot.get();
|
||||
}
|
||||
|
||||
bool ggml_hrx_dispatch(ggml_backend_hrx_context * ctx, ggml_hrx_kernel * kernel, const std::vector<int64_t> & workload,
|
||||
const std::vector<int64_t> & scalars, const std::vector<const ggml_tensor *> & buffers) {
|
||||
if (kernel == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (scalars.size() != kernel->n_scalars || buffers.size() != kernel->export_info.binding_count) {
|
||||
GGML_LOG_ERROR("%s: argument count mismatch: %zu scalars, %zu buffers\n", __func__, scalars.size(), buffers.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
ggml_hrx_loom_launch_config launch;
|
||||
if (!GGML_HRX_CHECK(ggml_hrx_loom_launch_program_eval(kernel->launch, workload.data(), workload.size(), &launch))) {
|
||||
return false;
|
||||
}
|
||||
const uint32_t max_yz = ctx->device->arch->max_workgroups_yz;
|
||||
if (launch.workgroup_count[1] > max_yz || launch.workgroup_count[2] > max_yz) {
|
||||
GGML_LOG_ERROR("%s: workgroup count y or z is above %u\n", __func__, max_yz);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> constants(kernel->n_scalars * kernel->scalar_size);
|
||||
for (size_t i = 0; i < scalars.size(); ++i) {
|
||||
const int64_t value = scalars[i];
|
||||
if (value < 0 || (kernel->scalar_size == 4 && (uint64_t) value > std::numeric_limits<uint32_t>::max())) {
|
||||
GGML_LOG_ERROR("%s: launch scalar %zu out of range: %" PRId64 "\n", __func__, i, value);
|
||||
return false;
|
||||
}
|
||||
if (kernel->scalar_size == 4) {
|
||||
const uint32_t v = (uint32_t) value;
|
||||
std::memcpy(constants.data() + i * 4, &v, 4);
|
||||
} else {
|
||||
const uint64_t v = (uint64_t) value;
|
||||
std::memcpy(constants.data() + i * 8, &v, 8);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<hrx_buffer_ref_t> refs;
|
||||
refs.reserve(buffers.size());
|
||||
for (const ggml_tensor * t : buffers) {
|
||||
if (t == nullptr || !ggml_hrx_tensor_on_device(t)) {
|
||||
GGML_LOG_ERROR("%s: tensor %s is not in an HRX buffer\n", __func__, t != nullptr ? t->name : "(null)");
|
||||
return false;
|
||||
}
|
||||
auto * bctx = (ggml_hrx_buffer_context *) ggml_hrx_tensor_buffer(t)->context;
|
||||
refs.push_back({ bctx->buffer, ggml_hrx_tensor_offset(t), ggml_nbytes(t) });
|
||||
}
|
||||
|
||||
const hrx_dispatch_config_t config = {
|
||||
{ launch.workgroup_count[0], launch.workgroup_count[1], launch.workgroup_count[2] },
|
||||
{ launch.workgroup_size[0], launch.workgroup_size[1], launch.workgroup_size[2] },
|
||||
launch.subgroup_size,
|
||||
};
|
||||
return GGML_HRX_CHECK(hrx_stream_dispatch(ctx->stream, kernel->executable, kernel->export_ordinal, &config,
|
||||
constants.data(), constants.size(), refs.data(), refs.size(), 0));
|
||||
}
|
||||
|
||||
static ggml_guid_t ggml_backend_hrx_guid() {
|
||||
static ggml_guid guid = { 0xd2, 0x3d, 0x72, 0x83, 0xb2, 0x82, 0x4d, 0xe0, 0x8a, 0x3e, 0x21, 0x1d, 0x68, 0x87, 0x2f, 0x4b };
|
||||
return &guid;
|
||||
}
|
||||
|
||||
static const char * ggml_backend_hrx_get_name(ggml_backend_t backend) {
|
||||
return ((ggml_backend_hrx_context *) backend->context)->name.c_str();
|
||||
}
|
||||
|
||||
static void ggml_hrx_readback_release(ggml_backend_hrx_context * ctx) {
|
||||
if (ctx->readback != nullptr) {
|
||||
GGML_HRX_CHECK(hrx_buffer_unmap(ctx->readback));
|
||||
hrx_buffer_release(ctx->readback);
|
||||
}
|
||||
ctx->readback = nullptr;
|
||||
ctx->readback_host = nullptr;
|
||||
ctx->readback_size = 0;
|
||||
}
|
||||
|
||||
// Returns false if the readback buffer cannot take size more bytes now.
|
||||
static bool ggml_hrx_readback_reserve(ggml_backend_hrx_context * ctx, size_t size) {
|
||||
if (ctx->readback_used + size <= ctx->readback_size) {
|
||||
return true;
|
||||
}
|
||||
// Pending copies use the current buffer.
|
||||
if (ctx->readback_failed || !ctx->readback_copies.empty()) {
|
||||
return false;
|
||||
}
|
||||
size = std::max(size, 2 * ctx->readback_size);
|
||||
ggml_hrx_readback_release(ctx);
|
||||
const hrx_buffer_params_t params = {
|
||||
HRX_MEMORY_TYPE_HOST_LOCAL | HRX_MEMORY_TYPE_HOST_COHERENT,
|
||||
HRX_MEMORY_ACCESS_ALL,
|
||||
HRX_BUFFER_USAGE_DEFAULT | HRX_BUFFER_USAGE_MAPPING_PERSISTENT,
|
||||
};
|
||||
void * host = nullptr;
|
||||
if (!GGML_HRX_CHECK(hrx_allocator_allocate_buffer(hrx_device_allocator(ctx->device->device), params, size, &ctx->readback)) ||
|
||||
!GGML_HRX_CHECK(hrx_buffer_map(ctx->readback, HRX_MAP_READ, 0, size, &host))) {
|
||||
if (ctx->readback != nullptr) {
|
||||
hrx_buffer_release(ctx->readback);
|
||||
ctx->readback = nullptr;
|
||||
}
|
||||
ctx->readback_failed = true;
|
||||
return false;
|
||||
}
|
||||
ctx->readback_host = (const uint8_t *) host;
|
||||
ctx->readback_size = size;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void ggml_hrx_stream_finish(ggml_backend_hrx_context * ctx) {
|
||||
if (!GGML_HRX_CHECK(hrx_stream_synchronize(ctx->stream))) {
|
||||
GGML_ABORT("%s: HRX synchronize failed", __func__);
|
||||
}
|
||||
for (const auto & c : ctx->readback_copies) {
|
||||
std::memcpy(c.dst, ctx->readback_host + c.offset, c.size);
|
||||
}
|
||||
ctx->readback_copies.clear();
|
||||
ctx->readback_used = 0;
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_free(ggml_backend_t backend) {
|
||||
auto * ctx = (ggml_backend_hrx_context *) backend->context;
|
||||
GGML_HRX_CHECK(hrx_stream_synchronize(ctx->stream));
|
||||
hrx_stream_release(ctx->stream);
|
||||
ggml_hrx_readback_release(ctx);
|
||||
delete ctx;
|
||||
delete backend;
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_synchronize(ggml_backend_t backend) {
|
||||
ggml_hrx_stream_finish((ggml_backend_hrx_context *) backend->context);
|
||||
}
|
||||
|
||||
// Make the backend stream wait for the uploads recorded in buffer_stream.
|
||||
static bool ggml_hrx_wait_uploads(ggml_backend_hrx_context * ctx) {
|
||||
ggml_hrx_device_context * dev = ctx->device;
|
||||
hrx_timeline_point_t point;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(dev->buffer_mutex);
|
||||
if (dev->upload_pending && !GGML_HRX_CHECK(hrx_stream_flush(dev->buffer_stream))) {
|
||||
return false;
|
||||
}
|
||||
if (!GGML_HRX_CHECK(hrx_stream_get_timeline_position(dev->buffer_stream, &point))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (point.value <= ctx->upload_wait) {
|
||||
return true;
|
||||
}
|
||||
ctx->upload_wait = point.value;
|
||||
return GGML_HRX_CHECK(hrx_stream_wait_on(ctx->stream, point));
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_set_tensor_async(ggml_backend_t backend, ggml_tensor * tensor, const void * data,
|
||||
size_t offset, size_t size) {
|
||||
auto * ctx = (ggml_backend_hrx_context *) backend->context;
|
||||
if (size == 0) {
|
||||
return;
|
||||
}
|
||||
GGML_ASSERT(ggml_hrx_tensor_on_device(tensor));
|
||||
auto * bctx = (ggml_hrx_buffer_context *) ggml_hrx_tensor_buffer(tensor)->context;
|
||||
const size_t dst = ggml_hrx_tensor_offset(tensor) + offset;
|
||||
const bool ok = ggml_hrx_wait_uploads(ctx) &&
|
||||
(size <= GGML_HRX_UPDATE_CHUNK ?
|
||||
GGML_HRX_CHECK(hrx_stream_update_buffer(ctx->stream, data, size, bctx->buffer, dst)) :
|
||||
GGML_HRX_CHECK(hrx_stream_copy_h2d(ctx->stream, data, bctx->buffer, dst, size)));
|
||||
if (!ok) {
|
||||
GGML_ABORT("%s: HRX upload failed", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_get_tensor_async(ggml_backend_t backend, const ggml_tensor * tensor, void * data,
|
||||
size_t offset, size_t size) {
|
||||
auto * ctx = (ggml_backend_hrx_context *) backend->context;
|
||||
if (size == 0) {
|
||||
return;
|
||||
}
|
||||
GGML_ASSERT(ggml_hrx_tensor_on_device(tensor));
|
||||
auto * bctx = (ggml_hrx_buffer_context *) ggml_hrx_tensor_buffer(tensor)->context;
|
||||
const size_t src = ggml_hrx_tensor_offset(tensor) + offset;
|
||||
if (!ggml_hrx_wait_uploads(ctx)) {
|
||||
GGML_ABORT("%s: HRX download failed", __func__);
|
||||
}
|
||||
if (ctx->device->tune->mapped_readback && ggml_hrx_readback_reserve(ctx, size)) {
|
||||
const size_t dst = ctx->readback_used;
|
||||
if (!GGML_HRX_CHECK(hrx_stream_copy_buffer(ctx->stream, bctx->buffer, src, ctx->readback, dst, size))) {
|
||||
GGML_ABORT("%s: HRX download failed", __func__);
|
||||
}
|
||||
ctx->readback_copies.push_back({ data, dst, size });
|
||||
ctx->readback_used = dst + GGML_PAD(size, GGML_HRX_ALIGNMENT);
|
||||
return;
|
||||
}
|
||||
if (!GGML_HRX_CHECK(hrx_stream_copy_d2h(ctx->stream, bctx->buffer, src, data, size))) {
|
||||
GGML_ABORT("%s: HRX download failed", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_event_record(ggml_backend_t backend, ggml_backend_event_t event) {
|
||||
auto * ctx = (ggml_backend_hrx_context *) backend->context;
|
||||
// The host copies of earlier get_tensor_async calls must be done when the event completes.
|
||||
if (!ctx->readback_copies.empty()) {
|
||||
ggml_hrx_stream_finish(ctx);
|
||||
}
|
||||
if (!GGML_HRX_CHECK(hrx_event_record((hrx_event_t) event->context, ctx->stream))) {
|
||||
GGML_ABORT("%s: HRX event record failed", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_event_wait(ggml_backend_t backend, ggml_backend_event_t event) {
|
||||
auto * ctx = (ggml_backend_hrx_context *) backend->context;
|
||||
if (!GGML_HRX_CHECK(hrx_stream_wait_event(ctx->stream, (hrx_event_t) event->context))) {
|
||||
GGML_ABORT("%s: HRX event wait failed", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
// llama.cpp runs FILL in place on recurrent state in device buffers. The CPU cannot run it there.
|
||||
static bool ggml_hrx_supports_fill(const ggml_tensor * op) {
|
||||
return (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) && ggml_is_contiguous(op);
|
||||
}
|
||||
|
||||
static bool ggml_hrx_op_fill(ggml_backend_hrx_context * ctx, const ggml_tensor * node) {
|
||||
const size_t size = ggml_nbytes(node);
|
||||
if (size == 0) {
|
||||
return true;
|
||||
}
|
||||
const float c = ggml_get_op_params_f32(node, 0);
|
||||
uint32_t pattern;
|
||||
size_t pattern_size;
|
||||
if (node->type == GGML_TYPE_F32) {
|
||||
std::memcpy(&pattern, &c, sizeof(c));
|
||||
pattern_size = sizeof(float);
|
||||
} else {
|
||||
pattern = ggml_fp32_to_fp16(c);
|
||||
pattern_size = sizeof(ggml_fp16_t);
|
||||
}
|
||||
auto * bctx = (ggml_hrx_buffer_context *) ggml_hrx_tensor_buffer(node)->context;
|
||||
return GGML_HRX_CHECK(
|
||||
hrx_stream_fill_buffer(ctx->stream, bctx->buffer, ggml_hrx_tensor_offset(node), size, &pattern, pattern_size));
|
||||
}
|
||||
|
||||
static bool ggml_hrx_compute_node(ggml_backend_hrx_context * ctx, const ggml_tensor * node) {
|
||||
switch (node->op) {
|
||||
case GGML_OP_FILL: return ggml_hrx_op_fill(ctx, node);
|
||||
case GGML_OP_CPY:
|
||||
case GGML_OP_DUP:
|
||||
case GGML_OP_CONT: return ggml_hrx_op_cpy(ctx, node);
|
||||
case GGML_OP_SET_ROWS: return ggml_hrx_op_set_rows(ctx, node);
|
||||
case GGML_OP_MUL_MAT: return ggml_hrx_op_mul_mat(ctx, node);
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
static enum ggml_status ggml_backend_hrx_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
|
||||
auto * ctx = (ggml_backend_hrx_context *) backend->context;
|
||||
const ggml_hrx_tune * tune = ctx->device->tune;
|
||||
int n_pending = 0;
|
||||
// The GPU starts work while the host records the next nodes. A short first interval starts a small graph sooner.
|
||||
int n_submit = tune->nodes_first_submit;
|
||||
|
||||
if (!ggml_hrx_wait_uploads(ctx)) {
|
||||
return GGML_STATUS_FAILED;
|
||||
}
|
||||
|
||||
for (int i = 0; i < cgraph->n_nodes; i++) {
|
||||
const ggml_tensor * node = cgraph->nodes[i];
|
||||
if (ggml_is_empty(node) || ggml_op_is_empty(node->op) || (node->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) {
|
||||
continue;
|
||||
}
|
||||
if (!ggml_hrx_compute_node(ctx, node)) {
|
||||
GGML_LOG_ERROR("%s: failed to run node %d (%s, %s)\n", __func__, i, ggml_op_desc(node), node->name);
|
||||
return GGML_STATUS_FAILED;
|
||||
}
|
||||
if (++n_pending >= n_submit) {
|
||||
n_pending = 0;
|
||||
n_submit = std::min(2 * n_submit, tune->nodes_per_submit);
|
||||
if (!GGML_HRX_CHECK(hrx_stream_flush(ctx->stream))) {
|
||||
return GGML_STATUS_FAILED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// synchronize waits for the graph.
|
||||
if (!GGML_HRX_CHECK(hrx_stream_flush(ctx->stream))) {
|
||||
return GGML_STATUS_FAILED;
|
||||
}
|
||||
return GGML_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static const ggml_backend_i ggml_backend_hrx_interface = {
|
||||
/* .get_name = */ ggml_backend_hrx_get_name,
|
||||
/* .free = */ ggml_backend_hrx_free,
|
||||
/* .set_tensor_async = */ ggml_backend_hrx_set_tensor_async,
|
||||
/* .get_tensor_async = */ ggml_backend_hrx_get_tensor_async,
|
||||
/* .set_tensor_2d_async = */ nullptr,
|
||||
/* .get_tensor_2d_async = */ nullptr,
|
||||
/* .cpy_tensor_async = */ nullptr,
|
||||
/* .synchronize = */ ggml_backend_hrx_synchronize,
|
||||
/* .graph_plan_create = */ nullptr,
|
||||
/* .graph_plan_free = */ nullptr,
|
||||
/* .graph_plan_update = */ nullptr,
|
||||
/* .graph_plan_compute = */ nullptr,
|
||||
/* .graph_compute = */ ggml_backend_hrx_graph_compute,
|
||||
/* .event_record = */ ggml_backend_hrx_event_record,
|
||||
/* .event_wait = */ ggml_backend_hrx_event_wait,
|
||||
/* .graph_optimize = */ nullptr,
|
||||
};
|
||||
|
||||
static ggml_hrx_device_context * ggml_hrx_dev_ctx(ggml_backend_dev_t dev) {
|
||||
return (ggml_hrx_device_context *) dev->context;
|
||||
}
|
||||
|
||||
static const char * ggml_backend_hrx_device_get_name(ggml_backend_dev_t dev) {
|
||||
return ggml_hrx_dev_ctx(dev)->name.c_str();
|
||||
}
|
||||
|
||||
static const char * ggml_backend_hrx_device_get_description(ggml_backend_dev_t dev) {
|
||||
return ggml_hrx_dev_ctx(dev)->description.c_str();
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
|
||||
// HRX does not report free memory yet.
|
||||
*free = ggml_hrx_dev_ctx(dev)->memory_total;
|
||||
*total = ggml_hrx_dev_ctx(dev)->memory_total;
|
||||
}
|
||||
|
||||
static enum ggml_backend_dev_type ggml_backend_hrx_device_get_type(ggml_backend_dev_t dev) {
|
||||
GGML_UNUSED(dev);
|
||||
return GGML_BACKEND_DEVICE_TYPE_GPU;
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) {
|
||||
props->name = ggml_backend_hrx_device_get_name(dev);
|
||||
props->description = ggml_backend_hrx_device_get_description(dev);
|
||||
props->type = ggml_backend_hrx_device_get_type(dev);
|
||||
props->device_id = nullptr;
|
||||
ggml_backend_hrx_device_get_memory(dev, &props->memory_free, &props->memory_total);
|
||||
props->caps = {
|
||||
/* .async = */ true,
|
||||
/* .host_buffer = */ false,
|
||||
/* .buffer_from_host_ptr = */ false,
|
||||
/* .events = */ true,
|
||||
/* .mmap_support = */ true,
|
||||
/* .fast_async_host_input = */ false,
|
||||
};
|
||||
}
|
||||
|
||||
static ggml_backend_t ggml_backend_hrx_device_init_backend(ggml_backend_dev_t dev, const char * params) {
|
||||
GGML_UNUSED(params);
|
||||
ggml_hrx_device_context * dev_ctx = ggml_hrx_dev_ctx(dev);
|
||||
|
||||
hrx_stream_t stream = nullptr;
|
||||
if (!GGML_HRX_CHECK(hrx_stream_create(dev_ctx->stream_queue, 0, &stream))) {
|
||||
return nullptr;
|
||||
}
|
||||
auto * ctx = new ggml_backend_hrx_context;
|
||||
ctx->device = dev_ctx;
|
||||
ctx->stream = stream;
|
||||
ctx->name = dev_ctx->name;
|
||||
return new ggml_backend{ ggml_backend_hrx_guid(), ggml_backend_hrx_interface, dev, ctx };
|
||||
}
|
||||
|
||||
static ggml_backend_buffer_type_t ggml_backend_hrx_device_get_buffer_type(ggml_backend_dev_t dev) {
|
||||
return &ggml_hrx_dev_ctx(dev)->buft;
|
||||
}
|
||||
|
||||
static bool ggml_backend_hrx_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
|
||||
const ggml_hrx_device_context * ctx = ggml_hrx_dev_ctx(dev);
|
||||
switch (op->op) {
|
||||
case GGML_OP_NONE:
|
||||
case GGML_OP_VIEW:
|
||||
case GGML_OP_RESHAPE:
|
||||
case GGML_OP_PERMUTE:
|
||||
case GGML_OP_TRANSPOSE: return true;
|
||||
case GGML_OP_FILL: return ggml_hrx_supports_fill(op);
|
||||
case GGML_OP_CPY:
|
||||
case GGML_OP_DUP:
|
||||
case GGML_OP_CONT: return ggml_hrx_supports_cpy(ctx, op);
|
||||
case GGML_OP_SET_ROWS: return ggml_hrx_supports_set_rows(ctx, op);
|
||||
case GGML_OP_MUL_MAT: return ggml_hrx_supports_mul_mat(ctx, op);
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool ggml_backend_hrx_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
|
||||
return buft == &ggml_hrx_dev_ctx(dev)->buft;
|
||||
}
|
||||
|
||||
static ggml_backend_event_t ggml_backend_hrx_device_event_new(ggml_backend_dev_t dev) {
|
||||
hrx_event_t event = nullptr;
|
||||
if (!GGML_HRX_CHECK(hrx_event_create(ggml_hrx_dev_ctx(dev)->device, HRX_EVENT_FLAG_DISABLE_TIMING, &event))) {
|
||||
return nullptr;
|
||||
}
|
||||
return new ggml_backend_event{ dev, event };
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_device_event_free(ggml_backend_dev_t dev, ggml_backend_event_t event) {
|
||||
GGML_UNUSED(dev);
|
||||
hrx_event_release((hrx_event_t) event->context);
|
||||
delete event;
|
||||
}
|
||||
|
||||
static void ggml_backend_hrx_device_event_synchronize(ggml_backend_dev_t dev, ggml_backend_event_t event) {
|
||||
GGML_UNUSED(dev);
|
||||
if (!GGML_HRX_CHECK(hrx_event_synchronize((hrx_event_t) event->context))) {
|
||||
GGML_ABORT("%s: HRX event synchronize failed", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
static const ggml_backend_device_i ggml_backend_hrx_device_interface = {
|
||||
/* .get_name = */ ggml_backend_hrx_device_get_name,
|
||||
/* .get_description = */ ggml_backend_hrx_device_get_description,
|
||||
/* .get_memory = */ ggml_backend_hrx_device_get_memory,
|
||||
/* .get_type = */ ggml_backend_hrx_device_get_type,
|
||||
/* .get_props = */ ggml_backend_hrx_device_get_props,
|
||||
/* .init_backend = */ ggml_backend_hrx_device_init_backend,
|
||||
/* .get_buffer_type = */ ggml_backend_hrx_device_get_buffer_type,
|
||||
/* .get_host_buffer_type = */ nullptr,
|
||||
/* .buffer_from_host_ptr = */ nullptr,
|
||||
/* .supports_op = */ ggml_backend_hrx_device_supports_op,
|
||||
/* .supports_buft = */ ggml_backend_hrx_device_supports_buft,
|
||||
/* .offload_op = */ nullptr,
|
||||
/* .event_new = */ ggml_backend_hrx_device_event_new,
|
||||
/* .event_free = */ ggml_backend_hrx_device_event_free,
|
||||
/* .event_synchronize = */ ggml_backend_hrx_device_event_synchronize,
|
||||
};
|
||||
|
||||
struct ggml_hrx_reg_context {
|
||||
hrx_runtime_t runtime = nullptr;
|
||||
std::vector<hrx_device_group_t> groups;
|
||||
|
||||
std::vector<std::unique_ptr<ggml_hrx_device_context>> contexts;
|
||||
std::vector<ggml_backend_device> devices;
|
||||
|
||||
~ggml_hrx_reg_context() {
|
||||
for (auto & dev : contexts) {
|
||||
dev->kernels.clear();
|
||||
ggml_hrx_loom_jit_free(dev->jit);
|
||||
if (dev->buffer_stream != nullptr) {
|
||||
hrx_stream_release(dev->buffer_stream);
|
||||
}
|
||||
hrx_device_release(dev->device);
|
||||
}
|
||||
for (hrx_device_group_t group : groups) {
|
||||
hrx_device_group_release(group);
|
||||
}
|
||||
if (runtime != nullptr) {
|
||||
hrx_runtime_release(runtime);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static std::optional<std::string> ggml_hrx_device_string(hrx_device_t device, hrx_device_property_t property) {
|
||||
std::vector<char> buffer(64);
|
||||
while (buffer.size() <= 4096) {
|
||||
hrx_status_t status = hrx_device_get_property(device, property, buffer.data(), buffer.size());
|
||||
if (hrx_status_is_ok(status)) {
|
||||
return std::string(buffer.data());
|
||||
}
|
||||
if (hrx_status_code(status) != HRX_STATUS_OUT_OF_RANGE) {
|
||||
GGML_HRX_CHECK(status);
|
||||
return std::nullopt;
|
||||
}
|
||||
hrx_status_ignore(status);
|
||||
buffer.resize(buffer.size() * 2);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
static bool ggml_hrx_find_queue(hrx_device_t device, hrx_queue_role_flags_t roles, uint32_t index, hrx_queue_t * queue) {
|
||||
*queue = nullptr;
|
||||
const size_t n_families = hrx_device_queue_family_count(device);
|
||||
for (size_t i = 0; i < n_families; ++i) {
|
||||
hrx_queue_family_info_t family = {};
|
||||
if (!GGML_HRX_CHECK(hrx_device_query_queue_family(device, (uint32_t) i, &family))) {
|
||||
return false;
|
||||
}
|
||||
if ((family.roles & roles) == roles && family.provisioned_queue_count > index) {
|
||||
*queue = hrx_device_get_queue(device, (uint32_t) i, index);
|
||||
if (*queue != nullptr) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Returns nullptr if the device is not usable. In that case, it releases all that it took.
|
||||
static std::unique_ptr<ggml_hrx_device_context> ggml_hrx_init_device(hrx_device_t device, int index) {
|
||||
const std::optional<std::string> name = ggml_hrx_device_string(device, HRX_DEVICE_PROPERTY_NAME);
|
||||
const std::optional<std::string> arch = ggml_hrx_device_string(device, HRX_DEVICE_PROPERTY_ARCHITECTURE);
|
||||
if (!name || !arch) {
|
||||
return nullptr;
|
||||
}
|
||||
const ggml_hrx_arch * info = ggml_hrx_arch_find(arch->c_str());
|
||||
if (info == nullptr) {
|
||||
GGML_LOG_INFO("ggml_hrx: skipping %s (%s): architecture is not supported\n", name->c_str(), arch->c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto ctx = std::make_unique<ggml_hrx_device_context>();
|
||||
ctx->device = device;
|
||||
ctx->arch = info;
|
||||
ctx->tune = info->tune;
|
||||
ctx->name = GGML_HRX_NAME + std::to_string(index);
|
||||
ctx->index = index;
|
||||
ctx->description = *name + " (" + *arch + ")";
|
||||
|
||||
uint64_t memory = 0;
|
||||
if (!GGML_HRX_CHECK(hrx_device_get_property(device, HRX_DEVICE_PROPERTY_TOTAL_MEMORY, &memory, sizeof(memory))) ||
|
||||
!GGML_HRX_CHECK(hrx_device_get_property(device, HRX_DEVICE_PROPERTY_COMPUTE_UNITS, &ctx->n_cu, sizeof(ctx->n_cu)))) {
|
||||
return nullptr;
|
||||
}
|
||||
ctx->memory_total = (size_t) memory;
|
||||
|
||||
// Synchronous transfers use a second queue if there is one. Then they do not wait for queued graphs.
|
||||
if ((!ggml_hrx_find_queue(device, HRX_QUEUE_ROLE_TRANSFER, 1, &ctx->transfer_queue) &&
|
||||
!ggml_hrx_find_queue(device, HRX_QUEUE_ROLE_TRANSFER, 0, &ctx->transfer_queue)) ||
|
||||
!ggml_hrx_find_queue(device, HRX_QUEUE_ROLE_DISPATCH | HRX_QUEUE_ROLE_TRANSFER, 0, &ctx->stream_queue)) {
|
||||
GGML_LOG_ERROR("ggml_hrx: %s has no usable queues\n", name->c_str());
|
||||
return nullptr;
|
||||
}
|
||||
if (!GGML_HRX_CHECK(hrx_stream_create(ctx->stream_queue, 0, &ctx->buffer_stream))) {
|
||||
return nullptr;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void ggml_hrx_discover(ggml_hrx_reg_context * reg) {
|
||||
hrx_runtime_options_t options = {};
|
||||
const hrx_host_allocator_t allocator = hrx_host_allocator_system();
|
||||
if (!GGML_HRX_CHECK(hrx_runtime_create(&options, allocator, ®->runtime))) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t count = 0;
|
||||
hrx_device_info_t * infos = nullptr;
|
||||
if (!GGML_HRX_CHECK(hrx_runtime_query_available_devices(reg->runtime, "amdgpu", allocator, &count, &infos))) {
|
||||
return;
|
||||
}
|
||||
// One group per device.
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
const hrx_device_create_params_t params = { infos[i].uri, HRX_WAIT_FLAG_NONE };
|
||||
hrx_device_group_t group = nullptr;
|
||||
if (!GGML_HRX_CHECK(hrx_device_group_create(reg->runtime, 1, ¶ms, &group))) {
|
||||
continue;
|
||||
}
|
||||
hrx_device_t device = hrx_device_group_get_device(group, 0);
|
||||
if (device == nullptr) {
|
||||
hrx_device_group_release(group);
|
||||
continue;
|
||||
}
|
||||
hrx_device_retain(device);
|
||||
auto ctx = ggml_hrx_init_device(device, (int) reg->contexts.size());
|
||||
if (!ctx) {
|
||||
hrx_device_release(device);
|
||||
hrx_device_group_release(group);
|
||||
continue;
|
||||
}
|
||||
reg->groups.push_back(group);
|
||||
reg->contexts.push_back(std::move(ctx));
|
||||
}
|
||||
hrx_host_allocator_free(allocator, infos);
|
||||
}
|
||||
|
||||
static const char * ggml_backend_hrx_reg_get_name(ggml_backend_reg_t reg) {
|
||||
GGML_UNUSED(reg);
|
||||
return GGML_HRX_NAME;
|
||||
}
|
||||
|
||||
static size_t ggml_backend_hrx_reg_get_device_count(ggml_backend_reg_t reg) {
|
||||
return ((ggml_hrx_reg_context *) reg->context)->devices.size();
|
||||
}
|
||||
|
||||
static ggml_backend_dev_t ggml_backend_hrx_reg_get_device(ggml_backend_reg_t reg, size_t index) {
|
||||
auto * ctx = (ggml_hrx_reg_context *) reg->context;
|
||||
GGML_ASSERT(index < ctx->devices.size());
|
||||
return &ctx->devices[index];
|
||||
}
|
||||
|
||||
static const ggml_backend_reg_i ggml_backend_hrx_reg_interface = {
|
||||
/* .get_name = */ ggml_backend_hrx_reg_get_name,
|
||||
/* .get_device_count = */ ggml_backend_hrx_reg_get_device_count,
|
||||
/* .get_device = */ ggml_backend_hrx_reg_get_device,
|
||||
/* .get_proc_address = */ nullptr,
|
||||
};
|
||||
|
||||
ggml_backend_reg_t ggml_backend_hrx_reg(void) {
|
||||
static ggml_hrx_reg_context reg_ctx;
|
||||
static ggml_backend_reg reg = { GGML_BACKEND_API_VERSION, ggml_backend_hrx_reg_interface, ®_ctx };
|
||||
|
||||
static std::once_flag once;
|
||||
std::call_once(once, [] {
|
||||
ggml_hrx_discover(®_ctx);
|
||||
reg_ctx.devices.reserve(reg_ctx.contexts.size());
|
||||
for (auto & ctx : reg_ctx.contexts) {
|
||||
reg_ctx.devices.push_back({ ggml_backend_hrx_device_interface, ®, ctx.get() });
|
||||
}
|
||||
for (size_t i = 0; i < reg_ctx.contexts.size(); ++i) {
|
||||
ggml_hrx_device_context * ctx = reg_ctx.contexts[i].get();
|
||||
ctx->buft = { ggml_backend_hrx_buffer_type_interface, ®_ctx.devices[i], ctx };
|
||||
}
|
||||
});
|
||||
return ®
|
||||
}
|
||||
|
||||
ggml_backend_t ggml_backend_hrx_init(size_t device) {
|
||||
ggml_backend_reg_t reg = ggml_backend_hrx_reg();
|
||||
if (device >= ggml_backend_reg_dev_count(reg)) {
|
||||
GGML_LOG_ERROR("%s: invalid device %zu\n", __func__, device);
|
||||
return nullptr;
|
||||
}
|
||||
return ggml_backend_dev_init(ggml_backend_reg_dev_get(reg, device), nullptr);
|
||||
}
|
||||
|
||||
bool ggml_backend_is_hrx(ggml_backend_t backend) {
|
||||
return backend != nullptr && ggml_guid_matches(backend->guid, ggml_backend_hrx_guid());
|
||||
}
|
||||
|
||||
int ggml_backend_hrx_get_device_count(void) {
|
||||
return (int) ggml_backend_reg_dev_count(ggml_backend_hrx_reg());
|
||||
}
|
||||
|
||||
ggml_backend_buffer_type_t ggml_backend_hrx_buffer_type(size_t device) {
|
||||
ggml_backend_reg_t reg = ggml_backend_hrx_reg();
|
||||
if (device >= ggml_backend_reg_dev_count(reg)) {
|
||||
return nullptr;
|
||||
}
|
||||
return ggml_backend_dev_buffer_type(ggml_backend_reg_dev_get(reg, device));
|
||||
}
|
||||
|
||||
GGML_BACKEND_DL_IMPL(ggml_backend_hrx_reg)
|
||||
@@ -0,0 +1,134 @@
|
||||
config.decl @ggml.cpy.dst_type : %value: index where [range(%value, 0, 3)]
|
||||
|
||||
config.decl @ggml.elementwise.wg_size : %value: index where [range(%value, 64, 1024), multiple_of(%value, 64)]
|
||||
|
||||
target.decl @ggml_wave64
|
||||
|
||||
kernel.def target(@ggml_wave64) export("ggml_cpy_f32") @ggml_cpy_f32(%n: index) {
|
||||
%one = index.constant 1 : index
|
||||
%wg_size = config.get @ggml.elementwise.wg_size : index
|
||||
%round = index.sub %wg_size, %one : index
|
||||
%n_round = index.add %n, %round : index
|
||||
%groups = index.div %n_round, %wg_size : index
|
||||
kernel.launch.config workgroups(%groups, %one, %one) workgroup_size(%wg_size, %one, %one) : index
|
||||
} launch(%n_in: index, %s_ne0_in: index, %s_ne1_in: index, %s_ne2_in: index, %s0_in: index, %s1_in: index, %s2_in: index, %s3_in: index, %s_span_in: index, %t_ne0_in: index, %t_ne1_in: index, %t_ne2_in: index, %t0_in: index, %t1_in: index, %t2_in: index, %t3_in: index, %t_span_in: index, %src: buffer, %dst: buffer) {
|
||||
// supports_cpy checks these ranges.
|
||||
%n = index.assume %n_in [range(%n_in, 1, 2147483647)] : index
|
||||
%s_ne0 = index.assume %s_ne0_in [range(%s_ne0_in, 1, 2147483647)] : index
|
||||
%s_ne1 = index.assume %s_ne1_in [range(%s_ne1_in, 1, 2147483647)] : index
|
||||
%s_ne2 = index.assume %s_ne2_in [range(%s_ne2_in, 1, 2147483647)] : index
|
||||
%s0 = index.assume %s0_in [range(%s0_in, 0, 2147483647)] : index
|
||||
%s1 = index.assume %s1_in [range(%s1_in, 0, 2147483647)] : index
|
||||
%s2 = index.assume %s2_in [range(%s2_in, 0, 2147483647)] : index
|
||||
%s3 = index.assume %s3_in [range(%s3_in, 0, 2147483647)] : index
|
||||
%s_span = index.assume %s_span_in [range(%s_span_in, 1, 2147483647)] : index
|
||||
%t_ne0 = index.assume %t_ne0_in [range(%t_ne0_in, 1, 2147483647)] : index
|
||||
%t_ne1 = index.assume %t_ne1_in [range(%t_ne1_in, 1, 2147483647)] : index
|
||||
%t_ne2 = index.assume %t_ne2_in [range(%t_ne2_in, 1, 2147483647)] : index
|
||||
%t0 = index.assume %t0_in [range(%t0_in, 0, 2147483647)] : index
|
||||
%t1 = index.assume %t1_in [range(%t1_in, 0, 2147483647)] : index
|
||||
%t2 = index.assume %t2_in [range(%t2_in, 0, 2147483647)] : index
|
||||
%t3 = index.assume %t3_in [range(%t3_in, 0, 2147483647)] : index
|
||||
%t_span = index.assume %t_span_in [range(%t_span_in, 1, 2147483647)] : index
|
||||
%dst_type = config.get @ggml.cpy.dst_type : index
|
||||
%wg_size = config.get @ggml.elementwise.wg_size : index
|
||||
%group = kernel.workgroup.id<x> : index
|
||||
%item = kernel.workitem.id<x> : index
|
||||
%base = index.mul %group, %wg_size : index
|
||||
%i = index.add %base, %item : index
|
||||
%in_bounds = index.cmp ult, %i, %n : index
|
||||
%zero = index.constant 0 : offset
|
||||
%src_view = buffer.view %src[%zero] : buffer -> view<[%s_span]xf32>
|
||||
scf.if %in_bounds {
|
||||
%a0 = index.rem %i, %s_ne0 : index
|
||||
%aq0 = index.div %i, %s_ne0 : index
|
||||
%a1 = index.rem %aq0, %s_ne1 : index
|
||||
%aq1 = index.div %aq0, %s_ne1 : index
|
||||
%a2 = index.rem %aq1, %s_ne2 : index
|
||||
%a3 = index.div %aq1, %s_ne2 : index
|
||||
%so0 = index.mul %a0, %s0 : index
|
||||
%so1 = index.mul %a1, %s1 : index
|
||||
%so2 = index.mul %a2, %s2 : index
|
||||
%so3 = index.mul %a3, %s3 : index
|
||||
%so01 = index.add %so0, %so1 : index
|
||||
%so23 = index.add %so2, %so3 : index
|
||||
%s_idx = index.add %so01, %so23 : index
|
||||
|
||||
%b0 = index.rem %i, %t_ne0 : index
|
||||
%bq0 = index.div %i, %t_ne0 : index
|
||||
%b1 = index.rem %bq0, %t_ne1 : index
|
||||
%bq1 = index.div %bq0, %t_ne1 : index
|
||||
%b2 = index.rem %bq1, %t_ne2 : index
|
||||
%b3 = index.div %bq1, %t_ne2 : index
|
||||
%to0 = index.mul %b0, %t0 : index
|
||||
%to1 = index.mul %b1, %t1 : index
|
||||
%to2 = index.mul %b2, %t2 : index
|
||||
%to3 = index.mul %b3, %t3 : index
|
||||
%to01 = index.add %to0, %to1 : index
|
||||
%to23 = index.add %to2, %to3 : index
|
||||
%t_idx = index.add %to01, %to23 : index
|
||||
|
||||
// The prover reads only branch conditions. Thus cap the offsets and check one bound in each scf.if.
|
||||
%s_off = index.assume %s_idx [range(%s_idx, 0, 2147483646)] : index
|
||||
%t_off = index.assume %t_idx [range(%t_idx, 0, 2147483646)] : index
|
||||
%s_ok = index.cmp ult, %s_off, %s_span : index
|
||||
scf.if %s_ok {
|
||||
%t_ok = index.cmp ult, %t_off, %t_span : index
|
||||
scf.if %t_ok {
|
||||
%x = view.load %src_view[%s_off] : view<[%s_span]xf32> -> f32
|
||||
%type_f16 = index.constant 1 : index
|
||||
%type_i32 = index.constant 2 : index
|
||||
%is_f16 = index.cmp eq, %dst_type, %type_f16 : index
|
||||
%is_i32 = index.cmp eq, %dst_type, %type_i32 : index
|
||||
scf.if %is_f16 {
|
||||
%h = scalar.fptrunc %x : f32 to f16
|
||||
%dst_f16 = buffer.view %dst[%zero] : buffer -> view<[%t_span]xf16>
|
||||
view.store %h, %dst_f16[%t_off] : f16, view<[%t_span]xf16>
|
||||
} else {
|
||||
scf.if %is_i32 {
|
||||
%v = scalar.fptosi %x : f32 to i32
|
||||
%dst_i32 = buffer.view %dst[%zero] : buffer -> view<[%t_span]xi32>
|
||||
view.store %v, %dst_i32[%t_off] : i32, view<[%t_span]xi32>
|
||||
} else {
|
||||
%dst_f32 = buffer.view %dst[%zero] : buffer -> view<[%t_span]xf32>
|
||||
view.store %x, %dst_f32[%t_off] : f32, view<[%t_span]xf32>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
kernel.return
|
||||
}
|
||||
|
||||
kernel.def target(@ggml_wave64) export("ggml_cpy_f32_stream") @ggml_cpy_f32_stream(%groups: index) {
|
||||
%one = index.constant 1 : index
|
||||
%wg_size = config.get @ggml.elementwise.wg_size : index
|
||||
kernel.launch.config workgroups(%groups, %one, %one) workgroup_size(%wg_size, %one, %one) : index
|
||||
} launch(%n_in: index, %groups_in: index, %src: buffer, %dst: buffer) {
|
||||
// The caller checks these ranges. n must be a multiple of 4. src and dst are contiguous f32.
|
||||
%n = index.assume %n_in [range(%n_in, 4, 2147483644)] : index
|
||||
%groups = index.assume %groups_in [range(%groups_in, 1, 65535)] : index
|
||||
%c3 = index.constant 3 : index
|
||||
%c4 = index.constant 4 : index
|
||||
%wg_size = config.get @ggml.elementwise.wg_size : index
|
||||
%group = kernel.workgroup.id<x> : index
|
||||
%item = kernel.workitem.id<x> : index
|
||||
%base = index.mul %group, %wg_size : index
|
||||
%lane = index.add %base, %item : index
|
||||
%first = index.mul %lane, %c4 : index
|
||||
%lanes = index.mul %groups, %wg_size : index
|
||||
%stride = index.mul %lanes, %c4 : index
|
||||
%zero = index.constant 0 : offset
|
||||
%src_view = buffer.view %src[%zero] : buffer -> view<[%n]xf32>
|
||||
%dst_view = buffer.view %dst[%zero] : buffer -> view<[%n]xf32>
|
||||
scf.for %j_raw = [%first to %n step %stride] {
|
||||
%j = index.assume %j_raw [range(%j_raw, 0, 2147483640)] : index
|
||||
%j_last = index.add %j, %c3 : index
|
||||
%ok = index.cmp ult, %j_last, %n : index
|
||||
scf.if %ok {
|
||||
%x = vector.load %src_view[%j] : view<[%n]xf32> -> vector<4xf32>
|
||||
vector.store %x, %dst_view[%j] : vector<4xf32>, view<[%n]xf32>
|
||||
}
|
||||
}
|
||||
kernel.return
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
// Weight type 2 is Q4_0. Weight units are 16-bit halves.
|
||||
|
||||
template.decl @ggml.weight.block(%wtype: index) -> (index, index)
|
||||
|
||||
template.decl @ggml.weight.values4(%wtype: index, %weight: buffer, %w_span: index, %block_base: index, %j: index) -> (vector<4xf32>)
|
||||
|
||||
// k is a multiple of 4 and below K.
|
||||
func.def inline @ggml_hrx_mm_quant4(%wtype: index, %weight: buffer, %w_span: index, %row_base: index, %k: index) -> (vector<4xf32>) {
|
||||
%block_values, %block_units = template.apply<@ggml.weight.block>(%wtype) : (index) -> (index, index)
|
||||
%block = index.div %k, %block_values : index
|
||||
%j = index.rem %k, %block_values : index
|
||||
%block_offset = index.mul %block, %block_units : index
|
||||
%block_base = index.add %row_base, %block_offset : index
|
||||
%values = template.apply<@ggml.weight.values4>(%wtype, %weight, %w_span, %block_base, %j) : (index, buffer, index, index, index) -> (vector<4xf32>)
|
||||
func.return %values : vector<4xf32>
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mm_load_input(%input: buffer, %x_span: index, %index: index) -> (f32) {
|
||||
%c0_offset = index.constant 0 : offset
|
||||
%zero = scalar.constant 0.0 : f32
|
||||
%input_view = buffer.view %input[%c0_offset] : buffer -> view<[%x_span]xf32>
|
||||
%off = index.assume %index [range(%index, 0, 2147483646)] : index
|
||||
%ok = index.cmp ult, %off, %x_span : index
|
||||
%value = scf.if %ok -> (f32) {
|
||||
%loaded = view.load %input_view[%off] : view<[%x_span]xf32> -> f32
|
||||
scf.yield %loaded : f32
|
||||
} else {
|
||||
scf.yield %zero : f32
|
||||
}
|
||||
func.return %value : f32
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mm_input_at(%input: buffer, %x_span: index, %x_col_base: index, %x_s0: index, %k: index, %k_size: index) -> (f32) {
|
||||
%zero = scalar.constant 0.0 : f32
|
||||
%k_in = index.cmp ult, %k, %k_size : index
|
||||
%value = scf.if %k_in -> (f32) {
|
||||
%offset = index.mul %k, %x_s0 : index
|
||||
%index = index.add %x_col_base, %offset : index
|
||||
%loaded = func.call @ggml_hrx_mm_load_input(%input, %x_span, %index) : (buffer, index, index) -> (f32)
|
||||
scf.yield %loaded : f32
|
||||
} else {
|
||||
scf.yield %zero : f32
|
||||
}
|
||||
func.return %value : f32
|
||||
}
|
||||
|
||||
// k is a multiple of 4. K is a multiple of the block size. Thus k < K covers all four values.
|
||||
func.def inline @ggml_hrx_mm_weight4(%wtype: index, %weight: buffer, %w_span: index, %row_base: index, %row_valid: i1, %k: index, %k_size: index) -> (vector<4xf32>) {
|
||||
%zero4 = vector.splatf 0.0 : vector<4xf32>
|
||||
%values = scf.if %row_valid -> (vector<4xf32>) {
|
||||
%k_in = index.cmp ult, %k, %k_size : index
|
||||
%quant_values = scf.if %k_in -> (vector<4xf32>) {
|
||||
%decoded = func.call @ggml_hrx_mm_quant4(%wtype, %weight, %w_span, %row_base, %k) : (index, buffer, index, index, index) -> (vector<4xf32>)
|
||||
scf.yield %decoded : vector<4xf32>
|
||||
} else {
|
||||
scf.yield %zero4 : vector<4xf32>
|
||||
}
|
||||
scf.yield %quant_values : vector<4xf32>
|
||||
} else {
|
||||
scf.yield %zero4 : vector<4xf32>
|
||||
}
|
||||
func.return %values : vector<4xf32>
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mm_stage_input(%input: buffer, %x_span: index, %x_base: index, %x_valid: i1, %x_s0: index, %k: index, %k_size: index) -> (f32) {
|
||||
%zero = scalar.constant 0.0 : f32
|
||||
%value = scf.if %x_valid -> (f32) {
|
||||
%loaded = func.call @ggml_hrx_mm_input_at(%input, %x_span, %x_base, %x_s0, %k, %k_size) : (buffer, index, index, index, index, index) -> (f32)
|
||||
scf.yield %loaded : f32
|
||||
} else {
|
||||
scf.yield %zero : f32
|
||||
}
|
||||
func.return %value : f32
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mm_tile(%wtype: index, %k_size: index, %w_base0: index, %w_valid0: i1, %w_base1: index, %w_valid1: i1, %x_base0: index, %x_valid0: i1, %x_base1: index, %x_valid1: i1, %x_base2: index, %x_valid2: i1, %x_base3: index, %x_valid3: i1, %x_s0: index, %w_span: index, %x_span: index, %weight: buffer, %input: buffer, %w_stage: buffer, %x_stage: buffer) -> (vector<4xf32>, vector<4xf32>) {
|
||||
%t0 = kernel.workitem.id<x> : index
|
||||
%t = index.assume %t0 [range(%t0, 0, 255)] : index
|
||||
%c0 = index.constant 0 : index
|
||||
%c1 = index.constant 1 : index
|
||||
%c2 = index.constant 2 : index
|
||||
%c3 = index.constant 3 : index
|
||||
%c4 = index.constant 4 : index
|
||||
%c8 = index.constant 8 : index
|
||||
%c16 = index.constant 16 : index
|
||||
%c24 = index.constant 24 : index
|
||||
%c32 = index.constant 32 : index
|
||||
%c0_offset = index.constant 0 : offset
|
||||
%zero4 = vector.splatf 0.0 : vector<4xf32>
|
||||
%w_view = buffer.view %w_stage[%c0_offset] : buffer -> view<32x64xf32>
|
||||
// Odd pitch prevents LDS bank conflicts.
|
||||
%x_view = buffer.view %x_stage[%c0_offset] : buffer -> view<32x33xf32>
|
||||
%w_packet = index.rem %t, %c8 : index
|
||||
%w_k0 = index.mul %w_packet, %c4 : index
|
||||
%w_k1 = index.add %w_k0, %c1 : index
|
||||
%w_k2 = index.add %w_k0, %c2 : index
|
||||
%w_k3 = index.add %w_k0, %c3 : index
|
||||
%w_row0 = index.div %t, %c8 : index
|
||||
%w_row1 = index.add %w_row0, %c32 : index
|
||||
%x_k = index.rem %t, %c32 : index
|
||||
%x_col0 = index.div %t, %c32 : index
|
||||
%x_col1 = index.add %x_col0, %c8 : index
|
||||
%x_col2 = index.add %x_col0, %c16 : index
|
||||
%x_col3 = index.add %x_col0, %c24 : index
|
||||
%out_row_group = index.rem %t, %c16 : index
|
||||
%out_row = index.mul %out_row_group, %c4 : index
|
||||
%out_col0 = index.div %t, %c16 : index
|
||||
%out_col1 = index.add %out_col0, %c16 : index
|
||||
%acc0, %acc1 = scf.for %k0 = [%c0 to %k_size step %c32](%tile_acc0 = %zero4 : vector<4xf32>, %tile_acc1 = %zero4 : vector<4xf32>) -> (vector<4xf32>, vector<4xf32>) {
|
||||
%w_k = index.add %k0, %w_k0 : index
|
||||
%w0 = func.call @ggml_hrx_mm_weight4(%wtype, %weight, %w_span, %w_base0, %w_valid0, %w_k, %k_size) : (index, buffer, index, index, i1, index, index) -> (vector<4xf32>)
|
||||
%w1 = func.call @ggml_hrx_mm_weight4(%wtype, %weight, %w_span, %w_base1, %w_valid1, %w_k, %k_size) : (index, buffer, index, index, i1, index, index) -> (vector<4xf32>)
|
||||
%w0_0 = vector.extract %w0[0] : vector<4xf32> -> f32
|
||||
%w0_1 = vector.extract %w0[1] : vector<4xf32> -> f32
|
||||
%w0_2 = vector.extract %w0[2] : vector<4xf32> -> f32
|
||||
%w0_3 = vector.extract %w0[3] : vector<4xf32> -> f32
|
||||
%w1_0 = vector.extract %w1[0] : vector<4xf32> -> f32
|
||||
%w1_1 = vector.extract %w1[1] : vector<4xf32> -> f32
|
||||
%w1_2 = vector.extract %w1[2] : vector<4xf32> -> f32
|
||||
%w1_3 = vector.extract %w1[3] : vector<4xf32> -> f32
|
||||
view.store %w0_0, %w_view[%w_k0, %w_row0] : f32, view<32x64xf32>
|
||||
view.store %w0_1, %w_view[%w_k1, %w_row0] : f32, view<32x64xf32>
|
||||
view.store %w0_2, %w_view[%w_k2, %w_row0] : f32, view<32x64xf32>
|
||||
view.store %w0_3, %w_view[%w_k3, %w_row0] : f32, view<32x64xf32>
|
||||
view.store %w1_0, %w_view[%w_k0, %w_row1] : f32, view<32x64xf32>
|
||||
view.store %w1_1, %w_view[%w_k1, %w_row1] : f32, view<32x64xf32>
|
||||
view.store %w1_2, %w_view[%w_k2, %w_row1] : f32, view<32x64xf32>
|
||||
view.store %w1_3, %w_view[%w_k3, %w_row1] : f32, view<32x64xf32>
|
||||
%x_k_abs = index.add %k0, %x_k : index
|
||||
%x0 = func.call @ggml_hrx_mm_stage_input(%input, %x_span, %x_base0, %x_valid0, %x_s0, %x_k_abs, %k_size) : (buffer, index, index, i1, index, index, index) -> (f32)
|
||||
%x1 = func.call @ggml_hrx_mm_stage_input(%input, %x_span, %x_base1, %x_valid1, %x_s0, %x_k_abs, %k_size) : (buffer, index, index, i1, index, index, index) -> (f32)
|
||||
%x2 = func.call @ggml_hrx_mm_stage_input(%input, %x_span, %x_base2, %x_valid2, %x_s0, %x_k_abs, %k_size) : (buffer, index, index, i1, index, index, index) -> (f32)
|
||||
%x3 = func.call @ggml_hrx_mm_stage_input(%input, %x_span, %x_base3, %x_valid3, %x_s0, %x_k_abs, %k_size) : (buffer, index, index, i1, index, index, index) -> (f32)
|
||||
view.store %x0, %x_view[%x_col0, %x_k] : f32, view<32x33xf32>
|
||||
view.store %x1, %x_view[%x_col1, %x_k] : f32, view<32x33xf32>
|
||||
view.store %x2, %x_view[%x_col2, %x_k] : f32, view<32x33xf32>
|
||||
view.store %x3, %x_view[%x_col3, %x_k] : f32, view<32x33xf32>
|
||||
kernel.barrier<workgroup> scope(workgroup) ordering(acq_rel)
|
||||
%next0, %next1 = scf.for %kk = [%c0 to %c32 step %c1](%k_acc0 = %tile_acc0 : vector<4xf32>, %k_acc1 = %tile_acc1 : vector<4xf32>) -> (vector<4xf32>, vector<4xf32>) {
|
||||
%w = vector.load %w_view[%kk, %out_row] : view<32x64xf32> -> vector<4xf32>
|
||||
%a0 = view.load %x_view[%out_col0, %kk] : view<32x33xf32> -> f32
|
||||
%a1 = view.load %x_view[%out_col1, %kk] : view<32x33xf32> -> f32
|
||||
%a0_vector = vector.splat %a0 : vector<4xf32>
|
||||
%a1_vector = vector.splat %a1 : vector<4xf32>
|
||||
%k_next0 = vector.fmaf %w, %a0_vector, %k_acc0 : vector<4xf32>
|
||||
%k_next1 = vector.fmaf %w, %a1_vector, %k_acc1 : vector<4xf32>
|
||||
scf.yield %k_next0, %k_next1 : vector<4xf32>, vector<4xf32>
|
||||
}
|
||||
// The next K step overwrites the stages.
|
||||
kernel.barrier<workgroup> scope(workgroup) ordering(acq_rel)
|
||||
scf.yield %next0, %next1 : vector<4xf32>, vector<4xf32>
|
||||
}
|
||||
func.return %acc0, %acc1 : vector<4xf32>, vector<4xf32>
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mm_store(%dst: buffer, %d_span: index, %index: index, %value: f32) {
|
||||
%c0_offset = index.constant 0 : offset
|
||||
%dst_view = buffer.view %dst[%c0_offset] : buffer -> view<[%d_span]xf32>
|
||||
%off = index.assume %index [range(%index, 0, 2147483646)] : index
|
||||
%ok = index.cmp ult, %off, %d_span : index
|
||||
scf.if %ok {
|
||||
view.store %value, %dst_view[%off] : f32, view<[%d_span]xf32>
|
||||
}
|
||||
func.return
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mm_store_row(%dst: buffer, %d_span: index, %d_col_base: index, %row: index, %n_rows: index, %value: f32) {
|
||||
%row_in = index.cmp ult, %row, %n_rows : index
|
||||
scf.if %row_in {
|
||||
%index = index.add %d_col_base, %row : index
|
||||
func.call @ggml_hrx_mm_store(%dst, %d_span, %index, %value) : (buffer, index, index, f32)
|
||||
}
|
||||
func.return
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mm_store4(%dst: buffer, %d_span: index, %d_col_base: index, %row0: index, %n_rows: index, %col_valid: i1, %values: vector<4xf32>) {
|
||||
%c1 = index.constant 1 : index
|
||||
%c2 = index.constant 2 : index
|
||||
%c3 = index.constant 3 : index
|
||||
scf.if %col_valid {
|
||||
%row1 = index.add %row0, %c1 : index
|
||||
%row2 = index.add %row0, %c2 : index
|
||||
%row3 = index.add %row0, %c3 : index
|
||||
%value0 = vector.extract %values[0] : vector<4xf32> -> f32
|
||||
%value1 = vector.extract %values[1] : vector<4xf32> -> f32
|
||||
%value2 = vector.extract %values[2] : vector<4xf32> -> f32
|
||||
%value3 = vector.extract %values[3] : vector<4xf32> -> f32
|
||||
func.call @ggml_hrx_mm_store_row(%dst, %d_span, %d_col_base, %row0, %n_rows, %value0) : (buffer, index, index, index, index, f32)
|
||||
func.call @ggml_hrx_mm_store_row(%dst, %d_span, %d_col_base, %row1, %n_rows, %value1) : (buffer, index, index, index, index, f32)
|
||||
func.call @ggml_hrx_mm_store_row(%dst, %d_span, %d_col_base, %row2, %n_rows, %value2) : (buffer, index, index, index, index, f32)
|
||||
func.call @ggml_hrx_mm_store_row(%dst, %d_span, %d_col_base, %row3, %n_rows, %value3) : (buffer, index, index, index, index, f32)
|
||||
}
|
||||
func.return
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// n_tile is a config constant. Thus use folds at compile time.
|
||||
func.def inline @ggml_hrx_mv_col_base(%i: index, %n_tile: index, %n_cols: index, %x_batch: index, %x_s1: index, %k_size: index, %x_span: index, %ok_in: i1) -> (index, i1, i1, i1) {
|
||||
%false = scalar.constant false : i1
|
||||
%use = index.cmp ult, %i, %n_tile : index
|
||||
%in_cols = index.cmp ult, %i, %n_cols : index
|
||||
%store = scf.select %use, %in_cols, %false : i1
|
||||
%x_offset = index.mul %i, %x_s1 : index
|
||||
%x_base = index.add %x_batch, %x_offset : index
|
||||
%x_end = index.add %x_base, %k_size : index
|
||||
%fits = index.cmp ule, %x_end, %x_span : index
|
||||
%col_ok = scf.select %use, %fits, %ok_in : i1
|
||||
%ok = scf.select %ok_in, %col_ok, %false : i1
|
||||
func.return %x_base, %use, %store, %ok : index, i1, i1, i1
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mv_xload(%use: i1, %input: buffer, %x_base: index, %sb: index, %o0: index, %o1: index, %o2: index, %o3: index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) {
|
||||
%c256 = index.constant 256 : index
|
||||
%b4 = index.constant 4 : offset
|
||||
%zero4 = vector.splatf 0.0 : vector<4xf32>
|
||||
%x0, %x1, %x2, %x3 = scf.if %use -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) {
|
||||
%sb_values = index.mul %sb, %c256 : index
|
||||
%x_index = index.add %x_base, %sb_values : index
|
||||
%x_byte = index.scale %x_index, %b4 : index, offset -> offset
|
||||
%x_view = buffer.view %input[%x_byte] : buffer -> view<256xf32>
|
||||
%y0 = vector.load %x_view[%o0] : view<256xf32> -> vector<4xf32>
|
||||
%y1 = vector.load %x_view[%o1] : view<256xf32> -> vector<4xf32>
|
||||
%y2 = vector.load %x_view[%o2] : view<256xf32> -> vector<4xf32>
|
||||
%y3 = vector.load %x_view[%o3] : view<256xf32> -> vector<4xf32>
|
||||
scf.yield %y0, %y1, %y2, %y3 : vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>
|
||||
} else {
|
||||
scf.yield %zero4, %zero4, %zero4, %zero4 : vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>
|
||||
}
|
||||
func.return %x0, %x1, %x2, %x3 : vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mv_dot16(%use: i1, %w0: vector<4xf32>, %w1: vector<4xf32>, %w2: vector<4xf32>, %w3: vector<4xf32>, %x0: vector<4xf32>, %x1: vector<4xf32>, %x2: vector<4xf32>, %x3: vector<4xf32>) -> (f32) {
|
||||
%zero = scalar.constant 0.0 : f32
|
||||
%result = scf.if %use -> (f32) {
|
||||
%p0 = vector.mulf %w0, %x0 : vector<4xf32>
|
||||
%p1 = vector.fmaf %w1, %x1, %p0 : vector<4xf32>
|
||||
%p2 = vector.fmaf %w2, %x2, %p1 : vector<4xf32>
|
||||
%p3 = vector.fmaf %w3, %x3, %p2 : vector<4xf32>
|
||||
%lane_sum = vector.reduce<addf> %p3, %zero : vector<4xf32>, f32
|
||||
%sum = kernel.subgroup.reduce<addf> %lane_sum {cluster_size = 16} : f32
|
||||
scf.yield %sum : f32
|
||||
} else {
|
||||
scf.yield %zero : f32
|
||||
}
|
||||
func.return %result : f32
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
// Weights and activations are rounded to f16. The sums are in f32.
|
||||
// Each stage row has 8 pad halves. The pad prevents LDS bank conflicts.
|
||||
|
||||
template.decl @ggml.weight.wmma16(%wtype: index, %weight: buffer, %w_span: index, %row_base: index, %row_valid: i1, %k: index, %k_size: index, %part: index) -> (vector<8xf16>, vector<8xf16>)
|
||||
|
||||
func.decl @ggml_hrx_mm_store_row(%dst: buffer, %d_span: index, %d_col_base: index, %row: index, %n_rows: index, %value: f32)
|
||||
|
||||
func.def inline @ggml_hrx_mm_wmma_store1(%dst: buffer, %d_span: index, %d_base: index, %d_s1: index, %col: index, %n_cols: index, %row: index, %n_rows: index, %value: f32) {
|
||||
%col_valid = index.cmp ult, %col, %n_cols : index
|
||||
scf.if %col_valid {
|
||||
%d_col_offset = index.mul %col, %d_s1 : index
|
||||
%d_col_base = index.add %d_base, %d_col_offset : index
|
||||
func.call @ggml_hrx_mm_store_row(%dst, %d_span, %d_col_base, %row, %n_rows, %value) : (buffer, index, index, index, index, f32)
|
||||
}
|
||||
func.return
|
||||
}
|
||||
|
||||
// The fragment layout is the gfx11 wave32 result layout.
|
||||
// Values out of range are not stored.
|
||||
func.def inline @ggml_hrx_mm_wmma_store(%dst: buffer, %d_span: index, %d_base: index, %d_s1: index, %col0: index, %n_cols: index, %row0: index, %n_rows: index, %acc: vector<8xf32>) {
|
||||
%lane0 = kernel.subgroup.lane.id : index
|
||||
%lane = index.assume %lane0 [range(%lane0, 0, 31)] : index
|
||||
%c2 = index.constant 2 : index
|
||||
%c4 = index.constant 4 : index
|
||||
%c6 = index.constant 6 : index
|
||||
%c8 = index.constant 8 : index
|
||||
%c10 = index.constant 10 : index
|
||||
%c12 = index.constant 12 : index
|
||||
%c14 = index.constant 14 : index
|
||||
%c16 = index.constant 16 : index
|
||||
%lane_col = index.div %lane, %c16 : index
|
||||
%lane_row = index.rem %lane, %c16 : index
|
||||
%row = index.add %row0, %lane_row : index
|
||||
%col_e0 = index.add %col0, %lane_col : index
|
||||
%col_e1 = index.add %col_e0, %c2 : index
|
||||
%col_e2 = index.add %col_e0, %c4 : index
|
||||
%col_e3 = index.add %col_e0, %c6 : index
|
||||
%col_e4 = index.add %col_e0, %c8 : index
|
||||
%col_e5 = index.add %col_e0, %c10 : index
|
||||
%col_e6 = index.add %col_e0, %c12 : index
|
||||
%col_e7 = index.add %col_e0, %c14 : index
|
||||
%v0 = vector.extract %acc[0] : vector<8xf32> -> f32
|
||||
%v1 = vector.extract %acc[1] : vector<8xf32> -> f32
|
||||
%v2 = vector.extract %acc[2] : vector<8xf32> -> f32
|
||||
%v3 = vector.extract %acc[3] : vector<8xf32> -> f32
|
||||
%v4 = vector.extract %acc[4] : vector<8xf32> -> f32
|
||||
%v5 = vector.extract %acc[5] : vector<8xf32> -> f32
|
||||
%v6 = vector.extract %acc[6] : vector<8xf32> -> f32
|
||||
%v7 = vector.extract %acc[7] : vector<8xf32> -> f32
|
||||
func.call @ggml_hrx_mm_wmma_store1(%dst, %d_span, %d_base, %d_s1, %col_e0, %n_cols, %row, %n_rows, %v0) : (buffer, index, index, index, index, index, index, index, f32)
|
||||
func.call @ggml_hrx_mm_wmma_store1(%dst, %d_span, %d_base, %d_s1, %col_e1, %n_cols, %row, %n_rows, %v1) : (buffer, index, index, index, index, index, index, index, f32)
|
||||
func.call @ggml_hrx_mm_wmma_store1(%dst, %d_span, %d_base, %d_s1, %col_e2, %n_cols, %row, %n_rows, %v2) : (buffer, index, index, index, index, index, index, index, f32)
|
||||
func.call @ggml_hrx_mm_wmma_store1(%dst, %d_span, %d_base, %d_s1, %col_e3, %n_cols, %row, %n_rows, %v3) : (buffer, index, index, index, index, index, index, index, f32)
|
||||
func.call @ggml_hrx_mm_wmma_store1(%dst, %d_span, %d_base, %d_s1, %col_e4, %n_cols, %row, %n_rows, %v4) : (buffer, index, index, index, index, index, index, index, f32)
|
||||
func.call @ggml_hrx_mm_wmma_store1(%dst, %d_span, %d_base, %d_s1, %col_e5, %n_cols, %row, %n_rows, %v5) : (buffer, index, index, index, index, index, index, index, f32)
|
||||
func.call @ggml_hrx_mm_wmma_store1(%dst, %d_span, %d_base, %d_s1, %col_e6, %n_cols, %row, %n_rows, %v6) : (buffer, index, index, index, index, index, index, index, f32)
|
||||
func.call @ggml_hrx_mm_wmma_store1(%dst, %d_span, %d_base, %d_s1, %col_e7, %n_cols, %row, %n_rows, %v7) : (buffer, index, index, index, index, index, index, index, f32)
|
||||
func.return
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mm_wmma_stage_weights64(%wtype: index, %weight: buffer, %w_span: index, %row_base: index, %row_valid: i1, %k: index, %k_size: index, %w_stage: buffer) {
|
||||
%t0 = kernel.workitem.id<x> : index
|
||||
%t = index.assume %t0 [range(%t0, 0, 255)] : index
|
||||
%c1 = index.constant 1 : index
|
||||
%c2 = index.constant 2 : index
|
||||
%c8 = index.constant 8 : index
|
||||
%c32 = index.constant 32 : index
|
||||
%c0_offset = index.constant 0 : offset
|
||||
%row = index.div %t, %c2 : index
|
||||
%pair = index.rem %t, %c2 : index
|
||||
%part0 = index.mul %pair, %c2 : index
|
||||
%part1 = index.add %part0, %c1 : index
|
||||
%k_local0 = index.mul %part0, %c8 : index
|
||||
%k_local0_high = index.add %k_local0, %c32 : index
|
||||
%k_local1 = index.mul %part1, %c8 : index
|
||||
%k_local1_high = index.add %k_local1, %c32 : index
|
||||
%w0_low, %w0_high = template.apply<@ggml.weight.wmma16>(%wtype, %weight, %w_span, %row_base, %row_valid, %k, %k_size, %part0) : (index, buffer, index, index, i1, index, index, index) -> (vector<8xf16>, vector<8xf16>)
|
||||
%w1_low, %w1_high = template.apply<@ggml.weight.wmma16>(%wtype, %weight, %w_span, %row_base, %row_valid, %k, %k_size, %part1) : (index, buffer, index, index, i1, index, index, index) -> (vector<8xf16>, vector<8xf16>)
|
||||
%w_view = buffer.view %w_stage[%c0_offset] : buffer -> view<128x72xf16>
|
||||
vector.store %w0_low, %w_view[%row, %k_local0] : vector<8xf16>, view<128x72xf16>
|
||||
vector.store %w0_high, %w_view[%row, %k_local0_high] : vector<8xf16>, view<128x72xf16>
|
||||
vector.store %w1_low, %w_view[%row, %k_local1] : vector<8xf16>, view<128x72xf16>
|
||||
vector.store %w1_high, %w_view[%row, %k_local1_high] : vector<8xf16>, view<128x72xf16>
|
||||
func.return
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mm_wmma_input8(%input: buffer, %x_span: index, %col_base: index, %col_valid: i1, %k: index, %k_size: index) -> (vector<8xf16>) {
|
||||
%c7 = index.constant 7 : index
|
||||
%c0_offset = index.constant 0 : offset
|
||||
%zero8 = vector.splatf 0.0 : vector<8xf32>
|
||||
%input_view = buffer.view %input[%c0_offset] : buffer -> view<[%x_span]xf32>
|
||||
%values = scf.if %col_valid -> (vector<8xf32>) {
|
||||
%k_in = index.cmp ult, %k, %k_size : index
|
||||
%k_values = scf.if %k_in -> (vector<8xf32>) {
|
||||
%index0 = index.add %col_base, %k : index
|
||||
%index = index.assume %index0 [range(%index0, 0, 2147483646)] : index
|
||||
%last0 = index.add %index, %c7 : index
|
||||
%last = index.assume %last0 [range(%last0, 0, 2147483646)] : index
|
||||
%in_span = index.cmp ult, %last, %x_span : index
|
||||
%loaded = scf.if %in_span -> (vector<8xf32>) {
|
||||
%v = vector.load %input_view[%index] : view<[%x_span]xf32> -> vector<8xf32>
|
||||
scf.yield %v : vector<8xf32>
|
||||
} else {
|
||||
scf.yield %zero8 : vector<8xf32>
|
||||
}
|
||||
scf.yield %loaded : vector<8xf32>
|
||||
} else {
|
||||
scf.yield %zero8 : vector<8xf32>
|
||||
}
|
||||
scf.yield %k_values : vector<8xf32>
|
||||
} else {
|
||||
scf.yield %zero8 : vector<8xf32>
|
||||
}
|
||||
%values_f16 = vector.fptrunc %values : vector<8xf32> to vector<8xf16>
|
||||
func.return %values_f16 : vector<8xf16>
|
||||
}
|
||||
|
||||
func.def inline @ggml_hrx_mm_wmma_stage_input64(%input: buffer, %x_span: index, %col_base: index, %col_valid: i1, %k: index, %k_size: index, %x_stage: buffer) {
|
||||
%t0 = kernel.workitem.id<x> : index
|
||||
%t = index.assume %t0 [range(%t0, 0, 255)] : index
|
||||
%c4 = index.constant 4 : index
|
||||
%c8 = index.constant 8 : index
|
||||
%c16 = index.constant 16 : index
|
||||
%c0_offset = index.constant 0 : offset
|
||||
%col = index.div %t, %c4 : index
|
||||
%part = index.rem %t, %c4 : index
|
||||
%k_local0 = index.mul %part, %c16 : index
|
||||
%k_local1 = index.add %k_local0, %c8 : index
|
||||
%k0 = index.add %k, %k_local0 : index
|
||||
%k1 = index.add %k, %k_local1 : index
|
||||
%x0 = func.call @ggml_hrx_mm_wmma_input8(%input, %x_span, %col_base, %col_valid, %k0, %k_size) : (buffer, index, index, i1, index, index) -> (vector<8xf16>)
|
||||
%x1 = func.call @ggml_hrx_mm_wmma_input8(%input, %x_span, %col_base, %col_valid, %k1, %k_size) : (buffer, index, index, i1, index, index) -> (vector<8xf16>)
|
||||
%x_view = buffer.view %x_stage[%c0_offset] : buffer -> view<64x72xf16>
|
||||
vector.store %x0, %x_view[%col, %k_local0] : vector<8xf16>, view<64x72xf16>
|
||||
vector.store %x1, %x_view[%col, %k_local1] : vector<8xf16>, view<64x72xf16>
|
||||
func.return
|
||||
}
|
||||
@@ -0,0 +1,452 @@
|
||||
// Weight strides and span are in weight units (see lib/mm_core.loom). Input and dst strides are in elements.
|
||||
// All kernels have the same launch arguments.
|
||||
|
||||
config.decl @ggml.mul_mat.weight_type : %value: index where [range(%value, 2, 2)]
|
||||
|
||||
// ggml_mul_mat_vec_xs unrolls one dot product for each column.
|
||||
config.decl @ggml.mul_mat.vec_cols : %value: index where [range(%value, 1, 8)]
|
||||
|
||||
config.decl @ggml.mul_mat.vec_xs_workgroups : %value: index where [range(%value, 1, 65536)]
|
||||
|
||||
config.decl @ggml.mul_mat.vec_xs_depth : %value: index where [range(%value, 1, 4)]
|
||||
|
||||
// 1: the last superblock is partial.
|
||||
config.decl @ggml.mul_mat.vec_k_tail : %value: index where [range(%value, 0, 1)]
|
||||
|
||||
target.decl @ggml_wave64
|
||||
|
||||
target.decl @ggml_wave32
|
||||
|
||||
// @ggml.weight.offsets16 gives the same offsets as @ggml.weight.part16.
|
||||
template.decl @ggml.weight.offsets16(%wtype: index, %part: index) -> (index, index, index, index)
|
||||
|
||||
template.decl @ggml.weight.part16(%wtype: index, %weight: buffer, %sb_byte: offset, %part: index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, index, index, index, index)
|
||||
|
||||
template.decl @ggml.weight.superblock(%wtype: index) -> (offset, offset, index)
|
||||
|
||||
func.decl @ggml_hrx_mm_tile(%wtype: index, %k_size: index, %w_base0: index, %w_valid0: i1, %w_base1: index, %w_valid1: i1, %x_base0: index, %x_valid0: i1, %x_base1: index, %x_valid1: i1, %x_base2: index, %x_valid2: i1, %x_base3: index, %x_valid3: i1, %x_s0: index, %w_span: index, %x_span: index, %weight: buffer, %input: buffer, %w_stage: buffer, %x_stage: buffer) -> (vector<4xf32>, vector<4xf32>)
|
||||
|
||||
func.decl @ggml_hrx_mm_store(%dst: buffer, %d_span: index, %index: index, %value: f32)
|
||||
|
||||
func.decl @ggml_hrx_mv_col_base(%i: index, %n_tile: index, %n_cols: index, %x_batch: index, %x_s1: index, %k_size: index, %x_span: index, %ok_in: i1) -> (index, i1, i1, i1)
|
||||
|
||||
func.decl @ggml_hrx_mv_xload(%use: i1, %input: buffer, %x_base: index, %sb: index, %o0: index, %o1: index, %o2: index, %o3: index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>)
|
||||
|
||||
func.decl @ggml_hrx_mv_dot16(%use: i1, %w0: vector<4xf32>, %w1: vector<4xf32>, %w2: vector<4xf32>, %w3: vector<4xf32>, %x0: vector<4xf32>, %x1: vector<4xf32>, %x2: vector<4xf32>, %x3: vector<4xf32>) -> (f32)
|
||||
|
||||
func.decl @ggml_hrx_mm_store4(%dst: buffer, %d_span: index, %d_col_base: index, %row0: index, %n_rows: index, %col_valid: i1, %values: vector<4xf32>)
|
||||
|
||||
func.def inline @ggml_hrx_mul_mat_batch(%batch: index, %ne12: index, %r2: index, %r3: index, %w_s2: index, %w_s3: index, %x_s2: index, %x_s3: index, %d_s2: index, %d_s3: index) -> (index, index, index) {
|
||||
%i12 = index.rem %batch, %ne12 : index
|
||||
%i13 = index.div %batch, %ne12 : index
|
||||
%i02 = index.div %i12, %r2 : index
|
||||
%i03 = index.div %i13, %r3 : index
|
||||
%w2 = index.mul %i02, %w_s2 : index
|
||||
%w3 = index.mul %i03, %w_s3 : index
|
||||
%w_base = index.add %w2, %w3 : index
|
||||
%x2 = index.mul %i12, %x_s2 : index
|
||||
%x3 = index.mul %i13, %x_s3 : index
|
||||
%x_base = index.add %x2, %x3 : index
|
||||
%d2 = index.mul %i12, %d_s2 : index
|
||||
%d3 = index.mul %i13, %d_s3 : index
|
||||
%d_base = index.add %d2, %d3 : index
|
||||
func.return %w_base, %x_base, %d_base : index, index, index
|
||||
}
|
||||
|
||||
// vec_xs_workgroups is a multiple of the CU count. Fewer workgroups give a long tail.
|
||||
func.def inline @ggml_hrx_mv_xs_plan(%n_rows: index, %k_size: index) -> (index, index, index, index) {
|
||||
%c0 = index.constant 0 : index
|
||||
%c1 = index.constant 1 : index
|
||||
%c16 = index.constant 16 : index
|
||||
%c32 = index.constant 32 : index
|
||||
%n_wg_target = config.get @ggml.mul_mat.vec_xs_workgroups : index
|
||||
%n_wg_target_m1 = index.sub %n_wg_target, %c1 : index
|
||||
%c255 = index.constant 255 : index
|
||||
%c256 = index.constant 256 : index
|
||||
%k_tail = config.get @ggml.mul_mat.vec_k_tail : index
|
||||
%k_pad = index.mul %k_tail, %c255 : index
|
||||
%k_round = index.add %k_size, %k_pad : index
|
||||
%n_sb0 = index.div %k_round, %c256 : index
|
||||
%n_sb1 = index.min %n_sb0, %c16 : index
|
||||
%no_sb = index.cmp eq, %n_sb1, %c0 : index
|
||||
%n_sb2 = scf.select %no_sb, %c1, %n_sb1 : index
|
||||
%n_sb = index.assume %n_sb2 [range(%n_sb2, 1, 16)] : index
|
||||
%rows_par0 = index.div %c16, %n_sb : index
|
||||
%rows_par = index.assume %rows_par0 [range(%rows_par0, 1, 16)] : index
|
||||
%rows_par_m1 = index.sub %rows_par, %c1 : index
|
||||
%rows_round = index.add %n_rows, %rows_par_m1 : index
|
||||
%total_pass = index.div %rows_round, %rows_par : index
|
||||
%pass_round = index.add %total_pass, %n_wg_target_m1 : index
|
||||
%n_pass0 = index.div %pass_round, %n_wg_target : index
|
||||
%n_pass1 = index.min %n_pass0, %c32 : index
|
||||
// Each workgroup loads all activations once. Thus use at least 4 passes when this keeps 16 workgroups.
|
||||
%c4 = index.constant 4 : index
|
||||
%c64 = index.constant 64 : index
|
||||
%many_pass = index.cmp uge, %total_pass, %c64 : index
|
||||
%min_pass = scf.select %many_pass, %c4, %c1 : index
|
||||
%few_pass = index.cmp ult, %n_pass1, %min_pass : index
|
||||
%n_pass2 = scf.select %few_pass, %min_pass, %n_pass1 : index
|
||||
%n_pass = index.assume %n_pass2 [range(%n_pass2, 1, 32)] : index
|
||||
%n_pass_m1 = index.sub %n_pass, %c1 : index
|
||||
%wg_round = index.add %total_pass, %n_pass_m1 : index
|
||||
%n_wg = index.div %wg_round, %n_pass : index
|
||||
func.return %n_sb, %rows_par, %n_pass, %n_wg : index, index, index, index
|
||||
}
|
||||
|
||||
// Callers keep p < 32, col < 8 and slot < 16. Thus the index is below 4096.
|
||||
func.def inline @ggml_hrx_mv_xs_stage_index(%p: index, %col: index, %slot: index) -> (index) {
|
||||
%c7 = index.constant 7 : index
|
||||
%c8 = index.constant 8 : index
|
||||
%c15 = index.constant 15 : index
|
||||
%c16 = index.constant 16 : index
|
||||
%c31 = index.constant 31 : index
|
||||
%p_min = index.min %p, %c31 : index
|
||||
%col_min = index.min %col, %c7 : index
|
||||
%slot_min = index.min %slot, %c15 : index
|
||||
%p_in = index.assume %p_min [range(%p_min, 0, 31)] : index
|
||||
%col_in = index.assume %col_min [range(%col_min, 0, 7)] : index
|
||||
%slot_in = index.assume %slot_min [range(%slot_min, 0, 15)] : index
|
||||
%p_cols = index.mul %p_in, %c8 : index
|
||||
%p_col = index.add %p_cols, %col_in : index
|
||||
%p_slots = index.mul %p_col, %c16 : index
|
||||
%index = index.add %p_slots, %slot_in : index
|
||||
func.return %index : index
|
||||
}
|
||||
|
||||
// For few input columns and K <= 4096. The activations stay in registers for all passes of a workgroup.
|
||||
// The caller makes sure that K is a multiple of 256 (32 with vec_k_tail) and that x is contiguous along K.
|
||||
kernel.def target(@ggml_wave32) export("ggml_mul_mat_vec_xs") @ggml_mul_mat_vec_xs(%n_rows: index, %k_size: index, %n_batch: index) {
|
||||
// Launch configuration cannot call functions. Thus this repeats @ggml_hrx_mv_xs_plan.
|
||||
%c0 = index.constant 0 : index
|
||||
%one = index.constant 1 : index
|
||||
%c16 = index.constant 16 : index
|
||||
%c32 = index.constant 32 : index
|
||||
%n_wg_target = config.get @ggml.mul_mat.vec_xs_workgroups : index
|
||||
%n_wg_target_m1 = index.sub %n_wg_target, %one : index
|
||||
%wg_size = index.constant 256 : index
|
||||
%c255 = index.constant 255 : index
|
||||
%k_tail = config.get @ggml.mul_mat.vec_k_tail : index
|
||||
%k_pad = index.mul %k_tail, %c255 : index
|
||||
%k_round = index.add %k_size, %k_pad : index
|
||||
%n_sb0 = index.div %k_round, %wg_size : index
|
||||
%n_sb1 = index.min %n_sb0, %c16 : index
|
||||
%no_sb = index.cmp eq, %n_sb1, %c0 : index
|
||||
%n_sb2 = scf.select %no_sb, %one, %n_sb1 : index
|
||||
%n_sb = index.assume %n_sb2 [range(%n_sb2, 1, 16)] : index
|
||||
%rows_par0 = index.div %c16, %n_sb : index
|
||||
%rows_par = index.assume %rows_par0 [range(%rows_par0, 1, 16)] : index
|
||||
%rows_par_m1 = index.sub %rows_par, %one : index
|
||||
%rows_round = index.add %n_rows, %rows_par_m1 : index
|
||||
%total_pass = index.div %rows_round, %rows_par : index
|
||||
%pass_round = index.add %total_pass, %n_wg_target_m1 : index
|
||||
%n_pass0 = index.div %pass_round, %n_wg_target : index
|
||||
%n_pass1 = index.min %n_pass0, %c32 : index
|
||||
%c4 = index.constant 4 : index
|
||||
%c64 = index.constant 64 : index
|
||||
%many_pass = index.cmp uge, %total_pass, %c64 : index
|
||||
%min_pass = scf.select %many_pass, %c4, %one : index
|
||||
%few_pass = index.cmp ult, %n_pass1, %min_pass : index
|
||||
%n_pass2 = scf.select %few_pass, %min_pass, %n_pass1 : index
|
||||
%n_pass = index.assume %n_pass2 [range(%n_pass2, 1, 32)] : index
|
||||
%n_pass_m1 = index.sub %n_pass, %one : index
|
||||
%wg_round = index.add %total_pass, %n_pass_m1 : index
|
||||
%n_wg = index.div %wg_round, %n_pass : index
|
||||
kernel.launch.config workgroups(%n_wg, %one, %n_batch) workgroup_size(%wg_size, %one, %one) : index
|
||||
} launch(%k_size_in: index, %n_rows_in: index, %n_cols_in: index, %ne12_in: index, %r2_in: index, %r3_in: index, %w_s1_in: index, %w_s2_in: index, %w_s3_in: index, %w_span_in: index, %x_s0_in: index, %x_s1_in: index, %x_s2_in: index, %x_s3_in: index, %x_span_in: index, %d_s1_in: index, %d_s2_in: index, %d_s3_in: index, %d_span_in: index, %weight: buffer, %input: buffer, %dst: buffer) {
|
||||
// supports_mul_mat checks these ranges.
|
||||
%k_size = index.assume %k_size_in [range(%k_size_in, 1, 2147483647)] : index
|
||||
%n_rows = index.assume %n_rows_in [range(%n_rows_in, 1, 2147483647)] : index
|
||||
%n_cols = index.assume %n_cols_in [range(%n_cols_in, 1, 2147483647)] : index
|
||||
%ne12 = index.assume %ne12_in [range(%ne12_in, 1, 2147483647)] : index
|
||||
%r2 = index.assume %r2_in [range(%r2_in, 1, 2147483647)] : index
|
||||
%r3 = index.assume %r3_in [range(%r3_in, 1, 2147483647)] : index
|
||||
%w_s1 = index.assume %w_s1_in [range(%w_s1_in, 0, 2147483647)] : index
|
||||
%w_s2 = index.assume %w_s2_in [range(%w_s2_in, 0, 2147483647)] : index
|
||||
%w_s3 = index.assume %w_s3_in [range(%w_s3_in, 0, 2147483647)] : index
|
||||
%w_span = index.assume %w_span_in [range(%w_span_in, 1, 2147483647)] : index
|
||||
%x_s1 = index.assume %x_s1_in [range(%x_s1_in, 0, 2147483647)] : index
|
||||
%x_s2 = index.assume %x_s2_in [range(%x_s2_in, 0, 2147483647)] : index
|
||||
%x_s3 = index.assume %x_s3_in [range(%x_s3_in, 0, 2147483647)] : index
|
||||
%x_span = index.assume %x_span_in [range(%x_span_in, 1, 2147483647)] : index
|
||||
%d_s1 = index.assume %d_s1_in [range(%d_s1_in, 0, 2147483647)] : index
|
||||
%d_s2 = index.assume %d_s2_in [range(%d_s2_in, 0, 2147483647)] : index
|
||||
%d_s3 = index.assume %d_s3_in [range(%d_s3_in, 0, 2147483647)] : index
|
||||
%d_span = index.assume %d_span_in [range(%d_span_in, 1, 2147483647)] : index
|
||||
%wtype = config.get @ggml.mul_mat.weight_type : index
|
||||
%n_tile = config.get @ggml.mul_mat.vec_cols : index
|
||||
%c0 = index.constant 0 : index
|
||||
%c1 = index.constant 1 : index
|
||||
%c2 = index.constant 2 : index
|
||||
%c3 = index.constant 3 : index
|
||||
%c4 = index.constant 4 : index
|
||||
%c5 = index.constant 5 : index
|
||||
%c6 = index.constant 6 : index
|
||||
%c7 = index.constant 7 : index
|
||||
%c8 = index.constant 8 : index
|
||||
%c16 = index.constant 16 : index
|
||||
%c256 = index.constant 256 : index
|
||||
%c4096 = index.constant 4096 : index
|
||||
%stage_bytes = index.constant 16384 : offset
|
||||
%b0 = index.constant 0 : offset
|
||||
%zero = scalar.constant 0.0 : f32
|
||||
%zero4 = vector.splatf 0.0 : vector<4xf32>
|
||||
%true = scalar.constant true : i1
|
||||
%false = scalar.constant false : i1
|
||||
%depth = config.get @ggml.mul_mat.vec_xs_depth : index
|
||||
%t0 = kernel.workitem.id<x> : index
|
||||
%t = index.assume %t0 [range(%t0, 0, 255)] : index
|
||||
%slot = index.div %t, %c16 : index
|
||||
%part = index.rem %t, %c16 : index
|
||||
%wg = kernel.workgroup.id<x> : index
|
||||
%batch = kernel.workgroup.id<z> : index
|
||||
%weight_noalias, %input_noalias, %dst_noalias = buffer.assume.noalias %weight, %input, %dst : buffer, buffer, buffer
|
||||
%stage = buffer.alloca<workgroup> align(16) %stage_bytes : buffer
|
||||
%stage_view = buffer.view %stage[%b0] : buffer -> view<4096xf32>
|
||||
%unit_bytes, %sb_bytes, %sb_units = template.apply<@ggml.weight.superblock>(%wtype) : (index) -> (offset, offset, index)
|
||||
%n_sb, %rows_par, %n_pass, %n_wg = func.call @ggml_hrx_mv_xs_plan(%n_rows, %k_size) : (index, index) -> (index, index, index, index)
|
||||
%row_off = index.div %slot, %n_sb : index
|
||||
%sb = index.rem %slot, %n_sb : index
|
||||
%slot_on = index.cmp ult, %row_off, %rows_par : index
|
||||
%pass0 = index.mul %wg, %n_pass : index
|
||||
%row0 = index.mul %pass0, %rows_par : index
|
||||
%wg_rows = index.mul %n_pass, %rows_par : index
|
||||
%row_end_raw = index.add %row0, %wg_rows : index
|
||||
%row_end = index.min %row_end_raw, %n_rows : index
|
||||
%last_row = index.sub %row_end, %c1 : index
|
||||
%w_batch, %x_batch, %d_batch = func.call @ggml_hrx_mul_mat_batch(%batch, %ne12, %r2, %r3, %w_s2, %w_s3, %x_s2, %x_s3, %d_s2, %d_s3) : (index, index, index, index, index, index, index, index, index, index) -> (index, index, index)
|
||||
// If the last row is in the weight span, all rows of the workgroup are in it.
|
||||
%w_last_offset = index.mul %last_row, %w_s1 : index
|
||||
%w_last_base = index.add %w_batch, %w_last_offset : index
|
||||
%k_tail = config.get @ggml.mul_mat.vec_k_tail : index
|
||||
%has_tail = index.cmp ne, %k_tail, %c0 : index
|
||||
%full_units = index.mul %n_sb, %sb_units : index
|
||||
%k_units = index.mul %k_size, %sb_units : index
|
||||
%tail_units = index.div %k_units, %c256 : index
|
||||
%row_units = scf.select %has_tail, %tail_units, %full_units : index
|
||||
%w_last_end = index.add %w_last_base, %row_units : index
|
||||
%w_ok = index.cmp ule, %w_last_end, %w_span : index
|
||||
%x_base0, %use0, %store0, %x_ok0 = func.call @ggml_hrx_mv_col_base(%c0, %n_tile, %n_cols, %x_batch, %x_s1, %k_size, %x_span, %true) : (index, index, index, index, index, index, index, i1) -> (index, i1, i1, i1)
|
||||
%x_base1, %use1, %store1, %x_ok1 = func.call @ggml_hrx_mv_col_base(%c1, %n_tile, %n_cols, %x_batch, %x_s1, %k_size, %x_span, %x_ok0) : (index, index, index, index, index, index, index, i1) -> (index, i1, i1, i1)
|
||||
%x_base2, %use2, %store2, %x_ok2 = func.call @ggml_hrx_mv_col_base(%c2, %n_tile, %n_cols, %x_batch, %x_s1, %k_size, %x_span, %x_ok1) : (index, index, index, index, index, index, index, i1) -> (index, i1, i1, i1)
|
||||
%x_base3, %use3, %store3, %x_ok3 = func.call @ggml_hrx_mv_col_base(%c3, %n_tile, %n_cols, %x_batch, %x_s1, %k_size, %x_span, %x_ok2) : (index, index, index, index, index, index, index, i1) -> (index, i1, i1, i1)
|
||||
%x_base4, %use4, %store4, %x_ok4 = func.call @ggml_hrx_mv_col_base(%c4, %n_tile, %n_cols, %x_batch, %x_s1, %k_size, %x_span, %x_ok3) : (index, index, index, index, index, index, index, i1) -> (index, i1, i1, i1)
|
||||
%x_base5, %use5, %store5, %x_ok5 = func.call @ggml_hrx_mv_col_base(%c5, %n_tile, %n_cols, %x_batch, %x_s1, %k_size, %x_span, %x_ok4) : (index, index, index, index, index, index, index, i1) -> (index, i1, i1, i1)
|
||||
%x_base6, %use6, %store6, %x_ok6 = func.call @ggml_hrx_mv_col_base(%c6, %n_tile, %n_cols, %x_batch, %x_s1, %k_size, %x_span, %x_ok5) : (index, index, index, index, index, index, index, i1) -> (index, i1, i1, i1)
|
||||
%x_base7, %use7, %store7, %x_ok = func.call @ggml_hrx_mv_col_base(%c7, %n_tile, %n_cols, %x_batch, %x_s1, %k_size, %x_span, %x_ok6) : (index, index, index, index, index, index, index, i1) -> (index, i1, i1, i1)
|
||||
%ok = scf.select %w_ok, %x_ok, %false : i1
|
||||
scf.if %ok {
|
||||
%o0, %o1, %o2, %o3 = template.apply<@ggml.weight.offsets16>(%wtype, %part) : (index, index) -> (index, index, index, index)
|
||||
// A lane past K in the partial superblock loads no data. The dot products stay uniform in each group of 16 lanes.
|
||||
%sb_k = index.mul %sb, %c256 : index
|
||||
%lane_last = index.add %o3, %c4 : index
|
||||
%lane_end = index.add %sb_k, %lane_last : index
|
||||
%lane_in = index.cmp ule, %lane_end, %k_size : index
|
||||
%lane_ok = scf.select %has_tail, %lane_in, %true : i1
|
||||
%xu0 = scf.select %lane_ok, %use0, %false : i1
|
||||
%xu1 = scf.select %lane_ok, %use1, %false : i1
|
||||
%xu2 = scf.select %lane_ok, %use2, %false : i1
|
||||
%xu3 = scf.select %lane_ok, %use3, %false : i1
|
||||
%xu4 = scf.select %lane_ok, %use4, %false : i1
|
||||
%xu5 = scf.select %lane_ok, %use5, %false : i1
|
||||
%xu6 = scf.select %lane_ok, %use6, %false : i1
|
||||
%xu7 = scf.select %lane_ok, %use7, %false : i1
|
||||
%x00, %x01, %x02, %x03 = func.call @ggml_hrx_mv_xload(%xu0, %input_noalias, %x_base0, %sb, %o0, %o1, %o2, %o3) : (i1, buffer, index, index, index, index, index, index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>)
|
||||
%x10, %x11, %x12, %x13 = func.call @ggml_hrx_mv_xload(%xu1, %input_noalias, %x_base1, %sb, %o0, %o1, %o2, %o3) : (i1, buffer, index, index, index, index, index, index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>)
|
||||
%x20, %x21, %x22, %x23 = func.call @ggml_hrx_mv_xload(%xu2, %input_noalias, %x_base2, %sb, %o0, %o1, %o2, %o3) : (i1, buffer, index, index, index, index, index, index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>)
|
||||
%x30, %x31, %x32, %x33 = func.call @ggml_hrx_mv_xload(%xu3, %input_noalias, %x_base3, %sb, %o0, %o1, %o2, %o3) : (i1, buffer, index, index, index, index, index, index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>)
|
||||
%x40, %x41, %x42, %x43 = func.call @ggml_hrx_mv_xload(%xu4, %input_noalias, %x_base4, %sb, %o0, %o1, %o2, %o3) : (i1, buffer, index, index, index, index, index, index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>)
|
||||
%x50, %x51, %x52, %x53 = func.call @ggml_hrx_mv_xload(%xu5, %input_noalias, %x_base5, %sb, %o0, %o1, %o2, %o3) : (i1, buffer, index, index, index, index, index, index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>)
|
||||
%x60, %x61, %x62, %x63 = func.call @ggml_hrx_mv_xload(%xu6, %input_noalias, %x_base6, %sb, %o0, %o1, %o2, %o3) : (i1, buffer, index, index, index, index, index, index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>)
|
||||
%x70, %x71, %x72, %x73 = func.call @ggml_hrx_mv_xload(%xu7, %input_noalias, %x_base7, %sb, %o0, %o1, %o2, %o3) : (i1, buffer, index, index, index, index, index, index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>)
|
||||
%w_batch_byte = index.scale %w_batch, %unit_bytes : index, offset -> offset
|
||||
%sb_offset = index.scale %sb, %sb_bytes : index, offset -> offset
|
||||
%w_sb_byte = index.add %w_batch_byte, %sb_offset : offset
|
||||
%in_tile = index.cmp ult, %part, %n_tile : index
|
||||
%writer = scf.select %slot_on, %in_tile, %false : i1
|
||||
%is1 = index.cmp eq, %part, %c1 : index
|
||||
%is2 = index.cmp eq, %part, %c2 : index
|
||||
%is3 = index.cmp eq, %part, %c3 : index
|
||||
%is4 = index.cmp eq, %part, %c4 : index
|
||||
%is5 = index.cmp eq, %part, %c5 : index
|
||||
%is6 = index.cmp eq, %part, %c6 : index
|
||||
%is7 = index.cmp eq, %part, %c7 : index
|
||||
scf.for %p = [%c0 to %n_pass step %c1] pipeline(%depth) {
|
||||
// A row past the end reads the last row. The final sum skips its partial.
|
||||
%pass = index.add %pass0, %p : index
|
||||
%pass_row = index.mul %pass, %rows_par : index
|
||||
%row_raw = index.add %pass_row, %row_off : index
|
||||
%row_in = index.cmp ult, %row_raw, %n_rows : index
|
||||
%row = scf.select %row_in, %row_raw, %last_row : index
|
||||
%w_row = index.mul %row, %w_s1 : index
|
||||
%w_row_byte = index.scale %w_row, %unit_bytes : index, offset -> offset
|
||||
%sb_byte = index.add %w_sb_byte, %w_row_byte : offset
|
||||
%d0, %d1, %d2, %d3 = scf.if %lane_ok -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) {
|
||||
%e0, %e1, %e2, %e3, %q0, %q1, %q2, %q3 = template.apply<@ggml.weight.part16>(%wtype, %weight_noalias, %sb_byte, %part) : (index, buffer, offset, index) -> (vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, index, index, index, index)
|
||||
scf.yield %e0, %e1, %e2, %e3 : vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>
|
||||
} else {
|
||||
scf.yield %zero4, %zero4, %zero4, %zero4 : vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>
|
||||
}
|
||||
%s0 = func.call @ggml_hrx_mv_dot16(%use0, %d0, %d1, %d2, %d3, %x00, %x01, %x02, %x03) : (i1, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) -> (f32)
|
||||
%s1 = func.call @ggml_hrx_mv_dot16(%use1, %d0, %d1, %d2, %d3, %x10, %x11, %x12, %x13) : (i1, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) -> (f32)
|
||||
%s2 = func.call @ggml_hrx_mv_dot16(%use2, %d0, %d1, %d2, %d3, %x20, %x21, %x22, %x23) : (i1, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) -> (f32)
|
||||
%s3 = func.call @ggml_hrx_mv_dot16(%use3, %d0, %d1, %d2, %d3, %x30, %x31, %x32, %x33) : (i1, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) -> (f32)
|
||||
%s4 = func.call @ggml_hrx_mv_dot16(%use4, %d0, %d1, %d2, %d3, %x40, %x41, %x42, %x43) : (i1, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) -> (f32)
|
||||
%s5 = func.call @ggml_hrx_mv_dot16(%use5, %d0, %d1, %d2, %d3, %x50, %x51, %x52, %x53) : (i1, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) -> (f32)
|
||||
%s6 = func.call @ggml_hrx_mv_dot16(%use6, %d0, %d1, %d2, %d3, %x60, %x61, %x62, %x63) : (i1, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) -> (f32)
|
||||
%s7 = func.call @ggml_hrx_mv_dot16(%use7, %d0, %d1, %d2, %d3, %x70, %x71, %x72, %x73) : (i1, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>, vector<4xf32>) -> (f32)
|
||||
%v1 = scf.select %is1, %s1, %s0 : f32
|
||||
%v2 = scf.select %is2, %s2, %v1 : f32
|
||||
%v3 = scf.select %is3, %s3, %v2 : f32
|
||||
%v4 = scf.select %is4, %s4, %v3 : f32
|
||||
%v5 = scf.select %is5, %s5, %v4 : f32
|
||||
%v6 = scf.select %is6, %s6, %v5 : f32
|
||||
%v7 = scf.select %is7, %s7, %v6 : f32
|
||||
scf.if %writer {
|
||||
%i_raw = func.call @ggml_hrx_mv_xs_stage_index(%p, %part, %slot) : (index, index, index) -> (index)
|
||||
%i = index.assume %i_raw [range(%i_raw, 0, 4095)] : index
|
||||
%i_in = index.cmp ult, %i, %c4096 : index
|
||||
scf.if %i_in {
|
||||
view.store %v7, %stage_view[%i] : f32, view<4096xf32>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
kernel.barrier<workgroup> scope(workgroup) ordering(acq_rel)
|
||||
scf.if %ok {
|
||||
%n_out = index.mul %wg_rows, %n_tile : index
|
||||
scf.for %o_raw = [%t to %n_out step %c256] {
|
||||
%o = index.assume %o_raw [range(%o_raw, 0, 4095)] : index
|
||||
%row_local = index.div %o, %n_tile : index
|
||||
%col = index.rem %o, %n_tile : index
|
||||
%p = index.div %row_local, %rows_par : index
|
||||
%ro = index.rem %row_local, %rows_par : index
|
||||
%row = index.add %row0, %row_local : index
|
||||
%row_in = index.cmp ult, %row, %n_rows : index
|
||||
%col_in = index.cmp ult, %col, %n_cols : index
|
||||
%st = scf.select %row_in, %col_in, %false : i1
|
||||
scf.if %st {
|
||||
%ro_slots = index.mul %ro, %n_sb : index
|
||||
%sum = scf.for %j = [%c0 to %n_sb step %c1](%acc = %zero : f32) -> (f32) {
|
||||
%slot_j = index.add %ro_slots, %j : index
|
||||
%i_raw = func.call @ggml_hrx_mv_xs_stage_index(%p, %col, %slot_j) : (index, index, index) -> (index)
|
||||
%i = index.assume %i_raw [range(%i_raw, 0, 4095)] : index
|
||||
%i_in = index.cmp ult, %i, %c4096 : index
|
||||
%next = scf.if %i_in -> (f32) {
|
||||
%part_sum = view.load %stage_view[%i] : view<4096xf32> -> f32
|
||||
%a = scalar.addf %acc, %part_sum : f32
|
||||
scf.yield %a : f32
|
||||
} else {
|
||||
scf.yield %acc : f32
|
||||
}
|
||||
scf.yield %next : f32
|
||||
}
|
||||
%d_offset = index.mul %col, %d_s1 : index
|
||||
%d_col = index.add %d_batch, %d_offset : index
|
||||
%d_index = index.add %d_col, %row : index
|
||||
func.call @ggml_hrx_mm_store(%dst_noalias, %d_span, %d_index, %sum) : (buffer, index, index, f32)
|
||||
}
|
||||
}
|
||||
}
|
||||
kernel.return
|
||||
}
|
||||
|
||||
kernel.def target(@ggml_wave64) export("ggml_mul_mat_tiled") @ggml_mul_mat_tiled(%n_rows: index, %n_cols: index, %n_batch: index) {
|
||||
%one = index.constant 1 : index
|
||||
%wg_size = index.constant 256 : index
|
||||
%c31 = index.constant 31 : index
|
||||
%c32 = index.constant 32 : index
|
||||
%c63 = index.constant 63 : index
|
||||
%c64 = index.constant 64 : index
|
||||
%rows_round = index.add %n_rows, %c63 : index
|
||||
%row_tiles = index.div %rows_round, %c64 : index
|
||||
%cols_round = index.add %n_cols, %c31 : index
|
||||
%col_tiles = index.div %cols_round, %c32 : index
|
||||
kernel.launch.config workgroups(%row_tiles, %col_tiles, %n_batch) workgroup_size(%wg_size, %one, %one) : index
|
||||
} launch(%k_size_in: index, %n_rows_in: index, %n_cols_in: index, %ne12_in: index, %r2_in: index, %r3_in: index, %w_s1_in: index, %w_s2_in: index, %w_s3_in: index, %w_span_in: index, %x_s0_in: index, %x_s1_in: index, %x_s2_in: index, %x_s3_in: index, %x_span_in: index, %d_s1_in: index, %d_s2_in: index, %d_s3_in: index, %d_span_in: index, %weight: buffer, %input: buffer, %dst: buffer) {
|
||||
// supports_mul_mat checks these ranges.
|
||||
%k_size = index.assume %k_size_in [range(%k_size_in, 1, 2147483647)] : index
|
||||
%n_rows = index.assume %n_rows_in [range(%n_rows_in, 1, 2147483647)] : index
|
||||
%n_cols = index.assume %n_cols_in [range(%n_cols_in, 1, 2147483647)] : index
|
||||
%ne12 = index.assume %ne12_in [range(%ne12_in, 1, 2147483647)] : index
|
||||
%r2 = index.assume %r2_in [range(%r2_in, 1, 2147483647)] : index
|
||||
%r3 = index.assume %r3_in [range(%r3_in, 1, 2147483647)] : index
|
||||
%w_s1 = index.assume %w_s1_in [range(%w_s1_in, 0, 2147483647)] : index
|
||||
%w_s2 = index.assume %w_s2_in [range(%w_s2_in, 0, 2147483647)] : index
|
||||
%w_s3 = index.assume %w_s3_in [range(%w_s3_in, 0, 2147483647)] : index
|
||||
%w_span = index.assume %w_span_in [range(%w_span_in, 1, 2147483647)] : index
|
||||
%x_s0 = index.assume %x_s0_in [range(%x_s0_in, 0, 2147483647)] : index
|
||||
%x_s1 = index.assume %x_s1_in [range(%x_s1_in, 0, 2147483647)] : index
|
||||
%x_s2 = index.assume %x_s2_in [range(%x_s2_in, 0, 2147483647)] : index
|
||||
%x_s3 = index.assume %x_s3_in [range(%x_s3_in, 0, 2147483647)] : index
|
||||
%x_span = index.assume %x_span_in [range(%x_span_in, 1, 2147483647)] : index
|
||||
%d_s1 = index.assume %d_s1_in [range(%d_s1_in, 0, 2147483647)] : index
|
||||
%d_s2 = index.assume %d_s2_in [range(%d_s2_in, 0, 2147483647)] : index
|
||||
%d_s3 = index.assume %d_s3_in [range(%d_s3_in, 0, 2147483647)] : index
|
||||
%d_span = index.assume %d_span_in [range(%d_span_in, 1, 2147483647)] : index
|
||||
%wtype = config.get @ggml.mul_mat.weight_type : index
|
||||
%c4 = index.constant 4 : index
|
||||
%c8 = index.constant 8 : index
|
||||
%c16 = index.constant 16 : index
|
||||
%c24 = index.constant 24 : index
|
||||
%c32 = index.constant 32 : index
|
||||
%c64 = index.constant 64 : index
|
||||
%w_stage_bytes = index.constant 8192 : offset
|
||||
%x_stage_bytes = index.constant 4224 : offset
|
||||
%t0 = kernel.workitem.id<x> : index
|
||||
%t = index.assume %t0 [range(%t0, 0, 255)] : index
|
||||
%row_tile = kernel.workgroup.id<x> : index
|
||||
%col_tile = kernel.workgroup.id<y> : index
|
||||
%batch = kernel.workgroup.id<z> : index
|
||||
%weight_noalias, %input_noalias, %dst_noalias = buffer.assume.noalias %weight, %input, %dst : buffer, buffer, buffer
|
||||
%w_stage = buffer.alloca<workgroup> align(16) %w_stage_bytes : buffer
|
||||
%x_stage = buffer.alloca<workgroup> align(16) %x_stage_bytes : buffer
|
||||
%w_batch, %x_batch, %d_batch = func.call @ggml_hrx_mul_mat_batch(%batch, %ne12, %r2, %r3, %w_s2, %w_s3, %x_s2, %x_s3, %d_s2, %d_s3) : (index, index, index, index, index, index, index, index, index, index) -> (index, index, index)
|
||||
%tile_row0 = index.mul %row_tile, %c64 : index
|
||||
%tile_col0 = index.mul %col_tile, %c32 : index
|
||||
|
||||
%w_local0 = index.div %t, %c8 : index
|
||||
%w_row0 = index.add %tile_row0, %w_local0 : index
|
||||
%w_row1 = index.add %w_row0, %c32 : index
|
||||
%w_valid0 = index.cmp ult, %w_row0, %n_rows : index
|
||||
%w_valid1 = index.cmp ult, %w_row1, %n_rows : index
|
||||
%w_offset0 = index.mul %w_row0, %w_s1 : index
|
||||
%w_offset1 = index.mul %w_row1, %w_s1 : index
|
||||
%w_base0 = index.add %w_batch, %w_offset0 : index
|
||||
%w_base1 = index.add %w_batch, %w_offset1 : index
|
||||
|
||||
%x_local0 = index.div %t, %c32 : index
|
||||
%x_col0 = index.add %tile_col0, %x_local0 : index
|
||||
%x_col1 = index.add %x_col0, %c8 : index
|
||||
%x_col2 = index.add %x_col0, %c16 : index
|
||||
%x_col3 = index.add %x_col0, %c24 : index
|
||||
%x_valid0 = index.cmp ult, %x_col0, %n_cols : index
|
||||
%x_valid1 = index.cmp ult, %x_col1, %n_cols : index
|
||||
%x_valid2 = index.cmp ult, %x_col2, %n_cols : index
|
||||
%x_valid3 = index.cmp ult, %x_col3, %n_cols : index
|
||||
%x_offset0 = index.mul %x_col0, %x_s1 : index
|
||||
%x_offset1 = index.mul %x_col1, %x_s1 : index
|
||||
%x_offset2 = index.mul %x_col2, %x_s1 : index
|
||||
%x_offset3 = index.mul %x_col3, %x_s1 : index
|
||||
%x_base0 = index.add %x_batch, %x_offset0 : index
|
||||
%x_base1 = index.add %x_batch, %x_offset1 : index
|
||||
%x_base2 = index.add %x_batch, %x_offset2 : index
|
||||
%x_base3 = index.add %x_batch, %x_offset3 : index
|
||||
|
||||
%acc0, %acc1 = func.call @ggml_hrx_mm_tile(%wtype, %k_size, %w_base0, %w_valid0, %w_base1, %w_valid1, %x_base0, %x_valid0, %x_base1, %x_valid1, %x_base2, %x_valid2, %x_base3, %x_valid3, %x_s0, %w_span, %x_span, %weight_noalias, %input_noalias, %w_stage, %x_stage) : (index, index, index, i1, index, i1, index, i1, index, i1, index, i1, index, i1, index, index, index, buffer, buffer, buffer, buffer) -> (vector<4xf32>, vector<4xf32>)
|
||||
|
||||
%out_row_group = index.rem %t, %c16 : index
|
||||
%out_row_local = index.mul %out_row_group, %c4 : index
|
||||
%out_row = index.add %tile_row0, %out_row_local : index
|
||||
%out_local0 = index.div %t, %c16 : index
|
||||
%out_col0 = index.add %tile_col0, %out_local0 : index
|
||||
%out_col1 = index.add %out_col0, %c16 : index
|
||||
%out_valid0 = index.cmp ult, %out_col0, %n_cols : index
|
||||
%out_valid1 = index.cmp ult, %out_col1, %n_cols : index
|
||||
%d_offset0 = index.mul %out_col0, %d_s1 : index
|
||||
%d_offset1 = index.mul %out_col1, %d_s1 : index
|
||||
%d_base0 = index.add %d_batch, %d_offset0 : index
|
||||
%d_base1 = index.add %d_batch, %d_offset1 : index
|
||||
func.call @ggml_hrx_mm_store4(%dst_noalias, %d_span, %d_base0, %out_row, %n_rows, %out_valid0, %acc0) : (buffer, index, index, index, index, i1, vector<4xf32>)
|
||||
func.call @ggml_hrx_mm_store4(%dst_noalias, %d_span, %d_base1, %out_row, %n_rows, %out_valid1, %acc1) : (buffer, index, index, index, index, i1, vector<4xf32>)
|
||||
kernel.return
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user