﻿# Which gradient layout does the merged GGML TRI backward change cover?

Validate the triangular gradient and input layout against the final merged revision on the backend you intend to run.

Author: Retrivium team
Updated: 2026-11-10
Canonical URL: https://retrivium.ai/journal/news-llamacpp-ggml-tri-backward-final-merge-contract/
Language: en

The llama.cpp b12228 pre-release adds a backward pass for the triangular mask operation in GGML. The review thread shows a later simplification that removed a transpose fixture and passed the gradient directly to TRI. Readers should distinguish what appeared in an intermediate patch from what the merged code and tests cover.

## Derive the expected gradient

The triangular mask is linear. Backpropagation applies the same mask to the incoming gradient, retaining contributions inside the selected region and zeroing the others. Build a small matrix whose entries differ so an incorrect upper versus lower choice or diagonal convention changes the expected answer in a visible way.

## Read the final patch as well as the summary

The initial proposal handled a non-contiguous gradient from a transpose and included a test for it. The later review commit says it removes that test and passes the gradient directly to TRI. Use the final merged code and existing contiguous F32 cases as evidence of what was actually tested.

## Exercise strides in your own graph

If your training graph introduces a view, inspect the incoming layout at the TRI node. Validate a contiguous reference before trying strided gradients, then test the exact expression your application uses. Report an unsupported layout separately from a numerical mismatch. A graph can be mathematically valid yet fail an implementation precondition.

## Qualify the backend explicitly

The contributor’s report describes CPU validation. Test forward and backward paths on every backend you intend to deploy and compare against the reference gradient. Record backend and revision with any unsupported operation. Inference-only graphs need a different test, because a backward pass that never runs cannot change their acceptance result.

## Save the acceptance fixture

Record layout, types, triangle mode and expected values. Keep the exact runtime build in the report. This turns a terse release item into a test that can be replayed after changes to graph construction or the backend used for training.

## Primary sources

- [llama.cpp b12228](https://github.com/ggml-org/llama.cpp/releases/tag/b12228)
- [Upstream implementation and review](https://github.com/ggml-org/llama.cpp/pull/30712)

## Related reading

- [Prefill and decode: the work your GPU performs during inference](/journal/llm-prefill-decode/)
- [Apple Silicon training: what gradient aggregation changes](/journal/apple-silicon-kpop-mlx-gradient-aggregation-ring/)
