Repository navigation
Conversation
Contributor
Author
|
/gemini review |
Contributor
There was a problem hiding this comment.
📋 Review Summary
This PR significantly improves the performance and accuracy of get_num_bytes for PyTorch and HuggingFace tensor model handlers by replacing element-wise Python iteration with tensor.element_size() * tensor.nelement(), and optimizes _default_pandas_inference_fn in scikit-learn inference by avoiding redundant iloc slicing when inputs are single-row DataFrames.
🔍 General Feedback
- The changes are clean, accurate, and well-targeted at hot paths during batch inference.
- Fixing
HuggingFaceModelHandlerTensor.get_num_bytesto handle non-dict PyTorch tensors also resolves an existing bug where calling.values()on atorch.Tensorwould fail or reference the bound method rather than computing tensor size.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
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.
Description
Optimizes two hot paths in
apache_beam.ml.inference:PytorchModelHandlerTensor,PytorchModelHandlerKeyedTensor,HuggingFaceModelHandlerTensor, andHuggingFaceModelHandlerKeyedTensor(get_num_bytes):get_num_bytesiterated over tensor elements in Python (sum(el.element_size() for tensor in batch for el in tensor)), which both iterated over the first dimension in Python and undercounted multi-dimensional tensors (summingelement_size()per row rather than total elements).tensor.element_size() * tensor.nelement()computes the exact tensor byte footprint in256x256float32tensor batches:4.17s->0.0021sfor 1,000 tensors)._default_pandas_inference_fninsklearn_inference.py:DataFrameinbatchis a single row (len(batch) == vectorized_batch.shape[0]), returns(predictions, batch)directly instead of slicingvectorized_batch.iloc[[i]]Testing
pytest sdks/python/apache_beam/ml/inference/pytorch_inference_test.py sdks/python/apache_beam/ml/inference/sklearn_inference_test.py sdks/python/apache_beam/ml/inference/huggingface_inference_test.py(64 passed).