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

Add support for categorical/dictionary types #6892

Merged
merged 7 commits into from
Jun 7, 2024

Conversation

EthanSteinberg
Copy link
Contributor

Arrow has a very useful dictionary/categorical type (https://arrow.apache.org/docs/python/generated/pyarrow.dictionary.html). This data type has significant speed, memory and disk benefits over pa.string() when there are only a few unique text strings in a column.

Unfortunately, huggingface datasets currently does not support this type. So huggingface datasets cannot natively read many parquet files that use this datatype .This PR adds support for Huggingface Datasets to read categorical/dictionary data.

Note: This PR functions by simply converting those dictionary/categorical types to strings. This means that huggingface datasets cannot take advantage of the compute benefits of categoricals, but it significantly simplifies logic. At this time, I do not think it makes sense to optimize categorical support within huggingface datasets and that we should only try to optimize later, if necessary.

Closes #5706

Copy link
Member

@lhoestq lhoestq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome ! I'll add a change to this PR to fix this if you don't mind:

import pyarrow as pa
from datasets import Dataset

animals = pa.array(["Flamingo", "Horse", "Brittle stars", "Centipede"]).cast(
    pa.dictionary(pa.int32(), pa.string())
)
t = pa.Table.from_arrays([animals], names=["animal"])
ds = Dataset(t)  # has 'string' feature type
ds.add_item({"animal": "Pikachu"})

currently it raises ArrowTypeError: Unable to merge: Field animal has incompatible types: dictionary<values=string, indices=int32, ordered=0> vs string because the ds.data is still the dictionary type

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@lhoestq lhoestq merged commit 686f5df into huggingface:main Jun 7, 2024
12 checks passed
Copy link

github-actions bot commented Jun 7, 2024

Show benchmarks

PyArrow==8.0.0

Show updated benchmarks!

Benchmark: benchmark_array_xd.json

metric read_batch_formatted_as_numpy after write_array2d read_batch_formatted_as_numpy after write_flattened_sequence read_batch_formatted_as_numpy after write_nested_sequence read_batch_unformated after write_array2d read_batch_unformated after write_flattened_sequence read_batch_unformated after write_nested_sequence read_col_formatted_as_numpy after write_array2d read_col_formatted_as_numpy after write_flattened_sequence read_col_formatted_as_numpy after write_nested_sequence read_col_unformated after write_array2d read_col_unformated after write_flattened_sequence read_col_unformated after write_nested_sequence read_formatted_as_numpy after write_array2d read_formatted_as_numpy after write_flattened_sequence read_formatted_as_numpy after write_nested_sequence read_unformated after write_array2d read_unformated after write_flattened_sequence read_unformated after write_nested_sequence write_array2d write_flattened_sequence write_nested_sequence
new / old (diff) 0.005388 / 0.011353 (-0.005965) 0.004004 / 0.011008 (-0.007005) 0.064037 / 0.038508 (0.025529) 0.031666 / 0.023109 (0.008557) 0.236493 / 0.275898 (-0.039405) 0.269047 / 0.323480 (-0.054432) 0.005008 / 0.007986 (-0.002977) 0.002964 / 0.004328 (-0.001364) 0.049926 / 0.004250 (0.045675) 0.048092 / 0.037052 (0.011039) 0.245563 / 0.258489 (-0.012926) 0.282614 / 0.293841 (-0.011227) 0.027488 / 0.128546 (-0.101058) 0.010904 / 0.075646 (-0.064742) 0.204892 / 0.419271 (-0.214379) 0.037161 / 0.043533 (-0.006372) 0.238488 / 0.255139 (-0.016651) 0.258192 / 0.283200 (-0.025008) 0.018819 / 0.141683 (-0.122864) 1.131573 / 1.452155 (-0.320582) 1.204084 / 1.492716 (-0.288632)

Benchmark: benchmark_getitem_100B.json

metric get_batch_of_1024_random_rows get_batch_of_1024_rows get_first_row get_last_row
new / old (diff) 0.095852 / 0.018006 (0.077846) 0.300225 / 0.000490 (0.299735) 0.000217 / 0.000200 (0.000017) 0.000052 / 0.000054 (-0.000002)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.018592 / 0.037411 (-0.018819) 0.062297 / 0.014526 (0.047772) 0.074344 / 0.176557 (-0.102212) 0.120654 / 0.737135 (-0.616481) 0.075567 / 0.296338 (-0.220772)

Benchmark: benchmark_iterating.json

metric read 5000 read 50000 read_batch 50000 10 read_batch 50000 100 read_batch 50000 1000 read_formatted numpy 5000 read_formatted pandas 5000 read_formatted tensorflow 5000 read_formatted torch 5000 read_formatted_batch numpy 5000 10 read_formatted_batch numpy 5000 1000 shuffled read 5000 shuffled read 50000 shuffled read_batch 50000 10 shuffled read_batch 50000 100 shuffled read_batch 50000 1000 shuffled read_formatted numpy 5000 shuffled read_formatted_batch numpy 5000 10 shuffled read_formatted_batch numpy 5000 1000
new / old (diff) 0.287700 / 0.215209 (0.072491) 2.829536 / 2.077655 (0.751882) 1.446296 / 1.504120 (-0.057824) 1.320912 / 1.541195 (-0.220283) 1.362744 / 1.468490 (-0.105746) 0.563732 / 4.584777 (-4.021045) 2.399904 / 3.745712 (-1.345808) 2.676706 / 5.269862 (-2.593156) 1.744780 / 4.565676 (-2.820896) 0.062884 / 0.424275 (-0.361391) 0.004936 / 0.007607 (-0.002671) 0.338084 / 0.226044 (0.112040) 3.309532 / 2.268929 (1.040603) 1.792791 / 55.444624 (-53.651833) 1.502038 / 6.876477 (-5.374439) 1.662417 / 2.142072 (-0.479655) 0.642835 / 4.805227 (-4.162393) 0.117002 / 6.500664 (-6.383662) 0.041880 / 0.075469 (-0.033589)

Benchmark: benchmark_map_filter.json

metric filter map fast-tokenizer batched map identity map identity batched map no-op batched map no-op batched numpy map no-op batched pandas map no-op batched pytorch map no-op batched tensorflow
new / old (diff) 0.974814 / 1.841788 (-0.866974) 11.430883 / 8.074308 (3.356575) 10.314734 / 10.191392 (0.123342) 0.139838 / 0.680424 (-0.540586) 0.014939 / 0.534201 (-0.519262) 0.288048 / 0.579283 (-0.291235) 0.269146 / 0.434364 (-0.165218) 0.324300 / 0.540337 (-0.216037) 0.421612 / 1.386936 (-0.965324)
PyArrow==latest
Show updated benchmarks!

Benchmark: benchmark_array_xd.json

metric read_batch_formatted_as_numpy after write_array2d read_batch_formatted_as_numpy after write_flattened_sequence read_batch_formatted_as_numpy after write_nested_sequence read_batch_unformated after write_array2d read_batch_unformated after write_flattened_sequence read_batch_unformated after write_nested_sequence read_col_formatted_as_numpy after write_array2d read_col_formatted_as_numpy after write_flattened_sequence read_col_formatted_as_numpy after write_nested_sequence read_col_unformated after write_array2d read_col_unformated after write_flattened_sequence read_col_unformated after write_nested_sequence read_formatted_as_numpy after write_array2d read_formatted_as_numpy after write_flattened_sequence read_formatted_as_numpy after write_nested_sequence read_unformated after write_array2d read_unformated after write_flattened_sequence read_unformated after write_nested_sequence write_array2d write_flattened_sequence write_nested_sequence
new / old (diff) 0.005660 / 0.011353 (-0.005692) 0.003723 / 0.011008 (-0.007285) 0.049909 / 0.038508 (0.011401) 0.033079 / 0.023109 (0.009970) 0.270940 / 0.275898 (-0.004958) 0.291173 / 0.323480 (-0.032307) 0.004336 / 0.007986 (-0.003650) 0.002793 / 0.004328 (-0.001535) 0.049619 / 0.004250 (0.045368) 0.041062 / 0.037052 (0.004010) 0.285026 / 0.258489 (0.026537) 0.322119 / 0.293841 (0.028278) 0.029653 / 0.128546 (-0.098894) 0.010785 / 0.075646 (-0.064861) 0.058680 / 0.419271 (-0.360591) 0.033300 / 0.043533 (-0.010233) 0.269452 / 0.255139 (0.014313) 0.285426 / 0.283200 (0.002226) 0.017655 / 0.141683 (-0.124028) 1.144713 / 1.452155 (-0.307442) 1.196828 / 1.492716 (-0.295888)

Benchmark: benchmark_getitem_100B.json

metric get_batch_of_1024_random_rows get_batch_of_1024_rows get_first_row get_last_row
new / old (diff) 0.096719 / 0.018006 (0.078713) 0.303532 / 0.000490 (0.303042) 0.000223 / 0.000200 (0.000023) 0.000049 / 0.000054 (-0.000005)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.022620 / 0.037411 (-0.014791) 0.077057 / 0.014526 (0.062532) 0.088570 / 0.176557 (-0.087987) 0.128715 / 0.737135 (-0.608421) 0.090844 / 0.296338 (-0.205494)

Benchmark: benchmark_iterating.json

metric read 5000 read 50000 read_batch 50000 10 read_batch 50000 100 read_batch 50000 1000 read_formatted numpy 5000 read_formatted pandas 5000 read_formatted tensorflow 5000 read_formatted torch 5000 read_formatted_batch numpy 5000 10 read_formatted_batch numpy 5000 1000 shuffled read 5000 shuffled read 50000 shuffled read_batch 50000 10 shuffled read_batch 50000 100 shuffled read_batch 50000 1000 shuffled read_formatted numpy 5000 shuffled read_formatted_batch numpy 5000 10 shuffled read_formatted_batch numpy 5000 1000
new / old (diff) 0.298101 / 0.215209 (0.082892) 2.919861 / 2.077655 (0.842206) 1.608945 / 1.504120 (0.104825) 1.487756 / 1.541195 (-0.053439) 1.520800 / 1.468490 (0.052310) 0.576615 / 4.584777 (-4.008162) 0.964250 / 3.745712 (-2.781462) 2.852968 / 5.269862 (-2.416893) 1.868768 / 4.565676 (-2.696908) 0.063934 / 0.424275 (-0.360341) 0.005093 / 0.007607 (-0.002514) 0.352984 / 0.226044 (0.126939) 3.507441 / 2.268929 (1.238513) 1.944467 / 55.444624 (-53.500158) 1.663985 / 6.876477 (-5.212492) 1.847029 / 2.142072 (-0.295043) 0.669228 / 4.805227 (-4.136000) 0.118990 / 6.500664 (-6.381675) 0.041788 / 0.075469 (-0.033681)

Benchmark: benchmark_map_filter.json

metric filter map fast-tokenizer batched map identity map identity batched map no-op batched map no-op batched numpy map no-op batched pandas map no-op batched pytorch map no-op batched tensorflow
new / old (diff) 1.004541 / 1.841788 (-0.837247) 12.525181 / 8.074308 (4.450873) 10.488167 / 10.191392 (0.296775) 0.141182 / 0.680424 (-0.539242) 0.016432 / 0.534201 (-0.517769) 0.283682 / 0.579283 (-0.295601) 0.128277 / 0.434364 (-0.306087) 0.321933 / 0.540337 (-0.218404) 0.416430 / 1.386936 (-0.970506)

@EthanSteinberg
Copy link
Contributor Author

@lhoestq Thanks a ton for helping this get merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support categorical data types for Parquet
3 participants