TRF051

Modeling code must not branch on _attn_implementation; the attention interface owns dispatch.

   
Default Enabled
Scope All models
Source mlinter/trf051.py
Show in terminal mlinter --rule TRF051

What it does

Checks modeling_*.py and modular_*.py for comparisons against a _attn_implementation attribute.

Why is this bad?

Backend dispatch belongs to ALL_ATTENTION_FUNCTIONS.get_interface, and backend-conditional tensor munging (padding, reshaping) belongs in the shared wrappers under integrations/. Inline branching keeps the model body kernel-aware and breaks when new backends register.

Example

-if self.config._attn_implementation == "flash_attention_2":
-    attn_output = flash_path(query_states, key_states, value_states)
-else:
-    attn_output = eager_path(query_states, key_states, value_states)
+attention_interface = ALL_ATTENTION_FUNCTIONS.get_interface(self.config._attn_implementation, eager_attention_forward)
+attn_output, attn_weights = attention_interface(self, query_states, key_states, value_states, ...)

Suppressing this rule

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

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

Show the 9 allowlisted models
  • axk2
  • decision_transformer
  • deepseek_v32
  • diffusion_gemma
  • falcon
  • glm_moe_dsa
  • gpt2
  • minimax_m3_vl
  • qwen2_5_omni