-
Notifications
You must be signed in to change notification settings - Fork 16
refactor: improved the throughput of the tokenized file writer by usi… #432
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
Open
le1nux
wants to merge
2
commits into
main
Choose a base branch
from
improve_data_writeout_perf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+54
−26
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import math | ||
| import os | ||
| import pickle | ||
| from itertools import repeat | ||
| from pathlib import Path | ||
| from typing import BinaryIO | ||
|
|
||
|
|
@@ -82,30 +81,56 @@ def _write_index_segment(file_descriptor: BinaryIO, index_list: list[tuple[int, | |
| def _write_data_segment( | ||
| file_descriptor: BinaryIO, token_data: list[np.ndarray], token_size_in_bytes: int, write_batch_size: int | ||
| ) -> list[tuple[int, int]]: | ||
| def encoded_token_to_bytes(encoded_token: int, token_size_in_bytes: int) -> bytes: | ||
| # Converts an token_ids to its byte representation. | ||
| try: | ||
| token_bytes = encoded_token.to_bytes(token_size_in_bytes, byteorder="little", signed=False) | ||
| except OverflowError as e: | ||
| raise ValueError(f"Token {encoded_token} cannot be represented by {token_size_in_bytes} bytes.") from e | ||
| return token_bytes | ||
|
|
||
| samples = [] | ||
| index_list = [] | ||
| # Fast path: vectorized cast + tobytes (no per-token Python work). | ||
| # Preserves little-endian unsigned representation and overflow checks. | ||
|
|
||
| if token_size_in_bytes == 1: | ||
| dtype = np.dtype("u1") | ||
| elif token_size_in_bytes == 2: | ||
| dtype = np.dtype("<u2") # force little-endian | ||
| elif token_size_in_bytes == 4: | ||
| dtype = np.dtype("<u4") # force little-endian | ||
| else: | ||
| raise ValueError("Currently only support token byte sizes of 1, 2, and 4.") | ||
|
|
||
| max_allowed = (1 << (8 * token_size_in_bytes)) - 1 | ||
|
|
||
| samples: list[bytes] = [] | ||
| index_list: list[tuple[int, int]] = [] | ||
| curr_offset = 0 | ||
| pending = 0 | ||
|
|
||
| for sample_tokens in token_data: | ||
| # convert token_ids to byte representation | ||
| sample_token_byte_string = b"".join( | ||
| map(encoded_token_to_bytes, sample_tokens.tolist(), repeat(token_size_in_bytes)) | ||
| ) | ||
| arr = np.asarray(sample_tokens) | ||
|
|
||
| # ---- Overflow / range check (preserves original semantics) ---- | ||
| if arr.size: | ||
| min_val = int(arr.min()) | ||
| max_val = int(arr.max()) | ||
| if min_val < 0 or max_val > max_allowed: | ||
| raise ValueError( | ||
| f"Token values out of range for {token_size_in_bytes} bytes: " | ||
| f"min={min_val}, max={max_val}, allowed=[0, {max_allowed}]" | ||
| ) | ||
|
Comment on lines
+111
to
+114
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be helpful to identify the faulty token (as in the previous implementation) or even better the index in token_data (via enumerate) and the index in arr (via argmax/argmin) here. |
||
| # ---------------------------------------------------------------- | ||
|
|
||
| # Cast to correct unsigned little-endian dtype | ||
| arr = np.asarray(arr, dtype=dtype, order="C") | ||
| sample_token_byte_string = arr.tobytes(order="C") | ||
|
|
||
| samples.append(sample_token_byte_string) | ||
| index_list.append((curr_offset, len(sample_token_byte_string))) | ||
| curr_offset += len(sample_token_byte_string) | ||
| if len(samples) % write_batch_size == 0: | ||
|
|
||
| pending += 1 | ||
| if pending >= write_batch_size: | ||
| file_descriptor.write(b"".join(samples)) | ||
| samples = [] | ||
| samples.clear() | ||
| pending = 0 | ||
|
|
||
| if len(samples) > 0: | ||
| file_descriptor.write(b"".join(samples)) | ||
|
|
||
| return index_list | ||
|
|
||
| @staticmethod | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the more readable version would be fast enough here. If we really need a fast one just hardcode the 3 possible values in above if-else-clause. :D