|
6 | 6 | """Unit tests for model frameworks. Includes tests for:
|
7 | 7 | - GenericModel
|
8 | 8 | """
|
| 9 | +import glob |
9 | 10 | import os
|
10 | 11 | import random
|
11 | 12 | import shutil
|
12 |
| -from copy import copy |
13 |
| -import glob |
14 | 13 | import tempfile
|
| 14 | +from copy import copy |
15 | 15 | from unittest.mock import MagicMock, PropertyMock, patch
|
| 16 | +from zipfile import ZipFile |
16 | 17 |
|
| 18 | +import numpy as np |
17 | 19 | import pandas as pd
|
18 | 20 | import pytest
|
19 | 21 | import yaml
|
20 |
| -import numpy as np |
| 22 | +from joblib import dump |
| 23 | +from oci.data_science.models.model_provenance import ModelProvenance |
| 24 | +from sklearn.datasets import load_iris |
| 25 | +from sklearn.ensemble import RandomForestClassifier |
| 26 | +from sklearn.model_selection import train_test_split |
| 27 | + |
21 | 28 | from ads.common import utils
|
22 |
| -from ads.config import ( |
23 |
| - JOB_RUN_COMPARTMENT_OCID, |
24 |
| - NB_SESSION_COMPARTMENT_OCID, |
25 |
| -) |
| 29 | +from ads.common.object_storage_details import ObjectStorageDetails |
| 30 | +from ads.config import JOB_RUN_COMPARTMENT_OCID, NB_SESSION_COMPARTMENT_OCID |
26 | 31 | from ads.model.artifact import ModelArtifact
|
| 32 | +from ads.model.datascience_model import DataScienceModel, OCIDataScienceModel |
27 | 33 | from ads.model.deployment import (
|
28 |
| - DEFAULT_POLL_INTERVAL, |
29 |
| - DEFAULT_WAIT_TIME, |
30 | 34 | ModelDeployer,
|
31 | 35 | ModelDeployment,
|
| 36 | + ModelDeploymentContainerRuntime, |
| 37 | + ModelDeploymentInfrastructure, |
32 | 38 | ModelDeploymentProperties,
|
33 | 39 | )
|
34 | 40 | from ads.model.deployment.common.utils import State as ModelDeploymentState
|
35 |
| -from ads.model.deployment import ( |
36 |
| - ModelDeploymentInfrastructure, |
37 |
| - ModelDeploymentContainerRuntime, |
38 |
| -) |
39 | 41 | from ads.model.generic_model import (
|
40 | 42 | _ATTRIBUTES_TO_SHOW_,
|
41 | 43 | GenericModel,
|
|
45 | 47 | )
|
46 | 48 | from ads.model.model_properties import ModelProperties
|
47 | 49 | from ads.model.runtime.runtime_info import RuntimeInfo
|
48 |
| -from ads.model.datascience_model import DataScienceModel, OCIDataScienceModel |
49 |
| -from joblib import dump |
50 |
| -from oci.data_science.models.model_provenance import ModelProvenance |
51 |
| -from sklearn.datasets import load_iris |
52 |
| -from sklearn.ensemble import RandomForestClassifier |
53 |
| -from sklearn.model_selection import train_test_split |
54 |
| -from zipfile import ZipFile |
55 |
| -from ads.model.deployment.common.utils import State |
56 | 50 |
|
57 | 51 | try:
|
58 | 52 | from yaml import CDumper as dumper
|
@@ -279,6 +273,21 @@ def test_prepare_fail(self, mock_handle_model_file_name):
|
279 | 273 | "oci://service-conda-packs@ociodscdev/service_pack/cpu/General_Machine_Learning_for_CPUs/1.0/mlcpuv1"
|
280 | 274 | )
|
281 | 275 |
|
| 276 | + @patch.object( |
| 277 | + ObjectStorageDetails, |
| 278 | + "fetch_metadata_of_object", |
| 279 | + side_effect=Exception("Connection Error."), |
| 280 | + ) |
| 281 | + def test_prepare_fail_missing_python_version_field( |
| 282 | + self, mock_fetch_metadata_of_object |
| 283 | + ): |
| 284 | + """Ensures that prepare method fails in case if python version not provided and cannot be resolved automatically.""" |
| 285 | + with pytest.raises( |
| 286 | + ValueError, |
| 287 | + match="Cannot automatically detect the inference python version. `inference_python_version` must be provided.", |
| 288 | + ): |
| 289 | + self.generic_model.prepare(inference_conda_env=INFERENCE_CONDA_ENV) |
| 290 | + |
282 | 291 | @patch("ads.model.runtime.env_info.get_service_packs")
|
283 | 292 | @patch("ads.common.auth.default_signer")
|
284 | 293 | def test_prepare_both_conda_env(self, mock_signer, mock_get_service_packs):
|
@@ -332,7 +341,8 @@ def test_prepare_both_conda_env(self, mock_signer, mock_get_service_packs):
|
332 | 341 | def test_prepare_with_custom_scorepy(self, mock_signer):
|
333 | 342 | """Test prepare a trained model with custom score.py."""
|
334 | 343 | self.generic_model.prepare(
|
335 |
| - INFERENCE_CONDA_ENV, |
| 344 | + inference_conda_env=INFERENCE_CONDA_ENV, |
| 345 | + inference_python_version=DEFAULT_PYTHON_VERSION, |
336 | 346 | model_file_name="fake_model_name",
|
337 | 347 | score_py_uri=f"{os.path.dirname(os.path.abspath(__file__))}/test_files/custom_score.py",
|
338 | 348 | )
|
@@ -467,11 +477,11 @@ def test_save_not_reload(
|
467 | 477 |
|
468 | 478 | def test_set_model_input_serializer(self):
|
469 | 479 | """Tests set_model_input_serializer() with different input types."""
|
| 480 | + from ads.model.serde.common import SERDE |
470 | 481 | from ads.model.serde.model_input import (
|
471 | 482 | CloudpickleModelInputSERDE,
|
472 | 483 | JsonModelInputSERDE,
|
473 | 484 | )
|
474 |
| - from ads.model.serde.common import SERDE |
475 | 485 |
|
476 | 486 | generic_model = GenericModel(estimator=self.clr, artifact_dir="fake_folder")
|
477 | 487 | # set by passing str
|
|
0 commit comments