Skip to content

Commit fc3de67

Browse files
committed
feat: add a switch to disable inplace pinning of tensors
1 parent 888c393 commit fc3de67

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

checkpoint_engine/ps.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -622,19 +622,25 @@ def _register_checkpoint(
622622
named_tensors: dict[str, torch.Tensor],
623623
rank: int | None = None,
624624
shared_pin_memory: list[MemoryBuffer] | None = None,
625+
inplace_pin: bool = False,
625626
) -> list[MemoryBuffer]:
626627
logger.info(
627628
f"[rank{rank}] start to register checkpoint with {len(files)} files and {len(named_tensors)} named_tensors"
628629
)
629630
if not files and not named_tensors:
630631
return []
631632
memory_buffers: list[MemoryBuffer] = []
632-
files_to_inplace_pin = [
633-
file
634-
for file in files
635-
if file.startswith("/dev/shm/") and file.endswith(".safetensors") # noqa: S108
636-
]
637-
files_to_normal_pin = [file for file in files if file not in files_to_inplace_pin]
633+
if inplace_pin:
634+
logger.info(f"[rank{rank}] allow inplace pin memory for /dev/shm/ safetensors files")
635+
files_to_inplace_pin = [
636+
file
637+
for file in files
638+
if file.startswith("/dev/shm/") and file.endswith(".safetensors") # noqa: S108
639+
]
640+
files_to_normal_pin = [file for file in files if file not in files_to_inplace_pin]
641+
else:
642+
files_to_normal_pin = files
643+
files_to_inplace_pin = []
638644
if files_to_normal_pin or named_tensors:
639645
memory_buffers.extend(
640646
_normal_pin_memory(
@@ -959,6 +965,7 @@ def register_checkpoint(
959965
files: list[str] | None = None,
960966
named_tensors: dict[str, torch.Tensor] | None = None,
961967
use_shared_memory_pool: bool = False,
968+
use_inplace_pin_memory: bool = False,
962969
) -> None:
963970
"""
964971
Register a checkpoint to the parameter server. Both files and named_tensors will be registered together.
@@ -974,6 +981,8 @@ def register_checkpoint(
974981
cannot accommodate checkpoints with different memory requirements.
975982
To free the actual memory of the shared pool or to modify its shape,
976983
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.
985+
Currently, this feature is experimental and may crush.
977986
"""
978987
try:
979988
if use_shared_memory_pool:
@@ -1002,7 +1011,10 @@ def register_checkpoint(
10021011
f"checkpoint {checkpoint_name} already registered"
10031012
)
10041013
self._memory_pool[checkpoint_name] = _register_checkpoint(
1005-
files=files or [], named_tensors=named_tensors or {}, rank=self._rank
1014+
files=files or [],
1015+
named_tensors=named_tensors or {},
1016+
rank=self._rank,
1017+
inplace_pin=use_inplace_pin_memory,
10061018
)
10071019
if self._p2p_store is not None:
10081020
self._register_parameters_to_p2p_store(checkpoint_name)

tests/test_update.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ def run_with_files(
218218
if rank == 0:
219219
import shutil
220220

221-
os.removedirs(dev_shm_dir)
221+
# this test should be run under use_inplace_pin_memory=False. Otherwise, the files in /dev/shm/ will be deleted.
222+
shutil.rmtree(dev_shm_dir)
222223
shutil.rmtree(disk_dir)
223224
assert proc.exitcode == 0
224225

0 commit comments

Comments
 (0)