-
Notifications
You must be signed in to change notification settings - Fork 536
Fix/tied weight export identity #2081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1063,13 +1063,12 @@ def postprocess_state_dict( | |||||||||||||||||||||||||||||||||||||||
| # Check for tied weights and remove duplicates | ||||||||||||||||||||||||||||||||||||||||
| seen_tensors = {} | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| # Remove any tied weights if found. | ||||||||||||||||||||||||||||||||||||||||
| # Remove any tied weights if found. Device and size distinguish independent tensors whose | ||||||||||||||||||||||||||||||||||||||||
| # allocator addresses happen to match. Zero-pointer tensors are left for serialization to reject. | ||||||||||||||||||||||||||||||||||||||||
| for key, value in post_state_dict.items(): | ||||||||||||||||||||||||||||||||||||||||
| if isinstance(value, torch.Tensor): | ||||||||||||||||||||||||||||||||||||||||
| # Use tensor data pointer to identify tied weights | ||||||||||||||||||||||||||||||||||||||||
| tensor_id = value.data_ptr() | ||||||||||||||||||||||||||||||||||||||||
| if isinstance(value, torch.Tensor) and value.data_ptr() != 0: | ||||||||||||||||||||||||||||||||||||||||
| tensor_id = (value.device, value.data_ptr(), value.numel() * value.element_size()) | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1066
to
+1070
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Use complete tensor metadata for deduplication.
Include dtype, shape, and stride, or require exact view metadata before removing a duplicate key. Add a regression for same-start views with different shape or stride. Based on the PR objective, deduplication must prevent false-positive removal without dropping distinct exported tensors. Suggested key- tensor_id = (value.device, value.data_ptr(), value.numel() * value.element_size())
+ tensor_id = (
+ value.device,
+ value.data_ptr(),
+ value.dtype,
+ tuple(value.shape),
+ tuple(value.stride()),
+ )📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need data_ptr still? |
||||||||||||||||||||||||||||||||||||||||
| if tensor_id in seen_tensors: | ||||||||||||||||||||||||||||||||||||||||
| # This is a tied weight, mark for deletion and warn | ||||||||||||||||||||||||||||||||||||||||
| keys_to_delete.append(key) | ||||||||||||||||||||||||||||||||||||||||
| logger.warning( | ||||||||||||||||||||||||||||||||||||||||
| f"Found tied weight: '{key}' is tied to '{seen_tensors[tensor_id]}'. " | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Guard against an unpopulated
weight_formats.ExportContext.__post_init__fillsduplicate_weight_mapandweight_locations, butweight_formatsis filled later, inside_process_quantized_modules(modelopt/torch/export/unified_export_hf.py, Lines 814-816). Any handler invoked with a context that has not passed through_process_quantized_modulestherefore sees a populatedduplicate_weight_mapand an emptyweight_formats. Line 53 then raisesKeyErrorinstead of returningFalse._export_transformers_checkpointalready passesexport_ctxto thePrepareMoEInputsRegistryhandlers at Line 923, beforeweight_formatsexists. Confirm that no handler on that path reaches this helper. The durable fix is to populateweight_formatsin__post_init__next to the other fields, so the context is fully initialized at construction.🤖 Prompt for AI Agents