Skip to content

Commit

Permalink
[ml] Re-enable samples testing (Azure#31907)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
diondrapeck authored Sep 11, 2023
1 parent 8c87393 commit be07e08
Show file tree
Hide file tree
Showing 18 changed files with 347 additions and 261 deletions.
2 changes: 2 additions & 0 deletions sdk/ml/automl.tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ extends:
Public:
SubscriptionConfiguration: $(python-ml-automl-sub-scope)
Location: eastus
MatrixReplace:
- TestSamples=.*/true
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def begin_delete(self, name: str, *, action: str = "Delete") -> LROPoller[None]:
.. admonition:: Example:
.. literalinclude:: ../../../../samples/ml_samples_misc.py
.. literalinclude:: ../../../../samples/ml_samples_compute.py
:start-after: [START compute_operations_delete]
:end-before: [END compute_operations_delete]
:language: python
Expand Down
82 changes: 48 additions & 34 deletions sdk/ml/azure-ai-ml/samples/ml_samples_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,70 @@ class MLClientSamples(object):
def ml_auth_azure_default_credential(self):
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_group = os.environ["RESOURCE_GROUP_NAME"]
workspace_name = "test-ws1"

# [START create_ml_client_default_credential]
from azure.identity import AzureAuthorityHosts, DefaultAzureCredential

from azure.ai.ml import MLClient

ml_client = MLClient(subscription_id, resource_group, credential=DefaultAzureCredential())
# [END create_ml_client_default_credential]

# [START create_ml_client_sovereign_cloud]
from azure.identity import AzureAuthorityHosts, DefaultAzureCredential

from azure.ai.ml import MLClient

kwargs = {"cloud": "AzureChinaCloud"}
ml_client = MLClient(
subscription_id,
resource_group,
credential=DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_CHINA),
**kwargs
subscription_id=subscription_id,
resource_group_name=resource_group,
workspace_name=workspace_name,
credential=DefaultAzureCredential(),
)
# [END create_ml_client_sovereign_cloud]
# [END create_ml_client_default_credential]

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

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

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

client = MLClient.from_config(
credential=DefaultAzureCredential(), file_name="team_workspace_configuration.json"
credential=DefaultAzureCredential(),
file_name="./sdk/ml/azure-ai-ml/samples/team_workspace_configuration.json",
)
# [END create_ml_client_from_config_custom_filename]

# [START ml_client_create_or_update]
from azure.ai.ml.entities import AmlTokenConfiguration, command
from azure.ai.ml import Input, command
from azure.ai.ml.constants import AssetTypes
from azure.ai.ml.entities import ManagedOnlineEndpoint, UserIdentityConfiguration

command_job = command(
description="description",
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu:33",
inputs=inputs,
code="./tests/test_configs/training/",
command="echo ${{inputs.uri}} ${{inputs.data_asset}} ${{inputs.local_data}}",
display_name="builder_command_job",
compute="testCompute",
experiment_name="mfe-test1-dataset",
identity=AmlTokenConfiguration(),
client = MLClient(
subscription_id=subscription_id,
resource_group_name=resource_group,
workspace_name=workspace_name,
credential=DefaultAzureCredential(),
)
job = command(
code="./sdk/ml/azure-ai-ml/samples/src",
command="echo hello world",
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu:1",
compute="cpu-cluster",
identity=UserIdentityConfiguration(),
)
created_job = client.create_or_update(command_job)

client.create_or_update(job)
# [END ml_client_create_or_update]

# [START ml_client_begin_create_or_update]
from random import randint

from azure.ai.ml.entities import ManagedOnlineEndpoint

client = MLClient(
subscription_id=subscription_id,
resource_group_name=resource_group,
workspace_name=workspace_name,
credential=DefaultAzureCredential(),
)
endpoint = ManagedOnlineEndpoint(
name="online_endpoint_name",
name=f"online-endpoint-name-{randint(1, 1000)}",
description="this is a sample online endpoint",
auth_mode="key",
)
Expand All @@ -99,7 +104,7 @@ def ml_auth_azure_default_credential(self):
from azure.ai.ml.entities import UserIdentityConfiguration

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

# Get a list of workspaces in a resource group
for ws in ml_client.workspaces.list():
print(ws.name, ":", ws.location, ":", ws.description)
# [START create_ml_client_sovereign_cloud]
from azure.ai.ml import MLClient
from azure.identity import AzureAuthorityHosts, DefaultAzureCredential

kwargs = {"cloud": "AzureChinaCloud"}
ml_client = MLClient(
subscription_id=subscription_id,
resource_group_name=resource_group,
credential=DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_CHINA),
**kwargs,
)
# [END create_ml_client_sovereign_cloud]


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

class CommandConfigurationOptions(object):
def ml_command_config(self):
from azure.identity import DefaultAzureCredential

from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential

subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_group = os.environ["RESOURCE_GROUP_NAME"]
workspace_name = "test-ws1"
credential = DefaultAzureCredential()
ml_client = MLClient(credential, subscription_id, resource_group, workspace_name="test-ws1")
ml_client = MLClient(credential, subscription_id, resource_group, workspace_name=workspace_name)

from azure.ai.ml import Input, Output
from azure.ai.ml.entities import CommandJob, CommandJobLimits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def ml_component_config(self):
from azure.ai.ml import load_component

component = load_component(
source="../tests/test_configs/components/helloworld_component.yml", params_override=[{"version": "1.0.2"}]
source="./sdk/ml/azure-ai-ml/tests/test_configs/components/helloworld_component.yml",
params_override=[{"version": "1.0.2"}],
)
registered_component = ml_client.components.create_or_update(component)
# [END configure_load_component]
Expand Down
126 changes: 72 additions & 54 deletions sdk/ml/azure-ai-ml/samples/ml_samples_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,51 @@
"""

import os
from random import randint

from azure.ai.ml import MLClient
from azure.core.exceptions import ResourceExistsError, ResourceNotFoundError
from azure.identity import DefaultAzureCredential


def handle_resource_exists_error(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except (ResourceExistsError, ResourceNotFoundError) as e:
pass

return wrapper


subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_group = os.environ["RESOURCE_GROUP_NAME"]
workspace_name = "test-ws1"
credential = DefaultAzureCredential()
ml_client = MLClient(credential, subscription_id, resource_group, workspace_name=workspace_name)

compute_name_1 = f"compute-{randint(1, 1000)}"
compute_name_2 = f"compute-{randint(1, 1000)}"
ci_name = f"ci-{randint(1, 1000)}"


class ComputeConfigurationOptions(object):
def ml_compute_config(self):
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
@handle_resource_exists_error
def ml_compute_config_setup_0(self):
# [START compute_instance]
from azure.ai.ml.entities import ComputeInstance

subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_group = os.environ["RESOURCE_GROUP_NAME"]
credential = DefaultAzureCredential()
ml_client = MLClient(credential, subscription_id, resource_group, workspace_name="test-ws1")
ci = ComputeInstance(
name=ci_name,
size="Standard_DS2_v2",
)
ml_client.compute.begin_create_or_update(ci)
# [END compute_instance]

@handle_resource_exists_error
def ml_compute_config_setup_1(self):
# [START compute_operations_get]
cpu_cluster = ml_client.compute.get("cpucluster")
cpu_cluster = ml_client.compute.get("cpu-cluster")
# [END compute_operations_get]

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

# [START compute_operations_list_nodes]
node_list = ml_client.compute.list_nodes(name="cpucluster")
node_list = ml_client.compute.list_nodes(name="cpu-cluster")
# [END compute_operations_list_nodes]

@handle_resource_exists_error
def ml_compute_config_setup_2(self):
# [START compute_operations_create_update]
from azure.ai.ml.entities import AmlCompute

compute_obj = AmlCompute(
name="example-compute",
name=compute_name_1,
tags={"key1": "value1", "key2": "value2"},
min_instances=0,
max_instances=10,
Expand All @@ -62,9 +95,13 @@ def ml_compute_config(self):
registered_compute = ml_client.compute.begin_create_or_update(compute_obj)
# [END compute_operations_create_update]

@handle_resource_exists_error
def ml_compute_config_setup_3(self):
# [START compute_operations_attach]
from azure.ai.ml.entities import AmlCompute

compute_obj = AmlCompute(
name="example-compute-2",
name=compute_name_2,
tags={"key1": "value1", "key2": "value2"},
min_instances=0,
max_instances=10,
Expand All @@ -74,52 +111,24 @@ def ml_compute_config(self):
# [END compute_operations_attach]

# [START compute_operations_update]
compute_obj = ml_client.compute.get("cpucluster")
compute_obj = ml_client.compute.get("cpu-cluster")
compute_obj.idle_time_before_scale_down = 200
updated_compute = ml_client.compute.begin_update(compute_obj)
# [END compute_operations_update]

# [START compute_operations_delete]
ml_client.compute.begin_delete("example-compute", action="Detach")

ml_client.compute.begin_delete("example-compute-2")
# [END compute_operations_delete]

compute_obj = ComputeInstance(
name="example-compute",
tags={"key1": "value1", "key2": "value2"},
min_instances=0,
max_instances=10,
idle_time_before_scale_down=100,
)
registered_compute = ml_client.compute.begin_create_or_update(compute_obj)

# [START compute_operations_start]
ml_client.compute.begin_start("example-compute")
# [END compute_operations_start]

# [START compute_operations_stop]
ml_client.compute.begin_stop("example-compute")
# [END compute_operations_stop]

# [START compute_operations_restart]
ml_client.compute.begin_stop("example-compute")
ml_client.compute.begin_restart("example-compute")
# [END compute_operations_restart]

# [START compute_operations_list_usage]
print(ml_client.compute.list_usage())
usage_list = ml_client.compute.list_usage()
# [END compute_operations_list_usage]

# [START compute_operations_list_sizes]
print(ml_client.compute.list_sizes())
size_list = ml_client.compute.list_sizes()
# [END compute_operations_list_sizes]

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

aml_compute = AmlCompute(
name="my-compute",
name="my-aml-compute",
min_instances=0,
max_instances=10,
idle_time_before_scale_down=100,
Expand Down Expand Up @@ -158,17 +167,13 @@ def ml_compute_config(self):
on_behalf_of_config = AssignedUserConfiguration(user_tenant_id="12345", user_object_id="abcdef")
# [END assigned_user_configuration]

# [START compute_instance]
from azure.ai.ml.entities import ComputeInstance
# [START compute_operations_stop]
ml_client.compute.begin_stop(ci_name)
# [END compute_operations_stop]

ci = ComputeInstance(
name="my-ci-resource",
location="eastus",
size="Standard_DS2_v2",
ssh_public_access_enabled=False,
ssh_key_value="ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZ administrator@MININT-2023",
)
# [END compute_instance]
# [START compute_operations_start]
ml_client.compute.begin_start(ci_name)
# [END compute_operations_start]

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

# [START compute_operations_restart]
ml_client.compute.begin_restart(ci_name)
# [END compute_operations_restart]

# [START compute_operations_delete]
ml_client.compute.begin_delete(compute_name_1, action="Detach")

ml_client.compute.begin_delete(compute_name_2)
# [END compute_operations_delete]


if __name__ == "__main__":
sample = ComputeConfigurationOptions()
sample.ml_compute_config()
sample.ml_compute_config_setup_0()
sample.ml_compute_config_setup_1()
sample.ml_compute_config_setup_2()
sample.ml_compute_config_setup_3()
Loading

0 comments on commit be07e08

Please sign in to comment.