You are viewing documentation about an older version (1.7.0). View latest version

snowflake.ml.model.ModelVersion

class snowflake.ml.model.ModelVersion

Bases: LineageNode

Model Version Object representing a specific version of the model that could be run.

Initializes a LineageNode instance.

Parameters:
  • session – The Snowflake session object.

  • name – Fully qualified name of the lineage node, which is in the format ‘<db>.<schema>.<object_name>’.

  • domain – The domain of the lineage node.

  • version – The version of the lineage node, if applies.

  • status – The status of the lineage node. Possible values are: - ‘MASKED’: The user does not have the privilege to view the node. - ‘DELETED’: The node has been deleted. - ‘ACTIVE’: The node is currently active.

  • created_on – The creation time of the lineage node.

Raises:

ValueError – If the name is not fully qualified.

Methods

create_service(*, service_name: str, image_build_compute_pool: Optional[str] = None, service_compute_pool: str, image_repo: str, ingress_enabled: bool = False, max_instances: int = 1, cpu_requests: Optional[str] = None, memory_requests: Optional[str] = None, gpu_requests: Optional[str] = None, num_workers: Optional[int] = None, max_batch_rows: Optional[int] = None, force_rebuild: bool = False, build_external_access_integration: Optional[str] = None) str
create_service(*, service_name: str, image_build_compute_pool: Optional[str] = None, service_compute_pool: str, image_repo: str, ingress_enabled: bool = False, max_instances: int = 1, cpu_requests: Optional[str] = None, memory_requests: Optional[str] = None, gpu_requests: Optional[str] = None, num_workers: Optional[int] = None, max_batch_rows: Optional[int] = None, force_rebuild: bool = False, build_external_access_integrations: Optional[List[str]] = None) str

Create an inference service with the given spec.

Parameters:
  • service_name – The name of the service, can be fully qualified. If not fully qualified, the database or schema of the model will be used.

  • image_build_compute_pool – The name of the compute pool used to build the model inference image. It uses the service compute pool if None.

  • service_compute_pool – The name of the compute pool used to run the inference service.

  • image_repo – The name of the image repository, can be fully qualified. If not fully qualified, the database or schema of the model will be used.

  • ingress_enabled – If true, creates an service endpoint associated with the service. User must have BIND SERVICE ENDPOINT privilege on the account.

  • max_instances – The maximum number of inference service instances to run. The same value it set to MIN_INSTANCES property of the service.

  • cpu_requests – The cpu limit for CPU based inference. Can be an integer, fractional or string values. If None, we attempt to utilize all the vCPU of the node.

  • memory_requests – The memory limit with for CPU based inference. Can be an integer or a fractional value, but requires a unit (GiB, MiB). If None, we attempt to utilize all the memory of the node.

  • gpu_requests – The gpu limit for GPU based inference. Can be integer, fractional or string values. Use CPU if None.

  • num_workers – The number of workers to run the inference service for handling requests in parallel within an instance of the service. By default, it is set to 2*vCPU+1 of the node for CPU based inference and 1 for GPU based inference. For GPU based inference, please see best practices before playing with this value.

  • max_batch_rows – The maximum number of rows to batch for inference. Auto determined if None. Minimum 32.

  • force_rebuild – Whether to force a model inference image rebuild.

  • build_external_access_integration – (Deprecated) The external access integration for image build. This is usually permitting access to conda & PyPI repositories.

  • build_external_access_integrations – The external access integrations for image build. This is usually permitting access to conda & PyPI repositories.

Raises:

ValueError – Illegal external access integration arguments.

Returns:

Result information about service creation from server.

delete_metric(metric_name: str) None

Delete a metric from metric storage.

Parameters:

metric_name – The name of the metric to be deleted.

Raises:

KeyError – When the requested metric name does not exist.

delete_service(service_name: str) None

Drops the given service.

Parameters:

service_name – The name of the service, can be fully qualified. If not fully qualified, the database or schema of the model will be used.

Raises:

ValueError – If the service does not exist or operation is not permitted by user or service does not belong to this model.

export(target_path: str, *, export_mode: ExportMode = ExportMode.MODEL) None

Export model files to a local directory.

Parameters:
  • target_path – Path to a local directory to export files to. A directory will be created if does not exist.

  • export_mode – The mode to export the model. Defaults to ExportMode.MODEL. ExportMode.MODEL: All model files including environment to load the model and model weights. ExportMode.FULL: Additional files to run the model in Warehouse, besides all files in MODEL mode,

Raises:

ValueError – Raised when the target path is a file or an non-empty folder.

get_metric(metric_name: str) Any

Get the value of a specific metric.

Parameters:

metric_name – The name of the metric.

Raises:

KeyError – When the requested metric name does not exist.

Returns:

The value of the metric.

get_model_task() Task
lineage(direction: Literal['upstream', 'downstream'] = 'downstream', domain_filter: Optional[Set[Literal['feature_view', 'dataset', 'model', 'table', 'view']]] = None) List[Union[FeatureView, Dataset, ModelVersion, LineageNode]]

Retrieves the lineage nodes connected to this node.

Parameters:
  • direction – The direction to trace lineage. Defaults to “downstream”.

  • domain_filter – Set of domains to filter nodes. Defaults to None.

Returns:

A list of connected lineage nodes.

Return type:

List[LineageNode]

This function or method is in private preview since 1.5.3.

list_services() DataFrame

List all the service names using this model version.

Returns:

The name of the service, can be fully qualified. If not fully qualified, the database

or schema of the model will be used.

Return type:

List of service_names

load(*, force: bool = False, options: Optional[Union[BaseModelLoadOption, CatBoostModelLoadOptions, CustomModelLoadOption, LGBMModelLoadOptions, SKLModelLoadOptions, XGBModelLoadOptions, SNOWModelLoadOptions, PyTorchLoadOptions, TorchScriptLoadOptions, TensorflowLoadOptions, MLFlowLoadOptions, HuggingFaceLoadOptions, SentenceTransformersLoadOptions]] = None) Union[catboost.CatBoost, lightgbm.LGBMModel, lightgbm.Booster, CustomModel, sklearn.base.BaseEstimator, sklearn.pipeline.Pipeline, xgboost.XGBModel, xgboost.Booster, torch.nn.Module, torch.jit.ScriptModule, tensorflow.Module, base.BaseEstimator, mlflow.pyfunc.PyFuncModel, transformers.Pipeline, sentence_transformers.SentenceTransformer, HuggingFacePipelineModel]
Load the underlying original Python object back from a model.

This operation requires to have the exact the same environment as the one when logging the model, otherwise, the model might be not functional or some other problems might occur.

Parameters:
  • force – Bypass the best-effort environment validation. Defaults to False.

  • options – Options to specify when loading the model, check snowflake.ml.model.type_hints for available options. Defaults to None.

Raises:

ValueError – Raised when the best-effort environment validation fails.

Returns:

The original Python object loaded from the model object.

run(X: Union[pd.DataFrame, dataframe.DataFrame], *, function_name: Optional[str] = None, partition_column: Optional[str] = None, strict_input_validation: bool = False) Union[pd.DataFrame, dataframe.DataFrame]
run(X: Union[pd.DataFrame, dataframe.DataFrame], *, service_name: str, function_name: Optional[str] = None, strict_input_validation: bool = False) Union[pd.DataFrame, dataframe.DataFrame]

Invoke a method in a model version object via the warehouse or a service.

Parameters:
  • X – The input data, which could be a pandas DataFrame or Snowpark DataFrame.

  • service_name – The service name. If None, the function is invoked via the warehouse. Otherwise, the function is invoked via the given service.

  • function_name – The function name to run. It is the name used to call a function in SQL.

  • partition_column – The partition column name to partition by.

  • strict_input_validation – Enable stricter validation for the input data. This will result value range based type validation to make sure your input data won’t overflow when providing to the model.

Raises:
  • ValueError – When no method with the corresponding name is available.

  • ValueError – When there are more than 1 target methods available in the model but no function name specified.

  • ValueError – When the partition column is not a valid Snowflake identifier.

Returns:

The prediction data. It would be the same type dataframe as your input.

set_alias(alias_name: str) None

Set alias to a model version.

Parameters:

alias_name – Alias to the model version.

set_metric(metric_name: str, value: Any) None

Set the value of a specific metric.

Parameters:
  • metric_name – The name of the metric.

  • value – The value of the metric.

show_functions() List[ModelFunctionInfo]

Show all functions information in a model version that is callable.

Returns:

  • name: The name of the function to be called (both in SQL and in Python SDK).

  • target_method: The original method name in the logged Python object.

  • signature: Python signature of the original method.

Return type:

A list of ModelFunctionInfo objects containing the following information

show_metrics() Dict[str, Any]

Show all metrics logged with the model version.

Returns:

A dictionary showing the metrics.

unset_alias(version_or_alias: str) None

unset alias to a model version.

Parameters:

version_or_alias – The name of the version or alias to a version.

Attributes

comment

The comment to the model version.

description

The description for the model version. This is an alias of comment.

fully_qualified_model_name

Return the fully qualified name of the model to which the model version belongs.

model_name

Return the name of the model to which the model version belongs, usable as a reference in SQL.

version_name

Return the name of the version to which the model version belongs, usable as a reference in SQL.