Skip to content

Commit e48d92f

Browse files
fix examples
Signed-off-by: Takeshi Yoshimura <tyos@jp.ibm.com>
1 parent e49a935 commit e48d92f

File tree

15 files changed

+122
-136
lines changed

15 files changed

+122
-136
lines changed

.github/workflows/test-torch.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ jobs:
5858
mkdir -p /tmp/pytest-log
5959
export TEST_FASTSAFETENSORS_FRAMEWORK=pytorch
6060
COVERAGE_FILE=.coverage_0 pytest -s --cov=${LIBDIR} test_fastsafetensors.py > /tmp/pytest-log/0.log 2>&1
61-
COVERAGE_FILE=.coverage_1 torchrun --nnodes=2 --master_addr=0.0.0.0 --master_port=1234 --node_rank=0 tests/test_multi.py --cov=${LIBDIR} -s tests/test_multi.py > /tmp/pytest-log/1.log 2>&1 &
62-
COVERAGE_FILE=.coverage_2 torchrun --nnodes=2 --master_addr=0.0.0.0 --master_port=1234 --node_rank=1 tests/test_multi.py --cov=${LIBDIR} -s tests/test_multi.py > /tmp/pytest-log/2.log 2>&1
61+
COVERAGE_FILE=.coverage_1 pytest -s --cov=${LIBDIR} test_vllm.py > /tmp/pytest-log/1.log 2>&1
62+
COVERAGE_FILE=.coverage_2 torchrun --nnodes=2 --master_addr=0.0.0.0 --master_port=1234 --node_rank=0 tests/test_multi.py --cov=${LIBDIR} -s tests/test_multi.py > /tmp/pytest-log/2.log 2>&1 &
63+
COVERAGE_FILE=.coverage_3 torchrun --nnodes=2 --master_addr=0.0.0.0 --master_port=1234 --node_rank=1 tests/test_multi.py --cov=${LIBDIR} -s tests/test_multi.py > /tmp/pytest-log/3.log 2>&1
6364
coverage combine .coverage_*
6465
coverage html
6566
mv htmlcov /tmp/pytest-log

Makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@ unittest:
1919
@FST_DIR=$(FST_DIR); \
2020
TEST_FASTSAFETENSORS_FRAMEWORK=torch COVERAGE_FILE=.coverage_0 pytest -s --cov=$(FST_DIR) tests/test_fastsafetensors.py && \
2121
TEST_FASTSAFETENSORS_FRAMEWORK=torch COVERAGE_FILE=.coverage_1 CUDA_VISIBLE_DEVICES="" pytest -s --cov=$(FST_DIR) tests/test_fastsafetensors.py && \
22-
TEST_FASTSAFETENSORS_FRAMEWORK=torch COVERAGE_FILE=.coverage_2 torchrun --nnodes=2 --master_addr=0.0.0.0 --master_port=1234 --node_rank=0 tests/test_multi.py --cov=$(FST_DIR) -s tests/test_multi.py > /tmp/2.log 2>&1 & \
23-
TEST_FASTSAFETENSORS_FRAMEWORK=torch COVERAGE_FILE=.coverage_3 torchrun --nnodes=2 --master_addr=0.0.0.0 --master_port=1234 --node_rank=1 tests/test_multi.py --cov=$(FST_DIR) -s tests/test_multi.py > /tmp/3.log 2>&1 && \
22+
TEST_FASTSAFETENSORS_FRAMEWORK=torch COVERAGE_FILE=.coverage_2 pytest -s --cov=$(FST_DIR) -s tests/test_vllm.py && \
23+
TEST_FASTSAFETENSORS_FRAMEWORK=torch COVERAGE_FILE=.coverage_3 torchrun --nnodes=2 --master_addr=0.0.0.0 --master_port=1234 --node_rank=0 tests/test_multi.py --cov=$(FST_DIR) -s tests/test_multi.py > /tmp/3.log 2>&1 & \
24+
TEST_FASTSAFETENSORS_FRAMEWORK=torch COVERAGE_FILE=.coverage_4 torchrun --nnodes=2 --master_addr=0.0.0.0 --master_port=1234 --node_rank=1 tests/test_multi.py --cov=$(FST_DIR) -s tests/test_multi.py > /tmp/4.log 2>&1 && \
2425
wait
2526

2627
unittest-paddle:
2728
@FST_DIR=$(FST_DIR); \
28-
TEST_FASTSAFETENSORS_FRAMEWORK=paddle COVERAGE_FILE=.coverage_4 CUDA_VISIBLE_DEVICES="" pytest -s --cov=$(FST_DIR) tests/test_fastsafetensors.py && \
29-
TEST_FASTSAFETENSORS_FRAMEWORK=paddle COVERAGE_FILE=.coverage_5 CUDA_VISIBLE_DEVICES="" WORLD_SIZE=2 python3 -m paddle.distributed.launch --nnodes 2 --master 127.0.0.1:1234 --rank 0 tests/test_multi.py --cov=$(FST_DIR) -s tests/test_multi.py > /tmp/2.log 2>&1 & \
30-
TEST_FASTSAFETENSORS_FRAMEWORK=paddle COVERAGE_FILE=.coverage_6 CUDA_VISIBLE_DEVICES="" WORLD_SIZE=2 python3 -m paddle.distributed.launch --nnodes 2 --master 127.0.0.1:1234 --rank 1 tests/test_multi.py --cov=$(FST_DIR) -s tests/test_multi.py > /tmp/3.log 2>&1 && \
29+
TEST_FASTSAFETENSORS_FRAMEWORK=paddle COVERAGE_FILE=.coverage_5 CUDA_VISIBLE_DEVICES="" pytest -s --cov=$(FST_DIR) tests/test_fastsafetensors.py && \
30+
TEST_FASTSAFETENSORS_FRAMEWORK=paddle COVERAGE_FILE=.coverage_6 CUDA_VISIBLE_DEVICES="" WORLD_SIZE=2 python3 -m paddle.distributed.launch --nnodes 2 --master 127.0.0.1:1234 --rank 0 tests/test_multi.py --cov=$(FST_DIR) -s tests/test_multi.py > /tmp/6.log 2>&1 & \
31+
TEST_FASTSAFETENSORS_FRAMEWORK=paddle COVERAGE_FILE=.coverage_7 CUDA_VISIBLE_DEVICES="" WORLD_SIZE=2 python3 -m paddle.distributed.launch --nnodes 2 --master 127.0.0.1:1234 --rank 1 tests/test_multi.py --cov=$(FST_DIR) -s tests/test_multi.py > /tmp/7.log 2>&1 && \
3132
wait
3233

3334
unittest-paddle-gpu:
3435
@FST_DIR=$(FST_DIR); \
35-
TEST_FASTSAFETENSORS_FRAMEWORK=paddle COVERAGE_FILE=.coverage_7 pytest -s --cov=$(FST_DIR) tests/test_fastsafetensors.py
36+
TEST_FASTSAFETENSORS_FRAMEWORK=paddle COVERAGE_FILE=.coverage_8 pytest -s --cov=$(FST_DIR) tests/test_fastsafetensors.py
3637

3738
htmlcov:
3839
coverage combine .coverage_* && \

examples/a_paddle.safetensors

360 Bytes
Binary file not shown.

examples/b_paddle.safetensors

360 Bytes
Binary file not shown.

examples/extract_keys.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
from typing import Dict, List
44

5-
from safetensors import safe_open
5+
from safetensors.torch import load_file
66

77
from fastsafetensors import SafeTensorsFileLoader, SingleGroup
88

@@ -23,9 +23,7 @@
2323
".safetensors"
2424
):
2525
src_files[0].append(input_file_or_dir)
26-
with safe_open(input_file_or_dir, framework="pytorch") as f:
27-
for key in f.keys():
28-
orig_keys[key] = f.get_tensor(key)
26+
orig_keys = load_file(input_file_or_dir)
2927
loader.add_filenames(src_files)
3028
fb = loader.copy_files_to_device()
3129
if len(orig_keys) > 0:

examples/gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def gen_paddle():
1919
from safetensors.paddle import save_file
2020

2121
t0 = paddle.concat(
22-
[paddle.full((1, 8), i, dtype=paddle.float16) for i in range(0, 16)], dim=0
22+
[paddle.full((1, 8), i, dtype=paddle.float16) for i in range(0, 16)], axis=0
2323
)
2424

2525
for file_prefix in ["a", "b"]:

examples/run_reuse_loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import torch
44

5-
from fastsafetensors import SafeTensorsFileLoader
5+
from fastsafetensors import SafeTensorsFileLoader, SingleGroup
66

77
sys.path.insert(0, "/nvme/manish/repos/fastsafetensors/fastsafetensors")
88

99
device = "cuda:0" if torch.cuda.is_available() else "cpu"
10-
loader = SafeTensorsFileLoader(device, nogds=True, debug_log=True)
10+
loader = SafeTensorsFileLoader(SingleGroup(), device, nogds=True, debug_log=True)
1111

1212
loader.add_filenames({0: ["a.safetensors"]}) # {rank: files}
1313
fb = loader.copy_files_to_device()

examples/run_single.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
#!/usr/bin/env python3
22

3-
4-
def run_torch():
5-
import torch
6-
7-
device = "cuda:0" if torch.cuda.is_available() else "cpu"
8-
return device
9-
10-
11-
def run_paddle():
12-
import paddle
13-
14-
device = "gpu" if paddle.device.cuda.device_count() else "cpu"
15-
return device
16-
17-
18-
runs = {
19-
"torch": run_torch,
20-
"paddle": run_paddle,
21-
}
22-
233
if __name__ == "__main__":
244
import sys
255

266
from fastsafetensors import cpp as fstcpp
277
from fastsafetensors import fastsafe_open
288

299
framework = "torch"
10+
filenames = ["a.safetensors", "b.safetensors"]
3011
if len(sys.argv) > 1:
3112
framework = sys.argv[1]
13+
if framework == "torch":
14+
import torch
15+
16+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
17+
elif framework == "paddle":
18+
import paddle
19+
20+
device = "gpu" if paddle.device.cuda.device_count() else "cpu"
21+
filenames = ["a_paddle.safetensors", "b_paddle.safetensors"]
22+
else:
23+
raise Exception(f"unknown framework: {framework}")
3224

33-
device = runs[framework]()
3425
with fastsafe_open(
35-
["a.safetensors", "b.safetensors"],
26+
filenames,
3627
device=device,
3728
nogds=not fstcpp.is_cufile_found(),
3829
framework=framework,

examples/run_vllm.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

fastsafetensors/__init__.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,3 @@
44
from .common import SafeTensorsMetadata, SingleGroup, TensorFrame, get_device_numa_node
55
from .file_buffer import FilesBufferOnDevice
66
from .loader import SafeTensorsFileLoader, fastsafe_open
7-
8-
loaded_nvidia: bool = False
9-
if not loaded_nvidia:
10-
from . import cpp
11-
12-
cpp.load_nvidia_functions()
13-
if cpp.init_gds() != 0:
14-
raise Exception(f"[FAIL] init_gds()")
15-
loaded_nvidia = True

0 commit comments

Comments
 (0)