read_csv performance issues
System information
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): 2014 Mac OS Mojave 10.14.6; Memory: 16 GB 1600 MHz DDR3; Processor: 2.2 GHz Intel Core i7
- Modin version (
modin.__version__):0.7.3 - Python version:3.7.6
- Code we can use to reproduce:
Running using a Jupyter notebook within Jupyter Lab.
import modin.pandas as mpd
import numpy as np
from timeit import default_timer as timer
arr = np.random.uniform(low=0.0, high=100.0, size=(100_000_000, 3))
# %time gives a result far before the code finishes running like 6.91 micro seconds for an operation that takes more like a minute using the timers below
start = timer()
mdf = mpd.DataFrame(arr)
end = timer()
print(end - start)
start = timer()
mdf.to_csv("test_100mx3.csv")
end = timer()
print(end - start)
start = timer()
mdf = mpd.read_csv("test_100mx3.csv")
end = timer()
print(end - start)
Describe the problem
Takes forever long. Vanilla pandas is far faster on my machine.
Hi @discdiver thanks for creating an issue. It is true that pandas is faster going directly from a numpy array, but most data is coming from a file, which can be accessed by multiple workers in Modin's distributed architecture. I have explained this at length here: #232. You should have seen a warning letting you know that it would take some time.
For the to_csv, that defaults to pandas at the moment, which also should have triggered a warning.
read_csv is still 2x faster.
In [1]: import modin.pandas as mpd
...: import numpy as np
...: from timeit import default_timer as timer
...:
...: arr = np.random.uniform(low=0.0, high=100.0, size=(100_000_000, 3))
...:
...: # %time gives a result far before the code finishes running like 6.91 micro seconds for an operation that takes more like a minute using the timers below
...:
...:
...: start = timer()
...: mdf = mpd.DataFrame(arr)
...: end = timer()
...: print(end - start)
...:
...: start = timer()
...: mdf.to_csv("test_100mx3.csv")
...: end = timer()
...: print(end - start)
...:
...: start = timer()
...: mdf = mpd.read_csv("test_100mx3.csv")
...: end = timer()
...: print(end - start)
...:
UserWarning: Distributing <class 'numpy.ndarray'> object. This may take some time.
86.96603366173804
UserWarning: `DataFrame.to_csv` defaulting to pandas implementation.
To request implementation, send an email to [email protected].
UserWarning: Distributing <class 'pandas.core.frame.DataFrame'> object. This may take some time.
588.060065000318
24.91445238608867
In [2]: import pandas
In [3]: start = timer()
...: df = pandas.read_csv("test_100mx3.csv")
...: end = timer()
...: print(end - start)
48.72373345633969
If you run the read_csv in a fresh environment, what is the performance?
When Modin eventually has a numpy interface, we will have much better performance in the conversion. For now, we have focused on optimizing for the things people are using. Average data scientists are not generating random numpy arrays to do analytics, their data comes from files. This is what we have focused on.
If you have some concrete questions about performance or Modin architecture I am happy to answer, but I did not see that in your issue text or title. Is there something concrete I can answer that I haven't already?
Also I should add that this has nothing to do with the shape of the data. If you change your size argument to np.random.uniform it will not change the performance of what you showed.
Thanks @devin-petersohn. I did get the warning UserWarning: Distributing <class 'numpy.ndarray'> object. This may take some time. I was just kind of shocked at how much time it took.
I recognize that most data comes from files. I switch data between NumPy arrays and DataFrames when working with Scikit-learn quite a bit. Good to know a NumPy interface is in the works.
I changed the title of the issue to better reflect a more narrowly tailored issue regarding reading csv files. You are getting a nice speed up from reading the file. Cool.
I just tried reading in the csv file again.
When I read the csv with Modin using Ray backend it takes 60.353286353 seconds vs 59.046780883000004 seconds for vanilla pandas. Different notebooks and kernels. Same code as you have above. Not much else running on my machine. I wonder why the different results?
I switched to Modin using dask backend and I got distributed.nanny - WARNING - Worker exceeded 95% memory budget. Restarting
distributed.nanny - WARNING - Restarting worker.
So I restarted Chrome and Jupyter Lab restart and made sure I had few chrome windows open. I then got 26.24551373 seconds for Modin with dask on read_csv which is the best time yet! However, I got a bunch of these warnings in the terminal: distributed.worker - WARNING - Memory use is high but worker has no data to store to disk. Perhaps some other process is leaking memory? Process memory: 1.58 GB -- Worker memory limit: 2.15 GB. When I checked Activity Monitor I see a bunch of python 3.7 threads using 400 MB each. Killing the kernel takes care of all of those threads.
So then I restarted the kernel, made sure there was no memory pressure, switched the Modin backend to Ray and tried again. I watched the memory in Activity Monitory - no problems, 64MB for each of the python processes. But the time to read in the csv was 61.626633927.
Using dask to read in the same csv data and materialize a DataFrame with the code below I get 30.535354624000004 seconds:
start = timer()
df_dask = dd.read_csv('test_pandas_100M_row_x_3C.csv').compute()
end = timer()
print(end - start)
It's interesting that Modin with Dask backend is reading in the file quickly but causing memory pressure. And Modin with Ray gives you a speedup but doesn't give me one.
Thanks for the further context, it is very helpful.
I changed the title of the issue to better reflect a more narrowly tailored issue regarding reading csv files. You are getting a nice speed up from reading the file. Cool.
I would much prefer you also saw this speedup 😄. It doesn't much matter if I see performance if nobody else does. I would like to get to the bottom of the weird performance you are seeing with the Ray engine. Does this change anything?
import ray
ray.init()
import modin.pandas as pd
df = pd.read_csv(...)
Take special care that you are evaluating Modin and not pandas, the times are suspiciously close and I would not expect that under any condition for a file of this size.
There may be some old Ray workers that are hanging around and hanging on to some state. ray stop from the terminal should kill any workers that are still around. I know they are working on that issue.
However, I got a bunch of these warnings in the terminal:
distributed.worker - WARNING - Memory use is high but worker has no data to store to disk. Perhaps some other process is leaking memory? Process memory: 1.58 GB -- Worker memory limit: 2.15 GB.
There is no real opportunity for Modin objects to leak memory in Dask. This is also a very strange issue worth digging into. Maybe there is some delay in the garbage collection, or some other strange artifact. Dask is using multithreading by default on Dask DataFrame, I believe. I wasn't able to reproduce this, but I will keep trying a few different things.
Thank you @devin-petersohn. Now the issue name is actually changed, oops.
I imported modin under a different alias, to ty to make sure I wasn't evaluating pandas.
Based on the docs I was doing:
# import os
# os.environ["MODIN_ENGINE"] = "ray" # the default
Is the following something I need to do?
import ray
ray.init()
Now I get: UserWarning: The pandas version installed does not match the required pandas version in Modin. This may cause undesired side effects!
If Modin needs a different version, should it just install it? I'm not an expert here. I have pandas 1.0.5 and don't want to downgrade my pandas version.
I ran the read_csv cell with modin and got a bunch of warnings like this: (pid=11638) E0624 14:46:29.273875 49481152 plasma_store_provider.cc:108] Failed to put object 6f53dca1f451ca94ffffffff010000c801000000 in object store because it is full. Object size is 399337401 bytes.
Then it crashed.
I then started a new kernel for my Jupyter notebook I've got around 10 GB of RAM free on my machine prior to start.
Running ray.init() I get the message 2020-06-24 14:53:58,499 INFO resource_spec.py:212 -- Starting Ray with 7.18 GiB memory available for workers and up to 3.6 GiB for objects. You can adjust these settings with ray.init(memory=<bytes>, object_store_memory=<bytes>).
2020-06-24 14:53:58,904 INFO services.py:1148 -- View the Ray dashboard at localhost:8265
Cool dashboard.
I import things and run this cell:
start = timer()
df5 = pdd.read_csv('test_pandas_100M_row_x_3C.csv')
end = timer()
print(end - start)
Time is 61.245690881 seconds. No crash. I am left with a bunch of python 3.7 processes in activity manager around 60MB in size. !ray stop from the notebook kills them all. Glad to hear that's being worked on. 👍
Then importing pandas and reading the csv with read_csv takes 57.168 s.
Are you recommending folks use Dask instead of Ray? Should I be setting memory limits or doing anything else to try to help with performance and memory management?
If Modin needs a different version, should it just install it? I'm not an expert here. I have pandas 1.0.5 and don't want to downgrade my pandas version.
No, you don't need to change for this case. We have update the version of pandas on the GitHub master, but have not released that version yet.
Is the following something I need to do?
Not generally, but as a debugging process I wanted to see if the behavior changed. It is more of an advanced usage capability we support for users who want more control. I do not see any change in behavior.
Memory management in Ray is something we're working with them on, but it is still peculiar because this isn't much data at all. What time gets printed when you run the following in a completely separate notebook (from the same working directory)?
import ray
ray.init()
import modin.pandas as pd
from timeit import default_timer as timer
start = timer()
df = pd.read_csv("test_100mx3.csv")
end = timer()
print(end - start)
To get to the bottom of this we should test this baseline in a completely new notebook environment.
Started a new JupyterLab instance with a new notebook. Ran the Modin Ray code above. Time was 63.91737544600001 s.
Then imported pandas under different alias. Ran the read_csv code. Time was 56.541993192999996 s.
I have seen some strange utilization issues with Ray on many core (100+) machines before, but never on a 4 or 6 core machine.
On the many core machine, updating ipython fixed the issue. Your issue looks very directly tied to your environment, similar to those cases. Have you tried running in a virtualenv? If you update ipython does that fix the issue?
I am adding the Ray tag to this issue since it looks like Dask is performing fine for you.
Hi @devin-petersohn I was testing a csv read for file size of 7 GB around with system memory available 15 GB, but during read the memory fills up up to 15 GB and then system hangs. I'm using Ubuntu with Python 3.8.2 and with latest default Ray and Modin versions. What am I missing here?
@chetansurwade In your case the 7GB on disk is probably more that 15GB in memory, often due to the data types in the file. If you can try the out of core functionality: https://modin.readthedocs.io/en/latest/out_of_core.html
Closing this thread since it is stale.