TRF012
_init_weights must use init primitives, not in-place operations on module weights.
| Default | Enabled |
| Scope | All models |
| Source | mlinter/trf012.py |
| Show in terminal | mlinter --rule TRF012 |
What it does
Checks that _init_weights(self, module) does not use in-place operations (e.g. .normal_(), .zero_()) directly on module weights.
Why is this bad?
We rely on internal flags set on parameters to track whether they need re-initialization. In-place ops bypass this mechanism. Use the init primitives instead.
Example
+from transformers import initialization as init
+
def _init_weights(self, module):
- module.weight.normal_(mean=0.0, std=0.02)
+ init.normal_(module.weight, mean=0.0, std=0.02)
Suppressing this rule
Add a # trf-ignore: TRF012 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.
