You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to run a 2SLS to estimate price elasticity with IV2SLS. This is what my data looks like: | ln_q | ln_p | .... weather variables ... | ... instruments ... |... user id dummies ...|
all data is np.float32. My data array is approx. (200000, 20000) which is about 16GB.
When running with the full dataset I consistently get the error: Unable to allocate 27.8GiB of memory to an array with shape (202507, 18450) and data type float64 and it looks like this line is the culprit: self._wz = self._z * w
which is where weights are assigned.
I'm running 64-bit python on a machine with 128 GB of RAM. I've tried to circumvent this issue by passing my own weights: results = IV2SLS(dependent, endog, exog, instruments, weights=np.ones(dependent.shape, dtype=np.float32)).fit()
but still get the same MemoryError even when I explicitly pass my own weights of data type float32.
32 GB of RAM usage just to create an array of 1s when weights = None seems like an awful lot of memory usage to essentially keep the input values unchanged. Further, why is it getting recast to float64, when all my other data is of data type float32 and I explicitly pass weights of datatype float32?
Why is an array of ~16GB using >100GB of RAM in this process? What can I do to get this regression to run?
I'm having a perhaps related issue with PanelOLS. As soon as I pass a weights vector fit takes a lot of time and ends up crashing the kernel. Without weights it's a matter of a few seconds to fit the same model.
Reopening: #862 closed this automatically, but it only fixes part of it.
#862 stops the IV models from storing weighted copies of the data when all of the weights are 1, which is the case without weights or with constant weights. That cuts memory by about a third. With 80 MiB of float32 data, the memory after constructing an IV2SLS model goes from 12.0x the data to 8.1x, and the peak of fit goes from 1604 MiB to 1290 MiB.
What remains for the float32 data in this issue:
IVData stores float32 input as float64, so the data doubles in size before the model sees it.
The model makes further full copies when it builds x and z (the np.c_[...] calls).
So 16 GB of float32 data would still need roughly 8x its size. Keeping float32, or avoiding those copies, is what is left to do here.
A memory-side fix will help, but the design is also doing a lot of unnecessary work. IV2SLS is being asked to carry roughly 20,000 user-id dummies as explicit columns (about 200,000 × 18,450 float64), which is why copies of Z and the weights blow up even on a 128 GB machine.
If those user dummies are fixed effects rather than coefficients you need to report, you can absorb them instead of materializing them. AbsorbingLS will absorb high-dimensional effects, but it is OLS only and has no instruments. For this 2SLS price-elasticity model, residualize ln_q, the weather controls, ln_p, and the instruments with respect to user id, then call IV2SLS on those residualized series without the dummies. The second-stage design is then only a handful of columns.
Alternatively, pyfixest demeans the fixed effects before 2SLS (FE-IV as in Wooldridge 2010, Ch. 11). In the 0.70 bracketed syntax:
(Replace the weather and instrument names with your columns. The older Y ~ X | fe | endog ~ inst form still works in 0.70 but is deprecated.) Either path avoids the 200,000 × 18,450 copies, independently of the remaining float32/copy work in #862.
Checked main after #862: the weight copies are gone, but with float32 inputs there are still two memory hits. IVData unconditionally upcasts to float64 (iv/data.py), and iv/model.py builds full copies for self._x and self._z via np.c_. On a 50,000x9 float32 input (~1.7 MiB), the model ends up holding ~7.6 MiB (~4.4x) all in float64.
Happy to work on the remainder if nobody's on it, just wanted to clarify scope:
Given the float64 contract, would you actually want IVData to preserve float32, or just avoid the _x/_z copies?
Are @markjayson13's fixed-effects absorption idea and @cpita-work's PanelOLS-with-weights crash in scope here, or separate issues?
Activity
cpita-work commentedon Nov 19, 2025
I'm having a perhaps related issue with PanelOLS. As soon as I pass a weights vector fit takes a lot of time and ends up crashing the kernel. Without weights it's a matter of a few seconds to fit the same model.
bashtage commentedon Nov 19, 2025
Can you post a demo example to help me understand the issue?
bashtage commentedon Nov 19, 2025
As for @ckarren 's issue, I believe that things are upcast to float64 at some point. That will be 32GB already.
DOC: Correct the IV introduction, add specification tests, AbsorbingL…
bashtage commentedon Nov 6, 2026
Reopening: #862 closed this automatically, but it only fixes part of it.
#862 stops the IV models from storing weighted copies of the data when all of the weights are 1, which is the case without weights or with constant weights. That cuts memory by about a third. With 80 MiB of float32 data, the memory after constructing an
IV2SLSmodel goes from 12.0x the data to 8.1x, and the peak offitgoes from 1604 MiB to 1290 MiB.What remains for the float32 data in this issue:
IVDatastores float32 input as float64, so the data doubles in size before the model sees it.xandz(thenp.c_[...]calls).So 16 GB of float32 data would still need roughly 8x its size. Keeping float32, or avoiding those copies, is what is left to do here.
markjayson13 commentedon Nov 11, 2026
A memory-side fix will help, but the design is also doing a lot of unnecessary work. IV2SLS is being asked to carry roughly 20,000 user-id dummies as explicit columns (about 200,000 × 18,450 float64), which is why copies of Z and the weights blow up even on a 128 GB machine.
If those user dummies are fixed effects rather than coefficients you need to report, you can absorb them instead of materializing them. AbsorbingLS will absorb high-dimensional effects, but it is OLS only and has no instruments. For this 2SLS price-elasticity model, residualize ln_q, the weather controls, ln_p, and the instruments with respect to user id, then call IV2SLS on those residualized series without the dummies. The second-stage design is then only a handful of columns.
Alternatively, pyfixest demeans the fixed effects before 2SLS (FE-IV as in Wooldridge 2010, Ch. 11). In the 0.70 bracketed syntax:
(Replace the weather and instrument names with your columns. The older
Y ~ X | fe | endog ~ instform still works in 0.70 but is deprecated.) Either path avoids the 200,000 × 18,450 copies, independently of the remaining float32/copy work in #862.TST: Tidy the tests of the unit weights of the IV models
DOC: Add bashtage#858, bashtage#859 and bashtage#862 to the 7.2 notes
DresdenGman commentedon Nov 25, 2026
Checked
mainafter #862: the weight copies are gone, but withfloat32inputs there are still two memory hits.IVDataunconditionally upcasts tofloat64(iv/data.py), andiv/model.pybuilds full copies forself._xandself._zvianp.c_. On a 50,000x9float32input (~1.7 MiB), the model ends up holding ~7.6 MiB (~4.4x) all infloat64.Happy to work on the remainder if nobody's on it, just wanted to clarify scope:
Given the
float64contract, would you actually wantIVDatato preservefloat32, or just avoid the_x/_zcopies?Are @markjayson13's fixed-effects absorption idea and @cpita-work's PanelOLS-with-weights crash in scope here, or separate issues?