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 1 commit
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
26 changes: 5 additions & 21 deletions sdks/python/apache_beam/ml/transforms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,17 @@ class PTransformProvider:
Data processing transforms that are intended to be used with MLTransform
should subclass PTransformProvider and implement the following methods:
1. get_ptransform_for_processing()
2. requires_chaining()

get_ptransform_for_processing() method should return a PTransform that can be
used to process the data.

requires_chaining() method should return True if the data processing
transforms needs to be chained sequentially with compatible data processing
transforms.
"""
@abc.abstractmethod
def get_ptransform_for_processing(self, **kwargs) -> beam.PTransform:
"""
Returns a PTransform that can be used to process the data.
"""

@abc.abstractmethod
def requires_chaining(self):
"""
Returns True if the data processing transforms needs to be chained
sequentially with compatible data processing transforms.
"""

def get_counter(self):
"""
Returns the counter name for the data processing transform.
Expand Down Expand Up @@ -196,10 +185,6 @@ def get_model_handler(self) -> ModelHandler:
Return framework specific model handler.
"""

def requires_chaining(self):
# each embedding config requires a separate PTransform. so no chaining.
return False

def get_columns_to_apply(self):
return self.columns

Expand Down Expand Up @@ -495,15 +480,14 @@ def create_ptransform_list(self):
artifact_location=os.path.join(
self._parent_artifact_location, uuid.uuid4().hex[:6]),
artifact_mode=self.artifact_mode)
# Determine if a new ptransform should be added to the list
is_different_type = (type(current_ptransform) != previous_ptransform_type)
if is_different_type or not transform.requires_chaining():
append_transform = hasattr(current_ptransform, 'append_transform')
if (type(current_ptransform) != previous_ptransform_type) or not append_transform:
ptransform_list.append(current_ptransform)
previous_ptransform_type = type(current_ptransform)

if hasattr(ptransform_list[-1], 'append_transform'):
# If different PTransform is appended to the list and the PTransform
# supports append_transform, append the transform to the PTransform.
if append_transform:
ptransform_list[-1].append_transform(transform)
damccorm marked this conversation as resolved.
Show resolved Hide resolved

return ptransform_list

def save_transforms_in_artifact_location(self, ptransform_list):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,4 @@ def get_model_handler(self):
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)))

def requires_chaining(self):
return False
return (RunInference(model_handler=_TextEmbeddingHandler(self)))
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, preprocessing_url: Optional[str], *args, **kwargs):
def load_model(self):
# unable to load the models with tf.keras.models.load_model so
damccorm marked this conversation as resolved.
Show resolved Hide resolved
# using hub.KerasLayer instead
model = hub.KerasLayer(self._model_uri)
model = hub.KerasLayer(self._model_uri, )
return model

def _convert_prediction_result_to_list(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import apache_beam as beam
from apache_beam.ml.transforms.base import MLTransform

hub_url = 'https://tfhub.dev/google/LEALLA/LEALLA-small/1'
damccorm marked this conversation as resolved.
Show resolved Hide resolved
hub_url = 'https://tfhub.dev/google/nnlm-en-dim128/2'
test_query_column = 'test_query'
test_query = 'This is a test query'

Expand Down Expand Up @@ -134,12 +134,12 @@ def test_embeddings_with_read_artifact_location(self):
pipeline=data, read_artifact_location=self.artifact_location)

def assert_element(element):
assert round(element, 2) == 0.21
# 0.29836970567703247
assert round(element, 2) == 0.3

_ = (
result_pcoll
| beam.Map(lambda x: max(x[test_query_column]))
# 0.14797046780586243
| beam.Map(assert_element))

def test_with_int_data_types(self):
Expand Down Expand Up @@ -185,12 +185,12 @@ def test_with_gcs_artifact_location(self):
pipeline=data, read_artifact_location=artifact_location)

def assert_element(element):
assert round(element, 2) == 0.21
# 0.29836970567703247
assert round(element, 2) == 0.3

_ = (
result_pcoll
| beam.Map(lambda x: max(x[test_query_column]))
# 0.14797046780586243
| beam.Map(assert_element))


Expand Down
Loading