-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_display_names.py
More file actions
278 lines (239 loc) · 10 KB
/
test_display_names.py
File metadata and controls
278 lines (239 loc) · 10 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""Semantic unit tests for Relation.to_display_name().
These tests verify that key *meaning* tokens appear in the output for each
relation type given a known params list. They do NOT test inference logic —
the params are constructed directly, so the tests remain stable even if the
inference algorithm changes.
"""
import pytest
from traincheck.invariant.base_cls import (
_NOT_SET,
APIParam,
InputOutputParam,
VarTypeParam,
)
from traincheck.invariant.consistency_relation import ConsistencyRelation
from traincheck.invariant.consistency_transient_vars import (
ConsistentInputOutputRelation,
ConsistentOutputRelation,
ThresholdRelation,
)
from traincheck.invariant.contain_relation import APIContainRelation
from traincheck.invariant.cover_relation import FunctionCoverRelation
from traincheck.invariant.DistinctArgumentRelation import DistinctArgumentRelation
from traincheck.invariant.lead_relation import FunctionLeadRelation
class TestAPIContainRelationDisplayName:
def test_state_transition(self):
params = [
APIParam("torch.optim.optimizer.Optimizer.zero_grad"),
VarTypeParam(
"torch.nn.Parameter", "grad", pre_value="non_zero", post_value=None
),
]
name = APIContainRelation.to_display_name(params)
assert name is not None
assert "zero_grad" in name
assert "grad" in name
assert "non" in name.lower() # "non-zero"
def test_api_calls_api(self):
params = [
APIParam("torch.optim.optimizer.Optimizer.step"),
APIParam("torch.optim.adadelta.adadelta"),
]
name = APIContainRelation.to_display_name(params)
assert name is not None
assert "step" in name
assert "adadelta" in name
def test_const_value(self):
params = [
APIParam("torch.nn.modules.module.Module.forward"),
VarTypeParam("torch.nn.Parameter", "requires_grad", const_value=True),
]
name = APIContainRelation.to_display_name(params)
assert name is not None
assert "forward" in name
assert "requires_grad" in name
def test_post_value_non_zero_normalized(self):
"""non_zero post-value should render as 'non-zero', not 'non_zero'."""
params = [
APIParam("torch.optim.sgd.SGD.step"),
VarTypeParam(
"torch.nn.Parameter",
"data",
pre_value="non_zero",
post_value="non_zero",
),
]
name = APIContainRelation.to_display_name(params)
assert name is not None
assert "non_zero" not in name
assert "non-zero" in name
def test_traincheck_internal_attr_hidden(self):
"""Attributes starting with _TRAINCHECK_ are internal proxy IDs and should be filtered."""
params = [
APIParam("torch.optim.sgd.SGD.step"),
VarTypeParam(
"torch.nn.Parameter",
"_TRAINCHECK_grad_ID",
pre_value="above_zero",
post_value="above_zero",
),
]
assert APIContainRelation.to_display_name(params) is None
def test_returns_none_for_empty_params(self):
assert APIContainRelation.to_display_name([]) is None
def test_returns_none_for_single_param(self):
assert APIContainRelation.to_display_name([APIParam("torch.foo")]) is None
class TestConsistencyRelationDisplayName:
def test_basic(self):
params = [VarTypeParam("torch.nn.Parameter", "grad")]
name = ConsistencyRelation.to_display_name(params)
assert name is not None
assert "Parameter" in name
assert "grad" in name
assert any(w in name.lower() for w in ("consistent", "stay", "step"))
def test_returns_none_for_empty(self):
assert ConsistencyRelation.to_display_name([]) is None
def test_returns_none_for_non_vartype(self):
assert ConsistencyRelation.to_display_name([APIParam("torch.foo.bar")]) is None
class TestFunctionCoverRelationDisplayName:
def test_cover_direction(self):
params = [
APIParam("torch.distributed.is_initialized"),
APIParam("torch.nn.modules.module.Module.eval"),
]
name = FunctionCoverRelation.to_display_name(params)
assert name is not None
assert "is_initialized" in name
assert "eval" in name
assert any(w in name.lower() for w in ("occurs", "cover", "when"))
def test_returns_none_for_insufficient_params(self):
assert FunctionCoverRelation.to_display_name([APIParam("torch.foo")]) is None
class TestFunctionLeadRelationDisplayName:
def test_ordering(self):
params = [
APIParam("torch.Tensor.backward"),
APIParam("torch.optim.optimizer.Optimizer.step"),
]
name = FunctionLeadRelation.to_display_name(params)
assert name is not None
assert "backward" in name
assert "step" in name
assert any(w in name.lower() for w in ("precede", "before", "lead"))
def test_merged_three_params(self):
"""Merged lead invariants can have 3 APIParams; display uses first and last."""
params = [
APIParam("torch.Tensor.backward"),
APIParam("torch.optim.optimizer.Optimizer.zero_grad"),
APIParam("torch.optim.optimizer.Optimizer.step"),
]
name = FunctionLeadRelation.to_display_name(params)
assert name is not None
assert "backward" in name
assert "step" in name
def test_returns_none_for_single_param(self):
assert FunctionLeadRelation.to_display_name([APIParam("torch.foo")]) is None
class TestDistinctArgumentRelationDisplayName:
def test_basic(self):
params = [APIParam("torch.nn.init.normal_")]
name = DistinctArgumentRelation.to_display_name(params)
assert name is not None
assert "normal_" in name
assert any(w in name.lower() for w in ("distinct", "different", "argument"))
def test_returns_none_for_empty(self):
assert DistinctArgumentRelation.to_display_name([]) is None
def test_returns_none_for_non_api_param(self):
params = [VarTypeParam("torch.nn.Parameter", "grad")]
assert DistinctArgumentRelation.to_display_name(params) is None
class TestConsistentOutputRelationDisplayName:
def test_with_const_value(self):
params = [
APIParam("torch.nn.functional.relu"),
VarTypeParam("torch.Tensor", "dtype", const_value="float32"),
]
name = ConsistentOutputRelation.to_display_name(params)
assert name is not None
assert "relu" in name
assert "dtype" in name
assert "float32" in name
assert any(w in name.lower() for w in ("consistent", "return"))
def test_without_const_value(self):
params = [
APIParam("torch.nn.functional.relu"),
VarTypeParam("torch.Tensor", "ndim"),
]
name = ConsistentOutputRelation.to_display_name(params)
assert name is not None
assert "relu" in name
assert "ndim" in name
def test_returns_none_for_insufficient_params(self):
assert ConsistentOutputRelation.to_display_name([APIParam("torch.foo")]) is None
class TestConsistentInputOutputRelationDisplayName:
def test_basic(self):
in_p = InputOutputParam(
name="input",
index=0,
type="torch.Tensor",
additional_path=("itemsize",),
api_name="kaiming_uniform_",
is_input=True,
)
out_p = InputOutputParam(
name="output",
index=0,
type="torch.Tensor",
additional_path=("ndim",),
api_name="kaiming_uniform_",
is_input=False,
)
api_p = APIParam("torch.nn.init.kaiming_uniform_")
name = ConsistentInputOutputRelation.to_display_name([in_p, api_p, out_p])
assert name is not None
assert "kaiming_uniform_" in name
assert "itemsize" in name
assert "ndim" in name
assert "input" in name.lower()
assert "output" in name.lower()
def test_returns_none_for_insufficient_params(self):
api_p = APIParam("torch.foo")
assert ConsistentInputOutputRelation.to_display_name([api_p]) is None
class TestThresholdRelationDisplayName:
def _make_output_param(self, api_name: str) -> InputOutputParam:
return InputOutputParam(
name="output_tensors",
index=0,
type="torch.Tensor",
additional_path=("value",),
api_name=api_name,
is_input=False,
)
def _make_threshold_param(self, name: str, api_name: str) -> InputOutputParam:
return InputOutputParam(
name=name,
index=None,
type="float",
additional_path=None,
api_name=api_name,
is_input=True,
)
def test_min_threshold_gte(self):
"""params=[output, api, threshold] → output ≥ threshold."""
api_p = APIParam("torch.optim.optimizer.Optimizer.step")
out_p = self._make_output_param("Optimizer.step")
thresh_p = self._make_threshold_param("lr", "Optimizer.step")
name = ThresholdRelation.to_display_name([out_p, api_p, thresh_p])
assert name is not None
assert "Optimizer.step" in name
assert "lr" in name
assert "≥" in name
def test_max_threshold_lte(self):
"""params=[threshold, api, output] → output ≤ threshold."""
api_p = APIParam("torch.optim.optimizer.Optimizer.step")
out_p = self._make_output_param("Optimizer.step")
thresh_p = self._make_threshold_param("lr", "Optimizer.step")
name = ThresholdRelation.to_display_name([thresh_p, api_p, out_p])
assert name is not None
assert "Optimizer.step" in name
assert "lr" in name
assert "≤" in name
def test_returns_none_for_insufficient_params(self):
assert ThresholdRelation.to_display_name([APIParam("torch.foo")]) is None