ahmednasr's picture
download
raw
2.19 kB
"""Save a local copy of the evaluated checkpoint, pinned to one revision.
Runs on CPU; no GPU is needed. Usage:
python save_model.py # download only
python save_model.py --verify # also load on CPU and run one prompt
"""
import argparse
import json
from pathlib import Path
from huggingface_hub import HfApi, snapshot_download
ROOT = Path(__file__).resolve().parent
model_id = "Qwen/Qwen3.5-4B"
REVISION = "851bf6e806efd8d0a36b00ddf55e13ccb7b8cd0a"
def verify_on_cpu(model_dir):
import torch
from transformers import AutoModelForImageTextToText, AutoProcessor
processor = AutoProcessor.from_pretrained(model_dir)
model = AutoModelForImageTextToText.from_pretrained(
model_dir,
dtype=torch.bfloat16,
device_map="cpu",
).eval()
inputs = processor.apply_chat_template(
[{
"role": "user",
"content": [{"type": "text", "text": "What is 19:47 plus 5 hours 28 minutes?"}],
}],
tokenize=True,
return_dict=True,
return_tensors="pt",
add_generation_prompt=True,
enable_thinking=False,
)
with torch.inference_mode():
output = model.generate(**inputs, max_new_tokens=32, do_sample=False)
tokens = output[0, inputs["input_ids"].shape[1]:]
print("CPU check:", processor.tokenizer.decode(tokens, skip_special_tokens=True))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--verify", action="store_true")
args = parser.parse_args()
# Fails early if the revision does not exist on the Hub.
revision = HfApi().model_info(model_id, revision=REVISION).sha
# Download directly into the project; reruns skip completed files.
model_dir = snapshot_download(
repo_id=model_id,
revision=revision,
local_dir=ROOT / "model",
)
(ROOT / "model_reference.json").write_text(
json.dumps({"model_id": model_id, "revision": revision}, indent=2),
encoding="utf-8",
)
print("Snapshot saved to:", model_dir)
print("Revision:", revision)
if args.verify:
verify_on_cpu(model_dir)
if __name__ == "__main__":
main()

Xet Storage Details

Size:
2.19 kB
·
Xet hash:
d04e2ca9868c1d0de3986dc451d3515e761199c4e9393b6353d569907c3195e9

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.