snowflake-ml-python 1.8.3__py3-none-any.whl → 1.8.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. snowflake/cortex/__init__.py +7 -1
  2. snowflake/ml/_internal/platform_capabilities.py +13 -11
  3. snowflake/ml/_internal/telemetry.py +42 -13
  4. snowflake/ml/_internal/utils/identifier.py +2 -2
  5. snowflake/ml/data/data_connector.py +1 -1
  6. snowflake/ml/jobs/_utils/constants.py +10 -1
  7. snowflake/ml/jobs/_utils/interop_utils.py +1 -1
  8. snowflake/ml/jobs/_utils/payload_utils.py +51 -34
  9. snowflake/ml/jobs/_utils/scripts/constants.py +6 -0
  10. snowflake/ml/jobs/_utils/scripts/get_instance_ip.py +4 -4
  11. snowflake/ml/jobs/_utils/scripts/mljob_launcher.py +86 -3
  12. snowflake/ml/jobs/_utils/spec_utils.py +8 -6
  13. snowflake/ml/jobs/decorators.py +13 -3
  14. snowflake/ml/jobs/job.py +206 -26
  15. snowflake/ml/jobs/manager.py +78 -34
  16. snowflake/ml/model/_client/model/model_version_impl.py +1 -1
  17. snowflake/ml/model/_client/ops/service_ops.py +31 -17
  18. snowflake/ml/model/_client/service/model_deployment_spec.py +351 -170
  19. snowflake/ml/model/_client/service/model_deployment_spec_schema.py +25 -0
  20. snowflake/ml/model/_client/sql/model_version.py +1 -1
  21. snowflake/ml/model/_client/sql/service.py +20 -32
  22. snowflake/ml/model/_model_composer/model_composer.py +44 -19
  23. snowflake/ml/model/_packager/model_handlers/_utils.py +32 -2
  24. snowflake/ml/model/_packager/model_handlers/custom.py +1 -1
  25. snowflake/ml/model/_packager/model_handlers/pytorch.py +1 -2
  26. snowflake/ml/model/_packager/model_handlers/sklearn.py +100 -41
  27. snowflake/ml/model/_packager/model_handlers/tensorflow.py +7 -4
  28. snowflake/ml/model/_packager/model_handlers/torchscript.py +2 -2
  29. snowflake/ml/model/_packager/model_handlers/xgboost.py +16 -7
  30. snowflake/ml/model/_packager/model_meta/model_meta.py +2 -1
  31. snowflake/ml/model/_packager/model_meta/model_meta_schema.py +1 -0
  32. snowflake/ml/model/_packager/model_runtime/_snowml_inference_alternative_requirements.py +5 -4
  33. snowflake/ml/model/_signatures/dmatrix_handler.py +15 -2
  34. snowflake/ml/model/custom_model.py +17 -4
  35. snowflake/ml/model/model_signature.py +3 -3
  36. snowflake/ml/modeling/calibration/calibrated_classifier_cv.py +9 -1
  37. snowflake/ml/modeling/cluster/affinity_propagation.py +9 -1
  38. snowflake/ml/modeling/cluster/agglomerative_clustering.py +9 -1
  39. snowflake/ml/modeling/cluster/birch.py +9 -1
  40. snowflake/ml/modeling/cluster/bisecting_k_means.py +9 -1
  41. snowflake/ml/modeling/cluster/dbscan.py +9 -1
  42. snowflake/ml/modeling/cluster/feature_agglomeration.py +9 -1
  43. snowflake/ml/modeling/cluster/k_means.py +9 -1
  44. snowflake/ml/modeling/cluster/mean_shift.py +9 -1
  45. snowflake/ml/modeling/cluster/mini_batch_k_means.py +9 -1
  46. snowflake/ml/modeling/cluster/optics.py +9 -1
  47. snowflake/ml/modeling/cluster/spectral_biclustering.py +9 -1
  48. snowflake/ml/modeling/cluster/spectral_clustering.py +9 -1
  49. snowflake/ml/modeling/cluster/spectral_coclustering.py +9 -1
  50. snowflake/ml/modeling/compose/column_transformer.py +9 -1
  51. snowflake/ml/modeling/compose/transformed_target_regressor.py +9 -1
  52. snowflake/ml/modeling/covariance/elliptic_envelope.py +9 -1
  53. snowflake/ml/modeling/covariance/empirical_covariance.py +9 -1
  54. snowflake/ml/modeling/covariance/graphical_lasso.py +9 -1
  55. snowflake/ml/modeling/covariance/graphical_lasso_cv.py +9 -1
  56. snowflake/ml/modeling/covariance/ledoit_wolf.py +9 -1
  57. snowflake/ml/modeling/covariance/min_cov_det.py +9 -1
  58. snowflake/ml/modeling/covariance/oas.py +9 -1
  59. snowflake/ml/modeling/covariance/shrunk_covariance.py +9 -1
  60. snowflake/ml/modeling/decomposition/dictionary_learning.py +9 -1
  61. snowflake/ml/modeling/decomposition/factor_analysis.py +9 -1
  62. snowflake/ml/modeling/decomposition/fast_ica.py +9 -1
  63. snowflake/ml/modeling/decomposition/incremental_pca.py +9 -1
  64. snowflake/ml/modeling/decomposition/kernel_pca.py +9 -1
  65. snowflake/ml/modeling/decomposition/mini_batch_dictionary_learning.py +9 -1
  66. snowflake/ml/modeling/decomposition/mini_batch_sparse_pca.py +9 -1
  67. snowflake/ml/modeling/decomposition/pca.py +9 -1
  68. snowflake/ml/modeling/decomposition/sparse_pca.py +9 -1
  69. snowflake/ml/modeling/decomposition/truncated_svd.py +9 -1
  70. snowflake/ml/modeling/discriminant_analysis/linear_discriminant_analysis.py +9 -1
  71. snowflake/ml/modeling/discriminant_analysis/quadratic_discriminant_analysis.py +9 -1
  72. snowflake/ml/modeling/ensemble/ada_boost_classifier.py +9 -1
  73. snowflake/ml/modeling/ensemble/ada_boost_regressor.py +9 -1
  74. snowflake/ml/modeling/ensemble/bagging_classifier.py +9 -1
  75. snowflake/ml/modeling/ensemble/bagging_regressor.py +9 -1
  76. snowflake/ml/modeling/ensemble/extra_trees_classifier.py +9 -1
  77. snowflake/ml/modeling/ensemble/extra_trees_regressor.py +9 -1
  78. snowflake/ml/modeling/ensemble/gradient_boosting_classifier.py +9 -1
  79. snowflake/ml/modeling/ensemble/gradient_boosting_regressor.py +9 -1
  80. snowflake/ml/modeling/ensemble/hist_gradient_boosting_classifier.py +9 -1
  81. snowflake/ml/modeling/ensemble/hist_gradient_boosting_regressor.py +9 -1
  82. snowflake/ml/modeling/ensemble/isolation_forest.py +9 -1
  83. snowflake/ml/modeling/ensemble/random_forest_classifier.py +9 -1
  84. snowflake/ml/modeling/ensemble/random_forest_regressor.py +9 -1
  85. snowflake/ml/modeling/ensemble/stacking_regressor.py +9 -1
  86. snowflake/ml/modeling/ensemble/voting_classifier.py +9 -1
  87. snowflake/ml/modeling/ensemble/voting_regressor.py +9 -1
  88. snowflake/ml/modeling/feature_selection/generic_univariate_select.py +9 -1
  89. snowflake/ml/modeling/feature_selection/select_fdr.py +9 -1
  90. snowflake/ml/modeling/feature_selection/select_fpr.py +9 -1
  91. snowflake/ml/modeling/feature_selection/select_fwe.py +9 -1
  92. snowflake/ml/modeling/feature_selection/select_k_best.py +9 -1
  93. snowflake/ml/modeling/feature_selection/select_percentile.py +9 -1
  94. snowflake/ml/modeling/feature_selection/sequential_feature_selector.py +9 -1
  95. snowflake/ml/modeling/feature_selection/variance_threshold.py +9 -1
  96. snowflake/ml/modeling/gaussian_process/gaussian_process_classifier.py +9 -1
  97. snowflake/ml/modeling/gaussian_process/gaussian_process_regressor.py +9 -1
  98. snowflake/ml/modeling/impute/iterative_imputer.py +9 -1
  99. snowflake/ml/modeling/impute/knn_imputer.py +9 -1
  100. snowflake/ml/modeling/impute/missing_indicator.py +9 -1
  101. snowflake/ml/modeling/kernel_approximation/additive_chi2_sampler.py +9 -1
  102. snowflake/ml/modeling/kernel_approximation/nystroem.py +9 -1
  103. snowflake/ml/modeling/kernel_approximation/polynomial_count_sketch.py +9 -1
  104. snowflake/ml/modeling/kernel_approximation/rbf_sampler.py +9 -1
  105. snowflake/ml/modeling/kernel_approximation/skewed_chi2_sampler.py +9 -1
  106. snowflake/ml/modeling/kernel_ridge/kernel_ridge.py +9 -1
  107. snowflake/ml/modeling/lightgbm/lgbm_classifier.py +9 -1
  108. snowflake/ml/modeling/lightgbm/lgbm_regressor.py +9 -1
  109. snowflake/ml/modeling/linear_model/ard_regression.py +9 -1
  110. snowflake/ml/modeling/linear_model/bayesian_ridge.py +9 -1
  111. snowflake/ml/modeling/linear_model/elastic_net.py +9 -1
  112. snowflake/ml/modeling/linear_model/elastic_net_cv.py +9 -1
  113. snowflake/ml/modeling/linear_model/gamma_regressor.py +9 -1
  114. snowflake/ml/modeling/linear_model/huber_regressor.py +9 -1
  115. snowflake/ml/modeling/linear_model/lars.py +9 -1
  116. snowflake/ml/modeling/linear_model/lars_cv.py +9 -1
  117. snowflake/ml/modeling/linear_model/lasso.py +9 -1
  118. snowflake/ml/modeling/linear_model/lasso_cv.py +9 -1
  119. snowflake/ml/modeling/linear_model/lasso_lars.py +9 -1
  120. snowflake/ml/modeling/linear_model/lasso_lars_cv.py +9 -1
  121. snowflake/ml/modeling/linear_model/lasso_lars_ic.py +9 -1
  122. snowflake/ml/modeling/linear_model/linear_regression.py +9 -1
  123. snowflake/ml/modeling/linear_model/logistic_regression.py +9 -1
  124. snowflake/ml/modeling/linear_model/logistic_regression_cv.py +9 -1
  125. snowflake/ml/modeling/linear_model/multi_task_elastic_net.py +9 -1
  126. snowflake/ml/modeling/linear_model/multi_task_elastic_net_cv.py +9 -1
  127. snowflake/ml/modeling/linear_model/multi_task_lasso.py +9 -1
  128. snowflake/ml/modeling/linear_model/multi_task_lasso_cv.py +9 -1
  129. snowflake/ml/modeling/linear_model/orthogonal_matching_pursuit.py +9 -1
  130. snowflake/ml/modeling/linear_model/passive_aggressive_classifier.py +9 -1
  131. snowflake/ml/modeling/linear_model/passive_aggressive_regressor.py +9 -1
  132. snowflake/ml/modeling/linear_model/perceptron.py +9 -1
  133. snowflake/ml/modeling/linear_model/poisson_regressor.py +9 -1
  134. snowflake/ml/modeling/linear_model/ransac_regressor.py +9 -1
  135. snowflake/ml/modeling/linear_model/ridge.py +9 -1
  136. snowflake/ml/modeling/linear_model/ridge_classifier.py +9 -1
  137. snowflake/ml/modeling/linear_model/ridge_classifier_cv.py +9 -1
  138. snowflake/ml/modeling/linear_model/ridge_cv.py +9 -1
  139. snowflake/ml/modeling/linear_model/sgd_classifier.py +9 -1
  140. snowflake/ml/modeling/linear_model/sgd_one_class_svm.py +9 -1
  141. snowflake/ml/modeling/linear_model/sgd_regressor.py +9 -1
  142. snowflake/ml/modeling/linear_model/theil_sen_regressor.py +9 -1
  143. snowflake/ml/modeling/linear_model/tweedie_regressor.py +9 -1
  144. snowflake/ml/modeling/manifold/isomap.py +9 -1
  145. snowflake/ml/modeling/manifold/mds.py +9 -1
  146. snowflake/ml/modeling/manifold/spectral_embedding.py +9 -1
  147. snowflake/ml/modeling/manifold/tsne.py +9 -1
  148. snowflake/ml/modeling/mixture/bayesian_gaussian_mixture.py +9 -1
  149. snowflake/ml/modeling/mixture/gaussian_mixture.py +9 -1
  150. snowflake/ml/modeling/multiclass/one_vs_one_classifier.py +9 -1
  151. snowflake/ml/modeling/multiclass/one_vs_rest_classifier.py +9 -1
  152. snowflake/ml/modeling/multiclass/output_code_classifier.py +9 -1
  153. snowflake/ml/modeling/naive_bayes/bernoulli_nb.py +9 -1
  154. snowflake/ml/modeling/naive_bayes/categorical_nb.py +9 -1
  155. snowflake/ml/modeling/naive_bayes/complement_nb.py +9 -1
  156. snowflake/ml/modeling/naive_bayes/gaussian_nb.py +9 -1
  157. snowflake/ml/modeling/naive_bayes/multinomial_nb.py +9 -1
  158. snowflake/ml/modeling/neighbors/k_neighbors_classifier.py +9 -1
  159. snowflake/ml/modeling/neighbors/k_neighbors_regressor.py +9 -1
  160. snowflake/ml/modeling/neighbors/kernel_density.py +9 -1
  161. snowflake/ml/modeling/neighbors/local_outlier_factor.py +9 -1
  162. snowflake/ml/modeling/neighbors/nearest_centroid.py +9 -1
  163. snowflake/ml/modeling/neighbors/nearest_neighbors.py +9 -1
  164. snowflake/ml/modeling/neighbors/neighborhood_components_analysis.py +9 -1
  165. snowflake/ml/modeling/neighbors/radius_neighbors_classifier.py +9 -1
  166. snowflake/ml/modeling/neighbors/radius_neighbors_regressor.py +9 -1
  167. snowflake/ml/modeling/neural_network/bernoulli_rbm.py +9 -1
  168. snowflake/ml/modeling/neural_network/mlp_classifier.py +9 -1
  169. snowflake/ml/modeling/neural_network/mlp_regressor.py +9 -1
  170. snowflake/ml/modeling/preprocessing/polynomial_features.py +9 -1
  171. snowflake/ml/modeling/semi_supervised/label_propagation.py +9 -1
  172. snowflake/ml/modeling/semi_supervised/label_spreading.py +9 -1
  173. snowflake/ml/modeling/svm/linear_svc.py +9 -1
  174. snowflake/ml/modeling/svm/linear_svr.py +9 -1
  175. snowflake/ml/modeling/svm/nu_svc.py +9 -1
  176. snowflake/ml/modeling/svm/nu_svr.py +9 -1
  177. snowflake/ml/modeling/svm/svc.py +9 -1
  178. snowflake/ml/modeling/svm/svr.py +9 -1
  179. snowflake/ml/modeling/tree/decision_tree_classifier.py +9 -1
  180. snowflake/ml/modeling/tree/decision_tree_regressor.py +9 -1
  181. snowflake/ml/modeling/tree/extra_tree_classifier.py +9 -1
  182. snowflake/ml/modeling/tree/extra_tree_regressor.py +9 -1
  183. snowflake/ml/modeling/xgboost/xgb_classifier.py +9 -1
  184. snowflake/ml/modeling/xgboost/xgb_regressor.py +9 -1
  185. snowflake/ml/modeling/xgboost/xgbrf_classifier.py +9 -1
  186. snowflake/ml/modeling/xgboost/xgbrf_regressor.py +9 -1
  187. snowflake/ml/monitoring/explain_visualize.py +424 -0
  188. snowflake/ml/registry/_manager/model_manager.py +23 -2
  189. snowflake/ml/registry/registry.py +10 -9
  190. snowflake/ml/utils/connection_params.py +8 -2
  191. snowflake/ml/version.py +1 -1
  192. {snowflake_ml_python-1.8.3.dist-info → snowflake_ml_python-1.8.5.dist-info}/METADATA +58 -8
  193. {snowflake_ml_python-1.8.3.dist-info → snowflake_ml_python-1.8.5.dist-info}/RECORD +196 -195
  194. {snowflake_ml_python-1.8.3.dist-info → snowflake_ml_python-1.8.5.dist-info}/WHEEL +1 -1
  195. {snowflake_ml_python-1.8.3.dist-info → snowflake_ml_python-1.8.5.dist-info}/licenses/LICENSE.txt +0 -0
  196. {snowflake_ml_python-1.8.3.dist-info → snowflake_ml_python-1.8.5.dist-info}/top_level.txt +0 -0
@@ -1,5 +1,6 @@
1
+ import json
1
2
  import pathlib
2
- from typing import Any, Optional, Union, overload
3
+ from typing import Any, Optional, Union
3
4
 
4
5
  import yaml
5
6
 
@@ -18,126 +19,89 @@ class ModelDeploymentSpec:
18
19
 
19
20
  def __init__(self, workspace_path: Optional[pathlib.Path] = None) -> None:
20
21
  self.workspace_path = workspace_path
22
+ self._models: list[model_deployment_spec_schema.Model] = []
23
+ self._image_build: Optional[model_deployment_spec_schema.ImageBuild] = None
24
+ self._service: Optional[model_deployment_spec_schema.Service] = None
25
+ self._job: Optional[model_deployment_spec_schema.Job] = None
26
+ self._model_loggings: Optional[list[model_deployment_spec_schema.ModelLogging]] = None
27
+ self._inference_spec: dict[str, Any] = {} # Common inference spec for service/job
21
28
 
22
- @overload
23
- def save(
24
- self,
25
- *,
26
- database_name: sql_identifier.SqlIdentifier,
27
- schema_name: sql_identifier.SqlIdentifier,
28
- model_name: sql_identifier.SqlIdentifier,
29
- version_name: sql_identifier.SqlIdentifier,
30
- service_database_name: Optional[sql_identifier.SqlIdentifier] = None,
31
- service_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
32
- service_name: sql_identifier.SqlIdentifier,
33
- inference_compute_pool_name: sql_identifier.SqlIdentifier,
34
- image_build_compute_pool_name: sql_identifier.SqlIdentifier,
35
- image_repo_database_name: Optional[sql_identifier.SqlIdentifier],
36
- image_repo_schema_name: Optional[sql_identifier.SqlIdentifier],
37
- image_repo_name: sql_identifier.SqlIdentifier,
38
- cpu: Optional[str],
39
- memory: Optional[str],
40
- gpu: Optional[Union[str, int]],
41
- num_workers: Optional[int],
42
- max_batch_rows: Optional[int],
43
- force_rebuild: bool,
44
- external_access_integrations: Optional[list[sql_identifier.SqlIdentifier]],
45
- # service spec
46
- ingress_enabled: bool,
47
- max_instances: int,
48
- ) -> str:
49
- ...
50
-
51
- @overload
52
- def save(
53
- self,
54
- *,
55
- database_name: sql_identifier.SqlIdentifier,
56
- schema_name: sql_identifier.SqlIdentifier,
57
- model_name: sql_identifier.SqlIdentifier,
58
- version_name: sql_identifier.SqlIdentifier,
59
- job_database_name: Optional[sql_identifier.SqlIdentifier] = None,
60
- job_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
61
- job_name: sql_identifier.SqlIdentifier,
62
- inference_compute_pool_name: sql_identifier.SqlIdentifier,
63
- image_build_compute_pool_name: sql_identifier.SqlIdentifier,
64
- image_repo_database_name: Optional[sql_identifier.SqlIdentifier],
65
- image_repo_schema_name: Optional[sql_identifier.SqlIdentifier],
66
- image_repo_name: sql_identifier.SqlIdentifier,
67
- cpu: Optional[str],
68
- memory: Optional[str],
69
- gpu: Optional[Union[str, int]],
70
- num_workers: Optional[int],
71
- max_batch_rows: Optional[int],
72
- force_rebuild: bool,
73
- external_access_integrations: Optional[list[sql_identifier.SqlIdentifier]],
74
- # job spec
75
- warehouse: sql_identifier.SqlIdentifier,
76
- target_method: str,
77
- input_table_database_name: Optional[sql_identifier.SqlIdentifier] = None,
78
- input_table_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
79
- input_table_name: sql_identifier.SqlIdentifier,
80
- output_table_database_name: Optional[sql_identifier.SqlIdentifier] = None,
81
- output_table_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
82
- output_table_name: sql_identifier.SqlIdentifier,
83
- ) -> str:
84
- ...
29
+ self.database: Optional[sql_identifier.SqlIdentifier] = None
30
+ self.schema: Optional[sql_identifier.SqlIdentifier] = None
85
31
 
86
- def save(
32
+ def clear(self) -> None:
33
+ """Reset the deployment spec to its initial state."""
34
+ self._models = []
35
+ self._image_build = None
36
+ self._service = None
37
+ self._job = None
38
+ self._model_loggings = None
39
+ self._inference_spec = {}
40
+ self.database = None
41
+ self.schema = None
42
+
43
+ def add_model_spec(
87
44
  self,
88
- *,
89
45
  database_name: sql_identifier.SqlIdentifier,
90
46
  schema_name: sql_identifier.SqlIdentifier,
91
47
  model_name: sql_identifier.SqlIdentifier,
92
48
  version_name: sql_identifier.SqlIdentifier,
93
- service_database_name: Optional[sql_identifier.SqlIdentifier] = None,
94
- service_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
95
- service_name: Optional[sql_identifier.SqlIdentifier] = None,
96
- job_database_name: Optional[sql_identifier.SqlIdentifier] = None,
97
- job_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
98
- job_name: Optional[sql_identifier.SqlIdentifier] = None,
99
- inference_compute_pool_name: sql_identifier.SqlIdentifier,
100
- image_build_compute_pool_name: sql_identifier.SqlIdentifier,
101
- image_repo_database_name: Optional[sql_identifier.SqlIdentifier],
102
- image_repo_schema_name: Optional[sql_identifier.SqlIdentifier],
103
- image_repo_name: sql_identifier.SqlIdentifier,
104
- cpu: Optional[str],
105
- memory: Optional[str],
106
- gpu: Optional[Union[str, int]],
107
- num_workers: Optional[int],
108
- max_batch_rows: Optional[int],
109
- force_rebuild: bool,
110
- external_access_integrations: Optional[list[sql_identifier.SqlIdentifier]],
111
- # service spec
112
- ingress_enabled: Optional[bool] = None,
113
- max_instances: Optional[int] = None,
114
- # job spec
115
- warehouse: Optional[sql_identifier.SqlIdentifier] = None,
116
- target_method: Optional[str] = None,
117
- input_table_database_name: Optional[sql_identifier.SqlIdentifier] = None,
118
- input_table_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
119
- input_table_name: Optional[sql_identifier.SqlIdentifier] = None,
120
- output_table_database_name: Optional[sql_identifier.SqlIdentifier] = None,
121
- output_table_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
122
- output_table_name: Optional[sql_identifier.SqlIdentifier] = None,
123
- ) -> str:
124
- # create the deployment spec
125
- # models spec
49
+ ) -> "ModelDeploymentSpec":
50
+ """Add model specification to the deployment spec.
51
+
52
+ Args:
53
+ database_name: Database name containing the model.
54
+ schema_name: Schema name containing the model.
55
+ model_name: Name of the model.
56
+ version_name: Version of the model.
57
+
58
+ Returns:
59
+ Self for chaining.
60
+ """
126
61
  fq_model_name = identifier.get_schema_level_object_identifier(
127
62
  database_name.identifier(), schema_name.identifier(), model_name.identifier()
128
63
  )
64
+ if not self.database:
65
+ self.database = database_name
66
+ if not self.schema:
67
+ self.schema = schema_name
129
68
  model = model_deployment_spec_schema.Model(name=fq_model_name, version=version_name.identifier())
69
+ self._models.append(model)
70
+ return self
71
+
72
+ def add_image_build_spec(
73
+ self,
74
+ image_build_compute_pool_name: sql_identifier.SqlIdentifier,
75
+ image_repo_name: sql_identifier.SqlIdentifier,
76
+ image_repo_database_name: Optional[sql_identifier.SqlIdentifier] = None,
77
+ image_repo_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
78
+ force_rebuild: bool = False,
79
+ external_access_integrations: Optional[list[sql_identifier.SqlIdentifier]] = None,
80
+ ) -> "ModelDeploymentSpec":
81
+ """Add image build specification to the deployment spec.
82
+
83
+ Args:
84
+ image_build_compute_pool_name: Compute pool for image building.
85
+ image_repo_name: Name of the image repository.
86
+ image_repo_database_name: Database name for the image repository.
87
+ image_repo_schema_name: Schema name for the image repository.
88
+ force_rebuild: Whether to force rebuilding the image.
89
+ external_access_integrations: List of external access integrations.
130
90
 
131
- # image_build spec
132
- saved_image_repo_database = image_repo_database_name or database_name
133
- saved_image_repo_schema = image_repo_schema_name or schema_name
91
+ Returns:
92
+ Self for chaining.
93
+ """
94
+ saved_image_repo_database = image_repo_database_name or self.database
95
+ saved_image_repo_schema = image_repo_schema_name or self.schema
96
+ assert saved_image_repo_database is not None
97
+ assert saved_image_repo_schema is not None
134
98
  fq_image_repo_name = identifier.get_schema_level_object_identifier(
135
99
  db=saved_image_repo_database.identifier(),
136
100
  schema=saved_image_repo_schema.identifier(),
137
101
  object_name=image_repo_name.identifier(),
138
102
  )
139
103
 
140
- image_build = model_deployment_spec_schema.ImageBuild(
104
+ self._image_build = model_deployment_spec_schema.ImageBuild(
141
105
  compute_pool=image_build_compute_pool_name.identifier(),
142
106
  image_repo=fq_image_repo_name,
143
107
  force_rebuild=force_rebuild,
@@ -145,96 +109,313 @@ class ModelDeploymentSpec:
145
109
  [eai.identifier() for eai in external_access_integrations] if external_access_integrations else None
146
110
  ),
147
111
  )
112
+ return self
148
113
 
149
- # universal base inference spec in service and job
150
- base_inference_spec: dict[str, Any] = {}
114
+ def _add_inference_spec(
115
+ self,
116
+ cpu: Optional[str],
117
+ memory: Optional[str],
118
+ gpu: Optional[Union[str, int]],
119
+ num_workers: Optional[int],
120
+ max_batch_rows: Optional[int],
121
+ ) -> None:
122
+ """Internal helper to store common inference specs."""
151
123
  if cpu:
152
- base_inference_spec["cpu"] = cpu
124
+ self._inference_spec["cpu"] = cpu
153
125
  if memory:
154
- base_inference_spec["memory"] = memory
126
+ self._inference_spec["memory"] = memory
155
127
  if gpu:
156
128
  if isinstance(gpu, int):
157
129
  gpu_str = str(gpu)
158
130
  else:
159
131
  gpu_str = gpu
160
- base_inference_spec["gpu"] = gpu_str
132
+ self._inference_spec["gpu"] = gpu_str
161
133
  if num_workers:
162
- base_inference_spec["num_workers"] = num_workers
134
+ self._inference_spec["num_workers"] = num_workers
163
135
  if max_batch_rows:
164
- base_inference_spec["max_batch_rows"] = max_batch_rows
165
-
166
- if service_name: # service spec
167
- assert ingress_enabled, "ingress_enabled is required for service spec"
168
- assert max_instances, "max_instances is required for service spec"
169
- saved_service_database = service_database_name or database_name
170
- saved_service_schema = service_schema_name or schema_name
171
- fq_service_name = identifier.get_schema_level_object_identifier(
172
- saved_service_database.identifier(), saved_service_schema.identifier(), service_name.identifier()
136
+ self._inference_spec["max_batch_rows"] = max_batch_rows
137
+
138
+ def add_service_spec(
139
+ self,
140
+ service_name: sql_identifier.SqlIdentifier,
141
+ inference_compute_pool_name: sql_identifier.SqlIdentifier,
142
+ service_database_name: Optional[sql_identifier.SqlIdentifier] = None,
143
+ service_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
144
+ ingress_enabled: bool = True,
145
+ max_instances: int = 1,
146
+ cpu: Optional[str] = None,
147
+ memory: Optional[str] = None,
148
+ gpu: Optional[Union[str, int]] = None,
149
+ num_workers: Optional[int] = None,
150
+ max_batch_rows: Optional[int] = None,
151
+ ) -> "ModelDeploymentSpec":
152
+ """Add service specification to the deployment spec.
153
+
154
+ Args:
155
+ service_name: Name of the service.
156
+ inference_compute_pool_name: Compute pool for inference.
157
+ service_database_name: Database name for the service.
158
+ service_schema_name: Schema name for the service.
159
+ ingress_enabled: Whether ingress is enabled.
160
+ max_instances: Maximum number of service instances.
161
+ cpu: CPU requirement.
162
+ memory: Memory requirement.
163
+ gpu: GPU requirement.
164
+ num_workers: Number of workers.
165
+ max_batch_rows: Maximum batch rows for inference.
166
+
167
+ Raises:
168
+ ValueError: If a job spec already exists.
169
+
170
+ Returns:
171
+ Self for chaining.
172
+ """
173
+ if self._job:
174
+ raise ValueError("Cannot add a service spec when a job spec already exists.")
175
+
176
+ saved_service_database = service_database_name or self.database
177
+ saved_service_schema = service_schema_name or self.schema
178
+ assert saved_service_database is not None
179
+ assert saved_service_schema is not None
180
+ fq_service_name = identifier.get_schema_level_object_identifier(
181
+ saved_service_database.identifier(), saved_service_schema.identifier(), service_name.identifier()
182
+ )
183
+
184
+ self._add_inference_spec(cpu, memory, gpu, num_workers, max_batch_rows)
185
+
186
+ self._service = model_deployment_spec_schema.Service(
187
+ name=fq_service_name,
188
+ compute_pool=inference_compute_pool_name.identifier(),
189
+ ingress_enabled=ingress_enabled,
190
+ max_instances=max_instances,
191
+ **self._inference_spec,
192
+ )
193
+ return self
194
+
195
+ def add_job_spec(
196
+ self,
197
+ job_name: sql_identifier.SqlIdentifier,
198
+ inference_compute_pool_name: sql_identifier.SqlIdentifier,
199
+ warehouse: sql_identifier.SqlIdentifier,
200
+ target_method: str,
201
+ input_table_name: sql_identifier.SqlIdentifier,
202
+ output_table_name: sql_identifier.SqlIdentifier,
203
+ job_database_name: Optional[sql_identifier.SqlIdentifier] = None,
204
+ job_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
205
+ input_table_database_name: Optional[sql_identifier.SqlIdentifier] = None,
206
+ input_table_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
207
+ output_table_database_name: Optional[sql_identifier.SqlIdentifier] = None,
208
+ output_table_schema_name: Optional[sql_identifier.SqlIdentifier] = None,
209
+ cpu: Optional[str] = None,
210
+ memory: Optional[str] = None,
211
+ gpu: Optional[Union[str, int]] = None,
212
+ num_workers: Optional[int] = None,
213
+ max_batch_rows: Optional[int] = None,
214
+ ) -> "ModelDeploymentSpec":
215
+ """Add job specification to the deployment spec.
216
+
217
+ Args:
218
+ job_name: Name of the job.
219
+ inference_compute_pool_name: Compute pool for inference.
220
+ job_database_name: Database name for the job.
221
+ job_schema_name: Schema name for the job.
222
+ warehouse: Warehouse for the job.
223
+ target_method: Target method for inference.
224
+ input_table_name: Input table name.
225
+ output_table_name: Output table name.
226
+ input_table_database_name: Database for input table.
227
+ input_table_schema_name: Schema for input table.
228
+ output_table_database_name: Database for output table.
229
+ output_table_schema_name: Schema for output table.
230
+ cpu: CPU requirement.
231
+ memory: Memory requirement.
232
+ gpu: GPU requirement.
233
+ num_workers: Number of workers.
234
+ max_batch_rows: Maximum batch rows for inference.
235
+
236
+ Raises:
237
+ ValueError: If a service spec already exists.
238
+
239
+ Returns:
240
+ Self for chaining.
241
+ """
242
+ if self._service:
243
+ raise ValueError("Cannot add a job spec when a service spec already exists.")
244
+
245
+ saved_job_database = job_database_name or self.database
246
+ saved_job_schema = job_schema_name or self.schema
247
+ input_table_database_name = input_table_database_name or self.database
248
+ input_table_schema_name = input_table_schema_name or self.schema
249
+ output_table_database_name = output_table_database_name or self.database
250
+ output_table_schema_name = output_table_schema_name or self.schema
251
+
252
+ assert saved_job_database is not None
253
+ assert saved_job_schema is not None
254
+ assert input_table_database_name is not None
255
+ assert input_table_schema_name is not None
256
+ assert output_table_database_name is not None
257
+ assert output_table_schema_name is not None
258
+
259
+ fq_job_name = identifier.get_schema_level_object_identifier(
260
+ saved_job_database.identifier(), saved_job_schema.identifier(), job_name.identifier()
261
+ )
262
+ fq_input_table_name = identifier.get_schema_level_object_identifier(
263
+ input_table_database_name.identifier(),
264
+ input_table_schema_name.identifier(),
265
+ input_table_name.identifier(),
266
+ )
267
+ fq_output_table_name = identifier.get_schema_level_object_identifier(
268
+ output_table_database_name.identifier(),
269
+ output_table_schema_name.identifier(),
270
+ output_table_name.identifier(),
271
+ )
272
+
273
+ self._add_inference_spec(cpu, memory, gpu, num_workers, max_batch_rows)
274
+
275
+ self._job = model_deployment_spec_schema.Job(
276
+ name=fq_job_name,
277
+ compute_pool=inference_compute_pool_name.identifier(),
278
+ warehouse=warehouse.identifier(),
279
+ target_method=target_method,
280
+ input_table_name=fq_input_table_name,
281
+ output_table_name=fq_output_table_name,
282
+ **self._inference_spec,
283
+ )
284
+ return self
285
+
286
+ def add_hf_logger_spec(
287
+ self,
288
+ hf_model_name: str,
289
+ hf_task: Optional[str] = None,
290
+ hf_token: Optional[str] = None,
291
+ hf_tokenizer: Optional[str] = None,
292
+ hf_revision: Optional[str] = None,
293
+ hf_trust_remote_code: Optional[bool] = False,
294
+ pip_requirements: Optional[list[str]] = None,
295
+ conda_dependencies: Optional[list[str]] = None,
296
+ target_platforms: Optional[list[str]] = None,
297
+ comment: Optional[str] = None,
298
+ warehouse: Optional[str] = None,
299
+ **kwargs: Any,
300
+ ) -> "ModelDeploymentSpec":
301
+ """Add Hugging Face logger specification.
302
+
303
+ Args:
304
+ hf_model_name: Hugging Face model name.
305
+ hf_task: Hugging Face task.
306
+ hf_token: Hugging Face token.
307
+ hf_tokenizer: Hugging Face tokenizer.
308
+ hf_revision: Hugging Face model revision.
309
+ hf_trust_remote_code: Whether to trust remote code.
310
+ pip_requirements: List of pip requirements.
311
+ conda_dependencies: List of conda dependencies.
312
+ target_platforms: List of target platforms.
313
+ comment: Comment for the model.
314
+ warehouse: Warehouse used to log the model.
315
+ **kwargs: Additional Hugging Face model arguments.
316
+
317
+ Raises:
318
+ ValueError: If Hugging Face model name is missing when other HF parameters are provided.
319
+
320
+ Returns:
321
+ Self for chaining.
322
+ """
323
+ # Validation moved here from save
324
+ if (
325
+ any(
326
+ [
327
+ hf_task,
328
+ hf_token,
329
+ hf_tokenizer,
330
+ hf_revision,
331
+ hf_trust_remote_code,
332
+ pip_requirements,
333
+ ]
173
334
  )
174
- service = model_deployment_spec_schema.Service(
175
- name=fq_service_name,
176
- compute_pool=inference_compute_pool_name.identifier(),
177
- ingress_enabled=ingress_enabled,
178
- max_instances=max_instances,
179
- **base_inference_spec,
335
+ and not hf_model_name
336
+ ):
337
+ # This condition might be redundant now as hf_model_name is mandatory
338
+ raise ValueError("Hugging Face model name is required when using Hugging Face model deployment.")
339
+
340
+ log_model_args = model_deployment_spec_schema.LogModelArgs(
341
+ pip_requirements=pip_requirements,
342
+ conda_dependencies=conda_dependencies,
343
+ target_platforms=target_platforms,
344
+ comment=comment,
345
+ warehouse=warehouse,
346
+ )
347
+ hf_model = model_deployment_spec_schema.HuggingFaceModel(
348
+ hf_model_name=hf_model_name,
349
+ task=hf_task,
350
+ hf_token=hf_token,
351
+ tokenizer=hf_tokenizer,
352
+ trust_remote_code=hf_trust_remote_code,
353
+ revision=hf_revision,
354
+ hf_model_kwargs=json.dumps(kwargs),
355
+ )
356
+ model_logging = model_deployment_spec_schema.ModelLogging(
357
+ log_model_args=log_model_args,
358
+ hf_model=hf_model,
359
+ )
360
+ if self._model_loggings is None:
361
+ self._model_loggings = [model_logging]
362
+ else:
363
+ self._model_loggings.append(model_logging)
364
+ return self
365
+
366
+ def save(self) -> str:
367
+ """Constructs the final deployment spec from added components and saves it.
368
+
369
+ Raises:
370
+ ValueError: If required components are missing or conflicting specs are added.
371
+ RuntimeError: If no service or job spec is found despite validation.
372
+
373
+ Returns:
374
+ The path to the saved YAML file as a string, or the YAML content as a string
375
+ if workspace_path was not provided.
376
+ """
377
+ # Validations
378
+ if not self._models:
379
+ raise ValueError("Model specification is required. Call add_model_spec().")
380
+ if not self._image_build:
381
+ raise ValueError("Image build specification is required. Call add_image_build_spec().")
382
+ if not self._service and not self._job:
383
+ raise ValueError(
384
+ "Either service or job specification is required. Call add_service_spec() or add_job_spec()."
180
385
  )
386
+ if self._service and self._job:
387
+ # This case should be prevented by checks in add_service_spec/add_job_spec, but double-check
388
+ raise ValueError("Cannot have both service and job specifications.")
181
389
 
182
- # model deployment spec
390
+ # Construct the final spec object
391
+ if self._service:
183
392
  model_deployment_spec: Union[
184
393
  model_deployment_spec_schema.ModelServiceDeploymentSpec,
185
394
  model_deployment_spec_schema.ModelJobDeploymentSpec,
186
395
  ] = model_deployment_spec_schema.ModelServiceDeploymentSpec(
187
- models=[model],
188
- image_build=image_build,
189
- service=service,
190
- )
191
- else: # job spec
192
- assert job_name, "job_name is required for job spec"
193
- assert warehouse, "warehouse is required for job spec"
194
- assert target_method, "target_method is required for job spec"
195
- assert input_table_name, "input_table_name is required for job spec"
196
- assert output_table_name, "output_table_name is required for job spec"
197
- saved_job_database = job_database_name or database_name
198
- saved_job_schema = job_schema_name or schema_name
199
- input_table_database_name = input_table_database_name or database_name
200
- input_table_schema_name = input_table_schema_name or schema_name
201
- output_table_database_name = output_table_database_name or database_name
202
- output_table_schema_name = output_table_schema_name or schema_name
203
- fq_job_name = identifier.get_schema_level_object_identifier(
204
- saved_job_database.identifier(), saved_job_schema.identifier(), job_name.identifier()
205
- )
206
- fq_input_table_name = identifier.get_schema_level_object_identifier(
207
- input_table_database_name.identifier(),
208
- input_table_schema_name.identifier(),
209
- input_table_name.identifier(),
396
+ models=self._models,
397
+ image_build=self._image_build,
398
+ service=self._service,
399
+ model_loggings=self._model_loggings,
210
400
  )
211
- fq_output_table_name = identifier.get_schema_level_object_identifier(
212
- output_table_database_name.identifier(),
213
- output_table_schema_name.identifier(),
214
- output_table_name.identifier(),
215
- )
216
- job = model_deployment_spec_schema.Job(
217
- name=fq_job_name,
218
- compute_pool=inference_compute_pool_name.identifier(),
219
- warehouse=warehouse.identifier(),
220
- target_method=target_method,
221
- input_table_name=fq_input_table_name,
222
- output_table_name=fq_output_table_name,
223
- **base_inference_spec,
224
- )
225
-
226
- # model deployment spec
401
+ elif self._job:
227
402
  model_deployment_spec = model_deployment_spec_schema.ModelJobDeploymentSpec(
228
- models=[model],
229
- image_build=image_build,
230
- job=job,
403
+ models=self._models,
404
+ image_build=self._image_build,
405
+ job=self._job,
406
+ model_loggings=self._model_loggings,
231
407
  )
408
+ else:
409
+ # Should not happen due to earlier validation
410
+ raise RuntimeError("Internal error: No service or job spec found despite validation.")
411
+
412
+ # Serialize and save/return
413
+ yaml_content = model_deployment_spec.model_dump(exclude_none=True)
232
414
 
233
415
  if self.workspace_path is None:
234
- return yaml.safe_dump(model_deployment_spec.model_dump(exclude_none=True))
416
+ return yaml.safe_dump(yaml_content)
235
417
 
236
- # save the yaml
237
418
  file_path = self.workspace_path / self.DEPLOY_SPEC_FILE_REL_PATH
238
419
  with file_path.open("w", encoding="utf-8") as f:
239
- yaml.safe_dump(model_deployment_spec.model_dump(exclude_none=True), f)
420
+ yaml.safe_dump(yaml_content, f)
240
421
  return str(file_path.resolve())
@@ -41,13 +41,38 @@ class Job(BaseModel):
41
41
  output_table_name: str
42
42
 
43
43
 
44
+ class LogModelArgs(BaseModel):
45
+ pip_requirements: Optional[list[str]] = None
46
+ conda_dependencies: Optional[list[str]] = None
47
+ target_platforms: Optional[list[str]] = None
48
+ comment: Optional[str] = None
49
+ warehouse: Optional[str] = None
50
+
51
+
52
+ class HuggingFaceModel(BaseModel):
53
+ hf_model_name: str
54
+ task: Optional[str] = None
55
+ tokenizer: Optional[str] = None
56
+ hf_token: Optional[str] = None
57
+ trust_remote_code: Optional[bool] = False
58
+ revision: Optional[str] = None
59
+ hf_model_kwargs: Optional[str] = "{}"
60
+
61
+
62
+ class ModelLogging(BaseModel):
63
+ log_model_args: Optional[LogModelArgs] = None
64
+ hf_model: Optional[HuggingFaceModel] = None
65
+
66
+
44
67
  class ModelServiceDeploymentSpec(BaseModel):
45
68
  models: list[Model]
46
69
  image_build: ImageBuild
47
70
  service: Service
71
+ model_loggings: Optional[list[ModelLogging]] = None
48
72
 
49
73
 
50
74
  class ModelJobDeploymentSpec(BaseModel):
51
75
  models: list[Model]
52
76
  image_build: ImageBuild
53
77
  job: Job
78
+ model_loggings: Optional[list[ModelLogging]] = None
@@ -293,7 +293,7 @@ class ModelVersionSQLClient(_base._BaseSQLClient):
293
293
  if snowpark_utils.is_in_stored_procedure(): # type: ignore[no-untyped-call]
294
294
  options = {"parallel": 10}
295
295
  cursor = self._session._conn._cursor
296
- cursor._download(stage_location_url, str(target_path), options) # type: ignore[union-attr]
296
+ cursor._download(stage_location_url, str(target_path), options)
297
297
  cursor.fetchall()
298
298
  else:
299
299
  query_result_checker.SqlResultValidator(