Skip to content

feat(access): derive tile shape from a target transaction width - #545

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
honeyspoon:up2/access-width-planner
Jul 29, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
honeyspoon:up2/access-width-planner

Conversation

@honeyspoon

Copy link
Copy Markdown
Contributor

Transaction width is a consequence of element alignment, so a kernel cannot request it. That makes the useful question the inverted one — given the width I want, what must the tile shape be? This adds the const arithmetic for that direction.

Why

The gemm_sol example already does this derivation by hand, in a comment (crates/rustc-codegen-cuda/examples/gemm_sol/src/main.rs:313):

Each K-group: 128 rows x 8 elements x 2 bytes = 2048 bytes

A 128-bit transaction of f16 is 8 elements, 128 rows move 1024 elements per group, and run against a K = 32 tile that forces M >= 1024 / 32 = 32 — which is how CUTLASS states the constraint for a row-major A operand at 128 threads and 8-element access. Every step is arithmetic on width, element size, and launch shape, so it can be computed rather than re-reasoned per kernel. A test reproduces exactly that derivation, so the module's stated justification cannot drift from its code.

API

const PLAN: AccessPlan = access::plan::<f16>(access::TXN_128, 128).unwrap();
const MIN_M: usize = PLAN.min_extent(32).unwrap();   // 32
Item Purpose
plan::<T>(txn_bytes, threads) elements per thread, per block, required alignment
min_extent(other) smallest extent of one axis given the other
fits_tile / passes_for_tile whether a tile divides into whole block-wide accesses
widest::<T>(tile_elems, threads) the auditing direction — widest whole fit for an existing tile

Everything is const, so a plan can drive a const generic or sit in a const {} assertion and turn a tile shape that cannot reach the intended width into a compile error rather than a silent narrowing.

Scope — what this does not claim

It answers "what shape does this width need", not "will I get this width". Measured on sm_86 with the element type 16-byte aligned throughout:

how the element is used                     LDG          STG
never decomposed                            LDG.E.128    STG.E.128
lanes read, whole value assembled+stored    LDG.E x4     STG.E.128
lanes read, scalar stored                   LDG.E x4     STG.E

So alignment is a necessary condition and, for loads, not a sufficient one: touching individual lanes lets the optimiser split the local apart and load the lanes separately whatever the alignment. Stores have no such condition. A plan therefore describes the shape a width needs, not a width the kernel is guaranteed to get — the data has to move as whole elements as well. The module docs say this, and point at the vectorization example for the alignment half.

Verification

fmt --check, clippy --workspace --all-targets -D warnings, RUSTDOCFLAGS='-D warnings' cargo doc, and cargo test -p cuda-device clean on nightly-2026-04-03.

@honeyspoon
honeyspoon force-pushed the up2/access-width-planner branch from 9348cb2 to 1b0995e Compare July 28, 2026 13:00
@honeyspoon

Copy link
Copy Markdown
Contributor Author

Turned the plan example into a real doctest. It was ```rust,ignore, which meant it was never compile-checked — and it wasn't correct: it named half::f16`, but `half` is not a dependency of this crate and `f16` is the primitive behind `#![feature(f16)]` (as `atomic.rs` uses it).

Since plan is host-computable const arithmetic there is no reason to skip it, so it now runs and asserts the derivation end to end:

test crates/cuda-device/src/access.rs - access::plan (line 122) ... ok

asserting (elems_per_thread, elems_per_block, min_extent(32)) == (8, 1024, 32). cargo test --doc -p cuda-device is green.

@honeyspoon

Copy link
Copy Markdown
Contributor Author

An addition to the "Scope — what this does not claim" section, from inventorying CCCL 13.2.27's headers.

The scope note says a plan describes the shape a width needs, not a width the kernel is guaranteed to get, because touching individual lanes lets the optimiser split the local apart and load the lanes separately. That is still accurate. What I did not know when writing it is that CUB does not treat this as a dead end — it changes the layout instead of fighting the optimiser.

cub/block/block_load.cuh, on BLOCK_LOAD_VECTORIZE:

A striped arrangement of data is read efficiently from memory and then locally transposed into a blocked arrangement.

So there are two layouts, not one: striped for the read (coalesced, and vectorizable precisely because nothing decomposes it there) and blocked for the arithmetic (one thread's items contiguous), bridged by an exchange. The decomposition that defeats a widened load happens after the exchange, on registers that never came from a wide load instruction.

That does not change what this module computes — the arithmetic relating width, element size and launch shape is the same either way, and min_extent/widest are as useful for planning a striped read as a direct one. It does mean the scope note is slightly pessimistic as phrased: the width is not unreachable for decomposing kernels in general, only unreachable through a direct blocked load. Happy to reword that paragraph if you would prefer it state the distinction explicitly rather than leaving it at "the data has to move as whole elements as well".

min_extent(other) only required other to divide elems_per_block, never
that elems_per_thread divides other. That admitted shape-impossible
extents on a strided operand: plan::<f16>(TXN_128, 128) gives 8
elements per thread, yet min_extent(4) returned Some(256) even though
a row of 4 f16 (8 bytes) cannot hold one thread's 16-byte access.
CUTLASS enforces exactly this divisibility (the access width divides
the contiguous extent).

Add the missing check, returning None in the module's own no-silent-
rounding idiom, and reword the doc from the contiguous-tile model (which
would admit lcm-based extents the function refuses, e.g. other=48 at
512 elems/block) to the strided model the function actually implements.

Also carry the contributor's follow-up from PR NVIDIA#512 into the Scope
note: the width is unreachable only through a direct blocked load;
CUB's BLOCK_LOAD_VECTORIZE reaches it by loading striped with wide
loads and exchanging through shared memory into the blocked layout.
That exchange primitive does not exist in cuda-oxide yet, so the note
records the gap as a missing layout change rather than an alignment
dead end.
Give the new access module an in-repo consumer: a const assertion in
the vectorization example derives f32x4_view_copy's shape from
access::plan::<f32>(TXN_128, N) instead of restating it by hand - one
4-element quad per thread, F32x4's 16-byte alignment, and the flat
N * 4 buffer moved in exactly one block-wide pass. A shape drift now
fails at compile time. N is hoisted to module scope so the plan and
the launch share the same thread count.
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks,

Two commits on top:

  • min_extent now also requires the extent to hold a whole per-thread access (the f16/TXN_128 plan blessed a 4-element row that can't fit one 16-byte access; CUTLASS enforces the same divisibility), with the doc reworded to the strided model it implements, and
  • the scope note now carries your own feat(cuda-device): over-aligned element types with a checked slice view #512 follow-up nuance (the width is unreachable only through a direct blocked load).

The vectorization example now derives its shape through access::plan as an in-repo consumer.

Merging.

@nihalpasham nihalpasham added enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings labels Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants