snowflake-ml-python 1.10.0__py3-none-any.whl → 1.12.0__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 (205) hide show
  1. snowflake/cortex/_complete.py +3 -2
  2. snowflake/ml/_internal/utils/service_logger.py +26 -1
  3. snowflake/ml/experiment/_client/artifact.py +76 -0
  4. snowflake/ml/experiment/_client/experiment_tracking_sql_client.py +64 -1
  5. snowflake/ml/experiment/callback/keras.py +63 -0
  6. snowflake/ml/experiment/callback/lightgbm.py +5 -1
  7. snowflake/ml/experiment/callback/xgboost.py +5 -1
  8. snowflake/ml/experiment/experiment_tracking.py +89 -4
  9. snowflake/ml/feature_store/feature_store.py +1150 -131
  10. snowflake/ml/feature_store/feature_view.py +122 -0
  11. snowflake/ml/jobs/_utils/__init__.py +0 -0
  12. snowflake/ml/jobs/_utils/constants.py +9 -14
  13. snowflake/ml/jobs/_utils/feature_flags.py +16 -0
  14. snowflake/ml/jobs/_utils/payload_utils.py +61 -19
  15. snowflake/ml/jobs/_utils/query_helper.py +5 -1
  16. snowflake/ml/jobs/_utils/runtime_env_utils.py +63 -0
  17. snowflake/ml/jobs/_utils/scripts/get_instance_ip.py +18 -7
  18. snowflake/ml/jobs/_utils/scripts/mljob_launcher.py +15 -7
  19. snowflake/ml/jobs/_utils/spec_utils.py +44 -13
  20. snowflake/ml/jobs/_utils/stage_utils.py +22 -9
  21. snowflake/ml/jobs/_utils/types.py +7 -8
  22. snowflake/ml/jobs/job.py +34 -18
  23. snowflake/ml/jobs/manager.py +107 -24
  24. snowflake/ml/model/__init__.py +6 -1
  25. snowflake/ml/model/_client/model/batch_inference_specs.py +27 -0
  26. snowflake/ml/model/_client/model/model_version_impl.py +225 -73
  27. snowflake/ml/model/_client/ops/service_ops.py +128 -174
  28. snowflake/ml/model/_client/service/model_deployment_spec.py +123 -64
  29. snowflake/ml/model/_client/service/model_deployment_spec_schema.py +25 -9
  30. snowflake/ml/model/_model_composer/model_composer.py +1 -70
  31. snowflake/ml/model/_model_composer/model_manifest/model_manifest.py +2 -43
  32. snowflake/ml/model/_packager/model_handlers/huggingface_pipeline.py +207 -2
  33. snowflake/ml/model/_packager/model_handlers/sklearn.py +3 -1
  34. snowflake/ml/model/_packager/model_runtime/_snowml_inference_alternative_requirements.py +3 -3
  35. snowflake/ml/model/_signatures/snowpark_handler.py +1 -1
  36. snowflake/ml/model/_signatures/utils.py +4 -2
  37. snowflake/ml/model/inference_engine.py +5 -0
  38. snowflake/ml/model/models/huggingface_pipeline.py +4 -3
  39. snowflake/ml/model/openai_signatures.py +57 -0
  40. snowflake/ml/modeling/_internal/estimator_utils.py +43 -1
  41. snowflake/ml/modeling/_internal/local_implementations/pandas_trainer.py +14 -3
  42. snowflake/ml/modeling/_internal/snowpark_implementations/snowpark_trainer.py +17 -6
  43. snowflake/ml/modeling/calibration/calibrated_classifier_cv.py +1 -1
  44. snowflake/ml/modeling/cluster/affinity_propagation.py +1 -1
  45. snowflake/ml/modeling/cluster/agglomerative_clustering.py +1 -1
  46. snowflake/ml/modeling/cluster/birch.py +1 -1
  47. snowflake/ml/modeling/cluster/bisecting_k_means.py +1 -1
  48. snowflake/ml/modeling/cluster/dbscan.py +1 -1
  49. snowflake/ml/modeling/cluster/feature_agglomeration.py +1 -1
  50. snowflake/ml/modeling/cluster/k_means.py +1 -1
  51. snowflake/ml/modeling/cluster/mean_shift.py +1 -1
  52. snowflake/ml/modeling/cluster/mini_batch_k_means.py +1 -1
  53. snowflake/ml/modeling/cluster/optics.py +1 -1
  54. snowflake/ml/modeling/cluster/spectral_biclustering.py +1 -1
  55. snowflake/ml/modeling/cluster/spectral_clustering.py +1 -1
  56. snowflake/ml/modeling/cluster/spectral_coclustering.py +1 -1
  57. snowflake/ml/modeling/compose/column_transformer.py +1 -1
  58. snowflake/ml/modeling/compose/transformed_target_regressor.py +1 -1
  59. snowflake/ml/modeling/covariance/elliptic_envelope.py +1 -1
  60. snowflake/ml/modeling/covariance/empirical_covariance.py +1 -1
  61. snowflake/ml/modeling/covariance/graphical_lasso.py +1 -1
  62. snowflake/ml/modeling/covariance/graphical_lasso_cv.py +1 -1
  63. snowflake/ml/modeling/covariance/ledoit_wolf.py +1 -1
  64. snowflake/ml/modeling/covariance/min_cov_det.py +1 -1
  65. snowflake/ml/modeling/covariance/oas.py +1 -1
  66. snowflake/ml/modeling/covariance/shrunk_covariance.py +1 -1
  67. snowflake/ml/modeling/decomposition/dictionary_learning.py +1 -1
  68. snowflake/ml/modeling/decomposition/factor_analysis.py +1 -1
  69. snowflake/ml/modeling/decomposition/fast_ica.py +1 -1
  70. snowflake/ml/modeling/decomposition/incremental_pca.py +1 -1
  71. snowflake/ml/modeling/decomposition/kernel_pca.py +1 -1
  72. snowflake/ml/modeling/decomposition/mini_batch_dictionary_learning.py +1 -1
  73. snowflake/ml/modeling/decomposition/mini_batch_sparse_pca.py +1 -1
  74. snowflake/ml/modeling/decomposition/pca.py +1 -1
  75. snowflake/ml/modeling/decomposition/sparse_pca.py +1 -1
  76. snowflake/ml/modeling/decomposition/truncated_svd.py +1 -1
  77. snowflake/ml/modeling/discriminant_analysis/linear_discriminant_analysis.py +1 -1
  78. snowflake/ml/modeling/discriminant_analysis/quadratic_discriminant_analysis.py +1 -1
  79. snowflake/ml/modeling/ensemble/ada_boost_classifier.py +1 -1
  80. snowflake/ml/modeling/ensemble/ada_boost_regressor.py +1 -1
  81. snowflake/ml/modeling/ensemble/bagging_classifier.py +1 -1
  82. snowflake/ml/modeling/ensemble/bagging_regressor.py +1 -1
  83. snowflake/ml/modeling/ensemble/extra_trees_classifier.py +1 -1
  84. snowflake/ml/modeling/ensemble/extra_trees_regressor.py +1 -1
  85. snowflake/ml/modeling/ensemble/gradient_boosting_classifier.py +1 -1
  86. snowflake/ml/modeling/ensemble/gradient_boosting_regressor.py +1 -1
  87. snowflake/ml/modeling/ensemble/hist_gradient_boosting_classifier.py +1 -1
  88. snowflake/ml/modeling/ensemble/hist_gradient_boosting_regressor.py +1 -1
  89. snowflake/ml/modeling/ensemble/isolation_forest.py +1 -1
  90. snowflake/ml/modeling/ensemble/random_forest_classifier.py +1 -1
  91. snowflake/ml/modeling/ensemble/random_forest_regressor.py +1 -1
  92. snowflake/ml/modeling/ensemble/stacking_regressor.py +1 -1
  93. snowflake/ml/modeling/ensemble/voting_classifier.py +1 -1
  94. snowflake/ml/modeling/ensemble/voting_regressor.py +1 -1
  95. snowflake/ml/modeling/feature_selection/generic_univariate_select.py +1 -1
  96. snowflake/ml/modeling/feature_selection/select_fdr.py +1 -1
  97. snowflake/ml/modeling/feature_selection/select_fpr.py +1 -1
  98. snowflake/ml/modeling/feature_selection/select_fwe.py +1 -1
  99. snowflake/ml/modeling/feature_selection/select_k_best.py +1 -1
  100. snowflake/ml/modeling/feature_selection/select_percentile.py +1 -1
  101. snowflake/ml/modeling/feature_selection/sequential_feature_selector.py +1 -1
  102. snowflake/ml/modeling/feature_selection/variance_threshold.py +1 -1
  103. snowflake/ml/modeling/gaussian_process/gaussian_process_classifier.py +1 -1
  104. snowflake/ml/modeling/gaussian_process/gaussian_process_regressor.py +1 -1
  105. snowflake/ml/modeling/impute/iterative_imputer.py +1 -1
  106. snowflake/ml/modeling/impute/knn_imputer.py +1 -1
  107. snowflake/ml/modeling/impute/missing_indicator.py +1 -1
  108. snowflake/ml/modeling/kernel_approximation/additive_chi2_sampler.py +1 -1
  109. snowflake/ml/modeling/kernel_approximation/nystroem.py +1 -1
  110. snowflake/ml/modeling/kernel_approximation/polynomial_count_sketch.py +1 -1
  111. snowflake/ml/modeling/kernel_approximation/rbf_sampler.py +1 -1
  112. snowflake/ml/modeling/kernel_approximation/skewed_chi2_sampler.py +1 -1
  113. snowflake/ml/modeling/kernel_ridge/kernel_ridge.py +1 -1
  114. snowflake/ml/modeling/lightgbm/lgbm_classifier.py +1 -1
  115. snowflake/ml/modeling/lightgbm/lgbm_regressor.py +1 -1
  116. snowflake/ml/modeling/linear_model/ard_regression.py +1 -1
  117. snowflake/ml/modeling/linear_model/bayesian_ridge.py +1 -1
  118. snowflake/ml/modeling/linear_model/elastic_net.py +1 -1
  119. snowflake/ml/modeling/linear_model/elastic_net_cv.py +1 -1
  120. snowflake/ml/modeling/linear_model/gamma_regressor.py +1 -1
  121. snowflake/ml/modeling/linear_model/huber_regressor.py +1 -1
  122. snowflake/ml/modeling/linear_model/lars.py +1 -1
  123. snowflake/ml/modeling/linear_model/lars_cv.py +1 -1
  124. snowflake/ml/modeling/linear_model/lasso.py +1 -1
  125. snowflake/ml/modeling/linear_model/lasso_cv.py +1 -1
  126. snowflake/ml/modeling/linear_model/lasso_lars.py +1 -1
  127. snowflake/ml/modeling/linear_model/lasso_lars_cv.py +1 -1
  128. snowflake/ml/modeling/linear_model/lasso_lars_ic.py +1 -1
  129. snowflake/ml/modeling/linear_model/linear_regression.py +1 -1
  130. snowflake/ml/modeling/linear_model/logistic_regression.py +1 -1
  131. snowflake/ml/modeling/linear_model/logistic_regression_cv.py +1 -1
  132. snowflake/ml/modeling/linear_model/multi_task_elastic_net.py +1 -1
  133. snowflake/ml/modeling/linear_model/multi_task_elastic_net_cv.py +1 -1
  134. snowflake/ml/modeling/linear_model/multi_task_lasso.py +1 -1
  135. snowflake/ml/modeling/linear_model/multi_task_lasso_cv.py +1 -1
  136. snowflake/ml/modeling/linear_model/orthogonal_matching_pursuit.py +1 -1
  137. snowflake/ml/modeling/linear_model/passive_aggressive_classifier.py +1 -1
  138. snowflake/ml/modeling/linear_model/passive_aggressive_regressor.py +1 -1
  139. snowflake/ml/modeling/linear_model/perceptron.py +1 -1
  140. snowflake/ml/modeling/linear_model/poisson_regressor.py +1 -1
  141. snowflake/ml/modeling/linear_model/ransac_regressor.py +1 -1
  142. snowflake/ml/modeling/linear_model/ridge.py +1 -1
  143. snowflake/ml/modeling/linear_model/ridge_classifier.py +1 -1
  144. snowflake/ml/modeling/linear_model/ridge_classifier_cv.py +1 -1
  145. snowflake/ml/modeling/linear_model/ridge_cv.py +1 -1
  146. snowflake/ml/modeling/linear_model/sgd_classifier.py +1 -1
  147. snowflake/ml/modeling/linear_model/sgd_one_class_svm.py +1 -1
  148. snowflake/ml/modeling/linear_model/sgd_regressor.py +1 -1
  149. snowflake/ml/modeling/linear_model/theil_sen_regressor.py +1 -1
  150. snowflake/ml/modeling/linear_model/tweedie_regressor.py +1 -1
  151. snowflake/ml/modeling/manifold/isomap.py +1 -1
  152. snowflake/ml/modeling/manifold/mds.py +1 -1
  153. snowflake/ml/modeling/manifold/spectral_embedding.py +1 -1
  154. snowflake/ml/modeling/manifold/tsne.py +1 -1
  155. snowflake/ml/modeling/mixture/bayesian_gaussian_mixture.py +1 -1
  156. snowflake/ml/modeling/mixture/gaussian_mixture.py +1 -1
  157. snowflake/ml/modeling/multiclass/one_vs_one_classifier.py +1 -1
  158. snowflake/ml/modeling/multiclass/one_vs_rest_classifier.py +1 -1
  159. snowflake/ml/modeling/multiclass/output_code_classifier.py +1 -1
  160. snowflake/ml/modeling/naive_bayes/bernoulli_nb.py +1 -1
  161. snowflake/ml/modeling/naive_bayes/categorical_nb.py +1 -1
  162. snowflake/ml/modeling/naive_bayes/complement_nb.py +1 -1
  163. snowflake/ml/modeling/naive_bayes/gaussian_nb.py +1 -1
  164. snowflake/ml/modeling/naive_bayes/multinomial_nb.py +1 -1
  165. snowflake/ml/modeling/neighbors/k_neighbors_classifier.py +1 -1
  166. snowflake/ml/modeling/neighbors/k_neighbors_regressor.py +1 -1
  167. snowflake/ml/modeling/neighbors/kernel_density.py +1 -1
  168. snowflake/ml/modeling/neighbors/local_outlier_factor.py +1 -1
  169. snowflake/ml/modeling/neighbors/nearest_centroid.py +1 -1
  170. snowflake/ml/modeling/neighbors/nearest_neighbors.py +1 -1
  171. snowflake/ml/modeling/neighbors/neighborhood_components_analysis.py +1 -1
  172. snowflake/ml/modeling/neighbors/radius_neighbors_classifier.py +1 -1
  173. snowflake/ml/modeling/neighbors/radius_neighbors_regressor.py +1 -1
  174. snowflake/ml/modeling/neural_network/bernoulli_rbm.py +1 -1
  175. snowflake/ml/modeling/neural_network/mlp_classifier.py +1 -1
  176. snowflake/ml/modeling/neural_network/mlp_regressor.py +1 -1
  177. snowflake/ml/modeling/preprocessing/polynomial_features.py +1 -1
  178. snowflake/ml/modeling/semi_supervised/label_propagation.py +1 -1
  179. snowflake/ml/modeling/semi_supervised/label_spreading.py +1 -1
  180. snowflake/ml/modeling/svm/linear_svc.py +1 -1
  181. snowflake/ml/modeling/svm/linear_svr.py +1 -1
  182. snowflake/ml/modeling/svm/nu_svc.py +1 -1
  183. snowflake/ml/modeling/svm/nu_svr.py +1 -1
  184. snowflake/ml/modeling/svm/svc.py +1 -1
  185. snowflake/ml/modeling/svm/svr.py +1 -1
  186. snowflake/ml/modeling/tree/decision_tree_classifier.py +1 -1
  187. snowflake/ml/modeling/tree/decision_tree_regressor.py +1 -1
  188. snowflake/ml/modeling/tree/extra_tree_classifier.py +1 -1
  189. snowflake/ml/modeling/tree/extra_tree_regressor.py +1 -1
  190. snowflake/ml/modeling/xgboost/xgb_classifier.py +1 -1
  191. snowflake/ml/modeling/xgboost/xgb_regressor.py +1 -1
  192. snowflake/ml/modeling/xgboost/xgbrf_classifier.py +1 -1
  193. snowflake/ml/modeling/xgboost/xgbrf_regressor.py +1 -1
  194. snowflake/ml/monitoring/_client/model_monitor_sql_client.py +91 -6
  195. snowflake/ml/monitoring/_manager/model_monitor_manager.py +3 -0
  196. snowflake/ml/monitoring/entities/model_monitor_config.py +3 -0
  197. snowflake/ml/monitoring/model_monitor.py +26 -0
  198. snowflake/ml/registry/_manager/model_manager.py +7 -35
  199. snowflake/ml/registry/_manager/model_parameter_reconciler.py +194 -5
  200. snowflake/ml/version.py +1 -1
  201. {snowflake_ml_python-1.10.0.dist-info → snowflake_ml_python-1.12.0.dist-info}/METADATA +87 -7
  202. {snowflake_ml_python-1.10.0.dist-info → snowflake_ml_python-1.12.0.dist-info}/RECORD +205 -197
  203. {snowflake_ml_python-1.10.0.dist-info → snowflake_ml_python-1.12.0.dist-info}/WHEEL +0 -0
  204. {snowflake_ml_python-1.10.0.dist-info → snowflake_ml_python-1.12.0.dist-info}/licenses/LICENSE.txt +0 -0
  205. {snowflake_ml_python-1.10.0.dist-info → snowflake_ml_python-1.12.0.dist-info}/top_level.txt +0 -0
@@ -88,7 +88,7 @@ class MidStreamException(Exception):
88
88
  message = reason
89
89
  if http_resp:
90
90
  message = f"Error in stream (HTTP Response: {http_resp.status}) - {http_resp.reason}"
91
- if request_id != "":
91
+ if request_id is not None and request_id != "":
92
92
  # add request_id to error message
93
93
  message += f" (Request ID: {request_id})"
94
94
  super().__init__(message)
@@ -327,7 +327,8 @@ def _return_stream_response(
327
327
  # This is the case of midstream errors which were introduced specifically for structured output.
328
328
  # TODO: discuss during code review
329
329
  if parsed_resp.get("error"):
330
- raise MidStreamException(reason=response.text, request_id=request_id)
330
+ error_info = parsed_resp["error"]
331
+ raise MidStreamException(reason=str(error_info), request_id=request_id)
331
332
  else:
332
333
  pass
333
334
 
@@ -9,8 +9,33 @@ from typing import Optional
9
9
 
10
10
  import platformdirs
11
11
 
12
+
13
+ class ProgressBarAwareConsoleHandler(logging.StreamHandler): # type: ignore[type-arg]
14
+ """A logging handler that adapts to different progress bar systems to avoid interfering with display."""
15
+
16
+ def emit(self, record: logging.LogRecord) -> None:
17
+ try:
18
+ msg = self.format(record)
19
+
20
+ # Check if tqdm progress bars are active - use tqdm.write() to avoid interference
21
+ try:
22
+ import tqdm
23
+
24
+ if hasattr(tqdm.tqdm, "_instances") and tqdm.tqdm._instances:
25
+ tqdm.tqdm.write(msg, file=self.stream)
26
+ return
27
+ except (ImportError, AttributeError):
28
+ pass
29
+
30
+ # Fallback to regular stream writing (works for all contexts including Streamlit)
31
+ self.stream.write(msg + "\n")
32
+ self.flush()
33
+ except Exception:
34
+ self.handleError(record)
35
+
36
+
12
37
  # Module-level logger for operational messages that should appear on console
13
- stdout_handler = logging.StreamHandler(sys.stdout)
38
+ stdout_handler = ProgressBarAwareConsoleHandler(sys.stdout)
14
39
  stdout_handler.setFormatter(logging.Formatter("%(message)s"))
15
40
 
16
41
  console_logger = logging.getLogger(__name__)
@@ -0,0 +1,76 @@
1
+ import os
2
+ import posixpath
3
+ from typing import NamedTuple
4
+
5
+
6
+ class ArtifactInfo(NamedTuple):
7
+ name: str
8
+ size: int
9
+ md5: str
10
+ last_modified: str
11
+
12
+
13
+ def get_put_path_pairs(local_path: str, artifact_path: str) -> list[tuple[str, str]]:
14
+ """Enumerate files to upload and their destination subdirectories.
15
+
16
+ Expands a local path (file or directory) into a list of pairs used for uploading
17
+ artifacts to the stage.
18
+
19
+ Args:
20
+ local_path: Absolute or relative path to a local file or directory to upload.
21
+ artifact_path: Destination subdirectory under the run's artifact root in the
22
+ stage. If empty, files are uploaded to the root. When uploading a
23
+ directory, this value is prepended to each file's relative path.
24
+
25
+ Returns:
26
+ A list of tuples ``(local_file_path, destination_artifact_subdir)``. For a
27
+ single file, the list contains one entry with ``destination_artifact_subdir``
28
+ set to ``artifact_path``. For directories, one entry per file is produced and
29
+ subdirectories are preserved using POSIX separators.
30
+
31
+ Raises:
32
+ FileNotFoundError: If ``local_path`` does not exist.
33
+ """
34
+ if not os.path.exists(local_path):
35
+ raise FileNotFoundError(f"Local path does not exist: {local_path}")
36
+
37
+ if os.path.isfile(local_path):
38
+ return [(local_path, artifact_path)]
39
+
40
+ pairs: list[tuple[str, str]] = []
41
+ base_dir = local_path
42
+ for root, _, files in os.walk(base_dir):
43
+ if not files:
44
+ continue
45
+ rel_dir = os.path.relpath(root, base_dir)
46
+ rel_dir_posix = "" if rel_dir in (".", "") else rel_dir.replace(os.sep, "/")
47
+ dest_artifact_path = posixpath.join(artifact_path, rel_dir_posix) if rel_dir_posix else artifact_path
48
+ for filename in files:
49
+ file_path = os.path.join(root, filename)
50
+ pairs.append((file_path, dest_artifact_path))
51
+ return pairs
52
+
53
+
54
+ def get_download_path_pairs(artifacts: list[ArtifactInfo], base_target_dir: str) -> list[tuple[str, str]]:
55
+ """Given artifact metadata entries, computes where each artifact should be written
56
+ locally.
57
+
58
+ Args:
59
+ artifacts: List of artifact metadata where ``name`` is a POSIX-style path
60
+ relative to the run's artifact root (e.g., ``"file.txt"`` or
61
+ ``"nested/dir/file.txt"``).
62
+ base_target_dir: Local base directory to download into. If empty, the
63
+ current working directory is used. Directories will be created as
64
+ needed.
65
+
66
+ Returns:
67
+ A list of tuples ``(artifact_relative_path, local_directory)``. Each tuple
68
+ provides the relative path to request from the stage and the local directory
69
+ where the file should be written.
70
+ """
71
+ planned: list[tuple[str, str]] = []
72
+ for info in artifacts:
73
+ rel_dir = os.path.dirname(info.name)
74
+ local_dir = base_target_dir if rel_dir in ("", ".") else os.path.join(base_target_dir, rel_dir)
75
+ planned.append((info.name, local_dir))
76
+ return planned
@@ -1,9 +1,10 @@
1
1
  from typing import Optional
2
2
 
3
3
  from snowflake.ml._internal.utils import query_result_checker, sql_identifier
4
+ from snowflake.ml.experiment._client import artifact
4
5
  from snowflake.ml.model._client.sql import _base
5
6
  from snowflake.ml.utils import sql_client
6
- from snowflake.snowpark import row, session
7
+ from snowflake.snowpark import file_operation, row, session
7
8
 
8
9
 
9
10
  class ExperimentTrackingSQLClient(_base._BaseSQLClient):
@@ -88,6 +89,59 @@ class ExperimentTrackingSQLClient(_base._BaseSQLClient):
88
89
  f"ALTER EXPERIMENT {experiment_fqn} MODIFY RUN {run_name} SET METADATA=$${run_metadata}$$",
89
90
  ).has_dimensions(expected_rows=1, expected_cols=1).validate()
90
91
 
92
+ def put_artifact(
93
+ self,
94
+ *,
95
+ experiment_name: sql_identifier.SqlIdentifier,
96
+ run_name: sql_identifier.SqlIdentifier,
97
+ artifact_path: str,
98
+ file_path: str,
99
+ auto_compress: bool = False,
100
+ ) -> file_operation.PutResult:
101
+ return self._session.file.put(
102
+ local_file_name=file_path,
103
+ stage_location=self._build_snow_uri(experiment_name, run_name, artifact_path),
104
+ overwrite=True,
105
+ auto_compress=auto_compress,
106
+ )[0]
107
+
108
+ def list_artifacts(
109
+ self,
110
+ *,
111
+ experiment_name: sql_identifier.SqlIdentifier,
112
+ run_name: sql_identifier.SqlIdentifier,
113
+ artifact_path: str,
114
+ ) -> list[artifact.ArtifactInfo]:
115
+ results = (
116
+ query_result_checker.SqlResultValidator(
117
+ self._session, f"LIST {self._build_snow_uri(experiment_name, run_name, artifact_path)}"
118
+ )
119
+ .has_dimensions(expected_cols=4)
120
+ .validate()
121
+ )
122
+ return [
123
+ artifact.ArtifactInfo(
124
+ name=str(result.name).removeprefix(f"/versions/{run_name}/"),
125
+ size=result.size,
126
+ md5=result.md5,
127
+ last_modified=result.last_modified,
128
+ )
129
+ for result in results
130
+ ]
131
+
132
+ def get_artifact(
133
+ self,
134
+ *,
135
+ experiment_name: sql_identifier.SqlIdentifier,
136
+ run_name: sql_identifier.SqlIdentifier,
137
+ artifact_path: str,
138
+ target_path: str,
139
+ ) -> file_operation.GetResult:
140
+ return self._session.file.get(
141
+ stage_location=self._build_snow_uri(experiment_name, run_name, artifact_path),
142
+ target_directory=target_path,
143
+ )[0]
144
+
91
145
  def show_runs_in_experiment(
92
146
  self, *, experiment_name: sql_identifier.SqlIdentifier, like: Optional[str] = None
93
147
  ) -> list[row.Row]:
@@ -96,3 +150,12 @@ class ExperimentTrackingSQLClient(_base._BaseSQLClient):
96
150
  return query_result_checker.SqlResultValidator(
97
151
  self._session, f"SHOW RUNS {like_clause} IN EXPERIMENT {experiment_fqn}"
98
152
  ).validate()
153
+
154
+ def _build_snow_uri(
155
+ self, experiment_name: sql_identifier.SqlIdentifier, run_name: sql_identifier.SqlIdentifier, artifact_path: str
156
+ ) -> str:
157
+ experiment_fqn = self.fully_qualified_object_name(self._database_name, self._schema_name, experiment_name)
158
+ uri = f"snow://experiment/{experiment_fqn}/versions/{run_name}"
159
+ if artifact_path:
160
+ uri += f"/{artifact_path}"
161
+ return uri
@@ -0,0 +1,63 @@
1
+ import json
2
+ from typing import TYPE_CHECKING, Any, Optional
3
+ from warnings import warn
4
+
5
+ import keras
6
+
7
+ from snowflake.ml.experiment import utils
8
+
9
+ if TYPE_CHECKING:
10
+ from snowflake.ml.experiment.experiment_tracking import ExperimentTracking
11
+ from snowflake.ml.model.model_signature import ModelSignature
12
+
13
+
14
+ class SnowflakeKerasCallback(keras.callbacks.Callback):
15
+ def __init__(
16
+ self,
17
+ experiment_tracking: "ExperimentTracking",
18
+ log_model: bool = True,
19
+ log_metrics: bool = True,
20
+ log_params: bool = True,
21
+ log_every_n_epochs: int = 1,
22
+ model_name: Optional[str] = None,
23
+ model_signature: Optional["ModelSignature"] = None,
24
+ ) -> None:
25
+ self._experiment_tracking = experiment_tracking
26
+ self.log_model = log_model
27
+ self.log_metrics = log_metrics
28
+ self.log_params = log_params
29
+ if log_every_n_epochs < 1:
30
+ raise ValueError("`log_every_n_epochs` must be positive.")
31
+ self.log_every_n_epochs = log_every_n_epochs
32
+ self.model_name = model_name
33
+ self.model_signature = model_signature
34
+
35
+ def on_train_begin(self, logs: Optional[dict[str, Any]] = None) -> None:
36
+ if self.log_params:
37
+ params = json.loads(self.model.to_json())
38
+ self._experiment_tracking.log_params(utils.flatten_nested_params(params))
39
+
40
+ def on_epoch_end(self, epoch: int, logs: Optional[dict[str, Any]] = None) -> None:
41
+ if self.log_metrics and logs and epoch % self.log_every_n_epochs == 0:
42
+ for key, value in logs.items():
43
+ try:
44
+ value = float(value)
45
+ except Exception:
46
+ pass
47
+ else:
48
+ self._experiment_tracking.log_metric(key=key, value=value, step=epoch)
49
+
50
+ def on_train_end(self, logs: Optional[dict[str, Any]] = None) -> None:
51
+ if self.log_model:
52
+ if not self.model_signature:
53
+ warn(
54
+ "Model will not be logged because model signature is missing. "
55
+ "To autolog the model, please specify `model_signature` when constructing SnowflakeKerasCallback."
56
+ )
57
+ return
58
+ model_name = self.model_name or self._experiment_tracking._get_or_set_experiment().name + "_model"
59
+ self._experiment_tracking.log_model( # type: ignore[call-arg]
60
+ model=self.model,
61
+ model_name=model_name,
62
+ signatures={"predict": self.model_signature},
63
+ )
@@ -15,6 +15,7 @@ class SnowflakeLightgbmCallback(lgb.callback._RecordEvaluationCallback):
15
15
  log_model: bool = True,
16
16
  log_metrics: bool = True,
17
17
  log_params: bool = True,
18
+ log_every_n_epochs: int = 1,
18
19
  model_name: Optional[str] = None,
19
20
  model_signature: Optional["ModelSignature"] = None,
20
21
  ) -> None:
@@ -22,6 +23,9 @@ class SnowflakeLightgbmCallback(lgb.callback._RecordEvaluationCallback):
22
23
  self.log_model = log_model
23
24
  self.log_metrics = log_metrics
24
25
  self.log_params = log_params
26
+ if log_every_n_epochs < 1:
27
+ raise ValueError("`log_every_n_epochs` must be positive.")
28
+ self.log_every_n_epochs = log_every_n_epochs
25
29
  self.model_name = model_name
26
30
  self.model_signature = model_signature
27
31
 
@@ -32,7 +36,7 @@ class SnowflakeLightgbmCallback(lgb.callback._RecordEvaluationCallback):
32
36
  if env.iteration == env.begin_iteration: # Log params only at the first iteration
33
37
  self._experiment_tracking.log_params(env.params)
34
38
 
35
- if self.log_metrics:
39
+ if self.log_metrics and env.iteration % self.log_every_n_epochs == 0:
36
40
  super().__call__(env)
37
41
  for dataset_name, metrics in self.eval_result.items():
38
42
  for metric_name, log in metrics.items():
@@ -18,6 +18,7 @@ class SnowflakeXgboostCallback(xgb.callback.TrainingCallback):
18
18
  log_model: bool = True,
19
19
  log_metrics: bool = True,
20
20
  log_params: bool = True,
21
+ log_every_n_epochs: int = 1,
21
22
  model_name: Optional[str] = None,
22
23
  model_signature: Optional["ModelSignature"] = None,
23
24
  ) -> None:
@@ -25,6 +26,9 @@ class SnowflakeXgboostCallback(xgb.callback.TrainingCallback):
25
26
  self.log_model = log_model
26
27
  self.log_metrics = log_metrics
27
28
  self.log_params = log_params
29
+ if log_every_n_epochs < 1:
30
+ raise ValueError("`log_every_n_epochs` must be positive.")
31
+ self.log_every_n_epochs = log_every_n_epochs
28
32
  self.model_name = model_name
29
33
  self.model_signature = model_signature
30
34
 
@@ -36,7 +40,7 @@ class SnowflakeXgboostCallback(xgb.callback.TrainingCallback):
36
40
  return model
37
41
 
38
42
  def after_iteration(self, model: Any, epoch: int, evals_log: dict[str, dict[str, Any]]) -> bool:
39
- if self.log_metrics:
43
+ if self.log_metrics and epoch % self.log_every_n_epochs == 0:
40
44
  for dataset_name, metrics in evals_log.items():
41
45
  for metric_name, log in metrics.items():
42
46
  metric_key = dataset_name + ":" + metric_name
@@ -5,14 +5,17 @@ from typing import Any, Optional, Union
5
5
  from urllib.parse import quote
6
6
 
7
7
  import snowflake.snowpark._internal.utils as snowpark_utils
8
- from snowflake.ml import model, registry
8
+ from snowflake.ml import model as ml_model, registry
9
9
  from snowflake.ml._internal.human_readable_id import hrid_generator
10
10
  from snowflake.ml._internal.utils import sql_identifier
11
11
  from snowflake.ml.experiment import (
12
12
  _entities as entities,
13
13
  _experiment_info as experiment_info,
14
14
  )
15
- from snowflake.ml.experiment._client import experiment_tracking_sql_client as sql_client
15
+ from snowflake.ml.experiment._client import (
16
+ artifact,
17
+ experiment_tracking_sql_client as sql_client,
18
+ )
16
19
  from snowflake.ml.model import type_hints
17
20
  from snowflake.ml.utils import sql_client as sql_client_utils
18
21
  from snowflake.snowpark import session
@@ -118,11 +121,11 @@ class ExperimentTracking:
118
121
  @functools.wraps(registry.Registry.log_model)
119
122
  def log_model(
120
123
  self,
121
- model: Union[type_hints.SupportedModelType, model.ModelVersion],
124
+ model: Union[type_hints.SupportedModelType, ml_model.ModelVersion],
122
125
  *,
123
126
  model_name: str,
124
127
  **kwargs: Any,
125
- ) -> model.ModelVersion:
128
+ ) -> ml_model.ModelVersion:
126
129
  run = self._get_or_start_run()
127
130
  with experiment_info.ExperimentInfoPatcher(experiment_info=run._get_experiment_info()):
128
131
  return self._registry.log_model(model, model_name=model_name, **kwargs)
@@ -280,6 +283,88 @@ class ExperimentTracking:
280
283
  run_metadata=json.dumps(metadata.to_dict()),
281
284
  )
282
285
 
286
+ def log_artifact(
287
+ self,
288
+ local_path: str,
289
+ artifact_path: Optional[str] = None,
290
+ ) -> None:
291
+ """
292
+ Log an artifact or a directory of artifacts under the current run. If no run is active, this method will create
293
+ a new run.
294
+
295
+ Args:
296
+ local_path: The path to the local file or directory to write.
297
+ artifact_path: The directory within the run directory to write the artifacts to. If None, the artifacts will
298
+ be logged in the root directory of the run.
299
+ """
300
+ run = self._get_or_start_run()
301
+ for file_path, dest_artifact_path in artifact.get_put_path_pairs(local_path, artifact_path or ""):
302
+ self._sql_client.put_artifact(
303
+ experiment_name=run.experiment_name,
304
+ run_name=run.name,
305
+ artifact_path=dest_artifact_path,
306
+ file_path=file_path,
307
+ )
308
+
309
+ def list_artifacts(
310
+ self,
311
+ run_name: str,
312
+ artifact_path: Optional[str] = None,
313
+ ) -> list[artifact.ArtifactInfo]:
314
+ """
315
+ List artifacts for a given run within the current experiment.
316
+
317
+ Args:
318
+ run_name: Name of the run to list artifacts from.
319
+ artifact_path: Optional subdirectory within the run's artifact directory to scope the listing.
320
+ If None, lists from the root of the run's artifact directory.
321
+
322
+ Returns:
323
+ A list of artifact entries under the specified path.
324
+
325
+ Raises:
326
+ RuntimeError: If no experiment is currently set.
327
+ """
328
+ if not self._experiment:
329
+ raise RuntimeError("No experiment set. Please set an experiment before listing artifacts.")
330
+
331
+ return self._sql_client.list_artifacts(
332
+ experiment_name=self._experiment.name,
333
+ run_name=sql_identifier.SqlIdentifier(run_name),
334
+ artifact_path=artifact_path or "",
335
+ )
336
+
337
+ def download_artifacts(
338
+ self,
339
+ run_name: str,
340
+ artifact_path: Optional[str] = None,
341
+ target_path: Optional[str] = None,
342
+ ) -> None:
343
+ """
344
+ Download artifacts from a run to a local directory.
345
+
346
+ Args:
347
+ run_name: Name of the run to download artifacts from.
348
+ artifact_path: Optional path to file or subdirectory within the run's artifact directory to download.
349
+ If None, downloads all artifacts from the root of the run's artifact directory.
350
+ target_path: Optional local directory to download files into. If None, downloads into the
351
+ current working directory.
352
+
353
+ Raises:
354
+ RuntimeError: If no experiment is currently set.
355
+ """
356
+ if not self._experiment:
357
+ raise RuntimeError("No experiment set. Please set an experiment before downloading artifacts.")
358
+
359
+ artifacts = self.list_artifacts(run_name=run_name, artifact_path=artifact_path or "")
360
+ for relative_path, local_dir in artifact.get_download_path_pairs(artifacts, target_path or ""):
361
+ self._sql_client.get_artifact(
362
+ experiment_name=self._experiment.name,
363
+ run_name=sql_identifier.SqlIdentifier(run_name),
364
+ artifact_path=relative_path,
365
+ target_path=local_dir,
366
+ )
367
+
283
368
  def _get_or_set_experiment(self) -> entities.Experiment:
284
369
  if self._experiment:
285
370
  return self._experiment