kvcache-ai-ktransformers/kt-kernel/examples
Benjamin ef6c47f9d2
[feat](kt-kernel): AVX2 MXFP4 MoE MXFP4 dispatch (#2015)
* [feat](kt-kernel): AVX2 MXFP4 MoE MXFP4 dispatch

- Add AVX2 MXFP4 MoE kernel (mxfp4-moe.hpp) with 4-token M-blocking
- Wire AVX2MXFP4_MOE binding in ext_bindings.cpp
- Support TP_MOE down_proj slicing and multi-pool per-expert loading
- Add test_fp4_moe_avx2.py integration test

* [fix](kt-kernel): address PR #2010 review — memory leaks, alignment, dynamic expert update

- Track aligned_alloc pointers in AVX2_MOE_BASE::owned_aligned_allocs_ and
  free them in the destructor (fixes BufferB backing memory leak on destroy).
- Track per-TP down_buf allocations in TP_MOE::tp_owned_down_bufs_ with
  nullptr checks and size rounding to alignment boundary.
- Add nibble-alignment runtime check for per_tp_interm in MXFP4 TP K-split.
- Add write_weight_scale_to_buffer override to TP_MOE<AVX2_MXFP4_MOE_TP>,
  enabling dynamic expert update with kt-threadpool-count>=2.
- Guard against ZeroDivisionError in test_fp4_moe_avx2.py.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* [fix](kt-kernel): add intermediate_size parity check in MXFP4 TP flat-buffer path

The per-expert path validates that intermediate_size is even (required for
nibble-aligned FP4 addressing), but the flat-buffer path was missing this
check — an odd value would silently truncate /2 divisions, corrupting
memcpy sizes and offsets.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(avx2-moe): fix TP offset calculation and add safety checks

C1-C4: Fix incorrect TP offset calculations in load_weights()
- Per-expert mode used per_tp_interm instead of full_interm for offsets
- This caused segfault when TP > 1 due to invalid pointer arithmetic

H1-H3: Add safety checks
- H1: Validate source weight pointers are not null
- H2: Check lid index is within bounds
- H3: Check BufferB.b is not null in gemm_mxfp4

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix(avx2-moe): revert incorrect C2/C4 offset changes, keep safety checks

Reverts the incorrect offset calculation changes from previous commit.
The original per_tp_interm-based offsets were correct:
- gate/up weights are N-split (along intermediate dim)
- Each TP partition handles per_tp_interm rows
- Offset = i * per_tp_interm * hidden / 2 (not full_interm)

Keeps H1-H3 safety checks:
- H1: Validate source weight pointers are not null
- H2: Check lid index is within bounds
- H3: Check BufferB.b is not null in gemm_mxfp4

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix(avx2): copy weights to owned buffers in per-expert mode

Previously, AVX2 MXFP4 MoE per-expert mode directly pointed BufferB.b
into mmap'd safetensor data. This caused use-after-free when Python
layer releases the mmap after load_weights() returns.

Now AVX2 copies weights into owned buffers via memcpy/from_raw_mat(),
matching AMX behavior. This decouples the MoE weights from mmap lifecycle.

Changes:
- buffer_b_required_size_impl: always allocate full buffer (weights + scales)
- make_buffer_b_impl: always create full BufferB with owned storage
- Single-TP per-expert: use from_raw_mat() instead of direct pointer
- TP_MOE per-expert: add gate/up owned buffers with memcpy
- Destructor: free gate/up buffers alongside down

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Revert "[fix] Add runtime AMX BF16 check to prevent SIGILL on pre-Sapphire Rapids CPUs (#2018)"

This reverts commit f1e2b82c74.

* Remove AMX tile MXFP4 kernel (GemmKernel224MXFP4)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-30 19:20:16 +08:00
..
.gitignore add kt-kernel 2025-10-12 05:13:00 +00:00
bench_moe_amx_int8.py [feat](kt-kernel): CPU-GPU experts sched (#1796) 2026-01-16 17:01:15 +08:00
configuration_deepseek_v3.py add kt-kernel 2025-10-12 05:13:00 +00:00
modeling_deepseek_v3.py add kt-kernel 2025-10-12 05:13:00 +00:00
repro_llamafile_re.py fix kt-kernel installation issue (#1603) 2025-11-12 15:56:02 +08:00
test-debug.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_apply_rope.py add kt-kernel 2025-10-12 05:13:00 +00:00
test_attention.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_awq_moe_amx.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_bf16_moe.py support GLM 4.7 (#1791) 2026-01-13 17:36:25 +08:00
test_deepseekv3.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_deepseekv3_prefill.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_deepseekv3_prefill_speed.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_fp4_moe_amx.py [New Model] DeepSeek-V4-Flash: kt-kernel MXFP4 MoE + sglang hybrid inference (#1970) 2026-05-03 10:48:31 +08:00
test_fp4_moe_avx2.py [feat](kt-kernel): AVX2 MXFP4 MoE MXFP4 dispatch (#2015) 2026-05-30 19:20:16 +08:00
test_fp4_moe_v4.py [New Model] DeepSeek-V4-Flash: kt-kernel MXFP4 MoE + sglang hybrid inference (#1970) 2026-05-03 10:48:31 +08:00
test_fp8_moe.py [feat](kt-kernel): CPU-GPU experts sched (#1796) 2026-01-16 17:01:15 +08:00
test_fp8_perchannel_moe.py support GLM 4.7 (#1791) 2026-01-13 17:36:25 +08:00
test_gate.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_k2_moe_amx.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_k2_write_buffer.py Kt minimax (#1742) 2025-12-24 15:39:44 +08:00
test_linear.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_mla.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_mla_qlen.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_mla_quant.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_mla_simple.py add kt-kernel 2025-10-12 05:13:00 +00:00
test_mla_torch.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_mlp.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_moe.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_moe_amx.py feat(sft): AMX MoE SFT backend with LoRA support (#1936) 2026-04-22 11:27:01 +08:00
test_moe_kernel.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_moe_kml.py [fix](test): fix import kt-kernel (#1728) 2025-12-17 19:46:32 +08:00
test_rope.cpp add kt-kernel 2025-10-12 05:13:00 +00:00
test_rope.py add kt-kernel 2025-10-12 05:13:00 +00:00
test_softmax.py add kt-kernel 2025-10-12 05:13:00 +00:00
test_write_buffer.py support GLM 4.7 (#1791) 2026-01-13 17:36:25 +08:00
torch_attention.py add kt-kernel 2025-10-12 05:13:00 +00:00