Esempio n. 1
0
def ParseAcceleratorFlag(accelerator, version):
  """Validates and returns an accelerator config message object."""
  if accelerator is None:
    return None
  types = list(c for c in GetAcceleratorTypeMapper(version).choices)
  raw_type = accelerator.get('type', None)
  if raw_type not in types:
    raise errors.ArgumentError("""\
The type of the accelerator can only be one of the following: {}.
""".format(', '.join(["'{}'".format(c) for c in types])))
  accelerator_count = accelerator.get('count', 1)
  if accelerator_count <= 0:
    raise errors.ArgumentError("""\
The count of the accelerator must be greater than 0.
""")
  if version == constants.ALPHA_VERSION:
    accelerator_msg = (
        apis.GetMessagesModule(constants.AI_PLATFORM_API_NAME,
                               constants.AI_PLATFORM_API_VERSION[version])
        .GoogleCloudAiplatformV1alpha1MachineSpec)
  elif version == constants.BETA_VERSION:
    accelerator_msg = (
        apis.GetMessagesModule(constants.AI_PLATFORM_API_NAME,
                               constants.AI_PLATFORM_API_VERSION[version])
        .GoogleCloudAiplatformV1beta1MachineSpec)
  else:
    accelerator_msg = (
        apis.GetMessagesModule(constants.AI_PLATFORM_API_NAME,
                               constants.AI_PLATFORM_API_VERSION[version])
        .GoogleCloudAiplatformV1MachineSpec)
  accelerator_type = arg_utils.ChoiceToEnum(
      raw_type, accelerator_msg.AcceleratorTypeValueValuesEnum)
  return accelerator_msg(
      acceleratorCount=accelerator_count, acceleratorType=accelerator_type)
Esempio n. 2
0
def _GetModelDeploymentResourceType(model_ref, client):
    """Gets the deployment resource type of a model.

  Each model resource belongs to exactly one supported deployment resource type.
  The value is the first item in the list, the length of which must be one.

  Args:
    model_ref: a model resource object.
    client: an apis.GetClientInstance object.

  Returns:
    A string which value must be 'DEDICATED_RESOURCES' or 'AUTOMATIC_RESOURCES'.

  Raises:
    ArgumentError: if the model resource object is not found.
  """
    try:
        model_msg = model_client.ModelsClient(client=client).Get(model_ref)
    except apitools_exceptions.HttpError:
        raise errors.ArgumentError(
            ('There is an error while getting the model information. '
             'Please make sure the model %r exists.' %
             model_ref.RelativeName()))
    model_resource = encoding.MessageToPyValue(model_msg)
    return model_resource['supportedDeploymentResourcesTypes'][0]
Esempio n. 3
0
    def _ConstructObjectiveConfigForCreate(self, location_ref, endpoint_name,
                                           feature_thresholds, dataset,
                                           bigquery_uri, data_format, gcs_uris,
                                           target_field,
                                           training_sampling_rate):
        """Construct monitoring objective config.

    Apply the feature thresholds for skew or drift detection to all the deployed
    models under the endpoint.
    Args:
      location_ref: Location reference.
      endpoint_name: Endpoint resource name.
      feature_thresholds: Dict or None, key: feature_name, value: thresholds.
      dataset: Vertex AI Dataset Id.
      bigquery_uri: The BigQuery table of the unmanaged Dataset used to train
        this Model.
      data_format: Google Cloud Storage format, supported format: csv,
        tf-record.
      gcs_uris: The Google Cloud Storage uri of the unmanaged Dataset used to
        train this Model.
      target_field: The target field name the model is to predict.
      training_sampling_rate: Training Dataset sampling rate.

    Returns:
      A list of model monitoring objective config.
    """
        objective_config_template = self.messages.GoogleCloudAiplatformV1beta1ModelDeploymentMonitoringObjectiveConfig(
        )
        if feature_thresholds:
            if dataset or bigquery_uri or gcs_uris or data_format:
                training_dataset = self.messages.GoogleCloudAiplatformV1beta1ModelMonitoringObjectiveConfigTrainingDataset(
                )
                if target_field is None:
                    raise errors.ArgumentError(
                        "Target field must be provided if you'd like to do training-prediction skew detection."
                    )
                training_dataset.targetField = target_field
                training_dataset.loggingSamplingStrategy = self.messages.GoogleCloudAiplatformV1beta1SamplingStrategy(
                    randomSampleConfig=self.messages.
                    GoogleCloudAiplatformV1beta1SamplingStrategyRandomSampleConfig(
                        sampleRate=training_sampling_rate))
                if dataset:
                    training_dataset.dataset = _ParseDataset(
                        dataset, location_ref).RelativeName()
                elif bigquery_uri:
                    training_dataset.bigquerySource = self.messages.GoogleCloudAiplatformV1beta1BigQuerySource(
                        inputUri=bigquery_uri)
                elif gcs_uris or data_format:
                    if gcs_uris is None:
                        raise errors.ArgumentError(
                            'Data format is defined but no Google Cloud Storage uris are provided. Please use --gcs-uris to provide training datasets.'
                        )
                    if data_format is None:
                        raise errors.ArgumentError(
                            'No Data format is defined for Google Cloud Storage training dataset. Please use --data-format to define the Data format.'
                        )
                    training_dataset.dataFormat = data_format
                    training_dataset.gcsSource = self.messages.GoogleCloudAiplatformV1beta1GcsSource(
                        uris=gcs_uris)
                training_prediction_skew_detection = self._ConstructSkewThresholds(
                    feature_thresholds)
                objective_config_template.objectiveConfig = self.messages.GoogleCloudAiplatformV1beta1ModelMonitoringObjectiveConfig(
                    trainingDataset=training_dataset,
                    trainingPredictionSkewDetectionConfig=
                    training_prediction_skew_detection)
            else:
                prediction_drift_detection = self._ConstructDriftThresholds(
                    feature_thresholds)
                objective_config_template.objectiveConfig = self.messages.GoogleCloudAiplatformV1beta1ModelMonitoringObjectiveConfig(
                    predictionDriftDetectionConfig=prediction_drift_detection)

        get_endpoint_req = self.messages.AiplatformProjectsLocationsEndpointsGetRequest(
            name=endpoint_name)
        endpoint = self.client.projects_locations_endpoints.Get(
            get_endpoint_req)
        objective_configs = []
        for deployed_model in endpoint.deployedModels:
            objective_config = copy.deepcopy(objective_config_template)
            objective_config.deployedModelId = deployed_model.id
            objective_configs.append(objective_config)
        return objective_configs