|
| 1 | +""" |
| 2 | +Tests for the Spatial.reuse / Spatial.may_reuse default behavior. |
| 3 | +
|
| 4 | +The defaults mirror Tensors.keep / Tensors.may_keep: |
| 5 | +- If neither is set, reuse defaults to "Nothing" and may_reuse defaults to "All". |
| 6 | +- If reuse is set, may_reuse defaults to the same value as reuse (so the spatial |
| 7 | + loop's eligible-for-reuse set equals its required-reuse set). |
| 8 | +- If may_reuse is set explicitly, the user value is preserved. |
| 9 | +""" |
| 10 | + |
| 11 | +import unittest |
| 12 | + |
| 13 | +from accelforge.frontend.arch import Spatial as ArchSpatial |
| 14 | + |
| 15 | + |
| 16 | +class TestSpatialReuseDefaults(unittest.TestCase): |
| 17 | + def _eval(self, **kwargs): |
| 18 | + s = ArchSpatial(name="X", fanout=4, **kwargs) |
| 19 | + evaluated, _ = s._eval_expressions() |
| 20 | + return evaluated |
| 21 | + |
| 22 | + def test_neither_defined_may_reuse_is_all(self): |
| 23 | + e = self._eval() |
| 24 | + self.assertEqual(e.may_reuse, "All") |
| 25 | + |
| 26 | + def test_neither_defined_reuse_is_nothing(self): |
| 27 | + e = self._eval() |
| 28 | + self.assertEqual(e.reuse, "Nothing") |
| 29 | + |
| 30 | + def test_reuse_defined_may_reuse_defaults_to_reuse(self): |
| 31 | + e = self._eval(reuse="input") |
| 32 | + self.assertEqual(e.reuse, "input") |
| 33 | + self.assertEqual(e.may_reuse, "input") |
| 34 | + |
| 35 | + def test_reuse_defined_with_complex_set(self): |
| 36 | + e = self._eval(reuse="input | weight") |
| 37 | + self.assertEqual(e.reuse, "input | weight") |
| 38 | + self.assertEqual(e.may_reuse, "input | weight") |
| 39 | + |
| 40 | + def test_may_reuse_explicit_when_reuse_undefined(self): |
| 41 | + e = self._eval(may_reuse="output") |
| 42 | + self.assertEqual(e.reuse, "Nothing") |
| 43 | + self.assertEqual(e.may_reuse, "output") |
| 44 | + |
| 45 | + def test_may_reuse_explicit_nothing_when_reuse_undefined(self): |
| 46 | + e = self._eval(may_reuse="Nothing") |
| 47 | + self.assertEqual(e.reuse, "Nothing") |
| 48 | + self.assertEqual(e.may_reuse, "Nothing") |
| 49 | + |
| 50 | + def test_both_explicit_preserves_both(self): |
| 51 | + e = self._eval(reuse="input", may_reuse="All") |
| 52 | + self.assertEqual(e.reuse, "input") |
| 53 | + self.assertEqual(e.may_reuse, "All") |
| 54 | + |
| 55 | + def test_may_reuse_disjoint_from_reuse_when_both_explicit(self): |
| 56 | + e = self._eval(reuse="input", may_reuse="output") |
| 57 | + self.assertEqual(e.reuse, "input") |
| 58 | + self.assertEqual(e.may_reuse, "output") |
| 59 | + |
| 60 | + |
| 61 | +class TestSpatialReuseDefaultsFromYaml(unittest.TestCase): |
| 62 | + """Verify YAML parsing applies the same defaults via Spec.from_yaml. |
| 63 | +
|
| 64 | + These check the post-Spatial-eval values (sentinels resolved) before the |
| 65 | + full spec evaluation, so values are still the raw parsed strings.""" |
| 66 | + |
| 67 | + def _spatial_from_yaml(self, spatial_yaml: str): |
| 68 | + import os |
| 69 | + import tempfile |
| 70 | + from pathlib import Path |
| 71 | + from accelforge.frontend.spec import Spec |
| 72 | + |
| 73 | + repo_root = Path(__file__).parent.parent.parent |
| 74 | + yaml_text = f""" |
| 75 | +arch: |
| 76 | + nodes: |
| 77 | + - !Container |
| 78 | + name: PE |
| 79 | + spatial: |
| 80 | + - {spatial_yaml} |
| 81 | +workload: |
| 82 | + rank_sizes: {{M: 8}} |
| 83 | + einsums: |
| 84 | + - name: E |
| 85 | + tensor_accesses: |
| 86 | + - {{name: a, projection: [m], bits_per_value: 8}} |
| 87 | + - {{name: b, projection: [m], output: True, bits_per_value: 8}} |
| 88 | +""" |
| 89 | + with tempfile.NamedTemporaryFile( |
| 90 | + mode="w", suffix=".yaml", delete=False, dir=str(repo_root) |
| 91 | + ) as f: |
| 92 | + f.write(yaml_text) |
| 93 | + f.flush() |
| 94 | + try: |
| 95 | + spec = Spec.from_yaml(f.name) |
| 96 | + spatial = spec.arch.find("PE").spatial[0] |
| 97 | + evaluated, _ = spatial._eval_expressions() |
| 98 | + return evaluated |
| 99 | + finally: |
| 100 | + os.unlink(f.name) |
| 101 | + |
| 102 | + def test_yaml_no_reuse_no_may_reuse(self): |
| 103 | + s = self._spatial_from_yaml("{name: dim, fanout: 4}") |
| 104 | + self.assertEqual(s.reuse, "Nothing") |
| 105 | + self.assertEqual(s.may_reuse, "All") |
| 106 | + |
| 107 | + def test_yaml_reuse_only(self): |
| 108 | + s = self._spatial_from_yaml("{name: dim, fanout: 4, reuse: a}") |
| 109 | + self.assertEqual(s.reuse, "a") |
| 110 | + self.assertEqual(s.may_reuse, "a") |
| 111 | + |
| 112 | + def test_yaml_may_reuse_only(self): |
| 113 | + s = self._spatial_from_yaml("{name: dim, fanout: 4, may_reuse: b}") |
| 114 | + self.assertEqual(s.reuse, "Nothing") |
| 115 | + self.assertEqual(s.may_reuse, "b") |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + unittest.main() |
0 commit comments