Skip to content

[Fix] Skip shmem for per-rank weights — use plain pinned tensor - #74

Merged
jiahy0825 merged 1 commit into
mainfrom
fix/per-rank-skip-shmem
Sep 10, 2026
Merged

[Fix] Skip shmem for per-rank weights — use plain pinned tensor#74
jiahy0825 merged 1 commit into
mainfrom
fix/per-rank-skip-shmem

Conversation

@cennn

@cennn cennn commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

🗂️ PR Category

  • 🚀 Optimization (performance, memory, etc.)

📝 Description

When per_rank=True (EP scenario, each rank has unique weights), the current code creates an mmap shared-memory file via _create_empty_shm, writes into it, then immediately deletes it — the file is never read by another rank. This is pure filesystem overhead.

Change: Replace _create_empty_shm + os.remove with a plain torch.empty(), keeping the streaming _stream_copy_and_replace and pin_memory_in_place unchanged.

The per_rank=False path (all ranks share identical weights) is untouched — it still uses mmap so rank 0 writes once and all other ranks map the same file.

Before

path = _shm_path(cls_name, dtype, rank=local_rank)
giant = _create_empty_shm(path, total_numel, dtype)
_stream_copy_and_replace(module, giant, param_list)
pin_memory_in_place(giant)
buffers.append(giant)
if os.path.exists(path):
    os.remove(path)

After

giant = torch.empty(total_numel, dtype=dtype, device="cpu")
_stream_copy_and_replace(module, giant, param_list)
pin_memory_in_place(giant)
buffers.append(giant)

When per_rank=True (EP scenario), each rank has unique weights and never
shares them with other ranks.  The mmap file was created only to be read
by the same rank and immediately deleted — pure overhead.

Replace _create_empty_shm + os.remove with a plain torch.empty, keeping
the streaming copy-and-replace and pin_memory_in_place unchanged.
@cennn cennn added the ci:run Trigger CI integration tests label Sep 10, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Sep 10, 2026
@cennn cennn added the ci:run Trigger CI integration tests label Sep 10, 2026
@github-actions github-actions Bot removed the ci:run Trigger CI integration tests label Sep 10, 2026

@jiahy0825 jiahy0825 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@jiahy0825
jiahy0825 merged commit 94431a9 into main Sep 10, 2026
39 of 41 checks passed
@jiahy0825
jiahy0825 deleted the fix/per-rank-skip-shmem branch September 10, 2026 07:20
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.

2 participants