ldmatrix / stmatrix#
The tile patterns support m8n8.b16 with .x1, .x2, or .x4
and optional .trans. Select the exact form in the opcode:
T.cuda.tile.apply(
T.ptx["ldmatrix.sync.aligned.m8n8.x2.shared.b16"],
registers, shared_words, scope="warp",
)
stmatrix reverses the operand order: shared_words, registers.
The implementation repeats the specified warp instruction, without picking
a matrix count or transpose mode from the layout.
Address providers and result owners#
Both regions end in (matrix,row,word)=(count,8,4) and use scalar 32-bit
containers. A container holds two adjacent 16-bit values. Use a dtype view
to expose packed half/bfloat16 storage as b32 words; this is not a conversion.
Each shared row has four consecutive words and a 16-byte-aligned start. Different rows may have gaps and may be interleaved with other matrices.
Lane
8*matrix+rowprovides that row’s shared address. For x1/x2, the remaining address providers repeat valid row addresses.Register word
(matrix,row,word)belongs to lane4*row+word. Each lane receivescountwords. Their register offsets need not be adjacent, and need not have the same order as another tensor declaration.
Thus, the lane supplying a shared address generally differs from the lanes receiving the values from that row. The contract checks both maps. All 32 lanes must participate, including those whose addresses are duplicates.
Transpose and stores#
.trans transposes each 8x8 matrix of 16-bit elements, not a matrix
of b32 words. Register image element (row,2*word+half) comes from shared
element (2*word+half,row). The register word’s lane owner remains
4*row+word. Reorganize operands with this image in mind; toggling
.trans is a semantic change, not a layout optimization hint.
stmatrix uses the corresponding register image as input and each
provider’s shared row address as output. Its supported architecture is
checked by the PTX compiler. Synchronize before a load consumes another
thread’s shared stores, and after a store before other threads consume or
reuse that shared storage. No fence or barrier is inserted by the pattern.
An insufficiently aligned row, wrong register owner, overlapping loaded registers, or a partially active warp is rejected. Gaps between rows or register slots are legal; gaps within a shared 16-byte row are not.
Implementation: python/tvm/backend/cuda/tile/patterns/ldmatrix.py.