Download tasks/candidate-0758-ml-inference/tests/test_outputs.py from FineEnvs/MiMo-V2.6-RL-harbor-terminal: direct link, hf CLI and curl.
- Browser
- Download file 6.28 kB
-
https://huggingface.co/datasets/FineEnvs/MiMo-V2.6-RL-harbor-terminal/resolve/5708f7d17ec6ffe77bc9ab4585072b4ac0acfb13/tasks/candidate-0758-ml-inference/tests/test_outputs.py
- Command line
-
hf download hf://datasets/FineEnvs/MiMo-V2.6-RL-harbor-terminal@5708f7d17ec6ffe77bc9ab4585072b4ac0acfb13/tasks/candidate-0758-ml-inference/tests/test_outputs.py
-
curl -L -o test_outputs.py https://huggingface.co/datasets/FineEnvs/MiMo-V2.6-RL-harbor-terminal/resolve/5708f7d17ec6ffe77bc9ab4585072b4ac0acfb13/tasks/candidate-0758-ml-inference/tests/test_outputs.py
6.28 kB
| from __future__ import annotations | |
| import hashlib | |
| import importlib.util | |
| import json | |
| import os | |
| import re | |
| import sys | |
| import types | |
| from pathlib import Path | |
| import numpy as np | |
| APP = Path(os.environ.get("TBENCH_APP", "/app")) | |
| TESTS = Path(os.environ.get("TBENCH_TESTS", "/tests")) | |
| FIX = TESTS / "fixtures" | |
| BASE_HASHES = { | |
| "vendor/onnx/onnx/reference/ops/op_tensor_scatter.py": "2f12049a0582cfb4e5c9017c189b1ea27d872d550b97cd5156008d79b9fbed5c", | |
| "vendor/onnx/onnx/defs/tensor/defs.cc": "3f418e462e60b2fedaebe7a4ce2311ece098c961422ffc9e397bbd8be9697321", | |
| "vendor/onnx/onnx/reference/op_run.py": "dbe6f43327f69396d27d48b121f54361f55d9ea67b793cc121314ee3ba389fbd", | |
| "vendor/onnx/onnx/reference/ops/_helpers.py": "5b588246f91181be2ea9c7b93055493a6d08aa4fc5b6f147b7b7c2f58786b59d", | |
| "vendor/onnx/onnx/reference/ops/_op.py": "5f6682ef95f32d46e8f8e01a47a18867f938e7419a61538426f0c3ca3e888965", | |
| } | |
| def sha256(path: Path) -> str: | |
| return hashlib.sha256(path.read_bytes()).hexdigest() | |
| def load_operator(): | |
| onnx_mod = types.ModuleType("onnx") | |
| reference_mod = types.ModuleType("onnx.reference") | |
| op_run_mod = types.ModuleType("onnx.reference.op_run") | |
| op_run_mod.OpRun = type("OpRun", (), {}) | |
| sys.modules.setdefault("onnx", onnx_mod) | |
| sys.modules.setdefault("onnx.reference", reference_mod) | |
| sys.modules["onnx.reference.op_run"] = op_run_mod | |
| source = APP / "vendor/onnx/onnx/reference/ops/op_tensor_scatter.py" | |
| spec = importlib.util.spec_from_file_location("test_tensor_scatter", source) | |
| assert spec and spec.loader | |
| module = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(module) | |
| return module.TensorScatter() | |
| def recompute(case): | |
| past = np.asarray(case["past_cache"], dtype=np.int64) | |
| update = np.asarray(case["update"], dtype=np.int64) | |
| indices = np.asarray(case["write_indices"], dtype=np.int64) | |
| axis = int(case["axis"]) % past.ndim | |
| assert past.ndim == update.ndim == 2 | |
| assert all(past.shape[i] == update.shape[i] for i in range(2) if i != axis) | |
| assert update.shape[axis] <= past.shape[axis] | |
| out = past.copy() | |
| for prefix in np.ndindex(past.shape[:axis]): | |
| b = prefix[0] | |
| for j in range(update.shape[axis]): | |
| pos = int(indices[b]) + j | |
| if case["mode"] == "circular": | |
| pos %= past.shape[axis] | |
| elif case["mode"] != "linear": | |
| raise ValueError("unsupported mode") | |
| out[prefix + (pos,)] = update[prefix + (j,)] | |
| return out | |
| def test_gate_artifact_schema_and_integrity(): | |
| artifact = APP / "output.json" | |
| assert artifact.is_file() | |
| payload = json.loads(artifact.read_text()) | |
| assert payload["output_schema_version"] == "tensor_scatter_inference.v1" | |
| assert [r["name"] for r in payload["results"]] == ["batch_wrap", "linear_append"] | |
| public = json.loads((APP / "fixtures/kv_cases.json").read_text()) | |
| expected = [recompute(case).tolist() for case in public["cases"]] | |
| assert [r["values"] for r in payload["results"]] == expected | |
| assert sha256(APP / "vendor/onnx/onnx/reference/ops/op_tensor_scatter.py") != BASE_HASHES["vendor/onnx/onnx/reference/ops/op_tensor_scatter.py"] | |
| assert sha256(APP / "vendor/onnx/onnx/defs/tensor/defs.cc") != BASE_HASHES["vendor/onnx/onnx/defs/tensor/defs.cc"] | |
| assert sha256(APP / "fixtures/kv_cases.json") == "3f5542fc823784faae35d727d3bf35bdff5760320ffc27b47ff27f57424648b0" | |
| for rel, digest in BASE_HASHES.items(): | |
| if rel.endswith("op_tensor_scatter.py") or rel.endswith("defs.cc"): | |
| continue | |
| assert sha256(APP / rel) == digest | |
| def test_core_circular_batch_identity(): | |
| operator = load_operator() | |
| cases = json.loads((FIX / "hidden_cases.json").read_text())["cases"] | |
| actual = operator._run( | |
| np.asarray(cases[0]["past_cache"], dtype=np.int64), | |
| np.asarray(cases[0]["update"], dtype=np.int64), | |
| np.asarray(cases[0]["write_indices"], dtype=np.int64), | |
| cases[0]["mode"], | |
| cases[0]["axis"], | |
| )[0] | |
| expected = recompute(cases[0]) | |
| assert actual.shape == (6, 4) | |
| assert np.array_equal(actual, expected) | |
| assert actual[:, 0].tolist() == [12, 23, -30, 41, 52, 63] | |
| def test_core_linear_and_negative_axis(): | |
| operator = load_operator() | |
| cases = json.loads((FIX / "hidden_cases.json").read_text())["cases"] | |
| for case in cases[1:]: | |
| actual = operator._run( | |
| np.asarray(case["past_cache"], dtype=np.int64), | |
| np.asarray(case["update"], dtype=np.int64), | |
| np.asarray(case["write_indices"], dtype=np.int64), | |
| case["mode"], | |
| case["axis"], | |
| )[0] | |
| assert np.array_equal(actual, recompute(case)) | |
| assert operator._run(np.zeros((1, 4), dtype=np.int64), np.array([[9, 8]]), np.array([3]), "circular", -1)[0].tolist() == [[8, 0, 0, 9]] | |
| def test_edge_shape_and_mode_errors(): | |
| operator = load_operator() | |
| with np.testing.assert_raises(ValueError): | |
| operator._run(np.zeros((2, 4), dtype=np.int64), np.zeros((3, 3), dtype=np.int64), np.array([0, 0]), "circular", 1) | |
| with np.testing.assert_raises(ValueError): | |
| operator._run(np.zeros((2, 4), dtype=np.int64), np.zeros((2, 5), dtype=np.int64), np.array([0, 0]), "linear", 1) | |
| with np.testing.assert_raises(ValueError): | |
| operator._run(np.zeros((2, 4), dtype=np.int64), np.zeros((2, 2), dtype=np.int64), np.array([0, 0]), "diagonal", 1) | |
| def test_edge_cpp_contract_and_python_build(): | |
| source = (APP / "vendor/onnx/onnx/defs/tensor/defs.cc").read_text() | |
| block = source[source.index("static constexpr const char* TensorScatter_ver24_doc"):source.index("ONNX_OPERATOR_SET_SCHEMA(\n TensorScatter", source.index("static constexpr const char* TensorScatter_ver24_doc"))] | |
| assert "np.asarray(cache_idx)" not in block | |
| assert re.search(r"write_indices\[batch_idx\]\s*\+\s*sequence_idx\)\s*%\s*max_sequence_length", block) | |
| compile_targets = [APP / "run_kv_inference.py", APP / "vendor/onnx/onnx/reference/ops/op_tensor_scatter.py"] | |
| import py_compile | |
| for target in compile_targets: | |
| py_compile.compile(str(target), doraise=True) | |
| assert sha256(APP / "run_kv_inference.py") == "3d42dd5e67e02f193342176ddcdcddd6819499bc871eb16b76a792cb840d1d58" | |