register_forward_hook()
register_full_backward_hook()
register_forward_pre_hook()
Forward (post)
from transformers import AutoModel model = AutoModel.from_pretrained("model-name") def get_activations(module, input, output): print(output) hook = model.transformer.layer[-1].register_forward_hook(get_activations) outputs = model(**inputs) hook.remove()
Forward pre
def __call__(self, module: nn.Module, inputs: tuple[Tensor]) -> Tensor: residual: Tensor = inputs[0]

Seonglae Cho