Пример #1
0
    def _PrepareModelPath(
            self, model_uri: Text,
            serving_spec: infra_validator_pb2.ServingSpec) -> Text:
        model_path = path_utils.serving_model_path(model_uri)
        serving_binary = serving_spec.WhichOneof('serving_binary')
        if serving_binary == 'tensorflow_serving':
            # TensorFlow Serving requires model to be stored in its own directory
            # structure flavor. If current model_path does not conform to the flavor,
            # we need to make a copy to the temporary path.
            try:
                # Check whether current model_path conforms to the tensorflow serving
                # model path flavor. (Parsed without exception)
                tf_serving_flavor.parse_model_path(
                    model_path, expected_model_name=serving_spec.model_name)
            except ValueError:
                # Copy the model to comply with the tensorflow serving model path
                # flavor.
                temp_model_path = tf_serving_flavor.make_model_path(
                    model_base_path=self._get_tmp_dir(),
                    model_name=serving_spec.model_name,
                    version=int(time.time()))
                io_utils.copy_dir(src=model_path, dst=temp_model_path)
                return temp_model_path

        return model_path
Пример #2
0
def _create_model_server_runner(
    model_path: Text,
    serving_binary: serving_bins.ServingBinary,
    serving_spec: infra_validator_pb2.ServingSpec):
  """Create a ModelServerRunner from a model, a ServingBinary and a ServingSpec.

  Args:
    model_path: An IV-flavored model path. (See model_path_utils.py)
    serving_binary: One of ServingBinary instances parsed from the
        `serving_spec`.
    serving_spec: A ServingSpec instance of this infra validation.

  Returns:
    A ModelServerRunner.
  """
  platform = serving_spec.WhichOneof('serving_platform')
  if platform == 'local_docker':
    return local_docker_runner.LocalDockerRunner(
        model_path=model_path,
        serving_binary=serving_binary,
        serving_spec=serving_spec
    )
  elif platform == 'kubernetes':
    return kubernetes_runner.KubernetesRunner(
        model_path=model_path,
        serving_binary=serving_binary,
        serving_spec=serving_spec
    )
  else:
    raise NotImplementedError('Invalid serving_platform {}'.format(platform))
Пример #3
0
    def __init__(self, model_path: str,
                 serving_binary: serving_bins.ServingBinary,
                 serving_spec: infra_validator_pb2.ServingSpec):
        """Create a kubernetes model server runner.

    Args:
      model_path: An IV-flavored model path. (See model_path_utils.py)
      serving_binary: A ServingBinary to run.
      serving_spec: A ServingSpec instance.
    """
        assert serving_spec.WhichOneof('serving_platform') == 'kubernetes', (
            'ServingSpec configuration mismatch.')
        self._config = serving_spec.kubernetes

        self._model_path = model_path
        self._serving_binary = serving_binary
        self._serving_spec = serving_spec
        self._k8s_core_api = kube_utils.make_core_v1_api()
        if not kube_utils.is_inside_kfp():
            raise NotImplementedError(
                'KubernetesRunner should be running inside KFP.')
        self._executor_pod = kube_utils.get_current_kfp_pod(self._k8s_core_api)
        self._executor_container = _get_container_or_error(
            self._executor_pod,
            container_name=kube_utils.ARGO_MAIN_CONTAINER_NAME)
        self._namespace = kube_utils.get_kfp_namespace()
        self._label_dict = {
            _APP_KEY: _MODEL_SERVER_APP_LABEL,
        }
        # Pod name would be populated once creation request sent.
        self._pod_name = None
        # Endpoint would be populated once the Pod is running.
        self._endpoint = None
Пример #4
0
def make_client_factory(
    model: standard_artifacts.Model,
    serving_spec: infra_validator_pb2.ServingSpec) -> ClientFactory:
  """Creates ClientFactory from Model artifact and ServingSpec configuration.

  Note that for each `serving_binary` in ServingSpec there is a corresponding
  ModelServerClient class. (1on1 mapping)

  Args:
    model: A `Model` artifact.
    serving_spec: A `ServingSpec` configuration.

  Returns:
    A ModelServerClient factory function that takes Text endpoint as an argument
    and returns a ModelServerClient.
  """
  serving_binary = serving_spec.WhichOneof('serving_binary')
  if not serving_binary:
    raise ValueError('serving_binary must be set.')

  if serving_binary == TENSORFLOW_SERVING:
    model_name = os.path.basename(
        os.path.dirname(path_utils.serving_model_path(model.uri)))
    return functools.partial(
        tensorflow_serving_client.TensorFlowServingClient,
        model_name=model_name)
  else:
    raise NotImplementedError('{} is not supported'.format(serving_binary))
Пример #5
0
    def _PrepareModelPath(
            self, model: types.Artifact,
            serving_spec: infra_validator_pb2.ServingSpec) -> str:
        model_path = path_utils.serving_model_path(
            model.uri, path_utils.is_old_model_artifact(model))
        serving_binary = serving_spec.WhichOneof('serving_binary')
        if serving_binary == _TENSORFLOW_SERVING:
            # TensorFlow Serving requires model to be stored in its own directory
            # structure flavor. If current model_path does not conform to the flavor,
            # we need to make a copy to the temporary path.
            try:
                # Check whether current model_path conforms to the tensorflow serving
                # model path flavor. (Parsed without exception)
                tf_serving_flavor.parse_model_path(
                    model_path, expected_model_name=serving_spec.model_name)
            except ValueError:
                # Copy the model to comply with the tensorflow serving model path
                # flavor.
                temp_model_path = tf_serving_flavor.make_model_path(
                    model_base_path=self._get_tmp_dir(),
                    model_name=serving_spec.model_name,
                    version=int(time.time()))
                io_utils.copy_dir(src=model_path, dst=temp_model_path)
                self._AddCleanup(io_utils.delete_dir,
                                 self._context.get_tmp_path())
                return temp_model_path

        return model_path
Пример #6
0
def parse_serving_binaries(  # pylint: disable=invalid-name
    serving_spec: infra_validator_pb2.ServingSpec) -> List['ServingBinary']:
    """Parse `ServingBinary`s from `ServingSpec`."""
    result = []
    serving_binary = serving_spec.WhichOneof('serving_binary')
    if serving_binary == 'tensorflow_serving':
        config = serving_spec.tensorflow_serving
        for tag in config.tags:
            result.append(
                TensorFlowServing(model_name=serving_spec.model_name, tag=tag))
        for digest in config.digests:
            result.append(
                TensorFlowServing(model_name=serving_spec.model_name,
                                  digest=digest))
        return result
    else:
        raise ValueError('Invalid serving_binary {}'.format(serving_binary))
Пример #7
0
def _create_model_server_runner(model: types.Artifact,
                                serving_binary: serving_bins.ServingBinary,
                                serving_spec: infra_validator_pb2.ServingSpec):
    """Create a ModelServerRunner from a model, a ServingBinary and a ServingSpec.

  Args:
    model: Model artifact that will be infra validated.
    serving_binary: One of ServingBinary instances parsed from the
        `serving_spec`.
    serving_spec: A ServingSpec instance of this infra validation.

  Returns:
    A ModelServerRunner.
  """
    platform = serving_spec.WhichOneof('serving_platform')
    if platform == 'local_docker':
        return local_docker_runner.LocalDockerRunner(
            model=model,
            serving_binary=serving_binary,
            serving_spec=serving_spec)
    else:
        raise NotImplementedError(
            'Invalid serving_platform {}'.format(platform))
Пример #8
0
def create_model_server_runners(
    model: standard_artifacts.Model,
    serving_spec: infra_validator_pb2.ServingSpec
) -> Iterable[base_runner.BaseModelServerRunner]:
  """Create model server runners based on given model and serving spec.

  In ServingSpec you can specify multiple versions for validation on single
  image. In such case it returns multiple model server runners for each
  (image, version) pair.

  Args:
    model: A model artifact whose uri contains the path to the servable model.
    serving_spec: A ServingSpec configuration.

  Returns:
    An iterable of `BaseModelServerRunner`.
  """
  platform_kind = serving_spec.WhichOneof('serving_platform')
  if platform_kind == 'local_docker':
    return _create_local_docker_runners(model, serving_spec)
  else:
    raise NotImplementedError('{} platform is not yet supported'
                              .format(platform_kind))