Skip to content

Commit be07e08

Browse files
authored
[ml] Re-enable samples testing (Azure#31907)
* Re-enable samples testing * Re-enable samples testing for sub-sections * Fix authentication/MLClient sample * Update workspace and compute name * Fix compute sample * Add helper files * Update example for compute operations begin_delete * Update ml_samples_misc * Make paths absolute * Extract hand_resource_exists_error * Update spark configuration samples * Update workspace name
1 parent 8c87393 commit be07e08

18 files changed

+347
-261
lines changed

sdk/ml/automl.tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ extends:
88
Public:
99
SubscriptionConfiguration: $(python-ml-automl-sub-scope)
1010
Location: eastus
11+
MatrixReplace:
12+
- TestSamples=.*/true

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def begin_delete(self, name: str, *, action: str = "Delete") -> LROPoller[None]:
253253
254254
.. admonition:: Example:
255255
256-
.. literalinclude:: ../../../../samples/ml_samples_misc.py
256+
.. literalinclude:: ../../../../samples/ml_samples_compute.py
257257
:start-after: [START compute_operations_delete]
258258
:end-before: [END compute_operations_delete]
259259
:language: python

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

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,65 +28,70 @@ class MLClientSamples(object):
2828
def ml_auth_azure_default_credential(self):
2929
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
3030
resource_group = os.environ["RESOURCE_GROUP_NAME"]
31+
workspace_name = "test-ws1"
3132

3233
# [START create_ml_client_default_credential]
33-
from azure.identity import AzureAuthorityHosts, DefaultAzureCredential
34-
3534
from azure.ai.ml import MLClient
36-
37-
ml_client = MLClient(subscription_id, resource_group, credential=DefaultAzureCredential())
38-
# [END create_ml_client_default_credential]
39-
40-
# [START create_ml_client_sovereign_cloud]
4135
from azure.identity import AzureAuthorityHosts, DefaultAzureCredential
4236

43-
from azure.ai.ml import MLClient
44-
45-
kwargs = {"cloud": "AzureChinaCloud"}
4637
ml_client = MLClient(
47-
subscription_id,
48-
resource_group,
49-
credential=DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_CHINA),
50-
**kwargs
38+
subscription_id=subscription_id,
39+
resource_group_name=resource_group,
40+
workspace_name=workspace_name,
41+
credential=DefaultAzureCredential(),
5142
)
52-
# [END create_ml_client_sovereign_cloud]
43+
# [END create_ml_client_default_credential]
5344

5445
# [START create_ml_client_from_config_default]
5546
from azure.ai.ml import MLClient
5647

57-
client = MLClient.from_config(credential=DefaultAzureCredential(), path="src")
48+
client = MLClient.from_config(credential=DefaultAzureCredential(), path="./sdk/ml/azure-ai-ml/samples/src")
5849
# [END create_ml_client_from_config_default]
5950

6051
# [START create_ml_client_from_config_custom_filename]
6152
from azure.ai.ml import MLClient
6253

6354
client = MLClient.from_config(
64-
credential=DefaultAzureCredential(), file_name="team_workspace_configuration.json"
55+
credential=DefaultAzureCredential(),
56+
file_name="./sdk/ml/azure-ai-ml/samples/team_workspace_configuration.json",
6557
)
6658
# [END create_ml_client_from_config_custom_filename]
6759

6860
# [START ml_client_create_or_update]
69-
from azure.ai.ml.entities import AmlTokenConfiguration, command
61+
from azure.ai.ml import Input, command
62+
from azure.ai.ml.constants import AssetTypes
63+
from azure.ai.ml.entities import ManagedOnlineEndpoint, UserIdentityConfiguration
7064

71-
command_job = command(
72-
description="description",
73-
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu:33",
74-
inputs=inputs,
75-
code="./tests/test_configs/training/",
76-
command="echo ${{inputs.uri}} ${{inputs.data_asset}} ${{inputs.local_data}}",
77-
display_name="builder_command_job",
78-
compute="testCompute",
79-
experiment_name="mfe-test1-dataset",
80-
identity=AmlTokenConfiguration(),
65+
client = MLClient(
66+
subscription_id=subscription_id,
67+
resource_group_name=resource_group,
68+
workspace_name=workspace_name,
69+
credential=DefaultAzureCredential(),
70+
)
71+
job = command(
72+
code="./sdk/ml/azure-ai-ml/samples/src",
73+
command="echo hello world",
74+
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu:1",
75+
compute="cpu-cluster",
76+
identity=UserIdentityConfiguration(),
8177
)
82-
created_job = client.create_or_update(command_job)
78+
79+
client.create_or_update(job)
8380
# [END ml_client_create_or_update]
8481

8582
# [START ml_client_begin_create_or_update]
83+
from random import randint
84+
8685
from azure.ai.ml.entities import ManagedOnlineEndpoint
8786

87+
client = MLClient(
88+
subscription_id=subscription_id,
89+
resource_group_name=resource_group,
90+
workspace_name=workspace_name,
91+
credential=DefaultAzureCredential(),
92+
)
8893
endpoint = ManagedOnlineEndpoint(
89-
name="online_endpoint_name",
94+
name=f"online-endpoint-name-{randint(1, 1000)}",
9095
description="this is a sample online endpoint",
9196
auth_mode="key",
9297
)
@@ -99,7 +104,7 @@ def ml_auth_azure_default_credential(self):
99104
from azure.ai.ml.entities import UserIdentityConfiguration
100105

101106
job = command(
102-
code="./src",
107+
code="./sdk/ml/azure-ai-ml/samples/src",
103108
command="python read_data.py --input_data ${{inputs.input_data}}",
104109
inputs={"input_data": Input(type=AssetTypes.MLTABLE, path="./sample_data")},
105110
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu:1",
@@ -126,9 +131,18 @@ def ml_auth_azure_default_credential(self):
126131
)
127132
# [END aml_token_configuration]
128133

129-
# Get a list of workspaces in a resource group
130-
for ws in ml_client.workspaces.list():
131-
print(ws.name, ":", ws.location, ":", ws.description)
134+
# [START create_ml_client_sovereign_cloud]
135+
from azure.ai.ml import MLClient
136+
from azure.identity import AzureAuthorityHosts, DefaultAzureCredential
137+
138+
kwargs = {"cloud": "AzureChinaCloud"}
139+
ml_client = MLClient(
140+
subscription_id=subscription_id,
141+
resource_group_name=resource_group,
142+
credential=DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_CHINA),
143+
**kwargs,
144+
)
145+
# [END create_ml_client_sovereign_cloud]
132146

133147

134148
if __name__ == "__main__":

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
class CommandConfigurationOptions(object):
2222
def ml_command_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"]
28+
workspace_name = "test-ws1"
2929
credential = DefaultAzureCredential()
30-
ml_client = MLClient(credential, subscription_id, resource_group, workspace_name="test-ws1")
30+
ml_client = MLClient(credential, subscription_id, resource_group, workspace_name=workspace_name)
3131

3232
from azure.ai.ml import Input, Output
3333
from azure.ai.ml.entities import CommandJob, CommandJobLimits

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ def ml_component_config(self):
4040
from azure.ai.ml import load_component
4141

4242
component = load_component(
43-
source="../tests/test_configs/components/helloworld_component.yml", params_override=[{"version": "1.0.2"}]
43+
source="./sdk/ml/azure-ai-ml/tests/test_configs/components/helloworld_component.yml",
44+
params_override=[{"version": "1.0.2"}],
4445
)
4546
registered_component = ml_client.components.create_or_update(component)
4647
# [END configure_load_component]

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

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,51 @@
1616
"""
1717

1818
import os
19+
from random import randint
20+
21+
from azure.ai.ml import MLClient
22+
from azure.core.exceptions import ResourceExistsError, ResourceNotFoundError
23+
from azure.identity import DefaultAzureCredential
24+
25+
26+
def handle_resource_exists_error(func):
27+
def wrapper(*args, **kwargs):
28+
try:
29+
return func(*args, **kwargs)
30+
except (ResourceExistsError, ResourceNotFoundError) as e:
31+
pass
32+
33+
return wrapper
34+
35+
36+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
37+
resource_group = os.environ["RESOURCE_GROUP_NAME"]
38+
workspace_name = "test-ws1"
39+
credential = DefaultAzureCredential()
40+
ml_client = MLClient(credential, subscription_id, resource_group, workspace_name=workspace_name)
41+
42+
compute_name_1 = f"compute-{randint(1, 1000)}"
43+
compute_name_2 = f"compute-{randint(1, 1000)}"
44+
ci_name = f"ci-{randint(1, 1000)}"
1945

2046

2147
class ComputeConfigurationOptions(object):
22-
def ml_compute_config(self):
23-
from azure.ai.ml import MLClient
24-
from azure.identity import DefaultAzureCredential
48+
@handle_resource_exists_error
49+
def ml_compute_config_setup_0(self):
50+
# [START compute_instance]
51+
from azure.ai.ml.entities import ComputeInstance
2552

26-
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
27-
resource_group = os.environ["RESOURCE_GROUP_NAME"]
28-
credential = DefaultAzureCredential()
29-
ml_client = MLClient(credential, subscription_id, resource_group, workspace_name="test-ws1")
53+
ci = ComputeInstance(
54+
name=ci_name,
55+
size="Standard_DS2_v2",
56+
)
57+
ml_client.compute.begin_create_or_update(ci)
58+
# [END compute_instance]
3059

60+
@handle_resource_exists_error
61+
def ml_compute_config_setup_1(self):
3162
# [START compute_operations_get]
32-
cpu_cluster = ml_client.compute.get("cpucluster")
63+
cpu_cluster = ml_client.compute.get("cpu-cluster")
3364
# [END compute_operations_get]
3465

3566
# [START load_compute]
@@ -46,14 +77,16 @@ def ml_compute_config(self):
4677
# [END compute_operations_list]
4778

4879
# [START compute_operations_list_nodes]
49-
node_list = ml_client.compute.list_nodes(name="cpucluster")
80+
node_list = ml_client.compute.list_nodes(name="cpu-cluster")
5081
# [END compute_operations_list_nodes]
5182

83+
@handle_resource_exists_error
84+
def ml_compute_config_setup_2(self):
5285
# [START compute_operations_create_update]
5386
from azure.ai.ml.entities import AmlCompute
5487

5588
compute_obj = AmlCompute(
56-
name="example-compute",
89+
name=compute_name_1,
5790
tags={"key1": "value1", "key2": "value2"},
5891
min_instances=0,
5992
max_instances=10,
@@ -62,9 +95,13 @@ def ml_compute_config(self):
6295
registered_compute = ml_client.compute.begin_create_or_update(compute_obj)
6396
# [END compute_operations_create_update]
6497

98+
@handle_resource_exists_error
99+
def ml_compute_config_setup_3(self):
65100
# [START compute_operations_attach]
101+
from azure.ai.ml.entities import AmlCompute
102+
66103
compute_obj = AmlCompute(
67-
name="example-compute-2",
104+
name=compute_name_2,
68105
tags={"key1": "value1", "key2": "value2"},
69106
min_instances=0,
70107
max_instances=10,
@@ -74,52 +111,24 @@ def ml_compute_config(self):
74111
# [END compute_operations_attach]
75112

76113
# [START compute_operations_update]
77-
compute_obj = ml_client.compute.get("cpucluster")
114+
compute_obj = ml_client.compute.get("cpu-cluster")
78115
compute_obj.idle_time_before_scale_down = 200
79116
updated_compute = ml_client.compute.begin_update(compute_obj)
80117
# [END compute_operations_update]
81118

82-
# [START compute_operations_delete]
83-
ml_client.compute.begin_delete("example-compute", action="Detach")
84-
85-
ml_client.compute.begin_delete("example-compute-2")
86-
# [END compute_operations_delete]
87-
88-
compute_obj = ComputeInstance(
89-
name="example-compute",
90-
tags={"key1": "value1", "key2": "value2"},
91-
min_instances=0,
92-
max_instances=10,
93-
idle_time_before_scale_down=100,
94-
)
95-
registered_compute = ml_client.compute.begin_create_or_update(compute_obj)
96-
97-
# [START compute_operations_start]
98-
ml_client.compute.begin_start("example-compute")
99-
# [END compute_operations_start]
100-
101-
# [START compute_operations_stop]
102-
ml_client.compute.begin_stop("example-compute")
103-
# [END compute_operations_stop]
104-
105-
# [START compute_operations_restart]
106-
ml_client.compute.begin_stop("example-compute")
107-
ml_client.compute.begin_restart("example-compute")
108-
# [END compute_operations_restart]
109-
110119
# [START compute_operations_list_usage]
111-
print(ml_client.compute.list_usage())
120+
usage_list = ml_client.compute.list_usage()
112121
# [END compute_operations_list_usage]
113122

114123
# [START compute_operations_list_sizes]
115-
print(ml_client.compute.list_sizes())
124+
size_list = ml_client.compute.list_sizes()
116125
# [END compute_operations_list_sizes]
117126

118127
# [START amlcompute]
119128
from azure.ai.ml.entities import AmlCompute, IdentityConfiguration, ManagedIdentityConfiguration
120129

121130
aml_compute = AmlCompute(
122-
name="my-compute",
131+
name="my-aml-compute",
123132
min_instances=0,
124133
max_instances=10,
125134
idle_time_before_scale_down=100,
@@ -158,17 +167,13 @@ def ml_compute_config(self):
158167
on_behalf_of_config = AssignedUserConfiguration(user_tenant_id="12345", user_object_id="abcdef")
159168
# [END assigned_user_configuration]
160169

161-
# [START compute_instance]
162-
from azure.ai.ml.entities import ComputeInstance
170+
# [START compute_operations_stop]
171+
ml_client.compute.begin_stop(ci_name)
172+
# [END compute_operations_stop]
163173

164-
ci = ComputeInstance(
165-
name="my-ci-resource",
166-
location="eastus",
167-
size="Standard_DS2_v2",
168-
ssh_public_access_enabled=False,
169-
ssh_key_value="ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZ administrator@MININT-2023",
170-
)
171-
# [END compute_instance]
174+
# [START compute_operations_start]
175+
ml_client.compute.begin_start(ci_name)
176+
# [END compute_operations_start]
172177

173178
# [START vm_ssh_settings]
174179
from azure.ai.ml.entities import VirtualMachineSshSettings
@@ -261,7 +266,20 @@ def ml_compute_config(self):
261266
materialization_compute = MaterializationComputeResource(instance_type="standard_e4s_v3")
262267
# [END materialization_compute_resource]
263268

269+
# [START compute_operations_restart]
270+
ml_client.compute.begin_restart(ci_name)
271+
# [END compute_operations_restart]
272+
273+
# [START compute_operations_delete]
274+
ml_client.compute.begin_delete(compute_name_1, action="Detach")
275+
276+
ml_client.compute.begin_delete(compute_name_2)
277+
# [END compute_operations_delete]
278+
264279

265280
if __name__ == "__main__":
266281
sample = ComputeConfigurationOptions()
267-
sample.ml_compute_config()
282+
sample.ml_compute_config_setup_0()
283+
sample.ml_compute_config_setup_1()
284+
sample.ml_compute_config_setup_2()
285+
sample.ml_compute_config_setup_3()

0 commit comments

Comments
 (0)