interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / conformance / test_max_runner_metadata.py

117 lines · 4584 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Host-only MAX runner metadata regression tests. No MAX installation or device.
4
5Run: python3 conformance/test_max_runner_metadata.py
6MAX_RUNNER_METADATA_SOURCE may select a private source mutant for this test.
7Import-only doubles exercise the real helper without constructing a graph,
8session, buffer or device. This does not establish MAX runtime compatibility.
9"""
10import contextlib
11import io
12import os
13from pathlib import Path
14import runpy
15import sys
16import types
17import unittest
18from unittest.mock import patch
19
20
21RUNNER = Path(os.environ.get(
22 "MAX_RUNNER_METADATA_SOURCE", Path(__file__).with_name("max_runner.py")))
23
24
25class UnexpectedConstruction:
26 def __init__(self, *args, **kwargs):
27 raise AssertionError("metadata test constructed a MAX runtime object")
28
29
30@contextlib.contextmanager
31def runner(module):
32 modules = {"max": module, "numpy": types.ModuleType("numpy")}
33 for name, attrs in {
34 "max.driver": ("Accelerator", "CPU", "Buffer", "accelerator_count"),
35 "max.dtype": ("DType",),
36 "max.engine": ("InferenceSession",),
37 "max.graph": ("DeviceRef", "Graph", "TensorType", "ops"),
38 }.items():
39 stub = types.ModuleType(name)
40 for attr in attrs:
41 setattr(stub, attr, UnexpectedConstruction)
42 modules[name] = stub
43 with patch.dict(sys.modules, modules), patch.object(sys, "path", sys.path.copy()):
44 yield runpy.run_path(str(RUNNER))
45
46
47class CpuRunHidesGpuTests(unittest.TestCase):
48 """A --device cpu run must refuse before constructing any MAX object while
49 an accelerator is visible (MAX 26.7.0 Graph() opens a visible GPU)."""
50
51 def run_cpu(self, visible):
52 with runner(types.ModuleType("max")) as namespace:
53 sys.modules["max.driver"].accelerator_count = lambda: visible
54 argv = ["max_runner.py", "--fixture", os.devnull, "--device", "cpu",
55 "--out", os.devnull]
56 with patch.object(sys, "argv", argv), \
57 patch("builtins.open", side_effect=AssertionError("fixture read")), \
58 patch("sys.stderr", new_callable=io.StringIO) as err:
59 try:
60 rc = namespace["main"]()
61 except AssertionError as e:
62 return "constructed: " + str(e), err.getvalue()
63 return rc, err.getvalue()
64
65 def test_visible_accelerator_is_refused(self):
66 for visible in (1, 2):
67 with self.subTest(visible=visible):
68 rc, err = self.run_cpu(visible)
69 self.assertEqual(rc, 2)
70 self.assertIn("CUDA_VISIBLE_DEVICES=", err)
71
72 def test_hidden_accelerator_proceeds_to_cpu(self):
73 rc, _ = self.run_cpu(0)
74 self.assertTrue(str(rc).startswith("constructed: "), rc)
75
76
77class MaxVersionMetadataTests(unittest.TestCase):
78 def test_observed_version_is_preserved(self):
79 for version in ("26.7.0", "26.8.0", "26.8.0.dev1+local"):
80 with self.subTest(version=version):
81 module = types.ModuleType("max")
82 module.__version__ = version
83 with runner(module) as namespace:
84 self.assertEqual(namespace["_max_version"](), version)
85
86 def test_missing_version_is_unknown(self):
87 with runner(types.ModuleType("max")) as namespace:
88 self.assertEqual(namespace["_max_version"](), "unknown")
89
90 def test_invalid_version_is_unknown(self):
91 for version in (None, "", " ", 267, True, (26, 7, 0)):
92 with self.subTest(version=version):
93 module = types.ModuleType("max")
94 module.__version__ = version
95 with runner(module) as namespace:
96 self.assertEqual(namespace["_max_version"](), "unknown")
97
98 def test_unavailable_module_is_unknown(self):
99 with runner(types.ModuleType("max")) as namespace:
100 with patch.dict(sys.modules, {"max": None}):
101 self.assertEqual(namespace["_max_version"](), "unknown")
102
103 def test_unreadable_version_is_unknown(self):
104 module = types.ModuleType("max")
105
106 def unavailable(name):
107 if name == "__version__":
108 raise RuntimeError("version metadata unavailable")
109 raise AttributeError(name)
110
111 module.__getattr__ = unavailable
112 with runner(module) as namespace:
113 self.assertEqual(namespace["_max_version"](), "unknown")
114
115
116if __name__ == "__main__":
117 unittest.main(verbosity=2)