Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/transformers/utils/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ class OutputRecorder:
index: int = 0
layer_name: str | None = None
class_name: str | None = None
layer_index: list[int] | None = None


def check_model_inputs(func=None, *, tie_last_hidden_states=True):
Expand Down Expand Up @@ -935,19 +936,26 @@ def wrapper(self, *args, **kwargs):
collected_outputs = defaultdict(tuple)
monkey_patched_layers = []

def make_capture_wrapper(module, orig_forward, key, index):
def make_capture_wrapper(module, orig_forward, key, spec):
index = spec.index
layer_index = spec.layer_index

def optionally_append(outputs, new_output):
if layer_index is None:
outputs += (new_output,)
else:
outputs += (new_output,) if len(outputs) in layer_index else (None,)
return outputs

@wraps(orig_forward)
def wrapped_forward(*args, **kwargs):
if key == "hidden_states" and len(collected_outputs[key]) == 0:
collected_outputs[key] += (args[0],)
output = orig_forward(*args, **kwargs)
if not isinstance(output, tuple):
collected_outputs[key] += (output,)
collected_outputs[key] = optionally_append(collected_outputs[key], output)
elif output[index] is not None:
if key not in collected_outputs:
collected_outputs[key] = (output[index],)
else:
collected_outputs[key] += (output[index],)
collected_outputs[key] = optionally_append(collected_outputs[key], output[index])
return output

return wrapped_forward
Expand All @@ -964,7 +972,16 @@ def wrapped_forward(*args, **kwargs):
index = 0 if "hidden_states" in key else 1
class_name = None if not isinstance(specs, str) else specs
target_class = specs if not isinstance(specs, str) else None
specs = OutputRecorder(target_class=target_class, index=index, class_name=class_name)
layer_index = (
None
if recordable_keys.get(f"output_{key}") in (True, False)
else recordable_keys.get(f"output_{key}")
)
if isinstance(layer_index, int):
layer_index = [layer_index]
specs = OutputRecorder(
target_class=target_class, index=index, class_name=class_name, layer_index=layer_index
)
capture_tasks.append((key, specs))

for name, module in self.named_modules():
Expand All @@ -977,7 +994,7 @@ def wrapped_forward(*args, **kwargs):
continue
# Monkey patch forward
original_forward = module.forward
module.forward = make_capture_wrapper(module, original_forward, key, specs.index)
module.forward = make_capture_wrapper(module, original_forward, key, specs)
monkey_patched_layers.append((module, original_forward))

try:
Expand Down