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

Conversation

AnandInguva
Copy link
Contributor

@AnandInguva AnandInguva commented Nov 29, 2023

  • Support embeddings in MLTransform using config based structure.

Each config will define its own model handler so that model handler can be compatible with the inputs passed to the MLTransform.
This PR supports vertex_ai, hugging face(sentence-transformers).

Changes made:

  1. Make ProcessHandler a PTransform.
  2. Create _TextEmbeddingHandler which takes a ModelHandler but it is responsible to work on Dict[str, Any] inputs.
  3. Create a helper class that would create and saves the PTransform list required to process data processing steps or embedding configs. MLTransform would be the container that holds the list of transforms and these list of transforms are passed to the _MLTransformToPTransformMapper which maps each data processing transforms to its PTransform
    • For example, tft based transforms are mapped to TFTProcessHandler.
    • Embedding configs are mapped to RunInference
  4. Use jsonpickle to store and load the ptransforms instances. This is done to seal the gap between training and inference. jsonpickle is cross compatible across python versions and has backward compatibility with older versions of its versions.

This PR will throw if input is a Dict[str, List[str]]. Works for Dict[str, str]


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@AnandInguva
Copy link
Contributor Author

R: @damccorm This is ready for review.

Copy link
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control

Copy link
Contributor

@damccorm damccorm left a comment

Choose a reason for hiding this comment

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

Chunking the review a bit. Just reviewed base.py thus far, though I imagine that's the place I'll have the most comments

sdks/python/apache_beam/ml/transforms/base.py Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Outdated Show resolved Hide resolved
@github-actions github-actions bot added the build label Dec 5, 2023
Copy link
Contributor

@damccorm damccorm left a comment

Choose a reason for hiding this comment

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

This mostly looks good, had a few more comments though

sdks/python/apache_beam/ml/transforms/base.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/tft.py Outdated Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/base.py Show resolved Hide resolved
sdks/python/apache_beam/ml/transforms/utils.py Outdated Show resolved Hide resolved
sdks/python/tox.ini Outdated Show resolved Hide resolved
sdks/python/tox.ini Show resolved Hide resolved
raise FileExistsError(
"The artifact location %s already exists and contains %s. Please "
"specify a different location." %
(artifact_location, _ATTRIBUTE_FILE_NAME))
Copy link
Contributor

Choose a reason for hiding this comment

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

Good call - one possible future enhancement would be to support an overwrite argument that allows users to do this

Copy link
Contributor

@damccorm damccorm left a comment

Choose a reason for hiding this comment

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

One last nit, otherwise this LGTM. Feel free to make the change and merge once checks pass

CHANGES.md Outdated Show resolved Hide resolved
Co-authored-by: Danny McCormick <dannymccormick@google.com>
@AnandInguva AnandInguva merged commit aacf1ee into apache:master Dec 11, 2023
78 of 79 checks passed
JayajP pushed a commit to JayajP/beam that referenced this pull request Dec 27, 2023
* Make base.py framework agnostic and add helper transforms

* Add tests for base.py

* Add sentence-transformers

* Add tensorflow hub

* Add vertex_ai

* Make TFTProcessHandler a PTransform

* raise RuntimeError in ArtifactsFetcher when it is used for embeddings

* Add JsonPickle to requirements

* Add tox tests

* Mock frameworks in pydocs

Fix tox.ini

Fix pydoc

Fix indent in pydoc

* Add Row type check

* Remove requires_chaining

* change name of PTransformProvider to MLTransformProvider

* remove batch_len in utility fun

* Change type annotation and redundant comments

* Remove get_transforms method

* remove requires_chaining from tft

* add tests to sentence-transformers

* Pass inference_args to RunInference

* Add TODO GH issue

* refactor variables in vertex_ai embeddings

* remove try/catch and throw error if options is empty for GCS artifact location

* Refactor NotImplementedError message

* remove tensorflow hub from this PR

* Add _validate_transform  method

* add more tests

* fix test

* Fix test

* Add more tests in sentence-transformer

* use np.max instead of max

* round to 2 decimals

* Remove gradle command action

* Refactor throwing dataflow client exception

* skip the test if gcp is not installed

* remove toxTests for hub

* remove toxTests for hub

* Fix values in assert for sentence_transformer_test

* rename sentence_transformers to huggingface

* fix pydocs

* Change the model name for tests since it is getting different results on different machines

* Fix pydoc in vertexai

* add suffix to artifact_location

* Revert "add suffix to artifact_location"

This reverts commit cfb1883.

* add no_xdist

* Try fixing pydoc for vertexai

* change tox.ini to use pytest directly

* raise FileExistError if Attribute file is already present

* modify build.gradle to match tox task names

* Add note to CHANGES.md

* change gcs bucket to gs://temp-storage-for-perf-tests

* Add TODO GH links

* Update CHANGES.md

Co-authored-by: Danny McCormick <dannymccormick@google.com>

---------

Co-authored-by: Danny McCormick <dannymccormick@google.com>
Naireen pushed a commit to Naireen/beam that referenced this pull request Jan 3, 2024
* Make base.py framework agnostic and add helper transforms

* Add tests for base.py

* Add sentence-transformers

* Add tensorflow hub

* Add vertex_ai

* Make TFTProcessHandler a PTransform

* raise RuntimeError in ArtifactsFetcher when it is used for embeddings

* Add JsonPickle to requirements

* Add tox tests

* Mock frameworks in pydocs

Fix tox.ini

Fix pydoc

Fix indent in pydoc

* Add Row type check

* Remove requires_chaining

* change name of PTransformProvider to MLTransformProvider

* remove batch_len in utility fun

* Change type annotation and redundant comments

* Remove get_transforms method

* remove requires_chaining from tft

* add tests to sentence-transformers

* Pass inference_args to RunInference

* Add TODO GH issue

* refactor variables in vertex_ai embeddings

* remove try/catch and throw error if options is empty for GCS artifact location

* Refactor NotImplementedError message

* remove tensorflow hub from this PR

* Add _validate_transform  method

* add more tests

* fix test

* Fix test

* Add more tests in sentence-transformer

* use np.max instead of max

* round to 2 decimals

* Remove gradle command action

* Refactor throwing dataflow client exception

* skip the test if gcp is not installed

* remove toxTests for hub

* remove toxTests for hub

* Fix values in assert for sentence_transformer_test

* rename sentence_transformers to huggingface

* fix pydocs

* Change the model name for tests since it is getting different results on different machines

* Fix pydoc in vertexai

* add suffix to artifact_location

* Revert "add suffix to artifact_location"

This reverts commit cfb1883.

* add no_xdist

* Try fixing pydoc for vertexai

* change tox.ini to use pytest directly

* raise FileExistError if Attribute file is already present

* modify build.gradle to match tox task names

* Add note to CHANGES.md

* change gcs bucket to gs://temp-storage-for-perf-tests

* Add TODO GH links

* Update CHANGES.md

Co-authored-by: Danny McCormick <dannymccormick@google.com>

---------

Co-authored-by: Danny McCormick <dannymccormick@google.com>
@AnandInguva AnandInguva linked an issue Jan 5, 2024 that may be closed by this pull request
16 tasks
@AnandInguva AnandInguva linked an issue Jan 5, 2024 that may be closed by this pull request
16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Task]: Support embeddings using ML models in MLTransform
2 participants