|
| 1 | +from dataclasses import dataclass |
| 2 | +import pytest |
| 3 | + |
| 4 | +from i6_models.config import ModelConfiguration |
| 5 | + |
| 6 | + |
| 7 | +def test_simple_configuration(): |
| 8 | + @dataclass |
| 9 | + class TestConfiguration(ModelConfiguration): |
| 10 | + num_layers: int = 5 |
| 11 | + hidden_dim: int = 256 |
| 12 | + name: str = "Cool Model Configuration" |
| 13 | + |
| 14 | + test_cfg = TestConfiguration(num_layers=12, name="Even Cooler Model Configuration") |
| 15 | + assert test_cfg.hidden_dim == 256 |
| 16 | + assert test_cfg.name == "Even Cooler Model Configuration" |
| 17 | + assert test_cfg.num_layers == 12 |
| 18 | + test_cfg.num_layers = 7 |
| 19 | + assert test_cfg.num_layers == 7 |
| 20 | + |
| 21 | + |
| 22 | +def test_nested_configuration(): |
| 23 | + @dataclass |
| 24 | + class TestConfiguration(ModelConfiguration): |
| 25 | + num_layers: int = 5 |
| 26 | + hidden_dim: int = 256 |
| 27 | + name: str = "Cool Model Configuration" |
| 28 | + |
| 29 | + @dataclass |
| 30 | + class TestNestedConfiguration(ModelConfiguration): |
| 31 | + encoder_config: TestConfiguration = TestConfiguration(num_layers=4, hidden_dim=3, name="encoder_config") |
| 32 | + decoder_config: TestConfiguration = TestConfiguration(num_layers=6, hidden_dim=5, name="decoder_config") |
| 33 | + |
| 34 | + dec_cfg = TestConfiguration() |
| 35 | + test_cfg = TestNestedConfiguration(decoder_config=dec_cfg) |
| 36 | + |
| 37 | + assert test_cfg.encoder_config.num_layers == 4 |
| 38 | + assert test_cfg.encoder_config.hidden_dim == 3 |
| 39 | + assert test_cfg.encoder_config.name == "encoder_config" |
| 40 | + assert test_cfg.decoder_config.num_layers == 5 |
| 41 | + assert test_cfg.decoder_config.hidden_dim == 256 |
| 42 | + assert test_cfg.decoder_config.name == "Cool Model Configuration" |
| 43 | + test_cfg.encoder_config = TestConfiguration(num_layers=1, hidden_dim=2, name="better_encoder_config") |
| 44 | + assert test_cfg.encoder_config.num_layers == 1 |
| 45 | + assert test_cfg.encoder_config.hidden_dim == 2 |
| 46 | + assert test_cfg.encoder_config.name == "better_encoder_config" |
| 47 | + |
| 48 | + |
| 49 | +def test_config_typing(): |
| 50 | + @dataclass |
| 51 | + class TestConfiguration(ModelConfiguration): |
| 52 | + num_layers: int = 4 |
| 53 | + hidden_dim: int = 13 |
| 54 | + name: str = "Cool Model Configuration" |
| 55 | + |
| 56 | + from typeguard import TypeCheckError |
| 57 | + |
| 58 | + TestConfiguration(num_layers=2, hidden_dim=1) |
| 59 | + with pytest.raises(TypeCheckError): |
| 60 | + TestConfiguration(num_layers=2.0, hidden_dim="One") |
0 commit comments