File size: 3,000 Bytes
10979b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Tests for reproducible experiment configuration behavior."""

from __future__ import annotations

import tempfile
import unittest
from pathlib import Path

from gmnet.config import load_config


class ConfigTests(unittest.TestCase):
    def test_recursive_merge_and_override(self) -> None:
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            (root / "base.yaml").write_text(
                "model:\n  variant: s3\n  gate_type: relu6_self\ntrain:\n  epochs: 300\n",
                encoding="utf-8",
            )
            (root / "child.yaml").write_text(
                "base: base.yaml\nmodel:\n  gate_type: gelu_self\n",
                encoding="utf-8",
            )
            config = load_config(
                root / "child.yaml", ["train.epochs=2", "model.drop_path_rate=0.02"]
            )
        self.assertEqual(config["model"]["variant"], "s3")
        self.assertEqual(config["model"]["gate_type"], "gelu_self")
        self.assertEqual(config["model"]["drop_path_rate"], 0.02)
        self.assertEqual(config["train"]["epochs"], 2)

    def test_new_gate_control_configs_are_explicit_and_loadable(self) -> None:
        root = Path(__file__).resolve().parents[1]
        expected = {
            "cifar100_gmnet_s1_smooth_corrected.yaml": (
                "smooth_clipped_self",
                True,
            ),
            "cifar100_gmnet_s1_smooth_fixed_c6.yaml": (
                "smooth_clipped_self",
                False,
            ),
            "cifar100_gmnet_s1_relu6_only.yaml": ("relu6_only", None),
            "imagenet_gmnet_s3_no_gate.yaml": ("no_gate", None),
            "imagenet_gmnet_s3_relu6_only.yaml": ("relu6_only", None),
            "imagenet_gmnet_s3_smooth_corrected.yaml": (
                "smooth_clipped_self",
                True,
            ),
            "imagenet_gmnet_s3_smooth_fixed_c6.yaml": (
                "smooth_clipped_self",
                False,
            ),
        }
        for filename, (gate_type, trainable) in expected.items():
            with self.subTest(filename=filename):
                config = load_config(root / "configs" / "e3_gate" / filename)
                self.assertEqual(config["model"]["gate_type"], gate_type)
                if trainable is not None:
                    self.assertEqual(
                        config["model"]["smooth_clip_trainable"], trainable
                    )
                    self.assertEqual(config["model"]["smooth_clip_init"], 6.0)

    def test_cycle_is_rejected(self) -> None:
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            (root / "a.yaml").write_text("base: b.yaml\n", encoding="utf-8")
            (root / "b.yaml").write_text("base: a.yaml\n", encoding="utf-8")
            with self.assertRaisesRegex(ValueError, "inheritance cycle"):
                load_config(root / "a.yaml")


if __name__ == "__main__":
    unittest.main()