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, image_name: Optional[str] = None, ingress_enabled: bool = False, min_instances: int = 1, max_instances: int = 1, gpu_requests: Optional[str] = None, force_rebuild: bool = False, build_external_access_integration: str) 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. Use 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.
image_name – The name of the model inference image. Use a generated name if None.
ingress_enabled – Whether to enable ingress.
min_instances – The minimum number of inference service instances to run.
max_instances – The maximum number of inference service instances to run.
gpu_requests – The gpu limit for GPU based inference. Can be integer, fractional or string values. Use CPU if None.
force_rebuild – Whether to force a model inference image rebuild.
build_external_access_integration – The external access integration for image build.
- Returns:
The service name.
- 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.
- 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.
- 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.
- load(*, force: bool = False, options: Optional[Union[BaseModelLoadOption, CatBoostModelLoadOptions, CustomModelLoadOption, LGBMModelLoadOptions, SKLModelLoadOptions, XGBModelLoadOptions, SNOWModelLoadOptions, PyTorchLoadOptions, TorchScriptLoadOptions, TensorflowLoadOptions, MLFlowLoadOptions, HuggingFaceLoadOptions, SentenceTransformersLoadOptions, LLMLoadOptions]] = 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, snowflake.ml.model.models.llm.LLM] ¶
- 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.