snowflake-ml-python 1.7.2__py3-none-any.whl → 1.7.3__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.
- snowflake/cortex/__init__.py +16 -8
- snowflake/cortex/_classify_text.py +12 -1
- snowflake/cortex/_complete.py +82 -13
- snowflake/cortex/_embed_text_1024.py +9 -2
- snowflake/cortex/_embed_text_768.py +9 -2
- snowflake/cortex/_extract_answer.py +9 -2
- snowflake/cortex/_sentiment.py +9 -2
- snowflake/cortex/_summarize.py +9 -2
- snowflake/cortex/_translate.py +9 -2
- snowflake/ml/_internal/env_utils.py +7 -52
- snowflake/ml/_internal/utils/identifier.py +4 -2
- snowflake/ml/data/__init__.py +3 -0
- snowflake/ml/data/_internal/arrow_ingestor.py +4 -4
- snowflake/ml/data/data_connector.py +53 -11
- snowflake/ml/data/data_ingestor.py +2 -1
- snowflake/ml/data/torch_utils.py +18 -5
- snowflake/ml/feature_store/examples/example_helper.py +2 -1
- snowflake/ml/fileset/fileset.py +18 -18
- snowflake/ml/model/_client/model/model_version_impl.py +5 -3
- snowflake/ml/model/_client/ops/model_ops.py +2 -6
- snowflake/ml/model/_client/sql/model_version.py +11 -0
- snowflake/ml/model/_model_composer/model_composer.py +8 -3
- snowflake/ml/model/_model_composer/model_manifest/model_manifest.py +20 -1
- snowflake/ml/model/_model_composer/model_manifest/model_manifest_schema.py +1 -0
- snowflake/ml/model/_model_composer/model_method/constants.py +1 -0
- snowflake/ml/model/_model_composer/model_method/function_generator.py +2 -0
- snowflake/ml/model/_model_composer/model_method/infer_function.py_template +1 -1
- snowflake/ml/model/_model_composer/model_method/infer_partitioned.py_template +1 -1
- snowflake/ml/model/_model_composer/model_method/infer_table_function.py_template +1 -1
- snowflake/ml/model/_model_composer/model_method/model_method.py +9 -1
- snowflake/ml/model/_model_composer/model_user_file/model_user_file.py +27 -0
- snowflake/ml/model/_packager/model_handlers/_utils.py +27 -2
- snowflake/ml/model/_packager/model_handlers/catboost.py +3 -3
- snowflake/ml/model/_packager/model_handlers/huggingface_pipeline.py +5 -1
- snowflake/ml/model/_packager/model_handlers/lightgbm.py +5 -3
- snowflake/ml/model/_packager/model_handlers/sentence_transformers.py +55 -20
- snowflake/ml/model/_packager/model_handlers/sklearn.py +9 -10
- snowflake/ml/model/_packager/model_handlers/snowmlmodel.py +66 -28
- snowflake/ml/model/_packager/model_handlers/tensorflow.py +70 -17
- snowflake/ml/model/_packager/model_handlers/xgboost.py +3 -3
- snowflake/ml/model/_packager/model_meta/model_meta.py +3 -0
- snowflake/ml/model/_packager/model_meta/model_meta_schema.py +6 -1
- snowflake/ml/model/_packager/model_runtime/_snowml_inference_alternative_requirements.py +2 -2
- snowflake/ml/model/_packager/model_task/model_task_utils.py +3 -2
- snowflake/ml/model/_signatures/pandas_handler.py +1 -1
- snowflake/ml/model/_signatures/snowpark_handler.py +8 -2
- snowflake/ml/model/type_hints.py +1 -0
- snowflake/ml/modeling/_internal/model_trainer_builder.py +0 -8
- snowflake/ml/modeling/_internal/model_transformer_builder.py +0 -13
- snowflake/ml/modeling/pipeline/pipeline.py +6 -176
- snowflake/ml/modeling/xgboost/xgb_classifier.py +161 -88
- snowflake/ml/modeling/xgboost/xgb_regressor.py +160 -85
- snowflake/ml/modeling/xgboost/xgbrf_classifier.py +160 -85
- snowflake/ml/modeling/xgboost/xgbrf_regressor.py +160 -85
- snowflake/ml/monitoring/_client/model_monitor_sql_client.py +4 -4
- snowflake/ml/registry/_manager/model_manager.py +70 -33
- snowflake/ml/registry/registry.py +41 -22
- snowflake/ml/version.py +1 -1
- {snowflake_ml_python-1.7.2.dist-info → snowflake_ml_python-1.7.3.dist-info}/METADATA +38 -9
- {snowflake_ml_python-1.7.2.dist-info → snowflake_ml_python-1.7.3.dist-info}/RECORD +63 -67
- {snowflake_ml_python-1.7.2.dist-info → snowflake_ml_python-1.7.3.dist-info}/WHEEL +1 -1
- snowflake/ml/_internal/utils/retryable_http.py +0 -39
- snowflake/ml/fileset/parquet_parser.py +0 -170
- snowflake/ml/fileset/tf_dataset.py +0 -88
- snowflake/ml/fileset/torch_datapipe.py +0 -57
- snowflake/ml/modeling/_internal/ml_runtime_implementations/ml_runtime_handlers.py +0 -151
- snowflake/ml/modeling/_internal/ml_runtime_implementations/ml_runtime_trainer.py +0 -66
- {snowflake_ml_python-1.7.2.dist-info → snowflake_ml_python-1.7.3.dist-info}/LICENSE.txt +0 -0
- {snowflake_ml_python-1.7.2.dist-info → snowflake_ml_python-1.7.3.dist-info}/top_level.txt +0 -0
@@ -125,111 +125,171 @@ class XGBRFRegressor(BaseTransformer):
|
|
125
125
|
can seriously hurt performance in gradient boosting. Set the batch_size as large as possible
|
126
126
|
based on the available memory.
|
127
127
|
|
128
|
-
n_estimators: int
|
128
|
+
n_estimators: Optional[int]
|
129
129
|
Number of trees in random forest to fit.
|
130
130
|
|
131
|
-
max_depth: Optional[int]
|
131
|
+
max_depth: typing.Optional[int]
|
132
|
+
|
132
133
|
Maximum tree depth for base learners.
|
133
|
-
|
134
|
+
|
135
|
+
max_leaves: typing.Optional[int]
|
136
|
+
|
134
137
|
Maximum number of leaves; 0 indicates no limit.
|
135
|
-
|
138
|
+
|
139
|
+
max_bin: typing.Optional[int]
|
140
|
+
|
136
141
|
If using histogram-based algorithm, maximum number of bins per feature
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
142
|
+
|
143
|
+
grow_policy: typing.Optional[str]
|
144
|
+
|
145
|
+
Tree growing policy.
|
146
|
+
|
147
|
+
- depthwise: Favors splitting at nodes closest to the node,
|
148
|
+
- lossguide: Favors splitting at nodes with highest loss change.
|
149
|
+
|
150
|
+
learning_rate: typing.Optional[float]
|
151
|
+
|
141
152
|
Boosting learning rate (xgb's "eta")
|
142
|
-
|
153
|
+
|
154
|
+
verbosity: typing.Optional[int]
|
155
|
+
|
143
156
|
The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
157
|
+
|
158
|
+
objective: typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
159
|
+
|
160
|
+
Specify the learning task and the corresponding learning objective or a custom
|
161
|
+
objective function to be used.
|
162
|
+
|
163
|
+
For custom objective, see :doc:`/tutorials/custom_metric_obj` and
|
164
|
+
:ref:`custom-obj-metric` for more information, along with the end note for
|
165
|
+
function signatures.
|
166
|
+
|
167
|
+
booster: typing.Optional[str]
|
168
|
+
|
169
|
+
Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
170
|
+
|
171
|
+
tree_method: typing.Optional[str]
|
172
|
+
|
150
173
|
Specify which tree method to use. Default to auto. If this parameter is set to
|
151
174
|
default, XGBoost will choose the most conservative option available. It's
|
152
175
|
recommended to study this option from the parameters document :doc:`tree method
|
153
176
|
</treemethod>`
|
154
|
-
|
177
|
+
|
178
|
+
n_jobs: typing.Optional[int]
|
179
|
+
|
155
180
|
Number of parallel threads used to run xgboost. When used with other
|
156
181
|
Scikit-Learn algorithms like grid search, you may choose which algorithm to
|
157
182
|
parallelize and balance the threads. Creating thread contention will
|
158
183
|
significantly slow down both algorithms.
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
184
|
+
|
185
|
+
gamma: typing.Optional[float]
|
186
|
+
|
187
|
+
(min_split_loss) Minimum loss reduction required to make a further partition on
|
188
|
+
a leaf node of the tree.
|
189
|
+
|
190
|
+
min_child_weight: typing.Optional[float]
|
191
|
+
|
163
192
|
Minimum sum of instance weight(hessian) needed in a child.
|
164
|
-
|
193
|
+
|
194
|
+
max_delta_step: typing.Optional[float]
|
195
|
+
|
165
196
|
Maximum delta step we allow each tree's weight estimation to be.
|
166
|
-
|
197
|
+
|
198
|
+
subsample: typing.Optional[float]
|
199
|
+
|
167
200
|
Subsample ratio of the training instance.
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
201
|
+
|
202
|
+
sampling_method: typing.Optional[str]
|
203
|
+
|
204
|
+
Sampling method. Used only by the GPU version of ``hist`` tree method.
|
205
|
+
|
206
|
+
- ``uniform``: Select random training instances uniformly.
|
207
|
+
- ``gradient_based``: Select random training instances with higher probability
|
208
|
+
when the gradient and hessian are larger. (cf. CatBoost)
|
209
|
+
|
210
|
+
colsample_bytree: typing.Optional[float]
|
211
|
+
|
174
212
|
Subsample ratio of columns when constructing each tree.
|
175
|
-
|
213
|
+
|
214
|
+
colsample_bylevel: typing.Optional[float]
|
215
|
+
|
176
216
|
Subsample ratio of columns for each level.
|
177
|
-
|
217
|
+
|
218
|
+
colsample_bynode: typing.Optional[float]
|
219
|
+
|
178
220
|
Subsample ratio of columns for each split.
|
179
|
-
|
221
|
+
|
222
|
+
reg_alpha: typing.Optional[float]
|
223
|
+
|
180
224
|
L1 regularization term on weights (xgb's alpha).
|
181
|
-
|
225
|
+
|
226
|
+
reg_lambda: typing.Optional[float]
|
227
|
+
|
182
228
|
L2 regularization term on weights (xgb's lambda).
|
183
|
-
|
229
|
+
|
230
|
+
scale_pos_weight: typing.Optional[float]
|
184
231
|
Balancing of positive and negative weights.
|
185
|
-
|
232
|
+
|
233
|
+
base_score: typing.Optional[float]
|
234
|
+
|
186
235
|
The initial prediction score of all instances, global bias.
|
187
|
-
|
236
|
+
|
237
|
+
random_state: typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
238
|
+
|
188
239
|
Random number seed.
|
189
240
|
|
190
241
|
Using gblinear booster with shotgun updater is nondeterministic as
|
191
242
|
it uses Hogwild algorithm.
|
192
243
|
|
193
|
-
missing: float
|
194
|
-
|
195
|
-
|
244
|
+
missing: float
|
245
|
+
|
246
|
+
Value in the data which needs to be present as a missing value. Default to
|
247
|
+
:py:data:`numpy.nan`.
|
248
|
+
|
249
|
+
num_parallel_tree: typing.Optional[int]
|
250
|
+
|
196
251
|
Used for boosting random forest.
|
197
|
-
|
252
|
+
|
253
|
+
monotone_constraints: typing.Union[typing.Dict[str, int], str, NoneType]
|
254
|
+
|
198
255
|
Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
|
199
256
|
for more information.
|
200
|
-
|
257
|
+
|
258
|
+
interaction_constraints: typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
259
|
+
|
201
260
|
Constraints for interaction representing permitted interactions. The
|
202
261
|
constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
|
203
262
|
3, 4]]``, where each inner list is a group of indices of features that are
|
204
263
|
allowed to interact with each other. See :doc:`tutorial
|
205
264
|
</tutorials/feature_interaction_constraint>` for more information
|
206
|
-
|
265
|
+
|
266
|
+
importance_type: typing.Optional[str]
|
267
|
+
|
207
268
|
The feature importance type for the feature_importances\_ property:
|
208
269
|
|
209
270
|
* For tree model, it's either "gain", "weight", "cover", "total_gain" or
|
210
271
|
"total_cover".
|
211
|
-
* For linear model, only "weight" is defined and it's the normalized
|
212
|
-
without bias.
|
272
|
+
* For linear model, only "weight" is defined and it's the normalized
|
273
|
+
coefficients without bias.
|
274
|
+
|
275
|
+
device: typing.Optional[str]
|
276
|
+
|
277
|
+
Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
278
|
+
|
279
|
+
validate_parameters: typing.Optional[bool]
|
213
280
|
|
214
|
-
gpu_id: Optional[int]
|
215
|
-
Device ordinal.
|
216
|
-
validate_parameters: Optional[bool]
|
217
281
|
Give warnings for unknown parameter.
|
218
|
-
|
219
|
-
Force XGBoost to use specific predictor, available choices are [cpu_predictor,
|
220
|
-
gpu_predictor].
|
282
|
+
|
221
283
|
enable_categorical: bool
|
222
284
|
|
223
|
-
|
224
|
-
should be used to specify categorical data type. Also, JSON/UBJSON
|
225
|
-
serialization format is required.
|
285
|
+
See the same parameter of :py:class:`DMatrix` for details.
|
226
286
|
|
227
|
-
feature_types:
|
287
|
+
feature_types: typing.Optional[typing.Sequence[str]]
|
228
288
|
|
229
289
|
Used for specifying feature types without constructing a dataframe. See
|
230
290
|
:py:class:`DMatrix` for details.
|
231
291
|
|
232
|
-
max_cat_to_onehot: Optional[int]
|
292
|
+
max_cat_to_onehot: typing.Optional[int]
|
233
293
|
|
234
294
|
A threshold for deciding whether XGBoost should use one-hot encoding based split
|
235
295
|
for categorical data. When number of categories is lesser than the threshold
|
@@ -238,36 +298,41 @@ class XGBRFRegressor(BaseTransformer):
|
|
238
298
|
categorical feature support. See :doc:`Categorical Data
|
239
299
|
</tutorials/categorical>` and :ref:`cat-param` for details.
|
240
300
|
|
241
|
-
max_cat_threshold: Optional[int]
|
301
|
+
max_cat_threshold: typing.Optional[int]
|
242
302
|
|
243
303
|
Maximum number of categories considered for each split. Used only by
|
244
304
|
partition-based splits for preventing over-fitting. Also, `enable_categorical`
|
245
305
|
needs to be set to have categorical feature support. See :doc:`Categorical Data
|
246
306
|
</tutorials/categorical>` and :ref:`cat-param` for details.
|
247
307
|
|
248
|
-
|
308
|
+
multi_strategy: typing.Optional[str]
|
309
|
+
|
310
|
+
The strategy used for training multi-target models, including multi-target
|
311
|
+
regression and multi-class classification. See :doc:`/tutorials/multioutput` for
|
312
|
+
more information.
|
313
|
+
|
314
|
+
- ``one_output_per_tree``: One model for each target.
|
315
|
+
- ``multi_output_tree``: Use multi-target trees.
|
316
|
+
|
317
|
+
eval_metric: typing.Union[str, typing.List[str], typing.Callable, NoneType]
|
249
318
|
|
250
319
|
Metric used for monitoring the training result and early stopping. It can be a
|
251
320
|
string or list of strings as names of predefined metric in XGBoost (See
|
252
|
-
doc/parameter.rst), one of the metrics in :py:mod:`sklearn.metrics`, or any
|
253
|
-
user defined metric that looks like `sklearn.metrics`.
|
321
|
+
doc/parameter.rst), one of the metrics in :py:mod:`sklearn.metrics`, or any
|
322
|
+
other user defined metric that looks like `sklearn.metrics`.
|
254
323
|
|
255
324
|
If custom objective is also provided, then custom metric should implement the
|
256
325
|
corresponding reverse link function.
|
257
326
|
|
258
327
|
Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
|
259
|
-
object is provided, it's assumed to be a cost function and by default XGBoost
|
260
|
-
minimize the result during early stopping.
|
261
|
-
|
262
|
-
For advanced usage on Early stopping like directly choosing to maximize instead of
|
263
|
-
minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
328
|
+
object is provided, it's assumed to be a cost function and by default XGBoost
|
329
|
+
will minimize the result during early stopping.
|
264
330
|
|
265
|
-
|
266
|
-
|
331
|
+
For advanced usage on Early stopping like directly choosing to maximize instead
|
332
|
+
of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
267
333
|
|
268
|
-
|
269
|
-
|
270
|
-
being used.
|
334
|
+
See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
|
335
|
+
information.
|
271
336
|
|
272
337
|
from sklearn.datasets import load_diabetes
|
273
338
|
from sklearn.metrics import mean_absolute_error
|
@@ -278,24 +343,29 @@ class XGBRFRegressor(BaseTransformer):
|
|
278
343
|
)
|
279
344
|
reg.fit(X, y, eval_set=[(X, y)])
|
280
345
|
|
281
|
-
early_stopping_rounds: Optional[int]
|
346
|
+
early_stopping_rounds: typing.Optional[int]
|
282
347
|
|
283
|
-
Activates early stopping. Validation metric needs to improve at least once in
|
284
|
-
|
285
|
-
|
348
|
+
- Activates early stopping. Validation metric needs to improve at least once in
|
349
|
+
every **early_stopping_rounds** round(s) to continue training. Requires at
|
350
|
+
least one item in **eval_set** in :py:meth:`fit`.
|
286
351
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
352
|
+
- If early stopping occurs, the model will have two additional attributes:
|
353
|
+
:py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
|
354
|
+
:py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
|
355
|
+
number of trees during inference. If users want to access the full model
|
356
|
+
(including trees built after early stopping), they can specify the
|
357
|
+
`iteration_range` in these inference methods. In addition, other utilities
|
358
|
+
like model plotting can also use the entire model.
|
291
359
|
|
292
|
-
If
|
293
|
-
|
294
|
-
:py:attr:`best_ntree_limit`.
|
360
|
+
- If you prefer to discard the trees after `best_iteration`, consider using the
|
361
|
+
callback function :py:class:`xgboost.callback.EarlyStopping`.
|
295
362
|
|
296
|
-
|
363
|
+
- If there's more than one item in **eval_set**, the last entry will be used for
|
364
|
+
early stopping. If there's more than one metric in **eval_metric**, the last
|
365
|
+
metric will be used for early stopping.
|
366
|
+
|
367
|
+
callbacks: typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
297
368
|
|
298
|
-
callbacks: Optional[List[TrainingCallback]]
|
299
369
|
List of callback functions that are applied at end of each iteration.
|
300
370
|
It is possible to use predefined callbacks by using
|
301
371
|
:ref:`Callback API <callback_api>`.
|
@@ -307,9 +377,11 @@ class XGBRFRegressor(BaseTransformer):
|
|
307
377
|
for params in parameters_grid:
|
308
378
|
# be sure to (re)initialize the callbacks before each run
|
309
379
|
callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
|
310
|
-
xgboost.
|
380
|
+
reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
|
381
|
+
reg.fit(X, y)
|
382
|
+
|
383
|
+
kwargs: typing.Optional[typing.Any]
|
311
384
|
|
312
|
-
kwargs: dict, optional
|
313
385
|
Keyword arguments for XGBoost Booster object. Full documentation of parameters
|
314
386
|
can be found :doc:`here </parameter>`.
|
315
387
|
Attempting to set a parameter via the constructor args and \*\*kwargs
|
@@ -320,13 +392,16 @@ class XGBRFRegressor(BaseTransformer):
|
|
320
392
|
with scikit-learn.
|
321
393
|
|
322
394
|
A custom objective function can be provided for the ``objective``
|
323
|
-
parameter. In this case, it should have the signature
|
324
|
-
``objective(y_true, y_pred
|
395
|
+
parameter. In this case, it should have the signature ``objective(y_true,
|
396
|
+
y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
|
397
|
+
-> [grad, hess]``:
|
325
398
|
|
326
399
|
y_true: array_like of shape [n_samples]
|
327
400
|
The target values
|
328
401
|
y_pred: array_like of shape [n_samples]
|
329
402
|
The predicted values
|
403
|
+
sample_weight :
|
404
|
+
Optional sample weights.
|
330
405
|
|
331
406
|
grad: array_like of shape [n_samples]
|
332
407
|
The value of the gradient for each sample point.
|
@@ -42,7 +42,7 @@ class ModelMonitorSQLClient:
|
|
42
42
|
def _infer_qualified_schema(
|
43
43
|
self, database_name: Optional[sql_identifier.SqlIdentifier], schema_name: Optional[sql_identifier.SqlIdentifier]
|
44
44
|
) -> str:
|
45
|
-
return f"{database_name or self._database_name}.{schema_name or self._schema_name}"
|
45
|
+
return f"""{database_name or self._database_name}.{schema_name or self._schema_name}"""
|
46
46
|
|
47
47
|
def create_model_monitor(
|
48
48
|
self,
|
@@ -74,17 +74,17 @@ class ModelMonitorSQLClient:
|
|
74
74
|
) -> None:
|
75
75
|
baseline_sql = ""
|
76
76
|
if baseline:
|
77
|
-
baseline_sql = f"BASELINE=
|
77
|
+
baseline_sql = f"""BASELINE={self._infer_qualified_schema(baseline_database, baseline_schema)}.{baseline}"""
|
78
78
|
query_result_checker.SqlResultValidator(
|
79
79
|
self._sql_client._session,
|
80
80
|
f"""
|
81
81
|
CREATE MODEL MONITOR {self._infer_qualified_schema(monitor_database, monitor_schema)}.{monitor_name}
|
82
82
|
WITH
|
83
|
-
MODEL=
|
83
|
+
MODEL={self._infer_qualified_schema(model_database, model_schema)}.{model_name}
|
84
84
|
VERSION='{version_name}'
|
85
85
|
FUNCTION='{function_name}'
|
86
86
|
WAREHOUSE='{warehouse_name}'
|
87
|
-
SOURCE=
|
87
|
+
SOURCE={self._infer_qualified_schema(source_database, source_schema)}.{source}
|
88
88
|
ID_COLUMNS={_build_sql_list_from_columns(id_columns)}
|
89
89
|
PREDICTION_SCORE_COLUMNS={_build_sql_list_from_columns(prediction_score_columns)}
|
90
90
|
PREDICTION_CLASS_COLUMNS={_build_sql_list_from_columns(prediction_class_columns)}
|
@@ -1,13 +1,13 @@
|
|
1
1
|
from types import ModuleType
|
2
|
-
from typing import Any, Dict, List, Optional, Union
|
2
|
+
from typing import Any, Dict, List, Optional, Tuple, Union
|
3
3
|
|
4
4
|
import pandas as pd
|
5
5
|
from absl.logging import logging
|
6
|
-
from packaging import version
|
7
6
|
|
8
7
|
from snowflake.ml._internal import telemetry
|
8
|
+
from snowflake.ml._internal.exceptions import error_codes, exceptions
|
9
9
|
from snowflake.ml._internal.human_readable_id import hrid_generator
|
10
|
-
from snowflake.ml._internal.utils import
|
10
|
+
from snowflake.ml._internal.utils import sql_identifier
|
11
11
|
from snowflake.ml.model import model_signature, type_hints as model_types
|
12
12
|
from snowflake.ml.model._client.model import model_impl, model_version_impl
|
13
13
|
from snowflake.ml.model._client.ops import metadata_ops, model_ops, service_ops
|
@@ -50,14 +50,40 @@ class ModelManager:
|
|
50
50
|
python_version: Optional[str] = None,
|
51
51
|
signatures: Optional[Dict[str, model_signature.ModelSignature]] = None,
|
52
52
|
sample_input_data: Optional[model_types.SupportedDataType] = None,
|
53
|
+
user_files: Optional[Dict[str, List[str]]] = None,
|
53
54
|
code_paths: Optional[List[str]] = None,
|
54
55
|
ext_modules: Optional[List[ModuleType]] = None,
|
55
56
|
task: model_types.Task = model_types.Task.UNKNOWN,
|
56
57
|
options: Optional[model_types.ModelSaveOption] = None,
|
57
58
|
statement_params: Optional[Dict[str, Any]] = None,
|
58
59
|
) -> model_version_impl.ModelVersion:
|
59
|
-
|
60
|
-
|
60
|
+
|
61
|
+
database_name_id, schema_name_id, model_name_id = self._parse_fully_qualified_name(model_name)
|
62
|
+
|
63
|
+
model_exists = self._model_ops.validate_existence(
|
64
|
+
database_name=database_name_id,
|
65
|
+
schema_name=schema_name_id,
|
66
|
+
model_name=model_name_id,
|
67
|
+
statement_params=statement_params,
|
68
|
+
)
|
69
|
+
|
70
|
+
if version_name is None:
|
71
|
+
if model_exists:
|
72
|
+
versions = self._model_ops.list_models_or_versions(
|
73
|
+
database_name=database_name_id,
|
74
|
+
schema_name=schema_name_id,
|
75
|
+
model_name=model_name_id,
|
76
|
+
statement_params=statement_params,
|
77
|
+
)
|
78
|
+
for _ in range(1000):
|
79
|
+
hrid = self._hrid_generator.generate()[1]
|
80
|
+
if sql_identifier.SqlIdentifier(hrid) not in versions:
|
81
|
+
version_name = hrid
|
82
|
+
break
|
83
|
+
if version_name is None:
|
84
|
+
raise RuntimeError("Random version name generation failed.")
|
85
|
+
else:
|
86
|
+
version_name = self._hrid_generator.generate()[1]
|
61
87
|
|
62
88
|
if isinstance(model, model_version_impl.ModelVersion):
|
63
89
|
(
|
@@ -75,10 +101,24 @@ class ModelManager:
|
|
75
101
|
schema_name=None,
|
76
102
|
model_name=sql_identifier.SqlIdentifier(model_name),
|
77
103
|
version_name=sql_identifier.SqlIdentifier(version_name),
|
104
|
+
model_exists=model_exists,
|
78
105
|
statement_params=statement_params,
|
79
106
|
)
|
80
107
|
return self.get_model(model_name=model_name, statement_params=statement_params).version(version_name)
|
81
108
|
|
109
|
+
version_name_id = sql_identifier.SqlIdentifier(version_name)
|
110
|
+
if model_exists and self._model_ops.validate_existence(
|
111
|
+
database_name=database_name_id,
|
112
|
+
schema_name=schema_name_id,
|
113
|
+
model_name=model_name_id,
|
114
|
+
version_name=version_name_id,
|
115
|
+
statement_params=statement_params,
|
116
|
+
):
|
117
|
+
raise ValueError(
|
118
|
+
f"Model {model_name} version {version_name} already existed. "
|
119
|
+
+ "To auto-generate `version_name`, skip that argument."
|
120
|
+
)
|
121
|
+
|
82
122
|
return self._log_model(
|
83
123
|
model=model,
|
84
124
|
model_name=model_name,
|
@@ -91,6 +131,7 @@ class ModelManager:
|
|
91
131
|
python_version=python_version,
|
92
132
|
signatures=signatures,
|
93
133
|
sample_input_data=sample_input_data,
|
134
|
+
user_files=user_files,
|
94
135
|
code_paths=code_paths,
|
95
136
|
ext_modules=ext_modules,
|
96
137
|
task=task,
|
@@ -103,7 +144,7 @@ class ModelManager:
|
|
103
144
|
model: model_types.SupportedModelType,
|
104
145
|
*,
|
105
146
|
model_name: str,
|
106
|
-
version_name:
|
147
|
+
version_name: str,
|
107
148
|
comment: Optional[str] = None,
|
108
149
|
metrics: Optional[Dict[str, Any]] = None,
|
109
150
|
conda_dependencies: Optional[List[str]] = None,
|
@@ -112,6 +153,7 @@ class ModelManager:
|
|
112
153
|
python_version: Optional[str] = None,
|
113
154
|
signatures: Optional[Dict[str, model_signature.ModelSignature]] = None,
|
114
155
|
sample_input_data: Optional[model_types.SupportedDataType] = None,
|
156
|
+
user_files: Optional[Dict[str, List[str]]] = None,
|
115
157
|
code_paths: Optional[List[str]] = None,
|
116
158
|
ext_modules: Optional[List[ModuleType]] = None,
|
117
159
|
task: model_types.Task = model_types.Task.UNKNOWN,
|
@@ -119,28 +161,8 @@ class ModelManager:
|
|
119
161
|
statement_params: Optional[Dict[str, Any]] = None,
|
120
162
|
) -> model_version_impl.ModelVersion:
|
121
163
|
database_name_id, schema_name_id, model_name_id = sql_identifier.parse_fully_qualified_name(model_name)
|
122
|
-
|
123
|
-
if not version_name:
|
124
|
-
version_name = self._hrid_generator.generate()[1]
|
125
164
|
version_name_id = sql_identifier.SqlIdentifier(version_name)
|
126
165
|
|
127
|
-
if self._model_ops.validate_existence(
|
128
|
-
database_name=database_name_id,
|
129
|
-
schema_name=schema_name_id,
|
130
|
-
model_name=model_name_id,
|
131
|
-
statement_params=statement_params,
|
132
|
-
) and self._model_ops.validate_existence(
|
133
|
-
database_name=database_name_id,
|
134
|
-
schema_name=schema_name_id,
|
135
|
-
model_name=model_name_id,
|
136
|
-
version_name=version_name_id,
|
137
|
-
statement_params=statement_params,
|
138
|
-
):
|
139
|
-
raise ValueError(
|
140
|
-
f"Model {model_name} version {version_name} already existed. "
|
141
|
-
+ "To auto-generate `version_name`, skip that argument."
|
142
|
-
)
|
143
|
-
|
144
166
|
stage_path = self._model_ops.prepare_model_stage_path(
|
145
167
|
database_name=database_name_id,
|
146
168
|
schema_name=schema_name_id,
|
@@ -148,13 +170,10 @@ class ModelManager:
|
|
148
170
|
)
|
149
171
|
|
150
172
|
platforms = None
|
151
|
-
# TODO(jbahk): Remove the version check after Snowflake 8.40.0 release
|
152
173
|
# User specified target platforms are defaulted to None and will not show up in the generated manifest.
|
153
|
-
|
154
|
-
if snowflake_env.get_current_snowflake_version(self._model_ops._session) >= version.parse("8.40.0"):
|
174
|
+
if target_platforms:
|
155
175
|
# Convert any string target platforms to TargetPlatform objects
|
156
|
-
|
157
|
-
platforms = [model_types.TargetPlatform(platform) for platform in target_platforms]
|
176
|
+
platforms = [model_types.TargetPlatform(platform) for platform in target_platforms]
|
158
177
|
|
159
178
|
logger.info("Start packaging and uploading your model. It might take some time based on the size of the model.")
|
160
179
|
|
@@ -170,6 +189,7 @@ class ModelManager:
|
|
170
189
|
pip_requirements=pip_requirements,
|
171
190
|
target_platforms=platforms,
|
172
191
|
python_version=python_version,
|
192
|
+
user_files=user_files,
|
173
193
|
code_paths=code_paths,
|
174
194
|
ext_modules=ext_modules,
|
175
195
|
options=options,
|
@@ -229,7 +249,7 @@ class ModelManager:
|
|
229
249
|
*,
|
230
250
|
statement_params: Optional[Dict[str, Any]] = None,
|
231
251
|
) -> model_impl.Model:
|
232
|
-
database_name_id, schema_name_id, model_name_id =
|
252
|
+
database_name_id, schema_name_id, model_name_id = self._parse_fully_qualified_name(model_name)
|
233
253
|
if self._model_ops.validate_existence(
|
234
254
|
database_name=database_name_id,
|
235
255
|
schema_name=schema_name_id,
|
@@ -289,7 +309,7 @@ class ModelManager:
|
|
289
309
|
*,
|
290
310
|
statement_params: Optional[Dict[str, Any]] = None,
|
291
311
|
) -> None:
|
292
|
-
database_name_id, schema_name_id, model_name_id =
|
312
|
+
database_name_id, schema_name_id, model_name_id = self._parse_fully_qualified_name(model_name)
|
293
313
|
|
294
314
|
self._model_ops.delete_model_or_version(
|
295
315
|
database_name=database_name_id,
|
@@ -297,3 +317,20 @@ class ModelManager:
|
|
297
317
|
model_name=model_name_id,
|
298
318
|
statement_params=statement_params,
|
299
319
|
)
|
320
|
+
|
321
|
+
def _parse_fully_qualified_name(
|
322
|
+
self, model_name: str
|
323
|
+
) -> Tuple[
|
324
|
+
Optional[sql_identifier.SqlIdentifier], Optional[sql_identifier.SqlIdentifier], sql_identifier.SqlIdentifier
|
325
|
+
]:
|
326
|
+
try:
|
327
|
+
return sql_identifier.parse_fully_qualified_name(model_name)
|
328
|
+
except ValueError:
|
329
|
+
raise exceptions.SnowflakeMLException(
|
330
|
+
error_code=error_codes.INVALID_ARGUMENT,
|
331
|
+
original_exception=ValueError(
|
332
|
+
f"The model_name `{model_name}` cannot be parsed as a SQL identifier. Alphanumeric characters and "
|
333
|
+
"underscores are permitted. See https://docs.snowflake.com/en/sql-reference/identifiers-syntax for "
|
334
|
+
"more information."
|
335
|
+
),
|
336
|
+
)
|