Skip to content

Fix interval merge bug in _find_disjoint that breaks weight tying - #47814

Closed
sharmax-vikas wants to merge 1 commit into
huggingface:mainfrom
sharmax-vikas:fix/find-disjoint-interval-merge
Closed

sharmax-vikas wants to merge 1 commit into
huggingface:mainfrom
sharmax-vikas:fix/find-disjoint-interval-merge

Conversation

@sharmax-vikas

@sharmax-vikas sharmax-vikas commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

CI

Summary

  • Fixed a bug in _find_disjoint() (src/transformers/modeling_utils.py, line 406) where last_stop = stop should be last_stop = max(last_stop, stop)
  • When a short tensor is fully contained inside a larger tensor in shared storage, last_stop shrinks to the smaller tensor's end pointer, causing subsequent overlapping tensors to be incorrectly classified as disjoint
  • This leads to shared weights being .clone()'d and saved as independent tensors, silently breaking weight tying after save_pretrained -> from_pretrained round-trip

Example

Tensor A: [0 ------------------- 100]   (large tensor)
Tensor B:    [10 --- 50]                 (contained in A)
Tensor C:            [60 --- 90]         (contained in A)

After processing B, last_stop drops from 100 to 50. Then C's start=60 >= 50 is true, so C is incorrectly put in a new disjoint group -- even though it overlaps with A.

Test plan

  • Verify existing _find_disjoint tests still pass
  • Test with 3+ tensors sharing storage where one is a slice fully contained inside another
  • Confirm save_pretrained -> from_pretrained round-trip preserves weight tying in this scenario

…n save/reload

When a short tensor is fully contained inside a larger one in shared storage,
`last_stop` shrinks to the smaller tensor's end pointer, causing subsequent
overlapping tensors to be incorrectly classified as disjoint. This leads to
shared weights being cloned and saved as independent tensors, silently breaking
weight tying after reload.
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 31093860016:2
Result: success | Jobs: 16 | Tests: 180,283 | Failures: 0 | Duration: 15h 34m

@sharmax-vikas

Copy link
Copy Markdown
Contributor Author

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! Can you give a real example on how you caught this bug ?

@sharmax-vikas

Copy link
Copy Markdown
Contributor Author

Investigated this properly — my original claim was wrong. There is no save/reload corruption: triggering the mis-classification requires strict containment, which guarantees the group is never "identical" in _find_identical, so remove_tied_weights_from_state_dict raises the same RuntimeError either way. I brute-forced all 3- and 4-interval configurations (54k cases) and the raise-vs-succeed outcome never differs.

The invariant itself is still wrong though (last_stop should be a running max), and fixing it makes the error message report the complete overlap set instead of a truncated one. Rewrote it with accurate scoping in #47860 — feel free to close that too if a one-line fix without a user-visible repro isn't worth the churn. Thanks for pushing back on the original.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants