Many covariates¶
The same kind of comparison as the AKM regression, holding the data
fixed and varying its width: the million-worker panel (8.5 million rows, 66,665
firms) with age entered as indicators,
log_earn ~ i(age_bin) | worker_id + firm_id, clustered by worker, in bins from
five years wide (8 indicators) down to one month (503). It is compared with
pyfixest, xhdfe, R’s fixest and Julia’s FixedEffectModels.jl.
hdfe_stream runs at its defaults and “sized to the design”, with bucket, batch
and row-group sizes scaled down as the design widens (the rule is below). Every
configuration that finished agrees with every other to within 1e-7.
python benchmarks/covariates_benchmark.py
The numbers are in benchmarks/results/covariates.csv.
Wall time and peak memory (”—”: out of memory at this machine’s limit, 29.6 GB of its 30.8 GB):
covariates |
pyfixest |
xhdfe |
fixest (R) |
FixedEffectModels.jl |
hdfe_stream |
hdfe_stream, sized |
|---|---|---|---|---|---|---|
8 |
10 s, 8.0 GB |
12 s, 9.5 GB |
18 s, 3.5 GB |
23 s, 3.3 GB |
5 s, 2.5 GB |
6 s, 3.3 GB |
41 |
28 s, 17.5 GB |
36 s, 22.4 GB |
63 s, 10.5 GB |
89 s, 7.8 GB |
11 s, 3.1 GB |
12 s, 3.5 GB |
83 |
— |
— |
119 s, 19.4 GB |
177 s, 13.7 GB |
22 s, 3.3 GB |
22 s, 4.3 GB |
167 |
— |
— |
— |
322 s, 24.1 GB |
45 s, 3.9 GB |
44 s, 4.0 GB |
503 |
— |
— |
— |
— |
106 s, 10.9 GB |
114 s, 10.5 GB |
The hdfe_stream columns are from a later run on the same instance type, which ran about 5% slower than the other libraries’ run (by the configuration sized to the design, measured on both).
Reading the figures¶
The in-memory libraries run out first. Their peak grows with rows × covariates. At 8.5 million rows pyfixest and xhdfe were out of memory at 83 indicators, and fixest at 167. FixedEffectModels.jl, the leanest of them, finished 167 indicators in 24.1 GB; at 503 the design alone is 34 GB, and none of them could hold it. hdfe_stream sized to the design goes from 3.3 GB at 8 indicators to 4.0 GB at 167 and 10.5 GB at 503. Part of that last figure is the memory-mapped cell table, whose pages count towards the peak although the kernel can drop them under pressure, so the memory the fit itself needs is less.
The defaults size the batches by the width. Every step that holds rows
holds all the covariates, so hdfe_stream sorts buckets of about 384 MB of rows
and reads batches of about 256 MB of the design, and from about 190 to 620
columns reads buckets of up to 1 GB whole. At its defaults it takes 3.9 GB at 167
indicators and 10.9 GB at 503, close to the configuration sized to the design
(4.0 and 10.5 GB), and is faster at 503 (106 s against 114 s). To trade memory
for time differently, size rows_per_bucket, batch_rows and row_group_size
yourself. The sized configuration aims at about 1 GB of design per bucket and
250 MB per batch and row group, never above the defaults:
per_row = 8 * (k + 4) # k covariates, plus outcome and ids
batch = min(2_000_000, int(250e6 // per_row))
fit = feols_stream(fml, data, rows_per_bucket=min(20_000_000, int(1e9 // per_row)),
batch_rows=batch, row_group_size=min(500_000, batch))
Wide designs puts this together.
Time grows with the width. The cross-products cost k × k per row; they are formed chunk by chunk as BLAS matrix products. With this machine’s 16 cores the time grew about in proportion to the width from 83 indicators up, though (2.0 times from 83 to 167, 2.6 times from 167 to 503, sized to the design), and 503 indicators took about 2 minutes where 167 took 44 seconds. hdfe_stream is the fastest configuration at every width measured.
Timings are noisy where memory is tight. Each point is one run, so differences of 15% or so between configurations should not be read as meaningful. The differences between libraries above are far larger than that.
Disk stays small. The cell sums, which are the part that grows with the width, are written to disk only when a later step reads them back, and none does here: 0.3 GB at 8 indicators, 0.5 GB at 503.
How the design is built matters. A formula term such as i(age_bin) is
carried through the partitioning as the one column it is computed from, and the
indicators are built one bucket at a time. An expression that needs the whole
column, possible only with the low-level interface, makes the fit build the
design up front instead; see wide designs.