Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

[Bug Report] including_permanent=True makes hook cleanup ignore level

Đã đóng Phù hợp với người mới
#1,913 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
75/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, pytorch
Lĩnh vực
backend, testing

Hướng nghiên cứu

Bắt đầu với vòng lặp xóa trong transformer_lens/hook_points.py và so sánh cách nó áp dụng level khi including_permanent là true hoặc false. Kiểm tra hợp đồng reset được ghi lại trong transformer_lens/model_bridge/bridge_core.py và tái hiện các trường hợp forward, backward và HookedRootModule.reset_hooks được mô tả trong issue. Công việc hoàn tất khi thao tác dọn dẹp có lọc theo level chỉ xóa các hook permanent ở level được chọn, còn thao tác dọn dẹp hook permanent không lọc vẫn xóa tất cả hook.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Describe the bug

On current dev, HookPoint.remove_hooks(including_permanent=True, level=2) removes hooks at every context level, not just level 2. I also reproduced this through HookedRootModule.reset_hooks(including_permanent=True, level=2).

This means clearing one context's permanent hooks can silently remove an unrelated intervention that should remain active for the next forward or backward pass.

The shared removal loop checks level only in the including_permanent=False branch. The documented reset contract says that a supplied level limits removal to hooks registered at that level. I would expect including_permanent=True to include permanent hooks within that selection, rather than discard the selection.

Code example

import torch
from transformer_lens.hook_points import HookPoint

point = HookPoint()
point.add_hook(lambda value, hook: value + 10, is_permanent=True, level=1)
point.add_hook(lambda value, hook: value + 1, is_permanent=True, level=2)

print("before:", point(torch.tensor(0.0)).item())
point.remove_hooks(including_permanent=True, level=2)
print("remaining levels:", [handle.context_level for handle in point.fwd_hooks])
print("after:", point(torch.tensor(0.0)).item())

Actual output:

before: 11.0
remaining levels: []
after: 0.0

Expected: remaining levels: [1] and after: 10.0.

System Info

  • macOS arm64, Python 3.13.16, PyTorch 2.14.1.
  • Unmodified source checkout at 177d2cc10ba479f767df9834d93ea624e7eba521 (dev), run through uv with compatible CPU dependencies. This was not a restored frozen development environment.
  • No model weights or GPU execution are needed for the example.

Additional context

Three focused regression cases fail at the unwanted removal: forward HookPoint cleanup, backward HookPoint cleanup, and HookedRootModule.reset_hooks. Fifteen controls pass, including the existing hook-introspection tests and checks that default cleanup preserves permanent hooks, ordinary level-filtered cleanup preserves other contexts, and an unfiltered permanent reset still removes everything.

These Python-only checks used the repository's coverage-style pytest options, retaining jaxtyping/beartype instrumentation. The stock notebook/doctest plugins and complete development environment were unavailable, and the unchanged source's full typecheck was not clean in the available environment. I have not verified a native TransformerBridge model or prepared a ready code patch.

This concerns explicit level-filtered cleanup, rather than the retained-helper-hook lifecycle addressed by #1908.

Checklist
  • I have checked that there is no similar issue in the repository.
Ngôn ngữ chính
Python
Star
3.9k
Fork
708
Merge trung bình
2 ngày 3 giờ
Pull request đã merge (30 ngày)
88

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của TransformerLensOrg/TransformerLens

Tất cả issue của TransformerLensOrg/TransformerLens

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.