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

feat: Support uploading local models #779

Merged

Conversation

Ark-kun
Copy link
Contributor

@Ark-kun Ark-kun commented Oct 19, 2021

Added framework-specific model uploaders for XGBoost, Scikit-learn and Tensorflow

This PR adds framework-specific upload functions to the Model class:

  • upload_scikit_learn_model_file
  • upload_xgboost_model_file
  • upload_tensorflow_saved_model

Here is what these functions do for the user:

  • Validate model file
  • Validate framework version or choose default one
  • Construct model display_name
  • Create directory for the model (every uploaded Vertex model must be a directory)
  • Rename the model file (Vertex AI only supports a small strict list of file names)
  • Create staging GCS bucket if needed
  • Upload the model directory to a unique location in the staging GCS bucket
  • Construct serving container URI based on framework, version, CPU vs. GPU, region etc
  • Upload the model

This PR also augments the Model.upload function such that when artifact_uri points to a local model file or directory, the model is automatically staged in Google Cloud Storage as required by the Model.upload API. The Model.upload function now also checks the model files for correctness.

This change does not add any new dependencies.

Example usage:

model = aiplatform.Model.upload_scikit_learn_model_file("iris.sklearn_model.joblib")
model = aiplatform.Model.upload_xgboost_model_file("iris.xgboost_model.bin")
model = aiplatform.Model.upload_tensorflow_saved_model("iris.tensorflow_model.SavedModel")

Prediction example:

model = aiplatform.Model.upload_scikit_learn_model_file("iris.sklearn_model.pkl")
endpoint = model.deploy(machine_type="n1-standard-2")
endpoint.predict(instances=[[1, 2, 3, 4]])

Output:

INFO:google.cloud.aiplatform.models:Using the 0.24 version of Scikit-learn.
INFO:google.cloud.aiplatform.utils:Uploading "/tmp/tmpagz_4y34" to "gs://avolkov-31337-staging/vertex_ai_auto_staging/2021-10-18-19:10:36.387"
INFO:google.cloud.aiplatform.models:Creating Model
INFO:google.cloud.aiplatform.models:Create Model backing LRO: projects/140626129697/locations/us-central1/models/9131602398354079744/operations/3090562396373123072
INFO:google.cloud.aiplatform.models:Model created. Resource name: projects/140626129697/locations/us-central1/models/9131602398354079744
INFO:google.cloud.aiplatform.models:To use this Model in another session:
INFO:google.cloud.aiplatform.models:model = aiplatform.Model('projects/140626129697/locations/us-central1/models/9131602398354079744')
INFO:google.cloud.aiplatform.models:Creating Endpoint
INFO:google.cloud.aiplatform.models:Create Endpoint backing LRO: projects/140626129697/locations/us-central1/endpoints/6185702884286398464/operations/802733785668911104
INFO:google.cloud.aiplatform.models:Endpoint created. Resource name: projects/140626129697/locations/us-central1/endpoints/6185702884286398464
INFO:google.cloud.aiplatform.models:To use this Endpoint in another session:
INFO:google.cloud.aiplatform.models:endpoint = aiplatform.Endpoint('projects/140626129697/locations/us-central1/endpoints/6185702884286398464')
INFO:google.cloud.aiplatform.models:Deploying model to Endpoint : projects/140626129697/locations/us-central1/endpoints/6185702884286398464
INFO:google.cloud.aiplatform.models:Deploy Endpoint model backing LRO: projects/140626129697/locations/us-central1/endpoints/6185702884286398464/operations/8087869132894109696
INFO:google.cloud.aiplatform.models:Endpoint model deployed. Resource name: projects/140626129697/locations/us-central1/endpoints/6185702884286398464
Prediction(predictions=[2.0], deployed_model_id='2568679064810291200', explanations=None)

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

@product-auto-label product-auto-label bot added the api: aiplatform Issues related to the AI Platform API. label Oct 19, 2021
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Oct 19, 2021
@Ark-kun Ark-kun force-pushed the feat--Support-uploading-local-models branch 2 times, most recently from 354f23e to d346c1d Compare October 19, 2021 21:21
@Ark-kun Ark-kun force-pushed the feat--Support-uploading-local-models branch from bfec598 to 8f61efb Compare October 20, 2021 18:53
Copy link
Member

@sasha-gitg sasha-gitg left a comment

Choose a reason for hiding this comment

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

Please add unit tests.

Test examples with mock Model service: https://github.com/googleapis/python-aiplatform/blob/main/tests/unit/aiplatform/test_models.py#L500

Testing with mock GCS: https://github.com/googleapis/python-aiplatform/blob/main/tests/unit/aiplatform/test_training_jobs.py#L311

Would also prefer to have one integration test as these methods use both GCS and Vertex. Here's an example: https://github.com/googleapis/python-aiplatform/blob/main/tests/system/aiplatform/test_e2e_tabular.py#L50

Let me know if you have any questions.

Thanks for this PR, Alexey!

google/cloud/aiplatform/models.py Show resolved Hide resolved
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/models.py Show resolved Hide resolved
google/cloud/aiplatform/models.py Show resolved Hide resolved
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/utils/__init__.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/utils/__init__.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/utils/__init__.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
@nicain nicain added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label Nov 9, 2021
TemporaryDirectory returns object with `.name` when called directly, but returns a path string when used as a context.
The buckets that we create are regional.
This prevents errors when some service required regional bucket.
E.g. "FailedPrecondition: 400 The Cloud Storage bucket of `gs://...` is in location `us`. It must be in the same regional location as the service location `us-central1`."
We are making the bucket name region-specific since the bucket is regional.
We cannot clean up the directory immediately after calling Model.upload since that call may be asynchronous and return before the model file has been read. The temporary data will be automatically cleaned up by the system later.
@Ark-kun
Copy link
Contributor Author

Ark-kun commented Nov 16, 2021

Please add unit tests.

Test examples with mock Model service: https://github.com/googleapis/python-aiplatform/blob/main/tests/unit/aiplatform/test_models.py#L500

Testing with mock GCS: https://github.com/googleapis/python-aiplatform/blob/main/tests/unit/aiplatform/test_training_jobs.py#L311

Thank you for the great pointers for the tests.

I've added unit tests for the new Model.upload_* methods and also for the expanded staging capabilities of the Model.upload method.

Please take a look.

@Ark-kun Ark-kun force-pushed the feat--Support-uploading-local-models branch from 52fb340 to 73a4868 Compare November 24, 2021 07:28
@Ark-kun Ark-kun force-pushed the feat--Support-uploading-local-models branch 2 times, most recently from b466cb2 to 9138036 Compare November 30, 2021 04:59
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/models.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/utils/gcs_utils.py Outdated Show resolved Hide resolved
google/cloud/aiplatform/utils/gcs_utils.py Show resolved Hide resolved
tests/system/aiplatform/test_model_upload.py Outdated Show resolved Hide resolved
@sasha-gitg sasha-gitg merged commit bffbd9d into googleapis:main Dec 1, 2021
Ark-kun added a commit to Ark-kun/pipeline_components that referenced this pull request Aug 1, 2022
Ark-kun added a commit to Ark-kun/pipeline_components that referenced this pull request Aug 1, 2022
Ark-kun added a commit to Ark-kun/pipeline_components that referenced this pull request Aug 1, 2022
Ark-kun added a commit to Ark-kun/pipeline_components that referenced this pull request Aug 1, 2022
Ark-kun added a commit to Ark-kun/pipeline_components that referenced this pull request Aug 1, 2022
Ark-kun added a commit to Ark-kun/pipeline_components that referenced this pull request Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: aiplatform Issues related to the AI Platform API. cla: yes This human has signed the Contributor License Agreement. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants