Skip to content

feat(device_buffer): add cast_chunks for a length-adjusting reinterpret - #513

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
honeyspoon:up/cast-chunks
Jul 28, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
honeyspoon:up/cast-chunks

Conversation

@honeyspoon

Copy link
Copy Markdown
Contributor

cast_elem requires the same size and alignment, which is right for the layout-identical case it exists for (u64 → DeviceAtomicU64). It cannot express grouping scalars into an over-aligned vector element, because such a type is by construction a different size and alignment.

That turns out to be the practical blocker on adopting wide memory accesses. Transaction width comes from the element type's alignment, so a kernel wanting LDG.E.128 must take an aligned element — but with no length-adjusting cast, that type has to be agreed by every producer and consumer of a buffer at once. Migrating a single buffer pair in our kernels cost six signature changes plus a dummy buffer, which is why the aligned types went unused despite being measured to work.

cast_chunks moves the decision to the boundary:

// Allocate and fill as f32, the way a producer kernel leaves it.
let quads: DeviceBuffer<F32x4> = floats.cast_chunks()?;

Errors rather than panics

Returns the buffer unchanged when the reinterpretation would not be exact: a byte extent that is not a whole number of A, a pointer not aligned for A, or a zero-sized A. A caller that cannot widen normally has a scalar path, so handing the buffer back keeps it available. It refuses rather than truncates a non-dividing length — dropping a tail belongs at the call site.

On the alignment precondition

cuMemAlloc returns at least 256-byte-aligned memory, which satisfies any vector type, so the check is expected to pass. It is checked rather than assumed because a buffer can also come from from_raw_parts with a hand-computed pointer — exactly the case where being wrong is silent. A test pins that every 256-byte-aligned base satisfies every vector alignment.

Both cast_chunks and can_cast_chunks route through one pure helper, so they cannot disagree and the decision is testable without a device.

Verification

On an RTX A2000: 1024 f32 allocated and filled, viewed as 256 F32x4 with no copy, passed to a kernel taking the aligned element type, data intact on readback. A 3-element buffer correctly refused.

5 tests on the pure helper, clippy --all-targets clean.

Pairs with the aligned types

This is the enabler; the element types it casts to are the companion PR. Either can land alone.

`cast_elem` requires the same size *and* alignment, which is right for the
layout-identical case it exists for (u64 -> DeviceAtomicU64). It cannot express
grouping scalars into an over-aligned vector element, because such a type is by
construction a different size and alignment.

That is the practical blocker on adopting wide memory accesses. Transaction
width comes from the element type's alignment, so a kernel wanting LDG.E.128
must take an aligned element - but with no length-adjusting cast, the element
type has to be agreed by every producer and consumer of a buffer at once.
Migrating a single buffer pair in our kernels cost six signature changes plus a
dummy buffer, which is why the aligned types went unused.

`cast_chunks` moves the decision to the boundary: allocate and fill as f32,
hand the same allocation to a kernel taking F32x4.

    let quads: DeviceBuffer<F32x4> = floats.cast_chunks()?;

Returns the buffer unchanged rather than panicking when the reinterpretation
would not be exact - a byte extent that is not a whole number of A, a pointer
not aligned for A, or a zero-sized A. A caller that cannot widen normally has a
scalar path, so handing the buffer back keeps it available. It refuses rather
than truncates a non-dividing length, since dropping a tail belongs at the call
site.

The alignment check is expected to pass, since cuMemAlloc returns at least
256-byte-aligned memory. It is checked rather than assumed because a buffer can
also come from `from_raw_parts` with a hand-computed pointer, which is exactly
where being wrong would be silent. A test pins that every 256-byte-aligned base
satisfies every vector alignment.

Both the cast and the `can_cast_chunks` predicate route through one pure
helper, so they cannot disagree and the decision is testable without a device.

Verified on an RTX A2000: 1024 f32 allocated and filled, viewed as 256 F32x4
with no copy, passed to a kernel taking the aligned type, data intact on
readback; a 3-element buffer correctly refused.

Signed-off-by: abder <[email protected]>
End-to-end exercise of `DeviceBuffer::cast_chunks`: allocate and fill a
flat `DeviceBuffer<f32>`, reinterpret it at the boundary into a
`#[repr(C, align(16))]` four-float element, launch a kernel taking that
element type, cast the output back to `f32`, and verify the round trip
bit-for-bit. Also pins the refusal path: a 3-float buffer is not a whole
number of quads, so `cast_chunks` must return it unchanged rather than
truncate. Lands in the smoketest standard category, so both paths run in
CI.
@nihalpasham nihalpasham added host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) enhancement New feature or request labels Jul 28, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks, merged together with #512.

I deliberately kept cast_chunks bounds unbounded for parity with cast_elem, and added a cast_chunks example on top that round-trips 1024 f32 through 256 align(16) quads on an RTX 5090 (sm_120) and pins the refusal path for a 3-float buffer.

The five new chunk_cast unit tests, clippy -D warnings, and the GPU smoke are all clean. Closes no open issue.

Merging

@nihalpasham
nihalpasham merged commit 87a0605 into NVIDIA:main Jul 28, 2026
33 checks passed
@honeyspoon
honeyspoon deleted the up/cast-chunks branch July 28, 2026 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants