Skip to content

Commit 9367722

Browse files
Model related docs refine (Azure#31552)
* model operation doc refine * model entities doc refine * resolve conflict * fix review comments * fix line length limit issue * minor fix on _load_functions.py * revert back change
1 parent 94f42bb commit 9367722

File tree

5 files changed

+184
-24
lines changed

5 files changed

+184
-24
lines changed

sdk/ml/azure-ai-ml/azure/ai/ml/entities/_assets/_artifacts/_package/model_configuration.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ class ModelConfiguration:
1616
:type mode: str
1717
:param mount_path: The mount path of the model.
1818
:type mount_path: str
19+
20+
.. admonition:: Example:
21+
22+
.. literalinclude:: ../../../../../../../samples/ml_samples_misc.py
23+
:start-after: [START model_configuration_entity_create]
24+
:end-before: [END model_configuration_entity_create]
25+
:language: python
26+
:dedent: 8
27+
:caption: Creating a Model Configuration object.
1928
"""
2029

2130
def __init__(self, *, mode: str = None, mount_path: str = None):

sdk/ml/azure-ai-ml/azure/ai/ml/entities/_assets/_artifacts/_package/model_package.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ class ModelPackageInput:
138138
:type mode: Optional[str]
139139
:param mount_path: The mount path for the input.
140140
:type mount_path: Optional[str]
141+
142+
.. admonition:: Example:
143+
144+
.. literalinclude:: ../../../../../../../samples/ml_samples_misc.py
145+
:start-after: [START model_package_input_entity_create]
146+
:end-before: [END model_package_input_entity_create]
147+
:language: python
148+
:dedent: 8
149+
:caption: Create a Model Package Input object.
141150
"""
142151

143152
def __init__(
@@ -191,7 +200,16 @@ class ModelPackage(Resource, PackageRequest):
191200
:param model_configuration: The model configuration.
192201
:type model_configuration: Optional[~azure.ai.ml.entities.ModelConfiguration]
193202
:param tags: The tags of the model package.
194-
:type tags: Optiona[dict[str, str]]
203+
:type tags: Optional[dict[str, str]]
204+
205+
.. admonition:: Example:
206+
207+
.. literalinclude:: ../../../../../../../samples/ml_samples_misc.py
208+
:start-after: [START model_package_entity_create]
209+
:end-before: [END model_package_entity_create]
210+
:language: python
211+
:dedent: 8
212+
:caption: Create a Model Package object.
195213
"""
196214

197215
def __init__(

sdk/ml/azure-ai-ml/azure/ai/ml/entities/_deployment/model_batch_deployment_settings.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
from typing import Dict, Optional
66

7+
from azure.ai.ml._schema._deployment.batch.model_batch_deployment_settings import ModelBatchDeploymentSettingsSchema
8+
from azure.ai.ml._utils._experimental import experimental
79
from azure.ai.ml.constants._common import BASE_PATH_CONTEXT_KEY
810
from azure.ai.ml.constants._deployment import BatchDeploymentOutputAction
911
from azure.ai.ml.entities._deployment.deployment_settings import BatchRetrySettings
10-
from azure.ai.ml._utils._experimental import experimental
11-
from azure.ai.ml._schema._deployment.batch.model_batch_deployment_settings import ModelBatchDeploymentSettingsSchema
1212

1313

1414
@experimental
@@ -39,6 +39,15 @@ class ModelBatchDeploymentSettings:
3939
:type error_threshold: int
4040
:param logging_level: Logging level for batch inference operation, defaults to "info"
4141
:type logging_level: str
42+
43+
.. admonition:: Example:
44+
45+
.. literalinclude:: ../../../../../samples/ml_samples_misc.py
46+
:start-after: [START model_batch_deployment_settings_entity_create]
47+
:end-before: [END model_batch_deployment_settings_entity_create]
48+
:language: python
49+
:dedent: 8
50+
:caption: Creating a Model Batch Deployment Settings object.
4251
"""
4352

4453
def __init__(

sdk/ml/azure-ai-ml/azure/ai/ml/operations/_model_operations.py

Lines changed: 107 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
# pylint: disable=protected-access
66

7+
from contextlib import contextmanager
78
from os import PathLike, path
89
from typing import Dict, Iterable, Optional, Union
9-
from contextlib import contextmanager
1010

1111
from marshmallow.exceptions import ValidationError as SchemaValidationError
1212

13-
from azure.ai.ml._utils._experimental import experimental
1413
from azure.ai.ml._artifacts._artifact_utilities import (
1514
_check_and_upload_path,
1615
_get_default_datastore_info,
@@ -21,40 +20,39 @@
2120
CHANGED_ASSET_PATH_MSG,
2221
CHANGED_ASSET_PATH_MSG_NO_PERSONAL_DATA,
2322
)
24-
from azure.ai.ml._utils._arm_id_utils import is_ARM_id_for_resource
25-
from azure.ai.ml._utils._registry_utils import get_registry_client
2623
from azure.ai.ml._exception_helper import log_and_raise_error
2724
from azure.ai.ml._restclient.v2021_10_01_dataplanepreview import (
2825
AzureMachineLearningWorkspaces as ServiceClient102021Dataplane,
2926
)
30-
from azure.ai.ml._restclient.v2023_04_01_preview.models import ListViewType, ModelVersion
3127
from azure.ai.ml._restclient.v2023_04_01_preview import AzureMachineLearningWorkspaces as ServiceClient042023Preview
28+
from azure.ai.ml._restclient.v2023_04_01_preview.models import ListViewType, ModelVersion
3229
from azure.ai.ml._scope_dependent_operations import (
3330
OperationConfig,
31+
OperationsContainer,
3432
OperationScope,
3533
_ScopeDependentOperations,
36-
OperationsContainer,
3734
)
38-
from azure.ai.ml.entities._assets._artifacts.code import Code
39-
40-
from azure.ai.ml.constants._common import ARM_ID_PREFIX
4135
from azure.ai.ml._telemetry import ActivityType, monitor_with_activity
36+
from azure.ai.ml._utils._arm_id_utils import is_ARM_id_for_resource
4237
from azure.ai.ml._utils._asset_utils import (
4338
_archive_or_restore,
4439
_get_latest,
45-
_resolve_label_to_asset,
4640
_get_next_version_from_container,
41+
_resolve_label_to_asset,
4742
)
43+
from azure.ai.ml._utils._experimental import experimental
4844
from azure.ai.ml._utils._logger_utils import OpsLogger
4945
from azure.ai.ml._utils._registry_utils import (
5046
get_asset_body_for_registry_storage,
47+
get_registry_client,
5148
get_sas_uri_for_registry_asset,
5249
get_storage_details_for_registry_assets,
5350
)
5451
from azure.ai.ml._utils._storage_utils import get_ds_name_and_path_prefix, get_storage_client
5552
from azure.ai.ml._utils.utils import resolve_short_datastore_url, validate_ml_flow_folder
56-
from azure.ai.ml.constants._common import ASSET_ID_FORMAT, AzureMLResourceType
57-
from azure.ai.ml.entities._assets import Model, ModelPackage, Environment
53+
from azure.ai.ml.constants._common import ARM_ID_PREFIX, ASSET_ID_FORMAT, AzureMLResourceType
54+
from azure.ai.ml.entities._assets import Environment, Model, ModelPackage
55+
from azure.ai.ml.entities._assets._artifacts.code import Code
5856
from azure.ai.ml.entities._assets.workspace_asset_reference import WorkspaceAssetReference
5957
from azure.ai.ml.entities._credentials import AccountKeyConfiguration
6058
from azure.ai.ml.exceptions import (
@@ -64,8 +62,9 @@
6462
ValidationErrorType,
6563
ValidationException,
6664
)
67-
from azure.core.exceptions import ResourceNotFoundError
6865
from azure.ai.ml.operations._datastore_operations import DatastoreOperations
66+
from azure.core.exceptions import ResourceNotFoundError
67+
6968
from ._operation_orchestrator import OperationOrchestrator
7069

7170
ops_logger = OpsLogger(__name__)
@@ -77,6 +76,21 @@ class ModelOperations(_ScopeDependentOperations):
7776
7877
You should not instantiate this class directly. Instead, you should create an MLClient instance that instantiates it
7978
for you and attaches it as an attribute.
79+
80+
:param operation_scope: Scope variables for the operations classes of an MLClient object.
81+
:type operation_scope: ~azure.ai.ml._scope_dependent_operations.OperationScope
82+
:param operation_config: Common configuration for operations classes of an MLClient object.
83+
:type operation_config: ~azure.ai.ml._scope_dependent_operations.OperationConfig
84+
:param service_client: Service client to allow end users to operate on Azure Machine Learning Workspace
85+
resources (ServiceClient042023Preview or ServiceClient102021Dataplane).
86+
:type service_client: typing.Union[
87+
~azure.ai.ml._restclient.v2023_04_01_preview._azure_machine_learning_workspaces.AzureMachineLearningWorkspaces,
88+
~azure.ai.ml._restclient.v2021_10_01_dataplanepreview._azure_machine_learning_workspaces.
89+
AzureMachineLearningWorkspaces]
90+
:param datastore_operations: Represents a client for performing operations on Datastores.
91+
:type datastore_operations: ~azure.ai.ml.operations._datastore_operations.DatastoreOperations
92+
:param all_operations: All operations classes of an MLClient object.
93+
:type all_operations: ~azure.ai.ml._scope_dependent_operations.OperationsContainer
8094
"""
8195

8296
# pylint: disable=unused-argument
@@ -116,6 +130,15 @@ def create_or_update(
116130
:raises ~azure.ai.ml.exceptions.EmptyDirectoryError: Raised if local path provided points to an empty directory.
117131
:return: Model asset object.
118132
:rtype: ~azure.ai.ml.entities.Model
133+
134+
.. admonition:: Example:
135+
136+
.. literalinclude:: ../../../../samples/ml_samples_misc.py
137+
:start-after: [START model_operations_create_or_update]
138+
:end-before: [END model_operations_create_or_update]
139+
:language: python
140+
:dedent: 8
141+
:caption: Create model example.
119142
"""
120143
try:
121144
name = model.name
@@ -274,6 +297,15 @@ def get(self, name: str, version: Optional[str] = None, label: Optional[str] = N
274297
Details will be provided in the error message.
275298
:return: Model asset object.
276299
:rtype: ~azure.ai.ml.entities.Model
300+
301+
.. admonition:: Example:
302+
303+
.. literalinclude:: ../../../../samples/ml_samples_misc.py
304+
:start-after: [START model_operations_get]
305+
:end-before: [END model_operations_get]
306+
:language: python
307+
:dedent: 8
308+
:caption: Get model example.
277309
"""
278310
if version and label:
279311
msg = "Cannot specify both version and label."
@@ -306,11 +338,23 @@ def get(self, name: str, version: Optional[str] = None, label: Optional[str] = N
306338
def download(self, name: str, version: str, download_path: Union[PathLike, str] = ".") -> None:
307339
"""Download files related to a model.
308340
309-
:param str name: Name of the model.
310-
:param str version: Version of the model.
311-
:param Union[PathLike, str] download_path: Local path as download destination,
312-
defaults to current working directory of the current user. Contents will be overwritten.
313-
:raise: ResourceNotFoundError if can't find a model matching provided name.
341+
:param name: Name of the model.
342+
:type name: str
343+
:param version: Version of the model.
344+
:type version: str
345+
:param download_path: Local path as download destination, defaults to current working directory of the current
346+
user. Contents will be overwritten.
347+
:type download_path: Union[PathLike, str]
348+
:raises ResourceNotFoundError: if can't find a model matching provided name.
349+
350+
.. admonition:: Example:
351+
352+
.. literalinclude:: ../../../../samples/ml_samples_misc.py
353+
:start-after: [START model_operations_download]
354+
:end-before: [END model_operations_download]
355+
:language: python
356+
:dedent: 8
357+
:caption: Download files to model example.
314358
"""
315359

316360
model_uri = self.get(name=name, version=version).path
@@ -379,6 +423,15 @@ def archive(
379423
:type version: str
380424
:param label: Label of the model asset. (mutually exclusive with version)
381425
:type label: str
426+
427+
.. admonition:: Example:
428+
429+
.. literalinclude:: ../../../../samples/ml_samples_misc.py
430+
:start-after: [START model_operations_archive]
431+
:end-before: [END model_operations_archive]
432+
:language: python
433+
:dedent: 8
434+
:caption: Archive a model example.
382435
"""
383436
_archive_or_restore(
384437
asset_operations=self,
@@ -402,6 +455,15 @@ def restore(
402455
:type version: str
403456
:param label: Label of the model asset. (mutually exclusive with version)
404457
:type label: str
458+
459+
.. admonition:: Example:
460+
461+
.. literalinclude:: ../../../../samples/ml_samples_misc.py
462+
:start-after: [START model_operations_restore]
463+
:end-before: [END model_operations_restore]
464+
:language: python
465+
:dedent: 8
466+
:caption: Restore a model example.
405467
"""
406468
_archive_or_restore(
407469
asset_operations=self,
@@ -431,7 +493,16 @@ def list(
431493
:attr:`ListViewType.ACTIVE_ONLY`.
432494
:type list_view_type: ListViewType
433495
:return: An iterator like instance of Model objects
434-
:rtype: ~azure.core.paging.ItemPaged[Model]
496+
:rtype: ~azure.core.paging.ItemPaged[~azure.ai.ml.entities.Model]
497+
498+
.. admonition:: Example:
499+
500+
.. literalinclude:: ../../../../samples/ml_samples_misc.py
501+
:start-after: [START model_operations_list]
502+
:end-before: [END model_operations_list]
503+
:language: python
504+
:dedent: 8
505+
:caption: List all models example.
435506
"""
436507
if name:
437508
return (
@@ -485,6 +556,15 @@ def share(self, name, version, *, share_with_name, share_with_version, registry_
485556
:paramtype registry_name: str
486557
:return: Model asset object.
487558
:rtype: ~azure.ai.ml.entities.Model
559+
560+
.. admonition:: Example:
561+
562+
.. literalinclude:: ../../../../samples/ml_samples_misc.py
563+
:start-after: [START model_operations_share]
564+
:end-before: [END model_operations_share]
565+
:language: python
566+
:dedent: 8
567+
:caption: Share a model example.
488568
"""
489569

490570
# Get workspace info to get workspace GUID
@@ -574,6 +654,14 @@ def package(self, name: str, version: str, package_request: ModelPackage, **kwar
574654
:return: Environment object
575655
:rtype: ~azure.ai.ml.entities.Environment
576656
657+
.. admonition:: Example:
658+
659+
.. literalinclude:: ../../../../samples/ml_samples_misc.py
660+
:start-after: [START model_operations_package]
661+
:end-before: [END model_operations_package]
662+
:language: python
663+
:dedent: 8
664+
:caption: Package a model example.
577665
"""
578666

579667
if not kwargs.get("skip_to_rest", False):

sdk/ml/azure-ai-ml/samples/ml_samples_misc.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020

2121
class MiscConfigurationOptions(object):
2222
def ml_misc_config(self):
23-
from azure.identity import DefaultAzureCredential
24-
2523
from azure.ai.ml import MLClient
24+
from azure.identity import DefaultAzureCredential
2625

2726
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
2827
resource_group = os.environ["RESOURCE_GROUP_NAME"]
@@ -120,6 +119,43 @@ def ml_misc_config(self):
120119
)
121120
# [END model_entity_create]
122121

122+
# [START model_batch_deployment_settings_entity_create]
123+
from azure.ai.ml.entities._deployment.model_batch_deployment_settings import ModelBatchDeploymentSettings
124+
125+
modelBatchDeploymentSetting = ModelBatchDeploymentSettings(
126+
mini_batch_size=256,
127+
instance_count=5,
128+
max_concurrency_per_instance=2,
129+
output_file_name="output-file-name",
130+
environment_variables={"env1": "value1", "env2": "value2"},
131+
error_threshold=2,
132+
logging_level=1,
133+
)
134+
# [END model_batch_deployment_settings_entity_create]
135+
136+
# [START model_configuration_entity_create]
137+
from azure.ai.ml.entities._assets._artifacts._package.model_configuration import ModelConfiguration
138+
139+
modelConfiguration = ModelConfiguration(mode="model-mode", mount_path="model-mount-path")
140+
# [END model_configuration_entity_create]
141+
142+
# [START model_package_input_entity_create]
143+
from azure.ai.ml.entities._assets._artifacts._package.model_package import ModelPackageInput
144+
145+
modelPackageInput = ModelPackageInput(type="input-type", mode="input-mode", mount_path="input-mount-path")
146+
# [END model_package_input_entity_create]
147+
148+
# [START model_package_entity_create]
149+
from azure.ai.ml.entities._assets._artifacts._package.model_package import ModelPackage
150+
151+
modelPackage = ModelPackage(
152+
target_environment_name="env-name",
153+
target_environment_version="1.0",
154+
environment_variables={"env1": "value1", "env2": "value2"},
155+
tags={"tag1": "value1", "tag2": "value2"},
156+
)
157+
# [END model_package_entity_create]
158+
123159
# [START create_inputs_outputs]
124160
from azure.ai.ml import Input, Output
125161
from azure.ai.ml.entities import CommandJob, CommandJobLimits

0 commit comments

Comments
 (0)