-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy patherror_check_test.py
More file actions
410 lines (324 loc) · 12.5 KB
/
Copy patherror_check_test.py
File metadata and controls
410 lines (324 loc) · 12.5 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# Copyright 2025 The JAX Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import traceback
from absl.testing import absltest
from absl.testing import parameterized
import jax
from jax._src import config
from jax._src import error_check
from jax._src import mesh as mesh_lib
from jax._src import test_util as jtu
import jax.export
import jax.numpy as jnp
from jax.sharding import NamedSharding, PartitionSpec as P
JaxValueError = error_check.JaxValueError
config.parse_flags_with_absl()
jtu.request_cpu_devices(4)
# TODO: AOT tests fails with the tracer leak checker.
# Re-enable once https://github.com/jax-ml/jax/issues/27315 is fixed.
# @jtu.with_config(jax_check_tracer_leaks=True)
class ErrorCheckTests(jtu.JaxTestCase):
@parameterized.product(jit=[True, False])
def test_error_check(self, jit):
def f(x):
error_check.set_error_if(x <= 0, "x must be greater than 0")
return x + 1
if jit:
f = jax.jit(f)
x = jnp.full((4,), -1, dtype=jnp.int32)
f(x)
with self.assertRaisesRegex(JaxValueError, "x must be greater than 0"):
error_check.raise_if_error()
@parameterized.product(jit=[True, False])
def test_error_check_no_error(self, jit):
def f(x):
error_check.set_error_if(x <= 0, "x must be greater than 0")
return x + 1
if jit:
f = jax.jit(f)
x = jnp.full((4,), 1, dtype=jnp.int32)
f(x)
error_check.raise_if_error() # should not raise error
@parameterized.product(jit=[True, False])
def test_error_check_should_report_the_first_error(self, jit):
def f(x):
error_check.set_error_if(x >= 1, "x must be less than 1 in f")
return x + 1
def g(x):
error_check.set_error_if(x >= 1, "x must be less than 1 in g")
return x + 1
if jit:
f = jax.jit(f)
g = jax.jit(g)
x = jnp.full((4,), 0, dtype=jnp.int32)
x = f(x) # check passes, so it should not set error
x = g(x) # check fails. so it should set error
_ = f(x) # check fails, but should not override the error
with self.assertRaisesRegex(JaxValueError, "x must be less than 1 in g"):
error_check.raise_if_error()
@parameterized.product(jit=[True, False])
def test_raise_if_error_clears_error(self, jit):
def f(x):
error_check.set_error_if(x <= 0, "x must be greater than 0 in f")
return x + 1
def g(x):
error_check.set_error_if(x <= 0, "x must be greater than 0 in g")
return x + 1
if jit:
f = jax.jit(f)
g = jax.jit(g)
x = jnp.full((4,), -1, dtype=jnp.int32)
f(x)
with self.assertRaisesRegex(JaxValueError, "x must be greater than 0 in f"):
error_check.raise_if_error()
error_check.raise_if_error() # should not raise error
g(x)
with self.assertRaisesRegex(JaxValueError, "x must be greater than 0 in g"):
error_check.raise_if_error()
@parameterized.product(jit=[True, False])
def test_error_includes_traceback(self, jit):
def function_that_triggers_error_for_traceback_test(x):
error_check.set_error_if( # This line must be included in the traceback.
x <= 0, "x must be greater than 0"
)
return x + 1
if jit:
function_that_triggers_error_for_traceback_test = jax.jit(
function_that_triggers_error_for_traceback_test
)
x = jnp.zeros((4,), dtype=jnp.int32)
function_that_triggers_error_for_traceback_test(x)
tb_string = ""
try:
error_check.raise_if_error()
except JaxValueError as e:
tb_string = traceback.format_tb(e.__traceback__)
tb_string = "".join(tb_string)
self.assertIn("function_that_triggers_error_for_traceback_test", tb_string)
self.assertIn("This line must be included in the traceback", tb_string)
@parameterized.product(jit=[True, False])
def test_error_check_works_with_cond(self, jit):
def f(x):
error_check.set_error_if(x == 0, "x must be non-zero in f")
return x + 1
def g(x):
error_check.set_error_if(x == 0, "x must be non-zero in g")
return x + 1
def body(pred, x):
return jax.lax.cond(pred, f, g, x)
if jit:
body = jax.jit(body)
x = jnp.zeros((4,), dtype=jnp.int32)
_ = body(jnp.bool_(True), x)
with self.assertRaisesRegex(JaxValueError, "x must be non-zero in f"):
error_check.raise_if_error()
_ = body(jnp.bool_(False), x)
with self.assertRaisesRegex(JaxValueError, "x must be non-zero in g"):
error_check.raise_if_error()
@parameterized.product(jit=[True, False])
def test_error_check_works_with_while_loop(self, jit):
def f(x):
error_check.set_error_if(x >= 10, "x must be less than 10")
return x + 1
def body(x):
return jax.lax.while_loop(lambda x: (x < 10).any(), f, x)
if jit:
body = jax.jit(body)
x = jnp.arange(4, dtype=jnp.int32)
_ = body(x)
with self.assertRaisesRegex(JaxValueError, "x must be less than 10"):
error_check.raise_if_error()
@parameterized.product(jit=[True, False])
def test_error_check_works_with_scan(self, jit):
def f(carry, x):
error_check.set_error_if(x >= 4, "x must be less than 4")
return carry + x, x + 1
def body(init, xs):
return jax.lax.scan(f, init=init, xs=xs)
if jit:
body = jax.jit(body)
init = jnp.int32(0)
xs = jnp.arange(5, dtype=jnp.int32)
_ = body(init, xs)
with self.assertRaisesRegex(JaxValueError, "x must be less than 4"):
error_check.raise_if_error()
xs = jnp.arange(4, dtype=jnp.int32)
_ = body(init, xs)
error_check.raise_if_error() # should not raise error
@parameterized.product(jit=[True, False])
def test_raise_if_error_fails_in_traced_context(self, jit):
def f(x):
error_check.set_error_if(x <= 0, "x must be greater than 0")
return x + 1
if jit:
f = jax.jit(f)
x = jnp.full((4,), 1, dtype=jnp.int32)
f(x)
with self.assertRaises(
ValueError,
msg=(
"raise_if_error() should not be called within a traced context,"
" such as within a jitted function."
),
):
jax.jit(error_check.raise_if_error)()
@parameterized.product(jit=[True, False])
@jtu.with_explicit_mesh((2, 2), ("x", "y"))
def test_error_check_explicit_mode(self, mesh, jit):
def f(x):
error_check.set_error_if(x <= 0, "x must be greater than 0")
return x + 1
if jit:
f = jax.jit(f)
with error_check.error_checking_context():
x = jnp.full((4, 4), -1, dtype=jnp.int32)
f(x)
with self.assertRaisesRegex(JaxValueError, "x must be greater than 0"):
error_check.raise_if_error()
sharding = NamedSharding(mesh, P("x", "y"))
with error_check.error_checking_context():
y = jnp.full((4, 4), -1, dtype=jnp.int32, device=sharding)
f(y)
with self.assertRaisesRegex(JaxValueError, "x must be greater than 0"):
error_check.raise_if_error()
# The unsharded version of `f` should still be able to check errors after
# exiting the error checking context.
f(x)
with self.assertRaisesRegex(JaxValueError, "x must be greater than 0"):
error_check.raise_if_error()
@parameterized.product(jit=[True, False])
@jtu.with_explicit_mesh(
(2, 2),
("x", "y"),
axis_types=(mesh_lib.AxisType.Auto, mesh_lib.AxisType.Auto),
)
@jtu.ignore_warning(
message=(
"When at least one mesh axis of `pred` is in auto mode, calling"
" `set_error_if` will cause implicit communication between devices."
" To avoid this, consider converting the mesh axis in auto mode to"
" explicit mode."
),
category=RuntimeWarning,
)
def test_error_check_auto_mode(self, jit, mesh):
def f(x):
error_check.set_error_if(x <= 0, "x must be greater than 0")
return x + 1
if jit:
f = jax.jit(f)
with error_check.error_checking_context():
sharding = NamedSharding(mesh, P("x", "y"))
x = jnp.full((4, 4), -1, dtype=jnp.int32, device=sharding)
f(x)
with self.assertRaisesRegex(JaxValueError, "x must be greater than 0"):
error_check.raise_if_error()
def test_error_check_aot(self):
def run_export():
def f(x):
error_check.set_error_if(x <= 0, "x must be greater than 0")
return x + 1
f = jax.jit(error_check.wrap_for_export(jax.jit(f)))
x = jax.ShapeDtypeStruct((), jnp.float32)
serialized = jax.export.export(f)(x).serialize()
return serialized
def run_import(serialized):
f = jax.export.deserialize(serialized).call
f = jax.jit(error_check.unwrap_from_import(jax.jit(f)))
x = jnp.float32(-3.)
_ = f(x)
with self.assertRaisesRegex(JaxValueError, "x must be greater than 0"):
error_check.raise_if_error()
serialized = run_export()
run_import(serialized)
def test_error_check_aot_includes_traceback(self):
def run_export():
def function_that_triggers_error_for_traceback_test(x):
error_check.set_error_if( # This line must be included in the traceback
x <= 0, "x must be greater than 0"
)
return x + 1
f = jax.jit(
error_check.wrap_for_export(
jax.jit(function_that_triggers_error_for_traceback_test)
)
)
x = jax.ShapeDtypeStruct((), jnp.float32)
serialized = jax.export.export(f)(x).serialize()
return serialized
def run_import(serialized):
f = jax.export.deserialize(serialized).call
f = jax.jit(error_check.unwrap_from_import(jax.jit(f)))
x = jnp.float32(-3.0)
_ = f(x)
msg = ""
try:
error_check.raise_if_error()
except JaxValueError as e:
msg = str(e)
self.assertIn("function_that_triggers_error_for_traceback_test", msg)
self.assertIn("This line must be included in the traceback", msg)
serialized = run_export()
run_import(serialized)
def test_error_check_aot_should_not_override_existing_error(self):
def f1(x):
error_check.set_error_if(x <= 0, "x must be greater than 0 in f1")
return x + 1
def run_export():
def f2(x):
error_check.set_error_if(x <= 0, "x must be greater than 0 in f2")
return x + 1
f2 = jax.jit(error_check.wrap_for_export(jax.jit(f2)))
x = jax.ShapeDtypeStruct((), jnp.float32)
serialized = jax.export.export(f2)(x).serialize()
return serialized
def run_import(serialized):
f2 = jax.export.deserialize(serialized).call
f2 = jax.jit(error_check.unwrap_from_import(jax.jit(f2)))
return f2
x = jnp.float32(-3.)
_ = f1(x) # check fails. so it should set error
serialized = run_export()
f2 = run_import(serialized)
_ = f2(x) # check fails, but should not override the error
with self.assertRaisesRegex(
JaxValueError, "x must be greater than 0 in f1"
):
error_check.raise_if_error()
def test_error_check_aot_invalid_error_code_regression(self):
"""Regression test for issue #34370.
Verifies that invalid error codes from corrupted AOT serialization data
are handled gracefully with a standard error message, rather than causing
an IndexError.
https://github.com/jax-ml/jax/issues/34370
"""
def make_corrupted_function():
"""Create a function that simulates corrupted AOT import data."""
def corrupted_fn(x):
# Return an _ErrorClass with an invalid error code (999)
# that is out of bounds of the error_list (which is empty here)
invalid_error_code = jnp.uint32(999)
empty_error_list = []
return x + 1, error_check._ErrorClass(invalid_error_code, empty_error_list)
return corrupted_fn
corrupted_fn = make_corrupted_function()
unwrapped_fn = error_check.unwrap_from_import(corrupted_fn)
x = jnp.float32(1.0)
_ = unwrapped_fn(x)
# Should raise JaxValueError with the standard error message for
# invalid error codes, NOT an IndexError
with self.assertRaisesRegex(JaxValueError, "unknown error"):
error_check.raise_if_error()
if __name__ == "__main__":
absltest.main(testLoader=jtu.JaxTestLoader())