Skip to content

feat: add packed cvt variants (rz, relu) for f16x2 and bf16x2 - #276

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
honeyspoon:upstream-cvt-variants
Jun 24, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
honeyspoon:upstream-cvt-variants

Conversation

@honeyspoon

Copy link
Copy Markdown
Contributor

Add four packed type-conversion intrinsics completing the f16x2/bf16x2 conversion family alongside the existing cvt.rn variants.

Summary

The existing cvt_f16x2_f32 converts two f32 values into a packed f16x2 using round-to-nearest-even (.rn). This PR adds the four remaining packed conversion variants that are important for inference workloads on sm_80+ hardware:

  • cvt.rz.f16x2.f32 and cvt.rz.bf16x2.f32: Round-toward-zero (truncation) instead of round-to-nearest-even. Truncation rounding is preferred in quantization-aware inference pipelines where deterministic floor-toward-zero behavior is needed, avoiding the slight upward bias of round-to-nearest-even on half-boundary values.

  • cvt.rn.relu.f16x2.f32 and cvt.rn.relu.bf16x2.f32: Fuse a ReLU clamp (negative values become zero) into the conversion in a single instruction. Fusing ReLU eliminates a separate max(x, 0) step, saving one instruction per element pair in activation layers.

All four instructions pack two f32 inputs into a single u32 (f16x2 or bf16x2), matching the existing cvt_f16x2_f32 calling convention (lo: f32, hi: f32) -> u32.

Device API PTX Instruction Description
cvt_rz_f16x2_f32(lo, hi) cvt.rz.f16x2.f32 d, hi, lo; f32 pair to packed f16x2, round-toward-zero
cvt_rn_relu_f16x2_f32(lo, hi) cvt.rn.relu.f16x2.f32 d, hi, lo; f32 pair to packed f16x2, round-to-nearest + ReLU
cvt_rn_relu_bf16x2_f32(lo, hi) cvt.rn.relu.bf16x2.f32 d, hi, lo; f32 pair to packed bf16x2, round-to-nearest + ReLU
cvt_rz_bf16x2_f32(lo, hi) cvt.rz.bf16x2.f32 d, hi, lo; f32 pair to packed bf16x2, round-toward-zero

Part of #274

Changes

  • cuda-device: 4 new device API stubs in convert.rs (cvt_rz_f16x2_f32, cvt_rn_relu_f16x2_f32, cvt_rn_relu_bf16x2_f32, cvt_rz_bf16x2_f32), each (lo: f32, hi: f32) -> u32, #[inline(never)].
  • dialect-nvvm: 4 new NVVM ops (CvtRzF16x2F32Op, CvtRnReluF16x2F32Op, CvtRnReluBf16x2F32Op, CvtRzBf16x2F32Op) with 2 operands and 1 result.
  • mir-importer: 4 emit_cvt_* translation functions dispatched from try_dispatch_intrinsic.
  • mir-lower: 4 convert_cvt_* lowering functions emitting pure inline PTX assembly (=r,f,f constraints, AsmKind::Pure); 4 MirToLlvmConversion trait impls.
  • tests: 4 new lowering tests verifying the correct PTX mnemonic in the lowered inline assembly.

Testing

  • cargo check -p dialect-nvvm -p mir-lower passes
  • cargo test -p mir-lower --test lowering_test passes (4 new + existing)
  • cargo fmt --check and cargo clippy --workspace -- -D warnings clean
  • E2E example deferred (will follow up)

Checklist

  • All commits signed off (git commit -s)
  • No files under cuda-bindings/ modified
  • SPDX headers on new source files

@honeyspoon
honeyspoon force-pushed the upstream-cvt-variants branch from aa72b1f to 00b31a2 Compare June 22, 2026 19:04
@nihalpasham
nihalpasham force-pushed the upstream-cvt-variants branch from 00b31a2 to 5741af2 Compare June 24, 2026 04:44
@nihalpasham nihalpasham added intrinsics Device intrinsics and libdevice math mappings codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) enhancement New feature or request labels Jun 24, 2026
honeyspoon and others added 2 commits June 24, 2026 10:30
Add four packed type-conversion intrinsics completing the f16x2/bf16x2
conversion family alongside the existing cvt.rn variants:

- cvt.rz.f16x2.f32 / cvt.rz.bf16x2.f32: round-toward-zero truncation
- cvt.rn.relu.f16x2.f32 / cvt.rn.relu.bf16x2.f32: ReLU-fused conversion

Lowered to pure inline PTX assembly with AsmKind::Pure, matching the
existing cvt_f16x2_f32 pattern. Critical for quantization-aware inference
(truncation rounding) and fused activation layers (ReLU).

Part of NVIDIA#274

Signed-off-by: abder <[email protected]>
Rebasing the rz/relu cvt intrinsics onto the post-NVIDIA#277 TypeHandle API
surfaced two breaks the stale base hid:

- The four new emit_* helpers built their result type with the removed
  `TypedHandle::to_ptr()`. Switch to `.to_handle()`, matching the
  sibling emit_cvt_f16x2_f32 on main.
- The commit had picked up seven stray `*Bf16x2Op` arithmetic imports
  (Abs/Add/Sub/Mul/Min/Max/Neg) from an unrelated branch; they are
  undefined here and unused, so drop them to keep the PR cvt-only.
@nihalpasham
nihalpasham force-pushed the upstream-cvt-variants branch from 5741af2 to 437f533 Compare June 24, 2026 05:02
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR. LGTM 👍🏾

  • this clones the cvt_f16x2_f32 path for the rz/relu variants. Operand order ($0,$2,$1 = d,hi,lo) and AsmKind::Pure match the ISA and the existing variant. So, we're good.

Rebased onto the TypeHandle-API main. The stale base hid two breaks I fixed on top:

  • the four new emit_* helpers built the result type with the removed TypedHandle::to_ptr(). Switched to .to_handle(), what the sibling emit_cvt_f16x2_f32 already uses.
  • the commit had picked up seven stray *Bf16x2Op arithmetic imports (Abs/Add/Sub/Mul/Min/Max/Neg) from another branch. Undefined here and unused, so I dropped them to keep this cvt-only. Worth double-checking your local bf16x2-arith branch still has them.

Reviewer note: no GPU e2e in this env. ReLU-on-negatives/NaN is only asserted via mnemonic substring, so an sm_80+ example is still owed. No issue closed (only "Part of #274").

@nihalpasham
nihalpasham merged commit 2186175 into NVIDIA:main Jun 24, 2026
24 checks passed
honeyspoon added a commit to honeyspoon/cuda-oxide that referenced this pull request Jun 24, 2026
Follow-up to NVIDIA#276, adds the GPU-verified example noted as owed.
Tests cvt.rz, cvt.rn.relu for both f16x2 and bf16x2, verifying
ReLU clamps negatives to zero and rz truncates toward zero.

Part of NVIDIA#274

Signed-off-by: abder <[email protected]>
nihalpasham pushed a commit to honeyspoon/cuda-oxide that referenced this pull request Jun 28, 2026
Follow-up to NVIDIA#276, adds the GPU-verified example noted as owed.
Tests cvt.rz, cvt.rn.relu for both f16x2 and bf16x2, verifying
ReLU clamps negatives to zero and rz truncates toward zero.

Part of NVIDIA#274

Signed-off-by: abder <[email protected]>
nihalpasham added a commit that referenced this pull request Jun 28, 2026
* feat: add packed cvt variants end-to-end example (sm_80+)

Follow-up to #276, adds the GPU-verified example noted as owed.
Tests cvt.rz, cvt.rn.relu for both f16x2 and bf16x2, verifying
ReLU clamps negatives to zero and rz truncates toward zero.

Part of #274

Signed-off-by: abder <[email protected]>

* fix: make packed conversion checks bit-exact

Signed-off-by: nihalp <[email protected]>

---------

Signed-off-by: abder <[email protected]>
Signed-off-by: nihalp <[email protected]>
Co-authored-by: nihalp <[email protected]>
@honeyspoon
honeyspoon deleted the upstream-cvt-variants branch June 30, 2026 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings IR-lowering Lowering between dialects (dialect-mir to LLVM dialect)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants