Backup / gmnet /code /journal_exp /tests /test_config.py
YFanwang's picture
Add files using upload-large-folder tool
10979b5 verified
Raw History Blame Contribute Delete
3 kB
"""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()