"""Tests for configuration loading.""" from __future__ import annotations import yaml import pytest from obliteratus.config import DatasetConfig, ModelConfig, StrategyConfig, StudyConfig SAMPLE_CONFIG = { "model": { "name": "gpt2", "task": "causal_lm", "dtype": "float32", "device": "cpu", }, "dataset": { "name": "wikitext", "subset": "wikitext-2-raw-v1", "split": "test", "text_column": "text", "max_samples": 50, }, "strategies": [ {"name": "layer_removal", "params": {}}, {"name": "ffn_ablation", "params": {}}, ], "metrics": ["perplexity"], "batch_size": 4, "max_length": 256, "output_dir": "results/test", } class TestStudyConfig: def test_from_dict(self): config = StudyConfig.from_dict(SAMPLE_CONFIG) assert config.model.name == "gpt2" assert config.model.task == "causal_lm" assert config.dataset.name == "wikitext" assert len(config.strategies) == 2 assert config.strategies[0].name == "layer_removal" def test_from_yaml(self, tmp_path): yaml_path = tmp_path / "test_config.yaml" yaml_path.write_text(yaml.dump(SAMPLE_CONFIG)) config = StudyConfig.from_yaml(yaml_path) assert config.model.name == "gpt2" assert config.batch_size == 4 def test_roundtrip(self): config = StudyConfig.from_dict(SAMPLE_CONFIG) d = config.to_dict() config2 = StudyConfig.from_dict(d) assert config2.model.name == config.model.name assert config2.dataset.name == config.dataset.name assert len(config2.strategies) == len(config.strategies) def test_model_quantization_and_label_count_roundtrip(self): raw = { **SAMPLE_CONFIG, "model": { **SAMPLE_CONFIG["model"], "num_labels": 7, "quantization": "4bit", }, } config = StudyConfig.from_dict(raw) assert config.model.quantization == "4bit" assert config.model.num_labels == 7 assert StudyConfig.from_dict(config.to_dict()).model == config.model def test_remote_config_roundtrip_preserves_security_and_execution_settings(self): raw = { **SAMPLE_CONFIG, "remote": { "host": "compute.example", "user": "runner", "port": 2222, "ssh_key": "/keys/id", "known_hosts_file": "/keys/known_hosts", "remote_dir": "/srv/obliteratus", "install_timeout": 120, "python": "/opt/python", "sync_results": False, "gpus": "02, 0", "install_source": "obliteratus==0.1.2", }, } config = StudyConfig.from_dict(raw) assert config.remote is not None assert config.remote.gpus == "2,0" assert StudyConfig.from_dict(config.to_dict()) == config @pytest.mark.parametrize( "remote", [ {"host": ""}, {"host": "host", "port": 0}, {"host": "host", "remote_dir": "relative"}, {"host": "host", "gpus": "0; injected"}, {"host": "host", "install_timeout": 0}, ], ) def test_remote_config_rejects_invalid_public_values(self, remote): with pytest.raises(ValueError): StudyConfig.from_dict({**SAMPLE_CONFIG, "remote": remote}) def test_snapshot_budget_round_trip(): from obliteratus.config import StudyConfig data = {'model': {'name': 'fixture'}, 'dataset': {'name': 'fixture'}, 'strategies': [], 'snapshot': {'directory': '/tmp/restore', 'max_bytes': 300000000000, 'staging_bytes': 4000000000}} config = StudyConfig.from_dict(data) assert config.to_dict()['snapshot'] == data['snapshot'] assert StudyConfig.from_dict(config.to_dict()).snapshot == config.snapshot def test_snapshot_default_is_none_and_survives_study_round_trip(): from obliteratus.config import SnapshotConfig assert SnapshotConfig().staging_bytes is None config = StudyConfig.from_dict(SAMPLE_CONFIG) assert config.snapshot.staging_bytes is None assert config.to_dict()["snapshot"]["staging_bytes"] is None assert StudyConfig.from_dict(config.to_dict()).snapshot == config.snapshot @pytest.mark.parametrize("staging_bytes", [None, 1, 1024**3, 4_000_000_000]) def test_snapshot_staging_budget_round_trip_preserves_none_or_explicit_int(staging_bytes): config = StudyConfig.from_dict({**SAMPLE_CONFIG, "snapshot": {"staging_bytes": staging_bytes}}) assert config.snapshot.staging_bytes == staging_bytes assert config.to_dict()["snapshot"]["staging_bytes"] == staging_bytes assert StudyConfig.from_dict(config.to_dict()).snapshot == config.snapshot @pytest.mark.parametrize("name, value", [ *(('staging_bytes', value) for value in (0, -1, True, False, 1.5, "1024")), *(('max_bytes', value) for value in (None, 0, -1, True, False, 1.5, "1024")), ]) def test_snapshot_config_rejects_invalid_byte_limits(name, value): with pytest.raises(ValueError, match="positive byte counts"): StudyConfig.from_dict({**SAMPLE_CONFIG, "snapshot": {name: value}}) def _study_without(*keys: str) -> dict: return {key: value for key, value in SAMPLE_CONFIG.items() if key not in keys} def test_from_yaml_applies_cli_preset_before_requiring_strategies(tmp_path): """#608: ``--preset`` must supply strategies omitted by the YAML.""" base = _study_without("strategies", "metrics", "batch_size", "max_length") flag_path = tmp_path / "flag.yaml" flag_path.write_text(yaml.safe_dump(base)) embedded_path = tmp_path / "embedded.yaml" embedded_path.write_text(yaml.safe_dump({**base, "preset": "quick"})) from_flag = StudyConfig.from_yaml(flag_path, preset="quick") from_embedded = StudyConfig.from_yaml(embedded_path) assert [s.name for s in from_flag.strategies] == ["layer_removal", "ffn_ablation"] assert from_flag == from_embedded def test_from_yaml_preset_keeps_explicit_strategies(tmp_path): path = tmp_path / "study.yaml" path.write_text(yaml.safe_dump({**SAMPLE_CONFIG, "strategies": [{"name": "head_pruning"}]})) config = StudyConfig.from_yaml(path, preset="quick") assert [s.name for s in config.strategies] == ["head_pruning"] assert config.batch_size == SAMPLE_CONFIG["batch_size"] CLASSIFICATION_CONFIG = { **_study_without("metrics"), "model": {"name": "./classifier", "task": "classification", "device": "cpu"}, } def test_classification_without_metrics_defaults_to_accuracy_and_f1(): """#610: an omitted ``metrics`` list must not give a classifier perplexity.""" config = StudyConfig.from_dict(dict(CLASSIFICATION_CONFIG)) assert config.metrics == ["accuracy", "f1"] def test_classification_preset_without_metrics_uses_classification_metrics(): raw = { key: value for key, value in CLASSIFICATION_CONFIG.items() if key != "strategies" } config = StudyConfig.from_dict({**raw, "preset": "quick"}) assert [s.name for s in config.strategies] == ["layer_removal", "ffn_ablation"] assert config.metrics == ["accuracy", "f1"] def test_explicit_classification_metrics_are_honored(): config = StudyConfig.from_dict({**CLASSIFICATION_CONFIG, "metrics": ["accuracy"]}) assert config.metrics == ["accuracy"] def test_causal_lm_without_metrics_still_defaults_to_perplexity(): assert StudyConfig.from_dict(_study_without("metrics")).metrics == ["perplexity"] raw = _study_without("metrics", "strategies") assert StudyConfig.from_dict({**raw, "preset": "quick"}).metrics == ["perplexity"] def test_model_label_count_defaults_to_checkpoint_configuration(): """#614: no implicit binary label count may override a checkpoint head.""" config = StudyConfig.from_dict(dict(CLASSIFICATION_CONFIG)) assert config.model.num_labels is None assert config.to_dict()["model"]["num_labels"] is None assert StudyConfig.from_dict(config.to_dict()).model == config.model QUICK_STRATEGIES = ["layer_removal", "ffn_ablation"] @pytest.mark.parametrize("yaml_key", ["preset", "study_preset"]) def test_cli_preset_replaces_preset_named_in_yaml(tmp_path, yaml_key): """PR #620 r1: ``--preset`` wins over either preset key in the YAML.""" path = tmp_path / "study.yaml" path.write_text(yaml.safe_dump({**_study_without("strategies"), yaml_key: "attention"})) config = StudyConfig.from_yaml(path, preset="quick") assert [s.name for s in config.strategies] == QUICK_STRATEGIES @pytest.mark.parametrize("yaml_key", [None, "preset", "study_preset"]) def test_unknown_cli_preset_is_rejected(tmp_path, yaml_key): """PR #620 r1: an unknown ``--preset`` must fail, never be silently ignored.""" raw = _study_without("strategies") if yaml_key: raw[yaml_key] = "attention" path = tmp_path / "study.yaml" path.write_text(yaml.safe_dump(raw)) with pytest.raises(ValueError, match=r"Unknown preset 'invalid'\. Available: .*quick"): StudyConfig.from_yaml(path, preset="invalid") def test_unknown_yaml_preset_is_rejected(): with pytest.raises(ValueError, match=r"Unknown preset 'nope'"): StudyConfig.from_dict({**_study_without("strategies"), "study_preset": "nope"}) def _direct_study(task: str, **kwargs) -> StudyConfig: return StudyConfig( model=ModelConfig(name="./model", task=task, device="cpu"), dataset=DatasetConfig(name="./dataset"), strategies=[StrategyConfig(name="layer_removal")], **kwargs, ) @pytest.mark.parametrize( ("task", "expected"), [("classification", ["accuracy", "f1"]), ("causal_lm", ["perplexity"])], ) def test_directly_constructed_study_resolves_metrics_from_task(task, expected): """PR #620 r1: omitted metrics follow the model task without ``from_dict``.""" config = _direct_study(task) assert config.metrics == expected assert config.to_dict()["metrics"] == expected assert StudyConfig.from_dict(config.to_dict()) == config def test_directly_constructed_study_keeps_explicit_metrics(): config = _direct_study("classification", metrics=["accuracy"]) assert config.metrics == ["accuracy"] assert _direct_study("causal_lm", metrics=["perplexity", "accuracy"]).metrics == [ "perplexity", "accuracy", ] def test_parsed_presets_do_not_share_mutable_state(): """PR #620 r1: one parsed study must not alter the preset or later studies.""" from obliteratus.study_presets import get_study_preset raw = _study_without("strategies", "metrics") first = StudyConfig.from_dict({**raw, "preset": "quick"}) first.strategies[0].params["layer"] = 0 first.metrics.append("accuracy") second = StudyConfig.from_dict({**raw, "preset": "quick"}) assert [s.params for s in second.strategies] == [{}, {}] assert second.metrics == ["perplexity"] preset = get_study_preset("quick") assert [s["params"] for s in preset.strategies] == [{}, {}] assert preset.metrics == ["perplexity"] def test_explicit_empty_metrics_list_is_rejected_when_the_study_is_parsed(): """#666: ``metrics: []`` fails at config time, before any model or dataset loads.""" with pytest.raises(ValueError, match="omit metrics or set it to null"): StudyConfig.from_dict({**_study_without("metrics"), "metrics": []}) with pytest.raises(ValueError, match="omit metrics or set it to null"): _direct_study("classification", metrics=[]) # Omitted and null metrics keep meaning the task defaults. assert StudyConfig.from_dict({**_study_without("metrics"), "metrics": None}).metrics == [ "perplexity", ]