3x Faster Text Preprocessing
NONE
Unfortunately, that won't improve the performance. StringZilla repository has extensive benchmarks comparing against different built-in functionality of several programming languages. Using `re.finditer` for tokenization is practically the slowest anti-pattern I've encountered in any language. The gap between that and a SIMD-accelerated kernel can be as big as 10 MB/s vs 10 GB/s.

I understand the need to keep the dependencies minimal. It helps the package remain small and portable. At this point, StringZilla provides 105 binaries for different OS and hardware versions (more portable than NumPy) and the [binary size generally ranges from 50 KB to 250 KB](https://pypi.org/project/stringzilla/), smaller than a single JPEG. 

2024-03-04T10:49:06Z
https://github.com/huggingface/datasets/pull/6711#issuecomment-1976298209
https://api.github.com/repos/huggingface/datasets/issues/6711
IC_kwDODunzps51y-rh
https://api.github.com/repos/huggingface/datasets/issues/comments/1976298209/reactions
2024-03-04T10:49:06Z
https://api.github.com/repos/huggingface/datasets/issues/comments/1976298209
https://avatars.githubusercontent.com/u/1983160?v=4
https://api.github.com/users/ashvardanian/events{/privacy}
https://api.github.com/users/ashvardanian/followers
https://api.github.com/users/ashvardanian/following{/other_user}
https://api.github.com/users/ashvardanian/gists{/gist_id}
https://github.com/ashvardanian
ashvardanian
MDQ6VXNlcjE5ODMxNjA=
https://api.github.com/users/ashvardanian/orgs
https://api.github.com/users/ashvardanian/received_events
https://api.github.com/users/ashvardanian/repos
https://api.github.com/users/ashvardanian/starred{/owner}{/repo}
https://api.github.com/users/ashvardanian/subscriptions
User
https://api.github.com/users/ashvardanian
public
COLLABORATOR
The `text` builder is not very popular, so I'm also not a fan of introducing a dependency for it.

Moreover, I couldn't find any projects of this size/usage depending on StringZilla (with GitHub search), so we should at least wait for its greater adoption to merge this PR.

2024-03-04T15:15:49Z
https://github.com/huggingface/datasets/pull/6711#issuecomment-1976817310
https://api.github.com/repos/huggingface/datasets/issues/6711
IC_kwDODunzps5109ae
https://api.github.com/repos/huggingface/datasets/issues/comments/1976817310/reactions
2024-03-04T15:15:49Z
https://api.github.com/repos/huggingface/datasets/issues/comments/1976817310
https://avatars.githubusercontent.com/u/47462742?v=4
https://api.github.com/users/mariosasko/events{/privacy}
https://api.github.com/users/mariosasko/followers
https://api.github.com/users/mariosasko/following{/other_user}
https://api.github.com/users/mariosasko/gists{/gist_id}
https://github.com/mariosasko
mariosasko
MDQ6VXNlcjQ3NDYyNzQy
https://api.github.com/users/mariosasko/orgs
https://api.github.com/users/mariosasko/received_events
https://api.github.com/users/mariosasko/repos
https://api.github.com/users/mariosasko/starred{/owner}{/repo}
https://api.github.com/users/mariosasko/subscriptions
User
https://api.github.com/users/mariosasko
public
NONE
> Moreover, I couldn't find any projects of this size/usage depending on StringZilla (with GitHub search), so we should at least wait for its greater adoption to merge this PR.

Meanwhile I understand that you want to wait for a greater adoption - if things change in the future you would be stuck with an unsupported dependency (although I think, that really applies to everything) - the performance improvement is really significant!
I wonder if it's worth, perhaps, to provide an additional 'datasets.extras' library by huggingface which support these 3rd party improvements.
It would reduce the risk on the core components and, at the same time, it would definitely help on the performance side!
2024-06-26T06:22:00Z
https://github.com/huggingface/datasets/pull/6711#issuecomment-2190857547
https://api.github.com/repos/huggingface/datasets/issues/6711
IC_kwDODunzps6CldVL
https://api.github.com/repos/huggingface/datasets/issues/comments/2190857547/reactions
2024-06-26T06:28:14Z
https://api.github.com/repos/huggingface/datasets/issues/comments/2190857547
https://avatars.githubusercontent.com/u/861162?v=4
https://api.github.com/users/danielealbano/events{/privacy}
https://api.github.com/users/danielealbano/followers
https://api.github.com/users/danielealbano/following{/other_user}
https://api.github.com/users/danielealbano/gists{/gist_id}
https://github.com/danielealbano
danielealbano
MDQ6VXNlcjg2MTE2Mg==
https://api.github.com/users/danielealbano/orgs
https://api.github.com/users/danielealbano/received_events
https://api.github.com/users/danielealbano/repos
https://api.github.com/users/danielealbano/starred{/owner}{/repo}
https://api.github.com/users/danielealbano/subscriptions
User
https://api.github.com/users/danielealbano
public
I was preparing some datasets for AI training and noticed that `datasets` by HuggingFace uses the conventional `open` mechanism to read the file and split it into chunks. I thought it can be significantly accelerated, and [started with a benchmark](https://gist.github.com/ashvardanian/55c2052e9f78b05b8d614aa90cb12347):

```sh
$ pip install --upgrade --force-reinstall datasets
$ python benchmark_huggingface_datasets.py xlsum.csv 
Generating train split: 1004598 examples [00:47, 21116.16 examples/s]
Time taken to load the dataset: 48.66838526725769 seconds
Time taken to chunk the dataset into parts of size 10000: 0.11466407775878906 seconds
Total time taken: 48.78304934501648 seconds
```

For benchmarks I've used a [large CSV file with mixed UTF-8 content](https://github.com/ashvardanian/StringZilla/blob/main/CONTRIBUTING.md#benchmarking-datasets), most common in modern large-scale pre-training pipelines. I've later patched the `datasets` library to use `stringzilla`, which resulted in significantly lower memory consumption and in 2.9x  throughput improvement on the AWS `r7iz` instances. That's using slow SSDs mounted over the network. Performance on local SSDs on something like a DGX-H100 should be even higher:

```sh
$ pip install -e .
$ python benchmark_huggingface_datasets.py xlsum.csv 
Generating train split: 1004598 examples [00:15, 64529.90 examples/s]
Time taken to load the dataset: 16.45028805732727 seconds
Time taken to chunk the dataset into parts of size 10000: 0.1291060447692871 seconds
Total time taken: 16.579394102096558 seconds
```

I've already [pushed the patches to my fork](https://github.com/ashvardanian/datasets/tree/faster-text-parsers), and would love to contribute them to the upstream repository.

---

All the tests pass, but they leave a couple of important questions open. The default Python `open(..., newline=None)` uses universal newlines, where `\n`, `\r`, and `\r\n` are all converted to `\n` on the fly. I am not sure if its a good idea for a general purpose dataset preparation pipeline?

I can simulate the same behavior (which I don't yet do) for `"line"` splitter. Adjusting it for `"paragraph"`-splitter would be harder. Should we stick exactly to the old Pythonic behavior or stay closer to how C and other programming languages do that?