TRF067

A sub-model call must not switch on every hidden state when the caller only reads some of them by index.

   
Default Enabled
Scope All models
Source mlinter/trf067.py
Show in terminal mlinter --rule TRF067

What it does

In modeling_*.py and modular_*.py, flags a call that switches on every hidden state (output_hidden_states=True, or setting it in **kwargs) when the function then only reads .hidden_states[...] by index. Code that uses the whole tuple (iterating, slicing, stacking or returning it) and @filter_output_hidden_states backbones are exempt.

Why is this bad?

Every layer’s hidden state stays alive until the call returns, so peak memory grows with depth. output_hidden_states also accepts an index or a list of indices and captures only those: decorate a method that forwards **kwargs with @select_hidden_states, or pass the index directly.

Example

+from ...utils.output_capturing import select_hidden_states
 ...
+    @select_hidden_states("vision_feature_layer")
     def get_image_features(self, pixel_values, vision_feature_layer=None, **kwargs):
-        kwargs["output_hidden_states"] = True
         image_outputs = self.vision_tower(pixel_values, return_dict=True, **kwargs)
         selected_image_feature = image_outputs.hidden_states[vision_feature_layer]
 ...
-        outputs = self.img_processor(img_embeds, output_hidden_states=True)
+        outputs = self.img_processor(img_embeds, output_hidden_states=self.layer_idx)
         return outputs.hidden_states[self.layer_idx]

Suppressing this rule

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