TRF046

forward must not write module attributes; modules are stateless in forward.

   
Default Enabled
Scope All models
Source mlinter/trf046.py
Show in terminal mlinter --rule TRF046

What it does

Flags assignments to self attributes in forward methods in modeling_*.py and modular_*.py.

Why is this bad?

State written during forward breaks batching, torch.compile, and reasoning about the module. Carried state is passed explicitly (cache objects, the generate loop); values that depend only on config or static shapes belong in __init__.

Example

 def forward(self, hidden_states):
-    self.sequence_length = hidden_states.shape[1]
-    embeddings = self.compute_embeddings(self.sequence_length)
+    embeddings = self.compute_embeddings(hidden_states.shape[1])

Suppressing this rule

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

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

Show the 13 allowlisted models
  • clvp
  • codegen
  • ctrl
  • fastspeech2_conformer
  • nllb_moe
  • qwen2_5_omni
  • qwen3_omni_moe
  • recurrent_gemma
  • seamless_m4t
  • switch_transformers
  • trocr
  • udop
  • wav2vec2_conformer