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
clvpcodegenctrlfastspeech2_conformernllb_moeqwen2_5_omniqwen3_omni_moerecurrent_gemmaseamless_m4tswitch_transformerstrocrudopwav2vec2_conformer
