File size: 3,713 Bytes
e88b8ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Exercise the actual publication staging code without contacting the Hub."""

import fnmatch
import hashlib
import json
from pathlib import Path
import shutil
import subprocess
import sys
import textwrap
from types import SimpleNamespace

import pytest


def _stage(tmp_path, monkeypatch, *, chat=False, manifest=True):
    root = tmp_path / "source"
    remote = tmp_path / "remote"
    saved = tmp_path / "download"
    out = tmp_path / "export"
    root.mkdir()
    remote.mkdir()
    saved.mkdir()
    (root / "README.md").write_text("# Current source\n")
    (remote / "README.md").write_text("---\nlicense: mit\n---\nOld card\n")
    folders = ["text", "physics"] + (["text-chat"] if chat else [])
    for name in folders:
        folder = remote / name
        folder.mkdir()
        (folder / "model.safetensors").write_bytes(b"saved-weight-bytes")
        if name != "text-chat" or manifest:
            (folder / "manifest.json").write_text("{}")
    files = [p for p in remote.rglob("*") if p.is_file()]
    info = SimpleNamespace(sha="hub-revision", siblings=[
        SimpleNamespace(rfilename=p.relative_to(remote).as_posix(),
                        blob_id=hashlib.sha1(p.read_bytes()).hexdigest(), size=p.stat().st_size) for p in files
    ])

    def snapshot_download(repo, *, repo_type, revision, allow_patterns):
        assert revision == info.sha
        for src in files:
            name = src.relative_to(remote).as_posix()
            if any(fnmatch.fnmatch(name, pattern) for pattern in allow_patterns):
                dest = saved / name
                dest.parent.mkdir(parents=True, exist_ok=True)
                shutil.copyfile(src, dest)
        return str(saved)

    monkeypatch.setitem(sys.modules, "huggingface_hub", SimpleNamespace(
        HfApi=lambda: SimpleNamespace(repo_info=lambda *a, **kw: info),
        snapshot_download=snapshot_download,
    ))
    monkeypatch.setattr(subprocess, "check_output", lambda *a, **kw: b"README.md\0")
    for key, value in {
        "HF_TOKEN": "test-only", "GITHUB_WORKSPACE": str(root), "EXPORT_DIR": str(out),
        "HF_REPO_ID": "owner/model", "HF_REPO_TYPE": "model",
        "GITHUB_REPOSITORY": "owner/source", "GITHUB_SHA": "source-revision",
    }.items():
        monkeypatch.setenv(key, value)
    workflow = (Path(__file__).parents[1] / ".github/workflows/sync-to-hub.yml").read_text()
    code = workflow.split("python - <<'PY'\n", 1)[1].split("\n          PY", 1)[0]
    exec(compile(textwrap.dedent(code), "hub-staging", "exec"), {})
    return out


@pytest.mark.parametrize("chat", [False, True])
def test_sync_preserves_published_checkpoint_bytes(tmp_path, monkeypatch, chat):
    out = _stage(tmp_path, monkeypatch, chat=chat)
    hashes = json.loads(out.with_suffix(".hashes.json").read_text())
    for folder in ["text", "physics"]:
        name = f"{folder}/model.safetensors"
        assert (out / name).read_bytes() == b"saved-weight-bytes"
        assert hashes[name] == hashlib.sha256(b"saved-weight-bytes").hexdigest()
    assert (out / "README.md").read_text() == "---\nlicense: mit\n---\n\n# Current source\n"
    assert json.loads((out / "source_snapshot.json").read_text())["github_commit"] == "source-revision"
    if chat:
        preserved = json.loads(out.with_suffix(".preserved.json").read_text())
        assert preserved["text-chat/model.safetensors"]["size"] == len(b"saved-weight-bytes")
        assert not (out / "text-chat").exists()  # no multi-GB copy required to preserve it


def test_sync_never_mirrors_deletions_of_hub_owned_weights():
    workflow = (Path(__file__).parents[1] / ".github/workflows/sync-to-hub.yml").read_text()
    assert "delete_removed: 'false'" in workflow