﻿# Padding matrix dimensions for tensor kernels: useful work versus issued work

Padding can improve a tensor kernel’s execution shape while adding arithmetic; compare useful output work and include the cost of preparing padded inputs.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/learn-tensor-kernel-padding/
Language: en

A tensor kernel may work most naturally on matrix shapes larger than the logical problem. Padding fills that physical shape while a mask preserves which elements belong to the task. This separates useful mathematical operations from the operations issued by an implementation, a distinction that matters when reading throughput results.

## Logical dimensions define the requested result

The application asks for a matrix operation with particular dimensions. A kernel may round its working dimensions to match an implementation tile. Extra positions belong to the physical representation, not automatically to the requested output. Their input values and masks must preserve the intended mathematical operation. For multiplication, padding the reduction dimension requires values that do not change the accepted sum, while output padding must be excluded from the returned logical matrix.

Primary reference: [CUDA Programming Guide: Writing SIMT Kernels](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/writing-cuda-kernels.html).

## A narrow matrix can pay a large edge cost

Imagine a logical matrix that occupies only a small part of its final tile. The implementation may issue work for many masked positions. A slightly larger logical input can then have almost the same physical execution shape. This explains why useful throughput can change sharply with dimensions without any change in the GPU itself. The example concerns shape efficiency rather than a measured speed claim about a particular model or device.

Primary reference: [CUDA C++ Best Practices Guide](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/).

## Preparation can move the bottleneck

Explicitly constructing a padded buffer consumes memory and copy work. Some kernels handle edges internally and avoid that external preparation, while others benefit from a regular packed layout. Compare the actual paths rather than assuming both receive the same input representation for free. If the packed form is reused across many operations, its preparation has a different amortization boundary from a buffer rebuilt for every request.

Primary reference: [Nsight Compute Profiling Guide](https://docs.nvidia.com/nsight-compute/ProfilingGuide/index.html).

## Report two kinds of arithmetic honestly

A rate computed from logical dimensions describes useful mathematical work. A rate based on the expanded execution shape describes issued work under that definition. Both can be informative when labeled, but they should not be mixed in one comparison. Conversion instructions and initialization also occupy execution resources. Retain the numerical format and accumulation path so a shape optimization is not confused with a precision change that alters the accepted task.

## Test dimensions around a tile boundary

Choose neighboring logical shapes that fall on different sides of a tile boundary. Keep the operation and accepted numerical error fixed, and inspect preparation, physical shape and returned coordinates. This proposed experiment reveals discontinuities caused by tiling. It supports a shape-specific decision about packing or kernel selection without claiming that padding always accelerates matrix operations or that a nominal tensor peak predicts useful throughput.

Related reading: [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/); [Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/).
