Skip to content

Commit 7ffebd1

Browse files
committed
Fix standalone ZeRO conversion script
Signed-off-by: gaoxiaomo <165135449+gaoxiaomo@users.noreply.github.com>
1 parent f38e9a0 commit 7ffebd1

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

deepspeed/utils/zero_to_torch.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
#!/usr/bin/env python
2+
13
# SPDX-License-Identifier: Apache-2.0
24
# DeepSpeed Team
35

46
import argparse
57

6-
import deepspeed.utils.zero_to_fp32 as zero_to_fp32
8+
if __package__:
9+
from . import zero_to_fp32
10+
else:
11+
import zero_to_fp32
712

813

914
def main(args=None):

tests/unit/checkpoint/test_convert_checkpoint.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
# DeepSpeed Team
55

6+
import shutil
7+
import subprocess
8+
import sys
9+
610
import torch
711
import torch.nn as nn
812
import pytest
913

1014
import deepspeed
1115
import deepspeed.utils.zero_to_fp32 as zero_to_fp32
16+
import deepspeed.utils.zero_to_torch as zero_to_torch
1217
from deepspeed.utils.zero_to_fp32 import (convert_zero_checkpoint_to_fp32_state_dict,
1318
convert_zero_checkpoint_to_state_dict, to_torch_tensor)
1419
from deepspeed.utils.zero_to_torch import main as zero_to_torch_main
@@ -67,6 +72,31 @@ def record_conversion(checkpoint_dir, output_dir, **kwargs):
6772
assert call["max_shard_size"] == "1GB"
6873

6974

75+
def test_zero_to_torch_standalone_uses_local_converter(tmp_path):
76+
script_path = tmp_path / "zero_to_torch.py"
77+
shutil.copyfile(zero_to_torch.__file__, script_path)
78+
(tmp_path / "zero_to_fp32.py").write_text(
79+
"from pathlib import Path\n"
80+
"OUTPUT_DTYPE_NAMES = {'fp16': None}\n"
81+
"debug = False\n"
82+
"def convert_zero_checkpoint_to_state_dict(checkpoint_dir, output_dir, **kwargs):\n"
83+
" Path(output_dir).write_text(kwargs['dtype'], encoding='utf-8')\n",
84+
encoding="utf-8")
85+
marker_path = tmp_path / "converter.txt"
86+
87+
subprocess.run([
88+
sys.executable,
89+
str(script_path),
90+
"checkpoint",
91+
str(marker_path),
92+
"--dtype",
93+
"fp16",
94+
], check=True)
95+
96+
assert script_path.read_text(encoding="utf-8").startswith("#!/usr/bin/env python\n")
97+
assert marker_path.read_text(encoding="utf-8") == "fp16"
98+
99+
70100
class ModelWithSharedWeights(nn.Module):
71101

72102
def __init__(self):

0 commit comments

Comments
 (0)