KIR Semantics and Numerical Contracts¶
KIR (algorithm/kir) is IntentDSL V2's target-independent algorithm IR. Authors express logical domains, accesses, reductions, contractions, and explicit state; KIR canonicalizes types, broadcasts, casts, alias groups, and numerical permissions (ci.semantics) before lowering to Block or CPU.
Default Numerical Semantics¶
When the author omits explicit overrides, IntentDSL aligns defaults with PyTorch and Inductor:
- Ordinary FP32 arithmetic: Preserves IEEE subnormals (
denormal_fp_math="ieee"). - Ordinary FP32
I.exp(x): Matches Inductor'sCppVecOverrides.expandcnnlSoftmaxForward_v2exponential accuracy (SLEEF u10 / 1 ULP target). PassingI.exp(x, approximate=True)attachesafnto that operation, licensing ATen SDPA'sfexp_u20on narrowed CPU attention weights or SFU__bang_active_exphpon BANG C. - Ordinary FP32 division: Preserves IEEE round-to-nearest (
RN) unlessI.fdiv(a, b, approximate=True)orfast_math=Truelicenses reciprocal approximations (arcp). - Narrow matrix contractions (
BF16/FP16/FP8/INT8):
Target Semantics of Narrow Dot Products¶
Each exit registers its native matrix instruction's input, accumulator, and subnormal contract:
- x86 (mojo, weft): Native AMX / VDPBF16PS (onednn-brgemm-bf16) may flush BF16 subnormal inputs/outputs when permitted; passing denormal_fp_math="ieee" or float32_matmul_precision="highest" selects widened IEEE FP32 products.
- MLU370 (bangc): Matches torch.bmm (cnnlStrideBatchMatMul_v3): BF16 dot products flush subnormals to zero while FP16 preserves IEEE subnormals with FP32 accumulation (Scoreboard).
- NVIDIA GPU (triton, cutile): Follows Tensor Core MMA operand/accumulator contracts; float32_matmul_precision="highest" requires exact FP32 products.
- RISC-V RVV (weft on SG2044): Widens BF16 contractions to IEEE FP32.
Explicit Author Overrides on I.jit¶
denormal_fp_math="ieee"or"preserve-sign": Require IEEE subnormal preservation or license subnormal flushing.float32_matmul_precision="highest": Disable TF32 / reduced-precision FP32 matrix products.fpmath_mode="strict": Require separate multiply/add rounding (no FMA fusion) and source-order normalization.I.ordered: Preserves declared sequential reduction or recurrence order.
Inter-Member Shared Computation¶
During @I.program composition, KIR aligns argument views and proves equivalence across pure/read-only contractions, reductions, and causal bounds, allowing downstream Block and CPU planners to compute shared intermediates (such as QK and dO·Vᵀ tiles in backward attention) once across dependent members without violating numerical permissions or side-effect ordering.
Full implementation references and hardware counterexamples are documented in the Chinese specification (doc/design/kir.md on GitHub), Numerics.cpp, and Intent-Bench numerical criteria.