﻿# Triton grouped program IDs: the final short group needs its actual height

Inspect Triton grouped matrix tile ordering, final-group height, and complete output coverage before changing program-ID arithmetic to improve cache reuse.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-triton-grouped-program-id-short-final-group/
Language: en

## Separate launch identity from tile coordinates

The [program_id interface](https://triton-lang.org/main/python-api/generated/triton.language.program_id.html) identifies a program instance in the launch grid. A matrix kernel can transform that identifier into output tile coordinates in different orders. This transformation changes locality without changing the mathematical product only if it still covers every intended output tile exactly as required. A cache-oriented schedule is therefore also an indexing contract, not merely a performance annotation attached to otherwise unchanged coordinates.

## Read the grouped mapping

The [matrix multiplication tutorial](https://triton-lang.org/main/getting-started/tutorials/03-matrix-multiplication.html) groups tile rows and traverses columns within each group to encourage operand reuse. It computes the first row of the group and the actual number of rows remaining there. That latter quantity can be smaller than the configured preferred group height. Use it in the within-group coordinate mapping rather than assuming that the last group has the same shape as earlier groups.

## Inspect the reusable implementation

The [standard library source](https://raw.githubusercontent.com/triton-lang/triton/main/python/triton/language/standard.py) contains `swizzle2d`, including adjustment for a final group with fewer rows. This supports a concrete review method: compare a proposed custom mapping with the intended permutation on a small tile grid. Keep launch count, tile-grid dimensions, and group height in the same coordinate system. Matrix element dimensions and tile counts are related, but substituting one for another in the permutation changes the schedule's domain.

## Test the case divisible examples miss

Imagine all initial benchmarks use a tile-row count divisible by the group height. A simplified mapping can pass those tests and fail when another matrix leaves a partial final group. Enumerate program IDs, collect output coordinates, and check both coverage and duplicates before running arithmetic. This detects schedule mistakes independently from floating-point tolerance. Then compare complete matrix results, including edge stores, because correct tile permutation does not itself validate every boundary mask or reduction load.

## Measure locality after correctness

Record tile dimensions, launch grid, grouping parameter, final-group calculation, and tested shapes. Guides to [tensor axes](/journal/tensor-axes-batch-sequence-hidden/) and [GPU acceptance evidence](/journal/gpu-acceptance-evidence/) provide context. Include partial groups when validating an optimized kernel. Improved cache reuse is hardware- and workload-dependent; the invariant is the intended output coverage. A published tutorial speedup should not replace a measurement of the actual kernel after its program-ID permutation has been verified.
