-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathtest_jax.py
More file actions
427 lines (325 loc) · 12.6 KB
/
test_jax.py
File metadata and controls
427 lines (325 loc) · 12.6 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import pytest
try:
import jax
import jax.numpy as jnp
import numpy as np
from jax import export
from jax.sharding import NamedSharding, PartitionSpec as P
import ml_dtypes
except ImportError:
pytest.skip("JAX module not found", allow_module_level=True)
import cuda.tile as ct
from cuda.tile.jax import OutputPlaceholder, InputOutput, cutile_call
@ct.kernel
def _scale(x, y, c: ct.Constant):
bid = ct.bid(0)
ct.store(y, bid, ct.load(x, bid, 1) * c)
@ct.kernel
def _copy(x, y):
bid = ct.bid(0)
ct.store(y, bid, ct.load(x, bid, 1))
@ct.kernel
def _scale_non_const(x, y, c):
bid = ct.bid(0)
ct.store(y, bid, ct.load(x, bid, 1) * c)
@ct.kernel
def _scale_non_const_i64(x, y, c: ct.ScalarInt64):
bid = ct.bid(0)
ct.store(y, bid, ct.load(x, bid, 1) * c)
@ct.kernel
def _negate(x, y):
bid = ct.bid(0)
ct.store(y, bid, -ct.load(x, bid, 1))
@ct.kernel
def _interleaved(c: ct.Constant, x, y):
bid = ct.bid(0)
ct.store(y, bid, ct.load(x, bid, 1) * c)
@ct.kernel
def _split(x, y, z):
bid = ct.bid(0)
val = ct.load(x, bid, 1)
ct.store(y, bid, val)
ct.store(z, bid, -val)
@ct.kernel
def _double_inplace(x):
bid = ct.bid(0)
ct.store(x, bid, ct.load(x, bid, 1) * 2)
@ct.kernel
def _scale_i64(x: ct.IndexedWithInt64, y: ct.IndexedWithInt64, c: ct.Constant):
bid = ct.bid(0)
ct.store(y, bid, ct.load(x, bid, 1) * c)
@ct.kernel
def _mixed_scalars(x, y,
gate: ct.Constant[bool],
mult: ct.Constant[int],
offset: ct.Constant[float]):
bid = ct.bid(0)
v = ct.load(x, bid, 1) * mult + offset
ct.store(y, bid, gate * v)
def _f32(*shape):
return jnp.arange(int(np.prod(shape) or 1), dtype=jnp.float32).reshape(shape)
def _array(*shape, dtype):
return jnp.arange(int(np.prod(shape) or 1), dtype=dtype).reshape(shape)
def test_eager_call():
x = _f32(10)
y = cutile_call((10,), _scale, (x, OutputPlaceholder(x.shape, x.dtype), 3))
np.testing.assert_array_equal(y, 3 * x)
def test_jit_call():
@jax.jit
def graph(x):
return cutile_call((10,), _scale,
(x, OutputPlaceholder(x.shape, x.dtype), 3))
x = _f32(10)
np.testing.assert_array_equal(graph(x), 3 * x)
@pytest.mark.parametrize("dtype", [ml_dtypes.bfloat16,
ml_dtypes.float8_e4m3fn,
ml_dtypes.float8_e5m2,
ml_dtypes.float8_e8m0fnu])
def test_dtype_support(dtype):
@jax.jit
def graph(x):
return cutile_call((10,), _copy, (x, OutputPlaceholder(x.shape, x.dtype)))
# float8_e8m0fnu encodes 2^x and has no representation of 0, so arange
# would round most values to NaN. Use exact powers of two instead.
if dtype == ml_dtypes.float8_e8m0fnu:
x = jnp.asarray(2.0 ** np.arange(10), dtype=dtype)
else:
x = _array(10, dtype=dtype)
try:
np.testing.assert_array_equal(graph(x), x)
except ct.TileUnsupportedFeatureError:
pass
def test_multiple_calls():
@jax.jit
def graph(x):
ph = OutputPlaceholder(x.shape, x.dtype)
y1 = cutile_call((10,), _scale, (x, ph, 1))
y2 = cutile_call((10,), _scale, (y1, ph, 1))
y3 = cutile_call((10,), _scale, (y2, ph, 1))
return y1 + y2 + y3
x = _f32(10)
np.testing.assert_array_equal(graph(x), 3 * x)
text = jax.jit(graph).lower(_f32(10)).as_text()
assert text.count('@cutile_launch') == 3
def test_distinct_kernels_in_one_graph():
"""Two distinct kernels in the same graph each emit a launch op."""
def graph(x):
ph = OutputPlaceholder(x.shape, x.dtype)
y = cutile_call((10,), _scale, (x, ph, 2))
z = cutile_call((10,), _negate, (y, ph))
return z
text = jax.jit(graph).lower(_f32(10)).as_text()
assert text.count('@cutile_launch') == 2
@pytest.mark.parametrize(
"kernel,value,expected_factor",
[
# bool -> packed as int32 (0 or 1)
(_scale_non_const, True, 1),
(_scale_non_const, False, 0),
# python int -> packed as int32 (default)
(_scale_non_const, 4, 4),
(_scale_non_const, -3, -3),
# python float -> packed as float32
(_scale_non_const, 0.5, 0.5),
(_scale_non_const, -1.25, -1.25),
# ct.ScalarInt64 annotation -> packed as int64; value > 2^31 OK
(_scale_non_const_i64, 1 << 32, 1 << 32),
],
ids=["bool_true", "bool_false", "int32_pos", "int32_neg",
"float_half", "float_neg", "int64_large"],
)
def test_runtime_scalar_for_non_constant_param(kernel, value, expected_factor):
"""A Python scalar passed for a non-Constant kernel parameter is forwarded
as a runtime scalar (packed bit-for-bit into the launch arg vector) and
does not bake the value into the cubin."""
x = _f32(10)
@jax.jit(static_argnums=1)
def f(x, c):
return cutile_call((10,), kernel,
(x, OutputPlaceholder(x.shape, x.dtype), c))
expected = np.float32(expected_factor) * np.asarray(x)
np.testing.assert_array_equal(np.asarray(f(x, value)), expected)
def test_args_in_non_standard_order():
x = _f32(10)
def graph(x):
args = (3, x, OutputPlaceholder(x.shape, x.dtype))
return cutile_call((10, 1, 1), _interleaved, args)
y = jax.jit(graph)(x)
np.testing.assert_array_equal(y, 3 * x)
def test_multiple_outputs():
"""Kernel writing to two output buffers returns a tuple of both."""
x = _f32(10)
def graph(x):
ph = OutputPlaceholder(x.shape, x.dtype)
return cutile_call((10, 1, 1), _split, (x, ph, ph))
y, z = jax.jit(graph)(x)
np.testing.assert_array_equal(y, x)
np.testing.assert_array_equal(z, -x)
def test_int64_indexed_array():
"""ct.IndexedWithInt64 annotation flows through to the array constraint
so the kernel is compiled and called with i64 shape/stride."""
@jax.jit
def graph(x):
return cutile_call((10,), _scale_i64,
(x, OutputPlaceholder(x.shape, x.dtype), 3))
x = _f32(10)
np.testing.assert_array_equal(graph(x), 3 * x)
def test_mixed_scalar_constant_types():
"""Bool, int, and float scalars all reach the kernel as baked-in
constants. Each (type, value) tuple compiles to a distinct cubin."""
x = _f32(10)
def run(gate, mult, offset):
@jax.jit
def graph(x):
return cutile_call(
(10,), _mixed_scalars,
(x, OutputPlaceholder(x.shape, x.dtype), gate, mult, offset))
return graph(x)
# bool=True path
y = run(True, 3, 0.5)
np.testing.assert_array_equal(y, 3 * x + 0.5)
# bool=False path: gate * v == 0
y = run(False, 3, 0.5)
np.testing.assert_array_equal(y, jnp.zeros_like(x))
# different int / float values reach the kernel
y = run(True, 2, -1.0)
np.testing.assert_array_equal(y, 2 * x - 1.0)
def test_cubin_id_round_trip(monkeypatch):
"""The 32-byte cubin_id digest round-trips through the FFI u8 array
attribute, including byte values with the high bit set."""
from cuda.tile.jax import _jax as _jax_mod
real_compile = _jax_mod.compile_kernel_cached
def force_high_bytes(kernel, constraints):
function_name, cubin_code, _ = real_compile(kernel, constraints)
return function_name, cubin_code, b"\xff" * 32
# Clear the python-side compile cache so the forced id is actually used
# (otherwise a prior test may have populated it with the real id).
monkeypatch.setattr(_jax_mod, "_COMPILE_CACHE", {})
monkeypatch.setattr(_jax_mod, "compile_kernel_cached", force_high_bytes)
@jax.jit
def graph(x):
return cutile_call((10,), _scale,
(x, OutputPlaceholder(x.shape, x.dtype), 3))
x = _f32(10)
np.testing.assert_array_equal(graph(x), 3 * x)
def test_inplace_update():
"""InputOutput arg aliases its input and output buffers; the kernel
reads and writes the same buffer."""
@jax.jit
def graph(x):
return cutile_call((10,), _double_inplace, (InputOutput(x),))
x = _f32(10)
np.testing.assert_array_equal(graph(x), 2 * x)
def _concurrent_worker(args):
"""Worker for test_concurrent_executions; runs in a thread or a fresh
spawn'd process. Defined at module scope so ProcessPoolExecutor can
pickle it."""
factor, iterations = args
@jax.jit
def graph(x):
return cutile_call((10,), _scale,
(x, OutputPlaceholder(x.shape, x.dtype), factor))
x = _f32(10)
for _ in range(iterations):
y = np.asarray(graph(x))
if not np.array_equal(y, factor * np.asarray(x)):
return f"factor={factor}: mismatch"
return None
def test_concurrent_executions():
"""Multiple workers compiling and running jitted cuTile graphs in
parallel."""
from concurrent.futures import ThreadPoolExecutor
factors = [1, 2, 3, 4, 42, 42, 42, 42] # 4 unique + 4 shared
iterations = 4
args = [(f, iterations) for f in factors]
executor = ThreadPoolExecutor(max_workers=len(factors))
with executor as ex:
results = list(ex.map(_concurrent_worker, args))
failures = [r for r in results if r is not None]
assert not failures, f"worker failures: {failures}"
def test_export_run_in_new_process(tmp_path):
"""Export a jitted cuTile graph, then load and run it in a fresh
Python process. Exercises FFI re-registration and the cubin embedded
in the export, with no shared state from the producer process."""
import os
import subprocess
import sys
import textwrap
@jax.jit
def graph(x):
return cutile_call((10,), _scale,
(x, OutputPlaceholder(x.shape, x.dtype), 3))
disabled = (
export.DisabledSafetyCheck.custom_call("cutile_launch"),
)
x = _f32(10)
exported = export.export(graph, disabled_checks=disabled)(
jax.ShapeDtypeStruct(x.shape, x.dtype))
blob = exported.serialize()
blob_path = tmp_path / "exp.bin"
x_path = tmp_path / "x.npy"
y_path = tmp_path / "y.npy"
blob_path.write_bytes(blob)
np.save(x_path, np.asarray(x))
runner = textwrap.dedent("""
import sys
import numpy as np
import cuda.tile.jax
from jax import export
blob = open(sys.argv[1], 'rb').read()
x = np.load(sys.argv[2])
y = export.deserialize(blob).call(x)
np.save(sys.argv[3], np.asarray(y))
""")
subprocess.run(
[sys.executable, "-c", runner, str(blob_path), str(x_path), str(y_path)],
check=True, env=os.environ.copy(),
)
np.testing.assert_array_equal(np.load(y_path), 3 * np.asarray(x))
# ============== Multigpu Sharding Test ===============
def per_block(scale_a, scale_b, TILE):
"""Build a per-shard cutile_call closure for a 1-D block of size TILE*K."""
def block(a, b):
out = OutputPlaceholder(a.shape, a.dtype)
grid = (a.shape[0] // TILE, 1, 1)
return cutile_call(grid, _scaled_add, (a, b, out, scale_a, scale_b, TILE))
return block
@ct.kernel
def _scaled_add(A, B, C, *,
scale_a: ct.Constant[float],
scale_b: ct.Constant[float],
TILE: ct.Constant[int]):
"""Per-tile elementwise: C = A * scale_a + B * scale_b."""
bid = ct.bid(0)
a = ct.load(A, index=(bid,), shape=(TILE,))
b = ct.load(B, index=(bid,), shape=(TILE,))
ct.store(C, index=(bid,), tile=a * scale_a + b * scale_b)
def _make_inputs(shape, dtype, key):
a_key, b_key = jax.random.split(key, 2)
a = jax.random.normal(a_key, shape, dtype=dtype)
b = jax.random.normal(b_key, shape, dtype=dtype)
return a, b
@pytest.mark.parametrize("n", range(3))
def test_jit_sharding(n):
TILE = 128
ngpu = jax.device_count()
mesh = jax.make_mesh((ngpu,), ("d",))
sharding = NamedSharding(mesh, P("d"))
N = TILE * 8 * ngpu
a, b = _make_inputs((N,), jnp.float32, jax.random.key(1123 + n))
a = jax.device_put(a, sharding)
b = jax.device_put(b, sharding)
@jax.jit(static_argnums=[2, 3])
def compute(a, b, sa, sb):
block = jax.shard_map(per_block(sa, sb, TILE),
mesh=mesh,
in_specs=(P("d"), P("d")),
out_specs=P("d"))
return block(a, b), a * sa + b * sb
c, c_ref = compute(a, b, 1.0, 2.0)
assert jnp.allclose(c, c_ref, atol=1e-5)
c, c_ref = compute(a, b, 3.0, 4.0)
assert jnp.allclose(c, c_ref, atol=1e-5)