Skip to content
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

[Python] Hugging Face pipeline support #27399

Merged
merged 35 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
06af6b7
automodel first pass
riteshghorse May 10, 2023
416166d
new model
riteshghorse May 16, 2023
6f063e5
updated model handler api
riteshghorse Jun 21, 2023
df87366
add model_class param
riteshghorse Jun 23, 2023
4da7edd
Merge branch 'master' of https://github.com/apache/beam into hf-model…
riteshghorse Jun 23, 2023
025cc52
update doc comments
riteshghorse Jun 26, 2023
8cf7a01
Merge branch 'master' of https://github.com/apache/beam into hf-model…
riteshghorse Jun 26, 2023
2c671ab
updated integration test and example
riteshghorse Jun 26, 2023
abaeb2a
unit test, modified params
riteshghorse Jun 27, 2023
d5e1cf3
add test setup for hugging face tests
riteshghorse Jun 27, 2023
4177c09
fix lints
riteshghorse Jun 27, 2023
6324752
fix import order
riteshghorse Jun 27, 2023
30029d3
refactor, doc, lints
riteshghorse Jun 28, 2023
c60d312
refactor, doc comments
riteshghorse Jun 29, 2023
a52536f
change test file
riteshghorse Jun 29, 2023
496d205
update types
riteshghorse Jul 7, 2023
8dd0ff2
add hugging face pipeline support
riteshghorse Jul 7, 2023
c670ada
integration test for pipeline
riteshghorse Jul 10, 2023
09e64a4
add doc, gs link
riteshghorse Jul 11, 2023
504b161
test raises exception
riteshghorse Jul 11, 2023
4ece137
fix python lints
riteshghorse Jul 18, 2023
250a2d5
add inference fn
riteshghorse Jul 24, 2023
4d6b6b2
Merge branch 'master', remote-tracking branch 'origin' into hf-pipeline
riteshghorse Jul 24, 2023
4787635
update doc
riteshghorse Jul 24, 2023
c9fa0d5
merge master
riteshghorse Jul 24, 2023
c592f91
docs, lint
riteshghorse Jul 24, 2023
db99ad0
docs, lint
riteshghorse Jul 24, 2023
b539d32
remove optional from inference_fn
riteshghorse Jul 24, 2023
e912d35
add enum for tasks
riteshghorse Jul 26, 2023
ba5e31f
update pydoc
riteshghorse Jul 26, 2023
6963a5d
update pydoc
riteshghorse Jul 26, 2023
44916b9
doc, formatting changes
riteshghorse Aug 1, 2023
4d3fdd0
fix doc
riteshghorse Aug 1, 2023
9b64975
fix optional in doc
riteshghorse Aug 1, 2023
7db987b
pin model version
riteshghorse Aug 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from apache_beam.ml.inference.base import PredictionResult
from apache_beam.ml.inference.base import RunInference
from apache_beam.ml.inference.huggingface_inference import HuggingFacePipelineModelHandler
from apache_beam.ml.inference.huggingface_inference import PipelineTask
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import SetupOptions
from apache_beam.runners.runner import PipelineResult
Expand Down Expand Up @@ -102,7 +103,10 @@ def run(
if not test_pipeline:
pipeline = beam.Pipeline(options=pipeline_options)

model_handler = HuggingFacePipelineModelHandler(model=known_args.model_name, )
model_handler = HuggingFacePipelineModelHandler(
task=PipelineTask.QuestionAnswering,
model=known_args.model_name,
load_model_args={'framework': 'pt'})
if not known_args.input:
text = (
pipeline | 'CreateSentences' >> beam.Create([
Expand Down
39 changes: 37 additions & 2 deletions sdks/python/apache_beam/ml/inference/huggingface_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import logging
import sys
from collections import defaultdict
from enum import Enum
from typing import Any
from typing import Callable
from typing import Dict
Expand Down Expand Up @@ -71,6 +72,39 @@
Iterable[PredictionResult]]


class PipelineTask(str, Enum):
riteshghorse marked this conversation as resolved.
Show resolved Hide resolved
AudioClassification = 'audio-classification'
riteshghorse marked this conversation as resolved.
Show resolved Hide resolved
AutomaticSpeechRecognition = 'automatic-speech-recognition'
Conversational = 'conversational'
DepthEstimation = 'depth-estimation'
DocumentQuestionAnswering = 'document-question-answering'
FeatureExtraction = 'feature-extraction'
FillMask = 'fill-mask'
ImageClassification = 'image-classification'
ImageSegmentation = 'image-segmentation'
ImageToText = 'image-to-text'
MaskGeneration = 'mask-generation'
NER = 'ner'
ObjectDetection = 'object-detection'
QuestionAnswering = 'question-answering'
SentimentAnalysis = 'sentiment-analysis'
Summarization = 'summarization'
TableQuestionAnswering = 'table-question-answering'
TextClassification = 'text-classification'
TextGeneration = 'text-generation'
Text2TextGeneration = 'text2text-generation'
TokenClassification = 'token-classification'
Translation = 'translation'
VideoClassification = 'video-classification'
VisualQuestionAnswering = 'visual-question-answering'
VQA = 'vqa'
ZeroShotAudioClassification = 'zero-shot-audio-classification'
ZeroShotClassification = 'zero-shot-classification'
ZeroShotImageClassification = 'zero-shot-image-classification'
ZeroShotObjectDetection = 'zero-shot-object-detection'
Translation_XX_to_YY = 'translation_XX_to_YY'


def _validate_constructor_args(model_uri, model_class):
message = (
"Please provide both model class and model uri to load the model."
Expand Down Expand Up @@ -525,7 +559,7 @@ class HuggingFacePipelineModelHandler(ModelHandler[str,
Pipeline]):
def __init__(
self,
task: str = "",
task: Union[str, PipelineTask] = "",
model=None,
*,
inference_fn: PipelineInferenceFn = _default_pipeline_inference_fn,
Expand All @@ -547,7 +581,8 @@ def __init__(
task="fill-mask"))

Args:
task (str): task supported by HuggingFace Pipelines.
task (str or Enum): task supported by HuggingFace Pipelines.
Accepts a string task or an Enum from PipelineTask.
model : path to pretrained model on Hugging Face Models Hub to use custom
model for the chosen task. If the model already defines the task then
no need to specify the task parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

@pytest.mark.uses_transformers
@pytest.mark.it_postcommit
@pytest.mark.timeout(1800)
class HuggingFaceInference(unittest.TestCase):
@pytest.mark.timeout(1800)
def test_hf_language_modeling(self):
test_pipeline = TestPipeline(is_integration_test=True)
# Path to text file containing some sentences
Expand Down
Loading