Error handling
jax compiles every allocation size in advance, so jz-tree has to estimate how much space interaction lists, tree nodes or communication buffers need. The defaults work for typical inputs, but far away from them an allocation may turn out too small. Instead of overflowing, the computation then stops with an error that says which configuration value to increase and by how much. This guide shows how to read such errors, catch them and fix the configuration automatically.
What an error looks like
Searching 2000 neighbours needs a much larger interaction list than the defaults provide:
import jax
import jztree as jz
pos = jax.random.uniform(jax.random.key(0), shape=(50000, 3))
rnn, inn = jz.knn.knn.jit(pos, k=2000)
rnn.block_until_ready()
JaxRuntimeError: FAILED_PRECONDITION: Check failed (trace-time call chain, most recent call last):
File "<ipython-input>", line 5, in <module>
rnn, inn = jz.knn.knn.jit(pos, k=2000)
File ".../jztree/knn.py", line 334, in knn
ilist = _knn_dual_walk(th, k, boxsize=boxsize, alloc_fac_ilist=cfg.alloc_fac_ilist,
File ".../jztree/knn.py", line 213, in _knn_dual_walk
ilist, counts = jax.lax.fori_loop(0, nlevels, handle_level,
File ".../jztree/knn.py", line 201, in handle_level
ilist = _knn_node2node_ilist(ilist, parent_spl, node_data, k=k, boxsize=boxsize,
File ".../jztree/knn.py", line 112, in _knn_node2node_ilist
ispl = ispl + raise_if(ispl[-1] > ilist.size(),
CheckFailed: The interaction list allocation is too small (need: 633252, have: 533760)
Fix: increase alloc_fac_ilist by a factor >= 1.19 (jz.apply_fix(cfg, error))
[jztree-check 4: 633252, 533760, 1.18639839] [executable_name='jit_knn']
The message consists of:
the call chain at trace time, i.e. where the failing check was traced inside your code and inside jz-tree (most recent call last, like a Python traceback),
what failed, with the required (
need) and the allocated (have) size,the suggested fix: the configuration value to increase and the minimal factor,
a machine-readable tag that
jztree.checks.check_info()uses.
The check stops the computation before any operation that depends on it runs, so nothing
overflows and the GPU stays usable. The error is an ordinary RuntimeError (more precisely a
jax.errors.JaxRuntimeError). Since jax dispatches asynchronously, it appears when the result
is used or awaited, e.g. at block_until_ready(), and not necessarily at the call itself.
Fixing the configuration
You can increase the value yourself, e.g. jz.config.KNNConfig(alloc_fac_ilist=400), or let
jztree.checks.apply_fix() do it. It returns a copy of the configuration with the
responsible value increased by the required factor plus a margin (10% by default):
cfg = jz.config.KNNConfig()
try:
rnn, inn = jz.knn.knn.jit(pos, k=2000, cfg=cfg)
rnn.block_until_ready()
except RuntimeError as e:
cfg = jz.apply_fix(cfg, e)
Output format (schematic; capacities depend on the input and configuration):
jztree: cfg.alloc_fac_ilist: <old value> -> <new value> (need <required>, have <capacity>)
The value may also be in a nested configuration, e.g. cfg.tree.alloc_fac_nodes; apply_fix
finds it by name. Radius queries work the same way with jz.config.QueryBallConfig, whose
alloc_fac_neighbours sets the capacity of the neighbour lists returned by query_ball. Errors that are not failed checks are raised again unchanged, and a
ValueError is raised if the check has no automatic fix or the configuration does not contain
the value.
Since a computation stops at the first failing check, another allocation may turn out too small
in the next attempt. jztree.checks.run_with_fixes() retries until the computation
succeeds and returns the result together with the working configuration, which you can reuse for
similar inputs:
(rnn, inn), cfg = jz.run_with_fixes(
lambda cfg: jz.knn.knn.jit(pos, k=2000, cfg=cfg), jz.config.KNNConfig()
)
print(rnn.shape, inn.shape, cfg.alloc_fac_ilist)
Output format (schematic; capacities depend on the input and configuration):
jztree: cfg.alloc_fac_ilist: <old value> -> <new value> (need <required>, have <capacity>)
(50000, 2000) (50000, 2000) <new value>
Each attempt compiles the function again with the new configuration. Errors that are not failed checks are raised unchanged.
Inspecting errors
If you pass allocation factors yourself instead of a configuration, or want to log what
happened, jztree.checks.check_info() returns the details of a failed check (or None
for other errors):
info = jz.check_info(e)
print(info.values, info.fix, info.callsite)
print(info.suggest_fixes())
{'need': 633252, 'have': 533760} alloc_fac_ilist .../jztree/knn.py:112
{'alloc_fac_ilist': 1.305038229}
suggest_fixes() returns the factors (including the margin) by which to multiply the named
values.
Multi-GPU
Under shard_map, every check is combined over all ranks before it can fail. All ranks then
raise the same error, instead of the failing ranks stopping while the others wait forever in the
next communication. The values in the message belong to the rank that needs the largest
increase, and the message ends with e.g. (on rank 3; 2 of 8 ranks failed). Since all ranks
see the same error, apply_fix and run_with_fixes compute the same new configuration on every
rank, which therefore compile the same program.
Multi-GPU runs can additionally fail because a rank receives more particles than its padded
particle arrays can hold. The suggested fix is then "padding"; see
Padding errors and internal padding in the
multi-GPU guide.
Disabling the checks
Setting the environment variable JZ_SKIP_CHECKS=1 (before the functions are traced) removes
all checks. This gives a small speed-up, but an allocation that is too small then silently
corrupts memory or results. It is only recommended for configurations that are known to work.
The checks also work with jax on the CPU, e.g. for debugging multi-device code with
--xla_force_host_platform_device_count. Only the tree algorithms themselves require a GPU.