pytorch - ✅(Solved) Fix Optimization: pin memory management optimization based on pre-allocation and buddy system [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#181094Fetched 2026-04-23 07:22:42
View on GitHub
Comments
0
Participants
1
Timeline
22
Reactions
0
Author
Participants
Timeline (top)
mentioned ×6subscribed ×6labeled ×5closed ×2

PR fix notes

PR #181217: pin memory management optimization

Description (problem / solution / changelog)

We propose a pin memory management mechanism based on warm-up and buddy system to resolve the cold-start performance problem during the early stages of model training. For detailed instructions, please refer to #181094

Changed files

  • aten/src/ATen/core/CachingHostAllocator.h (modified, +562/-17)

Code Example

export BUDDY_BLOCK_SIZE_MB=512

---

export BUDDY_BLOCK_COUNT=16

---

export BUDDY_THRESHOLD_MB=2
RAW_BUFFERClick to expand / collapse

🚀 The feature, motivation and pitch

motivation

The pin memory allocation module adopts a cold-up strategy, with an initially empty cache. During the first few training iterations of the model, frequent requests for memory resource allocation are often required, leading to prolonged training time and reduced training efficiency.

solution

To address this issue, we propose a pin memory management mechanism based on warm-up and buddy system to resolve the cold-start performance problem during the early stages of model training.

warm-up

Pre-allocate multiple memory blocks, with the quantity and size controllable via environment variables. By default, 8 memory blocks of 1 GB each are pre-allocated.

buddy system

When a memory request is made, if the free list for the corresponding size is empty, the system will first search in the free list for larger sizes. If a suitable block is found, it will be split, and the resulting smaller block will either be allocated or inserted into the corresponding free list. When a memory block is released, the system will search the free list for its buddy block and merge them to satisfy subsequent memory requests.

threshold control

The buddy system's splitting mechanism inevitably causes memory fragmentation, primarily from splitting triggered by small-block requests. Experimental results show that memory request time is roughly proportional to block size, meaning KB-level requests account for a negligible time overhead (<1%) during training. Thus, we introduce a threshold control mechanism, configurable via environment variables, that sets a minimum block size for buddy system allocation. Requests smaller than this threshold are handled by the original allocation mechanism, substantially reducing the fragmentation rate.

adaptive clear mechanism

When calling empty_cache to release memory, only complete pre-allocated blocks are returned; split blocks with unreleased portions are not returned. When a task that previously called empty_cache is run again, the previously returned memory blocks are reallocated to the task.

usage

set pre-allocated block size (unit: MB, default 1024)

export BUDDY_BLOCK_SIZE_MB=512

set pre-allocated block count (default 8)

export BUDDY_BLOCK_COUNT=16

set the threshold for memory requests allocated via the buddy system (unit: MB, default 4)

export BUDDY_THRESHOLD_MB=2

experiment

We conducted comparative experiments training the DeepSeek-V3 model on 4 machines with 32 H100 GPUs. Experimental results show that this solution reduces memory allocation time by over 40% and total training time by 11%. Additionally, the threshold mechanism effectively prevents memory fragmentation.

step_time/smemory before empty_cache(GB)memory after empty_cache(GB)
Baseline6.7208.3943.658
S=4GB C=7 T=4MB5.96732.3343.672
S=4GB C=7 T=0MB6.18936.44136.441

S: pre-allocated block size C: pre-allocated block count T: threshold

Currently, this optimization solution has been officially deployed internally at Kuaishou, demonstrating strong practicality.

Alternatives

No response

Additional context

No response

cc @jerryzh168 @ptrblck @msaroufim @eqy @tinglvv @nWEIdia

extent analysis

TL;DR

To address the cold-start performance issue in the pin memory allocation module, implement a warm-up strategy with a buddy system and threshold control mechanism to optimize memory allocation.

Guidance

  • Set the pre-allocated block size and count using environment variables BUDDY_BLOCK_SIZE_MB and BUDDY_BLOCK_COUNT to control the initial memory allocation.
  • Configure the threshold for memory requests allocated via the buddy system using BUDDY_THRESHOLD_MB to prevent memory fragmentation.
  • Monitor the memory allocation time and total training time to verify the effectiveness of the optimization solution.
  • Adjust the threshold value to balance memory allocation efficiency and fragmentation rate.

Example

export BUDDY_BLOCK_SIZE_MB=512
export BUDDY_BLOCK_COUNT=16
export BUDDY_THRESHOLD_MB=2

Notes

The optimization solution has been deployed internally at Kuaishou, demonstrating its practicality. However, the effectiveness of the solution may vary depending on the specific use case and system configuration.

Recommendation

Apply the workaround by implementing the warm-up strategy with a buddy system and threshold control mechanism to optimize memory allocation, as it has shown to reduce memory allocation time by over 40% and total training time by 11%.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix Optimization: pin memory management optimization based on pre-allocation and buddy system [1 pull requests, 1 participants]