GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: Added optional model args for custom training (#129) · googleapis/python-aiplatform@e45ebfc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • google/cloud/aiplatform
    • models.py
    • training_jobs.py
  • tests/unit/aiplatform
    • test_models.py
    • test_training_jobs.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎google/cloud/aiplatform/models.py‎

Copy file name to clipboardExpand all lines: google/cloud/aiplatform/models.py
+9-5Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,16 +1106,20 @@ def upload(
11061106
health_route=serving_container_health_route,
11071107
)
11081108

1109+
model_predict_schemata = None
1110+
if any([instance_schema_uri, parameters_schema_uri, prediction_schema_uri]):
1111+
model_predict_schemata = gca_model.PredictSchemata(
1112+
instance_schema_uri=instance_schema_uri,
1113+
parameters_schema_uri=parameters_schema_uri,
1114+
prediction_schema_uri=prediction_schema_uri,
1115+
)
1116+
11091117
managed_model = gca_model.Model(
11101118
display_name=display_name,
11111119
description=description,
11121120
artifact_uri=artifact_uri,
11131121
container_spec=container_spec,
1114-
predict_schemata=gca_model.PredictSchemata(
1115-
instance_schema_uri=instance_schema_uri,
1116-
parameters_schema_uri=parameters_schema_uri,
1117-
prediction_schema_uri=prediction_schema_uri,
1118-
),
1122+
predict_schemata=model_predict_schemata,
11191123
)
11201124

11211125
lro = api_client.upload_model(
Collapse file

‎google/cloud/aiplatform/training_jobs.py‎

Copy file name to clipboardExpand all lines: google/cloud/aiplatform/training_jobs.py
+137-23Lines changed: 137 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from google.cloud.aiplatform_v1beta1.services.pipeline_service import (
3939
client as pipeline_service_client,
4040
)
41+
from google.cloud.aiplatform_v1beta1.types import env_var
4142
from google.cloud.aiplatform_v1beta1.types import (
4243
accelerator_type as gca_accelerator_type,
4344
)
@@ -923,7 +924,6 @@ class CustomTrainingJob(_TrainingJob):
923924
in Cloud AI Platform Training.
924925
"""
925926

926-
# TODO(b/172365796) add remainder of model optional arguments
927927
def __init__(
928928
self,
929929
display_name: str,
@@ -933,6 +933,14 @@ def __init__(
933933
model_serving_container_image_uri: Optional[str] = None,
934934
model_serving_container_predict_route: Optional[str] = None,
935935
model_serving_container_health_route: Optional[str] = None,
936+
model_serving_container_command: Optional[Sequence[str]] = None,
937+
model_serving_container_args: Optional[Sequence[str]] = None,
938+
model_serving_container_environment_variables: Optional[Dict[str, str]] = None,
939+
model_serving_container_ports: Optional[Sequence[int]] = None,
940+
model_description: Optional[str] = None,
941+
model_instance_schema_uri: Optional[str] = None,
942+
model_parameters_schema_uri: Optional[str] = None,
943+
model_prediction_schema_uri: Optional[str] = None,
936944
project: Optional[str] = None,
937945
location: Optional[str] = None,
938946
credentials: Optional[auth_credentials.Credentials] = None,
@@ -978,7 +986,7 @@ def __init__(
978986
If the training produces a managed AI Platform Model, the URI of the
979987
Model serving container suitable for serving the model produced by the
980988
training script.
981-
model_serving_container_predict_route (str):.
989+
model_serving_container_predict_route (str):
982990
If the training produces a managed AI Platform Model, An HTTP path to
983991
send prediction requests to the container, and which must be supported
984992
by it. If not specified a default HTTP path will be used by AI Platform.
@@ -987,6 +995,80 @@ def __init__(
987995
send health check requests to the container, and which must be supported
988996
by it. If not specified a standard HTTP path will be used by AI
989997
Platform.
998+
model_serving_container_command (Sequence[str]):
999+
The command with which the container is run. Not executed within a
1000+
shell. The Docker image's ENTRYPOINT is used if this is not provided.
1001+
Variable references $(VAR_NAME) are expanded using the container's
1002+
environment. If a variable cannot be resolved, the reference in the
1003+
input string will be unchanged. The $(VAR_NAME) syntax can be escaped
1004+
with a double $$, ie: $$(VAR_NAME). Escaped references will never be
1005+
expanded, regardless of whether the variable exists or not.
1006+
model_serving_container_args (Sequence[str]):
1007+
The arguments to the command. The Docker image's CMD is used if this is
1008+
not provided. Variable references $(VAR_NAME) are expanded using the
1009+
container's environment. If a variable cannot be resolved, the reference
1010+
in the input string will be unchanged. The $(VAR_NAME) syntax can be
1011+
escaped with a double $$, ie: $$(VAR_NAME). Escaped references will
1012+
never be expanded, regardless of whether the variable exists or not.
1013+
model_serving_container_environment_variables (Dict[str, str]):
1014+
The environment variables that are to be present in the container.
1015+
Should be a dictionary where keys are environment variable names
1016+
and values are environment variable values for those names.
1017+
model_serving_container_ports (Sequence[int]):
1018+
Declaration of ports that are exposed by the container. This field is
1019+
primarily informational, it gives AI Platform information about the
1020+
network connections the container uses. Listing or not a port here has
1021+
no impact on whether the port is actually exposed, any port listening on
1022+
the default "0.0.0.0" address inside a container will be accessible from
1023+
the network.
1024+
model_description (str):
1025+
The description of the Model.
1026+
model_instance_schema_uri (str):
1027+
Optional. Points to a YAML file stored on Google Cloud
1028+
Storage describing the format of a single instance, which
1029+
are used in
1030+
``PredictRequest.instances``,
1031+
``ExplainRequest.instances``
1032+
and
1033+
``BatchPredictionJob.input_config``.
1034+
The schema is defined as an OpenAPI 3.0.2 `Schema
1035+
Object <https://tinyurl.com/y538mdwt#schema-object>`__.
1036+
AutoML Models always have this field populated by AI
1037+
Platform. Note: The URI given on output will be immutable
1038+
and probably different, including the URI scheme, than the
1039+
one given on input. The output URI will point to a location
1040+
where the user only has a read access.
1041+
model_parameters_schema_uri (str):
1042+
Optional. Points to a YAML file stored on Google Cloud
1043+
Storage describing the parameters of prediction and
1044+
explanation via
1045+
``PredictRequest.parameters``,
1046+
``ExplainRequest.parameters``
1047+
and
1048+
``BatchPredictionJob.model_parameters``.
1049+
The schema is defined as an OpenAPI 3.0.2 `Schema
1050+
Object <https://tinyurl.com/y538mdwt#schema-object>`__.
1051+
AutoML Models always have this field populated by AI
1052+
Platform, if no parameters are supported it is set to an
1053+
empty string. Note: The URI given on output will be
1054+
immutable and probably different, including the URI scheme,
1055+
than the one given on input. The output URI will point to a
1056+
location where the user only has a read access.
1057+
model_prediction_schema_uri (str):
1058+
Optional. Points to a YAML file stored on Google Cloud
1059+
Storage describing the format of a single prediction
1060+
produced by this Model, which are returned via
1061+
``PredictResponse.predictions``,
1062+
``ExplainResponse.explanations``,
1063+
and
1064+
``BatchPredictionJob.output_config``.
1065+
The schema is defined as an OpenAPI 3.0.2 `Schema
1066+
Object <https://tinyurl.com/y538mdwt#schema-object>`__.
1067+
AutoML Models always have this field populated by AI
1068+
Platform. Note: The URI given on output will be immutable
1069+
and probably different, including the URI scheme, than the
1070+
one given on input. The output URI will point to a location
1071+
where the user only has a read access.
9901072
project (str):
9911073
Project to run training in. Overrides project set in aiplatform.init.
9921074
location (str):
@@ -1007,12 +1089,52 @@ def __init__(
10071089

10081090
self._container_uri = container_uri
10091091
self._requirements = requirements
1010-
self._model_serving_container_image_uri = model_serving_container_image_uri
1011-
self._model_serving_container_predict_route = (
1012-
model_serving_container_predict_route
1092+
1093+
model_predict_schemata = None
1094+
if any(
1095+
[
1096+
model_instance_schema_uri,
1097+
model_parameters_schema_uri,
1098+
model_prediction_schema_uri,
1099+
]
1100+
):
1101+
model_predict_schemata = gca_model.PredictSchemata(
1102+
instance_schema_uri=model_instance_schema_uri,
1103+
parameters_schema_uri=model_parameters_schema_uri,
1104+
prediction_schema_uri=model_prediction_schema_uri,
1105+
)
1106+
1107+
# Create the container spec
1108+
env = None
1109+
ports = None
1110+
1111+
if model_serving_container_environment_variables:
1112+
env = [
1113+
env_var.EnvVar(name=str(key), value=str(value))
1114+
for key, value in model_serving_container_environment_variables.items()
1115+
]
1116+
1117+
if model_serving_container_ports:
1118+
ports = [
1119+
gca_model.Port(container_port=port)
1120+
for port in model_serving_container_ports
1121+
]
1122+
1123+
container_spec = gca_model.ModelContainerSpec(
1124+
image_uri=model_serving_container_image_uri,
1125+
command=model_serving_container_command,
1126+
args=model_serving_container_args,
1127+
env=env,
1128+
ports=ports,
1129+
predict_route=model_serving_container_predict_route,
1130+
health_route=model_serving_container_health_route,
10131131
)
1014-
self._model_serving_container_health_route = (
1015-
model_serving_container_health_route
1132+
1133+
# create model payload
1134+
self._managed_model = gca_model.Model(
1135+
description=model_description,
1136+
predict_schemata=model_predict_schemata,
1137+
container_spec=container_spec,
10161138
)
10171139

10181140
self._script_path = script_path
@@ -1132,7 +1254,7 @@ def run(
11321254
raise RuntimeError("Custom Training has already run.")
11331255

11341256
# if args needed for model is incomplete
1135-
if model_display_name and not self._model_serving_container_image_uri:
1257+
if model_display_name and not self._managed_model.container_spec.image_uri:
11361258
raise RuntimeError(
11371259
"""model_display_name was provided but
11381260
model_serving_container_image_uri was not provided when this
@@ -1148,26 +1270,18 @@ def run(
11481270
accelerator_type=accelerator_type,
11491271
).pool_specs
11501272

1151-
# create model payload
1152-
managed_model = None
1153-
if model_display_name:
1154-
utils.validate_display_name(model_display_name)
1155-
1156-
container_spec = gca_model.ModelContainerSpec(
1157-
image_uri=self._model_serving_container_image_uri,
1158-
predict_route=self._model_serving_container_predict_route,
1159-
health_route=self._model_serving_container_health_route,
1160-
)
1161-
1162-
managed_model = gca_model.Model(
1163-
display_name=model_display_name, container_spec=container_spec
1164-
)
1165-
11661273
# make and copy package
11671274
python_packager = _TrainingScriptPythonPackager(
11681275
script_path=self._script_path, requirements=self._requirements
11691276
)
11701277

1278+
managed_model = self._managed_model
1279+
if model_display_name:
1280+
utils.validate_display_name(model_display_name)
1281+
managed_model.display_name = model_display_name
1282+
else:
1283+
managed_model = None
1284+
11711285
return self._run(
11721286
python_packager=python_packager,
11731287
dataset=dataset,
Collapse file

‎tests/unit/aiplatform/test_models.py‎

Copy file name to clipboardExpand all lines: tests/unit/aiplatform/test_models.py
-15Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,6 @@ def test_upload_uploads_and_gets_model(self, sync):
293293
display_name=_TEST_MODEL_NAME,
294294
artifact_uri=_TEST_ARTIFACT_URI,
295295
container_spec=container_spec,
296-
predict_schemata=gca_model.PredictSchemata(
297-
instance_schema_uri=None,
298-
parameters_schema_uri=None,
299-
prediction_schema_uri=None,
300-
),
301296
)
302297

303298
api_client_mock.upload_model.assert_called_once_with(
@@ -429,11 +424,6 @@ def test_upload_uploads_and_gets_model_with_custom_project(self, sync):
429424
display_name=_TEST_MODEL_NAME,
430425
artifact_uri=_TEST_ARTIFACT_URI,
431426
container_spec=container_spec,
432-
predict_schemata=gca_model.PredictSchemata(
433-
instance_schema_uri=None,
434-
parameters_schema_uri=None,
435-
prediction_schema_uri=None,
436-
),
437427
)
438428

439429
api_client_mock.upload_model.assert_called_once_with(
@@ -485,11 +475,6 @@ def test_upload_uploads_and_gets_model_with_custom_location(self, sync):
485475
display_name=_TEST_MODEL_NAME,
486476
artifact_uri=_TEST_ARTIFACT_URI,
487477
container_spec=container_spec,
488-
predict_schemata=gca_model.PredictSchemata(
489-
instance_schema_uri=None,
490-
parameters_schema_uri=None,
491-
prediction_schema_uri=None,
492-
),
493478
)
494479

495480
api_client_mock.upload_model.assert_called_once_with(
Collapse file

‎tests/unit/aiplatform/test_training_jobs.py‎

Copy file name to clipboardExpand all lines: tests/unit/aiplatform/test_training_jobs.py
+54-2Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
client as pipeline_service_client,
4141
)
4242
from google.cloud.aiplatform_v1beta1.types import io as gca_io
43+
from google.cloud.aiplatform_v1beta1.types import env_var
4344
from google.cloud.aiplatform_v1beta1.types import model as gca_model
4445
from google.cloud.aiplatform_v1beta1.types import pipeline_state as gca_pipeline_state
4546
from google.cloud.aiplatform_v1beta1.types import (
@@ -87,9 +88,22 @@
8788
_TEST_TEST_FRACTION_SPLIT = 0.2
8889
_TEST_PREDEFINED_SPLIT_COLUMN_NAME = "split"
8990

91+
_TEST_MODEL_INSTANCE_SCHEMA_URI = "instance_schema_uri.yaml"
92+
_TEST_MODEL_PARAMETERS_SCHEMA_URI = "parameters_schema_uri.yaml"
93+
_TEST_MODEL_PREDICTION_SCHEMA_URI = "prediction_schema_uri.yaml"
94+
_TEST_MODEL_SERVING_CONTAINER_COMMAND = ["test_command"]
95+
_TEST_MODEL_SERVING_CONTAINER_ARGS = ["test_args"]
96+
_TEST_MODEL_SERVING_CONTAINER_ENVIRONMENT_VARIABLES = {
97+
"learning_rate": 0.01,
98+
"loss_fn": "mse",
99+
}
100+
_TEST_MODEL_SERVING_CONTAINER_PORTS = [8888, 10000]
101+
_TEST_MODEL_DESCRIPTION = "test description"
102+
90103
_TEST_OUTPUT_PYTHON_PACKAGE_PATH = "gs://test/ouput/python/trainer.tar.gz"
91104

92105
_TEST_MODEL_NAME = "projects/my-project/locations/us-central1/models/12345"
106+
_TEST_MODEL_LABELS = {"label_key": "label_value"}
93107

94108
_TEST_PIPELINE_RESOURCE_NAME = (
95109
"projects/my-project/locations/us-central1/trainingPipeline/12345"
@@ -426,6 +440,14 @@ def test_run_call_pipeline_service_create(
426440
model_serving_container_image_uri=_TEST_SERVING_CONTAINER_IMAGE,
427441
model_serving_container_predict_route=_TEST_SERVING_CONTAINER_PREDICTION_ROUTE,
428442
model_serving_container_health_route=_TEST_SERVING_CONTAINER_HEALTH_ROUTE,
443+
model_instance_schema_uri=_TEST_MODEL_INSTANCE_SCHEMA_URI,
444+
model_parameters_schema_uri=_TEST_MODEL_PARAMETERS_SCHEMA_URI,
445+
model_prediction_schema_uri=_TEST_MODEL_PREDICTION_SCHEMA_URI,
446+
model_serving_container_command=_TEST_MODEL_SERVING_CONTAINER_COMMAND,
447+
model_serving_container_args=_TEST_MODEL_SERVING_CONTAINER_ARGS,
448+
model_serving_container_environment_variables=_TEST_MODEL_SERVING_CONTAINER_ENVIRONMENT_VARIABLES,
449+
model_serving_container_ports=_TEST_MODEL_SERVING_CONTAINER_PORTS,
450+
model_description=_TEST_MODEL_DESCRIPTION,
429451
)
430452

431453
model_from_job = job.run(
@@ -476,14 +498,35 @@ def test_run_call_pipeline_service_create(
476498
test_fraction=_TEST_TEST_FRACTION_SPLIT,
477499
)
478500

501+
env = [
502+
env_var.EnvVar(name=str(key), value=str(value))
503+
for key, value in _TEST_MODEL_SERVING_CONTAINER_ENVIRONMENT_VARIABLES.items()
504+
]
505+
506+
ports = [
507+
gca_model.Port(container_port=port)
508+
for port in _TEST_MODEL_SERVING_CONTAINER_PORTS
509+
]
510+
479511
true_container_spec = gca_model.ModelContainerSpec(
480512
image_uri=_TEST_SERVING_CONTAINER_IMAGE,
481513
predict_route=_TEST_SERVING_CONTAINER_PREDICTION_ROUTE,
482514
health_route=_TEST_SERVING_CONTAINER_HEALTH_ROUTE,
515+
command=_TEST_MODEL_SERVING_CONTAINER_COMMAND,
516+
args=_TEST_MODEL_SERVING_CONTAINER_ARGS,
517+
env=env,
518+
ports=ports,
483519
)
484520

485521
true_managed_model = gca_model.Model(
486-
display_name=_TEST_MODEL_DISPLAY_NAME, container_spec=true_container_spec
522+
display_name=_TEST_MODEL_DISPLAY_NAME,
523+
description=_TEST_MODEL_DESCRIPTION,
524+
container_spec=true_container_spec,
525+
predict_schemata=gca_model.PredictSchemata(
526+
instance_schema_uri=_TEST_MODEL_INSTANCE_SCHEMA_URI,
527+
parameters_schema_uri=_TEST_MODEL_PARAMETERS_SCHEMA_URI,
528+
prediction_schema_uri=_TEST_MODEL_PREDICTION_SCHEMA_URI,
529+
),
487530
)
488531

489532
true_input_data_config = gca_training_pipeline.InputDataConfig(
@@ -899,6 +942,9 @@ def test_run_call_pipeline_service_create_distributed_training(
899942
model_serving_container_image_uri=_TEST_SERVING_CONTAINER_IMAGE,
900943
model_serving_container_predict_route=_TEST_SERVING_CONTAINER_PREDICTION_ROUTE,
901944
model_serving_container_health_route=_TEST_SERVING_CONTAINER_HEALTH_ROUTE,
945+
model_instance_schema_uri=_TEST_MODEL_INSTANCE_SCHEMA_URI,
946+
model_parameters_schema_uri=_TEST_MODEL_PARAMETERS_SCHEMA_URI,
947+
model_prediction_schema_uri=_TEST_MODEL_PREDICTION_SCHEMA_URI,
902948
)
903949

904950
model_from_job = job.run(
@@ -971,7 +1017,13 @@ def test_run_call_pipeline_service_create_distributed_training(
9711017
)
9721018

9731019
true_managed_model = gca_model.Model(
974-
display_name=_TEST_MODEL_DISPLAY_NAME, container_spec=true_container_spec
1020+
display_name=_TEST_MODEL_DISPLAY_NAME,
1021+
container_spec=true_container_spec,
1022+
predict_schemata=gca_model.PredictSchemata(
1023+
instance_schema_uri=_TEST_MODEL_INSTANCE_SCHEMA_URI,
1024+
parameters_schema_uri=_TEST_MODEL_PARAMETERS_SCHEMA_URI,
1025+
prediction_schema_uri=_TEST_MODEL_PREDICTION_SCHEMA_URI,
1026+
),
9751027
)
9761028

9771029
true_input_data_config = gca_training_pipeline.InputDataConfig(

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.