TRF037

torch.einsum is hard to read; prefer explicit matmul/transpose operations.

   
Default Disabled
Scope Models added on or after 2026-06-20
Source mlinter/trf037.py
Show in terminal mlinter --rule TRF037

What it does

In modeling_*.py and modular_*.py, flags calls to einsum and reports the equation string when it is a literal. Disabled by default: einsum is occasionally the clearest way to express a contraction, so this is opt-in rather than a hard convention.

Why is this bad?

An einsum equation encodes the shapes in a notation the reader has to decode, and when the equation is built dynamically the reader cannot tell which contraction runs at all. Reviewers ask for einsums to be expanded on almost every model that introduces them, which is why it is worth having the check available even though it is not enforced.

Example

-        pair_bias = torch.einsum("bqhc,bkhc->bhqk", query_states, key_states)
+        pair_bias = query_states.permute(0, 2, 1, 3) @ key_states.permute(0, 2, 3, 1)

Enabling this rule

TRF037 is off by default. Turn it on for a run with:

mlinter --enable-rules TRF037

Suppressing this rule

Add a # trf-ignore: TRF037 comment on the flagged line or the line directly above it. See Suppressing rules for whole-file directives and when a suppression is the wrong answer.

Allowlisted models

1 model are exempt from TRF037 in mlinter/rules.toml, because they predate the convention and cannot be changed without breaking backward compatibility.

Show the 1 allowlisted model
  • x_clip