File size: 7,808 Bytes
f440f03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Tests Maris Hugging Face compatibility layer."""

from __future__ import annotations

import json
import sys
import types
from pathlib import Path

from maris_core.training.hf_compat import (
    MARIS_COMPATIBILITY_ARTIFACT_NAME,
    apply_maris_compatibility_identity,
    maris_hf_compatible_path,
    write_maris_compatibility_artifact,
)


def test_write_maris_compatibility_artifact_sanitizes_loader_fields(tmp_path: Path) -> None:
    output_dir = tmp_path / "model"
    output_dir.mkdir()
    (output_dir / "config.json").write_text(
        json.dumps(
            {
                "_name_or_path": "MarisUK/maris-ai-master",
                "model_type": "qwen2",
                "architectures": ["Qwen2ForCausalLM"],
                "tokenizer_class": "Qwen2TokenizerFast",
                "auto_map": {"AutoModelForCausalLM": "qwen2.modeling_qwen2.Qwen2ForCausalLM"},
            }
        ),
        encoding="utf-8",
    )
    (output_dir / "tokenizer_config.json").write_text(
        json.dumps({"tokenizer_class": "Qwen2TokenizerFast"}),
        encoding="utf-8",
    )
    (output_dir / "adapter_config.json").write_text(
        json.dumps(
            {
                "base_model_class": "Qwen2ForCausalLM",
                "parent_library": "transformers.models.qwen2.modeling_qwen2",
            }
        ),
        encoding="utf-8",
    )

    write_maris_compatibility_artifact(output_dir, maris_model_id="MarisUK/maris-ai-master")
    apply_maris_compatibility_identity(output_dir)

    manifest = json.loads(
        (output_dir / MARIS_COMPATIBILITY_ARTIFACT_NAME).read_text(encoding="utf-8")
    )
    config_payload = json.loads((output_dir / "config.json").read_text(encoding="utf-8"))
    tokenizer_payload = json.loads(
        (output_dir / "tokenizer_config.json").read_text(encoding="utf-8")
    )
    adapter_payload = json.loads((output_dir / "adapter_config.json").read_text(encoding="utf-8"))

    assert manifest["artifact_type"] == "maris-hf-compatibility"
    assert sorted(manifest["artifacts"]) == [
        "adapter_config.json",
        "config.json",
        "tokenizer_config.json",
    ]
    assert config_payload["model_type"] == "maris"
    assert config_payload["architectures"] == ["MarisCompatibleCausalLM"]
    assert tokenizer_payload["tokenizer_class"] == "MarisCompatibleTokenizer"
    assert adapter_payload["base_model_class"] == "MarisCompatibleCausalLM"
    assert adapter_payload["parent_library"] == "maris.compat"


def test_maris_hf_compatible_path_restores_remote_snapshot(monkeypatch, tmp_path: Path) -> None:
    snapshot_dir = tmp_path / "snapshot"
    snapshot_dir.mkdir()
    (snapshot_dir / "config.json").write_text(
        json.dumps(
            {
                "_name_or_path": "MarisUK/maris-ai-master",
                "model_type": "qwen2",
                "architectures": ["Qwen2ForCausalLM"],
            }
        ),
        encoding="utf-8",
    )
    write_maris_compatibility_artifact(snapshot_dir, maris_model_id="MarisUK/maris-ai-master")
    apply_maris_compatibility_identity(snapshot_dir)

    monkeypatch.setitem(
        sys.modules,
        "huggingface_hub",
        types.SimpleNamespace(
            snapshot_download=lambda **kwargs: (
                str(snapshot_dir)
                if kwargs["repo_id"] == "MarisUK/maris-ai-master" and kwargs["repo_type"] == "model"
                else None
            )
        ),
    )
    monkeypatch.setenv("MARIS_HF_COMPAT_ALLOW_REMOTE_SNAPSHOT", "true")

    with maris_hf_compatible_path("MarisUK/maris-ai-master") as compatible_path:
        restored_dir = Path(compatible_path)
        assert restored_dir != snapshot_dir
        restored_config = json.loads(
            restored_dir.joinpath("config.json").read_text(encoding="utf-8")
        )
        assert restored_config["model_type"] == "qwen2"
        assert restored_config["architectures"] == ["Qwen2ForCausalLM"]

    original_config = json.loads((snapshot_dir / "config.json").read_text(encoding="utf-8"))
    assert original_config["model_type"] == "maris"


def test_maris_hf_compatible_path_can_force_remote_snapshot_restore(
    monkeypatch, tmp_path: Path
) -> None:
    snapshot_dir = tmp_path / "snapshot"
    snapshot_dir.mkdir()
    (snapshot_dir / "config.json").write_text(
        json.dumps(
            {
                "_name_or_path": "MarisUK/maris-ai-master",
                "model_type": "qwen2",
                "architectures": ["Qwen2ForCausalLM"],
            }
        ),
        encoding="utf-8",
    )
    write_maris_compatibility_artifact(snapshot_dir, maris_model_id="MarisUK/maris-ai-master")
    apply_maris_compatibility_identity(snapshot_dir)

    monkeypatch.setitem(
        sys.modules,
        "huggingface_hub",
        types.SimpleNamespace(
            snapshot_download=lambda **kwargs: (
                str(snapshot_dir)
                if kwargs["repo_id"] == "MarisUK/maris-ai-master" and kwargs["repo_type"] == "model"
                else None
            )
        ),
    )

    with maris_hf_compatible_path(
        "MarisUK/maris-ai-master", allow_remote_snapshot=True
    ) as compatible_path:
        restored_dir = Path(compatible_path)
        restored_config = json.loads(
            restored_dir.joinpath("config.json").read_text(encoding="utf-8")
        )
        assert restored_config["model_type"] == "qwen2"
        assert restored_config["architectures"] == ["Qwen2ForCausalLM"]


def test_maris_hf_compatible_path_restores_custom_runtime_snapshot(
    monkeypatch, tmp_path: Path
) -> None:
    snapshot_dir = tmp_path / "custom-runtime"
    snapshot_dir.mkdir()
    (snapshot_dir / "config.json").write_text(
        json.dumps(
            {
                "_name_or_path": "custom-user/maris-runtime",
                "model_type": "llama",
                "architectures": ["LlamaForCausalLM"],
            }
        ),
        encoding="utf-8",
    )
    write_maris_compatibility_artifact(snapshot_dir, maris_model_id="custom-user/maris-runtime")
    apply_maris_compatibility_identity(snapshot_dir)

    monkeypatch.setitem(
        sys.modules,
        "huggingface_hub",
        types.SimpleNamespace(
            snapshot_download=lambda **kwargs: (
                str(snapshot_dir)
                if kwargs["repo_id"] == "custom-user/maris-runtime"
                and kwargs["repo_type"] == "model"
                else None
            )
        ),
    )
    monkeypatch.setenv("MARIS_RUNTIME_COMPAT_ALLOW_REMOTE_SNAPSHOT", "true")

    with maris_hf_compatible_path("custom-user/maris-runtime") as compatible_path:
        restored_dir = Path(compatible_path)
        restored_config = json.loads(
            restored_dir.joinpath("config.json").read_text(encoding="utf-8")
        )
        assert restored_config["model_type"] == "llama"
        assert restored_config["architectures"] == ["LlamaForCausalLM"]


def test_maris_hf_compatible_path_returns_remote_snapshot_path_without_restore_artifact(
    monkeypatch,
    tmp_path: Path,
) -> None:
    snapshot_dir = tmp_path / "plain-runtime"
    snapshot_dir.mkdir()
    (snapshot_dir / "config.json").write_text(json.dumps({"model_type": "llama"}), encoding="utf-8")

    monkeypatch.setitem(
        sys.modules,
        "huggingface_hub",
        types.SimpleNamespace(
            snapshot_download=lambda **kwargs: (
                str(snapshot_dir)
                if kwargs["repo_id"] == "custom-user/plain-runtime"
                and kwargs["repo_type"] == "model"
                else None
            )
        ),
    )
    monkeypatch.setenv("MARIS_RUNTIME_COMPAT_ALLOW_REMOTE_SNAPSHOT", "true")

    with maris_hf_compatible_path("custom-user/plain-runtime") as compatible_path:
        assert compatible_path == str(snapshot_dir)