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, reporting the equation 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 a dynamically built equation hides which contraction runs at all. Expand it into explicit matmul/transpose operations.

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