@trillboards/edge-sdk 0.2.92 → 0.2.94

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ """The committed twin-parity measurement must stay a MEASUREMENT.
2
+
3
+ `bench/litert-twin-parity.json` is the only place the box-scale-per-layout fact
4
+ survives into CI: the verifier that produced it needs both runtimes and ~1 GB of
5
+ wheels, so it is a documented manual run. That makes the JSON the artifact a
6
+ future edit can quietly falsify — flip one `pass` to true, drop the failing row,
7
+ or widen a tolerance, and nothing downstream notices.
8
+
9
+ STDLIB ONLY, and unittest rather than pytest, because the `detector-service`
10
+ step in `ci.yml` runs `python3 -m unittest discover` with no pip install. An
11
+ earlier draft of this file imported the verifier (and therefore numpy) at module
12
+ scope and would have errored the whole lane before its existing tests ran, while
13
+ also contributing no collected cases of its own — a check that appears to run
14
+ and does not, which is precisely the defect the measurement below is about.
15
+
16
+ These are pure-logic checks over committed bytes. They do not re-measure; they
17
+ assert the recorded numbers are internally consistent with the code that claims
18
+ to have produced them, which is the part a hand-edit breaks.
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ import json
24
+ import sys
25
+ import unittest
26
+ from pathlib import Path
27
+
28
+ BENCH = Path(__file__).resolve().parent.parent / "bench"
29
+ CONTRACT = BENCH / "litert-twin-parity.json"
30
+
31
+ sys.path.insert(0, str(BENCH))
32
+
33
+ from twin_parity_contract import ( # noqa: E402
34
+ ANCHOR,
35
+ BOX_TOL_PX,
36
+ CLS_TOL,
37
+ COMPARABLE_LAYOUTS,
38
+ DETS_LABELS,
39
+ END2END,
40
+ ParityError,
41
+ box_scale_for,
42
+ classify_layout,
43
+ verdict,
44
+ )
45
+
46
+ #: The artifact whose row-level parity could NOT be established, named so the
47
+ #: assertion below cannot pass by the row being deleted. See the test.
48
+ UNVERIFIED_END2END_ARTIFACT = "queue_supermarket.onnx"
49
+
50
+
51
+ class TwinParityContract(unittest.TestCase):
52
+ @classmethod
53
+ def setUpClass(cls) -> None:
54
+ cls.contract = json.loads(CONTRACT.read_text())
55
+ cls.twins = cls.contract["twins"]
56
+
57
+ def test_tolerances_match_the_verifier(self):
58
+ """A widened tolerance in the JSON that the code does not share is a lie."""
59
+ self.assertEqual(self.contract["tolerances"]["box_residual_px"], BOX_TOL_PX)
60
+ self.assertEqual(self.contract["tolerances"]["cls_residual"], CLS_TOL)
61
+
62
+ def test_every_row_used_the_scale_its_layout_dictates(self):
63
+ for row in self.twins:
64
+ with self.subTest(onnx=row["onnx"]):
65
+ expected = box_scale_for(row["layout"], row["imgsz"])
66
+ self.assertEqual(
67
+ row["box_scale_applied"], expected,
68
+ f"{row['onnx']} recorded scale x{row['box_scale_applied']} but layout "
69
+ f"{row['layout']} dictates x{expected}",
70
+ )
71
+
72
+ def test_recorded_verdicts_follow_from_recorded_residuals(self):
73
+ """`pass` must be derivable from the numbers beside it, not asserted."""
74
+ for row in self.twins:
75
+ with self.subTest(onnx=row["onnx"]):
76
+ ok, why = verdict(row)
77
+ self.assertEqual(
78
+ ok, row["pass"],
79
+ f"{row['onnx']}: recorded pass={row['pass']} but verdict says {ok} ({why})",
80
+ )
81
+
82
+ def test_the_anchor_scale_is_load_bearing_on_every_anchor_row(self):
83
+ """An anchor twin whose RAW boxes already match is not normalised.
84
+
85
+ This is the 640x bug's signature. If it ever holds, the row passed for
86
+ the wrong reason and the layout was misread.
87
+ """
88
+ anchors = [r for r in self.twins if r["layout"] == ANCHOR]
89
+ self.assertTrue(anchors, "the contract has no anchor-layout rows to check")
90
+ for row in anchors:
91
+ with self.subTest(onnx=row["onnx"]):
92
+ self.assertGreater(
93
+ row["box_residual_unscaled"], BOX_TOL_PX,
94
+ f"{row['onnx']}: unscaled residual {row['box_residual_unscaled']} is already "
95
+ "inside tolerance, so multiplying by imgsz changed nothing",
96
+ )
97
+
98
+ def test_the_unverified_end2end_row_is_present_and_still_failing(self):
99
+ """We do NOT get to publish a twin we could not compare.
100
+
101
+ `queue_supermarket` returns zero detections above 0.25 on 40 real
102
+ production frames, so its row-level parity is unknown and it is recorded
103
+ failing on purpose.
104
+
105
+ The PRESENCE assertion is the load-bearing half. Checking only that
106
+ every `end2end_n_6` row still fails passes vacuously the moment somebody
107
+ deletes the row — which is exactly the hand-edit this file exists to
108
+ prevent, and the same "absence read as success" shape as an empty corpus
109
+ scoring `overallPass: true`. So the row is named.
110
+ """
111
+ by_name = {r["onnx"]: r for r in self.twins}
112
+ self.assertIn(
113
+ UNVERIFIED_END2END_ARTIFACT, by_name,
114
+ f"{UNVERIFIED_END2END_ARTIFACT} is missing from the contract. It is the only "
115
+ f"{END2END} twin and its parity is unverified; removing the row does not make "
116
+ "it verified. If it was genuinely re-measured on a corpus it fires on, update "
117
+ "this test with that corpus named.",
118
+ )
119
+ row = by_name[UNVERIFIED_END2END_ARTIFACT]
120
+ self.assertEqual(row["layout"], END2END)
121
+ self.assertFalse(
122
+ row["pass"],
123
+ f"{UNVERIFIED_END2END_ARTIFACT} is recorded passing — see the message above",
124
+ )
125
+
126
+ def test_layout_is_classified_from_the_graph_not_the_values(self):
127
+ """Shape and output names decide the layout. Nothing else may.
128
+
129
+ Dispatching on `shape[1] >= 5` alone routes a [1, 300, 6] end2end tensor
130
+ into the anchor branch and produces a confident, meaningless class
131
+ residual. A layout guess that reads the numbers it is interpreting
132
+ always finds a self-consistent story.
133
+ """
134
+ self.assertEqual(classify_layout((1, 300, 6), ["output0"]), END2END)
135
+ self.assertEqual(classify_layout((1, 14, 8400), ["output0"]), ANCHOR)
136
+ self.assertEqual(classify_layout((1, 6, 8400), ["output0"]), ANCHOR)
137
+ self.assertEqual(classify_layout((1, 300, 4), ["dets", "labels"]), DETS_LABELS)
138
+ with self.assertRaises(ParityError):
139
+ classify_layout((1, 8400), ["output0"])
140
+
141
+ def test_no_layout_gets_a_scale_by_default(self):
142
+ """An unknown layout must refuse, never fall through to 1.0.
143
+
144
+ A silent wrong-layout decode is worse than a refusal: it returns boxes,
145
+ and boxes that are plausible and wrong are what this file is about.
146
+ """
147
+ with self.assertRaises(ParityError):
148
+ box_scale_for("some_future_layout", 640)
149
+
150
+ def test_rfdetr_is_classified_but_not_advertised_as_comparable(self):
151
+ """Naming a layout is not the same as being able to compare it.
152
+
153
+ `classify_layout` recognises RF-DETR's two-output form and
154
+ `box_scale_for` knows its scale is 1.0, but this bench cannot split a
155
+ two-output graph into box and class halves, and RF-DETR ships no LiteRT
156
+ twin. So it must be absent from the comparable set — otherwise the
157
+ verifier advertises a capability that fails at the point of use.
158
+ """
159
+ self.assertNotIn(DETS_LABELS, COMPARABLE_LAYOUTS)
160
+ self.assertEqual(COMPARABLE_LAYOUTS, frozenset({ANCHOR, END2END}))
161
+ self.assertEqual(box_scale_for(DETS_LABELS, 640), 1.0)
162
+
163
+
164
+ if __name__ == "__main__":
165
+ unittest.main()