[distributed][tools] Fix MemoryTracker.load not restoring the operation count by ousamabenyounes · Pull Request #192476 · pytorch/pytorch · GitHub
Skip to content

[distributed][tools] Fix MemoryTracker.load not restoring the operation count - #192476

Closed
ousamabenyounes wants to merge 1 commit into
pytorch:mainfrom
ousamabenyounes:fix/issue-191397
Closed

ousamabenyounes wants to merge 1 commit into
pytorch:mainfrom
ousamabenyounes:fix/issue-191397

Conversation

@ousamabenyounes

Copy link
Copy Markdown
Contributor

Fixes #191397

Problem

MemoryTracker.save_stats() did not persist _op_index, and load() did not restore it. After load(), _op_index stayed 0, so summary()/show_traces() (which iterate range(1, self._op_index)) produced no output on a loaded tracker.

Fix

Persist op_index in save_stats() and restore it in load(), with a backward-compatible fallback (stats.get("op_index", len(self.memories_allocated))) for stats files written before this key existed.

Test verification (RED → GREEN)

New test test_save_load_preserves_op_index:

# RED (unmodified main): FAILED test_save_load_preserves_op_index
# GREEN (with fix): 1 passed

Disclosure (per AI_POLICY.md): this change was prepared with AI assistance and was reviewed, tested, and is submitted by me. Happy to iterate on any feedback.

@pytorch-bot

pytorch-bot Bot commented Aug 7, 2026 •

Copy link
Copy Markdown

@pytorch-bot pytorch-bot Bot added ciflow/h100-symm-mem ciflow/torchtitan Run TorchTitan integration tests topic: not user facing topic category labels Aug 7, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 7, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: ousamabenyounes / name: Ben Younes (5d9a5e3)

@pytorch-bot

pytorch-bot Bot commented Aug 7, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@pytorch-bot

pytorch-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/h100-symm-mem
  • ciflow/torchtitan

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@drisspg
drisspg requested a review from d4l3k August 10, 2026 17:50
@drisspg drisspg added triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module module: distributed_tool tools to help distributed training labels Aug 10, 2026

@d4l3k d4l3k 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.

Lgtm

@d4l3k

d4l3k commented Sep 22, 2026

Copy link
Copy Markdown
Member

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 22, 2026
@d4l3k

d4l3k commented Sep 22, 2026

Copy link
Copy Markdown
Member

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 4 checks: BC Lint, docs-build, pull, torchtitan-test / torchtitan-x-pytorch-test / test (torchtitan_models_integration, 1, 1, mt-l-x86aavx2-189-704-a10g-8)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: Command git -C /home/runner/work/pytorch/pytorch merge --squash __pull-request-192476__init__ returned non-zero exit code 1

Auto-merging test/distributed/_tools/test_memory_tracker.py
CONFLICT (content): Merge conflict in test/distributed/_tools/test_memory_tracker.py
Auto-merging torch/distributed/_tools/memory_tracker.py
CONFLICT (content): Merge conflict in torch/distributed/_tools/memory_tracker.py
Squash commit -- not updating HEAD
Automatic merge failed; fix conflicts and then commit the result.
Details for Dev Infra team Raised by workflow job

save_stats() persisted the memory traces but not _op_index, and load()
restored the traces without reconstructing it. summary() and show_traces()
bound their iteration with _op_index, so after a save/load round trip the
loaded tracker reported no operator deltas even though the trace entries were
present.

Persist op_index in save_stats() and restore it in load(), falling back to the
number of recorded operations for stats files written before op_index was
saved. Add a CPU regression test asserting the restored op count and that
summary() still emits the per-operator deltas.

Fixes pytorch#191397
@d4l3k
d4l3k force-pushed the fix/issue-191397 branch from 3991310 to 58c640c Compare October 2, 2026 02:16
@d4l3k

d4l3k commented Oct 2, 2026

Copy link
Copy Markdown
Member

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@d4l3k d4l3k closed this Oct 2, 2026
pytorchmergebot pushed a commit that referenced this pull request Oct 2, 2026
…on count (#192476)

Fixes #191397

### Problem
`MemoryTracker.save_stats()` did not persist `_op_index`, and `load()` did not restore it. After `load()`, `_op_index` stayed `0`, so `summary()`/`show_traces()` (which iterate `range(1, self._op_index)`) produced no output on a loaded tracker.

### Fix
Persist `op_index` in `save_stats()` and restore it in `load()`, with a backward-compatible fallback (`stats.get("op_index", len(self.memories_allocated))`) for stats files written before this key existed.

### Test verification (RED → GREEN)
New test `test_save_load_preserves_op_index`:
```
# RED (unmodified main): FAILED test_save_load_preserves_op_index
# GREEN (with fix): 1 passed
```

> **Disclosure (per [AI_POLICY.md](https://github.com/pytorch/pytorch/blob/main/AI_POLICY.md)):** this change was prepared with AI assistance and was reviewed, tested, and is submitted by me. Happy to iterate on any feedback.

Pull Request resolved: #192476
Approved by: https://github.com/d4l3k
cyyever pushed a commit to BakeLab/pytorch that referenced this pull request Oct 11, 2026
…on count (pytorch#192476)

Fixes pytorch#191397

### Problem
`MemoryTracker.save_stats()` did not persist `_op_index`, and `load()` did not restore it. After `load()`, `_op_index` stayed `0`, so `summary()`/`show_traces()` (which iterate `range(1, self._op_index)`) produced no output on a loaded tracker.

### Fix
Persist `op_index` in `save_stats()` and restore it in `load()`, with a backward-compatible fallback (`stats.get("op_index", len(self.memories_allocated))`) for stats files written before this key existed.

### Test verification (RED → GREEN)
New test `test_save_load_preserves_op_index`:
```
# RED (unmodified main): FAILED test_save_load_preserves_op_index
# GREEN (with fix): 1 passed
```

> **Disclosure (per [AI_POLICY.md](https://github.com/pytorch/pytorch/blob/main/AI_POLICY.md)):** this change was prepared with AI assistance and was reviewed, tested, and is submitted by me. Happy to iterate on any feedback.

Pull Request resolved: pytorch#192476
Approved by: https://github.com/d4l3k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100-symm-mem ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: distributed_tool tools to help distributed training open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Distributed Tools] MemoryTracker load does not restore operation count

5 participants