Fix silent string truncation in PyGrain shared memory batching#1328
Open
copybara-service[bot] wants to merge 1 commit into
Open
Fix silent string truncation in PyGrain shared memory batching#1328copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
The prompt truncation is caused by a silent data-loss bug in PyGrain's shared memory batch stacking implementation which is active during evaluation when `num_workers > 0` (multi-process prefetching). To improve multi-process data loading performance, Kauldron automatically enables `output_to_shared_memory=True` inside PyGrain's batching transformations. When batching elements inside `shm_stacking_function`, the destination `SharedMemoryArray`'s `shape` and `dtype` are inferred exclusively from the first element in the batch: ```python first_arg = np.asanyarray(args[0]) shape, dtype = (len(args),) + first_arg.shape, first_arg.dtype ``` In a batch containing variable-length string elements, the `SharedMemoryArray` is allocated with a fixed-width unicode dtype bounded by that first string. When `np.stack(args, out=...)` copies subsequent longer strings into the allocated fixed-width shared memory array, NumPy silently truncates the strings to the array's fixed width! PiperOrigin-RevId: 919292388
bace627 to
305ca6d
Compare
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.
Fix silent string truncation in PyGrain shared memory batching
The prompt truncation is caused by a silent data-loss bug in PyGrain's shared memory batch stacking implementation which is active during evaluation when
num_workers > 0(multi-process prefetching).To improve multi-process data loading performance, Kauldron automatically enables
output_to_shared_memory=Trueinside PyGrain's batching transformations.When batching elements inside
shm_stacking_function, the destinationSharedMemoryArray'sshapeanddtypeare inferred exclusively from the first element in the batch:In a batch containing variable-length string elements, the
SharedMemoryArrayis allocated with a fixed-width unicode dtype bounded by that first string.When
np.stack(args, out=...)copies subsequent longer strings into the allocated fixed-width shared memory array, NumPy silently truncates the strings to the array's fixed width!