Repository navigation
feat(device_buffer): add cast_chunks for a length-adjusting reinterpret - #513
Merged
Merged
Conversation
`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]>
This was referenced Jul 27, 2026
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.
Collaborator
|
Thanks, merged together with #512. I deliberately kept The five new Merging |
nihalpasham
approved these changes
Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
cast_elemrequires 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.128must 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_chunksmoves the decision to the boundary: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 forA, or a zero-sizedA. 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
cuMemAllocreturns 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 fromfrom_raw_partswith 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_chunksandcan_cast_chunksroute through one pure helper, so they cannot disagree and the decision is testable without a device.Verification
On an RTX A2000: 1024
f32allocated and filled, viewed as 256F32x4with 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-targetsclean.Pairs with the aligned types
This is the enabler; the element types it casts to are the companion PR. Either can land alone.