CPU, GPU AND RAM

Ragged GPU batches: padding, masks and useful arithmetic

In this article
THE QUICK TAKE

A mask specifies validity without guaranteeing skipped work; profile physical shapes, useful positions, and the kernel's actual treatment of padding.

A ragged batch contains examples of different lengths. Padding makes their storage and kernel shapes regular, while masks describe which positions are meaningful. The distinction matters because a masked position can still occupy memory and sometimes consume arithmetic. Understanding where padding is introduced and where invalid positions are excluded explains why equal token counts can produce different GPU costs.

Logical lengths differ from physical storage

A rectangular allocation can store several sequences at a common physical width while separate metadata records each valid length. The extra positions simplify indexing and can support efficient regular kernels. Their cost depends on the largest shape represented in the batch, not only the sum of useful elements. State both physical dimensions and logical lengths when comparing batches. Reporting useful tokens alone can conceal a large difference in padded work.

Primary reference: CUDA Programming Guide: Writing SIMT Kernels.

A validity mask is a semantic contract

The mask tells an operation which positions must not contribute to the meaningful result. Different operators enforce that contract differently: some avoid selected computation, while others compute regular tiles and exclude invalid contributions later. Inspect the actual implementation rather than assuming multiplication by a mask saves all preceding work. For reductions or normalization, choose the correct neutral or excluded treatment; simply inserting zeros can change the mathematical result.

Primary reference: CUDA C++ Best Practices Guide.

Packing removes padding but adds metadata

A packed representation can place valid elements consecutively with offsets describing example boundaries. This reduces certain unused regions while introducing indexing and boundary-management work. A kernel must avoid mixing neighboring examples when processing a contiguous tile. Compare the complete conversion and computation path with the padded baseline. Packing is attractive when the saved work exceeds its preparation and irregular-access costs, rather than because a compact array always looks more elegant.

Primary reference: Nsight Compute Profiling Guide.

Grouping similar lengths changes wasted work

Grouping examples with similar lengths can reduce padding while preserving regular execution. It also changes scheduling and can delay some examples while a suitable group forms. Evaluate this policy against the application’s latency requirements rather than only offline throughput. Preserve per-example identity when reordering inputs and restoring outputs. A batch with excellent arithmetic utilization is still incorrect if its results are returned in the wrong request order.

Use skewed lengths as a diagnostic case

Test a uniform batch, a strongly skewed batch, and empty or minimal examples using deterministic outputs. Record useful elements, physical allocation, kernel shapes, conversion time, and total completion. This reveals whether the bottleneck is padding, metadata, irregular indexing, or scheduling. Do not infer implementation efficiency from a mask’s existence in a framework graph. The evidence should connect actual executed work to the validity semantics the application requires.

Related reading: A GPU inference benchmark you can trust; Reproducible inference stacks: preserving a GPU pilot’s result.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles