Memory, disk and threads

Memory

Least squares never holds all the rows in memory. What a fit holds is:

  • the cross-products: one matrix of the predictors’ products per fold (K + 1 matrices for K-fold cross-validation), 8 × p² bytes each for p predictors: 8 MB for 1,000 predictors, 800 MB for 10,000;

  • a batch of rows while the pass reads them, the design evaluated on the batch: its size is set by hdfe-stream’s memory budget, not by the number of rows (with about 2,000 indicator columns, the pass peaks near 8 GB from a few hundred thousand rows up);

  • the fixed effects’ work, as in hdfe-stream’s regressions (see hdfe-stream’s resources page).

So memory grows with the square of the number of predictors, and with the number of folds, but not with the number of rows.

The penalized logit, probit, Poisson, multinomial and ordered models have two solvers. The streamed one reads a compact copy of the rows from disk at each Newton step and holds one information matrix per cross-validation model, for the predictors that can be active at the current penalty. The in-memory one (the logit, probit, Poisson and multinomial logit) holds the rows’ nonzeros and a few numbers per row for each model, and forms no matrix of the predictors’ products. solver="auto" uses the in-memory one only when that fits the memory budget (max_memory_gb=, else the memory available); on the benchmarks’ 8 million people it took 7 GB for the employment logit, where the streamed solver took 3.2 GB.

Pass max_memory_gb= and memory_reserve= to plan the pass with other limits than those stream-limits detects; batch_rows= sets the batch directly.

Disk

Intermediate files go under workdir (by default $HDFE_STREAM_WORKDIR, or the system’s temporary directory): the fixed-effect passes’ sorted rows, and, for the plug-in penalty, the double-selection estimators and the penalized logit, a copy of the partialled-out rows that later passes read. A fixed-effect fit’s scratch is about the size of the columns it uses; the saved rows are 8 bytes per value. Results that read the rows again (predict(), sample()) read the source, lazily. Results that keep saved rows have cleanup(), and delete them when they are garbage collected.

Threads

The pass uses every CPU the job has. On a cluster, the CPUs and memory of the job are detected by stream-limits, as for hdfe-stream; n_threads= caps the numba kernels. If your script imports Polars before penalized-stream, call stream_limits.set_limits() before that import, as hdfe-stream’s quickstart for cluster jobs explains.