Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High memory usage when calculating variance? #18824

Open
2 tasks done
ts826848 opened this issue Sep 18, 2024 · 0 comments
Open
2 tasks done

High memory usage when calculating variance? #18824

ts826848 opened this issue Sep 18, 2024 · 0 comments
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@ts826848
Copy link

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import polars as pl
# ~7.6 GB in Task Manager
s = pl.repeat(1.5, 1_000_000_000, eager=True)
s.var() # Memory usage jumps to >15 GB before result returned

Log output

No log output?

Issue description

Potential culprit might be squared in

fn var(&self, ddof: u8) -> Option<f64> {
let n_values = self.len() - self.null_count();
if n_values <= ddof as usize {
return None;
}
let mean = self.mean()?;
let squared: Float64Chunked = unary_elementwise_values(self, |value| {
let tmp = value.to_f64().unwrap() - mean;
tmp * tmp
});
squared
.sum()
.map(|sum| sum / (n_values as f64 - ddof as f64))
}

I'm not a stats/numerical analysis expert so I'm not sure whether the extra allocation is strictly necessary for numerical stability/other reasons. It feels like it shouldn't be necessary though?

Looks like NumPy has a similar issue? numpy/numpy#13199 (var, std memory consumption)

Expected behavior

Expected memory usage to not jump when calculating the variance

Installed versions

--------Version info---------
Polars:              1.7.1
Index type:          UInt32
Platform:            Windows-11-10.0.22631-SP0
Python:              3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)]

----Optional dependencies----
adbc_driver_manager  <not installed>
altair               <not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            0.11.6
fsspec               <not installed>
gevent               <not installed>
great_tables         <not installed>
matplotlib           <not installed>
nest_asyncio         <not installed>
numpy                2.1.1
openpyxl             <not installed>
pandas               <not installed>
pyarrow              17.0.0
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           <not installed>
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           3.2.0
@ts826848 ts826848 added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
None yet
Development

No branches or pull requests

1 participant