-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_marginal_mmle_rust_capability_contract.py
More file actions
190 lines (168 loc) · 5.55 KB
/
Copy pathtest_marginal_mmle_rust_capability_contract.py
File metadata and controls
190 lines (168 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""ABI compatibility contract for Rust marginal MMLE dispatch."""
from __future__ import annotations
import importlib
from types import SimpleNamespace
import numpy as np
import pytest
import fast_mlsirm
from fast_mlsirm.config import FitConfig
fit_module = importlib.import_module("fast_mlsirm.fit")
marginal_module = importlib.import_module("fast_mlsirm.estimators.marginal")
_MISSING = object()
@pytest.mark.parametrize(
"capability",
[
pytest.param(_MISSING, id="absent"),
pytest.param(None, id="none"),
pytest.param(False, id="boolean"),
pytest.param("1", id="string"),
pytest.param(0, id="stale"),
pytest.param(2, id="unsupported-future"),
],
)
def test_public_spatial_mmle_rejects_incompatible_rust_marginal_capability(
monkeypatch: pytest.MonkeyPatch,
capability: object,
) -> None:
"""Absent, malformed, stale, and unsupported ABIs fail before dispatch."""
responses = np.array(
[
[1.0, 1.0, 0.0, 0.0],
[1.0, 0.0, 1.0, 0.0],
[0.0, 1.0, 0.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 1.0, 0.0],
[1.0, 1.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
dtype=np.float64,
)
factor_id = np.zeros(responses.shape[1], dtype=np.int64)
stale_calls: list[tuple[object, ...]] = []
numpy_calls: list[tuple[object, ...]] = []
def stale_fit_marginal(
y: object,
observed: object,
factors: object,
n_persons: object,
n_items: object,
n_dims: object,
latent_dim: object,
model: object,
eps_distance: object,
) -> None:
stale_calls.append(
(
y,
observed,
factors,
n_persons,
n_items,
n_dims,
latent_dim,
model,
eps_distance,
)
)
def reject_numpy_reference(
*args: object, **_kwargs: object
) -> dict[str, object]:
numpy_calls.append(args)
raise AssertionError(
"public marginal MMLE entered NumPy production arithmetic"
)
core_attributes: dict[str, object] = {
"fit_marginal": stale_fit_marginal,
}
if capability is not _MISSING:
core_attributes["MARGINAL_CAPABILITY_VERSION"] = capability
stale_core = SimpleNamespace(**core_attributes)
monkeypatch.setattr(fast_mlsirm, "_core", stale_core, raising=False)
monkeypatch.setattr(fit_module, "resolve_backend", lambda _backend: "rust")
monkeypatch.setattr(
marginal_module, "fit_marginal_numpy", reject_numpy_reference
)
with pytest.raises(
RuntimeError,
match=r"compiled Rust core marginal ABI capability",
):
fit_module.fit(
responses,
factor_id,
FitConfig(
estimator="mmle",
model="MLS2PLM",
backend="rust",
latent_dim=1,
max_iter=1,
n_restarts=1,
),
)
assert stale_calls == []
assert numpy_calls == []
def test_public_spatial_mmle_rejects_versioned_callable_with_stale_signature(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A version-1 callable that rejects current keywords must not leak TypeError."""
responses = np.array(
[
[1.0, 1.0, 0.0, 0.0],
[1.0, 0.0, 1.0, 0.0],
[0.0, 1.0, 0.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 1.0, 0.0],
[1.0, 1.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
],
dtype=np.float64,
)
factor_id = np.zeros(responses.shape[1], dtype=np.int64)
stale_calls: list[tuple[object, ...]] = []
numpy_calls: list[tuple[object, ...]] = []
def stale_keyword_fit_marginal(*args: object, **_kwargs: object) -> None:
stale_calls.append(args)
raise TypeError(
"fit_marginal() got an unexpected keyword argument 'pop_kind'"
)
def reject_numpy_reference(
*args: object, **_kwargs: object
) -> dict[str, object]:
numpy_calls.append(args)
raise AssertionError(
"public marginal MMLE entered NumPy production arithmetic"
)
stale_core = SimpleNamespace(
MARGINAL_CAPABILITY_VERSION=fit_module._MARGINAL_CAPABILITY_VERSION,
fit_marginal=stale_keyword_fit_marginal,
)
monkeypatch.setattr(fast_mlsirm, "_core", stale_core, raising=False)
monkeypatch.setattr(fit_module, "resolve_backend", lambda _backend: "rust")
monkeypatch.setattr(
marginal_module, "fit_marginal_numpy", reject_numpy_reference
)
with pytest.raises(
RuntimeError,
match=r"compiled Rust core marginal ABI capability",
):
fit_module.fit(
responses,
factor_id,
FitConfig(
estimator="mmle",
model="MLS2PLM",
backend="rust",
latent_dim=1,
max_iter=1,
n_restarts=1,
),
)
assert stale_calls
assert numpy_calls == []
def test_compiled_rust_core_exports_current_marginal_capability() -> None:
"""The native module publishes the exact Python-supported ABI version."""
core = importlib.import_module("fast_mlsirm._core")
capability = core.MARGINAL_CAPABILITY_VERSION
assert type(capability) is int
assert capability == fit_module._MARGINAL_CAPABILITY_VERSION