33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
"""Benchmark validity checks must survive python -O."""
|
|
import math
|
|
import unittest
|
|
from benchmark import Session, require, vectors
|
|
|
|
|
|
class Contract(unittest.TestCase):
|
|
def test_bad_vectors_rejected(self):
|
|
for result in ([], [[1.0]], [[math.nan] * 768], [[0.0] * 768]):
|
|
with self.subTest(result_length=len(result)), self.assertRaises(ValueError):
|
|
vectors({"vectors": result}, 1)
|
|
|
|
def test_real_shape_normalized_vector_accepted(self):
|
|
vector = [1.0] + [0.0] * 767
|
|
self.assertEqual(vectors({"vectors": [vector]}, 1), [vector])
|
|
|
|
def test_cpu_contract_cannot_be_bypassed(self):
|
|
class FakeSession:
|
|
def send(self, value):
|
|
pass
|
|
def next(self):
|
|
return {"ok": True, "device": "gpu"}
|
|
with self.assertRaises(ValueError):
|
|
Session.worker(FakeSession(), {"op": "query"})
|
|
|
|
def test_explicit_guard_survives_optimization(self):
|
|
with self.assertRaises(ValueError):
|
|
require(False, "invalid asset")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|