Skip to content

[Python] Fix HuggingFacePipelineModelHandler single-dict batch handling and MLM IT - #41645

Merged
damccorm merged 1 commit into
masterfrom
users/damccorm/py313-inference-2-huggingface
Nov 25, 2026
Merged

damccorm merged 1 commit into
masterfrom
users/damccorm/py313-inference-2-huggingface

Conversation

@damccorm

@damccorm damccorm commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Part of #41652 (stack: https://github.com/apache/beam/stack/41650).

Stack 2/6 — Fixes two issues blocking direct:py313:transformersInferenceTest:

  1. HuggingFacePipelineModelHandler single-element batch regression (huggingface_inference.py, huggingface_inference_test.py):

    • Since use utils._convert_to_result for huggingface_inference #36593 switched HuggingFacePipelineModelHandler.run_inference to utils._convert_to_result, Hugging Face pipelines that return a single dict (instead of a list[dict]) for batch size 1 — such as QuestionAnsweringPipeline — crashed with TypeError: 'float' object is not iterable because utils._convert_to_result treats a bare dict as a columnar dict of batch-length iterables.
    • Wraps isinstance(predictions, dict) and len(batch) == 1 into [predictions] inside _default_pipeline_inference_fn so built-in HF pipelines work for batch size 1 while custom inference_fn implementations that intentionally return columnar dicts (such as Beam YAML's runinference_huggingface.yaml) remain untouched.
    • Adds unit tests covering single-dict predictions, batched list predictions, and custom columnar-dict inference_fns.
  2. Unavailable third-party Hub model & transformers>=5 tokenizer call (huggingface_inference_it_test.py, huggingface_language_modeling.py, examples/inference/README.md):

    • Replaces the deleted/private personal Hub repo stevhliu/my_awesome_eli5_mlm_model with FacebookAI/roberta-base, inlining _EXPECTED_MASKED_LM_OUTPUTS in huggingface_inference_it_test.py so the shared GCS golden file is left unchanged for older release branches.
    • Replaces removed tokenizer.encode_plus(...) with tokenizer(...) in huggingface_language_modeling.py for transformers>=5 compatibility.

Verification

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.73%. Comparing base (e502259) to head (c92f42e).

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #41645   +/-   ##
=========================================
  Coverage     58.72%   58.73%           
  Complexity     2314     2314           
=========================================
  Files          1135     1135           
  Lines        170486   170488    +2     
  Branches       1499     1499           
=========================================
+ Hits         100124   100137   +13     
+ Misses        67818    67807   -11     
  Partials       2544     2544           
Flag Coverage Δ
python 80.67% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@damccorm
damccorm marked this pull request as ready for review November 24, 2026 19:57
@damccorm

Copy link
Copy Markdown
Contributor Author

stop reviewer notifications

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR cleanly resolves two issues blocking direct:py313:transformersInferenceTest: it handles single-element batches where Hugging Face pipelines (such as QuestionAnsweringPipeline) return a bare dict instead of a list[dict], and it updates the masked language modeling example and integration tests to use FacebookAI/roberta-base and tokenizer(...) for transformers>=5 compatibility.

🔍 General Feedback

  • Wrapping bare dict predictions inside _default_pipeline_inference_fn when len(batch) == 1 is well-scoped, as it fixes built-in Hugging Face pipeline behavior without breaking custom inference_fn implementations that rely on utils._convert_to_result transposing columnar dicts.
  • Inlining _EXPECTED_MASKED_LM_OUTPUTS in huggingface_inference_it_test.py preserves backwards compatibility for release branches referencing the existing GCS golden file.
  • Unit and integration test coverage is thorough and well-structured.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

Base automatically changed from users/damccorm/py313-inference-1-xgboost to master November 24, 2026 20:08
…ng and MLM IT

- huggingface_inference.py & huggingface_inference_test.py: wrap single-dict
  predictions returned by Hugging Face pipelines (e.g. QuestionAnsweringPipeline
  on batch size 1) in a list inside _default_pipeline_inference_fn before
  utils._convert_to_result, fixing the regression from #36593 while preserving
  custom inference_fn implementations that return columnar dicts (e.g. Beam YAML).
- huggingface_inference_it_test.py, huggingface_language_modeling.py, README.md:
  replace the deleted/private stevhliu/my_awesome_eli5_mlm_model Hub checkpoint
  with FacebookAI/roberta-base (inlining expected outputs so the shared GCS
  golden file is unchanged) and replace removed tokenizer.encode_plus with
  tokenizer(...) for transformers 5.x compatibility.
@damccorm
damccorm force-pushed the users/damccorm/py313-inference-2-huggingface branch from 6d17cd5 to c92f42e Compare November 24, 2026 20:08

@jrmccluskey jrmccluskey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, big fan of inlining the expected output in tests for clarity

@github-actions

Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: requested by reviewer. If you'd like to restart, comment assign set of reviewers

@damccorm
damccorm merged commit 83a9f40 into master Nov 25, 2026
90 of 92 checks passed
@damccorm
damccorm deleted the users/damccorm/py313-inference-2-huggingface branch November 25, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples python size/s S: 10-49 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants