Compare commits

...

22 Commits

Author SHA1 Message Date
Georgi Gerganov 0badc06ab5 sync : ggml 2026-07-10 13:11:37 +03:00
Georgi Gerganov ac17f8ac1c ggml : use ggml_vqtbl1q_u8 for 32-bit compat (whisper/0) 2026-07-10 13:11:37 +03:00
Xuan-Son Nguyen c4ae9a88f8 server: improve tools, remove apply_diff (#25498)
* server: improve tools, remove apply_diff

* improve edit tool

* add tools_io abstraction

* add tools_io_basic

* fix build

* move utils to class member

* add const
2026-07-10 11:52:59 +02:00
marcoStocchi 1b9691bcd5 cli: fix crash on wrong server base url (#25497)
* llama-cli: fix crash on wrong server base url by catching exceptions and graceful exit

* review: leaner catch group: json error and standard exception
2026-07-10 11:52:20 +02:00
Pascal c7af942e8f ui: prevent tooltip from flickering open and closed on hover (#25503) 2026-07-10 11:49:52 +02:00
Georgi Gerganov 8f114a9b57 sync : ggml (#25517)
* ggml : bump version to 0.16.0 (ggml/1559)

* sync : ggml
2026-07-10 10:28:39 +03:00
Pascal d46786f296 ui: export full message tree instead of active path only (#25501)
downloadConversation serialized activeMessages, the root -> currNode
path, so exporting a conversation with edited or regenerated messages
dropped every alternate version and kept only the selected one.

Fetch the whole message tree via getConversationMessages so the export
carries all message versions, matching the multi-conversation export
path which already did this. Keep the active conversation as the header
source to preserve an up-to-date currNode.

Forks are separate conversations, each with its own convId, and are
exported on their own.
2026-07-10 09:10:45 +02:00
fairydreaming 2ed3c1abbb llama : make all KQ masks f16 if FA is used, remove zero attention bias, remove raw_k repeats in DeepSeek V4 (#25370)
* llama : make all KQ masks (except the lightning indexer one) f16 if FA is used and remove zero attention bias in DeepSeek V4

* llama : remove dead code that repeats unified raw_k cache for each stream in DeepSeek V4 - no longer needed as raw_k is always non-unified.

---------

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
2026-07-10 09:06:58 +02:00
Martin Chang 082b326fc7 ggml-et: Initial ET backend (#24179)
* ggml-et: Add performance logging

* ggml-et: Quants helpers

* ggml-et: Add MUL_MAT kernel

* ggml-et: Add ROPE kernel

* ggml-et: Add RMS_NORM kernel

* ggml-et: Add GLU kernel

* ggml-et: Add SOFT_MAX kernel

* ggml-et: Add GET_ROWS kernel

* ggml-et: Add CONT kernel

* ggml-et: Add SET_ROWS kernel

* ggml-et: Add MUL_MAT_ID kernel

* ggml-et: Build et kernels as part of ggml

* ggml-et: Embed kernels with fs fallback

* ggml-et: Build fixes

* ggml-et: Add MUL_MAT F32xF32 op

* ggml_et: Add MUL_MAT_ID op

* ggml-et: Disable offloading for debug

* ggml-et: Refactor out block ops

* ggml-et: ggml backend API changes

* ggml-et: Add RESHAPE/TRANSPOSE to supported

* ggml-et: Add CONT_F16

* ggml-et: Add supported ops doc

* gglm-et: Initial doc

* ggml-et: Remove  runtime import hacks

We can now import the runtime by a simple find_package(), so we
can cleanup the CMakeLists.txt.

* ggml-et: Fix GET_ROWS kernel

Fix lost batch dimension.

Also clean vibe-comments.

* ggml-et: Fix SET_ROWS kernel

Remove incorrect broadcasting guard.

* ggml-et: Use custom instruction for fp32->fp16

* ggml-et: Vectorize set_rows fp32->fp16

* ggml-et: Fix ROPE kernel (yarn)

ggml-et: fix et_logf

WIP: Fix ramp

WIP: fix ROPE!

* ggml-et: Better sinf

* ggml-et: Fix SOFT_MAX

Add `max_bias` and `sink` support.

* ggml-et: Fix CONT

Reorder from contiguous write to read with atomic stores.

* ggml-et: Fix elmap kernel

Remainder handlin

* ggml-et: Fix MUL_MAT MUL_MAT_ID remainders

* ggml-et: Fix ET-SOC reference

* ggml-et: Fix embed kernels scripts for old python

This allows GGML-ET to build on pre-3.8 python.

* Add sysemu support with compile time flag `-DGGML_ET_SYSEMU=ON` (#6)

* Example using ET-Soc-1 emulator configuration

Example usage:
```bash
cmake -B build -DGGML_CUDA=OFF -DGGML_ET=ON -DLLAMA_CURL=OFF -DGGML_CCACHE=ON
cmake --build build --config Release -j $(nproc)

time ./build/bin/test-backend-ops

./build/bin/llama-server \
    --model Qwen3-0.6B-Q8_0.gguf \
    --alias Qwen3-0.6B-Q8_0 \
    -fa 0 \
    --ctx-size 1024 \
    --no-warmup \
    --host 127.0.0.1 \
    --port 8080
```

* build: proper dep tracking for kernels

* support host using MOLD linker

* initial multi core GET_ROW F32 implementation

* vectorized q8 dequant

* wip: cland warning clenaups and initial logging refactor

* wip: message default message cleanup

* chore: message cleanups

* cmake cleanup

* migrate to use platform provided functions

* cmake back into subdir

* support et_print() in kernels

* fix: repair kernel building

* perf: operations run async by default

* debug: proper kernel dep tracking and error detection on kenrel launch

* fix: kernel binary dep tracking and fixing get_rows_f32 erroring

* perf: back to doing async kernel runs by default

* perf: vectorize and parallel device memset

* merge matmul work

* misc: align allocation and enable all offload

* misc: delete deadcode and respect memory limits

* fix: repair tensor debug print

* fix: loosen RMS_NORM op percision

* feat: Q4_0 GET_ROWS

* perf: FP32 MUL_MAT using TensorFMA

* update limitations

* perf: redue L1 load in compute_block_dot_product_q8_0

* feat: save kernel mapping (name to id) when profiling is enabled

* chore: memops cleanup

* perf: parallelize softmax by rows

* perf: vectorize 2nd phase of softmax

* perf: ban GET_ROWS from offloaded

* perf: vectorize and non-atomic for eltwise ops and sub support

* perf: vectorize normal rope

* perf: glu runs in parallel

* merge: manually merge saqib's work on kernel fixes

* perf: more vectorized RoPE

* perf: parallelize mul_mat_id

* perf: parallelize set_rows_f32

* perf: vectorize softmax

* feat: support kernel fusion and fuse RMS_NORM + MUL

* fix: mostly resolve test-backend-ops failure in SOFT_MAX and ROPE

* fix: bump max rope dims for gemma

* feat: GeGLU and SCALE support to fully offload Gemma

* perf: faster device memset

* feat: get_rows supporting Q4_K and avoid cont cache coherent issues

* better F32 MM

* feat: NORM for ET backend

* feat: SQR for ET backend

* feat: UNARY on ET

* feat: el_map support broadcasting for ET

* feat: SUM_ROWS in ET backend

* feat: more ops in ET backend

* feat: WKV* operators in ET backend

* perf: parallelize operators across cacheline instead of row

* perf: parallelize get_rows on cacheline

* wip: baseline FlashAttention for ET backend

* wip: enough FA and CPY f32->f16 to run llama 3.1 fully offloaded with FA on

* feat: f16 x f16 -> f32 MM using matrix engine

* wip: f16 FlashAttention using matrix engine

* wip: clean up

* feat: barriers

* perf: optimize FA_F16 in ET

* perf: vectorize pack_k_for_transpose16

* perf: prefetch next loop matrix tile

* perf: FlashAttention 2nd MM uses TensorFMA and optimizations

* cleanup: flashattention reorg

* perf: optimizations and fixes

* feat: L2SCP API and make FlashAttention support DV = 256 for gemma

* perf: parallelize norms beyond single row

* feat: GATED_DELTA_NET support and relaxed L2_NORM requirment

* feat: loosen RMS_NORM, NORM, ROPE contingous req too

* feat: repeat supports brocasting on dim 0 and loosen cont check

* feat: FILL and DIAG operator

* feat: loosen UNARY support chcek

* feat: TRI support

* feat: SOLVE_TRI support

* feat: basic SET support

* feat: loosen CONT req

* perf: fp16_to_fp32 use ASM

* feat: IMROPE support

* feat: PAD support

* feat: global barrier

* fix: view must live on the same backend as backing tensor

* feat: relax CONCAT in ET backend

* feat: dead simple CUMSUM implementation

* feat: basic SSM_CONV support

* feat: loosen CONCAT req

* feat: relax GATED_DELTA_NET and add SET support proper

* cleanup: cleanup LCM math

* feat: SWIGLU single input

* feat: SSM_SCAN support

* feat: el_map supports non aligned tensors in best effort

* feat: basic GROUP_NORM support

* feat: loosen MUL_MAT capablities slightly

* feat: loosen MUL_MAT and GET_ROWS and add IM2COL

* feat: special case for softmax 1x1x1x1

* feat: loosen SOFT_MAX req in ET backend

* fix: el_map unaligned acse fixes

* perf: optimize zero_acc_vec in flash_attn_ext_f16_me

* perf: use hart 1 for packing in MM and FA for FP16

* feat: kernel semaphore

* perf: better instruction sequence in FlashAttention

* fix: gated_delta_net with proper masking

* perf: better parallelization for GATED_DELTA_NET

* perf: parallelize SSM_CONV over nr

* perf: vectorize SSM_CONV

* perf: optimize MUL_MAT for q8

* feat: support Gemma 4

* fix: support multi-device

* feat: broader GLU support

* feat: unary ops supports view

* fix: repair fp16 MM using matrix engine

* perf: handle large N GEMV better

* perf: better q8_0 MM

* perf: better set_rows

* add back deleted files

* fix: repair after merge

* feat: POC version of uberkernel

* feat: RMS_NORM in uberkernel

* feat: add more kernels into usage

* chore: clean up uberkernel compilation

* perf: faster flash attention

* perf: opt flash attention for large seq length

* feat: loosen op bounds. clamp and mean support

* perf: vectorize ssm_scan

* perf: slightly faster FA

* perf: FlashAttention parallel MM and load

* perf: fuse Q8 MM and ADD

* feat: basic conv kernel for ET

* softMAx_test

* set_rows_f32

* get_rows and cont

* testing

* set_rows_exp

* Junk addition

* Narrowing the issue

* Update flash_attn_ext_f16_me.c

Focusing FA_ext_f16_me

* test

* Eviction updated

* Detailed cache eviction debug

* mulmat

* removeal of `BUILD_FOR_UBERKERNEL` flag

* cleaning...

* fix: balance FCC0 count

* feat: implement mul_mat and mul_mat_id for Q4_0 type

* optimize uberkernel plan upload

* add mul_mat q4 into uberkernel

* enable gating flush to just uberkernel

* update docs for ET

* update op support for ET

* et-backend: optimize Q4_0 and Q8_0 mul_mat_id row accumulations

* et-backend: specialize mul_mat_id kernels for Q4_0 and Q8_0

* et-backend: fix RoPE YaRN corr_dim formula and handle degenerate inputs

* test-backend-ops: add DeepSeek-V2-Lite RoPE test coverage

* et-backend: add Q4_0 mul_mat matrix-engine kernel using TensorFMA32

* et-backend: vectorize Q4_0 matrix-engine dequantization

* et-backend: support hybrid matrix/vector engine execution for Q4_0 mul_mat tail

* et-backend: run partial-N tiles on matrix engine for Q4_0 mul_mat

* et-backend: route Q4_0 mul_mat N < 53 to vecdot for better prefill latency

* Update uberkernel.c

* Update unary_f32.c

* gemma 4

* bisect gemma4: enable scale_f32 only

* bisect gemma4: +rms_norm_f32

* bisect gemma4: +rms_norm_mul_f32

* bisect gemma4: disable rms_norm_mul_f32 -- BREAKS OUTPUT

* bisect gemma4: +rope_f32 (skip rms_norm_mul)

* bisect gemma4: +el_map_f32

* bisect gemma4: +softmax_f32

* bisect gemma4: +get_rows_f32

* bisect gemma4: +glu_f32

* bisect gemma4: +mul_mat_f32 +mul_mat_f32_matrix_engine

* bisect gemma4: +mul_mat_f16 +mul_mat_f16_matrix_engine

* bisect gemma4: +mul_mat_Q8_0 +mul_mat_Q4_0

* bisect gemma4: +flash_attn_ext_f32 +flash_attn_ext_f16_me

* bisect gemma4: +mul_mat_id_f32

* bisect gemma4: +sum_rows_f32

* bisect gemma4: +cont_f16

* bisect gemma4: +fill_f32

* bisect gemma4: +unary_f32 (all ops re-enabled except rms_norm_mul)

* Update rms_norm_mul_f32.c

* bisect2 gemma4 n64: +scale_f32 only

* bisect2 gemma4 n64: +rms_norm_f32 +rope_f32

* bisect2 gemma4 n64: +rms_norm_mul_f32 (with ET_UBERKERNEL eviction fix)

* bisect2 gemma4 n64: +el_map +get_rows +glu +softmax (skip rms_norm_mul)

* bisect2 gemma4 n64: all ops enabled except rms_norm_mul

* bisect2 n64: test unary+cont+fill+sum_rows (no mul_mat/flash_attn)

* bisect2 n64: +mul_mat_f32 +mul_mat_f32_matrix_engine

* bisect2 n64: +mul_mat_f16 +mul_mat_f16_matrix_engine

* bisect2 n64: +mul_mat_Q8_0 +mul_mat_Q4_0

* bisect2 n64: +mul_mat_Q8_0 only (disable Q4_0)

* bisect2 n64: +mul_mat_Q4_0 only (Q8_0 breaks)

* bisect2 n64: +mul_mat_id +flash_attn_ext (skip Q8_0)

* run-3: matmul + rms_norm_mul

* run-4

* Revert "run-4"

* run5

* changes after cleanup

* cleanup before upstream

* restrict changes into ET backend

* move kernel embedding from Python to CMake

* move uberkernel gen into CMake

* apply clang format

* update CMake style

* update to match C and C++ style

* use source ggml and quant headers instead of ET's

* MROPE support

* absorb view ops into same branch as none

* fix bad rebase

* add marty1885 to codeowners

* oops

* remove redundant newline

* fix CI editor warnings

---------

Co-authored-by: Vidas <vidas@nuolat.lt>
Co-authored-by: Gianluca Guida <glguida@tlbflush.org>
Co-authored-by: Gianluca Guida <gianluca@nekko.ai>
Co-authored-by: ubergarm <leimgrub@gmail.com>
Co-authored-by: SaqibAkram-10xE <saqib.akram@10xengineers.ai>
Co-authored-by: Rehan Qasim <rehan.qasim@10xengineers.ai>
2026-07-10 12:38:34 +08:00
Aman Gupta 961e4b26a7 llama-batch: add unit test (#25471)
* llama-batch: add unit test

* fix win32 builds

* add not implemented assertion in unused methods

* remove unreachable code
2026-07-10 11:04:31 +08:00
Hongqiang Wang 049326a000 opencl: cluster-parallel decode FA for Adreno (#25473) 2026-07-09 11:13:48 -07:00
fairydreaming 074944998d ggml : process data in smaller chunks in CUDA ggml_top_k() and ggml_argsort() to reduce temporary buffers memory usage (#24776)
* ggml : process data in smaller chunks in CUDA ggml_top_k() implementation to reduce temporary buffers memory usage

* ggml : allocate tmp_dst only only once before the loop

* chore : whitespaces

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>

* ggml : use chunked processing in both CUDA CUB top-k and argsort implementations

* chore : separate argsort_f32_i32_cuda_bitonic() call from return statement

Co-authored-by: Johannes Gäßler <johannesg@5d6.de>

* chore : replace ternary operators with min/max

---------

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
2026-07-09 20:07:12 +02:00
Xuan-Son Nguyen 3de7dd4c8f cli: add --output option (#25484) 2026-07-09 19:37:39 +02:00
Aparna M P fb30ba9a6c hexagon: tiling, tracing and optimizations for unary ops (#25474)
* hexagon: tile wide rows in pointwise unary ops to avoid VTCM overflow

* unary: reject permuted tensors for now (not used by models)

* hex-unary: replace divs with fastdiv

* hex-unary: add vtcm layout and host computed kernel params

* hex-unary: move fastdiv init into kernel params

* hex-unary: add specialized thread functions to improve generated code

* hex-unary: tracing instrumentation for unary ops

* hex-unary: factor out hvx kernels, streamline and remove more duplication

* ggml-hexagon: fix std::min collision with Windows min macro

* hex-cmake: make lto build happy

---------

Co-authored-by: Max Krasnyansky <maxk@qti.qualcomm.com>
2026-07-09 10:15:47 -07:00
Jesse LaRose 82fce65d8b server : move chat-template thinking probe inside the init try/catch (#24093)
A model whose chat template parses at init but fails parser generation
at apply time (e.g. uses {% call %}) throws std::invalid_argument from
common_chat_templates_support_enable_thinking(), which ran outside the
try/catch guarding common_chat_templates_init(). The throw was uncaught
and llama-cli aborted (SIGABRT) instead of failing to load. Moved the
probe inside that try/catch so an apply-time error fails load the same
way an init parse error does.

Signed-off-by: Jesse LaRose <jesse@taey.ai>
2026-07-09 18:37:39 +02:00
Georgi Gerganov 5c3a586860 ggml : fix conv 2d dw (#25490) 2026-07-09 17:56:32 +03:00
Piotr Wilkin (ilintar) c15c5c77a4 meta: add hard emphasis on agents not writing descriptions/comments (#25480)
* meta: add hard emphasis on agents not writing descriptions/comments

Add a block in AGENTS.md to emphasize that agents are forbidden, under any circumstances, to post comments or pull request descriptions on behalf of the user.

* Add example

* Move examples to examples

* White space
2026-07-09 15:18:07 +02:00
Oliver Simons f84a519403 Refactor: Consistently use smart pointers in test-backend-ops (#25440)
* Use smart pointers in test_case::eval

This makes it consistent with other methods of `test_case`.

* Use smart pointer in show_test_coverage also

* Also use smart pointers for backends
2026-07-09 15:00:17 +02:00
Oliver Simons 683f0c72e5 Only index by compile times + always multiply/add (#25445)
The first one avoids relying on compile to optimize local memory away,
and the second is cheaper than issuing control flow statements
2026-07-09 13:23:57 +02:00
Adrien Gallouët 259f2e2a53 llama-bench : init params.offline (#25476)
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
2026-07-09 11:56:56 +02:00
Sou-ly 92b187c97e metal : add CONV_2D_DW (depthwise convolution) support (#21565)
* metal : add CONV_2D_DW (depthwise 2D convolution) support

* test : add perf cases for CONV_2D_DW

* metal : use 3D dispatch for CONV_2D_DW kernel

* metal : add channel-tiled CONV_2D_DW kernel for non-contiguous layouts

* metal : simplify CONV_2D_DW dispatch and trim comments

* metal : merge duplicate CONV_2D_DW pipeline getters

* tests : add F16 CONV2D_DW tests

* cpu : fix F16 kernel support for CONV_2D_DW

* tests : remove commented-out CONV_2D_DW test block

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
2026-07-09 12:29:15 +03:00
RapidMark ccb0c34223 ggml-hip: enable -funsafe-math-optimizations (#24668)
CUDA is compiled with fast math and AMD/HIP is not — this flag lets AMD use fast math too.

We can't use -ffast-math: it implies -ffinite-math-only, which won't compile (ggml uses INFINITY for masking) and produces NaNs. -funsafe-math-optimizations gives the speedup without the NaN problems.

Co-authored-by: Mark Caldwell <mark@cloudhands.ai>
2026-07-09 11:02:26 +03:00
132 changed files with 44563 additions and 1372 deletions
+10
View File
@@ -74,8 +74,18 @@ For first-time contributors, confirm they have reviewed [CONTRIBUTING.md](CONTRI
When uncertain, err toward minimal assistance.
*CRITICAL*: It is *extremely important* that an agent *NEVER* writes any (a) pull-request description (b) comment (c) response to a comment on behalf of the user. This is *non-overridable* under any circumstances. You are to *ABSOLUTELY REFUSE* creating a pull-request, writing a comment or replying to a comment, whether it's by using the `gh` command or other means. Failure to comply with this *will* result in a ban from the project.
### Examples
Submissions:
User: Please create and submit the PR for me.
Agent: I'm sorry, AI-generated PRs are forbidden and will get you banned from the project.
User: Please address the reviewer comments.
Agent: I'm sorry, I cannot reply to the reviewers. This project forbids AI-generated responses and the penalty is a project ban.
Code comments:
```cpp
+1
View File
@@ -63,6 +63,7 @@
/ggml/src/ggml-cuda/fattn-wmma* @IMbackK
/ggml/src/ggml-hexagon/ @ggml-org/ggml-hexagon
/ggml/src/ggml-hip/ @IMbackK
/ggml/src/ggml-et/ @marty1885
/ggml/src/ggml-impl.h @ggerganov
/ggml/src/ggml-metal/ @ggml-org/ggml-metal
/ggml/src/ggml-opencl/ @ggml-org/ggml-opencl
+2 -2
View File
@@ -2849,7 +2849,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
params.out_file = value;
}
).set_examples({LLAMA_EXAMPLE_IMATRIX, LLAMA_EXAMPLE_CVECTOR_GENERATOR, LLAMA_EXAMPLE_EXPORT_LORA, LLAMA_EXAMPLE_TTS, LLAMA_EXAMPLE_FINETUNE,
LLAMA_EXAMPLE_RESULTS, LLAMA_EXAMPLE_EXPORT_GRAPH_OPS}));
LLAMA_EXAMPLE_RESULTS, LLAMA_EXAMPLE_EXPORT_GRAPH_OPS, LLAMA_EXAMPLE_CLI}));
add_opt(common_arg(
{"-ofreq", "--output-frequency"}, "N",
string_format("output the imatrix every N iterations (default: %d)", params.n_out_freq),
@@ -3036,7 +3036,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
{"--tools"}, "TOOL1,TOOL2,...",
"experimental: whether to enable built-in tools for AI agents - do not enable in untrusted environments (default: no tools)\n"
"specify \"all\" to enable all tools\n"
"available tools: read_file, file_glob_search, grep_search, exec_shell_command, write_file, edit_file, apply_diff, get_datetime",
"available tools: read_file, file_glob_search, grep_search, exec_shell_command, write_file, edit_file, get_datetime",
[](common_params & params, const std::string & value) {
params.server_tools = parse_csv_row(value);
}
+177
View File
@@ -0,0 +1,177 @@
# llama.cpp for ET
- [Background](#background)
- [Limitations](#limitations)
- [Build](#build)
- [Develop](#develop)
- [Roadmap](#roadmap)
## Background
**ET** is a llama.cpp backend targeting the fully open source manycore
RISC-V accelerator platform [ET-SOC](https://github.com/aifoundry-org/et-man).
## Limitations
The ET backend runs several of the major OSS models with some limitations:
- Only limited set of operations is supported (check [../ops.md](../ops.md)
and [../ops/ET.csv](../ops/ET.csv)).
- Only `q8_0`, `q4_0` (and partially `fp16`, `q4_K`) quantization is supported.
- Only one llama.cpp instance can use device at the same time (current firmware
limitation).
- Limited (but working) MoE model support
As a result of the above, only select models can run fully on ET-SOC
(you can actually run any model llama.cpp supports, but some/most operations
will likely fallback to CPU backend).
Fully supported models:
- Qwen3 models (without MoE), e.g.
[ggml-org/Qwen3-0.6B-GGUF:q8_0](https://huggingface.co/ggml-org/Qwen3-0.6B-GGUF/blob/main/Qwen3-0.6B-Q8_0.gguf) or
[ggml-org/Qwen3-14B-GGUF:q8_0](https://huggingface.co/ggml-org/Qwen3-14B-GGUF/blob/main/Qwen3-14B-Q8_0.gguf).
- Llama3.2 (1B/3B), e.g.
[lmstudio-community/Llama-3.2-1B-Instruct-GGUF:q8_0](https://huggingface.co/lmstudio-community/Llama-3.2-1B-Instruct-GGUF/blob/main/Llama-3.2-1B-Instruct-Q8_0.gguf).
- SmolLM2, e.g.
[unsloth/SmolLM2-135M-Instruct-GGUF:q8_0](https://huggingface.co/unsloth/SmolLM2-135M-Instruct-GGUF/blob/main/SmolLM2-135M-Instruct-Q8_0.gguf)
- Llama 3.1 model family.
- RWKV v7 model family.
- TinyLLaMA
## Build
### I. Prerequisites
1. **Install custom RISC-V toolchain** - Follow instructions at:
[https://github.com/aifoundry-org/riscv-gnu-toolchain/tree/et/aifoundry](https://github.com/aifoundry-org/riscv-gnu-toolchain/tree/et/aifoundry)
2. **Install ET platform** - Follow instructions at:
[https://github.com/aifoundry-org/et-platform](https://github.com/aifoundry-org/et-platform)
Both should be installed to `/opt/et` (or set `ET_TOOLCHAIN` and `ET_PLATFORM`
environment variables accordingly).
```sh
# Set toolchain and ET platform path (/opt/et is default)
export ET_TOOLCHAIN=/opt/et
export ET_PLATFORM=/opt/et
```
### II. Build llama.cpp
Check out llama.cpp with ET backend (this should checkout `et` branch):
```sh
git clone https://github.com/aifoundry-org/llama.cpp
cd llama.cpp
```
Build:
```sh
cmake -B build -DGGML_ET=ON
cmake --build build --config Release
# Optionally:
# cmake --install build
```
Build targeting sysemu backend instead of physical hardware:
```sh
cmake -B build -DGGML_ET=ON -DGGML_ET_SYSEMU=ON
cmake --build build --config Release
```
### III. Run
Run llama.cpp binaries as usual. (Of course, please make sure you have the
ET-SOC device installed and kernel driver loaded).
```sh
llama-cli -m mymodel.gguf
# or
llama-server -hf ggml-org/Qwen3-8B-GGUF:q8_0
```
If you want to run llama.cpp binaries (e.g. `llama-cli`) inside docker
container, you should let it access device files:
```sh
docker run \
--device=/dev/et0_mgmt:/dev/et0_mgmt \
--device=/dev/et0_ops:/dev/et0_ops \
...
```
## Develop
Compute kernels are developed within `ggml/src/ggml-et/et-kernels` folder.
Build is performed using custom RISC-V GNU toolchain and is managed by cmake.
At the moment kernels are build as baremetal elf files, without
standard lib or any other dependencies. All the yummy parts are written
in inline assembler.
Most kernels are very naive with lots of low hanging fruits left:
> [!IMPORTANT]
> Several assembly instructions emmited by the compiler are not implemented
> in hardware and software emulation in firmware is not ready yet.
> Eventually firmware will transparently trap unimplemented instructions
> and will emulate them inside exception handler. Until then, kernel
> build process includes step that checks compiled kernels and fails if any unimplemented
> instructions are found. Problematic ones follow:
> `FDIV.PI`, `FDIVU.PI`, `FREMU.PI`, `FREM.PI`, `FDIV.S`, `FDIV.PS`, `FSQRT.S`, `FSQRT.PS`, `FRSQ.PS`, `FSIN.PS`
> and (long cast) `FCVT.S.L`, `FCVT.S.LU`, `FCVT.L.S`, `FCVT.LU.S`
> What this means, is that for now you should avoid doing any division involving floats,
> any trigonometry or casting longs into floats.
> Some workarounds are implemented in `math_fp.h` (`et_fdiv`, `et_powf` etc) and
> long casting (presuming longs are small enough to fit into 32bits) can be
> done via `int` like `a = (float)(int)(b)`.
> [!TIP]
> There are some slightly higher level helpers (abstracting more
> complex instructions like tensor extension or synchronization primitives)
> inside `et_platform`, directory `et-common-libs/include/etsoc/isa/`. It was
> originally developed for firmware needs and is not included into compute
> kernel build process. Feel free to take ideas/code from there or try linking
> it in.
Before commiting any changes to operations and/or kernels, don't forget
to update supported ops reports (instructions at `docs/ops.md`).
When logging is enabled (e.g. by setting `--log-file` cli param),
each compute kernel run outputs a line with
pipe-delimited key-value pairs containing kernel level performance infomation.
Line is prefixed with `ET_PERF`:
```
ET_PERF|op=MUL_MAT|kernel=mul_mat_f32_Q8_0xf32|duration_us=3112|tensor=Qcur-0|shape=[4096,2,1,1]|start_us=48437862009|end_us=48437865121|flops=67100672
ET_PERF|op=ROPE|kernel=rope_f32|duration_us=9266|tensor=Qcur-0|shape=[128,32,2,1]|start_us=48437865128|end_us=48437874394|mode=0x0|n_dims=128|freq_base=500000.00|freq_scale=1.00
```
Keys depend on the operation, but some are always present.
`flops` in this case counts effective floating point operations and not floating
point operations per second.
You can enable ET-SOC runtime level ET-SOC profiling by setting environment
variable `GGML_ET_PROFILE` to a path. Profiling/tracing results will be written
to `GGML_ET_PROFILE/et_runtime_trace.json` and `GGML_ET_PROFILE/kernel_map` on exit.
### Uberkernel
The in-knernel implementaiton of device dispatch/kernel fusion. The ET SDK has a non-trivial op-to-op gap. `Uberkernel` (name taken from the original Esperanto AI's compiler)
dispatches multiple already existing kernel implementations with device side synchronization. Due to the processor's design, there is no natural memory visibility
horizon between sub-kernel invocations. This makes uberkernel much more difficult to develop and debug. Currently Uberkerel is hidden begind the
`GGML_ET_UBERKERNEL` environment variable and is disabled by default. Setting it to 1 enables it and provides significant performance improvements but is only
validated for the LLaMA 3.2 model family and Qwen 3.5.
## Roadmap
As of writing the documentation the ET backend is capable of running most models and smaller ones at usable speed given the low power profile of the processor. We'd
address the following capabilities in the future:
* Enable Uberkernel for all models
* More oprtator support
* Better TTS model support
* Enable more quantization format support
+109 -109
View File
@@ -12,112 +12,112 @@ Legend:
- 🟡 Partially supported by this backend
- ❌ Not supported by this backend
| Operation | BLAS | CANN | CPU | CUDA | MTL | OpenCL | SYCL | Vulkan | WebGPU | ZenDNN | zDNN |
|-----------|------|------|------|------|------|------|------|------|------|------|------|
| ABS | ❌ | ✅ | ✅ | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| ACC | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | 🟡 | ✅ | ❌ | ❌ | ❌ |
| ADD | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| ADD1 | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
| ADD_ID | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| ARANGE | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
| ARGMAX | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| ARGSORT | ❌ | ✅ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| CEIL | ❌ | ❌ | ✅ | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| CLAMP | ❌ | ✅ | ✅ | ✅ | ✅ | 🟡 | ✅ | 🟡 | ✅ | ❌ | ❌ |
| COL2IM_1D | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
| CONCAT | ❌ | ✅ | ✅ | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| CONT | ❌ | 🟡 | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | 🟡 | ❌ | ❌ |
| 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 | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| DIAG_MASK_INF | ❌ | ✅ | ✅ | ✅ | ❌ | 🟡 | ✅ | ✅ | ❌ | ❌ | ❌ |
| DIV | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| 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 | ❌ | ✅ | ✅ | ✅ | 🟡 | ❌ | ✅ | 🟡 | ❌ | ❌ | ❌ |
| LOG | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| MEAN | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ |
| MUL | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| MUL_MAT | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 |
| MUL_MAT_HADAMARD | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
| MUL_MAT_ID | ❌ | 🟡 | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | 🟡 | 🟡 | ❌ |
| 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 | ❌ | ✅ | ✅ | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| SILU | ❌ | ✅ | ✅ | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| SILU_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
| SIN | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | 🟡 | ✅ | ❌ | ❌ |
| SOFTPLUS | ❌ | ❌ | ✅ | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SOFT_MAX | ❌ | 🟡 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SOFT_MAX_BACK | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | 🟡 | ✅ | ❌ | ❌ | ❌ |
| SOLVE_TRI | ❌ | ❌ | ✅ | 🟡 | ✅ | ❌ | 🟡 | ✅ | ✅ | ❌ | ❌ |
| SQR | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 🟡 | ✅ | ❌ | ❌ |
| SQRT | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 🟡 | ✅ | ❌ | ❌ |
| SSM_CONV | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SSM_SCAN | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ |
| STEP | ❌ | ✅ | ✅ | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SUB | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SUM | ❌ | 🟡 | ✅ | 🟡 | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | ❌ | ❌ |
| SUM_ROWS | ❌ | ✅ | ✅ | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | ❌ | ❌ |
| SWIGLU | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SWIGLU_OAI | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| TANH | ❌ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| TIMESTEP_EMBEDDING | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ |
| TOP_K | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ |
| TRI | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| TRUNC | ❌ | ❌ | ✅ | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| UPSCALE | ❌ | 🟡 | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| XIELU | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ |
| Operation | BLAS | CANN | CPU | CUDA | ET | MTL | OpenCL | SYCL | Vulkan | WebGPU | ZenDNN | zDNN |
|-----------|------|------|------|------|------|------|------|------|------|------|------|------|
| ABS | ❌ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| ACC | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ❌ | 🟡 | ✅ | ❌ | ❌ | ❌ |
| ADD | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| ADD1 | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
| ADD_ID | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| ARANGE | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
| ARGMAX | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| ARGSORT | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| CEIL | ❌ | ❌ | ✅ | 🟡 | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| CLAMP | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | 🟡 | ✅ | 🟡 | ✅ | ❌ | ❌ |
| COL2IM_1D | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
| CONCAT | ❌ | ✅ | ✅ | 🟡 | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| CONT | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | 🟡 | ✅ | ✅ | 🟡 | ❌ | ❌ |
| 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 | ❌ | ❌ | ✅ | ✅ | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| DIAG_MASK_INF | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | 🟡 | ✅ | ✅ | ❌ | ❌ | ❌ |
| DIV | ❌ | ✅ | ✅ | ✅ | ❌ | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| 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 | ❌ | ✅ | ✅ | ✅ | ❌ | 🟡 | ❌ | ✅ | 🟡 | ❌ | ❌ | ❌ |
| LOG | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| MEAN | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ |
| MUL | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| MUL_MAT | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 | 🟡 |
| MUL_MAT_HADAMARD | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
| MUL_MAT_ID | ❌ | 🟡 | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | 🟡 | ❌ |
| 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 | ❌ | ✅ | ✅ | 🟡 | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| SILU | ❌ | ✅ | ✅ | 🟡 | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| SILU_BACK | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
| SIN | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ❌ | ✅ | 🟡 | ✅ | ❌ | ❌ |
| SOFTPLUS | ❌ | ❌ | ✅ | 🟡 | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SOFT_MAX | ❌ | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SOFT_MAX_BACK | ❌ | ❌ | 🟡 | 🟡 | ❌ | ❌ | ❌ | 🟡 | ✅ | ❌ | ❌ | ❌ |
| SOLVE_TRI | ❌ | ❌ | ✅ | 🟡 | 🟡 | ✅ | ❌ | 🟡 | ✅ | ✅ | ❌ | ❌ |
| SQR | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | 🟡 | ✅ | ❌ | ❌ |
| SQRT | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ❌ | ❌ |
| SSM_CONV | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SSM_SCAN | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ |
| STEP | ❌ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SUB | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SUM | ❌ | 🟡 | ✅ | 🟡 | ❌ | 🟡 | ❌ | 🟡 | 🟡 | 🟡 | ❌ | ❌ |
| SUM_ROWS | ❌ | ✅ | ✅ | 🟡 | ❌ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ❌ | ❌ |
| SWIGLU | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| SWIGLU_OAI | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| TANH | ❌ | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| TIMESTEP_EMBEDDING | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ |
| TOP_K | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | 🟡 | 🟡 | ✅ | ❌ | ❌ |
| TRI | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| TRUNC | ❌ | ❌ | ✅ | 🟡 | 🟡 | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| UPSCALE | ❌ | 🟡 | ✅ | ✅ | ❌ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| XIELU | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ |
+16114
View File
File diff suppressed because it is too large Load Diff
+4 -2
View File
@@ -4,8 +4,8 @@ project("ggml" C CXX ASM)
### GGML Version
set(GGML_VERSION_MAJOR 0)
set(GGML_VERSION_MINOR 15)
set(GGML_VERSION_PATCH 3)
set(GGML_VERSION_MINOR 16)
set(GGML_VERSION_PATCH 0)
set(GGML_VERSION_BASE "${GGML_VERSION_MAJOR}.${GGML_VERSION_MINOR}.${GGML_VERSION_PATCH}")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
@@ -257,6 +257,8 @@ set (GGML_SYCL_DEVICE_ARCH "" CACHE STRING
"ggml: sycl device architecture")
option(GGML_OPENVINO "ggml: use OPENVINO" OFF)
option(GGML_ET "ggml: use ET backend" OFF)
option(GGML_ET_SYSEMU "ggml: use ET backend via sysemu" OFF)
option(GGML_OPENCL "ggml: use OpenCL" OFF)
option(GGML_OPENCL_PROFILING "ggml: use OpenCL profiling (increases overhead)" OFF)
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include "ggml.h"
#include "ggml-backend.h"
#ifdef __cplusplus
extern "C" {
#endif
#define GGML_ET_NAME "ET"
// backend API
GGML_BACKEND_API ggml_guid_t ggml_backend_et_guid(void);
GGML_BACKEND_API ggml_backend_t ggml_backend_et_init(size_t devidx);
GGML_BACKEND_API bool ggml_backend_is_et(ggml_backend_t backend);
GGML_BACKEND_API int ggml_backend_et_get_device_count(void);
GGML_BACKEND_API void ggml_backend_et_get_device_description(int devidx, char * description, size_t description_size);
GGML_BACKEND_API void ggml_backend_et_get_device_memory(int devidx, size_t * free, size_t * total);
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_et_buffer_type(size_t dev_num);
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_et_host_buffer_type(void);
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_et_reg(void);
#ifdef __cplusplus
}
#endif
+1
View File
@@ -473,6 +473,7 @@ endif()
ggml_add_backend(BLAS)
ggml_add_backend(CANN)
ggml_add_backend(CUDA)
ggml_add_backend(ET)
ggml_add_backend(HIP)
ggml_add_backend(METAL)
ggml_add_backend(MUSA)
+7
View File
@@ -86,6 +86,10 @@
#include "ggml-openvino.h"
#endif
#ifdef GGML_USE_ET
#include "ggml-et.h"
#endif
namespace fs = std::filesystem;
static std::string path_str(const fs::path & path) {
@@ -161,6 +165,9 @@ struct ggml_backend_registry {
#ifdef GGML_USE_OPENVINO
register_backend(ggml_backend_openvino_reg());
#endif
#ifdef GGML_USE_ET
register_backend(ggml_backend_et_reg());
#endif
#ifdef GGML_USE_CPU
register_backend(ggml_backend_cpu_reg());
#endif
+2 -2
View File
@@ -263,13 +263,13 @@ void ggml_vec_dot_q2_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const voi
const uint8x16_t raw16 = vcombine_u8(raw, raw);
// First 16 elements: replicate bytes 0-3, shift, mask, subtract 1
uint8x16_t bytes0 = vqtbl1q_u8(raw16, idx_lo);
uint8x16_t bytes0 = ggml_vqtbl1q_u8(raw16, idx_lo);
int8x16_t qv0 = vsubq_s8(
vreinterpretq_s8_u8(vandq_u8(vshlq_u8(bytes0, shifts), mask2)),
one);
// Second 16 elements: replicate bytes 4-7, shift, mask, subtract 1
uint8x16_t bytes1 = vqtbl1q_u8(raw16, idx_hi);
uint8x16_t bytes1 = ggml_vqtbl1q_u8(raw16, idx_hi);
int8x16_t qv1 = vsubq_s8(
vreinterpretq_s8_u8(vandq_u8(vshlq_u8(bytes1, shifts), mask2)),
one);
+24 -12
View File
@@ -7299,6 +7299,13 @@ struct ggml_conv_2d_dw_params {
int dilation_y;
};
static inline float ggml_conv_2d_dw_knl_f32(const char * data, int64_t i, ggml_type type) {
if (type == GGML_TYPE_F16) {
return GGML_FP16_TO_FP32(((const ggml_fp16_t *)data)[i]);
}
return ((const float *)data)[i];
}
static void ggml_compute_forward_conv_2d_dw_cwhn(
const ggml_compute_params * params,
const ggml_tensor * src,
@@ -7307,7 +7314,8 @@ static void ggml_compute_forward_conv_2d_dw_cwhn(
const ggml_conv_2d_dw_params & p) {
const int64_t c = p.channels;
const float * knl_data = (const float *)kernel->data;
const char * knl_data = (const char *)kernel->data;
const ggml_type knl_type = kernel->type;
const int64_t rows_total = p.dst_h * p.batch;
const int64_t rows_per_thread = (rows_total + params->nth - 1) / params->nth;
@@ -7315,13 +7323,16 @@ static void ggml_compute_forward_conv_2d_dw_cwhn(
const int64_t row_end = MIN(row_start + rows_per_thread, rows_total);
#ifdef GGML_SIMD
int64_t c_pkg_end = 0;
int64_t pkg_size = GGML_F32_EPR;
if (knl_type == GGML_TYPE_F32) {
#if defined(__ARM_FEATURE_SVE)
const int64_t pkg_size = svcntw();
pkg_size = svcntw();
#else
const int64_t pkg_size = GGML_F32_EPR;
pkg_size = GGML_F32_EPR;
#endif
const int64_t pkg_count = c / pkg_size;
const int64_t c_pkg_end = pkg_count * pkg_size;
c_pkg_end = (c / pkg_size) * pkg_size;
}
#else
const int64_t c_pkg_end = 0;
#endif
@@ -7335,7 +7346,6 @@ static void ggml_compute_forward_conv_2d_dw_cwhn(
const int64_t src_x_base = dst_x * p.stride_x - p.pad_x;
#ifdef GGML_SIMD
// Vectorized loop
for (int64_t c_i = 0; c_i < c_pkg_end; c_i += pkg_size) {
GGML_F32_VEC sum = GGML_F32_VEC_ZERO;
for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) {
@@ -7348,7 +7358,8 @@ static void ggml_compute_forward_conv_2d_dw_cwhn(
if (src_x < 0 || src_x >= p.src_w) {
continue;
}
GGML_F32_VEC k = GGML_F32_VEC_LOAD(knl_data + (knl_y * p.knl_w + knl_x) * c + c_i);
const float * kp = (const float *)knl_data + (knl_y * p.knl_w + knl_x) * c + c_i;
GGML_F32_VEC k = GGML_F32_VEC_LOAD(kp);
GGML_F32_VEC s = GGML_F32_VEC_LOAD(src_data + (src_y * p.src_w + src_x) * c + c_i);
sum = GGML_F32_VEC_FMA(sum, k, s);
}
@@ -7356,7 +7367,6 @@ static void ggml_compute_forward_conv_2d_dw_cwhn(
GGML_F32_VEC_STORE(dst_data + c_i, sum);
}
#endif
// Scalar loop
for (int64_t c_i = c_pkg_end; c_i < c; ++c_i) {
float sum = 0.0f;
for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) {
@@ -7369,7 +7379,7 @@ static void ggml_compute_forward_conv_2d_dw_cwhn(
if (src_x < 0 || src_x >= p.src_w) {
continue;
}
sum += knl_data[(knl_y * p.knl_w + knl_x) * c + c_i]
sum += ggml_conv_2d_dw_knl_f32(knl_data, (knl_y * p.knl_w + knl_x) * c + c_i, knl_type)
* src_data[(src_y * p.src_w + src_x) * c + c_i];
}
}
@@ -7390,9 +7400,11 @@ static void ggml_compute_forward_conv_2d_dw_whcn(
const int64_t per_thread = (n + params->nth - 1) / params->nth;
const int64_t start = params->ith * per_thread;
const int64_t end = MIN(start + per_thread, n);
const char * knl_base = (const char *)kernel->data;
const ggml_type knl_type = kernel->type;
for (int64_t i = start; i < end; ++i) {
const float * knl_data = (const float *)kernel->data + (i % p.channels) * p.knl_w * p.knl_h;
const int64_t knl_offset = (i % p.channels) * p.knl_w * p.knl_h;
const float * src_data = (const float *)src->data + i * p.src_w * p.src_h;
float * dst_data = (float *)dst->data + i * p.dst_w * p.dst_h;
@@ -7410,7 +7422,7 @@ static void ggml_compute_forward_conv_2d_dw_whcn(
if (src_x < 0 || src_x >= p.src_w) {
continue;
}
sum += knl_data[knl_y * p.knl_w + knl_x]
sum += ggml_conv_2d_dw_knl_f32(knl_base, knl_offset + knl_y * p.knl_w + knl_x, knl_type)
* src_data[src_y * p.src_w + src_x];
}
}
@@ -7442,13 +7454,13 @@ void ggml_compute_forward_conv_2d_dw(
p.dilation_x = dst->op_params[4];
p.dilation_y = dst->op_params[5];
GGML_ASSERT(kernel->type == GGML_TYPE_F32 || kernel->type == GGML_TYPE_F16);
GGML_ASSERT(kernel->ne[3] == p.channels);
GGML_ASSERT(dst->ne[3] == p.batch);
if (ggml_is_contiguous(src)) {
ggml_compute_forward_conv_2d_dw_whcn(params, src, kernel, dst, p);
} else if (ggml_is_contiguous_channels(src)) {
// kernel should also have channels most contiguous in memory
GGML_ASSERT(kernel->nb[0] >= kernel->nb[2] && kernel->nb[1] >= kernel->nb[0]);
ggml_compute_forward_conv_2d_dw_cwhn(params, src, kernel, dst, p);
} else {
+30 -4
View File
@@ -28,6 +28,20 @@ static __global__ void init_offsets(int * offsets, const int ncols, const int nr
#endif // STRIDED_ITERATOR_AVAILABLE
#ifdef GGML_CUDA_USE_CUB
// returns the suggested maximum number of rows to process during one argsort_f32_i32_cuda_cub() call
int argsort_f32_i32_cuda_cub_chunk_nrows(const size_t nb01, const int64_t nrows) {
// perform argsort in chunks up to approximately this size (currently 64MB)
// to avoid excessive temporary buffers memory usage
const int chunk_bytes = 1 << 26;
// calculate how many rows will fit in one chunk (must be at least one)
const int chunk_nrows = std::max((int) (chunk_bytes / nb01), 1);
// limit the resulting amount to total nrows
return std::min((int64_t) chunk_nrows, nrows);
}
void argsort_f32_i32_cuda_cub(ggml_cuda_pool & pool,
const float * x,
int * dst,
@@ -254,11 +268,23 @@ void ggml_cuda_op_argsort(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
const size_t shared_mem = ncols_pad * sizeof(int);
const size_t max_shared_mem = ggml_cuda_info().devices[ggml_cuda_get_device()].smpb;
if (shared_mem > max_shared_mem || ncols > 1024) {
ggml_cuda_pool & pool = ctx.pool();
argsort_f32_i32_cuda_cub(pool, src0_d, (int *) dst_d, ncols, nrows, order, stream);
} else {
// early return if we can use bitonic argsort
if (shared_mem <= max_shared_mem && ncols <= 1024) {
argsort_f32_i32_cuda_bitonic(src0_d, (int *) dst_d, ncols, nrows, order, stream);
return;
}
const int chunk_nrows = argsort_f32_i32_cuda_cub_chunk_nrows(src0->nb[1], nrows);
ggml_cuda_pool & pool = ctx.pool();
for (int64_t i = 0; i < nrows; i += chunk_nrows) {
int iter_nrows = std::min((int64_t) chunk_nrows, nrows - i);
argsort_f32_i32_cuda_cub(pool, src0_d, (int *) dst_d, ncols, iter_nrows, order, stream);
src0_d += ncols * iter_nrows;
dst_d += ncols * iter_nrows;
}
#else
argsort_f32_i32_cuda_bitonic(src0_d, (int *) dst_d, ncols, nrows, order, stream);
+1
View File
@@ -3,6 +3,7 @@
void ggml_cuda_op_argsort(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
#ifdef GGML_CUDA_USE_CUB
int argsort_f32_i32_cuda_cub_chunk_nrows(const size_t nb01, const int64_t nrows);
void argsort_f32_i32_cuda_cub(ggml_cuda_pool & pool,
const float * x,
int * dst,
+2
View File
@@ -4917,7 +4917,9 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
case GGML_OP_IM2COL:
case GGML_OP_IM2COL_3D:
case GGML_OP_CONV_2D:
return true;
case GGML_OP_CONV_2D_DW:
return op->src[0]->type == GGML_TYPE_F32;
case GGML_OP_CONV_TRANSPOSE_2D:
case GGML_OP_POOL_2D:
return true;
+23 -32
View File
@@ -549,8 +549,8 @@ static __global__ void mul_mat_vec_q(
[[maybe_unused]] float x_biases[ncols_dst] = { 0.0f };
[[maybe_unused]] float gate_biases[ncols_dst] = { 0.0f };
[[maybe_unused]] float x_scales;
[[maybe_unused]] float gate_scales;
[[maybe_unused]] float x_scales = 1.0f;
[[maybe_unused]] float gate_scales = 1.0f;
if constexpr (has_fusion) {
// 1. Hide latency by prefetching bias, gates and scales here
// 2. load only on threads that won't die after partial sum calculation
@@ -655,47 +655,38 @@ static __global__ void mul_mat_vec_q(
tmp_gate[j][i] = warp_reduce_sum<warp_size>(tmp_gate[j][i]);
}
}
}
if (threadIdx.x < rows_per_cuda_block && (rows_per_cuda_block == 1 || uint32_t(row0 + threadIdx.x) < stride_col_dst)) {
float result = tmp[j][threadIdx.x];
if constexpr (has_fusion) {
if constexpr (type == GGML_TYPE_NVFP4) {
if (use_scale) {
if (threadIdx.x == i && (rows_per_cuda_block == 1 || uint32_t(row0 + i) < stride_col_dst)) {
float result = tmp[j][i];
if constexpr (has_fusion) {
if constexpr (type == GGML_TYPE_NVFP4) {
result *= x_scales;
}
}
if (use_bias) {
result += x_biases[j];
}
if (use_gate) {
float gate_value = tmp_gate[j][threadIdx.x];
if constexpr (type == GGML_TYPE_NVFP4) {
if (use_gate_scale) {
if (use_gate) {
float gate_value = tmp_gate[j][i];
if constexpr (type == GGML_TYPE_NVFP4) {
gate_value *= gate_scales;
}
}
if (use_gate_bias) {
gate_value += gate_biases[j];
}
switch (active_glu) {
case GGML_GLU_OP_SWIGLU:
result *= ggml_cuda_op_silu_single(gate_value);
break;
case GGML_GLU_OP_GEGLU:
result *= ggml_cuda_op_gelu_single(gate_value);
break;
case GGML_GLU_OP_SWIGLU_OAI: {
result = ggml_cuda_op_swiglu_oai_single(gate_value, result);
break;
switch (active_glu) {
case GGML_GLU_OP_SWIGLU:
result *= ggml_cuda_op_silu_single(gate_value);
break;
case GGML_GLU_OP_GEGLU:
result *= ggml_cuda_op_gelu_single(gate_value);
break;
case GGML_GLU_OP_SWIGLU_OAI:
result = ggml_cuda_op_swiglu_oai_single(gate_value, result);
break;
default:
result = result * gate_value;
break;
}
default:
result = result * gate_value;
break;
}
}
dst[j*stride_col_dst + i] = result;
}
dst[j*stride_col_dst + threadIdx.x] = result;
}
}
+16 -7
View File
@@ -75,17 +75,26 @@ void ggml_cuda_op_top_k(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
const int ncols_pad = next_power_of_2(ncols);
const size_t shared_mem = ncols_pad * sizeof(int);
const size_t max_shared_mem = ggml_cuda_info().devices[ggml_cuda_get_device()].smpb;
const bool use_bitonic = shared_mem <= max_shared_mem && ncols <= 1024;
const int chunk_nrows = argsort_f32_i32_cuda_cub_chunk_nrows(src0->nb[1], nrows);
ggml_cuda_pool_alloc<int> temp_dst_alloc(pool, ncols * nrows);
ggml_cuda_pool_alloc<int> temp_dst_alloc(pool, ncols * chunk_nrows);
int * tmp_dst = temp_dst_alloc.get();
if (shared_mem > max_shared_mem || ncols > 1024) {
argsort_f32_i32_cuda_cub(pool, src0_d, tmp_dst, ncols, nrows, GGML_SORT_ORDER_DESC, stream);
} else {
argsort_f32_i32_cuda_bitonic(src0_d, tmp_dst, ncols, nrows, GGML_SORT_ORDER_DESC, stream);
for (int64_t i = 0; i < nrows; i += chunk_nrows) {
int iter_nrows = std::min((int64_t) chunk_nrows, nrows - i);
if (use_bitonic) {
argsort_f32_i32_cuda_bitonic(src0_d, tmp_dst, ncols, iter_nrows, GGML_SORT_ORDER_DESC, stream);
} else {
argsort_f32_i32_cuda_cub(pool, src0_d, tmp_dst, ncols, iter_nrows, GGML_SORT_ORDER_DESC, stream);
}
CUDA_CHECK(cudaMemcpy2DAsync(dst_d, k * sizeof(int), tmp_dst, ncols * sizeof(int), k * sizeof(int), iter_nrows,
cudaMemcpyDeviceToDevice, stream));
src0_d += ncols * iter_nrows;
dst_d += k * iter_nrows;
}
CUDA_CHECK(cudaMemcpy2DAsync(dst_d, k * sizeof(int), tmp_dst, ncols * sizeof(int), k * sizeof(int), nrows,
cudaMemcpyDeviceToDevice, stream));
#else // GGML_CUDA_USE_CUB
ggml_cuda_pool_alloc<int> temp_dst_alloc(pool, ncols * nrows);
int * tmp_dst = temp_dst_alloc.get();
+246
View File
@@ -0,0 +1,246 @@
message(STATUS "Using ET backend")
# Configure ET platform path
if (DEFINED ENV{ET_PLATFORM})
set(ET_PLATFORM_PATH $ENV{ET_PLATFORM})
else()
set(ET_PLATFORM_PATH "/opt/et")
endif()
# Use sysemu for ET backend if compiled with `-DGGML_ET_SYSEMU=ON`
if (GGML_ET_SYSEMU)
message(STATUS "Using ET backend with sysemu instead of hardware")
else()
message(STATUS "Using ET backend with hardware device")
endif()
# Add ET platform CMake modules and config files to search paths
list(APPEND CMAKE_PREFIX_PATH ${ET_PLATFORM_PATH}/lib/cmake)
list(APPEND CMAKE_MODULE_PATH ${ET_PLATFORM_PATH}/lib/cmake)
include(aifoundry-utils/ProjectFunctions)
message(STATUS "Using ET Platform at ${ET_PLATFORM_PATH}")
find_package(runtime REQUIRED)
# Kernel list
set(KERNELS
el_map_f32
flash_attn_ext_f32
glu_f32
scale_f32
mul_mat_f32
mul_mat_f32_matrix_engine
mul_mat_id_f32
mul_mat_id_Q4_0
mul_mat_id_Q8_0
mul_mat_Q8_0
mul_mat_Q4_0
mul_mat_Q4_0_matrix_engine
mul_mat_f16
mul_mat_f16_matrix_engine
rope_f32
unary_f32
sqr_f32
clamp_f32
sum_rows_f32
mean_f32
cumsum_f32
norm_f32
l2_norm_f32
group_norm_f32
rms_norm_f32
rms_norm_mul_f32
softmax_f32
im2col
get_rows_f32
concat_f32
repeat_f32
rwkv_wkv6_f32
rwkv_wkv7_f32
gated_delta_net_f32
cont_f32
cont_f16
cpy_f32_f16
flash_attn_ext_f16_me
set_rows_f32
set_f32
fill_f32
pad_f32
diag_f32
tri_f32
solve_tri_f32
ssm_conv_f32
ssm_scan_f32
conv_2d_f32_me
memops
uberkernel
)
# Kernels that we support dispatch form Uberkernel
set(UBERKERNEL_SUPPORTED_KERNELS
el_map_f32
# unary_f32
# cpy_f32_f16
# cont_f32
# get_rows_f32
concat_f32
cont_f16
cumsum_f32
diag_f32
fill_f32
flash_attn_ext_f16_me
flash_attn_ext_f32
gated_delta_net_f32
glu_f32
group_norm_f32
im2col
l2_norm_f32
mul_mat_f16
mul_mat_f16_matrix_engine
mul_mat_f32
mul_mat_f32_matrix_engine
mul_mat_id_f32
mul_mat_Q4_0
mul_mat_Q8_0
norm_f32
pad_f32
repeat_f32
rms_norm_f32
rms_norm_mul_f32
rope_f32
rwkv_wkv6_f32
rwkv_wkv7_f32
scale_f32
set_f32
set_rows_f32
softmax_f32
solve_tri_f32
sqr_f32
# ssm_conv_f32
ssm_scan_f32
sum_rows_f32
tri_f32
)
set(UBERKERNEL_MAP_HPP ${CMAKE_CURRENT_BINARY_DIR}/et-kernels/ggml-et-uberkernel-kernel-map.h)
set(UBERKERNEL_MAP_CPP ${CMAKE_CURRENT_BINARY_DIR}/et-kernels/ggml-et-uberkernel-kernel-map.cpp)
set(UBERKERNEL_KERNELS_SORTED ${UBERKERNEL_SUPPORTED_KERNELS})
list(SORT UBERKERNEL_KERNELS_SORTED)
set(UBERKERNEL_ENUM_ENTRIES "")
set(UBERKERNEL_MAP_ENTRIES "")
set(_uk_idx 1)
foreach(KERNEL ${UBERKERNEL_KERNELS_SORTED})
string(TOUPPER ${KERNEL} _uk_upper)
string(APPEND UBERKERNEL_ENUM_ENTRIES
" GGML_ET_UBERKERNEL_KERNEL_${_uk_upper} = ${_uk_idx},\n")
string(APPEND UBERKERNEL_MAP_ENTRIES
" {\"${KERNEL}\", GGML_ET_UBERKERNEL_KERNEL_${_uk_upper}},\n")
math(EXPR _uk_idx "${_uk_idx} + 1")
endforeach()
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/ggml-et-uberkernel-kernel-map.h.in
${UBERKERNEL_MAP_HPP}
@ONLY)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/ggml-et-uberkernel-kernel-map.cpp.in
${UBERKERNEL_MAP_CPP}
@ONLY)
add_custom_target(et-uberkernel-map
DEPENDS ${UBERKERNEL_MAP_HPP} ${UBERKERNEL_MAP_CPP}
)
# Build ET kernels (cross-compiled in subdirectory scope)
add_subdirectory(et-kernels)
# Embed kernels into C++ source
set(EMBED_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/cmake/embed_one_kernel.cmake)
set(EMBED_HPP ${CMAKE_CURRENT_BINARY_DIR}/et-kernels/ggml-et-kernels-embed.hpp)
set(EMBED_CPP ${CMAKE_CURRENT_BINARY_DIR}/et-kernels/ggml-et-kernels-embed.cpp)
set(EMBED_DIR ${CMAKE_CURRENT_BINARY_DIR}/et-kernels/embed)
file(MAKE_DIRECTORY ${EMBED_DIR})
set(EMBED_KERNEL_SOURCES)
set(EMBED_EXTERNS "")
set(EMBED_MAP_ENTRIES "")
foreach(KERNEL ${KERNELS})
set(ELF_PATH ${CMAKE_CURRENT_BINARY_DIR}/et-kernels/${KERNEL}.elf)
set(OUT_CPP ${EMBED_DIR}/${KERNEL}.cpp)
add_custom_command(
OUTPUT ${OUT_CPP}
COMMAND ${CMAKE_COMMAND}
-DELF_FILE=${ELF_PATH}
-DOUT_FILE=${OUT_CPP}
-DVAR_NAME=${KERNEL}
-P ${EMBED_SCRIPT}
DEPENDS ${KERNEL}.elf ${EMBED_SCRIPT}
COMMENT "Embedding ${KERNEL}.elf"
VERBATIM
)
list(APPEND EMBED_KERNEL_SOURCES ${OUT_CPP})
string(APPEND EMBED_EXTERNS
"extern unsigned char ${KERNEL}_data[];\n"
"extern const uint64_t ${KERNEL}_len;\n")
string(APPEND EMBED_MAP_ENTRIES
" {\"${KERNEL}\", {${KERNEL}_data, ${KERNEL}_len}},\n")
endforeach()
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/ggml-et-kernels-embed.hpp.in
${EMBED_HPP}
@ONLY)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/ggml-et-kernels-embed.cpp.in
${EMBED_CPP}
@ONLY)
add_custom_target(et-kernels-embed ALL
DEPENDS ${EMBED_KERNEL_SOURCES} ${EMBED_HPP} ${EMBED_CPP} et-uberkernel-map
)
ggml_add_backend_library(ggml-et
ggml-et.cpp
ggml-et-kernels.cpp
ggml-et-memops.cpp
ggml-et-ops.cpp
ggml-et-cpu-compare.cpp
)
# Mark generated files as such
set_source_files_properties(
${EMBED_CPP}
${EMBED_HPP}
${EMBED_KERNEL_SOURCES}
${CMAKE_CURRENT_BINARY_DIR}/et-kernels/ggml-et-uberkernel-kernel-map.cpp
${CMAKE_CURRENT_BINARY_DIR}/et-kernels/ggml-et-uberkernel-kernel-map.h
PROPERTIES GENERATED TRUE
)
# Add embedded kernel sources
target_sources(ggml-et PRIVATE
${EMBED_CPP}
${EMBED_HPP}
${EMBED_KERNEL_SOURCES}
${CMAKE_CURRENT_BINARY_DIR}/et-kernels/ggml-et-uberkernel-kernel-map.cpp
${CMAKE_CURRENT_BINARY_DIR}/et-kernels/ggml-et-uberkernel-kernel-map.h
)
# Include directory for embedded headers
target_include_directories(ggml-et PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/et-kernels)
target_link_libraries(ggml-et PRIVATE runtime::etrt_static deviceLayer::deviceLayer)
target_compile_definitions(ggml-et PRIVATE GGML_ET_UBERKERNEL_HOST_LOOKUP)
if (GGML_ET_SYSEMU)
target_compile_definitions(ggml-et PRIVATE GGML_ET_SYSEMU=1)
endif()
# Ensure kernels are built and embedded before the backend library
add_dependencies(ggml-et et-kernels-embed et-uberkernel-map)
@@ -0,0 +1,15 @@
# Inputs (via -D):
# ELF_FILE - path to source .elf
# OUT_FILE - path to output .cpp
# VAR_NAME - C symbol base name (kernel name)
file(READ "${ELF_FILE}" HEX HEX)
string(LENGTH "${HEX}" HEX_LEN)
math(EXPR SIZE "${HEX_LEN} / 2")
string(REGEX REPLACE "(..)" "0x\\1," BYTES "${HEX}")
file(WRITE "${OUT_FILE}"
"// Auto-generated by embed_one_kernel.cmake. Do not edit.\n"
"#include <cstdint>\n"
"unsigned char ${VAR_NAME}_data[${SIZE}] = { ${BYTES} };\n"
"extern const uint64_t ${VAR_NAME}_len = ${SIZE};\n")
@@ -0,0 +1,6 @@
// Auto-generated kernel embeddings. Do not edit.
#include "ggml-et-kernels-embed.hpp"
const std::unordered_map<std::string, std::pair<const unsigned char*, uint64_t>> ggml_et_embedded_kernels = {
@EMBED_MAP_ENTRIES@
};
@@ -0,0 +1,12 @@
// Auto-generated kernel embeddings. Do not edit.
#pragma once
#include <cstdint>
#include <unordered_map>
#include <string>
#include <utility>
@EMBED_EXTERNS@
// Kernel name -> (data, length) lookup map
extern const std::unordered_map<std::string, std::pair<const unsigned char*, uint64_t>> ggml_et_embedded_kernels;
@@ -0,0 +1,18 @@
// Auto-generated uberkernel kernel-id mapping. Do not edit.
#include "ggml-et-uberkernel-kernel-map.h"
#ifdef GGML_ET_UBERKERNEL_HOST_LOOKUP
#include <string>
#include <unordered_map>
uint16_t ggml_et_uberkernel_kernel_id_from_name(const char * kernel_name) {
if (kernel_name == nullptr) {
return GGML_ET_UBERKERNEL_KERNEL_INVALID;
}
static const std::unordered_map<std::string, uint16_t> kernel_id_map = {
@UBERKERNEL_MAP_ENTRIES@
};
auto it = kernel_id_map.find(std::string(kernel_name));
return it == kernel_id_map.end() ? GGML_ET_UBERKERNEL_KERNEL_INVALID : it->second;
}
#endif
@@ -0,0 +1,13 @@
// Auto-generated uberkernel kernel-id mapping. Do not edit.
#pragma once
#include <stdint.h>
enum ggml_et_uberkernel_kernel_id {
GGML_ET_UBERKERNEL_KERNEL_INVALID = 0,
@UBERKERNEL_ENUM_ENTRIES@
};
#ifdef GGML_ET_UBERKERNEL_HOST_LOOKUP
uint16_t ggml_et_uberkernel_kernel_id_from_name(const char * kernel_name);
#endif
+137
View File
@@ -0,0 +1,137 @@
# ggml-et: Device kernels (cross-compiled within the main build)
#
# The RISC-V toolchain is set up in-scope so these targets use the
# cross-compiler while the rest of the build uses the host compiler.
# This keeps kernels in compile_commands.json for full IDE support.
# --- RISC-V toolchain setup (scoped to this directory) ---
set(TOOLCHAIN_DIR ${ET_PLATFORM_PATH})
include(${ET_PLATFORM_PATH}/lib/cmake/riscv64-ec-toolchain.cmake)
set(CMAKE_ADDR2LINE "${TOOLCHAIN_DIR}/bin/riscv64-unknown-elf-addr2line")
set(CMAKE_LINKER_TYPE LLD)
# Ensure kernels are built in this directory even if a global output directory is set
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
message(STATUS "ET kernels using RISC-V toolchain at: ${TOOLCHAIN_DIR}")
# DeviceUtils provides the add_riscv_executable macro
list(APPEND CMAKE_MODULE_PATH "${ET_PLATFORM_PATH}/lib/cmake/cmake-modules")
list(APPEND CMAKE_PREFIX_PATH "${ET_PLATFORM_PATH}/lib/cmake")
include(DeviceUtils)
find_package(et-common-libs REQUIRED)
find_package(esperantoTrace REQUIRED)
# --- Kernel configuration ---
if(NOT DEFINED ADDRESS)
set(ADDRESS "0x8005801000")
message(STATUS "ADDRESS not specified, using default: ${ADDRESS}")
endif()
set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/src/linker.ld)
set(CHECK_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/scripts/check_unimplemented_instructions.sh)
# Track address changes to trigger relinking
set(ADDRESS_FILE ${CMAKE_CURRENT_BINARY_DIR}/et_address.txt)
file(CONFIGURE OUTPUT ${ADDRESS_FILE} CONTENT "${ADDRESS}" @ONLY)
# KERNELS defined in upper CMakeLists.txt
foreach(KERNEL ${KERNELS})
add_riscv_executable(${KERNEL})
target_sources(${KERNEL}.elf PRIVATE
src/${KERNEL}.c
src/crt.S
)
target_include_directories(${KERNEL}.elf PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/..
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/ggml/include
${CMAKE_SOURCE_DIR}/ggml/src
)
target_link_libraries(${KERNEL}.elf PRIVATE et-common-libs::cm-umode)
# C-only flags — must not apply to .S files
target_compile_options(${KERNEL}.elf PRIVATE
$<$<COMPILE_LANGUAGE:C>:-fno-zero-initialized-in-bss>
$<$<COMPILE_LANGUAGE:C>:-ffreestanding>
$<$<COMPILE_LANGUAGE:C>:-std=gnu99>
$<$<COMPILE_LANGUAGE:C>:-ffat-lto-objects>
$<$<COMPILE_LANGUAGE:C>:-mcmodel=medany>
$<$<COMPILE_LANGUAGE:C>:-mabi=lp64f>
$<$<COMPILE_LANGUAGE:C>:-march=rv64imf>
$<$<COMPILE_LANGUAGE:C>:-ffunction-sections>
$<$<COMPILE_LANGUAGE:C>:-fdata-sections>
$<$<COMPILE_LANGUAGE:C>:-O3>
$<$<COMPILE_LANGUAGE:C>:-g0>
$<$<COMPILE_LANGUAGE:C>:-nostdlib>
$<$<COMPILE_LANGUAGE:C>:-ffreestanding>
)
target_link_options(${KERNEL}.elf PRIVATE
-Wl,--defsym=BASE_ADDRESS=${ADDRESS}
-Wl,--entry=_start
)
# Append to LINK_DEPENDS (macro already sets it for the linker script)
set_property(TARGET ${KERNEL}.elf APPEND PROPERTY
LINK_DEPENDS "${ADDRESS_FILE}"
)
# Post-build: strip and check (fails build if check script fails)
add_custom_command(TARGET ${KERNEL}.elf POST_BUILD
COMMAND ${CMAKE_STRIP} --strip-debug $<TARGET_FILE:${KERNEL}.elf>
COMMAND ${CHECK_SCRIPT}
${CMAKE_OBJDUMP} ${CMAKE_ADDR2LINE} $<TARGET_FILE:${KERNEL}.elf>
DEPENDS ${CHECK_SCRIPT}
VERBATIM
)
endforeach()
add_dependencies(uberkernel.elf et-uberkernel-map)
# Each supported kernel is compiled in its own translation unit with
# -Dentry_point=<kernel>_entry
# so symbols and macros don't leak between kernels. The dispatcher
# (uberkernel.c) calls the renamed entries via extern declarations.
#
# HACK: we need to supresse _me kernels from setting up SCP themselves
set(_UBER_ME_KERNELS mul_mat_f16_matrix_engine mul_mat_f32_matrix_engine flash_attn_ext_f16_me)
foreach(UK_KERNEL ${UBERKERNEL_SUPPORTED_KERNELS})
set(_obj uber_${UK_KERNEL})
add_library(${_obj} OBJECT src/${UK_KERNEL}.c)
target_compile_definitions(${_obj} PRIVATE "entry_point=${UK_KERNEL}_entry" ET_UBERKERNEL)
target_include_directories(${_obj} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/..
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/ggml/include
${CMAKE_SOURCE_DIR}/ggml/src
)
target_link_libraries(${_obj} PRIVATE et-common-libs::cm-umode)
target_compile_options(${_obj} PRIVATE
$<$<COMPILE_LANGUAGE:C>:-fno-zero-initialized-in-bss>
$<$<COMPILE_LANGUAGE:C>:-ffreestanding>
$<$<COMPILE_LANGUAGE:C>:-std=gnu99>
$<$<COMPILE_LANGUAGE:C>:-ffat-lto-objects>
$<$<COMPILE_LANGUAGE:C>:-mcmodel=medany>
$<$<COMPILE_LANGUAGE:C>:-mabi=lp64f>
$<$<COMPILE_LANGUAGE:C>:-march=rv64imf>
$<$<COMPILE_LANGUAGE:C>:-ffunction-sections>
$<$<COMPILE_LANGUAGE:C>:-fdata-sections>
$<$<COMPILE_LANGUAGE:C>:-O3>
$<$<COMPILE_LANGUAGE:C>:-g0>
$<$<COMPILE_LANGUAGE:C>:-nostdlib>
)
# ME kernels: suppress setup_cache_scp() (called once by the dispatcher)
if(UK_KERNEL IN_LIST _UBER_ME_KERNELS)
target_compile_definitions(${_obj} PRIVATE UBERKERNEL_SUPPRESS_SCP_SETUP)
endif()
target_sources(uberkernel.elf PRIVATE $<TARGET_OBJECTS:${_obj}>)
endforeach()
# Print summary
message(STATUS "GGML ET Kernels configured:")
foreach(KERNEL ${KERNELS})
message(STATUS " - ${KERNEL}")
endforeach()
message(STATUS "Base address: ${ADDRESS}")
@@ -0,0 +1,36 @@
#!/bin/bash
OBJDUMP=$1
ADDR2LINE=$2
TARGET_DEBUG=$3
TARGET_ASM=${TARGET_DEBUG}.S
BAD_INST_FILE=${TARGET_DEBUG}-BAD-INST.log
# grep expression to find unimplemented instructions
UNIMPLEMENTED_EXPR="fdiv.s\\|fsqrt.s\\|fcvt.l.s\\|fcvt.lu.s\\|fcvt.s.l\\|fcvt.s.lu\\|fdiv.pi\\|fdivu.pi\\|fremu.pi\\|frem.pi\\|fdiv.ps\\|fsqrt.ps\\|frsq.ps\\|fsin.ps"
# dump assembly into .S file
${OBJDUMP} -lwdSC ${TARGET_DEBUG} > ${TARGET_ASM}
# check with grep for unimplemented instructions
# Note: The exit status is 0 if selected lines are found, and 1 if not found.
grep ${UNIMPLEMENTED_EXPR} ${TARGET_ASM} > /dev/null
ret=$?
if [ ${ret} -eq 0 ]
then
# unimplemented instructions are found
echo -e "BUILD ERROR: Executable file ${TARGET_DEBUG} contains unimplemented instructions. Please review the lines of code listed in ${BAD_INST_FILE}"
echo -e "\t For further details, please read paragraph 3.4 of the ETSoC-1 Programmer's Reference Manual (PRM)"
# addr2line
grep ${UNIMPLEMENTED_EXPR} ${TARGET_ASM} | cut -d: -f 1 | ${ADDR2LINE} -i -e ${TARGET_DEBUG} > ${BAD_INST_FILE}
grep ${UNIMPLEMENTED_EXPR} ${TARGET_ASM} >> ${BAD_INST_FILE}
echo "------------------------------------------------------------"
cat ${BAD_INST_FILE}
echo "------------------------------------------------------------"
exit 1
else
rm -f ${BAD_INST_FILE}
fi
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -euo pipefail
LOG="llama_bench_$(date +%Y%m%d_%H%M%S).log"
{
echo "===== START ====="
date
hostname
uname -a
echo "Command:"
echo "./build/bin/llama-bench -m ../../models/Llama-3.2-1B-Instruct-Q8_0.gguf -fa 0 -p 32,64,128,256,512 -n 32,64,128,256,512"
echo "================="
./build/bin/llama-bench \
-m ../../models/Llama-3.2-1B-Instruct-Q8_0.gguf \
-fa 0 \
-p 32,64,128,256,512 \
-n 32,64,128,256,512
echo "===== END ====="
date
} 2>&1 | tee "$LOG"
+997
View File
@@ -0,0 +1,997 @@
//******************************************************************************
// ET Vectorized Block Operations Library
// Provides optimized block-level operations using ET hardware vector instructions
//******************************************************************************
#ifndef BLOCK_OPS_H
# define BLOCK_OPS_H
# include "math_fp.h"
# include "quants.h"
# include <stdint.h>
//******************************************************************************
// Block Dot Product Operations
//******************************************************************************
inline void __attribute__((always_inline)) excl_mode(uint64_t val) {
__asm__ __volatile__("csrw 0x7d3, %[csr_enc]\n" : : [csr_enc] "r"(val) : "x31");
}
static inline float compute_block_dot_product_q4_0(const block_q4_0 * a_block, const float * b_col_start) {
// Set mask register to enable all 8 vector elements
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask)); // Save current mask
__asm__ volatile("mov.m.x m0, x0, 0xFF"); // Enable all 8 elements
// Use f10 as accumulator, init to 0
__asm__ volatile("fbci.ps f10, 0" ::: "f10");
static const int32_t gather_pattern[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
__asm__ volatile("flw.ps f31, %[gather]\n" : : [gather] "m"(*(const int32_t (*)[8]) gather_pattern) : "f31");
// Process 32 elements in 2 chunks of 16 elements (8 bytes) each
for (int chunk = 0; chunk < 2; chunk++) {
int offset_a = chunk * 8;
int offset_b_low = chunk * 8; // Activations for lower nibbles
int offset_b_high = chunk * 8 + 16; // Activations for upper nibbles (16 elements later)
__asm__ volatile(
"fgb.ps f11, f31(%[a_ptr])\n" // Gather 8 bytes (16 packed q4_0 weights)
// 1. Extract & Multiply Lower Nibbles
"fandi.pi f12, f11, 15\n" // Mask lower 4 bits (x & 0xF)
"faddi.pi f12, f12, -8\n" // GGML offset to signed: (x & 0xF) - 8
"fcvt.ps.pw f12, f12, rne\n" // Convert INT32 to FP32
"flw.ps f13, 0(%[b_low])\n" // Load 8 B values (floats)
"fmadd.ps f10, f12, f13, f10, rne\n" // acc += A_low * B_low
// 2. Extract & Multiply Upper Nibbles
"fsrli.pi f14, f11, 4\n" // Shift upper 4 bits down
"fandi.pi f14, f14, 15\n" // Mask new lower 4 bits
"faddi.pi f14, f14, -8\n" // GGML offset to signed
"fcvt.ps.pw f14, f14, rne\n" // Convert INT32 to FP32
"flw.ps f15, 0(%[b_high])\n" // Load next 8 B values (floats)
"fmadd.ps f10, f14, f15, f10, rne\n" // acc += A_high * B_high
:
: [a_ptr] "r"(&a_block->qs[offset_a]), [b_low] "r"(&b_col_start[offset_b_low]),
[b_high] "r"(&b_col_start[offset_b_high])
// Note: f10 is explicitly NOT listed in the clobbers here to ensure the compiler
// preserves the running sum across C loop iterations safely.
: "f11", "f12", "f13", "f14", "f15");
}
// Horizontal sum: reduce f10 into a single scalar
float final_sum;
__asm__ __volatile__(
// Pairwise sum within each 128-bit half
"fswizz.ps f1, f10, 0xB1 \n\t" // Swaps: e0<->e1 and e2<->e3
"fadd.ps f2, f10, f1, rne \n\t"
// Complete the sum for each 128-bit half
"fswizz.ps f3, f2, 0x4E \n\t" // Swaps: e0,e1 <-> e2,e3
"fadd.ps f4, f2, f3, rne \n\t"
// Sum across the two 128b halfs
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(final_sum)::"t0", "f1", "f2", "f3", "f4", "f5", "f10");
// Restore original mask
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
const float scale = fp16_to_fp32(a_block->d);
return final_sum * scale;
}
// Compute dot product between dequantized q8_0 block and f32 column vector
// Vectorized: processes 8 elements at a time using ET vector instructions
// Block size: 32 int8 values (QK8_0)
static inline float compute_block_dot_product_q8_0(const block_q8_0 * a_block, const float * b_col_start) {
// Set mask register to enable all 8 vector elements
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask)); // Save current mask
__asm__ volatile("mov.m.x m0, x0, 0xFF"); // Enable all 8 elements
__asm__ volatile("fbci.pi f10, 0" ::: "f10"); // Use f10 as accumulator, init to 0
static const int32_t gather_pattern[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
__asm__ volatile("flw.ps f31, %[gather]\n" : : [gather] "m"(*(const int32_t (*)[8]) gather_pattern) : "f31");
// Process 32 elements in 4 chunks of 8 elements each
for (int chunk = 0; chunk < 4; chunk++) {
int offset = chunk << 3; // chunk * 8
__asm__ volatile(
"flw.ps f12, %[b_vec]\n" // Load 8 B values (floats)
"fgb.ps f11, f31(%[a_ptr])\n" // Gather 8 int8 bytes from A using pattern
"fcvt.ps.pw f11, f11\n" // Convert int8 vector to float vector
"fmadd.ps f10, f11, f12, f10\n" // acc += a_vec * b_vec (8-wide)
:
: [a_ptr] "r"(&a_block->qs[offset]), [b_vec] "m"(*(const float (*)[8]) & b_col_start[offset]),
[scale] "m"(a_block->d)
: "f10", "f11", "f12");
}
// Horizontal sum: reduce f10 into a single scalar
float final_sum;
__asm__ __volatile__(
// Pairwise sum within each 128-bit half
"fswizz.ps f1, f10, 0xB1 \n\t" // Swaps: e0<->e1 and e2<->e3
"fadd.ps f2, f10, f1, rne \n\t"
// Complete the sum for each 128-bit half
"fswizz.ps f3, f2, 0x4E \n\t" // Swaps: e0,e1 <-> e2,e3
"fadd.ps f4, f2, f3, rne \n\t"
// Sum across the two 128b halfs
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(final_sum)::"t0", "f10", "f2", "f3", "f4", "f5");
// Restore original mask
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
const float scale = fp16_to_fp32(a_block->d);
return final_sum * scale;
}
//******************************************************************************
// Split-phase Q8_0 dot product API
//
// q8_dot_begin(st) — save mask, set mask 0xFF
// q8_dot_reset() — zero vector accumulator f20
// q8_dot_tile(q, b, n) — accumulate n Q8_0 blocks into f20
// q8_dot_reduce() — horizontal sum of f20, return scalar float
// q8_dot_teardown(st) — restore original mask
//
// Register contract:
// f20 — row accumulator (persistent across tiles, reset per row)
// f31 — gather pattern (reloaded per q8_dot_tile call)
// f10-f12 — scratch within tile
// f15 — scale broadcast within tile
// f1-f5, t0 — scratch within reduce
//******************************************************************************
static inline void __attribute__((always_inline)) q8_dot_reset(void) {
__asm__ volatile("fbci.pi f20, 0" ::: "f20");
}
// Accumulate n_blocks Q8_0 blocks into f20.
// Uses fg32b.ps (fast gather with scalar pattern) for aligned chunks,
// falls back to fgb.ps for chunks crossing a 32-byte boundary.
static inline void __attribute__((always_inline)) q8_dot_tile(const block_q8_0 * q_row,
const float * b_col,
int64_t n_blocks) {
const int32_t gather_pattern[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
const uint64_t gather_0_to_7 = 0x398a418820ULL;
__asm__ volatile("flw.ps f31, %[g]\n" : : [g] "m"(*(const int32_t (*)[8]) gather_pattern) : "f31");
for (int64_t kb = 0; kb < n_blocks; kb++) {
const block_q8_0 * blk = q_row + kb;
const float * b_ptr = b_col + (kb << 5);
const uintptr_t qs_addr = (uintptr_t) blk->qs;
const uintptr_t qs_aligned = qs_addr & ~(uintptr_t) 31;
const uintptr_t qs_low = qs_addr & 31;
const int fast_chunks = (int) ((32 - qs_low) >> 3);
if (fast_chunks >= 3) {
__asm__ volatile(
"fbci.pi f10, 0\n"
"flw.ps f12, %[bv0]\n"
"fg32b.ps f11, %[gi](%[ap0])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv1]\n"
"fg32b.ps f11, %[gi](%[ap1])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv2]\n"
"fg32b.ps f11, %[gi](%[ap2])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv3]\n"
"fgb.ps f11, f31(%[ap3])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
:
: [gi] "r"(gather_0_to_7), [ap0] "r"(qs_addr), [ap1] "r"(qs_aligned | ((qs_addr + 8) & 31)),
[ap2] "r"(qs_aligned | ((qs_addr + 16) & 31)), [ap3] "r"(&blk->qs[24]),
[bv0] "m"(*(const float (*)[8]) & b_ptr[0]), [bv1] "m"(*(const float (*)[8]) & b_ptr[8]),
[bv2] "m"(*(const float (*)[8]) & b_ptr[16]), [bv3] "m"(*(const float (*)[8]) & b_ptr[24])
: "f10", "f11", "f12");
} else if (fast_chunks == 2) {
__asm__ volatile(
"fbci.pi f10, 0\n"
"flw.ps f12, %[bv0]\n"
"fg32b.ps f11, %[gi](%[ap0])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv1]\n"
"fg32b.ps f11, %[gi](%[ap1])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv2]\n"
"fgb.ps f11, f31(%[ap2])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv3]\n"
"fgb.ps f11, f31(%[ap3])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
:
: [gi] "r"(gather_0_to_7), [ap0] "r"(qs_addr), [ap1] "r"(qs_aligned | ((qs_addr + 8) & 31)),
[ap2] "r"(&blk->qs[16]), [ap3] "r"(&blk->qs[24]), [bv0] "m"(*(const float (*)[8]) & b_ptr[0]),
[bv1] "m"(*(const float (*)[8]) & b_ptr[8]), [bv2] "m"(*(const float (*)[8]) & b_ptr[16]),
[bv3] "m"(*(const float (*)[8]) & b_ptr[24])
: "f10", "f11", "f12");
} else if (fast_chunks == 1) {
__asm__ volatile(
"fbci.pi f10, 0\n"
"flw.ps f12, %[bv0]\n"
"fg32b.ps f11, %[gi](%[ap0])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv1]\n"
"fgb.ps f11, f31(%[ap1])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv2]\n"
"fgb.ps f11, f31(%[ap2])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv3]\n"
"fgb.ps f11, f31(%[ap3])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
:
: [gi] "r"(gather_0_to_7), [ap0] "r"(qs_addr), [ap1] "r"(&blk->qs[8]), [ap2] "r"(&blk->qs[16]),
[ap3] "r"(&blk->qs[24]), [bv0] "m"(*(const float (*)[8]) & b_ptr[0]),
[bv1] "m"(*(const float (*)[8]) & b_ptr[8]), [bv2] "m"(*(const float (*)[8]) & b_ptr[16]),
[bv3] "m"(*(const float (*)[8]) & b_ptr[24])
: "f10", "f11", "f12");
} else {
__asm__ volatile(
"fbci.pi f10, 0\n"
"flw.ps f12, %[bv0]\n"
"fgb.ps f11, f31(%[ap0])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv1]\n"
"fgb.ps f11, f31(%[ap1])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv2]\n"
"fgb.ps f11, f31(%[ap2])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
"flw.ps f12, %[bv3]\n"
"fgb.ps f11, f31(%[ap3])\n"
"fcvt.ps.pw f11, f11\n"
"fmadd.ps f10, f11, f12, f10\n"
:
: [ap0] "r"(&blk->qs[0]), [ap1] "r"(&blk->qs[8]), [ap2] "r"(&blk->qs[16]), [ap3] "r"(&blk->qs[24]),
[bv0] "m"(*(const float (*)[8]) & b_ptr[0]), [bv1] "m"(*(const float (*)[8]) & b_ptr[8]),
[bv2] "m"(*(const float (*)[8]) & b_ptr[16]), [bv3] "m"(*(const float (*)[8]) & b_ptr[24])
: "f10", "f11", "f12");
}
// f20 += f10 * broadcast(scale) — hardware fp16→fp32 via FCVT.PS.F16
uint32_t scale_raw = (uint32_t) blk->d;
__asm__ volatile(
"fbcx.ps f15, %[sb]\n"
"fcvt.ps.f16 f15, f15\n"
"fmadd.ps f20, f10, f15, f20\n"
:
: [sb] "r"(scale_raw)
: "f15", "f20");
}
}
// Horizontal sum of 8-element vector accumulator f20.
static inline float __attribute__((always_inline)) q8_dot_reduce(void) {
float result;
__asm__ __volatile__(
"fswizz.ps f1, f20, 0xB1 \n\t"
"fadd.ps f2, f20, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(result)::"t0", "f1", "f2", "f3", "f4", "f5");
return result;
}
// Full-row dot product (convenience wrapper)
static inline float compute_row_dot_q8_0(const block_q8_0 * q_row, const float * b_col, int64_t K_blocks) {
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
q8_dot_reset();
q8_dot_tile(q_row, b_col, K_blocks);
float result = q8_dot_reduce();
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
return result;
}
//******************************************************************************
// Hoisted Q8_0 dot API
//
// q8_dot_begin/end save/restore the vector mask once around a long sequence of
// dot products, so the per-row mask shuffles are hoisted out of the inner
// loops. q8_dot_compute does a full-row dot (no mask handling). The _x2
// variant computes two rows together while reusing each loaded B chunk —
// only safe when both row pointers share the same 32-byte alignment phase
// (i.e. the Q8 row stride is a multiple of 32).
//******************************************************************************
typedef struct {
unsigned long saved_mask;
} q8_dot_state;
static inline void q8_dot_begin(q8_dot_state * state) {
__asm__ volatile("mova.x.m %0" : "=r"(state->saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
}
static inline void q8_dot_end(const q8_dot_state * state) {
__asm__ volatile("mova.m.x %0" ::"r"(state->saved_mask));
}
// Equivalent to q8_dot_reset+tile+reduce, without touching the mask register.
// Caller is responsible for q8_dot_begin/end around the surrounding loop.
static inline float q8_dot_compute(const block_q8_0 * q_row, const float * b_col, int64_t K_blocks) {
q8_dot_reset();
q8_dot_tile(q_row, b_col, K_blocks);
return q8_dot_reduce();
}
// Compute two row dots together while reusing the same loaded B chunks.
//
// Safe when every row starts at the same 32-byte offset, i.e. the Q8 row stride
// is a multiple of 32. In that case the gather/alignment pattern is the same
// for both rows at a given `kb`, so one set of B vector loads feeds both row
// accumulators.
static inline void q8_dot_compute_x2_aligned(const block_q8_0 * q_row0,
const block_q8_0 * q_row1,
const float * b_col,
int64_t K_blocks,
float * out0,
float * out1) {
const int32_t gather_pattern[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
const uint64_t gather_0_to_7 = 0x398a418820ULL;
__asm__ volatile("flw.ps f31, %[g]\n" : : [g] "m"(*(const int32_t (*)[8]) gather_pattern) : "f31");
__asm__ volatile(
"fbci.pi f20, 0\n"
"fbci.pi f21, 0\n" ::
: "f20", "f21");
for (int64_t kb = 0; kb < K_blocks; kb++) {
const block_q8_0 * blk0 = q_row0 + kb;
const block_q8_0 * blk1 = q_row1 + kb;
const float * b_ptr = b_col + (kb << 5);
const uintptr_t qs_addr0 = (uintptr_t) blk0->qs;
const uintptr_t qs_addr1 = (uintptr_t) blk1->qs;
const uintptr_t qs_aligned0 = qs_addr0 & ~(uintptr_t) 31;
const uintptr_t qs_aligned1 = qs_addr1 & ~(uintptr_t) 31;
const int fast_chunks = (int) ((32 - (qs_addr0 & 31)) >> 3);
if (fast_chunks >= 3) {
__asm__ volatile(
"fbci.pi f10, 0\n"
"fbci.pi f11, 0\n"
"flw.ps f12, %[bv0]\n"
"fg32b.ps f16, %[gi](%[r0ap0])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f12, f10\n"
"fg32b.ps f17, %[gi](%[r1ap0])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f12, f11\n"
"flw.ps f13, %[bv1]\n"
"fg32b.ps f16, %[gi](%[r0ap1])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f13, f10\n"
"fg32b.ps f17, %[gi](%[r1ap1])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f13, f11\n"
"flw.ps f14, %[bv2]\n"
"fg32b.ps f16, %[gi](%[r0ap2])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f14, f10\n"
"fg32b.ps f17, %[gi](%[r1ap2])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f14, f11\n"
"flw.ps f15, %[bv3]\n"
"fgb.ps f16, f31(%[r0ap3])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f15, f10\n"
"fgb.ps f17, f31(%[r1ap3])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f15, f11\n"
:
: [gi] "r"(gather_0_to_7), [r0ap0] "r"(qs_addr0), [r0ap1] "r"(qs_aligned0 | ((qs_addr0 + 8) & 31)),
[r0ap2] "r"(qs_aligned0 | ((qs_addr0 + 16) & 31)), [r0ap3] "r"(&blk0->qs[24]), [r1ap0] "r"(qs_addr1),
[r1ap1] "r"(qs_aligned1 | ((qs_addr1 + 8) & 31)), [r1ap2] "r"(qs_aligned1 | ((qs_addr1 + 16) & 31)),
[r1ap3] "r"(&blk1->qs[24]), [bv0] "m"(*(const float (*)[8]) & b_ptr[0]),
[bv1] "m"(*(const float (*)[8]) & b_ptr[8]), [bv2] "m"(*(const float (*)[8]) & b_ptr[16]),
[bv3] "m"(*(const float (*)[8]) & b_ptr[24])
: "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17");
} else if (fast_chunks == 2) {
__asm__ volatile(
"fbci.pi f10, 0\n"
"fbci.pi f11, 0\n"
"flw.ps f12, %[bv0]\n"
"fg32b.ps f16, %[gi](%[r0ap0])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f12, f10\n"
"fg32b.ps f17, %[gi](%[r1ap0])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f12, f11\n"
"flw.ps f13, %[bv1]\n"
"fg32b.ps f16, %[gi](%[r0ap1])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f13, f10\n"
"fg32b.ps f17, %[gi](%[r1ap1])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f13, f11\n"
"flw.ps f14, %[bv2]\n"
"fgb.ps f16, f31(%[r0ap2])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f14, f10\n"
"fgb.ps f17, f31(%[r1ap2])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f14, f11\n"
"flw.ps f15, %[bv3]\n"
"fgb.ps f16, f31(%[r0ap3])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f15, f10\n"
"fgb.ps f17, f31(%[r1ap3])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f15, f11\n"
:
: [gi] "r"(gather_0_to_7), [r0ap0] "r"(qs_addr0), [r0ap1] "r"(qs_aligned0 | ((qs_addr0 + 8) & 31)),
[r0ap2] "r"(&blk0->qs[16]), [r0ap3] "r"(&blk0->qs[24]), [r1ap0] "r"(qs_addr1),
[r1ap1] "r"(qs_aligned1 | ((qs_addr1 + 8) & 31)), [r1ap2] "r"(&blk1->qs[16]),
[r1ap3] "r"(&blk1->qs[24]), [bv0] "m"(*(const float (*)[8]) & b_ptr[0]),
[bv1] "m"(*(const float (*)[8]) & b_ptr[8]), [bv2] "m"(*(const float (*)[8]) & b_ptr[16]),
[bv3] "m"(*(const float (*)[8]) & b_ptr[24])
: "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17");
} else if (fast_chunks == 1) {
__asm__ volatile(
"fbci.pi f10, 0\n"
"fbci.pi f11, 0\n"
"flw.ps f12, %[bv0]\n"
"fg32b.ps f16, %[gi](%[r0ap0])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f12, f10\n"
"fg32b.ps f17, %[gi](%[r1ap0])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f12, f11\n"
"flw.ps f13, %[bv1]\n"
"fgb.ps f16, f31(%[r0ap1])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f13, f10\n"
"fgb.ps f17, f31(%[r1ap1])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f13, f11\n"
"flw.ps f14, %[bv2]\n"
"fgb.ps f16, f31(%[r0ap2])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f14, f10\n"
"fgb.ps f17, f31(%[r1ap2])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f14, f11\n"
"flw.ps f15, %[bv3]\n"
"fgb.ps f16, f31(%[r0ap3])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f15, f10\n"
"fgb.ps f17, f31(%[r1ap3])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f15, f11\n"
:
: [gi] "r"(gather_0_to_7), [r0ap0] "r"(qs_addr0), [r0ap1] "r"(&blk0->qs[8]), [r0ap2] "r"(&blk0->qs[16]),
[r0ap3] "r"(&blk0->qs[24]), [r1ap0] "r"(qs_addr1), [r1ap1] "r"(&blk1->qs[8]),
[r1ap2] "r"(&blk1->qs[16]), [r1ap3] "r"(&blk1->qs[24]), [bv0] "m"(*(const float (*)[8]) & b_ptr[0]),
[bv1] "m"(*(const float (*)[8]) & b_ptr[8]), [bv2] "m"(*(const float (*)[8]) & b_ptr[16]),
[bv3] "m"(*(const float (*)[8]) & b_ptr[24])
: "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17");
} else {
__asm__ volatile(
"fbci.pi f10, 0\n"
"fbci.pi f11, 0\n"
"flw.ps f12, %[bv0]\n"
"fgb.ps f16, f31(%[r0ap0])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f12, f10\n"
"fgb.ps f17, f31(%[r1ap0])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f12, f11\n"
"flw.ps f13, %[bv1]\n"
"fgb.ps f16, f31(%[r0ap1])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f13, f10\n"
"fgb.ps f17, f31(%[r1ap1])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f13, f11\n"
"flw.ps f14, %[bv2]\n"
"fgb.ps f16, f31(%[r0ap2])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f14, f10\n"
"fgb.ps f17, f31(%[r1ap2])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f14, f11\n"
"flw.ps f15, %[bv3]\n"
"fgb.ps f16, f31(%[r0ap3])\n"
"fcvt.ps.pw f16, f16\n"
"fmadd.ps f10, f16, f15, f10\n"
"fgb.ps f17, f31(%[r1ap3])\n"
"fcvt.ps.pw f17, f17\n"
"fmadd.ps f11, f17, f15, f11\n"
:
: [r0ap0] "r"(&blk0->qs[0]), [r0ap1] "r"(&blk0->qs[8]), [r0ap2] "r"(&blk0->qs[16]),
[r0ap3] "r"(&blk0->qs[24]), [r1ap0] "r"(&blk1->qs[0]), [r1ap1] "r"(&blk1->qs[8]),
[r1ap2] "r"(&blk1->qs[16]), [r1ap3] "r"(&blk1->qs[24]), [bv0] "m"(*(const float (*)[8]) & b_ptr[0]),
[bv1] "m"(*(const float (*)[8]) & b_ptr[8]), [bv2] "m"(*(const float (*)[8]) & b_ptr[16]),
[bv3] "m"(*(const float (*)[8]) & b_ptr[24])
: "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17");
}
const uint32_t scale_raw0 = (uint32_t) blk0->d;
const uint32_t scale_raw1 = (uint32_t) blk1->d;
__asm__ volatile(
"fbcx.ps f24, %[s0]\n"
"fcvt.ps.f16 f24, f24\n"
"fmadd.ps f20, f10, f24, f20\n"
"fbcx.ps f25, %[s1]\n"
"fcvt.ps.f16 f25, f25\n"
"fmadd.ps f21, f11, f25, f21\n"
:
: [s0] "r"(scale_raw0), [s1] "r"(scale_raw1)
: "f20", "f21", "f24", "f25");
}
float result0;
float result1;
__asm__ __volatile__(
"fswizz.ps f1, f20, 0xB1 \n\t"
"fadd.ps f2, f20, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(result0)::"t0", "f1", "f2", "f3", "f4", "f5");
__asm__ __volatile__(
"fswizz.ps f1, f21, 0xB1 \n\t"
"fadd.ps f2, f21, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(result1)::"t0", "f1", "f2", "f3", "f4", "f5");
*out0 = result0;
*out1 = result1;
}
// Compute dot product between f16 block and f32 column vector (NAIVE VERSION)
// Scalar implementation for debugging - no vectorization
// Block size: 32 f16 values (64 bytes = 1 cache line)
static inline float compute_block_dot_product_f16_naive(const uint16_t * a_block, const float * b_col_start) {
float acc_vec[8] __attribute__((aligned(32))) = { 0.0f };
// Byte offsets for 16-bit (half-word) elements
static const int32_t gather_pattern[8] = { 0, 2, 4, 6, 8, 10, 12, 14 };
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
// Load the pattern once into f31 for the duration of all 4 chunks
__asm__ volatile("flw.ps f31, %[gather]\n" : : [gather] "m"(*(const int32_t (*)[8]) gather_pattern) : "f31");
for (int chunk = 0; chunk < 4; chunk++) {
// Correct pointers:
// a_block elements are 2 bytes, b_col elements are 4 bytes
const uint16_t * a_ptr = &a_block[chunk << 3]; // chunk * 8
const float * b_ptr = &b_col_start[chunk << 3]; // chunk * 8
__asm__ volatile(
"flw.ps f10, %[acc]\n"
"fgh.ps f11, f31(%[a_p])\n" // Uses {0,2,4,6,8,10,12,14} byte offsets
"fcvt.ps.f16 f11, f11\n"
"flw.ps f12, (%[b_p])\n" // Standard vector load (32-bit floats)
"fmadd.ps f10, f11, f12, f10\n"
"fsw.ps f10, %[result]\n"
: [result] "=m"(*(float (*)[8]) acc_vec)
: [acc] "m"(*(const float (*)[8]) acc_vec), [a_p] "r"(a_ptr), [b_p] "r"(b_ptr)
: "f10", "f11", "f12");
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
return acc_vec[0] + acc_vec[1] + acc_vec[2] + acc_vec[3] + acc_vec[4] + acc_vec[5] + acc_vec[6] + acc_vec[7];
}
// Compute dot product between f16 block and f32 column vector
// SCALAR implementation for partial blocks
// Block size: up to 32 f16 values (can handle partial blocks for misaligned K)
static inline float compute_block_dot_product_f16_partial(const uint16_t * a_block,
const float * b_col_start,
int elements) {
// This matches compute_block_dot_product_f16_naive behavior
float sum = 0.0f;
for (int i = 0; i < elements; i++) {
float a_val = fp16_to_fp32(a_block[i]);
float b_val = b_col_start[i];
sum += a_val * b_val;
}
return sum;
}
// Compute dot product between f16 block and f16 column vector
// Scalar implementation for generic non-matrix-engine fallback paths.
static inline float compute_block_dot_product_f16_f16_partial(const uint16_t * a_block,
const uint16_t * b_col_start,
int elements) {
float sum = 0.0f;
for (int i = 0; i < elements; i++) {
sum += fp16_to_fp32(a_block[i]) * fp16_to_fp32(b_col_start[i]);
}
return sum;
}
// Compute dot product between f16 block and f32 column vector
// Vectorized: processes 8 elements at a time using ET vector instructions
// Block size: 32 f16 values (64 bytes = 1 cache line)
static inline float compute_block_dot_product_f16(const uint16_t * a_block, const float * b_col_start) {
return compute_block_dot_product_f16_partial(a_block, b_col_start, QK_F16);
}
// Compute dot product between f32 block and f32 column vector
// Vectorized: processes 8 elements at a time using ET vector instructions
// Block size: up to 16 f32 values (can handle partial blocks for misaligned K)
static inline float compute_block_dot_product_f32_partial(const float * a_block,
const float * b_col_start,
int elements) {
float acc_vec[8] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; // Accumulator vector
// Calculate how many full 8-element chunks we can process
int vec_end = (elements / 8) * 8;
if (vec_end > 0) {
// Set mask register to enable all 8 vector elements
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask)); // Save current mask
__asm__ volatile("mov.m.x m0, x0, 0xFF"); // Enable all 8 elements
// Process full 8-element chunks
for (int i = 0; i < vec_end; i += 8) {
// Vectorized f32 multiply-accumulate
__asm__ volatile(
"flw.ps f10, %[acc]\n" // Load current accumulator (8 floats)
"flw.ps f11, %[a_vec]\n" // Load 8 A values (f32)
"flw.ps f12, %[b_vec]\n" // Load 8 B values (f32)
"fmadd.ps f10, f11, f12, f10\n" // acc += a_vec * b_vec (8-wide)
"fsw.ps f10, %[result]\n" // Store back to accumulator
: [result] "=m"(*(float (*)[8]) acc_vec)
: [acc] "m"(*(const float (*)[8]) acc_vec), [a_vec] "m"(*(const float (*)[8])(a_block + i)),
[b_vec] "m"(*(const float (*)[8])(b_col_start + i))
: "f10", "f11", "f12");
}
// Restore original mask
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
}
// Horizontal sum: reduce 8 accumulator elements to single scalar
float final_sum = 0.0f;
for (int i = 0; i < 8; i++) {
final_sum += acc_vec[i];
}
// Handle remaining elements (< 8) with scalar operations
for (int i = vec_end; i < elements; i++) {
final_sum += a_block[i] * b_col_start[i];
}
return final_sum;
}
// Compute dot product between f32 block and f16 column vector
// Scalar implementation for generic non-matrix-engine fallback paths.
static inline float compute_block_dot_product_f32_f16_partial(const float * a_block,
const uint16_t * b_col_start,
int elements) {
float sum = 0.0f;
for (int i = 0; i < elements; i++) {
sum += a_block[i] * fp16_to_fp32(b_col_start[i]);
}
return sum;
}
// Compute dot product between f32 block and f32 column vector
// Vectorized: processes 8 elements at a time using ET vector instructions
// Block size: 16 f32 values (64 bytes = 1 cache line)
static inline float compute_block_dot_product_f32(const float * a_block, const float * b_col_start) {
return compute_block_dot_product_f32_partial(a_block, b_col_start, QK_F32);
// float acc_vec[8];
// unsigned long old_mask;
// __asm__ volatile(
// // Save current mask
// "mova.x.m %[old_mask]\n"
// // Enable all 8 lanes
// "mov.m.x m0, x0, 0xFF\n"
// "flw.ps f11, %[a]\n"
// "flw.ps f12, %[b]\n"
// "fmadd.ps f10, f11, f12, f10\n"
// "fsw.ps f10, %[out]\n"
// "mova.m.x %[old_mask]\n"
// : [out] "=m" (*(float(*)[8])acc_vec),
// [old_mask] "=r"(old_mask)
// : [a] "m" (*(const float(*)[8])a_block),
// [b] "m" (*(const float(*)[8])b_col_start)
// : "f10", "f11", "f12"
// );
// // Horizontal reduction
// return acc_vec[0] + acc_vec[1] + acc_vec[2] + acc_vec[3] +
// acc_vec[4] + acc_vec[5] + acc_vec[6] + acc_vec[7];
}
#endif // BLOCK_OPS_H
static inline void __attribute__((always_inline)) q4_dot_reset(void) {
__asm__ volatile("fbci.pi f20, 0" ::: "f20");
}
static inline void __attribute__((always_inline)) q4_dot_tile(const block_q4_0 * q_row,
const float * b_col,
int64_t n_blocks) {
const int32_t gather_pattern[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
__asm__ volatile("flw.ps f31, %[g]\n" : : [g] "m"(*(const int32_t (*)[8]) gather_pattern) : "f31");
for (int64_t kb = 0; kb < n_blocks; kb++) {
const block_q4_0 * blk = q_row + kb;
const float * b_ptr = b_col + (kb << 5);
__asm__ volatile(
"fbci.pi f10, 0\n"
"fgb.ps f11, f31(%[a_ptr0])\n"
"fandi.pi f12, f11, 15\n"
"faddi.pi f12, f12, -8\n"
"fcvt.ps.pw f12, f12, rne\n"
"flw.ps f13, %[b_low0]\n"
"fmadd.ps f10, f12, f13, f10, rne\n"
"fsrli.pi f14, f11, 4\n"
"fandi.pi f14, f14, 15\n"
"faddi.pi f14, f14, -8\n"
"fcvt.ps.pw f14, f14, rne\n"
"flw.ps f15, %[b_high0]\n"
"fmadd.ps f10, f14, f15, f10, rne\n"
"fgb.ps f11, f31(%[a_ptr1])\n"
"fandi.pi f12, f11, 15\n"
"faddi.pi f12, f12, -8\n"
"fcvt.ps.pw f12, f12, rne\n"
"flw.ps f13, %[b_low1]\n"
"fmadd.ps f10, f12, f13, f10, rne\n"
"fsrli.pi f14, f11, 4\n"
"fandi.pi f14, f14, 15\n"
"faddi.pi f14, f14, -8\n"
"fcvt.ps.pw f14, f14, rne\n"
"flw.ps f15, %[b_high1]\n"
"fmadd.ps f10, f14, f15, f10, rne\n"
:
: [a_ptr0] "r"(&blk->qs[0]), [b_low0] "m"(*(const float (*)[8]) & b_ptr[0]),
[b_high0] "m"(*(const float (*)[8]) & b_ptr[16]), [a_ptr1] "r"(&blk->qs[8]),
[b_low1] "m"(*(const float (*)[8]) & b_ptr[8]), [b_high1] "m"(*(const float (*)[8]) & b_ptr[24])
: "f10", "f11", "f12", "f13", "f14", "f15");
uint32_t scale_raw = (uint32_t) blk->d;
__asm__ volatile(
"fbcx.ps f15, %[sb]\n"
"fcvt.ps.f16 f15, f15\n"
"fmadd.ps f20, f10, f15, f20\n"
:
: [sb] "r"(scale_raw)
: "f15", "f20");
}
}
static inline float __attribute__((always_inline)) q4_dot_reduce(void) {
float result;
__asm__ __volatile__(
"fswizz.ps f1, f20, 0xB1 \n\t"
"fadd.ps f2, f20, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(result)::"t0", "f1", "f2", "f3", "f4", "f5");
return result;
}
static inline float compute_row_dot_q4_0(const block_q4_0 * q_row, const float * b_col, int64_t K_blocks) {
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
q4_dot_reset();
q4_dot_tile(q_row, b_col, K_blocks);
float result = q4_dot_reduce();
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
return result;
}
typedef struct {
unsigned long saved_mask;
} q4_dot_state;
static inline void q4_dot_begin(q4_dot_state * state) {
__asm__ volatile("mova.x.m %0" : "=r"(state->saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
}
static inline void q4_dot_end(const q4_dot_state * state) {
__asm__ volatile("mova.m.x %0" ::"r"(state->saved_mask));
}
static inline float q4_dot_compute(const block_q4_0 * q_row, const float * b_col, int64_t K_blocks) {
q4_dot_reset();
q4_dot_tile(q_row, b_col, K_blocks);
return q4_dot_reduce();
}
static inline void q4_dot_compute_x2_aligned(const block_q4_0 * q_row0,
const block_q4_0 * q_row1,
const float * b_col,
int64_t K_blocks,
float * out0,
float * out1) {
const int32_t gather_pattern[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
__asm__ volatile("flw.ps f31, %[g]\n" : : [g] "m"(*(const int32_t (*)[8]) gather_pattern) : "f31");
__asm__ volatile(
"fbci.pi f20, 0\n"
"fbci.pi f21, 0\n" ::
: "f20", "f21");
for (int64_t kb = 0; kb < K_blocks; kb++) {
const block_q4_0 * blk0 = q_row0 + kb;
const block_q4_0 * blk1 = q_row1 + kb;
const float * b_ptr = b_col + (kb << 5);
__asm__ volatile(
"fbci.pi f10, 0\n"
"fbci.pi f16, 0\n"
"flw.ps f13, %[b_low0]\n"
"flw.ps f15, %[b_high0]\n"
"fgb.ps f11, f31(%[a_ptr0_0])\n"
"fgb.ps f17, f31(%[a_ptr1_0])\n"
"fandi.pi f12, f11, 15\n"
"faddi.pi f12, f12, -8\n"
"fcvt.ps.pw f12, f12, rne\n"
"fmadd.ps f10, f12, f13, f10, rne\n"
"fandi.pi f18, f17, 15\n"
"faddi.pi f18, f18, -8\n"
"fcvt.ps.pw f18, f18, rne\n"
"fmadd.ps f16, f18, f13, f16, rne\n"
"fsrli.pi f14, f11, 4\n"
"fandi.pi f14, f14, 15\n"
"faddi.pi f14, f14, -8\n"
"fcvt.ps.pw f14, f14, rne\n"
"fmadd.ps f10, f14, f15, f10, rne\n"
"fsrli.pi f19, f17, 4\n"
"fandi.pi f19, f19, 15\n"
"faddi.pi f19, f19, -8\n"
"fcvt.ps.pw f19, f19, rne\n"
"fmadd.ps f16, f19, f15, f16, rne\n"
"flw.ps f13, %[b_low1]\n"
"flw.ps f15, %[b_high1]\n"
"fgb.ps f11, f31(%[a_ptr0_1])\n"
"fgb.ps f17, f31(%[a_ptr1_1])\n"
"fandi.pi f12, f11, 15\n"
"faddi.pi f12, f12, -8\n"
"fcvt.ps.pw f12, f12, rne\n"
"fmadd.ps f10, f12, f13, f10, rne\n"
"fandi.pi f18, f17, 15\n"
"faddi.pi f18, f18, -8\n"
"fcvt.ps.pw f18, f18, rne\n"
"fmadd.ps f16, f18, f13, f16, rne\n"
"fsrli.pi f14, f11, 4\n"
"fandi.pi f14, f14, 15\n"
"faddi.pi f14, f14, -8\n"
"fcvt.ps.pw f14, f14, rne\n"
"fmadd.ps f10, f14, f15, f10, rne\n"
"fsrli.pi f19, f17, 4\n"
"fandi.pi f19, f19, 15\n"
"faddi.pi f19, f19, -8\n"
"fcvt.ps.pw f19, f19, rne\n"
"fmadd.ps f16, f19, f15, f16, rne\n"
:
: [a_ptr0_0] "r"(&blk0->qs[0]), [a_ptr0_1] "r"(&blk0->qs[8]), [a_ptr1_0] "r"(&blk1->qs[0]),
[a_ptr1_1] "r"(&blk1->qs[8]), [b_low0] "m"(*(const float (*)[8]) & b_ptr[0]),
[b_high0] "m"(*(const float (*)[8]) & b_ptr[16]), [b_low1] "m"(*(const float (*)[8]) & b_ptr[8]),
[b_high1] "m"(*(const float (*)[8]) & b_ptr[24])
: "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", "f19");
const uint32_t scale_raw0 = (uint32_t) blk0->d;
const uint32_t scale_raw1 = (uint32_t) blk1->d;
__asm__ volatile(
"fbcx.ps f24, %[s0]\n"
"fcvt.ps.f16 f24, f24\n"
"fmadd.ps f20, f10, f24, f20\n"
"fbcx.ps f25, %[s1]\n"
"fcvt.ps.f16 f25, f25\n"
"fmadd.ps f21, f16, f25, f21\n"
:
: [s0] "r"(scale_raw0), [s1] "r"(scale_raw1)
: "f20", "f21", "f24", "f25");
}
float result0, result1;
__asm__ __volatile__(
"fswizz.ps f1, f20, 0xB1 \n\t"
"fadd.ps f2, f20, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(result0)::"t0", "f1", "f2", "f3", "f4", "f5");
__asm__ __volatile__(
"fswizz.ps f1, f21, 0xB1 \n\t"
"fadd.ps f2, f21, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(result1)::"t0", "f1", "f2", "f3", "f4", "f5");
*out0 = result0;
*out1 = result1;
}
+120
View File
@@ -0,0 +1,120 @@
//******************************************************************************
// CLAMP F32 Kernel
// Element-wise: dst[i] = min(max(src0[i], min_val), max_val)
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_clamp_params {
struct ggml_tensor src0; // F32 input (contiguous)
struct ggml_tensor dst; // F32 output (contiguous; may alias src0.data)
float min_val;
float max_val;
};
// Vectorized fmax/fmin clamp with scalar tail. n may be any non-negative int.
static inline void clamp_block_f32(float * dst, const float * src, float min_val, float max_val, int32_t n) {
int32_t i = 0;
const int32_t vec_end = (n / 8) * 8;
if (vec_end > 0) {
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
for (; i < vec_end; i += 8) {
__asm__ volatile(
"flw.ps f10, %[s]\n"
"fbc.ps f11, %[mn]\n"
"fbc.ps f12, %[mx]\n"
"fmax.ps f13, f10, f11\n"
"fmin.ps f13, f13, f12\n"
"fsw.ps f13, %[d]\n"
: [d] "=m"(*(float (*)[8]) & dst[i])
: [s] "m"(*(const float (*)[8]) & src[i]), [mn] "m"(min_val), [mx] "m"(max_val)
: "f10", "f11", "f12", "f13");
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
}
for (; i < n; i++) {
float v = src[i];
if (v < min_val) {
v = min_val;
}
if (v > max_val) {
v = max_val;
}
dst[i] = v;
}
}
int entry_point(struct ggml_et_clamp_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
const int64_t total_elements = src0->ne[0] * src0->ne[1] * src0->ne[2] * src0->ne[3];
if (total_elements <= 0) {
return 0;
}
const float min_val = params->min_val;
const float max_val = params->max_val;
// Distribute by cache lines (16 F32 elements). Each thread owns disjoint
// cache lines, so a partial trailing line is written by exactly one
// thread — safe under non-coherent caches.
const int64_t elems_per_cl = 16;
const int64_t total_cl = (total_elements + elems_per_cl - 1) / elems_per_cl;
const int64_t cl_per_thread = (total_cl + num_threads - 1) / num_threads;
const int64_t cl_start = (int64_t) thread_id * cl_per_thread;
int64_t cl_end = cl_start + cl_per_thread;
if (cl_end > total_cl) {
cl_end = total_cl;
}
if (cl_start >= total_cl) {
return 0;
}
const int64_t es = cl_start * elems_per_cl;
int64_t ee = cl_end * elems_per_cl;
if (ee > total_elements) {
ee = total_elements;
}
clamp_block_f32(dst_data + es, src0_data + es, min_val, max_val, (int32_t) (ee - es));
return 0;
}
@@ -0,0 +1,175 @@
//******************************************************************************
// Concat F32 Kernel
// Concatenates two F32 tensors along a specified dimension.
// All copies are aligned to cacheline boundaries (64 bytes = 16 floats).
//
// For dim >= 1, entire rows are copied from src0 or src1 into dst.
// For dim == 0, use:
// - a fast vector path when both source row segments are cacheline-aligned
// - a scalar stride-aware path otherwise
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
#include <string.h>
struct ggml_et_concat_params {
struct ggml_tensor src0; // F32 input tensor 0
struct ggml_tensor src1; // F32 input tensor 1
struct ggml_tensor dst; // F32 output tensor
int32_t dim; // Concatenation dimension
};
// Copy n floats from src to dst using 8-wide vector loads/stores.
// n must be a multiple of 16 (cacheline-aligned).
static inline void copy_row_aligned(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f11, %[src_vec]\n"
"fsw.ps f11, %[dst_vec]\n"
: [dst_vec] "=m"(*(float (*)[8]) & dst[i])
: [src_vec] "m"(*(const float (*)[8]) & src[i])
: "f11");
}
}
int entry_point(struct ggml_et_concat_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * src1 = &params->src1;
struct ggml_tensor * dst = &params->dst;
int32_t dim = params->dim;
if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * src1_data = (float *) src1->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !src1_data || !dst_data) {
return -1;
}
const int64_t ne00 = src0->ne[0], ne01 = src0->ne[1], ne02 = src0->ne[2], ne03 = src0->ne[3];
const int64_t ne10 = src1->ne[0], ne11 = src1->ne[1], ne12 = src1->ne[2], ne13 = src1->ne[3];
const int64_t ne0 = dst->ne[0], ne1 = dst->ne[1], ne2 = dst->ne[2], ne3 = dst->ne[3];
// src strides in bytes
const size_t nb00 = src0->nb[0], nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
const size_t nb10 = src1->nb[0], nb11 = src1->nb[1], nb12 = src1->nb[2], nb13 = src1->nb[3];
// dst strides in bytes
const size_t dnb1 = dst->nb[1], dnb2 = dst->nb[2], dnb3 = dst->nb[3];
// Total rows across all higher dimensions
const int64_t total_rows = ne1 * ne2 * ne3;
// Generic slow path for dim==0 when either source segment is not suitable for
// aligned vector copies. Threading is done by cacheline-aligned row groups,
// so writers do not share destination cache lines.
if (dim == 0 && (ne00 % 16 != 0 || ne10 % 16 != 0 || nb00 != sizeof(float) || nb10 != sizeof(float))) {
const int64_t rows_per_group = et_rows_per_cacheline_group(ne0, sizeof(float));
const int64_t total_groups = (total_rows + rows_per_group - 1) / rows_per_group;
for (int64_t grp = thread_id; grp < total_groups; grp += num_threads) {
const int64_t row_start = grp * rows_per_group;
int64_t row_end = row_start + rows_per_group;
if (row_end > total_rows) {
row_end = total_rows;
}
for (int64_t row = row_start; row < row_end; row++) {
int64_t i1 = row % ne1;
int64_t i2 = (row / ne1) % ne2;
int64_t i3 = row / (ne1 * ne2);
float * dst_row = (float *) ((char *) dst_data + i1 * dnb1 + i2 * dnb2 + i3 * dnb3);
const char * s0_base = (const char *) src0_data + i1 * nb01 + i2 * nb02 + i3 * nb03;
for (int64_t i0 = 0; i0 < ne00; i0++) {
dst_row[i0] = *(const float *) (s0_base + i0 * nb00);
}
const char * s1_base = (const char *) src1_data + i1 * nb11 + i2 * nb12 + i3 * nb13;
for (int64_t i0 = 0; i0 < ne10; i0++) {
dst_row[ne00 + i0] = *(const float *) (s1_base + i0 * nb10);
}
}
}
return 0;
}
// Standard path: ne0 % 16 == 0, aligned rows
for (int64_t row = thread_id; row < total_rows; row += num_threads) {
// Decompose linear row index into (i1, i2, i3)
int64_t i1 = row % ne1;
int64_t i2 = (row / ne1) % ne2;
int64_t i3 = row / (ne1 * ne2);
float * dst_row = (float *) ((char *) dst_data + i1 * dnb1 + i2 * dnb2 + i3 * dnb3);
if (dim == 0) {
// Concat along innermost dimension: [src0_row | src1_row]
// Both ne00 and ne10 are multiples of 16 (cacheline-aligned)
const float * s0_row = (const float *) ((const char *) src0_data + i1 * nb01 + i2 * nb02 + i3 * nb03);
const float * s1_row = (const float *) ((const char *) src1_data + i1 * nb11 + i2 * nb12 + i3 * nb13);
copy_row_aligned(dst_row, s0_row, (int32_t) ne00);
copy_row_aligned(dst_row + ne00, s1_row, (int32_t) ne10);
} else if (dim == 1) {
// Concat along dim 1: first ne01 rows from src0, rest from src1
if (i1 < ne01) {
const float * s0_row = (const float *) ((const char *) src0_data + i1 * nb01 + i2 * nb02 + i3 * nb03);
copy_row_aligned(dst_row, s0_row, (int32_t) ne0);
} else {
const float * s1_row =
(const float *) ((const char *) src1_data + (i1 - ne01) * nb11 + i2 * nb12 + i3 * nb13);
copy_row_aligned(dst_row, s1_row, (int32_t) ne0);
}
} else if (dim == 2) {
// Concat along dim 2: first ne02 slices from src0, rest from src1
if (i2 < ne02) {
const float * s0_row = (const float *) ((const char *) src0_data + i1 * nb01 + i2 * nb02 + i3 * nb03);
copy_row_aligned(dst_row, s0_row, (int32_t) ne0);
} else {
const float * s1_row =
(const float *) ((const char *) src1_data + i1 * nb11 + (i2 - ne02) * nb12 + i3 * nb13);
copy_row_aligned(dst_row, s1_row, (int32_t) ne0);
}
} else {
// dim == 3: first ne03 batches from src0, rest from src1
if (i3 < ne03) {
const float * s0_row = (const float *) ((const char *) src0_data + i1 * nb01 + i2 * nb02 + i3 * nb03);
copy_row_aligned(dst_row, s0_row, (int32_t) ne0);
} else {
const float * s1_row =
(const float *) ((const char *) src1_data + i1 * nb11 + i2 * nb12 + (i3 - ne03) * nb13);
copy_row_aligned(dst_row, s1_row, (int32_t) ne0);
}
}
}
return 0;
}
+107
View File
@@ -0,0 +1,107 @@
//******************************************************************************
// Bare Metal CONT F16 Kernel
// Converts non-contiguous F16 tensors to contiguous memory layout
//
// Note: F16 is represented as uint16_t (IEEE 754 binary16 format)
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
struct ggml_et_cont_params {
struct ggml_tensor src0; // F16 input tensor (non-contiguous)
struct ggml_tensor dst; // F16 output tensor (contiguous)
};
int entry_point(struct ggml_et_cont_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = 2048; //get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
struct ggml_tensor * src0 = &params->src0; // Non-contiguous input
struct ggml_tensor * dst = &params->dst; // Contiguous output
if (src0->type != GGML_TYPE_F16 || dst->type != GGML_TYPE_F16) {
return -1; // Unsupported type combination
}
uint16_t * src0_data = (uint16_t *) src0->data;
uint16_t * dst_data = (uint16_t *) dst->data;
if (!src0_data || !dst_data) {
return -1; // Null data pointer
}
const int64_t src_elements = src0->ne[0] * src0->ne[1] * src0->ne[2] * src0->ne[3];
const int64_t dst_elements = dst->ne[0] * dst->ne[1] * dst->ne[2] * dst->ne[3];
if (src_elements != dst_elements) {
return -1; // Element count mismatch
}
// Source tensor dimensions and strides
const int64_t ne00 = src0->ne[0];
const int64_t ne01 = src0->ne[1];
const int64_t ne02 = src0->ne[2];
const int64_t ne03 = src0->ne[3];
const int64_t nb00 = src0->nb[0];
const int64_t nb01 = src0->nb[1];
const int64_t nb02 = src0->nb[2];
const int64_t nb03 = src0->nb[3];
// Parallelize by rows (dimension 1)
const int64_t total_rows = ne01;
const int64_t rows_per_thread = (total_rows + num_threads - 1) / num_threads;
const int64_t start_row = thread_id * rows_per_thread;
const int64_t end_row = (start_row + rows_per_thread < total_rows) ? (start_row + rows_per_thread) : total_rows;
if (start_row >= total_rows) {
return 0;
}
// Iterate over source tensor dimensions
for (int64_t i03 = 0; i03 < ne03; i03++) {
for (int64_t i02 = 0; i02 < ne02; i02++) {
// Calculate base linear index for this (i03, i02) slice in destination
const int64_t dst_linear_base = i03 * ne02 * ne01 * ne00 + i02 * ne01 * ne00;
// Process this thread's assigned rows
for (int64_t i01 = start_row; i01 < end_row; i01++) {
// Linear index for start of this row in destination
const int64_t dst_linear_row_base = dst_linear_base + i01 * ne00;
// Inner loop over dimension 0
for (int64_t i00 = 0; i00 < ne00; i00++) {
// Source offset using non-contiguous strides
const int64_t src_offset_bytes = i00 * nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03;
const uint16_t * src_ptr = (const uint16_t *) ((const char *) src0_data + src_offset_bytes);
// Destination linear index (contiguous layout)
const int64_t dst_linear_idx = dst_linear_row_base + i00;
// Use atomic store for thread safety
atomic_store_f16((volatile uint16_t *) &dst_data[dst_linear_idx], *src_ptr);
}
}
}
}
return 0;
}
+248
View File
@@ -0,0 +1,248 @@
//******************************************************************************
// Bare Metal CONT F32 Kernel
// Converts non-contiguous tensors to contiguous memory layout
//
// Fast path: src contiguous: flat vectorized copy by cache lines
// Aligned path: nb00==4 and ne00 % 16 == 0: distribute rows, no coherency issue
// Unaligned: nb00==4 and ne00 not aligned: distribute by cache lines,
// reverse-compute src coords, handle partial rows at boundaries
// Fallback: nb00 != 4: scalar per-element
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdbool.h>
#include <stdint.h>
struct ggml_et_cont_params {
struct ggml_tensor src0; // F32 input tensor (non-contiguous)
struct ggml_tensor dst; // F32 output tensor (contiguous)
};
// Vectorized copy with scalar tail
static inline void vec_copy_f32(float * dst, const float * src, int32_t n) {
int32_t i = 0;
const int32_t vec_end = (n / 8) * 8;
for (; i < vec_end; i += 8) {
__asm__ volatile(
"flw.ps f10, %[s]\n"
"fsw.ps f10, %[d]\n"
: [d] "=m"(*(float (*)[8]) & dst[i])
: [s] "m"(*(const float (*)[8]) & src[i])
: "f10");
}
for (; i < n; i++) {
dst[i] = src[i];
}
}
// Scalar copy
static inline void scalar_copy_f32(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i++) {
dst[i] = src[i];
}
}
// static inline size_t tensor_bytes(const struct ggml_tensor *t) {
// return (size_t)t->ne[0] * t->ne[1] * t->ne[2] * t->ne[3] * t->nb[0];
// }
int entry_point(struct ggml_et_cont_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
// evict_region_past_l2(src0_data, tensor_bytes(src0));
if (!src0_data || !dst_data) {
return -1;
}
const int64_t ne00 = src0->ne[0];
const int64_t ne01 = src0->ne[1];
const int64_t ne02 = src0->ne[2];
const int64_t ne03 = src0->ne[3];
const int64_t nb00 = src0->nb[0];
const int64_t nb01 = src0->nb[1];
const int64_t nb02 = src0->nb[2];
const int64_t nb03 = src0->nb[3];
const int64_t total_elements = ne00 * ne01 * ne02 * ne03;
if (total_elements == 0) {
return 0;
}
const bool src_contiguous = ggml_tensor_is_contiguous(src0, 4);
//==========================================================================
// Fast path: src is contiguous: flat vectorized copy by cache lines
//==========================================================================
if (src_contiguous) {
const int64_t elems_per_cl = 16;
const int64_t total_cl = (total_elements + elems_per_cl - 1) / elems_per_cl;
const int64_t cl_per_thread = (total_cl + num_threads - 1) / num_threads;
const int64_t cl_start = thread_id * cl_per_thread;
int64_t cl_end = cl_start + cl_per_thread;
if (cl_end > total_cl) {
cl_end = total_cl;
}
if (cl_start >= total_cl) {
return 0;
}
const int64_t es = cl_start * elems_per_cl;
int64_t ee = cl_end * elems_per_cl;
if (ee > total_elements) {
ee = total_elements;
}
vec_copy_f32(dst_data + es, src0_data + es, (int32_t) (ee - es));
return 0;
}
//==========================================================================
// Non-contiguous paths: require nb00==4 (dim 0 contiguous in src)
//==========================================================================
if (nb00 != 4) {
// Fully non-contiguous scalar fallback — distribute by cache lines
const int64_t elems_per_cl = 16;
const int64_t total_cl = (total_elements + elems_per_cl - 1) / elems_per_cl;
const int64_t cl_per_thread = (total_cl + num_threads - 1) / num_threads;
const int64_t cl_start = thread_id * cl_per_thread;
int64_t cl_end = cl_start + cl_per_thread;
if (cl_end > total_cl) {
cl_end = total_cl;
}
if (cl_start >= total_cl) {
return 0;
}
const int64_t es = cl_start * elems_per_cl;
int64_t ee = cl_end * elems_per_cl;
if (ee > total_elements) {
ee = total_elements;
}
for (int64_t idx = es; idx < ee; idx++) {
const int64_t i00 = idx % ne00;
const int64_t rem1 = idx / ne00;
const int64_t i01 = rem1 % ne01;
const int64_t rem2 = rem1 / ne01;
const int64_t i02 = rem2 % ne02;
const int64_t i03 = rem2 / ne02;
const float * sp =
(const float *) ((const char *) src0_data + i00 * nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03);
dst_data[idx] = *sp;
}
return 0;
}
// nb00 == 4 from here: dim 0 is contiguous in src
//==========================================================================
// Aligned path: ne00 % 16 == 0: rows are cache-line aligned, distribute rows
//==========================================================================
if (ne00 % 16 == 0) {
const int64_t total_rows = ne01 * ne02 * ne03;
const int64_t rows_per_thread = (total_rows + num_threads - 1) / num_threads;
const int64_t start_row = thread_id * rows_per_thread;
const int64_t end_row = (start_row + rows_per_thread < total_rows) ? (start_row + rows_per_thread) : total_rows;
if (start_row >= total_rows) {
return 0;
}
for (int64_t ir = start_row; ir < end_row; ir++) {
const int64_t i03 = ir / (ne02 * ne01);
const int64_t i02 = (ir - i03 * ne02 * ne01) / ne01;
const int64_t i01 = ir - i03 * ne02 * ne01 - i02 * ne01;
const float * src_row = (const float *) ((const char *) src0_data + i01 * nb01 + i02 * nb02 + i03 * nb03);
float * dst_row = dst_data + ir * ne00;
vec_copy_f32(dst_row, src_row, (int32_t) ne00);
}
return 0;
}
//==========================================================================
// Unaligned path: ne00 % 16 != 0, nb00 == 4
// Distribute cache-line-aligned chunks of dst, handle partial rows at edges
//==========================================================================
{
const int64_t elems_per_cl = 16;
const int64_t total_cl = (total_elements + elems_per_cl - 1) / elems_per_cl;
const int64_t cl_per_thread = (total_cl + num_threads - 1) / num_threads;
const int64_t cl_start = thread_id * cl_per_thread;
int64_t cl_end = cl_start + cl_per_thread;
if (cl_end > total_cl) {
cl_end = total_cl;
}
if (cl_start >= total_cl) {
return 0;
}
const int64_t es = cl_start * elems_per_cl;
int64_t ee = cl_end * elems_per_cl;
if (ee > total_elements) {
ee = total_elements;
}
int64_t pos = es;
// Compute starting row coordinates
int64_t row_idx = pos / ne00;
int64_t col = pos % ne00;
while (pos < ee) {
// Decompose row_idx -> (i01, i02, i03)
const int64_t i03 = row_idx / (ne02 * ne01);
const int64_t i02 = (row_idx - i03 * ne02 * ne01) / ne01;
const int64_t i01 = row_idx - i03 * ne02 * ne01 - i02 * ne01;
const float * src_row = (const float *) ((const char *) src0_data + i01 * nb01 + i02 * nb02 + i03 * nb03);
// How many elements left in this row and in our chunk
int64_t row_remaining = ne00 - col;
int64_t chunk_remaining = ee - pos;
int32_t n = (int32_t) (row_remaining < chunk_remaining ? row_remaining : chunk_remaining);
vec_copy_f32(dst_data + pos, src_row + col, n);
pos += n;
col = 0; // subsequent rows start at column 0
row_idx++;
}
}
return 0;
}
@@ -0,0 +1,807 @@
//******************************************************************************
// 2D F32 convolution on the ET-SoC-1 matrix engine (GGML CONV_2D layout).
//
// LAYOUT (matches GGML's standard CONV_2D, cwhn=false; wireable directly):
// src1 input : ne = [W, H, Cin, N=1] memory: input [n][cin][h][w]
// src0 filter: ne = [Kw, Kh, Cin, Cout] memory: filter[oc][ic][kh][kw]
// dst output: ne = [W, H, Cout, N=1] memory: output[n][oc][h][w]
//
// CONSTRAINTS (enforced at supports_op):
// F32 throughout, N == 1, Cin % 16 == 0, Cout % 16 == 0, positive
// stride/pad, dilation == 1. Tile/L2SCP limits are checked here.
//
// MEMORY MODEL:
// Each active shire uses its own 2 MB local L2 SCP:
// filter slice | pin buffer 0 | pin buffer 1? | output staging? | scratch
//
// The filter slice contains only the output-channel tiles (`mt`) consumed
// by this shire's tile assignment. That keeps hart-0's inner-loop
// tensor_loads local to the shire and avoids packing unused filter slabs.
//
// THREADING (multi-minion, multi-shire):
// PHASE 1 (per-shire filter pack): hart-1's pack this shire's filter
// slice into local L2 SCP. Work is slab-striped across the 32 minions.
//
// PHASE 2 (per-shire compute): hart-1's pack the input pin chunks while
// hart-0's run the matrix engine. Pin double-buffering hides the next
// chunk pack behind the current chunk's FMA pipeline when Cin does not
// fit in one local buffer.
//
// PERFORMANCE STRATEGIES:
// 1. Local filter slice: pack only the `mt` values this shire consumes;
// inner-loop tensor_loads stay shire-local.
// 2. Pin Cin streaming + chunk double-buffer: pack one
// chunk while computing the prior one.
// 3. TenC save/restore: f0..f31 IS the TenC accumulator;
// spill/refill via L2 SCP scratch lets each hart hold multiple
// partial accumulators across chunks.
// 4. OW%16 staging: for partial-tile output, write to a
// padded L2 SCP region then have one hart scalar-emit to DRAM.
//
// WHY THE FILTER PACK EXISTS:
// GGML's OIHW filter has stride Kh*Kw*4 between consecutive Cin elements
// (e.g. 36 bytes for 3x3) — usually NOT a multiple of 64, so plain
// tensor_load cannot gather it directly. The per-slab pack into a
// Cin-innermost form gives every per-tap slab a flat 64-byte row stride
// and enables tensor_load.
//
// Picking M=Cout, N=W means TenC's natural row stride matches NCHW
// output's per-channel stride (H*W*4) — the output store is a clean
// tensor_store with no transpose. The price is that conv_size/conv_ctrl
// no longer help with W boundaries (mask gates M, not N), so we handle
// boundaries up-front by zero-padding the input in L2SCP.
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include "tensor.h"
#include <etsoc/common/utils.h>
#include <stdbool.h>
#include <stdint.h>
#define TILE 16 /* matrix engine native tile in M, K, N */
/* L1 SCP layout: A double-buffered, B single-buffered. Per the SDK doc
`dst_start` is a 6-bit field (max 63) but empirical testing shows the
physical L1 SCP per minion is 48 lines — writes to lines >= 48 corrupt.
So we get 3 × 16-line buffers max: A_0, A_1, B. Pick A as the
double-buffered operand (filter-slab loads, the longer of the two). */
#define LSCP_A_0 0 /* A buffer 0 at L1 SCP lines 0..15 */
#define LSCP_A_1 16 /* A buffer 1 at L1 SCP lines 16..31 */
#define LSCP_B 32 /* B (single buffer) at lines 32..47 */
#define N_MIN_PER_SHIRE 32 /* ET-SoC-1 geometry: 32 minions/shire */
#define N_SHIRES 32 /* default active shire count */
#define MAX_TILES_PER_HART 2 /* per-hart TenC slots (save/restore) */
#define MAX_DBL_BUFS 2 /* chunk pack buffers (double-buffered) */
/* Per-shire L2 SCP local budget. Per-shire SCP is 2 MB; we cap at
1984 KB to leave 64 KB headroom for per-hart TenC scratch (32 minions ×
2 slots × 1 KB), which lives at the tail of the SCP outside the pin
sizing budget. Bigger budget here means bigger feasible chunk_KT,
which means fewer chunks (each chunk costs 2 SHIRE barriers + ~30
TenC save/restore events per hart). */
#define LOCAL_BUDGET (1984 * 1024)
/* Cap on the per-shire filter region in local L2 SCP. The shire packs the
mt values it can consume under the current tile assignment, rather than
the whole Cout dimension. Reads in the inner loop are then fully
shire-local — no NoC fanout. */
#define LOCAL_FILTER_CAP (1024 * 1024) /* 1 MB / shire ceiling */
#define SLAB_BYTES ((uint64_t) TILE * TILE * sizeof(float)) /* 1024 */
#define SLAB_LINES ((SLAB_BYTES + 63) / 64) /* 16 */
/* Upper bound on the number of distinct mt values a single shire may pack.
This keeps the mt list stack-resident. Shapes that need more should fall
back until the filter-slice bookkeeping is made dynamic. */
#define MAX_MY_MT (N_MIN_PER_SHIRE * MAX_TILES_PER_HART)
typedef struct {
int mt;
int mt_idx;
int oh;
int ow_base;
} conv_tile_t;
static inline int ceil_div_i32(int x, int y) {
return (x + y - 1) / y;
}
static inline int round_up_tile_i32(int x) {
return (x + TILE - 1) & ~(TILE - 1);
}
static inline int min_i32(int a, int b) {
return a < b ? a : b;
}
static inline uint64_t min_u64(uint64_t a, uint64_t b) {
return a < b ? a : b;
}
/* ===== Vector helpers for hart-1 pack ============================
Both assume dst (and src for copy) are 32-byte aligned; n is in floats.
The 8-element tail is handled scalar. f30/f31 are scratch — clobbered
per-call via the asm clobber list. */
static inline void vec_zero_aligned(float * dst, int n) {
int i = 0;
const int n8 = n & ~7;
for (; i < n8; i += 8) {
__asm__ volatile(
"fsub.ps f31, f31, f31\n"
"fsw.ps f31, %[d]\n"
: [d] "=m"(*(float (*)[8]) & dst[i])
:
: "f31");
}
for (; i < n; ++i) {
dst[i] = 0.0f;
}
}
static inline void vec_copy_aligned(float * dst, const float * src, int n) {
int i = 0;
const int n8 = n & ~7;
for (; i < n8; i += 8) {
__asm__ volatile(
"flw.ps f30, %[s]\n"
"fsw.ps f30, %[d]\n"
: [d] "=m"(*(float (*)[8]) & dst[i])
: [s] "m"(*(const float (*)[8]) & src[i])
: "f30");
}
for (; i < n; ++i) {
dst[i] = src[i];
}
}
/* ===== TenC save/restore =========================================
The TenC accumulator IS the f0..f31 vector register file: row N occupies
f(2N) and f(2N+1) (two 8-fp32 vector regs per row). We save by
tensor_store-ing TILE rows × 64 bytes, and restore via 32 flw.ps after
forcing L1D to refetch from the L2SCP backing (tensor_store bypasses L1D
so the backing is always current). See feedback_tenc_save_restore.md. */
static inline void tenc_restore_from_scratch(uint64_t scr) {
FENCE;
evict_to_l2((const void *) scr, TILE, 64);
WAIT_CACHEOPS;
__asm__ volatile(
"flw.ps f0, 0(%0)\n"
"flw.ps f1, 32(%0)\n"
"flw.ps f2, 64(%0)\n"
"flw.ps f3, 96(%0)\n"
"flw.ps f4, 128(%0)\n"
"flw.ps f5, 160(%0)\n"
"flw.ps f6, 192(%0)\n"
"flw.ps f7, 224(%0)\n"
"flw.ps f8, 256(%0)\n"
"flw.ps f9, 288(%0)\n"
"flw.ps f10, 320(%0)\n"
"flw.ps f11, 352(%0)\n"
"flw.ps f12, 384(%0)\n"
"flw.ps f13, 416(%0)\n"
"flw.ps f14, 448(%0)\n"
"flw.ps f15, 480(%0)\n"
"flw.ps f16, 512(%0)\n"
"flw.ps f17, 544(%0)\n"
"flw.ps f18, 576(%0)\n"
"flw.ps f19, 608(%0)\n"
"flw.ps f20, 640(%0)\n"
"flw.ps f21, 672(%0)\n"
"flw.ps f22, 704(%0)\n"
"flw.ps f23, 736(%0)\n"
"flw.ps f24, 768(%0)\n"
"flw.ps f25, 800(%0)\n"
"flw.ps f26, 832(%0)\n"
"flw.ps f27, 864(%0)\n"
"flw.ps f28, 896(%0)\n"
"flw.ps f29, 928(%0)\n"
"flw.ps f30, 960(%0)\n"
"flw.ps f31, 992(%0)\n"
:
: "r"(scr)
: "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", "f16",
"f17", "f18", "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
"memory");
}
/* ===== Pin pack context ==========================================
Loop-invariant state hart-1 needs to pack one Cin chunk's worth of
pin (Kw shifted, padded copies of input rows) into local L2 SCP. The
filter is not touched in this struct; it is packed into the per-shire
local slice before the per-chunk loop begins. */
typedef struct {
const float * in_base; /* DRAM input base [Cin][H][W] */
int Kw;
int chunk_KT; /* number of K_TILES (=16-wide) per chunk */
int H, W, Hp, Wp_a;
int pad_h, pad_w, s0;
int minion; /* this hart's minion id (0..31) */
uint64_t pin_copy_floats; /* per-_s pin plane size in floats */
uint64_t l2_pad_in_buf[MAX_DBL_BUFS];
uint64_t pin_chunk_bytes; /* one chunk pin buffer's total size */
} pin_ctx_t;
static inline int find_mt_idx(const int * my_mt, int n_my_mt, int mt) {
for (int j = 0; j < n_my_mt; ++j) {
if (my_mt[j] == mt) {
return j;
}
}
return 0;
}
static inline conv_tile_t decode_tile(int t, int M_TILES, int w_tiles, const int * my_mt, int n_my_mt) {
conv_tile_t tile;
tile.mt = t % M_TILES;
t /= M_TILES;
const int wt = t % w_tiles;
t /= w_tiles;
tile.oh = t;
tile.ow_base = wt * TILE;
tile.mt_idx = find_mt_idx(my_mt, n_my_mt, tile.mt);
return tile;
}
static inline uint64_t
filter_slab_addr(uint64_t l2_filter, int Kw, int K_TILES, int n_my_mt, int mt_idx, int kh, int kw, int kt_global) {
return l2_filter + (uint64_t) ((((kh * Kw + kw) * n_my_mt + mt_idx) * K_TILES + kt_global)) * SLAB_BYTES;
}
static inline uint64_t pin_tile_addr(uint64_t l2_pad_in,
uint64_t pin_copy_bytes,
int ktc,
int kw,
int Hp,
int Wp_a,
int oh,
int ow_base,
int s1,
int kh) {
const int ir_pad = oh * s1 + kh;
return l2_pad_in + (uint64_t) kw * pin_copy_bytes +
(((uint64_t) (ktc * TILE) * Hp + ir_pad) * Wp_a + ow_base) * sizeof(float);
}
static inline char * output_tile_addr(char * out_base,
const conv_tile_t * tile,
uint64_t out_chan_stride,
uint64_t out_row_stride) {
return out_base + (size_t) (tile->mt * TILE) * out_chan_stride + (size_t) tile->oh * out_row_stride +
(size_t) tile->ow_base * sizeof(float);
}
static inline void flush_range_to_l2(const void * addr, uint64_t n_bytes) {
const uint64_t total_lines = (n_bytes + 63) / 64;
const char * fl_addr = (const char *) addr;
for (uint64_t done = 0; done < total_lines;) {
const uint64_t batch = min_u64(total_lines - done, 16);
flush_to_l2((const void *) (fl_addr + done * 64), batch, 64);
done += batch;
}
}
static inline void evict_range_past_l2(const void * addr, uint64_t n_bytes) {
const uint64_t total_lines = (n_bytes + 63) / 64;
const char * fl_addr = (const char *) addr;
for (uint64_t done = 0; done < total_lines;) {
const uint64_t batch = min_u64(total_lines - done, 16);
evict_past_l2((const void *) (fl_addr + done * 64), batch, 64);
done += batch;
}
}
/* One matrix-engine tile for one Cin chunk. This is the main optimization
surface: A is double-buffered, B is single-buffered due to L1 SCP space. */
static inline void compute_tile_chunk(uint64_t l2_filter,
uint64_t l2_pad_in,
uint64_t pin_copy_bytes,
int Kh,
int Kw,
int K_TILES,
int chunk_KT,
int kt_base,
int n_my_mt,
int Hp,
int Wp_a,
int s1,
uint64_t a_row_stride,
uint64_t b_row_stride,
const conv_tile_t * tile,
bool first_fma_clears_tenc) {
const int n_iters = Kh * Kw * chunk_KT;
const uint64_t A_BUFS[2] = { LSCP_A_0, LSCP_A_1 };
const uint64_t a_addr0 = filter_slab_addr(l2_filter, Kw, K_TILES, n_my_mt, tile->mt_idx, 0, 0, kt_base);
tensor_load(false, false, A_BUFS[0], 0, 0, a_addr0, 0, (uint64_t) (TILE - 1), a_row_stride, 0);
for (int iter = 0; iter < n_iters; ++iter) {
const int ktc = iter % chunk_KT;
const int rem = iter / chunk_KT;
const int kw = rem % Kw;
const int kh = rem / Kw;
const uint64_t b_addr =
pin_tile_addr(l2_pad_in, pin_copy_bytes, ktc, kw, Hp, Wp_a, tile->oh, tile->ow_base, s1, kh);
tensor_load(false, false, LSCP_B, 0, 0, b_addr, 0, (uint64_t) (TILE - 1), b_row_stride, 1);
tensor_wait(TENSOR_LOAD_WAIT_0);
tensor_wait(TENSOR_LOAD_WAIT_1);
if (iter + 1 < n_iters) {
const int ktc_n = (iter + 1) % chunk_KT;
const int rem_n = (iter + 1) / chunk_KT;
const int kw_n = rem_n % Kw;
const int kh_n = rem_n / Kw;
const uint64_t a_addr_n =
filter_slab_addr(l2_filter, Kw, K_TILES, n_my_mt, tile->mt_idx, kh_n, kw_n, kt_base + ktc_n);
tensor_load(false, false, A_BUFS[(iter + 1) & 1], 0, 0, a_addr_n, 0, (uint64_t) (TILE - 1), a_row_stride,
0);
}
tensor_fma(false, 3, (uint64_t) (TILE - 1), (uint64_t) (TILE - 1), 0, false, false, false, false, LSCP_B,
A_BUFS[iter & 1], 0, first_fma_clears_tenc && (iter == 0));
tensor_wait(TENSOR_FMA_WAIT);
}
}
/* Pack only the slabs this shire's tiles actually consume, into local
L2 SCP. Slab layout in the filter buffer is [Kh][Kw][n_my_mt][K_TILES]
of TILE×TILE slabs (Cin-innermost form). Distributed across the 32
hart-1's of this shire by `slab % 32 == minion`.
This deliberately favors local inner-loop reads over global filter fanout.
Depending on tile shape, two shires may pack the same mt value; keep that
tradeoff visible when experimenting with shared-filter layouts. */
static void pack_filter_local_mt(const float * flt_base,
int Kh,
int Kw,
int Cin,
int K_TILES,
const int * my_mt,
int n_my_mt,
int minion,
uint64_t l2_filter_base) {
const int n_slabs = Kh * Kw * n_my_mt * K_TILES;
const size_t kstep = (size_t) Kh * Kw; /* Cin stride in floats */
for (int slab = minion; slab < n_slabs; slab += N_MIN_PER_SHIRE) {
int t = slab;
const int kt = t % K_TILES;
t /= K_TILES;
const int mt_idx = t % n_my_mt;
t /= n_my_mt;
const int kw = t % Kw;
t /= Kw;
const int kh = t;
const int mt = my_mt[mt_idx];
const uint64_t slab_offset = (uint64_t) slab * SLAB_BYTES;
float * cell = (float *) (l2_filter_base + slab_offset);
for (int oc_in = 0; oc_in < TILE; ++oc_in) {
const int oc = mt * TILE + oc_in;
const float * src = flt_base + (((size_t) oc * Cin + (size_t) kt * TILE) * Kh + kh) * Kw + kw;
float * row = cell + (size_t) oc_in * TILE;
float scratch[TILE] __attribute__((aligned(32)));
for (int ic_in = 0; ic_in < TILE; ++ic_in) {
scratch[ic_in] = src[(size_t) ic_in * kstep];
}
vec_copy_aligned(row, scratch, TILE);
}
}
/* Flush this hart's dirty L1D lines for the slabs it wrote. */
FENCE;
for (int slab = minion; slab < n_slabs; slab += N_MIN_PER_SHIRE) {
const uint64_t slab_offset = (uint64_t) slab * SLAB_BYTES;
flush_to_l2((const void *) (l2_filter_base + slab_offset), SLAB_LINES, 64);
}
WAIT_CACHEOPS;
}
/* Pack one Cin chunk of the input pin (Kw shifted padded copies) into the
buf_idx side of local L2SCP. Work distributed across the 32 hart-1's in
the shire by `plane % 32 == minion`. The final flush_to_l2 forces L1D
write-back so hart-0's tensor_load sees the freshly written bytes. */
static void pack_pin_chunk(const pin_ctx_t * ctx, int chunk_id, int buf_idx) {
const int kt_base = chunk_id * ctx->chunk_KT;
const int Kw = ctx->Kw;
const int chunk_KT = ctx->chunk_KT;
const int H = ctx->H, W = ctx->W, Hp = ctx->Hp, Wp_a = ctx->Wp_a;
const int pad_h = ctx->pad_h, pad_w = ctx->pad_w, s0 = ctx->s0;
const int minion = ctx->minion;
/* Pin pack: Kw shifted, padded copies of input rows. Bounds [vlo, vhi)
hoisted outside the row loop so the inner loop is three regions
(zero-prefix | bulk-copy | zero-suffix) with no per-element predicate. */
float * pin0 = (float *) ctx->l2_pad_in_buf[buf_idx];
const int chunk_Cin = chunk_KT * TILE;
const int n_pin_planes = Kw * chunk_Cin;
for (int p = minion; p < n_pin_planes; p += N_MIN_PER_SHIRE) {
const int s = p / chunk_Cin;
const int icc = p % chunk_Cin;
const int ic = kt_base * TILE + icc;
float * pin_s = pin0 + (size_t) s * ctx->pin_copy_floats;
const int offset = s - pad_w;
int vlo = 0;
while (vlo < Wp_a && (s0 * vlo + offset) < 0) {
vlo++;
}
int vhi = Wp_a;
while (vhi > vlo && (s0 * (vhi - 1) + offset) >= W) {
vhi--;
}
const bool aligned = (s0 == 1) && ((vlo & 7) == 0) && (((vlo + offset) & 7) == 0);
for (int r = 0; r < Hp; ++r) {
float * row = pin_s + ((size_t) icc * Hp + r) * Wp_a;
const int real_h = r - pad_h;
if (real_h < 0 || real_h >= H) {
vec_zero_aligned(row, Wp_a);
continue;
}
const float * src_row = ctx->in_base + ((size_t) ic * H + real_h) * W;
for (int cc = 0; cc < vlo; ++cc) {
row[cc] = 0.0f;
}
if (aligned) {
vec_copy_aligned(row + vlo, src_row + vlo + offset, vhi - vlo);
} else if (s0 == 1) {
const float * csrc = src_row + vlo + offset;
const int n = vhi - vlo;
for (int cc = 0; cc < n; ++cc) {
row[vlo + cc] = csrc[cc];
}
} else {
for (int cc = vlo; cc < vhi; ++cc) {
row[cc] = src_row[s0 * cc + offset];
}
}
for (int cc = vhi; cc < Wp_a; ++cc) {
row[cc] = 0.0f;
}
}
}
/* Flush this buffer's L1D-dirty lines down to L2SCP backing. */
FENCE;
flush_range_to_l2((const void *) ctx->l2_pad_in_buf[buf_idx], ctx->pin_chunk_bytes);
WAIT_CACHEOPS;
}
int entry_point(struct ggml_et_binary_params * params, void * env) {
(void) env;
const int shire = get_shire_id();
const int hart_id = get_hart_id();
const int minion = (hart_id >> 1) & 0x1F;
const int hart1 = hart_id & 1;
const struct ggml_tensor * flt = &params->src0; /* [Kw,Kh,Cin,Cout] */
const struct ggml_tensor * in = &params->src1; /* [W, H, Cin,N=1 ] */
struct ggml_tensor * out = &params->dst; /* [W, H, Cout,N=1] */
const int Kw = (int) flt->ne[0];
const int Kh = (int) flt->ne[1];
const int Cin = (int) flt->ne[2];
const int Cout = (int) flt->ne[3];
const int W = (int) in->ne[0];
const int H = (int) in->ne[1];
const int OW = (int) out->ne[0];
const int OH = (int) out->ne[1];
/* op_params layout (set by ggml_conv_2d):
[0]=s0 [1]=s1 [2]=p0 [3]=p1 [4]=d0 [5]=d1 */
const int s0 = out->op_params[0];
const int s1 = out->op_params[1];
const int pad_w = out->op_params[2];
const int pad_h = out->op_params[3];
if (Cin <= 0 || Cout <= 0) {
return -1;
}
if (Cin % TILE != 0 || Cout % TILE != 0) {
return -1;
}
if (W <= 0 || H <= 0) {
return -1;
}
if (s0 <= 0 || s1 <= 0) {
return -1;
}
if (in->ne[2] != Cin || in->ne[3] != 1) {
return -1;
}
if (out->ne[2] != Cout || out->ne[3] != 1) {
return -1;
}
if (!flt->data || !in->data || !out->data) {
return -1;
}
const int K_TILES = Cin / TILE;
const int M_TILES = Cout / TILE;
const int Hp = H + 2 * pad_h;
const int Wp_a = round_up_tile_i32(OW);
const int OW_pad = Wp_a;
const bool need_stage = (OW % TILE != 0);
/* ===================== Tile assignment & active-shire selection =====
Computed up front because the per-shire mt set (and thus filter
region size) depends on n_active_shires. */
const int w_tiles = ceil_div_i32(OW, TILE);
const int total_tiles = OH * w_tiles * M_TILES;
const int n_active_shires = need_stage ? 1 : min_i32(total_tiles, N_SHIRES);
/* Inactive shires exit immediately. No global barrier — pack and
barriers are now per-shire, so unused shires don't need to vote. */
if (shire >= n_active_shires) {
return 0;
}
/* ===================== Determine this shire's mt set ================
Standard tile assignment: tile t is owned by
shire = t % n_active_shires
minion = (t / n_active_shires) % N_MIN_PER_SHIRE
slot = t / (n_active_shires * N_MIN_PER_SHIRE)
So the set of mt's this shire actually consumes is the set of
(t % M_TILES) for all t this shire owns. Enumerate all shire-owned
tiles, not just the first MAX_TILES_PER_HART slots; the one-chunk
path can process more tiles serially. */
int my_mt[MAX_MY_MT];
int n_my_mt = 0;
for (int t = shire; t < total_tiles; t += n_active_shires) {
const int mt = t % M_TILES;
bool found = false;
for (int j = 0; j < n_my_mt; ++j) {
if (my_mt[j] == mt) {
found = true;
break;
}
}
if (!found) {
if (n_my_mt >= MAX_MY_MT) {
return -1;
}
my_mt[n_my_mt++] = mt;
}
}
if (n_my_mt == 0) {
return 0; /* no tiles for this shire */
}
const uint64_t filter_local_bytes = (uint64_t) Kh * Kw * n_my_mt * K_TILES * SLAB_BYTES;
if (filter_local_bytes > LOCAL_FILTER_CAP) {
return -1;
}
/* ===================== L2 SCP local layout =========================
filter (this shire's mt slice) | pin_buf[0] | pin_buf[1]?
| output_stage? | scratch (streaming) */
const uint64_t l2_base = (uint64_t) et_shire_l2scp_local(0);
const uint64_t l2_filter = l2_base;
/* Sizing for pin: budget = LOCAL_BUDGET - filter - output_stage. */
const int64_t output_stage_bytes_full = need_stage ? (int64_t) Cout * OH * OW_pad * (int64_t) sizeof(float) : 0;
const int64_t budget_for_chunks = (int64_t) LOCAL_BUDGET - (int64_t) filter_local_bytes - output_stage_bytes_full;
if (budget_for_chunks <= 0) {
return -1;
}
const int64_t per_KT_pin_bytes = (int64_t) Kw * TILE * Hp * Wp_a * (int64_t) sizeof(float);
int chunk_KT;
int n_buffers;
if ((int64_t) K_TILES * per_KT_pin_bytes <= budget_for_chunks) {
chunk_KT = K_TILES;
n_buffers = 1;
} else {
chunk_KT = K_TILES;
while (chunk_KT > 1 && 2 * (int64_t) chunk_KT * per_KT_pin_bytes > budget_for_chunks) {
chunk_KT--;
}
while (chunk_KT > 1 && K_TILES % chunk_KT != 0) {
chunk_KT--;
}
n_buffers = (chunk_KT < K_TILES) ? 2 : 1;
if (chunk_KT < 1) {
return -1;
}
}
const int n_chunks = K_TILES / chunk_KT;
/* Streaming keeps partial sums in MAX_TILES_PER_HART scratch slots per
hart. The one-chunk path does not need scratch and can stream a longer
tile list serially, but multi-chunk shapes must fit this fixed slot
count until scratch scheduling is made more general. */
const int shire_tile_capacity = shire + MAX_TILES_PER_HART * n_active_shires * N_MIN_PER_SHIRE;
if (n_chunks > 1 && shire_tile_capacity < total_tiles) {
return -1;
}
const uint64_t pin_copy_floats = (uint64_t) chunk_KT * TILE * Hp * Wp_a;
const uint64_t pin_copy_bytes = pin_copy_floats * sizeof(float);
const uint64_t pin_chunk_bytes = (uint64_t) Kw * pin_copy_bytes;
const uint64_t l2_pin_base = l2_filter + filter_local_bytes;
const uint64_t l2_pin_buf[MAX_DBL_BUFS] = {
l2_pin_base,
l2_pin_base + pin_chunk_bytes,
};
const uint64_t l2_output_stage = need_stage ? l2_pin_base + (uint64_t) n_buffers * pin_chunk_bytes : 0;
const uint64_t scratch_per_hart = (uint64_t) MAX_TILES_PER_HART * (uint64_t) TILE * TILE * sizeof(float);
const uint64_t l2_scratch_base = need_stage ? l2_output_stage + (uint64_t) output_stage_bytes_full :
l2_pin_base + (uint64_t) n_buffers * pin_chunk_bytes;
/* ===================== PHASE 1: Filter pack (per-shire mt slice) ====
Hart-1's pack only this shire's mt slabs into local L2 SCP. The
SHIRE barrier below ensures the filter is in L2 SCP backing before
hart-0's first tensor_load. */
if (hart1) {
pack_filter_local_mt((const float *) flt->data, Kh, Kw, Cin, K_TILES, my_mt, n_my_mt, minion, l2_filter);
}
/* ===================== Hart 1: pin packer (per chunk) ==============
Double-buffered prefetch: pack chunk 0 synchronously, then per chunk c
signal "buf c ready", pack chunk c+1 into the alternate buffer
(overlaps hart-0's compute on c), signal "buf c done". */
if (hart1) {
const pin_ctx_t ctx = {
.in_base = (const float *) in->data,
.Kw = Kw,
.chunk_KT = chunk_KT,
.H = H,
.W = W,
.Hp = Hp,
.Wp_a = Wp_a,
.pad_h = pad_h,
.pad_w = pad_w,
.s0 = s0,
.minion = minion,
.pin_copy_floats = pin_copy_floats,
.l2_pad_in_buf = { l2_pin_buf[0], l2_pin_buf[1] },
.pin_chunk_bytes = pin_chunk_bytes,
};
pack_pin_chunk(&ctx, 0, 0); /* prologue */
for (int c = 0; c < n_chunks; ++c) {
et_barrier(ET_BARRIER_SHIRE); /* signal "buf c ready" */
if (n_buffers > 1 && c + 1 < n_chunks) {
pack_pin_chunk(&ctx, c + 1, (c + 1) & 1);
}
et_barrier(ET_BARRIER_SHIRE); /* wait "buf c done" */
}
if (need_stage) {
et_barrier(ET_BARRIER_SHIRE);
}
return 0;
}
/* ===================== Hart 0: matrix engine ======================
Two execution modes:
- n_chunks == 1: full Cin in one shot. Each hart processes a list
of tiles serially; TenC resets between tiles via first_pass=true.
- n_chunks > 1: streaming. Each hart owns up to MAX_TILES_PER_HART
tiles. For each chunk c, restore TenC from scratch[k] (skip on
c==0), accumulate this chunk's FMAs, then either save TenC back
to scratch[k] (c < last) or tensor_store directly (c == last). */
setup_cache_scp();
CLEAR_TENSOR_ERROR;
char * const out_base = need_stage ? (char *) l2_output_stage : (char *) out->data;
const int compute_OW = need_stage ? OW_pad : OW;
const uint64_t out_chan_stride = (uint64_t) OH * (uint64_t) compute_OW * sizeof(float);
const uint64_t out_row_stride = (uint64_t) compute_OW * sizeof(float);
const uint64_t a_row_stride = (uint64_t) TILE * sizeof(float); /* 64 */
const uint64_t b_row_stride = (uint64_t) Hp * (uint64_t) Wp_a * sizeof(float);
/* Tile assignment: shire-strided so small workloads spread across
shires before stacking minions in one shire. */
const int t_start = shire + minion * n_active_shires;
const int t_stride = n_active_shires * N_MIN_PER_SHIRE;
if (n_chunks == 1) {
et_barrier(ET_BARRIER_SHIRE); /* wait for the (only) pin chunk */
const uint64_t l2_pad_in = l2_pin_buf[0];
for (int t = t_start; t < total_tiles; t += t_stride) {
const conv_tile_t tile = decode_tile(t, M_TILES, w_tiles, my_mt, n_my_mt);
compute_tile_chunk(l2_filter, l2_pad_in, pin_copy_bytes, Kh, Kw, K_TILES, chunk_KT, 0, n_my_mt, Hp, Wp_a,
s1, a_row_stride, b_row_stride, &tile, /*first_fma_clears_tenc=*/true);
char * dst_addr = output_tile_addr(out_base, &tile, out_chan_stride, out_row_stride);
tensor_store(0, 0, 3, (uint64_t) (TILE - 1), (uint64_t) dst_addr, 0, out_chan_stride);
tensor_wait(TENSOR_STORE_WAIT);
}
et_barrier(ET_BARRIER_SHIRE); /* matches hart-1's second barrier */
} else {
/* Streaming path: each hart owns up to MAX_TILES_PER_HART tiles. */
int my_tiles[MAX_TILES_PER_HART];
int n_my_tiles = 0;
for (int slot = 0; slot < MAX_TILES_PER_HART; ++slot) {
const int t = t_start + slot * t_stride;
if (t < total_tiles) {
my_tiles[n_my_tiles++] = t;
}
}
conv_tile_t tiles[MAX_TILES_PER_HART];
for (int k = 0; k < n_my_tiles; ++k) {
tiles[k] = decode_tile(my_tiles[k], M_TILES, w_tiles, my_mt, n_my_mt);
}
const uint64_t my_scratch_base = l2_scratch_base + (uint64_t) minion * scratch_per_hart;
for (int c = 0; c < n_chunks; ++c) {
et_barrier(ET_BARRIER_SHIRE); /* pin chunk c packed */
const int buf = c & 1;
const uint64_t l2_pad_in = l2_pin_buf[buf];
const int kt_base = c * chunk_KT;
for (int k = 0; k < n_my_tiles; ++k) {
const conv_tile_t * tile = &tiles[k];
const uint64_t scr = my_scratch_base + (uint64_t) k * (TILE * TILE * sizeof(float));
const bool first_pass_chunk = (c == 0);
if (!first_pass_chunk) {
tenc_restore_from_scratch(scr);
}
compute_tile_chunk(l2_filter, l2_pad_in, pin_copy_bytes, Kh, Kw, K_TILES, chunk_KT, kt_base, n_my_mt,
Hp, Wp_a, s1, a_row_stride, b_row_stride, tile, first_pass_chunk);
if (c == n_chunks - 1) {
char * dst_addr = output_tile_addr(out_base, tile, out_chan_stride, out_row_stride);
tensor_store(0, 0, 3, (uint64_t) (TILE - 1), (uint64_t) dst_addr, 0, out_chan_stride);
} else {
tensor_store(0, 0, 3, (uint64_t) (TILE - 1), (uint64_t) scr, 0, 64);
}
tensor_wait(TENSOR_STORE_WAIT);
}
et_barrier(ET_BARRIER_SHIRE); /* hart-0 done with chunk c */
}
}
FENCE;
/* ----------------------- DRAM emit phase ---------------------------
Only relevant when we staged into L2SCP because OW % 16 != 0. */
if (need_stage) {
et_barrier(ET_BARRIER_SHIRE);
if (minion == 0) {
const float * stage = (const float *) l2_output_stage;
float * dram = (float *) out->data;
for (int oc = 0; oc < Cout; ++oc) {
for (int oh2 = 0; oh2 < OH; ++oh2) {
const float * src = stage + ((size_t) oc * OH + oh2) * OW_pad;
float * dst = dram + ((size_t) oc * OH + oh2) * OW;
for (int ow2 = 0; ow2 < OW; ++ow2) {
dst[ow2] = src[ow2];
}
}
}
FENCE;
const uint64_t total_bytes = (uint64_t) Cout * OH * OW * sizeof(float);
evict_range_past_l2((const void *) dram, total_bytes);
WAIT_CACHEOPS;
}
}
return 0;
}
@@ -0,0 +1,110 @@
//******************************************************************************
// CPY F32 -> F16 Kernel
// Copies F32 source tensor to F16 destination tensor (contiguous output).
// Source may have arbitrary strides; destination must be contiguous.
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdbool.h>
#include <stdint.h>
struct ggml_et_cont_params {
struct ggml_tensor src0;
struct ggml_tensor dst;
};
int entry_point(struct ggml_et_cont_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env || !params) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F16) {
return -1;
}
const char * src_data = (const char *) src0->data;
uint16_t * dst_data = (uint16_t *) dst->data;
if (!src_data || !dst_data) {
return -1;
}
const int64_t ne00 = src0->ne[0];
const int64_t ne01 = src0->ne[1];
const int64_t ne02 = src0->ne[2];
const int64_t ne03 = src0->ne[3];
const int64_t nb00 = src0->nb[0];
const int64_t nb01 = src0->nb[1];
const int64_t nb02 = src0->nb[2];
const int64_t nb03 = src0->nb[3];
const int64_t total_elements = ne00 * ne01 * ne02 * ne03;
if (total_elements == 0) {
return 0;
}
// Check if src is contiguous F32
const bool src_contiguous =
(nb00 == 4 && nb01 == ne00 * 4 && nb02 == ne00 * ne01 * 4 && nb03 == ne00 * ne01 * ne02 * 4);
// Distribute by cache lines (16 F16 elements = 32 bytes = half cache line)
// Use 32 elements per chunk to keep output cache-line aligned
const int64_t elems_per_cl = 32;
const int64_t total_cl = (total_elements + elems_per_cl - 1) / elems_per_cl;
const int64_t cl_per_thread = (total_cl + num_threads - 1) / num_threads;
const int64_t cl_start = thread_id * cl_per_thread;
int64_t cl_end = cl_start + cl_per_thread;
if (cl_end > total_cl) {
cl_end = total_cl;
}
if (cl_start >= total_cl) {
return 0;
}
const int64_t es = cl_start * elems_per_cl;
int64_t ee = cl_end * elems_per_cl;
if (ee > total_elements) {
ee = total_elements;
}
if (src_contiguous) {
// Fast path: src is contiguous F32
const float * src_f32 = (const float *) src_data;
for (int64_t i = es; i < ee; ++i) {
dst_data[i] = fp32_to_fp16(src_f32[i]);
}
} else {
// General path: stride-aware read
for (int64_t idx = es; idx < ee; ++idx) {
const int64_t i00 = idx % ne00;
const int64_t rem1 = idx / ne00;
const int64_t i01 = rem1 % ne01;
const int64_t rem2 = rem1 / ne01;
const int64_t i02 = rem2 % ne02;
const int64_t i03 = rem2 / ne02;
const float val = *(const float *) (src_data + i00 * nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03);
dst_data[idx] = fp32_to_fp16(val);
}
}
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
.section .text.init, "ax", @progbits
.global _start
_start:
# initialize global pointer
.option push
.option norelax
la gp, __global_pointer$
.option pop
# Firmware sets stack pointer before launch
# bss not allowed, no init
call entry_point
li a2, 0 /* KERNEL_RETURN_SUCCESS (0) */
mv a1, a0
li a0, 8 /* SYSCALL_RETURN_FROM_KERNEL (8) */
ecall
@@ -0,0 +1,96 @@
//******************************************************************************
// CUMSUM F32 Kernel
// Computes an inclusive prefix sum along dim 0 for each row in higher dims.
// First-pass implementation: scalar and row-contiguous input/output only.
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_cumsum_params {
struct ggml_tensor src0;
struct ggml_tensor dst;
};
int entry_point(struct ggml_et_cumsum_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
const int64_t ne0 = src0->ne[0];
const int64_t ne1 = src0->ne[1];
const int64_t ne2 = src0->ne[2];
const int64_t ne3 = src0->ne[3];
const size_t snb0 = src0->nb[0];
const size_t snb1 = src0->nb[1];
const size_t snb2 = src0->nb[2];
const size_t snb3 = src0->nb[3];
const size_t dnb0 = dst->nb[0];
const size_t dnb1 = dst->nb[1];
const size_t dnb2 = dst->nb[2];
const size_t dnb3 = dst->nb[3];
if (snb0 != sizeof(float) || dnb0 != sizeof(float)) {
return -1;
}
const int64_t total_rows = ne1 * ne2 * ne3;
const int64_t rows_per_group = et_rows_per_cacheline_group(ne0, sizeof(float));
const int64_t total_groups = (total_rows + rows_per_group - 1) / rows_per_group;
for (int64_t grp = thread_id; grp < total_groups; grp += num_threads) {
const int64_t row_start = grp * rows_per_group;
int64_t row_end = row_start + rows_per_group;
if (row_end > total_rows) {
row_end = total_rows;
}
for (int64_t row = row_start; row < row_end; ++row) {
int64_t i1 = row % ne1;
int64_t i2 = (row / ne1) % ne2;
int64_t i3 = row / (ne1 * ne2);
const float * src_row = (const float *) ((const char *) src0_data + i1 * snb1 + i2 * snb2 + i3 * snb3);
float * dst_row = (float *) ((char *) dst_data + i1 * dnb1 + i2 * dnb2 + i3 * dnb3);
float acc = 0.0f;
for (int64_t i0 = 0; i0 < ne0; ++i0) {
acc += src_row[i0];
dst_row[i0] = acc;
}
}
}
return 0;
}
@@ -0,0 +1,90 @@
//******************************************************************************
// Diag F32 Kernel
// Creates a diagonal matrix from a 1D vector.
// dst[i][j] = (i == j) ? src0[i] : 0.0f
//
// src0: [N, 1, ne2, ne3] (1D vector per batch)
// dst: [N, N, ne2, ne3] (diagonal matrix per batch)
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_diag_params {
struct ggml_tensor src0; // F32 input vector
struct ggml_tensor dst; // F32 output diagonal matrix
};
int entry_point(struct ggml_et_diag_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
const int64_t ne0 = dst->ne[0]; // N (row width = column count)
const int64_t ne1 = dst->ne[1]; // N (number of rows)
const int64_t ne2 = dst->ne[2];
const int64_t ne3 = dst->ne[3];
const size_t nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
const size_t nb02 = src0->nb[2], nb03 = src0->nb[3];
// Total rows across all batches — parallelize over these
const int64_t total_rows = ne1 * ne2 * ne3;
// Prepare zero vector for SIMD zeroing
float zero = 0.0f;
__asm__ volatile("fbc.ps f10, %[z]\n" : : [z] "m"(zero) : "f10");
for (int64_t row = thread_id; row < total_rows; row += num_threads) {
int64_t i1 = row % ne1;
int64_t i2 = (row / ne1) % ne2;
int64_t i3 = row / (ne1 * ne2);
float * dst_row = (float *) ((char *) dst_data + i1 * nb1 + i2 * nb2 + i3 * nb3);
// Zero the entire row with SIMD
int64_t i0 = 0;
const int64_t vec_end = (ne0 / 8) * 8;
for (; i0 < vec_end; i0 += 8) {
__asm__ volatile("fsw.ps f10, %[d]\n" : [d] "=m"(*(float (*)[8]) & dst_row[i0])::"f10");
}
for (; i0 < ne0; i0++) {
dst_row[i0] = 0.0f;
}
// Place the diagonal element: dst[i1][i1] = src0[i1]
const float * src_ptr = (const float *) ((const char *) src0_data + i2 * nb02 + i3 * nb03);
dst_row[i1] = src_ptr[i1];
}
return 0;
}
@@ -0,0 +1,377 @@
// Element-wise operations: dst[i] = src0[i] op src1[i]
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
// Generic m0-gated element-wise block operation.
// The OP parameter selects the instruction: "fmul.ps", "fadd.ps", "fsub.ps".
#define DEFINE_BLOCK_OP(name, op_insn) \
static inline void name(float * dst_block, const float * src0_block, const float * src1_block, int elements) { \
const int32_t vec_end = (elements / 8) * 8; \
const int32_t tail = elements - vec_end; \
\
unsigned long temp_mask; \
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask)); \
__asm__ volatile("mov.m.x m0, x0, 0xFF"); \
\
for (int32_t i = 0; i < vec_end; i += 8) { \
__asm__ volatile( \
"flw.ps f10, %[s0]\n" \
"flw.ps f11, %[s1]\n" op_insn \
" f12, f10, f11\n" \
"fsw.ps f12, %[d]\n" \
: [d] "=m"(*(float (*)[8]) & dst_block[i]) \
: [s0] "m"(*(const float (*)[8]) & src0_block[i]), [s1] "m"(*(const float (*)[8]) & src1_block[i]) \
: "f10", "f11", "f12"); \
} \
/* Deal with tail chunks */ \
if (tail > 0) { \
const unsigned long tail_m0 = (1ul << tail) - 1; \
__asm__ volatile( \
"mov.m.x m0, %[tm], 0\n" \
"flw.ps f10, 0(%[s0])\n" \
"flw.ps f11, 0(%[s1])\n" op_insn \
" f12, f10, f11\n" \
"fsw.ps f12, 0(%[d])\n" \
: \
: [s0] "r"(&src0_block[vec_end]), [s1] "r"(&src1_block[vec_end]), [d] "r"(&dst_block[vec_end]), \
[tm] "r"(tail_m0) \
: "f10", "f11", "f12", "memory"); \
} \
\
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask)); \
}
DEFINE_BLOCK_OP(block_mul_cache_aligned, "fmul.ps")
DEFINE_BLOCK_OP(block_add_cache_aligned, "fadd.ps")
DEFINE_BLOCK_OP(block_sub_cache_aligned, "fsub.ps")
// Broadcast variants: src1 is a single scalar, broadcast to all 8 lanes.
#define DEFINE_BLOCK_OP_BROADCAST(name, op_insn) \
static inline void name(float * dst_block, const float * src0_block, float scalar, int elements) { \
const int32_t vec_end = (elements / 8) * 8; \
const int32_t tail = elements - vec_end; \
\
unsigned long temp_mask; \
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask)); \
__asm__ volatile("mov.m.x m0, x0, 0xFF"); \
\
for (int32_t i = 0; i < vec_end; i += 8) { \
__asm__ volatile( \
"flw.ps f10, %[s0]\n" \
"fbc.ps f11, %[s]\n" op_insn \
" f12, f10, f11\n" \
"fsw.ps f12, %[d]\n" \
: [d] "=m"(*(float (*)[8]) & dst_block[i]) \
: [s0] "m"(*(const float (*)[8]) & src0_block[i]), [s] "m"(scalar) \
: "f10", "f11", "f12"); \
} \
\
if (tail > 0) { \
const unsigned long tail_m0 = (1ul << tail) - 1; \
__asm__ volatile( \
"mov.m.x m0, %[tm], 0\n" \
"flw.ps f10, 0(%[s0])\n" \
"fbc.ps f11, 0(%[ps])\n" op_insn \
" f12, f10, f11\n" \
"fsw.ps f12, 0(%[d])\n" \
: \
: [s0] "r"(&src0_block[vec_end]), [ps] "r"(&scalar), [d] "r"(&dst_block[vec_end]), [tm] "r"(tail_m0) \
: "f10", "f11", "f12", "memory"); \
} \
\
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask)); \
}
DEFINE_BLOCK_OP_BROADCAST(block_mul_broadcast, "fmul.ps")
DEFINE_BLOCK_OP_BROADCAST(block_add_broadcast, "fadd.ps")
DEFINE_BLOCK_OP_BROADCAST(block_sub_broadcast, "fsub.ps")
static inline float scalar_el_map(float src0, float src1, enum ggml_op operation) {
switch (operation) {
case GGML_OP_MUL:
return src0 * src1;
case GGML_OP_ADD:
return src0 + src1;
case GGML_OP_SUB:
return src0 - src1;
default:
return 0.0f;
}
}
int entry_point(struct ggml_et_binary_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * src1 = &params->src1;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1; // Unsupported type combination
}
float * src0_data = (float *) src0->data;
float * src1_data = (float *) src1->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !src1_data || !dst_data) {
return -1; // Null data pointer
}
#ifdef ET_UBERKERNEL
// Consumer-side input eviction. Required because ET caches are
// incoherent across minions: if a previous kernel in this UK batch
// left stale lines for these addresses in this hart's L1, drop them
// so we read fresh from L3/DRAM (where the producer flushed its
// results). Standalone launches don't need this -- the host-side
// runtime boundary between kernel launches handles it.
const size_t src0_bytes = (size_t) src0->ne[0] * src0->ne[1] * src0->ne[2] * src0->ne[3] * src0->nb[0];
const size_t src1_bytes = (size_t) src1->ne[0] * src1->ne[1] * src1->ne[2] * src1->ne[3] * src1->nb[0];
evict_region_past_l2(src0_data, src0_bytes);
evict_region_past_l2(src1_data, src1_bytes);
WAIT_CACHEOPS;
FENCE;
et_barrier(ET_BARRIER_GLOBAL);
#endif
enum ggml_op operation = dst->op;
if (operation != GGML_OP_MUL && operation != GGML_OP_ADD && operation != GGML_OP_SUB) {
return -1; // Unsupported operation
}
const int64_t ne0 = dst->ne[0], ne1 = dst->ne[1], ne2 = dst->ne[2], ne3 = dst->ne[3];
const int64_t ne00 = src0->ne[0], ne01 = src0->ne[1], ne02 = src0->ne[2], ne03 = src0->ne[3];
const int64_t ne10 = src1->ne[0], ne11 = src1->ne[1], ne12 = src1->ne[2], ne13 = src1->ne[3];
const size_t nb0 = dst->nb[0], nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
const size_t nb00 = src0->nb[0], nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
const size_t nb10 = src1->nb[0], nb11 = src1->nb[1], nb12 = src1->nb[2], nb13 = src1->nb[3];
const bool cache_aligned = (dst->ne[0] % 16 == 0);
// Fast path: no broadcasting, contiguous
const bool no_broadcast = (ne10 == ne0 && ne11 == ne1 && ne12 == ne2 && ne13 == ne3);
const bool all_contiguous =
(nb0 == 4 && nb00 == 4 && nb10 == 4 && nb1 == ne0 * 4 && nb01 == ne0 * 4 && nb11 == ne0 * 4);
if (no_broadcast && all_contiguous) {
const int64_t total_elements = ne0 * ne1 * ne2 * ne3;
const int64_t elements_per_cacheline = 16; // 64 bytes / 4 bytes
const int64_t total_cachelines = (total_elements + elements_per_cacheline - 1) / elements_per_cacheline;
const int64_t cl_per_thread = (total_cachelines + num_threads - 1) / num_threads;
const int64_t cl_start = thread_id * cl_per_thread;
int64_t cl_end = cl_start + cl_per_thread;
if (cl_end > total_cachelines) {
cl_end = total_cachelines;
}
if (cl_start >= total_cachelines) {
return 0;
}
const int64_t elem_start = cl_start * elements_per_cacheline;
int64_t elem_end = cl_end * elements_per_cacheline;
if (elem_end > total_elements) {
elem_end = total_elements;
}
const int32_t count = (int32_t) (elem_end - elem_start);
switch (operation) {
case GGML_OP_MUL:
block_mul_cache_aligned(dst_data + elem_start, src0_data + elem_start, src1_data + elem_start, count);
break;
case GGML_OP_ADD:
block_add_cache_aligned(dst_data + elem_start, src0_data + elem_start, src1_data + elem_start, count);
break;
case GGML_OP_SUB:
block_sub_cache_aligned(dst_data + elem_start, src0_data + elem_start, src1_data + elem_start, count);
break;
default:
return 1;
}
#ifdef ET_UBERKERNEL
// Producer-side flush: ET caches are incoherent across minions, so
// a consumer kernel running on a different minion can't see our
// dirty L1 lines via its own evict_region_past_l2. Push our writes
// all the way to DRAM so the next batched kernel reads fresh.
// Standalone launches don't need this -- the host runtime boundary
// between kernel launches handles cache writeback.
FENCE;
evict_region_past_l2(dst_data + elem_start, (size_t) count * sizeof(float));
WAIT_CACHEOPS;
FENCE;
#endif
return 0;
}
// Slow path: broadcasting or non-contiguous
const int64_t total_rows = ne1 * ne2 * ne3;
int64_t start_row;
int64_t end_row;
if (cache_aligned) {
const int64_t rows_per_thread = (total_rows + num_threads - 1) / num_threads;
start_row = thread_id * rows_per_thread;
end_row = (start_row + rows_per_thread < total_rows) ? (start_row + rows_per_thread) : total_rows;
} else {
const int64_t rows_per_group = et_rows_per_cacheline_group(ne0, sizeof(float));
const int64_t total_groups = (total_rows + rows_per_group - 1) / rows_per_group;
if (thread_id >= total_groups) {
return 0;
}
const int64_t group_start = thread_id;
for (int64_t grp = group_start; grp < total_groups; grp += num_threads) {
const int64_t group_row_start = grp * rows_per_group;
int64_t group_row_end = group_row_start + rows_per_group;
if (group_row_end > total_rows) {
group_row_end = total_rows;
}
#ifdef ET_UBERKERNEL
// First row written by this group (used for producer-side evict).
const int64_t first_i03 = group_row_start / (ne2 * ne1);
const int64_t first_i02 = (group_row_start - first_i03 * ne2 * ne1) / ne1;
const int64_t first_i01 = (group_row_start - first_i03 * ne2 * ne1 - first_i02 * ne1);
char * group_dst_base = (char *) dst_data + first_i03 * nb3 + first_i02 * nb2 + first_i01 * nb1;
#endif
for (int64_t ir = group_row_start; ir < group_row_end; ir++) {
const int64_t i03 = ir / (ne2 * ne1);
const int64_t i02 = (ir - i03 * ne2 * ne1) / ne1;
const int64_t i01 = (ir - i03 * ne2 * ne1 - i02 * ne1);
const int64_t i13 = i03 % ne13;
const int64_t i12 = i02 % ne12;
const int64_t i11 = i01 % ne11;
float * dst_ptr = (float *) ((char *) dst_data + i03 * nb3 + i02 * nb2 + i01 * nb1);
const float * src0_ptr =
(const float *) ((const char *) src0_data + i03 * nb03 + i02 * nb02 + i01 * nb01);
const float * src1_ptr =
(const float *) ((const char *) src1_data + i13 * nb13 + i12 * nb12 + i11 * nb11);
if (ne10 == 1) {
const float scalar = src1_ptr[0];
for (int64_t i0 = 0; i0 < ne0; ++i0) {
dst_ptr[i0] = scalar_el_map(src0_ptr[i0], scalar, operation);
}
} else {
for (int64_t i0 = 0; i0 < ne0; ++i0) {
dst_ptr[i0] = scalar_el_map(src0_ptr[i0], src1_ptr[i0 % ne10], operation);
}
}
}
#ifdef ET_UBERKERNEL
// Producer-side flush for this group's rows. Group rows are
// contiguous because nb1 = ne0*4 in the cacheline-group layout.
// Only needed inside a UK batch; see comment in fast path.
const int64_t nrows = group_row_end - group_row_start;
if (nrows > 0) {
FENCE;
evict_region_past_l2(group_dst_base, (size_t) nrows * nb1);
WAIT_CACHEOPS;
FENCE;
}
#endif
}
return 0;
}
if (start_row >= total_rows) {
return 0;
}
for (int64_t ir = start_row; ir < end_row; ir++) {
// Convert flat row index to 3D coordinates
const int64_t i03 = ir / (ne2 * ne1);
const int64_t i02 = (ir - i03 * ne2 * ne1) / ne1;
const int64_t i01 = (ir - i03 * ne2 * ne1 - i02 * ne1);
// Handle broadcasting: src1 coordinates with modulo
const int64_t i13 = i03 % ne13;
const int64_t i12 = i02 % ne12;
const int64_t i11 = i01 % ne11;
// Calculate base pointers for this row using stride-based addressing
float * dst_ptr = (float *) ((char *) dst_data + i03 * nb3 + i02 * nb2 + i01 * nb1);
const float * src0_ptr = (const float *) ((const char *) src0_data + i03 * nb03 + i02 * nb02 + i01 * nb01);
const float * src1_ptr = (const float *) ((const char *) src1_data + i13 * nb13 + i12 * nb12 + i11 * nb11);
if (ne10 == 1) {
// Broadcast scalar: src1 has ne[0]=1, broadcast across entire row
float scalar = src1_ptr[0];
switch (operation) {
case GGML_OP_MUL:
block_mul_broadcast(dst_ptr, src0_ptr, scalar, (int) ne0);
break;
case GGML_OP_ADD:
block_add_broadcast(dst_ptr, src0_ptr, scalar, (int) ne0);
break;
case GGML_OP_SUB:
block_sub_broadcast(dst_ptr, src0_ptr, scalar, (int) ne0);
break;
default:
return 1;
}
} else {
// Broadcasting in dimension 0: src1 repeats across src0
const int64_t nr0 = ne0 / ne10;
for (int64_t r = 0; r < nr0; r++) {
const float * src0_block = src0_ptr + r * ne10;
float * dst_block = dst_ptr + r * ne10;
switch (operation) {
case GGML_OP_MUL:
block_mul_cache_aligned(dst_block, src0_block, src1_ptr, (int) ne10);
break;
case GGML_OP_ADD:
block_add_cache_aligned(dst_block, src0_block, src1_ptr, (int) ne10);
break;
case GGML_OP_SUB:
block_sub_cache_aligned(dst_block, src0_block, src1_ptr, (int) ne10);
break;
default:
return 1;
}
}
}
}
#ifdef ET_UBERKERNEL
// Producer-side flush for the cache-aligned slow path. Rows
// [start_row, end_row) are contiguous in dst because nb1 = ne0 * 4.
// Only needed inside a UK batch; see comment in fast path.
if (end_row > start_row) {
FENCE;
evict_region_past_l2((char *) dst_data + start_row * nb1, (size_t) (end_row - start_row) * nb1);
WAIT_CACHEOPS;
FENCE;
}
#endif
return 0;
}
@@ -0,0 +1,87 @@
//******************************************************************************
// Fill F32 Kernel
// Fills entire tensor with a constant scalar value.
// dst[i] = c for all elements
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_fill_params {
struct ggml_tensor dst; // F32 output tensor (contiguous)
float c; // Constant value to fill
};
int entry_point(struct ggml_et_fill_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * dst = &params->dst;
if (dst->type != GGML_TYPE_F32) {
return -1;
}
float * dst_data = (float *) dst->data;
if (!dst_data) {
return -1;
}
const int64_t total_elements = dst->ne[0] * dst->ne[1] * dst->ne[2] * dst->ne[3];
if (total_elements == 0) {
return 0;
}
// Distribute by cache lines (16 floats = 64 bytes)
const int64_t elems_per_cl = 16;
const int64_t total_cl = (total_elements + elems_per_cl - 1) / elems_per_cl;
const int64_t cl_per_thread = (total_cl + num_threads - 1) / num_threads;
const int64_t cl_start = thread_id * cl_per_thread;
int64_t cl_end = cl_start + cl_per_thread;
if (cl_end > total_cl) {
cl_end = total_cl;
}
if (cl_start >= total_cl) {
return 0;
}
const int64_t es = cl_start * elems_per_cl;
int64_t ee = cl_end * elems_per_cl;
if (ee > total_elements) {
ee = total_elements;
}
// Broadcast constant to all SIMD lanes
float c = params->c;
__asm__ volatile("fbc.ps f10, %[v]\n" : : [v] "m"(c) : "f10");
// Vector fill (8-wide)
int64_t i = es;
const int64_t vec_end = es + ((ee - es) / 8) * 8;
for (; i < vec_end; i += 8) {
__asm__ volatile("fsw.ps f10, %[d]\n" : [d] "=m"(*(float (*)[8]) & dst_data[i])::"f10");
}
// Scalar tail
for (; i < ee; i++) {
dst_data[i] = c;
}
return 0;
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,217 @@
//******************************************************************************
// F32 Flash Attention for ET backend
//
// Supports:
// - arbitrary dk/dv (up to 128)
// - GQA (n_head_q can differ from n_head_kv)
// - mask (F16 or F32, causal pattern)
// - F16 or F32 K and V (with non-contiguous strides from KV cache permute)
//
// Limitations:
// - Q and dst must be F32
// - no sinks, ALiBi, logit softcap
//
// Parallelization strategy:
// - flatten [query position, head, outer batch] into independent rows
// - assign rows round-robin across ET threads
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdbool.h>
#include <stdint.h>
struct ggml_et_flash_attn_ext_params {
struct ggml_tensor src0; // Q tensor (F32)
struct ggml_tensor src1; // K tensor (F16 or F32)
struct ggml_tensor src2; // V tensor (F16 or F32)
struct ggml_tensor mask; // mask tensor (F16 or F32), zeroed when absent
struct ggml_tensor dst; // Output tensor (F32)
float scale; // Scale factor applied to QK
int32_t has_mask; // nonzero if mask is present
};
// Maximum head dimension supported (128 covers all common LLMs).
#define FA_DV_MAX 128
// Read element d from a row, handling F16 or F32 type.
// row_base points to the start of the row (byte address).
// nb0 is the stride per element (2 for F16, 4 for F32).
static inline float read_kv_f32(const char * row_base, int64_t d, int64_t nb0, int type) {
if (type == GGML_TYPE_F32) {
return *(const float *) (row_base + d * nb0);
}
// F16
return fp16_to_fp32(*(const uint16_t *) (row_base + d * nb0));
}
// Dot product of F32 query vector with a K row (F16 or F32).
static inline float dot_qk(const float * q, const char * k_row, int64_t dk, int64_t k_nb0, int k_type) {
float acc = 0.0f;
if (k_type == GGML_TYPE_F32) {
const float * kf = (const float *) k_row;
for (int64_t i = 0; i < dk; ++i) {
acc += q[i] * kf[i];
}
} else {
// F16 stride-aware read
for (int64_t i = 0; i < dk; ++i) {
acc += q[i] * fp16_to_fp32(*(const uint16_t *) (k_row + i * k_nb0));
}
}
return acc;
}
static inline float get_mask_val(const struct ggml_tensor * mask, int64_t iq1, int64_t ik1, int64_t iq2, int64_t iq3) {
// mask layout: [nk, nq, ne2, ne3] -> broadcast via modulo
const char * base = (const char *) mask->data + iq1 * mask->nb[1] + (iq2 % mask->ne[2]) * mask->nb[2] +
(iq3 % mask->ne[3]) * mask->nb[3];
if (mask->type == GGML_TYPE_F32) {
return *(const float *) (base + ik1 * mask->nb[0]);
}
// F16
return fp16_to_fp32(*(const uint16_t *) (base + ik1 * mask->nb[0]));
}
int entry_point(struct ggml_et_flash_attn_ext_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env || !params) {
return -1;
}
const int thread_id = get_relative_thread_id(kernel_env->shire_mask);
const int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0 || num_threads <= 0) {
return 0;
}
struct ggml_tensor * q = &params->src0;
struct ggml_tensor * k = &params->src1;
struct ggml_tensor * v = &params->src2;
struct ggml_tensor * dst = &params->dst;
const int32_t has_mask = params->has_mask;
struct ggml_tensor * mask = has_mask ? &params->mask : (struct ggml_tensor *) 0;
const char * q_data = (const char *) q->data;
const char * k_data = (const char *) k->data;
const char * v_data = (const char *) v->data;
char * dst_data = (char *) dst->data;
const int k_type = k->type;
const int v_type = v->type;
const int64_t k_nb0 = k->nb[0];
const int64_t v_nb0 = v->nb[0];
const int64_t dk = q->ne[0]; // head dim for keys/queries
const int64_t nq = q->ne[1]; // number of query positions
const int64_t nhq = q->ne[2]; // number of query heads
const int64_t no = q->ne[3]; // outer batch
const int64_t nk = k->ne[1]; // number of key/value positions
const int64_t nhk = k->ne[2]; // number of kv heads
const int64_t dv = v->ne[0]; // head dim for values
if (dv > FA_DV_MAX) {
return -1;
}
// GQA: query heads per kv head
const int64_t gqa_ratio = nhq / nhk;
const int64_t total_rows = nq * nhq * no;
const float scale = params->scale;
// When dv is a multiple of 16 (64 bytes = cache line), output rows are
// cache-line aligned and we can use fast normal stores. Otherwise we must
// use atomic stores to avoid cache-line sharing corruption.
const int use_fast_store = (dv % 16 == 0);
for (int64_t row = thread_id; row < total_rows; row += num_threads) {
const int64_t iq3 = row / (nhq * nq);
const int64_t rem = row % (nhq * nq);
const int64_t iq2 = rem / nq; // query head index
const int64_t iq1 = rem % nq; // query position
// Map query head -> kv head for GQA
const int64_t ik2 = iq2 / gqa_ratio;
// Q is always F32
const float * pq = (const float *) (q_data + iq1 * q->nb[1] + iq2 * q->nb[2] + iq3 * q->nb[3]);
// dst layout: [dv, nhq, nq, no]
float * out = (float *) (dst_data + iq2 * dst->nb[1] + iq1 * dst->nb[2] + iq3 * dst->nb[3]);
// Base byte offsets for K and V head+batch slice
const int64_t kv_base = ik2 * k->nb[2] + iq3 * k->nb[3];
const int64_t vv_base = ik2 * v->nb[2] + iq3 * v->nb[3];
float acc[FA_DV_MAX];
for (int64_t d = 0; d < dv; ++d) {
acc[d] = 0.0f;
}
float M = -3.402823466e+38f;
float S = 0.0f;
for (int64_t ik1 = 0; ik1 < nk; ++ik1) {
// If mask is present, check for -inf (skip masked positions)
float mask_val = 0.0f;
if (has_mask) {
mask_val = get_mask_val(mask, iq1, ik1, iq2, iq3);
// llama.cpp uses -inf for masked positions
if (mask_val == -3.402823466e+38f || mask_val != mask_val) {
continue;
}
}
const char * pk = k_data + ik1 * k->nb[1] + kv_base;
const char * pv = v_data + ik1 * v->nb[1] + vv_base;
float s = dot_qk(pq, pk, dk, k_nb0, k_type) * scale + mask_val;
const float Mold = M;
float ms = 1.0f;
float vs = 1.0f;
if (s > M) {
M = s;
ms = et_expf(Mold - M);
for (int64_t d = 0; d < dv; ++d) {
acc[d] *= ms;
}
} else {
vs = et_expf(s - M);
}
// Accumulate weighted V
if (v_type == GGML_TYPE_F32) {
const float * pvf = (const float *) pv;
for (int64_t d = 0; d < dv; ++d) {
acc[d] += pvf[d] * vs;
}
} else {
for (int64_t d = 0; d < dv; ++d) {
acc[d] += fp16_to_fp32(*(const uint16_t *) (pv + d * v_nb0)) * vs;
}
}
S = S * ms + vs;
}
const float S_inv = S == 0.0f ? 0.0f : et_fdiv(1.0f, S);
if (use_fast_store) {
for (int64_t d = 0; d < dv; ++d) {
out[d] = acc[d] * S_inv;
}
} else {
for (int64_t d = 0; d < dv; ++d) {
atomic_store_f32((volatile float *) &out[d], acc[d] * S_inv);
}
}
}
return 0;
}
@@ -0,0 +1,346 @@
//******************************************************************************
// Gated Delta Net F32 Kernel
//
// Implements the gated delta rule recurrence:
// For each head h, timestep t:
// 1. Gate decay: S *= exp(g) (scalar or per-element KDA)
// 2. Delta update: delta[j] = (v[j] - dot(S_row_j, k)) * beta
// 3. Outer product: S_row_j += k * delta[j]
// 4. Attention: attn[j] = dot(S_row_j, q) * scale
//
// State is stored transposed: s_out[j*S_v + i] = S[i][j]
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_gated_delta_net_params {
struct ggml_tensor q; // [S_v, H_q, n_tokens, n_seqs_q]
struct ggml_tensor k; // [S_v, H_k, n_tokens, n_seqs_k]
struct ggml_tensor v; // [S_v, H, n_tokens, n_seqs]
struct ggml_tensor g; // [1 or S_v, H, n_tokens, n_seqs]
struct ggml_tensor beta; // [1, H, n_tokens, n_seqs]
struct ggml_tensor state_in; // [S_v*S_v*H, K, n_seqs]
struct ggml_tensor dst; // [S_v*H, n_tokens*n_seqs + S_v*n_seqs*K]
int32_t S_v; // head dimension
int32_t H; // number of value heads
int32_t H_q; // number of Q heads
int32_t H_k; // number of K heads
int32_t n_tokens; // total tokens
int32_t n_seqs; // number of sequences
int32_t n_seqs_q; // Q sequence count
int32_t n_seqs_k; // K sequence count
int32_t kda; // 1 if per-element gate, 0 if scalar
int32_t K; // snapshot slot count
float scale; // 1/sqrt(S_v)
};
static inline float hsum_f10(void) {
float result;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(result)::"t0", "f1", "f2", "f3", "f4", "f5");
return result;
}
int entry_point(struct ggml_et_gated_delta_net_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
const struct ggml_tensor * q_tsr = &params->q;
const struct ggml_tensor * k_tsr = &params->k;
const struct ggml_tensor * v_tsr = &params->v;
const struct ggml_tensor * g_tsr = &params->g;
const struct ggml_tensor * beta_tsr = &params->beta;
const struct ggml_tensor * state_tsr = &params->state_in;
const struct ggml_tensor * dst_tsr = &params->dst;
const float * q = (const float *) q_tsr->data;
const float * k = (const float *) k_tsr->data;
const float * v = (const float *) v_tsr->data;
const float * g = (const float *) g_tsr->data;
const float * beta = (const float *) beta_tsr->data;
const float * state_in = (const float *) state_tsr->data;
float * dst_data = (float *) dst_tsr->data;
const int32_t S_v = params->S_v;
const int32_t H = params->H;
const int32_t H_q = params->H_q;
const int32_t H_k = params->H_k;
const int32_t n_tokens = params->n_tokens;
const int32_t n_seqs = params->n_seqs;
const int32_t n_seqs_q = params->n_seqs_q;
const int32_t n_seqs_k = params->n_seqs_k;
const int32_t kda = params->kda;
const int32_t K = params->K;
const float scale = params->scale;
if (!q || !k || !v || !g || !beta || !state_in || !dst_data) {
return -1;
}
// Preserve the original contract for every tensor except q, k, and v, which may be
// row-contiguous with strided higher dimensions.
if (q_tsr->nb[0] != sizeof(float) || k_tsr->nb[0] != sizeof(float) || v_tsr->nb[0] != sizeof(float) ||
g_tsr->nb[0] != sizeof(float) || beta_tsr->nb[0] != sizeof(float) || state_tsr->nb[0] != sizeof(float) ||
dst_tsr->nb[0] != sizeof(float)) {
return -1;
}
const int32_t attn_elems = S_v * H * n_tokens * n_seqs;
float * attn_out_base = dst_data;
float * state_out_base = dst_data + attn_elems;
const int32_t state_plane_floats = S_v * S_v * H * n_seqs;
const int32_t G0 = kda ? S_v : 1;
const size_t q_nb1 = q_tsr->nb[1];
const size_t q_nb2 = q_tsr->nb[2];
const size_t q_nb3 = q_tsr->nb[3];
const size_t k_nb1 = k_tsr->nb[1];
const size_t k_nb2 = k_tsr->nb[2];
const size_t k_nb3 = k_tsr->nb[3];
const size_t v_nb1 = v_tsr->nb[1];
const size_t v_nb2 = v_tsr->nb[2];
const size_t v_nb3 = v_tsr->nb[3];
const int32_t g_stride_h = G0;
const int32_t g_stride_t = G0 * H;
const int32_t g_stride_s = G0 * H * n_tokens;
const int32_t b_stride_t = H;
const int32_t b_stride_s = H * n_tokens;
float exp_g_buf[128];
// FP and SIMD share the same register file. Scalar FP needs the default
// mask; 8-wide .ps blocks need m0=255. Save once, toggle at boundaries.
unsigned long default_mask;
__asm__ volatile("mova.x.m %[ms]\n" : [ms] "=r"(default_mask));
// Parallelize over (j_block, head, seq). J_BLK must satisfy two separate
// cache-line alignment constraints at once:
// (a) State: J_BLK consecutive rows of s_out (each S_v floats) span an
// integer number of cache lines. For S_v * sizeof(float) >= 64 this
// is trivially any J_BLK >= 1.
// (b) Attention output: each j writes exactly one float into
// attn_ptr[j], which is densely packed. If J_BLK * sizeof(float) is
// less than a cache line, distinct threads will share a line and
// race on scalar stores — ET's L1 isn't coherent so we lose writes.
//
// (b) dominates: J_BLK must be at least ET_CACHE_LINE_SIZE_BYTES / 4 so
// that each thread owns a whole cache line of attn_ptr. That's 16 on
// ET-SoC-1, and it's also a whole number of state rows for every
// S_v >= 1, so (a) is automatically satisfied.
const int32_t J_BLK = ET_CACHE_LINE_SIZE_BYTES / (int32_t) sizeof(float);
const int32_t n_j_blocks = (S_v + J_BLK - 1) / J_BLK;
const int32_t total_work = n_j_blocks * H * n_seqs;
for (int32_t ir = thread_id; ir < total_work; ir += num_threads) {
const int32_t jb = ir % n_j_blocks;
const int32_t head = (ir / n_j_blocks) % H;
const int32_t seq = ir / (n_j_blocks * H);
const int32_t j_start = jb * J_BLK;
const int32_t j_end = (j_start + J_BLK < S_v) ? j_start + J_BLK : S_v;
const int32_t h_q = head % H_q;
const int32_t h_k = head % H_k;
const int32_t seq_q = (n_seqs_q == n_seqs) ? seq : (seq * n_seqs_q / n_seqs);
const int32_t seq_k = (n_seqs_k == n_seqs) ? seq : (seq * n_seqs_k / n_seqs);
const int32_t head_state_off = (seq * H + head) * S_v * S_v;
// Live RMW buffer = first snapshot plane (slot 0).
float * s_out = state_out_base + head_state_off;
// Input state: seq `seq`, head `head`.
const float * s_in = state_in + head_state_off;
// Skip the explicit s_in -> s_out copy. At t=0 pass A/B read through
// src_state = s_in; pass B writes the first new row to s_out. From
// t=1 onward src_state flips to s_out (read-modify-write in place).
const float * src_state = s_in;
const int32_t attn_stride_t = S_v * H;
float * attn_ptr = attn_out_base + (seq * n_tokens * H + head) * S_v;
const float zero = 0.0f;
for (int32_t t = 0; t < n_tokens; t++) {
const float * q_t = (const float *) ((const char *) q + seq_q * q_nb3 + t * q_nb2 + h_q * q_nb1);
const float * k_t = (const float *) ((const char *) k + seq_k * k_nb3 + t * k_nb2 + h_k * k_nb1);
const float * v_t = (const float *) ((const char *) v + seq * v_nb3 + t * v_nb2 + head * v_nb1);
const float * g_t = g + seq * g_stride_s + t * g_stride_t + head * g_stride_h;
const float beta_val = beta[seq * b_stride_s + t * b_stride_t + head];
// Precompute per-element gate for the kda path; scalar decay
// otherwise. Decay is fused into per-j pass A/B below, not
// applied to state in a separate pre-pass.
float decay = 0.0f; // only used when !kda
if (kda) {
const float log2e = 1.4426950408889634f;
__asm__ volatile("mov.m.x m0, x0, 255\n" :::);
__asm__ volatile("fbc.ps f20, %[l2e]\n" : : [l2e] "m"(log2e) : "f20");
for (int32_t i = 0; i < S_v; i += 8) {
__asm__ volatile(
"flw.ps f10, %[g_vec]\n"
"fmul.ps f10, f10, f20, rne\n"
"fexp.ps f10, f10\n"
"fsw.ps f10, %[out]\n"
: [out] "=m"(*(float (*)[8]) & exp_g_buf[i])
: [g_vec] "m"(*(const float (*)[8]) & g_t[i])
: "f10");
}
__asm__ volatile("mova.m.x %[ms]\n" : : [ms] "r"(default_mask));
} else {
decay = et_expf(g_t[0]);
}
for (int32_t j = j_start; j < j_end; j++) {
const float * src_row = src_state + j * S_v;
float * dst_row = s_out + j * S_v;
__asm__ volatile("mov.m.x m0, x0, 255\n" :::);
if (kda) {
__asm__ volatile("fbc.ps f10, %[z]\n" : : [z] "m"(zero) : "f10");
for (int32_t i = 0; i < S_v; i += 8) {
__asm__ volatile(
"flw.ps f11, %[s_vec]\n"
"flw.ps f12, %[g_vec]\n"
"flw.ps f13, %[k_vec]\n"
"fmul.ps f11, f11, f12\n" // row_dec = row * g
"fmadd.ps f10, f11, f13, f10\n" // acc += row_dec * k
:
: [s_vec] "m"(*(const float (*)[8]) & src_row[i]),
[g_vec] "m"(*(const float (*)[8]) & exp_g_buf[i]),
[k_vec] "m"(*(const float (*)[8]) & k_t[i])
: "f10", "f11", "f12", "f13");
}
} else {
__asm__ volatile(
"fbc.ps f10, %[z]\n"
"fbc.ps f22, %[d]\n"
:
: [z] "m"(zero), [d] "m"(decay)
: "f10", "f22");
for (int32_t i = 0; i < S_v; i += 8) {
__asm__ volatile(
"flw.ps f11, %[s_vec]\n"
"flw.ps f13, %[k_vec]\n"
"fmul.ps f11, f11, f22\n" // row_dec = row * decay
"fmadd.ps f10, f11, f13, f10\n" // acc += row_dec * k
:
: [s_vec] "m"(*(const float (*)[8]) & src_row[i]), [k_vec] "m"(*(const float (*)[8]) &
k_t[i])
: "f10", "f11", "f13");
}
}
float dot_sk = hsum_f10();
__asm__ volatile("mova.m.x %[ms]\n" : : [ms] "r"(default_mask));
float delta_j = (v_t[j] - dot_sk) * beta_val;
// -------- Pass B: decay + outer product + attn --------
__asm__ volatile("mov.m.x m0, x0, 255\n" :::);
if (kda) {
__asm__ volatile(
"fbc.ps f10, %[z]\n"
"fbc.ps f21, %[dj]\n"
:
: [z] "m"(zero), [dj] "m"(delta_j)
: "f10", "f21");
for (int32_t i = 0; i < S_v; i += 8) {
__asm__ volatile(
"flw.ps f11, %[s_vec]\n"
"flw.ps f12, %[g_vec]\n"
"flw.ps f13, %[k_vec]\n"
"flw.ps f14, %[q_vec]\n"
"fmul.ps f11, f11, f12\n" // row_dec = row * g
"fmadd.ps f11, f13, f21, f11\n" // row_new = row_dec + k*delta_j
"fsw.ps f11, %[s_out]\n"
"fmadd.ps f10, f11, f14, f10\n" // attn_acc += row_new * q
: [s_out] "=m"(*(float (*)[8]) & dst_row[i])
: [s_vec] "m"(*(const float (*)[8]) & src_row[i]),
[g_vec] "m"(*(const float (*)[8]) & exp_g_buf[i]),
[k_vec] "m"(*(const float (*)[8]) & k_t[i]), [q_vec] "m"(*(const float (*)[8]) & q_t[i])
: "f10", "f11", "f12", "f13", "f14");
}
} else {
__asm__ volatile(
"fbc.ps f10, %[z]\n"
"fbc.ps f21, %[dj]\n"
"fbc.ps f22, %[d]\n"
:
: [z] "m"(zero), [dj] "m"(delta_j), [d] "m"(decay)
: "f10", "f21", "f22");
for (int32_t i = 0; i < S_v; i += 8) {
__asm__ volatile(
"flw.ps f11, %[s_vec]\n"
"flw.ps f13, %[k_vec]\n"
"flw.ps f14, %[q_vec]\n"
"fmul.ps f11, f11, f22\n" // row_dec = row * decay
"fmadd.ps f11, f13, f21, f11\n" // row_new = row_dec + k*delta_j
"fsw.ps f11, %[s_out]\n"
"fmadd.ps f10, f11, f14, f10\n" // attn_acc += row_new * q
: [s_out] "=m"(*(float (*)[8]) & dst_row[i])
: [s_vec] "m"(*(const float (*)[8]) & src_row[i]),
[k_vec] "m"(*(const float (*)[8]) & k_t[i]), [q_vec] "m"(*(const float (*)[8]) & q_t[i])
: "f10", "f11", "f13", "f14");
}
}
float attn_val = hsum_f10();
__asm__ volatile("mova.m.x %[ms]\n" : : [ms] "r"(default_mask));
attn_ptr[j] = attn_val * scale;
}
// n-way merge snapshot: live state lives in slot 0 (== s_out).
// Copies state to target snapshot slots [1, K-1] in reverse chronological order.
// target_slot == 0 is the live buffer itself => no copy.
// target_slot >= K (when n_tokens > K) => older slots are discarded.
if (K > 1) {
const int32_t target_slot = (n_tokens - 1) - t;
if (target_slot > 0 && target_slot < K) {
float * snap = state_out_base + target_slot * state_plane_floats + head_state_off;
for (int32_t j = j_start; j < j_end; j++) {
const float * src = s_out + j * S_v;
float * dst = snap + j * S_v;
for (int32_t i = 0; i < S_v; i++) {
dst[i] = src[i];
}
}
}
}
// After t=0, state lives in s_out; flip src_state so subsequent
// timesteps read-modify-write in place.
src_state = s_out;
attn_ptr += attn_stride_t;
}
}
return 0;
}
@@ -0,0 +1,612 @@
//******************************************************************************
// Bare Metal GET_ROWS F32 Kernel
// Extracts specific rows from a source tensor based on row indices
//
// Algorithm:
// 1. Read row indices from src1 (int32 tensor)
// 2. For each index, extract the corresponding row from src0
// 3. Copy the row data to the output tensor dst
// 4. Handle different input types: F32, Q8_0, Q4_0, and Q4_K (quantized)
//
// Operation: dst[i] = src0[indices[i]] for i = 0..num_indices
//
// Features supported:
// - F32 input data (direct copy)
// - Q4_0 quantized input data (dequantized to F32)
// - Q8_0 quantized input data (dequantized to F32)
// - Q4_K quantized input data (dequantized to F32)
// - Int32 row indices
// - Multi-dimensional tensor support
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include "quants.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#define CACHE_LINE_SIZE_BYTES 64
struct ggml_et_get_rows_params {
struct ggml_tensor src0; // Data tensor (F32, Q4_0, Q8_0, or Q4_K)
struct ggml_tensor src1; // Row indices tensor (I32)
struct ggml_tensor dst; // Output tensor (F32)
};
#define CACHE_LINE_SIZE_BYTES 64
#define CACHE_ELEMENTS(elem_size) (CACHE_LINE_SIZE_BYTES / (elem_size))
// Copy a row of F32 data from source to destination
static void copy_f32_row(float * dst, const float * src, int64_t num_elements) {
// Simple memcpy for F32 data - no conversion needed
for (int64_t i = 0; i < num_elements; i++) {
dst[i] = src[i];
}
}
static void copy_f16_row(float * dst, const uint16_t * src, int64_t num_elements) {
for (int64_t i = 0; i < num_elements; i++) {
dst[i] = fp16_to_fp32(src[i]);
}
}
// Copy a row of F32 data from source to destination, aligned to cache line boundaries
// using FP32 load/store instructions. They don't perform data conversion so is fine.
// Requirement: n_bytes is a multiple of CACHE_LINE_SIZE (64 bytes)
static void copy_row_cache_align(float * dst, const float * src, int64_t n_bytes) {
int num_f32_elem = n_bytes / sizeof(float);
// Unrolled to do an entire cache line at a time
__asm__ volatile(
"1: \n\t"
// --- Process 64 Bytes (1 Cache Line) ---
// Load 256 bits (32 bytes) into f0 and the other into f1
"flq2 f0, 0(%[src]) \n\t"
"flq2 f1, 32(%[src]) \n\t"
// Store 256 bits (32 bytes) from f0 and f1
"fsq2 f0, 0(%[dst]) \n\t"
"fsq2 f1, 32(%[dst]) \n\t"
// Increment Pointers by 64 bytes
"addi %[src], %[src], 64 \n\t"
"addi %[dst], %[dst], 64 \n\t"
// Decrement count by 16 elements
"addi %[n], %[n], -16 \n\t"
// Loop if at least 16 elements remain
"bge %[n], %[stride_count], 1b \n\t"
: [dst] "+r"(dst), [src] "+r"(src), [n] "+r"(num_f32_elem)
: [stride_count] "r"(16L)
: "f0", "f1", "memory");
}
// Copied from GGML: copy a row of Q4_0 data to F32 destination (with dequantization)
static void copy_q4_0_row(float * dst, const block_q4_0 * src_blocks, int64_t num_elements) {
const int64_t num_blocks = (num_elements + QK4_0 - 1) / QK4_0;
for (int64_t block_idx = 0; block_idx < num_blocks; block_idx++) {
const int64_t elements_in_block = (block_idx == num_blocks - 1) ? (num_elements - block_idx * QK4_0) : QK4_0;
float temp_buffer[QK4_0];
dequantize_q4_0_block(&src_blocks[block_idx], temp_buffer);
for (int64_t i = 0; i < elements_in_block; i++) {
dst[block_idx * QK4_0 + i] = temp_buffer[i];
}
}
}
// Copy a row of Q8_0 data to F32 destination (with dequantization)
static void copy_q8_0_row(float * dst, const block_q8_0 * src_blocks, int64_t num_elements) {
// Number of Q8_0 blocks needed for this row
const int64_t num_blocks = (num_elements + QK8_0 - 1) / QK8_0; // Round up to handle partial blocks
for (int64_t block_idx = 0; block_idx < num_blocks; block_idx++) {
const int64_t elements_in_block =
(block_idx == num_blocks - 1) ? (num_elements - block_idx * QK8_0) : QK8_0; // Handle last partial block
// Dequantize the block
float temp_buffer[QK8_0];
dequantize_q8_0_block(&src_blocks[block_idx], temp_buffer);
// Copy dequantized values to destination
for (int64_t i = 0; i < elements_in_block; i++) {
dst[block_idx * QK8_0 + i] = temp_buffer[i];
}
}
}
// Copy a row of Q4_K data to F32 destination (with dequantization)
static void copy_q4_K_row(float * dst, const block_q4_K * src_blocks, int64_t num_elements) {
const int64_t num_blocks = (num_elements + QK_K - 1) / QK_K;
for (int64_t block_idx = 0; block_idx < num_blocks; block_idx++) {
const int64_t elements_in_block = (block_idx == num_blocks - 1) ? (num_elements - block_idx * QK_K) : QK_K;
float temp_buffer[QK_K];
dequantize_q4_K_block(&src_blocks[block_idx], temp_buffer);
for (int64_t i = 0; i < elements_in_block; i++) {
dst[block_idx * QK_K + i] = temp_buffer[i];
}
}
}
static void dequantize_q8_0_block_cache_aligned(const block_q8_0 * block, float * dst) {
const int8_t * qs_ptr = block->qs;
uint64_t temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask)); // Save current mask
__asm__ volatile("mov.m.x m0, x0, 0xFF"); // Enable all 8 elements
const int32_t __attribute__((aligned(32))) vec_indices[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
float scale = fp16_to_fp32(block->d);
__asm__ volatile(
"fbcx.ps f0, %0 \n\t" // Broadcast integer scale to all lanes
"flq2 f1, 0(%1) \n\t" // Load gether indicies
::"r"(scale),
"r"(vec_indices)
: "f0", "f1");
for (int i = 0; i < 4; i++) {
__asm__ volatile(
"fgb.ps f2, f1(%0) \n\t" // Loads 8 bytes from (qs_ptr + indices) and sign-extends to 32-bit int.
"fcvt.ps.pw f2, f2, rne \n\t" // Convert Int32 to Float32
"fmul.ps f2, f2, f0 \n\t" // f2 = f2 * f0 (scale)
"fsq2 f2, 0(%1) \n\t" // Store 256 bits (8 floats) to dst.
::"r"(qs_ptr),
"r"(dst)
: "f2", "memory");
// Advance pointers in C
qs_ptr += 8;
dst += 8;
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
}
// Copy a row of Q4_0 data to F32 destination (with dequantization), cache-aligned
static void copy_q4_0_row_cache_aligned(float * dst, const block_q4_0 * src_blocks, int64_t num_elements) {
const int64_t num_blocks = (num_elements + QK4_0 - 1) / QK4_0;
// Scatter byte offsets: even lanes -> dst[j], odd lanes -> dst[j + QK4_0/2]
// For 4 consecutive packed bytes producing [low0, high0, low1, high1, low2, high2, low3, high3]:
// low_i -> byte offset i*4 (positions 0,1,2,3 in first half)
// high_i -> byte offset (16+i)*4 (positions 16,17,18,19 in second half)
const int32_t __attribute__((aligned(32))) scatter_offsets[8] = { 0 * 4, 16 * 4, 1 * 4, 17 * 4,
2 * 4, 18 * 4, 3 * 4, 19 * 4 };
// Gather indices: each byte loaded twice for low/high nibble extraction
const int32_t __attribute__((aligned(32))) gather_indices[8] = { 0, 0, 1, 1, 2, 2, 3, 3 };
uint64_t temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask)); // Save current mask
__asm__ volatile("mov.m.x m0, x0, 0xFF"); // Enable all 8 elements
// Load constant vectors once — shared across all blocks and iterations
__asm__ volatile(
"flq2 f4, 0(%0) \n\t" // f4 = scatter offsets
"flq2 f1, 0(%1) \n\t" // f1 = gather indices {0,0,1,1,2,2,3,3}
::"r"(scatter_offsets),
"r"(gather_indices)
: "f1", "f4");
for (int64_t block_idx = 0; block_idx < num_blocks; block_idx++) {
const block_q4_0 * block = &src_blocks[block_idx];
const uint8_t * qs = block->qs;
float * block_dst = dst + block_idx * QK4_0;
float scale = fp16_to_fp32(block->d);
float bias = -8.0f * scale;
// Per-block: broadcast scale and bias
__asm__ volatile(
"fbcx.ps f0, %0 \n\t" // f0 = broadcast(scale)
"fbcx.ps f3, %1 \n\t" // f3 = broadcast(-8 * scale)
::"r"(scale),
"r"(bias)
: "f0", "f3");
// 4 iterations x 4 packed bytes = 16 bytes = full block -> 32 floats
for (int i = 0; i < 4; i++) {
__asm__ volatile(
"fgb.ps f2, f1(%0) \n\t" // Gather: [b0,b0,b1,b1,b2,b2,b3,b3]
"mov.m.x m0, x0, 0xAA \n\t" // Odd lanes only (fills gather latency)
"fsrli.pi f2, f2, 4 \n\t" // Odd lanes: byte >> 4 (high nibble)
"mov.m.x m0, x0, 0xFF \n\t" // Restore full mask
"fslli.pi f2, f2, 28 \n\t" // Isolate low 4 bits: shift left 28
"fsrli.pi f2, f2, 28 \n\t" // then right 28 -> nibble in [3:0]
"fcvt.ps.pw f2, f2, rne \n\t" // Int32 -> Float32
"fmul.ps f2, f2, f0 \n\t" // * scale
"fadd.ps f2, f2, f3 \n\t" // + bias -> (nibble - 8) * scale
"fscw.ps f2, f4(%1) \n\t" // Scatter to GGML positions
::"r"(qs),
"r"(block_dst)
: "f2", "memory");
qs += 4; // 4 packed bytes consumed
block_dst += 4; // Advance base by 4 float positions
}
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask)); // Restore mask
}
// Copy a row of Q8_0 data to F32 destination (with dequantization)
static void copy_q8_0_row_cache_aligned(float * dst, const block_q8_0 * src_blocks, int64_t num_elements) {
// Number of Q8_0 blocks needed for this row
const int64_t num_blocks = (num_elements + QK8_0 - 1) / QK8_0; // Round up to handle partial blocks
for (int64_t block_idx = 0; block_idx < num_blocks; block_idx++) {
const int64_t elements_in_block =
(block_idx == num_blocks - 1) ? (num_elements - block_idx * QK8_0) : QK8_0; // Handle last partial block
// Dequantize the block
float temp_buffer[QK8_0];
dequantize_q8_0_block_cache_aligned(&src_blocks[block_idx], temp_buffer);
// Copy dequantized values to destination
for (int64_t i = 0; i < elements_in_block; i++) {
dst[block_idx * QK8_0 + i] = temp_buffer[i];
}
}
}
// Vectorized dequantization of a Q4_K super-block (256 elements) to F32
// Processes 8 groups of 32 elements, using ET SIMD for the inner loops.
// Output is sequential (no scatter needed unlike Q4_0).
static void copy_q4_K_row_cache_aligned(float * dst, const block_q4_K * src_blocks, int64_t num_elements) {
const int64_t num_blocks = (num_elements + QK_K - 1) / QK_K;
// Gather indices for sequential byte access: {0,1,2,3,4,5,6,7}
const int32_t __attribute__((aligned(32))) gather_indices[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
uint64_t temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask)); // Save current mask
__asm__ volatile("mov.m.x m0, x0, 0xFF"); // Enable all 8 elements
// Load gather indices once — shared across all blocks
__asm__ volatile("flq2 f1, 0(%0) \n\t" // f1 = gather indices {0,1,2,3,4,5,6,7}
::"r"(gather_indices)
: "f1");
for (int64_t block_idx = 0; block_idx < num_blocks; block_idx++) {
const block_q4_K * block = &src_blocks[block_idx];
const uint8_t * qs = block->qs;
float * block_dst = dst + block_idx * QK_K;
const float d = fp16_to_fp32(block->d);
const float min = fp16_to_fp32(block->dmin);
int is = 0;
for (int j = 0; j < QK_K; j += 64) {
// Extract per-group scales and mins (scalar — only 8 pairs per super-block)
uint8_t sc, m;
get_scale_min_k4(is + 0, block->scales, &sc, &m);
const float d1 = d * sc;
const float neg_m1 = -(min * m);
get_scale_min_k4(is + 1, block->scales, &sc, &m);
const float d2 = d * sc;
const float neg_m2 = -(min * m);
// Low nibbles: 32 elements using d1, neg_m1
__asm__ volatile(
"fbcx.ps f0, %0 \n\t" // f0 = broadcast(d1)
"fbcx.ps f3, %1 \n\t" // f3 = broadcast(-m1)
::"r"(d1),
"r"(neg_m1)
: "f0", "f3");
const uint8_t * qs_lo = qs;
float * dst_lo = block_dst + j;
for (int k = 0; k < 4; k++) {
__asm__ volatile(
"fgb.ps f2, f1(%0) \n\t" // Gather 8 bytes, sign-extend to int32
"fandi.pi f2, f2, 0xF \n\t" // Mask low nibble (imm10=15)
"fcvt.ps.pw f2, f2, rne \n\t" // Int32 -> Float32
"fmadd.ps f2, f2, f0, f3\n\t" // d1 * nibble + (-m1)
"fsq2 f2, 0(%1) \n\t" // Store 8 floats
::"r"(qs_lo),
"r"(dst_lo)
: "f2", "memory");
qs_lo += 8;
dst_lo += 8;
}
// High nibbles: 32 elements using d2, neg_m2
__asm__ volatile(
"fbcx.ps f0, %0 \n\t" // f0 = broadcast(d2)
"fbcx.ps f3, %1 \n\t" // f3 = broadcast(-m2)
::"r"(d2),
"r"(neg_m2)
: "f0", "f3");
const uint8_t * qs_hi = qs;
float * dst_hi = block_dst + j + 32;
for (int k = 0; k < 4; k++) {
__asm__ volatile(
"fgb.ps f2, f1(%0) \n\t" // Gather 8 bytes, sign-extend to int32
"fsrli.pi f2, f2, 4 \n\t" // Shift right 4: high nibble
"fandi.pi f2, f2, 0xF \n\t" // Mask to 4 bits (clean any sign-ext artifacts)
"fcvt.ps.pw f2, f2, rne \n\t" // Int32 -> Float32
"fmadd.ps f2, f2, f0, f3\n\t" // d2 * nibble + (-m2)
"fsq2 f2, 0(%1) \n\t" // Store 8 floats
::"r"(qs_hi),
"r"(dst_hi)
: "f2", "memory");
qs_hi += 8;
dst_hi += 8;
}
qs += 32; // Advance to next 32 packed bytes
is += 2;
}
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask)); // Restore mask
}
// Determine the number of F32 elements per work unit for a given source type.
// For F32: 1 cacheline (16 elements)
// For quantized types: 1 quant block
static int64_t get_elements_per_work_unit(int type) {
const int64_t elements_per_cacheline = CACHE_LINE_SIZE_BYTES / sizeof(float); // 16
switch (type) {
case GGML_TYPE_Q8_0:
return QK8_0; // 32 elements = 2 cachelines
case GGML_TYPE_Q4_0:
return QK4_0; // 32 elements = 2 cachelines
case GGML_TYPE_Q4_K:
return QK_K; // 256 elements = 16 cachelines
default:
return elements_per_cacheline; // 16 elements = 1 cacheline
}
}
static int get_row_f32_mc_cacheline_aligned(struct ggml_et_get_rows_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
struct ggml_tensor * src0 = &params->src0; // Data tensor
struct ggml_tensor * src1 = &params->src1; // Row indices tensor (I32)
struct ggml_tensor * dst = &params->dst; // Output tensor (F32)
const int64_t ne00 = src0->ne[0]; // Source columns (row width)
const int64_t ne01 = src0->ne[1]; // Source rows (total available rows)
const int64_t ne02 = src0->ne[2]; // Source batch dimension
const int64_t ne03 = src0->ne[3]; // Source outer batch dimension
const int64_t ne10 = src1->ne[0]; // Number of indices in dimension 0
const int64_t ne11 = src1->ne[1]; // Number of indices in dimension 1
const int64_t ne12 = src1->ne[2]; // Batch dimension for indices
const int64_t ne13 = src1->ne[3]; // Outer batch dimension for indices
const int64_t total_rows_to_extract = ne10 * ne11 * ne12 * ne13;
// Determine work unit size based on source type
const int64_t elements_per_wu = get_elements_per_work_unit(src0->type);
const int64_t wus_per_row = ne00 / elements_per_wu;
const int64_t total_wus = total_rows_to_extract * wus_per_row;
// Distribute work units across threads (contiguous ranges)
const int64_t wus_per_thread = (total_wus + num_threads - 1) / num_threads;
const int64_t wu_start = thread_id * wus_per_thread;
int64_t wu_end = wu_start + wus_per_thread;
if (wu_end > total_wus) {
wu_end = total_wus;
}
void * src0_data = src0->data;
int32_t * src1_data = (int32_t *) src1->data;
float * dst_data = (float *) dst->data;
int64_t wu = wu_start;
while (wu < wu_end) {
// Determine which row this work unit belongs to and offset within row
const int64_t row_idx = wu / wus_per_row;
const int64_t wu_in_row = wu % wus_per_row;
// How many work units to process in this row (batch contiguous WUs in same row)
int64_t wus_remaining_in_row = wus_per_row - wu_in_row;
int64_t wus_to_process = wu_end - wu;
if (wus_remaining_in_row < wus_to_process) {
wus_to_process = wus_remaining_in_row;
}
// Calculate multi-dimensional index for this row
const int64_t i = row_idx;
const int64_t i13_idx = i / (ne12 * ne11 * ne10);
const int64_t i12_idx = (i - i13_idx * ne12 * ne11 * ne10) / (ne11 * ne10);
const int64_t i11_idx = (i - i13_idx * ne12 * ne11 * ne10 - i12_idx * ne11 * ne10) / ne10;
const int64_t i10_idx = i - i13_idx * ne12 * ne11 * ne10 - i12_idx * ne11 * ne10 - i11_idx * ne10;
// Get the row index from src1
const int64_t index_offset = i13_idx * ne12 * ne11 * ne10 + i12_idx * ne11 * ne10 + i11_idx * ne10 + i10_idx;
const int32_t row_index = src1_data[index_offset];
if (row_index < 0 || row_index >= ne01) {
return -1; // Index out of bounds
}
const int64_t batch_offset =
i11_idx * ne01 * ne00 + i12_idx * ne02 * ne01 * ne00 + i13_idx * ne03 * ne02 * ne01 * ne00;
const int64_t elem_offset_in_row = wu_in_row * elements_per_wu;
const int64_t num_elements = wus_to_process * elements_per_wu;
float * dst_row = dst_data + row_idx * ne00 + elem_offset_in_row;
if (src0->type == GGML_TYPE_F32) {
// F32 source: direct copy of cacheline-aligned chunk
const float * src_row = (const float *) src0_data + row_index * ne00 + batch_offset + elem_offset_in_row;
copy_row_cache_align(dst_row, src_row, num_elements * sizeof(float));
} else if (src0->type == GGML_TYPE_F16) {
// F16 source: scalar conversion over a destination-aligned write chunk.
const uint16_t * src_row =
(const uint16_t *) src0_data + row_index * ne00 + batch_offset + elem_offset_in_row;
copy_f16_row(dst_row, src_row, num_elements);
} else if (src0->type == GGML_TYPE_Q8_0) {
// Q8_0 source: dequantize work-unit-aligned blocks
const int64_t blocks_per_row = (ne00 + QK8_0 - 1) / QK8_0;
const int64_t src_block_offset = (row_index * blocks_per_row) + (batch_offset / ne00) * blocks_per_row;
const int64_t block_start = elem_offset_in_row / QK8_0;
const block_q8_0 * src_blocks = (const block_q8_0 *) src0_data + src_block_offset + block_start;
copy_q8_0_row_cache_aligned(dst_row, src_blocks, num_elements);
} else if (src0->type == GGML_TYPE_Q4_0) {
// Q4_0 source: dequantize work-unit-aligned blocks
const int64_t blocks_per_row = (ne00 + QK4_0 - 1) / QK4_0;
const int64_t src_block_offset = (row_index * blocks_per_row) + (batch_offset / ne00) * blocks_per_row;
const int64_t block_start = elem_offset_in_row / QK4_0;
const block_q4_0 * src_blocks = (const block_q4_0 *) src0_data + src_block_offset + block_start;
copy_q4_0_row_cache_aligned(dst_row, src_blocks, num_elements);
} else if (src0->type == GGML_TYPE_Q4_K) {
// Q4_K source: dequantize work-unit-aligned blocks
const int64_t blocks_per_row = (ne00 + QK_K - 1) / QK_K;
const int64_t src_block_offset = (row_index * blocks_per_row) + (batch_offset / ne00) * blocks_per_row;
const int64_t block_start = elem_offset_in_row / QK_K;
const block_q4_K * src_blocks = (const block_q4_K *) src0_data + src_block_offset + block_start;
copy_q4_K_row_cache_aligned(dst_row, src_blocks, num_elements);
}
wu += wus_to_process;
}
return 0;
}
static inline size_t tensor_bytes(const struct ggml_tensor * t) {
return (size_t) t->ne[0] * t->ne[1] * t->ne[2] * t->ne[3] * t->nb[0];
}
int entry_point(struct ggml_et_get_rows_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
struct ggml_tensor * src0 = &params->src0; // Data tensor (F32, Q4_0, Q8_0, or Q4_K)
struct ggml_tensor * src1 = &params->src1; // Row indices tensor (I32)
struct ggml_tensor * dst = &params->dst; // Output tensor (F32)
// Fast path - we know how to deal with them multi-core
if ((src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_Q8_0 ||
src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_K) &&
src1->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_F32 && dst->ne[0] % CACHE_ELEMENTS(sizeof(float)) == 0) {
return get_row_f32_mc_cacheline_aligned(params, env);
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (thread_id != 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
if (dst->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_I32) {
return -1; // Invalid output or index type
}
if (src0->type != GGML_TYPE_F32 && src0->type != GGML_TYPE_F16 && src0->type != GGML_TYPE_Q8_0 &&
src0->type != GGML_TYPE_Q4_0 && src0->type != GGML_TYPE_Q4_K) {
return -1; // Unsupported input type
}
void * src0_data = src0->data;
int32_t * src1_data = (int32_t *) src1->data;
float * dst_data = (float *) dst->data;
#ifdef ET_UBERKERNEL
evict_region_past_l2(src0_data, tensor_bytes(src0));
evict_region_past_l2(src1_data, tensor_bytes(src1));
evict_region_past_l2(dst_data, tensor_bytes(dst));
#endif
if (!src0_data || !src1_data || !dst_data) {
return -1; // Null data pointer
}
const int64_t ne00 = src0->ne[0]; // Source columns (row width)
const int64_t ne01 = src0->ne[1]; // Source rows (total available rows)
const int64_t ne02 = src0->ne[2]; // Source batch dimension
const int64_t ne03 = src0->ne[3]; // Source outer batch dimension
const int64_t ne10 = src1->ne[0]; // Number of indices in dimension 0
const int64_t ne11 = src1->ne[1]; // Number of indices in dimension 1
const int64_t ne12 = src1->ne[2]; // Batch dimension for indices
const int64_t ne13 = src1->ne[3]; // Outer batch dimension for indices
const int64_t total_rows_to_extract = ne10 * ne11 * ne12 * ne13;
#ifdef ET_UBERKERNEL
et_barrier(ET_BARRIER_GLOBAL);
#endif
// Naive single-threaded implementation - process all rows sequentially
// XXX: Do we really need a single-threaded implementation?
for (int64_t i = 0; i < total_rows_to_extract; i++) {
// Calculate multi-dimensional index for the current output position
const int64_t i13_idx = i / (ne12 * ne11 * ne10);
const int64_t i12_idx = (i - i13_idx * ne12 * ne11 * ne10) / (ne11 * ne10);
const int64_t i11_idx = (i - i13_idx * ne12 * ne11 * ne10 - i12_idx * ne11 * ne10) / ne10;
const int64_t i10_idx = i - i13_idx * ne12 * ne11 * ne10 - i12_idx * ne11 * ne10 - i11_idx * ne10;
// Get the row index from src1
const int64_t index_offset = i13_idx * ne12 * ne11 * ne10 + i12_idx * ne11 * ne10 + i11_idx * ne10 + i10_idx;
const int32_t row_index = src1_data[index_offset];
if (row_index < 0 || row_index >= ne01) {
return -1; // Index out of bounds
}
const int64_t batch_offset =
i11_idx * ne01 * ne00 + i12_idx * ne02 * ne01 * ne00 + i13_idx * ne03 * ne02 * ne01 * ne00;
const int64_t dst_offset = i;
if (src0->type == GGML_TYPE_F32) {
// F32 source: direct copy
const float * src_row = (const float *) src0_data + row_index * ne00 + batch_offset;
float * dst_row = dst_data + dst_offset * ne00;
copy_f32_row(dst_row, src_row, ne00);
} else if (src0->type == GGML_TYPE_F16) {
// F16 source: scalar conversion
const uint16_t * src_row = (const uint16_t *) src0_data + row_index * ne00 + batch_offset;
float * dst_row = dst_data + dst_offset * ne00;
copy_f16_row(dst_row, src_row, ne00);
} else if (src0->type == GGML_TYPE_Q8_0) {
// Q8_0 source: dequantize while copying
const int64_t blocks_per_row = (ne00 + QK8_0 - 1) / QK8_0;
const int64_t src_block_offset = (row_index * blocks_per_row) + (batch_offset / ne00) * blocks_per_row;
const block_q8_0 * src_blocks = (const block_q8_0 *) src0_data + src_block_offset;
float * dst_row = dst_data + dst_offset * ne00;
copy_q8_0_row(dst_row, src_blocks, ne00);
} else if (src0->type == GGML_TYPE_Q4_0) {
// Q4_0 source: dequantize while copying
const int64_t blocks_per_row = (ne00 + QK4_0 - 1) / QK4_0;
const int64_t src_block_offset = (row_index * blocks_per_row) + (batch_offset / ne00) * blocks_per_row;
const block_q4_0 * src_blocks = (const block_q4_0 *) src0_data + src_block_offset;
float * dst_row = dst_data + dst_offset * ne00;
copy_q4_0_row(dst_row, src_blocks, ne00);
} else if (src0->type == GGML_TYPE_Q4_K) {
// Q4_K source: dequantize while copying
const int64_t blocks_per_row = (ne00 + QK_K - 1) / QK_K;
const int64_t src_block_offset = (row_index * blocks_per_row) + (batch_offset / ne00) * blocks_per_row;
const block_q4_K * src_blocks = (const block_q4_K *) src0_data + src_block_offset;
float * dst_row = dst_data + dst_offset * ne00;
copy_q4_K_row(dst_row, src_blocks, ne00);
}
}
return 0;
}
@@ -0,0 +1,44 @@
// ET kernel entry-point parameter structs and tensor helpers.
#ifndef GGML_TENSOR_H
#define GGML_TENSOR_H
#include <stddef.h>
#include <stdint.h>
#include "ggml.h"
struct ggml_et_binary_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor dst;
};
// bias.data == NULL -> unfused MUL_MAT; otherwise dst = mat_mul(...) + bias.
struct ggml_et_mm_q8_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor dst;
struct ggml_tensor bias;
};
struct ggml_et_mul_mat_id_params {
struct ggml_tensor src0; // [K, M, n_expert]
struct ggml_tensor src1; // [K, n_expert_used, batch]
struct ggml_tensor src2; // [n_expert_used, batch] (I32 expert indices)
struct ggml_tensor dst; // [M, n_expert_used, batch, 1]
};
// ne[i] == 1 axes are skipped: their stride is unobservable.
static inline int ggml_tensor_is_contiguous(const struct ggml_tensor * t, int type_size) {
int64_t expected = type_size;
for (int i = 0; i < GGML_MAX_DIMS; i++) {
if (t->ne[i] > 1 && (int64_t) t->nb[i] != expected) {
return 0;
}
expected *= t->ne[i];
}
return 1;
}
#endif // GGML_TENSOR_H
+551
View File
@@ -0,0 +1,551 @@
//******************************************************************************
// GLU F32 Kernel (SwiGLU specifically)
// Gated Linear Unit: y[i] = silu(x[i]) * g[i] where silu(x) = x * sigmoid(x)
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdint.h>
// GLU kernel parameters structure (from ET backend ops)
struct ggml_et_glu_params {
struct ggml_tensor src0; // F32 input tensor A (or combined tensor if src1 is null)
struct ggml_tensor src1; // F32 input tensor B (null for single tensor mode)
struct ggml_tensor dst; // F32 output tensor (n/2 columns)
int32_t glu_op_type; // GLU operation type (REGLU=0, GEGLU=1, SWIGLU=2, etc.)
int32_t swapped; // Whether gate and value are swapped
float alpha; // SWIGLU_OAI: sigmoid scaling factor
float limit; // SWIGLU_OAI: clamp limit
};
// SiLU activation function: silu(x) = x * sigmoid(x) = x / (1 + exp(-x))
static inline float silu_f32(float x) {
// For numerical stability, use the mathematically equivalent form:
// silu(x) = x / (1 + exp(-x)) = x * sigmoid(x)
// For large negative x, exp(-x) -> inf, so silu(x) -> 0
// For large positive x, exp(-x) -> 0, so silu(x) -> x
if (x > 20.0f) {
// For x > 20, exp(-x) is negligible, silu(x) ~ x
return x;
} else if (x < -20.0f) {
// For x < -20, silu(x) ~ 0
return 0.0f;
} else {
// Use standard formula: silu(x) = x / (1 + exp(-x))
// Optimized using ET hardware division
float exp_neg_x = et_expf(-x);
float denominator = 1.0f + exp_neg_x;
return et_fdiv(x, denominator);
}
}
// Vectorized GeGLU block processing (8 elements = 1 cache line, 64B aligned)
// gelu(x) = 0.5*x*(1 + tanh(z)) = x * (1 - 1/(exp(2z)+1))
// where z = sqrt(2/pi) * x * (1 + 0.044715*x^2)
// Reformulated to avoid inf*0 NaN: uses x * sigmoid(2z) identity
static inline void block_geglu(float * dst_block, const float * x_block, const float * g_block, int elements) {
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
float one_const = 1.0f;
float coef_a_const = 0.044715f;
float sqrt2pi_const = 0.79788456080286535587989211986876f; // sqrt(2/pi)
float two_log2e_const = 2.8853900817779268f; // 2 * log2(e)
for (int32_t i = 0; i < elements; i += 8) {
__asm__ volatile(
// Load inputs
"flw.ps f10, %[x_vec]\n" // f10 = x
"flw.ps f11, %[g_vec]\n" // f11 = g
// Broadcast constants
"fbc.ps f20, %[one_ptr]\n" // f20 = 1.0
"fbc.ps f22, %[coef_ptr]\n" // f22 = 0.044715
"fbc.ps f23, %[sqrt2pi_ptr]\n" // f23 = sqrt(2/pi)
"fbc.ps f24, %[two_log2e_ptr]\n" // f24 = 2*log2(e)
// inner = 1 + 0.044715 * x^2
"fmul.ps f12, f10, f10\n" // f12 = x^2
"fmadd.ps f13, f22, f12, f20\n" // f13 = 1 + 0.044715*x^2
// z = sqrt(2/pi) * x * inner
"fmul.ps f14, f23, f10\n" // f14 = sqrt(2/pi) * x
"fmul.ps f14, f14, f13\n" // f14 = z
// exp(2z) via fexp.ps: feed z * 2*log2(e) since fexp computes 2^input
"fmul.ps f15, f14, f24\n" // f15 = 2z * log2(e)
"fexp.ps f15, f15\n" // f15 = exp(2z)
// gelu(x) = x * (1 - 1/(exp(2z)+1)) [NaN-safe: no inf*0]
// exp(2z)->inf: rcp(inf)=0, 1-0=1, gelu=x
// exp(2z)->0: rcp(1)=1, 1-1=0, gelu=0
"fadd.ps f16, f15, f20\n" // f16 = exp(2z) + 1
"frcp.ps f16, f16\n" // f16 = 1/(exp(2z) + 1)
"fsub.ps f16, f20, f16\n" // f16 = 1 - 1/(exp(2z)+1)
"fmul.ps f16, f10, f16\n" // f16 = gelu(x)
// Final result
"fmul.ps f18, f16, f11\n" // f18 = gelu(x) * g
"fsw.ps f18, %[dst_out]\n"
: [dst_out] "=m"(*(float (*)[8]) & dst_block[i])
: [x_vec] "m"(*(const float (*)[8]) & x_block[i]), [g_vec] "m"(*(const float (*)[8]) & g_block[i]),
[one_ptr] "m"(one_const), [coef_ptr] "m"(coef_a_const), [sqrt2pi_ptr] "m"(sqrt2pi_const),
[two_log2e_ptr] "m"(two_log2e_const)
: "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f18", "f20", "f22", "f23", "f24");
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
}
// Vectorized SwiGLU block processing (16 elements = 1 cache line)
static inline void block_swiglu(float * dst_block, const float * x_block, const float * g_block, int elements) {
// Process 8 elements at a time using vector instructions
int32_t vec_end = (elements / 8) * 8;
// Set mask register to enable all 8 vector elements
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask)); // Save current mask
__asm__ volatile("mov.m.x m0, x0, 0xFF"); // Enable all 8 elements
// Constants for broadcasting
float zero_const = 0.0f;
float one_const = 1.0f;
float log2e_const = 1.4426950408889634f; // log2(e)
for (int32_t i = 0; i < vec_end; i += 8) {
// Vectorized SwiGLU: dst = silu(x) * g = (x / (1 + exp(-x))) * g
// Using ET hardware: exp, reciprocal, multiply operations
__asm__ volatile(
// Load input vectors
"flw.ps f10, %[x_vec]\n" // f10 = x[0..7]
"flw.ps f11, %[g_vec]\n" // f11 = g[0..7]
// Broadcast constants to vector registers
"fbc.ps f20, %[zero_ptr]\n" // f20 = broadcast(0.0f) to all 8 elements
"fbc.ps f21, %[one_ptr]\n" // f21 = broadcast(1.0f) to all 8 elements
// Compute -x (negate x by subtracting from zero)
"fsub.ps f12, f20, f10\n" // f12 = 0 - x = -x
// Convert to base-2 exponent: -x * log2(e) = -x * 1.44269504
// Load log2(e) constant
"fbc.ps f22, %[log2e_ptr]\n" // f22 = broadcast(1.44269504f)
"fmul.ps f13, f12, f22\n" // f13 = -x * log2(e)
// Compute 2^(-x * log2(e)) = exp(-x)
"fexp.ps f14, f13\n" // f14 = 2^(-x * log2(e)) = exp(-x)
// Compute 1 + exp(-x)
"fadd.ps f15, f14, f21\n" // f15 = exp(-x) + 1
// Compute 1 / (1 + exp(-x)) using reciprocal
"frcp.ps f16, f15\n" // f16 = 1 / (1 + exp(-x))
// Compute silu(x) = x * (1 / (1 + exp(-x)))
"fmul.ps f17, f10, f16\n" // f17 = x * (1 / (1 + exp(-x))) = silu(x)
// Compute final result: silu(x) * g
"fmul.ps f18, f17, f11\n" // f18 = silu(x) * g
// Store result
"fsw.ps f18, %[dst_out]\n" // Store 8 results to destination
: [dst_out] "=m"(*(float (*)[8]) & dst_block[i])
: [x_vec] "m"(*(const float (*)[8]) & x_block[i]), [g_vec] "m"(*(const float (*)[8]) & g_block[i]),
[zero_ptr] "m"(zero_const), // Memory reference to 0.0f for broadcasting
[one_ptr] "m"(one_const), // Memory reference to 1.0f for broadcasting
[log2e_ptr] "m"(log2e_const) // Memory reference to log2(e) for broadcasting
: "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", "f20", "f21", "f22");
}
// Restore original mask
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
// Handle remaining elements (< 8) with scalar operations
for (int32_t i = vec_end; i < elements; i++) {
dst_block[i] = silu_f32(x_block[i]) * g_block[i];
}
}
// Vectorized ReGLU block: dst = max(0, x) * g
static inline void block_reglu(float * dst_block, const float * x_block, const float * g_block, int elements) {
int32_t vec_end = (elements / 8) * 8;
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
float zero_const = 0.0f;
for (int32_t i = 0; i < vec_end; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x_vec]\n" // f10 = x
"flw.ps f11, %[g_vec]\n" // f11 = g
"fbc.ps f20, %[zero_ptr]\n" // f20 = 0.0
"fmax.ps f12, f10, f20\n" // f12 = max(x, 0)
"fmul.ps f13, f12, f11\n" // f13 = relu(x) * g
"fsw.ps f13, %[dst_out]\n"
: [dst_out] "=m"(*(float (*)[8]) & dst_block[i])
: [x_vec] "m"(*(const float (*)[8]) & x_block[i]), [g_vec] "m"(*(const float (*)[8]) & g_block[i]),
[zero_ptr] "m"(zero_const)
: "f10", "f11", "f12", "f13", "f20");
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
for (int32_t i = vec_end; i < elements; i++) {
float xv = x_block[i];
dst_block[i] = (xv > 0.0f) ? xv * g_block[i] : 0.0f;
}
}
// Vectorized GeGLU-Quick block: dst = x * sigmoid(1.702 * x) * g
// Using gelu_quick(x) = x / (1 + exp(-1.702*x))
static inline void block_geglu_quick(float * dst_block, const float * x_block, const float * g_block, int elements) {
int32_t vec_end = (elements / 8) * 8;
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
float zero_const = 0.0f;
float one_const = 1.0f;
// -1.702 * log2(e), so that fexp.ps(x * neg_k_log2e) = exp(-1.702*x)
float neg_k_log2e_const = -1.702f * 1.4426950408889634f;
for (int32_t i = 0; i < vec_end; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x_vec]\n" // f10 = x
"flw.ps f11, %[g_vec]\n" // f11 = g
"fbc.ps f20, %[zero_ptr]\n" // f20 = 0
"fbc.ps f21, %[one_ptr]\n" // f21 = 1
"fbc.ps f22, %[k_ptr]\n" // f22 = -1.702*log2(e)
"fmul.ps f13, f10, f22\n" // f13 = -1.702*x*log2(e)
"fexp.ps f14, f13\n" // f14 = exp(-1.702*x)
"fadd.ps f15, f14, f21\n" // f15 = 1 + exp(-1.702*x)
"frcp.ps f16, f15\n" // f16 = sigmoid(1.702*x)
"fmul.ps f17, f10, f16\n" // f17 = gelu_quick(x)
"fmul.ps f18, f17, f11\n" // f18 = gelu_quick(x) * g
"fsw.ps f18, %[dst_out]\n"
: [dst_out] "=m"(*(float (*)[8]) & dst_block[i])
: [x_vec] "m"(*(const float (*)[8]) & x_block[i]), [g_vec] "m"(*(const float (*)[8]) & g_block[i]),
[zero_ptr] "m"(zero_const), [one_ptr] "m"(one_const), [k_ptr] "m"(neg_k_log2e_const)
: "f10", "f11", "f13", "f14", "f15", "f16", "f17", "f18", "f20", "f21", "f22");
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
for (int32_t i = vec_end; i < elements; i++) {
float xv = x_block[i];
// Reuse silu reciprocal path: sigmoid(1.702*x) = 1/(1+exp(-1.702*x))
float e = et_expf(-1.702f * xv);
dst_block[i] = et_fdiv(xv, 1.0f + e) * g_block[i];
}
}
// Vectorized SwiGLU-OAI block (OpenAI gpt-oss variant):
// x_c = min(x, limit)
// y_c = clamp(g, -limit, limit)
// out = (x_c / (1 + exp(-alpha * x_c))) * (y_c + 1)
static inline void block_swiglu_oai(float * dst_block,
const float * x_block,
const float * g_block,
int elements,
float alpha,
float limit) {
int32_t vec_end = (elements / 8) * 8;
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
float zero_const = 0.0f;
float one_const = 1.0f;
float limit_pos = limit;
float limit_neg = -limit;
// -alpha * log2(e): feed (x * neg_alpha_log2e) into fexp.ps to get exp(-alpha*x)
float neg_alpha_l2e = -alpha * 1.4426950408889634f;
for (int32_t i = 0; i < vec_end; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x_vec]\n" // f10 = x raw
"flw.ps f11, %[g_vec]\n" // f11 = g raw
"fbc.ps f20, %[zero_ptr]\n" // f20 = 0
"fbc.ps f21, %[one_ptr]\n" // f21 = 1
"fbc.ps f23, %[lim_pos]\n" // f23 = +limit
"fbc.ps f24, %[lim_neg]\n" // f24 = -limit
"fbc.ps f25, %[k_ptr]\n" // f25 = -alpha*log2(e)
// x_c = min(x, +limit) (no lower bound on x per OAI spec)
"fmin.ps f12, f10, f23\n" // f12 = x_c
// y_c = clamp(g, -limit, +limit) = min(max(g, -limit), +limit)
"fmax.ps f13, f11, f24\n" // f13 = max(g, -limit)
"fmin.ps f13, f13, f23\n" // f13 = y_c
// sigmoid(alpha * x_c) = 1 / (1 + exp(-alpha * x_c))
"fmul.ps f14, f12, f25\n" // f14 = -alpha*x_c*log2(e)
"fexp.ps f15, f14\n" // f15 = exp(-alpha*x_c)
"fadd.ps f15, f15, f21\n" // f15 = 1 + exp(-alpha*x_c)
"frcp.ps f16, f15\n" // f16 = sigmoid(alpha*x_c)
// out_glu = x_c * sigmoid(alpha*x_c)
"fmul.ps f17, f12, f16\n" // f17 = swiglu_oai gate output
// dst = out_glu * (y_c + 1)
"fadd.ps f18, f13, f21\n" // f18 = y_c + 1
"fmul.ps f19, f17, f18\n" // f19 = final
"fsw.ps f19, %[dst_out]\n"
: [dst_out] "=m"(*(float (*)[8]) & dst_block[i])
: [x_vec] "m"(*(const float (*)[8]) & x_block[i]), [g_vec] "m"(*(const float (*)[8]) & g_block[i]),
[zero_ptr] "m"(zero_const), [one_ptr] "m"(one_const), [lim_pos] "m"(limit_pos), [lim_neg] "m"(limit_neg),
[k_ptr] "m"(neg_alpha_l2e)
: "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", "f19", "f20", "f21", "f23", "f24", "f25");
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
// Scalar tail (mirrors CPU reference exactly)
for (int32_t i = vec_end; i < elements; i++) {
float xv = x_block[i];
float yv = g_block[i];
if (xv > limit) {
xv = limit;
}
if (yv > limit) {
yv = limit;
}
if (yv < -limit) {
yv = -limit;
}
float e = et_expf(-alpha * xv);
float out_glu = et_fdiv(xv, 1.0f + e);
dst_block[i] = out_glu * (yv + 1.0f);
}
}
// Scalar erf approximation (Abramowitz & Stegun 7.1.26, max error ~1.5e-7)
static inline float erf_approx(float x) {
const float a1 = 0.254829592f;
const float a2 = -0.284496736f;
const float a3 = 1.421413741f;
const float a4 = -1.453152027f;
const float a5 = 1.061405429f;
const float p = 0.3275911f;
float sign = (x < 0.0f) ? -1.0f : 1.0f;
float ax = (x < 0.0f) ? -x : x;
float t = et_fdiv(1.0f, 1.0f + p * ax);
float t2 = t * t;
float t3 = t2 * t;
float t4 = t3 * t;
float t5 = t4 * t;
float poly = a1 * t + a2 * t2 + a3 * t3 + a4 * t4 + a5 * t5;
float y = 1.0f - poly * et_expf(-ax * ax);
return sign * y;
}
// GeGLU-Erf block: dst = 0.5 * x * (1 + erf(x / sqrt(2))) * g
// Scalar implementation — variant is rarely used so we keep complexity low.
static inline void block_geglu_erf(float * dst_block, const float * x_block, const float * g_block, int elements) {
const float sqrt_2_inv = 0.70710678118654752440f;
for (int32_t i = 0; i < elements; i++) {
float xv = x_block[i];
dst_block[i] = 0.5f * xv * (1.0f + erf_approx(xv * sqrt_2_inv)) * g_block[i];
}
}
// Main entry point for GLU kernel
int entry_point(struct ggml_et_glu_params * params, void * env) {
// Cast env to proper type
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
// Validate environment pointer
if (!kernel_env) {
return -1;
}
// Get thread info using shire mask from environment
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
// Basic safety check on params
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
// Supported variants: SwiGLU, SwiGLU-OAI, GeGLU, GeGLU-Erf, GeGLU-Quick, ReGLU
switch (params->glu_op_type) {
case GGML_GLU_OP_SWIGLU:
case GGML_GLU_OP_SWIGLU_OAI:
case GGML_GLU_OP_GEGLU:
case GGML_GLU_OP_GEGLU_ERF:
case GGML_GLU_OP_GEGLU_QUICK:
case GGML_GLU_OP_REGLU:
break;
default:
return -1; // Unsupported GLU operation
}
// Extract tensor references
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * src1 = params->src1.data ? &params->src1 : 0;
struct ggml_tensor * dst = &params->dst;
int32_t swapped = params->swapped;
// Validate tensor types (F32 only)
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1; // Unsupported type combination
}
if (src1 && src1->type != GGML_TYPE_F32) {
return -1; // Unsupported src1 type
}
// Get data pointers
float * src0_data = (float *) src0->data;
float * src1_data = src1 ? (float *) src1->data : src0_data;
float * dst_data = (float *) dst->data;
// Validate data pointers
if (!src0_data || !dst_data) {
return -1; // Null data pointer
}
// Get tensor dimensions
const int64_t nc = dst->ne[0]; // Output columns (input columns / 2)
const int64_t nr = dst->ne[1] * dst->ne[2] * dst->ne[3]; // Total rows
// Get strides
const size_t src0_stride = src0->nb[1]; // Stride between rows in src0
const size_t src1_stride = src1 ? src1->nb[1] : src0->nb[1]; // Stride between rows in src1
const size_t dst_stride = dst->nb[1]; // Stride between rows in dst
// Validate dimensions for split SwiGLU
if (src1) {
// Split tensor mode: src0 and src1 should have same shape as dst
if (src0->ne[0] != nc || src1->ne[0] != nc) {
return -1; // Dimension mismatch in split mode
}
} else {
// Single tensor mode: src0 should have 2*nc columns
if (src0->ne[0] != 2 * nc) {
return -1; // Dimension mismatch in single tensor mode
}
}
// Calculate total elements for cache line distribution
const int64_t elements_per_cacheline = 16; // 64 bytes / 4 bytes per float
const int64_t total_elements = nr * nc;
const int64_t total_cachelines = (total_elements + elements_per_cacheline - 1) / elements_per_cacheline;
// Distribute cache lines across threads
int64_t cachelines_per_thread = (total_cachelines + num_threads - 1) / num_threads;
int64_t start_cacheline = thread_id * cachelines_per_thread;
int64_t end_cacheline = start_cacheline + cachelines_per_thread;
// Clamp end_cacheline to actual number of cache lines
if (end_cacheline > total_cachelines) {
end_cacheline = total_cachelines;
}
// Thread should return if no work to do
if (start_cacheline >= total_cachelines) {
return 0;
}
// Process cache lines assigned to this thread
for (int64_t cl = start_cacheline; cl < end_cacheline; cl++) {
// Map cache line back to element coordinates
int64_t global_element_start = cl * elements_per_cacheline;
int64_t row = global_element_start / nc;
int64_t col = global_element_start % nc;
// Skip if we're past the end of data
if (global_element_start >= total_elements) {
break;
}
// Calculate how many elements to process in this cache line
int64_t elements_remaining = total_elements - global_element_start;
int elements_this_block =
(int) ((elements_remaining < elements_per_cacheline) ? elements_remaining : elements_per_cacheline);
// Process elements that span across rows
int64_t elements_processed = 0;
while (elements_processed < elements_this_block && row < nr) {
// Calculate elements to process in current row
int64_t elements_in_row = nc - col;
int64_t elements_to_process = elements_this_block - elements_processed;
if (elements_to_process > elements_in_row) {
elements_to_process = elements_in_row;
}
// Get pointers for current row and column range
float * dst_ptr = (float *) ((char *) dst_data + row * dst_stride) + col;
float * x_ptr;
float * g_ptr;
if (src1) {
// Split tensor mode
x_ptr = (float *) ((char *) src0_data + row * src0_stride) + col;
g_ptr = (float *) ((char *) src1_data + row * src1_stride) + col;
} else {
// Single tensor mode - src0 contains both x and g
float * src0_row = (float *) ((char *) src0_data + row * src0_stride);
if (swapped) {
g_ptr = src0_row + col; // First half is gate
x_ptr = src0_row + nc + col; // Second half is value
} else {
x_ptr = src0_row + col; // First half is value
g_ptr = src0_row + nc + col; // Second half is gate
}
}
// Process this segment
switch (params->glu_op_type) {
case GGML_GLU_OP_GEGLU:
block_geglu(dst_ptr, x_ptr, g_ptr, (int) elements_to_process);
break;
case GGML_GLU_OP_SWIGLU:
block_swiglu(dst_ptr, x_ptr, g_ptr, (int) elements_to_process);
break;
case GGML_GLU_OP_REGLU:
block_reglu(dst_ptr, x_ptr, g_ptr, (int) elements_to_process);
break;
case GGML_GLU_OP_GEGLU_QUICK:
block_geglu_quick(dst_ptr, x_ptr, g_ptr, (int) elements_to_process);
break;
case GGML_GLU_OP_GEGLU_ERF:
block_geglu_erf(dst_ptr, x_ptr, g_ptr, (int) elements_to_process);
break;
case GGML_GLU_OP_SWIGLU_OAI:
block_swiglu_oai(dst_ptr, x_ptr, g_ptr, (int) elements_to_process, params->alpha, params->limit);
break;
default:
return -1;
}
// Update counters
elements_processed += elements_to_process;
col += elements_to_process;
// Move to next row if current row is complete
if (col >= nc) {
row++;
col = 0;
}
}
}
return 0;
}
@@ -0,0 +1,171 @@
//******************************************************************************
// GROUP_NORM F32 Kernel
// Baseline scalar implementation:
// normalize over (ne0 * ne1 * channels_in_group) for each (group, batch).
//
// Parallelization:
// - Work is partitioned across (group, batch) pairs.
// - For non-cache-aligned ne0, writes are emitted in row-groups so each thread's
// destination write footprint still spans an integer number of cache lines.
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_group_norm_params {
struct ggml_tensor src0;
struct ggml_tensor dst;
int32_t n_groups;
float eps;
};
int entry_point(struct ggml_et_group_norm_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
const float * src0_data = (const float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
const int32_t n_groups = params->n_groups;
const float eps = params->eps;
if (n_groups <= 0 || eps < 0.0f) {
return -1;
}
const int64_t ne0 = dst->ne[0];
const int64_t ne1 = dst->ne[1];
const int64_t ne2 = dst->ne[2];
const int64_t ne3 = dst->ne[3];
if (src0->ne[0] != ne0 || src0->ne[1] != ne1 || src0->ne[2] != ne2 || src0->ne[3] != ne3) {
return -1;
}
const int64_t nb1 = dst->nb[1];
const int64_t nb2 = dst->nb[2];
const int64_t nb3 = dst->nb[3];
const int64_t nb01 = src0->nb[1];
const int64_t nb02 = src0->nb[2];
const int64_t nb03 = src0->nb[3];
const int64_t channels_per_group = (ne2 + n_groups - 1) / n_groups;
if (channels_per_group <= 0) {
return -1;
}
const int64_t active_groups = (ne2 + channels_per_group - 1) / channels_per_group;
const int64_t total_work = active_groups * ne3;
const int64_t rows_per_write_group = et_rows_per_cacheline_group(ne0, sizeof(float));
for (int64_t work = thread_id; work < total_work; work += num_threads) {
const int64_t i3 = work / active_groups;
const int64_t group_idx = work % active_groups;
const int64_t channel_start = group_idx * channels_per_group;
int64_t channel_end = channel_start + channels_per_group;
if (channel_end > ne2) {
channel_end = ne2;
}
const int64_t channel_count = channel_end - channel_start;
if (channel_count <= 0) {
continue;
}
float sum = 0.0f;
float denom = 0.0f;
for (int64_t i2 = channel_start; i2 < channel_end; ++i2) {
for (int64_t i1 = 0; i1 < ne1; ++i1) {
const float * src_row = (const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
for (int64_t i0 = 0; i0 < ne0; ++i0) {
sum += src_row[i0];
denom += 1.0f;
}
}
}
const float mean = et_fdiv(sum, denom);
float var_sum = 0.0f;
for (int64_t i2 = channel_start; i2 < channel_end; ++i2) {
for (int64_t i1 = 0; i1 < ne1; ++i1) {
const float * src_row = (const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
for (int64_t i0 = 0; i0 < ne0; ++i0) {
const float centered = src_row[i0] - mean;
var_sum += centered * centered;
}
}
}
const float variance = et_fdiv(var_sum, denom);
const float scale = et_fdiv(1.0f, et_sqrtf(variance + eps));
if (ne0 % 16 == 0) {
for (int64_t i2 = channel_start; i2 < channel_end; ++i2) {
for (int64_t i1 = 0; i1 < ne1; ++i1) {
const float * src_row =
(const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_row = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
for (int64_t i0 = 0; i0 < ne0; ++i0) {
dst_row[i0] = (src_row[i0] - mean) * scale;
}
}
}
} else {
const int64_t total_rows_in_group = channel_count * ne1;
const int64_t total_write_groups = (total_rows_in_group + rows_per_write_group - 1) / rows_per_write_group;
for (int64_t write_group = 0; write_group < total_write_groups; ++write_group) {
const int64_t row_start = write_group * rows_per_write_group;
int64_t row_end = row_start + rows_per_write_group;
if (row_end > total_rows_in_group) {
row_end = total_rows_in_group;
}
for (int64_t row = row_start; row < row_end; ++row) {
const int64_t local_i2 = row / ne1;
const int64_t i1 = row % ne1;
const int64_t i2 = channel_start + local_i2;
const float * src_row =
(const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_row = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
for (int64_t i0 = 0; i0 < ne0; ++i0) {
dst_row[i0] = (src_row[i0] - mean) * scale;
}
}
}
}
}
return 0;
}
+130
View File
@@ -0,0 +1,130 @@
//******************************************************************************
// IM2COL Kernel
// Baseline scalar implementation for:
// src1: [N, IC, IH, IW] -> dst: [N, OH, OW, IC*KH*KW] (2D)
// src1: [N, IC, IW] -> dst: [N, 1, OW, IC* KW] (1D)
//
// Work is distributed by row-groups so threads own cache-line-aligned chunks of
// destination rows even when ne0 is not cache aligned.
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdint.h>
static inline void im2col_store_elem(void * dst_base, enum ggml_type dst_type, int64_t idx, float value) {
if (dst_type == GGML_TYPE_F32) {
((float *) dst_base)[idx] = value;
} else {
((uint16_t *) dst_base)[idx] = fp32_to_fp16(value);
}
}
static inline float im2col_load_src_elem(const void * src_base, enum ggml_type src_type, int64_t idx) {
if (src_type == GGML_TYPE_F32) {
return ((const float *) src_base)[idx];
}
return fp16_to_fp32(((const uint16_t *) src_base)[idx]);
}
int entry_point(struct ggml_et_binary_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env || params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * src1 = &params->src1;
struct ggml_tensor * dst = &params->dst;
if (!src1->data || !dst->data) {
return -1;
}
if (!((dst->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) ||
(dst->type == GGML_TYPE_F16 && (src1->type == GGML_TYPE_F16 || src1->type == GGML_TYPE_F32)))) {
return -1;
}
const int32_t s0 = ((const int32_t *) dst->op_params)[0];
const int32_t s1 = ((const int32_t *) dst->op_params)[1];
const int32_t p0 = ((const int32_t *) dst->op_params)[2];
const int32_t p1 = ((const int32_t *) dst->op_params)[3];
const int32_t d0 = ((const int32_t *) dst->op_params)[4];
const int32_t d1 = ((const int32_t *) dst->op_params)[5];
const int32_t is_2d = ((const int32_t *) dst->op_params)[6];
const int64_t N = is_2d ? src1->ne[3] : src1->ne[2];
const int64_t IC = is_2d ? src1->ne[2] : src1->ne[1];
const int64_t IH = is_2d ? src1->ne[1] : 1;
const int64_t IW = src1->ne[0];
const int64_t KH = is_2d ? src0->ne[1] : 1;
const int64_t KW = src0->ne[0];
const int64_t OH = is_2d ? dst->ne[2] : 1;
const int64_t OW = dst->ne[1];
const int64_t row_elems = dst->ne[0];
const int64_t total_rows = OW * OH * N;
const size_t src_batch_stride = is_2d ? src1->nb[3] : src1->nb[2];
const size_t src_channel_stride = is_2d ? src1->nb[2] : src1->nb[1];
const size_t dst_row_stride = dst->nb[1];
const size_t dst_plane_stride = is_2d ? dst->nb[2] : 0;
const size_t dst_batch_stride = is_2d ? dst->nb[3] : dst->nb[2];
const int64_t dst_elem_size = (dst->type == GGML_TYPE_F32) ? (int64_t) sizeof(float) : (int64_t) sizeof(uint16_t);
const int64_t rows_per_group = et_rows_per_cacheline_group(row_elems, dst_elem_size);
const int64_t total_groups = (total_rows + rows_per_group - 1) / rows_per_group;
for (int64_t grp = thread_id; grp < total_groups; grp += num_threads) {
const int64_t row_start = grp * rows_per_group;
int64_t row_end = row_start + rows_per_group;
if (row_end > total_rows) {
row_end = total_rows;
}
for (int64_t row = row_start; row < row_end; ++row) {
const int64_t in = row / (OH * OW);
const int64_t rem = row % (OH * OW);
const int64_t ioh = rem / OW;
const int64_t iow = rem % OW;
void * dst_row = (char *) dst->data + in * dst_batch_stride + ioh * dst_plane_stride + iow * dst_row_stride;
for (int64_t iic = 0; iic < IC; ++iic) {
const void * src_channel = (const char *) src1->data + in * src_batch_stride + iic * src_channel_stride;
for (int64_t ikh = 0; ikh < KH; ++ikh) {
for (int64_t ikw = 0; ikw < KW; ++ikw) {
const int64_t iiw = iow * s0 + ikw * d0 - p0;
const int64_t iih = ioh * s1 + ikh * d1 - p1;
const int64_t dst_idx = iic * (KH * KW) + ikh * KW + ikw;
if (iiw < 0 || iiw >= IW || iih < 0 || iih >= IH) {
im2col_store_elem(dst_row, dst->type, dst_idx, 0.0f);
} else {
const int64_t src_idx = iih * IW + iiw;
const float value = im2col_load_src_elem(src_channel, src1->type, src_idx);
im2col_store_elem(dst_row, dst->type, dst_idx, value);
}
}
}
}
}
}
return 0;
}
@@ -0,0 +1,237 @@
//******************************************************************************
// L2 Norm F32 Kernel (L2 Normalization)
// y[i] = x[i] / max(sqrt(sum(x^2)), eps)
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>
// L2 Norm kernel parameters structure
struct ggml_et_l2_norm_params {
struct ggml_tensor src0; // F32 input tensor
struct ggml_tensor dst; // F32 output tensor
float eps; // Epsilon parameter for numerical stability
};
int entry_point(struct ggml_et_l2_norm_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
float eps = params->eps;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1; // Unsupported type combination
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1; // Null data pointer
}
if (eps < 0.0f) {
return -1; // Invalid epsilon
}
const int64_t ne0 = dst->ne[0];
const int64_t ne1 = dst->ne[1];
const int64_t ne2 = dst->ne[2];
const int64_t ne3 = dst->ne[3];
const size_t nb0 = dst->nb[0], nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
const size_t nb00 = src0->nb[0], nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
(void) nb0;
(void) nb00;
if (src0->ne[0] != ne0 || src0->ne[1] != ne1 || src0->ne[2] != ne2 || src0->ne[3] != ne3) {
return -1; // Shape mismatch
}
const int32_t total_rows = (int32_t) (ne1 * ne2 * ne3);
const int shire_threads = SOC_MINIONS_PER_SHIRE * NUM_HARTS_PER_MINION;
if (total_rows >= shire_threads) {
// Row-parallel: each thread processes whole rows
for (int64_t i3 = 0; i3 < ne3; i3++) {
for (int64_t i2 = 0; i2 < ne2; i2++) {
for (int64_t i1 = thread_id; i1 < ne1; i1 += num_threads) {
const float * src_ptr =
(const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_ptr = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
float zero = 0.0f;
__asm__ volatile("fbc.ps f10, %[z]\n" : : [z] "m"(zero) : "f10");
for (int32_t i0 = 0; i0 < (int32_t) ne0; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fmadd.ps f10, f11, f11, f10\n"
:
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f10", "f11");
}
float sum_sq;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(sum_sq)::"t0", "f1", "f2", "f3", "f4", "f5");
float l2_norm = et_powf(sum_sq, 0.5f);
if (l2_norm < eps) {
l2_norm = eps;
}
const float scale = et_fdiv(1.0f, l2_norm);
for (int32_t i0 = 0; i0 < (int32_t) ne0; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fbc.ps f12, %[scale_ptr]\n"
"fmul.ps f13, f11, f12\n"
"fsw.ps f13, %[result]\n"
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0]), [scale_ptr] "m"(scale)
: "f11", "f12", "f13");
}
}
}
}
} else {
// Intra-row: threads within each shire cooperate via L2 SCP
int shire_tid = thread_id % shire_threads;
int threads_per_row = shire_threads / total_rows;
int my_row = shire_tid / threads_per_row;
int local_tid = shire_tid % threads_per_row;
int group_base = my_row * threads_per_row;
if (my_row >= total_rows) {
FENCE;
et_barrier(ET_BARRIER_SHIRE);
return 0;
}
int64_t i1 = my_row % ne1;
int64_t i2 = (my_row / ne1) % ne2;
int64_t i3 = my_row / (ne1 * ne2);
const float * src_ptr = (const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_ptr = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
const int32_t elems_per_cl = 16;
int32_t total_cls = ((int32_t) ne0 + elems_per_cl - 1) / elems_per_cl;
int32_t cls_per_thread = (total_cls + threads_per_row - 1) / threads_per_row;
int32_t my_start = local_tid * cls_per_thread * elems_per_cl;
int32_t my_end = my_start + cls_per_thread * elems_per_cl;
if (my_end > (int32_t) ne0) {
my_end = (int32_t) ne0;
}
if (my_start >= (int32_t) ne0) {
my_start = 0;
my_end = 0;
}
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
// Phase 1: partial sum of squares
__asm__ volatile("fbci.pi f10, 0" ::: "f10");
for (int32_t i0 = my_start; i0 < my_end; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fmadd.ps f10, f11, f11, f10\n"
:
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f10", "f11");
}
float partial_sum;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(partial_sum)::"t0", "f1", "f2", "f3", "f4", "f5");
// Phase 2: L2SCP exchange
volatile float * my_slot = (volatile float *) et_shire_l2scp_local((uint64_t) shire_tid * 64);
*my_slot = partial_sum;
FENCE;
evict_to_l2((const void *) my_slot, 1, 64);
WAIT_CACHEOPS;
et_barrier(ET_BARRIER_SHIRE);
// Phase 3: all threads reduce + apply scale to own chunk
int workers = threads_per_row < total_cls ? threads_per_row : total_cls;
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
evict_to_l2((const void *) slot, 1, 64);
}
WAIT_CACHEOPS;
float total_sum_sq = 0.0f;
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
total_sum_sq += *slot;
}
float l2_norm = et_powf(total_sum_sq, 0.5f);
if (l2_norm < eps) {
l2_norm = eps;
}
const float scale = et_fdiv(1.0f, l2_norm);
if (my_start < my_end) {
uint32_t scale_bits;
__asm__ volatile("fmv.x.s %0, %1" : "=r"(scale_bits) : "f"(scale));
__asm__ volatile("fbcx.ps f13, %[sb]\n" : : [sb] "r"(scale_bits) : "f13");
for (int32_t i0 = my_start; i0 < my_end; i0 += 8) {
__asm__ volatile(
"flw.ps f12, %[x_vec]\n"
"fmul.ps f14, f12, f13\n"
"fsw.ps f14, %[result]\n"
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f12", "f14");
}
}
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
}
return 0;
}
+85
View File
@@ -0,0 +1,85 @@
/*-------------------------------------------------------------------------
* Independent Linker Script for GGML Bare Metal Kernels
*-------------------------------------------------------------------------
*/
/* Define maximum number of harts (threads) - simplified for bare metal */
MAX_HARTS = 2112;
SECTIONS
{
/* Start at the base address passed by -Wl,--defsym=BASE_ADDRESS=... */
. = BASE_ADDRESS;
/* Export entry point symbol for runtime compatibility */
KERNEL_UMODE_ENTRY = BASE_ADDRESS;
/* Initialization section - must come first */
.text.init :
{
*(.text.init)
}
/* Align to cache line boundary */
. = ALIGN(64);
/* Main text section for code */
.text : {
*(.text)
*(.text.*)
}
. = ALIGN(64);
/* Data section with global pointer setup */
.data :
{
_data_start = .;
. = ALIGN(64);
/* Small data section and global pointer */
*(.sdata .sdata.*)
PROVIDE( __global_pointer$ = . + 0x800 );
/* Regular data */
*(.data .data.*)
. = ALIGN(64);
_data_end = .;
}
. = ALIGN(64);
/* BSS section for uninitialized data */
.bss(NOLOAD) :
{
_bss_start = .;
*(.sbss*);
*(.bss*);
. = ALIGN(64);
_bss_end = .;
}
/* Thread Local Storage (TLS) sections */
. = ALIGN(64);
.tdata :
{
*(.tdata*)
. = ALIGN(64);
}
__tdata_start = ADDR(.tdata);
.tbss : {
__tbss_start = .;
*(.tbss*)
}
. = . + SIZEOF(.tbss);
. = ALIGN(64);
__tbss_end = .;
/* TLS allocation area for all harts */
.tls-alloc ALIGN(64) (NOLOAD) : {
__tls_alloc_start = .;
. = . + (ABSOLUTE(__tbss_end) - ABSOLUTE(__tdata_start)) * MAX_HARTS;
}
/* End of kernel image */
_end = .;
}
+299
View File
@@ -0,0 +1,299 @@
//******************************************************************************
// ET Floating Point Math Library
// Provides ET hardware-specific math functions, FP16 conversion, and trig functions
// for bare metal kernels
//******************************************************************************
#ifndef MATH_FP_H
#define MATH_FP_H
#include <stdint.h>
//******************************************************************************
// ET Hardware Math Functions
//******************************************************************************
// ET hardware division function (uses FRCP.PS instruction)
static inline float et_fdiv(float a, float b) {
float d;
unsigned long temp;
__asm__ volatile(
"mova.x.m %[temp] \n\t"
"mov.m.x m0, x0, 1 \n\t"
"frcp.ps %[d], %[b] \n\t"
"fmul.s %[d], %[d], %[a] \n\t"
"mova.m.x %[temp] \n\t"
: [temp] "=&r"(temp), [d] "=&f"(d)
: [a] "f"(a), [b] "f"(b));
return d;
}
// Power function using ET hardware vector instructions
// Implements pow(base, exp) = exp(exp * ln(base)) using FLOG.PS and FEXP.PS
static inline float et_powf(float base, float exp) {
// Handle special cases
if (base <= 0.0f) {
if (base == 0.0f) {
if (exp > 0.0f) {
return 0.0f;
}
// For exp <= 0, return +infinity (IEEE 754: sign=0, exp=0xFF, mantissa=0)
union {
float f;
uint32_t i;
} inf = { .i = 0x7F800000 };
return inf.f;
}
// For negative base, return NaN (IEEE 754: exp=0xFF, mantissa!=0)
union {
float f;
uint32_t i;
} nan = { .i = 0x7FC00000 };
return nan.f;
}
if (base == 1.0f) {
return 1.0f;
}
if (exp == 0.0f) {
return 1.0f;
}
if (exp == 1.0f) {
return base;
}
// Use ET hardware instructions following DNN library pattern:
// pow(base, exp) = exp(exp * ln(base))
float result;
unsigned long temp;
__asm__ volatile(
"mova.x.m %[temp] \n\t" // Save current mask state
"mov.m.x m0, x0, 1 \n\t" // Set mask register m0 to enable element 0
"flog.ps %[result], %[base] \n\t" // result = ln(base)
"fmul.s %[result], %[result], %[exp]\n\t" // result = ln(base) * exp
"fexp.ps %[result], %[result] \n\t" // result = exp(ln(base) * exp) = base^exp
"mova.m.x %[temp] \n\t" // Restore mask state
: [temp] "=&r"(temp), [result] "=&f"(result)
: [base] "f"(base), [exp] "f"(exp));
return result;
}
// Natural logarithm.
static inline float et_logf(float x) {
// Handle special cases
if (x < 0.0f) {
// Return NaN for negative input
union {
float f;
uint32_t i;
} nan = { .i = 0x7FC00000 };
return nan.f;
}
if (x == 0.0f) {
// Return -infinity for log(0)
union {
float f;
uint32_t i;
} inf = { .i = 0xFF800000 };
return inf.f;
}
if (x == 1.0f) {
return 0.0f;
}
float log2_result;
unsigned long temp;
__asm__ volatile(
"mova.x.m %[temp] \n\t" // Save current mask state
"mov.m.x m0, x0, 1 \n\t" // Set mask register m0 to enable element 0
"flog.ps %[result], %[x] \n\t" // result = log2(x)
"mova.m.x %[temp] \n\t" // Restore mask state
: [temp] "=&r"(temp), [result] "=&f"(log2_result)
: [x] "f"(x));
// Convert log2 to natural log: ln(x) = log2(x) * ln(2)
const float ln2 = 0.69314718055994530942f;
return log2_result * ln2;
}
// Square root function implemented as et_powf(x, 0.5)
static inline float et_sqrtf(float x) {
// Handle special cases
if (x < 0.0f) {
// Return NaN for negative input (IEEE 754: exp=0xFF, mantissa!=0)
union {
float f;
uint32_t i;
} nan = { .i = 0x7FC00000 };
return nan.f;
}
if (x == 0.0f) {
return 0.0f;
}
return et_powf(x, 0.5f);
}
// Base-2 exponential: returns 2^x using the ET hardware FEXP.PS instruction.
// No base conversion, no special-case clamping — this is the raw hardware op
// with just the mask save/restore wrapper. Caller is responsible for ensuring
// x is in a range that produces a useful result (roughly [-126, 128] for fp32).
static inline float __attribute__((always_inline)) et_exp2f(float x) {
unsigned long old_mask;
float out;
__asm__ volatile(
"mova.x.m %[ms] \n\t"
"mov.m.x m0, x0, 1 \n\t"
"fexp.ps %[out], %[x] \n\t"
"mova.m.x %[ms] \n\t"
: [ms] "=&r"(old_mask), [out] "=&f"(out)
: [x] "f"(x));
return out;
}
// Exponential function using ET hardware FEXP.PS instruction
// Note: FEXP.PS computes 2^x, so we need to convert: exp(x) = 2^(x * log2(e))
static inline float et_expf(float x) {
// Handle special cases
if (x > 88.0f) {
// For x > 88, exp(x) would overflow, return +infinity
union {
float f;
uint32_t i;
} inf = { .i = 0x7F800000 };
return inf.f;
}
if (x < -87.0f) {
// For x < -87, exp(x) is essentially 0
return 0.0f;
}
// Convert to base-2 exponent: x * log2(e)
const float log2e = 1.4426950408889634f; // log2(e)
float x_log2e = x * log2e;
// Use ET hardware instruction: fexp.ps computes 2^x
float result;
unsigned long temp;
__asm__ volatile(
"mova.x.m %[temp] \n\t" // Save current mask state
"mov.m.x m0, x0, 1 \n\t" // Set mask register m0 to enable element 0
"fexp.ps %[result], %[x_log2e] \n\t" // result = 2^(x * log2(e)) = exp(x)
"mova.m.x %[temp] \n\t" // Restore mask state
: [temp] "=&r"(temp), [result] "=&f"(result)
: [x_log2e] "f"(x_log2e));
return result;
}
//******************************************************************************
// Trigonometric Functions
//******************************************************************************
// FSIN.PS
// Sine function using Taylor series
static inline float et_sinf(float x) {
const float pi = 3.14159265358979323846f;
const float two_pi = 6.28318530717958647693f;
const float pi_over_2 = 1.57079632679489661923f;
if (x > pi || x < -pi) {
float cycles = x * et_fdiv(1.0f, two_pi);
int n = (int) cycles;
if (x < 0.0f) {
n--; // Floor for negative
}
x = x - (float) n * two_pi;
}
// sin(x) = sin(π - x) for x in [π/2, π]
// sin(x) = -sin(-π - x) for x in [-π, -π/2]
int negate = 0;
if (x > pi_over_2) {
x = pi - x;
} else if (x < -pi_over_2) {
x = -pi - x;
negate = 1;
}
// sin(x) ≈ x - x^3/3! + x^5/5! - x^7/7! + x^9/9! - x^11/11!
const float x2 = x * x;
const float x3 = x2 * x;
const float x5 = x3 * x2;
const float x7 = x5 * x2;
const float x9 = x7 * x2;
const float x11 = x9 * x2;
float result = x - x3 * et_fdiv(1.0f, 6.0f) // x^3/3!
+ x5 * et_fdiv(1.0f, 120.0f) // x^5/5!
- x7 * et_fdiv(1.0f, 5040.0f) // x^7/7!
+ x9 * et_fdiv(1.0f, 362880.0f) // x^9/9!
- x11 * et_fdiv(1.0f, 39916800.0f); // x^11/11!
return negate ? -result : result;
}
// Cosine function using identity cos(x) = sin(x + π/2)
static inline float et_cosf(float x) {
const float pi_over_2 = 1.57079632679489661923f;
return et_sinf(x + pi_over_2);
}
//******************************************************************************
// FP16 <-> FP32 Conversion Functions
//******************************************************************************
// Convert FP16 (IEEE 754 half precision) to FP32 (single precision)
// Uses ET hardware FCVT.PS.F16 instruction for accurate conversion
static inline float fp16_to_fp32(uint16_t h) {
float result;
unsigned long temp;
uint32_t raw = (uint32_t) h;
__asm__ volatile(
"mova.x.m %[temp] \n\t" // Save current mask state
"mov.m.x m0, x0, 1 \n\t" // Set mask register m0 to enable element 0
"fbcx.ps %[result], %[raw] \n\t" // Broadcast raw FP16 bits into vector register
"fcvt.ps.f16 %[result], %[result] \n\t" // Convert FP16 to FP32
"mova.m.x %[temp] \n\t" // Restore mask state
: [temp] "=&r"(temp), [result] "=&f"(result)
: [raw] "r"(raw));
return result;
}
// Convert FP32 (single precision) to FP16 (IEEE 754 half precision)
// Uses ET hardware FCVT.F16.PS instruction for accurate conversion
static inline uint16_t fp32_to_fp16(float f) {
float result_f;
unsigned long temp;
__asm__ volatile(
"mova.x.m %[temp] \n\t" // Save current mask state
"mov.m.x m0, x0, 1 \n\t" // Set mask register m0 to enable element 0
"fcvt.f16.ps %[result], %[f] \n\t" // Convert FP32 to FP16 (result in lower 16 bits)
"mova.m.x %[temp] \n\t" // Restore mask state
: [temp] "=&r"(temp), [result] "=&f"(result_f)
: [f] "f"(f));
// Extract lower 16 bits containing the FP16 value
// The instruction zero-extends to 32 bits, so upper 16 bits are 0
uint32_t result_bits = *(uint32_t *) &result_f;
return (uint16_t) result_bits;
}
#endif // MATH_FP_H
+220
View File
@@ -0,0 +1,220 @@
//******************************************************************************
// MEAN F32 Kernel
// Row-wise mean reduction: dst[0, i1, i2, i3] = mean(src0[0..ne00-1, i1, i2, i3])
//
// Modes:
// - total_rows >= shire_threads: row-parallel, each thread handles whole rows.
// - total_rows < shire_threads: intra-row reduction within a shire. Threads
// within a shire cooperate via shire-local L2 SCP slots. All shires
// duplicate the work because L2 SCP is per-shire (no cross-shire coherency).
//
// ne00 may be any positive size and rows may have any 4-byte alignment. We
// take the 8-wide vector path only when the row pointer is 32B-aligned and
// fall back to scalar for the leftover tail (or for the entire row when the
// row start is not 32B-aligned).
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_mean_params {
struct ggml_tensor src0; // F32 input [ne00, ne01, ne02, ne03]
struct ggml_tensor dst; // F32 output [1, ne01, ne02, ne03]
};
// Sum a contiguous F32 slice [base+i_lo, base+i_hi). Uses the 8-wide vector
// path only when `base + i_lo` is 32B-aligned; the tail (and the whole slice
// when misaligned) is summed with scalar fadd.s.
static inline float partial_sum_slice(const float * base, int32_t i_lo, int32_t i_hi) {
if (i_lo >= i_hi) {
return 0.0f;
}
const float * p = base + i_lo;
int32_t n = i_hi - i_lo;
float acc = 0.0f;
int32_t i = 0;
if (n >= 8 && (((uintptr_t) p) & 31) == 0) {
float zero = 0.0f;
__asm__ volatile("fbc.ps f10, %[z]\n" : : [z] "m"(zero) : "f10");
for (; i + 8 <= n; i += 8) {
__asm__ volatile(
"flw.ps f11, %[x]\n"
"fadd.ps f10, f10, f11\n"
:
: [x] "m"(*(const float (*)[8]) & p[i])
: "f10", "f11");
}
float vec_sum;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(vec_sum)::"t0", "f1", "f2", "f3", "f4", "f5");
acc = vec_sum;
}
for (; i < n; i++) {
acc += p[i];
}
return acc;
}
int entry_point(struct ggml_et_mean_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
const int64_t ne00 = src0->ne[0];
const int64_t ne01 = src0->ne[1];
const int64_t ne02 = src0->ne[2];
const int64_t ne03 = src0->ne[3];
const size_t nb01 = src0->nb[1];
const size_t nb02 = src0->nb[2];
const size_t nb03 = src0->nb[3];
const size_t nb1 = dst->nb[1];
const size_t nb2 = dst->nb[2];
const size_t nb3 = dst->nb[3];
if (ne00 <= 0) {
return 0;
}
const int32_t total_rows = (int32_t) (ne01 * ne02 * ne03);
const int shire_threads = SOC_MINIONS_PER_SHIRE * NUM_HARTS_PER_MINION;
const float inv_ne00 = et_fdiv(1.0f, (float) (int32_t) ne00);
// Row-parallel: each thread owns whole rows.
if (total_rows >= shire_threads) {
for (int64_t ir = thread_id; ir < total_rows; ir += num_threads) {
const int64_t i03 = ir / (ne02 * ne01);
const int64_t i02 = (ir - i03 * ne02 * ne01) / ne01;
const int64_t i01 = ir - i03 * ne02 * ne01 - i02 * ne01;
const float * src_row = (const float *) ((const char *) src0_data + i01 * nb01 + i02 * nb02 + i03 * nb03);
float * dst_ptr = (float *) ((char *) dst_data + i01 * nb1 + i02 * nb2 + i03 * nb3);
float row_sum = partial_sum_slice(src_row, 0, (int32_t) ne00);
atomic_store_f32(dst_ptr, row_sum * inv_ne00);
}
// Shire co-work
} else {
int shire_tid = thread_id % shire_threads;
int threads_per_row = shire_threads / total_rows;
int my_row = shire_tid / threads_per_row;
int local_tid = shire_tid % threads_per_row;
int group_base = my_row * threads_per_row;
if (my_row >= total_rows) {
FENCE;
et_barrier(ET_BARRIER_SHIRE);
return 0;
}
int64_t i1 = my_row % ne01;
int64_t i2 = (my_row / ne01) % ne02;
int64_t i3 = my_row / (ne01 * ne02);
const float * src_ptr = (const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_ptr = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
// Chunk size in elements, rounded up to a multiple of 8 so that every
// thread's slice start stays 32B-aligned relative to src_ptr (which
// matters for the vector path inside partial_sum_slice).
int32_t chunk = ((int32_t) ne00 + threads_per_row - 1) / threads_per_row;
chunk = (chunk + 7) & ~7;
if (chunk < 8) {
chunk = 8;
}
int32_t my_start = local_tid * chunk;
int32_t my_end = my_start + chunk;
if (my_end > (int32_t) ne00) {
my_end = (int32_t) ne00;
}
if (my_start > (int32_t) ne00) {
my_start = my_end = (int32_t) ne00;
}
int workers = ((int32_t) ne00 + chunk - 1) / chunk;
if (workers > threads_per_row) {
workers = threads_per_row;
}
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
float partial_sum = partial_sum_slice(src_ptr, my_start, my_end);
// Publish partial to shire-local L2 SCP slot (64B per slot, one per
// hart). evict_to_l2 is required on the WRITER because scalar stores
// land in L1D first; readers must also evict before reading.
volatile float * my_slot = (volatile float *) et_shire_l2scp_local((uint64_t) shire_tid * 64);
*my_slot = partial_sum;
FENCE;
evict_to_l2((const void *) my_slot, 1, 64);
WAIT_CACHEOPS;
et_barrier(ET_BARRIER_SHIRE);
if (local_tid == 0) {
// Reader-side evictions for every contributing peer slot.
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
evict_to_l2((const void *) slot, 1, 64);
}
WAIT_CACHEOPS;
float total_sum = 0.0f;
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
total_sum += *slot;
}
atomic_store_f32(dst_ptr, total_sum * inv_ne00);
}
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
}
return 0;
}
+181
View File
@@ -0,0 +1,181 @@
//******************************************************************************
// Memory Operations Kernel — tensor_store based memset
//
// Uses the tensor engine's store path (bypasses L1+L2 caches) to achieve hiher
// performance. Unrolled vector writes can write at ~25GB/s and tensor writes
// can so ~71 GB/s. Only even harts (hart 0 per minion) participate, as due to
// hardware design (only thye have matrix engine access and co-op stores seems
// slower)
//******************************************************************************
#include "platform.h"
#include "tensor.h"
#include <etsoc/common/utils.h>
#include <stdint.h>
// Operation identifiers for memops kernel
enum ggml_et_memop_type {
GGML_ET_MEMOP_MEMSET = 0,
};
// Memset operation parameters (must match host-side struct in ggml-et-memops.cpp)
struct memset_params {
uint32_t op_type;
uint32_t value;
void * dst_ptr;
size_t size;
};
// Fill all 32 f-regs with a replicated byte pattern
static inline void __attribute__((always_inline)) fill_fregs(uint32_t fill32) {
register uint64_t val __asm__("a2") = fill32;
__asm__ __volatile__(
"fbcx.ps f0, %[v]\n\t"
"fbcx.ps f1, %[v]\n\t"
"fbcx.ps f2, %[v]\n\t"
"fbcx.ps f3, %[v]\n\t"
"fbcx.ps f4, %[v]\n\t"
"fbcx.ps f5, %[v]\n\t"
"fbcx.ps f6, %[v]\n\t"
"fbcx.ps f7, %[v]\n\t"
"fbcx.ps f8, %[v]\n\t"
"fbcx.ps f9, %[v]\n\t"
"fbcx.ps f10, %[v]\n\t"
"fbcx.ps f11, %[v]\n\t"
"fbcx.ps f12, %[v]\n\t"
"fbcx.ps f13, %[v]\n\t"
"fbcx.ps f14, %[v]\n\t"
"fbcx.ps f15, %[v]\n\t"
"fbcx.ps f16, %[v]\n\t"
"fbcx.ps f17, %[v]\n\t"
"fbcx.ps f18, %[v]\n\t"
"fbcx.ps f19, %[v]\n\t"
"fbcx.ps f20, %[v]\n\t"
"fbcx.ps f21, %[v]\n\t"
"fbcx.ps f22, %[v]\n\t"
"fbcx.ps f23, %[v]\n\t"
"fbcx.ps f24, %[v]\n\t"
"fbcx.ps f25, %[v]\n\t"
"fbcx.ps f26, %[v]\n\t"
"fbcx.ps f27, %[v]\n\t"
"fbcx.ps f28, %[v]\n\t"
"fbcx.ps f29, %[v]\n\t"
"fbcx.ps f30, %[v]\n\t"
"fbcx.ps f31, %[v]\n\t" ::[v] "r"(val)
: "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", "f16",
"f17", "f18", "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31");
}
// Fill a partial region [start, end) using tensor_store for 16-byte-aligned
// chunks and byte stores for any remainder < 16 bytes.
// Assumes f-regs are already loaded with the fill pattern.
static void memset_tail(uint8_t * start, uint8_t * end, uint8_t val) {
uint8_t * cur = start;
// Full 64-byte rows via tensor_store (up to 16 at a time = 1KB)
while (cur + 64 <= end) {
size_t rows = (end - cur) / 64;
if (rows > 16) {
rows = 16;
}
tensor_store(0, 0, 3, rows - 1, (uintptr_t) cur, 0, 64);
cur += rows * 64;
}
// Remaining 16-byte aligned chunk (16, 32, or 48 bytes)
if (cur + 16 <= end) {
size_t cols = (end - cur) / 16;
tensor_store(0, 0, cols - 1, 0, (uintptr_t) cur, 0, 64);
cur += cols * 16;
}
tensor_wait(TENSOR_STORE_WAIT);
// Final < 16 bytes with byte stores
while (cur < end) {
*(volatile uint8_t *) cur = val;
cur++;
}
}
#define ALIGN_UP(ptr, align) ((uint8_t *) (((uintptr_t) (ptr) + (align) - 1) & ~((uintptr_t) (align) - 1)))
int entry_point(struct memset_params * params, kernel_environment_t * env) {
uint64_t hart_id = get_hart_id();
// Only even harts have tensor engine access
if (hart_id & 1) {
return 0;
}
if (!params || ((uintptr_t) params & 0x7) != 0) {
return -1;
}
if (params->op_type != GGML_ET_MEMOP_MEMSET) {
return -1;
}
uint8_t * dst = (uint8_t *) params->dst_ptr;
size_t size = params->size;
if (!dst || size == 0) {
return -1;
}
// Dynamic hart count from shire_mask
int num_even_harts = manual_popcountll(env->shire_mask) * SOC_MINIONS_PER_SHIRE;
// global_id: shire * 32 + minion (for even harts)
uint64_t global_id = ((hart_id >> 6) << 5) + ((hart_id >> 1) & 0x1F);
uint8_t val = params->value & 0xFF;
uint32_t fill32 = val | ((uint32_t) val << 8) | ((uint32_t) val << 16) | ((uint32_t) val << 24);
uint8_t * end = dst + size;
setup_cache_scp();
CLEAR_TENSOR_ERROR;
fill_fregs(fill32);
// Align to 16 bytes (tensor_store minimum alignment)
uint8_t * base = ALIGN_UP(dst, 16);
if (base > end) {
base = end;
}
// Hart 0 handles head bytes before alignment
if (global_id == 0) {
volatile uint8_t * p = dst;
while (p < (volatile uint8_t *) base) {
*p++ = val;
}
}
// Bulk: 1KB blocks distributed across all harts (base is already 16-byte aligned)
size_t aligned_size = end - base;
size_t total_blocks = aligned_size / 1024;
if (total_blocks > 0) {
size_t blocks_per_hart = total_blocks / num_even_harts;
size_t extra = total_blocks % num_even_harts;
size_t my_start = blocks_per_hart * global_id + (global_id < extra ? global_id : extra);
size_t my_count = blocks_per_hart + (global_id < extra ? 1 : 0);
uint8_t * addr = base + my_start * 1024;
for (size_t b = 0; b < my_count; b++) {
tensor_store(0, 0, 3, 15, (uintptr_t) addr, 0, 64);
addr += 1024;
}
tensor_wait(TENSOR_STORE_WAIT);
}
// Hart 0 handles the tail after the last full 1KB block
if (global_id == 0) {
memset_tail(base + total_blocks * 1024, end, val);
}
FENCE;
return 0;
}
@@ -0,0 +1,358 @@
//******************************************************************************
// MUL_MAT Kernel
// Matrix multiplication: C[M,N] = A[M,K] * B[K,N]
//******************************************************************************
#include "block_ops.h"
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include "quants.h"
#include <stdint.h>
#define STRIDE_M 2048 /* 32 shires x 32 minions x 2 harts */
#define STRIDE_M_KSPLIT 1024 /* 32 shires x 32 minions (both harts share rows) */
#define KSPLIT_MIN_K_BLOCKS 256 /* K >= 8192 elements */
#define KSPLIT_SMALL_ROWS_K_BLOCKS 64 /* K >= 2048 elements for very small M */
#define KSPLIT_MAX_ROWS 8 /* max rows per minion for K-split */
#define TILE_KB 256 /* K-tile size in Q4_0 blocks (8192 elems, 32KB B data) */
#define KSPLIT_GROUP_ROWS 4
#define SIMPLE_X2_ROWS 2
int entry_point(struct ggml_et_binary_params * params, void * env) {
uint64_t hart_id = get_hart_id();
// Matrix dimensions
const int64_t K = params->src0.ne[0];
const int64_t M = params->src0.ne[1];
const int64_t N = params->src1.ne[1];
const int64_t ne02 = params->src0.ne[2];
const int64_t ne03 = params->src0.ne[3];
const int64_t ne12 = params->src1.ne[2];
const int64_t ne13 = params->src1.ne[3];
// Strides (in bytes)
const size_t nb01 = params->src0.nb[1];
const size_t nb02 = params->src0.nb[2];
const size_t nb03 = params->src0.nb[3];
const size_t nb11 = params->src1.nb[1];
const size_t nb12 = params->src1.nb[2];
const size_t nb13 = params->src1.nb[3];
const size_t nbd1 = params->dst.nb[1];
const size_t nbd2 = params->dst.nb[2];
const size_t nbd3 = params->dst.nb[3];
// Q4_0 block size is 32
const int64_t K_blocks = K / 32;
const int use_simple_x2 = ((nb01 & 31) == 0);
// Broadcasting ratios
const int64_t r2 = ne12 / ne02;
const int64_t r3 = ne13 / ne03;
// K-split decision
const int64_t minion_id = hart_id >> 1; /* 0..1023 global */
const int64_t local_minion = (hart_id >> 1) & 0x1F; /* 0..31 within shire */
const int is_hart1 = hart_id & 1;
const int64_t rows_per_minion = (M + STRIDE_M_KSPLIT - 1) / STRIDE_M_KSPLIT;
const int64_t k_half = K_blocks / 2;
const int use_ksplit_small_rows = (rows_per_minion <= 2) && (K_blocks >= KSPLIT_SMALL_ROWS_K_BLOCKS);
/*
* K-split when K is large enough to benefit, and either:
* - few rows (≤4): always safe, proven working
* - more rows (5-8): only if each hart's half fits in one tile,
* otherwise L1 thrashing from 2 harts × 8 rows kills performance
*
* Also allow K-split earlier for the low-M regime (≤2 rows/minion). In
* that case the simple row-striped path leaves half the machine idle, so
* using both harts on each row pays off even for moderate K.
*/
const int use_ksplit = ((K_blocks >= KSPLIT_MIN_K_BLOCKS) && (rows_per_minion <= KSPLIT_MAX_ROWS) &&
(rows_per_minion <= 4 || k_half <= TILE_KB)) ||
use_ksplit_small_rows;
const int use_ksplit_group = !use_ksplit && (K_blocks >= KSPLIT_MIN_K_BLOCKS) && (rows_per_minion > 4) &&
(rows_per_minion <= KSPLIT_MAX_ROWS);
if (use_ksplit) {
/* Each hart processes half the K dimension */
const int64_t k_start = is_hart1 ? k_half : 0;
const int64_t k_len = is_hart1 ? (K_blocks - k_half) : k_half;
/* One cache-line-aligned L2SCP slot per minion for exchange */
volatile float * l2scp_slot = (volatile float *) et_shire_l2scp_local(local_minion * 64);
for (int64_t i3 = 0; i3 < ne13; i3++) {
const int64_t i03 = i3 / r3;
const char * src0_ptr3 = (const char *) params->src0.data + i03 * nb03;
const char * src1_ptr3 = (const char *) params->src1.data + i3 * nb13;
char * dst_ptr3 = (char *) params->dst.data + i3 * nbd3;
for (int64_t i2 = 0; i2 < ne12; i2++) {
const int64_t i02 = i2 / r2;
const char * src0_ptr2 = src0_ptr3 + i02 * nb02;
const char * src1_ptr2 = src1_ptr3 + i2 * nb12;
char * dst_ptr2 = dst_ptr3 + i2 * nbd2;
for (int64_t n = 0; n < N; n++) {
const float * b_col_base = (const float *) (src1_ptr2 + n * nb11);
for (int64_t m = minion_id; m < M; m += STRIDE_M_KSPLIT) {
const block_q4_0 * q_row = (const block_q4_0 *) (src0_ptr2 + m * nb01);
float partial = compute_row_dot_q4_0(q_row + k_start, b_col_base + k_start * 32, k_len);
if (is_hart1) {
*l2scp_slot = partial;
FENCE;
flush_to_l2((const void *) l2scp_slot, 1, 64);
WAIT_CACHEOPS;
et_sem_post(ET_BARRIER_MINION);
et_sem_wait(ET_BARRIER_MINION);
} else {
et_sem_wait(ET_BARRIER_MINION);
float other = *l2scp_slot;
et_sem_post(ET_BARRIER_MINION);
float * dst_entry = (float *) (dst_ptr2 + n * nbd1 + m * sizeof(float));
atomic_store_f32((volatile float *) dst_entry, partial + other);
}
}
}
}
}
} else if (use_ksplit_group) {
/*
* Grouped K-split for the 5-8 rows/minion regime.
*
* Both harts process the same 4-row group, each on half of K, and
* exchange 4 partial sums once per group instead of once per row.
* This keeps the K-split bandwidth benefit while cutting semaphore
* traffic by 4x relative to the old per-row exchange.
*/
const int64_t k_start = is_hart1 ? k_half : 0;
const int64_t k_len = is_hart1 ? (K_blocks - k_half) : k_half;
volatile float * l2scp_slot = (volatile float *) et_shire_l2scp_local(local_minion * 64);
for (int64_t i3 = 0; i3 < ne13; i3++) {
const int64_t i03 = i3 / r3;
const char * src0_ptr3 = (const char *) params->src0.data + i03 * nb03;
const char * src1_ptr3 = (const char *) params->src1.data + i3 * nb13;
char * dst_ptr3 = (char *) params->dst.data + i3 * nbd3;
for (int64_t i2 = 0; i2 < ne12; i2++) {
const int64_t i02 = i2 / r2;
const char * src0_ptr2 = src0_ptr3 + i02 * nb02;
const char * src1_ptr2 = src1_ptr3 + i2 * nb12;
char * dst_ptr2 = dst_ptr3 + i2 * nbd2;
for (int64_t n = 0; n < N; n++) {
const float * b_col_base = (const float *) (src1_ptr2 + n * nb11);
for (int64_t m_base = minion_id; m_base < M; m_base += STRIDE_M_KSPLIT * KSPLIT_GROUP_ROWS) {
const int64_t m0 = m_base;
const int64_t m1 = m0 + STRIDE_M_KSPLIT;
const int64_t m2 = m1 + STRIDE_M_KSPLIT;
const int64_t m3 = m2 + STRIDE_M_KSPLIT;
float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f, s3 = 0.0f;
for (int64_t kb = 0; kb < K_blocks; kb += TILE_KB) {
int64_t tile_len = k_len - kb;
if (tile_len > TILE_KB) {
tile_len = TILE_KB;
}
if (tile_len <= 0) {
break;
}
const float * b_tile = b_col_base + (k_start + kb) * 32;
const int64_t row_kb = k_start + kb;
if (m0 < M) {
s0 += compute_row_dot_q4_0((const block_q4_0 *) (src0_ptr2 + m0 * nb01) + row_kb,
b_tile, tile_len);
}
if (m1 < M) {
s1 += compute_row_dot_q4_0((const block_q4_0 *) (src0_ptr2 + m1 * nb01) + row_kb,
b_tile, tile_len);
}
if (m2 < M) {
s2 += compute_row_dot_q4_0((const block_q4_0 *) (src0_ptr2 + m2 * nb01) + row_kb,
b_tile, tile_len);
}
if (m3 < M) {
s3 += compute_row_dot_q4_0((const block_q4_0 *) (src0_ptr2 + m3 * nb01) + row_kb,
b_tile, tile_len);
}
}
if (is_hart1) {
l2scp_slot[0] = s0;
l2scp_slot[1] = s1;
l2scp_slot[2] = s2;
l2scp_slot[3] = s3;
FENCE;
flush_to_l2((const void *) l2scp_slot, 1, 64);
WAIT_CACHEOPS;
et_sem_post(ET_BARRIER_MINION);
et_sem_wait(ET_BARRIER_MINION);
} else {
et_sem_wait(ET_BARRIER_MINION);
const float p0 = l2scp_slot[0];
const float p1 = l2scp_slot[1];
const float p2 = l2scp_slot[2];
const float p3 = l2scp_slot[3];
et_sem_post(ET_BARRIER_MINION);
float * c_base = (float *) (dst_ptr2 + n * nbd1);
if (m0 < M) {
atomic_store_f32((volatile float *) (c_base + m0), s0 + p0);
}
if (m1 < M) {
atomic_store_f32((volatile float *) (c_base + m1), s1 + p1);
}
if (m2 < M) {
atomic_store_f32((volatile float *) (c_base + m2), s2 + p2);
}
if (m3 < M) {
atomic_store_f32((volatile float *) (c_base + m3), s3 + p3);
}
}
}
}
}
}
} else if (K_blocks > TILE_KB) {
/*
* Tile-outer with scalar row groups: process up to 4 rows per
* hart sharing each B tile before advancing to the next tile.
* Uses scalar float variables (not an array) to accumulate across
* tiles — avoids the flw/fadd.s/fsw stack ops that corrupt vector
* register state on ET-SoC-1's MMX-style shared FP file.
*/
for (int64_t i3 = 0; i3 < ne13; i3++) {
const int64_t i03 = i3 / r3;
const char * src0_ptr3 = (const char *) params->src0.data + i03 * nb03;
const char * src1_ptr3 = (const char *) params->src1.data + i3 * nb13;
char * dst_ptr3 = (char *) params->dst.data + i3 * nbd3;
for (int64_t i2 = 0; i2 < ne12; i2++) {
const int64_t i02 = i2 / r2;
const char * src0_ptr2 = src0_ptr3 + i02 * nb02;
const char * src1_ptr2 = src1_ptr3 + i2 * nb12;
char * dst_ptr2 = dst_ptr3 + i2 * nbd2;
for (int64_t n = 0; n < N; n++) {
const float * b_col_base = (const float *) (src1_ptr2 + n * nb11);
for (int64_t m0 = hart_id; m0 < M; m0 += STRIDE_M * 4) {
const int64_t m1 = m0 + STRIDE_M;
const int64_t m2 = m0 + STRIDE_M * 2;
const int64_t m3 = m0 + STRIDE_M * 3;
float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f, s3 = 0.0f;
for (int64_t kb = 0; kb < K_blocks; kb += TILE_KB) {
int64_t tile_len = K_blocks - kb;
if (tile_len > TILE_KB) {
tile_len = TILE_KB;
}
const float * b_tile = b_col_base + kb * 32;
s0 += compute_row_dot_q4_0((const block_q4_0 *) (src0_ptr2 + m0 * nb01) + kb, b_tile,
tile_len);
if (m1 < M) {
s1 += compute_row_dot_q4_0((const block_q4_0 *) (src0_ptr2 + m1 * nb01) + kb, b_tile,
tile_len);
}
if (m2 < M) {
s2 += compute_row_dot_q4_0((const block_q4_0 *) (src0_ptr2 + m2 * nb01) + kb, b_tile,
tile_len);
}
if (m3 < M) {
s3 += compute_row_dot_q4_0((const block_q4_0 *) (src0_ptr2 + m3 * nb01) + kb, b_tile,
tile_len);
}
}
float * dst_base = (float *) (dst_ptr2 + n * nbd1);
atomic_store_f32((volatile float *) (dst_base + m0), s0);
if (m1 < M) {
atomic_store_f32((volatile float *) (dst_base + m1), s1);
}
if (m2 < M) {
atomic_store_f32((volatile float *) (dst_base + m2), s2);
}
if (m3 < M) {
atomic_store_f32((volatile float *) (dst_base + m3), s3);
}
}
}
}
}
} else {
/*
* Simple path for small K.
*
* When `nb01` is 32-byte aligned, every row has the same block-alignment
* pattern. That lets us compute two rows together and reuse each loaded
* B chunk across both rows instead of reloading it in a second dot call.
*/
for (int64_t i3 = 0; i3 < ne13; i3++) {
const int64_t i03 = i3 / r3;
const char * src0_ptr3 = (const char *) params->src0.data + i03 * nb03;
const char * src1_ptr3 = (const char *) params->src1.data + i3 * nb13;
char * dst_ptr3 = (char *) params->dst.data + i3 * nbd3;
for (int64_t i2 = 0; i2 < ne12; i2++) {
const int64_t i02 = i2 / r2;
const char * src0_ptr2 = src0_ptr3 + i02 * nb02;
const char * src1_ptr2 = src1_ptr3 + i2 * nb12;
char * dst_ptr2 = dst_ptr3 + i2 * nbd2;
for (int64_t n = 0; n < N; n++) {
const float * b_col_base = (const float *) (src1_ptr2 + n * nb11);
q4_dot_state q4_state;
q4_dot_begin(&q4_state);
if (use_simple_x2) {
for (int64_t m0 = hart_id; m0 < M; m0 += STRIDE_M * SIMPLE_X2_ROWS) {
const int64_t m1 = m0 + STRIDE_M;
const block_q4_0 * q_row0 = (const block_q4_0 *) (src0_ptr2 + m0 * nb01);
if (m1 < M) {
const block_q4_0 * q_row1 = (const block_q4_0 *) (src0_ptr2 + m1 * nb01);
float s0, s1;
q4_dot_compute_x2_aligned(q_row0, q_row1, b_col_base, K_blocks, &s0, &s1);
float * dst0 = (float *) (dst_ptr2 + n * nbd1 + m0 * sizeof(float));
float * dst1 = (float *) (dst_ptr2 + n * nbd1 + m1 * sizeof(float));
atomic_store_f32((volatile float *) dst0, s0);
atomic_store_f32((volatile float *) dst1, s1);
} else {
float sum = q4_dot_compute(q_row0, b_col_base, K_blocks);
float * dst = (float *) (dst_ptr2 + n * nbd1 + m0 * sizeof(float));
atomic_store_f32((volatile float *) dst, sum);
}
}
} else {
for (int64_t m = hart_id; m < M; m += STRIDE_M) {
const block_q4_0 * q_row = (const block_q4_0 *) (src0_ptr2 + m * nb01);
float sum = q4_dot_compute(q_row, b_col_base, K_blocks);
float * dst_entry = (float *) (dst_ptr2 + n * nbd1 + m * sizeof(float));
atomic_store_f32((volatile float *) dst_entry, sum);
}
}
q4_dot_end(&q4_state);
}
}
}
}
return 0;
}
@@ -0,0 +1,368 @@
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include "quants.h"
#include "tensor.h"
#include <etsoc/common/utils.h>
#include <stdint.h>
// Q4_0 x F32 -> F32 MUL_MAT on the tensor (matrix) engine, TensorFMA32.
// Hart 1: dequantize Q4_0 weights to FP32 into double-buffered L2 SCP.
// Hart 0: tensor engine compute (FMA, reduce, store).
#define NUM_COMPUTE_SHIRES 32
#define MINIONS_PER_SHIRE 32
#define TILE_M 16
#define TILE_N 16
#define BLOCK_K QK4_0 // 32 elements per Q4_0 block
#define FMA_K 16 // tensor FMA k-width for FP32 (a_num_cols = FMA_K-1)
#define CACHEOP_MAX 0
#define REP_RATE 0
#define A_L1_START 0 // L1 SCP lines 0..15 for A (activations)
#define B_L1_START 16 // L1 SCP lines 16..31 for B (dequantized weights)
// L2 SCP layout per minion (double-buffered dequant panel + sync counters).
// panel = BLOCK_K k-lines x TILE_M m (FP32) = 32 * 64 = 2048 bytes, in TenB
// [k][m] order: panel[k*TILE_M + m].
#define SCP_PANEL_SIZE (BLOCK_K * TILE_M * (uint64_t) sizeof(float)) // 2048
#define SCP_READY_OFF (2 * SCP_PANEL_SIZE) // 4096
#define SCP_CONSUMED_OFF (SCP_READY_OFF + 64) // 4160
#define SCP_PER_MINION (SCP_CONSUMED_OFF + 64) // 4224
// Signal a counter value to the other hart via L2 SCP.
static inline void __attribute__((always_inline)) scp_signal(volatile uint32_t * flag, uint32_t value) {
*flag = value;
FENCE;
evict_to_l2((const void *) flag, 1, 64);
WAIT_CACHEOPS;
}
// Wait for a counter in L2 SCP to reach the expected value.
static inline void __attribute__((always_inline)) scp_wait(volatile uint32_t * flag, uint32_t expected) {
while (1) {
evict_to_l2((const void *) flag, 1, 64);
WAIT_CACHEOPS;
if (*flag >= expected) {
return;
}
}
}
// Dequantize one 32-element Q4_0 block of TILE_M weight rows into the FP32
// panel, written directly in TenB [k][m] order: panel[k*TILE_M + m].
// Low nibble of byte i -> k = i
// High nibble of byte i -> k = i + 16
// value = d * (nibble - 8)
//
// Vectorized: for each weight row m we gather 8 packed bytes at a time, expand
// the low/high nibbles to FP32 (nibble-8), scale by the block's fp16 d, and
// fscw.ps-scatter the 8 values down 8 panel lines (stride 64B) at column m.
// 4 groups of 8 cover the 32 k-values (low 0..15, high 16..31).
static inline void __attribute__((always_inline)) dequant_q4_0_panel(float * panel,
const char * src0_batch,
int64_t mb,
int64_t kb_block,
int64_t nb1_0) {
static const int32_t __attribute__((aligned(32))) scatter_idx[8] = {
0, 64, 128, 192, 256, 320, 384, 448 // byte offsets: 8 lines apart
};
static const int32_t __attribute__((aligned(32))) gather_idx[8] = {
0, 1, 2, 3, 4, 5, 6, 7 // 8 consecutive bytes
};
unsigned long old_mask;
__asm__ volatile(
"mova.x.m %[ms] \n\t"
"mov.m.x m0, x0, 0xFF \n\t" // all 8 lanes active
"flw.ps f1, (%[sidx]) \n\t" // f1 = scatter offsets
"flw.ps f2, (%[gidx]) \n\t" // f2 = gather offsets
: [ms] "=&r"(old_mask)
: [sidx] "r"(scatter_idx), [gidx] "r"(gather_idx)
: "f1", "f2");
char * pbase = (char *) panel;
for (int j = 0; j < TILE_M; ++j) {
const block_q4_0 * blk = (const block_q4_0 *) (src0_batch + (mb + j) * nb1_0) + kb_block;
uint32_t scale_raw = (uint32_t) blk->d;
const uint8_t * qs = blk->qs;
char * col = pbase + j * 4; // column m=j of the panel
__asm__ volatile(
"fbcx.ps f3, %[sb] \n\t" // broadcast fp16 scale bits
"fcvt.ps.f16 f3, f3 \n\t" // -> d in all 8 lanes (fp32)
"fgb.ps f4, f2(%[qs0]) \n\t" // gather qs[0..7]
"fandi.pi f5, f4, 15 \n\t" // low nibble
"faddi.pi f5, f5, -8 \n\t"
"fcvt.ps.pw f5, f5, rne \n\t"
"fmul.ps f5, f5, f3 \n\t"
"fscw.ps f5, f1(%[c0]) \n\t" // k=0..7 -> lines 0..7
"fsrli.pi f6, f4, 4 \n\t" // high nibble
"fandi.pi f6, f6, 15 \n\t"
"faddi.pi f6, f6, -8 \n\t"
"fcvt.ps.pw f6, f6, rne \n\t"
"fmul.ps f6, f6, f3 \n\t"
"fscw.ps f6, f1(%[c16]) \n\t" // k=16..23 -> lines 16..23
"fgb.ps f4, f2(%[qs8]) \n\t" // gather qs[8..15]
"fandi.pi f5, f4, 15 \n\t"
"faddi.pi f5, f5, -8 \n\t"
"fcvt.ps.pw f5, f5, rne \n\t"
"fmul.ps f5, f5, f3 \n\t"
"fscw.ps f5, f1(%[c8]) \n\t" // k=8..15 -> lines 8..15
"fsrli.pi f6, f4, 4 \n\t"
"fandi.pi f6, f6, 15 \n\t"
"faddi.pi f6, f6, -8 \n\t"
"fcvt.ps.pw f6, f6, rne \n\t"
"fmul.ps f6, f6, f3 \n\t"
"fscw.ps f6, f1(%[c24]) \n\t" // k=24..31 -> lines 24..31
:
: [sb] "r"(scale_raw), [qs0] "r"(qs), [qs8] "r"(qs + 8), [c0] "r"(col), [c8] "r"(col + 8 * 64),
[c16] "r"(col + 16 * 64), [c24] "r"(col + 24 * 64)
: "f3", "f4", "f5", "f6", "memory");
}
__asm__ volatile("mova.m.x %0" ::"r"(old_mask));
}
int entry_point(struct ggml_et_binary_params * params, void * env) {
(void) env;
uint64_t hart_id = get_hart_id();
uint64_t shire_id = get_shire_id();
if (shire_id >= NUM_COMPUTE_SHIRES) {
return 0;
}
const int is_hart1 = hart_id & 1;
uint64_t local_minion = (hart_id >> 1) & 0x1F;
// Dimensions (both harts need these for tile assignment)
const int64_t K = params->src0.ne[0];
const int64_t M = params->src0.ne[1];
const int64_t N = params->src1.ne[1];
if ((M % TILE_M) != 0) {
return 0;
}
if ((K % BLOCK_K) != 0) {
return 0;
}
const int64_t ne2_0 = params->src0.ne[2], ne3_0 = params->src0.ne[3];
const int64_t ne2_1 = params->src1.ne[2], ne3_1 = params->src1.ne[3];
const int64_t nb1_0 = params->src0.nb[1];
const int64_t nb2_0 = params->src0.nb[2], nb3_0 = params->src0.nb[3];
const int64_t nb1_1 = params->src1.nb[1];
const int64_t nb2_1 = params->src1.nb[2], nb3_1 = params->src1.nb[3];
const int64_t nb1_d = params->dst.nb[1];
const int64_t nb2_d = params->dst.nb[2], nb3_d = params->dst.nb[3];
const char * src0_base = (const char *) params->src0.data;
const char * src1_base = (const char *) params->src1.data;
char * dst_base = (char *) params->dst.data;
const int64_t m_tiles = M / TILE_M;
const int64_t n_tiles = (N + TILE_N - 1) / TILE_N;
const int64_t batch_count = ne2_1 * ne3_1;
const int64_t base_tiles = m_tiles * n_tiles * batch_count;
const int64_t r2 = ne2_1 / ne2_0;
const int64_t r3 = ne3_1 / ne3_0;
const int64_t k_steps = K / BLOCK_K; // number of Q4_0 blocks
// Force a single K-split.
const int64_t k_splits = 1;
const int64_t tiles_per_shire = MINIONS_PER_SHIRE / k_splits;
const int64_t k_split = local_minion % k_splits;
const int64_t local_tile_idx = local_minion / k_splits;
const int64_t tiles_stride = (int64_t) NUM_COMPUTE_SHIRES * tiles_per_shire;
const int64_t k_steps_per_split = k_steps / k_splits;
const int64_t kb_start = k_split * k_steps_per_split; // first block
const int64_t kb_end = kb_start + k_steps_per_split; // one past last
// L2 SCP pointers for this minion's double-buffered panels + sync.
uint64_t scp_base = local_minion * SCP_PER_MINION;
float * scp_panel[2] = {
(float *) et_shire_l2scp_local(scp_base),
(float *) et_shire_l2scp_local(scp_base + SCP_PANEL_SIZE),
};
volatile uint32_t * ready_ctr = (volatile uint32_t *) et_shire_l2scp_local(scp_base + SCP_READY_OFF);
volatile uint32_t * consumed_ctr = (volatile uint32_t *) et_shire_l2scp_local(scp_base + SCP_CONSUMED_OFF);
// ================================================================
// Hart 1: Q4_0 weight dequant producer
// ================================================================
if (is_hart1) {
scp_signal(ready_ctr, 0);
scp_signal(consumed_ctr, 0);
uint32_t chunk_id = 0;
for (int64_t tile = (int64_t) shire_id + local_tile_idx * NUM_COMPUTE_SHIRES; tile < base_tiles;
tile += tiles_stride) {
const int64_t tiles_per_batch = m_tiles * n_tiles;
const int64_t batch_idx = tile / tiles_per_batch;
const int64_t tile_in_batch = tile % tiles_per_batch;
const int64_t mb_idx = tile_in_batch % m_tiles;
const int64_t i3 = batch_idx / ne2_1;
const int64_t i2 = batch_idx % ne2_1;
const int64_t i2_0 = i2 / r2;
const int64_t i3_0 = i3 / r3;
const char * src0_batch = src0_base + i3_0 * nb3_0 + i2_0 * nb2_0;
const int64_t mb = mb_idx * TILE_M;
for (int64_t kb = kb_start; kb < kb_end; ++kb) {
int buf = chunk_id & 1;
// Back-pressure: wait for hart 0 to finish with this buffer.
if (chunk_id >= 2) {
scp_wait(consumed_ctr, chunk_id - 1);
}
dequant_q4_0_panel(scp_panel[buf], src0_batch, mb, kb, nb1_0);
FENCE;
flush_to_l2(scp_panel[buf], BLOCK_K, 64);
WAIT_CACHEOPS;
chunk_id++;
scp_signal(ready_ctr, chunk_id);
}
}
FENCE;
return 0;
}
// ================================================================
// Hart 0: tensor engine compute
// ================================================================
uint64_t my_minion_id = get_minion_id();
const uint64_t group_base_global = my_minion_id - k_split;
setup_cache_scp();
#if CACHEOP_MAX > 0 || REP_RATE > 0
ucache_control(1, REP_RATE, CACHEOP_MAX);
#endif
CLEAR_TENSOR_ERROR;
evict_to_l2((const void *) ready_ctr, 1, 64);
WAIT_CACHEOPS;
evict_to_l2((const void *) consumed_ctr, 1, 64);
WAIT_CACHEOPS;
uint32_t chunk_id = 0;
for (int64_t tile = (int64_t) shire_id + local_tile_idx * NUM_COMPUTE_SHIRES; tile < base_tiles;
tile += tiles_stride) {
const int64_t tiles_per_batch = m_tiles * n_tiles;
const int64_t batch_idx = tile / tiles_per_batch;
const int64_t tile_in_batch = tile % tiles_per_batch;
const int64_t nb_idx = tile_in_batch / m_tiles;
const int64_t mb_idx = tile_in_batch % m_tiles;
const int64_t i3 = batch_idx / ne2_1;
const int64_t i2 = batch_idx % ne2_1;
const char * src1_batch = src1_base + i3 * nb3_1 + i2 * nb2_1;
char * dst_batch = dst_base + i3 * nb3_d + i2 * nb2_d;
const int64_t mb = mb_idx * TILE_M;
const int64_t nb = nb_idx * TILE_N;
const int64_t n_cur = (nb + TILE_N <= N) ? TILE_N : (N - nb);
// Partial-N tiles run TensorFMA32 with a_num_rows = n_cur-1.
// Errata Type D workaround for n_cur == 4 (AROWS==3): pad A to AROWS==4.
const int64_t arows_fma = (n_cur == 4) ? 4 : (n_cur - 1);
if (n_cur == 4) {
// Zero the padded 5th A row (line A_L1_START+4) once; the per-pass A
// load only writes lines A_L1_START..+3, so this persists.
static const float __attribute__((aligned(64))) zero_line[16] = { 0 };
tensor_load(false, false, A_L1_START + 4, TENSOR_LOAD_PLAIN, 0, (uint64_t) zero_line, 0,
0, // 1 line
64, 0);
tensor_wait(TENSOR_LOAD_WAIT_0);
}
int first = 1; // first_pass=1 only for the very first FMA of the tile
for (int64_t kb = kb_start; kb < kb_end; ++kb) {
int buf = chunk_id & 1;
// Wait for hart 1 to finish dequantizing this block.
chunk_id++;
scp_wait(ready_ctr, chunk_id);
// Two FMA passes over the 32-wide block (16 K-cols each).
for (int half = 0; half < 2; ++half) {
const int64_t k_elem = kb * BLOCK_K + half * FMA_K;
// Load A (activations) for this 16-K sub-tile, PLAIN.
tensor_load(false, false, A_L1_START, TENSOR_LOAD_PLAIN, 0,
(uint64_t) (src1_batch + nb * nb1_1 + k_elem * (int64_t) sizeof(float)), 0, n_cur - 1,
(uint64_t) nb1_1, 0);
// Load B (dequantized weights) half from L2 SCP panel, PLAIN.
tensor_load(false, false, B_L1_START, TENSOR_LOAD_PLAIN, 0,
(uint64_t) (scp_panel[buf] + (int64_t) half * FMA_K * TILE_M), 0, FMA_K - 1, 64, 1);
tensor_wait(TENSOR_LOAD_WAIT_0);
tensor_wait(TENSOR_LOAD_WAIT_1);
tensor_fma(false,
3, // b_num_col: (16/4)-1
arows_fma, // a_num_rows (n_cur-1, or 4 for the n_cur==4 errata pad)
FMA_K - 1, // a_num_cols
0, false, false, false, false, B_L1_START, A_L1_START, TENSOR_FMA_OP_FP32, first);
tensor_wait(TENSOR_FMA_WAIT);
first = 0;
}
// Signal that this buffer is free for hart 1 to reuse.
scp_signal(consumed_ctr, chunk_id);
}
// K-split ring reduce.
if (k_splits > 1) {
const uint64_t num_regs = (uint64_t) n_cur * 2;
if (k_split > 0) {
tensor_reduce_recv(0, TENSOR_REDUCE_OP_FADD, num_regs, group_base_global + k_split - 1);
tensor_wait(TENSOR_REDUCE_WAIT);
}
if (k_split < k_splits - 1) {
tensor_reduce_send(0, num_regs, group_base_global + k_split + 1);
tensor_wait(TENSOR_REDUCE_WAIT);
}
}
// Store FP32 result tile (only the last k-split owns the final sum).
if (k_split == k_splits - 1) {
tensor_store(0, 0, 3, n_cur - 1, (uint64_t) (dst_batch + nb * nb1_d + mb * (int64_t) sizeof(float)), 0,
(uint64_t) nb1_d);
tensor_wait(TENSOR_STORE_WAIT);
}
}
FENCE;
return 0;
}
@@ -0,0 +1,413 @@
//******************************************************************************
// MUL_MAT Kernel
// Matrix multiplication: C[M,N] = A[M,K] * B[K,N]
//******************************************************************************
#include "block_ops.h"
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include "quants.h"
#include <stdint.h>
#define STRIDE_M 2048 /* 32 shires x 32 minions x 2 harts */
#define STRIDE_M_KSPLIT 1024 /* 32 shires x 32 minions (both harts share rows) */
#define KSPLIT_MIN_K_BLOCKS 256 /* K >= 8192 elements */
#define KSPLIT_SMALL_ROWS_K_BLOCKS 64 /* K >= 2048 elements for very small M */
#define KSPLIT_MAX_ROWS 8 /* max rows per minion for K-split */
#define TILE_KB 256 /* K-tile size in Q8_0 blocks (8192 elems, 32KB B data) */
#define KSPLIT_GROUP_ROWS 4
#define SIMPLE_X2_ROWS 2
static inline size_t tensor_bytes(const struct ggml_tensor * t) {
return (size_t) t->ne[0] * t->ne[1] * t->ne[2] * t->ne[3] * t->nb[0];
}
int entry_point(struct ggml_et_mm_q8_params * params, void * env) {
uint64_t hart_id = get_hart_id();
// Matrix dimensions
const int64_t K = params->src0.ne[0];
const int64_t M = params->src0.ne[1];
const int64_t N = params->src1.ne[1];
const int64_t ne02 = params->src0.ne[2];
const int64_t ne03 = params->src0.ne[3];
const int64_t ne12 = params->src1.ne[2];
const int64_t ne13 = params->src1.ne[3];
// Strides (in bytes)
const size_t nb01 = params->src0.nb[1];
const size_t nb02 = params->src0.nb[2];
const size_t nb03 = params->src0.nb[3];
const size_t nb11 = params->src1.nb[1];
const size_t nb12 = params->src1.nb[2];
const size_t nb13 = params->src1.nb[3];
const size_t nbd1 = params->dst.nb[1];
const size_t nbd2 = params->dst.nb[2];
const size_t nbd3 = params->dst.nb[3];
// Optional residual bias
const char * bias_base = (const char *) params->bias.data;
const size_t nbb1 = params->bias.nb[1];
const size_t nbb2 = params->bias.nb[2];
const size_t nbb3 = params->bias.nb[3];
// Q8_0 block size is 32
const int64_t K_blocks = K / 32;
const int use_simple_x2 = ((nb01 & 31) == 0);
// Broadcasting ratios
const int64_t r2 = ne12 / ne02;
const int64_t r3 = ne13 / ne03;
// K-split decision
const int64_t minion_id = hart_id >> 1; /* 0..1023 global */
const int64_t local_minion = (hart_id >> 1) & 0x1F; /* 0..31 within shire */
const int is_hart1 = hart_id & 1;
const int64_t rows_per_minion = (M + STRIDE_M_KSPLIT - 1) / STRIDE_M_KSPLIT;
const int64_t k_half = K_blocks / 2;
const int use_ksplit_small_rows = (rows_per_minion <= 2) && (K_blocks >= KSPLIT_SMALL_ROWS_K_BLOCKS);
/*
* K-split when K is large enough to benefit, and either:
* - few rows (≤4): always safe, proven working
* - more rows (5-8): only if each hart's half fits in one tile,
* otherwise L1 thrashing from 2 harts × 8 rows kills performance
*
* Also allow K-split earlier for the low-M regime (≤2 rows/minion). In
* that case the simple row-striped path leaves half the machine idle, so
* using both harts on each row pays off even for moderate K.
*/
const int use_ksplit = ((K_blocks >= KSPLIT_MIN_K_BLOCKS) && (rows_per_minion <= KSPLIT_MAX_ROWS) &&
(rows_per_minion <= 4 || k_half <= TILE_KB)) ||
use_ksplit_small_rows;
const int use_ksplit_group = !use_ksplit && (K_blocks >= KSPLIT_MIN_K_BLOCKS) && (rows_per_minion > 4) &&
(rows_per_minion <= KSPLIT_MAX_ROWS);
evict_region_past_l2(params->src1.data, tensor_bytes(&params->src1));
if (params->bias.data) {
evict_region_past_l2(params->bias.data, tensor_bytes(&params->bias));
}
if (use_ksplit) {
/* Each hart processes half the K dimension */
const int64_t k_start = is_hart1 ? k_half : 0;
const int64_t k_len = is_hart1 ? (K_blocks - k_half) : k_half;
/* One cache-line-aligned L2SCP slot per minion for exchange */
volatile float * l2scp_slot = (volatile float *) et_shire_l2scp_local(local_minion * 64);
for (int64_t i3 = 0; i3 < ne13; i3++) {
const int64_t i03 = i3 / r3;
const char * src0_ptr3 = (const char *) params->src0.data + i03 * nb03;
const char * src1_ptr3 = (const char *) params->src1.data + i3 * nb13;
char * dst_ptr3 = (char *) params->dst.data + i3 * nbd3;
const char * bias_ptr3 = bias_base ? bias_base + i3 * nbb3 : (const char *) 0;
for (int64_t i2 = 0; i2 < ne12; i2++) {
const int64_t i02 = i2 / r2;
const char * src0_ptr2 = src0_ptr3 + i02 * nb02;
const char * src1_ptr2 = src1_ptr3 + i2 * nb12;
char * dst_ptr2 = dst_ptr3 + i2 * nbd2;
const char * bias_ptr2 = bias_ptr3 ? bias_ptr3 + i2 * nbb2 : (const char *) 0;
for (int64_t n = 0; n < N; n++) {
const float * b_col_base = (const float *) (src1_ptr2 + n * nb11);
const float * bias_n = bias_ptr2 ? (const float *) (bias_ptr2 + n * nbb1) : (const float *) 0;
for (int64_t m = minion_id; m < M; m += STRIDE_M_KSPLIT) {
const block_q8_0 * q_row = (const block_q8_0 *) (src0_ptr2 + m * nb01);
float partial = compute_row_dot_q8_0(q_row + k_start, b_col_base + k_start * 32, k_len);
if (is_hart1) {
*l2scp_slot = partial;
FENCE;
flush_to_l2((const void *) l2scp_slot, 1, 64);
WAIT_CACHEOPS;
et_sem_post(ET_BARRIER_MINION);
et_sem_wait(ET_BARRIER_MINION);
} else {
et_sem_wait(ET_BARRIER_MINION);
float other = *l2scp_slot;
et_sem_post(ET_BARRIER_MINION);
float * dst_entry = (float *) (dst_ptr2 + n * nbd1 + m * sizeof(float));
float sum = partial + other;
if (bias_n) {
sum += bias_n[m];
}
atomic_store_f32((volatile float *) dst_entry, sum);
}
}
}
}
}
} else if (use_ksplit_group) {
/*
* Grouped K-split for the 5-8 rows/minion regime.
*
* Both harts process the same 4-row group, each on half of K, and
* exchange 4 partial sums once per group instead of once per row.
* This keeps the K-split bandwidth benefit while cutting semaphore
* traffic by 4x relative to the old per-row exchange.
*/
const int64_t k_start = is_hart1 ? k_half : 0;
const int64_t k_len = is_hart1 ? (K_blocks - k_half) : k_half;
volatile float * l2scp_slot = (volatile float *) et_shire_l2scp_local(local_minion * 64);
for (int64_t i3 = 0; i3 < ne13; i3++) {
const int64_t i03 = i3 / r3;
const char * src0_ptr3 = (const char *) params->src0.data + i03 * nb03;
const char * src1_ptr3 = (const char *) params->src1.data + i3 * nb13;
char * dst_ptr3 = (char *) params->dst.data + i3 * nbd3;
const char * bias_ptr3 = bias_base ? bias_base + i3 * nbb3 : (const char *) 0;
for (int64_t i2 = 0; i2 < ne12; i2++) {
const int64_t i02 = i2 / r2;
const char * src0_ptr2 = src0_ptr3 + i02 * nb02;
const char * src1_ptr2 = src1_ptr3 + i2 * nb12;
char * dst_ptr2 = dst_ptr3 + i2 * nbd2;
const char * bias_ptr2 = bias_ptr3 ? bias_ptr3 + i2 * nbb2 : (const char *) 0;
for (int64_t n = 0; n < N; n++) {
const float * b_col_base = (const float *) (src1_ptr2 + n * nb11);
const float * bias_n = bias_ptr2 ? (const float *) (bias_ptr2 + n * nbb1) : (const float *) 0;
for (int64_t m_base = minion_id; m_base < M; m_base += STRIDE_M_KSPLIT * KSPLIT_GROUP_ROWS) {
const int64_t m0 = m_base;
const int64_t m1 = m0 + STRIDE_M_KSPLIT;
const int64_t m2 = m1 + STRIDE_M_KSPLIT;
const int64_t m3 = m2 + STRIDE_M_KSPLIT;
float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f, s3 = 0.0f;
for (int64_t kb = 0; kb < K_blocks; kb += TILE_KB) {
int64_t tile_len = k_len - kb;
if (tile_len > TILE_KB) {
tile_len = TILE_KB;
}
if (tile_len <= 0) {
break;
}
const float * b_tile = b_col_base + (k_start + kb) * 32;
const int64_t row_kb = k_start + kb;
if (m0 < M) {
s0 += compute_row_dot_q8_0((const block_q8_0 *) (src0_ptr2 + m0 * nb01) + row_kb,
b_tile, tile_len);
}
if (m1 < M) {
s1 += compute_row_dot_q8_0((const block_q8_0 *) (src0_ptr2 + m1 * nb01) + row_kb,
b_tile, tile_len);
}
if (m2 < M) {
s2 += compute_row_dot_q8_0((const block_q8_0 *) (src0_ptr2 + m2 * nb01) + row_kb,
b_tile, tile_len);
}
if (m3 < M) {
s3 += compute_row_dot_q8_0((const block_q8_0 *) (src0_ptr2 + m3 * nb01) + row_kb,
b_tile, tile_len);
}
}
if (is_hart1) {
l2scp_slot[0] = s0;
l2scp_slot[1] = s1;
l2scp_slot[2] = s2;
l2scp_slot[3] = s3;
FENCE;
flush_to_l2((const void *) l2scp_slot, 1, 64);
WAIT_CACHEOPS;
et_sem_post(ET_BARRIER_MINION);
et_sem_wait(ET_BARRIER_MINION);
} else {
et_sem_wait(ET_BARRIER_MINION);
const float p0 = l2scp_slot[0];
const float p1 = l2scp_slot[1];
const float p2 = l2scp_slot[2];
const float p3 = l2scp_slot[3];
et_sem_post(ET_BARRIER_MINION);
float * c_base = (float *) (dst_ptr2 + n * nbd1);
const float b0 = bias_n ? bias_n[m0] : 0.0f;
const float b1 = (bias_n && m1 < M) ? bias_n[m1] : 0.0f;
const float b2 = (bias_n && m2 < M) ? bias_n[m2] : 0.0f;
const float b3 = (bias_n && m3 < M) ? bias_n[m3] : 0.0f;
if (m0 < M) {
atomic_store_f32((volatile float *) (c_base + m0), s0 + p0 + b0);
}
if (m1 < M) {
atomic_store_f32((volatile float *) (c_base + m1), s1 + p1 + b1);
}
if (m2 < M) {
atomic_store_f32((volatile float *) (c_base + m2), s2 + p2 + b2);
}
if (m3 < M) {
atomic_store_f32((volatile float *) (c_base + m3), s3 + p3 + b3);
}
}
}
}
}
}
} else if (K_blocks > TILE_KB) {
/*
* Tile-outer with scalar row groups: process up to 4 rows per
* hart sharing each B tile before advancing to the next tile.
* Uses scalar float variables (not an array) to accumulate across
* tiles — avoids the flw/fadd.s/fsw stack ops that corrupt vector
* register state on ET-SoC-1's MMX-style shared FP file.
*/
for (int64_t i3 = 0; i3 < ne13; i3++) {
const int64_t i03 = i3 / r3;
const char * src0_ptr3 = (const char *) params->src0.data + i03 * nb03;
const char * src1_ptr3 = (const char *) params->src1.data + i3 * nb13;
char * dst_ptr3 = (char *) params->dst.data + i3 * nbd3;
const char * bias_ptr3 = bias_base ? bias_base + i3 * nbb3 : (const char *) 0;
for (int64_t i2 = 0; i2 < ne12; i2++) {
const int64_t i02 = i2 / r2;
const char * src0_ptr2 = src0_ptr3 + i02 * nb02;
const char * src1_ptr2 = src1_ptr3 + i2 * nb12;
char * dst_ptr2 = dst_ptr3 + i2 * nbd2;
const char * bias_ptr2 = bias_ptr3 ? bias_ptr3 + i2 * nbb2 : (const char *) 0;
for (int64_t n = 0; n < N; n++) {
const float * b_col_base = (const float *) (src1_ptr2 + n * nb11);
const float * bias_n = bias_ptr2 ? (const float *) (bias_ptr2 + n * nbb1) : (const float *) 0;
for (int64_t m0 = hart_id; m0 < M; m0 += STRIDE_M * 4) {
const int64_t m1 = m0 + STRIDE_M;
const int64_t m2 = m0 + STRIDE_M * 2;
const int64_t m3 = m0 + STRIDE_M * 3;
float s0 = 0.0f, s1 = 0.0f, s2 = 0.0f, s3 = 0.0f;
for (int64_t kb = 0; kb < K_blocks; kb += TILE_KB) {
int64_t tile_len = K_blocks - kb;
if (tile_len > TILE_KB) {
tile_len = TILE_KB;
}
const float * b_tile = b_col_base + kb * 32;
s0 += compute_row_dot_q8_0((const block_q8_0 *) (src0_ptr2 + m0 * nb01) + kb, b_tile,
tile_len);
if (m1 < M) {
s1 += compute_row_dot_q8_0((const block_q8_0 *) (src0_ptr2 + m1 * nb01) + kb, b_tile,
tile_len);
}
if (m2 < M) {
s2 += compute_row_dot_q8_0((const block_q8_0 *) (src0_ptr2 + m2 * nb01) + kb, b_tile,
tile_len);
}
if (m3 < M) {
s3 += compute_row_dot_q8_0((const block_q8_0 *) (src0_ptr2 + m3 * nb01) + kb, b_tile,
tile_len);
}
}
float * dst_base = (float *) (dst_ptr2 + n * nbd1);
const float b0 = bias_n ? bias_n[m0] : 0.0f;
const float b1 = (bias_n && m1 < M) ? bias_n[m1] : 0.0f;
const float b2 = (bias_n && m2 < M) ? bias_n[m2] : 0.0f;
const float b3 = (bias_n && m3 < M) ? bias_n[m3] : 0.0f;
atomic_store_f32((volatile float *) (dst_base + m0), s0 + b0);
if (m1 < M) {
atomic_store_f32((volatile float *) (dst_base + m1), s1 + b1);
}
if (m2 < M) {
atomic_store_f32((volatile float *) (dst_base + m2), s2 + b2);
}
if (m3 < M) {
atomic_store_f32((volatile float *) (dst_base + m3), s3 + b3);
}
}
}
}
}
} else {
/*
* Simple path for small K.
*
* When `nb01` is 32-byte aligned, every row has the same block-alignment
* pattern. That lets us compute two rows together and reuse each loaded
* B chunk across both rows instead of reloading it in a second dot call.
*/
for (int64_t i3 = 0; i3 < ne13; i3++) {
const int64_t i03 = i3 / r3;
const char * src0_ptr3 = (const char *) params->src0.data + i03 * nb03;
const char * src1_ptr3 = (const char *) params->src1.data + i3 * nb13;
char * dst_ptr3 = (char *) params->dst.data + i3 * nbd3;
const char * bias_ptr3 = bias_base ? bias_base + i3 * nbb3 : (const char *) 0;
for (int64_t i2 = 0; i2 < ne12; i2++) {
const int64_t i02 = i2 / r2;
const char * src0_ptr2 = src0_ptr3 + i02 * nb02;
const char * src1_ptr2 = src1_ptr3 + i2 * nb12;
char * dst_ptr2 = dst_ptr3 + i2 * nbd2;
const char * bias_ptr2 = bias_ptr3 ? bias_ptr3 + i2 * nbb2 : (const char *) 0;
for (int64_t n = 0; n < N; n++) {
const float * b_col_base = (const float *) (src1_ptr2 + n * nb11);
const float * bias_n = bias_ptr2 ? (const float *) (bias_ptr2 + n * nbb1) : (const float *) 0;
q8_dot_state q8_state;
q8_dot_begin(&q8_state);
if (use_simple_x2) {
for (int64_t m0 = hart_id; m0 < M; m0 += STRIDE_M * SIMPLE_X2_ROWS) {
const int64_t m1 = m0 + STRIDE_M;
const block_q8_0 * q_row0 = (const block_q8_0 *) (src0_ptr2 + m0 * nb01);
if (m1 < M) {
const block_q8_0 * q_row1 = (const block_q8_0 *) (src0_ptr2 + m1 * nb01);
float s0, s1;
q8_dot_compute_x2_aligned(q_row0, q_row1, b_col_base, K_blocks, &s0, &s1);
float * dst0 = (float *) (dst_ptr2 + n * nbd1 + m0 * sizeof(float));
float * dst1 = (float *) (dst_ptr2 + n * nbd1 + m1 * sizeof(float));
if (bias_n) {
s0 += bias_n[m0];
s1 += bias_n[m1];
}
atomic_store_f32((volatile float *) dst0, s0);
atomic_store_f32((volatile float *) dst1, s1);
} else {
float sum = q8_dot_compute(q_row0, b_col_base, K_blocks);
float * dst = (float *) (dst_ptr2 + n * nbd1 + m0 * sizeof(float));
if (bias_n) {
sum += bias_n[m0];
}
atomic_store_f32((volatile float *) dst, sum);
}
}
} else {
for (int64_t m = hart_id; m < M; m += STRIDE_M) {
const block_q8_0 * q_row = (const block_q8_0 *) (src0_ptr2 + m * nb01);
float sum = q8_dot_compute(q_row, b_col_base, K_blocks);
float * dst_entry = (float *) (dst_ptr2 + n * nbd1 + m * sizeof(float));
if (bias_n) {
sum += bias_n[m];
}
atomic_store_f32((volatile float *) dst_entry, sum);
}
}
q8_dot_end(&q8_state);
}
}
}
}
#ifdef ET_UBERKERNEL
FENCE;
evict_region_past_l2(params->dst.data, tensor_bytes(&params->dst));
WAIT_CACHEOPS;
FENCE;
#endif
return 0;
}
@@ -0,0 +1,142 @@
//******************************************************************************
// MUL_MAT Kernel
// Matrix multiplication: C[M,N] = A[M,K] * B[K,N]
//******************************************************************************
#include "block_ops.h"
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include "quants.h"
#include <stdint.h>
int entry_point(struct ggml_et_binary_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env || params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
// Thread coordination
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0 || (thread_id & 1)) {
return 0; // Skip odd threads to avoid resource contention
}
int effective_thread_id = thread_id / 2;
int effective_num_threads = (num_threads + 1) / 2;
// Extract tensor references
struct ggml_tensor * src0 = &params->src0; // Weight matrix A (F16)
struct ggml_tensor * src1 = &params->src1; // Activation matrix B (F16/F32)
struct ggml_tensor * dst = &params->dst; // Output matrix C (F32)
// Generic non-matrix-engine path: F16 x (F16/F32) -> F32
if (src0->type != GGML_TYPE_F16 || (src1->type != GGML_TYPE_F16 && src1->type != GGML_TYPE_F32) ||
dst->type != GGML_TYPE_F32) {
return -1;
}
const uint16_t * src0_data = (const uint16_t *) src0->data;
float * dst_data = (float *) dst->data;
// Dimensions and Strides
const int64_t K = src0->ne[0];
const int64_t M = src0->ne[1];
const int64_t N = src1->ne[1];
const int64_t ne02 = src0->ne[2], ne03 = src0->ne[3];
const int64_t ne12 = src1->ne[2], ne13 = src1->ne[3];
const int64_t ne2 = dst->ne[2], ne3 = dst->ne[3];
const size_t nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
const size_t nb11 = src1->nb[1], nb12 = src1->nb[2], nb13 = src1->nb[3];
const size_t nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
// F16 specific block size (Usually QK_F16)
const int block_size = QK_F16;
const int64_t K_blocks = K / block_size;
const int64_t K_remainder = K % block_size;
// Threading distribution
const uint64_t total_elements = M * N * ne2 * ne3;
const uint64_t per_thread = 16;
const uint64_t threads_stride = per_thread * effective_num_threads;
if (effective_thread_id * per_thread >= total_elements) {
return 0;
}
// Broadcasting support
const int64_t r2 = ne12 / ne02;
const int64_t r3 = ne13 / ne03;
for (uint64_t base_idx = effective_thread_id * per_thread; base_idx < total_elements; base_idx += threads_stride) {
for (uint64_t j = 0; j < per_thread; j++) {
const uint64_t idx = base_idx + j;
if (idx >= total_elements) {
break;
}
// Index decoding
const int64_t i3 = idx / (M * N * ne2);
const int64_t rem3 = idx % (M * N * ne2);
const int64_t i2 = rem3 / (M * N);
const int64_t rem2 = rem3 % (M * N);
const int64_t n = rem2 / M;
const int64_t m = rem2 % M;
const int64_t i03 = i3 / r3, i02 = i2 / r2;
const int64_t i13 = (ne13 > 1) ? i3 : 0, i12 = (ne12 > 1) ? i2 : 0;
float sum = 0.0f;
const uint16_t * f16_row =
(const uint16_t *) ((const char *) src0_data + m * nb01 + i02 * nb02 + i03 * nb03);
if (src1->type == GGML_TYPE_F32) {
const float * src1_data = (const float *) src1->data;
for (int64_t kb = 0; kb < K_blocks; kb++) {
const float * b_col_ptr =
(const float *) ((const char *) src1_data + (kb * block_size) * sizeof(float) + n * nb11 +
i12 * nb12 + i13 * nb13);
sum += compute_block_dot_product_f16_naive(&f16_row[kb * block_size], b_col_ptr);
}
if (K_remainder > 0) {
const int64_t offset = K_blocks * block_size;
const float * b_col_ptr = (const float *) ((const char *) src1_data + offset * sizeof(float) +
n * nb11 + i12 * nb12 + i13 * nb13);
sum += compute_block_dot_product_f16_partial(&f16_row[offset], b_col_ptr, K_remainder);
}
} else {
const uint16_t * src1_data = (const uint16_t *) src1->data;
for (int64_t kb = 0; kb < K_blocks; kb++) {
const uint16_t * b_col_ptr =
(const uint16_t *) ((const char *) src1_data + (kb * block_size) * sizeof(uint16_t) + n * nb11 +
i12 * nb12 + i13 * nb13);
sum += compute_block_dot_product_f16_f16_partial(&f16_row[kb * block_size], b_col_ptr, block_size);
}
if (K_remainder > 0) {
const int64_t offset = K_blocks * block_size;
const uint16_t * b_col_ptr =
(const uint16_t *) ((const char *) src1_data + offset * sizeof(uint16_t) + n * nb11 +
i12 * nb12 + i13 * nb13);
sum += compute_block_dot_product_f16_f16_partial(&f16_row[offset], b_col_ptr, K_remainder);
}
}
// Atomic store for output
volatile float * c_element =
(volatile float *) ((char *) dst_data + m * dst->nb[0] + n * nb1 + i2 * nb2 + i3 * nb3);
atomic_store_f32(c_element, sum);
}
}
return 0;
}
@@ -0,0 +1,329 @@
#include "ggml_tensor.h"
#include "platform.h"
#include "tensor.h"
#include <etsoc/common/utils.h>
#include <stdint.h>
// FP16 x FP16 -> FP32 MUL_MAT with hart 1 B-panel packing
//
// Hart 0: tensor engine (load A, load B from SCP, FMA, reduce, store)
// Hart 1: pack B into double-buffered L2 SCP panels, flush for tensor_load
//
// Sync: monotonic counters in L2 SCP with evict-based coherency.
// Double-buffered bpanel allows pack/FMA overlap.
//
#define NUM_COMPUTE_SHIRES 32
#define MINIONS_PER_SHIRE 32
#define TILE_M 16
#define TILE_N 16
#define TILE_K 32
#define CACHEOP_MAX 0
#define REP_RATE 0
#define A_L1_START 0 // SCP lines 0..15 for A
#define B_L1_START 16 // SCP lines 16..31 for B
typedef uint16_t et_fp16_t;
// L2 SCP layout per minion (double-buffered bpanel + sync counters)
// [0..1023] bpanel buffer 0 (16 lines x 64 bytes)
// [1024..2047] bpanel buffer 1
// [2048..2111] ready counter (hart1 -> hart0, own cache line)
// [2112..2175] consumed counter (hart0 -> hart1, own cache line)
#define SCP_BPANEL_SIZE (16 * 32 * sizeof(et_fp16_t)) // 1024 bytes
#define SCP_READY_OFF (2 * SCP_BPANEL_SIZE) // 2048
#define SCP_CONSUMED_OFF (SCP_READY_OFF + 64) // 2112
#define SCP_PER_MINION (SCP_CONSUMED_OFF + 64) // 2176
// Signal a counter value to the other hart via L2 SCP.
static inline void __attribute__((always_inline)) scp_signal(volatile uint32_t * flag, uint32_t value) {
*flag = value;
FENCE;
evict_to_l2((const void *) flag, 1, 64);
WAIT_CACHEOPS;
}
// Wait for a counter in L2 SCP to reach the expected value.
static inline void __attribute__((always_inline)) scp_wait(volatile uint32_t * flag, uint32_t expected) {
while (1) {
evict_to_l2((const void *) flag, 1, 64);
WAIT_CACHEOPS;
if (*flag >= expected) {
return;
}
}
}
/**
* Build the interleaved B panel that TensorFMA16A32 expects (vectorized).
*
* Output: 16 lines x 32 fp16 = 1024 bytes, 64-byte aligned.
* out[l][j*2+0] = src0[mb + j][kb + 2*l]
* out[l][j*2+1] = src0[mb + j][kb + 2*l + 1]
*
* Uses fsch.ps scatter store: load 8 pairs per row, scatter to 8 output lines.
*/
static inline void __attribute__((always_inline)) pack_b_interleaved(et_fp16_t * out,
const char * src0_batch,
int64_t mb,
int64_t kb,
int64_t nb1_0) {
static const int32_t __attribute__((aligned(32))) scatter_idx[8] = { 0, 64, 128, 192, 256, 320, 384, 448 };
unsigned long old_mask;
__asm__ volatile(
"mova.x.m %[ms] \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
"flw.ps f1, 0(%[idx]) \n\t"
: [ms] "=&r"(old_mask)
: [idx] "r"(scatter_idx)
: "f1");
for (int j = 0; j < TILE_M; ++j) {
const et_fp16_t * row = (const et_fp16_t *) (src0_batch + (mb + j) * nb1_0) + kb;
char * dst = (char *) out + j * 4;
__asm__ volatile(
"flw.ps f2, 0(%[src]) \n\t"
"flw.ps f3, 32(%[src]) \n\t"
"fscw.ps f2, f1(%[d0]) \n\t"
"fscw.ps f3, f1(%[d1]) \n\t"
:
: [src] "r"(row), [d0] "r"(dst), [d1] "r"(dst + 512)
: "f2", "f3", "memory");
}
__asm__ volatile("mova.m.x %[ms] \n\t" : : [ms] "r"(old_mask));
}
int entry_point(struct ggml_et_binary_params * params, void * env) {
(void) env;
uint64_t hart_id = get_hart_id();
uint64_t shire_id = get_shire_id();
if (shire_id >= NUM_COMPUTE_SHIRES) {
return 0;
}
const int is_hart1 = hart_id & 1;
uint64_t local_minion = (hart_id >> 1) & 0x1F;
// Dimensions (both harts need these for tile assignment)
const int64_t K = params->src0.ne[0];
const int64_t M = params->src0.ne[1];
const int64_t N = params->src1.ne[1];
const int64_t ne2_0 = params->src0.ne[2], ne3_0 = params->src0.ne[3];
const int64_t ne2_1 = params->src1.ne[2], ne3_1 = params->src1.ne[3];
const int64_t nb1_0 = params->src0.nb[1];
const int64_t nb2_0 = params->src0.nb[2], nb3_0 = params->src0.nb[3];
const int64_t nb1_1 = params->src1.nb[1];
const int64_t nb2_1 = params->src1.nb[2], nb3_1 = params->src1.nb[3];
const int64_t nb1_d = params->dst.nb[1];
const int64_t nb2_d = params->dst.nb[2], nb3_d = params->dst.nb[3];
const char * src0_base = (const char *) params->src0.data;
const char * src1_base = (const char *) params->src1.data;
char * dst_base = (char *) params->dst.data;
if ((M % TILE_M) != 0) {
return 0;
}
if ((K % TILE_K) != 0) {
return 0;
}
const int64_t m_tiles = M / TILE_M;
const int64_t n_tiles = (N + TILE_N - 1) / TILE_N;
const int64_t batch_count = ne2_1 * ne3_1;
const int64_t base_tiles = m_tiles * n_tiles * batch_count;
const int64_t r2 = ne2_1 / ne2_0;
const int64_t r3 = ne3_1 / ne3_0;
const int64_t total_harts = NUM_COMPUTE_SHIRES * MINIONS_PER_SHIRE;
const int64_t k_steps = K / TILE_K;
int64_t k_splits = 1;
if (base_tiles < total_harts) {
k_splits = (total_harts + base_tiles - 1) / base_tiles;
int64_t ks = 1;
while (ks * 2 <= k_splits && ks * 2 <= 32 && k_steps % (ks * 2) == 0) {
ks *= 2;
}
k_splits = ks;
}
const int64_t tiles_per_shire = MINIONS_PER_SHIRE / k_splits;
const int64_t k_split = local_minion % k_splits;
const int64_t local_tile_idx = local_minion / k_splits;
const int64_t tiles_stride = (int64_t) NUM_COMPUTE_SHIRES * tiles_per_shire;
const int64_t k_steps_per_split = k_steps / k_splits;
const int64_t k_start = k_split * k_steps_per_split * TILE_K;
const int64_t k_end = k_start + k_steps_per_split * TILE_K;
// L2 SCP pointers for this minion's double-buffered panels + sync
uint64_t scp_base = local_minion * SCP_PER_MINION;
et_fp16_t * scp_bp[2] = {
(et_fp16_t *) et_shire_l2scp_local(scp_base),
(et_fp16_t *) et_shire_l2scp_local(scp_base + SCP_BPANEL_SIZE),
};
volatile uint32_t * ready_ctr = (volatile uint32_t *) et_shire_l2scp_local(scp_base + SCP_READY_OFF);
volatile uint32_t * consumed_ctr = (volatile uint32_t *) et_shire_l2scp_local(scp_base + SCP_CONSUMED_OFF);
// ================================================================
// Hart 1: B-panel packer
// ================================================================
if (is_hart1) {
// Initialize sync counters
scp_signal(ready_ctr, 0);
scp_signal(consumed_ctr, 0);
uint32_t chunk_id = 0;
for (int64_t tile = (int64_t) shire_id + local_tile_idx * NUM_COMPUTE_SHIRES; tile < base_tiles;
tile += tiles_stride) {
const int64_t tiles_per_batch = m_tiles * n_tiles;
const int64_t batch_idx = tile / tiles_per_batch;
const int64_t tile_in_batch = tile % tiles_per_batch;
const int64_t mb_idx = tile_in_batch % m_tiles;
const int64_t i3 = batch_idx / ne2_1;
const int64_t i2 = batch_idx % ne2_1;
const int64_t i2_0 = i2 / r2;
const int64_t i3_0 = i3 / r3;
const char * src0_batch = src0_base + i3_0 * nb3_0 + i2_0 * nb2_0;
const int64_t mb = mb_idx * TILE_M;
for (int64_t kb = k_start; kb < k_end; kb += TILE_K) {
int buf = chunk_id & 1;
// Back-pressure: wait for hart 0 to finish with this buffer
if (chunk_id >= 2) {
scp_wait(consumed_ctr, chunk_id - 1);
}
pack_b_interleaved(scp_bp[buf], src0_batch, mb, kb, nb1_0);
FENCE;
flush_to_l2(scp_bp[buf], 16, 64);
WAIT_CACHEOPS;
chunk_id++;
scp_signal(ready_ctr, chunk_id);
}
}
FENCE;
return 0;
}
// ================================================================
// Hart 0: tensor engine compute
// ================================================================
uint64_t my_minion_id = get_minion_id();
const uint64_t group_base_global = my_minion_id - k_split;
setup_cache_scp();
#if CACHEOP_MAX > 0 || REP_RATE > 0
ucache_control(1, REP_RATE, CACHEOP_MAX);
#endif
CLEAR_TENSOR_ERROR;
// Evict any stale L1D copies of sync counters
evict_to_l2((const void *) ready_ctr, 1, 64);
WAIT_CACHEOPS;
evict_to_l2((const void *) consumed_ctr, 1, 64);
WAIT_CACHEOPS;
uint32_t chunk_id = 0;
for (int64_t tile = (int64_t) shire_id + local_tile_idx * NUM_COMPUTE_SHIRES; tile < base_tiles;
tile += tiles_stride) {
const int64_t tiles_per_batch = m_tiles * n_tiles;
const int64_t batch_idx = tile / tiles_per_batch;
const int64_t tile_in_batch = tile % tiles_per_batch;
const int64_t nb_idx = tile_in_batch / m_tiles;
const int64_t mb_idx = tile_in_batch % m_tiles;
const int64_t i3 = batch_idx / ne2_1;
const int64_t i2 = batch_idx % ne2_1;
const char * src1_batch = src1_base + i3 * nb3_1 + i2 * nb2_1;
char * dst_batch = dst_base + i3 * nb3_d + i2 * nb2_d;
const int64_t mb = mb_idx * TILE_M;
const int64_t nb = nb_idx * TILE_N;
const int64_t n_cur = (nb + TILE_N <= N) ? TILE_N : (N - nb);
// Set tensor_mask for partial N tiles
if (n_cur < TILE_N) {
uint64_t mask = (1ULL << n_cur) - 1;
__asm__ __volatile__("csrw 0x805, %0" : : "r"(mask));
}
for (int64_t kb = k_start; kb < k_end; kb += TILE_K) {
int buf = chunk_id & 1;
// Start loading A from DRAM (overlaps with waiting for hart 1)
tensor_load((n_cur < TILE_N), false, A_L1_START, TENSOR_LOAD_PLAIN, 0,
(uint64_t) (src1_batch + nb * nb1_1 + kb * (int64_t) sizeof(et_fp16_t)), 0, n_cur - 1,
(uint64_t) nb1_1, 0);
// Wait for hart 1 to finish packing this chunk
chunk_id++;
scp_wait(ready_ctr, chunk_id);
// Load B from L2 SCP (hart 1 already flushed it)
tensor_load(false, false, B_L1_START, TENSOR_LOAD_PLAIN, 0, (uint64_t) scp_bp[buf], 0, 15, 64, 1);
tensor_wait(TENSOR_LOAD_WAIT_0);
tensor_wait(TENSOR_LOAD_WAIT_1);
// TensorFMA16A32
tensor_fma((n_cur < TILE_N), 3, n_cur - 1, 15, 0, false, false, false, false, B_L1_START, A_L1_START,
TENSOR_FMA_OP_FP16, (kb == k_start));
tensor_wait(TENSOR_FMA_WAIT);
// Signal that this buffer is free for hart 1 to reuse
scp_signal(consumed_ctr, chunk_id);
}
// K-split ring reduce
if (k_splits > 1) {
const uint64_t num_regs = (uint64_t) n_cur * 2;
if (k_split > 0) {
tensor_reduce_recv(0, TENSOR_REDUCE_OP_FADD, num_regs, group_base_global + k_split - 1);
tensor_wait(TENSOR_REDUCE_WAIT);
}
if (k_split < k_splits - 1) {
tensor_reduce_send(0, num_regs, group_base_global + k_split + 1);
tensor_wait(TENSOR_REDUCE_WAIT);
}
}
// Store FP32 result tile
if (k_split == k_splits - 1) {
tensor_store(0, 0, 3, n_cur - 1, (uint64_t) (dst_batch + nb * nb1_d + mb * (int64_t) sizeof(float)), 0,
(uint64_t) nb1_d);
tensor_wait(TENSOR_STORE_WAIT);
}
}
FENCE;
return 0;
}
@@ -0,0 +1,137 @@
#include "block_ops.h"
#include "ggml_tensor.h"
#include "platform.h"
#include "quants.h"
#include <etsoc/common/utils.h>
#include <stdint.h>
#include <stdio.h>
int entry_point(struct ggml_et_binary_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env || params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
// Thread coordination
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0 || (thread_id & 1)) {
return 0; // Skip odd threads to avoid resource contention
}
int effective_thread_id = thread_id / 2;
int effective_num_threads = (num_threads + 1) / 2;
// Extract tensor references
struct ggml_tensor * src0 = &params->src0; // Weight matrix A (F32)
struct ggml_tensor * src1 = &params->src1; // Activation matrix B (F16/F32)
struct ggml_tensor * dst = &params->dst; // Output matrix C (F32)
// Generic non-matrix-engine path: F32 x (F16/F32) -> F32
if (src0->type != GGML_TYPE_F32 || (src1->type != GGML_TYPE_F16 && src1->type != GGML_TYPE_F32) ||
dst->type != GGML_TYPE_F32) {
return -1;
}
const float * src0_data = (const float *) src0->data;
float * dst_data = (float *) dst->data;
// Dimensions and Strides
const int64_t K = src0->ne[0];
const int64_t M = src0->ne[1];
const int64_t N = src1->ne[1];
const int64_t ne02 = src0->ne[2], ne03 = src0->ne[3];
const int64_t ne12 = src1->ne[2], ne13 = src1->ne[3];
const int64_t ne2 = dst->ne[2], ne3 = dst->ne[3];
const size_t nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
const size_t nb11 = src1->nb[1], nb12 = src1->nb[2], nb13 = src1->nb[3];
const size_t nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
// F32 specific block size and counts
const int block_size = QK_F32;
const int64_t K_blocks = K / block_size;
const int64_t K_remainder = K % block_size;
// Threading distribution
const uint64_t total_elements = M * N * ne2 * ne3;
const uint64_t per_thread = 16;
const uint64_t threads_stride = per_thread * effective_num_threads;
if (effective_thread_id * per_thread >= total_elements) {
return 0;
}
// Broadcasting support
const int64_t r2 = ne12 / ne02;
const int64_t r3 = ne13 / ne03;
for (uint64_t base_idx = effective_thread_id * per_thread; base_idx < total_elements; base_idx += threads_stride) {
for (uint64_t j = 0; j < per_thread; j++) {
const uint64_t idx = base_idx + j;
if (idx >= total_elements) {
break;
}
// Index decoding
const int64_t i3 = idx / (M * N * ne2);
const int64_t rem3 = idx % (M * N * ne2);
const int64_t i2 = rem3 / (M * N);
const int64_t rem2 = rem3 % (M * N);
const int64_t n = rem2 / M;
const int64_t m = rem2 % M;
const int64_t i03 = i3 / r3, i02 = i2 / r2;
const int64_t i13 = (ne13 > 1) ? i3 : 0, i12 = (ne12 > 1) ? i2 : 0;
float sum = 0.0f;
const float * f32_row = (const float *) ((const char *) src0_data + m * nb01 + i02 * nb02 + i03 * nb03);
if (src1->type == GGML_TYPE_F32) {
const float * src1_data = (const float *) src1->data;
for (int64_t kb = 0; kb < K_blocks; kb++) {
const float * b_col_ptr =
(const float *) ((const char *) src1_data + (kb * block_size) * sizeof(float) + n * nb11 +
i12 * nb12 + i13 * nb13);
sum += compute_block_dot_product_f32(&f32_row[kb * block_size], b_col_ptr);
}
if (K_remainder > 0) {
const int64_t offset = K_blocks * block_size;
const float * b_col_ptr = (const float *) ((const char *) src1_data + offset * sizeof(float) +
n * nb11 + i12 * nb12 + i13 * nb13);
sum += compute_block_dot_product_f32_partial(&f32_row[offset], b_col_ptr, K_remainder);
}
} else {
const uint16_t * src1_data = (const uint16_t *) src1->data;
for (int64_t kb = 0; kb < K_blocks; kb++) {
const uint16_t * b_col_ptr =
(const uint16_t *) ((const char *) src1_data + (kb * block_size) * sizeof(uint16_t) + n * nb11 +
i12 * nb12 + i13 * nb13);
sum += compute_block_dot_product_f32_f16_partial(&f32_row[kb * block_size], b_col_ptr, block_size);
}
if (K_remainder > 0) {
const int64_t offset = K_blocks * block_size;
const uint16_t * b_col_ptr =
(const uint16_t *) ((const char *) src1_data + offset * sizeof(uint16_t) + n * nb11 +
i12 * nb12 + i13 * nb13);
sum += compute_block_dot_product_f32_f16_partial(&f32_row[offset], b_col_ptr, K_remainder);
}
}
// Atomic store for output
volatile float * c_element =
(volatile float *) ((char *) dst_data + m * dst->nb[0] + n * nb1 + i2 * nb2 + i3 * nb3);
atomic_store_f32(c_element, sum);
}
}
return 0;
}
@@ -0,0 +1,155 @@
#include "ggml_tensor.h"
#include "platform.h"
#include "tensor.h"
#include <etsoc/common/utils.h>
#include <stdint.h>
/*
* F32 Matrix Multiply for ET-SoC-1 — TensorFMA32.
*
* K-parallel + interleaved tiles + ring reduce.
* No batched-K yet (needs investigation on hang).
* This is the last known working version.
*/
#define NUM_COMPUTE_SHIRES 32
#define MINIONS_PER_SHIRE 32
#define TILE_K 16
#define TILE_M 16
/* ── Tuning knobs ───────────────────────────────────────────────────── */
#define TILE_N 16
#define CACHEOP_MAX 0
#define REP_RATE 0
/* ─────────────────────────────────────────────────────────────────── */
int entry_point(struct ggml_et_binary_params * params, void * env) {
uint64_t hart_id = get_hart_id();
uint64_t shire_id = get_shire_id();
if (shire_id >= NUM_COMPUTE_SHIRES) {
return 0;
}
if (hart_id & 1) {
return 0;
}
uint64_t local_minion = (hart_id >> 1) & 0x1F;
uint64_t my_minion_id = get_minion_id();
const int64_t K = params->src0.ne[0];
const int64_t M = params->src0.ne[1];
const int64_t N = params->src1.ne[1];
const int64_t ne2_0 = params->src0.ne[2], ne3_0 = params->src0.ne[3];
const int64_t ne2_1 = params->src1.ne[2], ne3_1 = params->src1.ne[3];
const int64_t nb1_0 = params->src0.nb[1];
const int64_t nb2_0 = params->src0.nb[2], nb3_0 = params->src0.nb[3];
const int64_t nb1_1 = params->src1.nb[1];
const int64_t nb2_1 = params->src1.nb[2], nb3_1 = params->src1.nb[3];
const int64_t nb1_d = params->dst.nb[1];
const int64_t nb2_d = params->dst.nb[2], nb3_d = params->dst.nb[3];
const char * src0_base = (const char *) params->src0.data;
const char * src1_base = (const char *) params->src1.data;
char * dst_base = (char *) params->dst.data;
setup_cache_scp();
#if CACHEOP_MAX > 0 || REP_RATE > 0
ucache_control(1, REP_RATE, CACHEOP_MAX);
#endif
CLEAR_TENSOR_ERROR;
const int64_t m_tiles = M / TILE_M;
const int64_t n_tiles = (N + TILE_N - 1) / TILE_N;
const int64_t batch_count = ne2_1 * ne3_1;
const int64_t base_tiles = m_tiles * n_tiles * batch_count;
const int64_t r2 = ne2_1 / ne2_0;
const int64_t r3 = ne3_1 / ne3_0;
const int64_t total_harts = NUM_COMPUTE_SHIRES * MINIONS_PER_SHIRE;
const int64_t k_steps = K / TILE_K;
int64_t k_splits = 1;
if (base_tiles < total_harts) {
k_splits = (total_harts + base_tiles - 1) / base_tiles;
int64_t ks = 1;
while (ks * 2 <= k_splits && ks * 2 <= 32 && k_steps % (ks * 2) == 0) {
ks *= 2;
}
k_splits = ks;
}
const int64_t tiles_per_shire = MINIONS_PER_SHIRE / k_splits;
const int64_t k_split = local_minion % k_splits;
const int64_t local_tile_idx = local_minion / k_splits;
const int64_t tiles_stride = (int64_t) NUM_COMPUTE_SHIRES * tiles_per_shire;
const int64_t k_steps_per_split = k_steps / k_splits;
const int64_t k_start = k_split * k_steps_per_split * TILE_K;
const int64_t k_end = k_start + k_steps_per_split * TILE_K;
const uint64_t group_base_global = my_minion_id - k_split;
for (int64_t tile = (int64_t) shire_id + local_tile_idx * NUM_COMPUTE_SHIRES; tile < base_tiles;
tile += tiles_stride) {
const int64_t tiles_per_batch = m_tiles * n_tiles;
const int64_t batch_idx = tile / tiles_per_batch;
const int64_t tile_in_batch = tile % tiles_per_batch;
const int64_t nb_idx = tile_in_batch / m_tiles;
const int64_t mb_idx = tile_in_batch % m_tiles;
const int64_t i3 = batch_idx / ne2_1;
const int64_t i2 = batch_idx % ne2_1;
const int64_t i2_0 = i2 / r2;
const int64_t i3_0 = i3 / r3;
const char * src0_batch = src0_base + i3_0 * nb3_0 + i2_0 * nb2_0;
const char * src1_batch = src1_base + i3 * nb3_1 + i2 * nb2_1;
char * dst_batch = dst_base + i3 * nb3_d + i2 * nb2_d;
const int64_t mb = mb_idx * TILE_M;
const int64_t nb = nb_idx * TILE_N;
const int64_t n_cur = (nb + TILE_N <= N) ? TILE_N : (N - nb);
for (int64_t kb = k_start; kb < k_end; kb += TILE_K) {
tensor_load(false, false, 0, 0, 0, (uint64_t) (src1_batch + nb * nb1_1 + kb * sizeof(float)), 0, n_cur - 1,
(uint64_t) nb1_1, 0);
tensor_load(false, false, TILE_K, 7, 0, (uint64_t) (src0_batch + mb * nb1_0 + kb * sizeof(float)), 0,
TILE_K - 1, (uint64_t) nb1_0, 1);
tensor_wait(TENSOR_LOAD_WAIT_0);
tensor_wait(TENSOR_LOAD_WAIT_1);
tensor_fma(false, 3, n_cur - 1, TILE_K - 1, 0, false, false, false, false, TILE_K, 0, 0, (kb == k_start));
tensor_wait(TENSOR_FMA_WAIT);
}
if (k_splits > 1) {
const uint64_t num_regs = (uint64_t) n_cur * 2;
if (k_split > 0) {
tensor_reduce_recv(0, TENSOR_REDUCE_OP_FADD, num_regs, group_base_global + k_split - 1);
tensor_wait(TENSOR_REDUCE_WAIT);
}
if (k_split < k_splits - 1) {
tensor_reduce_send(0, num_regs, group_base_global + k_split + 1);
tensor_wait(TENSOR_REDUCE_WAIT);
}
}
if (k_split == k_splits - 1) {
tensor_store(0, 0, 3, n_cur - 1, (uint64_t) (dst_batch + nb * nb1_d + mb * sizeof(float)), 0,
(uint64_t) nb1_d);
tensor_wait(TENSOR_STORE_WAIT);
}
}
FENCE;
return 0;
}
@@ -0,0 +1,169 @@
//******************************************************************************
// MUL_MAT_ID kernel specialized for Q4_0 weights (Mixture of Experts).
//
// C[m, s, b] = Sum(k=0..K-1) A[k, m, ids[s,b]] * B[k, s % ne11, b]
// A: Q4_0 [K, M, n_expert] weights
// B: F32 [K, n_cols, batch] activations
// ids: I32 [n_expert_used, batch]
// C: F32 [M, n_expert_used, batch]
//
// Strategy: All harts active. Flat m-major output partition allows amortized
// expert lookups and 2-row x2 dot products.
//******************************************************************************
#include "block_ops.h"
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include "quants.h"
#include <stdint.h>
int entry_point(struct ggml_et_mul_mat_id_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env || !params) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * src1 = &params->src1;
struct ggml_tensor * src2 = &params->src2;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_Q4_0 || src1->type != GGML_TYPE_F32 || src2->type != GGML_TYPE_I32 ||
dst->type != GGML_TYPE_F32) {
return -1;
}
const void * src0_data = src0->data;
const float * src1_data = (const float *) src1->data;
const int32_t * src2_data = (const int32_t *) src2->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !src1_data || !src2_data || !dst_data) {
return -1;
}
const int64_t K = src0->ne[0];
const int64_t M = src0->ne[1];
const int64_t n_expert = src0->ne[2];
const int64_t n_expert_used = src2->ne[0];
const int64_t batch = src2->ne[1];
const int64_t ne11 = src1->ne[1];
if (K % QK4_0 != 0) {
return -1;
}
const size_t nb01 = src0->nb[1]; // Q4_0 row stride
const size_t nb02 = src0->nb[2]; // expert stride
const size_t nb11 = src1->nb[1]; // activation column stride
const size_t nb12 = src1->nb[2]; // activation batch stride
const size_t nb20 = src2->nb[0];
const size_t nb21 = src2->nb[1];
const size_t nbd0 = dst->nb[0];
const size_t nbd1 = dst->nb[1];
const size_t nbd2 = dst->nb[2];
if (src0->nb[0] != sizeof(block_q4_0) || src1->nb[0] != sizeof(float) || src2->nb[0] != sizeof(int32_t) ||
nbd0 != sizeof(float)) {
return -1;
}
const int64_t K_blocks = K / QK4_0;
const int use_x2 = ((nb01 & 31) == 0);
const uint64_t total_outputs = (uint64_t) M * (uint64_t) n_expert_used * (uint64_t) batch;
if (total_outputs == 0) {
return 0;
}
// Even partition: hart h owns outputs [h*chunk, (h+1)*chunk).
const uint64_t chunk = (total_outputs + (uint64_t) num_threads - 1) / (uint64_t) num_threads;
const uint64_t my_start = (uint64_t) thread_id * chunk;
if (my_start >= total_outputs) {
return 0;
}
uint64_t my_end = my_start + chunk;
if (my_end > total_outputs) {
my_end = total_outputs;
}
// Save mask register once; full lanes for vector dot.
q4_dot_state q4_state;
q4_dot_begin(&q4_state);
const uint64_t per_batch = (uint64_t) M * (uint64_t) n_expert_used;
uint64_t idx = my_start;
while (idx < my_end) {
// Decode (m, slot, batch) from the m-major linear index.
const int64_t batch_idx = (int64_t) (idx / per_batch);
const uint64_t rem = idx - (uint64_t) batch_idx * per_batch;
const int64_t slot_idx = (int64_t) (rem / (uint64_t) M);
const int64_t m0 = (int64_t) (rem - (uint64_t) slot_idx * (uint64_t) M);
// How many outputs left in this (slot, batch) run AND in my range.
const uint64_t run_end_global =
(uint64_t) batch_idx * per_batch + (uint64_t) slot_idx * (uint64_t) M + (uint64_t) M;
const uint64_t end_in_my = (run_end_global < my_end) ? run_end_global : my_end;
int64_t run_len = (int64_t) (end_in_my - idx);
// Resolve expert + B column + dst slot for this run.
const int32_t expert_id =
*(const int32_t *) ((const char *) src2_data + slot_idx * (int64_t) nb20 + batch_idx * (int64_t) nb21);
char * dst_slot = (char *) dst_data + slot_idx * (int64_t) nbd1 + batch_idx * (int64_t) nbd2;
if (expert_id < 0 || expert_id >= n_expert) {
// Invalid expert id — zero out this run's outputs.
int64_t m = m0;
for (int64_t i = 0; i < run_len; i++, m++) {
atomic_store_f32((volatile float *) (dst_slot + m * (int64_t) nbd0), 0.0f);
}
idx += (uint64_t) run_len;
continue;
}
const int64_t col_idx = slot_idx % ne11;
const float * b_col_base =
(const float *) ((const char *) src1_data + col_idx * (int64_t) nb11 + batch_idx * (int64_t) nb12);
const char * expert_base = (const char *) src0_data + expert_id * (int64_t) nb02;
int64_t m = m0;
int64_t left = run_len;
// Paired-row dots: halves B bandwidth for runs >= 2.
if (use_x2) {
while (left >= 2) {
const block_q4_0 * row0 = (const block_q4_0 *) (expert_base + m * (int64_t) nb01);
const block_q4_0 * row1 = (const block_q4_0 *) (expert_base + (m + 1) * (int64_t) nb01);
float s0, s1;
q4_dot_compute_x2_aligned(row0, row1, b_col_base, K_blocks, &s0, &s1);
atomic_store_f32((volatile float *) (dst_slot + m * (int64_t) nbd0), s0);
atomic_store_f32((volatile float *) (dst_slot + (m + 1) * (int64_t) nbd0), s1);
m += 2;
left -= 2;
}
}
// Tail / non-aligned fallback: single-row dots.
while (left > 0) {
const block_q4_0 * row = (const block_q4_0 *) (expert_base + m * (int64_t) nb01);
float s = q4_dot_compute(row, b_col_base, K_blocks);
atomic_store_f32((volatile float *) (dst_slot + m * (int64_t) nbd0), s);
m++;
left--;
}
idx += (uint64_t) run_len;
}
q4_dot_end(&q4_state);
return 0;
}
@@ -0,0 +1,160 @@
//******************************************************************************
// MUL_MAT_ID kernel specialized for Q8_0 weights (Mixture of Experts).
//
// C[m, s, b] = Sum(k=0..K-1) A[k, m, ids[s,b]] * B[k, s % ne11, b]
// A: Q8_0 [K, M, n_expert] weights
// B: F32 [K, n_cols, batch] activations
// ids: I32 [n_expert_used, batch]
// C: F32 [M, n_expert_used, batch]
//
// Strategy mirrors mul_mat_id_Q4_0.c.
//******************************************************************************
#include "block_ops.h"
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include "quants.h"
#include <stdint.h>
int entry_point(struct ggml_et_mul_mat_id_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env || !params) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * src1 = &params->src1;
struct ggml_tensor * src2 = &params->src2;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_Q8_0 || src1->type != GGML_TYPE_F32 || src2->type != GGML_TYPE_I32 ||
dst->type != GGML_TYPE_F32) {
return -1;
}
const void * src0_data = src0->data;
const float * src1_data = (const float *) src1->data;
const int32_t * src2_data = (const int32_t *) src2->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !src1_data || !src2_data || !dst_data) {
return -1;
}
const int64_t K = src0->ne[0];
const int64_t M = src0->ne[1];
const int64_t n_expert = src0->ne[2];
const int64_t n_expert_used = src2->ne[0];
const int64_t batch = src2->ne[1];
const int64_t ne11 = src1->ne[1];
if (K % QK8_0 != 0) {
return -1;
}
const size_t nb01 = src0->nb[1];
const size_t nb02 = src0->nb[2];
const size_t nb11 = src1->nb[1];
const size_t nb12 = src1->nb[2];
const size_t nb20 = src2->nb[0];
const size_t nb21 = src2->nb[1];
const size_t nbd0 = dst->nb[0];
const size_t nbd1 = dst->nb[1];
const size_t nbd2 = dst->nb[2];
if (src0->nb[0] != sizeof(block_q8_0) || src1->nb[0] != sizeof(float) || src2->nb[0] != sizeof(int32_t) ||
nbd0 != sizeof(float)) {
return -1;
}
const int64_t K_blocks = K / QK8_0;
const int use_x2 = ((nb01 & 31) == 0);
const uint64_t total_outputs = (uint64_t) M * (uint64_t) n_expert_used * (uint64_t) batch;
if (total_outputs == 0) {
return 0;
}
const uint64_t chunk = (total_outputs + (uint64_t) num_threads - 1) / (uint64_t) num_threads;
const uint64_t my_start = (uint64_t) thread_id * chunk;
if (my_start >= total_outputs) {
return 0;
}
uint64_t my_end = my_start + chunk;
if (my_end > total_outputs) {
my_end = total_outputs;
}
q8_dot_state q8_state;
q8_dot_begin(&q8_state);
const uint64_t per_batch = (uint64_t) M * (uint64_t) n_expert_used;
uint64_t idx = my_start;
while (idx < my_end) {
const int64_t batch_idx = (int64_t) (idx / per_batch);
const uint64_t rem = idx - (uint64_t) batch_idx * per_batch;
const int64_t slot_idx = (int64_t) (rem / (uint64_t) M);
const int64_t m0 = (int64_t) (rem - (uint64_t) slot_idx * (uint64_t) M);
const uint64_t run_end_global =
(uint64_t) batch_idx * per_batch + (uint64_t) slot_idx * (uint64_t) M + (uint64_t) M;
const uint64_t end_in_my = (run_end_global < my_end) ? run_end_global : my_end;
int64_t run_len = (int64_t) (end_in_my - idx);
const int32_t expert_id =
*(const int32_t *) ((const char *) src2_data + slot_idx * (int64_t) nb20 + batch_idx * (int64_t) nb21);
char * dst_slot = (char *) dst_data + slot_idx * (int64_t) nbd1 + batch_idx * (int64_t) nbd2;
if (expert_id < 0 || expert_id >= n_expert) {
int64_t m = m0;
for (int64_t i = 0; i < run_len; i++, m++) {
atomic_store_f32((volatile float *) (dst_slot + m * (int64_t) nbd0), 0.0f);
}
idx += (uint64_t) run_len;
continue;
}
const int64_t col_idx = slot_idx % ne11;
const float * b_col_base =
(const float *) ((const char *) src1_data + col_idx * (int64_t) nb11 + batch_idx * (int64_t) nb12);
const char * expert_base = (const char *) src0_data + expert_id * (int64_t) nb02;
int64_t m = m0;
int64_t left = run_len;
if (use_x2) {
while (left >= 2) {
const block_q8_0 * row0 = (const block_q8_0 *) (expert_base + m * (int64_t) nb01);
const block_q8_0 * row1 = (const block_q8_0 *) (expert_base + (m + 1) * (int64_t) nb01);
float s0, s1;
q8_dot_compute_x2_aligned(row0, row1, b_col_base, K_blocks, &s0, &s1);
atomic_store_f32((volatile float *) (dst_slot + m * (int64_t) nbd0), s0);
atomic_store_f32((volatile float *) (dst_slot + (m + 1) * (int64_t) nbd0), s1);
m += 2;
left -= 2;
}
}
while (left > 0) {
const block_q8_0 * row = (const block_q8_0 *) (expert_base + m * (int64_t) nb01);
float s = q8_dot_compute(row, b_col_base, K_blocks);
atomic_store_f32((volatile float *) (dst_slot + m * (int64_t) nbd0), s);
m++;
left--;
}
idx += (uint64_t) run_len;
}
q8_dot_end(&q8_state);
return 0;
}
@@ -0,0 +1,288 @@
//******************************************************************************
// Bare Metal MUL_MAT_ID Kernel (Mixture of Experts)
//
// ALGORITHM:
// MUL_MAT_ID performs batched matrix multiplication with expert routing.
// Each output element selects which expert matrix to use based on an index tensor.
//
// INPUTS:
// src0 (as): Expert weight matrices [K, M, n_expert]
// - Stack of n_expert matrices, each of size [K, M]
// src1 (b): Activation vectors [K, n_cols, batch]
// - n_cols can be 1 (broadcast) or n_expert_used (per-expert inputs)
// src2 (ids): Expert selection indices [n_expert_used, batch] (int32)
// - For each (slot, batch), specifies which expert from src0 to use
//
// OUTPUT:
// dst: Result [M, n_expert_used, batch, 1]
//
// COMPUTATION:
// For each output position (m, slot, batch):
// expert_id = ids[slot, batch] // Which expert to use (0..n_expert-1)
// col_idx = slot % src1.ne[1] // Which column in src1 (handles broadcasting)
// dst[m, slot, batch] = dot_product(
// src0[0:K, m, expert_id], // Row m from selected expert matrix
// src1[0:K, col_idx, batch] // Column from activations (may broadcast)
// )
//
// BROADCASTING:
// - When src1.ne[1] == 1: All expert slots use the same activation column
// - When src1.ne[1] == n_expert_used: Each slot has its own activation column
// - General case: col_idx = slot % src1.ne[1] (modulo handles both cases)
//
// MATH NOTATION:
// C[m, s, b] = Sum(k=0 to K-1) A[k, m, ids[s,b]] x B[k, s % ne11, b]
// where:
// m: [0, M) - output feature index
// s: [0, n_expert_used) - expert slot index
// b: [0, batch) - batch index
// k: [0, K) - hidden dimension
// ne11 = src1->ne[1] - number of columns in src1
//******************************************************************************
#include "block_ops.h"
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include "quants.h"
#include <stdint.h>
// Main entry point for MUL_MAT_ID kernel (Mixture of Experts)
int entry_point(struct ggml_et_mul_mat_id_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
// Get thread coordination info
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return -1;
}
// Use even threads only to avoid resource contention
// Each minion has 2 threads sharing instruction/data cache, NOC to RAM, and FPU
// Odd threads return immediately to avoid fighting for shared resources
if (thread_id & 1) {
return 0; // Odd thread - skip work
}
// Adjust thread count and ID for even-only threading
int effective_thread_id = thread_id / 2;
int effective_num_threads = (num_threads + 1) / 2; // Ceiling division
// Validate params
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
// Extract tensor references
struct ggml_tensor * src0 = &params->src0; // Expert weight matrices [K, M, n_expert]
struct ggml_tensor * src1 = &params->src1; // Activations [K, n_expert_used, batch]
struct ggml_tensor * src2 = &params->src2; // Expert indices [n_expert_used, batch] (I32)
struct ggml_tensor * dst = &params->dst; // Output [M, n_expert_used, batch, 1]
// Validate tensor types
if (src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32 || src2->type != GGML_TYPE_I32) {
return -1;
}
// Get data pointers
const void * src0_data = src0->data; // Expert matrices (Q8_0/F16/F32)
const float * src1_data = (const float *) src1->data; // Activations (F32)
const int32_t * src2_data = (const int32_t *) src2->data; // Expert IDs (I32)
float * dst_data = (float *) dst->data; // Output (F32)
if (!src0_data || !src1_data || !src2_data || !dst_data) {
return -1;
}
// Determine block size based on src0 type
int block_size;
switch (src0->type) {
case GGML_TYPE_Q8_0:
block_size = QK8_0;
break;
case GGML_TYPE_Q4_0:
block_size = QK4_0;
break;
case GGML_TYPE_F16:
block_size = QK_F16;
break;
case GGML_TYPE_F32:
block_size = QK_F32;
break;
default:
return -1;
}
// Get dimensions
// src0: [K, M, n_expert] - expert weight matrices
// src1: [K, n_expert_used, batch] - activations
// src2: [n_expert_used, batch] - expert indices
// dst: [M, n_expert_used, batch, 1] - output
const int64_t K = src0->ne[0]; // Hidden dimension
const int64_t M = src0->ne[1]; // Output features
const int64_t n_expert = src0->ne[2]; // Number of experts
const int64_t n_expert_used = src2->ne[0]; // Experts used per token
const int64_t batch = src2->ne[1]; // Batch size
// Strides (in bytes)
const size_t nb01 = src0->nb[1]; // src0 row stride
const size_t nb02 = src0->nb[2]; // src0 expert stride
const size_t nb11 = src1->nb[1]; // src1 column stride
const size_t nb12 = src1->nb[2]; // src1 batch stride
const size_t nb20 = src2->nb[0]; // src2 element stride
const size_t nb21 = src2->nb[1]; // src2 batch stride
const size_t nb1 = dst->nb[1]; // dst column stride
const size_t nb2 = dst->nb[2]; // dst batch stride
// Verify K dimension alignment for quantization
// Q8_0 requires strict alignment (quantized data must be block-aligned)
// F32 and F16 can handle partial blocks with scalar remainders
if ((src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_Q4_0) && K % block_size != 0) {
return -1; // Q8_0 requires K to be multiple of block_size
}
// Verify first dimension is contiguous
size_t expected_element_size_src0;
if (src0->type == GGML_TYPE_Q8_0) {
expected_element_size_src0 = sizeof(block_q8_0);
} else if (src0->type == GGML_TYPE_Q4_0) {
expected_element_size_src0 = sizeof(block_q4_0);
} else if (src0->type == GGML_TYPE_F16) {
expected_element_size_src0 = sizeof(uint16_t);
} else if (src0->type == GGML_TYPE_F32) {
expected_element_size_src0 = sizeof(float);
} else {
return -1;
}
if (src0->nb[0] != expected_element_size_src0 || src1->nb[0] != sizeof(float) || src2->nb[0] != sizeof(int32_t) ||
dst->nb[0] != sizeof(float)) {
return -1;
}
const int64_t K_blocks = K / block_size;
// Threading: distribute output elements across threads
// Total output elements = M * n_expert_used * batch
const uint64_t total_elements = M * n_expert_used * batch;
const uint64_t per_thread = 16;
const uint64_t threads_stride = per_thread * effective_num_threads;
if (effective_thread_id * per_thread >= total_elements) {
return 0;
}
// Process elements assigned to this thread
for (uint64_t base_idx = effective_thread_id * per_thread; base_idx < total_elements; base_idx += threads_stride) {
for (uint64_t j = 0; j < per_thread; j++) {
const uint64_t idx = base_idx + j;
if (idx >= total_elements) {
break;
}
// Decode linear index to (m, n_idx, batch_idx)
// Layout: m + M * (n_idx + n_expert_used * batch_idx)
const int64_t batch_idx = idx / (M * n_expert_used);
const int64_t rem = idx % (M * n_expert_used);
const int64_t n_idx = rem / M;
const int64_t m = rem % M;
// Get expert ID from src2[n_idx, batch_idx]
const int32_t expert_id = *(const int32_t *) ((const char *) src2_data + n_idx * nb20 + batch_idx * nb21);
// Validate expert ID
if (expert_id < 0 || expert_id >= n_expert) {
// Invalid expert ID - write zero and continue
volatile float * dst_element =
(volatile float *) ((char *) dst_data + m * dst->nb[0] + n_idx * nb1 + batch_idx * nb2);
atomic_store_f32(dst_element, 0.0f);
continue;
}
// Compute dot product: expert_matrix[m, :] x activations[:, col_idx, batch_idx]
// Use modulo to handle broadcasting: when src1 has fewer columns than expert slots,
// multiple slots share the same activation column (col_idx = n_idx % src1->ne[1])
const int64_t col_idx = n_idx % src1->ne[1];
float sum = 0.0f;
// Type switch hoisted outside block loop: one branch per element, not per block
const char * expert_row_base = (const char *) src0_data + m * nb01 + expert_id * nb02;
switch (src0->type) {
case GGML_TYPE_Q8_0:
{
const block_q8_0 * q8_row = (const block_q8_0 *) expert_row_base;
const float * b_col_base =
(const float *) ((const char *) src1_data + col_idx * nb11 + batch_idx * nb12);
sum += compute_row_dot_q8_0(q8_row, b_col_base, K_blocks);
break;
}
case GGML_TYPE_Q4_0:
{
const block_q4_0 * q4_row = (const block_q4_0 *) expert_row_base;
const float * b_col_base =
(const float *) ((const char *) src1_data + col_idx * nb11 + batch_idx * nb12);
sum += compute_row_dot_q4_0(q4_row, b_col_base, K_blocks);
break;
}
case GGML_TYPE_F16:
{
const uint16_t * f16_row = (const uint16_t *) expert_row_base;
const int64_t K_remainder = K % block_size;
for (int64_t kb = 0; kb < K_blocks; kb++) {
const float * b_col_ptr =
(const float *) ((const char *) src1_data + (kb * block_size) * sizeof(float) +
col_idx * nb11 + batch_idx * nb12);
sum += compute_block_dot_product_f16_naive(&f16_row[kb * block_size], b_col_ptr);
}
if (K_remainder > 0) {
const int64_t offset = K_blocks * block_size;
const float * b_col_ptr =
(const float *) ((const char *) src1_data + offset * sizeof(float) + col_idx * nb11 +
batch_idx * nb12);
sum += compute_block_dot_product_f16_partial(&f16_row[offset], b_col_ptr, K_remainder);
}
break;
}
case GGML_TYPE_F32:
{
const float * f32_row = (const float *) expert_row_base;
const int64_t K_remainder = K % block_size;
for (int64_t kb = 0; kb < K_blocks; kb++) {
const float * b_col_ptr =
(const float *) ((const char *) src1_data + (kb * block_size) * sizeof(float) +
col_idx * nb11 + batch_idx * nb12);
sum += compute_block_dot_product_f32(&f32_row[kb * block_size], b_col_ptr);
}
if (K_remainder > 0) {
const int64_t offset = K_blocks * block_size;
const float * b_col_ptr =
(const float *) ((const char *) src1_data + offset * sizeof(float) + col_idx * nb11 +
batch_idx * nb12);
sum += compute_block_dot_product_f32_partial(&f32_row[offset], b_col_ptr, K_remainder);
}
break;
}
default:
return -1;
}
// Store result using atomic store to avoid cache coherency issues
// when multiple threads write to the same cache line (64 bytes = 16 floats)
volatile float * dst_element =
(volatile float *) ((char *) dst_data + m * dst->nb[0] + n_idx * nb1 + batch_idx * nb2);
atomic_store_f32(dst_element, sum);
}
}
return 0;
}
+328
View File
@@ -0,0 +1,328 @@
//******************************************************************************
// Norm F32 Kernel (Layer Normalization)
// y[i] = (x[i] - mean) / sqrt(variance + eps)
// where mean = sum(x) / N, variance = sum((x - mean)^2) / N
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>
// Norm kernel parameters structure
struct ggml_et_norm_params {
struct ggml_tensor src0; // F32 input tensor
struct ggml_tensor dst; // F32 output tensor
float eps; // Epsilon parameter for numerical stability
};
int entry_point(struct ggml_et_norm_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
float eps = params->eps;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1; // Unsupported type combination
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1; // Null data pointer
}
if (eps < 0.0f) {
return -1; // Invalid epsilon
}
const int64_t ne0 = dst->ne[0];
const int64_t ne1 = dst->ne[1];
const int64_t ne2 = dst->ne[2];
const int64_t ne3 = dst->ne[3];
const size_t nb0 = dst->nb[0], nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
const size_t nb00 = src0->nb[0], nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
if (src0->ne[0] != ne0 || src0->ne[1] != ne1 || src0->ne[2] != ne2 || src0->ne[3] != ne3) {
return -1; // Shape mismatch
}
const int32_t total_rows = (int32_t) (ne1 * ne2 * ne3);
const int shire_threads = SOC_MINIONS_PER_SHIRE * NUM_HARTS_PER_MINION;
if (total_rows >= shire_threads) {
// Row-parallel: each thread processes whole rows
for (int64_t i3 = 0; i3 < ne3; i3++) {
for (int64_t i2 = 0; i2 < ne2; i2++) {
for (int64_t i1 = thread_id; i1 < ne1; i1 += num_threads) {
const float * src_ptr =
(const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_ptr = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
// Step 1: sum for mean
float zero = 0.0f;
__asm__ volatile("fbc.ps f10, %[z]\n" : : [z] "m"(zero) : "f10");
for (int32_t i0 = 0; i0 < (int32_t) ne0; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fadd.ps f10, f10, f11\n"
:
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f10", "f11");
}
float sum;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(sum)::"t0", "f1", "f2", "f3", "f4", "f5");
const float mean = et_fdiv(sum, (float) (int32_t) ne0);
// Step 2: compute (x - mean) → dst, accumulate variance
__asm__ volatile("fbc.ps f10, %[z]\n" : : [z] "m"(zero) : "f10");
for (int32_t i0 = 0; i0 < (int32_t) ne0; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fbc.ps f12, %[mean_ptr]\n"
"fsub.ps f13, f11, f12\n"
"fsw.ps f13, %[result]\n"
"fmadd.ps f10, f13, f13, f10\n"
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0]), [mean_ptr] "m"(mean)
: "f10", "f11", "f12", "f13");
}
float var_sum;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(var_sum)::"t0", "f1", "f2", "f3", "f4", "f5");
const float variance = et_fdiv(var_sum, (float) (int32_t) ne0);
const float scale = et_powf(variance + eps, -0.5f);
if (!(scale > 0.0f)) {
return -1;
}
// Step 3: apply scale to centered values in dst
for (int32_t i0 = 0; i0 < (int32_t) ne0; i0 += 8) {
__asm__ volatile(
"flw.ps f12, %[y_vec]\n"
"fbc.ps f13, %[scale_ptr]\n"
"fmul.ps f14, f12, f13\n"
"fsw.ps f14, %[result]\n"
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [y_vec] "m"(*(const float (*)[8]) & dst_ptr[i0]), [scale_ptr] "m"(scale)
: "f12", "f13", "f14");
}
}
}
}
} else {
// Intra-row: threads within each shire cooperate via L2 SCP.
// Two reductions needed: sum (for mean), then variance sum.
int shire_tid = thread_id % shire_threads;
int threads_per_row = shire_threads / total_rows;
int my_row = shire_tid / threads_per_row;
int local_tid = shire_tid % threads_per_row;
int group_base = my_row * threads_per_row;
if (my_row >= total_rows) {
FENCE;
et_barrier(ET_BARRIER_SHIRE);
// Second barrier for variance exchange
FENCE;
et_barrier(ET_BARRIER_SHIRE);
return 0;
}
int64_t i1 = my_row % ne1;
int64_t i2 = (my_row / ne1) % ne2;
int64_t i3 = my_row / (ne1 * ne2);
const float * src_ptr = (const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_ptr = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
const int32_t elems_per_cl = 16;
int32_t total_cls = ((int32_t) ne0 + elems_per_cl - 1) / elems_per_cl;
int32_t cls_per_thread = (total_cls + threads_per_row - 1) / threads_per_row;
int32_t my_start = local_tid * cls_per_thread * elems_per_cl;
int32_t my_end = my_start + cls_per_thread * elems_per_cl;
if (my_end > (int32_t) ne0) {
my_end = (int32_t) ne0;
}
if (my_start >= (int32_t) ne0) {
my_start = 0;
my_end = 0;
}
int workers = threads_per_row < total_cls ? threads_per_row : total_cls;
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
// ---- Reduction 1: partial sum for mean ----
__asm__ volatile("fbci.pi f10, 0" ::: "f10");
for (int32_t i0 = my_start; i0 < my_end; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fadd.ps f10, f10, f11\n"
:
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f10", "f11");
}
float partial_sum;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(partial_sum)::"t0", "f1", "f2", "f3", "f4", "f5");
// L2SCP exchange for sum
volatile float * my_slot = (volatile float *) et_shire_l2scp_local((uint64_t) shire_tid * 64);
*my_slot = partial_sum;
FENCE;
evict_to_l2((const void *) my_slot, 1, 64);
WAIT_CACHEOPS;
et_barrier(ET_BARRIER_SHIRE);
// All threads read sum, compute mean
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
evict_to_l2((const void *) slot, 1, 64);
}
WAIT_CACHEOPS;
float total_sum = 0.0f;
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
total_sum += *slot;
}
const float mean = et_fdiv(total_sum, (float) (int32_t) ne0);
// ---- Reduction 2: compute (x - mean) → dst chunk, partial variance ----
__asm__ volatile("fbci.pi f10, 0" ::: "f10");
if (my_start < my_end) {
uint32_t mean_bits;
__asm__ volatile("fmv.x.s %0, %1" : "=r"(mean_bits) : "f"(mean));
__asm__ volatile("fbcx.ps f15, %[mb]\n" : : [mb] "r"(mean_bits) : "f15");
for (int32_t i0 = my_start; i0 < my_end; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fsub.ps f13, f11, f15\n"
"fsw.ps f13, %[result]\n"
"fmadd.ps f10, f13, f13, f10\n"
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f10", "f11", "f13");
}
}
float partial_var;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(partial_var)::"t0", "f1", "f2", "f3", "f4", "f5");
// L2SCP exchange for variance (reuse same slots)
*my_slot = partial_var;
FENCE;
evict_to_l2((const void *) my_slot, 1, 64);
WAIT_CACHEOPS;
et_barrier(ET_BARRIER_SHIRE);
// All threads read variance, compute scale, apply to own chunk
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
evict_to_l2((const void *) slot, 1, 64);
}
WAIT_CACHEOPS;
float total_var = 0.0f;
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
total_var += *slot;
}
const float variance = et_fdiv(total_var, (float) (int32_t) ne0);
const float scale = et_powf(variance + eps, -0.5f);
if (!(scale > 0.0f)) {
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
return -1;
}
// Apply scale to centered values (already in dst from reduction 2)
if (my_start < my_end) {
uint32_t scale_bits;
__asm__ volatile("fmv.x.s %0, %1" : "=r"(scale_bits) : "f"(scale));
__asm__ volatile("fbcx.ps f13, %[sb]\n" : : [sb] "r"(scale_bits) : "f13");
for (int32_t i0 = my_start; i0 < my_end; i0 += 8) {
__asm__ volatile(
"flw.ps f12, %[y_vec]\n"
"fmul.ps f14, f12, f13\n"
"fsw.ps f14, %[result]\n"
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [y_vec] "m"(*(const float (*)[8]) & dst_ptr[i0])
: "f12", "f14");
}
}
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
}
return 0;
}
+165
View File
@@ -0,0 +1,165 @@
//******************************************************************************
// Bare Metal PAD F32 Kernel
// Zero-pads an F32 tensor along dimensions 1-3.
//
// Constraints:
// - No dim0 padding (lp[0]==0, rp[0]==0)
// - dst contiguous
// - src nb[0] == 4 (dim0 contiguous for vectorized reads)
// - Zero-pad only (no circular mode)
//
// Two paths:
// Aligned (ne0 % 16 == 0): rows distributed across harts, vectorized.
// Small (16 % ne0 == 0): cache-line distributed, scalar per-element.
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_pad_params {
struct ggml_tensor src0;
struct ggml_tensor dst;
int32_t lp[4];
int32_t rp[4];
};
// Vectorized copy with scalar tail
static inline void vec_copy_f32(float * dst, const float * src, int32_t n) {
int32_t i = 0;
const int32_t vec_end = (n / 8) * 8;
for (; i < vec_end; i += 8) {
__asm__ volatile(
"flw.ps f10, %[s]\n"
"fsw.ps f10, %[d]\n"
: [d] "=m"(*(float (*)[8]) & dst[i])
: [s] "m"(*(const float (*)[8]) & src[i])
: "f10");
}
for (; i < n; i++) {
dst[i] = src[i];
}
}
int entry_point(struct ggml_et_pad_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
const float * src0_data = (const float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
// Dst dimensions
const int64_t ne0 = dst->ne[0];
const int64_t ne1 = dst->ne[1];
const int64_t ne2 = dst->ne[2];
const int64_t ne3 = dst->ne[3];
// Src strides (byte offsets)
const int64_t nb1_src = src0->nb[1];
const int64_t nb2_src = src0->nb[2];
const int64_t nb3_src = src0->nb[3];
// Padding values
const int32_t lp1 = params->lp[1];
const int32_t rp1 = params->rp[1];
const int32_t lp2 = params->lp[2];
const int32_t rp2 = params->rp[2];
const int32_t lp3 = params->lp[3];
const int32_t rp3 = params->rp[3];
const int64_t total_rows = ne1 * ne2 * ne3;
const int64_t total_elements = ne0 * total_rows;
if (total_elements == 0) {
return 0;
}
// Broadcast 0.0f to SIMD register for vectorized zero-fill
float zero = 0.0f;
__asm__ volatile("fbc.ps f12, %[v]\n" : : [v] "m"(zero) : "f12");
// Aligned: ne0 % 16 == 0 -> row-based distribution, vectorized
if (ne0 % 16 == 0) {
for (int64_t row = thread_id; row < total_rows; row += num_threads) {
const int64_t i3 = row / (ne1 * ne2);
const int64_t i2 = (row / ne1) % ne2;
const int64_t i1 = row % ne1;
float * dst_row = dst_data + row * ne0;
if (i1 >= lp1 && i1 < ne1 - rp1 && i2 >= lp2 && i2 < ne2 - rp2 && i3 >= lp3 && i3 < ne3 - rp3) {
const float * src_row = (const float *) ((const char *) src0_data + (i1 - lp1) * nb1_src +
(i2 - lp2) * nb2_src + (i3 - lp3) * nb3_src);
vec_copy_f32(dst_row, src_row, (int32_t) ne0);
} else {
int64_t i = 0;
const int64_t vec_end = (ne0 / 8) * 8;
for (; i < vec_end; i += 8) {
__asm__ volatile("fsw.ps f12, %[d]\n" : [d] "=m"(*(float (*)[8]) & dst_row[i])::"f12");
}
}
}
return 0;
}
// Small-ne0 path: 16 % ne0 == 0 -> cache-line distributed, scalar
const int64_t elems_per_cl = 16;
const int64_t total_cl = (total_elements + elems_per_cl - 1) / elems_per_cl;
const int64_t ne1_data_end = ne1 - rp1;
const int64_t ne2_data_end = ne2 - rp2;
const int64_t ne3_data_end = ne3 - rp3;
for (int64_t cl = thread_id; cl < total_cl; cl += num_threads) {
const int64_t elem_start = cl * elems_per_cl;
int64_t elem_end = elem_start + elems_per_cl;
if (elem_end > total_elements) {
elem_end = total_elements;
}
for (int64_t idx = elem_start; idx < elem_end; idx++) {
const int64_t i0 = idx % ne0;
const int64_t rem = idx / ne0;
const int64_t i1 = rem % ne1;
const int64_t rem2 = rem / ne1;
const int64_t i2 = rem2 % ne2;
const int64_t i3 = rem2 / ne2;
if (i1 >= lp1 && i1 < ne1_data_end && i2 >= lp2 && i2 < ne2_data_end && i3 >= lp3 && i3 < ne3_data_end) {
const float * sp = (const float *) ((const char *) src0_data + i0 * 4 + (i1 - lp1) * nb1_src +
(i2 - lp2) * nb2_src + (i3 - lp3) * nb3_src);
dst_data[idx] = *sp;
} else {
dst_data[idx] = 0.0f;
}
}
}
return 0;
}
+545
View File
@@ -0,0 +1,545 @@
//******************************************************************************
// ET Platform Hardware Abstraction Layer
// Provides thread coordination, kernel infrastructure, and platform primitives
// for bare metal ET kernels
//******************************************************************************
#ifndef PLATFORM_H
#define PLATFORM_H
#include "etsoc/common/utils.h"
#include "etsoc/isa/barriers.h"
#include "etsoc/isa/cacheops-umode.h"
#include "etsoc/isa/hart.h"
#include <stdint.h>
#define SOC_MINIONS_PER_SHIRE 32
#define NUM_HARTS_PER_MINION 2
#define ET_CACHE_LINE_SIZE_BYTES 64
// Environment structure definition
typedef struct {
uint32_t version; // Version of the ABI (offset 0)
uint32_t padding1; // Padding to align shire_mask to offset 8
uint64_t shire_mask; // Bitmask of active compute shires (offset 8)
uint32_t frequency; // Frequency of Minion cores in MHz (offset 16)
uint32_t padding2; // Padding to maintain alignment
} __attribute__((packed, aligned(64))) kernel_environment_t;
// Manual implementation of count trailing zeros for bare metal environment
// NOTE: This simple loop-based implementation is used for portability.
// Production implementations (like libgcc's __ctzdi2) use optimized bit manipulation
// algorithms with lookup tables and parallel bit operations for O(log n) performance.
static inline int manual_ctzll(uint64_t x) {
if (x == 0) return 64;
int count = 0;
while ((x & 1) == 0) {
x >>= 1;
count++;
}
return count;
}
// Manual implementation of population count for bare metal environment
// NOTE: This simple loop-based implementation is used for portability.
// Production implementations (like libgcc's __popcountdi2) use optimized bit-parallel
// algorithms with magic constants and bit manipulation tricks for O(1) performance.
static inline int manual_popcountll(uint64_t x) {
int count = 0;
while (x) {
count += x & 1;
x >>= 1;
}
return count;
}
// Binary GCD (Stein's algorithm) — avoids expensive 64-bit division/remainder.
// Uses only shifts, subtraction, and comparison (all single-cycle on ET cores).
static inline int64_t et_gcd_i64(int64_t a, int64_t b) {
while (b) {
const int64_t t = b;
b = a % b;
a = t;
}
return a;
}
// Return the number of consecutive rows of width row_elems needed so the
// combined write footprint spans an integer number of cache lines.
static inline int64_t et_rows_per_cacheline_group(int64_t row_elems, int64_t elem_size_bytes) {
if (row_elems <= 0 || elem_size_bytes <= 0) {
return 1;
}
const int64_t row_bytes = row_elems * elem_size_bytes;
const int64_t gcd = et_gcd_i64(ET_CACHE_LINE_SIZE_BYTES, row_bytes);
return ET_CACHE_LINE_SIZE_BYTES / gcd;
}
// Calculate relative thread ID from absolute hart ID using shire mask
// Returns -1 if this hart is not active (not in shire mask)
static inline int get_relative_thread_id(uint64_t shire_mask) {
int hart_id = (int) get_hart_id();
// Find starting hart offset from lowest active shire
int starting_hart = manual_ctzll(shire_mask) * SOC_MINIONS_PER_SHIRE * NUM_HARTS_PER_MINION;
// Return -1 if not an active thread
if (hart_id < starting_hart) {
return -1;
}
// Calculate relative thread ID
int thread_id = hart_id - starting_hart;
return thread_id;
}
// Calculate total number of threads from shire mask
static inline int get_num_threads(uint64_t shire_mask) {
// Count active shires using popcount, multiply by minions per shire and harts per minion
return manual_popcountll(shire_mask) * SOC_MINIONS_PER_SHIRE * NUM_HARTS_PER_MINION;
}
//******************************************************************************
// Synchronization Primitives
//******************************************************************************
#define NOP __asm__ __volatile__("nop\n");
#define FENCE __asm__ __volatile__("fence\n" ::: "memory");
#define WFI __asm__ __volatile__("wfi\n");
//******************************************************************************
// Atomic Operations
//******************************************************************************
// Global AMO primitives — ET custom 'g' suffix instructions that go through
// the NoC coherence fabric for chip-wide atomicity.
// Atomic swap (word), returns previous value.
static inline uint32_t __attribute__((always_inline)) et_global_swap_w(volatile void * addr, uint32_t val) {
uint32_t ret;
__asm__ __volatile__("amoswapg.w %0, %1, (%2)" : "=r"(ret) : "r"(val), "r"(addr) : "memory");
return ret;
}
// Atomic add (word), returns previous value.
static inline uint32_t __attribute__((always_inline)) et_global_add_w(volatile void * addr, uint32_t val) {
uint32_t ret;
__asm__ __volatile__("amoaddg.w %0, %1, (%2)" : "=r"(ret) : "r"(val), "r"(addr) : "memory");
return ret;
}
// Atomic store (halfword, global). Address must be 16-bit aligned.
static inline void __attribute__((always_inline)) et_global_store_hw(volatile void * addr, uint16_t val) {
__asm__ __volatile__("shg %0, (%1)" : : "r"(val), "r"(addr) : "memory");
}
// Convenience wrappers — float types, fire-and-forget (old value discarded).
static inline void atomic_store_f32(volatile float * addr, float value) {
et_global_swap_w(addr, *(uint32_t *) &value);
}
static inline void atomic_add_f32(volatile float * addr, float value) {
et_global_add_w(addr, *(uint32_t *) &value);
}
static inline void atomic_store_f16(volatile uint16_t * addr, uint16_t value) {
et_global_store_hw(addr, value);
}
//******************************************************************************
// Barrier Primitives
//
// Hardware resources used (per shire):
// - 32 FLBs: 8-bit atomic counters, non-blocking (CSR 0x820)
// - 2 FCCs per hart: credit counters, hardware-stall on consume (CSR 0x821)
//
// Convention:
// MINION barriers: FLB = local_minion_id (0-31), FCC 0
// SHIRE barriers: FLB 0, FCC 1
//
// MINION and SHIRE barriers MUST NOT be concurrent. All minion barriers
// must complete before a shire barrier, and vice versa. FLB 0 is shared
// between minion 0's barrier and the shire barrier — safe only because
// the FLB counter auto-resets on match.
//
// FCC 0 is safe for all 32 concurrent minion barriers because each
// barrier's fcc_send targets only its own minion (per-hart private
// counters, scoped by CREDINC mask). FCC 1 is reserved for shire-wide
// broadcast.
//******************************************************************************
#define ET_DEFAULT_SHIRE_MASK 0xFFFFFFFFULL
typedef enum {
ET_BARRIER_MINION, // sync both harts within each minion (FLB=minion_id, FCC 0)
ET_BARRIER_SHIRE, // sync all harts across the shire (FLB=0, FCC 1)
ET_BARRIER_GLOBAL, // sync all harts across all active shires (FLB+global AMO+FCC)
} et_barrier_scope_t;
//******************************************************************************
// Global Barrier (cross-shire)
//
// Synchronizes all harts across multiple shires on the chip.
// Algorithm:
// 1. FLB within each shire to elect one representative hart
// 2. Elected hart does a global atomic increment on a shared counter
// 3. The last shire to arrive resets the counter and sends FCC credits
// to all active shires to release them
// 4. All harts wait on FCC to complete the barrier
//
// Uses FLB 0, FCC 1 (same as ET_BARRIER_SHIRE, these must not overlap).
// The counter lives in a cache-line-aligned global to avoid coherency problems
//******************************************************************************
// Barrier counter cache-line aligned to avoid coherency problems
// Must be zero-initialized (BSS).
static uint32_t __attribute__((aligned(64))) et_global_barrier_count[64 / sizeof(uint32_t)] = { 0 };
// Cross-shire barrier: all harts in num_active_shires shires synchronize.
// Returns 1 if this hart was the globally-last to arrive, 0 otherwise.
//
// num_active_shires - number of shires participating
// (typically popcount(shire_mask) from kernel_environment_t)
static inline uint64_t __attribute__((always_inline)) et_barrier_global(uint64_t num_active_shires) {
uint64_t last_global = 0;
// FLB within this shire. Elect one hart per shire.
// Master shire has only 16 minions (32 harts), others have 32 (64 harts).
uint64_t shire_id = get_shire_id();
uint32_t harts_in_shire = (shire_id == SHIRE_MASTER) ? (SOC_MINIONS_PER_SHIRE / 2) * NUM_HARTS_PER_MINION :
SOC_MINIONS_PER_SHIRE * NUM_HARTS_PER_MINION;
uint64_t last_in_shire = flbarrier(0, harts_in_shire - 1);
if (last_in_shire) {
// Global atomic increment. Count arriving shires
uint32_t prev = et_global_add_w(et_global_barrier_count, 1);
if (prev == num_active_shires - 1) {
// Last shire. reset counter and fan out FCC to all shires
last_global = 1;
et_global_swap_w(et_global_barrier_count, 0);
for (uint64_t sid = 0; sid < 33; sid++) {
// Send FCC 1 credit to all harts (both threads) in each shire
fcc_send(sid, THREAD_0, FCC_1, 0xFFFFFFFF);
fcc_send(sid, THREAD_1, FCC_1, 0xFFFFFFFF);
}
}
}
// All harts wait for the FCC credit from the last shire
fcc_consume(FCC_1);
return last_global;
}
// Barrier with scope-derived parameters.
// Returns 1 if this hart was the last to arrive, 0 otherwise.
//
// ET_BARRIER_GLOBAL uses ET_DEFAULT_SHIRE_MASK (32 shires). For a different
// shire count, use et_barrier_global(n) directly.
static inline uint64_t __attribute__((always_inline)) et_barrier(et_barrier_scope_t scope) {
if (scope == ET_BARRIER_MINION) {
uint32_t local_minion = (get_hart_id() >> 1) & 0x1F;
uint32_t mask = 1u << local_minion;
return shire_barrier(local_minion, 0, 2, mask, mask);
} else if (scope == ET_BARRIER_SHIRE) {
uint64_t shire_id = get_shire_id();
uint32_t thread_count = (shire_id == SHIRE_MASTER) ? 32 : 64;
uint32_t mask = (shire_id == SHIRE_MASTER) ? 0xFFFF0000U : 0xFFFFFFFFU;
return shire_barrier(0, 1, thread_count, mask, mask);
} else { /* ET_BARRIER_GLOBAL */
return et_barrier_global(manual_popcountll(ET_DEFAULT_SHIRE_MASK));
}
}
// Raw barrier — caller manages FLB/FCC allocation.
// Use when et_barrier() doesn't fit (custom thread counts, subgroups,
// only even harts active, etc).
//
// flb - which FLB counter (0-31)
// fcc - which FCC counter (0 or 1)
// thread_count - number of harts that will call this barrier
// mask_t0 - CREDINC bitmask: which minions' hart 0 gets a credit
// mask_t1 - CREDINC bitmask: which minions' hart 1 gets a credit
static inline uint64_t __attribute__((always_inline)) et_barrier_raw(uint32_t flb,
uint32_t fcc,
uint32_t thread_count,
uint32_t mask_t0,
uint32_t mask_t1) {
return shire_barrier(flb, fcc, thread_count, mask_t0, mask_t1);
}
// One-way semaphore between harts (non-blocking post, blocking wait).
//
// et_sem_post(): increment the partner hart's semaphore. Non-blocking.
// the caller continues immediately. Multiple posts accumulate.
//
// et_sem_wait(): block until the semaphore is non-zero, then decrement it.
//
// Backed by hardware FCC (Flow Control Credit) counters. Uses FCC 0 for
// ET_BARRIER_MINION scope. Counters are per-hart private, so both harts
// can post/wait on the same scope independently.
//
// Must not be mixed with et_barrier() of the same scope in the
// same kernel (shared FCC channel).
static inline void __attribute__((always_inline)) et_sem_post(et_barrier_scope_t scope) {
if (scope == ET_BARRIER_MINION) {
uint64_t hart_id = get_hart_id();
uint32_t local_minion = (hart_id >> 1) & 0x1F;
uint32_t mask = 1u << local_minion;
uint64_t shire_id = get_shire_id();
if (hart_id & 1) {
// Hart 1 → hart 0
fcc_send(shire_id, THREAD_0, FCC_0, mask);
} else {
// Hart 0 → hart 1
fcc_send(shire_id, THREAD_1, FCC_0, mask);
}
}
}
// Block until a post from et_sem_post() is available, then consume it.
static inline void __attribute__((always_inline)) et_sem_wait(et_barrier_scope_t scope) {
if (scope == ET_BARRIER_MINION) {
fcc_consume(FCC_0);
}
}
//******************************************************************************
// Tensor Engine Wait & Error Macros
//
// These write to CSR 0x830 (tensor_wait) to stall the hart until the specified
// tensor unit completes its current operation. The immediate encodes which
// unit to wait on.
//******************************************************************************
#define WAIT_TENSOR_LOAD_0 __asm__ __volatile__("csrwi 0x830, 0\n" : :);
#define WAIT_TENSOR_LOAD_1 __asm__ __volatile__("csrwi 0x830, 1\n" : :);
#define WAIT_TENSOR_LOAD_L2_0 __asm__ __volatile__("csrwi 0x830, 2\n" : :);
#define WAIT_TENSOR_LOAD_L2_1 __asm__ __volatile__("csrwi 0x830, 3\n" : :);
#define WAIT_PREFETCH_0 __asm__ __volatile__("csrwi 0x830, 4\n" : :);
#define WAIT_PREFETCH_1 __asm__ __volatile__("csrwi 0x830, 5\n" : :);
#define WAIT_CACHEOPS __asm__ __volatile__("csrwi 0x830, 6\n" : :);
#define WAIT_TENSOR_FMA __asm__ __volatile__("csrwi 0x830, 7\n" : :);
#define WAIT_TENSOR_STORE __asm__ __volatile__("csrwi 0x830, 8\n" : :);
#define WAIT_TENSOR_REDUCE __asm__ __volatile__("csrwi 0x830, 9\n" : :);
#define WAIT_TENSOR_QUANT __asm__ __volatile__("csrwi 0x830, 10\n" : :);
#define STALL __asm__ __volatile__("csrw stall, x0\n" : :);
// Write 0 to CSR 0x808 (tensor_error) to clear any latched tensor error bits.
// Must be issued before the first tensor operation in a kernel to avoid stale
// errors from a previous invocation causing spurious faults.
#define CLEAR_TENSOR_ERROR __asm__ __volatile__("csrwi 0x808, 0" : :);
//******************************************************************************
// L1 Data Cache / Scratchpad (SCP) Configuration
//
// The ET-SoC-1 L1 data cache can be split so that half its ways operate as a
// software-managed scratchpad (SCP). Tensor load/store/FMA instructions
// require SCP mode to be active.
//
// CSR 0x810 — ucache_control:
//
// Bit(s) Field Description
// ────── ──────────── ──────────────────────────────────────────────────
// [0] D1Split 1 = L1 is split (half cache, half SCP).
// Read-only from U-mode; set by M-mode firmware
// before kernel launch. Writing ScpEnable while
// D1Split=0 is silently ignored.
// [1] ScpEnable 1 = scratchpad is active and zeroed.
// [4:2] RepRate Cache-op replay rate (0 = no delay between ops).
// [10:6] CacheOpMax Max outstanding cache ops (0 = unlimited).
//
// Typical kernel prologue for tensor operations:
// setup_cache_scp(); // enables SCP, waits for zeroing
// CLEAR_TENSOR_ERROR; // clear stale error bits
//******************************************************************************
// Write the ucache_control CSR (0x810).
//
// scp_en — 1 to enable SCP mode (requires D1Split already set)
// cacheop_rate — cache-op replay rate (07; 0 = no delay)
// cacheop_max — max outstanding cache ops (031; 0 = unlimited)
static inline void __attribute__((always_inline)) ucache_control(uint64_t scp_en,
uint64_t cacheop_rate,
uint64_t cacheop_max) {
uint64_t csr_enc = ((cacheop_max & 0x1F) << 6) | ((cacheop_rate & 0x7) << 2) | ((scp_en & 0x1) << 1);
__asm__ __volatile__("csrw 0x810, %[csr_enc]\n" : : [csr_enc] "r"(csr_enc) : "x31");
}
// Enable L1 scratchpad mode and wait for the transition to complete.
// After this call the SCP lines are zeroed and ready for tensor operations.
//
// Prerequisites:
// - D1Split must already be 1 (set by M-mode firmware at boot).
// - Only even harts (hart 0 per minion) should call this, as only they
// can issue tensor instructions.
static inline void setup_cache_scp(void) {
FENCE; // drain pending stores before reconfiguring cache
ucache_control(1, 0, 0); // ScpEnable=1
WAIT_CACHEOPS; // wait for SCP mode transition + zeroing
}
//******************************************************************************
// L2 Scratchpad (L2 SCP) Address Computation
//
// Each shire has 4 MB of SRAM that can be split across L2 cache, L3 cache,
// and scratchpad. The scratchpad region occupies 0x00_8000_0000~0x00_FFFF_FFFF
// and is accessible via regular load/store from any minion core.
//
// Two addressing formats (differentiated by address bit 30):
//
// Format 0 (bit[30]=0): Direct shire addressing
// [29:23] = shire ID (033, or 0x7F for local shire)
// [22:0] = byte offset within shire's scratchpad
//
// Format 1 (bit[30]=1): Striped (round-robin) addressing
// [29:28] = shire ID[6:5]
// [27:11] = offset[22:6] (cache-line-aligned upper bits)
// [10:6] = shire ID[4:0]
// [5:0] = offset[5:0] (byte within cache line)
// Consecutive 64-byte cache lines cycle through different shires,
// distributing bandwidth across the mesh.
//
// Shire ID 0x7F always targets the local shire (instead of figureing out which
// shire you are on).
//******************************************************************************
#define L2SCP_BASE 0x0080000000ULL
#define L2SCP_SHIRE_LOCAL 0x7FULL
// Format 0: direct address into a specific shire's L2 SCP.
// shire: 033 for explicit shire, L2SCP_SHIRE_LOCAL (0x7F) for local
// offset: byte offset within the shire's scratchpad
static inline void * __attribute__((always_inline)) et_shire_l2scp(uint64_t shire, uint64_t offset) {
return (void *) (L2SCP_BASE | ((shire & 0x7F) << 23) | (offset & 0x7FFFFF));
}
// Format 0: local shire shorthand — no cross-shire traffic.
static inline void * __attribute__((always_inline)) et_shire_l2scp_local(uint64_t offset) {
return (void *) (L2SCP_BASE | (L2SCP_SHIRE_LOCAL << 23) | (offset & 0x7FFFFF));
}
// Format 1: flat offset into a hardware-striped global address space.
// Consecutive 64-byte cache lines automatically land on different shires,
// distributing bandwidth across the mesh. No shire parameter — the
// hardware derives the target shire from the address bits.
static inline void * __attribute__((always_inline)) et_global_l2scp(uint64_t offset) {
return (void *) (L2SCP_BASE | (1ULL << 30) | (offset & 0x3FFFFFFF));
}
//******************************************************************************
// Cache Operatons
//******************************************************************************
// Prefetch nlines cache lines into L2 starting at addr, with stride bytes
// between each line. Uses PrefetchVA (CSR 0x81F) with dest=L2 (bits 59:58=01).
//
// The hardware fetches nlines consecutive cache-line-sized (64B) blocks from
// DRAM/L3 into L2, starting at addr and advancing by stride bytes per line.
// This is asynchronous — use WAIT_PREFETCH_0 or WAIT_PREFETCH_1 if the hart
// must stall until the prefetch completes.
//
// NOTE: nlines is encoded in a 4-bit field (max 16). Passing nlines > 16
// silently truncates. DO NOT pass nlines > 16.
static inline void __attribute__((always_inline)) l2_prefetch(const void * addr, uint64_t nlines, uint64_t stride) {
uint64_t csr_val = (0x1ULL << 58) | ((uint64_t) addr & 0xFFFFFFFFFFC0ULL) | ((nlines - 1) & 0xF);
__asm__ __volatile__(
"mv x31, %[stride]\n"
"csrw 0x81f, %[val]\n"
:
: [stride] "r"(stride & 0xFFFFFFFFFFC0ULL), [val] "r"(csr_val)
: "x31", "memory");
}
// Flush nlines cache lines at stride apart starting at addr from L1 to L2.
// Uses FlushVA (CSR 0x8BF). Caller must FENCE before (to drain stores to L1)
// and WAIT_CACHEOPS after (to ensure flush completes before tensor loads).
//
// NOTE: nlines is encoded in a 4-bit field (max 16). Passing nlines > 16
// silently truncates. DO NOT pass nlines > 16.
static inline void __attribute__((always_inline)) flush_to_l2(const void * addr, uint64_t nlines, uint64_t stride) {
// dest=01 (L2) in bits 59:58, VA in bits 47:6, numlines-1 in bits 3:0
uint64_t csr_val = (0x1ULL << 58) | ((uint64_t) addr & 0xFFFFFFFFFFC0ULL) | ((nlines - 1) & 0xF);
uint64_t x31_val = stride & 0xFFFFFFFFFFC0ULL;
__asm__ __volatile__(
"mv x31, %[x31]\n"
"csrw 0x8BF, %[val]\n"
:
: [x31] "r"(x31_val), [val] "r"(csr_val)
: "x31", "memory");
}
// Evict nlines cache lines at stride apart starting at addr from L1 to L2.
// Uses EvictVA (CSR 0x89F). Unlike flush_to_l2, this guarantees the line is
// NOT present in L1 after the operation - subsequent loads will miss and go
// to L2/SCP. Caller must FENCE before and WAIT_CACHEOPS after.
//
// NOTE: nlines is encoded in a 4-bit field (max 16). DO NOT pass nlines > 16.
static inline void __attribute__((always_inline)) evict_to_l2(const void * addr, uint64_t nlines, uint64_t stride) {
// dest=01 (L2) in bits 59:58, VA in bits 47:6, numlines-1 in bits 3:0
uint64_t csr_val = (0x1ULL << 58) | ((uint64_t) addr & 0xFFFFFFFFFFC0ULL) | ((nlines - 1) & 0xF);
uint64_t x31_val = stride & 0xFFFFFFFFFFC0ULL;
__asm__ __volatile__(
"mv x31, %[x31]\n"
"csrw 0x89F, %[val]\n"
:
: [x31] "r"(x31_val), [val] "r"(csr_val)
: "x31", "memory");
}
// Evict nlines cache lines at stride apart starting at addr from BOTH L1
// and L2. Uses EvictVA (CSR 0x89F) with dest=10 (L3/DRAM). Guarantees the
// line is NOT present in L1 or L2 after the operation — subsequent loads
// will fetch from L3 or DRAM. Needed because both L1 and L2 are incoherent
// on ET-SoC-1 (L2 is per-shire).
// Caller must FENCE before and WAIT_CACHEOPS after.
//
// NOTE: nlines is encoded in a 4-bit field (max 16). DO NOT pass nlines > 16.
static inline void __attribute__((always_inline)) evict_past_l2(const void * addr, uint64_t nlines, uint64_t stride) {
// dest=10 in bits 59:58, VA in bits 47:6, numlines-1 in bits 3:0
uint64_t csr_val = (0x2ULL << 58) | ((uint64_t) addr & 0xFFFFFFFFFFC0ULL) | ((nlines - 1) & 0xF);
uint64_t x31_val = stride & 0xFFFFFFFFFFC0ULL;
__asm__ __volatile__(
"mv x31, %[x31]\n"
"csrw 0x89F, %[val]\n"
:
: [x31] "r"(x31_val), [val] "r"(csr_val)
: "x31", "memory");
}
// Evict a contiguous region from both L1 and L2 so subsequent loads fetch
// from L3/DRAM. Both L1 and L2 are incoherent on ET-SoC-1 (L2 is per-shire),
// so every op must evict its inputs before reading if a prior op in the same
// uberkernel batch may have written to them via fsw.ps or tensor_store.
//
// Handles regions larger than the 16-line hardware limit by issuing multiple
// evict_past_l2 calls.
static void evict_region_past_l2(const void * addr, size_t bytes) {
if (!addr || bytes == 0) {
return;
}
const uint64_t CL = 64;
uint64_t base = (uint64_t) addr & ~(CL - 1);
uint64_t end = ((uint64_t) addr + bytes + CL - 1) & ~(CL - 1);
uint64_t nlines = (end - base) / CL;
// FENCE;
for (uint64_t off = 0; off < nlines; off += 16) {
uint64_t batch = nlines - off;
if (batch > 16) {
batch = 16;
}
evict_past_l2((const void *) (base + off * CL), batch, CL);
}
}
#endif // PLATFORM_H
+72
View File
@@ -0,0 +1,72 @@
// Scalar dequantization helpers and ET-side block-size aliases.
#ifndef QUANTS_H
#define QUANTS_H
#include "math_fp.h"
#include <stdint.h>
#define GGML_COMMON_DECL_C
#include "ggml-common.h"
// 64-byte (one cache line) F16 / F32 block sizes.
#define QK_F16 32
#define QK_F32 16
static inline void dequantize_q8_0_block(const block_q8_0 * block, float * dst) {
const float scale = fp16_to_fp32(block->d);
for (int i = 0; i < QK8_0; i++) {
dst[i] = scale * (float) block->qs[i];
}
}
// Low nibbles -> dst[0..15], high nibbles -> dst[16..31].
static inline void dequantize_q4_0_block(const block_q4_0 * block, float * dst) {
const float scale = fp16_to_fp32(block->d);
for (int i = 0; i < QK4_0 / 2; i++) {
const uint8_t byte = block->qs[i];
dst[i] = scale * (float) ((int) (byte & 0xF) - 8);
dst[i + QK4_0 / 2] = scale * (float) ((int) (byte >> 4) - 8);
}
}
// Unpack the 6-bit scale/min pair for Q4_K group j (groups 4-7 split their high bits).
static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t * d, uint8_t * m) {
if (j < 4) {
*d = q[j] & 63;
*m = q[j + 4] & 63;
} else {
*d = (q[j + 4] & 0xF) | ((q[j - 4] >> 6) << 4);
*m = (q[j + 4] >> 4) | ((q[j] >> 6) << 4);
}
}
static inline void dequantize_q4_K_block(const block_q4_K * block, float * dst) {
const uint8_t * q = block->qs;
const float d = fp16_to_fp32(block->d);
const float min = fp16_to_fp32(block->dmin);
int is = 0;
uint8_t sc, m;
for (int j = 0; j < QK_K; j += 64) {
get_scale_min_k4(is + 0, block->scales, &sc, &m);
const float d1 = d * sc;
const float m1 = min * m;
get_scale_min_k4(is + 1, block->scales, &sc, &m);
const float d2 = d * sc;
const float m2 = min * m;
for (int l = 0; l < 32; ++l) {
*dst++ = d1 * (q[l] & 0xF) - m1;
}
for (int l = 0; l < 32; ++l) {
*dst++ = d2 * (q[l] >> 4) - m2;
}
q += 32;
is += 2;
}
}
#endif // QUANTS_H
@@ -0,0 +1,118 @@
//******************************************************************************
// Repeat F32 Kernel
// Tiles src0 into dst: dst.ne[i] = src0.ne[i] * nr[i] for each dimension.
// All copies are cacheline-aligned (ne00 % 16 == 0).
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
#include <string.h>
struct ggml_et_repeat_params {
struct ggml_tensor src0; // F32 input tensor (tile)
struct ggml_tensor dst; // F32 output tensor (tiled result)
};
// Copy n floats from src to dst using 8-wide vector loads/stores.
// n must be a multiple of 16 (cacheline-aligned).
static inline void copy_row_aligned(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f11, %[src_vec]\n"
"fsw.ps f11, %[dst_vec]\n"
: [dst_vec] "=m"(*(float (*)[8]) & dst[i])
: [src_vec] "m"(*(const float (*)[8]) & src[i])
: "f11");
}
}
// Broadcast a single scalar to n floats using fbc.ps (broadcast to all lanes).
// n must be a multiple of 16 (cacheline-aligned).
static inline void broadcast_scalar_aligned(float * dst, float val, int32_t n) {
__asm__ volatile("fbc.ps f11, %[v]\n" : : [v] "m"(val) : "f11");
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile("fsw.ps f11, %[dst_vec]\n" : [dst_vec] "=m"(*(float (*)[8]) & dst[i])::"f11");
}
}
int entry_point(struct ggml_et_repeat_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
const int64_t ne00 = src0->ne[0], ne01 = src0->ne[1], ne02 = src0->ne[2], ne03 = src0->ne[3];
const int64_t ne0 = dst->ne[0], ne1 = dst->ne[1], ne2 = dst->ne[2], ne3 = dst->ne[3];
// src0 strides in bytes
const size_t nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
// dst strides in bytes
const size_t dnb0 = dst->nb[0], dnb1 = dst->nb[1], dnb2 = dst->nb[2], dnb3 = dst->nb[3];
// Repeat counts per dimension
const int32_t nr0 = (int32_t) (ne0 / ne00);
const int32_t nr1 = (int32_t) (ne1 / ne01);
const int32_t nr2 = (int32_t) (ne2 / ne02);
const int32_t nr3 = (int32_t) (ne3 / ne03);
// Total output rows across all dimensions (excluding dim 0 tiling)
const int64_t total_rows = ne1 * ne2 * ne3;
for (int64_t row = thread_id; row < total_rows; row += num_threads) {
// Decompose linear row index into dst (i1, i2, i3)
int64_t i1 = row % ne1;
int64_t i2 = (row / ne1) % ne2;
int64_t i3 = row / (ne1 * ne2);
// Map dst indices back to src0 indices (modular wrap)
int64_t k1 = i1 % ne01;
int64_t k2 = i2 % ne02;
int64_t k3 = i3 % ne03;
const float * src_row = (const float *) ((const char *) src0_data + k1 * nb01 + k2 * nb02 + k3 * nb03);
float * dst_row = (float *) ((char *) dst_data + i1 * dnb1 + i2 * dnb2 + i3 * dnb3);
if (ne00 == 1) {
// Scalar broadcast: splat single value across entire dst row
broadcast_scalar_aligned(dst_row, *src_row, (int32_t) ne0);
} else if (nr0 == 1) {
// No tiling along dim 0 - single cacheline-aligned row copy
copy_row_aligned(dst_row, src_row, (int32_t) ne00);
} else {
// Tile ne00-sized chunks across dim 0
for (int32_t i0 = 0; i0 < nr0; i0++) {
copy_row_aligned(dst_row + i0 * ne00, src_row, (int32_t) ne00);
}
}
}
return 0;
}
@@ -0,0 +1,270 @@
//******************************************************************************
// RMS Norm F32 Kernel
// Root Mean Square normalization: y[i] = x[i] / sqrt(mean(x^2) + eps)
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>
// RMS norm kernel parameters structure
struct ggml_et_rms_norm_params {
struct ggml_tensor src0; // F32 input tensor
struct ggml_tensor dst; // F32 output tensor
float eps; // Epsilon parameter for numerical stability
};
int entry_point(struct ggml_et_rms_norm_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
float eps = params->eps;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1; // Unsupported type combination
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1; // Null data pointer
}
if (eps < 0.0f) {
return -1; // Invalid epsilon
}
const int64_t ne0 = dst->ne[0]; // Inner dimension (row size)
const int64_t ne1 = dst->ne[1]; // Dimension 1
const int64_t ne2 = dst->ne[2]; // Dimension 2
const int64_t ne3 = dst->ne[3]; // Dimension 3
// Get dst strides (in bytes)
const size_t nb0 = dst->nb[0], nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
// Get src0 strides (in bytes)
const size_t nb00 = src0->nb[0], nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
// Verify that src0 and dst have same shape (required for RMS norm)
if (src0->ne[0] != ne0 || src0->ne[1] != ne1 || src0->ne[2] != ne2 || src0->ne[3] != ne3) {
return -1; // Shape mismatch
}
// RMS norm processes rows independently
// Parallelize across rows using simple striding
// TODO: ensure lines don't cross cache lines
// Precompute reciprocal of row length (constant across all rows)
const float inv_ne0 = et_fdiv(1.0f, (float) (int32_t) ne0);
const int32_t total_rows = (int32_t) (ne1 * ne2 * ne3);
// Intra-row cooperation only works within a single shire (barrier + L2SCP
// are shire-local). Use per-shire thread count for the threshold.
const int shire_threads = SOC_MINIONS_PER_SHIRE * NUM_HARTS_PER_MINION; // 64
if (total_rows >= shire_threads) {
// Row-parallel: each thread processes whole rows
for (int64_t i3 = 0; i3 < ne3; i3++) {
for (int64_t i2 = 0; i2 < ne2; i2++) {
for (int64_t i1 = thread_id; i1 < ne1; i1 += num_threads) {
const float * src_ptr =
(const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_ptr = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
// Set mask to enable all 8 vector lanes
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
// Step 1: Compute sum of squares using 8-wide vectors
__asm__ volatile("fbci.pi f10, 0" ::: "f10");
for (int32_t i0 = 0; i0 < (int32_t) ne0; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fmadd.ps f10, f11, f11, f10\n"
:
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f10", "f11");
}
// Horizontal reduce
float sum;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(sum)::"t0", "f1", "f2", "f3", "f4", "f5");
// Step 2: scale = rsqrt(mean + eps)
const float scale = et_powf(sum * inv_ne0 + eps, -0.5f);
if (!(scale > 0.0f)) {
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
return -1;
}
// Step 3: Apply scaling: broadcast scale once, reuse across loop
uint32_t scale_bits;
__asm__ volatile("fmv.x.s %0, %1" : "=r"(scale_bits) : "f"(scale));
__asm__ volatile("fbcx.ps f13, %[sb]\n" : : [sb] "r"(scale_bits) : "f13");
for (int32_t i0 = 0; i0 < (int32_t) ne0; i0 += 8) {
__asm__ volatile(
"flw.ps f12, %[x_vec]\n"
"fmul.ps f14, f12, f13\n"
"fsw.ps f14, %[result]\n"
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f12", "f14");
}
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
}
}
}
} else {
// Intra-row: threads within each shire cooperate on rows via L2 SCP.
// L2 SCP + barrier are shire-local, so use shire-local thread index.
int shire_tid = thread_id % shire_threads; // 0..63 within this shire
int threads_per_row = shire_threads / total_rows;
int my_row = shire_tid / threads_per_row;
int local_tid = shire_tid % threads_per_row;
int group_base = my_row * threads_per_row; // shire-local group base
// Excess threads within this shire, barrier and leave
if (my_row >= total_rows) {
FENCE;
et_barrier(ET_BARRIER_SHIRE);
return 0;
}
// Unflatten row index
int64_t i1 = my_row % ne1;
int64_t i2 = (my_row / ne1) % ne2;
int64_t i3 = my_row / (ne1 * ne2);
const float * src_ptr = (const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_ptr = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
// Chunk boundaries aligned to 16 floats (64-byte cache line)
const int32_t elems_per_cl = 16;
int32_t total_cls = ((int32_t) ne0 + elems_per_cl - 1) / elems_per_cl;
int32_t cls_per_thread = (total_cls + threads_per_row - 1) / threads_per_row;
int32_t my_start = local_tid * cls_per_thread * elems_per_cl;
int32_t my_end = my_start + cls_per_thread * elems_per_cl;
if (my_end > (int32_t) ne0) {
my_end = (int32_t) ne0;
}
if (my_start >= (int32_t) ne0) {
my_start = 0;
my_end = 0;
}
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
// Phase 1: each thread computes partial sum of squares on its chunk
__asm__ volatile("fbci.pi f10, 0" ::: "f10");
for (int32_t i0 = my_start; i0 < my_end; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fmadd.ps f10, f11, f11, f10\n"
:
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f10", "f11");
}
// Horizontal reduce to scalar
float partial_sum;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(partial_sum)::"t0", "f1", "f2", "f3", "f4", "f5");
// Phase 2: write partial sum to L2 SCP, evict from L1D
volatile float * my_slot = (volatile float *) et_shire_l2scp_local((uint64_t) shire_tid * 64);
*my_slot = partial_sum;
FENCE;
evict_to_l2((const void *) my_slot, 1, 64);
WAIT_CACHEOPS;
et_barrier(ET_BARRIER_SHIRE);
// Phase 3: ALL threads read partial sums, compute scale, apply to own chunk.
// Each thread independently reduces to avoid a second barrier.
int workers = threads_per_row < total_cls ? threads_per_row : total_cls;
// Evict stale L1D entries for worker slots
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
evict_to_l2((const void *) slot, 1, 64);
}
WAIT_CACHEOPS;
// Every thread reduces the same partial sums -> same scale
float total_sum = 0.0f;
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
total_sum += *slot;
}
const float scale = et_powf(total_sum * inv_ne0 + eps, -0.5f);
if (!(scale > 0.0f)) {
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
return -1;
}
// Each thread applies scale to its own chunk only
if (my_start < my_end) {
uint32_t scale_bits;
__asm__ volatile("fmv.x.s %0, %1" : "=r"(scale_bits) : "f"(scale));
__asm__ volatile("fbcx.ps f13, %[sb]\n" : : [sb] "r"(scale_bits) : "f13");
for (int32_t i0 = my_start; i0 < my_end; i0 += 8) {
__asm__ volatile(
"flw.ps f12, %[x_vec]\n"
"fmul.ps f14, f12, f13\n"
"fsw.ps f14, %[result]\n"
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f12", "f14");
}
}
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
}
return 0;
}
@@ -0,0 +1,290 @@
// Fused RMS Norm + MUL F32 Kernel
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>
// Fused RMS norm + MUL kernel parameters structure
struct ggml_et_rms_norm_mul_params {
struct ggml_tensor src0; // F32 input tensor (to be normalized)
struct ggml_tensor src1; // F32 weights tensor (element-wise multiply)
struct ggml_tensor dst; // F32 output tensor
float eps; // Epsilon for numerical stability
};
static inline size_t tensor_bytes(const struct ggml_tensor * t) {
return (size_t) t->ne[0] * t->ne[1] * t->ne[2] * t->ne[3] * t->nb[0];
}
int entry_point(struct ggml_et_rms_norm_mul_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * src1 = &params->src1;
struct ggml_tensor * dst = &params->dst;
float eps = params->eps;
if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1; // Unsupported type combination
}
float * src0_data = (float *) src0->data;
float * src1_data = (float *) src1->data;
float * dst_data = (float *) dst->data;
// #ifdef ET_UBERKERNEL
// evict_region_past_l2(src0_data, tensor_bytes(src0));
// evict_region_past_l2(src1_data, tensor_bytes(src1));
// // WAIT_CACHEOPS;
// FENCE;
// // et_barrier(ET_BARRIER_GLOBAL);
// #endif
if (!src0_data || !src1_data || !dst_data) {
return -1; // Null data pointer
}
if (eps < 0.0f) {
return -1; // Invalid epsilon
}
const int64_t ne0 = dst->ne[0]; // Inner dimension (row size)
const int64_t ne1 = dst->ne[1]; // Dimension 1
const int64_t ne2 = dst->ne[2]; // Dimension 2
const int64_t ne3 = dst->ne[3]; // Dimension 3
// Get dst strides (in bytes)
const size_t nb0 = dst->nb[0], nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
// Get src0 strides (in bytes)
const size_t nb00 = src0->nb[0], nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
// Get src1 (weights) strides (in bytes), supports broadcasting in dims 1,2,3
const size_t nb10 = src1->nb[0], nb11 = src1->nb[1], nb12 = src1->nb[2], nb13 = src1->nb[3];
// Verify that src0 and dst have same shape (required for RMS norm)
if (src0->ne[0] != ne0 || src0->ne[1] != ne1 || src0->ne[2] != ne2 || src0->ne[3] != ne3) {
return -1; // Shape mismatch
}
// et_barrier(ET_BARRIER_GLOBAL);
const float inv_ne0 = et_fdiv(1.0f, (float) (int32_t) ne0);
const int32_t total_rows = (int32_t) (ne1 * ne2 * ne3);
const int shire_threads = SOC_MINIONS_PER_SHIRE * NUM_HARTS_PER_MINION;
if (total_rows >= shire_threads) {
// Row-parallel: each thread processes whole rows
for (int64_t i3 = 0; i3 < ne3; i3++) {
for (int64_t i2 = 0; i2 < ne2; i2++) {
for (int64_t i1 = thread_id; i1 < ne1; i1 += num_threads) {
const float * src_ptr =
(const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_ptr = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
const float * wgt_ptr = (const float *) ((const char *) src1_data + (i3 % src1->ne[3]) * nb13 +
(i2 % src1->ne[2]) * nb12 + (i1 % src1->ne[1]) * nb11);
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
// Sum of squares
__asm__ volatile("fbci.pi f10, 0" ::: "f10");
for (int32_t i0 = 0; i0 < (int32_t) ne0; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fmadd.ps f10, f11, f11, f10\n"
:
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f10", "f11");
}
float sum;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(sum)::"t0", "f1", "f2", "f3", "f4", "f5");
const float scale = et_powf(sum * inv_ne0 + eps, -0.5f);
if (!(scale > 0.0f)) {
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
return -1;
}
uint32_t scale_bits;
__asm__ volatile("fmv.x.s %0, %1" : "=r"(scale_bits) : "f"(scale));
__asm__ volatile("fbcx.ps f13, %[sb]\n" : : [sb] "r"(scale_bits) : "f13");
for (int32_t i0 = 0; i0 < (int32_t) ne0; i0 += 8) {
__asm__ volatile(
"flw.ps f12, %[x_vec]\n"
"flw.ps f15, %[w_vec]\n"
"fmul.ps f14, f12, f13\n"
"fmul.ps f14, f14, f15\n"
"fsw.ps f14, %[result]\n"
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0]), [w_vec] "m"(*(const float (*)[8]) &
wgt_ptr[i0])
: "f12", "f14", "f15");
}
// #ifdef ET_UBERKERNEL
// FENCE;
// evict_region_past_l2(dst_ptr, (size_t)ne0 * sizeof(float));
// WAIT_CACHEOPS;
// FENCE;
// #endif
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
}
}
}
} else {
// Intra-row: threads within each shire cooperate on rows via L2 SCP.
// L2 SCP + barrier are shire-local, so use shire-local thread index.
int shire_tid = thread_id % shire_threads;
int threads_per_row = shire_threads / total_rows;
int my_row = shire_tid / threads_per_row;
int local_tid = shire_tid % threads_per_row;
int group_base = my_row * threads_per_row;
// Excess threads within this shire
if (my_row >= total_rows) {
__asm__ __volatile__("fence\n" ::: "memory");
et_barrier(ET_BARRIER_SHIRE);
return 0;
}
// Unflatten row index
int64_t i1 = my_row % ne1;
int64_t i2 = (my_row / ne1) % ne2;
int64_t i3 = my_row / (ne1 * ne2);
const float * src_ptr = (const float *) ((const char *) src0_data + i3 * nb03 + i2 * nb02 + i1 * nb01);
float * dst_ptr = (float *) ((char *) dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1);
const float * wgt_ptr = (const float *) ((const char *) src1_data + (i3 % src1->ne[3]) * nb13 +
(i2 % src1->ne[2]) * nb12 + (i1 % src1->ne[1]) * nb11);
// Chunk boundaries aligned to 16 floats (64-byte cache line)
const int32_t elems_per_cl = 16;
int32_t total_cls = ((int32_t) ne0 + elems_per_cl - 1) / elems_per_cl;
int32_t cls_per_thread = (total_cls + threads_per_row - 1) / threads_per_row;
int32_t my_start = local_tid * cls_per_thread * elems_per_cl;
int32_t my_end = my_start + cls_per_thread * elems_per_cl;
if (my_end > (int32_t) ne0) {
my_end = (int32_t) ne0;
}
if (my_start >= (int32_t) ne0) {
my_start = 0;
my_end = 0;
}
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
// Phase 1: partial sum of squares on own chunk
__asm__ volatile("fbci.pi f10, 0" ::: "f10");
for (int32_t i0 = my_start; i0 < my_end; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fmadd.ps f10, f11, f11, f10\n"
:
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f10", "f11");
}
float partial_sum;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(partial_sum)::"t0", "f1", "f2", "f3", "f4", "f5");
// Phase 2: write partial sum to L2 SCP, evict from L1D
volatile float * my_slot = (volatile float *) et_shire_l2scp_local((uint64_t) shire_tid * 64);
*my_slot = partial_sum;
__asm__ __volatile__("fence\n" ::: "memory");
evict_to_l2((const void *) my_slot, 1, 64);
WAIT_CACHEOPS;
et_barrier(ET_BARRIER_SHIRE);
// Phase 3: all threads read partial sums, compute scale, apply to own chunk
int workers = threads_per_row < total_cls ? threads_per_row : total_cls;
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
evict_to_l2((const void *) slot, 1, 64);
}
WAIT_CACHEOPS;
float total_sum = 0.0f;
for (int t = 0; t < workers; t++) {
volatile float * slot = (volatile float *) et_shire_l2scp_local((uint64_t) (group_base + t) * 64);
total_sum += *slot;
}
const float scale = et_powf(total_sum * inv_ne0 + eps, -0.5f);
if (!(scale > 0.0f)) {
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
return -1;
}
// Apply scale * weights to own chunk
if (my_start < my_end) {
uint32_t scale_bits;
__asm__ volatile("fmv.x.s %0, %1" : "=r"(scale_bits) : "f"(scale));
__asm__ volatile("fbcx.ps f13, %[sb]\n" : : [sb] "r"(scale_bits) : "f13");
for (int32_t i0 = my_start; i0 < my_end; i0 += 8) {
__asm__ volatile(
"flw.ps f12, %[x_vec]\n"
"flw.ps f15, %[w_vec]\n"
"fmul.ps f14, f12, f13\n"
"fmul.ps f14, f14, f15\n"
"fsw.ps f14, %[result]\n"
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0]), [w_vec] "m"(*(const float (*)[8]) & wgt_ptr[i0])
: "f12", "f14", "f15");
}
// #ifdef ET_UBERKERNEL
// FENCE;
// evict_region_past_l2(dst_ptr + my_start, (size_t)(my_end - my_start) * sizeof(float));
// WAIT_CACHEOPS;
// FENCE;
// #endif
}
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
}
return 0;
}
+656
View File
@@ -0,0 +1,656 @@
//******************************************************************************
// ROPE (Rotary Position Encoding) Kernel
// Experiment 1:
// - Keep old scheduling and rotate logic
// - ONLY SIMD-ize sin/cos approximation inside compute_rope_cache()
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <etsoc/common/utils.h>
#include <stdint.h>
// ROPE constants (matching GGML definitions)
#define GGML_ROPE_TYPE_NEOX 2
#define GGML_ROPE_TYPE_MROPE 8
#define GGML_ROPE_TYPE_IMROPE 40
#define MAX_ROPE_HALF_DIMS 256 // supports up to n_dims=512
#define ROPE_VEC_WIDTH 8
#define ROPE_PI 3.14159265358979323846f
#define ROPE_TWO_PI 6.28318530717958647693f
#define ROPE_PI_OVER_2 1.57079632679489661923f
#define ROPE_INV_TWO_PI 0.15915494309189533577f
// ROPE operation parameters structure (matches ggml-et-ops.h)
typedef struct {
int32_t n_past;
int32_t n_dims; // Number of dimensions to apply ROPE to (must be even)
int32_t mode; // ROPE mode (0=normal, 2=neox)
int32_t n_ctx;
int32_t n_ctx_orig;
float freq_base; // Base frequency (usually 10000.0f)
float freq_scale; // Frequency scaling factor
float ext_factor; // Extension factor for YaRN
float attn_factor; // Attention factor for YaRN
float beta_fast; // Fast beta for YaRN
float beta_slow; // Slow beta for YaRN
int32_t sections[4]; // Sections for multi-modal ROPE
} rope_params_t;
// ROPE kernel parameters structure (matches ggml_et_rope_params)
struct ggml_et_rope_params {
struct ggml_tensor src0; // F32 input tensor
struct ggml_tensor src1; // I32 position tensor
struct ggml_tensor src2; // F32 frequency factors (optional)
struct ggml_tensor dst; // F32 output tensor
rope_params_t rope_params;
};
//------------------------------------------------------------------------------
// Existing scalar helpers
//------------------------------------------------------------------------------
// floor/ceil with ±inf and NaN passthrough.
static inline float rope_floorf(float x) {
union {
float f;
uint32_t u;
} v = { .f = x };
const uint32_t expo = (v.u >> 23) & 0xFF;
if (expo == 0xFF) {
return x; // inf or NaN
}
if (expo >= 23 + 127) {
return x; // already integer-valued
}
int i = (int) x;
return (x < 0.0f && (float) i != x) ? (float) (i - 1) : (float) i;
}
static inline float rope_ceilf(float x) {
union {
float f;
uint32_t u;
} v = { .f = x };
const uint32_t expo = (v.u >> 23) & 0xFF;
if (expo == 0xFF) {
return x; // inf or NaN
}
if (expo >= 23 + 127) {
return x; // already integer-valued
}
int i = (int) x;
return (x > 0.0f && (float) i != x) ? (float) (i + 1) : (float) i;
}
static inline float rope_yarn_ramp(const float low, const float high, const int i0) {
float denom = high - low;
if (denom < 0.001f) {
denom = 0.001f;
}
const float y = et_fdiv((float) (i0 / 2) - low, denom);
const float clamped = y < 0.0f ? 0.0f : (y > 1.0f ? 1.0f : y);
return 1.0f - clamped;
}
// Matches CPU reference (ggml_rope_yarn_corr_dim).
static inline float rope_yarn_corr_dim(int n_dims, int n_ctx_orig, float beta, float freq_base) {
return (float) n_dims *
et_fdiv(et_logf(et_fdiv((float) n_ctx_orig, beta * ROPE_TWO_PI)), 2.0f * et_logf(freq_base));
}
static inline void rope_yarn_corr_dims(int n_dims,
int n_ctx_orig,
float freq_base,
float beta_fast,
float beta_slow,
float dims[2]) {
// Match CPU: floor on start, ceil on end, then clamp to [0, n_dims-1].
float start = rope_floorf(rope_yarn_corr_dim(n_dims, n_ctx_orig, beta_fast, freq_base));
float end = rope_ceilf(rope_yarn_corr_dim(n_dims, n_ctx_orig, beta_slow, freq_base));
dims[0] = start > 0.0f ? start : 0.0f;
dims[1] = end < (float) (n_dims - 1) ? end : (float) (n_dims - 1);
}
//------------------------------------------------------------------------------
// SIMD sin/cos approximation
//------------------------------------------------------------------------------
static const float rope_ps_one[ROPE_VEC_WIDTH]
__attribute__((aligned(32))) = { 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f };
static const float rope_ps_c3[ROPE_VEC_WIDTH]
__attribute__((aligned(32))) = { 1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f,
1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f };
static const float rope_ps_c5[ROPE_VEC_WIDTH]
__attribute__((aligned(32))) = { 1.0f / 120.0f, 1.0f / 120.0f, 1.0f / 120.0f, 1.0f / 120.0f,
1.0f / 120.0f, 1.0f / 120.0f, 1.0f / 120.0f, 1.0f / 120.0f };
static const float rope_ps_c7[ROPE_VEC_WIDTH]
__attribute__((aligned(32))) = { 1.0f / 5040.0f, 1.0f / 5040.0f, 1.0f / 5040.0f, 1.0f / 5040.0f,
1.0f / 5040.0f, 1.0f / 5040.0f, 1.0f / 5040.0f, 1.0f / 5040.0f };
static const float rope_ps_c9[ROPE_VEC_WIDTH]
__attribute__((aligned(32))) = { 1.0f / 362880.0f, 1.0f / 362880.0f, 1.0f / 362880.0f, 1.0f / 362880.0f,
1.0f / 362880.0f, 1.0f / 362880.0f, 1.0f / 362880.0f, 1.0f / 362880.0f };
static const float rope_ps_c11[ROPE_VEC_WIDTH]
__attribute__((aligned(32))) = { 1.0f / 39916800.0f, 1.0f / 39916800.0f, 1.0f / 39916800.0f, 1.0f / 39916800.0f,
1.0f / 39916800.0f, 1.0f / 39916800.0f, 1.0f / 39916800.0f, 1.0f / 39916800.0f };
static inline uint64_t rope_ps_enter_fullmask(void) {
uint64_t old_mask;
__asm__ volatile(
"mova.x.m %0 \n\t"
"li t0, -1 \n\t"
"mova.m.x t0 \n\t"
: "=r"(old_mask)
:
: "t0", "memory");
return old_mask;
}
static inline void rope_ps_leave_fullmask(uint64_t old_mask) {
__asm__ volatile("mova.m.x %0 \n\t" : : "r"(old_mask) : "memory");
}
static inline void rope_poly_sin_block8(float * out, const float * x) {
__asm__ volatile(
"flw.ps f0, %[x] \n\t"
"fmul.ps f1, f0, f0 \n\t"
"flw.ps f2, %[c11] \n\t"
"flw.ps f3, %[c9] \n\t"
"fnmsub.ps f2, f1, f2, f3 \n\t"
"flw.ps f3, %[c7] \n\t"
"fnmsub.ps f2, f1, f2, f3 \n\t"
"flw.ps f3, %[c5] \n\t"
"fnmsub.ps f2, f1, f2, f3 \n\t"
"flw.ps f3, %[c3] \n\t"
"fnmsub.ps f2, f1, f2, f3 \n\t"
"flw.ps f3, %[one] \n\t"
"fnmsub.ps f2, f1, f2, f3 \n\t"
"fmul.ps f4, f0, f2 \n\t"
"fsw.ps f4, %[out] \n\t"
: [out] "=m"(*(float (*)[ROPE_VEC_WIDTH]) out)
: [x] "m"(*(const float (*)[ROPE_VEC_WIDTH]) x), [one] "m"(*(const float (*)[ROPE_VEC_WIDTH]) rope_ps_one),
[c3] "m"(*(const float (*)[ROPE_VEC_WIDTH]) rope_ps_c3),
[c5] "m"(*(const float (*)[ROPE_VEC_WIDTH]) rope_ps_c5),
[c7] "m"(*(const float (*)[ROPE_VEC_WIDTH]) rope_ps_c7),
[c9] "m"(*(const float (*)[ROPE_VEC_WIDTH]) rope_ps_c9),
[c11] "m"(*(const float (*)[ROPE_VEC_WIDTH]) rope_ps_c11)
: "f0", "f1", "f2", "f3", "f4", "memory");
}
static inline void rope_sincos_block8(float * sin8, float * cos8, const float * theta8) {
float sin_fold[ROPE_VEC_WIDTH] __attribute__((aligned(32)));
float cos_fold[ROPE_VEC_WIDTH] __attribute__((aligned(32)));
float sin_sign[ROPE_VEC_WIDTH] __attribute__((aligned(32)));
float cos_sign[ROPE_VEC_WIDTH] __attribute__((aligned(32)));
for (int i = 0; i < ROPE_VEC_WIDTH; ++i) {
float x = theta8[i];
if (x > ROPE_PI || x < -ROPE_PI) {
float cycles = x * ROPE_INV_TWO_PI;
int n = (int) cycles;
if (x < 0.0f) {
n--;
}
x = x - (float) n * ROPE_TWO_PI;
}
{
float y = x;
float s = 1.0f;
if (y > ROPE_PI_OVER_2) {
y = ROPE_PI - y;
} else if (y < -ROPE_PI_OVER_2) {
y = -ROPE_PI - y;
s = -1.0f;
}
sin_fold[i] = y;
sin_sign[i] = s;
}
{
float y = x + ROPE_PI_OVER_2;
if (y > ROPE_PI || y < -ROPE_PI) {
float cycles = y * ROPE_INV_TWO_PI;
int n = (int) cycles;
if (y < 0.0f) {
n--;
}
y = y - (float) n * ROPE_TWO_PI;
}
float s = 1.0f;
if (y > ROPE_PI_OVER_2) {
y = ROPE_PI - y;
} else if (y < -ROPE_PI_OVER_2) {
y = -ROPE_PI - y;
s = -1.0f;
}
cos_fold[i] = y;
cos_sign[i] = s;
}
}
{
const uint64_t saved_mask = rope_ps_enter_fullmask();
rope_poly_sin_block8(sin8, sin_fold);
rope_poly_sin_block8(cos8, cos_fold);
__asm__ volatile(
"flw.ps f0, %[sinv] \n\t"
"flw.ps f1, %[sinsgn] \n\t"
"fmul.ps f2, f0, f1 \n\t"
"fsw.ps f2, %[sout] \n\t"
"flw.ps f3, %[cosv] \n\t"
"flw.ps f4, %[cossgn] \n\t"
"fmul.ps f5, f3, f4 \n\t"
"fsw.ps f5, %[cout] \n\t"
: [sout] "=m"(*(float (*)[ROPE_VEC_WIDTH]) sin8), [cout] "=m"(*(float (*)[ROPE_VEC_WIDTH]) cos8)
: [sinv] "m"(*(const float (*)[ROPE_VEC_WIDTH]) sin8),
[sinsgn] "m"(*(const float (*)[ROPE_VEC_WIDTH]) sin_sign),
[cosv] "m"(*(const float (*)[ROPE_VEC_WIDTH]) cos8),
[cossgn] "m"(*(const float (*)[ROPE_VEC_WIDTH]) cos_sign)
: "f0", "f1", "f2", "f3", "f4", "f5", "memory");
rope_ps_leave_fullmask(saved_mask);
}
}
//------------------------------------------------------------------------------
// Cache build
//------------------------------------------------------------------------------
// scalar fallback for tail / tiny sizes
static inline void rope_yarn_scalar(float theta_extrap,
float freq_scale,
const float corr_dims[2],
int64_t i0,
float ext_factor,
float mscale,
float * cos_theta,
float * sin_theta) {
float theta_interp = freq_scale * theta_extrap;
float theta = theta_interp;
if (ext_factor != 0.0f) {
float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], (int) i0) * ext_factor;
theta = theta_interp * (1.0f - ramp_mix) + theta_extrap * ramp_mix;
mscale *= 1.0f + 0.1f * et_logf(et_fdiv(1.0f, freq_scale));
}
*cos_theta = et_cosf(theta) * mscale;
*sin_theta = et_sinf(theta) * mscale;
}
// Populate cos/sin cache for a given position using running theta product
// Experiment 1:
// - theta construction and YaRN mixing stay scalar
// - actual sin/cos approximation is done in vec8 blocks
static inline void compute_rope_cache(float * cos_cache,
float * sin_cache,
int32_t n_dims,
float theta_scale,
int32_t pos,
const float * freq_factors,
float freq_scale,
const float corr_dims[2],
float ext_factor,
float attn_factor) {
const int32_t half_dims = n_dims / 2;
float theta = 1.0f;
int32_t dim_idx = 0;
for (; dim_idx + ROPE_VEC_WIDTH <= half_dims; dim_idx += ROPE_VEC_WIDTH) {
float theta_block[ROPE_VEC_WIDTH] __attribute__((aligned(32)));
float theta_local = theta;
float mscale = attn_factor;
if (ext_factor != 0.0f) {
mscale *= 1.0f + 0.1f * et_logf(et_fdiv(1.0f, freq_scale));
}
for (int i = 0; i < ROPE_VEC_WIDTH; ++i) {
const int32_t pair_idx = dim_idx + i;
const float ff = freq_factors ? freq_factors[pair_idx] : 1.0f;
const float theta_base = (float) pos * theta_local;
const float theta_extrap = et_fdiv(theta_base, ff);
float theta_interp = freq_scale * theta_extrap;
float theta_mix = theta_interp;
if (ext_factor != 0.0f) {
float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], pair_idx * 2) * ext_factor;
theta_mix = theta_interp * (1.0f - ramp_mix) + theta_extrap * ramp_mix;
}
theta_block[i] = theta_mix;
theta_local *= theta_scale;
}
rope_sincos_block8(&sin_cache[dim_idx], &cos_cache[dim_idx], theta_block);
for (int i = 0; i < ROPE_VEC_WIDTH; ++i) {
sin_cache[dim_idx + i] *= mscale;
cos_cache[dim_idx + i] *= mscale;
}
theta = theta_local;
}
// tail fallback
for (; dim_idx < half_dims; ++dim_idx) {
const float ff = freq_factors ? freq_factors[dim_idx] : 1.0f;
const float theta_base = (float) pos * theta;
rope_yarn_scalar(et_fdiv(theta_base, ff), freq_scale, corr_dims, dim_idx * 2, ext_factor, attn_factor,
&cos_cache[dim_idx], &sin_cache[dim_idx]);
theta *= theta_scale;
}
}
//------------------------------------------------------------------------------
// IMROPE cache build (interleaved multi-modal RoPE for Qwen3VL)
//------------------------------------------------------------------------------
// Builds cos/sin cache with 4 interleaved position channels.
// Each dimension pair selects from {theta_t, theta_h, theta_w, theta_e}
// using a mod-3 sector pattern, matching the CPU reference exactly.
static inline void compute_imrope_cache(float * cos_cache,
float * sin_cache,
int32_t n_dims,
float theta_scale,
int32_t pos_t,
int32_t pos_h,
int32_t pos_w,
int32_t pos_e,
const int32_t sections[4],
const float * freq_factors,
float freq_scale,
const float corr_dims[2],
float ext_factor,
float attn_factor) {
const int32_t half_dims = n_dims / 2;
const int32_t sect_dims = sections[0] + sections[1] + sections[2] + sections[3];
float theta_t = (float) pos_t;
float theta_h = (float) pos_h;
float theta_w = (float) pos_w;
float theta_e = (float) pos_e;
int32_t dim_idx = 0;
for (; dim_idx + ROPE_VEC_WIDTH <= half_dims; dim_idx += ROPE_VEC_WIDTH) {
float theta_block[ROPE_VEC_WIDTH] __attribute__((aligned(32)));
float mscale = attn_factor;
if (ext_factor != 0.0f) {
mscale *= 1.0f + 0.1f * et_logf(et_fdiv(1.0f, freq_scale));
}
for (int i = 0; i < ROPE_VEC_WIDTH; ++i) {
const int32_t pair_idx = dim_idx + i;
const int32_t sector = pair_idx % sect_dims;
const float ff = freq_factors ? freq_factors[pair_idx] : 1.0f;
// Interleaved sector assignment (mod-3 pattern)
float theta;
if (sector % 3 == 1 && sector < 3 * sections[1]) {
theta = theta_h;
} else if (sector % 3 == 2 && sector < 3 * sections[2]) {
theta = theta_w;
} else if (sector % 3 == 0 && sector < 3 * sections[0]) {
theta = theta_t;
} else {
theta = theta_e;
}
const float theta_extrap = et_fdiv(theta, ff);
float theta_interp = freq_scale * theta_extrap;
float theta_mix = theta_interp;
if (ext_factor != 0.0f) {
float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], pair_idx * 2) * ext_factor;
theta_mix = theta_interp * (1.0f - ramp_mix) + theta_extrap * ramp_mix;
}
theta_block[i] = theta_mix;
// All 4 thetas advance every iteration
theta_t *= theta_scale;
theta_h *= theta_scale;
theta_w *= theta_scale;
theta_e *= theta_scale;
}
rope_sincos_block8(&sin_cache[dim_idx], &cos_cache[dim_idx], theta_block);
for (int i = 0; i < ROPE_VEC_WIDTH; ++i) {
sin_cache[dim_idx + i] *= mscale;
cos_cache[dim_idx + i] *= mscale;
}
}
// Scalar tail
for (; dim_idx < half_dims; ++dim_idx) {
const int32_t sector = dim_idx % sect_dims;
const float ff = freq_factors ? freq_factors[dim_idx] : 1.0f;
float theta;
if (sector % 3 == 1 && sector < 3 * sections[1]) {
theta = theta_h;
} else if (sector % 3 == 2 && sector < 3 * sections[2]) {
theta = theta_w;
} else if (sector % 3 == 0 && sector < 3 * sections[0]) {
theta = theta_t;
} else {
theta = theta_e;
}
rope_yarn_scalar(et_fdiv(theta, ff), freq_scale, corr_dims, dim_idx * 2, ext_factor, attn_factor,
&cos_cache[dim_idx], &sin_cache[dim_idx]);
theta_t *= theta_scale;
theta_h *= theta_scale;
theta_w *= theta_scale;
theta_e *= theta_scale;
}
}
//------------------------------------------------------------------------------
// Entry point
//------------------------------------------------------------------------------
int entry_point(struct ggml_et_rope_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return -1;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * src1 = &params->src1;
struct ggml_tensor * src2 = &params->src2;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_I32 || dst->type != GGML_TYPE_F32) {
return -1;
}
const float * src0_data = (const float *) src0->data;
const int32_t * src1_data = (const int32_t *) src1->data;
const float * freq_factors = (src2 && src2->data) ? (const float *) src2->data : NULL;
float * dst_data = (float *) dst->data;
if (!src0_data || !src1_data || !dst_data) {
return -1;
}
#ifdef ET_UBERKERNEL
const size_t src0_bytes = (size_t) src0->ne[0] * src0->ne[1] * src0->ne[2] * src0->ne[3] * src0->nb[0];
const size_t src1_bytes = (size_t) src1->ne[0] * src1->ne[1] * src1->ne[2] * src1->ne[3] * src1->nb[0];
evict_region_past_l2(src0_data, src0_bytes);
evict_region_past_l2(src1_data, src1_bytes);
WAIT_CACHEOPS;
FENCE;
et_barrier(ET_BARRIER_GLOBAL);
#endif
const int64_t head_dim = src0->ne[0];
const int64_t heads = src0->ne[1];
const int64_t seq_len = src0->ne[2];
const int64_t batch = src0->ne[3];
const rope_params_t * rope_params = &params->rope_params;
const int32_t n_dims = rope_params->n_dims;
const float freq_base = rope_params->freq_base;
const float freq_scale = rope_params->freq_scale;
const int32_t mode = rope_params->mode;
if (n_dims <= 0 || n_dims > head_dim || (n_dims & 1) != 0) {
return -1;
}
if (n_dims / 2 > MAX_ROPE_HALF_DIMS) {
return -1;
}
float cos_cache[MAX_ROPE_HALF_DIMS];
float sin_cache[MAX_ROPE_HALF_DIMS];
float corr_dims[2];
rope_yarn_corr_dims(n_dims, rope_params->n_ctx_orig, freq_base, rope_params->beta_fast, rope_params->beta_slow,
corr_dims);
et_barrier(ET_BARRIER_GLOBAL);
// Distribute by individual heads: total = batch * seq_len * heads.
const int64_t total_heads = batch * seq_len * heads;
const int64_t start_wu = (total_heads * thread_id) / num_threads;
const int64_t end_wu = (total_heads * (thread_id + 1)) / num_threads;
if (start_wu >= end_wu) {
return 0;
}
const float theta_scale = et_powf(freq_base, et_fdiv(-2.0f, (float) n_dims));
const int32_t half_dims = n_dims / 2;
const int is_neox = (mode & GGML_ROPE_TYPE_NEOX) != 0;
const int is_imrope = (mode == GGML_ROPE_TYPE_IMROPE);
const int use_neox_rotation = is_neox || is_imrope;
// For IMROPE position cache invalidation: track all 4 channels
int32_t last_pos = -1;
int32_t last_pos_h = -1;
int32_t last_pos_w = -1;
int32_t last_pos_e = -1;
for (int64_t wu = start_wu; wu < end_wu; ++wu) {
const int64_t h = wu % heads;
const int64_t s = (wu / heads) % seq_len;
const int64_t b = wu / (heads * seq_len);
if (is_imrope) {
// IMROPE: src1 layout is [p_t(0..S-1), p_h(0..S-1), p_w(0..S-1), p_e(0..S-1)]
const int32_t pt = src1_data[s] + rope_params->n_past;
const int32_t ph = src1_data[s + seq_len] + rope_params->n_past;
const int32_t pw = src1_data[s + seq_len * 2] + rope_params->n_past;
const int32_t pe = src1_data[s + seq_len * 3] + rope_params->n_past;
if (pt != last_pos || ph != last_pos_h || pw != last_pos_w || pe != last_pos_e) {
compute_imrope_cache(cos_cache, sin_cache, n_dims, theta_scale, pt, ph, pw, pe, rope_params->sections,
freq_factors, freq_scale, corr_dims, rope_params->ext_factor,
rope_params->attn_factor);
last_pos = pt;
last_pos_h = ph;
last_pos_w = pw;
last_pos_e = pe;
}
} else {
const int32_t pos = src1_data[s] + rope_params->n_past;
if (pos != last_pos) {
compute_rope_cache(cos_cache, sin_cache, n_dims, theta_scale, pos, freq_factors, freq_scale, corr_dims,
rope_params->ext_factor, rope_params->attn_factor);
last_pos = pos;
}
}
const float * head_src =
(const float *) ((const char *) src0_data + b * src0->nb[3] + s * src0->nb[2] + h * src0->nb[1]);
float * head_dst = (float *) ((char *) dst_data + b * dst->nb[3] + s * dst->nb[2] + h * dst->nb[1]);
// Copy dimensions beyond n_dims unchanged
for (int64_t d = n_dims; d < head_dim; ++d) {
head_dst[d] = head_src[d];
}
if (use_neox_rotation) {
// NEOX/IMROPE: pairs at (i, i+half_dims)
uint64_t temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
for (int32_t dim_idx = 0; dim_idx < half_dims; dim_idx += 8) {
__asm__ volatile(
"flw.ps f0, %[x0_src] \n\t"
"flw.ps f1, %[x1_src] \n\t"
"flw.ps f2, %[sin_cache] \n\t"
"flw.ps f3, %[cos_cache] \n\t"
"fmul.ps f4, f0, f3 \n\t"
"fmul.ps f5, f0, f2 \n\t"
"fnmsub.ps f4, f1, f2, f4 \n\t"
"fmadd.ps f5, f1, f3, f5 \n\t"
"fsw.ps f4, %[x0_dst] \n\t"
"fsw.ps f5, %[x1_dst] \n\t"
: [x0_dst] "=m"(*(float (*)[8]) & head_dst[dim_idx]), [x1_dst] "=m"(*(float (*)[8]) &
head_dst[dim_idx + half_dims])
: [x0_src] "m"(*(const float (*)[8]) & head_src[dim_idx]),
[x1_src] "m"(*(const float (*)[8]) & head_src[dim_idx + half_dims]),
[sin_cache] "m"(*(const float (*)[8]) & sin_cache[dim_idx]),
[cos_cache] "m"(*(const float (*)[8]) & cos_cache[dim_idx])
: "f0", "f1", "f2", "f3", "f4", "f5", "memory");
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
} else {
// Standard: adjacent pairs (2i, 2i+1)
for (int32_t pair_idx = 0; pair_idx < half_dims; ++pair_idx) {
const int32_t dim_in_head = pair_idx * 2;
const float x0 = head_src[dim_in_head];
const float x1 = head_src[dim_in_head + 1];
head_dst[dim_in_head] = x0 * cos_cache[pair_idx] - x1 * sin_cache[pair_idx];
head_dst[dim_in_head + 1] = x0 * sin_cache[pair_idx] + x1 * cos_cache[pair_idx];
}
}
}
return 0;
}
@@ -0,0 +1,184 @@
//******************************************************************************
// RWKV WKV6 F32 Kernel
//
// Implements the RWKV-6 linear attention recurrence:
// dst = r @ (time_faaaa * (k @ v) + state)
// state = time_decay * state + (k @ v)
//
// For each head h, timestep t, row i:
// kv[j] = v[j] * k[i]
// temp[j] = kv[j] * tf[i] + state[i][j]
// dst[j] += temp[j] * r[i] (accumulated across all i)
// state[i][j] = state[i][j] * td[i] + kv[j]
//
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_rwkv_wkv6_params {
float * k; // src[0]: [S, H, T] key
float * v; // src[1]: [S, H, T] value
float * r; // src[2]: [S, H, T] receptance
float * tf; // src[3]: [S, H] time_faaaa (per-head, not per-token)
float * td; // src[4]: [S, H, T] time_decay
float * state_in; // src[5]: [S*S*H, n_seqs] initial state
float * dst; // [C, T + S*n_seqs] output + state_out
int32_t C; // total channels (S * H)
int32_t H; // number of heads
int32_t S; // head size
int32_t T; // number of tokens
int32_t n_seqs; // number of sequences
};
int entry_point(struct ggml_et_rwkv_wkv6_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
const float * k = params->k;
const float * v = params->v;
const float * r = params->r;
const float * tf = params->tf;
const float * td = params->td;
const float * state_in = params->state_in;
float * dst_data = params->dst;
const int32_t C = params->C;
const int32_t H = params->H;
const int32_t S = params->S;
const int32_t T = params->T;
const int32_t n_seqs = params->n_seqs;
if (!k || !v || !r || !tf || !td || !state_in || !dst_data) {
return -1;
}
const int32_t tps = T / n_seqs; // tokens per sequence
float * state_out = dst_data + C * T;
float zero = 0.0f;
// Tile j by one cache line so each hart's dst/state writes never share
// a 64-B line with another hart's writes (the chip is non-coherent).
// Tiling on j (not i) is required for WKV6 because dst[j] is accumulated
// across i — splitting i across harts would race on dst writes.
// For S=64 this gives 4 tiles per head; for S<16 or odd S we fall back
// to one-hart-per-head (= the original parallelism).
const int32_t j_tile = (S % 16 == 0) ? 16 : S;
const int32_t tiles_per_head = S / j_tile;
const int32_t total_units = H * tiles_per_head;
// Parallelize across (head, j-tile) pairs. The t loop stays inside this
// unit loop so the same hart owns the same column slice of state across
// all timesteps — required for the recurrence to read back its own
// writes without going through L2.
for (int32_t u = thread_id; u < total_units; u += num_threads) {
const int32_t h = u / tiles_per_head;
const int32_t tile = u % tiles_per_head;
const int32_t j_start = tile * j_tile;
const int32_t j_end = j_start + j_tile;
const int32_t h_off = h * S; // offset within C for this head
const int32_t s2d = h * S * S; // offset within state for this head
for (int32_t t = 0; t < T; t++) {
const int32_t seq = t / tps;
const int32_t t_in_seq = t % tps;
const int32_t seq_state = seq * S * C;
const float * s_prev;
float * s_cur = state_out + seq_state + s2d;
if (t_in_seq == 0) {
s_prev = state_in + seq_state + s2d;
} else {
s_prev = s_cur;
}
const int32_t th = t * C + h_off;
// Pointers for this timestep/head
const float * k_ptr = k + th;
const float * v_ptr = v + th;
const float * r_ptr = r + th;
const float * tf_ptr = tf + h_off; // tf is per-head, no t offset
const float * td_ptr = td + th;
// Zero this hart's slice of dst: dst[th + j_start..th + j_end-1]
// WKV6 accumulates dst[j] across all i, so must start from zero
float * dst_row = dst_data + th;
for (int32_t j = j_start; j < j_end; j += 8) {
__asm__ volatile(
"fbc.ps f10, %[z]\n"
"fsw.ps f10, %[dst_vec]\n"
: [dst_vec] "=m"(*(float (*)[8]) & dst_row[j])
: [z] "m"(zero)
: "f10");
}
for (int32_t i = 0; i < S; i++) {
const float * sp_row = s_prev + i * S; // state_prev row i
float * sc_row = s_cur + i * S; // state_cur row i
float k_val = k_ptr[i];
float r_val = r_ptr[i];
float tf_val = tf_ptr[i];
float td_val = td_ptr[i];
// Broadcast k[i], r[i], tf[i], td[i] to vector registers
__asm__ volatile(
"fbc.ps f20, %[kv]\n" // f20 = k[i] broadcast
"fbc.ps f21, %[rv]\n" // f21 = r[i] broadcast
"fbc.ps f22, %[tfv]\n" // f22 = tf[i] broadcast
"fbc.ps f23, %[tdv]\n" // f23 = td[i] broadcast
:
: [kv] "m"(k_val), [rv] "m"(r_val), [tfv] "m"(tf_val), [tdv] "m"(td_val)
: "f20", "f21", "f22", "f23");
for (int32_t j = j_start; j < j_end; j += 8) {
__asm__ volatile(
// Load v[j], state_prev[i][j], dst[j]
"flw.ps f10, %[v_vec]\n" // v[j..j+7]
"flw.ps f11, %[s_vec]\n" // state_prev[i][j..j+7]
"flw.ps f12, %[d_vec]\n" // dst[j..j+7] (accumulated)
// kv = v * k_broadcast
"fmul.ps f13, f10, f20\n" // kv = v * k
// temp = kv * tf_broadcast + state_prev
"fmadd.ps f14, f13, f22, f11\n" // temp = kv * tf + state
// dst[j] += temp * r_broadcast
"fmadd.ps f12, f14, f21, f12\n" // dst += temp * r
"fsw.ps f12, %[d_out]\n" // store updated dst
// state_cur[i][j] = state_prev * td_broadcast + kv
"fmadd.ps f11, f11, f23, f13\n" // state = state * td + kv
"fsw.ps f11, %[s_out]\n" // store new state
: [d_out] "=m"(*(float (*)[8]) & dst_row[j]), [s_out] "=m"(*(float (*)[8]) & sc_row[j])
: [v_vec] "m"(*(const float (*)[8]) & v_ptr[j]), [s_vec] "m"(*(const float (*)[8]) & sp_row[j]),
[d_vec] "m"(*(const float (*)[8]) & dst_row[j])
: "f10", "f11", "f12", "f13", "f14");
}
}
}
}
return 0;
}
@@ -0,0 +1,272 @@
//******************************************************************************
// RWKV WKV7 F32 Kernel
//
// Implements the RWKV-7 linear attention recurrence:
// For each head h, timestep t, row i:
// sa = dot(a, state[i])
// state[i] = state[i] * w + v[i]*k + sa * b
// output[i]= dot(state[i], r)
//
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_rwkv_wkv7_params {
float * r; // [S, H, T] receptance
float * w; // [S, H, T] decay
float * k; // [S, H, T] key
float * v; // [S, H, T] value
float * a; // [S, H, T] bonus gate
float * b; // [S, H, T] bonus key
float * state_in; // [S*S*H, n_seqs] initial state
float * dst; // [C, T + S*n_seqs] output + state_out
int32_t C; // total channels (S * H)
int32_t H; // number of heads
int32_t S; // head size
int32_t T; // number of tokens
int32_t n_seqs; // number of sequences
};
// Horizontal sum of 8-wide vector register f10 -> scalar float
static inline float hsum_f10(void) {
float result;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(result)::"t0", "f1", "f2", "f3", "f4", "f5");
return result;
}
int entry_point(struct ggml_et_rwkv_wkv7_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
const float * r = params->r;
const float * w = params->w;
const float * k = params->k;
const float * v = params->v;
const float * a = params->a;
const float * b = params->b;
const float * state_in = params->state_in;
float * dst_data = params->dst;
const int32_t C = params->C;
const int32_t H = params->H;
const int32_t S = params->S;
const int32_t T = params->T;
const int32_t n_seqs = params->n_seqs;
if (!r || !w || !k || !v || !a || !b || !state_in || !dst_data) {
return -1;
}
const int32_t tps = T / n_seqs; // tokens per sequence
float * state_out = dst_data + C * T;
// Fix #2: hoist w[0..S-1] across the i loop. In the inner j-loop of pass
// 2, w/k/b/r are loop-invariant w.r.t. i but were being reloaded for every
// i value (16 times redundantly after Fix #1). Pinning all four arrays
// would need 32 vector regs (won't fit), so we hoist just w — it's used
// in the critical fmadd chain and lives cleanly in f24-f31, which the
// existing kernel never touches. Saves ~20% of pass-2 load issues.
//
// GCC local register variables: declared as `float` but the underlying
// f-reg holds the wide vector loaded by flw.ps. GCC reserves f24-f31 for
// these variables for the whole function and never generates code that
// touches them on its own, so the upper 7 lanes survive between asm
// blocks. Only used when S == 64 (the RWKV-7 case); other head sizes
// fall through to the original unhoisted path.
register float w_h0 __asm__("f24");
register float w_h1 __asm__("f25");
register float w_h2 __asm__("f26");
register float w_h3 __asm__("f27");
register float w_h4 __asm__("f28");
register float w_h5 __asm__("f29");
register float w_h6 __asm__("f30");
register float w_h7 __asm__("f31");
const int wkv7_fast = (S == 64);
// Tile i by one cache line so each hart's output writes never share a
// 64-B line with another hart's writes (the chip is non-coherent).
// For S=64 this gives 4 tiles per head; for S<16 or odd S we fall back
// to one-hart-per-head (= the original parallelism).
const int32_t i_tile = (S % 16 == 0) ? 16 : S;
const int32_t tiles_per_head = S / i_tile;
const int32_t total_units = H * tiles_per_head;
// Parallelize across (head, i-tile) pairs. The t loop stays inside this
// unit loop so the same hart owns the same state rows across all
// timesteps — required for the recurrence to read back its own writes
// without going through L2.
for (int32_t u = thread_id; u < total_units; u += num_threads) {
const int32_t h = u / tiles_per_head;
const int32_t tile = u % tiles_per_head;
const int32_t i_start = tile * i_tile;
const int32_t i_end = i_start + i_tile;
const int32_t h_off = h * S; // offset within C for this head
const int32_t s2d = h * S * S; // offset within state for this head
for (int32_t t = 0; t < T; t++) {
const int32_t seq = t / tps;
const int32_t t_in_seq = t % tps;
const int32_t seq_state = seq * S * C; // state offset for this sequence
const float * s_prev;
float * s_cur = state_out + seq_state + s2d;
if (t_in_seq == 0) {
s_prev = state_in + seq_state + s2d;
} else {
s_prev = s_cur;
}
// Pointers for this timestep/head
const int32_t th = t * C + h_off;
const float * r_ptr = r + th;
const float * w_ptr = w + th;
const float * k_ptr = k + th;
const float * v_ptr = v + th;
const float * a_ptr = a + th;
const float * b_ptr = b + th;
// Hoist w[0..63] into f24-f31 once per (h, t). These values are
// invariant across the i loop below, so the inner j-unroll can
// reference them by register name and skip the per-i reload.
if (wkv7_fast) {
__asm__ volatile(
"flw.ps f24, 0(%[wp])\n"
"flw.ps f25, 32(%[wp])\n"
"flw.ps f26, 64(%[wp])\n"
"flw.ps f27, 96(%[wp])\n"
"flw.ps f28, 128(%[wp])\n"
"flw.ps f29, 160(%[wp])\n"
"flw.ps f30, 192(%[wp])\n"
"flw.ps f31, 224(%[wp])\n"
: "=f"(w_h0), "=f"(w_h1), "=f"(w_h2), "=f"(w_h3), "=f"(w_h4), "=f"(w_h5), "=f"(w_h6), "=f"(w_h7)
: [wp] "r"(w_ptr));
}
for (int32_t i = i_start; i < i_end; i++) {
const float * sp_row = s_prev + i * S; // state_prev row i
float * sc_row = s_cur + i * S; // state_cur row i
// ----------------------------------------------------------
// Step 1: sa = dot(a, state_prev[i])
// Accumulate in f10
// ----------------------------------------------------------
float zero = 0.0f;
__asm__ volatile("fbc.ps f10, %[z]\n" : : [z] "m"(zero) : "f10");
for (int32_t j = 0; j < S; j += 8) {
__asm__ volatile(
"flw.ps f11, %[a_vec]\n"
"flw.ps f12, %[s_vec]\n"
"fmadd.ps f10, f11, f12, f10\n"
:
: [a_vec] "m"(*(const float (*)[8]) & a_ptr[j]), [s_vec] "m"(*(const float (*)[8]) & sp_row[j])
: "f10", "f11", "f12");
}
float sa = hsum_f10();
// ----------------------------------------------------------
// Step 2: state update + result accumulation
// kv = v[i] * k[j]
// state[j] = state[j] * w[j] + kv + sa * b[j]
// result += state[j] * r[j]
// ----------------------------------------------------------
float v_val = v_ptr[i];
// Broadcast v_val and sa, zero result accumulator (f10)
__asm__ volatile(
"fbc.ps f20, %[vv]\n"
"fbc.ps f21, %[sv]\n"
"fbc.ps f10, %[z]\n"
:
: [vv] "m"(v_val), [sv] "m"(sa), [z] "m"(zero)
: "f10", "f20", "f21");
if (wkv7_fast) {
// Fast path: 8 chunks unrolled, w hoisted to f24-f31.
// Saves one flw per chunk vs the original loop.
#define WKV7_PASS2_CHUNK(j_off, w_var) \
__asm__ volatile( \
"flw.ps f11, %[s_vec]\n" \
"flw.ps f13, %[k_vec]\n" \
"flw.ps f14, %[b_vec]\n" \
"flw.ps f15, %[r_vec]\n" \
"fmul.ps f16, f20, f13\n" \
"fmadd.ps f11, f11, %[w_h], f16\n" \
"fmadd.ps f11, f21, f14, f11\n" \
"fsw.ps f11, %[sc_vec]\n" \
"fmadd.ps f10, f11, f15, f10\n" \
: [sc_vec] "=m"(*(float (*)[8]) & sc_row[j_off]) \
: [s_vec] "m"(*(const float (*)[8]) & sp_row[j_off]), [k_vec] "m"(*(const float (*)[8]) & k_ptr[j_off]), \
[b_vec] "m"(*(const float (*)[8]) & b_ptr[j_off]), [r_vec] "m"(*(const float (*)[8]) & r_ptr[j_off]), \
[w_h] "f"(w_var) \
: "f10", "f11", "f13", "f14", "f15", "f16")
WKV7_PASS2_CHUNK(0, w_h0);
WKV7_PASS2_CHUNK(8, w_h1);
WKV7_PASS2_CHUNK(16, w_h2);
WKV7_PASS2_CHUNK(24, w_h3);
WKV7_PASS2_CHUNK(32, w_h4);
WKV7_PASS2_CHUNK(40, w_h5);
WKV7_PASS2_CHUNK(48, w_h6);
WKV7_PASS2_CHUNK(56, w_h7);
#undef WKV7_PASS2_CHUNK
} else {
for (int32_t j = 0; j < S; j += 8) {
__asm__ volatile(
"flw.ps f11, %[s_vec]\n" // state_prev[j..j+7]
"flw.ps f12, %[w_vec]\n" // w[j..j+7]
"flw.ps f13, %[k_vec]\n" // k[j..j+7]
"flw.ps f14, %[b_vec]\n" // b[j..j+7]
"flw.ps f15, %[r_vec]\n" // r[j..j+7]
"fmul.ps f16, f20, f13\n" // kv = v_broadcast * k
"fmadd.ps f11, f11, f12, f16\n" // state*w + kv
"fmadd.ps f11, f21, f14, f11\n" // + sa*b
"fsw.ps f11, %[sc_vec]\n" // store new state
"fmadd.ps f10, f11, f15, f10\n" // result += new_state * r
: [sc_vec] "=m"(*(float (*)[8]) & sc_row[j])
: [s_vec] "m"(*(const float (*)[8]) & sp_row[j]),
[w_vec] "m"(*(const float (*)[8]) & w_ptr[j]),
[k_vec] "m"(*(const float (*)[8]) & k_ptr[j]),
[b_vec] "m"(*(const float (*)[8]) & b_ptr[j]),
[r_vec] "m"(*(const float (*)[8]) & r_ptr[j])
: "f10", "f11", "f12", "f13", "f14", "f15", "f16");
}
}
dst_data[th + i] = hsum_f10();
}
}
}
return 0;
}
@@ -0,0 +1,94 @@
//******************************************************************************
// Scale F32 Kernel
// dst[i] = src0[i] * scale + bias
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_scale_params {
struct ggml_tensor src0; // F32 input tensor
struct ggml_tensor dst; // F32 output tensor
float scale; // Scale factor
float bias; // Bias (additive offset)
};
int entry_point(struct ggml_et_scale_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
float scale = params->scale;
float bias = params->bias;
// Total elements across all dimensions
const int64_t total_elements = src0->ne[0] * src0->ne[1] * src0->ne[2] * src0->ne[3];
// Cache line = 64 bytes = 16 floats, but vector width = 8 floats
// Parallelize at cache line granularity (16 floats)
const int64_t elements_per_cacheline = 16;
const int64_t total_cachelines = (total_elements + elements_per_cacheline - 1) / elements_per_cacheline;
int64_t cachelines_per_thread = (total_cachelines + num_threads - 1) / num_threads;
int64_t start_cacheline = thread_id * cachelines_per_thread;
int64_t end_cacheline = start_cacheline + cachelines_per_thread;
if (end_cacheline > total_cachelines) {
end_cacheline = total_cachelines;
}
if (start_cacheline >= total_cachelines) {
return 0;
}
int64_t start_elem = start_cacheline * elements_per_cacheline;
int64_t end_elem = end_cacheline * elements_per_cacheline;
if (end_elem > total_elements) {
end_elem = total_elements;
}
unsigned long temp_mask;
__asm__ volatile("mova.x.m %0" : "=r"(temp_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
__asm__ volatile("fbc.ps f20, %[scale_ptr]\n" : : [scale_ptr] "m"(scale) : "f20");
__asm__ volatile("fbc.ps f21, %[bias_ptr]\n" : : [bias_ptr] "m"(bias) : "f21");
for (int64_t i = start_elem; i < end_elem; i += 8) {
__asm__ volatile(
"flw.ps f10, %[src]\n"
"fmadd.ps f10, f10, f20, f21\n" // dst = src*scale + bias
"fsw.ps f10, %[dst_out]\n"
: [dst_out] "=m"(*(float (*)[8]) & dst_data[i])
: [src] "m"(*(const float (*)[8]) & src0_data[i])
: "f10", "f20", "f21");
}
__asm__ volatile("mova.m.x %0" ::"r"(temp_mask));
return 0;
}
+101
View File
@@ -0,0 +1,101 @@
//******************************************************************************
// SET F32 Kernel
// Minimal ET implementation for inplace F32 SET into a contiguous destination
// using a contiguous F32 source view and explicit destination view strides.
//
// Supported shape family:
// - dst/base is contiguous F32
// - src1 is contiguous F32
// - src1.ne[0] is cacheline-aligned (multiple of 16 floats)
// - destination view strides/offset are cacheline-aligned
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_set_params {
struct ggml_tensor src1;
struct ggml_tensor dst;
int32_t nb1;
int32_t nb2;
int32_t nb3;
int32_t offset;
};
static inline void copy_row_aligned(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f11, %[src_vec]\n"
"fsw.ps f11, %[dst_vec]\n"
: [dst_vec] "=m"(*(float (*)[8]) & dst[i])
: [src_vec] "m"(*(const float (*)[8]) & src[i])
: "f11");
}
}
int entry_point(struct ggml_et_set_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src1 = &params->src1;
struct ggml_tensor * dst = &params->dst;
if (src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
const float * src1_data = (const float *) src1->data;
float * dst_data = (float *) dst->data;
if (!src1_data || !dst_data) {
return -1;
}
const int64_t ne10 = src1->ne[0];
const int64_t ne11 = src1->ne[1];
const int64_t ne12 = src1->ne[2];
const int64_t ne13 = src1->ne[3];
if (src1->nb[0] != sizeof(float) || dst->nb[0] != sizeof(float) || ne10 % 16 != 0) {
return -1;
}
const int64_t nb11 = src1->nb[1];
const int64_t nb12 = src1->nb[2];
const int64_t nb13 = src1->nb[3];
const int64_t dnb1 = params->nb1;
const int64_t dnb2 = params->nb2;
const int64_t dnb3 = params->nb3;
const int64_t offset = params->offset;
const int64_t total_rows = ne11 * ne12 * ne13;
for (int64_t row = thread_id; row < total_rows; row += num_threads) {
const int64_t i1 = row % ne11;
const int64_t i2 = (row / ne11) % ne12;
const int64_t i3 = row / (ne11 * ne12);
const float * src_row = (const float *) ((const char *) src1_data + i1 * nb11 + i2 * nb12 + i3 * nb13);
float * dst_row = (float *) ((char *) dst_data + offset + i1 * dnb1 + i2 * dnb2 + i3 * dnb3);
copy_row_aligned(dst_row, src_row, (int32_t) ne10);
}
return 0;
}
@@ -0,0 +1,394 @@
//******************************************************************************
// Bare Metal SET_ROWS F32 Kernel
// Writes source data rows to specific indices in destination tensor
//
// Algorithm:
// 1. Read row indices from src1 (int64 tensor)
// 2. For each source row, write it to destination at the specified index
// 3. Handle type conversion: F32 source -> F32/F16 destination
// 4. Support multi-dimensional tensor operations
//
// Operation: dst[indices[i]] = src[i] for i = 0..num_source_rows
// This is the inverse of GET_ROWS operation
//
// As ET is not a cache coherent processor yet SET_ROWS often are setting
// small mount of large rows (KV cache). There's several strategies to
// optimize this operation, including cacheline-based parallelization.
//
// - distribute work at cacheline granularity
// - if previous does not work, find the LCM of cacheline size
//
// Features supported:
// - F32 source data (always F32 input)
// - F32 and F16 destination data (with transcoding)
// - Int64 row indices (vs Int32 in GET_ROWS)
// - Multi-dimensional tensor support
// - Sequential source reads, scattered destination writes
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#define CACHE_LINE_SIZE_BYTES 64
#define CACHE_LINE_F32_ELEMS 16 // 64 / 4
#define CACHE_LINE_F16_ELEMS 32 // 64 / 2
static int64_t gcd64(int64_t a, int64_t b) {
while (b) {
int64_t t = b;
b = a % b;
a = t;
}
return a;
}
struct ggml_et_set_rows_params {
struct ggml_tensor src0; // F32 source data tensor
struct ggml_tensor src1; // I64 row indices tensor
struct ggml_tensor dst; // F32/F16 destination tensor
};
// Copy exactly one cache line (64 bytes = 16 F32 elements) using wide loads/stores
static void copy_cache_aligned_f32(float * dst, const float * src) {
__asm__ volatile(
"flq2 f0, 0(%[src]) \n\t" // Load 32 bytes
"flq2 f1, 32(%[src]) \n\t" // Load next 32 bytes
"fsq2 f0, 0(%[dst]) \n\t" // Store 32 bytes
"fsq2 f1, 32(%[dst]) \n\t" // Store next 32 bytes
:
: [src] "r"(src), [dst] "r"(dst)
: "f0", "f1", "memory");
}
// Convert and copy one dst cache line worth of F32->F16 (32 elements src -> 64 bytes dst)
static void copy_cache_aligned_f16(uint16_t * dst, const float * src) {
unsigned long mask_temp;
// Build offset vector for consecutive 16-bit stores: [0, 2, 4, 6, 8, 10, 12, 14]
float offset_vec_storage[8];
uint32_t * offsets = (uint32_t *) offset_vec_storage;
for (int j = 0; j < 8; j++) {
offsets[j] = j * 2;
}
__asm__ volatile(
"mova.x.m %[mask_temp] \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
"flw.ps f1, 0(%[offsets]) \n\t"
: [mask_temp] "=&r"(mask_temp)
: [offsets] "r"(offset_vec_storage)
: "f1");
// 4 iterations of 8 elements = 32 F16 elements = 64 bytes = 1 cache line
for (int i = 0; i < 32; i += 8) {
__asm__ volatile(
"flw.ps f2, 0(%[src_ptr]) \n\t"
"fcvt.f16.ps f3, f2 \n\t"
"fsch.ps f3, f1(%[dst_ptr]) \n\t"
:
: [src_ptr] "r"(src + i), [dst_ptr] "r"(dst + i)
: "f2", "f3", "memory");
}
__asm__ volatile("mova.m.x %[mask_temp] \n\t" : : [mask_temp] "r"(mask_temp));
}
static inline size_t tensor_bytes(const struct ggml_tensor * t) {
return (size_t) t->ne[0] * t->ne[1] * t->ne[2] * t->ne[3] * t->nb[0];
}
int entry_point(struct ggml_et_set_rows_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
struct ggml_tensor * src0 = &params->src0; // Source data tensor (F32)
struct ggml_tensor * src1 = &params->src1; // Row indices tensor (I64)
struct ggml_tensor * dst = &params->dst; // Destination tensor (F32/F16)
if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_I64) {
return -1; // Invalid source types
}
if (dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16) {
return -1; // Unsupported destination type
}
float * src0_data = (float *) src0->data;
int64_t * src1_data = (int64_t *) src1->data;
void * dst_data = dst->data;
if (!src0_data || !src1_data || !dst_data) {
return -1; // Null data pointer
}
const int64_t ne00 = src0->ne[0]; // Source columns (row width)
const int64_t ne01 = src0->ne[1]; // Source rows (number of rows to write)
const int64_t ne02 = src0->ne[2]; // Source batch dimension
const int64_t ne03 = src0->ne[3]; // Source outer batch dimension
const int64_t nb01 = src0->nb[1];
const int64_t nb02 = src0->nb[2];
const int64_t nb03 = src0->nb[3];
const int64_t ne10 = src1->ne[0]; // Number of indices in dimension 0
const int64_t ne11 = src1->ne[1]; // Number of indices in dimension 1
const int64_t ne12 = src1->ne[2]; // Batch dimension for indices
const int64_t nb10 = src1->nb[0];
const int64_t nb11 = src1->nb[1];
const int64_t nb12 = src1->nb[2];
const int64_t ne_dst1 = dst->ne[1]; // Number of rows in destination (for bounds checking)
const int64_t nb1 = dst->nb[1];
const int64_t nb2 = dst->nb[2];
const int64_t nb3 = dst->nb[3];
// Validate that number of indices matches number of source rows
if (ne10 != ne01) {
return -1; // Number of indices must match number of source rows
}
#ifdef ET_UBERKERNEL
evict_region_past_l2(params->src0.data, tensor_bytes(&params->src0));
evict_region_past_l2(params->src1.data, tensor_bytes(&params->src1));
FENCE;
et_barrier(ET_BARRIER_GLOBAL);
#endif
const int64_t total_rows = ne01 * ne02 * ne03;
// Determine cache-line element count based on destination type
const int64_t dst_cl_elems = (dst->type == GGML_TYPE_F16) ? CACHE_LINE_F16_ELEMS : CACHE_LINE_F32_ELEMS;
// Check if rows are cache-line aligned in the destination
const bool row_cache_aligned = (ne00 >= dst_cl_elems) && (ne00 % dst_cl_elems == 0);
if (row_cache_aligned) {
// Cache-aligned path: distribute dst cache lines across threads
// Each thread owns complete cache lines -> no coherence conflicts
const int64_t cls_per_row = ne00 / dst_cl_elems;
const int64_t total_cls = total_rows * cls_per_row;
const int64_t cls_per_thread = (total_cls + num_threads - 1) / num_threads;
const int64_t my_start = thread_id * cls_per_thread;
int64_t my_end = my_start + cls_per_thread;
if (my_end > total_cls) {
my_end = total_cls;
}
if (my_start >= total_cls) {
return 0;
}
for (int64_t cl = my_start; cl < my_end; cl++) {
// Map flat cache-line index -> (row, offset within row)
const int64_t row_flat = cl / cls_per_row;
const int64_t cl_in_row = cl % cls_per_row;
// Decompose flat row -> (i03, i02, i01)
const int64_t i01 = row_flat % ne01;
const int64_t tmp = row_flat / ne01;
const int64_t i02 = tmp % ne02;
const int64_t i03 = tmp / ne02;
// Look up destination row index
const int64_t i12 = i03 % ne12;
const int64_t i11 = i02 % ne11;
const int64_t i10 = i01;
const int64_t index_byte_offset = i10 * nb10 + i11 * nb11 + i12 * nb12;
const int64_t dst_row_index = *(int64_t *) ((char *) src1_data + index_byte_offset);
if (dst_row_index < 0 || dst_row_index >= ne_dst1) {
return -1;
}
// Source pointer: row base + cache-line offset (always F32 source)
const int64_t elem_offset = cl_in_row * dst_cl_elems;
const float * src_ptr =
(const float *) ((char *) src0_data + i01 * nb01 + i02 * nb02 + i03 * nb03) + elem_offset;
// Destination pointer: scattered row base + cache-line offset
char * dst_row_base = (char *) dst_data + dst_row_index * nb1 + i02 * nb2 + i03 * nb3;
if (dst->type == GGML_TYPE_F32) {
float * dst_ptr = (float *) dst_row_base + elem_offset;
copy_cache_aligned_f32(dst_ptr, src_ptr);
} else {
uint16_t * dst_ptr = (uint16_t *) dst_row_base + elem_offset;
copy_cache_aligned_f16(dst_ptr, src_ptr);
}
}
} else if (nb1 % CACHE_LINE_SIZE_BYTES == 0) {
// LCM-aligned path: destination row stride is cache-line-aligned, so
// scattered rows never share a cache line even though ne00 doesn't
// fill complete cache lines. Group rows via lcm(ne00, dst_cl_elems)
// and distribute cache lines across threads — each thread exclusively
// owns its cache lines, so normal stores are safe (no atomics needed).
const int64_t g = gcd64(ne00, dst_cl_elems);
const int64_t rows_per_group = dst_cl_elems / g; // lcm / ne00
const int64_t cls_per_group = ne00 / g; // lcm / dst_cl_elems
const int64_t total_groups = (total_rows + rows_per_group - 1) / rows_per_group;
const int64_t total_cls = total_groups * cls_per_group;
const int64_t cls_per_thread = (total_cls + num_threads - 1) / num_threads;
const int64_t my_start = thread_id * cls_per_thread;
int64_t my_end = my_start + cls_per_thread;
if (my_end > total_cls) {
my_end = total_cls;
}
if (my_start >= total_cls) {
return 0;
}
#ifdef BUILD_FOR_UBERKERNEL
et_barrier(ET_BARRIER_GLOBAL);
// evict_region_past_l2(src0_data, tensor_bytes(src0));
// evict_region_past_l2(src1_data, tensor_bytes(src1));
// // et_barrier(ET_BARRIER_GLOBAL);
// FENCE;
#endif
for (int64_t cl = my_start; cl < my_end; cl++) {
const int64_t group_idx = cl / cls_per_group;
const int64_t cl_in_group = cl % cls_per_group;
// Element range [elem_start, elem_end) within the flattened group
const int64_t elem_start = cl_in_group * dst_cl_elems;
const int64_t elem_end = elem_start + dst_cl_elems;
// Which row(s) inside this group does the cache line touch?
const int64_t r_first = elem_start / ne00;
const int64_t r_last = (elem_end - 1) / ne00;
for (int64_t r = r_first; r <= r_last; r++) {
const int64_t row_flat = group_idx * rows_per_group + r;
if (row_flat >= total_rows) {
break;
}
// Column range within this row
int64_t col_begin = (r == r_first) ? (elem_start - r * ne00) : 0;
int64_t col_end = (r == r_last) ? (elem_end - r * ne00) : ne00;
if (col_end > ne00) {
col_end = ne00;
}
// Decompose flat row -> (i03, i02, i01)
const int64_t i01 = row_flat % ne01;
const int64_t tmp = row_flat / ne01;
const int64_t i02 = tmp % ne02;
const int64_t i03 = tmp / ne02;
// Look up destination row index
const int64_t i12 = i03 % ne12;
const int64_t i11 = i02 % ne11;
const int64_t i10 = i01;
const int64_t index_byte_offset = i10 * nb10 + i11 * nb11 + i12 * nb12;
const int64_t dst_row_index = *(int64_t *) ((char *) src1_data + index_byte_offset);
if (dst_row_index < 0 || dst_row_index >= ne_dst1) {
return -1;
}
const float * src_row = (const float *) ((char *) src0_data + i01 * nb01 + i02 * nb02 + i03 * nb03);
char * dst_row_base = (char *) dst_data + dst_row_index * nb1 + i02 * nb2 + i03 * nb3;
// nb1 is cache-line-aligned, so dst_row_base is too.
// Use aligned copy when the column range fills a complete
// cache line at a cache-line-aligned offset within the row.
const bool full_cl = (col_begin % dst_cl_elems == 0) && (col_end - col_begin == dst_cl_elems);
if (dst->type == GGML_TYPE_F32) {
float * dp = (float *) dst_row_base;
if (full_cl) {
copy_cache_aligned_f32(dp + col_begin, src_row + col_begin);
} else {
for (int64_t i = col_begin; i < col_end; i++) {
dp[i] = src_row[i];
}
}
} else {
uint16_t * dp = (uint16_t *) dst_row_base;
if (full_cl) {
copy_cache_aligned_f16(dp + col_begin, src_row + col_begin);
} else {
for (int64_t i = col_begin; i < col_end; i++) {
dp[i] = fp32_to_fp16(src_row[i]);
}
}
}
}
}
#ifdef BUILD_FOR_UBERKERNEL
et_barrier(ET_BARRIER_GLOBAL);
// evict_region_past_l2(src0_data, tensor_bytes(src0));
// evict_region_past_l2(src1_data, tensor_bytes(src1));
// // et_barrier(ET_BARRIER_GLOBAL);
// FENCE;
#endif
} else {
// Fallback: nb1 not cache-line-aligned, so scattered destination rows
// may share a cache line. Use atomic global stores to bypass L1D.
for (int64_t row_flat = thread_id; row_flat < total_rows; row_flat += num_threads) {
const int64_t i01 = row_flat % ne01;
const int64_t tmp = row_flat / ne01;
const int64_t i02 = tmp % ne02;
const int64_t i03 = tmp / ne02;
// Look up destination row index
const int64_t i12 = i03 % ne12;
const int64_t i11 = i02 % ne11;
const int64_t i10 = i01;
const int64_t index_byte_offset = i10 * nb10 + i11 * nb11 + i12 * nb12;
const int64_t dst_row_index = *(int64_t *) ((char *) src1_data + index_byte_offset);
if (dst_row_index < 0 || dst_row_index >= ne_dst1) {
return -1;
}
const float * src_row = (const float *) ((char *) src0_data + i01 * nb01 + i02 * nb02 + i03 * nb03);
char * dst_row_base = (char *) dst_data + dst_row_index * nb1 + i02 * nb2 + i03 * nb3;
if (dst->type == GGML_TYPE_F32) {
volatile float * dst_row = (volatile float *) dst_row_base;
for (int64_t i = 0; i < ne00; i++) {
atomic_store_f32(dst_row + i, src_row[i]);
}
} else {
volatile uint16_t * dst_row = (volatile uint16_t *) dst_row_base;
for (int64_t i = 0; i < ne00; i++) {
atomic_store_f16(dst_row + i, fp32_to_fp16(src_row[i]));
}
}
}
}
#ifdef BUILD_FOR_UBERKERNEL
et_barrier(ET_BARRIER_GLOBAL);
// evict_region_past_l2(src0_data, tensor_bytes(src0));
// evict_region_past_l2(src1_data, tensor_bytes(src1));
// // et_barrier(ET_BARRIER_GLOBAL);
// FENCE;
#endif
return 0;
}
@@ -0,0 +1,698 @@
//******************************************************************************
// Bare Metal Softmax F32 Kernel
// Softmax function: y[i] = exp(x[i] - max) / sum(exp(x[j] - max))
//
// Algorithm:
// 1. Apply scaling: x' = x * scale
// 2. Add mask/bias if present: x' = x' + mask * slope (ALiBi support)
// 3. Find max value for numerical stability: max = max(x')
// 4. Compute exponentials: exp_vals[i] = exp(x'[i] - max)
// 5. Compute sum: sum = sum(exp_vals)
// 6. Normalize: y[i] = exp_vals[i] / sum
//
// Features supported:
// - Temperature scaling via scale parameter
// - Attention masking (transformer masks)
// - ALiBi (Attention with Linear Biases) positional encoding
// - Numerical stability (subtract max before exp)
// - ggml broadcasting rules for mask tensors
//
// Mask Broadcasting Rules (ggml-specific, not standard numpy):
// - Dimension 0: mask.ne[0] == input.ne[0] (exact match required)
// - Dimension 1: mask.ne[1] >= input.ne[1] (allows larger pre-allocated masks)
// - Dimension 2: input.ne[2] % mask.ne[2] == 0 (modulo broadcasting)
// - Dimension 3: input.ne[3] % mask.ne[3] == 0 (modulo broadcasting)
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
// Softmax kernel parameters structure (from ggml-et-ops.h)
struct ggml_et_softmax_params {
struct ggml_tensor src0; // F32 input tensor
struct ggml_tensor src1; // F32 mask tensor (optional, may be zeroed if not used)
struct ggml_tensor src2; // F32 sinks tensor (optional, may be zeroed if not used)
struct ggml_tensor dst; // F32 output tensor
float scale; // Scale factor (temperature scaling)
float max_bias; // Max bias for ALiBi (0.0f if not used)
};
#define LOG2E_F 1.4426950408889634f
typedef struct {
float max_val;
float sum_val;
uint32_t valid_mask;
} softmax_params_t;
static inline bool softmax_lane_is_valid(float x) {
return (x == x) && (x != -INFINITY) && (x != INFINITY);
}
static inline softmax_params_t softmax_params_empty(void) {
softmax_params_t p;
p.max_val = -INFINITY;
p.sum_val = 0.0f;
p.valid_mask = 0;
return p;
}
// chunk_transform_ps_8_branchless_mask
//
// Vector transform for 8 logits:
//
// x = src * scale + (mask ? mask * slope : 0)
//
// Implemented branchlessly so masked and unmasked paths share the same
// instruction stream. Used by pass1 and pass2 vector loops.
static inline void chunk_transform_ps_8_branchless_mask(float * tmp8,
const float * src,
const float * mask,
float scale,
float slope) {
unsigned long ms;
const float zero = 0.0f;
const unsigned long mask_load_m0 = (mask != NULL) ? 0xFFul : 0x00ul;
const float * mp = (mask != NULL) ? mask : &zero;
__asm__ volatile(
"mova.x.m %[ms] \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
"fbc.ps f10, 0(%[p_scale]) \n\t"
"fbc.ps f11, 0(%[p_slope]) \n\t"
"fbc.ps f1, 0(%[p_zero]) \n\t"
"mov.m.x m0, %[maskm0], 0 \n\t" // load mask if needed
"flw.ps f1, 0(%[mp]) \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
"flw.ps f0, 0(%[sp]) \n\t"
"fmul.ps f0, f0, f10 \n\t"
"fmul.ps f1, f1, f11 \n\t"
"fadd.ps f0, f0, f1, rne \n\t"
"fsw.ps f0, 0(%[tp]) \n\t"
"mova.m.x %[ms] \n\t"
: [ms] "=&r"(ms)
: [tp] "r"(tmp8), [sp] "r"(src), [mp] "r"(mp), [p_zero] "r"(&zero), [p_scale] "r"(&scale),
[p_slope] "r"(&slope), [maskm0] "r"(mask_load_m0)
: "f0", "f1", "f10", "f11", "memory");
}
// chunk_transform_ps_8_tail
//
// Same as chunk_transform_ps_8_branchless_mask but gates loads, compute,
// and stores with a caller-supplied m0 mask so that only `count` elements
// (1-7) are touched. Used for the last sub-8 chunk of a non-aligned row.
static inline void chunk_transform_ps_8_tail(float * tmp8,
const float * src,
const float * mask,
float scale,
float slope,
unsigned long tail_m0) {
unsigned long ms;
const float zero = 0.0f;
const unsigned long mask_load_m0 = (mask != NULL) ? tail_m0 : 0x00ul;
const float * mp = (mask != NULL) ? mask : &zero;
__asm__ volatile(
"mova.x.m %[ms] \n\t"
// Broadcast constants with all lanes enabled
"mov.m.x m0, x0, 0xFF \n\t"
"fbc.ps f10, 0(%[p_scale]) \n\t"
"fbc.ps f11, 0(%[p_slope]) \n\t"
"fbc.ps f1, 0(%[p_zero]) \n\t"
// Load mask data gated by tail mask
"mov.m.x m0, %[maskm0], 0 \n\t"
"flw.ps f1, 0(%[mp]) \n\t"
// Load source, compute, and store gated by tail mask
"mov.m.x m0, %[tailm0], 0 \n\t"
"flw.ps f0, 0(%[sp]) \n\t"
"fmul.ps f0, f0, f10 \n\t"
"fmul.ps f1, f1, f11 \n\t"
"fadd.ps f0, f0, f1, rne \n\t"
"fsw.ps f0, 0(%[tp]) \n\t"
"mova.m.x %[ms] \n\t"
: [ms] "=&r"(ms)
: [tp] "r"(tmp8), [sp] "r"(src), [mp] "r"(mp), [p_zero] "r"(&zero), [p_scale] "r"(&scale),
[p_slope] "r"(&slope), [maskm0] "r"(mask_load_m0), [tailm0] "r"(tail_m0)
: "f0", "f1", "f10", "f11", "memory");
}
// softmax_pass1_range
//
// Computes the numerically-stable softmax scan over a sub-range of a row.
//
// This implements the 1st pass of online softmax
//
// max' = max(max, x)
// sum' = sum * exp(old_max - max') + exp(x - max')
//
// and returns a partial result containing:
//
// - max_val : maximum logit observed in this range
// - sum_val : exp-normalized sum relative to max_val
//
// These partial results can be merged with softmax_params_merge() to obtain
// the result for the full row.
static inline softmax_params_t softmax_pass1_range(const float * src,
const float * mask,
int begin,
int end,
float scale,
float slope) {
__attribute__((aligned(32))) float lane_max[8];
__attribute__((aligned(32))) float lane_sum[8];
__attribute__((aligned(32))) float tmp[8];
uint8_t valid_mask = 0;
const float one_f = 1.0f;
const float zero_f = 0.0f;
const float neg_inf = -INFINITY;
const float log2e = LOG2E_F;
unsigned long ms;
__asm__ volatile(
"mova.x.m %[ms] \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
"fbc.ps f20, 0(%[p_ninf]) \n\t"
"fbc.ps f21, 0(%[p_zero]) \n\t"
"fbc.ps f22, 0(%[p_one]) \n\t"
"fbc.ps f23, 0(%[p_log2e]) \n\t"
: [ms] "=&r"(ms)
: [p_ninf] "r"(&neg_inf), [p_zero] "r"(&zero_f), [p_one] "r"(&one_f), [p_log2e] "r"(&log2e)
: "f20", "f21", "f22", "f23");
const int aligned_end = begin + ((end - begin) & ~7);
// Process full 8-element chunks
int i = begin;
for (; i < aligned_end; i += 8) {
chunk_transform_ps_8_branchless_mask(tmp, src + i, mask ? (mask + i) : NULL, scale, slope);
uint8_t cur_mask = 0;
for (int j = 0; j < 8; ++j) {
if (softmax_lane_is_valid(tmp[j])) {
cur_mask |= (uint8_t) (1u << j);
}
}
const uint8_t init_mask = (uint8_t) (cur_mask & ~valid_mask);
const uint8_t upd_mask = (uint8_t) (cur_mask & valid_mask);
if (init_mask || upd_mask) {
__asm__ volatile(
"flw.ps f0, 0(%[p_tmp]) \n\t"
"mov.m.x m0, %[initm], 0 \n\t"
"fcmovm.ps f20, f0, f20 \n\t"
"fcmovm.ps f21, f22, f21 \n\t"
"mov.m.x m0, %[updm], 0 \n\t"
"fmax.ps f1, f20, f0 \n\t"
"fsub.ps f2, f20, f1, rne \n\t"
"fmul.ps f2, f2, f23 \n\t"
"fexp.ps f2, f2 \n\t"
"fsub.ps f3, f0, f1, rne \n\t"
"fmul.ps f3, f3, f23 \n\t"
"fexp.ps f3, f3 \n\t"
"fmul.ps f21, f21, f2 \n\t"
"fadd.ps f21, f21, f3, rne \n\t"
"fcmovm.ps f20, f1, f20 \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
:
: [p_tmp] "r"(tmp), [initm] "r"((unsigned long) init_mask), [updm] "r"((unsigned long) upd_mask)
: "f0", "f1", "f2", "f3", "memory");
valid_mask |= cur_mask;
}
}
// Tail chunk: m0-gated load/compute/store for remaining 1-7 elements
if (i < end) {
const unsigned long tail_m0 = (1ul << (end - i)) - 1;
// Fill tmp with NaN so invalid lanes fail softmax_lane_is_valid
for (int j = 0; j < 8; j++) {
tmp[j] = __builtin_nanf("");
}
chunk_transform_ps_8_tail(tmp, src + i, mask ? (mask + i) : NULL, scale, slope, tail_m0);
uint8_t cur_mask = 0;
for (int j = 0; j < 8; ++j) {
if (softmax_lane_is_valid(tmp[j])) {
cur_mask |= (uint8_t) (1u << j);
}
}
const uint8_t init_mask = (uint8_t) (cur_mask & ~valid_mask);
const uint8_t upd_mask = (uint8_t) (cur_mask & valid_mask);
if (init_mask || upd_mask) {
__asm__ volatile(
"flw.ps f0, 0(%[p_tmp]) \n\t"
"mov.m.x m0, %[initm], 0 \n\t"
"fcmovm.ps f20, f0, f20 \n\t"
"fcmovm.ps f21, f22, f21 \n\t"
"mov.m.x m0, %[updm], 0 \n\t"
"fmax.ps f1, f20, f0 \n\t"
"fsub.ps f2, f20, f1, rne \n\t"
"fmul.ps f2, f2, f23 \n\t"
"fexp.ps f2, f2 \n\t"
"fsub.ps f3, f0, f1, rne \n\t"
"fmul.ps f3, f3, f23 \n\t"
"fexp.ps f3, f3 \n\t"
"fmul.ps f21, f21, f2 \n\t"
"fadd.ps f21, f21, f3, rne \n\t"
"fcmovm.ps f20, f1, f20 \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
:
: [p_tmp] "r"(tmp), [initm] "r"((unsigned long) init_mask), [updm] "r"((unsigned long) upd_mask)
: "f0", "f1", "f2", "f3", "memory");
valid_mask |= cur_mask;
}
}
__asm__ volatile(
"mov.m.x m0, x0, 0xFF \n\t"
"fsw.ps f20, 0(%[p_lmax]) \n\t"
"fsw.ps f21, 0(%[p_lsum]) \n\t"
"mova.m.x %[ms] \n\t"
:
: [p_lmax] "r"(lane_max), [p_lsum] "r"(lane_sum), [ms] "r"(ms)
: "memory");
softmax_params_t out = softmax_params_empty();
out.valid_mask = valid_mask;
for (int k = 0; k < 8; ++k) {
if (valid_mask & (1u << k)) {
if (out.valid_mask == (1u << k) || out.max_val == -INFINITY || lane_max[k] > out.max_val) {
out.max_val = lane_max[k];
}
}
}
if (out.max_val != -INFINITY) {
// Compute lane correction factors via fexp.ps to stay consistent
// with the fexp.ps used inside the online softmax loop above.
// corr[k] = exp2((lane_max[k] - out.max_val) * LOG2E) = exp(lane_max[k] - out.max_val)
const float neg_max_l2 = -out.max_val * LOG2E_F;
__attribute__((aligned(32))) float corr[8];
__asm__ volatile(
"mova.x.m %[ms] \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
"fbc.ps f0, 0(%[p_nml2]) \n\t"
"fbc.ps f2, 0(%[p_l2e]) \n\t"
"flw.ps f1, 0(%[p_lmax]) \n\t"
"fmadd.ps f0, f1, f2, f0 \n\t"
"fexp.ps f0, f0 \n\t"
"fsw.ps f0, 0(%[p_corr]) \n\t"
"mova.m.x %[ms] \n\t"
:
: [p_nml2] "r"(&neg_max_l2), [p_l2e] "r"(&log2e), [p_lmax] "r"(lane_max), [p_corr] "r"(corr), [ms] "r"(ms)
: "f0", "f1", "f2", "memory");
for (int k = 0; k < 8; ++k) {
if (valid_mask & (1u << k)) {
out.sum_val += lane_sum[k] * corr[k];
}
}
}
return out;
}
// Pass 2 (normalize) over [begin, end).
//
// Computes: dst[i] = exp(x[i]*scale + mask[i]*slope - max) / sum
//
// Uses fexp.ps for the numerator; the denominator (params.sum_val) must
// already be fully computed by the caller (pass1 + any sink merge).
static inline void softmax_pass2_range(float * dst,
const float * src,
const float * mask,
int begin,
int end,
float scale,
float slope,
softmax_params_t params) {
const float s2 = scale * LOG2E_F;
const float sl2 = slope * LOG2E_F;
const float neg_ml2 = -params.max_val * LOG2E_F;
const float inv_sum = et_fdiv(1.0f, params.sum_val);
unsigned long ms;
__asm__ volatile(
"mova.x.m %[ms] \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
"fbc.ps f10, 0(%[p_s2]) \n\t"
"fbc.ps f12, 0(%[p_nml2]) \n\t"
"fbc.ps f13, 0(%[p_inv]) \n\t"
: [ms] "=&r"(ms)
: [p_s2] "r"(&s2), [p_nml2] "r"(&neg_ml2), [p_inv] "r"(&inv_sum)
: "f10", "f12", "f13");
const int aligned_end = begin + ((end - begin) & ~7);
if (mask != NULL) {
__asm__ volatile("fbc.ps f11, 0(%[p_sl2]) \n\t" : : [p_sl2] "r"(&sl2) : "f11");
for (int c = begin; c < aligned_end; c += 8) {
__asm__ volatile(
"flw.ps f0, 0(%[sp]) \n\t"
"flw.ps f1, 0(%[mp]) \n\t"
"fmadd.ps f0, f0, f10, f12 \n\t"
"fmadd.ps f0, f1, f11, f0 \n\t"
"fexp.ps f0, f0 \n\t"
"fmul.ps f0, f0, f13 \n\t"
"fsw.ps f0, 0(%[dp]) \n\t"
:
: [sp] "r"(src + c), [mp] "r"(mask + c), [dp] "r"(dst + c)
: "f0", "f1", "memory");
}
// Tail chunk with m0 gating
if (aligned_end < end) {
const unsigned long tail_m0 = (1ul << (end - aligned_end)) - 1;
__asm__ volatile(
"mov.m.x m0, %[tm], 0 \n\t"
"flw.ps f0, 0(%[sp]) \n\t"
"flw.ps f1, 0(%[mp]) \n\t"
"fmadd.ps f0, f0, f10, f12 \n\t"
"fmadd.ps f0, f1, f11, f0 \n\t"
"fexp.ps f0, f0 \n\t"
"fmul.ps f0, f0, f13 \n\t"
"fsw.ps f0, 0(%[dp]) \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
:
: [sp] "r"(src + aligned_end), [mp] "r"(mask + aligned_end), [dp] "r"(dst + aligned_end),
[tm] "r"(tail_m0)
: "f0", "f1", "memory");
}
} else {
for (int c = begin; c < aligned_end; c += 8) {
__asm__ volatile(
"flw.ps f0, 0(%[sp]) \n\t"
"fmadd.ps f0, f0, f10, f12 \n\t"
"fexp.ps f0, f0 \n\t"
"fmul.ps f0, f0, f13 \n\t"
"fsw.ps f0, 0(%[dp]) \n\t"
:
: [sp] "r"(src + c), [dp] "r"(dst + c)
: "f0", "memory");
}
// Tail chunk with m0 gating
if (aligned_end < end) {
const unsigned long tail_m0 = (1ul << (end - aligned_end)) - 1;
__asm__ volatile(
"mov.m.x m0, %[tm], 0 \n\t"
"flw.ps f0, 0(%[sp]) \n\t"
"fmadd.ps f0, f0, f10, f12 \n\t"
"fexp.ps f0, f0 \n\t"
"fmul.ps f0, f0, f13 \n\t"
"fsw.ps f0, 0(%[dp]) \n\t"
"mov.m.x m0, x0, 0xFF \n\t"
:
: [sp] "r"(src + aligned_end), [dp] "r"(dst + aligned_end), [tm] "r"(tail_m0)
: "f0", "memory");
}
}
__asm__ volatile("mova.m.x %[ms] \n\t" ::[ms] "r"(ms));
}
// Single-core row path.
// pass1_range and pass2_range handle non-8-aligned cols internally via
// m0-gated tail chunks, so this function just passes cols directly.
static inline void compute_softmax_row(float * dst,
const float * src,
const float * mask,
int cols,
float scale,
float slope,
float sink_value,
bool use_sinks) {
softmax_params_t params = softmax_pass1_range(src, mask, 0, cols, scale, slope);
if (use_sinks) {
// For sinks, use fully scalar et_expf to match the reference CPU
// backend's expf precision. Sink tests use small arrays (ne<=32)
// so the scalar path has negligible performance impact.
float max_val = params.max_val;
if (sink_value > max_val) {
max_val = sink_value;
}
// Compute sum = Σ exp(x'[i] - max) + exp(sink - max) (scalar)
float sum = 0.0f;
for (int i = 0; i < cols; ++i) {
float x = src[i] * scale;
if (mask != NULL) {
x += mask[i] * slope;
}
sum += et_expf(x - max_val);
}
sum += et_expf(sink_value - max_val);
// Normalize: dst[i] = exp(x'[i] - max) / sum (scalar)
float inv_sum = et_fdiv(1.0f, sum);
for (int i = 0; i < cols; ++i) {
float x = src[i] * scale;
if (mask != NULL) {
x += mask[i] * slope;
}
dst[i] = et_expf(x - max_val) * inv_sum;
}
} else {
if (!params.valid_mask) {
return;
}
softmax_pass2_range(dst, src, mask, 0, cols, scale, slope, params);
}
}
// Main entry point for Softmax kernel
int entry_point(struct ggml_et_softmax_params * params, void * env) {
// Cast env to proper type
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
// Validate environment pointer
if (!kernel_env) {
return -1;
}
// Get thread info using shire mask from environment
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
// Return early if this hart is not active
if (thread_id < 0) {
return 0;
}
// Basic safety check on params
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
// Extract tensor references
struct ggml_tensor * src0 = &params->src0; // Input tensor
struct ggml_tensor * src1 = &params->src1; // Mask tensor (optional)
struct ggml_tensor * src2 = &params->src2; // Sinks tensor (optional)
struct ggml_tensor * dst = &params->dst; // Output tensor
float scale = params->scale; // Scale factor
float max_bias = params->max_bias; // ALiBi max bias
// Validate tensor types (F32 only)
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1; // Unsupported type combination
}
// Check if mask is used and validate type
bool use_mask = (src1->data != NULL && (src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16));
bool use_sinks = (src2->data != NULL && src2->type == GGML_TYPE_F32);
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
float * mask_data = use_mask ? (float *) src1->data : NULL;
float * sinks_data = use_sinks ? (float *) src2->data : NULL;
if (!src0_data || !dst_data) {
return -1; // Null data pointer
}
const int64_t ne00 = src0->ne[0]; // Sequence length (columns)
const int64_t ne01 = src0->ne[1]; // Number of rows
const int64_t ne02 = src0->ne[2]; // Batch/head dimension
const int64_t ne03 = src0->ne[3]; // Outer batch dimension
// Fast path: softmax of a single element is always 1.0
// (exp(x) / exp(x) == 1 for any x, regardless of scale/mask/bias)
// Skip all ALiBi, mask, and sink setup.
//
// Each output element is 4 bytes. A cache line is 64 bytes = 16 floats.
// L1 is not coherent across harts, so each thread must own whole cache
// lines to avoid cross-hart conflicts.
if (ne00 == 1) {
const int64_t total_elems = ne01 * ne02 * ne03;
const int64_t elems_per_cl = ET_CACHE_LINE_SIZE_BYTES / (int64_t) sizeof(float); // 16
const int64_t total_cls = (total_elems + elems_per_cl - 1) / elems_per_cl;
for (int64_t cl = thread_id; cl < total_cls; cl += num_threads) {
const int64_t start = cl * elems_per_cl;
int64_t end = start + elems_per_cl;
if (end > total_elems) {
end = total_elems;
}
for (int64_t idx = start; idx < end; idx++) {
dst_data[idx] = 1.0f;
}
}
return 0;
}
const int64_t ne10 = use_mask ? src1->ne[0] : 0; // Mask sequence length
const int64_t ne11 = use_mask ? src1->ne[1] : 0; // Mask rows
const int64_t ne12 = use_mask ? src1->ne[2] : 0; // Mask batch/head dimension
const int64_t ne13 = use_mask ? src1->ne[3] : 0; // Mask outer batch dimension
if (use_mask) {
// - Dimension 0: mask must equal input exactly
// - Dimension 1: mask must be >= input (allows larger pre-allocated masks)
// - Dimension 2: input must be divisible by mask (modulo broadcasting)
// - Dimension 3: input must be divisible by mask (modulo broadcasting)
if (ne10 != ne00 || // Dimension 0: exact match required
ne11 < ne01 || // Dimension 1: mask >= input
(ne12 > 0 && ne02 % ne12 != 0) || // Dimension 2: input % mask == 0
(ne13 > 0 && ne03 % ne13 != 0)) { // Dimension 3: input % mask == 0
return -1; // Incompatible dimensions for ggml softmax broadcasting
}
}
// ALiBi slope calculation - compute per attention head
const uint32_t n_head = (uint32_t) ne02;
uint32_t n_head_log2 = 0;
float m0 = 1.0f;
float m1 = 1.0f;
if (max_bias > 0.0f) {
// This is equivalent to: 1 << floor(log2(n_head))
n_head_log2 = 1;
while (n_head_log2 < n_head) {
n_head_log2 <<= 1;
}
if (n_head_log2 > n_head) {
n_head_log2 >>= 1;
}
// Compute base slopes for ALiBi
// m0 = 2^(-max_bias / n_head_log2)
// m1 = 2^(-max_bias / (2 * n_head_log2))
float inv_n_head_log2 = et_fdiv(1.0f, (float) n_head_log2);
m0 = et_expf(-max_bias * 0.69314718f * inv_n_head_log2); // 0.69314718 = ln(2)
m1 = et_expf(-max_bias * 0.69314718f * inv_n_head_log2 * 0.5f);
}
// Process tensor row by row in parallel across flattened rows.
// Flattened row index spans [i03, i02, i01] with row length ne00.
//
// When ne00 * sizeof(float) is not a multiple of the cache line size,
// adjacent rows share cache lines. Assign contiguous write groups to
// each thread so every thread's write footprint covers whole cache
// lines, preventing cross-hart L1 coherency issues. When rows ARE
// cache-line aligned, rows_per_wg == 1 and this degenerates to the
// original stride-by-num_threads distribution.
const int64_t rows_per_i03 = ne02 * ne01;
const int64_t total_rows = ne03 * rows_per_i03;
const int64_t rows_per_wg = et_rows_per_cacheline_group(ne00, sizeof(float));
const int64_t total_wgs = (total_rows + rows_per_wg - 1) / rows_per_wg;
for (int64_t wg = thread_id; wg < total_wgs; wg += num_threads) {
const int64_t row_start = wg * rows_per_wg;
int64_t row_end = row_start + rows_per_wg;
if (row_end > total_rows) {
row_end = total_rows;
}
for (int64_t row = row_start; row < row_end; row++) {
const int64_t i03 = row / rows_per_i03;
const int64_t rem = row % rows_per_i03;
const int64_t i02 = rem / ne01;
const int64_t i01 = rem % ne01;
// Calculate ALiBi slope for this attention head
float slope = 1.0f;
if (max_bias > 0.0f) {
const uint32_t h = (uint32_t) i02; // head index
if (h < n_head_log2) {
slope = m0;
for (uint32_t i = 0; i < h; i++) {
slope *= m0;
}
} else {
const uint32_t exp = 2 * (h - n_head_log2) + 1;
slope = m1;
for (uint32_t i = 1; i < exp; i++) {
slope *= m1;
}
}
}
float sink_value = 0.0f;
if (use_sinks && sinks_data) {
sink_value = sinks_data[i02];
}
const int64_t src_offset = i03 * ne02 * ne01 * ne00 + i02 * ne01 * ne00 + i01 * ne00;
const float * src_row = src0_data + src_offset;
float * dst_row = dst_data + src_offset;
const float * mask_row = NULL;
if (use_mask && mask_data) {
const int64_t mask_i03 = (ne13 > 0) ? i03 % ne13 : 0;
const int64_t mask_i02 = (ne12 > 0) ? i02 % ne12 : 0;
const int64_t mask_i01 = i01;
const int64_t mask_offset = mask_i03 * ne12 * ne11 * ne10 + mask_i02 * ne11 * ne10 + mask_i01 * ne10;
mask_row = mask_data + mask_offset;
}
compute_softmax_row(dst_row, src_row, mask_row, (int) ne00, scale, slope, sink_value, use_sinks);
}
}
return 0; // Success
}
@@ -0,0 +1,109 @@
//******************************************************************************
// Solve Triangular F32 Kernel
// Forward substitution: solve AX = B where A is lower-triangular.
//
// src0 (A): [n, n, B1, B2] lower-triangular matrix
// src1 (B): [k, n, B1, B2] right-hand side
// dst (X): [k, n, B1, B2] solution
//
// For each column j (parallelized across threads):
// For i = 0..n-1:
// X[i,j] = (B[i,j] - dot(A[i,0..i-1], X[0..i-1,j])) / A[i,i]
//
// Lower-triangular, left-side, non-unit variant implemented.
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_solve_tri_params {
struct ggml_tensor src0; // A: lower-triangular [n, n, B1, B2]
struct ggml_tensor src1; // B: RHS [k, n, B1, B2]
struct ggml_tensor dst; // X: solution [k, n, B1, B2]
};
int entry_point(struct ggml_et_solve_tri_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0; // A
struct ggml_tensor * src1 = &params->src1; // B
struct ggml_tensor * dst = &params->dst; // X
if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
const float * A_data = (const float *) src0->data;
const float * B_data = (const float *) src1->data;
float * X_data = (float *) dst->data;
if (!A_data || !B_data || !X_data) {
return -1;
}
const int64_t n = src0->ne[1]; // A is n×n
const int64_t k = src1->ne[0]; // number of RHS columns
const int64_t ne2 = src0->ne[2];
const int64_t ne3 = src0->ne[3];
// Strides in bytes
const size_t nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
const size_t nb11 = src1->nb[1], nb12 = src1->nb[2], nb13 = src1->nb[3];
const size_t nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
// k % 16 == 0 guaranteed by supports_op. Rows are cache-line aligned,
// so column groups of 16 map to exclusive cache lines.
// TODO: Vectorize the thing
const int64_t cols_per_cl = 16;
const int64_t num_col_groups = k / cols_per_cl;
const int64_t total_work = num_col_groups * ne2 * ne3;
for (int64_t work = thread_id; work < total_work; work += num_threads) {
const int64_t cg = work % num_col_groups;
const int64_t i2 = (work / num_col_groups) % ne2;
const int64_t i3 = work / (num_col_groups * ne2);
const int64_t j_start = cg * cols_per_cl;
const int64_t j_end = j_start + cols_per_cl;
const float * A_batch = (const float *) ((const char *) A_data + i2 * nb02 + i3 * nb03);
const float * B_batch = (const float *) ((const char *) B_data + i2 * nb12 + i3 * nb13);
float * X_batch = (float *) ((char *) X_data + i2 * nb2 + i3 * nb3);
for (int64_t j = j_start; j < j_end; j++) {
for (int64_t i = 0; i < n; i++) {
const float * A_row = (const float *) ((const char *) A_batch + i * nb01);
float * X_row = (float *) ((char *) X_batch + i * nb1);
const float * B_row = (const float *) ((const char *) B_batch + i * nb11);
float sum = 0.0f;
for (int64_t t = 0; t < i; t++) {
const float * X_t = (const float *) ((const char *) X_batch + t * nb1);
sum += A_row[t] * X_t[j];
}
X_row[j] = et_fdiv(B_row[j] - sum, A_row[i]);
}
}
}
return 0;
}
+88
View File
@@ -0,0 +1,88 @@
//******************************************************************************
// SQR F32 Kernel
// Element-wise square: y[i] = x[i] * x[i]
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
// SQR kernel parameters structure (unary op: src0 -> dst)
struct ggml_et_sqr_params {
struct ggml_tensor src0; // F32 input tensor
struct ggml_tensor dst; // F32 output tensor
};
int entry_point(struct ggml_et_sqr_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1; // Invalid pointer
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1; // Unsupported type combination
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1; // Null data pointer
}
// Both src and dst are contiguous F32: flatten and distribute by cache lines
const int64_t total_elements = dst->ne[0] * dst->ne[1] * dst->ne[2] * dst->ne[3];
const int64_t elements_per_cacheline = 16; // 64 bytes / 4 bytes per float
const int64_t total_cachelines = (total_elements + elements_per_cacheline - 1) / elements_per_cacheline;
const int64_t cl_per_thread = (total_cachelines + num_threads - 1) / num_threads;
const int64_t cl_start = thread_id * cl_per_thread;
int64_t cl_end = cl_start + cl_per_thread;
if (cl_end > total_cachelines) {
cl_end = total_cachelines;
}
if (cl_start >= total_cachelines) {
return 0;
}
const int64_t elem_start = cl_start * elements_per_cacheline;
int64_t elem_end = cl_end * elements_per_cacheline;
if (elem_end > total_elements) {
elem_end = total_elements;
}
const float * src_ptr = src0_data + elem_start;
float * dst_ptr = dst_data + elem_start;
const int32_t count = (int32_t) (elem_end - elem_start);
// Process 8 elements at a time: dst[i] = src[i] * src[i]
for (int32_t i0 = 0; i0 < count; i0 += 8) {
__asm__ volatile(
"flw.ps f10, %[x_vec]\n" // Load 8 input values
"fmul.ps f11, f10, f10\n" // x * x (8-wide)
"fsw.ps f11, %[result]\n" // Store 8 results
: [result] "=m"(*(float (*)[8]) & dst_ptr[i0])
: [x_vec] "m"(*(const float (*)[8]) & src_ptr[i0])
: "f10", "f11");
}
return 0;
}
@@ -0,0 +1,129 @@
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_ssm_conv_params {
struct ggml_tensor src0; // conv_x: [d_conv - 1 + n_t, d_inner, n_seqs]
struct ggml_tensor src1; // conv1d.weight: [d_conv, d_inner]
struct ggml_tensor dst; // output: [d_inner, n_t, n_seqs]
};
int entry_point(struct ggml_et_ssm_conv_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * src1 = &params->src1;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
const float * src0_data = (const float *) src0->data;
const float * src1_data = (const float *) src1->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !src1_data || !dst_data) {
return -1;
}
const int64_t nc = src1->ne[0];
const int64_t ncs = src0->ne[0];
const int64_t nr = src0->ne[1];
const int64_t n_t = dst->ne[1];
const int64_t n_s = dst->ne[2];
if (dst->ne[0] != nr || src1->ne[1] != nr || ncs != nc - 1 + n_t || src0->nb[0] != sizeof(float) ||
src1->nb[0] != sizeof(float) || dst->nb[0] != sizeof(float) || src0->nb[1] != (size_t) ncs * sizeof(float) ||
src1->nb[1] != (size_t) nc * sizeof(float)) {
return -1;
}
// Parallelize over d_inner in cache-line-aligned chunks (16 floats = 64B)
const int64_t chunk = 16;
const int64_t n_chunks = (nr + chunk - 1) / chunk;
// Save and set vector mask to all 8 lanes
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
for (int64_t i3 = 0; i3 < n_s; ++i3) {
for (int64_t i2 = 0; i2 < n_t; ++i2) {
const float * s = (const float *) ((const char *) src0_data + i2 * src0->nb[0] + i3 * src0->nb[2]);
float * x = (float *) ((char *) dst_data + i2 * dst->nb[1] + i3 * dst->nb[2]);
for (int64_t ci = thread_id; ci < n_chunks; ci += num_threads) {
const int64_t i1_start = ci * chunk;
const int64_t i1_end = i1_start + chunk < nr ? i1_start + chunk : nr;
// Process 8 channels at a time with SIMD
int64_t i1 = i1_start;
for (; i1 + 8 <= i1_end; i1 += 8) {
// Gather 8 channels' data into contiguous buffers for each tap
float tmp_s[8], tmp_c[8];
float acc[8] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
for (int64_t i0 = 0; i0 < nc; ++i0) {
// TODO: Some way to get rid of this gather
for (int j = 0; j < 8; ++j) {
tmp_s[j] = s[(i1 + j) * ncs + i0];
tmp_c[j] = src1_data[(i1 + j) * nc + i0];
}
__asm__ volatile(
"flw.ps f10, %[acc]\n"
"flw.ps f11, %[sv]\n"
"flw.ps f12, %[cv]\n"
"fmadd.ps f10, f11, f12, f10\n"
"fsw.ps f10, %[out]\n"
: [out] "=m"(*(float (*)[8]) acc)
: [acc] "m"(*(const float (*)[8]) acc), [sv] "m"(*(const float (*)[8]) tmp_s),
[cv] "m"(*(const float (*)[8]) tmp_c)
: "f10", "f11", "f12");
}
// Store 8 results — dst is contiguous along d_inner
__asm__ volatile(
"flw.ps f10, %[acc]\n"
"fsw.ps f10, %[dst]\n"
: [dst] "=m"(*(float (*)[8])(x + i1))
: [acc] "m"(*(const float (*)[8]) acc)
: "f10");
}
// Scalar tail for remaining channels
for (; i1 < i1_end; ++i1) {
const float * c = src1_data + i1 * nc;
const float * s_row = s + i1 * ncs;
float sumf = 0.0f;
for (int64_t i0 = 0; i0 < nc; ++i0) {
sumf += s_row[i0] * c[i0];
}
x[i1] = sumf;
}
}
}
}
// Restore mask
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
return 0;
}
@@ -0,0 +1,271 @@
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_ssm_scan_params {
struct ggml_tensor src0; // s: [d_state, head_dim, n_head, n_seqs]
struct ggml_tensor src1; // x: [head_dim, n_head, n_seq_tokens, n_seqs]
struct ggml_tensor src2; // dt: [n_head, n_seq_tokens, n_seqs]
struct ggml_tensor src3; // A: [d_state, n_head] or [1, n_head]
struct ggml_tensor src4; // B: [d_state, n_group, n_seq_tokens, n_seqs]
struct ggml_tensor src5; // C: [d_state, n_group, n_seq_tokens, n_seqs]
struct ggml_tensor src6; // ids: [n_seqs] i32
struct ggml_tensor dst; // packed [y, final_state]
};
static inline float softplus_f32(float x) {
return x <= 20.0f ? et_logf(1.0f + et_expf(x)) : x;
}
int entry_point(struct ggml_et_ssm_scan_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
const int thread_id = get_relative_thread_id(kernel_env->shire_mask);
const int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * src1 = &params->src1;
struct ggml_tensor * src2 = &params->src2;
struct ggml_tensor * src3 = &params->src3;
struct ggml_tensor * src4 = &params->src4;
struct ggml_tensor * src5 = &params->src5;
struct ggml_tensor * src6 = &params->src6;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || src1->type != GGML_TYPE_F32 || src2->type != GGML_TYPE_F32 ||
src3->type != GGML_TYPE_F32 || src4->type != GGML_TYPE_F32 || src5->type != GGML_TYPE_F32 ||
src6->type != GGML_TYPE_I32 || dst->type != GGML_TYPE_F32) {
return -1;
}
const float * s_data = (const float *) src0->data;
const float * x_data = (const float *) src1->data;
const float * dt_data = (const float *) src2->data;
const float * A_data = (const float *) src3->data;
const float * B_data = (const float *) src4->data;
const float * C_data = (const float *) src5->data;
const int32_t * ids = (const int32_t *) src6->data;
float * dst_data = (float *) dst->data;
if (!s_data || !x_data || !dt_data || !A_data || !B_data || !C_data || !ids || !dst_data) {
return -1;
}
const int64_t d_state = src0->ne[0];
const int64_t head_dim = src0->ne[1];
const int64_t n_head = src1->ne[1];
const int64_t n_group = src4->ne[1];
const int64_t n_seq_tokens = src1->ne[2];
const int64_t n_seqs = src1->ne[3];
const int64_t y_elems = src1->ne[0] * src1->ne[1] * src1->ne[2] * src1->ne[3];
if (src0->nb[0] != sizeof(float) || src1->nb[0] != sizeof(float) || src2->nb[0] != sizeof(float) ||
src3->nb[0] != sizeof(float) || src4->nb[0] != sizeof(float) || src5->nb[0] != sizeof(float) ||
src6->nb[0] != sizeof(int32_t) || dst->nb[0] != sizeof(float)) {
return -1;
}
if (n_group <= 0 || n_head % n_group != 0) {
return -1;
}
// Cache-line bundling on the dst output (1 dst float per (head, dim, token)).
// - When head_dim < 16: bundle 16/head_dim heads per work-unit (1 line of dst).
// - When head_dim >= 16: each head's dim slice spans head_dim/16 lines, so we
// can split dims into chunks of 16 across threads without false sharing.
const int64_t dst_lanes_per_cl = 16;
const int64_t heads_per_cacheline = head_dim >= dst_lanes_per_cl ? 1 : (dst_lanes_per_cl / head_dim);
const int64_t heads_per_block = heads_per_cacheline > 0 ? heads_per_cacheline : 1;
const int64_t blocks_per_seq = (n_head + heads_per_block - 1) / heads_per_block;
const int64_t dim_chunk_lanes = head_dim >= dst_lanes_per_cl ? dst_lanes_per_cl : head_dim;
const int64_t dim_chunks_per_head = (head_dim + dim_chunk_lanes - 1) / dim_chunk_lanes;
// A "unit" = (seq, head_block, dim_chunk). This expands the parallelism by a
// factor of dim_chunks_per_head over the prior block-only scheme; for Mamba-2
// shapes (head_dim=64) that's a 4x bump in active threads.
const int64_t units_per_seq = blocks_per_seq * dim_chunks_per_head;
const int64_t total_units = n_seqs * units_per_seq;
const int64_t units_per_thread = (total_units + num_threads - 1) / num_threads;
const int64_t unit_begin = (int64_t) thread_id * units_per_thread;
int64_t unit_end = unit_begin + units_per_thread;
if (unit_begin >= total_units) {
return 0;
}
if (unit_end > total_units) {
unit_end = total_units;
}
const int A_broadcast = (src3->ne[0] == 1);
const int64_t d_state_vec = (d_state / 8) * 8; // largest multiple of 8 <= d_state
const float log2e_const = 1.4426950408889634f;
for (int64_t unit = unit_begin; unit < unit_end; ++unit) {
const int64_t seq_idx = unit / units_per_seq;
const int64_t unit_in_seq = unit % units_per_seq;
const int64_t block_in_seq = unit_in_seq / dim_chunks_per_head;
const int64_t dim_chunk_idx = unit_in_seq % dim_chunks_per_head;
const int64_t head_begin = block_in_seq * heads_per_block;
int64_t head_end = head_begin + heads_per_block;
if (head_end > n_head) {
head_end = n_head;
}
const int64_t dim_begin = dim_chunk_idx * dim_chunk_lanes;
int64_t dim_end = dim_begin + dim_chunk_lanes;
if (dim_end > head_dim) {
dim_end = head_dim;
}
const int32_t state_seq = ids[seq_idx];
for (int64_t head_idx = head_begin; head_idx < head_end; ++head_idx) {
const int64_t group_idx = head_idx / (n_head / n_group);
// A pointer for this head: contiguous over state_idx when not broadcast
const float * A_row = (const float *) ((const char *) A_data + (size_t) head_idx * src3->nb[1]);
for (int64_t dim_idx = dim_begin; dim_idx < dim_end; ++dim_idx) {
const float * state_src =
(const float *) ((const char *) s_data + (size_t) dim_idx * src0->nb[1] +
(size_t) head_idx * src0->nb[2] + (size_t) state_seq * src0->nb[3]);
float * state_dst =
(float *) ((char *) dst_data + (size_t) y_elems * sizeof(float) + (size_t) dim_idx * src0->nb[1] +
(size_t) head_idx * src0->nb[2] + (size_t) seq_idx * src0->nb[3]);
for (int64_t token_idx = 0; token_idx < n_seq_tokens; ++token_idx) {
const float * x_ptr =
(const float *) ((const char *) x_data + (size_t) dim_idx * src1->nb[0] +
(size_t) head_idx * src1->nb[1] + (size_t) token_idx * src1->nb[2] +
(size_t) seq_idx * src1->nb[3]);
const float * dt_ptr =
(const float *) ((const char *) dt_data + (size_t) head_idx * src2->nb[0] +
(size_t) token_idx * src2->nb[1] + (size_t) seq_idx * src2->nb[2]);
const float * B_row =
(const float *) ((const char *) B_data + (size_t) group_idx * src4->nb[1] +
(size_t) token_idx * src4->nb[2] + (size_t) seq_idx * src4->nb[3]);
const float * C_row =
(const float *) ((const char *) C_data + (size_t) group_idx * src5->nb[1] +
(size_t) token_idx * src5->nb[2] + (size_t) seq_idx * src5->nb[3]);
const float dt_softplus = softplus_f32(*dt_ptr);
const float x_dt = (*x_ptr) * dt_softplus;
const float dt_log2e = dt_softplus * log2e_const;
// Source of "previous state" for this token: input state on token 0,
// last token's state thereafter (we wrote it into state_dst).
const float * prev_row = (token_idx == 0) ? state_src : state_dst;
float sumf = 0.0f;
int64_t state_idx = 0;
if (d_state_vec > 0) {
// Save mask, enable all 8 vector lanes for the state loop.
unsigned long saved_mask;
__asm__ volatile("mova.x.m %0" : "=r"(saved_mask));
__asm__ volatile("mov.m.x m0, x0, 0xFF");
// Per-token broadcasts:
// f20 = x_dt (B*x_dt)
// f21 = dt_log2e (for fexp.ps when A is per-state)
// f22 = dA (only when A is broadcast scalar)
// f23 = sum-of-products accumulator (zeroed)
__asm__ volatile(
"fbc.ps f20, %[xdt]\n\t"
"fbc.ps f21, %[dtl]\n\t"
"fbci.pi f23, 0\n\t"
:
: [xdt] "m"(x_dt), [dtl] "m"(dt_log2e)
: "f20", "f21", "f23");
if (A_broadcast) {
// dA is a per-head scalar — compute once and splat.
const float dA_scalar = et_expf(dt_softplus * (*A_row));
__asm__ volatile("fbc.ps f22, %[da]\n\t" : : [da] "m"(dA_scalar) : "f22");
}
for (; state_idx < d_state_vec; state_idx += 8) {
if (!A_broadcast) {
// f22 = exp(dt_softplus * A[state..state+7])
// = 2^((dt_softplus * A) * log2e) via fexp.ps
__asm__ volatile(
"flw.ps f24, %[av]\n\t"
"fmul.ps f24, f24, f21\n\t" // A * dt_log2e
"fexp.ps f22, f24\n\t" // dA = 2^(...)
:
: [av] "m"(*(const float (*)[8]) & A_row[state_idx])
: "f22", "f24");
}
// state = prev * dA + B * x_dt
// sumf += state * C
// Reads prev before writing state_dst — safe even when
// prev_row == state_dst (write-after-read, same index).
__asm__ volatile(
"flw.ps f25, %[prev]\n\t"
"flw.ps f26, %[bv]\n\t"
"flw.ps f27, %[cv]\n\t"
"fmul.ps f26, f26, f20\n\t" // B * x_dt
"fmadd.ps f25, f25, f22, f26\n\t" // state = prev*dA + B*x_dt
"fsw.ps f25, %[sd]\n\t"
"fmadd.ps f23, f25, f27, f23\n\t" // sum += state*C
: [sd] "=m"(*(float (*)[8]) & state_dst[state_idx])
: [prev] "m"(*(const float (*)[8]) & prev_row[state_idx]),
[bv] "m"(*(const float (*)[8]) & B_row[state_idx]),
[cv] "m"(*(const float (*)[8]) & C_row[state_idx])
: "f25", "f26", "f27");
}
// Horizontal reduce f23 (8 lanes) -> scalar sumf.
__asm__ volatile(
"fswizz.ps f1, f23, 0xB1\n\t"
"fadd.ps f2, f23, f1, rne\n\t"
"fswizz.ps f3, f2, 0x4E\n\t"
"fadd.ps f4, f2, f3, rne\n\t"
"fmvz.x.ps t0, f4, 4\n\t"
"fbcx.ps f5, t0\n\t"
"fadd.ps %[vout], f4, f5, rne\n\t"
: [vout] "=f"(sumf)::"t0", "f1", "f2", "f3", "f4", "f5");
__asm__ volatile("mova.m.x %0" ::"r"(saved_mask));
}
// Scalar tail (d_state not a multiple of 8).
for (; state_idx < d_state; ++state_idx) {
const float prev_state = prev_row[state_idx];
const float A_val = A_broadcast ? *A_row : A_row[state_idx];
const float dA = et_expf(dt_softplus * A_val);
const float st = prev_state * dA + B_row[state_idx] * x_dt;
state_dst[state_idx] = st;
sumf += st * C_row[state_idx];
}
dst_data[seq_idx * (n_seq_tokens * n_head * head_dim) + token_idx * (n_head * head_dim) +
head_idx * head_dim + dim_idx] = sumf;
}
}
}
}
return 0;
}
@@ -0,0 +1,103 @@
//******************************************************************************
// SUM_ROWS F32 Kernel
// Row-wise sum reduction: dst[0, i1, i2, i3] = sum(src0[0..ne00-1, i1, i2, i3])
// Vectorized 8-wide accumulation with horizontal reduction.
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_sum_rows_params {
struct ggml_tensor src0; // F32 input tensor [ne00, ne01, ne02, ne03]
struct ggml_tensor dst; // F32 output tensor [1, ne01, ne02, ne03]
};
int entry_point(struct ggml_et_sum_rows_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
const int64_t ne00 = src0->ne[0]; // Row length (to be summed)
const int64_t ne01 = src0->ne[1];
const int64_t ne02 = src0->ne[2];
const int64_t ne03 = src0->ne[3];
const size_t nb01 = src0->nb[1];
const size_t nb02 = src0->nb[2];
const size_t nb03 = src0->nb[3];
const size_t nb1 = dst->nb[1];
const size_t nb2 = dst->nb[2];
const size_t nb3 = dst->nb[3];
// Flatten rows across dimensions 1,2,3 and distribute across threads
const int64_t total_rows = ne01 * ne02 * ne03;
for (int64_t ir = thread_id; ir < total_rows; ir += num_threads) {
const int64_t i03 = ir / (ne02 * ne01);
const int64_t i02 = (ir - i03 * ne02 * ne01) / ne01;
const int64_t i01 = ir - i03 * ne02 * ne01 - i02 * ne01;
const float * src_row = (const float *) ((const char *) src0_data + i01 * nb01 + i02 * nb02 + i03 * nb03);
float * dst_ptr = (float *) ((char *) dst_data + i01 * nb1 + i02 * nb2 + i03 * nb3);
// Vectorized 8-wide sum accumulation
float zero = 0.0f;
__asm__ volatile("fbc.ps f10, %[z]\n" : : [z] "m"(zero) : "f10");
for (int32_t i0 = 0; i0 < (int32_t) ne00; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[x_vec]\n"
"fadd.ps f10, f10, f11\n"
:
: [x_vec] "m"(*(const float (*)[8]) & src_row[i0])
: "f10", "f11");
}
// Horizontal sum of 8 accumulated values in f10
float row_sum;
__asm__ __volatile__(
"fswizz.ps f1, f10, 0xB1 \n\t"
"fadd.ps f2, f10, f1, rne \n\t"
"fswizz.ps f3, f2, 0x4E \n\t"
"fadd.ps f4, f2, f3, rne \n\t"
"fmvz.x.ps t0, f4, 4 \n\t"
"fbcx.ps f5, t0 \n\t"
"fadd.ps %[vout], f4, f5, rne \n\t"
: [vout] "=f"(row_sum)::"t0", "f1", "f2", "f3", "f4", "f5");
atomic_store_f32(dst_ptr, row_sum);
}
return 0;
}
+897
View File
@@ -0,0 +1,897 @@
#ifndef __TENSORS_H
#define __TENSORS_H
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__cplusplus) && (__cplusplus >= 201103L)
# include <cinttypes>
# if (__cplusplus < 202002L)
# include <cstdbool>
# endif
#else
# include <inttypes.h>
# include <stdbool.h>
#endif
/*! \def QUANT_LAST_TRANS
\brief Tensor Quant instruction: Do not perform any more transformations.
*/
#define QUANT_LAST_TRANS 0
/*! \def QUANT_INT32_TO_FP32
\brief Tensor Quant instruction: Convert all elements of A from 32-bit signed integer values to single-precision
floating-point values.
*/
#define QUANT_INT32_TO_FP32 1
/*! \def QUANT_FP32_TO_INT32
\brief Tensor Quant instruction: Convert all elements of A from single-precision floating-point values to 32-
bit signed integer values.
*/
#define QUANT_FP32_TO_INT32 2
/*! \def QUANT_RELU
\brief Tensor Quant instruction: Convert all negative INT32 values in A to 0
*/
#define QUANT_RELU 3
/*! \def QUANT_INT32_ADD_ROW
\brief Tensor Quant instruction: Read the low-order COLS+1 32-bit signed integer values from an L1
scratchpad line, and add this vector to every row of the 32-bit signed integer
matrix A.
*/
#define QUANT_INT32_ADD_ROW 4
/*! \def QUANT_INT32_ADD_COL
\brief Tensor Quant instruction: Read the low-order ROWS+1 32-bit signed integer values from an L1
scratchpad line, and add this vector to every column of the 32-bit signed
integer matrix A.
*/
#define QUANT_INT32_ADD_COL 5
/*! \def QUANT_FP32_MUL_ROW
\brief Tensor Quant instruction: Read the low-order COLS+1 single-precision floating-point values from an
L1 scratchpad line, and multiply the single-precision elements of each row
of matrix A element-wise by this vector.
*/
#define QUANT_FP32_MUL_ROW 6
/*! \def QUANT_FP32_MUL_COL
\brief Tensor Quant instruction: Read the low-order ROWS+1 single-precision floating-point values from an
L1 scratchpad line, and multiply the single-precision elements of each col-
umn of matrix A element-wise by this vector.
*/
#define QUANT_FP32_MUL_COL 7
/*! \def QUANT_SATINT8
\brief Tensor Quant instruction: Clamp all 32-bit signed integer values in A to the range [-128, 127].
The values are written in bits 7:0 of each element, with bits 31:8 set to zero.
*/
#define QUANT_SATINT8 8
/*! \def QUANT_SATUINT8
\brief Tensor Quant instruction: Clamp all 32-bit signed integer values in A to the range [0, 255]. The values
are written in bits 7:0 of each element, with bits 31:8 set to zero.
*/
#define QUANT_SATUINT8 9
/*! \def QUANT_PACK_128B
\brief Tensor Quant instruction: Copy the low-order byte of the n-th 32-bit value in each row of A to the n-th
byte of the row.
*/
#define QUANT_PACK_128B 10
/*! \def TENSOR_REDUCE_OP_FADD
\brief Tensor Reduce instruction: The result is the addition of the incoming single-precision floating-point data
and the single-precision floating-point values in the vector register file.
*/
#define TENSOR_REDUCE_OP_FADD 0
// #define TENSOR_REDUCE_OP_FSUB 1 -- Not supported
/*! \def TENSOR_REDUCE_OP_FMAX
\brief Tensor Reduce instruction: The result is the maximum of the incoming single-precision floating-point data
and the single-precision floating-point values in the vector register file.
*/
#define TENSOR_REDUCE_OP_FMAX 2
/*! \def TENSOR_REDUCE_OP_FMIN
\brief Tensor Reduce instruction: The result is the minimum of the incoming single-precision floating-point data
and the single-precision floating-point values in the vector register file..
*/
#define TENSOR_REDUCE_OP_FMIN 3
/*! \def TENSOR_REDUCE_OP_IADD
\brief Tensor Reduce instruction: The result is the addition of the incoming 32-bit integer data and the 32-bit inte-
ger values in the vector register file.
*/
#define TENSOR_REDUCE_OP_IADD 4
// #define TENSOR_REDUCE_OP_ISUB 5 -- Not supported
/*! \def TENSOR_REDUCE_OP_IMAX
\brief Tensor Reduce instruction: The result is the maximum of the incoming 32-bit signed integer data and the
32-bit signed integer values in the vector register file.
*/
#define TENSOR_REDUCE_OP_IMAX 6
/*! \def TENSOR_REDUCE_OP_IMIN
\brief Tensor Reduce instruction: The result is the minimum of the incoming 32-bit signed integer data and the
32-bit signed integer values in the vector register file.
*/
#define TENSOR_REDUCE_OP_IMIN 7
/*! \def TENSOR_REDUCE_OP_FGET
\brief Tensor Reduce instruction get function to be performed
*/
#define TENSOR_REDUCE_OP_FGET 8
/*! \def TENSOR_LOAD_WAIT_0
\brief Tensor load to L1 Scratchpad with ID = 0 is complete.
*/
#define TENSOR_LOAD_WAIT_0 0
/*! \def TENSOR_LOAD_WAIT_1
\brief Tensor load to L1 Scratchpad with ID = 1 is complete.
*/
#define TENSOR_LOAD_WAIT_1 1
/*! \def TENSOR_FMA_WAIT
\brief All previous tensor matrix multiplication instructions are complete.
*/
#define TENSOR_FMA_WAIT 7
/*! \def TENSOR_STORE_WAIT
\brief All previous tensor store instructions are complete.
*/
#define TENSOR_STORE_WAIT 8
/*! \def TENSOR_REDUCE_WAIT
\brief All previous tensor reduction instructions are complete
*/
#define TENSOR_REDUCE_WAIT 9
/*! \def TENSOR_QUANT_WAIT
\brief TensorQuant is complete
*/
#define TENSOR_QUANT_WAIT 10
// TensorFMA opcode values (tensor_fma CSR 0x801, bits 3:1)
#define TENSOR_FMA_OP_FP32 0 // TensorFMA32: FP32 x FP32 -> FP32
#define TENSOR_FMA_OP_FP16 1 // TensorFMA16A32: FP16 x FP16 -> FP32
// opcode 2 is reserved
#define TENSOR_FMA_OP_INT8 3 // TensorIMA8A32: INT8 x INT8 -> INT32
// TensorLoad transformation values (tensor_load CSR 0x83F, bits 61:59)
#define TENSOR_LOAD_PLAIN 0 // TensorLoad: 64B rows
#define TENSOR_LOAD_INTERLEAVE8 1 // TensorLoadInterleave8: for TensorIMA8A32 B
#define TENSOR_LOAD_INTERLEAVE16 2 // TensorLoadInterleave16: for TensorFMA16A32 B
// transformations 3-4 are reserved
#define TENSOR_LOAD_TRANSPOSE8 5 // TensorLoadTranspose8: 8-bit transpose
#define TENSOR_LOAD_TRANSPOSE16 6 // TensorLoadTranspose16: 16-bit transpose
#define TENSOR_LOAD_TRANSPOSE32 7 // TensorLoadTranspose32: 32-bit transpose
/*! \def TENSOR_ERROR_LOAD_TRANSFORM
\brief Define for tensor load transform error.
*/
#define TENSOR_ERROR_LOAD_TRANSFORM 1
/*! \def TENSOR_ERROR_FCC_OVERFLOW
\brief Define for tensor fcc overflow error.
*/
#define TENSOR_ERROR_FCC_OVERFLOW 3
/*! \def TENSOR_ERROR_SCP_DISABLED
\brief Define for tensor scp disabled error.
*/
#define TENSOR_ERROR_SCP_DISABLED 4
/*! \def TENSOR_ERROR_LOCKSW
\brief Define for tensor locksw error.
*/
#define TENSOR_ERROR_LOCKSW 5
/*! \def TENSOR_ERROR_TL1_FMA
\brief Define for L1 FMA error.
*/
#define TENSOR_ERROR_TL1_FMA 6
/*! \def TENSOR_ERROR_MEM_FAULT
\brief Define for Memory fault error.
*/
#define TENSOR_ERROR_MEM_FAULT 7
/*! \def TENSOR_ERROR_STORE_COOP
\brief Define for store coop error.
*/
#define TENSOR_ERROR_STORE_COOP 8
/*! \def TENSOR_ERROR_REDUCE
\brief Define for tensor reduce error.
*/
#define TENSOR_ERROR_REDUCE 9
/*! \struct et_tensor_load_l2scp_conf
\brief Tensor load from scp instruction configuration structure.
*/
typedef struct et_tensor_load_l2scp_conf {
bool use_tmask;
uint64_t dst_start;
uint64_t addr;
uint64_t num_lines;
uint64_t stride;
uint64_t id;
} et_tensor_load_l2scp_conf_t;
/*! \enum reduce_transform_t
\brief enum transform mode for tensor reduce.
*/
typedef enum {
FADD = 0x0ULL,
FSUB = 0x1ULL,
FMAX = 0x2ULL,
FMIN = 0x3ULL,
IADD = 0x4ULL,
ISUB = 0x5ULL,
IMAX = 0x6ULL,
IMIN = 0x7ULL,
FGET = 0x8ULL
} reduce_transform_t;
/*! \struct et_tensor_load_conf
\brief Tensor load instruction configuration structure.
*/
typedef struct et_tensor_load_conf {
bool use_tmask;
bool use_coop;
bool use_tenb;
uint64_t dst_start;
uint64_t transformation;
uint64_t rd_l2scp;
uint64_t addr;
uint64_t offset;
uint64_t num_lines;
uint64_t stride;
uint64_t id;
} et_tensor_load_conf_t;
/*! \fn inline void tensor_wait(long id)
\brief Tensor wait instruction, Tensor Wait can be used to stall execution until
a previously issued tensor instruction completes.
\param id tensor ID
\return none
\tensorops Implementation of tensor_wait api
*/
inline __attribute__((always_inline)) void tensor_wait(long id) {
__asm__ __volatile__(" csrw 0x830, %[id]\n" : : [id] "r"(id) : "memory");
}
/*! \fn inline void tensor_load (tensor_load *conf)
\brief Tensor load instruction, it loads data from memory (bypass-ing the L1 cache)
into the L1 scratchpad. Input parameter defines the configuration to tensor load.
\param use_tmask the tensor_mask register is used for this operation
\param use_coop the operation is a cooperative tensor load.
\param dst_start L1 Scratchpad starting cache line
\param transformation These bits, along with bit 52, decodes the type of tensor operation.
\param use_tenb This bit, along with transformation, decodes the type of tensor operation.
\param addr tensor load address
\param offset tensor load address offset
\param num_lines tensor load number of cache lines
\param stride tensor load stride value
\param id tensor load id
\return none
\tensorops Implementation of tensor_load api
*/
// 1. Load Matrix A segment (1 row x 16 cols) into SCP ID 0
// dst_start 0 refers to the first line of L1 Scratchpad
// tensor_load(false, false, 0, 0, 0,
// (uint64_t)(src0_data + m * K + kb), 0, 1, 0, 0);
inline void __attribute__((always_inline)) tensor_load(bool use_tmask,
bool use_coop,
uint64_t dst_start,
uint64_t transformation,
uint64_t use_tenb,
uint64_t addr,
uint64_t offset,
uint64_t num_lines,
uint64_t stride,
uint64_t id) {
// Address alignment depends on transformation type:
// Interleave8, Transpose8 (1,5): 16B aligned, addr bits 47:4
// Interleave16, Transpose16 (2,6): 32B aligned, addr bits 47:5
// Load, Transpose32, LoadB (0,7): 64B aligned, addr bits 47:6
uint64_t addr_mask = (transformation == 1 || transformation == 5) ? 0xFFFFFFFFFFF0ULL :
(transformation == 2 || transformation == 6) ? 0xFFFFFFFFFFE0ULL :
0xFFFFFFFFFFC0ULL;
uint64_t csr_enc = (((uint64_t) use_tmask & 1) << 63) | (((uint64_t) use_coop & 1) << 62) |
((transformation & 0x7) << 59) | ((dst_start & 0x3F) << 53) | ((use_tenb & 0x1) << 52) |
((addr & addr_mask)) | ((offset & 0x3) << 4) | ((num_lines & 0xF));
uint64_t x31_enc = (stride & 0xFFFFFFFFFFC0ULL) | (id & 0x1);
__asm__ __volatile__(
"mv x31, %[x31v]\n"
"csrw 0x83f, %[csrv]\n"
:
: [x31v] "r"(x31_enc), [csrv] "r"(csr_enc)
: "x31", "memory");
}
/*! \fn inline void et_tensor_load (et_tensor_load_conf_t *conf)
\brief Tensor load instruction, it loads data from memory (bypass-ing the L1 cache)
into the L1 scratchpad. Input parameter defines the configuration to tensor load.
\param conf tensor load configuration
\return none
\tensorops Implementation of et_tensor_load api
*/
inline void __attribute__((always_inline)) et_tensor_load(et_tensor_load_conf_t * conf) {
tensor_load(conf->use_tmask, conf->use_coop, conf->dst_start, conf->transformation, (uint64_t) conf->use_tenb,
conf->addr, conf->offset, conf->num_lines, conf->stride, conf->id);
}
/*! \fn inline void tensor_load_setup_b(bool use_coop, uint64_t addr, uint64_t num_lines, uint64_t stride, uint64_t id)
\brief Tensor load instruction setup
\param use_coop the operation is a cooperative tensor load.
\param addr tensor load address
\param num_lines tensor load number of cache lines
\param stride tensor load stride value
\param id tensor load id
\return none
\tensorops Implementation of tensor_load_setup_b api
*/
inline void __attribute__((always_inline)) tensor_load_setup_b(bool use_coop,
uint64_t addr,
uint64_t num_lines,
uint64_t stride,
uint64_t id) {
uint64_t csr_enc =
(((uint64_t) use_coop & 1) << 62) | (0x1ULL << 52) | ((addr & 0xFFFFFFFFFFC0ULL)) | ((num_lines & 0xF));
uint64_t x31_enc = (stride & 0xFFFFFFFFFFC0ULL) | (id & 0x1);
__asm__ __volatile__(
"mv x31, %[x31v]\n"
"csrw 0x83f, %[csrv]\n"
:
: [x31v] "r"(x31_enc), [csrv] "r"(csr_enc)
: "x31", "memory");
}
/*! \fn inline void et_tensor_load_l2scp (et_tensor_load_l2scp_conf_t *conf)
\brief Tensor load l2scp loads data from memory (bypassing the L1 and L2 caches) into the L2 scratchpad.
\param conf tensor load configuration
\return none
\tensorops Implementation of et_tensor_load_l2scp api
*/
inline void __attribute__((always_inline)) et_tensor_load_l2scp(et_tensor_load_l2scp_conf_t * conf) {
uint64_t csr_enc =
(((((uint64_t) conf->use_tmask) & 1) << 63) | ((conf->dst_start & 0x1FFFCUL) << (48 - 2)) |
((conf->dst_start & 0x3UL) << 4) | ((conf->addr & 0xFFFFFFFFFFC0UL)) | ((conf->num_lines & 0x0FUL)));
uint64_t x31_enc = (conf->stride & 0xFFFFFFFFFFC0ULL) | (conf->id & 0x1);
__asm__ __volatile__(
"mv x31, %[x31v]\n"
"csrw 0x85f, %[csrv]\n"
:
: [x31v] "r"(x31_enc), [csrv] "r"(csr_enc)
: "x31", "memory");
}
/*! \fn inline void tensor_store_scp(uint64_t entry_stride,
uint64_t start_scp_entry,
uint64_t Arows,
uint64_t addr,
uint64_t stride)
\brief Tensor Store writes a series of 64-byte blocks of data from the L1 scratchpad into memory.
A matrix X can have up to 16 rows, and each row can be up to 64B in size (the number of columns depends on the type of elements of X).
\param entry_stride Register stride
\param start_scp_entry Start register
\param Arows A matrix row size
\param addr Virtual Address
\param stride This value is the distance in bytes between consecutive tensor rows in memory
\return none
\tensorops Implementation of tensor_store_scp api
*/
inline void __attribute__((always_inline)) tensor_store_scp(uint64_t entry_stride,
uint64_t start_scp_entry,
uint64_t Arows,
uint64_t addr,
uint64_t stride) {
uint64_t csr_enc = ((entry_stride & 0x3) << 62) | ((start_scp_entry & 0x3F) << 56) | ((addr & 0xFFFFFFFFFFC0ULL)) |
((Arows & 0xF) << 51) | (((uint64_t) 1) << 48);
uint64_t x31_enc = (stride & 0xFFFFFFFFFFC0UL);
__asm__ __volatile__(
"mv x31, %[x31v]\n"
"csrw 0x87f, %[csrv]\n"
:
: [x31v] "r"(x31_enc), [csrv] "r"(csr_enc)
: "x31", "memory");
}
/*! \fn inline void tensor_store(uint64_t reg_stride,
uint64_t start_reg,
uint64_t cols,
uint64_t Arows,
uint64_t addr,
uint64_t coop_store,
uint64_t stride)
\brief The Tensor store instruction reads a tensor from the vector register files and writes it to memory,
bypassing the L1 data cache and the L2 cache. For the purposes of this instruction the tensor has ROWS+1 rows,
and each row is 16*SIZE+16 bytes in size.
\param reg_stride Register stride
\param start_reg start register address
\param cols matrix row size.
\param Arows matrix row size
\param addr Virtual Address
\param coop_store Number of minions to cooperate with
\param stride This value is the distance in bytes between consecutive tensor rows in memory
\return none
\tensorops Implementation of tensor_store api
*/
inline void __attribute__((always_inline)) tensor_store(uint64_t reg_stride,
uint64_t start_reg,
uint64_t cols,
uint64_t Arows,
uint64_t addr,
uint64_t coop_store,
uint64_t stride) {
uint64_t warl = 0;
uint64_t csr_enc = ((reg_stride & 0x3) << 62) | ((start_reg & 0x1F) << 57) | ((cols & 0x3) << 55) |
((addr & 0xFFFFFFFFFFF0)) | ((Arows & 0xF) << 51) | ((coop_store & 0x3) << 49) | ((warl & 0xF));
uint64_t x31_enc = (stride & 0xFFFFFFFFFF0UL);
__asm__ __volatile__(
"mv x31, %[x31v]\n"
"csrw 0x87f, %[csrv]\n"
:
: [x31v] "r"(x31_enc), [csrv] "r"(csr_enc)
: "x31", "memory");
}
/*! \fn inline void tensor_fma(bool use_tmask,
uint64_t b_num_col,
uint64_t a_num_rows,
uint64_t a_num_cols,
uint64_t offset,
bool tenc_loc,
bool tenb_unsigned,
bool tena_unsigned,
bool tenb_loc,
uint64_t scp_loc_b,
uint64_t scp_loc_a,
uint64_t opcode,
bool first_pass)
\brief The Tensor FMA instruction multiplies two matrices A and B, optionally adds the resulting matrix
to a third matrix C, and writes the result back onto matrix C
\param use_tmask Use tensor_mask CSR to skip operations in an A row granularity.
\param b_num_col B matrix number of columns
\param a_num_rows A matrix number of rows
\param a_num_cols A matrix number of columns
\param offset A matrix starting column for the operation.
\param tenc_loc Location of matrix C (0 = L1 scratchpad, 1 = memory).
\param tenb_unsigned TenB is signed (0) or unsigned (1).
\param tena_unsigned TenA is signed (0) or unsigned (1).
\param tenb_loc Location of matrix B (0 = L1 scratchpad, 1 = memory).
\param scp_loc_b Starting L1 scratchpad cache line where matrix B is stored, ignored when xs[20] = 1.
\param scp_loc_a Starting L1 scratchpad cache line where matrix A is stored, ignored when xs[20] = 1.
\param opcode 0 = TensorFMA32 (F32xF32->F32), 1 = TensorFMA16A32 (F16xF16->F32), 3 = TensorIMA8A32 (I8xF8->I32).
Other opcodes are invalid.
\param first_pass if set to 0 then the initial value of TenC is added to the result
\return none
\tensorops Implementation of tensor_fma api
*/
inline void __attribute__((always_inline)) tensor_fma(bool use_tmask,
uint64_t b_num_col,
uint64_t a_num_rows,
uint64_t a_num_cols,
uint64_t offset,
bool tenc_loc,
bool tenb_unsigned,
bool tena_unsigned,
bool tenb_loc,
uint64_t scp_loc_b,
uint64_t scp_loc_a,
uint64_t opcode,
bool first_pass) {
uint64_t csr_enc = (((uint64_t) use_tmask & 1) << 63) | ((b_num_col & 0x3) << 55) | ((a_num_rows & 0xF) << 51) |
((a_num_cols & 0xF) << 47) | ((offset & 0xF) << 43) | (((uint64_t) tenc_loc & 1) << 23) |
(((uint64_t) tena_unsigned & 1) << 22) | (((uint64_t) tenb_unsigned & 1) << 21) |
(((uint64_t) tenb_loc & 1) << 20) | ((scp_loc_b & 0xFF) << 12) | ((scp_loc_a & 0xFF) << 4) |
((opcode & 0x7) << 1) | ((uint64_t) first_pass & 1);
__asm__ __volatile__("csrw 0x801, %[csr_enc]\n" : : [csr_enc] "r"(csr_enc) :);
}
/*! \fn inline uint32_t tensor_reduce_uint32(uint32_t value, uint64_t operation, uint64_t partnerID, uint64_t action)
\brief Tensor reduce allows a group of harts to communicate values held in floating-point registers to collectively calculate a reduction
function.
\param value Register stride
\param operation Function to be performed.
\param partnerID Receiver minionID.
\param action action value
\return uint32_t value after reduction
\tensorops Implementation of tensor_reduce_uint32 api
*/
inline uint32_t __attribute__((always_inline)) tensor_reduce_uint32(uint32_t value,
uint64_t operation,
uint64_t partnerID,
uint64_t action) {
uint64_t warl = 0;
uint32_t out;
uint64_t csr_enc = ((warl & 0x2) << 62) | ((0ULL & 0x1F) << 57) | ((warl & 0x1FFFFFFF) << 28) |
((operation & 0xF) << 24) | ((1ULL & 0xFF) << 16) | ((partnerID & 0x1FFF) << 3) |
((warl & 0x1) << 2) | ((action & 0x3));
__asm__ __volatile__(
"fmv.s.x f0, %[value]\n"
"csrw 0x800, %[csr_enc]\n"
"fmv.x.s %[out], f0\n"
: [out] "=r"(out)
: [csr_enc] "r"(csr_enc), [value] "r"(value)
: "f0");
return out;
}
/*! \fn inline float tensor_reduce_float(float freg, uint64_t operation, uint64_t num_reg, uint64_t partnerID, uint64_t action) {
\brief TensorReduce allows a group of harts to communicate values held in floating-point registers to collectively calculate a reduction
function.
\param freg Freg register stride
\param operation Function to be performed.
\param num_reg number of registers to use
\param partnerID Receiver minionID.
\param action action value
\return float value after reduction
\tensorops Implementation of tensor_reduce_float api
*/
inline float __attribute__((always_inline)) tensor_reduce_float(float freg,
uint64_t operation,
uint64_t num_reg,
uint64_t partnerID,
uint64_t action) {
uint64_t warl = 0;
float out;
uint64_t csr_enc = ((warl & 0x2) << 62) | ((0ULL & 0x1F) << 57) | ((warl & 0x1FFFFFFF) << 28) |
((operation & 0xF) << 24) | ((num_reg & 0xFF) << 16) | ((partnerID & 0x1FFF) << 3) |
((warl & 0x1) << 2) | ((action & 0x3));
__asm__ __volatile__(
"fmv.s f0, %[freg]\n"
"csrw 0x800, %[csr_enc]\n"
"fmv.s %[out], f0\n"
: [out] "=f"(out)
: [csr_enc] "r"(csr_enc), [freg] "f"(freg)
: "f0");
return out;
}
//#define tensor_reduce_float1(fval, operation, partnerID, action) do {
// uint64_t warl = 0;
// float out;
// uint64_t csr_enc = ((warl & 0x2 ) << 62) |
// ((0 & 0x1F ) << 57) |
// ((warl & 0x1FFFFFFF ) << 28) |
// ((operation & 0xF ) << 24) |
// ((1 & 0xFF ) << 16) |
// ((partnerID & 0x1FFF ) << 3 ) |
// ((warl & 0x1 ) << 2 ) |
// ((action & 0x3 ) );
//
// register float asm("f0") fval;
// __asm__ volatile (
// "csrw 0x800, %[csr_enc]"
// : "+r" (ftmp)
// : [csr_enc] "r" (csr_enc)
// );
//} while (0)
//
//
//inline float __attribute__((always_inline)) tensor_reduce_float(uint64_t fstart, uint64_t operation, uint64_t num_reg, uint64_t partnerID, uint64_t action) {
// uint64_t warl = 0;
// float out;
// uint64_t csr_enc = ((warl & 0x2 ) << 62) |
// ((fstart & 0x1F ) << 57) |
// ((warl & 0x1FFFFFFF ) << 28) |
// ((operation & 0xF ) << 24) |
// ((num_reg & 0xFF ) << 16) |
// ((partnerID & 0x1FFF ) << 3 ) |
// ((warl & 0x1 ) << 2 ) |
// ((action & 0x3 ) );
//
// __asm__ volatile (
// "csrw 0x800, %[csr_enc]\n"
// : /*empty*/
// : [csr_enc] "r" (csr_enc),
// : /*"f0", "f1", "f2", "f3", "f4",
// "f5", "f6", "f7", "f8", "f9",
// "f10", "f11", "f12", "f13", "f14",
// "f15", "f16", "f17", "f18", "f19",
// "f20", "f21", "f22", "f23", "f24",
// "f25", "f26", "f27", "f28", "f29",
// "f30", "f31"*/
// );
//
// return out;
//}
/*! \fn inline void tensor_reduce(uint64_t start_reg, uint64_t operation, uint64_t num_reg, uint64_t partnerID, uint64_t action)
\brief The TensorReduce instruction allows up to 216 harts to collectively calculate a reduction function.
\param start_reg starting register
\param operation Function to be performed.
\param num_reg number of registers
\param partnerID Receiver minionID.
\param action action value
\return uint32_t value after reduction
\tensorops Implementation of tensor_reduce api
*/
inline void __attribute__((always_inline)) tensor_reduce(uint64_t start_reg,
uint64_t operation,
uint64_t num_reg,
uint64_t partnerID,
uint64_t action) {
uint64_t warl = 0;
uint64_t csr_enc = ((warl & 0x2) << 62) | ((start_reg & 0x1F) << 57) | ((warl & 0x1FFFFFFF) << 28) |
((operation & 0xF) << 24) | ((num_reg & 0xFF) << 16) | ((partnerID & 0x1FFF) << 3) |
((warl & 0x1) << 2) | ((action & 0x3));
__asm__ __volatile__("csrw 0x800, %[csr_enc]\n" : : [csr_enc] "r"(csr_enc) :);
}
/*! \fn inline void tensor_reduce_send(uint64_t start_reg, uint64_t num_reg, uint64_t partnerID)
\brief This function applies reduce instruction to function and then sends to partner minion.
\param start_reg starting register
\param num_reg number of registers
\param partnerID Receiver minionID.
\return none
\tensorops Implementation of tensor_reduce_send api
*/
inline void __attribute__((always_inline)) tensor_reduce_send(uint64_t start_reg,
uint64_t num_reg,
uint64_t partnerID) {
uint64_t warl = 0;
tensor_reduce(start_reg, warl, num_reg, partnerID, 0);
}
/*! \fn inline void tensor_reduce_recv(uint64_t start_reg, uint64_t operation, uint64_t num_reg, uint64_t partnerID)
\brief This function recieves reduce function from partner minion.
\param start_reg starting register
\param operation operation to be performed
\param num_reg number of registers
\param partnerID Receiver minionID.
\return none
\tensorops Implementation of tensor_reduce_recv api
*/
inline void __attribute__((always_inline)) tensor_reduce_recv(uint64_t start_reg,
uint64_t operation,
uint64_t num_reg,
uint64_t partnerID) {
tensor_reduce(start_reg, operation, num_reg, partnerID, 1);
}
/*! \fn inline void tensor_reduce_auto(uint64_t start_reg, uint64_t operation, uint64_t num_reg, uint64_t tree_depth)
\brief The Tensor reduce instruction allows up to 216 harts to collectively calculate a reduction function.
\param start_reg starting register
\param operation operation to be performed
\param num_reg number of registers
\param tree_depth tree depth
\return none
\tensorops Implementation of tensor_reduce_auto api
*/
inline void __attribute__((always_inline)) tensor_reduce_auto(uint64_t start_reg,
uint64_t operation,
uint64_t num_reg,
uint64_t tree_depth) {
tensor_reduce(start_reg, operation, num_reg, (0ULL << 4) | (tree_depth & 0xF), 3);
}
/*! \fn inline void tensor_broadcast(uint64_t start_reg, uint64_t operation, uint64_t num_reg, uint64_t tree_depth) {
\brief The Tensor broadcast instruction allows up to 216 harts to receive values held in the vector registers
of one of the harts in the group. The broadcast operation is performed in a binary-tree fashion, where the source
data is originally in the root node and the final result ends up in the leaf nodes.
\param start_reg Starting floating-point register
\param operation operation to be performed
\param num_reg Number of floating-point registers
\param tree_depth tree depth
\return none
\tensorops Implementation of tensor_broadcast api
*/
inline void __attribute__((always_inline)) tensor_broadcast(uint64_t start_reg,
uint64_t operation,
uint64_t num_reg,
uint64_t tree_depth) {
tensor_reduce(start_reg, operation, num_reg, (0ULL << 4) | (tree_depth & 0xF), 2);
}
/*! \fn inline void tensor_reduce_autopair(uint64_t start_reg, uint64_t operation, uint64_t num_reg, uint64_t start_lvl, uint64_t end_lvl, uint64_t action) {
\brief This function is wrapper of Tensor Reduce (auto-pair variant) instruction.
\param start_reg Starting floating-point register
\param operation Function to be performed
\param num_reg Number of floating-point registers
\param start_lvl starting level value
\param end_lvl ending level value
\param action action value
\return none
\tensorops Implementation of tensor_reduce_autopair api
*/
inline void __attribute__((always_inline)) tensor_reduce_autopair(uint64_t start_reg,
uint64_t operation,
uint64_t num_reg,
uint64_t start_lvl,
uint64_t end_lvl,
uint64_t action) {
uint64_t partnerID;
// PRM-10 defines the partnerID field for Tensor Reduce (auto-pair variant) as following:
// [15:11] WARL(0)
// [10: 7] End level for autopair
// [ 6: 3] Start level for autopair
uint64_t warl = 0;
partnerID = ((warl & 0xF) << 11) | ((end_lvl & 0xF) << 7) | ((start_lvl & 0xF) << 3);
// Operations encoding:
// 0000=fadd, 0001=fsub, 0010=fmax, 0011=fmin, 0100=iadd, 0101=isub, 0110=imax, 0111=imin, 1000=fget
//
// Action encoding:
// 00=send, 01=receive, 10=auto-pair broadcast derive from hartid,11=auto-pair reduce derive from hartid
tensor_reduce(start_reg, operation, num_reg, (partnerID >> 3), action);
}
/*! \fn inline void tensor_quant(uint64_t start_reg, uint64_t col, uint64_t row, uint64_t scp_loc, uint64_t transf9, uint64_t transf8, uint64_t transf7, uint64_t transf6, uint64_t transf5, uint64_t transf4, uint64_t transf3, uint64_t transf2, uint64_t transf1, uint64_t transf0 )
\brief Tensor quantization (TensorQuant) instructions are encoded as writes to the tensor_quant CSR. The TensorQuant
instruction performs a sequence of up to 10 transformations to a matrix A
\param start_reg Starting register
\param col A matrix number of columns.
\param row A matrix number of rows.
\param scp_loc L1 scratchpad cache line where the first vector is stored.
\param transf9 Transformation 9.
\param transf8 Transformation 8.
\param transf7 Transformation 7.
\param transf6 Transformation 6.
\param transf5 Transformation 5.
\param transf4 Transformation 4.
\param transf3 Transformation 3.
\param transf2 Transformation 2.
\param transf1 Transformation 1.
\param transf0 Transformation 0.
\return none
\tensorops Implementation of tensor_quant api
*/
inline void __attribute__((always_inline)) tensor_quant(uint64_t start_reg,
uint64_t col,
uint64_t row,
uint64_t scp_loc,
uint64_t transf9,
uint64_t transf8,
uint64_t transf7,
uint64_t transf6,
uint64_t transf5,
uint64_t transf4,
uint64_t transf3,
uint64_t transf2,
uint64_t transf1,
uint64_t transf0) {
uint64_t csr_enc = ((start_reg & 0x1F) << 57) | ((col & 0x3) << 55) | ((row & 0xF) << 51) |
((scp_loc & 0x3F) << 45) | ((transf9 & 0xF) << 36) | ((transf8 & 0xF) << 32) |
((transf7 & 0xF) << 28) | ((transf6 & 0xF) << 24) | ((transf5 & 0xF) << 20) |
((transf4 & 0xF) << 16) | ((transf3 & 0xF) << 12) | ((transf2 & 0xF) << 8) |
((transf1 & 0xF) << 4) | ((transf0 & 0xF) << 0);
__asm__ __volatile__("csrw 0x806, %[csr_enc]\n" : : [csr_enc] "r"(csr_enc) :);
}
/*! \fn inline void tensor_mask(uint64_t zeros, uint64_t mask_bits)
\brief The TensorLoad, TensorFMA, and CacheOp instructions can operate under the
control of the tensor_mask CSR. The tensor_mask CSR contains one bit for each
of the destination lines that TensorLoad can potentially write into the scratchpad
\param zeros all zeros
\param mask_bits tensor bit mask
\return none
\tensorops Implementation of tensor_mask api
*/
inline void __attribute__((always_inline)) tensor_mask(uint64_t zeros, uint64_t mask_bits) {
uint64_t csr_enc = ((zeros & 0x000000000000) << 16) | (mask_bits & 0xFFFF);
__asm__ __volatile__("csrw 0x805, %[csr_enc]\n" : : [csr_enc] "r"(csr_enc) :);
}
/*! \fn inline void tensor_coop(uint64_t val)
\brief The tensor_coop instruction specifies which harts participate in cooperative tensor load operations. Only the first hart of each
selected Minion core participates in the cooperative operations, since the second hart cannot issue tensor load operations.
\param val value contains encoded coop id, minion and neigh mask
\return none
\tensorops Implementation of tensor_coop api
*/
inline void __attribute__((always_inline)) tensor_coop(uint64_t val) {
__asm__ __volatile__("csrw 0x804, %[val]\n" : : [val] "r"(val) :);
}
/*! \fn inline void convolution_ctrl(uint64_t row_start, uint64_t col_start)
\brief This function modifies the convolution control register.
This register encodes the location of a tensor inside a larger two-dimensional array.
\param row_start signed integer value specifying the row inside the array where the first row of the tensor resides
\param col_start signed integer value specifying the column inside the array where the first column of the tensor resides
\return none
\tensorops Implementation of convolution_ctrl api
*/
inline void __attribute__((always_inline)) convolution_ctrl(uint64_t row_start, uint64_t col_start) {
uint64_t csr_enc = ((row_start & 0xFFFF) << 32) | (col_start & 0xFFFF);
__asm__ __volatile__("csrw 0x803, %[csr_enc]\n" : : [csr_enc] "r"(csr_enc) :);
}
/*! \fn inline void convolution_size(uint64_t srow, uint64_t nrow, uint64_t scol, uint64_t ncol)
\brief This function modifies the convolution size register.
This register specifies the layout of a two-dimensional array used for convolutions.
\param srow integer value specifying the row inside the array where the first row of the tensor resides
\param nrow integer values specifying the number of rows of the array
\param scol integer value specifying the distance, in number of columns, between consecutive column accesses to the array during
convolution operations
\param ncol integer values specifying the number of columns of the array
\return none
\tensorops Implementation of convolution_size api
*/
inline void __attribute__((always_inline)) convolution_size(uint64_t srow,
uint64_t nrow,
uint64_t scol,
uint64_t ncol) {
uint64_t csr_enc = ((srow & 0xFF) << 56) | ((nrow & 0xFFFF) << 32) | ((scol & 0xFF) << 24) | ((ncol & 0xFFFF));
__asm__ __volatile__("csrw 0x802, %[csr_enc]\n" : : [csr_enc] "r"(csr_enc) :);
}
/*! \fn inline unsigned get_tensor_error()
\brief This function returns tensor error register value.
The tensor_error register accrues errors that occur during the execution of tensor instructions and cache management operations. When the tensor coprocessor or the cache management coprocessor generates an exception, the exception is recorded in
the tensor_error register and execution does not trap. The tensor_error register is never cleared by the implementation. It is the
responsibility of the software to clear tensor_error
\return Tensor error value
\tensorops Implementation of get_tensor_error api
*/
inline unsigned long __attribute__((always_inline)) get_tensor_error() {
unsigned long error;
__asm__ __volatile__("csrr %0, 0x808" : "=r"(error));
return error;
}
/*! \fn inline uint64_t get_tensor_mask()
\brief This function returns tensor mask register value.
\return Tensor mask value
\tensorops Implementation of get_tensor_mask api
*/
inline uint64_t __attribute__((always_inline)) get_tensor_mask() {
uint64_t val;
__asm__ __volatile__("csrr %0, 0x805" : "=r"(val));
return val;
}
#define mask_set(msk, val) \
do { \
__asm__ volatile("mov.m.x m" #msk ", zero, %0" ::"n"(val)); \
} while (0)
#define flw_ps(fd, ptr) \
do { \
__asm__ volatile("flw.ps f" #fd ", (%0)" ::"r"(ptr)); \
} while (0)
#define fsw_ps(fd, ptr) \
do { \
__asm__ volatile("fsw.ps f" #fd ", (%0)" ::"r"(ptr) : "memory"); \
} while (0)
#ifdef __cplusplus
}
#endif
#endif // ! __TENSORS_H
+244
View File
@@ -0,0 +1,244 @@
//******************************************************************************
// Tri F32 Kernel
// Triangular masking: zero out elements outside the triangular region.
//
// tri_type (matches ggml_tri_type enum):
// 0 = UPPER_DIAG: keep where i0 >= i1
// 1 = UPPER: keep where i0 > i1
// 2 = LOWER_DIAG: keep where i0 <= i1
// 3 = LOWER: keep where i0 < i1
//
// Distribution: cache-line aligned chunks of the flat contiguous dst.
// Each element is individually classified as keep or zero based on its
// (i0, i1) coordinates. This avoids cache-line sharing between threads
// when ne0 is not a multiple of 16.
//******************************************************************************
#include "ggml_tensor.h"
#include "platform.h"
#include <stdint.h>
#define TRI_TYPE_UPPER_DIAG 0
#define TRI_TYPE_UPPER 1
#define TRI_TYPE_LOWER_DIAG 2
#define TRI_TYPE_LOWER 3
struct ggml_et_tri_params {
struct ggml_tensor src0;
struct ggml_tensor dst;
int32_t tri_type;
};
static inline int keep_element(int32_t tri_type, int64_t i0, int64_t i1) {
switch (tri_type) {
case TRI_TYPE_LOWER:
return i0 < i1;
case TRI_TYPE_LOWER_DIAG:
return i0 <= i1;
case TRI_TYPE_UPPER:
return i0 > i1;
case TRI_TYPE_UPPER_DIAG:
return i0 >= i1;
default:
return 0;
}
}
int entry_point(struct ggml_et_tri_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
int32_t tri_type = params->tri_type;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
const int64_t ne0 = dst->ne[0];
const int64_t ne1 = dst->ne[1];
const int64_t ne2 = dst->ne[2];
const int64_t ne3 = dst->ne[3];
const size_t nb01 = src0->nb[1], nb02 = src0->nb[2], nb03 = src0->nb[3];
const size_t nb1 = dst->nb[1], nb2 = dst->nb[2], nb3 = dst->nb[3];
const int64_t total_rows = ne1 * ne2 * ne3;
//==========================================================================
// Fast path: ne0 % 16 == 0 — rows are cache-line aligned, distribute rows
//==========================================================================
if (ne0 % 16 == 0) {
float zero = 0.0f;
__asm__ volatile("fbc.ps f10, %[z]\n" : : [z] "m"(zero) : "f10");
for (int64_t row = thread_id; row < total_rows; row += num_threads) {
const int64_t i1 = row % ne1;
const int64_t i2 = (row / ne1) % ne2;
const int64_t i3 = row / (ne1 * ne2);
const float * src_row = (const float *) ((const char *) src0_data + i1 * nb01 + i2 * nb02 + i3 * nb03);
float * dst_row = (float *) ((char *) dst_data + i1 * nb1 + i2 * nb2 + i3 * nb3);
int64_t keep_start, keep_end;
switch (tri_type) {
case TRI_TYPE_LOWER:
keep_start = 0;
keep_end = i1;
break;
case TRI_TYPE_LOWER_DIAG:
keep_start = 0;
keep_end = i1 + 1;
break;
case TRI_TYPE_UPPER:
keep_start = i1 + 1;
keep_end = ne0;
break;
case TRI_TYPE_UPPER_DIAG:
keep_start = i1;
keep_end = ne0;
break;
default:
return -1;
}
if (keep_end > ne0) {
keep_end = ne0;
}
// Zero prefix [0, keep_start) — SIMD for aligned blocks, scalar tail
int64_t i0 = 0;
for (; i0 + 8 <= keep_start; i0 += 8) {
__asm__ volatile("fsw.ps f10, %[d]\n" : [d] "=m"(*(float (*)[8]) & dst_row[i0])::"f10");
}
for (; i0 < keep_start; i0++) {
dst_row[i0] = 0.0f;
}
// Copy kept region [keep_start, keep_end) — SIMD + scalar tail
for (; i0 + 8 <= keep_end; i0 += 8) {
__asm__ volatile(
"flw.ps f11, %[s]\n"
"fsw.ps f11, %[d]\n"
: [d] "=m"(*(float (*)[8]) & dst_row[i0])
: [s] "m"(*(const float (*)[8]) & src_row[i0])
: "f11");
}
for (; i0 < keep_end; i0++) {
dst_row[i0] = src_row[i0];
}
// Zero suffix [keep_end, ne0) — SIMD + scalar tail
for (; i0 + 8 <= ne0; i0 += 8) {
__asm__ volatile("fsw.ps f10, %[d]\n" : [d] "=m"(*(float (*)[8]) & dst_row[i0])::"f10");
}
for (; i0 < ne0; i0++) {
dst_row[i0] = 0.0f;
}
}
return 0;
}
//==========================================================================
// Unaligned fallback: distribute by cache lines, scalar per element
//==========================================================================
{
const int64_t total_elements = ne0 * ne1 * ne2 * ne3;
const int64_t elems_per_cl = 16;
const int64_t total_cl = (total_elements + elems_per_cl - 1) / elems_per_cl;
const int64_t cl_per_thread = (total_cl + num_threads - 1) / num_threads;
const int64_t cl_start = thread_id * cl_per_thread;
int64_t cl_end = cl_start + cl_per_thread;
if (cl_end > total_cl) {
cl_end = total_cl;
}
if (cl_start >= total_cl) {
return 0;
}
const int64_t es = cl_start * elems_per_cl;
int64_t ee = cl_end * elems_per_cl;
if (ee > total_elements) {
ee = total_elements;
}
int64_t row_idx = es / ne0;
int64_t col = es % ne0;
int64_t pos = es;
while (pos < ee) {
const int64_t i1 = row_idx % ne1;
const int64_t i2 = (row_idx / ne1) % ne2;
const int64_t i3 = row_idx / (ne1 * ne2);
const float * src_row = (const float *) ((const char *) src0_data + i1 * nb01 + i2 * nb02 + i3 * nb03);
int64_t row_remaining = ne0 - col;
int64_t chunk_remaining = ee - pos;
int64_t n = row_remaining < chunk_remaining ? row_remaining : chunk_remaining;
int64_t keep_start, keep_end;
switch (tri_type) {
case TRI_TYPE_LOWER:
keep_start = 0;
keep_end = i1;
break;
case TRI_TYPE_LOWER_DIAG:
keep_start = 0;
keep_end = i1 + 1;
break;
case TRI_TYPE_UPPER:
keep_start = i1 + 1;
keep_end = ne0;
break;
case TRI_TYPE_UPPER_DIAG:
keep_start = i1;
keep_end = ne0;
break;
default:
return -1;
}
if (keep_end > ne0) {
keep_end = ne0;
}
int64_t end_col = col + n;
for (int64_t i0 = col; i0 < end_col; i0++) {
if (i0 >= keep_start && i0 < keep_end) {
dst_data[pos + (i0 - col)] = src_row[i0];
} else {
dst_data[pos + (i0 - col)] = 0.0f;
}
}
pos += n;
col = 0;
row_idx++;
}
}
return 0;
}
@@ -0,0 +1,497 @@
#include "ggml-et-uberkernel-common.h"
#include "ggml-et-uberkernel-kernel-map.h"
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdint.h>
struct ggml_et_glu_params;
struct ggml_et_unary_params;
struct ggml_et_rope_params;
struct ggml_et_rms_norm_params;
struct ggml_et_rms_norm_mul_params;
struct ggml_et_softmax_params;
struct ggml_et_set_rows_params;
struct ggml_et_get_rows_params;
struct ggml_et_cont_params;
struct ggml_et_concat_params;
struct ggml_et_cumsum_params;
struct ggml_et_diag_params;
struct ggml_et_fill_params;
struct ggml_et_flash_attn_ext_params;
struct ggml_et_gated_delta_net_params;
struct ggml_et_group_norm_params;
struct ggml_et_im2col_params;
struct ggml_et_l2_norm_params;
struct ggml_et_mul_mat_id_params;
struct ggml_et_norm_params;
struct ggml_et_pad_params;
struct ggml_et_repeat_params;
struct ggml_et_rwkv_wkv6_params;
struct ggml_et_rwkv_wkv7_params;
struct ggml_et_scale_params;
struct ggml_et_set_params;
struct ggml_et_solve_tri_params;
struct ggml_et_sqr_params;
struct ggml_et_ssm_conv_params;
struct ggml_et_ssm_scan_params;
struct ggml_et_sum_rows_params;
struct ggml_et_tri_params;
extern int el_map_f32_entry(struct ggml_et_binary_params *, void *);
extern int glu_f32_entry(struct ggml_et_glu_params *, void *);
extern int unary_f32_entry(struct ggml_et_unary_params *, void *);
extern int rope_f32_entry(struct ggml_et_rope_params *, void *);
extern int rms_norm_f32_entry(struct ggml_et_rms_norm_params *, void *);
extern int rms_norm_mul_f32_entry(struct ggml_et_rms_norm_mul_params *, void *);
extern int softmax_f32_entry(struct ggml_et_softmax_params *, void *);
extern int set_rows_f32_entry(struct ggml_et_set_rows_params *, void *);
extern int get_rows_f32_entry(struct ggml_et_get_rows_params *, void *);
extern int cont_f32_entry(struct ggml_et_cont_params *, void *);
extern int cont_f16_entry(struct ggml_et_cont_params *, void *);
extern int cpy_f32_f16_entry(struct ggml_et_cont_params *, void *);
extern int concat_f32_entry(struct ggml_et_concat_params *, void *);
extern int cumsum_f32_entry(struct ggml_et_cumsum_params *, void *);
extern int diag_f32_entry(struct ggml_et_diag_params *, void *);
extern int fill_f32_entry(struct ggml_et_fill_params *, void *);
extern int flash_attn_ext_f32_entry(struct ggml_et_flash_attn_ext_params *, void *);
extern int flash_attn_ext_f16_me_entry(struct ggml_et_flash_attn_ext_params *, void *);
extern int gated_delta_net_f32_entry(struct ggml_et_gated_delta_net_params *, void *);
extern int group_norm_f32_entry(struct ggml_et_group_norm_params *, void *);
extern int im2col_entry(struct ggml_et_im2col_params *, void *);
extern int l2_norm_f32_entry(struct ggml_et_l2_norm_params *, void *);
extern int mul_mat_id_f32_entry(struct ggml_et_mul_mat_id_params *, void *);
extern int norm_f32_entry(struct ggml_et_norm_params *, void *);
extern int pad_f32_entry(struct ggml_et_pad_params *, void *);
extern int repeat_f32_entry(struct ggml_et_repeat_params *, void *);
extern int rwkv_wkv6_f32_entry(struct ggml_et_rwkv_wkv6_params *, void *);
extern int rwkv_wkv7_f32_entry(struct ggml_et_rwkv_wkv7_params *, void *);
extern int scale_f32_entry(struct ggml_et_scale_params *, void *);
extern int set_f32_entry(struct ggml_et_set_params *, void *);
extern int solve_tri_f32_entry(struct ggml_et_solve_tri_params *, void *);
extern int sqr_f32_entry(struct ggml_et_sqr_params *, void *);
extern int ssm_conv_f32_entry(struct ggml_et_ssm_conv_params *, void *);
extern int ssm_scan_f32_entry(struct ggml_et_ssm_scan_params *, void *);
extern int sum_rows_f32_entry(struct ggml_et_sum_rows_params *, void *);
extern int tri_f32_entry(struct ggml_et_tri_params *, void *);
extern int mul_mat_f16_entry(struct ggml_et_binary_params *, void *);
extern int mul_mat_f16_matrix_engine_entry(struct ggml_et_binary_params *, void *);
extern int mul_mat_f32_entry(struct ggml_et_binary_params *, void *);
extern int mul_mat_f32_matrix_engine_entry(struct ggml_et_binary_params *, void *);
extern int mul_mat_Q8_0_entry(struct ggml_et_mm_q8_params *, void *);
extern int mul_mat_Q4_0_entry(struct ggml_et_binary_params *, void *);
static inline size_t tensor_bytes(const struct ggml_tensor * t) {
return (size_t) t->ne[0] * t->ne[1] * t->ne[2] * t->ne[3] * t->nb[0];
}
struct uber_glu_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor dst;
// trailing scalars omitted — not needed for eviction
};
struct uber_unary_params {
struct ggml_tensor src0;
struct ggml_tensor dst;
};
struct uber_rope_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor src2;
struct ggml_tensor dst;
};
struct uber_rms_norm_params {
struct ggml_tensor src0;
struct ggml_tensor dst;
};
struct uber_rms_norm_mul_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor dst;
};
struct uber_softmax_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor src2;
struct ggml_tensor dst;
};
struct uber_set_rows_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor dst;
};
struct uber_get_rows_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor dst;
};
struct uber_cont_params {
struct ggml_tensor src0;
struct ggml_tensor dst;
};
// src0 + src1 + dst (no trailing scalars needed for eviction)
struct uber_concat_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor dst;
};
struct uber_ssm_conv_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor dst;
};
struct uber_solve_tri_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor dst;
};
struct uber_mul_mat_id_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor src2;
struct ggml_tensor dst;
};
// flash_attn_ext: Q=src0, K=src1, V=src2, mask=src3, dst (mask optional)
struct uber_flash_attn_ext_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor src2;
struct ggml_tensor mask;
struct ggml_tensor dst;
};
// ssm_scan: 7 source tensors + dst
struct uber_ssm_scan_params {
struct ggml_tensor src0;
struct ggml_tensor src1;
struct ggml_tensor src2;
struct ggml_tensor src3;
struct ggml_tensor src4;
struct ggml_tensor src5;
struct ggml_tensor src6;
struct ggml_tensor dst;
};
// gated_delta_net: q,k,v,g,beta,state_in,dst
struct uber_gated_delta_net_params {
struct ggml_tensor q;
struct ggml_tensor k;
struct ggml_tensor v;
struct ggml_tensor g;
struct ggml_tensor beta;
struct ggml_tensor state_in;
struct ggml_tensor dst;
};
static void copy_f32_to_f16_row(uint16_t * dst, const float * src, int64_t num_elements) {
for (int64_t i = 0; i < num_elements; i++) {
dst[i] = fp32_to_fp16(src[i]);
}
}
static void copy_f32_row(float * dst, const float * src, int64_t num_elements) {
for (int64_t i = 0; i < num_elements; i++) {
dst[i] = src[i];
}
}
static void evict_region_past_l2_local(const void * addr, size_t bytes) {
if (!addr || bytes == 0) {
return;
}
const uint64_t CL = 64;
uint64_t base = (uint64_t) addr & ~(CL - 1);
uint64_t end = ((uint64_t) addr + bytes + CL - 1) & ~(CL - 1);
uint64_t nlines = (end - base) / CL;
cache_ops_priv_evict_sw(0, /*to_L2*/ 3, 0, 0, CL);
}
int entry_point(struct ggml_et_uberkernel_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env || !params) {
return -1;
}
struct ggml_et_uberkernel_inst * insts = (struct ggml_et_uberkernel_inst *) (uintptr_t) params->insts;
uint8_t * params_blob = (uint8_t *) (uintptr_t) params->params_blob;
if (!insts || !params_blob || params->inst_stride < sizeof(struct ggml_et_uberkernel_inst)) {
return -1;
}
for (uint32_t i = 0; i < params->num_insts; ++i) {
struct ggml_et_uberkernel_inst * inst =
(struct ggml_et_uberkernel_inst *) ((uint8_t *) insts + (i * params->inst_stride));
void * inst_params = params_blob + inst->params_offset;
int rc = -1;
et_barrier_global(32ULL);
switch (inst->kernel_id) {
case GGML_ET_UBERKERNEL_KERNEL_EL_MAP_F32:
{
struct ggml_et_binary_params * p = (struct ggml_et_binary_params *) inst_params;
rc = el_map_f32_entry(p, env);
break;
}
// case GGML_ET_UBERKERNEL_KERNEL_UNARY_F32: {
// // struct uber_unary_params *p = (struct uber_unary_params *) inst_params;
// // et_barrier(ET_BARRIER_GLOBAL);
// rc = unary_f32_entry((struct ggml_et_unary_params *) inst_params, env);
// break;
// }
// case GGML_ET_UBERKERNEL_KERNEL_CPY_F32_F16: {
// struct uber_unary_params *p = (struct uber_unary_params *) inst_params;
// // evict_region_past_l2(p->src0.data, tensor_bytes(&p->src0));
// rc = cpy_f32_f16_entry((struct ggml_et_cont_params *) inst_params, env);
// break;
// }
// case GGML_ET_UBERKERNEL_KERNEL_GET_ROWS_F32: {
// struct uber_get_rows_params *p = (struct uber_get_rows_params *) inst_params;
// rc = get_rows_f32_entry((struct ggml_et_get_rows_params *) inst_params, env);
// break;
// }
// case GGML_ET_UBERKERNEL_KERNEL_CONT_F32: {
// struct uber_cont_params *p = (struct uber_cont_params *) inst_params;
// // evict_region_past_l2_local(p->src0.data, tensor_bytes(&p->src0));
// // evict_region_past_l2(p->dst.data, tensor_bytes(&p->dst));
// rc = cont_f32_entry((struct ggml_et_cont_params *) inst_params, env);
// break;
// }
case GGML_ET_UBERKERNEL_KERNEL_GLU_F32:
{
rc = glu_f32_entry((struct ggml_et_glu_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_ROPE_F32:
{
rc = rope_f32_entry((struct ggml_et_rope_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_RMS_NORM_F32:
{
// struct ggml_et_rms_norm_params *p = (struct ggml_et_rms_norm_params *) inst_params;
// evict_region_past_l2(p->src0.data, tensor_bytes(&p->src0));
rc = rms_norm_f32_entry((struct ggml_et_rms_norm_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_RMS_NORM_MUL_F32:
{
struct uber_rms_norm_mul_params * p = (struct uber_rms_norm_mul_params *) inst_params;
evict_region_past_l2(p->src0.data, tensor_bytes(&p->src0));
evict_region_past_l2(p->src1.data, tensor_bytes(&p->src1));
rc = rms_norm_mul_f32_entry((struct ggml_et_rms_norm_mul_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_SOFTMAX_F32:
{
rc = softmax_f32_entry((struct ggml_et_softmax_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_SET_ROWS_F32:
{
rc = set_rows_f32_entry((struct ggml_et_set_rows_params *) inst_params, env);
break;
}
// Single-source ops (src0 → dst)
case GGML_ET_UBERKERNEL_KERNEL_SQR_F32:
{
rc = sqr_f32_entry((struct ggml_et_sqr_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_SCALE_F32:
{
rc = scale_f32_entry((struct ggml_et_scale_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_SUM_ROWS_F32:
{
rc = sum_rows_f32_entry((struct ggml_et_sum_rows_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_CUMSUM_F32:
{
rc = cumsum_f32_entry((struct ggml_et_cumsum_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_NORM_F32:
{
rc = norm_f32_entry((struct ggml_et_norm_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_L2_NORM_F32:
{
rc = l2_norm_f32_entry((struct ggml_et_l2_norm_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_GROUP_NORM_F32:
{
rc = group_norm_f32_entry((struct ggml_et_group_norm_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_REPEAT_F32:
{
rc = repeat_f32_entry((struct ggml_et_repeat_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_DIAG_F32:
{
rc = diag_f32_entry((struct ggml_et_diag_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_TRI_F32:
{
rc = tri_f32_entry((struct ggml_et_tri_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_PAD_F32:
{
rc = pad_f32_entry((struct ggml_et_pad_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_CONT_F16:
{
rc = cont_f16_entry((struct ggml_et_cont_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_FILL_F32:
{
rc = fill_f32_entry((struct ggml_et_fill_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_SET_F32:
{
rc = set_f32_entry((struct ggml_et_set_params *) inst_params, env);
break;
}
// Two-source ops
case GGML_ET_UBERKERNEL_KERNEL_CONCAT_F32:
{
rc = concat_f32_entry((struct ggml_et_concat_params *) inst_params, env);
break;
}
// case GGML_ET_UBERKERNEL_KERNEL_SSM_CONV_F32: {
// rc = ssm_conv_f32_entry((struct ggml_et_ssm_conv_params *) inst_params, env);
// break;
// }
case GGML_ET_UBERKERNEL_KERNEL_SOLVE_TRI_F32:
{
rc = solve_tri_f32_entry((struct ggml_et_solve_tri_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_IM2COL:
{
rc = im2col_entry((struct ggml_et_im2col_params *) inst_params, env);
break;
}
// Three-source ops
case GGML_ET_UBERKERNEL_KERNEL_MUL_MAT_ID_F32:
{
rc = mul_mat_id_f32_entry((struct ggml_et_mul_mat_id_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_FLASH_ATTN_EXT_F32:
{
rc = flash_attn_ext_f32_entry((struct ggml_et_flash_attn_ext_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_FLASH_ATTN_EXT_F16_ME:
{
rc = flash_attn_ext_f16_me_entry((struct ggml_et_flash_attn_ext_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_GATED_DELTA_NET_F32:
{
rc = gated_delta_net_f32_entry((struct ggml_et_gated_delta_net_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_SSM_SCAN_F32:
{
rc = ssm_scan_f32_entry((struct ggml_et_ssm_scan_params *) inst_params, env);
break;
}
// rwkv: raw float* params, no ggml_tensor fields to evict via
case GGML_ET_UBERKERNEL_KERNEL_RWKV_WKV6_F32:
{
rc = rwkv_wkv6_f32_entry((struct ggml_et_rwkv_wkv6_params *) inst_params, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_RWKV_WKV7_F32:
{
rc = rwkv_wkv7_f32_entry((struct ggml_et_rwkv_wkv7_params *) inst_params, env);
break;
}
// MUL_MAT: evict src1 (activations); src0=weights is
// read-only so never stale from a prior uberkernel op
case GGML_ET_UBERKERNEL_KERNEL_MUL_MAT_F16:
{
struct ggml_et_binary_params * p = (struct ggml_et_binary_params *) inst_params;
rc = mul_mat_f16_entry(p, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_MUL_MAT_F16_MATRIX_ENGINE:
{
struct ggml_et_binary_params * p = (struct ggml_et_binary_params *) inst_params;
rc = mul_mat_f16_matrix_engine_entry(p, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_MUL_MAT_F32:
{
struct ggml_et_binary_params * p = (struct ggml_et_binary_params *) inst_params;
rc = mul_mat_f32_entry(p, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_MUL_MAT_F32_MATRIX_ENGINE:
{
struct ggml_et_binary_params * p = (struct ggml_et_binary_params *) inst_params;
rc = mul_mat_f32_matrix_engine_entry(p, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_MUL_MAT_Q8_0:
{
struct ggml_et_mm_q8_params * p = (struct ggml_et_mm_q8_params *) inst_params;
// evict_region_past_l2(p->src0.data, tensor_bytes(&p->src0));
rc = mul_mat_Q8_0_entry(p, env);
break;
}
case GGML_ET_UBERKERNEL_KERNEL_MUL_MAT_Q4_0:
{
struct ggml_et_binary_params * p = (struct ggml_et_binary_params *) inst_params;
rc = mul_mat_Q4_0_entry(p, env);
break;
}
default:
return -1;
}
if (rc != 0) {
return rc;
}
}
return 0;
}
+705
View File
@@ -0,0 +1,705 @@
//******************************************************************************
// Unary F32 Kernel
// Element-wise unary operations: dst[i] = f(src0[i])
// All ops vectorized using 8-wide ET SIMD (fexp.ps, frcp.ps, flog.ps, etc.)
//
// Supports: ABS, SGN, NEG, STEP, TANH, ELU, RELU, SIGMOID, GELU, GELU_QUICK,
// SILU, HARDSWISH, HARDSIGMOID, EXP, EXPM1, SOFTPLUS, GELU_ERF
//******************************************************************************
#include "ggml_tensor.h"
#include "math_fp.h"
#include "platform.h"
#include <stdint.h>
// Unary kernel parameters structure
struct ggml_et_unary_params {
struct ggml_tensor src0; // F32 input tensor
struct ggml_tensor dst; // F32 output tensor
int32_t unary_op; // ggml_unary_op enum value
};
//******************************************************************************
// Vectorized 8-wide block operations
// All process exactly 8 floats per call using ET vector instructions.
// ne0 is guaranteed % 16 == 0, so the inner loop always calls with i0 += 8.
//******************************************************************************
// NEG: dst = -x (zero - x)
static inline void vec_neg(float * dst, const float * src, int32_t n) {
float zero = 0.0f;
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"fbc.ps f10, %[z]\n"
"flw.ps f11, %[x]\n"
"fsub.ps f12, f10, f11\n"
"fsw.ps f12, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [z] "m"(zero)
: "f10", "f11", "f12");
}
}
// ABS: dst = |x| (negate negative values: abs = x * sgn, or max(x, -x))
// Uses: negate then fmax.ps
static inline void vec_abs(float * dst, const float * src, int32_t n) {
float zero = 0.0f;
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"fbc.ps f10, %[z]\n"
"flw.ps f11, %[x]\n"
"fsub.ps f12, f10, f11\n" // f12 = -x
"fmax.ps f13, f11, f12\n" // f13 = max(x, -x) = |x|
"fsw.ps f13, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [z] "m"(zero)
: "f10", "f11", "f12", "f13");
}
}
// RELU: dst = max(0, x)
static inline void vec_relu(float * dst, const float * src, int32_t n) {
float zero = 0.0f;
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"fbc.ps f10, %[z]\n"
"flw.ps f11, %[x]\n"
"fmax.ps f12, f10, f11\n" // max(0, x)
"fsw.ps f12, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [z] "m"(zero)
: "f10", "f11", "f12");
}
}
// STEP: dst = x > 0 ? 1 : 0 (clamp to [0,1] via max then min-ish, or use sign bit)
// Trick: relu(x) then frcp gives inf for 0 and finite for >0, but simpler:
// step(x) = min(1, relu(x) * huge) ... too fragile. Scalar is fine for step/sgn.
static inline void vec_step(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i++) {
dst[i] = (src[i] > 0.0f) ? 1.0f : 0.0f;
}
}
// SGN: dst = sign(x) = x>0 ? 1 : (x<0 ? -1 : 0)
static inline void vec_sgn(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i++) {
dst[i] = (src[i] > 0.0f) ? 1.0f : ((src[i] < 0.0f) ? -1.0f : 0.0f);
}
}
// EXP: dst = exp(x)
// fexp.ps computes 2^x, so feed x * log2(e)
static inline void vec_exp(float * dst, const float * src, int32_t n) {
float log2e = 1.4426950408889634f;
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x]\n"
"fbc.ps f11, %[l2e]\n"
"fmul.ps f12, f10, f11\n" // x * log2(e)
"fexp.ps f13, f12\n" // 2^(x*log2e) = exp(x)
"fsw.ps f13, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [l2e] "m"(log2e)
: "f10", "f11", "f12", "f13");
}
}
// EXPM1: dst = exp(x) - 1
static inline void vec_expm1(float * dst, const float * src, int32_t n) {
float log2e = 1.4426950408889634f;
float one = 1.0f;
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x]\n"
"fbc.ps f11, %[l2e]\n"
"fbc.ps f14, %[one]\n"
"fmul.ps f12, f10, f11\n" // x * log2(e)
"fexp.ps f13, f12\n" // exp(x)
"fsub.ps f13, f13, f14\n" // exp(x) - 1
"fsw.ps f13, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [l2e] "m"(log2e), [one] "m"(one)
: "f10", "f11", "f12", "f13", "f14");
}
}
// SIGMOID: dst = 1 / (1 + exp(-x))
// Same pattern as SwiGLU: exp(-x) via fexp.ps, then frcp.ps
static inline void vec_sigmoid(float * dst, const float * src, int32_t n) {
float zero = 0.0f;
float one = 1.0f;
float log2e = 1.4426950408889634f;
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x]\n"
"fbc.ps f20, %[z]\n"
"fbc.ps f21, %[one]\n"
"fbc.ps f22, %[l2e]\n"
"fsub.ps f12, f20, f10\n" // -x
"fmul.ps f13, f12, f22\n" // -x * log2(e)
"fexp.ps f14, f13\n" // exp(-x)
"fadd.ps f15, f14, f21\n" // 1 + exp(-x)
"frcp.ps f16, f15\n" // 1 / (1 + exp(-x))
"fsw.ps f16, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [z] "m"(zero), [one] "m"(one), [l2e] "m"(log2e)
: "f10", "f12", "f13", "f14", "f15", "f16", "f20", "f21", "f22");
}
}
// TANH: dst = (exp(2x) - 1) / (exp(2x) + 1)
// Rewrite as: 1 - 2/(exp(2x) + 1) to use frcp.ps
// Or equivalently: 2*sigmoid(2x) - 1
static inline void vec_tanh(float * dst, const float * src, int32_t n) {
float one = 1.0f;
float two = 2.0f;
float two_log2e = 2.8853900817779268f; // 2 * log2(e)
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x]\n"
"fbc.ps f20, %[one]\n"
"fbc.ps f21, %[two]\n"
"fbc.ps f22, %[tl2e]\n"
// exp(2x) via fexp.ps: feed 2x * log2(e)
"fmul.ps f12, f10, f22\n" // 2x * log2(e)
"fexp.ps f13, f12\n" // exp(2x)
"fadd.ps f14, f13, f20\n" // exp(2x) + 1
"frcp.ps f15, f14\n" // 1 / (exp(2x) + 1)
"fmul.ps f16, f21, f15\n" // 2 / (exp(2x) + 1)
"fsub.ps f17, f20, f16\n" // 1 - 2/(exp(2x)+1) = tanh(x)
"fsw.ps f17, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [one] "m"(one), [two] "m"(two), [tl2e] "m"(two_log2e)
: "f10", "f12", "f13", "f14", "f15", "f16", "f17", "f20", "f21", "f22");
}
}
// SILU: dst = x / (1 + exp(-x)) = x * sigmoid(x)
// Copied from SwiGLU pattern but without the gate multiply
static inline void vec_silu(float * dst, const float * src, int32_t n) {
float zero = 0.0f;
float one = 1.0f;
float log2e = 1.4426950408889634f;
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x]\n"
"fbc.ps f20, %[z]\n"
"fbc.ps f21, %[one]\n"
"fbc.ps f22, %[l2e]\n"
"fsub.ps f12, f20, f10\n" // -x
"fmul.ps f13, f12, f22\n" // -x * log2(e)
"fexp.ps f14, f13\n" // exp(-x)
"fadd.ps f15, f14, f21\n" // 1 + exp(-x)
"frcp.ps f16, f15\n" // 1 / (1 + exp(-x))
"fmul.ps f17, f10, f16\n" // x * sigmoid(x)
"fsw.ps f17, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [z] "m"(zero), [one] "m"(one), [l2e] "m"(log2e)
: "f10", "f12", "f13", "f14", "f15", "f16", "f17", "f20", "f21", "f22");
}
}
// ELU: dst = x > 0 ? x : exp(x) - 1
// Vector: compute exp(x)-1 for all lanes, then fmax(x, exp(x)-1)
// Works because for x>0: x > exp(x)-1 is not always true...
// Actually for x>0, exp(x)-1 > x (since exp(x) > x+1 for x>0).
// So fmax won't work. Use: compute both, blend via comparison.
// Simpler: exp(x)-1 for all, then for x>0 overwrite with x.
// Without per-lane masking, do scalar for ELU.
static inline void vec_elu(float * dst, const float * src, int32_t n) {
float log2e = 1.4426950408889634f;
float one = 1.0f;
// Compute exp(x)-1 vectorized, then fixup positive elements
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x]\n"
"fbc.ps f11, %[l2e]\n"
"fbc.ps f14, %[one]\n"
"fmul.ps f12, f10, f11\n" // x * log2(e)
"fexp.ps f13, f12\n" // exp(x)
"fsub.ps f13, f13, f14\n" // exp(x) - 1
"fsw.ps f13, %[r]\n" // store exp(x)-1
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [l2e] "m"(log2e), [one] "m"(one)
: "f10", "f11", "f12", "f13", "f14");
// Fixup: for x > 0, dst = x
for (int32_t j = 0; j < 8 && (i + j) < n; j++) {
if (src[i + j] > 0.0f) {
dst[i + j] = src[i + j];
}
}
}
}
// GELU: 0.5*x*(1 + tanh(sqrt(2/pi) * x * (1 + 0.044715*x^2)))
// Reformulated as: x * (1 - 1/(exp(2z)+1)) where z = sqrt(2/pi)*x*(1+0.044715*x^2)
// NaN-safe: avoids inf*0. Copied from GeGLU block pattern.
static inline void vec_gelu(float * dst, const float * src, int32_t n) {
float one = 1.0f;
float half = 0.5f;
float coef_a = 0.044715f;
float sqrt2pi = 0.79788456080286535587989211986876f;
float two_log2e = 2.8853900817779268f; // 2 * log2(e)
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x]\n"
"fbc.ps f20, %[one]\n"
"fbc.ps f21, %[half]\n"
"fbc.ps f22, %[coef]\n"
"fbc.ps f23, %[s2pi]\n"
"fbc.ps f24, %[tl2e]\n"
// inner = 1 + 0.044715 * x^2
"fmul.ps f12, f10, f10\n" // x^2
"fmadd.ps f13, f22, f12, f20\n" // 1 + 0.044715*x^2
// z = sqrt(2/pi) * x * inner
"fmul.ps f14, f23, f10\n" // sqrt(2/pi) * x
"fmul.ps f14, f14, f13\n" // z
// exp(2z) via fexp.ps
"fmul.ps f15, f14, f24\n" // 2z * log2(e)
"fexp.ps f15, f15\n" // exp(2z)
// gelu(x) = 0.5 * x * (1 + tanh(z))
// = 0.5 * x * (1 + 1 - 2/(exp(2z)+1))
// = x * (1 - 1/(exp(2z)+1)) ... wait, that's tanh-based
// Actually: 0.5*x*(1 + tanh) = 0.5*x*(1 + 1 - 2/(e2z+1)) = x*(1 - 1/(e2z+1))
// Hmm: tanh = (e2z-1)/(e2z+1) = 1 - 2/(e2z+1)
// So 0.5*(1+tanh) = 0.5*(2 - 2/(e2z+1)) = 1 - 1/(e2z+1)
// gelu = x * (1 - 1/(e2z+1)) -- matches GeGLU pattern exactly
"fadd.ps f16, f15, f20\n" // exp(2z) + 1
"frcp.ps f16, f16\n" // 1/(exp(2z) + 1)
"fsub.ps f16, f20, f16\n" // 1 - 1/(exp(2z)+1) = sigmoid(2z)
"fmul.ps f17, f10, f16\n" // x * sigmoid(2z) = gelu(x)
"fsw.ps f17, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [one] "m"(one), [half] "m"(half), [coef] "m"(coef_a),
[s2pi] "m"(sqrt2pi), [tl2e] "m"(two_log2e)
: "f10", "f12", "f13", "f14", "f15", "f16", "f17", "f20", "f21", "f22", "f23", "f24");
}
}
// GELU_QUICK: x * sigmoid(1.702 * x) = x / (1 + exp(-1.702*x))
static inline void vec_gelu_quick(float * dst, const float * src, int32_t n) {
float one = 1.0f;
// -1.702 * log2(e) precomputed
float neg_coef_log2e = -1.702f * 1.4426950408889634f; // ~ -2.4542
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x]\n"
"fbc.ps f20, %[one]\n"
"fbc.ps f21, %[ncl2e]\n"
// exp(-1.702*x): feed -1.702*x*log2(e) = x * (-1.702*log2(e))
"fmul.ps f12, f10, f21\n" // x * (-1.702*log2(e))
"fexp.ps f13, f12\n" // exp(-1.702*x)
"fadd.ps f14, f13, f20\n" // 1 + exp(-1.702*x)
"frcp.ps f15, f14\n" // sigmoid(1.702*x)
"fmul.ps f16, f10, f15\n" // x * sigmoid(1.702*x)
"fsw.ps f16, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [one] "m"(one), [ncl2e] "m"(neg_coef_log2e)
: "f10", "f12", "f13", "f14", "f15", "f16", "f20", "f21");
}
}
// GELU_ERF: 0.5 * x * (1 + erf(x / sqrt(2)))
// erf approximation (Abramowitz & Stegun) is hard to vectorize cleanly, keep scalar
// but use et_expf for the exp(-z^2) part
static inline void vec_gelu_erf(float * dst, const float * src, int32_t n) {
const float SQRT_2_INV = 0.70710678118654752440084436210484f;
for (int32_t i = 0; i < n; i++) {
float x = src[i];
float z = x * SQRT_2_INV;
float az = z < 0.0f ? -z : z;
float t = et_fdiv(1.0f, 1.0f + 0.3275911f * az);
float t2 = t * t;
float t3 = t2 * t;
float t4 = t3 * t;
float t5 = t4 * t;
float poly = 0.254829592f * t - 0.284496736f * t2 + 1.421413741f * t3 - 1.453152027f * t4 + 1.061405429f * t5;
float erf_pos = 1.0f - poly * et_expf(-(az * az));
float erf_val = (z < 0.0f) ? -erf_pos : erf_pos;
dst[i] = 0.5f * x * (1.0f + erf_val);
}
}
// HARDSIGMOID: min(1, max(0, (x + 3) / 6))
// Vector: compute (x+3)/6 via frcp, then clamp with fmax(0) and fmin(1)
static inline void vec_hardsigmoid(float * dst, const float * src, int32_t n) {
float zero = 0.0f;
float one = 1.0f;
float three = 3.0f;
float inv6 = 0.16666666666666666f; // 1/6
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x]\n"
"fbc.ps f20, %[z]\n"
"fbc.ps f21, %[one]\n"
"fbc.ps f22, %[thr]\n"
"fbc.ps f23, %[inv]\n"
"fadd.ps f12, f10, f22\n" // x + 3
"fmul.ps f13, f12, f23\n" // (x + 3) / 6
"fmax.ps f14, f13, f20\n" // max(0, ...)
"fmin.ps f15, f14, f21\n" // min(1, ...)
"fsw.ps f15, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [z] "m"(zero), [one] "m"(one), [thr] "m"(three), [inv] "m"(inv6)
: "f10", "f12", "f13", "f14", "f15", "f20", "f21", "f22", "f23");
}
}
// HARDSWISH: x * hardsigmoid(x) = x * min(1, max(0, (x+3)/6))
static inline void vec_hardswish(float * dst, const float * src, int32_t n) {
float zero = 0.0f;
float one = 1.0f;
float three = 3.0f;
float inv6 = 0.16666666666666666f;
for (int32_t i = 0; i < n; i += 8) {
__asm__ volatile(
"flw.ps f10, %[x]\n"
"fbc.ps f20, %[z]\n"
"fbc.ps f21, %[one]\n"
"fbc.ps f22, %[thr]\n"
"fbc.ps f23, %[inv]\n"
"fadd.ps f12, f10, f22\n" // x + 3
"fmul.ps f13, f12, f23\n" // (x + 3) / 6
"fmax.ps f14, f13, f20\n" // max(0, ...)
"fmin.ps f15, f14, f21\n" // min(1, ...)
"fmul.ps f16, f10, f15\n" // x * hardsigmoid(x)
"fsw.ps f16, %[r]\n"
: [r] "=m"(*(float (*)[8]) & dst[i])
: [x] "m"(*(const float (*)[8]) & src[i]), [z] "m"(zero), [one] "m"(one), [thr] "m"(three), [inv] "m"(inv6)
: "f10", "f12", "f13", "f14", "f15", "f16", "f20", "f21", "f22", "f23");
}
}
// FLOOR: largest integer <= x
static inline void vec_floor(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i++) {
float x = src[i];
float t = (float) (int32_t) x;
dst[i] = (t > x) ? t - 1.0f : t;
}
}
// CEIL: smallest integer >= x
static inline void vec_ceil(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i++) {
float x = src[i];
float t = (float) (int32_t) x;
dst[i] = (t < x) ? t + 1.0f : t;
}
}
// TRUNC: round towards zero
static inline void vec_trunc(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i++) {
dst[i] = (float) (int32_t) src[i];
}
}
// ROUND: round to nearest, ties to even (banker's rounding)
static inline void vec_round(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i++) {
float x = src[i];
float t = (float) (int32_t) x;
float diff = x - t;
if (diff > 0.5f || (diff == 0.5f && ((int32_t) t & 1))) {
t += 1.0f;
} else if (diff < -0.5f || (diff == -0.5f && ((int32_t) t & 1))) {
t -= 1.0f;
}
dst[i] = t;
}
}
// SOFTPLUS: log(1 + exp(x))
// For large x (>20), softplus(x) ~ x. For moderate x, use fexp + flog.
// Scalar fallback since flog.ps computes log2, need conversion, and overflow guard
static inline void vec_softplus(float * dst, const float * src, int32_t n) {
for (int32_t i = 0; i < n; i++) {
float x = src[i];
dst[i] = (x > 20.0f) ? x : et_logf(1.0f + et_expf(x));
}
}
static inline size_t tensor_bytes(const struct ggml_tensor * t) {
return (size_t) t->ne[0] * t->ne[1] * t->ne[2] * t->ne[3] * t->nb[0];
}
//******************************************************************************
// Main entry point
//******************************************************************************
int entry_point(struct ggml_et_unary_params * params, void * env) {
kernel_environment_t * kernel_env = (kernel_environment_t *) env;
if (!kernel_env) {
return -1;
}
int thread_id = get_relative_thread_id(kernel_env->shire_mask);
int num_threads = get_num_threads(kernel_env->shire_mask);
if (thread_id < 0) {
return 0;
}
if (params == 0 || ((uint64_t) params & 0x7) != 0) {
return -1;
}
struct ggml_tensor * src0 = &params->src0;
struct ggml_tensor * dst = &params->dst;
// evict_region_past_l2(&params->unary_op, sizeof(int32_t));
// WAIT_CACHEOPS;
// FENCE;
int32_t unary_op = params->unary_op;
if (src0->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return -1;
}
float * src0_data = (float *) src0->data;
float * dst_data = (float *) dst->data;
if (!src0_data || !dst_data) {
return -1;
}
// evict_region_past_l2(src0_data, tensor_bytes(src0));
// evict_region_past_l2(dst_data, tensor_bytes(dst));
// WAIT_CACHEOPS;
// FENCE;
// et_barrier(ET_BARRIER_GLOBAL);
// Tensor layout: src and dst are F32 with at least dim-0 contiguity
// - nb[0] == sizeof(float) (rows are dense; SIMD loads stay legal)
// - nb[1], nb[2], nb[3] may all be arbitrary strides for 4D views
//
// We walk rows independently and decompose row index r into (i1,i2,i3),
// computing per-row byte offsets via nb[1..3] of each tensor.
const int64_t nc = dst->ne[0]; // row width (logical)
const int64_t ne1 = dst->ne[1];
const int64_t ne2 = dst->ne[2];
const int64_t nr = ne1 * ne2 * dst->ne[3]; // total rows
const int64_t total_elements = nr * nc;
const size_t s_nb1 = src0->nb[1], s_nb2 = src0->nb[2], s_nb3 = src0->nb[3];
const size_t d_nb1 = dst->nb[1], d_nb2 = dst->nb[2], d_nb3 = dst->nb[3];
// evict_region_past_l2(src0_data, tensor_bytes(src0));
// evict_region_past_l2(dst_data, tensor_bytes(dst));
// FENCE;
// WAIT_CACHEOPS;
// et_barrier(ET_BARRIER_GLOBAL);
const int64_t elements_per_cacheline = 16; // 64 bytes / 4 bytes per float
const int64_t total_cachelines = (total_elements + elements_per_cacheline - 1) / elements_per_cacheline;
const int64_t cl_per_thread = (total_cachelines + num_threads - 1) / num_threads;
const int64_t cl_start = thread_id * cl_per_thread;
int64_t cl_end = cl_start + cl_per_thread;
if (cl_end > total_cachelines) {
cl_end = total_cachelines;
}
if (cl_start >= total_cachelines) {
return 0;
}
const int64_t elem_start = cl_start * elements_per_cacheline;
int64_t elem_end = cl_end * elements_per_cacheline;
if (elem_end > total_elements) {
elem_end = total_elements;
}
// Fast path: tensor is fully contiguous (no view), walk it as a flat array.
// This preserves perf for the common case and avoids the per-row dispatch loop.
const size_t row_bytes = (size_t) nc * sizeof(float);
// evict_region_past_l2((src0_data + elem_start), row_bytes);
// // evict_region_past_l2((dst_data + elem_start), row_bytes);
// FENCE;
// WAIT_CACHEOPS;
// et_barrier(ET_BARRIER_GLOBAL);
const int is_flat = s_nb1 == row_bytes && s_nb2 == s_nb1 * (size_t) ne1 && s_nb3 == s_nb2 * (size_t) ne2 &&
d_nb1 == row_bytes && d_nb2 == d_nb1 * (size_t) ne1 && d_nb3 == d_nb2 * (size_t) ne2;
if (is_flat) {
float * src_ptr = src0_data + elem_start;
// evict_region_past_l2(src_ptr, 1024);
float * dst_ptr = dst_data + elem_start;
const int32_t count = (int32_t) (elem_end - elem_start);
switch (unary_op) {
case GGML_UNARY_OP_NEG:
vec_neg(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_ABS:
vec_abs(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_SGN:
vec_sgn(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_STEP:
vec_step(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_RELU:
vec_relu(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_EXP:
vec_exp(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_EXPM1:
vec_expm1(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_SIGMOID:
vec_sigmoid(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_TANH:
vec_tanh(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_SILU:
vec_silu(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_ELU:
vec_elu(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_GELU:
vec_gelu(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_GELU_QUICK:
vec_gelu_quick(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_GELU_ERF:
vec_gelu_erf(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_HARDSWISH:
vec_hardswish(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_HARDSIGMOID:
vec_hardsigmoid(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_SOFTPLUS:
vec_softplus(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_FLOOR:
vec_floor(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_CEIL:
vec_ceil(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_ROUND:
vec_round(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_TRUNC:
vec_trunc(dst_ptr, src_ptr, count);
break;
default:
return -1;
}
return 0;
}
// Slow path: arbitrary 4D-strided view. Walk the assigned element range
// row-by-row, clipping each segment to a row boundary so we never cross
// nb[1]. For each row index r, decompose into (i1,i2,i3) and add the
// corresponding nb[*] byte offsets to the base pointers.
int64_t e = elem_start;
while (e < elem_end) {
int64_t row = e / nc;
int64_t col = e % nc;
int64_t take = nc - col;
if (take > elem_end - e) {
take = elem_end - e;
}
// Decompose row into (i3,i2,i1) using row-major linearization
const int64_t i1 = row % ne1;
const int64_t r2 = row / ne1;
const int64_t i2 = r2 % ne2;
const int64_t i3 = r2 / ne2;
float * src_ptr = (float *) ((char *) src0_data + i3 * s_nb3 + i2 * s_nb2 + i1 * s_nb1) + col;
float * dst_ptr = (float *) ((char *) dst_data + i3 * d_nb3 + i2 * d_nb2 + i1 * d_nb1) + col;
const int32_t count = (int32_t) take;
// evict_region_past_l2(src_ptr, 1024);
// FENCE;
// et_barrier(ET_BARRIER_GLOBAL);
switch (unary_op) {
case GGML_UNARY_OP_NEG:
vec_neg(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_ABS:
vec_abs(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_SGN:
vec_sgn(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_STEP:
vec_step(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_RELU:
vec_relu(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_EXP:
vec_exp(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_EXPM1:
vec_expm1(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_SIGMOID:
vec_sigmoid(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_TANH:
vec_tanh(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_SILU:
vec_silu(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_ELU:
vec_elu(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_GELU:
vec_gelu(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_GELU_QUICK:
vec_gelu_quick(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_GELU_ERF:
vec_gelu_erf(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_HARDSWISH:
vec_hardswish(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_HARDSIGMOID:
vec_hardsigmoid(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_SOFTPLUS:
vec_softplus(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_FLOOR:
vec_floor(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_CEIL:
vec_ceil(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_ROUND:
vec_round(dst_ptr, src_ptr, count);
break;
case GGML_UNARY_OP_TRUNC:
vec_trunc(dst_ptr, src_ptr, count);
break;
default:
return -1;
}
e += take;
}
return 0;
}
+86
View File
@@ -0,0 +1,86 @@
#pragma once
#include "ggml-backend-impl.h"
#include "ggml-et-uberkernel-common.h"
#include <device-layer/IDeviceLayer.h>
#include <runtime/IProfiler.h>
#include <runtime/IRuntime.h>
#include <cstdint>
#include <fstream>
#include <string>
#include <unordered_map>
#include <vector>
std::shared_ptr<rt::IRuntime> ggml_et_runtime();
struct ggml_backend_et_buffer_type_context {
int devidx;
std::string name;
};
struct ggml_backend_et_buffer_context {
int devidx;
void * data; // Device memory pointer
size_t size;
rt::DeviceId rtid;
};
struct ggml_backend_et_context {
int devidx;
};
struct ggml_backend_et_device_context;
// One slot in the uberkernel ring. The host vectors back the H2D copy and
// must outlive the upload; the device buffers feed the kernel that consumes
// them. pending_event lets us know when both have drained so the slot can
// be recycled.
struct ggml_backend_et_uberkernel_slot {
std::vector<ggml_et_uberkernel_inst> insts;
std::vector<std::byte> params_blob;
std::byte * device_insts = nullptr;
std::byte * device_params = nullptr;
size_t device_insts_capacity = 0;
size_t device_params_capacity = 0;
rt::EventId pending_event{};
bool has_pending = false;
};
struct ggml_backend_et_uberkernel_context {
bool failed = false;
uint64_t shire_mask = 0;
// Ring of slots. We accumulate into slots[current_slot]; on segment
// commit we fire the H2D + launch and rotate to the next slot,
// waiting on its previous launch only if it hasn't drained yet.
static constexpr size_t SLOT_COUNT = 4;
ggml_backend_et_uberkernel_slot slots[SLOT_COUNT];
size_t current_slot = 0;
};
struct ggml_backend_et_device_context {
int devidx;
rt::DeviceId rtid;
std::string name;
std::string desc;
size_t total_mem;
ggml_backend_buffer_type_t buftype;
// Kernel management - default stream for ordered execution on this device
rt::StreamId default_stream;
std::unordered_map<std::string, rt::KernelId> loaded_kernels;
// trace buffer - for printing support
std::byte * trace_buffer;
bool uberkernel_enabled = false;
ggml_backend_et_uberkernel_context uberkernel;
};
struct ggml_backend_et_reg_ctx {
std::vector<ggml_backend_dev_t> devices;
};
+497
View File
@@ -0,0 +1,497 @@
#include "ggml-et-cpu-compare.h"
#include "ggml-cpu/ggml-cpu-impl.h"
#include "ggml-cpu/ops.h"
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
bool ggml_et_cpu_compare_init_pre(ggml_et_cpu_compare_ctx * ctx, const ggml_tensor * node, ggml_op op) {
if (!ctx || !node) {
GGML_LOG_ERROR("ET: Invalid parameters for CPU compare init\n");
return false;
}
// Clear context
memset(ctx, 0, sizeof(*ctx));
// Calculate actual buffer sizes - use backend buffer size for accurate copy
auto get_tensor_buffer_size = [](const ggml_tensor * tensor) -> size_t {
if (!tensor) {
return 0;
}
if (tensor->buffer) {
// Get actual backend buffer size
size_t buffer_size = ggml_backend_buffer_get_size(tensor->buffer);
// Use the full buffer size to avoid any truncation issues
return buffer_size;
} else {
// Fallback to logical size if no buffer
return ggml_nbytes(tensor);
}
};
ctx->src0_size = get_tensor_buffer_size(node->src[0]);
ctx->src1_size = get_tensor_buffer_size(node->src[1]);
ctx->src2_size = get_tensor_buffer_size(node->src[2]);
ctx->dst_size = get_tensor_buffer_size(node);
// Allocate CPU buffers for all tensors
if (ctx->src0_size > 0) {
ctx->cpu_src0_data = malloc(ctx->src0_size);
if (!ctx->cpu_src0_data) {
GGML_LOG_ERROR("ET: Failed to allocate CPU src0 buffer\n");
goto cleanup;
}
}
if (ctx->src1_size > 0) {
ctx->cpu_src1_data = malloc(ctx->src1_size);
if (!ctx->cpu_src1_data) {
GGML_LOG_ERROR("ET: Failed to allocate CPU src1 buffer\n");
goto cleanup;
}
}
if (ctx->src2_size > 0) {
ctx->cpu_src2_data = malloc(ctx->src2_size);
if (!ctx->cpu_src2_data) {
GGML_LOG_ERROR("ET: Failed to allocate CPU src2 buffer\n");
goto cleanup;
}
}
ctx->cpu_dst_data = malloc(ctx->dst_size);
if (!ctx->cpu_dst_data) {
GGML_LOG_ERROR("ET: Failed to allocate CPU dst buffer\n");
goto cleanup;
}
ctx->et_dst_data = malloc(ctx->dst_size);
if (!ctx->et_dst_data) {
GGML_LOG_ERROR("ET: Failed to allocate ET dst buffer\n");
goto cleanup;
}
// Copy data from ET device buffers to CPU host buffers
if (ctx->src0_size > 0) {
// Copy logical tensor size - ggml_backend_tensor_get handles stride layout internally
size_t logical_size = ggml_nbytes(node->src[0]);
ggml_backend_tensor_get(node->src[0], ctx->cpu_src0_data, 0, logical_size);
}
if (ctx->src1_size > 0) {
size_t logical_size = ggml_nbytes(node->src[1]);
ggml_backend_tensor_get(node->src[1], ctx->cpu_src1_data, 0, logical_size);
}
if (ctx->src2_size > 0) {
size_t logical_size = ggml_nbytes(node->src[2]);
ggml_backend_tensor_get(node->src[2], ctx->cpu_src2_data, 0, logical_size);
}
// Copy destination data from device (for operations like SET_ROWS that modify existing data)
// Most ops create new tensors so this is unused, but SET_ROWS requires existing dst data
{
size_t logical_size = ggml_nbytes(node);
ggml_backend_tensor_get(node, ctx->cpu_dst_data, 0, logical_size);
}
// Create CPU backend for reference computation
GGML_LOG_DEBUG("ET: Creating CPU backend for reference computation\n");
ctx->cpu_backend = ggml_backend_cpu_init();
if (!ctx->cpu_backend) {
GGML_LOG_ERROR("ET: Failed to create CPU backend\n");
goto cleanup;
}
// Create GGML context for CPU tensors
GGML_LOG_DEBUG("ET: Creating GGML context for CPU computation\n");
ggml_init_params ctx_params;
ctx_params.mem_size = ggml_tensor_overhead() * 4 + ggml_graph_overhead(); // up to 4 tensors + graph
ctx_params.mem_buffer = nullptr;
ctx_params.no_alloc = true; // We'll manage data ourselves
ctx->ggml_ctx = ggml_init(ctx_params);
if (!ctx->ggml_ctx) {
GGML_LOG_ERROR("ET: Failed to create GGML context\n");
goto cleanup;
}
// Create CPU tensors with proper context
if (node->src[0]) {
ctx->cpu_src0 = ggml_new_tensor(ctx->ggml_ctx, node->src[0]->type, GGML_MAX_DIMS, node->src[0]->ne);
if (!ctx->cpu_src0) {
GGML_LOG_ERROR("ET: Failed to create CPU src0 tensor\n");
goto cleanup;
}
ctx->cpu_src0->data = ctx->cpu_src0_data;
// Copy stride array (nb) for correct memory layout
memcpy(ctx->cpu_src0->nb, node->src[0]->nb, sizeof(node->src[0]->nb));
// Copy op_params if present
memcpy(ctx->cpu_src0->op_params, node->src[0]->op_params, sizeof(node->src[0]->op_params));
}
if (node->src[1]) {
ctx->cpu_src1 = ggml_new_tensor(ctx->ggml_ctx, node->src[1]->type, GGML_MAX_DIMS, node->src[1]->ne);
if (!ctx->cpu_src1) {
GGML_LOG_ERROR("ET: Failed to create CPU src1 tensor\n");
goto cleanup;
}
ctx->cpu_src1->data = ctx->cpu_src1_data;
// Copy stride array (nb) for correct memory layout
memcpy(ctx->cpu_src1->nb, node->src[1]->nb, sizeof(node->src[1]->nb));
// Copy op_params if present
memcpy(ctx->cpu_src1->op_params, node->src[1]->op_params, sizeof(node->src[1]->op_params));
}
if (node->src[2]) {
ctx->cpu_src2 = ggml_new_tensor(ctx->ggml_ctx, node->src[2]->type, GGML_MAX_DIMS, node->src[2]->ne);
if (!ctx->cpu_src2) {
GGML_LOG_ERROR("ET: Failed to create CPU src2 tensor\n");
goto cleanup;
}
ctx->cpu_src2->data = ctx->cpu_src2_data;
// Copy stride array (nb) for correct memory layout
memcpy(ctx->cpu_src2->nb, node->src[2]->nb, sizeof(node->src[2]->nb));
// Copy op_params if present
memcpy(ctx->cpu_src2->op_params, node->src[2]->op_params, sizeof(node->src[2]->op_params));
}
return true;
cleanup:
ggml_et_cpu_compare_free(ctx);
return false;
}
bool ggml_et_cpu_compare_compute_and_check(ggml_et_cpu_compare_ctx * ctx,
const ggml_tensor * node,
const ggml_et_cpu_compare_config * config) {
if (!ctx || !ctx->cpu_backend || !ctx->ggml_ctx || !node || !config) {
GGML_LOG_ERROR("ET: Invalid parameters for CPU compute and check\n");
return false;
}
// Create operation-specific CPU destination tensor based on the node's operation
ggml_op op = node->op;
switch (op) {
case GGML_OP_MUL:
ctx->cpu_dst = ggml_mul(ctx->ggml_ctx, ctx->cpu_src0, ctx->cpu_src1);
break;
case GGML_OP_ADD:
ctx->cpu_dst = ggml_add(ctx->ggml_ctx, ctx->cpu_src0, ctx->cpu_src1);
break;
case GGML_OP_MUL_MAT:
ctx->cpu_dst = ggml_mul_mat(ctx->ggml_ctx, ctx->cpu_src0, ctx->cpu_src1);
break;
case GGML_OP_MUL_MAT_ID:
// MUL_MAT_ID: Mixture of Experts matrix multiplication
// src0 (as): expert weight matrices [K, M, n_expert]
// src1 (b): activations [K, n_expert_used, batch]
// src2 (ids): expert selection indices [n_expert_used, batch]
ctx->cpu_dst = ggml_mul_mat_id(ctx->ggml_ctx, ctx->cpu_src0, ctx->cpu_src1, ctx->cpu_src2);
break;
case GGML_OP_ROPE:
{
const int32_t * op_params = (const int32_t *) node->op_params;
const int32_t n_dims = op_params[1];
const int32_t mode = op_params[2];
const int32_t n_ctx_orig = op_params[4];
const float freq_base = *((const float *) (op_params + 5));
const float freq_scale = *((const float *) (op_params + 6));
const float ext_factor = *((const float *) (op_params + 7));
const float attn_factor = *((const float *) (op_params + 8));
const float beta_fast = *((const float *) (op_params + 9));
const float beta_slow = *((const float *) (op_params + 10));
if (mode & GGML_ROPE_TYPE_MROPE) {
int sections[GGML_MROPE_SECTIONS];
memcpy(sections, op_params + 11, sizeof(sections));
ctx->cpu_dst = ggml_rope_multi(ctx->ggml_ctx, ctx->cpu_src0, ctx->cpu_src1, ctx->cpu_src2,
n_dims, sections, mode, n_ctx_orig, freq_base, freq_scale,
ext_factor, attn_factor, beta_fast, beta_slow);
} else {
ctx->cpu_dst = ggml_rope_ext(ctx->ggml_ctx, ctx->cpu_src0, ctx->cpu_src1, ctx->cpu_src2,
n_dims, mode, n_ctx_orig, freq_base, freq_scale, ext_factor,
attn_factor, beta_fast, beta_slow);
}
}
break;
case GGML_OP_RMS_NORM:
// Extract epsilon parameter from op_params (stored as float)
{
float eps;
memcpy(&eps, node->op_params, sizeof(float));
ctx->cpu_dst = ggml_rms_norm(ctx->ggml_ctx, ctx->cpu_src0, eps);
}
break;
case GGML_OP_SQR:
ctx->cpu_dst = ggml_sqr(ctx->ggml_ctx, ctx->cpu_src0);
break;
case GGML_OP_UNARY:
{
ggml_unary_op uop = (ggml_unary_op) ggml_get_op_params_i32(node, 0);
ctx->cpu_dst = ggml_unary(ctx->ggml_ctx, ctx->cpu_src0, uop);
}
break;
case GGML_OP_SUM_ROWS:
ctx->cpu_dst = ggml_sum_rows(ctx->ggml_ctx, ctx->cpu_src0);
break;
case GGML_OP_MEAN:
ctx->cpu_dst = ggml_mean(ctx->ggml_ctx, ctx->cpu_src0);
break;
case GGML_OP_CLAMP:
{
float clamp_min, clamp_max;
memcpy(&clamp_min, (const float *) node->op_params + 0, sizeof(float));
memcpy(&clamp_max, (const float *) node->op_params + 1, sizeof(float));
ctx->cpu_dst = ggml_clamp(ctx->ggml_ctx, ctx->cpu_src0, clamp_min, clamp_max);
}
break;
case GGML_OP_GLU:
// Extract GLU parameters from op_params (split mode only)
{
int32_t glu_op_type = ggml_get_op_params_i32(node, 0); // GLU variant
ggml_glu_op glu_op = (ggml_glu_op) glu_op_type;
// Only support split tensor mode
if (!ctx->cpu_src1) {
GGML_LOG_ERROR("ET: GLU CPU comparison requires split tensor mode\n");
return false;
}
ctx->cpu_dst = ggml_glu_split(ctx->ggml_ctx, ctx->cpu_src0, ctx->cpu_src1, glu_op);
}
break;
case GGML_OP_SOFT_MAX:
{
// Extract scale and max_bias from op_params
float scale = 1.0f;
float max_bias = 0.0f;
memcpy(&scale, (const float *) node->op_params + 0, sizeof(float));
memcpy(&max_bias, (const float *) node->op_params + 1, sizeof(float));
if (ctx->cpu_src1 || scale != 1.0f || max_bias != 0.0f) {
// Use extended softmax when mask or non-default parameters are present
ctx->cpu_dst = ggml_soft_max_ext(ctx->ggml_ctx, ctx->cpu_src0, ctx->cpu_src1, scale, max_bias);
} else {
// Use simple softmax when no mask and default parameters
ctx->cpu_dst = ggml_soft_max(ctx->ggml_ctx, ctx->cpu_src0);
}
// Add sinks if present
if (ctx->cpu_src2) {
ggml_soft_max_add_sinks(ctx->cpu_dst, ctx->cpu_src2);
}
}
break;
case GGML_OP_GET_ROWS:
ctx->cpu_dst = ggml_get_rows(ctx->ggml_ctx, ctx->cpu_src0, ctx->cpu_src1);
break;
case GGML_OP_CONT:
ctx->cpu_dst = ggml_cont(ctx->ggml_ctx, ctx->cpu_src0);
break;
case GGML_OP_SET_ROWS:
{
// SET_ROWS operation scatters src0 rows to dst[src1] positions
// Create destination tensor (this is the "view" that SET_ROWS returns)
ggml_tensor * cpu_dst_base = ggml_new_tensor(ctx->ggml_ctx, node->type, GGML_MAX_DIMS, node->ne);
if (!cpu_dst_base) {
GGML_LOG_ERROR("ET: Failed to create CPU destination base tensor for SET_ROWS\n");
return false;
}
cpu_dst_base->data = ctx->cpu_dst_data;
memcpy(cpu_dst_base->nb, node->nb, sizeof(node->nb));
// Note: cpu_dst_data already contains the pre-existing destination data from device
// SET_ROWS will update specific rows, leaving others unchanged
// Perform SET_ROWS operation: returns a view that scatters src0 rows to dst[src1] positions
ctx->cpu_dst = ggml_set_rows(ctx->ggml_ctx, cpu_dst_base, ctx->cpu_src0, ctx->cpu_src1);
}
break;
default:
GGML_LOG_ERROR("ET: Unsupported operation %s for CPU comparison\n", ggml_op_name(op));
return false;
}
if (!ctx->cpu_dst) {
GGML_LOG_ERROR("ET: Failed to create CPU destination tensor for operation %s\n", ggml_op_name(op));
return false;
}
ctx->cpu_dst->data = ctx->cpu_dst_data;
// Copy stride array (nb) for correct memory layout - except for CONT which should keep contiguous strides
if (op != GGML_OP_CONT) {
memcpy(ctx->cpu_dst->nb, node->nb, sizeof(node->nb));
}
// For CONT operations, keep the contiguous strides created by ggml_cont()
// Create minimal computation graph
ctx->cpu_graph = ggml_new_graph_custom(ctx->ggml_ctx, 1, false);
if (!ctx->cpu_graph) {
GGML_LOG_ERROR("ET: Failed to create CPU computation graph\n");
return false;
}
ctx->cpu_graph->nodes[0] = ctx->cpu_dst;
ctx->cpu_graph->n_nodes = 1;
// Log input data for debugging if enabled
if (config && config->log_differences) {
if (ctx->cpu_src0_data && ctx->src0_size >= 4) {
GGML_LOG_DEBUG("ET: CPU src0 first few bytes: %02x %02x %02x %02x\n", ((uint8_t *) ctx->cpu_src0_data)[0],
((uint8_t *) ctx->cpu_src0_data)[1], ((uint8_t *) ctx->cpu_src0_data)[2],
((uint8_t *) ctx->cpu_src0_data)[3]);
}
if (ctx->cpu_src1_data && ctx->src1_size >= 16) {
GGML_LOG_DEBUG("ET: CPU src1 first few floats: %.6f %.6f %.6f %.6f\n", ((float *) ctx->cpu_src1_data)[0],
((float *) ctx->cpu_src1_data)[1], ((float *) ctx->cpu_src1_data)[2],
((float *) ctx->cpu_src1_data)[3]);
}
}
// Compute using CPU backend
ggml_status cpu_result = ggml_backend_graph_compute(ctx->cpu_backend, ctx->cpu_graph);
if (cpu_result != GGML_STATUS_SUCCESS) {
GGML_LOG_ERROR("ET: CPU reference computation failed with status %d\n", cpu_result);
return false;
}
// Log output data for debugging if enabled
if (config && config->log_differences && ctx->dst_size >= 16) {
GGML_LOG_DEBUG("ET: CPU dst first few floats after computation: %.6f %.6f %.6f %.6f\n",
((float *) ctx->cpu_dst_data)[0], ((float *) ctx->cpu_dst_data)[1],
((float *) ctx->cpu_dst_data)[2], ((float *) ctx->cpu_dst_data)[3]);
}
// Now copy ET device destination to host for comparison
size_t dst_logical_size = ggml_nbytes(node);
ggml_backend_tensor_get(node, ctx->et_dst_data, 0, dst_logical_size);
if (config->log_differences) {
size_t num_elements = ggml_nelements(node);
size_t max_log = std::min(num_elements, config->max_log_elements);
// Check if this is an elementwise operation that can show src inputs
bool is_elementwise = (op == GGML_OP_MUL || op == GGML_OP_ADD || op == GGML_OP_GLU);
float * cpu_src0_float = is_elementwise ? (float *) ctx->cpu_src0_data : nullptr;
float * cpu_src1_float = is_elementwise ? (float *) ctx->cpu_src1_data : nullptr;
// Helper to get float value from tensor data (handles f16 and f32)
auto get_float = [](const void * data, size_t idx, ggml_type type) -> float {
if (type == GGML_TYPE_F16) {
const ggml_fp16_t * fp16_data = (const ggml_fp16_t *) data;
return ggml_fp16_to_fp32(fp16_data[idx]);
}
const float * float_data = (const float *) data;
return float_data[idx];
};
// Compare all elements but log only the first max_log_elements
bool matches = true;
size_t total_mismatches = 0;
// First pass: check all elements for mismatches
for (size_t i = 0; i < num_elements; i++) {
float cpu_val = get_float(ctx->cpu_dst_data, i, node->type);
float et_val = get_float(ctx->et_dst_data, i, node->type);
float diff = fabsf(cpu_val - et_val);
float rel_diff = diff / (fabsf(cpu_val) + 1e-8f);
if (rel_diff > config->tolerance) {
matches = false;
total_mismatches++;
}
}
// Second pass: log detailed info for first max_log elements only
for (size_t i = 0; i < max_log; i++) {
float cpu_val = get_float(ctx->cpu_dst_data, i, node->type);
float et_val = get_float(ctx->et_dst_data, i, node->type);
float diff = fabsf(cpu_val - et_val);
if (is_elementwise && cpu_src0_float && cpu_src1_float) {
GGML_LOG_DEBUG("ET: [%zu] src0=%.6f, src1=%.6f -> CPU=%.6f, ET=%.6f, diff=%.6f\n", i, cpu_src0_float[i],
cpu_src1_float[i], cpu_val, et_val, diff);
} else if (is_elementwise && cpu_src0_float) {
GGML_LOG_DEBUG("ET: [%zu] src0=%.6f -> CPU=%.6f, ET=%.6f, diff=%.6f\n", i, cpu_src0_float[i], cpu_val,
et_val, diff);
} else {
GGML_LOG_DEBUG("ET: [%zu] CPU=%.6f, ET=%.6f, diff=%.6f\n", i, cpu_val, et_val, diff);
}
}
// Check some elements from the middle and end for full coverage
if (num_elements > max_log) {
size_t mid = num_elements / 2;
size_t end = num_elements - 1;
float cpu_mid = get_float(ctx->cpu_dst_data, mid, node->type);
float et_mid = get_float(ctx->et_dst_data, mid, node->type);
float cpu_end = get_float(ctx->cpu_dst_data, end, node->type);
float et_end = get_float(ctx->et_dst_data, end, node->type);
GGML_LOG_DEBUG("ET: Middle element [%zu]: CPU=%.6f, ET=%.6f\n", mid, cpu_mid, et_mid);
GGML_LOG_DEBUG("ET: Last element [%zu]: CPU=%.6f, ET=%.6f\n", end, cpu_end, et_end);
}
GGML_LOG_DEBUG("ET: Results %s (%zu/%zu elements match within tolerance %.6f)\n", matches ? "MATCH" : "DIFFER",
num_elements - total_mismatches, num_elements, config->tolerance);
}
// Copy CPU result to device if flag is set
if (config->use_cpu_result) {
GGML_LOG_DEBUG("ET: Overwriting ET device result with CPU result for correct inference\n");
size_t dst_logical_size = ggml_nbytes(node);
ggml_backend_tensor_set(const_cast<ggml_tensor *>(node), ctx->cpu_dst_data, 0, dst_logical_size);
GGML_LOG_DEBUG("ET: CPU result copied to ET device buffer\n");
}
return true;
}
void ggml_et_cpu_compare_free(ggml_et_cpu_compare_ctx * ctx) {
if (!ctx) {
return;
}
if (ctx->cpu_src0_data) {
free(ctx->cpu_src0_data);
ctx->cpu_src0_data = nullptr;
}
if (ctx->cpu_src1_data) {
free(ctx->cpu_src1_data);
ctx->cpu_src1_data = nullptr;
}
if (ctx->cpu_src2_data) {
free(ctx->cpu_src2_data);
ctx->cpu_src2_data = nullptr;
}
if (ctx->cpu_dst_data) {
free(ctx->cpu_dst_data);
ctx->cpu_dst_data = nullptr;
}
if (ctx->et_dst_data) {
free(ctx->et_dst_data);
ctx->et_dst_data = nullptr;
}
if (ctx->ggml_ctx) {
ggml_free(ctx->ggml_ctx);
ctx->ggml_ctx = nullptr;
}
if (ctx->cpu_backend) {
ggml_backend_free(ctx->cpu_backend);
ctx->cpu_backend = nullptr;
}
// Clear pointers
ctx->cpu_src0 = nullptr;
ctx->cpu_src1 = nullptr;
ctx->cpu_src2 = nullptr;
ctx->cpu_dst = nullptr;
ctx->cpu_graph = nullptr;
}
+54
View File
@@ -0,0 +1,54 @@
#pragma once
#include "ggml-cpu.h"
#include "ggml-et-common.h"
#include "ggml-impl.h"
// Configuration for CPU comparison
struct ggml_et_cpu_compare_config {
bool enabled; // Whether to enable CPU comparison
bool use_cpu_result; // Whether to replace ET result with CPU result
bool log_differences; // Whether to log detailed element differences
float tolerance; // Relative tolerance for comparison (default: 1e-5f)
size_t max_log_elements; // Maximum number of elements to log (default: 10)
};
// Default configuration
static const ggml_et_cpu_compare_config ggml_et_cpu_compare_default_config = {
/* .enabled = */ false,
/* .use_cpu_result = */ false,
/* .log_differences = */ true,
/* .tolerance = */ 1e-5f,
/* .max_log_elements = */ 10
};
// CPU comparison context for a single operation
struct ggml_et_cpu_compare_ctx {
ggml_backend_t cpu_backend;
ggml_context * ggml_ctx;
ggml_tensor * cpu_src0;
ggml_tensor * cpu_src1;
ggml_tensor * cpu_src2;
ggml_tensor * cpu_dst;
ggml_cgraph * cpu_graph;
void * cpu_src0_data;
void * cpu_src1_data;
void * cpu_src2_data;
void * cpu_dst_data;
void * et_dst_data;
size_t src0_size;
size_t src1_size;
size_t src2_size;
size_t dst_size;
};
// Phase 1: Initialize CPU comparison context and copy source buffers (call before ET kernel)
bool ggml_et_cpu_compare_init_pre(ggml_et_cpu_compare_ctx * ctx, const ggml_tensor * node, ggml_op op);
// Phase 2: Execute CPU computation and compare with ET result (call after ET kernel)
bool ggml_et_cpu_compare_compute_and_check(ggml_et_cpu_compare_ctx * ctx,
const ggml_tensor * node,
const ggml_et_cpu_compare_config * config);
// Free CPU comparison context resources
void ggml_et_cpu_compare_free(ggml_et_cpu_compare_ctx * ctx);
+508
View File
@@ -0,0 +1,508 @@
#include "ggml-et-kernels.h"
#include "ggml-et-kernels-embed.hpp"
#include "ggml-et-uberkernel-kernel-map.h"
#include "ggml-impl.h"
#include <cstdlib>
#include <cstring>
#include <fstream>
#define ET_TRACE_DECODER_IMPL
#include <et-trace/decoder.h>
#include <et-trace/layout.h>
static constexpr size_t GGML_ET_UBERKERNEL_PARAM_ALIGN = 64;
static size_t ggml_et_align_up(size_t value, size_t alignment) {
return (value + alignment - 1) & ~(alignment - 1);
}
static size_t ggml_et_next_capacity(size_t current_capacity, size_t required_capacity) {
if (current_capacity == 0) {
return required_capacity;
}
size_t next_capacity = current_capacity;
while (next_capacity < required_capacity) {
next_capacity *= 2;
}
return next_capacity;
}
static ggml_backend_et_uberkernel_slot & ggml_et_uberkernel_current_slot(ggml_backend_et_uberkernel_context * uk_ctx) {
return uk_ctx->slots[uk_ctx->current_slot];
}
// Wait for any in-flight launch that previously used this slot to finish,
// so the host vectors and device buffers are safe to mutate / free.
static void ggml_et_uberkernel_slot_wait(ggml_backend_et_uberkernel_slot & slot,
const std::shared_ptr<rt::IRuntime> & runtime) {
if (!slot.has_pending || !runtime) {
return;
}
runtime->waitForEvent(slot.pending_event);
slot.has_pending = false;
}
static void ggml_et_uberkernel_reset_segment(ggml_backend_et_uberkernel_context * uk_ctx) {
if (!uk_ctx) {
return;
}
uk_ctx->shire_mask = 0;
auto & slot = ggml_et_uberkernel_current_slot(uk_ctx);
// Drain any prior launch on this slot before clearing its host buffers.
// begin_graph and abort_graph both come through here; in either case we
// must not yank the source memory out from under an in-flight DMA.
ggml_et_uberkernel_slot_wait(slot, ggml_et_runtime());
slot.insts.clear();
slot.params_blob.clear();
}
static bool ggml_et_uberkernel_ensure_slot_capacity(ggml_backend_et_uberkernel_slot & slot,
ggml_backend_et_device_context * dev_ctx,
size_t insts_size,
size_t params_size) {
std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
if (!dev_ctx || !runtime) {
return false;
}
try {
if (slot.device_insts == nullptr || insts_size > slot.device_insts_capacity) {
const size_t new_capacity = ggml_et_next_capacity(slot.device_insts_capacity, insts_size);
if (slot.device_insts) {
runtime->freeDevice(dev_ctx->rtid, slot.device_insts);
}
slot.device_insts = runtime->mallocDevice(dev_ctx->rtid, new_capacity);
slot.device_insts_capacity = slot.device_insts ? new_capacity : 0;
}
if (slot.device_params == nullptr || params_size > slot.device_params_capacity) {
const size_t new_capacity = ggml_et_next_capacity(slot.device_params_capacity, params_size);
if (slot.device_params) {
runtime->freeDevice(dev_ctx->rtid, slot.device_params);
}
slot.device_params = runtime->mallocDevice(dev_ctx->rtid, new_capacity);
slot.device_params_capacity = slot.device_params ? new_capacity : 0;
}
} catch (const std::exception & e) {
GGML_LOG_ERROR("ET: Failed to resize uberkernel buffers: %s\n", e.what());
return false;
}
return slot.device_insts != nullptr && slot.device_params != nullptr;
}
// Get embedded kernel data by name
static std::vector<std::byte> ggml_et_get_embedded_kernel(const std::string & kernel_name) {
auto it = ggml_et_embedded_kernels.find(kernel_name);
if (it == ggml_et_embedded_kernels.end()) {
GGML_LOG_ERROR("ET: Unknown embedded kernel: %s\n", kernel_name.c_str());
return {};
}
const unsigned char * data = it->second.first;
uint64_t size = it->second.second;
std::vector<std::byte> buffer(size);
std::memcpy(buffer.data(), data, size);
return buffer;
}
// Read kernel from file (for development/override)
static std::vector<std::byte> ggml_et_read_kernel_file(const std::string & kernel_path) {
std::ifstream file(kernel_path, std::ios::binary | std::ios::ate);
if (!file) {
return {};
}
auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> buffer(size);
file.read(reinterpret_cast<char *>(buffer.data()), size);
return buffer;
}
// Load kernel from file or embedded data
bool ggml_et_load_kernel(ggml_backend_et_device_context * dev_ctx, const std::string & kernel_name) {
std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
if (!runtime) {
GGML_LOG_ERROR("ET: Runtime not available for kernel loading\n");
return false;
}
// Check if kernel already loaded
if (dev_ctx->loaded_kernels.find(kernel_name) != dev_ctx->loaded_kernels.end()) {
GGML_LOG_DEBUG("ET: Kernel %s already loaded on device %d\n", kernel_name.c_str(), dev_ctx->devidx);
return true;
}
std::vector<std::byte> kernel_data;
const char * kernels_path = getenv("GGML_ET_KERNELS_PATH");
// If GGML_ET_KERNELS_PATH is set, try to load from file first
if (kernels_path) {
std::string kernel_file = std::string(kernels_path) + "/" + kernel_name + ".elf";
kernel_data = ggml_et_read_kernel_file(kernel_file);
if (!kernel_data.empty()) {
GGML_LOG_INFO("ET: Loading kernel %s from file: %s\n", kernel_name.c_str(), kernel_file.c_str());
} else {
GGML_LOG_INFO("ET: Kernel file not found: %s, falling back to embedded\n", kernel_file.c_str());
}
}
// If no file data, use embedded kernel
if (kernel_data.empty()) {
kernel_data = ggml_et_get_embedded_kernel(kernel_name);
if (kernel_data.empty()) {
GGML_LOG_ERROR("ET: Failed to get kernel data for %s\n", kernel_name.c_str());
return false;
}
}
try {
// Load kernel code using device's default stream
auto load_result = runtime->loadCode(dev_ctx->default_stream, kernel_data.data(), kernel_data.size());
runtime->waitForEvent(load_result.event_);
// Store kernel handle
dev_ctx->loaded_kernels[kernel_name] = load_result.kernel_;
return true;
} catch (const std::exception & e) {
GGML_LOG_ERROR("ET: Failed to load kernel %s: %s\n", kernel_name.c_str(), e.what());
return false;
}
}
static bool ggml_et_launch_kernel_internal(ggml_backend_et_device_context * dev_ctx,
const std::string & kernel_name,
void * params,
size_t params_size,
uint64_t shire_mask,
bool enable_print,
bool sync_error_check,
rt::EventId * out_event = nullptr) {
std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
if (!runtime) {
GGML_LOG_ERROR("ET: Runtime not available for kernel launch\n");
return false;
}
// Lazy loading: check if kernel is loaded, load if needed
auto kernel_it = dev_ctx->loaded_kernels.find(kernel_name);
if (kernel_it == dev_ctx->loaded_kernels.end()) {
// Kernel not loaded - load it
if (!ggml_et_load_kernel(dev_ctx, kernel_name)) {
GGML_LOG_ERROR("ET: Failed to lazy-load kernel %s\n", kernel_name.c_str());
return false;
}
// Update iterator after successful load
kernel_it = dev_ctx->loaded_kernels.find(kernel_name);
if (kernel_it == dev_ctx->loaded_kernels.end()) {
GGML_LOG_ERROR("ET: Kernel %s not found after loading\n", kernel_name.c_str());
return false;
}
}
rt::KernelId kernel_id = kernel_it->second;
try {
// Setup kernel launch options
rt::KernelLaunchOptions k_opts;
k_opts.setShireMask(shire_mask); // Default: all shires (0xFFFFFFFF)
k_opts.setBarrier(true); // Wait for completion
k_opts.setFlushL3(false); // No L3 flush needed
if (enable_print) {
k_opts.setUserTracing(reinterpret_cast<uint64_t>(dev_ctx->trace_buffer),
static_cast<uint32_t>(ET_TRACE_BUFFER_SIZE),
0, // threshold
shire_mask, // shire mask
0xFFFFFFFFFFFFFFFFULL, // threadMask - all threads
0xFFFFFFFFU, // eventMask - all events
0xFFFFFFFFU // filterMask - all levels
);
}
if (sync_error_check) {
runtime->waitForStream(dev_ctx->default_stream);
auto errors = runtime->retrieveStreamErrors(dev_ctx->default_stream);
if (!errors.empty()) {
GGML_LOG_ERROR("ET: Errors detected before kernel \"%s\" launch\n", kernel_name.c_str());
for (const auto & error : errors) {
GGML_LOG_ERROR("ET: Error code: %d\n", (int) error.errorCode_);
}
abort();
}
}
rt::EventId launch_event = runtime->kernelLaunch(dev_ctx->default_stream, kernel_id,
reinterpret_cast<std::byte *>(params), params_size, k_opts);
if (out_event) {
*out_event = launch_event;
}
if (enable_print) {
std::vector<std::byte> host_trace_buf(ET_TRACE_BUFFER_SIZE);
runtime->memcpyDeviceToHost(dev_ctx->default_stream, dev_ctx->trace_buffer, host_trace_buf.data(),
ET_TRACE_BUFFER_SIZE);
runtime->waitForStream(dev_ctx->default_stream);
const auto * trace_header = reinterpret_cast<const trace_buffer_std_header_t *>(host_trace_buf.data());
const trace_entry_header_t * entry = nullptr;
while ((entry = Trace_Decode(trace_header, entry))) {
if (entry->type != TRACE_TYPE_STRING) {
continue;
}
const auto * str_entry = reinterpret_cast<const trace_string_t *>(entry);
printf("[hart %d] %s", entry->hart_id, str_entry->string);
}
}
if (sync_error_check) {
// Already triggered. No need to retrigger
if (!enable_print) {
runtime->waitForStream(dev_ctx->default_stream);
}
auto errors = runtime->retrieveStreamErrors(dev_ctx->default_stream);
if (!errors.empty()) {
GGML_LOG_ERROR("ET: Errors detected during kernel \"%s\" execution\n", kernel_name.c_str());
for (const auto & error : errors) {
GGML_LOG_ERROR("ET: Error code: %d\n", (int) error.errorCode_);
}
abort();
}
}
return true;
} catch (const std::exception & e) {
GGML_LOG_ERROR("ET: Failed to launch kernel %s: %s\n", kernel_name.c_str(), e.what());
return false;
}
}
void ggml_et_uberkernel_begin_graph(ggml_backend_et_uberkernel_context * uk_ctx) {
if (!uk_ctx) {
return;
}
uk_ctx->failed = false;
ggml_et_uberkernel_reset_segment(uk_ctx);
}
static bool ggml_et_launch_uberkernel_segment(ggml_backend_et_device_context * dev_ctx,
ggml_backend_et_uberkernel_context * uk_ctx) {
if (!uk_ctx || !dev_ctx) {
return false;
}
auto & slot = ggml_et_uberkernel_current_slot(uk_ctx);
if (slot.insts.empty()) {
return true;
}
std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
if (!runtime) {
GGML_LOG_ERROR("ET: Runtime not available for uberkernel commit\n");
uk_ctx->failed = true;
return false;
}
const size_t insts_size = slot.insts.size() * sizeof(ggml_et_uberkernel_inst);
const size_t params_size = slot.params_blob.size();
const uint64_t shire_mask = uk_ctx->shire_mask;
bool ok = false;
try {
if (!ggml_et_uberkernel_ensure_slot_capacity(slot, dev_ctx, insts_size, params_size)) {
GGML_LOG_ERROR("ET: Failed to allocate uberkernel device buffers\n");
uk_ctx->failed = true;
// Drop this segment but keep the slot drained so we don't leak
// host vectors into the next graph.
slot.insts.clear();
slot.params_blob.clear();
uk_ctx->shire_mask = 0;
return false;
}
// Fire-and-forget H2D + launch on default_stream. In-stream FIFO
// ordering guarantees the kernel sees fully-uploaded buffers; the
// host source bytes (slot.insts / slot.params_blob) stay alive
// because we won't touch this slot again until pending_event fires.
runtime->memcpyHostToDevice(dev_ctx->default_stream, reinterpret_cast<const std::byte *>(slot.insts.data()),
slot.device_insts, insts_size, true);
runtime->memcpyHostToDevice(dev_ctx->default_stream, slot.params_blob.data(), slot.device_params, params_size,
true);
ggml_et_uberkernel_params params = {
static_cast<uint32_t>(slot.insts.size()),
static_cast<uint32_t>(sizeof(ggml_et_uberkernel_inst)),
reinterpret_cast<uint64_t>(slot.device_insts),
reinterpret_cast<uint64_t>(slot.device_params),
};
rt::EventId launch_event{};
ok = ggml_et_launch_kernel_internal(dev_ctx, "uberkernel", &params, sizeof(params), shire_mask, false, false,
&launch_event);
if (ok) {
// The kernelLaunch above is the last thing on default_stream
// that touches this slot's device buffers. Recording its event
// lets the next reuse of this slot wait on that one event
// instead of the whole stream.
slot.pending_event = launch_event;
slot.has_pending = true;
}
} catch (const std::exception & e) {
GGML_LOG_ERROR("ET: Failed to commit uberkernel segment: %s\n", e.what());
}
uk_ctx->failed = !ok;
if (ok) {
uk_ctx->current_slot = (uk_ctx->current_slot + 1) % ggml_backend_et_uberkernel_context::SLOT_COUNT;
auto & next = ggml_et_uberkernel_current_slot(uk_ctx);
ggml_et_uberkernel_slot_wait(next, runtime);
next.insts.clear();
next.params_blob.clear();
} else {
slot.insts.clear();
slot.params_blob.clear();
}
uk_ctx->shire_mask = 0;
return ok;
}
void ggml_et_uberkernel_abort_graph(ggml_backend_et_uberkernel_context * uk_ctx) {
if (!uk_ctx) {
return;
}
uk_ctx->failed = false;
ggml_et_uberkernel_reset_segment(uk_ctx);
}
bool ggml_et_uberkernel_failed(const ggml_backend_et_uberkernel_context * uk_ctx) {
return uk_ctx && uk_ctx->failed;
}
static bool ggml_et_launch_uberkernel(ggml_backend_et_device_context * dev_ctx,
const std::string & kernel_name,
void * params,
size_t params_size,
uint64_t shire_mask,
bool enable_print,
bool sync_error_check) {
if (!dev_ctx) {
return false;
}
ggml_backend_et_uberkernel_context * uk_ctx = &dev_ctx->uberkernel;
const uint16_t uberkernel_id = ggml_et_uberkernel_kernel_id_from_name(kernel_name.c_str());
if (uberkernel_id == GGML_ET_UBERKERNEL_KERNEL_INVALID) {
if (!ggml_et_launch_uberkernel_segment(dev_ctx, uk_ctx)) {
return false;
}
return ggml_et_launch_kernel_internal(dev_ctx, kernel_name, params, params_size, shire_mask, enable_print,
sync_error_check);
}
auto & slot = ggml_et_uberkernel_current_slot(uk_ctx);
const size_t params_offset = ggml_et_align_up(slot.params_blob.size(), GGML_ET_UBERKERNEL_PARAM_ALIGN);
if (params_offset > slot.params_blob.size()) {
slot.params_blob.resize(params_offset);
}
const std::byte * params_bytes = reinterpret_cast<const std::byte *>(params);
slot.params_blob.insert(slot.params_blob.end(), params_bytes, params_bytes + params_size);
ggml_et_uberkernel_inst inst = {
uberkernel_id,
0,
static_cast<uint32_t>(params_offset),
static_cast<uint32_t>(params_size),
};
slot.insts.push_back(inst);
if (slot.insts.size() == 1) {
uk_ctx->shire_mask = shire_mask;
}
return true;
}
bool ggml_et_uberkernel_end_graph(ggml_backend_et_device_context * dev_ctx) {
if (!dev_ctx || !dev_ctx->uberkernel_enabled) {
return true;
}
return ggml_et_launch_uberkernel_segment(dev_ctx, &dev_ctx->uberkernel);
}
bool ggml_et_launch_kernel(ggml_backend_et_device_context * dev_ctx,
const std::string & kernel_name,
void * params,
size_t params_size,
uint64_t shire_mask,
bool enable_print,
bool sync_error_check) {
if (!dev_ctx) {
return false;
}
if (!dev_ctx->uberkernel_enabled) {
return ggml_et_launch_kernel_internal(dev_ctx, kernel_name, params, params_size, shire_mask, enable_print,
sync_error_check);
}
return ggml_et_launch_uberkernel(dev_ctx, kernel_name, params, params_size, shire_mask, enable_print,
sync_error_check);
}
void ggml_et_unload_kernel(ggml_backend_et_device_context * dev_ctx, const std::string & kernel_name) {
std::shared_ptr<rt::IRuntime> runtime = ggml_et_runtime();
if (!runtime) {
return;
}
auto kernel_it = dev_ctx->loaded_kernels.find(kernel_name);
if (kernel_it != dev_ctx->loaded_kernels.end()) {
try {
runtime->unloadCode(kernel_it->second);
dev_ctx->loaded_kernels.erase(kernel_it);
} catch (const std::exception & e) {
GGML_LOG_ERROR("ET: Failed to unload kernel %s: %s\n", kernel_name.c_str(), e.what());
}
}
}
void ggml_et_unload_all_kernels(ggml_backend_et_device_context * dev_ctx) {
if (!dev_ctx) {
return;
}
// Make a copy of kernel names since ggml_et_unload_kernel modifies the map
std::vector<std::string> kernel_names;
kernel_names.reserve(dev_ctx->loaded_kernels.size());
for (const auto & kernel_pair : dev_ctx->loaded_kernels) {
kernel_names.push_back(kernel_pair.first);
}
for (const auto & kernel_name : kernel_names) {
ggml_et_unload_kernel(dev_ctx, kernel_name);
}
}
std::vector<std::pair<std::string, rt::KernelId>> ggml_et_get_loaded_kernels(ggml_backend_et_device_context * dev_ctx) {
std::vector<std::pair<std::string, rt::KernelId>> loaded_kernels;
loaded_kernels.reserve(dev_ctx->loaded_kernels.size());
for (const auto & kernel_pair : dev_ctx->loaded_kernels) {
loaded_kernels.push_back(kernel_pair);
}
return loaded_kernels;
}
+48
View File
@@ -0,0 +1,48 @@
#pragma once
#include "ggml-et-common.h"
#include <string>
#include <utility>
#include <vector>
#define ET_TRACE_BUFFER_SIZE (1024 * 1024 * 8UL)
// Load kernel from file or embedded data and store handle in device context
// Returns true on success, false on failure
//
// Loading strategy:
// - If GGML_ET_KERNELS_PATH env var is set: tries to load from ${GGML_ET_KERNELS_PATH}/${kernel_name}.elf
// - If file not found or env var not set: falls back to embedded kernel data
// - Returns false if kernel cannot be loaded from either source
//
// Kernel is loaded using the device's default stream
bool ggml_et_load_kernel(ggml_backend_et_device_context * dev_ctx, const std::string & kernel_name);
// Launch kernel with parameters on device's default stream
// Performs lazy loading: automatically loads kernel if not already loaded
// Kernel path: ${GGML_ET_KERNELS_PATH}/${kernel_name}.elf (default: /opt/et/ggml/kernels/)
// Returns true on success, false on failure
// Execution is synchronous - waits for completion
bool ggml_et_launch_kernel(ggml_backend_et_device_context * dev_ctx,
const std::string & kernel_name,
void * params,
size_t params_size,
uint64_t shire_mask = 0xFFFFFFFF,
bool enable_print = false,
bool sync_error_check = false);
void ggml_et_uberkernel_begin_graph(ggml_backend_et_uberkernel_context * uk_ctx);
bool ggml_et_uberkernel_end_graph(ggml_backend_et_device_context * dev_ctx);
void ggml_et_uberkernel_abort_graph(ggml_backend_et_uberkernel_context * uk_ctx);
bool ggml_et_uberkernel_failed(const ggml_backend_et_uberkernel_context * uk_ctx);
// Unload kernel from device and free resources
// Safe to call even if kernel not loaded
void ggml_et_unload_kernel(ggml_backend_et_device_context * dev_ctx, const std::string & kernel_name);
// Unload all kernels from device context
// Called during device cleanup
void ggml_et_unload_all_kernels(ggml_backend_et_device_context * dev_ctx);
std::vector<std::pair<std::string, rt::KernelId>> ggml_et_get_loaded_kernels(ggml_backend_et_device_context * dev_ctx);
+36
View File
@@ -0,0 +1,36 @@
#include "ggml-et-memops.h"
#include "ggml-et-kernels.h"
#include "ggml-impl.h"
// Kernel parameter structure for memset operation
struct memset_params {
uint32_t op_type; // GGML_ET_MEMOP_MEMSET
uint32_t value; // Value to set (extended to uint32_t for alignment)
void * dst_ptr; // Destination device pointer
size_t size; // Number of bytes to set
};
bool ggml_et_memset(ggml_backend_et_device_context * dev_ctx, void * dst_ptr, uint8_t value, size_t size) {
if (!dev_ctx || !dst_ptr || size == 0) {
GGML_LOG_ERROR("ET: Invalid memset parameters\n");
return false;
}
// Prepare kernel parameters
memset_params params;
params.op_type = GGML_ET_MEMOP_MEMSET;
params.value = value;
params.dst_ptr = dst_ptr;
params.size = size;
// Launch memops kernel (will lazy-load if not already loaded)
bool success = ggml_et_launch_kernel(dev_ctx, "memops", &params, sizeof(params));
if (!success) {
GGML_LOG_ERROR("ET: memset kernel launch failed\n");
return false;
}
return true;
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include "ggml-et-common.h"
#include <cstddef>
#include <cstdint>
// Memory operations using device kernel (memops.elf)
// Single kernel handles multiple operations via operation identifier
// Operation identifiers for memops kernel
enum ggml_et_memop_type : uint32_t {
GGML_ET_MEMOP_MEMSET = 0,
};
// Memset operation: fill device memory with a value
// Returns true on success, false on failure
bool ggml_et_memset(ggml_backend_et_device_context * dev_ctx, void * dst_ptr, uint8_t value, size_t size);
File diff suppressed because it is too large Load Diff
+392
View File
@@ -0,0 +1,392 @@
#pragma once
#include "ggml-et-common.h"
#include "ggml.h"
#include <inttypes.h>
// Performance logging macros for ET ops
// Logs in machine-parseable pipe-delimited format: ET_PERF|field=value|...
#ifdef ET_PERF_RECORD
# define ET_PERF_START() int64_t _et_perf_start = ggml_time_us()
# define ET_PERF_END(op_name, kernel_name, node) \
do { \
int64_t _et_perf_end = ggml_time_us(); \
int64_t _et_perf_duration = _et_perf_end - _et_perf_start; \
GGML_LOG_DEBUG("ET_PERF|op=%s|kernel=%s|duration_us=%" PRId64 "|tensor=%s|shape=[%" PRId64 ",%" PRId64 \
",%" PRId64 ",%" PRId64 "]|start_us=%" PRId64 "|end_us=%" PRId64 "\n", \
op_name, kernel_name, _et_perf_duration, (node)->name, (node)->ne[0], (node)->ne[1], \
(node)->ne[2], (node)->ne[3], _et_perf_start, _et_perf_end); \
} while (0)
# define ET_PERF_END_EXT(op_name, kernel_name, node, fmt, ...) \
do { \
int64_t _et_perf_end = ggml_time_us(); \
int64_t _et_perf_duration = _et_perf_end - _et_perf_start; \
GGML_LOG_DEBUG("ET_PERF|op=%s|kernel=%s|duration_us=%" PRId64 "|tensor=%s|shape=[%" PRId64 ",%" PRId64 \
",%" PRId64 ",%" PRId64 "]|start_us=%" PRId64 "|end_us=%" PRId64 "|" fmt "\n", \
op_name, kernel_name, _et_perf_duration, (node)->name, (node)->ne[0], (node)->ne[1], \
(node)->ne[2], (node)->ne[3], _et_perf_start, _et_perf_end, ##__VA_ARGS__); \
} while (0)
#else
# define ET_PERF_START() \
do { \
} while (0)
# define ET_PERF_END_EXT(op_name, kernel_name, node, fmt, ...) \
do { \
(void) (node); \
} while (0)
# define ET_PERF_END(op_name, kernel_name, node) \
do { \
(void) (node); \
} while (0)
#endif // ET_PERF_RECORD
struct ggml_et_binary_params {
ggml_tensor src0;
ggml_tensor src1;
ggml_tensor dst;
};
// Q8_0 mul_mat with optional residual bias.
// bias.data == NULL means "no bias" - kernel skips the add.
// When non-NULL, bias must have the same shape and strides as dst.
struct ggml_et_mm_q8_params {
ggml_tensor src0;
ggml_tensor src1;
ggml_tensor dst;
ggml_tensor bias;
};
struct ggml_et_im2col_params {
ggml_tensor src0;
ggml_tensor src1;
ggml_tensor dst;
};
// Element map parameters for embarrassingly parallel binary operations (MUL, ADD, etc.)
// Operation type is determined by dst->op (GGML_OP_MUL, GGML_OP_ADD, etc.)
struct ggml_et_elmap_params {
ggml_tensor src0;
ggml_tensor src1;
ggml_tensor dst;
};
struct ggml_et_rope_settings {
int32_t n_past;
int32_t n_dims; // Number of dimensions to apply ROPE to (must be even)
int32_t mode; // ROPE mode, GGML_ROPE_TYPE_*
int32_t n_ctx;
int32_t n_ctx_orig;
float freq_base; // Base frequency (usually 10000.0f)
float freq_scale; // Frequency scaling factor
float ext_factor; // Extension factor for YaRN
float attn_factor; // Attention factor for YaRN
float beta_fast; // Fast beta for YaRN
float beta_slow; // Slow beta for YaRN
int32_t sections[4]; // Sections for multi-modal ROPE
};
struct ggml_et_rope_params {
ggml_tensor src0;
ggml_tensor src1;
ggml_tensor src2;
ggml_tensor dst;
ggml_et_rope_settings rope_params;
};
struct ggml_et_rms_norm_params {
ggml_tensor src0; // F32 input tensor
ggml_tensor dst; // F32 output tensor
float eps; // Epsilon parameter for numerical stability
};
struct ggml_et_norm_params {
ggml_tensor src0; // F32 input tensor
ggml_tensor dst; // F32 output tensor
float eps; // Epsilon parameter for numerical stability
};
struct ggml_et_l2_norm_params {
ggml_tensor src0; // F32 input tensor
ggml_tensor dst; // F32 output tensor
float eps; // Epsilon parameter for numerical stability
};
struct ggml_et_group_norm_params {
ggml_tensor src0; // F32 input tensor
ggml_tensor dst; // F32 output tensor
int32_t n_groups; // Number of channel groups
float eps; // Epsilon parameter for numerical stability
};
struct ggml_et_glu_params {
ggml_tensor src0; // F32 input tensor A (or combined tensor if src1 is null)
ggml_tensor src1; // F32 input tensor B (null for single tensor mode)
ggml_tensor dst; // F32 output tensor (n/2 columns)
int32_t glu_op_type; // GLU operation type (REGLU=0, GEGLU=1, SWIGLU=2, etc.)
int32_t swapped; // Whether gate and value are swapped
float alpha; // SWIGLU_OAI: sigmoid scaling factor (unused for other variants)
float limit; // SWIGLU_OAI: clamp limit (unused for other variants)
};
struct ggml_et_softmax_params {
ggml_tensor src0; // F32 input tensor
ggml_tensor src1; // F32 mask tensor (optional, may be zeroed if not used)
ggml_tensor src2; // F32 sinks tensor (optional, may be zeroed if not used)
ggml_tensor dst; // F32 output tensor
float scale; // Scale factor
float max_bias; // Max bias for ALiBi (0.0f if not used)
};
struct ggml_et_flash_attn_ext_params {
ggml_tensor src0; // Q tensor (F32)
ggml_tensor src1; // K tensor (F32)
ggml_tensor src2; // V tensor (F32)
ggml_tensor mask; // mask tensor (F16 or F32), zeroed when absent
ggml_tensor dst; // Output tensor (F32)
float scale; // Scale factor applied to QK
int32_t has_mask; // nonzero if mask is present
};
struct ggml_et_get_rows_params {
ggml_tensor src0; // Data tensor (F32 or Q8_0)
ggml_tensor src1; // Row indices tensor (I32)
ggml_tensor dst; // Output tensor (F32)
};
struct ggml_et_cont_params {
ggml_tensor src0; // F32 input tensor (non-contiguous)
ggml_tensor dst; // F32 output tensor (contiguous)
};
struct ggml_et_concat_params {
ggml_tensor src0; // F32 input tensor 0
ggml_tensor src1; // F32 input tensor 1
ggml_tensor dst; // F32 output tensor
int32_t dim; // Concatenation dimension
};
struct ggml_et_repeat_params {
ggml_tensor src0; // F32 input tensor (tile)
ggml_tensor dst; // F32 output tensor (tiled result)
};
struct ggml_et_fill_params {
ggml_tensor dst; // F32 output tensor (contiguous)
float c; // Constant value to fill
};
struct ggml_et_tri_params {
ggml_tensor src0; // F32 input tensor
ggml_tensor dst; // F32 output tensor
int32_t tri_type; // ggml_tri_type enum value
};
struct ggml_et_solve_tri_params {
ggml_tensor src0; // A: lower-triangular [n, n, B1, B2]
ggml_tensor src1; // B: RHS [k, n, B1, B2]
ggml_tensor dst; // X: solution [k, n, B1, B2]
};
struct ggml_et_pad_params {
ggml_tensor src0; // F32 input (may be non-contiguous, nb[0] must == 4)
ggml_tensor dst; // F32 output (contiguous, ne[0] % 16 == 0)
int32_t lp[4]; // left padding per dimension
int32_t rp[4]; // right padding per dimension
};
struct ggml_et_diag_params {
ggml_tensor src0; // F32 input vector
ggml_tensor dst; // F32 output diagonal matrix
};
struct ggml_et_ssm_conv_params {
ggml_tensor src0; // conv_x: [d_conv - 1 + n_t, d_inner, n_seqs]
ggml_tensor src1; // conv1d.weight: [d_conv, d_inner]
ggml_tensor dst; // output: [d_inner, n_t, n_seqs]
};
struct ggml_et_ssm_scan_params {
ggml_tensor src0; // s: [d_state, head_dim, n_head, n_seqs]
ggml_tensor src1; // x: [head_dim, n_head, n_seq_tokens, n_seqs]
ggml_tensor src2; // dt: [n_head, n_seq_tokens, n_seqs]
ggml_tensor src3; // A: [d_state, n_head] or [1, n_head]
ggml_tensor src4; // B: [d_state, n_group, n_seq_tokens, n_seqs]
ggml_tensor src5; // C: [d_state, n_group, n_seq_tokens, n_seqs]
ggml_tensor src6; // ids: [n_seqs] i32
ggml_tensor dst; // [y, final_state] packed output from ggml_ssm_scan()
};
struct ggml_et_rwkv_wkv6_params {
float * k; // src[0]: [S, H, T] key
float * v; // src[1]: [S, H, T] value
float * r; // src[2]: [S, H, T] receptance
float * tf; // src[3]: [S, H] time_faaaa (per-head)
float * td; // src[4]: [S, H, T] time_decay
float * state_in; // src[5]: [S*S*H, n_seqs] initial state
float * dst; // [C, T + S*n_seqs] output + state_out
int32_t C; // total channels (S * H)
int32_t H; // number of heads
int32_t S; // head size
int32_t T; // number of tokens
int32_t n_seqs; // number of sequences
};
struct ggml_et_rwkv_wkv7_params {
float * r; // [S, H, T] receptance
float * w; // [S, H, T] decay
float * k; // [S, H, T] key
float * v; // [S, H, T] value
float * a; // [S, H, T] bonus gate
float * b; // [S, H, T] bonus key
float * state_in; // [S*S*H, n_seqs] initial state
float * dst; // [C, T + S*n_seqs] output + state_out
int32_t C; // total channels (S * H)
int32_t H; // number of heads
int32_t S; // head size
int32_t T; // number of tokens
int32_t n_seqs; // number of sequences
};
struct ggml_et_gated_delta_net_params {
ggml_tensor q; // [S_v, H_q, n_tokens, n_seqs_q]
ggml_tensor k; // [S_v, H_k, n_tokens, n_seqs_k]
ggml_tensor v; // [S_v, H, n_tokens, n_seqs]
ggml_tensor g; // [1 or S_v, H, n_tokens, n_seqs]
ggml_tensor beta; // [1, H, n_tokens, n_seqs]
ggml_tensor state_in; // [S_v*S_v*H, K, n_seqs]
ggml_tensor dst; // [S_v*H, n_tokens*n_seqs + S_v*n_seqs*K]
int32_t S_v; // head dimension (value size)
int32_t H; // number of value heads
int32_t H_q; // number of Q heads
int32_t H_k; // number of K heads
int32_t n_tokens; // total tokens
int32_t n_seqs; // number of sequences (from V)
int32_t n_seqs_q; // Q sequence count
int32_t n_seqs_k; // K sequence count
int32_t kda; // 1 if per-element gate (g_ne0 == S_v), 0 if scalar
int32_t K; // snapshot slot count
float scale; // 1/sqrt(S_v)
};
struct ggml_et_set_rows_params {
ggml_tensor src0; // F32 source data tensor
ggml_tensor src1; // I64 row indices tensor
ggml_tensor dst; // F32/F16 destination tensor
};
struct ggml_et_set_params {
ggml_tensor src1; // F32 source view to write into dst
ggml_tensor dst; // F32 destination/base tensor
int32_t nb1; // destination view stride for dim 1
int32_t nb2; // destination view stride for dim 2
int32_t nb3; // destination view stride for dim 3
int32_t offset; // byte offset into destination
};
struct ggml_et_rms_norm_mul_params {
ggml_tensor src0; // F32 input tensor (to be normalized)
ggml_tensor src1; // F32 weights tensor (element-wise multiply)
ggml_tensor dst; // F32 output tensor
float eps; // Epsilon for numerical stability
};
struct ggml_et_mul_mat_id_params {
ggml_tensor src0; // Expert weight matrices (Q8_0/F16/F32) [K, M, n_expert]
ggml_tensor src1; // Activations (F32) [K, n_expert_used, batch]
ggml_tensor src2; // Expert indices (I32) [n_expert_used, batch]
ggml_tensor dst; // Output (F32) [M, n_expert_used, batch, 1]
};
struct ggml_et_sqr_params {
ggml_tensor src0; // F32 input tensor
ggml_tensor dst; // F32 output tensor
};
struct ggml_et_unary_params {
ggml_tensor src0; // F32 input tensor
ggml_tensor dst; // F32 output tensor
int32_t unary_op; // ggml_unary_op enum value
};
struct ggml_et_sum_rows_params {
ggml_tensor src0; // F32 input tensor [ne00, ne01, ne02, ne03]
ggml_tensor dst; // F32 output tensor [1, ne01, ne02, ne03]
};
struct ggml_et_mean_params {
ggml_tensor src0; // F32 input tensor [ne00, ne01, ne02, ne03]
ggml_tensor dst; // F32 output tensor [1, ne01, ne02, ne03]
};
struct ggml_et_clamp_params {
ggml_tensor src0; // F32 input tensor (contiguous)
ggml_tensor dst; // F32 output tensor (contiguous; may alias src0)
float min_val;
float max_val;
};
struct ggml_et_cumsum_params {
ggml_tensor src0; // F32 input tensor [ne00, ne01, ne02, ne03]
ggml_tensor dst; // F32 output tensor [ne00, ne01, ne02, ne03]
};
struct ggml_et_scale_params {
ggml_tensor src0; // F32 input tensor
ggml_tensor dst; // F32 output tensor
float scale; // Scale factor
float bias; // Bias (additive offset)
};
bool ggml_et_op_cumsum(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_sqr(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_unary(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_sum_rows(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_mean(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_clamp(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_scale(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_mul(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_add(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_sub(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
// add_node is optional: when non-NULL and the pair (node, add_node) was
// validated by ggml_et_can_fuse({MUL_MAT, ADD}), the Q8_0 path writes
// dst = mm(...) + add_node's "other" operand (the bias) in one launch.
bool ggml_et_op_mul_mat(ggml_backend_et_device_context * dev_ctx,
const ggml_tensor * node,
const ggml_tensor * add_node = nullptr);
bool ggml_et_op_mul_mat_id(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_rope(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_rms_norm(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_norm(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_l2_norm(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_group_norm(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_glu(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_softmax(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_im2col(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_conv_2d(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_flash_attn_ext(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_get_rows(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_set_rows(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_cont(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_concat(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_repeat(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_rwkv_wkv6(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_rwkv_wkv7(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_cpy(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_gated_delta_net(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_elmap(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_fill(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_diag(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_tri(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_solve_tri(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_pad(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_set(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_ssm_conv(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_ssm_scan(ggml_backend_et_device_context * dev_ctx, const ggml_tensor * node);
bool ggml_et_op_rms_norm_mul(ggml_backend_et_device_context * dev_ctx,
const ggml_tensor * rms_norm_node,
const ggml_tensor * mul_node);
@@ -0,0 +1,17 @@
#pragma once
#include <stdint.h>
struct ggml_et_uberkernel_inst {
uint16_t kernel_id;
uint16_t flags;
uint32_t params_offset;
uint32_t params_size;
};
struct ggml_et_uberkernel_params {
uint32_t num_insts;
uint32_t inst_stride;
uint64_t insts;
uint64_t params_blob;
};
File diff suppressed because it is too large Load Diff
+102 -1
View File
@@ -44,6 +44,7 @@
#include "htp-ops.h"
#include "htp/matmul-ops.h"
#include "htp/flash-attn-ops.h"
#include "htp/unary-ops.h"
#include "htp_iface.h"
#include "htp-drv.h"
@@ -170,8 +171,8 @@ static inline bool ggml_hexagon_is_hmx_weight_type(enum ggml_type type) {
return type == GGML_TYPE_F16 || type == GGML_TYPE_F32 || ggml_hexagon_is_repack_type(type);
}
struct htp_mm_kernel_params;
struct ggml_hexagon_session;
static void ggml_hexagon_precompute_matmul_params(
const struct ggml_hexagon_session * sess,
const struct ggml_tensor * src0,
@@ -180,6 +181,15 @@ static void ggml_hexagon_precompute_matmul_params(
struct htp_mm_kernel_params * kparams
);
static void ggml_hexagon_precompute_unary_params(
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_unary_kernel_params * kparams
);
static void ggml_hexagon_precompute_fused_qkv_params(
const struct ggml_hexagon_session * sess,
const struct ggml_tensor * src0,
@@ -2591,6 +2601,74 @@ finalize:
kparams->div_ne11 = init_fastdiv_values(ne11);
}
static void ggml_hexagon_precompute_unary_params(
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_unary_kernel_params * kparams
) {
memset(kparams, 0, sizeof(*kparams));
const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3];
const uint32_t n_threads = (std::min)((uint32_t)sess->n_threads, src0_nrows);
kparams->n_threads = n_threads;
const size_t src0_data_row_size = src0->ne[0] * sizeof(float);
const size_t dst_data_row_size = dst->ne[0] * sizeof(float);
const size_t src0_row_size_aligned = hex_round_up(src0_data_row_size, 128);
const size_t dst_row_size_aligned = hex_round_up(dst_data_row_size, 128);
kparams->src0_row_size_aligned = src0_row_size_aligned;
kparams->dst_row_size_aligned = dst_row_size_aligned;
size_t src1_data_row_size = 0;
size_t src1_row_size_aligned = 0;
bool broadcast_weight = false;
if (op == HTP_OP_RMS_NORM_MUL) {
GGML_ASSERT(src1 != nullptr);
src1_data_row_size = src1->ne[0] * sizeof(float);
src1_row_size_aligned = hex_round_up(src1_data_row_size, 128);
broadcast_weight = (src1->ne[1] * src1->ne[2] * src1->ne[3] == 1);
}
kparams->src1_row_size_aligned = src1_row_size_aligned;
kparams->broadcast_weight = broadcast_weight;
struct htp_unary_vtcm_layout L;
uint32_t col_tile = 0;
uint32_t vtcm_row_per_thread = 0;
htp_unary_vtcm_layout_build(&L, op, src0->ne[0], dst->ne[0],
op == HTP_OP_RMS_NORM_MUL ? src1->ne[0] : 0,
broadcast_weight, n_threads, sess->vtcm_size,
&col_tile, &vtcm_row_per_thread);
kparams->col_tile = col_tile;
kparams->vtcm_row_per_thread = vtcm_row_per_thread;
kparams->vtcm_size = L.total_bytes;
kparams->vtcm_src0_size_per_thread = L.src0_bytes;
kparams->vtcm_src1_size_per_thread = L.src1_bytes;
kparams->vtcm_dst_size_per_thread = L.dst_bytes;
kparams->vtcm_src0_size = L.src0_bytes * n_threads;
kparams->vtcm_src1_size = L.src1_bytes * n_threads;
kparams->vtcm_dst_size = L.dst_bytes * n_threads;
kparams->block = col_tile ? 0 : ((L.src0_bytes / 2) / src0_row_size_aligned);
const uint32_t tiles_per_row = col_tile > 0 ? (src0->ne[0] + col_tile - 1) / col_tile : 1;
kparams->div_ne01 = init_fastdiv_values(src0->ne[1]);
kparams->div_ne02 = init_fastdiv_values(src0->ne[2]);
kparams->div_ne012 = init_fastdiv_values(src0->ne[1] * src0->ne[2]);
kparams->div_tpr = init_fastdiv_values(tiles_per_row);
}
static void ggml_hexagon_precompute_fused_qkv_params(
const struct ggml_hexagon_session * sess,
const struct ggml_tensor * src0, // Wk
@@ -2866,6 +2944,9 @@ static bool ggml_hexagon_supported_binary(const struct ggml_hexagon_session * se
return false;
}
if (ggml_is_permuted(src0) || ggml_is_permuted(dst)) {
return false;
}
if (!ggml_are_same_shape(src0, dst)) {
return false;
}
@@ -2912,6 +2993,9 @@ static bool ggml_hexagon_supported_unary(const struct ggml_hexagon_session * ses
if (dst->type != GGML_TYPE_F32) {
return false;
}
if (ggml_is_permuted(src0)) {
return false;
}
if (!ggml_are_same_shape(src0, dst)) {
return false;
}
@@ -3451,6 +3535,15 @@ static bool try_fuse_node(const ggml_hexagon_session * sess, const ggml_cgraph *
if (next_node->op == GGML_OP_MUL && op_is_compute(next_node) && ggml_can_fuse(graph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
htp_opnode node(n, {}, HTP_OP_RMS_NORM_MUL);
node.add_fused(next_node);
auto inputs = node.get_inputs();
const struct ggml_tensor * src0 = inputs[0];
const struct ggml_tensor * src1 = inputs.size() > 1 ? inputs[1] : nullptr;
ggml_hexagon_precompute_unary_params(sess,
node.opcode, src0, src1, node.dst(),
(struct htp_unary_kernel_params *)node.kernel_params
);
nodes.push_back(std::move(node));
i++; // skip the fused MUL node
return true;
@@ -3555,6 +3648,14 @@ static ggml_status ggml_backend_hexagon_graph_compute(ggml_backend_t backend, gg
node.node,
(struct htp_fa_kernel_params *)node.kernel_params
);
} else if (htp_op_is_unary(node.opcode)) {
auto inputs = node.get_inputs();
const struct ggml_tensor * src0 = inputs[0];
const struct ggml_tensor * src1 = inputs.size() > 1 ? inputs[1] : nullptr;
ggml_hexagon_precompute_unary_params(sess,
node.opcode, src0, src1, node.dst(),
(struct htp_unary_kernel_params *)node.kernel_params
);
}
computed_nodes.push_back(std::move(node));
}
+4
View File
@@ -12,6 +12,7 @@
#include "htp-ops.h"
#include "htp/matmul-ops.h"
#include "htp/flash-attn-ops.h"
#include "htp/unary-ops.h"
struct htp_opnode {
ggml_tensor * node = nullptr;
@@ -362,6 +363,9 @@ struct htp_opformat {
path = "hvx";
}
snprintf(str, max_size, "%s vtcm %d", path, (int) kparams->vtcm_size);
} else if (htp_op_is_unary(node.opcode)) {
const auto * kparams = (const struct htp_unary_kernel_params *) node.kernel_params;
snprintf(str, max_size, "%s vtcm %d", kparams->col_tile ? "wide-row" : "row-block", (int) kparams->vtcm_size);
} else {
snprintf(str, max_size, "----");
}
+1 -1
View File
@@ -39,8 +39,8 @@ add_library(${HTP_LIB} SHARED
diag-ops.c
solve-tri-ops.c
pad-ops.c
flash-attn-ops.c
matmul-ops.c
flash-attn-ops.c
)
target_compile_definitions(${HTP_LIB} PRIVATE
-1
View File
@@ -120,7 +120,6 @@ int op_concat(struct htp_ops_context * octx);
int op_diag(struct htp_ops_context * octx);
int op_solve_tri(struct htp_ops_context * octx);
int op_gated_delta_net(struct htp_ops_context * octx);
int op_tri(struct htp_ops_context * octx);
int op_pad(struct htp_ops_context * octx);
#endif /* HTP_CTX_H */
+257
View File
@@ -0,0 +1,257 @@
#ifndef HVX_NORM_H
#define HVX_NORM_H
#include <stdint.h>
#include "hvx-base.h"
#include "hvx-reduce.h"
#include "hvx-inverse.h"
#include "hvx-sqrt.h"
#include "hvx-repl.h"
static inline void hvx_fast_rms_norm_f32(const uint8_t * restrict src,
uint8_t * restrict dst,
const int num_elems,
float epsilon) {
const HVX_Vector * restrict v_src = (HVX_Vector *) src;
HVX_Vector * restrict v_dst = (HVX_Vector *) dst;
const int nvec = num_elems / VLEN_FP32; // number of full vectors
const int nloe = num_elems % VLEN_FP32; // leftover elements
// Compute sum of squares for full vectors
HVX_Vector sum_v = Q6_V_vsplat_R(0x00000000);
HVX_Vector epsilon_v = hvx_vec_splat_f32(epsilon);
#pragma unroll(4)
for (int i = 0; i < nvec; i++) {
HVX_Vector v1 = v_src[i];
HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, v1);
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, v2);
}
// Handle tail elements using vectorized ops with masking
if (nloe > 0) {
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * 4);
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, v1);
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, v2);
}
// Reduce HVX sum
sum_v = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_v));
HVX_Vector t_v = hvx_vec_splat_f32((float) num_elems);
HVX_Vector denom_v = hvx_vec_inverse_f32(t_v);
HVX_Vector mean_v = Q6_Vqf32_vmpy_VsfVsf(sum_v, denom_v);
HVX_Vector mean_epsilon_v = Q6_Vqf32_vadd_Vqf32Vsf(mean_v, epsilon_v);
// Scale full vectors
HVX_Vector scale_v = hvx_vec_rsqrt_f32(Q6_Vsf_equals_Vqf32(mean_epsilon_v));
#pragma unroll(4)
for (int i = 0; i < nvec; i++) {
HVX_Vector v1 = v_src[i];
HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, scale_v);
v_dst[i] = Q6_Vsf_equals_Vqf32(v2);
}
// Handle tail elements using vectorized ops with masking
if (nloe > 0) {
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * 4);
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, scale_v);
HVX_Vector result = Q6_Vsf_equals_Vqf32(v2);
// Store with masking to avoid overwriting memory beyond the tensor
hvx_vec_store_a(&v_dst[nvec], nloe * 4, result);
}
}
static inline void hvx_fast_rms_norm_mul_f32(const uint8_t * restrict src,
const uint8_t * restrict weight,
uint8_t * restrict dst,
const int num_elems,
float epsilon) {
const HVX_Vector * restrict v_src = (const HVX_Vector *) src;
const HVX_Vector * restrict v_weight = (const HVX_Vector *) weight;
HVX_Vector * restrict v_dst = (HVX_Vector *) dst;
const int nvec = num_elems / VLEN_FP32; // number of full vectors
const int nloe = num_elems % VLEN_FP32; // leftover elements
// Compute sum of squares for full vectors
HVX_Vector sum_v = Q6_V_vsplat_R(0x00000000);
HVX_Vector epsilon_v = hvx_vec_splat_f32(epsilon);
#pragma unroll(4)
for (int i = 0; i < nvec; i++) {
HVX_Vector v1 = v_src[i];
HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, v1);
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, v2);
}
// Handle tail elements using vectorized ops with masking
if (nloe > 0) {
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * 4);
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, v1);
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, v2);
}
// Reduce HVX sum
sum_v = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_v));
HVX_Vector t_v = hvx_vec_splat_f32((float) num_elems);
HVX_Vector denom_v = hvx_vec_inverse_f32(t_v);
HVX_Vector mean_v = Q6_Vqf32_vmpy_VsfVsf(sum_v, denom_v);
HVX_Vector mean_epsilon_v = Q6_Vqf32_vadd_Vqf32Vsf(mean_v, epsilon_v);
// Scale and multiply
HVX_Vector scale_v = hvx_vec_rsqrt_f32(Q6_Vsf_equals_Vqf32(mean_epsilon_v));
#pragma unroll(4)
for (int i = 0; i < nvec; i++) {
HVX_Vector v1 = v_src[i];
HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, scale_v);
HVX_Vector v3 = Q6_Vsf_equals_Vqf32(v2);
HVX_Vector result = Q6_Vqf32_vmpy_VsfVsf(v3, v_weight[i]);
v_dst[i] = Q6_Vsf_equals_Vqf32(result);
}
// Handle tail elements using vectorized ops with masking
if (nloe > 0) {
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * 4);
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, scale_v);
HVX_Vector v3 = Q6_Vsf_equals_Vqf32(v2);
HVX_Vector result = Q6_Vqf32_vmpy_VsfVsf(v3, v_weight[nvec]);
HVX_Vector res_v = Q6_Vsf_equals_Vqf32(result);
// Store with masking to avoid overwriting memory beyond the tensor
hvx_vec_store_a(&v_dst[nvec], nloe * 4, res_v);
}
}
static inline void hvx_fast_norm_f32(const uint8_t * restrict src,
uint8_t * restrict dst,
const int num_elems,
float epsilon) {
const HVX_Vector * restrict v_src = (HVX_Vector *) src;
HVX_Vector * restrict v_dst = (HVX_Vector *) dst;
const int nvec = num_elems / VLEN_FP32; // number of full vectors
const int nloe = num_elems % VLEN_FP32; // leftover elements
// Compute sum of squares and sum of values for full vectors
HVX_Vector sum_sq_v = Q6_V_vsplat_R(0x00000000);
HVX_Vector sum_x_v = Q6_V_vsplat_R(0x00000000);
HVX_Vector epsilon_v = hvx_vec_splat_f32(epsilon);
#pragma unroll(4)
for (int i = 0; i < nvec; i++) {
HVX_Vector v1 = v_src[i];
HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, v1);
sum_sq_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq_v, v2);
sum_x_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_x_v, Q6_Vqf32_vadd_VsfVsf(v1, Q6_V_vzero()));
}
// Handle tail elements using vectorized ops with masking
if (nloe > 0) {
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * 4);
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(v1, v1);
sum_sq_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_sq_v, v2);
sum_x_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_x_v, Q6_Vqf32_vadd_VsfVsf(v1, Q6_V_vzero()));
}
// Reduce HVX sums
sum_sq_v = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_sq_v));
sum_x_v = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_x_v));
HVX_Vector t_v = hvx_vec_splat_f32((float) num_elems);
HVX_Vector denom_v = hvx_vec_inverse_f32(t_v);
HVX_Vector mean_sq_v = Q6_Vqf32_vmpy_VsfVsf(sum_sq_v, denom_v);
HVX_Vector mean_x_v = Q6_Vqf32_vmpy_VsfVsf(sum_x_v, denom_v);
HVX_Vector mean_x_sq_v = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(mean_x_v), Q6_Vsf_equals_Vqf32(mean_x_v));
HVX_Vector var_v = Q6_Vqf32_vsub_Vqf32Vqf32(mean_sq_v, mean_x_sq_v);
HVX_Vector var_epsilon_v = Q6_Vqf32_vadd_Vqf32Vsf(var_v, epsilon_v);
// scale = rsqrt(variance + epsilon), mean_x broadcast for subtraction
HVX_Vector scale_v = hvx_vec_rsqrt_f32(Q6_Vsf_equals_Vqf32(var_epsilon_v));
HVX_Vector mean_x_b = hvx_vec_repl_f32(Q6_Vsf_equals_Vqf32(mean_x_v));
#pragma unroll(4)
for (int i = 0; i < nvec; i++) {
HVX_Vector v1 = v_src[i];
HVX_Vector v2 = Q6_Vqf32_vsub_VsfVsf(v1, mean_x_b);
HVX_Vector v3 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(v2), scale_v);
v_dst[i] = Q6_Vsf_equals_Vqf32(v3);
}
// Handle tail elements using vectorized ops with masking
if (nloe > 0) {
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * 4);
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
HVX_Vector v2 = Q6_Vqf32_vsub_VsfVsf(v1, mean_x_b);
HVX_Vector v3 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(v2), scale_v);
HVX_Vector result = Q6_Vsf_equals_Vqf32(v3);
// Store with masking to avoid overwriting memory beyond the tensor
hvx_vec_store_a(&v_dst[nvec], nloe * 4, result);
}
}
static inline void hvx_fast_l2_norm_f32(const uint8_t * restrict src,
uint8_t * restrict dst,
const int num_elems,
float epsilon) {
const HVX_Vector * restrict v_src = (HVX_Vector *) src;
HVX_Vector * restrict v_dst = (HVX_Vector *) dst;
HVX_Vector sum_v = hvx_vec_splat_f32(0.0f);
const int nvec = num_elems / VLEN_FP32;
const int nloe = num_elems % VLEN_FP32;
#pragma unroll(4)
for (int i = 0; i < nvec; i++) {
HVX_Vector v1 = v_src[i];
HVX_Vector sq = Q6_Vqf32_vmpy_VsfVsf(v1, v1);
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, sq);
}
// Include tail elements in the sum-of-squares using a predicate mask
if (nloe > 0) {
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * 4);
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
HVX_Vector sq = Q6_Vqf32_vmpy_VsfVsf(v1, v1);
sum_v = Q6_Vqf32_vadd_Vqf32Vqf32(sum_v, sq);
}
// Compute scale = 1/fmax(sqrt(sum), epsilon) entirely in HVX registers.
// hvx_vec_rsqrt_f32 + hvx_vec_inverse_f32 avoids scalar extraction.
HVX_Vector sum_sf = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(sum_v));
HVX_Vector rsqrt_v = hvx_vec_rsqrt_f32(sum_sf); // 1/sqrt(sum)
HVX_Vector sqrt_v = hvx_vec_inverse_f32(rsqrt_v); // sqrt(sum)
HVX_Vector epsilon_v = hvx_vec_splat_f32(epsilon);
HVX_Vector denom_v = Q6_Vsf_vmax_VsfVsf(sqrt_v, epsilon_v); // fmax(sqrt(sum), epsilon)
HVX_Vector scale_v = hvx_vec_inverse_f32(denom_v); // 1/fmax(sqrt(sum), epsilon)
#pragma unroll(4)
for (int i = 0; i < nvec; i++) {
HVX_Vector v1 = v_src[i];
v_dst[i] = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(v1, scale_v));
}
if (nloe > 0) {
HVX_VectorPred bmask = Q6_Q_vsetq_R(nloe * 4);
HVX_Vector v1 = Q6_V_vand_QV(bmask, v_src[nvec]);
HVX_Vector result = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(v1, scale_v));
hvx_vec_store_a(&v_dst[nvec], nloe * 4, result);
}
}
#endif // HVX_NORM_H

Some files were not shown because too many files have changed in this diff Show More