Starting Fast On Ephemeral Clusters#
superglm’s numerical kernels are compiled by Numba the first time a process uses them, and Numba keeps the compiled code in an on-disk cache. On a laptop that compile happens once. On a cluster that starts from a clean disk every time, such as a Databricks job cluster, every new cluster compiles again.
The following historical measurements predate the bounded-response and
Dirichlet kernels. They describe one machine (AMD EPYC 9645, Python 3.13,
Numba 0.68), with a first Tweedie fit_reml and summary on 20,000 rows;
they do not measure the current all-family warmup:
Empty cache |
Cache from an earlier cluster |
|
|---|---|---|
First Tweedie fit and summary |
4.7 s |
0.8 s |
|
52 s |
0.6 s |
A larger model compiles a few more kernels: a first fit of each family on 20,000 rows of the freMTPL2 book with four interactions spent 5.0 to 8.2 seconds compiling, depending on family and route.
Keep Numba’s cache on persistent storage#
Set NUMBA_CACHE_DIR to a directory that outlives the cluster, before Python
starts, so Numba reads it when it is imported. On Databricks, set it in the
cluster’s environment variables to a path on a Unity Catalog volume or other
persistent storage:
NUMBA_CACHE_DIR=/Volumes/<catalog>/<schema>/<volume>/numba-cache/superglm-<version>
Putting the superglm version in the path gives each release its own cache.
Numba finds its entries by three keys, and each must be the same from one cluster to the next:
The path superglm is installed at. Numba names the cache subdirectory after the absolute path of each source directory, so install superglm the same way on every cluster, for example as a cluster library or from an init script. An environment whose path changes per session starts an empty cache.
The CPU. Entries are compiled for the machine’s CPU model and features. A cluster on another node type compiles its own entries into the same directory; both sets are kept.
The source files. Numba 0.68 and later key the cache on a hash of each file’s contents, so a fresh install of the same superglm version at the same path reuses it. Numba 0.67 and earlier key it on each file’s modification time, and
pipstamps every file with the time of the install, so each new cluster compiles again. With those versions, give the installed files a fixed time after installing:SUPERGLM_DIR=$(python -c "import os, superglm; print(os.path.dirname(superglm.__file__))") find "$SUPERGLM_DIR" -name '*.py' -exec touch -d '2000-01-01 00:00:00' {} +
Only do this with a versioned cache directory, as above: Numba does not notice when a function in one file calls a changed function in another, and a fixed time hides an upgrade from it.
Measured with a first Tweedie fit against a cache that an earlier install of the same wheel wrote, after each of these changes:
Between the installs |
Numba 0.67 |
Numba 0.68 |
|---|---|---|
superglm reinstalled in place with |
compiles again (4.4 s) |
loads (0.5 s) |
every installed file touched |
compiles again (5.3 s) |
loads (0.5 s) |
environment deleted and created again at the same path |
compiles again (6.5 s) |
loads (1.1 s) |
installed at another path |
compiles again (5.8 s) |
compiles again (5.4 s) |
With Numba 0.67, fixing the file times as above makes a reinstall load the cache (1.0 s).
Several workers can load from one directory. Two processes compiling the
same kernel into it at once can overwrite each other’s entry, because Numba
keeps one index file per kernel, so fill the directory before the workers
fit, for example with superglm.warmup() in the init script below.
Optionally, warm up in the init script#
The cache fills as fits run: a first fit compiles only the kernels it needs
and saves them, so a cluster needs nothing more than the directory above. To
pay the compile before anyone fits, call superglm.warmup() once in the
cluster’s init script. Outside cluster start-up, an image build or a
benchmark, there is no reason to call it.
A cluster init script that installs superglm, keeps the cache and warms it.
It must use the Python that notebooks and jobs run, so that they import this
install and Numba finds the cache under the same path. On Databricks Runtime
that is /databricks/python/bin/python, the interpreter Databricks’ own
init-script examples install into:
#!/bin/bash
set -euo pipefail
VERSION=<version>
# The Python notebooks and jobs run, so they see this install at this path.
PY=/databricks/python/bin/python
"$PY" -m pip install "superglm==$VERSION"
export NUMBA_CACHE_DIR=/Volumes/<catalog>/<schema>/<volume>/numba-cache/superglm-$VERSION
# Numba 0.67 and earlier only: give the installed files a fixed time.
SUPERGLM_DIR=$("$PY" -c "import os, superglm; print(os.path.dirname(superglm.__file__))")
find "$SUPERGLM_DIR" -name '*.py' -exec touch -d '2000-01-01 00:00:00' {} +
"$PY" -c "import superglm; superglm.warmup()"
Set the same NUMBA_CACHE_DIR in the cluster’s environment variables, so
notebooks and jobs read the cache the script filled.
warmup() compiles in worker processes, one per physical core by default, and
then loads what they compiled. SUPERGLM_N_JOBS sets how many run at once, and
SUPERGLM_MAX_MEMORY allows one per 600 MB; with one, it compiles in the
calling process.
warmup() covers every family’s core fitting route, including bounded scalar
responses and complete Dirichlet compositions. Startup cost depends on the
platform and cache contents. The generated primitive digests invalidate cached
Dirichlet and gamma-window code when an imported numerical primitive changes.
Numba keys its cache to the CPU model, so a cache built on one node type compiles again on
another; the entries for both are kept side by side. After warmup(), the
first fits we measured compiled nothing: every family and link, SuperGLM
and SuperLSS, exact and binned, and with RandomEffect and FactorSmooth
terms (basis="fs" and "sz"). A few fallbacks that rarely run, such as
the sum taken when a product leaves float64’s range, also compile on first
use. Two groups of kernels are left to the first
call that uses them, because few fits do and they take seconds to compile:
the third and fourth derivatives a SuperLSS fit needs only when a
smoothing parameter reaches its endpoint (7 seconds across the Tweedie,
negative binomial and gamma families), and a fit’s diagnostics (the
smoother of a diagnostic plot, the hat diagonal of a FactorSmooth fit on
the structured engine). Each compiles once on first use and then joins the
persistent cache like the rest.