Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make debug.print work with static args. Fixes: https://github.com/google/jax/issues/23600 #23683

Merged
merged 1 commit into from
Sep 18, 2024
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
26 changes: 22 additions & 4 deletions jax/_src/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from jax._src.lib.mlir.dialects import hlo
from jax._src.sharding import Sharding
from jax._src.sharding_impls import NamedSharding, parse_flatten_op_sharding
from jax._src.api_util import shaped_abstractify
from jax._src.state import discharge as state_discharge

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -256,12 +257,29 @@ def debug_callback(callback: Callable[..., None], *args: Any,
raise TypeError("first argument to jax.debug.callback must be callable, "
f"but got an object of type {type(callback)}")
flat_args, in_tree = tree_util.tree_flatten((args, kwargs))
effect = ordered_debug_effect if ordered else debug_effect
def _flat_callback(*flat_args):
args, kwargs = tree_util.tree_unflatten(in_tree, flat_args)
static_args, dyn_args = {}, []
for i, a in enumerate(flat_args):
try:
shaped_abstractify(a)
dyn_args.append(a)
except (AssertionError, TypeError):
static_args[i] = a

def _flat_callback(*dyn_args):
all_args = [None] * (len(static_args) + len(dyn_args))
di = iter(dyn_args)
for i in range(len(all_args)):
if i in static_args:
all_args[i] = static_args[i]
else:
all_args[i] = next(di)
assert next(di, None) is None
args, kwargs = tree_util.tree_unflatten(in_tree, all_args)
callback(*args, **kwargs)
return ()
debug_callback_p.bind(*flat_args, callback=_flat_callback, effect=effect)

effect = ordered_debug_effect if ordered else debug_effect
debug_callback_p.bind(*dyn_args, callback=_flat_callback, effect=effect)

class _DebugPrintFormatChecker(string.Formatter):

Expand Down
12 changes: 12 additions & 0 deletions tests/debugging_primitives_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ def f(x):
jax.effects_barrier()
self.assertEqual(output(), "x: 2\n")

def test_static_args(self):
@jax.jit
def f(arr):
jax.debug.print("arr {array}, dtype: {dtype}, arr {array2}",
array=arr, dtype=arr.dtype, array2=arr)
arr = jnp.array([1, 2, 3], dtype=jnp.float32)
with jtu.capture_stdout() as output:
f(arr)
jax.effects_barrier()
self.assertEqual(
output(), "arr [1. 2. 3.], dtype: float32, arr [1. 2. 3.]\n")

def test_debug_print_works_with_named_format_strings(self):
def f(x):
debug_print('x: {x}', x=x)
Expand Down
Loading