Send map workers only their shard's rows instead of the whole table - #8611
Open
behroozazarkhalili wants to merge 1 commit into
Open
Send map workers only their shard's rows instead of the whole table#8611behroozazarkhalili wants to merge 1 commit into
behroozazarkhalili wants to merge 1 commit into
Conversation
…table With num_proc>1, Dataset.map shards the table with Arrow slices, and Arrow slices share their parent buffers. Pickling a shard for a worker therefore serialized the entire table once per shard, so a 2-process map moved 2x the dataset through the pool queues and was slower than a single process on in-memory datasets (huggingface#1992). Before a job is submitted, compact its shard to only its rows: take the shard's indices when it carries an indices mapping (select, filter, shuffle, train_test_split), copy each chunk so the chunk layout, and therefore 32-bit offset limits, are preserved, and rebuild string_view and binary_view columns from their values since concatenating a view array keeps the parent buffer. In-memory ConcatenationTables are compacted through their combined table; memory-mapped tables are left alone because they pickle by path. Compaction is per job, lazily, on a shallow copy of the shard so fingerprints, features and format are unchanged, and any Arrow error falls back to the uncompacted shard so unsupported types still map. Measured with 100 unique rows and 2 workers: each worker payload now carries 50 rows instead of 100 for plain, indexed and concatenated in-memory datasets. Fixes huggingface#1992
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #1992
Root cause
Dataset.map(num_proc>1)shards the table with Arrow slices (table.slice), and Arrow slices share their parent buffers. When a shard is pickled for a worker,InMemoryTable.__getstate__serializes the sliced arrays' full buffers, so every worker receives the whole table. A 2-process map therefore moved 2x the dataset through the pool queues, which is why it came out slower than a single process on in-memory datasets.Fix
Right before each job is submitted to the pool, compact its shard to only its rows:
select,filter,shuffle,train_test_split), so index-mapped shards no longer ship the full table either.string_view/binary_viewcolumns (top-level or nested) from their values, because concatenating a view array keeps the parent value buffer.ConcatenationTableinputs (the result ofconcatenate_datasets) are compacted through their combined table; memory-mapped tables are left alone because they pickle by path.Measurements
100 unique 140-byte rows,
num_proc=2, bytes of each worker's pickled shard and how many of the 100 source rows appear in it:Dataset.from_dictselectconcatenate_datasets(70 + 30 rows)string_viewcolumnMap results, order, features and fingerprints match upstream for all of these, including formatted (numpy/torch) datasets,
Array2Dextension columns, large types, andnum_proc > num_shards.Tests
New
tests/test_map_multiprocessing.py: captures the exact bytes each worker receives and asserts other shards' rows are absent for plain, indexed, concatenated and view-typed inputs; checks results and fingerprints across memory/indexed/disk storage; preserves chunk boundaries above the offset limit and nested chunked features; and exercises the fallback path by injecting Arrow errors.