|
7 | 7 |
|
8 | 8 | import torch |
9 | 9 |
|
| 10 | +from tabpfn import TabPFNClassifier, TabPFNRegressor |
| 11 | +from tabpfn.architectures import base |
| 12 | +from tabpfn.architectures.base.bar_distribution import FullSupportBarDistribution |
| 13 | +from tabpfn.architectures.base.config import ModelConfig |
| 14 | +from tabpfn.base import ClassifierModelSpecs, RegressorModelSpecs |
10 | 15 | from tabpfn.constants import ModelVersion |
11 | 16 | from tabpfn.inference_config import InferenceConfig |
12 | 17 | from tabpfn.preprocessing import PreprocessorConfig |
@@ -69,3 +74,108 @@ def test__override_with_user_input__override_is_None__returns_copy_of_config() - |
69 | 74 | new_config = config.override_with_user_input_and_resolve_auto(user_config=None) |
70 | 75 | assert new_config is not config |
71 | 76 | assert new_config == config |
| 77 | + |
| 78 | + |
| 79 | +def _make_classifier_specs() -> ClassifierModelSpecs: |
| 80 | + config = ModelConfig( |
| 81 | + emsize=8, |
| 82 | + features_per_group=1, |
| 83 | + max_num_classes=10, |
| 84 | + nhead=2, |
| 85 | + nlayers=2, |
| 86 | + remove_duplicate_features=True, |
| 87 | + num_buckets=100, |
| 88 | + ) |
| 89 | + model = base.get_architecture(config=config, cache_trainset_representation=False) |
| 90 | + inference_config = InferenceConfig.get_default( |
| 91 | + task_type="multiclass", model_version=ModelVersion.V2_5 |
| 92 | + ) |
| 93 | + return ClassifierModelSpecs( |
| 94 | + model=model, |
| 95 | + architecture_config=config, |
| 96 | + inference_config=inference_config, |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def _make_regressor_specs() -> RegressorModelSpecs: |
| 101 | + config = ModelConfig( |
| 102 | + emsize=8, |
| 103 | + features_per_group=1, |
| 104 | + max_num_classes=10, |
| 105 | + nhead=2, |
| 106 | + nlayers=2, |
| 107 | + remove_duplicate_features=True, |
| 108 | + num_buckets=100, |
| 109 | + ) |
| 110 | + model = base.get_architecture(config=config, cache_trainset_representation=False) |
| 111 | + borders = torch.linspace(-3, 3, config.num_buckets + 1) |
| 112 | + norm_criterion = FullSupportBarDistribution(borders) |
| 113 | + inference_config = InferenceConfig.get_default( |
| 114 | + task_type="regression", model_version=ModelVersion.V2_5 |
| 115 | + ) |
| 116 | + return RegressorModelSpecs( |
| 117 | + model=model, |
| 118 | + architecture_config=config, |
| 119 | + inference_config=inference_config, |
| 120 | + norm_criterion=norm_criterion, |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +def test__classifier_get_inference_config__before_fit__returns_config() -> None: |
| 125 | + specs = _make_classifier_specs() |
| 126 | + clf = TabPFNClassifier(model_path=specs, device="cpu") |
| 127 | + assert not hasattr(clf, "inference_config_") |
| 128 | + config = clf.get_inference_config() |
| 129 | + assert isinstance(config, InferenceConfig) |
| 130 | + assert config == specs.inference_config |
| 131 | + |
| 132 | + |
| 133 | +def test__classifier_get_inference_config__returns_deepcopy() -> None: |
| 134 | + specs = _make_classifier_specs() |
| 135 | + clf = TabPFNClassifier(model_path=specs, device="cpu") |
| 136 | + config = clf.get_inference_config() |
| 137 | + assert config is not clf.inference_config_ |
| 138 | + config.PREPROCESS_TRANSFORMS.clear() |
| 139 | + assert len(clf.inference_config_.PREPROCESS_TRANSFORMS) > 0 |
| 140 | + |
| 141 | + |
| 142 | +def test__classifier_get_inference_config__with_override__applies_override() -> None: |
| 143 | + specs = _make_classifier_specs() |
| 144 | + clf = TabPFNClassifier( |
| 145 | + model_path=specs, |
| 146 | + device="cpu", |
| 147 | + inference_config={"POLYNOMIAL_FEATURES": "all"}, |
| 148 | + ) |
| 149 | + config = clf.get_inference_config() |
| 150 | + assert config.POLYNOMIAL_FEATURES == "all" |
| 151 | + assert specs.inference_config.POLYNOMIAL_FEATURES == "no" |
| 152 | + |
| 153 | + |
| 154 | +def test__regressor_get_inference_config__before_fit__returns_config() -> None: |
| 155 | + specs = _make_regressor_specs() |
| 156 | + reg = TabPFNRegressor(model_path=specs, device="cpu") |
| 157 | + assert not hasattr(reg, "inference_config_") |
| 158 | + config = reg.get_inference_config() |
| 159 | + assert isinstance(config, InferenceConfig) |
| 160 | + assert config == specs.inference_config |
| 161 | + |
| 162 | + |
| 163 | +def test__regressor_get_inference_config__returns_deepcopy() -> None: |
| 164 | + specs = _make_regressor_specs() |
| 165 | + reg = TabPFNRegressor(model_path=specs, device="cpu") |
| 166 | + config = reg.get_inference_config() |
| 167 | + assert config is not reg.inference_config_ |
| 168 | + config.PREPROCESS_TRANSFORMS.clear() |
| 169 | + assert len(reg.inference_config_.PREPROCESS_TRANSFORMS) > 0 |
| 170 | + |
| 171 | + |
| 172 | +def test__regressor_get_inference_config__with_override__applies_override() -> None: |
| 173 | + specs = _make_regressor_specs() |
| 174 | + reg = TabPFNRegressor( |
| 175 | + model_path=specs, |
| 176 | + device="cpu", |
| 177 | + inference_config={"POLYNOMIAL_FEATURES": "all"}, |
| 178 | + ) |
| 179 | + config = reg.get_inference_config() |
| 180 | + assert config.POLYNOMIAL_FEATURES == "all" |
| 181 | + assert specs.inference_config.POLYNOMIAL_FEATURES == "no" |
0 commit comments