|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from datetime import timedelta, datetime |
| 7 | + |
| 8 | +from termcolor import cprint |
| 9 | + |
| 10 | +from azureml.core import Experiment, ScriptRunConfig |
| 11 | +from azureml.core.runconfig import MpiConfiguration |
| 12 | + |
| 13 | +from common import ( |
| 14 | + get_or_create_workspace, |
| 15 | + create_or_update_environment, |
| 16 | + create_or_update_cluster, |
| 17 | +) |
| 18 | + |
| 19 | +import sharedconfig |
| 20 | + |
| 21 | +k_runclass = "BeeOND" |
| 22 | +k_beeond_map = "/data" |
| 23 | + |
| 24 | + |
| 25 | +with open("clusterkey.pub", "rt") as fh: |
| 26 | + sharedconfig.ssh_key = fh.readline() |
| 27 | + |
| 28 | + |
| 29 | +def generate_training_opts(sas, beeond_map, stage): |
| 30 | + """Populate common Mask RCNN command line options""" |
| 31 | + opts = ["--output-dir", "./outputs"] |
| 32 | + opts.extend(["--data-dir", beeond_map + "/cosmoflow/cosmoUniverse_2019_05_4parE_tf"]) |
| 33 | + opts.extend(["--rank-gpu"]) |
| 34 | + opts.extend(["--distributed"]) |
| 35 | + opts.extend(["--verbose"]) |
| 36 | + opts.extend(["--account", sharedconfig.storage_account]) |
| 37 | + opts.extend(["--container", sharedconfig.storage_container]) |
| 38 | + opts.extend(["--sas", sas]) |
| 39 | + if stage: |
| 40 | + opts.extend(["--beeond-stage"]) |
| 41 | + |
| 42 | + opts.extend(["configs/cosmo_runs_gpu.yaml"]) |
| 43 | + |
| 44 | + return opts |
| 45 | + |
| 46 | + |
| 47 | +def generate_sas(): |
| 48 | + """Generate a short-lived sas for dataset download via az cli""" |
| 49 | + exp = (datetime.utcnow() + timedelta(hours=1)).isoformat("T", "minutes") |
| 50 | + # fmt: off |
| 51 | + sas_gen_cmd = [ |
| 52 | + "az", "storage", "account", "generate-sas", |
| 53 | + "--account-name", sharedconfig.storage_account, |
| 54 | + "--services", "b", |
| 55 | + "--permissions", "rl", |
| 56 | + "--resource-types", "co", |
| 57 | + "--expiry", exp + 'Z', |
| 58 | + "--output", "tsv" |
| 59 | + ] |
| 60 | + # fmt: on |
| 61 | + |
| 62 | + sasres = subprocess.run(sas_gen_cmd, capture_output=True) |
| 63 | + |
| 64 | + return sasres.stdout.strip() |
| 65 | + |
| 66 | + |
| 67 | +def main(): |
| 68 | + |
| 69 | + parser = argparse.ArgumentParser( |
| 70 | + description="Submit Cosmoflow to BeeOND enabled cluster" |
| 71 | + ) |
| 72 | + |
| 73 | + parser.add_argument("num_nodes", type=int, help="Number of nodes") |
| 74 | + parser.add_argument("--follow", action="store_true", help="Follow run output") |
| 75 | + parser.add_argument( |
| 76 | + "--keep-cluster", |
| 77 | + action="store_true", |
| 78 | + help="Don't autoscale cluster down when idle (after run completed)", |
| 79 | + ) |
| 80 | + parser.add_argument( |
| 81 | + "--epochs", |
| 82 | + type=int, |
| 83 | + default=sharedconfig.default_epochs, |
| 84 | + help="Number of training iterations", |
| 85 | + ) |
| 86 | + parser.add_argument( |
| 87 | + "--keep-failed-cluster", dest="terminate_on_failure", action="store_false" |
| 88 | + ) |
| 89 | + parser.add_argument("--skip-staging", action="store_false", dest="stage") |
| 90 | + |
| 91 | + args = parser.parse_args() |
| 92 | + |
| 93 | + workspace = get_or_create_workspace( |
| 94 | + sharedconfig.subscription_id, |
| 95 | + sharedconfig.resource_group_name, |
| 96 | + sharedconfig.workspace_name, |
| 97 | + sharedconfig.location, |
| 98 | + ) |
| 99 | + |
| 100 | + try: |
| 101 | + clusterconnector = create_or_update_cluster( |
| 102 | + workspace, |
| 103 | + sharedconfig.cluster_name, |
| 104 | + args.num_nodes, |
| 105 | + sharedconfig.ssh_key, |
| 106 | + sharedconfig.vm_type, |
| 107 | + terminate_on_failure=args.terminate_on_failure, |
| 108 | + use_beeond=True, |
| 109 | + ) |
| 110 | + except RuntimeError: |
| 111 | + cprint("Fatal Error - exiting", "red", attrs=["bold"]) |
| 112 | + sys.exit(-1) |
| 113 | + |
| 114 | + docker_args = ["-v", "{}:{}".format(clusterconnector.beeond_mnt, k_beeond_map)] |
| 115 | + |
| 116 | + # Get and update the AzureML Environment object |
| 117 | + environment = create_or_update_environment( |
| 118 | + workspace, sharedconfig.environment_name, sharedconfig.docker_image, docker_args |
| 119 | + ) |
| 120 | + |
| 121 | + # Get/Create an experiment object |
| 122 | + experiment = Experiment(workspace=workspace, name=sharedconfig.experiment_name) |
| 123 | + |
| 124 | + # Configure the distributed compute settings |
| 125 | + parallelconfig = MpiConfiguration( |
| 126 | + node_count=args.num_nodes, process_count_per_node=sharedconfig.gpus_per_node |
| 127 | + ) |
| 128 | + |
| 129 | + # Collect arguments to be passed to training script |
| 130 | + script_args = generate_training_opts( |
| 131 | + generate_sas().decode(), k_beeond_map, args.stage |
| 132 | + ) |
| 133 | + |
| 134 | + # Define the configuration for running the training script |
| 135 | + script_conf = ScriptRunConfig( |
| 136 | + source_directory="cosmoflow-benchmark", |
| 137 | + script="train.py", |
| 138 | + compute_target=clusterconnector.cluster, |
| 139 | + environment=environment, |
| 140 | + arguments=script_args, |
| 141 | + distributed_job_config=parallelconfig, |
| 142 | + ) |
| 143 | + |
| 144 | + # We can use these tags make a note of run parameters (avoids grepping the logs) |
| 145 | + runtags = { |
| 146 | + "class": k_runclass, |
| 147 | + "vmtype": sharedconfig.vm_type, |
| 148 | + "num_nodes": args.num_nodes, |
| 149 | + "ims_per_gpu": sharedconfig.ims_per_gpu, |
| 150 | + "epochs": args.epochs, |
| 151 | + } |
| 152 | + |
| 153 | + # Submit the run |
| 154 | + run = experiment.submit(config=script_conf, tags=runtags) |
| 155 | + |
| 156 | + # Can optionally choose to follow the output on the command line |
| 157 | + if args.follow: |
| 158 | + run.wait_for_completion(show_output=True) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + main() |
0 commit comments