This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
4
tests/fixtures/fake-model/config.json
vendored
Normal file
4
tests/fixtures/fake-model/config.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"model_type": "llama",
|
||||
"architectures": ["LlamaForCausalLM"]
|
||||
}
|
||||
3
tests/fixtures/fake-model/tokenizer.json
vendored
Normal file
3
tests/fixtures/fake-model/tokenizer.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"version": "1.0"
|
||||
}
|
||||
3
tests/fixtures/fake-model/tokenizer_config.json
vendored
Normal file
3
tests/fixtures/fake-model/tokenizer_config.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"tokenizer_class": "LlamaTokenizer"
|
||||
}
|
||||
96
tests/test_contract.py
Normal file
96
tests/test_contract.py
Normal file
@@ -0,0 +1,96 @@
|
||||
import os
|
||||
import subprocess
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class EngineContractTest(unittest.TestCase):
|
||||
def test_locked_values_match(self) -> None:
|
||||
example = (ROOT / "task_info.env.example").read_text(encoding="utf-8")
|
||||
workflow_env = (ROOT / ".gitea/workflows/task_info.env").read_text(encoding="utf-8")
|
||||
dockerfile = (ROOT / "Dockerfile").read_text(encoding="utf-8")
|
||||
self.assertIn("FRAMEWORK=vllm_0_17_0_corex_4_4_0", example)
|
||||
self.assertIn("GPU_TYPE=Iluvatar_bi-150", example)
|
||||
self.assertIn("FRAMEWORK=vllm_0_17_0_corex_4_4_0", workflow_env)
|
||||
self.assertIn("GPU_TYPE=Iluvatar_bi-150", workflow_env)
|
||||
self.assertIn("FRAMEWORK=vllm_0_17_0_corex_4_4_0", dockerfile)
|
||||
self.assertIn("GPU_TYPE=Iluvatar_bi-150", dockerfile)
|
||||
self.assertIn(
|
||||
"registry.iluvatar.com.cn:10443/customer/sz/vllm0.17.0-4.4.0-x86:v4.1",
|
||||
dockerfile,
|
||||
)
|
||||
self.assertNotIn("FRAMEWORK=vllm_fix_tokenizer", example)
|
||||
self.assertNotIn("FRAMEWORK=vllm_fix_tokenizer", workflow_env)
|
||||
self.assertNotIn("FRAMEWORK=vllm_fix_tokenizer", dockerfile)
|
||||
|
||||
def test_ci_does_not_submit_platform_task(self) -> None:
|
||||
workflow = (ROOT / ".gitea/workflows/build.yml").read_text(encoding="utf-8")
|
||||
self.assertNotIn("adminApi", workflow)
|
||||
self.assertNotIn("modelhub.org.cn", workflow)
|
||||
self.assertIn("build-only", workflow)
|
||||
self.assertIn('tags:', workflow)
|
||||
|
||||
def test_no_zip_or_contest_targets(self) -> None:
|
||||
makefile = (ROOT / "Makefile").read_text(encoding="utf-8")
|
||||
self.assertNotIn("package", makefile)
|
||||
self.assertNotIn("submit", makefile)
|
||||
self.assertFalse((ROOT / "contest").exists())
|
||||
self.assertFalse((ROOT / "computility-run.yaml").exists())
|
||||
|
||||
def test_dry_run_prints_plan(self) -> None:
|
||||
env = os.environ.copy()
|
||||
env.pop("FRAMEWORK", None)
|
||||
env.pop("GPU_TYPE", None)
|
||||
env.pop("MODEL_DIR", None)
|
||||
completed = subprocess.run(
|
||||
["bash", "scripts/entrypoint.sh", "--dry-run"],
|
||||
cwd=ROOT,
|
||||
env=env,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
check=False,
|
||||
)
|
||||
self.assertEqual(completed.returncode, 0, completed.stderr)
|
||||
self.assertIn("FRAMEWORK=vllm_0_17_0_corex_4_4_0", completed.stdout)
|
||||
self.assertIn("GPU_TYPE=Iluvatar_bi-150", completed.stdout)
|
||||
self.assertIn("python3 -m vllm.entrypoints.openai.api_server", completed.stdout)
|
||||
self.assertIn("dry-run: 不启动进程,不访问平台", completed.stdout)
|
||||
self.assertNotIn("modelhub.org.cn", completed.stdout)
|
||||
self.assertIn("<|user|>", completed.stdout)
|
||||
|
||||
def test_dry_run_rejects_forbidden_framework(self) -> None:
|
||||
env = os.environ.copy()
|
||||
env["FRAMEWORK"] = "vllm_fix_tokenizer"
|
||||
completed = subprocess.run(
|
||||
["bash", "scripts/entrypoint.sh", "--dry-run"],
|
||||
cwd=ROOT,
|
||||
env=env,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
check=False,
|
||||
)
|
||||
self.assertNotEqual(completed.returncode, 0)
|
||||
self.assertIn("vllm_fix_tokenizer", completed.stderr)
|
||||
|
||||
def test_tokenizer_patch_stays_off(self) -> None:
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
import apply_patches
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
model = Path(tmp) / "model"
|
||||
shutil.copytree(ROOT / "tests" / "fixtures" / "fake-model", model)
|
||||
result = apply_patches.apply_all(
|
||||
model,
|
||||
(ROOT / "patches/chat_template/default_chat_template.jinja").read_text(encoding="utf-8"),
|
||||
apply_patches.load_whitelist(ROOT / "patches/architectures/whitelist.txt"),
|
||||
{},
|
||||
)
|
||||
self.assertFalse(result["tokenizer"]["applied"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
67
tests/test_r2_chat_template.py
Normal file
67
tests/test_r2_chat_template.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import json
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import apply_patches
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
FIXTURE = ROOT / "tests" / "fixtures" / "fake-model"
|
||||
TEMPLATE = (ROOT / "patches" / "chat_template" / "default_chat_template.jinja").read_text(encoding="utf-8")
|
||||
WHITELIST = apply_patches.load_whitelist(ROOT / "patches" / "architectures" / "whitelist.txt")
|
||||
ALIASES = apply_patches.load_aliases(ROOT / "patches" / "architectures" / "aliases.json")
|
||||
|
||||
|
||||
class ChatTemplateTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.model = Path(self.tmp.name) / "model"
|
||||
shutil.copytree(FIXTURE, self.model)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self.tmp.cleanup()
|
||||
|
||||
def test_injects_missing_template(self) -> None:
|
||||
result = apply_patches.apply_chat_template(self.model, TEMPLATE)
|
||||
self.assertTrue(result["injected"])
|
||||
data = json.loads((self.model / "tokenizer_config.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(data["chat_template"], TEMPLATE)
|
||||
|
||||
def test_keeps_existing_template(self) -> None:
|
||||
path = self.model / "tokenizer_config.json"
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
data["chat_template"] = "already-set"
|
||||
path.write_text(json.dumps(data), encoding="utf-8")
|
||||
result = apply_patches.apply_chat_template(self.model, TEMPLATE)
|
||||
self.assertFalse(result["injected"])
|
||||
kept = json.loads(path.read_text(encoding="utf-8"))
|
||||
self.assertEqual(kept["chat_template"], "already-set")
|
||||
|
||||
def test_force_overwrites_template(self) -> None:
|
||||
path = self.model / "tokenizer_config.json"
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
data["chat_template"] = "already-set"
|
||||
path.write_text(json.dumps(data), encoding="utf-8")
|
||||
result = apply_patches.apply_chat_template(self.model, TEMPLATE, force=True)
|
||||
self.assertTrue(result["injected"])
|
||||
|
||||
def test_missing_tokenizer_config_fails(self) -> None:
|
||||
(self.model / "tokenizer_config.json").unlink()
|
||||
with self.assertRaises(apply_patches.PatchError):
|
||||
apply_patches.apply_chat_template(self.model, TEMPLATE)
|
||||
|
||||
def test_preview_renders_roles(self) -> None:
|
||||
text = apply_patches.preview_default_template(
|
||||
[
|
||||
{"role": "system", "content": "你是助手"},
|
||||
{"role": "user", "content": "你好"},
|
||||
]
|
||||
)
|
||||
self.assertIn("<|user|>\n你好<|end|>\n", text)
|
||||
self.assertTrue(text.endswith("<|assistant|>\n"))
|
||||
self.assertIn("message['role']", TEMPLATE)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
65
tests/test_r4_architectures.py
Normal file
65
tests/test_r4_architectures.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import json
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import apply_patches
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
FIXTURE = ROOT / "tests" / "fixtures" / "fake-model"
|
||||
WHITELIST = apply_patches.load_whitelist(ROOT / "patches" / "architectures" / "whitelist.txt")
|
||||
|
||||
|
||||
class ArchitectureTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.model = Path(self.tmp.name) / "model"
|
||||
shutil.copytree(FIXTURE, self.model)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self.tmp.cleanup()
|
||||
|
||||
def write_architectures(self, names: list[str]) -> None:
|
||||
path = self.model / "config.json"
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
data["architectures"] = names
|
||||
path.write_text(json.dumps(data), encoding="utf-8")
|
||||
|
||||
def test_whitelist_passes(self) -> None:
|
||||
result = apply_patches.apply_architectures(self.model, WHITELIST, {}, "fail")
|
||||
self.assertEqual(result["architectures"], ["LlamaForCausalLM"])
|
||||
self.assertFalse(result["changed"])
|
||||
|
||||
def test_unknown_architecture_fails(self) -> None:
|
||||
self.write_architectures(["ExampleForCausalLM"])
|
||||
with self.assertRaises(apply_patches.PatchError) as ctx:
|
||||
apply_patches.apply_architectures(self.model, WHITELIST, {}, "fail")
|
||||
self.assertIn("ExampleForCausalLM", str(ctx.exception))
|
||||
|
||||
def test_unknown_architecture_skip_keeps_name(self) -> None:
|
||||
self.write_architectures(["ExampleForCausalLM"])
|
||||
result = apply_patches.apply_architectures(self.model, WHITELIST, {}, "skip")
|
||||
self.assertEqual(result["skipped"], ["ExampleForCausalLM"])
|
||||
data = json.loads((self.model / "config.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(data["architectures"], ["ExampleForCausalLM"])
|
||||
|
||||
def test_alias_rewrites_to_whitelist(self) -> None:
|
||||
self.write_architectures(["LlamaForCausalLMAlias"])
|
||||
result = apply_patches.apply_architectures(
|
||||
self.model,
|
||||
WHITELIST,
|
||||
{"LlamaForCausalLMAlias": "LlamaForCausalLM"},
|
||||
"fail",
|
||||
)
|
||||
self.assertEqual(result["rewritten"], ["LlamaForCausalLMAlias->LlamaForCausalLM"])
|
||||
data = json.loads((self.model / "config.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(data["architectures"], ["LlamaForCausalLM"])
|
||||
|
||||
def test_shipped_aliases_are_empty(self) -> None:
|
||||
aliases = apply_patches.load_aliases(ROOT / "patches" / "architectures" / "aliases.json")
|
||||
self.assertEqual(aliases, {})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user