catalyst.debug.print

print(fmt, *args, **kwargs)[source]

A qjit() compatible print function for printing values at runtime.

This print function allows printing of values at runtime, unlike usage of standard Python print which will print values at program capture/compile time.

debug.print is a minimal wrapper around callback() which calls Python’s builtins.print function, and thus will use the same formatting styles as Python’s built-in print function.

Parameters
  • fmt (str) – The string to be printed. Note that this may also be a format string used to format input arguments (for example cost={x}), similar to those permitted by str.format. See the Python docs on string formatting and format string syntax.

  • **args – Arguments to be passed to the format string.

  • **kwargs – Keyword arguments to be passed to the format string.

Example

>>> @qjit
... def f(a, b, c):
...     debug.print("c={c} b={b} a={a}", a=a, b=b, c=c)
>>> f(1, 2, 3)
c=3 b=2 a=1

In addition to passing keyword arguments to the format string, we can also pass arguments positionally:

>>> @qjit
... @grad
... def f(x, y):
...     debug.print("Value of x = {0:.2f}", x)
...     return x * jnp.sin(y)
>>> f(0.543, 0.23)
Value of x = 0.54
Array(0.22797752, dtype=float64)

Note that during differentiation, printing will only be executed during the forward pass.

Note

Using Python f-strings as the fmt string will not work as expected since they will be treated as Python objects.

This means that array values embedded in them will have their compile-time representation printed, instead of actual data.