ComputeTarget Class

Abstract parent class for all compute targets managed by Azure Machine Learning.

A compute target is a designated compute resource/environment where you run your training script or host your service deployment. This location may be your local machine or a cloud-based compute resource. For more information, see What are compute targets in Azure Machine Learning?

Class ComputeTarget constructor.

Retrieve a cloud representation of a Compute object associated with the provided workspace. Returns an instance of a child class corresponding to the specific type of the retrieved Compute object.

Inheritance
ComputeTarget

Constructor

ComputeTarget(workspace, name)

Parameters

workspace
Workspace
Required

The workspace object containing the Compute object to retrieve.

name
str
Required

The name of the Compute object to retrieve.

workspace
Workspace
Required

The workspace object containing the Compute object to retrieve.

name
str
Required

The name of the of the Compute object to retrieve.

Remarks

Use the ComputeTarget constructor to retrieve the cloud representation of a Compute object associated with the provided workspace. The constructor returns an instance of a child class corresponding to the specific type of the retrieved Compute object. If the Compute object is not found, a ComputeTargetException is raised.

Methods

attach

Attach a Compute object to a workspace using the specified name and configuration information.

create

Provision a Compute object by specifying a compute type and related configuration.

This method creates a new compute target rather than attaching an existing one.

delete

Remove the Compute object from its associated workspace.

This abstract method is implemented by child classes of ComputeTarget.

deserialize

Convert a JSON object into a Compute object.

detach

Detach the Compute object from its associated workspace.

This abstract method is implemented by child classes of ComputeTarget. Underlying cloud objects are not deleted, only their associations are removed.

get_status

Retrieve the current provisioning state of the Compute object.

list

List all ComputeTarget objects within the workspace.

Return a list of instantiated child objects corresponding to the specific type of Compute. Objects are children of ComputeTarget.

refresh_state

Perform an in-place update of the properties of the object.

Update properties based on the current state of the corresponding cloud object. This is useful for manual polling of compute state.

This abstract method is implemented by child classes of ComputeTarget.

serialize

Convert this Compute object into a JSON serialized dictionary.

wait_for_completion

Wait for the current provisioning operation to finish on the cluster.

This method returns a ComputeTargetException if there is a problem polling the compute object.

attach

Attach a Compute object to a workspace using the specified name and configuration information.

static attach(workspace, name, attach_configuration)

Parameters

workspace
Workspace
Required

The workspace object to attach the Compute object to.

name
str
Required

The name to associate with the Compute object.

attach_configuration
ComputeTargetAttachConfiguration
Required

A ComputeTargetAttachConfiguration object that is used to determine the type of Compute object to attach, and how to configure it.

Returns

An instance of a child of ComputeTarget corresponding to the type of object attached.

Return type

Exceptions

Remarks

The type of object to pass to the parameter attach_configuration is a ComputeTargetAttachConfiguration object built using the attach_configuration function on any of the child classes of ComputeTarget.

The following example shows how to attach an ADLA account to a workspace using the attach_configuration method of AdlaCompute.


   adla_compute_name = 'testadl' # Name to associate with new compute in workspace

   # ADLA account details needed to attach as compute to workspace
   adla_account_name = "<adla_account_name>" # Name of the Azure Data Lake Analytics account
   adla_resource_group = "<adla_resource_group>" # Name of the resource group which contains this account

   try:
       # check if already attached
       adla_compute = AdlaCompute(ws, adla_compute_name)
   except ComputeTargetException:
       print('attaching adla compute...')
       attach_config = AdlaCompute.attach_configuration(resource_group=adla_resource_group, account_name=adla_account_name)
       adla_compute = ComputeTarget.attach(ws, adla_compute_name, attach_config)
       adla_compute.wait_for_completion()

   print("Using ADLA compute:{}".format(adla_compute.cluster_resource_id))
   print("Provisioning state:{}".format(adla_compute.provisioning_state))
   print("Provisioning errors:{}".format(adla_compute.provisioning_errors))

Full sample is available from https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-use-adla-as-compute-target.ipynb

create

Provision a Compute object by specifying a compute type and related configuration.

This method creates a new compute target rather than attaching an existing one.

static create(workspace, name, provisioning_configuration)

Parameters

workspace
Workspace
Required

The workspace object to create the Compute object under.

name
str
Required

The name to associate with the Compute object.

provisioning_configuration
ComputeTargetProvisioningConfiguration
Required

A ComputeTargetProvisioningConfiguration object that is used to determine the type of Compute object to provision, and how to configure it.

Returns

An instance of a child of ComputeTarget corresponding to the type of object provisioned.

Return type

Exceptions

Remarks

The type of object provisioned is determined by the provisioning configuration provided.

In the following example, a persistent compute target provisioned by AmlCompute is created. The provisioning_configuration parameter in this example is of type AmlComputeProvisioningConfiguration.


   from azureml.core.compute import ComputeTarget, AmlCompute
   from azureml.core.compute_target import ComputeTargetException

   # Choose a name for your CPU cluster
   cpu_cluster_name = "cpu-cluster"

   # Verify that cluster does not exist already
   try:
       cpu_cluster = ComputeTarget(workspace=ws, name=cpu_cluster_name)
       print('Found existing cluster, use it.')
   except ComputeTargetException:
       compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_D2_V2',
                                                              max_nodes=4)
       cpu_cluster = ComputeTarget.create(ws, cpu_cluster_name, compute_config)

   cpu_cluster.wait_for_completion(show_output=True)

Full sample is available from https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/training/train-on-amlcompute/train-on-amlcompute.ipynb

delete

Remove the Compute object from its associated workspace.

This abstract method is implemented by child classes of ComputeTarget.

abstract delete()

Exceptions

Remarks

If this object was created through Azure Machine Learning, the corresponding cloud-based objects will also be deleted. If this object was created externally and only attached to the workspace, this method raises an exception and nothing is changed.

deserialize

Convert a JSON object into a Compute object.

abstract static deserialize(workspace, object_dict)

Parameters

workspace
Workspace
Required

The workspace object the Compute object is associated with.

object_dict
dict
Required

A JSON object to convert to a Compute object.

Returns

The Compute representation of the provided JSON object.

Return type

Exceptions

Remarks

Raises a ComputeTargetException if the provided workspace is not the workspace the Compute is associated with.

detach

Detach the Compute object from its associated workspace.

This abstract method is implemented by child classes of ComputeTarget. Underlying cloud objects are not deleted, only their associations are removed.

abstract detach()

Exceptions

get_status

Retrieve the current provisioning state of the Compute object.

get_status()

Returns

The current provisioning_state.

Return type

str

Exceptions

Remarks

Values returned are listed in the Azure REST API Reference for ProvisioningState.

list

List all ComputeTarget objects within the workspace.

Return a list of instantiated child objects corresponding to the specific type of Compute. Objects are children of ComputeTarget.

static list(workspace)

Parameters

workspace
Workspace
Required

The workspace object containing the objects to list.

Returns

List of compute targets within the workspace.

Return type

Exceptions

refresh_state

Perform an in-place update of the properties of the object.

Update properties based on the current state of the corresponding cloud object. This is useful for manual polling of compute state.

This abstract method is implemented by child classes of ComputeTarget.

abstract refresh_state()

Exceptions

serialize

Convert this Compute object into a JSON serialized dictionary.

abstract serialize()

Returns

The JSON representation of this Compute object.

Return type

Exceptions

wait_for_completion

Wait for the current provisioning operation to finish on the cluster.

This method returns a ComputeTargetException if there is a problem polling the compute object.

wait_for_completion(show_output=False, is_delete_operation=False)

Parameters

show_output
bool
default value: False

Indicates whether to provide more verbose output.

is_delete_operation
bool
default value: False

Indicates whether the operation is meant for deleting.

Exceptions