layer_forward_full_batched mirrors layer_forward_full at M=B: 3-query mix [B*3,h] split strided via gather_channel(nq=3) into in0/in1/in2 with separate q/k/v norms; DYNAMIC ShortConv K/V via shortconv4_prefill_par(dyn_taps[B,n_kv,4]) (already supported dyn path, no new kernel); Q/R l2_clip with per-token tau; global flash window=0; MoE sublayer identical to batched SWA. k_l2_clip gains pos_arr (device [M]): tau uses pos_arr[r/scale_rows] per token (full-attn) else scalar pos. l2_clip passes null (M=1 unchanged); l2_clip_batched takes Option<&DeviceBuf> pos_arr. layer_full_batched_parity (real weights ip=5, B=8, f16 KV + device MoE): cur[B,h] BIT-EXACT (0.0) AND KV byte-identical vs 8 sequential M=1 calls. Both layer types (SWA + full-attn) now batched and verified.
199 lines
9.9 KiB
C
199 lines
9.9 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).
|
|
// Set blocking host-wait schedule (no 100% CPU spin during GPU waits); call once
|
|
// before the first allocation creates the device context.
|
|
int vetta_hip_init_blocking_sync(void);
|
|
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);
|
|
// Device-to-device copies (prefix-checkpoint snapshot/restore).
|
|
int vetta_buf_copy(void* dst, const void* src, size_t bytes);
|
|
int vetta_buf_copy2d(void* dst, size_t dpitch, const void* src, size_t spitch,
|
|
size_t width, size_t height);
|
|
|
|
// 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_router_gemv (M=1): y[N] = x_f32[K] @ w_bf16[N,K]^T, f32 accumulate. The
|
|
// router reads F32 mlpin (exact strata B=1 launch_f32_linear), not bf16.
|
|
int vetta_router_gemv(const float* x, const uint16_t* w, float* y,
|
|
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_flash_swa_gqa_decode: Inkling flash SWA GQA — SWA window + relpos bias +
|
|
// attention sink + sigmoid gate. rel_bias/sink_logit/gate may be NULL.
|
|
int vetta_flash_swa_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, int swa_window, int cur_pos,
|
|
const float* rel_bias, int max_rel,
|
|
const float* sink_logit, const float* gate,
|
|
float* scores_scratch, vetta_stream_t stream);
|
|
|
|
// k_flash_swa_gqa_decode_f16: FP16 KV cache variant (strata default FP16 KV).
|
|
int vetta_flash_swa_gqa_decode_f16(const float* q, const uint16_t* k, const uint16_t* v,
|
|
float* out, int n_head, int n_kv, int seq, int hd,
|
|
float scale, int swa_window, int cur_pos,
|
|
const float* rel_bias, int max_rel,
|
|
const float* sink_logit, const float* gate,
|
|
float* scores_scratch, 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_cast_f32_f16: f32 activation -> FP16 (RNE), exact strata k_f32_to_f16.
|
|
int vetta_cast_f32_f16(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);
|
|
|
|
// k_nvfp4_gemv_f16: FP16-activation variant (exact strata B=1 expert path).
|
|
int vetta_nvfp4_gemv_f16(const uint8_t* packed, const uint8_t* scales,
|
|
float outer_scale, const uint16_t* x, float* y,
|
|
int N, int K, vetta_stream_t stream);
|
|
|
|
// k_mxfp8_gemv (M=1): MXFP8 experts (layers 20..23). y[N] = sum_k e4m3(W[n,k])
|
|
// * ue8m0(scale[n,k/32]) * x[k]. weights [N,K] F8_E4M3, scales [N,K/32] U8.
|
|
int vetta_mxfp8_gemv(const uint8_t* weights, const uint8_t* scales,
|
|
const float* x, float* y, int N, int K, vetta_stream_t stream);
|
|
|
|
// k_mxfp8_gemv_f16: FP16-activation variant (exact strata MX8 expert path).
|
|
int vetta_mxfp8_gemv_f16(const uint8_t* weights, const uint8_t* scales,
|
|
const uint16_t* x, float* y, int N, int K, vetta_stream_t stream);
|
|
|
|
// k_situ: SiTU double-softcapped MoE activation. gu INTERLEAVED gate/up
|
|
// (row 2j=gate[j], 2j+1=up[j], 2*inter) -> act f32.
|
|
int vetta_situ(const float* gu, float* act, int inter, float beta, float lb,
|
|
vetta_stream_t stream);
|
|
|
|
// k_situ_f16: SiTU computed in f32, stored FP16 (exact strata act_out half).
|
|
int vetta_situ_f16(const float* gu, uint16_t* act, int inter, float beta, float lb,
|
|
vetta_stream_t stream);
|
|
|
|
// k_moe_reduce: out[d] = sum_e tk_w[e]*y_part[e,d] (weighted expert sum).
|
|
int vetta_moe_reduce(const float* y_part, const float* tk_w, float* out,
|
|
int top_k, int hidden, vetta_stream_t stream);
|
|
|
|
// k_shortconv4_decode: Inkling causal depthwise ShortConv4 (static), history
|
|
// ring [slots,3,C] shifted in place. cur/out [M,C], static_w [C,4].
|
|
int vetta_shortconv4_decode(const float* cur, const float* static_w, float* hist,
|
|
float* out, const int32_t* slot_ids, int M, int C,
|
|
vetta_stream_t stream);
|
|
|
|
// k_shortconv4_dyn_decode: full-attn ShortConv4 with dynamic taps [M,n_heads,4].
|
|
int vetta_shortconv4_dyn_decode(const float* cur, const float* static_w, const float* dyn_taps,
|
|
float* hist, float* out, const int32_t* slot_ids,
|
|
int M, int C, int n_heads, vetta_stream_t stream);
|
|
|
|
// k_attn_res_mix: Inkling AttnRes deep-residual block-mix. blocks [n_blocks,M,D]
|
|
// snapshots + cur [M,D] -> out [M,n_queries,D] (RMS-normalized depth mix).
|
|
int vetta_attn_res_mix(const float* blocks, const float* cur, const float* queries,
|
|
float* out, int n_blocks, int M, int n_queries, int D,
|
|
float eps, vetta_stream_t stream);
|
|
|
|
// k_l2_clip: per-head L2 clip + optional per-dim scale + optional tau (in place).
|
|
int vetta_l2_clip(float* x, const float* scale, int rows, int hd, float clip,
|
|
int apply_tau, int pos, int scale_rows, const int32_t* pos_arr, vetta_stream_t stream);
|
|
|
|
// k_relpos_bias: rel_bias[h,dist] = sum_rc R[h,rc]*rel_proj[rc,dist], dist<lim.
|
|
int vetta_relpos_bias(const float* r, const float* rel_proj, float* rel_bias,
|
|
int n_head, int d_rel, int rel_extent, int lim,
|
|
vetta_stream_t stream);
|
|
|
|
// k_kv_store: K (pre-clipped) as-is + V RMS-normalized -> contiguous f32 cache.
|
|
int vetta_kv_store(const float* k_in, const float* v_in, float* k_cache,
|
|
float* v_cache, int pos, int n_kv, int hd, int seq,
|
|
float rms_eps, vetta_stream_t stream);
|
|
|
|
// k_kv_store_f16: K/V rounded to FP16 (RNE) -> contiguous u16 cache (strata FP16 KV).
|
|
int vetta_kv_store_f16(const float* k_in, const float* v_in, uint16_t* k_cache,
|
|
uint16_t* v_cache, int pos, int n_kv, int hd, int seq,
|
|
float rms_eps, vetta_stream_t stream);
|
|
|
|
// k_softcap: y = cap * tanh(x / cap) (final logit softcapping).
|
|
int vetta_softcap(const float* x, float* y, size_t n, float cap, vetta_stream_t stream);
|
|
|
|
// k_ctrl_pack: RMS-normalize cur & msg -> packed BF16 [2D] (loop controller input).
|
|
int vetta_ctrl_pack(const float* cur, const float* msg, uint16_t* packed, int D,
|
|
float eps, vetta_stream_t stream);
|
|
|
|
// k_ctrl_apply: out = (1-tanh(forget))*cur + tanh(gain)*msg (loop gating).
|
|
int vetta_ctrl_apply(const float* cur, const float* msg, const float* gain,
|
|
const float* forget, float* out, size_t n, 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
|