-
Notifications
You must be signed in to change notification settings - Fork 417
feat(input_pipeline): Add support for chunking long sequences instead truncation #2354
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
bzantium
wants to merge
17
commits into
AI-Hypercomputer:main
Choose a base branch
from
bzantium:feature/#2344
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.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2c0512e
feat(input_pipeline): Add support for chunking long sequences instead…
bzantium 04e8255
Merge branch 'main' into feature/#2344
bzantium 4138263
docs(config): Clarify use_truncation flag with implementation details
bzantium b0b78c3
refactor(input_pipeline): Decouple tokenization from rekeying and sim…
bzantium 3316f5d
Merge branch 'feature/#2344' of https://github.com/bzantium/maxtext i…
bzantium 755ad01
Add comment explaining TokenizeAndChunk behavior
bzantium 8334e32
Update grain tokenizer implementation
bzantium d4dfc19
Add documentation for use_truncation config
bzantium fb770c7
Refactor tokenizer classes to use common variables in base class
bzantium 591376f
Update tokenizer transform test
bzantium 9e47989
Merge branch 'main' of https://github.com/bzantium/maxtext into featu…
bzantium 374fe95
merge changes from latest commmits
bzantium 6176a7a
update grain version
bzantium fa95d40
Merge branch 'main' of https://github.com/bzantium/maxtext into featu…
bzantium 570e36d
use pypi grain version to use pre-compiled wheel and remove add_bos/e…
bzantium b2bc5d3
remove add_bos/eos from tokenizer transform
bzantium cc90eb0
make as it is
bzantium 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
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 |
---|---|---|
@@ -0,0 +1,152 @@ | ||
# Copyright 2023–2025 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" Tests for tokenizer | ||
""" | ||
|
||
import unittest | ||
|
||
import grain.python as grain | ||
import numpy as np | ||
from MaxText.input_pipeline import _grain_tokenizer | ||
from MaxText.input_pipeline import _input_pipeline_utils | ||
from numpy.testing import assert_array_equal | ||
|
||
|
||
class MockTokenizer: | ||
""" | ||
Mocks a tokenizer by splitting on space and mapping letters to simple ints. | ||
e.g., "a b c" -> [1, 2, 3] | ||
""" | ||
def encode(self, text: str) -> list[int]: | ||
if not text: | ||
return [] | ||
# Simple 'a'=1, 'b'=2, ... mapping | ||
return [ord(c) - ord('a') + 1 for c in text.split(' ')] | ||
|
||
|
||
class TokenizerTransformTest(unittest.TestCase): | ||
"""Tests for chunking, trimming, and padding transformations.""" | ||
|
||
def setUp(self): | ||
self.max_len = 5 | ||
self.pad_length = 7 | ||
self.pad_id = 0 | ||
self.feature_names = "text" | ||
self.mock_tokenizer = MockTokenizer() | ||
self.source_data = [ | ||
{"text": "a b c"}, | ||
{"text": "d e f g h i j"}, | ||
{"text": ""}, | ||
{"text": "k l m n o p q r s t"} | ||
] | ||
self.base_ds = grain.MapDataset.source(self.source_data).to_iter_dataset() | ||
|
||
def test_tokenize_and_trim(self): | ||
"""Tests the 1:1 MapTransform (truncation) logic.""" | ||
trim_op = _grain_tokenizer.TokenizeAndTrim( | ||
feature_names=self.feature_names, | ||
sequence_length=self.max_len, | ||
tokenizer=self.mock_tokenizer | ||
) | ||
trim_ds = self.base_ds.map(trim_op) | ||
results = list(trim_ds) | ||
self.assertEqual(len(results), len(self.source_data)) | ||
expected_inputs = [ | ||
np.array([1, 2, 3], dtype=np.int32), | ||
np.array([4, 5, 6, 7, 8], dtype=np.int32), | ||
np.array([], dtype=np.int32), | ||
np.array([11, 12, 13, 14, 15], dtype=np.int32) | ||
] | ||
result_inputs = [r["text"] for r in results] | ||
self.assertEqual(len(result_inputs), len(expected_inputs)) | ||
for res, exp in zip(result_inputs, expected_inputs): | ||
assert_array_equal(res, exp) | ||
|
||
def test_tokenize_and_chunk(self): | ||
"""Tests the 1:N FlatMapTransform (chunking) logic.""" | ||
chunk_op = _grain_tokenizer.TokenizeAndChunk( | ||
feature_names=self.feature_names, | ||
sequence_length=self.max_len, | ||
tokenizer=self.mock_tokenizer | ||
) | ||
chunk_ds = self.base_ds.apply(chunk_op) | ||
results = list(chunk_ds) | ||
self.assertEqual(len(results), 5) | ||
expected_inputs = [ | ||
np.array([1, 2, 3], dtype=np.int32), | ||
np.array([4, 5, 6, 7, 8], dtype=np.int32), | ||
np.array([9, 10], dtype=np.int32), | ||
np.array([11, 12, 13, 14, 15], dtype=np.int32), | ||
np.array([16, 17, 18, 19, 20], dtype=np.int32) | ||
] | ||
result_inputs = [r["text"] for r in results] | ||
self.assertEqual(len(result_inputs), len(expected_inputs)) | ||
for res, exp in zip(result_inputs, expected_inputs): | ||
assert_array_equal(res, exp) | ||
|
||
def test_trim_and_pad_chaining(self): | ||
"""Tests chaining TokenizeAndTrim.map() -> PadOrTrimToMaxLength.map()""" | ||
trim_op = _grain_tokenizer.TokenizeAndTrim( | ||
feature_names=self.feature_names, | ||
sequence_length=self.max_len, | ||
tokenizer=self.mock_tokenizer | ||
) | ||
pad_op = _input_pipeline_utils.PadOrTrimToMaxLength( | ||
max_length=self.pad_length, | ||
pad_id=self.pad_id | ||
) | ||
chained_ds = self.base_ds.map(trim_op).map(pad_op) | ||
results = list(chained_ds) | ||
self.assertEqual(len(results), len(self.source_data)) | ||
expected_inputs = [ | ||
np.array([1, 2, 3, 0, 0, 0, 0], dtype=np.int32), | ||
np.array([4, 5, 6, 7, 8, 0, 0], dtype=np.int32), | ||
np.array([0, 0, 0, 0, 0, 0, 0], dtype=np.int32), | ||
np.array([11, 12, 13, 14, 15, 0, 0], dtype=np.int32) | ||
] | ||
result_inputs = [r["text"] for r in results] | ||
self.assertEqual(len(result_inputs), len(expected_inputs)) | ||
for res, exp in zip(result_inputs, expected_inputs): | ||
assert_array_equal(res, exp) | ||
|
||
def test_chunk_and_pad_chaining(self): | ||
"""Tests chaining TokenizeAndChunk.apply() -> PadOrTrimToMaxLength.map()""" | ||
chunk_op = _grain_tokenizer.TokenizeAndChunk( | ||
feature_names=self.feature_names, | ||
sequence_length=self.max_len, | ||
tokenizer=self.mock_tokenizer | ||
) | ||
pad_op = _input_pipeline_utils.PadOrTrimToMaxLength( | ||
max_length=self.pad_length, | ||
pad_id=self.pad_id | ||
) | ||
chained_ds = self.base_ds.apply(chunk_op).map(pad_op) | ||
results = list(chained_ds) | ||
self.assertEqual(len(results), 5) | ||
expected_inputs = [ | ||
np.array([1, 2, 3, 0, 0, 0, 0], dtype=np.int32), | ||
np.array([4, 5, 6, 7, 8, 0, 0], dtype=np.int32), | ||
np.array([9, 10, 0, 0, 0, 0, 0], dtype=np.int32), | ||
np.array([11, 12, 13, 14, 15, 0, 0], dtype=np.int32), | ||
np.array([16, 17, 18, 19, 20, 0, 0], dtype=np.int32), | ||
] | ||
result_inputs = [r["text"] for r in results] | ||
self.assertEqual(len(result_inputs), len(expected_inputs)) | ||
for res, exp in zip(result_inputs, expected_inputs): | ||
assert_array_equal(res, exp) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
Uh oh!
There was an error while loading. Please reload this page.