Files
wehub-resource-sync edf74f4e18
Build and Deploy Sphinx Docs / deploy (push) Has been skipped
tests / tests (ubuntu-latest, 3.11, 4.57.1) (push) Failing after 1s
tests / tests (ubuntu-latest, 3.13, ) (push) Failing after 1s
docker / build (cuda) (push) Failing after 1s
docker / build (npu-a3) (push) Failing after 1s
tests / tests (ubuntu-latest, 3.11, ) (push) Failing after 1s
docker / build (npu-a2) (push) Failing after 1s
Build and Deploy Sphinx Docs / build (push) Failing after 1s
tests / tests (ubuntu-latest, 3.11, 4.55.0) (push) Failing after 0s
tests / tests (ubuntu-latest, 3.12, ) (push) Failing after 1s
tests / tests (windows-latest, 3.11, ) (push) Has been cancelled
tests / tests (windows-latest, 3.12, ) (push) Has been cancelled
tests / tests (macos-latest, 3.11, ) (push) Has been cancelled
tests / tests (macos-latest, 3.12, ) (push) Has been cancelled
tests / tests (macos-latest, 3.13, ) (push) Has been cancelled
tests / tests (windows-latest, 3.13, ) (push) Has been cancelled
tests_cuda / tests (linux-x86_64-gpu-2, 3.11) (push) Has been cancelled
tests_npu / tests (linux-aarch64-a2-4, 3.11, 2.7.1) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:11:53 +08:00

64 lines
2.0 KiB
Python

# Copyright 2025 the LlamaFactory team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Convert a HuggingFace model to DCP checkpoint format.
Usage:
python scripts/hf2dcp.py convert --hf_path=/path/to/hf --dcp_path=/path/to/dcp
Arguments:
hf_path: Path to the HuggingFace model directory.
dcp_path: Output path (directory) for DCP checkpoint.
"""
import fire
import torch
import torch.distributed.checkpoint as dcp
import transformers
from transformers import AutoConfig
def convert(hf_path: str, dcp_path: str) -> None:
"""Convert HF model weights to DCP.
Args:
hf_path: HuggingFace model directory.
dcp_path: Output path (directory) for DCP checkpoint.
"""
if not hf_path or not dcp_path:
raise ValueError("Both 'hf_path' and 'dcp_path' are required.")
print(f"Loading HF model from {hf_path}...")
config = AutoConfig.from_pretrained(hf_path)
architectures = getattr(config, "architectures", [])
if architectures:
model_cls = getattr(transformers, architectures[0], transformers.AutoModelForCausalLM)
else:
model_cls = transformers.AutoModelForCausalLM
model = model_cls.from_pretrained(hf_path, device_map="cpu", torch_dtype=torch.bfloat16)
print(f"Saving to DCP format at {dcp_path}...")
dcp.save(model.state_dict(), checkpoint_id=dcp_path)
print("Done!")
def help() -> None:
"""Show help message."""
print(__doc__)
if __name__ == "__main__":
fire.Fire({"convert": convert, "help": help, "--convert": convert})