TRF018
_init_weights overrides should call super()._init_weights(module), except modular-file sentinels.
| Default | Enabled |
| Scope | All models |
| Source | mlinter/trf018.py |
| Show in terminal | mlinter --rule TRF018 |
What it does
Checks that every PreTrainedModel subclass overriding _init_weights(self, module, ...) chains up via super()._init_weights(...). Modular files also accept the sentinels PreTrainedModel._init_weights(self, module), PreTrainedModel._init_weights(module) and raise AttributeError(...). For a deliberate full override, suppress with # trf-ignore: TRF018 above the method.
Why is this bad?
The base _init_weights covers the standard module types (Linear, Embedding, LayerNorm, RotaryEmbedding, …). Skipping it leaves every submodule the override misses uninitialized – which passes tests and surfaces much later as a subtle weight-init bug (cf. https://github.com/huggingface/transformers/pull/45597).
Example
from ... import initialization as init
def _init_weights(self, module):
+ super()._init_weights(module)
if isinstance(module, AcmeCustomLayer):
- module.gate.data.zero_()
+ init.zeros_(module.gate)
Suppressing this rule
Add a # trf-ignore: TRF018 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.
