﻿# Triton histograms: a safe zero-padded load still adds observations to the zero bin

Distinguish masked memory safety from histogram observation validity, preserving real zero labels while excluding padded lanes from local categorical counts.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-triton-histogram-validity-mask-zero-bin/
Language: en

## Distinguish memory access from counting

The [Triton load API](https://triton-lang.org/main/python-api/generated/triton.language.load.html) lets a masked-out lane return a supplied fallback without reading the corresponding address. That protects memory access. It does not automatically remove the fallback value from subsequent computations. A histogram makes this distinction particularly visible because a fallback can look exactly like a valid category and contribute a real count unless the aggregation receives its own validity information.

## Read the categorical counting contract

The [histogram API](https://triton-lang.org/main/python-api/generated/triton.language.histogram.html) uses unit-width bins starting at zero and exposes a mask that excludes selected inputs. The [language source](https://raw.githubusercontent.com/triton-lang/triton/main/python/triton/language/core.py) forwards that mask to the histogram operation. Keep memory validity and observation validity associated through the kernel. A protected load followed by an unmasked histogram is a different computation from counting only the original valid observations.

## Preserve genuine zero labels

Suppose a partially filled tile loads invalid lanes with zero. Counting all loaded lanes inflates the zero bin. Filtering every zero afterward is also wrong when the source data contains legitimate zero-valued labels. The needed distinction is provenance: which positions came from real input, not which numeric value they happen to contain. Carry the position-derived validity mask instead of reconstructing it from the category values after loading.

## Test the conservation property

Build a small categorical fixture containing valid zero labels and a final partial tile. Compare the complete histogram with a straightforward reference and check that total counts equal the number of included observations. This detects padding bias even when the nonzero bins look correct. If the application also excludes categories outside its supported domain, define that filtering policy explicitly rather than relying on undocumented behavior for invalid bin indices or treating every loaded integer as a supported label.

## Evaluate local and merged counts

Record bin configuration, inclusion policy, load fallback, per-tile validity, and how local histograms are combined. Guides to [tensor axes](/journal/tensor-axes-batch-sequence-hidden/) and [GPU acceptance evidence](/journal/gpu-acceptance-evidence/) support the audit. Check both partial-tile counts and the final aggregate before benchmarking. A histogram kernel is correct when it counts the intended observations exactly, because safe memory reads and numerically plausible bins do not by themselves establish that padding stopped being data.
