TRF052

No *_ATTENTION_CLASSES dispatch dicts; attention backends route through the attention interface.

   
Default Enabled
Scope All models
Source mlinter/trf052.py
Show in terminal mlinter --rule TRF052

What it does

Checks modeling_*.py and modular_*.py for module-level assignments to names ending in _ATTENTION_CLASSES.

Why is this bad?

Per-backend attention classes selected from a dict are the pre-interface idiom: three near-identical classes drift apart, and hub attention kernels registered into ALL_ATTENTION_FUNCTIONS never reach them. One attention class dispatching through the interface replaces the dict; do not propagate it from a legacy parent.

Example

-ACME_ATTENTION_CLASSES = {
-    "eager": AcmeAttention,
-    "flash_attention_2": AcmeFlashAttention2,
-    "sdpa": AcmeSdpaAttention,
-}
+class AcmeAttention(nn.Module):
+    def forward(self, ...):
+        attention_interface = ALL_ATTENTION_FUNCTIONS.get_interface(self.config._attn_implementation, eager_attention_forward)

Suppressing this rule

Add a # trf-ignore: TRF052 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

8 models are exempt from TRF052 in mlinter/rules.toml, because they predate the convention and cannot be changed without breaking backward compatibility.

Show the 8 allowlisted models
  • bark
  • data2vec
  • falcon
  • git
  • gpt_neo
  • gptj
  • sam
  • superglue