Skip to content

Conversation

BraveY
Copy link

@BraveY BraveY commented Jun 15, 2025

Motivation

The purpose of this PR is to optimize the cold start performance of the inference engine when model weights are already stored on the local disk. The current model loading approach fails to fully utilize available disk bandwidth during initial startup, resulting in suboptimal loading speeds. By implementing a disk bandwidth-optimized loading strategy for cold start scenarios, we can significantly accelerate the engine's initialization process. This improvement will directly enhance Pod scaling efficiency and deployment speed in production environments, enabling faster resource provisioning and workload handling capabilities.

Modifications

Introduce a new load format 'prefetch_auto' that performs concurrent mmap with MAP_POPULATE to prefetch safetensors files into the page cache. This helps maximize storage bandwidth and improve model loading performance, especially on systems with high disk I/O capacity.

Checklist

Test Plan

Our test env:

#free -h
              total        used        free      shared  buff/cache   available
Mem:           2.0T         23G        1.3T         92M        640G        1.9T
Swap:            0B          0B          0B

lsblk result:

NAME            FSTYPE      LABEL    MOUNTPOINT   SIZE MODEL
nvme0n1                                           3.5T SAMSUNG MZQL23T8HCLS-00B7C
└─nvme0n1p1     LVM2_member                       3.5T
  └─vgdata-data ext4                 /data       10.5T
nvme3n1                                           3.5T SAMSUNG MZQL23T8HCLS-00B7C
└─nvme3n1p1     ext4                 /home        3.5T
sdb                                             447.1G SAMSUNG MZ7L3480
├─sdb4          iso9660     config-2             64.8M
├─sdb2          ext4                 /boot          1G
├─sdb3          ext4                 /           58.5G
└─sdb1          vfat                 /boot/efi    512M
nvme2n1                                           3.5T SAMSUNG MZQL23T8HCLS-00B7C
└─nvme2n1p1     LVM2_member                       3.5T
  └─vgdata-data ext4                 /data       10.5T
nvme1n1                                           3.5T SAMSUNG MZQL23T8HCLS-00B7C
└─nvme1n1p1     LVM2_member                       3.5T
  └─vgdata-data ext4                 /data       10.5T
sda                                             447.1G SAMSUNG MZ7L3480

The loaded model is DeepSeek-R1, the weight file size: 642GB. All the weight file is mounted in /data. Three 3.5 TB NVMe Samsung MZQL23T8HCLS-00B7C drives are configured in RAID 0 with LVM, mounted to the /data directory, providing a total storage capacity of 10 TB.

We set the tp=8 in start command.

Test Result

Module Loader Load Time (s) Peak I/O Bandwidth (GB/s) Average Bandwidth (GB/s)
Default Module Loader 384 5.16 1.72
Prefetch Auto Module Loader 96 18.75 15.28

Time Reduction: The prefetch loader reduces loading time from 384s to 96s (75% improvement).

Introduce a new load format 'prefetch_auto' that performs concurrent
mmap with MAP_POPULATE to prefetch safetensors files into the page cache.
This helps maximize storage bandwidth and improve model loading performance,
especially on systems with high disk I/O capacity.

Signed-off-by: Yang Kaiyong <yangkaiyong.yky@antgroup.com>
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @BraveY, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request aims to significantly improve the cold start performance of the inference engine by optimizing the model loading process. It introduces a new prefetch_auto load format that leverages concurrent mmap with MAP_POPULATE to prefetch model weight files into the page cache, thereby maximizing disk bandwidth utilization during initialization. This change is expected to reduce model loading times, especially on systems with high-speed storage like NVMe drives, leading to faster Pod scaling and deployment.

Highlights

  • New Load Format: Introduced a new load_format option called prefetch_auto.
  • Parallel Prefetching: Implemented parallel file prefetching using mmap with the MAP_POPULATE flag to load weight files into the page cache concurrently.
  • Performance Improvement: Integrated the new prefetching logic into the model loading process to accelerate cold start performance, particularly for large models on fast storage.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configureGemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a prefetch_auto load format to accelerate model loading using mmap with MAP_POPULATE. Key areas for consideration include the platform-specific nature of MAP_POPULATE and handling potential zero-size weight files.

fileno=f.fileno(),
length=file_size,
prot=mmap.PROT_READ,
flags=mmap.MAP_SHARED | mmap.MAP_POPULATE
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The MAP_POPULATE flag is Linux-specific. Consider documenting this limitation or providing alternative implementations for other platforms.

Style Guide References

Suggested change
flags=mmap.MAP_SHARED | mmap.MAP_POPULATE
flags=mmap.MAP_SHARED | mmap.MAP_POPULATE # Linux-specific

Comment on lines +473 to +481
with open(st_file, "rb") as f:
file_size = os.path.getsize(st_file)
mm = mmap.mmap(
fileno=f.fileno(),
length=file_size,
prot=mmap.PROT_READ,
flags=mmap.MAP_SHARED | mmap.MAP_POPULATE
)
mm.close()
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider handling the edge case where file_size is 0 to prevent potential errors with mmap.mmap. An empty file check could be added.

Style Guide References

Suggested change
with open(st_file, "rb") as f:
file_size = os.path.getsize(st_file)
mm = mmap.mmap(
fileno=f.fileno(),
length=file_size,
prot=mmap.PROT_READ,
flags=mmap.MAP_SHARED | mmap.MAP_POPULATE
)
mm.close()
def _mmap_single_file(st_file: str) -> None:
file_size = os.path.getsize(st_file)
if file_size == 0:
logger.info(f"Skipping mmap for empty file: {st_file}")
return
with open(st_file, "rb") as f:
mm = mmap.mmap(
fileno=f.fileno(),
length=file_size,
prot=mmap.PROT_READ,
flags=mmap.MAP_SHARED | mmap.MAP_POPULATE
)
mm.close()

…ight

Signed-off-by: Yang Kaiyong <yangkaiyong.yky@antgroup.com>
@EvanCley
Copy link

Test Result

Module Loader Load Time (s) Peak I/O Bandwidth (GB/s) Average Bandwidth (GB/s)
Default Module Loader 384 5.16 1.72
Prefetch Auto Module Loader 96 18.75 15.28
Time Reduction: The prefetch loader reduces loading time from 384s to 96s (75% improvement).

I am curious if this optimized time includes the time of the model from cpu memory to gpu memory? Or just the time from disk to cpu memory

@BraveY
Copy link
Author

BraveY commented Jun 18, 2025

Update the test result between this PR and @xianzhiT's PR:
Weight in raid0 with three NVME ssd. The test model is ds-r1 fp8.

Multi-thread with the config: --tp-size 8 --quantization fp8 --model-loader-extra-config '{"enable_multithread_load": true, "num_threads": 8}'

$grep -C 5 "Load weight" sglang-test-2025-06-18_15:18.log
[2025-06-18 15:19:16 TP0] MLA optimization is turned on. Use flashinfer backend.
[2025-06-18 15:19:16 TP0] Chunked prefix cache is turned on.
[2025-06-18 15:19:16 TP0] Init torch distributed begin.
[2025-06-18 15:19:18 TP0] sglang is using nccl==2.26.2
[2025-06-18 15:19:22 TP0] Init torch distributed ends. mem usage=1.12 GB
[2025-06-18 15:19:24 TP0] Load weight begin. avail mem=93.52 GB
[2025-06-18 15:19:24 TP0] Detected fp8 checkpoint.
Multi-thread loading shards:   0% Completed | 0/163 [00:00<?, ?it/s]
Multi-thread loading shards:   1% Completed | 1/163 [01:22<3:42:45, 82.50s/it]
Multi-thread loading shards: 100% Completed | 163/163 [01:22<00:00,  1.98it/s]

[2025-06-18 15:20:48 TP0] Load weight end. type=DeepseekV3ForCausalLM, dtype=torch.bfloat16, avail mem=13.97 GB, mem usage=79.54 GB.
[2025-06-18 15:20:51 TP4] KV Cache is allocated. #tokens: 69931, KV size: 4.58 GB
[2025-06-18 15:20:51 TP7] KV Cache is allocated. #tokens: 69931, KV size: 4.58 GB
[2025-06-18 15:20:51 TP3] KV Cache is allocated. #tokens: 69931, KV size: 4.58 GB
[2025-06-18 15:20:51 TP2] KV Cache is allocated. #tokens: 69931, KV size: 4.58 GB
[2025-06-18 15:20:51 TP0] KV Cache is allocated. #tokens: 69931, KV size: 4.58 GB

The loading time is 1m24s: 15:19:24->15:20:48

My PR with barrier commit id 4e31c43. Config: --tp-size 8 --quantization fp8 --load-format prefetch_auto

$grep -C 5 "Load weight" sglang-test-2025-06-18_15:27.log
[2025-06-18 15:28:26 TP0] MLA optimization is turned on. Use flashinfer backend.
[2025-06-18 15:28:26 TP0] Chunked prefix cache is turned on.
[2025-06-18 15:28:26 TP0] Init torch distributed begin.
[2025-06-18 15:28:28 TP0] sglang is using nccl==2.26.2
[2025-06-18 15:28:32 TP0] Init torch distributed ends. mem usage=1.12 GB
[2025-06-18 15:28:34 TP0] Load weight begin. avail mem=93.52 GB
[2025-06-18 15:28:34 TP0] Detected fp8 checkpoint.
[2025-06-18 15:28:35 TP6] Mmaping 20 files concurrently
[2025-06-18 15:28:35 TP5] Mmaping 20 files concurrently
[2025-06-18 15:28:35 TP3] Mmaping 20 files concurrently
[2025-06-18 15:28:35 TP4] Mmaping 20 files concurrently
--
Loading safetensors checkpoint shards:  99% Completed | 161/163 [00:51<00:00,  3.16it/s]
Loading safetensors checkpoint shards:  99% Completed | 162/163 [00:52<00:00,  2.84it/s]
Loading safetensors checkpoint shards: 100% Completed | 163/163 [00:52<00:00,  2.76it/s]
Loading safetensors checkpoint shards: 100% Completed | 163/163 [00:52<00:00,  3.11it/s]

[2025-06-18 15:30:09 TP0] Load weight end. type=DeepseekV3ForCausalLM, dtype=torch.bfloat16, avail mem=13.07 GB, mem usage=80.44 GB.
[2025-06-18 15:30:09 TP5] KV Cache is allocated. #tokens: 55457, KV size: 3.63 GB
[2025-06-18 15:30:09 TP2] KV Cache is allocated. #tokens: 55457, KV size: 3.63 GB
[2025-06-18 15:30:09 TP0] KV Cache is allocated. #tokens: 55457, KV size: 3.63 GB
[2025-06-18 15:30:09 TP0] Memory pool end. avail mem=9.38 GB
[2025-06-18 15:30:09 TP3] KV Cache is allocated. #tokens: 55457, KV size: 3.63 GB

The loading time is 1m35s: 15:28:34->15:30:09

My PR without barrier commit id 1ae9d55.

$grep -C 5 "Load weight" sglang-test-2025-06-18_16:24.log
[2025-06-18 16:24:55 TP0] MLA optimization is turned on. Use flashinfer backend.
[2025-06-18 16:24:55 TP0] Chunked prefix cache is turned on.
[2025-06-18 16:24:55 TP0] Init torch distributed begin.
[2025-06-18 16:24:58 TP0] sglang is using nccl==2.26.2
[2025-06-18 16:25:01 TP0] Init torch distributed ends. mem usage=1.12 GB
[2025-06-18 16:25:03 TP0] Load weight begin. avail mem=93.52 GB
[2025-06-18 16:25:03 TP0] Detected fp8 checkpoint.
[2025-06-18 16:25:03 TP5] Mmaping 20 files concurrently
[2025-06-18 16:25:04 TP1] Mmaping 21 files concurrently
[2025-06-18 16:25:04 TP0] Mmaping 21 files concurrently
[2025-06-18 16:25:04 TP3] Mmaping 20 files concurrently
--
Loading safetensors checkpoint shards:  99% Completed | 161/163 [00:43<00:00,  3.78it/s]
Loading safetensors checkpoint shards:  99% Completed | 162/163 [00:43<00:00,  3.51it/s]
Loading safetensors checkpoint shards: 100% Completed | 163/163 [00:43<00:00,  3.48it/s]
Loading safetensors checkpoint shards: 100% Completed | 163/163 [00:43<00:00,  3.72it/s]

[2025-06-18 16:26:28 TP0] Load weight end. type=DeepseekV3ForCausalLM, dtype=torch.bfloat16, avail mem=13.84 GB, mem usage=79.68 GB.
[2025-06-18 16:26:38 TP5] KV Cache is allocated. #tokens: 67872, KV size: 4.44 GB
[2025-06-18 16:26:38 TP1] KV Cache is allocated. #tokens: 67872, KV size: 4.44 GB
[2025-06-18 16:26:38 TP6] KV Cache is allocated. #tokens: 67872, KV size: 4.44 GB
[2025-06-18 16:26:38 TP0] KV Cache is allocated. #tokens: 67872, KV size: 4.44 GB
[2025-06-18 16:26:38 TP7] KV Cache is allocated. #tokens: 67872, KV size: 4.44 GB

The loading time is 1m25s:16:25:03->16:26:28.

We obtained the same performance boost. Excellent work!

@BraveY
Copy link
Author

BraveY commented Jun 18, 2025

Test Result

Module Loader Load Time (s) Peak I/O Bandwidth (GB/s) Average Bandwidth (GB/s)
Default Module Loader 384 5.16 1.72
Prefetch Auto Module Loader 96 18.75 15.28
Time Reduction: The prefetch loader reduces loading time from 384s to 96s (75% improvement).

I am curious if this optimized time includes the time of the model from cpu memory to gpu memory? Or just the time from disk to cpu memory

Yes, it includes the time of the model from cpu memory to gpu memory.

@junliu-mde
Copy link
Contributor

Good idea. I've also noticed in practice that if the model weights are in the page cache, the startup is much faster. This PR seems more user-friendly than #7277.

Comment on lines 449 to 457
def prefetch_weight_files(hf_weights_files: List[str]) -> None:
"""Prefetch and mmap weight files in parallel for the current distributed rank."""
world_size = 1
rank = 0
if torch.distributed.is_initialized():
world_size = torch.distributed.get_world_size()
rank = torch.distributed.get_rank()
local_files = hf_weights_files[rank::world_size]
mmap_files_concurrently(local_files)
Copy link

@TrafalgarZZZ TrafalgarZZZ Jul 10, 2025

Choose a reason for hiding this comment

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

@BraveY I've tested this in my environment and it turns out to be very useful on a single node. But when I launched a distributed serving, it does not work pretty well. So In distributed serving cases, I think it should prefetch all weights on each node?

Copy link
Author

@BraveY BraveY Jul 11, 2025

Choose a reason for hiding this comment

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

Confirmed, I've identified this issue as well. We need to prefetch all weights on each node. I'll implement the fix shortly.

Signed-off-by: Yang Kaiyong <yangkaiyong.yky@antgroup.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants