Skip to content

Commit 0158e7a

Browse files
committed
doc
1 parent afd880d commit 0158e7a

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

checkpoint_engine/ps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ def register_checkpoint(
969969
) -> None:
970970
"""
971971
Register a checkpoint to the parameter server. Both files and named_tensors will be registered together.
972-
Warning: .safetensors files in /dev/shm/ will be pinned in-place, and the files will be REMOVED after pinning.
972+
Warning: if `use_inplace_pin_memory` is True, .safetensors files in /dev/shm/ will be pinned in-place, and the files will be REMOVED after pinning.
973973
Please make sure to copy the files to disks if you need to keep them.
974974
975975
Args:
@@ -981,7 +981,7 @@ def register_checkpoint(
981981
cannot accommodate checkpoints with different memory requirements.
982982
To free the actual memory of the shared pool or to modify its shape,
983983
please unregister the current user of the shared memory pool using `unregister_checkpoint` with `force=True`.
984-
use_inplace_pin_memory: If True, allows inplace pin memory for /dev/shm/ safetensors files.
984+
use_inplace_pin_memory: If True, allows inplace pin memory for /dev/shm/ safetensors files. This option is ignored when ``use_shared_memory_pool`` is True.
985985
Currently, this feature is experimental and may crash.
986986
"""
987987
try:

tests/test_unpin.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import json
2+
import os
3+
import random
4+
import subprocess
5+
import sys
6+
import time
7+
from contextlib import nullcontext
8+
from types import SimpleNamespace
9+
10+
import torch
11+
import zmq
12+
from torch.multiprocessing import Queue, get_context
13+
14+
from checkpoint_engine.device_utils import DeviceManager
15+
from checkpoint_engine.ps import ParameterServer, _get_physical_gpu_id
16+
from checkpoint_engine.worker import update_weights_from_ipc
17+
18+
device_manager = DeviceManager()
19+
20+
def gen_test_tensors(rank: int) -> list[tuple[str, torch.Tensor]]:
21+
tensors = []
22+
for layer in range(random.randint(10, 50)):
23+
for num in range(random.randint(50, 100)):
24+
r = random.randint(0, 16)
25+
if r < 4:
26+
dtype = torch.bfloat16
27+
elif r < 10:
28+
dtype = torch.float16
29+
elif r < 14:
30+
dtype = torch.float8_e4m3fn
31+
else:
32+
dtype = torch.float
33+
tensors.append(
34+
(
35+
f"rank{rank}.layer{layer}.num{num}",
36+
torch.randn([random.randint(100, 500), random.randint(500, 1000)]).to(dtype),
37+
)
38+
)
39+
return tensors
40+
41+
def get_world_size() -> int:
42+
return device_manager.device_module.device_count()
43+
44+
def get_files():
45+
rank = int(os.getenv("RANK"))
46+
named_tensors = dict(gen_test_tensors(rank))
47+
import safetensors.torch
48+
49+
files = []
50+
dev_shm_dir = "/dev/shm/checkpoint_engine_tests" # noqa: S108
51+
os.makedirs(dev_shm_dir, exist_ok=True)
52+
tensors_in_dev_shm = named_tensors
53+
time.sleep(1)
54+
dev_shm_files = [
55+
os.path.join(dev_shm_dir, f"rank{rank}_checkpoint.safetensors")
56+
for _ in range(get_world_size())
57+
]
58+
safetensors.torch.save_file(tensors_in_dev_shm, dev_shm_files[rank])
59+
time.sleep(1)
60+
files.append(dev_shm_files[rank])
61+
return files
62+
63+
64+
def run_with_files(num):
65+
ps = ParameterServer(auto_pg=True)
66+
checkpoint_name = "test_with_files"
67+
for _ in range(num):
68+
files = get_files()
69+
ps.register_checkpoint(checkpoint_name, files=files)
70+
ps.gather_metas(checkpoint_name)
71+
ps.unregister_checkpoint(checkpoint_name)
72+
73+
if __name__ == "__main__":
74+
run_with_files(3)

0 commit comments

Comments
 (0)