Skip to content

feat: add integer dot product instructions (dp4a, dp2a) for sm_61+ - #275

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

nihalpasham merged 2 commits into
NVIDIA:mainfrom
honeyspoon:upstream-dot-products

Conversation

@honeyspoon

Copy link
Copy Markdown
Contributor

Add dp4a (4-element byte dot product) and dp2a (2-element half-word dot product) intrinsics in signed and unsigned variants, lowered to pure inline PTX assembly.

Summary

Implements the PTX dp4a and dp2a integer dot-product-with-accumulate family across the full compiler pipeline. These single-thread, non-convergent instructions perform packed-integer multiply-accumulate in a single cycle and are the building blocks for INT8/INT4 quantised inference on sm_61+ GPUs (Pascal and later).

Each operation takes three 32-bit inputs (a, b, c) and produces one 32-bit output d = c + dot(a, b). For dp4a, a and b are treated as 4 packed bytes; for dp2a, a is 2 packed 16-bit values and the lower 2 bytes of b are used. Both come in signed (s32) and unsigned (u32) flavors. The lowering uses pure inline PTX assembly (no LLVM intrinsic exists for these), tagged with AsmKind::Pure (non-convergent, no side effects), matching the pattern used by fma_bf16x2.

Device API PTX Instruction Semantics
dp4a_s32(a, b, c) dp4a.s32.s32 %d, %a, %b, %c; 4x signed byte dot product + i32 accumulate
dp4a_u32(a, b, c) dp4a.u32.u32 %d, %a, %b, %c; 4x unsigned byte dot product + u32 accumulate
dp2a_s32(a, b, c) dp2a.lo.s32.s32 %d, %a, %b, %c; 2x signed half-word × byte dot product + i32 accumulate
dp2a_u32(a, b, c) dp2a.lo.u32.u32 %d, %a, %b, %c; 2x unsigned half-word × byte dot product + u32 accumulate

Part of #274

Changes

  • cuda-device: 4 new device-side stubs in dotprod module (dp4a_s32, dp4a_u32, dp2a_s32, dp2a_u32), each #[inline(never)] with unreachable! host-side fallback.
  • dialect-nvvm: 4 new pliron ops (Dp4aS32Op, Dp4aU32Op, Dp2aS32Op, Dp2aU32Op), each with 3 operands (a, b, c) and 1 result, registered in the NVVM dialect.
  • mir-importer: 4 emit_dp* translation functions dispatched from try_dispatch_intrinsic for the cuda_device::dotprod::* call paths.
  • mir-lower: 4 convert_dp* functions lowering NVVM ops to InlineAsmOp with AsmKind::Pure; 4 MirToLlvmConversion trait impls wiring them up.
  • llvm-export: No changes needed, inline asm export path already handles InlineAsmOp.
  • tests: 4 new lowering tests in mir-lower/tests/lowering_test.rs verifying each op produces the expected PTX mnemonic in the inline asm output.

Testing

  • cargo test -p mir-lower --test lowering_test passes (4 new + 9 existing = 13/13)
  • cargo check -p dialect-nvvm -p mir-lower passes
  • cargo fmt --check and cargo clippy --workspace -- -D warnings clean
  • E2E example deferred (dp4a/dp2a requires sm_61+ runtime; 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-dot-products branch from 1950bd6 to 4f65da3 Compare June 22, 2026 19:04
honeyspoon and others added 2 commits June 24, 2026 10:00
Add dp4a (4-element byte dot product) and dp2a (2-element half-word dot
product) intrinsics in signed and unsigned variants, lowered to pure
inline PTX assembly with AsmKind::Pure.

These single-thread, non-convergent instructions perform packed-integer
multiply-accumulate in a single cycle and are the building blocks for
INT8/INT4 quantised inference on sm_61+ GPUs (Pascal and later).

Part of NVIDIA#274

Signed-off-by: abder <[email protected]>
dp4a/dp2a are pure per-thread register arithmetic with no memory or
pointer/aliasing contract, so the four device stubs are now safe
`pub fn`, matching every sibling pure-arithmetic intrinsic (fma_bf16x2,
cvt_f16x2_f32) instead of forcing callers into needless unsafe blocks.
Drop the now-unnecessary `# Safety` doc sections (they would also trip
clippy::unnecessary_safety_doc on a safe fn). Reflow the dialect-nvvm
import list after rebasing onto the TypeHandle-API main.
@nihalpasham
nihalpasham force-pushed the upstream-dot-products branch from 4f65da3 to 38618c0 Compare June 24, 2026 04:39
@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
@nihalpasham
nihalpasham merged commit 3fbe3d1 into NVIDIA:main Jun 24, 2026
24 checks passed
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR. LGTM 👍🏾

  • I'm glad you caught the result signedness: Signed for s32, Unsigned for u32 is what the MirStoreOp verifier needs and copying the always-unsigned bf16x2 helper would have failed the store typecheck.

Rebased onto the TypeHandle-API main and layered a small commit:

  • made the four dp4a/dp2a device stubs safe pub fn (were unsafe). They're pure register ops with no memory/pointer contract, so they now match the safe-fn convention of fma_bf16x2/cvt_f16x2_f32 and drop the needless # Safety docs + unsafe blocks at call sites.

Reviewer note: no GPU e2e in this env, so ptxas acceptance + numerics on sm_61+ still want a follow-up example. Ticked the dp4a/dp2a box in #274; not closing the tracking issue.

honeyspoon added a commit to honeyspoon/cuda-oxide that referenced this pull request Jun 24, 2026
Follow-up to NVIDIA#275, adds the GPU-verified example noted as owed.

Part of NVIDIA#274

Signed-off-by: abder <[email protected]>
honeyspoon added a commit to honeyspoon/cuda-oxide that referenced this pull request Jun 26, 2026
Follow-up to NVIDIA#275, adds the GPU-verified example noted as owed.

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#275, adds the GPU-verified example noted as owed.

Part of NVIDIA#274

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

Follow-up to #275, adds the GPU-verified example noted as owed.

Part of #274

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

* fix: make dot product target explicit

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-dot-products branch June 30, 2026 01:47
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
Retain tensor storage and device access leases through submission completion, independently of borrowed or projected outputs. Forgotten futures leak these owners rather than freeing in-flight memory or permitting conflicting cross-stream accesses. Cover kernels, views, copies, partial errors and panics, and graph replay.

Preserve the f8 nearest-even rounding and i32 Latency generic fixes in this PR. Document the runtime contracts for 0.4.0.

Validation: full CPU and GPU test scripts, nine gated GPU regressions, example builds, formatting, and Clippy with warnings denied. The borrowed-copy and mapped-view lifetime regressions fail on the previous PR head.

Signed-off-by: Melih Elibol <[email protected]>
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
…submissions (NVIDIA#302)

Per launch, each tensor argument cost two heap allocations (an Arc<()>
access id and a Box<dyn Send> owner); access ids are now u64 counters and
leases are stored inline in the submission. Graph replays walked every
recorded context and re-registered every recorded resource; the recorded
resources are now frozen and deduplicated by storage at capture time, and
a replay reacquires each distinct buffer once. Lifetime and cross-stream
conflict semantics are unchanged: every in-flight access is still
registered on its storage and released when the submission completes.

Host-only enqueue cost (RTX 5090, benches/host_overhead):
  tiny 4-tensor launch   1.954 -> 1.884 us (pre-NVIDIA#275: 1.640)
  32-node graph replay  14.68  -> 1.67  us (pre-NVIDIA#275: 1.139)

Signed-off-by: Melih Elibol <[email protected]>
elibol added a commit that referenced this pull request Nov 14, 2026
Transplant of NVlabs/cutile-rs#270
(NVlabs/cutile-rs#270) onto cuda-rust after the
repository move: one commit, original author and sign-off, host-crate
paths following the move to the repository root.

Documentation only. #275 made graph replay sound at runtime: every
storage recorded during capture stays alive while the graph can replay,
each launch re-leases those accesses on its own submission, a replay
that conflicts with an in-flight access on another stream is rejected
before it is enqueued, and a forgotten launch future leaks rather than
frees. The public docs still described only the capture-time safety
argument.

This adds a "Replay safety" section to `CudaGraph`, a matching section
to `Scope`, notes on `take_output`, `launch`, and `scope`, and one
paragraph in the host API reference. No API or code changes; `cargo test
--doc` passes and the new docs build without rustdoc warnings.
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