|
19 | 19 | import zmq |
20 | 20 | from loguru import logger |
21 | 21 | from pydantic import BaseModel, PlainSerializer, PlainValidator, WithJsonSchema |
22 | | -from safetensors.torch import _TYPES, _getdtype, safe_open |
| 22 | +from safetensors.torch import _getdtype, safe_open |
23 | 23 | from torch.multiprocessing.reductions import reduce_tensor |
24 | 24 |
|
25 | 25 | from checkpoint_engine.device_utils import DeviceManager, get_ip, npu_generate_uuid |
@@ -476,27 +476,27 @@ def _pin(t: torch.Tensor): |
476 | 476 | """ |
477 | 477 | cudart = torch.cuda.cudart() |
478 | 478 | 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}" |
480 | 480 |
|
481 | 481 | 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 | + """ |
482 | 488 | # TODO: should only support /dev/shm? but we found files in disk also work? |
483 | 489 | 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. |
490 | 490 | 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 | + ) |
498 | 495 | 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 | + ) |
500 | 500 | header_tensor = t[flag_size:start_pos] |
501 | 501 | header = json.loads(header_tensor.numpy().tobytes()) |
502 | 502 | if "__metadata__" in header: |
|
0 commit comments