Files
vetta-infer/crates/vetta-infer-abi/src/vplan.rs
T
adm 87a66aee37 [RT][HIP] GPU executor core + first composed trunk forward verified on gfx1150
- rt::executor (feature `hip`): the bridge from the arena-validated plan to
  GPU compute. ExecOp { op, reads, writes, params } + OpParams (concrete
  shapes) + execute() dispatching the verified microkernels (embed, rms_norm,
  gemm_bf16, flash_gqa, moe_router, vec_add, cast). Lowering CompiledPlans+IR
  -> ExecOp lands with the packer; this core proves dispatch + composition.
- First COMPOSED GPU forward: embed gather -> rms_norm -> cast f32->bf16 ->
  gemm(unembed), verified end-to-end vs a CPU oracle (<1e-4). The composition
  test caught a real dtype-transition bug (f32 activation fed to a BF16 GEMM)
  — fixed by adding the cast kernel the reference also inserts before matmul.
- kernels/hip: k_cast_f32_bf16 (f32 activation -> BF16 truncate) + C ABI +
  wrapper. abi::vplan: CastF32Bf16 opcode (31 total).
- rt: optional hip dependency, feature-gated (default workspace stays
  host-independent).

Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS;
lean proofs (4 files, 42 theorems) green; GPU parity 7/7 + composed trunk
forward 1/1 (--features hip). Principle recorded: parity-first baseline, then
our own measured improvements (Federico).
2026-12-01 13:57:07 +01:00

167 lines
6.3 KiB
Rust

//! `.vplan` micro-bytecode opcodes (docs/ARCHITECTURE.md §4).
//!
//! Flat, fixed, non-Turing-complete: each compute opcode maps 1:1 to a HIP
//! microkernel in `kernels/hip/vetta_kernels.h`. Control flow is limited to
//! `LoopLayer` / `BranchB` so `memory.vplan` arena safety stays decidable
//! in Lean 4. Operands are resolved at boot into `CompiledPlan` (rt), never
//! looked up per token.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VplanOp {
// Compute opcodes (1:1 with the <15 microkernel set)
EmbedBf16,
RmsNorm,
QuantizeXG64,
GemmQ8G64T16,
GemmQ4G64T16,
GemmNvfp4Mxfp8,
GemmBf16,
RopeKvWrite,
FlashGqaWmma,
GdnChunkwiseWy,
GdnRecurrentStep,
MoeRouteBucket,
MoeW13Wmma,
MoeW2Wmma,
MoeReduce,
// Inkling-specific stages (docs/inkling-forward-design.md).
AttnResMix,
ShortConv4,
LoopCtrl,
L2Clip,
KvStore,
RelPosBias,
MoeRouterTopk,
VecAdd,
CastF32Bf16,
VitFlashAttnWmma,
GpuPenaltiesTopk,
// Encoder lane (M1.5 embedder, docs/ARCHITECTURE.md §4)
LayerNorm,
FlashFullAttn,
GeluErf,
/// Pooling + L2 normalize; mode (mean | cls | last_token) comes from the
/// manifest `encoder_recipe`, never guessed from the architecture.
PoolL2norm,
// Restricted control flow
/// Repeat the enclosed layer body for each model layer.
LoopLayer,
/// Batch-size dispatch (e.g. prefill `B >= 16` vs Split-KV decode `B <= 2`).
BranchB,
}
impl VplanOp {
/// True for the two restricted control-flow opcodes.
pub fn is_control(self) -> bool {
matches!(self, Self::LoopLayer | Self::BranchB)
}
/// Canonical wire name (as written in `graph.vplan`).
pub fn name(self) -> &'static str {
match self {
Self::EmbedBf16 => "embed_bf16",
Self::RmsNorm => "rms_norm",
Self::QuantizeXG64 => "quantize_x_g64",
Self::GemmQ8G64T16 => "gemm_q8_g64_t16",
Self::GemmQ4G64T16 => "gemm_q4_g64_t16",
Self::GemmNvfp4Mxfp8 => "gemm_nvfp4_mxfp8",
Self::GemmBf16 => "gemm_bf16",
Self::RopeKvWrite => "rope_kv_write",
Self::FlashGqaWmma => "flash_gqa_wmma",
Self::GdnChunkwiseWy => "gdn_chunkwise_wy",
Self::GdnRecurrentStep => "gdn_recurrent_step",
Self::MoeRouteBucket => "moe_route_bucket",
Self::MoeW13Wmma => "moe_w13_wmma",
Self::MoeW2Wmma => "moe_w2_wmma",
Self::MoeReduce => "moe_reduce",
Self::AttnResMix => "attn_res_mix",
Self::ShortConv4 => "shortconv4",
Self::LoopCtrl => "loop_ctrl",
Self::L2Clip => "l2_clip",
Self::KvStore => "kv_store",
Self::RelPosBias => "relpos_bias",
Self::MoeRouterTopk => "moe_router_topk",
Self::VecAdd => "vec_add",
Self::CastF32Bf16 => "cast_f32_bf16",
Self::VitFlashAttnWmma => "vit_flash_attn_wmma",
Self::GpuPenaltiesTopk => "gpu_penalties_topk",
Self::LayerNorm => "layer_norm",
Self::FlashFullAttn => "flash_full_attn",
Self::GeluErf => "gelu_erf",
Self::PoolL2norm => "pool_l2norm",
Self::LoopLayer => "loop_layer",
Self::BranchB => "branch_b",
}
}
/// Parse a wire name; unknown opcodes are rejected explicitly (never silent).
pub fn from_name(s: &str) -> Option<Self> {
Some(match s {
"embed_bf16" => Self::EmbedBf16,
"rms_norm" => Self::RmsNorm,
"quantize_x_g64" => Self::QuantizeXG64,
"gemm_q8_g64_t16" => Self::GemmQ8G64T16,
"gemm_q4_g64_t16" => Self::GemmQ4G64T16,
"gemm_nvfp4_mxfp8" => Self::GemmNvfp4Mxfp8,
"gemm_bf16" => Self::GemmBf16,
"rope_kv_write" => Self::RopeKvWrite,
"flash_gqa_wmma" => Self::FlashGqaWmma,
"gdn_chunkwise_wy" => Self::GdnChunkwiseWy,
"gdn_recurrent_step" => Self::GdnRecurrentStep,
"moe_route_bucket" => Self::MoeRouteBucket,
"moe_w13_wmma" => Self::MoeW13Wmma,
"moe_w2_wmma" => Self::MoeW2Wmma,
"moe_reduce" => Self::MoeReduce,
"attn_res_mix" => Self::AttnResMix,
"shortconv4" => Self::ShortConv4,
"loop_ctrl" => Self::LoopCtrl,
"l2_clip" => Self::L2Clip,
"kv_store" => Self::KvStore,
"relpos_bias" => Self::RelPosBias,
"moe_router_topk" => Self::MoeRouterTopk,
"vec_add" => Self::VecAdd,
"cast_f32_bf16" => Self::CastF32Bf16,
"vit_flash_attn_wmma" => Self::VitFlashAttnWmma,
"gpu_penalties_topk" => Self::GpuPenaltiesTopk,
"layer_norm" => Self::LayerNorm,
"flash_full_attn" => Self::FlashFullAttn,
"gelu_erf" => Self::GeluErf,
"pool_l2norm" => Self::PoolL2norm,
"loop_layer" => Self::LoopLayer,
"branch_b" => Self::BranchB,
_ => return None,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn control_vs_compute() {
assert!(VplanOp::LoopLayer.is_control());
assert!(VplanOp::BranchB.is_control());
assert!(!VplanOp::GemmQ8G64T16.is_control());
assert!(!VplanOp::GdnChunkwiseWy.is_control());
}
#[test]
fn name_roundtrip_all_opcodes() {
let all = [
VplanOp::EmbedBf16, VplanOp::RmsNorm, VplanOp::QuantizeXG64,
VplanOp::GemmQ8G64T16, VplanOp::GemmQ4G64T16, VplanOp::GemmNvfp4Mxfp8, VplanOp::GemmBf16,
VplanOp::RopeKvWrite, VplanOp::FlashGqaWmma, VplanOp::GdnChunkwiseWy,
VplanOp::GdnRecurrentStep, VplanOp::MoeRouteBucket, VplanOp::MoeW13Wmma,
VplanOp::MoeW2Wmma, VplanOp::MoeReduce, VplanOp::VitFlashAttnWmma,
VplanOp::AttnResMix, VplanOp::ShortConv4, VplanOp::LoopCtrl, VplanOp::L2Clip,
VplanOp::KvStore, VplanOp::RelPosBias, VplanOp::MoeRouterTopk, VplanOp::VecAdd, VplanOp::CastF32Bf16,
VplanOp::GpuPenaltiesTopk, VplanOp::LayerNorm, VplanOp::FlashFullAttn,
VplanOp::GeluErf, VplanOp::PoolL2norm, VplanOp::LoopLayer, VplanOp::BranchB,
];
for op in all {
assert_eq!(VplanOp::from_name(op.name()), Some(op));
}
assert_eq!(VplanOp::from_name("bogus"), None);
}
}