liveBook · Manning

Triton.testing.assert_close

As explained in GPU Programming with Triton

triton.testing.assert_close is a Triton testing utility that compares result and reference tensors within absolute and relative tolerances and raises an error when they differ excessively.

A kernel should pass a correctness check before its timing is trusted. `triton.testing.assert_close` compares a result tensor with a reference and raises an error when their values exceed the allowed absolute and relative tolerances. Tolerance matters because floating-point results can differ slightly when calculations accumulate in a different order. section 2.2.10

import torch
import triton

x = torch.rand(98432, device="cuda", dtype=torch.float32)
y = torch.rand_like(x)

reference = x + y            # trusted PyTorch result
result = add(x, y)           # our Triton kernel from listing 2.1

triton.testing.assert_close(result, reference)
print("Triton output matches the reference within tolerance.")

Example: compare a Triton vector-add result with a PyTorch reference before benchmarking. section 2.2.10

How GPU Programming with Triton covers it

How deep it goes
Defines tensor comparison, error behavior, tolerance arguments, and defaults without detailing implementation internals.
Examples
Compares a Triton vector-add result with the PyTorch result for the same inputs.
What it assumes
Uses result and reference tensors, and explains why floating-point comparisons need not require exact equality.
What you can do after
Apply triton.testing.assert_close to validate tensor results with configurable absolute and relative tolerances.
How it connects
Links PyTorch reference results, floating-point rounding, and GPU kernel benchmarking.

This explainer was generated by Manning's knowledge graph from the text of GPU Programming with Triton. The section links point to the parts of the book it draws on.