Files
vetta-infer/kernels/hip/vetta_kernels.h
T
adm bd49da5636 [HIP] NVFP4 expert GEMV kernel (exact E2M1+E4M3 dequant, GPU-verified ~4e-7)
- kernels/hip: NVFP4 (SF4) codecs ported exactly from strata ink_hip_common —
  sf4_e2m1_f32 (E2M1 nibble LUT {0,0.5,1,1.5,2,3,4,6,...}), e4m3fn_to_f32
  (F8_E4M3FN: subnormal mant*2^-9, NaN->0, bias 7). Layout: packed_e2m1
  [N,K/2] (low nib even col), scales_e4m3 [N,K/16], f32 outer_scale.
- k_nvfp4_gemv (M=1 decode): y[N] = dequant(W)[N,K] @ x[K], one block/row,
  threads stride K/16 groups, f32 accumulate. Correctness-first; the Wave32
  bandwidth-saturated path is the performance pass. This is the core of the
  MoE expert compute (w13/w2); the full direct path (SiTU activation +
  weighted reduce) builds on it.
- C ABI + safe wrapper nvfp4_gemv + CPU-oracle parity test (Rust e2m1/e4m3
  matching the kernel).

Verified LIVE on gfx1150: nvfp4_gemv max_rel_err ~4e-7 across (8,64)/(16,256)/
(32,128). Eight compute kernels now verified. Default suite 97/97, lean 4
files green; GPU parity 8/8 (--features hip).
2026-12-01 14:07:14 +01:00

92 lines
3.7 KiB
C

// kernels/hip/vetta_kernels.h
// Minimal C ABI for the <15 reusable RDNA 3.5 (gfx1150) HIP microkernels shared across all MoE models.
#pragma once
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef void* vetta_stream_t;
typedef void* vetta_event_t;
typedef struct vetta_device_info_t {
char name[128];
char gcn_arch[64];
int multi_processor_count;
int wave_size;
uint64_t total_vram_bytes;
int is_uma;
} vetta_device_info_t;
// Runtime & UMA zero-copy buffer lifecycle (Milestone 1+)
int vetta_hip_query_device(int dev_id, vetta_device_info_t* out_info);
int vetta_hip_register_uma_pages(void* host_aligned_ptr, size_t bytes, void** out_dev_ptr);
int vetta_hip_unregister_uma_pages(void* host_aligned_ptr);
// Minimal device buffer lifecycle (executor foundation).
int vetta_buf_alloc(void** out, size_t bytes);
int vetta_buf_free(void* p);
int vetta_buf_h2d(void* dst, const void* src, size_t bytes);
int vetta_buf_d2h(void* dst, const void* src, size_t bytes);
// k_rms_norm (kernel #2): y[row] = x[row] * rsqrt(mean(x^2)+eps) * w.
int vetta_rms_norm_f32(const float* x, const float* w, float* y,
int rows, int D, float eps, vetta_stream_t stream);
// k_embed_bf16 (kernel #1): gather token rows from a BF16 table -> f32.
int vetta_embed_bf16_gather(const uint16_t* table, const int32_t* tokens,
float* out, int B, int D, int vocab,
vetta_stream_t stream);
// k_gemm_bf16: y[M,N] = x[M,K] @ w[N,K]^T, BF16 inputs, f32 accumulate.
int vetta_gemm_bf16(const uint16_t* x, const uint16_t* w, float* y,
int M, int N, int K, vetta_stream_t stream);
// k_flash_gqa_decode (base): single-token GQA attention over a f32 KV cache.
int vetta_flash_gqa_decode(const float* q, const float* k, const float* v,
float* out, int n_head, int n_kv, int seq,
int hd, float scale, vetta_stream_t stream);
// k_moe_router_topk: Inkling exact sigmoid router (top-K, route_scale norm).
int vetta_moe_router_topk(const float* logits, const float* bias,
int32_t* topk_ids, float* topk_weights,
int M, int n_experts, int K_top,
float route_scale, vetta_stream_t stream);
// k_vec_add: y = a + b elementwise (residual stream).
int vetta_vec_add(const float* a, const float* b, float* y, size_t n,
vetta_stream_t stream);
// k_cast_f32_bf16: f32 activation -> BF16 (truncate) before a BF16 GEMM.
int vetta_cast_f32_bf16(const float* x, uint16_t* y, size_t n,
vetta_stream_t stream);
// k_nvfp4_gemv (M=1): y[N] = dequant(W)[N,K] @ x[K], NVFP4 (E2M1 + E4M3/16 +
// f32 outer_scale). packed [N,K/2], scales [N,K/16]. K % 16 == 0.
int vetta_nvfp4_gemv(const uint8_t* packed, const uint8_t* scales,
float outer_scale, const float* x, float* y,
int N, int K, vetta_stream_t stream);
// Core <15 microkernel entry points (implemented progressively in M1 & M2):
// 1. k_embed_bf16
// 2. k_rms_norm
// 3. k_quantize_x_g64
// 4. k_gemm_q8_g64_t16
// 5. k_gemm_q4_g64_t16 (SWAR 3-inst INT4->INT8 WMMA)
// 6. k_gemm_nvfp4_mxfp8
// 7. k_rope_kv_write
// 8. k_flash_gqa_wmma
// 9. k_gdn_chunkwise_wy (WY-representation parallel prefill)
// 10. k_gdn_recurrent_step (0-spill decode + rollback history)
// 11. k_moe_route_bucket (100% GPU routing + bitonic coreset + NaN/mask safety)
// 12. k_moe_grouped_wmma (compacted w13 fused SwiGLU+quantize & w2 + reduce)
// 13. k_vit_flash_attn_wmma (64x64 FP16 WMMA Vision)
// 14. k_gpu_penalties_topk (GPU penalties + top-64 extraction, <=512 B D2H/row)
#ifdef __cplusplus
}
#endif