Skip to content

Commit 0ea864e

Browse files
lhoestqdavanstrien
andauthored
Update dask docs (#1532)
* update dask docs * typo * add column and filter pushdown * minor * Apply suggestions from code review Co-authored-by: Daniel van Strien <[email protected]> * Update docs/hub/datasets-dask.md * explain column skipping better --------- Co-authored-by: Daniel van Strien <[email protected]>
1 parent 6484a58 commit 0ea864e

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

docs/hub/datasets-dask.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# Dask
22

33
[Dask](https://github.com/dask/dask) is a parallel and distributed computing library that scales the existing Python and PyData ecosystem.
4-
Since it uses [fsspec](https://filesystem-spec.readthedocs.io) to read and write remote data, you can use the Hugging Face paths ([`hf://`](/docs/huggingface_hub/guides/hf_file_system#integrations)) to read and write data on the Hub:
4+
5+
In particular, we can use Dask DataFrame to scale up pandas workflows. Dask DataFrame parallelizes pandas to handle large tabular data. It closely mirrors the pandas API, making it simple to transition from testing on a single dataset to processing the full dataset. Dask is particularly effective with Parquet, the default format on Hugging Face Datasets, as it supports rich data types, efficient columnar filtering, and compression.
6+
7+
A good practical use case for Dask is running data processing or model inference on a dataset in a distributed manner. See, for example, Coiled's excellent blog post on [Scaling AI-Based Data Processing with Hugging Face + Dask](https://huggingface.co/blog/dask-scaling).
8+
9+
# Read and Write
10+
11+
Since Dask uses [fsspec](https://filesystem-spec.readthedocs.io) to read and write remote data, you can use the Hugging Face paths ([`hf://`](/docs/huggingface_hub/guides/hf_file_system#integrations)) to read and write data on the Hub;
512

613
First you need to [Login with your Hugging Face account](/docs/huggingface_hub/quick-start#login), for example using:
714

@@ -17,7 +24,8 @@ from huggingface_hub import HfApi
1724
HfApi().create_repo(repo_id="username/my_dataset", repo_type="dataset")
1825
```
1926

20-
Finally, you can use [Hugging Face paths](/docs/huggingface_hub/guides/hf_file_system#integrations) in Dask:
27+
Finally, you can use [Hugging Face paths](/docs/huggingface_hub/guides/hf_file_system#integrations) in Dask.
28+
Dask DataFrame supports distributed writing to Parquet on Hugging Face, which uses commits to track dataset changes:
2129

2230
```python
2331
import dask.dataframe as dd
@@ -30,6 +38,14 @@ df_valid.to_parquet("hf://datasets/username/my_dataset/validation")
3038
df_test .to_parquet("hf://datasets/username/my_dataset/test")
3139
```
3240

41+
Since this creates one commit per file, it is recommended to squash the history after the upload:
42+
43+
```python
44+
from huggingface_hub import HfApi
45+
46+
HfApi().super_squash_history(repo_id=repo_id, repo_type="dataset")
47+
```
48+
3349
This creates a dataset repository `username/my_dataset` containing your Dask dataset in Parquet format.
3450
You can reload it later:
3551

@@ -45,3 +61,58 @@ df_test = dd.read_parquet("hf://datasets/username/my_dataset/test")
4561
```
4662

4763
For more information on the Hugging Face paths and how they are implemented, please refer to the [the client library's documentation on the HfFileSystem](/docs/huggingface_hub/guides/hf_file_system).
64+
65+
# Process data
66+
67+
To process a dataset in parallel using Dask, you can first define your data processing function for a pandas DataFrame or Series, and then use the Dask `map_partitions` function to apply this function to all the partitions of a dataset in parallel:
68+
69+
```python
70+
def dummy_count_words(texts):
71+
return pd.Series([len(text.split(" ")) for text in texts])
72+
```
73+
74+
In pandas you can use this function on a text column:
75+
76+
```python
77+
# pandas API
78+
df["num_words"] = dummy_count_words(df.text)
79+
```
80+
81+
And in Dask you can run this function on every partition:
82+
83+
```python
84+
# Dask API: run the function on every partition
85+
df["num_words"] = df.text.map_partitions(dummy_count_words, meta=int)
86+
```
87+
88+
Note that you also need to provide `meta` which is the type of the pandas Series or DataFrame in the output of your function.
89+
This is needed because Dask DataFrame uses a lazy API. Since Dask will only run the data processing once `.compute()` is called, it needs
90+
the `meta` argument to know the type of the new column in the meantime.
91+
92+
# Predicate and Projection Pushdown
93+
94+
When reading Parquet data from Hugging Face, Dask automatically leverages the metadata in Parquet files to skip entire files or row groups if they are not needed. For example if you apply a filter (predicate) on a Hugging Face Dataset in Parquet format or if you select a subset of the columns (projection), Dask will read the metadata of the Paquet files to discard the parts that are not needed without downloading them.
95+
96+
This is possible thanks to the `dask-expr` package which is generally installed by default with Dask. You can read more about `dask-expr` in its [introduction blog post](https://blog.dask.org/2023/08/25/dask-expr-introduction) and in this more recent [blog post on dask optimizations](https://blog.dask.org/2024/05/30/dask-is-fast#optimizer)
97+
98+
For example this subset of FineWeb-Edu contains many Parquet files. If you can filter the dataset to keep the text from recent CC dumps, Dask will skip most of the files and only download the data that match the filter:
99+
100+
```python
101+
import dask.dataframe as dd
102+
103+
df = dd.read_parquet("hf://datasets/HuggingFaceFW/fineweb-edu/sample/10BT/*.parquet")
104+
105+
# Dask will skip the files or row groups that don't
106+
# match the query without downloading them.
107+
df = df[df.dump >= "CC-MAIN-2023"]
108+
```
109+
110+
Dask will also read only the required columns for your computation and skip the rest.
111+
For example if you drop a column late in your code, it will not bother to load it early on in the pipeline if it's not needed.
112+
This is useful when you want to manipulate a subset of the columns or for analytics:
113+
114+
```python
115+
# Dask will download the 'dump' and 'token_count' needed
116+
# for the filtering and computation and skip the other columns.
117+
df.token_count.mean().compute()
118+
```

0 commit comments

Comments
 (0)