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

Support Embeddings in mltransform #29564

Merged
merged 58 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
80e5c4a
Make base.py framework agnostic and add helper transforms
AnandInguva Nov 29, 2023
0d34847
Add tests for base.py
AnandInguva Nov 29, 2023
58b24f6
Add sentence-transformers
AnandInguva Nov 29, 2023
88f9ceb
Add tensorflow hub
AnandInguva Nov 29, 2023
23f7027
Add vertex_ai
AnandInguva Nov 29, 2023
04ebdb0
Make TFTProcessHandler a PTransform
AnandInguva Nov 29, 2023
f86c259
raise RuntimeError in ArtifactsFetcher when it is used for embeddings
AnandInguva Nov 29, 2023
fc4ec00
Add JsonPickle to requirements
AnandInguva Nov 29, 2023
3da5ce8
Add tox tests
AnandInguva Nov 29, 2023
4b4ee58
Mock frameworks in pydocs
AnandInguva Nov 29, 2023
01ba217
Add Row type check
AnandInguva Dec 4, 2023
f080c25
Remove requires_chaining
AnandInguva Dec 4, 2023
6111c31
change name of PTransformProvider to MLTransformProvider
AnandInguva Dec 4, 2023
ba24e81
remove batch_len in utility fun
AnandInguva Dec 4, 2023
d690aec
Change type annotation and redundant comments
AnandInguva Dec 4, 2023
af7496b
Remove get_transforms method
AnandInguva Dec 4, 2023
d713555
remove requires_chaining from tft
AnandInguva Dec 4, 2023
50450f3
add tests to sentence-transformers
AnandInguva Dec 4, 2023
c2b691f
Merge remote-tracking branch 'origin/master' into embeddings_mltransform
AnandInguva Dec 5, 2023
8823a75
Pass inference_args to RunInference
AnandInguva Dec 5, 2023
a7e2bd3
Add TODO GH issue
AnandInguva Dec 5, 2023
519b3ed
Merge branch 'embeddings_mltransform' of https://github.com/AnandIngu…
AnandInguva Dec 5, 2023
f77ae60
refactor variables in vertex_ai embeddings
AnandInguva Dec 5, 2023
95ed3c5
remove try/catch and throw error if options is empty for GCS artifact…
AnandInguva Dec 5, 2023
c235499
Refactor NotImplementedError message
AnandInguva Dec 5, 2023
6eebfa4
remove tensorflow hub from this PR
AnandInguva Dec 5, 2023
c27aabb
Add _validate_transform method
AnandInguva Dec 5, 2023
422a86a
add more tests
AnandInguva Dec 5, 2023
08b3665
fix test
AnandInguva Dec 6, 2023
91255ad
Fix test
AnandInguva Dec 6, 2023
c7237c3
Add more tests in sentence-transformer
AnandInguva Dec 6, 2023
a942885
use np.max instead of max
AnandInguva Dec 6, 2023
89c19fb
round to 2 decimals
AnandInguva Dec 6, 2023
2db4a20
Remove gradle command action
AnandInguva Dec 6, 2023
b7a48d5
Refactor throwing dataflow client exception
AnandInguva Dec 6, 2023
eb46e08
Merge branch 'embeddings_mltransform' of https://github.com/AnandIngu…
AnandInguva Dec 6, 2023
bad1b3b
skip the test if gcp is not installed
AnandInguva Dec 6, 2023
b850cee
remove toxTests for hub
AnandInguva Dec 6, 2023
ffff21a
remove toxTests for hub
AnandInguva Dec 6, 2023
88412ea
Fix values in assert for sentence_transformer_test
AnandInguva Dec 7, 2023
617f9d6
rename sentence_transformers to huggingface
AnandInguva Dec 7, 2023
5cae04b
fix pydocs
AnandInguva Dec 7, 2023
489200f
Change the model name for tests since it is getting different results…
AnandInguva Dec 7, 2023
816174a
Fix pydoc in vertexai
AnandInguva Dec 7, 2023
cfb1883
add suffix to artifact_location
AnandInguva Dec 8, 2023
2cb6f03
Revert "add suffix to artifact_location"
AnandInguva Dec 8, 2023
cd7050e
add no_xdist
AnandInguva Dec 8, 2023
98cd949
Try fixing pydoc for vertexai
AnandInguva Dec 8, 2023
8ea0906
change tox.ini to use pytest directly
AnandInguva Dec 8, 2023
5187b0e
Merge remote-tracking branch 'origin/master' into embeddings_mltransform
AnandInguva Dec 8, 2023
6f83d3c
raise FileExistError if Attribute file is already present
AnandInguva Dec 8, 2023
c9ddb25
Merge branch 'embeddings_mltransform' of https://github.com/AnandIngu…
AnandInguva Dec 8, 2023
9dce3cf
modify build.gradle to match tox task names
AnandInguva Dec 8, 2023
539c9ad
Add note to CHANGES.md
AnandInguva Dec 8, 2023
b967cd8
change gcs bucket to gs://temp-storage-for-perf-tests
AnandInguva Dec 8, 2023
f1bb42c
Add TODO GH links
AnandInguva Dec 11, 2023
8d0b47d
Merge remote-tracking branch 'origin/master' into embeddings_mltransform
AnandInguva Dec 11, 2023
c173d6a
Update CHANGES.md
AnandInguva Dec 11, 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
444 changes: 401 additions & 43 deletions sdks/python/apache_beam/ml/transforms/base.py

Large diffs are not rendered by default.

339 changes: 325 additions & 14 deletions sdks/python/apache_beam/ml/transforms/base_test.py

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions sdks/python/apache_beam/ml/transforms/embeddings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# 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.
# TODO: Add dead letter queue for RunInference transforms.

"""
This module contains embedding configs that can be used to generate
embeddings using MLTransform.
"""
131 changes: 131 additions & 0 deletions sdks/python/apache_beam/ml/transforms/embeddings/huggingface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#
# 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.

__all__ = ["SentenceTransformerEmbeddings"]

from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import Mapping
from typing import Optional
from typing import Sequence

import apache_beam as beam
from apache_beam.ml.inference.base import ModelHandler
from apache_beam.ml.inference.base import RunInference
from apache_beam.ml.transforms.base import EmbeddingsManager
from apache_beam.ml.transforms.base import _TextEmbeddingHandler
from sentence_transformers import SentenceTransformer


# TODO: https://github.com/apache/beam/issues/29621
# Use HuggingFaceModelHandlerTensor once the import issue is fixed.
# Right now, the hugging face model handler import torch and tensorflow
# at the same time, which adds too much weigth to the container unnecessarily.
class _SentenceTransformerModelHandler(ModelHandler):
"""
Note: Intended for internal use and guarantees no backwards compatibility.
"""
def __init__(
self,
model_name: str,
model_class: Callable,
load_model_args: Optional[dict] = None,
min_batch_size: Optional[int] = None,
max_batch_size: Optional[int] = None,
max_seq_length: Optional[int] = None,
large_model: bool = False,
**kwargs):
self._max_seq_length = max_seq_length
self.model_name = model_name
self._model_class = model_class
self._load_model_args = load_model_args
self._min_batch_size = min_batch_size
self._max_batch_size = max_batch_size
self._large_model = large_model
self._kwargs = kwargs

def run_inference(
self,
batch: Sequence[str],
model: SentenceTransformer,
inference_args: Optional[Dict[str, Any]] = None,
):
inference_args = inference_args or {}
return model.encode(batch, **inference_args)

def load_model(self):
model = self._model_class(self.model_name, **self._load_model_args)
if self._max_seq_length:
model.max_seq_length = self._max_seq_length
return model

def share_model_across_processes(self) -> bool:
return self._large_model

def batch_elements_kwargs(self) -> Mapping[str, Any]:
batch_sizes = {}
if self._min_batch_size:
batch_sizes["min_batch_size"] = self._min_batch_size
if self._max_batch_size:
batch_sizes["max_batch_size"] = self._max_batch_size
return batch_sizes


class SentenceTransformerEmbeddings(EmbeddingsManager):
def __init__(
self,
model_name: str,
columns: List[str],
max_seq_length: Optional[int] = None,
**kwargs):
"""
Embedding config for sentence-transformers. This config can be used with
MLTransform to embed text data. Models are loaded using the RunInference
PTransform with the help of ModelHandler.

Args:
model_name: Name of the model to use. The model should be hosted on
HuggingFace Hub or compatible with sentence_transformers.
columns: List of columns to be embedded.
max_seq_length: Max sequence length to use for the model if applicable.
min_batch_size: The minimum batch size to be used for inference.
max_batch_size: The maximum batch size to be used for inference.
large_model: Whether to share the model across processes.
"""
super().__init__(columns, **kwargs)
self.model_name = model_name
self.max_seq_length = max_seq_length

def get_model_handler(self):
return _SentenceTransformerModelHandler(
model_class=SentenceTransformer,
max_seq_length=self.max_seq_length,
model_name=self.model_name,
load_model_args=self.load_model_args,
min_batch_size=self.min_batch_size,
max_batch_size=self.max_batch_size,
large_model=self.large_model)

def get_ptransform_for_processing(self, **kwargs) -> beam.PTransform:
# wrap the model handler in a _TextEmbeddingHandler since
# the SentenceTransformerEmbeddings works on text input data.
return (
RunInference(
model_handler=_TextEmbeddingHandler(self),
inference_args=self.inference_args,
))
Loading