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 that overrides _init_weights(self, module, ...) chains the call up via super()._init_weights(...). In modular files, PreTrainedModel._init_weights(self, module) and raise AttributeError(...) are accepted because they are modularization sentinels. If a model intentionally fully overrides initialization, suppress with # trf-ignore: TRF018 on the line above the method.

Why is this bad?

The base _init_weights covers standard module types (Linear, Embedding, LayerNorm, RotaryEmbedding, …). Skipping super()._init_weights(...) silently leaves submodules unhandled by the override uninitialized, which can pass tests and surface much later as subtle weight-init bugs (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.