gemini-code-assist[bot] commented on code in PR #38696: URL: https://github.com/apache/beam/pull/38696#discussion_r3329051556
########## sdks/python/apache_beam/yaml/tests/runinference_huggingface.yaml: ########## @@ -0,0 +1,62 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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 +# +# http://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. + +pipelines: + - pipeline: + type: chain + transforms: + - type: Create + config: + elements: + - text: "I love Apache Beam!" + - text: "I hate this error." + - type: RunInference + config: + model_handler: + type: "HuggingFacePipeline" + config: + task: "text-classification" + inference_fn: + callable: | + def real_inference(batch, pipeline, inference_args): + predictions = pipeline(batch, **inference_args) + + # If it's a single dictionary (batch size of 1), wrap it in a list + if isinstance(predictions, dict): + predictions = [predictions] + + return { + 'label': [p['label'] for p in predictions], + 'score': [p['score'] for p in predictions] + } Review Comment:  The custom `inference_fn` (`real_inference`) currently returns a single dictionary of lists. In Apache Beam's `RunInference`, the model handler's `run_inference` (or the custom `inference_fn`) is expected to return an iterable of predictions of the same length as the input `batch`. Returning a single dictionary will cause Beam to iterate over the dictionary's keys (`'label'` and `'score'`) when zipping with the batch elements. This results in incorrect mapping and will cause a `TypeError` downstream when attempting to access `x.inference.inference["label"]`. Instead, the function should return a list of dictionaries, where each dictionary corresponds to a single prediction in the batch. ```yaml return [ { 'label': p['label'], 'score': p['score'] } for p in predictions ] ``` ########## sdks/python/setup.py: ########## @@ -654,7 +654,8 @@ def get_portability_package_data(): 'transformers': [ 'transformers>=4.28.0,<4.56.0', 'tensorflow>=2.12.0', - 'torch>=1.9.0' + # Avoid torch 2.12.0+ which fails to run unit tests with segfault + 'torch>=1.9.0,<2.12.0' Review Comment:  The PyTorch version constraint is set to `'torch>=1.9.0,<2.12.0'` with a comment mentioning version `2.12.0+`. However, PyTorch (torch) does not have a version `2.12.0` (the current latest versions are in the `2.x` range, up to `2.5.x`/`2.6.x`). This seems to be a typo or confusion with TensorFlow's version `2.12.0`. Please clarify which PyTorch version actually causes the segfault (e.g., `2.1.2` or `2.2.0`) and update both the comment and the version constraint accordingly. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
