tvalentyn opened a new issue, #41913: URL: https://github.com/apache/beam/issues/41913
### What happened? disclaimer: I asked an AI tool to summarize this bug and a fix. ### What happened? `HuggingFacePipelineModelHandler` breaks when wrapped in `KeyedModelHandler` (single model) or `MaybeKeyedModelHandler` as soon as a batch has more than one element. Beam YAML `RunInference` is affected too, because it always wraps the handler in `KeyedModelHandler`. **Cause.** Both wrappers split the batch with `keys, unkeyed_batch = zip(*batch)` and pass the resulting `tuple` to the wrapped handler ([KeyedModelHandler](https://github.com/apache/beam/blob/2317184bc1b65d2097ed6ef9e90e8f21ea4b4539/sdks/python/apache_beam/ml/inference/base.py#L1043), [MaybeKeyedModelHandler](https://github.com/apache/beam/blob/2317184bc1b65d2097ed6ef9e90e8f21ea4b4539/sdks/python/apache_beam/ml/inference/base.py#L1402)). The handler [passes it to the pipeline unchanged](https://github.com/apache/beam/blob/2317184bc1b65d2097ed6ef9e90e8f21ea4b4539/sdks/python/apache_beam/ml/inference/huggingface_inference.py#L791). A Hugging Face pipeline only treats a `list` as a batch (`is_list = isinstance(inputs, list)` in `Pipeline.__call__`); a tuple is processed as a single input. **Effect** (Beam at 2317184bc1b, transformers 5.20.2, keyed batch of 3): | Task | Result | |---|---| | `text-classification` | `TypeError: 'float' object is not iterable`, or `ValueError: Unable to create tensor ...` when the texts differ in token length | | `text-generation` | `TypeError: can only concatenate str (not "tuple") to str`, also for a batch of one | | `fill-mask` | no error when token lengths match, but wrong output: each key gets a candidate that belongs to the first input | The same inputs work without keys, and with a multi-model `KeyedModelHandler` (`KeyModelMapping`), because those paths pass a `list`. **Reproduce** ```python import apache_beam as beam from apache_beam.ml.inference.base import KeyedModelHandler from apache_beam.ml.inference.base import RunInference from apache_beam.ml.inference.huggingface_inference import HuggingFacePipelineModelHandler model_handler = HuggingFacePipelineModelHandler( task='text-classification', model='distilbert-base-uncased-finetuned-sst-2-english', min_batch_size=3, max_batch_size=3) with beam.Pipeline() as p: _ = ( p | beam.Create([('a', 'I love Apache Beam!'), ('b', 'I hate this error message.'), ('c', 'Fine.')]) | RunInference(KeyedModelHandler(model_handler)) | beam.Map(print)) ``` Actual: `ValueError: Unable to create tensor, you should probably activate truncation and/or padding ...` Expected: one prediction per key, the same as without `KeyedModelHandler`. ### Issue Priority Priority: 2 (default / most bugs should be filed as P2) ### Issue Components - [x] Component: Python SDK - [ ] Component: Java SDK - [ ] Component: Go SDK - [ ] Component: Typescript SDK - [ ] Component: IO connector - [ ] Component: Beam YAML - [ ] Component: Beam examples - [ ] Component: Beam playground - [ ] Component: Beam katas - [ ] Component: Website - [ ] Component: Infrastructure - [ ] Component: Spark Runner - [ ] Component: Flink Runner - [ ] Component: Prism Runner - [ ] Component: Hazelcast Jet Runner - [ ] Component: Google Cloud Dataflow Runner -- 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]
