positronixlab 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- positronixlab/__init__.py +8 -0
- positronixlab/baselines/__init__.py +40 -0
- positronixlab/baselines/base.py +195 -0
- positronixlab/baselines/csordas/__init__.py +55 -0
- positronixlab/baselines/csordas/datasets.py +319 -0
- positronixlab/baselines/csordas/discoverer.py +157 -0
- positronixlab/baselines/csordas/models.py +334 -0
- positronixlab/baselines/lepori/__init__.py +49 -0
- positronixlab/baselines/lepori/_cvr_gen/__init__.py +55 -0
- positronixlab/baselines/lepori/_cvr_gen/shape.py +344 -0
- positronixlab/baselines/lepori/_cvr_gen/subnetwork_tasks.py +2437 -0
- positronixlab/baselines/lepori/_cvr_gen/utils.py +520 -0
- positronixlab/baselines/lepori/datasets.py +290 -0
- positronixlab/baselines/lepori/models.py +330 -0
- positronixlab/baselines/loader.py +428 -0
- positronixlab/baselines/neurosurgeon/__init__.py +19 -0
- positronixlab/baselines/neurosurgeon/adapter.py +351 -0
- positronixlab/baselines/neurosurgeon/discoverer.py +103 -0
- positronixlab/dataprovider/__init__.py +161 -0
- positronixlab/dataprovider/addmul_dataprovider.py +293 -0
- positronixlab/dataprovider/cifar100_dataprovider.py +65 -0
- positronixlab/dataprovider/cifar10_dataprovider.py +65 -0
- positronixlab/dataprovider/class_removal_dataprovider.py +137 -0
- positronixlab/dataprovider/config.py +56 -0
- positronixlab/dataprovider/covertype_dataprovider.py +96 -0
- positronixlab/dataprovider/cvr_oddoneout_dataprovider.py +236 -0
- positronixlab/dataprovider/dataprovider.py +687 -0
- positronixlab/dataprovider/dataprovider_factory.py +149 -0
- positronixlab/dataprovider/fmnist_dataprovider.py +69 -0
- positronixlab/dataprovider/higgs_dataprovider.py +464 -0
- positronixlab/dataprovider/iris_dataprovider.py +54 -0
- positronixlab/dataprovider/math_dataprovider.py +65 -0
- positronixlab/dataprovider/mnist_dataprovider.py +65 -0
- positronixlab/dataprovider/oddoneout_dataprovider.py +78 -0
- positronixlab/dataprovider/permuted_mnist_dataprovider.py +141 -0
- positronixlab/dataprovider/scan_dataprovider.py +65 -0
- positronixlab/dataprovider/syneval_oddoneout_dataprovider.py +231 -0
- positronixlab/dataprovider/tuple_arithmetic_dataprovider.py +297 -0
- positronixlab/deeplearning/__init__.py +12 -0
- positronixlab/deeplearning/config.py +15 -0
- positronixlab/deeplearning/deeplearning_base.py +196 -0
- positronixlab/deeplearning/deeplearning_eval.py +73 -0
- positronixlab/deeplearning/deeplearning_train.py +263 -0
- positronixlab/metamodel/__init__.py +31 -0
- positronixlab/metamodel/config.py +37 -0
- positronixlab/metamodel/metacriterion.py +76 -0
- positronixlab/metamodel/metamodel.py +437 -0
- positronixlab/metamodel/metatransform.py +60 -0
- positronixlab/modularcluster/README.md +244 -0
- positronixlab/modularcluster/__init__.py +9 -0
- positronixlab/modularcluster/modularcluster_kmeans.py +265 -0
- positronixlab/modularcluster/modularcluster_kmeans_stream.py +306 -0
- positronixlab/modularcluster/streaming_cluster_tracker.py +259 -0
- positronixlab/modularobserver/__init__.py +41 -0
- positronixlab/modularobserver/layerobserver.py +62 -0
- positronixlab/modularobserver/modelmapper.py +529 -0
- positronixlab/modularobserver/modularobserver.py +195 -0
- positronixlab/modularspace/__init__.py +35 -0
- positronixlab/modularspace/config.py +45 -0
- positronixlab/modularspace/custertracker.py +297 -0
- positronixlab/modularspace/layerspace.py +343 -0
- positronixlab/modularspace/modularoverspace.py +85 -0
- positronixlab/modularspace/modularspace.py +156 -0
- positronixlab/modularspace/presets.py +86 -0
- positronixlab/modularspace/space_step.py +289 -0
- positronixlab/modularui/README.md +4 -0
- positronixlab/modularui/__init__.py +37 -0
- positronixlab/modularui/assets/css/main.css +79 -0
- positronixlab/modularui/assets/css/toggle-switches.css +59 -0
- positronixlab/modularui/components.py +289 -0
- positronixlab/modularui/docks.py +134 -0
- positronixlab/modularui/layouts.py +182 -0
- positronixlab/modularui/modularui.py +488 -0
- positronixlab/modularui/plots.py +428 -0
- positronixlab/modularui/settings.py +35 -0
- positronixlab/nas/__init__.py +12 -0
- positronixlab/nas/experiment.py +579 -0
- positronixlab/nas/trial_config.py +64 -0
- positronixlab/nas/trial_pipeline.py +181 -0
- positronixlab/pipeline/__init__.py +27 -0
- positronixlab/pipeline/config.py +84 -0
- positronixlab/pipeline/pipeline.py +200 -0
- positronixlab/pipeline/pipeline_config.py +45 -0
- positronixlab/pipeline/pipeline_config_v1.py +71 -0
- positronixlab/pipeline/pipeline_config_v2.py +89 -0
- positronixlab/py.typed +1 -0
- positronixlab/spacemath/__init__.py +118 -0
- positronixlab/spacemath/common.py +152 -0
- positronixlab/spacemath/contrast.py +224 -0
- positronixlab/spacemath/losses.py +135 -0
- positronixlab/spacemath/reducers.py +134 -0
- positronixlab/spacemath/scoring.py +141 -0
- positronixlab/spacemath/sigreg.py +38 -0
- positronixlab/spacemath/space_type.py +101 -0
- positronixlab/spacemath/spacemath.py +274 -0
- positronixlab/spacemath/weighting.py +144 -0
- positronixlab/stats/__init__.py +25 -0
- positronixlab/stats/common.py +26 -0
- positronixlab/stats/filter.py +36 -0
- positronixlab/stats/processor.py +211 -0
- positronixlab/stats/reducer.py +183 -0
- positronixlab/stats/stream.py +91 -0
- positronixlab/tasks/README.md +0 -0
- positronixlab/tasks/__init__.py +35 -0
- positronixlab/tasks/atlas_discoverer.py +441 -0
- positronixlab/tasks/benchmarks.py +313 -0
- positronixlab/tasks/compositionality/README.md +0 -0
- positronixlab/tasks/compositionality/__init__.py +0 -0
- positronixlab/tasks/compositionality/atlas_check.py +315 -0
- positronixlab/tasks/compositionality/compositionality.py +234 -0
- positronixlab/tasks/compositionality/weightmask_check.py +234 -0
- positronixlab/tasks/discoverer.py +86 -0
- positronixlab/tasks/masks.py +406 -0
- positronixlab/template/__init__.py +33 -0
- positronixlab/template/template_base.py +321 -0
- positronixlab/template/template_covertype.py +40 -0
- positronixlab/template/template_factory.py +83 -0
- positronixlab/template/template_fmnist.py +24 -0
- positronixlab/template/template_higgs.py +33 -0
- positronixlab/template/template_mnist.py +22 -0
- positronixlab/template/template_variant.py +116 -0
- positronixlab/utils/__init__.py +47 -0
- positronixlab/utils/utils.py +295 -0
- positronixlab-0.1.0.dist-info/METADATA +229 -0
- positronixlab-0.1.0.dist-info/RECORD +128 -0
- positronixlab-0.1.0.dist-info/WHEEL +5 -0
- positronixlab-0.1.0.dist-info/licenses/LICENSE +373 -0
- positronixlab-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""positronixlab: A complete framework to explore modularity in neural networks."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version as _package_version
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
__version__ = _package_version("parallelazy")
|
|
7
|
+
except PackageNotFoundError:
|
|
8
|
+
__version__ = "0.0.0+unknown"
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""External SOTA baselines organized by work (csordas, lepori, neurosurgeon)."""
|
|
2
|
+
|
|
3
|
+
from positronixlab.baselines import csordas, lepori, neurosurgeon
|
|
4
|
+
from positronixlab.baselines.base import (
|
|
5
|
+
MASKABLE_LAYER_TYPES,
|
|
6
|
+
CompositionalDataProvider,
|
|
7
|
+
SOTADatasetSpec,
|
|
8
|
+
SOTAModel,
|
|
9
|
+
SOTAModelSpec,
|
|
10
|
+
SubtaskSplit,
|
|
11
|
+
get_maskable_layer_names,
|
|
12
|
+
)
|
|
13
|
+
from positronixlab.baselines.csordas import CsordasGumbelDiscoverer
|
|
14
|
+
from positronixlab.baselines.lepori import OddOneOutHead
|
|
15
|
+
from positronixlab.baselines.loader import DataProviderLoader, ModelLoader
|
|
16
|
+
from positronixlab.baselines.neurosurgeon import (
|
|
17
|
+
NeuroSurgeonDiscoverer,
|
|
18
|
+
is_neurosurgeon_available,
|
|
19
|
+
wrap_with_neurosurgeon,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"CompositionalDataProvider",
|
|
24
|
+
"CsordasGumbelDiscoverer",
|
|
25
|
+
"DataProviderLoader",
|
|
26
|
+
"MASKABLE_LAYER_TYPES",
|
|
27
|
+
"ModelLoader",
|
|
28
|
+
"NeuroSurgeonDiscoverer",
|
|
29
|
+
"OddOneOutHead",
|
|
30
|
+
"SOTADatasetSpec",
|
|
31
|
+
"SOTAModel",
|
|
32
|
+
"SOTAModelSpec",
|
|
33
|
+
"SubtaskSplit",
|
|
34
|
+
"csordas",
|
|
35
|
+
"get_maskable_layer_names",
|
|
36
|
+
"is_neurosurgeon_available",
|
|
37
|
+
"lepori",
|
|
38
|
+
"neurosurgeon",
|
|
39
|
+
"wrap_with_neurosurgeon",
|
|
40
|
+
]
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""Shared base abstractions for external baseline models and compositional datasets.
|
|
2
|
+
|
|
3
|
+
Intentionally independent of ``positronixlab.pipeline`` / ``parallelazy`` so
|
|
4
|
+
baseline models, datasets, and benchmarks run cleanly in standalone PyTorch
|
|
5
|
+
environments.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from positronixlab.utils.utils import load_data, save_data
|
|
11
|
+
import time
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type
|
|
15
|
+
|
|
16
|
+
import torch
|
|
17
|
+
import torch.nn as nn
|
|
18
|
+
|
|
19
|
+
# Layer types supported by both Atlas observation and weight/neuron masking
|
|
20
|
+
MASKABLE_LAYER_TYPES: Tuple[Type[nn.Module], ...] = (nn.Linear, nn.Conv2d, nn.Conv1d)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _get_maskable_types() -> Tuple[Type[nn.Module], ...]:
|
|
24
|
+
"""Return tuple of maskable PyTorch layer classes (including HF Conv1D if available)."""
|
|
25
|
+
try:
|
|
26
|
+
from transformers.pytorch_utils import Conv1D # type: ignore
|
|
27
|
+
|
|
28
|
+
return MASKABLE_LAYER_TYPES + (Conv1D,)
|
|
29
|
+
except Exception:
|
|
30
|
+
return MASKABLE_LAYER_TYPES
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def get_maskable_layer_names(
|
|
34
|
+
model: nn.Module,
|
|
35
|
+
include_types: Optional[Tuple[Type[nn.Module], ...]] = None,
|
|
36
|
+
exclude_patterns: Iterable[str] = (),
|
|
37
|
+
) -> List[str]:
|
|
38
|
+
"""Return canonical dotted names of maskable layers in *model*."""
|
|
39
|
+
types = include_types if include_types is not None else _get_maskable_types()
|
|
40
|
+
patterns = tuple(exclude_patterns)
|
|
41
|
+
names: List[str] = []
|
|
42
|
+
for name, module in model.named_modules():
|
|
43
|
+
if not name:
|
|
44
|
+
continue
|
|
45
|
+
if isinstance(module, types):
|
|
46
|
+
if any(pat in name for pat in patterns):
|
|
47
|
+
continue
|
|
48
|
+
names.append(name)
|
|
49
|
+
return names
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True)
|
|
53
|
+
class SOTAModelSpec:
|
|
54
|
+
"""Metadata describing a registered baseline model architecture."""
|
|
55
|
+
|
|
56
|
+
arch: str
|
|
57
|
+
source: str # "csordas" | "lepori" | "neurosurgeon" | ...
|
|
58
|
+
task_type: str # "classification" | "odd_one_out" | "seq2seq" | "sequence"
|
|
59
|
+
builder: Callable[..., "SOTAModel"]
|
|
60
|
+
description: str = ""
|
|
61
|
+
default_exclude_patterns: Tuple[str, ...] = field(default_factory=tuple)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class SOTAModel(nn.Module):
|
|
65
|
+
"""Unified base class for baseline architectures loaded through :class:`ModelLoader`.
|
|
66
|
+
|
|
67
|
+
Implements the ``BaseModel`` / ``DeeplearningTrain`` contract (``name``,
|
|
68
|
+
``device``, ``loss(x, y, y_hat)``, ``save()``, ``load()``) plus modularity
|
|
69
|
+
inspection helpers (``maskable_layers()``, ``to_neurosurgeon()``).
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
source: str = "custom"
|
|
73
|
+
arch: str = "custom"
|
|
74
|
+
task_type: str = "classification"
|
|
75
|
+
default_exclude_patterns: Tuple[str, ...] = ()
|
|
76
|
+
|
|
77
|
+
def __init__(
|
|
78
|
+
self,
|
|
79
|
+
name: Optional[str] = None,
|
|
80
|
+
device: Optional[torch.device | str] = None,
|
|
81
|
+
criterion: Optional[nn.Module] = None,
|
|
82
|
+
) -> None:
|
|
83
|
+
super().__init__()
|
|
84
|
+
self.name = name or self.__class__.__name__
|
|
85
|
+
self.timestamp = time.time()
|
|
86
|
+
self.basepath: Optional[str] = None
|
|
87
|
+
self.criterion: nn.Module = criterion if criterion is not None else nn.CrossEntropyLoss()
|
|
88
|
+
self._device = torch.device(device) if device is not None else torch.device("cpu")
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def device(self) -> torch.device:
|
|
92
|
+
for p in self.parameters():
|
|
93
|
+
return p.device
|
|
94
|
+
for b in self.buffers():
|
|
95
|
+
return b.device
|
|
96
|
+
return self._device
|
|
97
|
+
|
|
98
|
+
@device.setter
|
|
99
|
+
def device(self, value: torch.device | str) -> None:
|
|
100
|
+
self._device = torch.device(value)
|
|
101
|
+
|
|
102
|
+
def to(self, *args: Any, **kwargs: Any) -> "SOTAModel":
|
|
103
|
+
result = super().to(*args, **kwargs)
|
|
104
|
+
for p in result.parameters():
|
|
105
|
+
result._device = p.device
|
|
106
|
+
break
|
|
107
|
+
return result
|
|
108
|
+
|
|
109
|
+
def loss(self, x: Any, y: torch.Tensor, y_hat: torch.Tensor) -> torch.Tensor:
|
|
110
|
+
"""Compute task loss compatible with ``DeeplearningTrain``."""
|
|
111
|
+
return self.criterion(y_hat, y)
|
|
112
|
+
|
|
113
|
+
def maskable_layers(
|
|
114
|
+
self,
|
|
115
|
+
exclude_patterns: Optional[Iterable[str]] = None,
|
|
116
|
+
) -> List[str]:
|
|
117
|
+
"""Return canonical dotted names of maskable/observable layers."""
|
|
118
|
+
patterns = (
|
|
119
|
+
self.default_exclude_patterns
|
|
120
|
+
if exclude_patterns is None
|
|
121
|
+
else tuple(exclude_patterns)
|
|
122
|
+
)
|
|
123
|
+
return get_maskable_layer_names(self, exclude_patterns=patterns)
|
|
124
|
+
|
|
125
|
+
def to_neurosurgeon(
|
|
126
|
+
self,
|
|
127
|
+
target_layers: Optional[List[str]] = None,
|
|
128
|
+
mask_method: str = "continuous_sparsification",
|
|
129
|
+
mask_unit: str = "weight",
|
|
130
|
+
mask_hparams: Optional[Dict[str, Any]] = None,
|
|
131
|
+
freeze_base: bool = True,
|
|
132
|
+
l0_lambda: float = 1e-8,
|
|
133
|
+
deepcopy_model: bool = True,
|
|
134
|
+
) -> Any:
|
|
135
|
+
"""Wrap this model in a :class:`NeuroSurgeonWrappedModel`."""
|
|
136
|
+
from positronixlab.baselines.neurosurgeon.adapter import wrap_with_neurosurgeon
|
|
137
|
+
|
|
138
|
+
layers = target_layers if target_layers is not None else self.maskable_layers()
|
|
139
|
+
return wrap_with_neurosurgeon(
|
|
140
|
+
self,
|
|
141
|
+
target_layers=layers,
|
|
142
|
+
mask_method=mask_method,
|
|
143
|
+
mask_unit=mask_unit,
|
|
144
|
+
mask_hparams=mask_hparams,
|
|
145
|
+
freeze_base=freeze_base,
|
|
146
|
+
l0_lambda=l0_lambda,
|
|
147
|
+
deepcopy_model=deepcopy_model,
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
def model_path(self, path: Optional[str | Path] = None) -> str:
|
|
151
|
+
if path is None and self.basepath is None:
|
|
152
|
+
resolved = Path("./checkpoint") / f"{self.name}.pth"
|
|
153
|
+
elif path is None:
|
|
154
|
+
resolved = Path(self.basepath) / f"{self.name}.pth"
|
|
155
|
+
else:
|
|
156
|
+
resolved = Path(path)
|
|
157
|
+
return str(resolved.resolve())
|
|
158
|
+
|
|
159
|
+
def save(self, path: Optional[str | Path] = None, **kwargs: Any) -> str:
|
|
160
|
+
resolved = self.model_path(path)
|
|
161
|
+
save_data(resolved, self.state_dict(), **kwargs)
|
|
162
|
+
return resolved
|
|
163
|
+
|
|
164
|
+
def load(
|
|
165
|
+
self,
|
|
166
|
+
path: Optional[str | Path] = None,
|
|
167
|
+
strict: bool = True,
|
|
168
|
+
map_location: Optional[torch.device | str] = None,
|
|
169
|
+
**kwargs: Any,
|
|
170
|
+
) -> Optional[str]:
|
|
171
|
+
resolved = self.model_path(path)
|
|
172
|
+
loc = map_location if map_location is not None else self.device
|
|
173
|
+
state = load_data(resolved, map_location=loc, **kwargs)
|
|
174
|
+
if state is None:
|
|
175
|
+
return None
|
|
176
|
+
self.load_state_dict(state, strict=strict)
|
|
177
|
+
return resolved
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
from positronixlab.dataprovider.dataprovider import (
|
|
181
|
+
CompositionalDataProvider,
|
|
182
|
+
SubtaskSplit,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
@dataclass(frozen=True)
|
|
187
|
+
class SOTADatasetSpec:
|
|
188
|
+
"""Registry metadata for a baseline benchmark dataset."""
|
|
189
|
+
|
|
190
|
+
name: str
|
|
191
|
+
source: str # "csordas" | "lepori" | ...
|
|
192
|
+
task_type: str # "classification" | "odd_one_out" | "arithmetic"
|
|
193
|
+
builder: Callable[..., CompositionalDataProvider]
|
|
194
|
+
description: str = ""
|
|
195
|
+
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Csordas et al. (ICLR 2021) — are_neural_nets_modular baseline."""
|
|
2
|
+
|
|
3
|
+
from positronixlab.baselines.csordas.datasets import (
|
|
4
|
+
PAPER_CONFIGS,
|
|
5
|
+
AddMulDataProvider,
|
|
6
|
+
AddMulDataset,
|
|
7
|
+
AddMulLoss,
|
|
8
|
+
ClassRemovalDataProvider,
|
|
9
|
+
TupleArithmeticDataProvider,
|
|
10
|
+
TupleArithmeticDataset,
|
|
11
|
+
TupleArithmeticLoss,
|
|
12
|
+
addmul,
|
|
13
|
+
addmul_exact_accuracy,
|
|
14
|
+
cifar10_class_removal,
|
|
15
|
+
deepmind_math,
|
|
16
|
+
permuted_mnist,
|
|
17
|
+
scan,
|
|
18
|
+
tuple_arithmetic,
|
|
19
|
+
tuple_arithmetic_accuracy,
|
|
20
|
+
)
|
|
21
|
+
from positronixlab.baselines.csordas.discoverer import CsordasGumbelDiscoverer
|
|
22
|
+
from positronixlab.baselines.csordas.models import (
|
|
23
|
+
CsordasConvModel,
|
|
24
|
+
CsordasFeedforwardModel,
|
|
25
|
+
CsordasLSTMModel,
|
|
26
|
+
CsordasResNet110,
|
|
27
|
+
CsordasTransformerEncDec,
|
|
28
|
+
strip_csordas_mask_keys,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"AddMulDataProvider",
|
|
33
|
+
"AddMulDataset",
|
|
34
|
+
"AddMulLoss",
|
|
35
|
+
"ClassRemovalDataProvider",
|
|
36
|
+
"CsordasConvModel",
|
|
37
|
+
"CsordasFeedforwardModel",
|
|
38
|
+
"CsordasGumbelDiscoverer",
|
|
39
|
+
"CsordasLSTMModel",
|
|
40
|
+
"CsordasResNet110",
|
|
41
|
+
"CsordasTransformerEncDec",
|
|
42
|
+
"PAPER_CONFIGS",
|
|
43
|
+
"TupleArithmeticDataProvider",
|
|
44
|
+
"TupleArithmeticDataset",
|
|
45
|
+
"TupleArithmeticLoss",
|
|
46
|
+
"addmul",
|
|
47
|
+
"addmul_exact_accuracy",
|
|
48
|
+
"cifar10_class_removal",
|
|
49
|
+
"deepmind_math",
|
|
50
|
+
"permuted_mnist",
|
|
51
|
+
"scan",
|
|
52
|
+
"strip_csordas_mask_keys",
|
|
53
|
+
"tuple_arithmetic",
|
|
54
|
+
"tuple_arithmetic_accuracy",
|
|
55
|
+
]
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
"""Paper-calibrated dataset references for Csordas et al. (ICLR 2021).
|
|
2
|
+
|
|
3
|
+
Reference: ``libs/are_neural_nets_modular/``
|
|
4
|
+
|
|
5
|
+
Every dataset from the paper is exposed here as a typed builder function
|
|
6
|
+
pointing to the canonical ``positronixlab.dataprovider`` class with the
|
|
7
|
+
original paper defaults, alongside :data:`PAPER_CONFIGS`:
|
|
8
|
+
|
|
9
|
+
- :func:`addmul` — Modulo-100 Add vs. Mul (``n_digits=2``, 100k/10k/10k, per-digit loss)
|
|
10
|
+
- :func:`tuple_arithmetic` — Double-Addition / Multiplication (``n_digits=2``, ``n_tuples=2``, 100k/10k/10k)
|
|
11
|
+
- :func:`cifar10_class_removal` — CIFAR-10 leave-one-class-out (10 subtasks ``without_0..9``)
|
|
12
|
+
- :func:`permuted_mnist` — Permuted MNIST (deterministic pixel permutations ``0..10``)
|
|
13
|
+
- :func:`scan` — SCAN sequence-to-sequence benchmark
|
|
14
|
+
- :func:`deepmind_math` — DeepMind Mathematics dataset
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from typing import Dict, Literal, Optional, Unpack
|
|
20
|
+
|
|
21
|
+
from positronixlab.dataprovider.addmul_dataprovider import (
|
|
22
|
+
AddMulDataProvider as _BaseAddMulDataProvider,
|
|
23
|
+
AddMulDataset as _BaseAddMulDataset,
|
|
24
|
+
AddMulLoss,
|
|
25
|
+
TargetMode,
|
|
26
|
+
addmul_exact_accuracy,
|
|
27
|
+
)
|
|
28
|
+
from positronixlab.dataprovider.cifar10_dataprovider import CIFAR10DataProvider
|
|
29
|
+
from positronixlab.dataprovider.class_removal_dataprovider import (
|
|
30
|
+
ClassRemovalDataProvider,
|
|
31
|
+
)
|
|
32
|
+
from positronixlab.dataprovider.config import DataConfig
|
|
33
|
+
from positronixlab.dataprovider.dataprovider import DataProviderOptions
|
|
34
|
+
from positronixlab.dataprovider.math_dataprovider import MathDataProvider
|
|
35
|
+
from positronixlab.dataprovider.permuted_mnist_dataprovider import (
|
|
36
|
+
PermutedMnistDataProvider,
|
|
37
|
+
)
|
|
38
|
+
from positronixlab.dataprovider.scan_dataprovider import SCANDataProvider
|
|
39
|
+
from positronixlab.dataprovider.tuple_arithmetic_dataprovider import (
|
|
40
|
+
TupleArithmeticDataProvider as _BaseTupleArithmeticDataProvider,
|
|
41
|
+
TupleArithmeticDataset as _BaseTupleArithmeticDataset,
|
|
42
|
+
TupleArithmeticLoss,
|
|
43
|
+
tuple_arithmetic_accuracy,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# ---------------------------------------------------------------------------
|
|
48
|
+
# Paper-calibrated builders (original Csordas et al. ICLR 2021 settings)
|
|
49
|
+
# ---------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def addmul(
|
|
53
|
+
*,
|
|
54
|
+
n_digits: int = 2,
|
|
55
|
+
n_train: int = 100_000,
|
|
56
|
+
n_val: int = 10_000,
|
|
57
|
+
n_test: int = 10_000,
|
|
58
|
+
target_mode: TargetMode = "digits",
|
|
59
|
+
batch_size: int = 128,
|
|
60
|
+
**kwargs: Unpack[DataProviderOptions],
|
|
61
|
+
) -> _BaseAddMulDataProvider:
|
|
62
|
+
"""Csordas et al. Add/Mul task (``n_digits=2``, 100k/10k/10k, per-digit CE)."""
|
|
63
|
+
return _BaseAddMulDataProvider(
|
|
64
|
+
n_digits=n_digits,
|
|
65
|
+
n_train=n_train,
|
|
66
|
+
n_val=n_val,
|
|
67
|
+
n_test=n_test,
|
|
68
|
+
target_mode=target_mode,
|
|
69
|
+
batch_size=batch_size,
|
|
70
|
+
**kwargs,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def tuple_arithmetic(
|
|
75
|
+
*,
|
|
76
|
+
n_digits: int = 2,
|
|
77
|
+
n_tuples: int = 2,
|
|
78
|
+
n_train: int = 100_000,
|
|
79
|
+
n_val: int = 10_000,
|
|
80
|
+
n_test: int = 10_000,
|
|
81
|
+
op: Literal["add", "mul"] = "add",
|
|
82
|
+
target_mode: TargetMode = "digits",
|
|
83
|
+
batch_size: int = 128,
|
|
84
|
+
**kwargs: Unpack[DataProviderOptions],
|
|
85
|
+
) -> _BaseTupleArithmeticDataProvider:
|
|
86
|
+
"""Csordas et al. Double-Addition / TupleArithmetic task (``n_digits=2``, ``n_tuples=2``)."""
|
|
87
|
+
return _BaseTupleArithmeticDataProvider(
|
|
88
|
+
n_digits=n_digits,
|
|
89
|
+
n_tuples=n_tuples,
|
|
90
|
+
n_train=n_train,
|
|
91
|
+
n_val=n_val,
|
|
92
|
+
n_test=n_test,
|
|
93
|
+
op=op,
|
|
94
|
+
target_mode=target_mode,
|
|
95
|
+
batch_size=batch_size,
|
|
96
|
+
**kwargs,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def cifar10_class_removal(
|
|
101
|
+
*,
|
|
102
|
+
batch_size: int = 128,
|
|
103
|
+
**kwargs: Unpack[DataProviderOptions],
|
|
104
|
+
) -> ClassRemovalDataProvider:
|
|
105
|
+
"""Csordas et al. CIFAR-10 leave-one-class-out benchmark (``all`` + ``without_0..9``)."""
|
|
106
|
+
cifar = CIFAR10DataProvider(batch_size=batch_size, **kwargs)
|
|
107
|
+
return ClassRemovalDataProvider.from_provider(
|
|
108
|
+
cifar,
|
|
109
|
+
name="csordas:cifar10_class_removal",
|
|
110
|
+
batch_size=batch_size,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def permuted_mnist(
|
|
115
|
+
*,
|
|
116
|
+
permutation: int = 0,
|
|
117
|
+
flatten: bool = True,
|
|
118
|
+
normalize: bool = True,
|
|
119
|
+
batch_size: int = 128,
|
|
120
|
+
**kwargs: Unpack[DataProviderOptions],
|
|
121
|
+
) -> PermutedMnistDataProvider:
|
|
122
|
+
"""Csordas et al. Permuted MNIST transfer/reuse benchmark (permutations ``0..10``)."""
|
|
123
|
+
return PermutedMnistDataProvider(
|
|
124
|
+
permutation=permutation,
|
|
125
|
+
flatten=flatten,
|
|
126
|
+
normalize=normalize,
|
|
127
|
+
batch_size=batch_size,
|
|
128
|
+
**kwargs,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def scan(
|
|
133
|
+
*,
|
|
134
|
+
batch_size: int = 128,
|
|
135
|
+
**kwargs: Unpack[DataProviderOptions],
|
|
136
|
+
) -> SCANDataProvider:
|
|
137
|
+
"""Csordas et al. SCAN compositional generalization dataset."""
|
|
138
|
+
return SCANDataProvider(batch_size=batch_size, **kwargs)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def deepmind_math(
|
|
142
|
+
*,
|
|
143
|
+
batch_size: int = 128,
|
|
144
|
+
**kwargs: Unpack[DataProviderOptions],
|
|
145
|
+
) -> MathDataProvider:
|
|
146
|
+
"""Csordas et al. DeepMind Mathematics dataset."""
|
|
147
|
+
return MathDataProvider(batch_size=batch_size, **kwargs)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
PAPER_CONFIGS: Dict[str, DataConfig] = {
|
|
151
|
+
"addmul": DataConfig(
|
|
152
|
+
name="csordas_addmul",
|
|
153
|
+
dataset_name="addmul",
|
|
154
|
+
batch_size=128,
|
|
155
|
+
dataset_kwargs={
|
|
156
|
+
"n_digits": 2,
|
|
157
|
+
"n_train": 100_000,
|
|
158
|
+
"n_val": 10_000,
|
|
159
|
+
"n_test": 10_000,
|
|
160
|
+
"target_mode": "digits",
|
|
161
|
+
},
|
|
162
|
+
),
|
|
163
|
+
"tuple_arithmetic": DataConfig(
|
|
164
|
+
name="csordas_tuple_arithmetic",
|
|
165
|
+
dataset_name="tuple_arithmetic",
|
|
166
|
+
batch_size=128,
|
|
167
|
+
dataset_kwargs={
|
|
168
|
+
"n_digits": 2,
|
|
169
|
+
"n_tuples": 2,
|
|
170
|
+
"n_train": 100_000,
|
|
171
|
+
"n_val": 10_000,
|
|
172
|
+
"n_test": 10_000,
|
|
173
|
+
"op": "add",
|
|
174
|
+
"target_mode": "digits",
|
|
175
|
+
},
|
|
176
|
+
),
|
|
177
|
+
"permuted_mnist": DataConfig(
|
|
178
|
+
name="csordas_permuted_mnist",
|
|
179
|
+
dataset_name="permuted_mnist",
|
|
180
|
+
batch_size=128,
|
|
181
|
+
flatten=True,
|
|
182
|
+
dataset_kwargs={"permutation": 0, "normalize": True},
|
|
183
|
+
),
|
|
184
|
+
"scan": DataConfig(
|
|
185
|
+
name="csordas_scan",
|
|
186
|
+
dataset_name="scan",
|
|
187
|
+
batch_size=128,
|
|
188
|
+
),
|
|
189
|
+
"deepmind_math": DataConfig(
|
|
190
|
+
name="csordas_deepmind_math",
|
|
191
|
+
dataset_name="deepmind_math",
|
|
192
|
+
batch_size=128,
|
|
193
|
+
),
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
# ---------------------------------------------------------------------------
|
|
198
|
+
# DataProviderLoader compatibility wrappers (single-head integer mode)
|
|
199
|
+
# ---------------------------------------------------------------------------
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
class TupleArithmeticDataset(_BaseTupleArithmeticDataset):
|
|
203
|
+
"""Csordas single-head Double-Addition / Multiplication dataset."""
|
|
204
|
+
|
|
205
|
+
def __init__(
|
|
206
|
+
self,
|
|
207
|
+
n_samples: int = 4096,
|
|
208
|
+
n_tuples: int = 2,
|
|
209
|
+
n_digits: int = 1,
|
|
210
|
+
op: Literal["add", "mul"] = "add",
|
|
211
|
+
restrict_tuple: Optional[int] = None,
|
|
212
|
+
seed: int = 0x12345678,
|
|
213
|
+
) -> None:
|
|
214
|
+
super().__init__(
|
|
215
|
+
split="train",
|
|
216
|
+
n_digits=n_digits,
|
|
217
|
+
n_tuples=n_tuples,
|
|
218
|
+
n_samples=n_samples,
|
|
219
|
+
op=op,
|
|
220
|
+
restrict_tuple=restrict_tuple,
|
|
221
|
+
target_mode="integer",
|
|
222
|
+
seed=seed,
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
class TupleArithmeticDataProvider(_BaseTupleArithmeticDataProvider, register=False):
|
|
227
|
+
"""Csordas Double-Addition / Multiplication compositional benchmark."""
|
|
228
|
+
|
|
229
|
+
def __init__(
|
|
230
|
+
self,
|
|
231
|
+
n_train: int = 4096,
|
|
232
|
+
n_val: int = 512,
|
|
233
|
+
n_test: int = 512,
|
|
234
|
+
n_tuples: int = 2,
|
|
235
|
+
n_digits: int = 1,
|
|
236
|
+
op: Literal["add", "mul"] = "add",
|
|
237
|
+
batch_size: int = 64,
|
|
238
|
+
seed: int = 0x12345678,
|
|
239
|
+
**kwargs: Unpack[DataProviderOptions],
|
|
240
|
+
) -> None:
|
|
241
|
+
super().__init__(
|
|
242
|
+
n_digits=n_digits,
|
|
243
|
+
n_tuples=n_tuples,
|
|
244
|
+
n_train=n_train,
|
|
245
|
+
n_val=n_val,
|
|
246
|
+
n_test=n_test,
|
|
247
|
+
op=op,
|
|
248
|
+
target_mode="integer",
|
|
249
|
+
seed=seed,
|
|
250
|
+
batch_size=batch_size,
|
|
251
|
+
**kwargs,
|
|
252
|
+
)
|
|
253
|
+
self.name = "csordas:tuple_arithmetic"
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
class AddMulDataset(_BaseAddMulDataset):
|
|
257
|
+
"""Csordas single-head Add/Mul synthetic dataset."""
|
|
258
|
+
|
|
259
|
+
def __init__(
|
|
260
|
+
self,
|
|
261
|
+
n_samples: int = 4096,
|
|
262
|
+
n_digits: int = 1,
|
|
263
|
+
restrict_op: Optional[str] = None,
|
|
264
|
+
seed: int = 0x12345678,
|
|
265
|
+
) -> None:
|
|
266
|
+
super().__init__(
|
|
267
|
+
split="train",
|
|
268
|
+
n_samples=n_samples,
|
|
269
|
+
n_digits=n_digits,
|
|
270
|
+
restrict_op=restrict_op,
|
|
271
|
+
target_mode="integer",
|
|
272
|
+
seed=seed,
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
class AddMulDataProvider(_BaseAddMulDataProvider, register=False):
|
|
277
|
+
"""Csordas Add/Mul compositional benchmark with ``full``, ``add``, and ``mul`` subtasks."""
|
|
278
|
+
|
|
279
|
+
def __init__(
|
|
280
|
+
self,
|
|
281
|
+
n_train: int = 4096,
|
|
282
|
+
n_val: int = 512,
|
|
283
|
+
n_test: int = 512,
|
|
284
|
+
n_digits: int = 1,
|
|
285
|
+
batch_size: int = 64,
|
|
286
|
+
seed: int = 0x12345678,
|
|
287
|
+
**kwargs: Unpack[DataProviderOptions],
|
|
288
|
+
) -> None:
|
|
289
|
+
super().__init__(
|
|
290
|
+
n_digits=n_digits,
|
|
291
|
+
n_train=n_train,
|
|
292
|
+
n_val=n_val,
|
|
293
|
+
n_test=n_test,
|
|
294
|
+
target_mode="integer",
|
|
295
|
+
seed=seed,
|
|
296
|
+
batch_size=batch_size,
|
|
297
|
+
**kwargs,
|
|
298
|
+
)
|
|
299
|
+
self.name = "csordas:add_mul"
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
__all__ = [
|
|
303
|
+
"AddMulDataProvider",
|
|
304
|
+
"AddMulDataset",
|
|
305
|
+
"AddMulLoss",
|
|
306
|
+
"ClassRemovalDataProvider",
|
|
307
|
+
"PAPER_CONFIGS",
|
|
308
|
+
"TupleArithmeticDataProvider",
|
|
309
|
+
"TupleArithmeticDataset",
|
|
310
|
+
"TupleArithmeticLoss",
|
|
311
|
+
"addmul",
|
|
312
|
+
"addmul_exact_accuracy",
|
|
313
|
+
"cifar10_class_removal",
|
|
314
|
+
"deepmind_math",
|
|
315
|
+
"permuted_mnist",
|
|
316
|
+
"scan",
|
|
317
|
+
"tuple_arithmetic",
|
|
318
|
+
"tuple_arithmetic_accuracy",
|
|
319
|
+
]
|