Memory, disk and threads¶
The memory budget¶
max_memory_gb is the memory a fit may use; by default, what the job may use (a
container or scheduler limit if there is one, otherwise the machine’s memory).
memory_reserve keeps a share of it (15 percent by default) back for what the model
does not count. The trees are grown in batches sized to what is left. A smaller budget
means smaller batches and more passes over the data, which costs little until a batch
holds fewer trees than there are threads: then threads wait, and the fit takes up to
threads / trees per batch times as long as it would. profile=True says how many trees a
batch holds, and warns if that is too few.
fs.causal_forest("y ~ w | x1 + x2 + x3", "big.parquet", max_memory_gb=8, profile=True)
profile=True returns the estimate of what the fit will need, component by component
(the streaming chunk, a batch’s splitting samples and nodes, the binned copy, the trees),
without fitting. Each tree’s splitting sample is held once, sample_fraction times
honesty_fraction times the rows, at a few dozen bytes a row.
Memory that does not depend on the budget: the compiled kernels and libraries (a few hundred megabytes), and, for a causal forest with fixed effects, the vectors that backfitting keeps (16 bytes a row for one round’s predictions; see Fixed effects). hdfe-stream’s own passes, for the fixed effects, use the memory its documentation describes.
Disk¶
A fit writes to its working directory:
the binned copy of the data: one byte per feature and 24 bytes a row, written once and shared by every forest the fit grows;
the trees: each tree is at most
2 m / min_node_size + 3nodes formrows in its splitting half, each node about 64 bytes; most trees have fewer;for fixed effects, hdfe-stream’s own files and a few vectors of one number a row.
workdir= is the directory in which the fit makes its own run directory (forest_run_...),
which it deletes when the result is closed or garbage collected, unless keep_workdir=True.
Nothing else in workdir is touched. Point workdir at a disk with room. The default is
the system’s temporary directory, and on some systems that is a RAM-backed file system
of limited size, where a large fit will fill it and the memory the budget assumed.
predict and the other methods of a fit read the trees and binned copy from the working
directory, so it has to stay while the result is in use. result.close() deletes it, and the
result cannot predict afterwards.
Threads¶
The trees of a batch are grown in parallel, and so are predictions, and the sums over rows
in the passes. n_threads= sets how many threads a call uses; by default, all that the job
may use (the CPUs of a container or scheduler allocation, not just those of the machine).
The same seed gives the same forest whatever the number of threads and whatever the
batch size, up to rounding in the last digits.
Polars reads the data with its own threads. If a job has a memory limit, set it before
Polars is imported (stream_limits.limit_memory(), or POLARS_OOC_MEMORY_BUDGET_MB), or
Polars warns that its out-of-core budget could not be set.