TRF026
A module whose forward only delegates to its single submodule adds nothing; inline it.
| Default | Enabled |
| Scope | Models added on or after 2026-06-20 |
| Source | mlinter/trf026.py |
| Show in terminal | mlinter --rule TRF026 |
What it does
In modeling_*.py and modular_*.py, flags a non-PreTrainedModel class that defines only __init__ and forward, assigns exactly one self.<attr> in __init__, and whose forward body is exactly return self.<attr>(...) for it (a leading docstring is ignored). Any other method, extra attribute, statement before the return, or super() call in forward means the class does work of its own.
Why is this bad?
The wrapper adds a level to every weight name, to _no_split_modules, parallelism plans and every conversion mapping, while computing nothing – and readers have to open one more class to find that out. PreTrainedModel subclasses are exempt: they exist for from_pretrained and the auto classes even when forward only delegates.
Example
-class AcmeAtomTransformer(nn.Module):
- def __init__(self, config):
- super().__init__()
- self.encoder = AcmeEncoder(config)
-
- def forward(self, hidden_states, **kwargs):
- return self.encoder(hidden_states, **kwargs)
-
class AcmeModel(AcmePreTrainedModel):
def __init__(self, config):
super().__init__(config)
- self.atom_transformer = AcmeAtomTransformer(config)
+ self.encoder = AcmeEncoder(config)
Suppressing this rule
Add a # trf-ignore: TRF026 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.
