Skip to content

Fixes memory leak in async_save in distributed checkpoint - #170431

Closed
arkadip-maitra wants to merge 4 commits into
pytorch:mainfrom
arkadip-maitra:fix_#170163
Closed

arkadip-maitra wants to merge 4 commits into
pytorch:mainfrom
arkadip-maitra:fix_#170163

Conversation

@arkadip-maitra

@arkadip-maitra arkadip-maitra commented Dec 15, 2025 •

Copy link
Copy Markdown
Collaborator

Fixes #170163

Cause
When async_save is called without a user-provided async_stager, it creates an internal DefaultStager that caches CPU storage copies. This cache wasn't being cleared, and the stager went out of scope without cleanup

Changes
_1. _traverse.py: del _traverse_obj, _is_terminal

_2. state_dict_saver.py: clear _deepcopy_dispatch in close()

Tracked the memory leak and used this changes to fix it

cc @LucasLLC @pradeepfn @H-Huang @awgu @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @pragupta @msaroufim @dcci

@pytorch-bot

pytorch-bot Bot commented Dec 15, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/170431

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 94d5d07 with merge base b71eaf9 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@arkadip-maitra arkadip-maitra added oncall: distributed Add this issue/PR to distributed oncall triage queue topic: not user facing topic category labels Dec 15, 2025
@pytorch-bot pytorch-bot Bot removed the oncall: distributed Add this issue/PR to distributed oncall triage queue label Dec 15, 2025
@pytorch-bot

pytorch-bot Bot commented Dec 15, 2025

Copy link
Copy Markdown

The label oncall: distributed is only applicable to issues and has been removed. Please only use this label on issues.

@arkadip-maitra arkadip-maitra added the oncall: distributed checkpointing Oncall label should be attached to any issues related to distributed checkpointing. label Dec 15, 2025
@pytorch-bot pytorch-bot Bot removed the oncall: distributed checkpointing Oncall label should be attached to any issues related to distributed checkpointing. label Dec 15, 2025
@pytorch-bot

pytorch-bot Bot commented Dec 15, 2025

Copy link
Copy Markdown

The label oncall: distributed checkpointing is only applicable to issues and has been removed. Please only use this label on issues.

Comment thread torch/distributed/checkpoint/staging.py Resolved
@cyyever

cyyever commented Dec 16, 2025 •

Copy link
Copy Markdown
Collaborator

Garbage collection is the default behavior. How much is the typical memory occupied by the closed _state_dict_stager? If there is not significant wast, don't use gc.collect.

@arkadip-maitra

arkadip-maitra commented Dec 16, 2025 •

Copy link
Copy Markdown
Collaborator Author

@Skylion007 @cyyever

Without explicit garbage collection call the pattern of memory accumulation is this:

Cycle	  Steps	   Peak Memory	     GC Triggered
1	         0-5	           10,918 MB	          Step 6 (678 MB)
2	        6-14	   17,062 MB	        Step 15 (678 MB)
3	       15-24	   19,110 MB	        Step 25 (678 MB)

Memory accumulates ~2GB per step
Python's GC eventually kicks in and drops memory back to ~678 MB
Cycle length increases: 6 steps → 9 steps → 10 steps

2025-12-16 05:57:11,736 - INFO - Creating 2GB Dummy Model on cuda:0 ...                                                                                                                                    
2025-12-16 05:57:13,577 - INFO - Model created. Size: 2048.00 MB                                                                                                                                           
2025-12-16 05:57:14,387 - INFO - Starting synchronous save loop...                                                                                                                                         
2025-12-16 05:57:14,387 - INFO - Step 0: Host RSS Memory: 673.67 MB                                                                                                                                        
2025-12-16 05:57:14,387 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_0...                                                                                                        
2025-12-16 05:57:16,621 - INFO - Async checkpoint completed synchronously. Time: 2.23s. Size: 2048.0 MB. Speed: 916.8 MB/s                                                                                 
2025-12-16 05:57:17,622 - INFO - Step 1: Host RSS Memory: 2726.07 MB                                                                                                                                       
2025-12-16 05:57:17,622 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_1...                                                                                                        
2025-12-16 05:57:19,927 - INFO - Async checkpoint completed synchronously. Time: 2.31s. Size: 2048.0 MB. Speed: 888.5 MB/s                                                                                 
2025-12-16 05:57:20,928 - INFO - Step 2: Host RSS Memory: 4774.08 MB                                                                                                                                       
2025-12-16 05:57:20,928 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_2...                                                                                                        
2025-12-16 05:57:23,292 - INFO - Async checkpoint completed synchronously. Time: 2.36s. Size: 2048.0 MB. Speed: 866.5 MB/s                                                                                 
2025-12-16 05:57:24,292 - INFO - Step 3: Host RSS Memory: 6822.09 MB                                                                                                                                       
2025-12-16 05:57:24,292 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_3...                                                                                                        
2025-12-16 05:57:26,687 - INFO - Async checkpoint completed synchronously. Time: 2.39s. Size: 2048.0 MB. Speed: 855.4 MB/s                                                                                 
2025-12-16 05:57:27,687 - INFO - Step 4: Host RSS Memory: 8870.11 MB                                                                                                                                       
2025-12-16 05:57:27,687 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_4...                                                                                                        
2025-12-16 05:57:30,077 - INFO - Async checkpoint completed synchronously. Time: 2.39s. Size: 2048.0 MB. Speed: 857.1 MB/s                                                                                 
2025-12-16 05:57:31,077 - INFO - Step 5: Host RSS Memory: 10918.12 MB                                                                                                                                      
2025-12-16 05:57:31,078 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_5...                                                                                                        
2025-12-16 05:57:33,461 - INFO - Async checkpoint completed synchronously. Time: 2.38s. Size: 2048.0 MB. Speed: 859.3 MB/s                                                                                 
2025-12-16 05:57:34,461 - INFO - Step 6: Host RSS Memory: 678.12 MB                                                                                                                                        
2025-12-16 05:57:34,462 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_6...                                                                                                        
2025-12-16 05:57:36,948 - INFO - Async checkpoint completed synchronously. Time: 2.49s. Size: 2048.0 MB. Speed: 823.5 MB/s                                                                                 
2025-12-16 05:57:37,949 - INFO - Step 7: Host RSS Memory: 2726.12 MB                                                                                                                                       
2025-12-16 05:57:37,949 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_7...                                                                                                        
2025-12-16 05:57:40,415 - INFO - Async checkpoint completed synchronously. Time: 2.47s. Size: 2048.0 MB. Speed: 830.5 MB/s                                                                                 
2025-12-16 05:57:41,416 - INFO - Step 8: Host RSS Memory: 4774.13 MB                                                                                                                                       
2025-12-16 05:57:41,416 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_8...                                                                                                        
2025-12-16 05:57:43,764 - INFO - Async checkpoint completed synchronously. Time: 2.35s. Size: 2048.0 MB. Speed: 872.4 MB/s                                                                                 
2025-12-16 05:57:44,764 - INFO - Step 9: Host RSS Memory: 6822.14 MB                                                                                                                                       
2025-12-16 05:57:44,764 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_9...                                                                                                        
2025-12-16 05:57:47,060 - INFO - Async checkpoint completed synchronously. Time: 2.30s. Size: 2048.0 MB. Speed: 892.1 MB/s                                                                                 
2025-12-16 05:57:48,061 - INFO - Step 10: Host RSS Memory: 8870.14 MB                                                                                                                                      
2025-12-16 05:57:48,061 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_10...                                                                                                       
2025-12-16 05:57:50,465 - INFO - Async checkpoint completed synchronously. Time: 2.40s. Size: 2048.0 MB. Speed: 851.9 MB/s                                                                                 
2025-12-16 05:57:51,465 - INFO - Step 11: Host RSS Memory: 10918.15 MB                                                                                                                                     
2025-12-16 05:57:51,466 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_11...                                                                                                       
2025-12-16 05:57:53,795 - INFO - Async checkpoint completed synchronously. Time: 2.33s. Size: 2048.0 MB. Speed: 879.1 MB/s                                                                                 
2025-12-16 05:57:54,796 - INFO - Step 12: Host RSS Memory: 12966.16 MB                                                                                                                                     
2025-12-16 05:57:54,796 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_12...                                                                                                       
2025-12-16 05:57:57,148 - INFO - Async checkpoint completed synchronously. Time: 2.35s. Size: 2048.0 MB. Speed: 870.6 MB/s 
2025-12-16 05:57:58,149 - INFO - Step 13: Host RSS Memory: 15014.17 MB
2025-12-16 05:57:58,149 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_13...
2025-12-16 05:58:00,572 - INFO - Async checkpoint completed synchronously. Time: 2.42s. Size: 2048.0 MB. Speed: 845.3 MB/s
2025-12-16 05:58:01,572 - INFO - Step 14: Host RSS Memory: 17062.19 MB
2025-12-16 05:58:01,572 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_14...
2025-12-16 05:58:03,921 - INFO - Async checkpoint completed synchronously. Time: 2.35s. Size: 2048.0 MB. Speed: 872.1 MB/s
2025-12-16 05:58:04,921 - INFO - Step 15: Host RSS Memory: 678.16 MB
2025-12-16 05:58:04,921 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_15...
2025-12-16 05:58:07,245 - INFO - Async checkpoint completed synchronously. Time: 2.32s. Size: 2048.0 MB. Speed: 881.4 MB/s
2025-12-16 05:58:08,246 - INFO - Step 16: Host RSS Memory: 2726.16 MB
2025-12-16 05:58:08,246 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_16...
2025-12-16 05:58:10,682 - INFO - Async checkpoint completed synchronously. Time: 2.44s. Size: 2048.0 MB. Speed: 840.6 MB/s
2025-12-16 05:58:11,683 - INFO - Step 17: Host RSS Memory: 4774.17 MB
2025-12-16 05:58:11,683 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_17...
2025-12-16 05:58:13,984 - INFO - Async checkpoint completed synchronously. Time: 2.30s. Size: 2048.0 MB. Speed: 890.1 MB/s
2025-12-16 05:58:14,984 - INFO - Step 18: Host RSS Memory: 6822.18 MB
2025-12-16 05:58:14,985 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_18...
2025-12-16 05:58:17,388 - INFO - Async checkpoint completed synchronously. Time: 2.40s. Size: 2048.0 MB. Speed: 852.2 MB/s
2025-12-16 05:58:18,388 - INFO - Step 19: Host RSS Memory: 8870.18 MB
2025-12-16 05:58:18,388 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_19...
2025-12-16 05:58:20,738 - INFO - Async checkpoint completed synchronously. Time: 2.35s. Size: 2048.0 MB. Speed: 871.8 MB/s
2025-12-16 05:58:21,738 - INFO - Step 20: Host RSS Memory: 10918.20 MB
2025-12-16 05:58:21,738 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_20...
2025-12-16 05:58:24,206 - INFO - Async checkpoint completed synchronously. Time: 2.47s. Size: 2048.0 MB. Speed: 829.9 MB/s
2025-12-16 05:58:25,207 - INFO - Step 21: Host RSS Memory: 12966.20 MB
2025-12-16 05:58:25,207 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_21...
2025-12-16 05:58:27,547 - INFO - Async checkpoint completed synchronously. Time: 2.34s. Size: 2048.0 MB. Speed: 875.3 MB/s
2025-12-16 05:58:28,547 - INFO - Step 22: Host RSS Memory: 15014.20 MB
2025-12-16 05:58:28,548 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_22...
2025-12-16 05:58:30,882 - INFO - Async checkpoint completed synchronously. Time: 2.33s. Size: 2048.0 MB. Speed: 877.4 MB/s
2025-12-16 05:58:31,882 - INFO - Step 23: Host RSS Memory: 17062.21 MB
2025-12-16 05:58:31,882 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_23...
2025-12-16 05:58:34,211 - INFO - Async checkpoint completed synchronously. Time: 2.33s. Size: 2048.0 MB. Speed: 879.7 MB/s
2025-12-16 05:58:35,211 - INFO - Step 24: Host RSS Memory: 19110.21 MB
2025-12-16 05:58:35,211 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_24...
2025-12-16 05:58:37,621 - INFO - Async checkpoint completed synchronously. Time: 2.41s. Size: 2048.0 MB. Speed: 850.0 MB/s
2025-12-16 05:58:38,621 - INFO - Step 25: Host RSS Memory: 678.18 MB
2025-12-16 05:58:38,621 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_25...

cyyever
cyyever previously approved these changes Dec 16, 2025
@arkadip-maitra

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Dec 16, 2025
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: Approvers from one of the following sets are needed:

  • Distributed (wconstab, mrshenli, pritamdamania87, zhaojuanmao, rohan-varma, ...)
  • superuser (pytorch/metamates)
  • Core Reviewers (mruberry, lezcano, Skylion007, ngimel, peterbell10, ...)
  • Core Maintainers (soumith, gchanan, ezyang, dzhulgakov, malfet, ...)
Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@Skylion007

Copy link
Copy Markdown
Collaborator

@Skylion007 @cyyever

Without explicit garbage collection call the pattern of memory accumulation is this:


Cycle	  Steps	   Peak Memory	     GC Triggered

1	         0-5	           10,918 MB	          Step 6 (678 MB)

2	        6-14	   17,062 MB	        Step 15 (678 MB)

3	       15-24	   19,110 MB	        Step 25 (678 MB)

Memory accumulates ~2GB per step

Python's GC eventually kicks in and drops memory back to ~678 MB

Cycle length increases: 6 steps → 9 steps → 10 steps


2025-12-16 05:57:11,736 - INFO - Creating 2GB Dummy Model on cuda:0 ...                                                                                                                                    

2025-12-16 05:57:13,577 - INFO - Model created. Size: 2048.00 MB                                                                                                                                           

2025-12-16 05:57:14,387 - INFO - Starting synchronous save loop...                                                                                                                                         

2025-12-16 05:57:14,387 - INFO - Step 0: Host RSS Memory: 673.67 MB                                                                                                                                        

2025-12-16 05:57:14,387 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_0...                                                                                                        

2025-12-16 05:57:16,621 - INFO - Async checkpoint completed synchronously. Time: 2.23s. Size: 2048.0 MB. Speed: 916.8 MB/s                                                                                 

2025-12-16 05:57:17,622 - INFO - Step 1: Host RSS Memory: 2726.07 MB                                                                                                                                       

2025-12-16 05:57:17,622 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_1...                                                                                                        

2025-12-16 05:57:19,927 - INFO - Async checkpoint completed synchronously. Time: 2.31s. Size: 2048.0 MB. Speed: 888.5 MB/s                                                                                 

2025-12-16 05:57:20,928 - INFO - Step 2: Host RSS Memory: 4774.08 MB                                                                                                                                       

2025-12-16 05:57:20,928 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_2...                                                                                                        

2025-12-16 05:57:23,292 - INFO - Async checkpoint completed synchronously. Time: 2.36s. Size: 2048.0 MB. Speed: 866.5 MB/s                                                                                 

2025-12-16 05:57:24,292 - INFO - Step 3: Host RSS Memory: 6822.09 MB                                                                                                                                       

2025-12-16 05:57:24,292 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_3...                                                                                                        

2025-12-16 05:57:26,687 - INFO - Async checkpoint completed synchronously. Time: 2.39s. Size: 2048.0 MB. Speed: 855.4 MB/s                                                                                 

2025-12-16 05:57:27,687 - INFO - Step 4: Host RSS Memory: 8870.11 MB                                                                                                                                       

2025-12-16 05:57:27,687 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_4...                                                                                                        

2025-12-16 05:57:30,077 - INFO - Async checkpoint completed synchronously. Time: 2.39s. Size: 2048.0 MB. Speed: 857.1 MB/s                                                                                 

2025-12-16 05:57:31,077 - INFO - Step 5: Host RSS Memory: 10918.12 MB                                                                                                                                      

2025-12-16 05:57:31,078 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_5...                                                                                                        

2025-12-16 05:57:33,461 - INFO - Async checkpoint completed synchronously. Time: 2.38s. Size: 2048.0 MB. Speed: 859.3 MB/s                                                                                 

2025-12-16 05:57:34,461 - INFO - Step 6: Host RSS Memory: 678.12 MB                                                                                                                                        

2025-12-16 05:57:34,462 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_6...                                                                                                        

2025-12-16 05:57:36,948 - INFO - Async checkpoint completed synchronously. Time: 2.49s. Size: 2048.0 MB. Speed: 823.5 MB/s                                                                                 

2025-12-16 05:57:37,949 - INFO - Step 7: Host RSS Memory: 2726.12 MB                                                                                                                                       

2025-12-16 05:57:37,949 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_7...                                                                                                        

2025-12-16 05:57:40,415 - INFO - Async checkpoint completed synchronously. Time: 2.47s. Size: 2048.0 MB. Speed: 830.5 MB/s                                                                                 

2025-12-16 05:57:41,416 - INFO - Step 8: Host RSS Memory: 4774.13 MB                                                                                                                                       

2025-12-16 05:57:41,416 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_8...                                                                                                        

2025-12-16 05:57:43,764 - INFO - Async checkpoint completed synchronously. Time: 2.35s. Size: 2048.0 MB. Speed: 872.4 MB/s                                                                                 

2025-12-16 05:57:44,764 - INFO - Step 9: Host RSS Memory: 6822.14 MB                                                                                                                                       

2025-12-16 05:57:44,764 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_9...                                                                                                        

2025-12-16 05:57:47,060 - INFO - Async checkpoint completed synchronously. Time: 2.30s. Size: 2048.0 MB. Speed: 892.1 MB/s                                                                                 

2025-12-16 05:57:48,061 - INFO - Step 10: Host RSS Memory: 8870.14 MB                                                                                                                                      

2025-12-16 05:57:48,061 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_10...                                                                                                       

2025-12-16 05:57:50,465 - INFO - Async checkpoint completed synchronously. Time: 2.40s. Size: 2048.0 MB. Speed: 851.9 MB/s                                                                                 

2025-12-16 05:57:51,465 - INFO - Step 11: Host RSS Memory: 10918.15 MB                                                                                                                                     

2025-12-16 05:57:51,466 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_11...                                                                                                       

2025-12-16 05:57:53,795 - INFO - Async checkpoint completed synchronously. Time: 2.33s. Size: 2048.0 MB. Speed: 879.1 MB/s                                                                                 

2025-12-16 05:57:54,796 - INFO - Step 12: Host RSS Memory: 12966.16 MB                                                                                                                                     

2025-12-16 05:57:54,796 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_12...                                                                                                       

2025-12-16 05:57:57,148 - INFO - Async checkpoint completed synchronously. Time: 2.35s. Size: 2048.0 MB. Speed: 870.6 MB/s 

2025-12-16 05:57:58,149 - INFO - Step 13: Host RSS Memory: 15014.17 MB

2025-12-16 05:57:58,149 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_13...

2025-12-16 05:58:00,572 - INFO - Async checkpoint completed synchronously. Time: 2.42s. Size: 2048.0 MB. Speed: 845.3 MB/s

2025-12-16 05:58:01,572 - INFO - Step 14: Host RSS Memory: 17062.19 MB

2025-12-16 05:58:01,572 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_14...

2025-12-16 05:58:03,921 - INFO - Async checkpoint completed synchronously. Time: 2.35s. Size: 2048.0 MB. Speed: 872.1 MB/s

2025-12-16 05:58:04,921 - INFO - Step 15: Host RSS Memory: 678.16 MB

2025-12-16 05:58:04,921 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_15...

2025-12-16 05:58:07,245 - INFO - Async checkpoint completed synchronously. Time: 2.32s. Size: 2048.0 MB. Speed: 881.4 MB/s

2025-12-16 05:58:08,246 - INFO - Step 16: Host RSS Memory: 2726.16 MB

2025-12-16 05:58:08,246 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_16...

2025-12-16 05:58:10,682 - INFO - Async checkpoint completed synchronously. Time: 2.44s. Size: 2048.0 MB. Speed: 840.6 MB/s

2025-12-16 05:58:11,683 - INFO - Step 17: Host RSS Memory: 4774.17 MB

2025-12-16 05:58:11,683 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_17...

2025-12-16 05:58:13,984 - INFO - Async checkpoint completed synchronously. Time: 2.30s. Size: 2048.0 MB. Speed: 890.1 MB/s

2025-12-16 05:58:14,984 - INFO - Step 18: Host RSS Memory: 6822.18 MB

2025-12-16 05:58:14,985 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_18...

2025-12-16 05:58:17,388 - INFO - Async checkpoint completed synchronously. Time: 2.40s. Size: 2048.0 MB. Speed: 852.2 MB/s

2025-12-16 05:58:18,388 - INFO - Step 19: Host RSS Memory: 8870.18 MB

2025-12-16 05:58:18,388 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_19...

2025-12-16 05:58:20,738 - INFO - Async checkpoint completed synchronously. Time: 2.35s. Size: 2048.0 MB. Speed: 871.8 MB/s

2025-12-16 05:58:21,738 - INFO - Step 20: Host RSS Memory: 10918.20 MB

2025-12-16 05:58:21,738 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_20...

2025-12-16 05:58:24,206 - INFO - Async checkpoint completed synchronously. Time: 2.47s. Size: 2048.0 MB. Speed: 829.9 MB/s

2025-12-16 05:58:25,207 - INFO - Step 21: Host RSS Memory: 12966.20 MB

2025-12-16 05:58:25,207 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_21...

2025-12-16 05:58:27,547 - INFO - Async checkpoint completed synchronously. Time: 2.34s. Size: 2048.0 MB. Speed: 875.3 MB/s

2025-12-16 05:58:28,547 - INFO - Step 22: Host RSS Memory: 15014.20 MB

2025-12-16 05:58:28,548 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_22...

2025-12-16 05:58:30,882 - INFO - Async checkpoint completed synchronously. Time: 2.33s. Size: 2048.0 MB. Speed: 877.4 MB/s

2025-12-16 05:58:31,882 - INFO - Step 23: Host RSS Memory: 17062.21 MB

2025-12-16 05:58:31,882 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_23...

2025-12-16 05:58:34,211 - INFO - Async checkpoint completed synchronously. Time: 2.33s. Size: 2048.0 MB. Speed: 879.7 MB/s

2025-12-16 05:58:35,211 - INFO - Step 24: Host RSS Memory: 19110.21 MB

2025-12-16 05:58:35,211 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_24...

2025-12-16 05:58:37,621 - INFO - Async checkpoint completed synchronously. Time: 2.41s. Size: 2048.0 MB. Speed: 850.0 MB/s

2025-12-16 05:58:38,621 - INFO - Step 25: Host RSS Memory: 678.18 MB

2025-12-16 05:58:38,621 - INFO - Starting async_save (will wait) for ./temp_dcp_sync_test/step_25...

Okay, the real bug is with this PR then: #169613

@Skylion007

Copy link
Copy Markdown
Collaborator

FYI: @ankitageorge for a proper GC fix

@arkadip-maitra

Copy link
Copy Markdown
Collaborator Author

@Skylion007 can you approve pls. seems like this change is not related to #169613

@LucasLLC LucasLLC left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tysm for looking into this! Some qq's if you don't mind

staging_future_or_state_dict = stage_state_dict()

if _internally_created_stager:
async_stager.close()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

curious -- why not always call async_stager.close here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

if async_stager is user provided it can be used across checkpoints.
Looking at the docstring explaining close function i think its better to move this close call at the end since "After calling close(), the stager should not be used for further staging operations"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased fix_#170163 onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout fix_#170163 && git pull --rebase)

@arkadip-maitra

Copy link
Copy Markdown
Collaborator Author

@cyyever @Skylion007 @daulet-askarov
Can you check again pls. Removed gc.close()

LucasLLC
LucasLLC previously approved these changes Feb 3, 2026

@LucasLLC LucasLLC left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for fix!

@daulet-askarov daulet-askarov reopened this Feb 3, 2026
@pytorch-bot
pytorch-bot Bot dismissed stale reviews from cyyever and LucasLLC February 3, 2026 18:45

This PR was reopened (likely due to being reverted), so your approval was removed. Please request another review.

@daulet-askarov daulet-askarov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for getting to the bottom of this reference cycle and fixing it!

@arkadip-maitra

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Feb 4, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

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

radeksm pushed a commit to radeksm/pytorch that referenced this pull request Feb 20, 2026
…0431)

Fixes pytorch#170163

**Cause**
When `async_save` is called without a user-provided `async_stager`, it creates an internal `DefaultStager` that caches CPU storage copies. This cache wasn't being cleared, and the stager went out of scope without cleanup

**Changes**
_1. _traverse.py: del _traverse_obj, _is_terminal

_2. state_dict_saver.py: clear _deepcopy_dispatch in close()

Tracked the memory leak and used this changes to fix it

Pull Request resolved: pytorch#170431
Approved by: https://github.com/LucasLLC, https://github.com/daulet-askarov, https://github.com/Skylion007
libohao1201 pushed a commit to libohao1201/pytorch that referenced this pull request Mar 2, 2026
…0431)

Fixes pytorch#170163

**Cause**
When `async_save` is called without a user-provided `async_stager`, it creates an internal `DefaultStager` that caches CPU storage copies. This cache wasn't being cleared, and the stager went out of scope without cleanup

**Changes**
_1. _traverse.py: del _traverse_obj, _is_terminal

_2. state_dict_saver.py: clear _deepcopy_dispatch in close()

Tracked the memory leak and used this changes to fix it

Pull Request resolved: pytorch#170431
Approved by: https://github.com/LucasLLC, https://github.com/daulet-askarov, https://github.com/Skylion007
pytorchmergebot pushed a commit that referenced this pull request Jun 2, 2026
Fixes #183413

DefaultStager stopped being reusable because DefaultStager._stage() closed its internal StateDictStager after every stage, because of my earlier pr [#170431](#170431) that fixed issue #170163

This fixes that by keeping user provided stagers alive across saves and moving cleanup of internally created default stagers into `async_save()`.  Added test to verify re-usability of `DefaultStager` and verified to regression.
Pull Request resolved: #183424
Approved by: https://github.com/fegin
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Fixes pytorch#183413

DefaultStager stopped being reusable because DefaultStager._stage() closed its internal StateDictStager after every stage, because of my earlier pr [pytorch#170431](pytorch#170431) that fixed issue pytorch#170163

This fixes that by keeping user provided stagers alive across saves and moving cleanup of internally created default stagers into `async_save()`.  Added test to verify re-usability of `DefaultStager` and verified to regression.
Pull Request resolved: pytorch#183424
Approved by: https://github.com/fegin
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Fixes pytorch#183413

DefaultStager stopped being reusable because DefaultStager._stage() closed its internal StateDictStager after every stage, because of my earlier pr [pytorch#170431](pytorch#170431) that fixed issue pytorch#170163

This fixes that by keeping user provided stagers alive across saves and moving cleanup of internally created default stagers into `async_save()`.  Added test to verify re-usability of `DefaultStager` and verified to regression.
Pull Request resolved: pytorch#183424
Approved by: https://github.com/fegin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[dcp]Memory Leak in torch.distributed.checkpoint.async_save with Nightly 2.10.0

8 participants