Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion jax/experimental/mosaic/gpu/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,14 +677,28 @@ def as_gpu_kernel(
if launch_ctx.is_device_collective and not supports_cross_device_collectives():
raise RuntimeError("Kernel is a cross-device collective but no support is available.")

expected_arg_treedef = jax.tree.structure(in_shape)
expected_arg_tys, expected_arg_treedef = jax.tree.flatten(in_shape)
def _check_args(*args):
arg_treedef = jax.tree.structure(args)
if arg_treedef != expected_arg_treedef:
raise ValueError(
f"Invalid argument structure: expected {expected_arg_treedef}, got"
f" {arg_treedef}, ({args=})"
)
for arg, expected_ty in zip(args, expected_arg_tys):
if arg.shape != expected_ty.shape:
raise ValueError(
f"Argument shape mismatch: expected {expected_ty.shape}, got"
f" {arg.shape}"
)
if arg.dtype != expected_ty.dtype:
hint = ""
if not arg.shape:
hint = f". Hint: cast the scalar to {expected_ty.dtype} explicitly."
raise ValueError(
f"Argument dtype mismatch: expected {expected_ty.dtype}, got"
f" {arg.dtype}{hint}"
)

def bind(*args) -> Any:
return mosaic_gpu_p.bind(*args, module=module, out_types=out_shape)
Expand Down
2 changes: 1 addition & 1 deletion tests/mosaic/gpu_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def test_scalar_argument(self, dtype):
" values read from the 32-bit input buffer to sometimes"
" (nondeterministically) contain garbage.")

scalar = 42
scalar = dtype(42)
expected = np.full((128, 128), scalar, dtype=dtype)

def kernel(ctx, inp, out, _):
Expand Down
Loading