Fix TraceEvent.__init__() got an unexpected keyword argument pool_id#15
Closed
HugeEngine wants to merge 1 commit into
Closed
Fix TraceEvent.__init__() got an unexpected keyword argument pool_id#15HugeEngine wants to merge 1 commit into
HugeEngine wants to merge 1 commit into
Conversation
Summary:
## Problem
Newer PyTorch memory snapshots (introduced in D97221533) include a `pool_id` field in trace events. Mosaic's `TraceEvent.from_raw()` classmethod splats the raw dict directly into `TraceEvent(**raw_modified)`, causing a `TypeError` for any unrecognized field:
```
TypeError: TraceEvent.__init__() got an unexpected keyword argument 'pool_id'
```
This breaks all memory snapshot analysis tools (peak_memory_analysis, categorical_profiling, annotation_analysis, memory_diff) when used with snapshots from newer PyTorch versions.
## Root Cause
`TraceEvent.from_raw()` in `fbcode/mosaic/libmosaic/utils/data_utils.py` individually pops known extra fields (e.g., `frames`, `user_metadata`) before splatting the remaining dict into the dataclass constructor. When PyTorch added `pool_id` to snapshot trace events (D97221533), this field leaked through as an unexpected kwarg.
## Fix
Replaced the individual `.pop()` approach (previously used for `user_metadata` in D88310416) with **generic field filtering** — `raw_modified` is now filtered to only include keys that match actual `TraceEvent` dataclass fields using `dataclasses.fields()`. This is future-proof: any new fields PyTorch adds to snapshot trace events will be silently ignored without requiring another code change.
## Changes
1. **Updated import** (line 13): Added `fields as dataclass_fields` to the `dataclasses` import
2. **Generic field filtering** (lines 158-166): Replaced individual `.pop()` calls with `{k: v for k, v in raw_modified.items() if k in valid_fields}` filtering
Reviewed By: basilwong
Differential Revision: D99884123
|
@HugeEngine has exported this pull request. If you are a Meta employee, you can view the originating Diff in D99884123. |
|
This pull request has been merged in 67b27bb. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Problem
Newer PyTorch memory snapshots (introduced in D97221533) include a
pool_idfield in trace events. Mosaic'sTraceEvent.from_raw()classmethod splats the raw dict directly intoTraceEvent(**raw_modified), causing aTypeErrorfor any unrecognized field:This breaks all memory snapshot analysis tools (peak_memory_analysis, categorical_profiling, annotation_analysis, memory_diff) when used with snapshots from newer PyTorch versions.
Root Cause
TraceEvent.from_raw()infbcode/mosaic/libmosaic/utils/data_utils.pyindividually pops known extra fields (e.g.,frames,user_metadata) before splatting the remaining dict into the dataclass constructor. When PyTorch addedpool_idto snapshot trace events (D97221533), this field leaked through as an unexpected kwarg.Fix
Replaced the individual
.pop()approach (previously used foruser_metadatain D88310416) with generic field filtering —raw_modifiedis now filtered to only include keys that match actualTraceEventdataclass fields usingdataclasses.fields(). This is future-proof: any new fields PyTorch adds to snapshot trace events will be silently ignored without requiring another code change.Changes
fields as dataclass_fieldsto thedataclassesimport.pop()calls with{k: v for k, v in raw_modified.items() if k in valid_fields}filteringReviewed By: basilwong
Differential Revision: D99884123