How to fetch child job models from automl job in sdkv2 ??

Aryan Rastogi 0 Reputation points
2025-05-07T08:48:00.0433333+00:00

In SDK v1 this is the way to fetch the fitted_model using get_output function. How to do it in SDK v2 as ml_client.jobs.get(child_job_id) doesn't work.

	match = re.search(r'\_(\d+)$', child_run_id)
    iteration = match.group(1)
    assert iteration.isdigit(), "Invalid iteration number"
    automl_run = AutoMLRun(experiment, run_id=best_run_id)
    best_run, fitted_model = automl_run.get_output(iteration=iteration)
Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
3,261 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Manas Mohanty 3,700 Reputation points Microsoft External Staff Moderator
    2025-05-07T12:03:51.6733333+00:00

    Hi Aryan Rastogi

    If you are trying to register and fit the best model from all child jobs in SDK v2, You can refer below code.

    It fetches the run id of best child job with "automl_best_child_run" then download the artifacts and register the mlflow model from the artifacts.

    # Get the best model's child run
    best_child_run_id = mlflow_parent_run.data.tags["automl_best_child_run_id"]
    print(f"Found best child run id: {best_child_run_id}")
    best_run = mlflow_client.get_run(best_child_run_id)
    print("Best child run: ")
    print(best_run)
    
    # Create local folder
    local_dir = "./artifact_downloads"
    if not os.path.exists(local_dir):
        os.mkdir(local_dir)
    
    # Download run's artifacts/outputs
    local_path = mlflow_client.download_artifacts(
        best_run.info.run_id, "outputs", local_dir
    )
    print(f"Artifacts downloaded in: {local_path}")
    print(f"Artifacts: {os.listdir(local_path)}")
    
    #registers model from job artifacts
    
    model_name = "od-fridge-items-mlflow-model"
    model = Model(
        path=f"azureml://jobs/{best_run.info.run_id}/outputs/artifacts/outputs/mlflow-model/",
        name=model_name,
        description="my sample object detection model",
        type=AssetTypes.MLFLOW_MODEL,
    )
    # for downloaded file
    # model = Model(
    #     path=mlflow_model_dir,
    #     name=model_name,
    #     description="my sample object detection model",
    #     type=AssetTypes.MLFLOW_MODEL,
    # )
    registered_model = ml_client.models.create_or_update(model)
    

    Reference used -

    https://github.com/Azure/azureml-examples/blob/main/sdk/python/jobs/automl-standalone-jobs/automl-classification-task-bankmarketing/automl-classification-task-bankmarketing.ipynb

    https://learn.microsoft.com/en-us/azure/machine-learning/tutorial-auto-train-image-models?view=azureml-api-2&tabs=python#get-the-best-trial

    I think we can change the tag to tag of other child job to download respective model artifact. From UI, we can download all models from models and Job tab of parent job

    Trying to repro and update you. Please allow us some time.

    Thank you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.