Skip to content

Commit 3b3371b

Browse files
committed
fix: fix PR issues
1 parent 923cbe7 commit 3b3371b

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

checkpoint_engine/ps.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import zmq
2020
from loguru import logger
2121
from pydantic import BaseModel, PlainSerializer, PlainValidator, WithJsonSchema
22-
from safetensors.torch import _TYPES, _getdtype, safe_open
22+
from safetensors.torch import _getdtype, safe_open
2323
from torch.multiprocessing.reductions import reduce_tensor
2424

2525
from checkpoint_engine.device_utils import DeviceManager, get_ip, npu_generate_uuid
@@ -476,27 +476,27 @@ def _pin(t: torch.Tensor):
476476
"""
477477
cudart = torch.cuda.cudart()
478478
r = cudart.cudaHostRegister(t.data_ptr(), t.numel() * t.element_size(), 0)
479-
assert r == 0, f"pin memory error, error code: {r.value}"
479+
assert r == 0, f"pin memory error, error code: {r}"
480480

481481
def _inplace_pin_memory(file_path: str) -> MemoryBuffer:
482+
"""
483+
safetensors format see https://huggingface.co/docs/safetensors/en/index#format.
484+
We load the safetensors file as bytes, then parse the header manually to get parameter metas.
485+
The actual tensor data is in the remaining bytes and is naturally aligned.
486+
We pin the remaining bytes as the buffer, making pinning faster.
487+
"""
482488
# TODO: should only support /dev/shm? but we found files in disk also work?
483489
size = os.stat(file_path).st_size
484-
t = torch.from_file(file_path, True, size, dtype=torch.uint8)
485-
486-
# safetensors format see https://huggingface.co/docs/safetensors/en/index#format.
487-
# We load the safetensors file as bytes, then parse the header manually to get parameter metas.
488-
# and the actual tensor data is in the remaining bytes.
489-
# We pin the remaining bytes as the buffer, making pinning faster.
490490
flag_size = 8
491-
with open(file_path, "rb") as f:
492-
n = bytearray(flag_size)
493-
data = f.readinto(n)
494-
assert data == flag_size, f"data {data} should be equal to flag_size {flag_size}"
495-
n = int.from_bytes(n, byteorder="little", signed=False)
496-
start_pos = n + flag_size
497-
491+
t = torch.from_file(file_path, True, size, dtype=torch.uint8)
492+
assert t.nbytes > flag_size, (
493+
f"tensor nbytes {t.nbytes} should be greater than flag_size {flag_size}"
494+
)
498495
os.remove(file_path)
499-
time.sleep(3)
496+
start_pos = (
497+
int.from_bytes(t[0:flag_size].numpy().tobytes(), byteorder="little", signed=False)
498+
+ flag_size
499+
)
500500
header_tensor = t[flag_size:start_pos]
501501
header = json.loads(header_tensor.numpy().tobytes())
502502
if "__metadata__" in header:

0 commit comments

Comments
 (0)