|
38 | 38 | from google.cloud.aiplatform_v1beta1.services.pipeline_service import ( |
39 | 39 | client as pipeline_service_client, |
40 | 40 | ) |
| 41 | +from google.cloud.aiplatform_v1beta1.types import env_var |
41 | 42 | from google.cloud.aiplatform_v1beta1.types import ( |
42 | 43 | accelerator_type as gca_accelerator_type, |
43 | 44 | ) |
@@ -923,7 +924,6 @@ class CustomTrainingJob(_TrainingJob): |
923 | 924 | in Cloud AI Platform Training. |
924 | 925 | """ |
925 | 926 |
|
926 | | - # TODO(b/172365796) add remainder of model optional arguments |
927 | 927 | def __init__( |
928 | 928 | self, |
929 | 929 | display_name: str, |
@@ -933,6 +933,14 @@ def __init__( |
933 | 933 | model_serving_container_image_uri: Optional[str] = None, |
934 | 934 | model_serving_container_predict_route: Optional[str] = None, |
935 | 935 | 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, |
936 | 944 | project: Optional[str] = None, |
937 | 945 | location: Optional[str] = None, |
938 | 946 | credentials: Optional[auth_credentials.Credentials] = None, |
@@ -978,7 +986,7 @@ def __init__( |
978 | 986 | If the training produces a managed AI Platform Model, the URI of the |
979 | 987 | Model serving container suitable for serving the model produced by the |
980 | 988 | training script. |
981 | | - model_serving_container_predict_route (str):. |
| 989 | + model_serving_container_predict_route (str): |
982 | 990 | If the training produces a managed AI Platform Model, An HTTP path to |
983 | 991 | send prediction requests to the container, and which must be supported |
984 | 992 | by it. If not specified a default HTTP path will be used by AI Platform. |
@@ -987,6 +995,80 @@ def __init__( |
987 | 995 | send health check requests to the container, and which must be supported |
988 | 996 | by it. If not specified a standard HTTP path will be used by AI |
989 | 997 | 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. |
990 | 1072 | project (str): |
991 | 1073 | Project to run training in. Overrides project set in aiplatform.init. |
992 | 1074 | location (str): |
@@ -1007,12 +1089,52 @@ def __init__( |
1007 | 1089 |
|
1008 | 1090 | self._container_uri = container_uri |
1009 | 1091 | 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, |
1013 | 1131 | ) |
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, |
1016 | 1138 | ) |
1017 | 1139 |
|
1018 | 1140 | self._script_path = script_path |
@@ -1132,7 +1254,7 @@ def run( |
1132 | 1254 | raise RuntimeError("Custom Training has already run.") |
1133 | 1255 |
|
1134 | 1256 | # 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: |
1136 | 1258 | raise RuntimeError( |
1137 | 1259 | """model_display_name was provided but |
1138 | 1260 | model_serving_container_image_uri was not provided when this |
@@ -1148,26 +1270,18 @@ def run( |
1148 | 1270 | accelerator_type=accelerator_type, |
1149 | 1271 | ).pool_specs |
1150 | 1272 |
|
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 | | - |
1166 | 1273 | # make and copy package |
1167 | 1274 | python_packager = _TrainingScriptPythonPackager( |
1168 | 1275 | script_path=self._script_path, requirements=self._requirements |
1169 | 1276 | ) |
1170 | 1277 |
|
| 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 | + |
1171 | 1285 | return self._run( |
1172 | 1286 | python_packager=python_packager, |
1173 | 1287 | dataset=dataset, |
|