File: ml_samples_compute.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (285 lines) | stat: -rw-r--r-- 10,144 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: ml_samples_compute_configurations.py
DESCRIPTION:
    These samples demonstrate different ways to configure Compute.
USAGE:
    python ml_samples_compute_configurations.py

"""

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)}-sample"
compute_name_2 = f"compute-{randint(1, 1000)}-sample"
ci_name = f"ci-{randint(1, 1000)}-sample"


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

        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("cpu-cluster")
        # [END compute_operations_get]

        # [START load_compute]
        from azure.ai.ml import load_compute

        compute = load_compute(
            "../tests/test_configs/compute/compute-vm.yaml",
            params_override=[{"description": "loaded from compute-vm.yaml"}],
        )
        # [END load_compute]

        # [START compute_operations_list]
        compute_list = ml_client.compute.list(compute_type="AMLK8s")  # cspell:disable-line
        # [END compute_operations_list]

        # [START compute_operations_list_nodes]
        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=compute_name_1,
            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)
        # [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=compute_name_2,
            tags={"key1": "value1", "key2": "value2"},
            min_instances=0,
            max_instances=10,
            idle_time_before_scale_down=100,
        )
        attached_compute = ml_client.compute.begin_attach(compute_obj)
        # [END compute_operations_attach]

        # [START compute_operations_update]
        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_list_usage]
        usage_list = ml_client.compute.list_usage()
        # [END compute_operations_list_usage]

        # [START compute_operations_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-aml-compute",
            min_instances=0,
            max_instances=10,
            idle_time_before_scale_down=100,
            identity=IdentityConfiguration(
                type="UserAssigned",
                user_assigned_identities=[
                    ManagedIdentityConfiguration(
                        resource_id="/subscriptions/1234567-abcd-ef12-1234-12345/resourcegroups/our_rg_eastus/providers/Microsoft.ManagedIdentity/userAssignedIdentities/our-agent-aks"
                    )
                ],
            ),
        )
        # [END amlcompute]

        # [START aml_compute_ssh_settings]
        from azure.ai.ml.entities import AmlComputeSshSettings

        ssh_settings = AmlComputeSshSettings(
            admin_username="azureuser",
            ssh_key_value="ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZ administrator@MININT-2023",
            admin_password="password123",
        )
        # [END aml_compute_ssh_settings]

        # [START compute_instance_ssh_settings]
        from azure.ai.ml.entities import ComputeInstanceSshSettings

        ssh_settings = ComputeInstanceSshSettings(  # type:ignore
            ssh_key_value="ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZ administrator@MININT-2023"
        )
        # [END compute_instance_ssh_settings]

        # [START assigned_user_configuration]
        from azure.ai.ml.entities import AssignedUserConfiguration

        on_behalf_of_config = AssignedUserConfiguration(user_tenant_id="12345", user_object_id="abcdef")
        # [END assigned_user_configuration]

        # [START compute_operations_stop]
        ml_client.compute.begin_stop(ci_name)
        # [END compute_operations_stop]

        # [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

        ssh_settings = VirtualMachineSshSettings(  # type:ignore
            admin_username="azureuser",
            admin_password="azureuserpassword",
            ssh_port=8888,
            ssh_private_key_file="../tests/test_configs/compute/ssh_fake_key.txt",
        )
        # [END vm_ssh_settings]

        # [START vm_compute]
        from azure.ai.ml.entities import VirtualMachineCompute

        vm_compute = VirtualMachineCompute(
            name="vm-compute",
            resource_id="/subscriptions/123456-1234-1234-1234-123456789/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachines/my-vm",
            ssh_settings=ssh_settings,  # type:ignore
        )
        # [END vm_compute]

        # [START network_settings]
        from azure.ai.ml.entities import (
            AmlCompute,
            IdentityConfiguration,
            ManagedIdentityConfiguration,
            NetworkSettings,
        )

        aml_compute = AmlCompute(
            name="my-compute",
            min_instances=0,
            max_instances=10,
            idle_time_before_scale_down=100,
            network_settings=NetworkSettings(vnet_name="my-vnet", subnet="default"),
        )
        # [END network_settings]

        # [START compute_runtime]
        from azure.ai.ml.entities import ComputeRuntime

        compute_runtime = ComputeRuntime(spark_runtime_version="3.4.0")
        # [END compute_runtime]

        # [START compute_start_stop_schedule]
        from azure.ai.ml.constants import TimeZone
        from azure.ai.ml.entities import ComputeSchedules, ComputeStartStopSchedule, CronTrigger

        start_stop = ComputeStartStopSchedule(
            trigger=CronTrigger(
                expression="15 10 * * 1",
                start_time="2022-03-10 10:15:00",
                end_time="2022-06-10 10:15:00",
                time_zone=TimeZone.PACIFIC_STANDARD_TIME,
            )
        )
        compute_schedules = ComputeSchedules(compute_start_stop=[start_stop])

        # [END compute_start_stop_schedule]

        # [START image_metadata]
        from azure.ai.ml.entities import ImageMetadata

        os_image_metadata = ImageMetadata(
            current_image_version="22.08.19",
            latest_image_version="22.08.20",
            is_latest_os_image_version=False,
        )
        # [END image_metadata]

        # [START kubernetes_compute]
        from azure.ai.ml.entities import KubernetesCompute

        k8s_compute = KubernetesCompute(
            identity=IdentityConfiguration(
                type="UserAssigned",
                user_assigned_identities=[
                    ManagedIdentityConfiguration(
                        resource_id="/subscriptions/1234567-abcd-ef12-1234-12345/resourcegroups/our_rg_eastus/providers/Microsoft.ManagedIdentity/userAssignedIdentities/our-agent-aks"
                    )
                ],
            ),
        )
        # [END kubernetes_compute]

        # [START materialization_compute_resource]
        from azure.ai.ml.entities import MaterializationComputeResource

        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_setup_0()
    sample.ml_compute_config_setup_1()
    sample.ml_compute_config_setup_2()
    sample.ml_compute_config_setup_3()