class_docstr: A mixin class for SageMaker ``Inference Recommender`` that will be extended by ``Model``
class_docstr_tok: ["A","mixin","class","for","SageMaker","``","Inference","Recommender","``","that","will","be","extended","by","``","Model","``"]
docstr: Update parameters with inference recommendation results.
  
  Args:
      instance_type (str): The EC2 instance type to deploy this Model to.
          For example, 'ml.p2.xlarge', or 'local' for local mode. If not using
          serverless inference, then it is required to deploy a model.
      initial_instance_count (int): The initial number of instances to run
          in the ``Endpoint`` created from this ``Model``. If not using
          serverless inference, then it need to be a number larger or equals
          to 1.
      accelerator_type (str): Type of Elastic Inference accelerator to
          deploy this model for model loading and inference, for example,
          'ml.eia1.medium'. If not specified, no Elastic Inference
          accelerator will be attached to the endpoint. For more
          information:
          https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html
      async_inference_config (sagemaker.model_monitor.AsyncInferenceConfig): Specifies
          configuration related to async endpoint. Use this configuration when trying
          to create async endpoint and make async inference. If empty config object
          passed through, will use default config to deploy async endpoint. Deploy a
          real-time endpoint if it's None.
      serverless_inference_config (sagemaker.serverless.ServerlessInferenceConfig):
          Specifies configuration related to serverless endpoint. Use this configuration
          when trying to create serverless endpoint and make serverless inference. If
          empty object passed through, will use pre-defined values in
          ``ServerlessInferenceConfig`` class to deploy serverless endpoint. Deploy an
          instance based endpoint if it's None.
      inference_recommendation_id (str): The recommendation id which specifies
          the recommendation you picked from inference recommendation job
          results and would like to deploy the model and endpoint with
          recommended parameters.
      explainer_config (sagemaker.explainer.ExplainerConfig): Specifies online explainability
          configuration for use with Amazon SageMaker Clarify. Default: None.
  Raises:
      ValueError: If arguments combination check failed in these circumstances:
          - If only one of instance type or instance count specified or
          - If recommendation id does not follow the required format or
          - If recommendation id is not valid or
          - If inference recommendation id is specified along with incompatible parameters
  Returns:
      (string, int): instance type and associated instance count from selected
      inference recommendation id if arguments combination check passed.
docstr_tok: ["Update","parameters","with","inference","recommendation","results",".","Args",":","instance_type","(","str",")",":","The","EC2","instance","type","to","deploy","this","Model","to",".","For","example",",","'ml.p2.xlarge","'",",","or","'local","'","for","local","mode",".","If","not","using","serverless","inference",",","then","it","is","required","to","deploy","a","model",".","initial_instance_count","(","int",")",":","The","initial","number","of","instances","to","run","in","the","``","Endpoint","``","created","from","this","``","Model","``",".","If","not","using","serverless","inference",",","then","it","need","to","be","a","number","larger","or","equals","to","1.","accelerator_type","(","str",")",":","Type","of","Elastic","Inference","accelerator","to","deploy","this","model","for","model","loading","and","inference",",","for","example",",","'ml.eia1.medium","'",".","If","not","specified",",","no","Elastic","Inference","accelerator","will","be","attached","to","the","endpoint",".","For","more","information",":","https",":","\/\/docs.aws.amazon.com\/sagemaker\/latest\/dg\/ei.html","async_inference_config","(","sagemaker.model_monitor.AsyncInferenceConfig",")",":","Specifies","configuration","related","to","async","endpoint",".","Use","this","configuration","when","trying","to","create","async","endpoint","and","make","async","inference",".","If","empty","config","object","passed","through",",","will","use","default","config","to","deploy","async","endpoint",".","Deploy","a","real-time","endpoint","if","it","'s","None",".","serverless_inference_config","(","sagemaker.serverless.ServerlessInferenceConfig",")",":","Specifies","configuration","related","to","serverless","endpoint",".","Use","this","configuration","when","trying","to","create","serverless","endpoint","and","make","serverless","inference",".","If","empty","object","passed","through",",","will","use","pre-defined","values","in","``","ServerlessInferenceConfig","``","class","to","deploy","serverless","endpoint",".","Deploy","an","instance","based","endpoint","if","it","'s","None",".","inference_recommendation_id","(","str",")",":","The","recommendation","id","which","specifies","the","recommendation","you","picked","from","inference","recommendation","job","results","and","would","like","to","deploy","the","model","and","endpoint","with","recommended","parameters",".","explainer_config","(","sagemaker.explainer.ExplainerConfig",")",":","Specifies","online","explainability","configuration","for","use","with","Amazon","SageMaker","Clarify",".","Default",":","None",".","Raises",":","ValueError",":","If","arguments","combination","check","failed","in","these","circumstances",":","-","If","only","one","of","instance","type","or","instance","count","specified","or","-","If","recommendation","id","does","not","follow","the","required","format","or","-","If","recommendation","id","is","not","valid","or","-","If","inference","recommendation","id","is","specified","along","with","incompatible","parameters","Returns",":","(","string",",","int",")",":","instance","type","and","associated","instance","count","from","selected","inference","recommendation","id","if","arguments","combination","check","passed","."]
code: def _update_params_for_recommendation_id(
      self,
      instance_type,
      initial_instance_count,
      accelerator_type,
      async_inference_config,
      serverless_inference_config,
      inference_recommendation_id,
      explainer_config,
  ):
      """Update parameters with inference recommendation results.
  
      Args:
          instance_type (str): The EC2 instance type to deploy this Model to.
              For example, 'ml.p2.xlarge', or 'local' for local mode. If not using
              serverless inference, then it is required to deploy a model.
          initial_instance_count (int): The initial number of instances to run
              in the ``Endpoint`` created from this ``Model``. If not using
              serverless inference, then it need to be a number larger or equals
              to 1.
          accelerator_type (str): Type of Elastic Inference accelerator to
              deploy this model for model loading and inference, for example,
              'ml.eia1.medium'. If not specified, no Elastic Inference
              accelerator will be attached to the endpoint. For more
              information:
              https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html
          async_inference_config (sagemaker.model_monitor.AsyncInferenceConfig): Specifies
              configuration related to async endpoint. Use this configuration when trying
              to create async endpoint and make async inference. If empty config object
              passed through, will use default config to deploy async endpoint. Deploy a
              real-time endpoint if it's None.
          serverless_inference_config (sagemaker.serverless.ServerlessInferenceConfig):
              Specifies configuration related to serverless endpoint. Use this configuration
              when trying to create serverless endpoint and make serverless inference. If
              empty object passed through, will use pre-defined values in
              ``ServerlessInferenceConfig`` class to deploy serverless endpoint. Deploy an
              instance based endpoint if it's None.
          inference_recommendation_id (str): The recommendation id which specifies
              the recommendation you picked from inference recommendation job
              results and would like to deploy the model and endpoint with
              recommended parameters.
          explainer_config (sagemaker.explainer.ExplainerConfig): Specifies online explainability
              configuration for use with Amazon SageMaker Clarify. Default: None.
      Raises:
          ValueError: If arguments combination check failed in these circumstances:
              - If only one of instance type or instance count specified or
              - If recommendation id does not follow the required format or
              - If recommendation id is not valid or
              - If inference recommendation id is specified along with incompatible parameters
      Returns:
          (string, int): instance type and associated instance count from selected
          inference recommendation id if arguments combination check passed.
      """
  
      if (
          instance_type is not None
          and initial_instance_count is not None
      ):
          logger.warning(
              "Both instance_type and initial_instance_count are specified,"
              "overriding the recommendation result."
          )
          return (instance_type, initial_instance_count)
  
      # Validate non-compatible parameters with recommendation id
      if accelerator_type is not None:
          raise ValueError(
              "accelerator_type is not compatible with inference_recommendation_id."
          )
      if async_inference_config is not None:
          raise ValueError(
              "async_inference_config is not compatible with inference_recommendation_id."
          )
      if serverless_inference_config is not None:
          raise ValueError(
              "serverless_inference_config is not compatible with inference_recommendation_id."
          )
      if explainer_config is not None:
          raise ValueError(
              "explainer_config is not compatible with inference_recommendation_id."
          )
  
      # Validate recommendation id
      if not re.match(
          r"[a-zA-Z0-9](-*[a-zA-Z0-9]){0,63}\/\w{8}$",
          inference_recommendation_id,
      ):
          raise ValueError("inference_recommendation_id is not valid")
      job_or_model_name = inference_recommendation_id.split("/")[0]
  
      sage_client = self.sagemaker_session.sagemaker_client
      # Get recommendation from right size job and model
      (
          right_size_recommendation,
          model_recommendation,
          right_size_job_res,
      ) = self._get_recommendation(
          sage_client=sage_client,
          job_or_model_name=job_or_model_name,
          inference_recommendation_id=inference_recommendation_id,
      )
  
      # Update params beased on model recommendation
      if model_recommendation:
          if initial_instance_count is None:
              raise ValueError(
                  "Must specify model recommendation id and instance count."
              )
          self.env.update(model_recommendation["Environment"])
          instance_type = model_recommendation["InstanceType"]
          return (instance_type, initial_instance_count)
  
      # Update params based on default inference recommendation
      if bool(instance_type) != bool(initial_instance_count):
          raise ValueError(
              "instance_type and initial_instance_count are mutually exclusive with"
              "recommendation id since they are in recommendation."
              "Please specify both of them if you want to override the recommendation."
          )
      input_config = right_size_job_res["InputConfig"]
      model_config = right_size_recommendation["ModelConfiguration"]
      envs = (
          model_config["EnvironmentParameters"]
          if "EnvironmentParameters" in model_config
          else None
      )
      # Update envs
      recommend_envs = {}
      if envs is not None:
          for env in envs:
              recommend_envs[env["Key"]] = env["Value"]
      self.env.update(recommend_envs)
  
      # Update params with non-compilation recommendation results
      if (
          "InferenceSpecificationName" not in model_config
          and "CompilationJobName" not in model_config
      ):
          if "ModelPackageVersionArn" in input_config:
              modelpkg_res = sage_client.describe_model_package(
                  ModelPackageName=input_config[
                      "ModelPackageVersionArn"
                  ]
              )
              self.model_data = modelpkg_res["InferenceSpecification"][
                  "Containers"
              ][0]["ModelDataUrl"]
              self.image_uri = modelpkg_res["InferenceSpecification"][
                  "Containers"
              ][0]["Image"]
          elif "ModelName" in input_config:
              model_res = sage_client.describe_model(
                  ModelName=input_config["ModelName"]
              )
              self.model_data = model_res["PrimaryContainer"][
                  "ModelDataUrl"
              ]
              self.image_uri = model_res["PrimaryContainer"]["Image"]
      else:
          if "InferenceSpecificationName" in model_config:
              modelpkg_res = sage_client.describe_model_package(
                  ModelPackageName=input_config[
                      "ModelPackageVersionArn"
                  ]
              )
              self.model_data = modelpkg_res[
                  "AdditionalInferenceSpecificationDefinition"
              ]["Containers"][0]["ModelDataUrl"]
              self.image_uri = modelpkg_res[
                  "AdditionalInferenceSpecificationDefinition"
              ]["Containers"][0]["Image"]
          elif "CompilationJobName" in model_config:
              compilation_res = sage_client.describe_compilation_job(
                  CompilationJobName=model_config["CompilationJobName"]
              )
              self.model_data = compilation_res["ModelArtifacts"][
                  "S3ModelArtifacts"
              ]
              self.image_uri = compilation_res["InferenceImage"]
  
      instance_type = right_size_recommendation[
          "EndpointConfiguration"
      ]["InstanceType"]
      initial_instance_count = right_size_recommendation[
          "EndpointConfiguration"
      ]["InitialInstanceCount"]
  
      return (instance_type, initial_instance_count)
  
imports: from __future__ import absolute_import
  import logging
  import re
  from typing import List, Dict, Optional
  import sagemaker
  from sagemaker.parameter import CategoricalParameter
label_desc: Use image node_id 4 for calling the InferenceRecommenderMixin obj's underlying member method code with example usage: obj._update_params_for_recommendation_id(instance_type, initial_instance_count, accelerator_type, async_inference_config, serverless_inference_config, inference_recommendation_id, explainer_config) and returns: instance_type,  initial_instance_count, instance_type,  initial_instance_count, instance_type,  initial_instance_count