Edit

Share via


Upgrade script run to SDK v2

In SDK v2, "experiments" and "runs" are consolidated into jobs.

A job has a type. Most jobs are command jobs that run a command, like python main.py. What runs in a job is agnostic to any programming language, so you can run bash scripts, invoke python interpreters, run a bunch of curl commands, or anything else.

To upgrade, change your code for submitting jobs to SDK v2. You don't need to upgrade what you run within the job to SDK v2. However, remove any code specific to Azure Machine Learning from your model training scripts. This separation allows for an easier transition between local and cloud and is considered best practice for mature MLOps. In practice, this means removing azureml.* lines of code. Replace model logging and tracking code with MLflow. For more details, see how to use MLflow in v2.

This article gives a comparison of scenarios in SDK v1 and SDK v2.

Submit a script run

  • SDK v1

    from azureml.core import Workspace, Experiment, Environment, ScriptRunConfig
    
    # connect to the workspace
    ws = Workspace.from_config()
    
    # define and configure the experiment
    experiment = Experiment(workspace=ws, name='day1-experiment-train')
    config = ScriptRunConfig(source_directory='./src',
                                script='train.py',
                                compute_target='cpu-cluster')
    
    # set up pytorch environment
    env = Environment.from_conda_specification(
        name='pytorch-env',
        file_path='pytorch-env.yml')
    config.run_config.environment = env
    
    run = experiment.submit(config)
    
    aml_url = run.get_portal_url()
    print(aml_url)
    
  • SDK v2

    #import required libraries
    from azure.ai.ml import MLClient, command
    from azure.ai.ml.entities import Environment
    from azure.identity import DefaultAzureCredential
    
    #connect to the workspace
    ml_client = MLClient.from_config(credential=DefaultAzureCredential())
    
    # set up pytorch environment
    env = Environment(
        image="mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu22.04",
        conda_file="pytorch-env.yml",
        name="pytorch-env"
    )
    
    # define the command
    command_job = command(
        code="./src",
        command="python train.py",
        environment=env,
        compute="cpu-cluster",
    )
    
    returned_job = ml_client.jobs.create_or_update(command_job)
    print(f"Job submitted. View in studio: {returned_job.studio_url}")
    

Mapping of key functionality in v1 and v2

Functionality in SDK v1 Rough mapping in SDK v2
experiment.submit MLClient.jobs.create_or_update
ScriptRunConfig() command()

Next steps

For more information, see: