TRF058

Buffers must be declared as nn.Buffer attributes, not registered with register_buffer().

   
Default Enabled
Scope All models
Source mlinter/trf058.py
Show in terminal mlinter --rule TRF058

What it does

In modeling_*.py and modular_*.py, flags register_buffer("<name>", ...) calls whose buffer name is a string literal, on any receiver (self, or another module such as layer.mamba). A computed name – a variable or f-string, e.g. one buffer per layer inside a loop – has no attribute-assignment equivalent and is exempt.

Why is this bad?

Since torch>=2.5 nn.Buffer registers a buffer through plain attribute assignment, like nn.Parameter. A buffer created by a method call only exists as a side effect of running __init__, so a modular file that wants to tweak one has to redefine the whole __init__. Assigned as an attribute, it can be inherited and overridden on its own.

Example

-        self.register_buffer("inv_freq", inv_freq, persistent=False)
-        self.register_buffer(
-            "position_ids", torch.arange(config.max_position_embeddings).expand((1, -1)), persistent=False
-        )
+        self.inv_freq = nn.Buffer(inv_freq, persistent=False)
+        self.position_ids = nn.Buffer(torch.arange(config.max_position_embeddings).expand((1, -1)), persistent=False)

Suppressing this rule

Add a # trf-ignore: TRF058 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

2 models are exempt from TRF058 in mlinter/rules.toml, because they predate the convention and cannot be changed without breaking backward compatibility.

Show the 2 allowlisted models
  • falcon_h1
  • pp_doclayout_v2