-
Notifications
You must be signed in to change notification settings - Fork 10
Implement initial version of Rust batch support #229
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright 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. | ||
|
|
||
| pub use super::example_iteration::{ | ||
| get_shard_progress, CompressionType, Example, ExampleIterator, ShardInfo, ShardProgress, | ||
| }; | ||
| pub use super::parallel_map::parallel_map; | ||
| pub use super::shard_generated::sedpack::io::flatbuffer::shardfile::{root_as_shard, Shard}; | ||
|
|
||
| /// Single attribute which has been batched. | ||
| pub enum BatchedAttribute { | ||
| /// Row-major order batch of the attribute with static (fixed) size. That is in NumPy C-order | ||
| /// we can index as data[batch_index][attribute_index] where batch_index in 0..batch_size and | ||
| /// attribute_index in 0..len(attribute). | ||
| Static { data: numpy::ndarray::Array<u8, numpy::Ix1> }, | ||
| /// Dynamic data where we do not know shape up front (e.g., string, bytearray) is represented | ||
| /// as a vector with the same indexing semantic. | ||
| Dynamic { data: Vec<numpy::ndarray::Array<u8, numpy::Ix1>> }, | ||
| } | ||
|
|
||
| pub type Batch = Vec<BatchedAttribute>; | ||
|
|
||
| struct Batcher { | ||
| example_iterator: Box<dyn Iterator<Item = Example> + Send>, | ||
| batch_size: usize, | ||
| has_fixed_shape: Vec<bool>, | ||
| } | ||
|
|
||
| impl Iterator for Batcher { | ||
| type Item = Batch; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| // Collect examples. | ||
| let cache: Vec<Example> = self.example_iterator.by_ref().take(self.batch_size).collect(); | ||
|
|
||
| // Decide if we have enough (the last batch might not have batch_size examples). | ||
| if cache.is_empty() { | ||
| return None; | ||
| } | ||
|
|
||
| // Batch the examples. | ||
| let mut result = Batch::new(); | ||
| for (attribute_index, is_fixed) in self.has_fixed_shape.iter().enumerate() { | ||
| // Collect batched version of current attribute across all cached examples. | ||
| let current_batched_attribute = match is_fixed { | ||
| true => BatchedAttribute::Static { | ||
| data: numpy::ndarray::Array::<u8, numpy::Ix1>::from_iter( | ||
| cache.iter().flat_map(|e| e[attribute_index].iter().cloned()), | ||
| ), | ||
| }, | ||
| false => BatchedAttribute::Dynamic { | ||
| data: cache | ||
| .iter() | ||
| .map(|e| { | ||
| numpy::ndarray::Array::<u8, numpy::Ix1>::from_iter( | ||
| e[attribute_index].iter().cloned(), | ||
| ) | ||
| }) | ||
| .collect(), | ||
| }, | ||
| }; | ||
|
|
||
| // Save the batched attribute. | ||
| result.push(current_batched_attribute); | ||
| } | ||
| Some(result) | ||
| } | ||
| } | ||
|
|
||
| pub struct BatchIterator { | ||
| batch_iterator: Box<dyn Iterator<Item = Batch> + Send>, | ||
| } | ||
|
|
||
| impl BatchIterator { | ||
| pub fn new( | ||
| files: Vec<ShardInfo>, threads: usize, batch_size: usize, has_fixed_shape: Vec<bool>, | ||
| ) -> Self { | ||
| let example_iterator = Box::new(ExampleIterator::new(files, threads)); | ||
| let batch_iterator = Box::new(Batcher { example_iterator, batch_size, has_fixed_shape }); | ||
| BatchIterator { batch_iterator } | ||
| } | ||
| } | ||
|
|
||
| impl Iterator for BatchIterator { | ||
| type Item = Batch; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| self.batch_iterator.next() | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.