import jsonimport osfrom dask.distributed import Clientfrom devitohorizon.cloud.azure import azure_vm_clusterfrom devitohorizon.data import Datasetfrom devitohorizon.model import load_modelfrom devitohorizon.physics import DevitoPhysicsfrom devitohorizon.scheduler import DaskScheduleros.environ['DEVITO_LOGGING'] = 'ERROR'Azure Dask Workflow
Shows the API for using datasets/models in Azure Blob storage, starting a Dask cluster, and running distributed gradient computations via physics.objective() with a DaskScheduler.
Prerequisites: an Azure credentials file (azure_creds.json), a zarr shot dataset, and an initial model already uploaded to blob storage. Cells that provision cloud resources are marked NBVAL_SKIP.
with open('azure_creds.json') as f: creds = json.load(f)container = creds['container_name']print('Using container:', container)param = {
'tn': 1200,
't0': 0.0,
'f0': 0.010,
'nshots': 8,
'shape': (101, 101),
'spacing': (10.0, 10.0),
'origin': (0.0, 0.0),
'nbl': 40,
'space_order': 8,
'creds': creds,
'data_path': f"{container}/synthetic-demo",
'dump_dir': f"{container}/synthetic-demo",
'gradient': {'mute': {'type': 'datum'}},
'parameters': ('m',),
'data_format': 'zarr',
}
print(param['data_path'])# NBVAL_SKIP
# Provision a VM cluster in Azure (requires credentials and quota)
cluster = azure_vm_cluster(creds, vm_size='Standard_DS1_v2', n_workers=4)
client = Client(cluster)
print('Dashboard:', client.dashboard_link)# NBVAL_SKIP# Load observed shot data from blob storage.# Replace data_path with the container path to your zarr shot dataset.dataset = Dataset.from_zarr(param['data_path'], creds=creds)print(f'Loaded {len(dataset.shot_ids)} shots from {param["data_path"]}')# NBVAL_SKIP# Load the initial velocity model from blob storage.# Replace model_path with the container path to your starting model.model_path = f"{param['dump_dir']}/initial_model.zarr"model0 = load_model(model_path, nbl=param['nbl'], space_order=param['space_order'], creds=creds)param_runtime = dict(param)param_runtime['model'] = model_path# NBVAL_SKIP
# Build Physics wrapper using the composable API
physics = DevitoPhysics(
nbl=param_runtime['nbl'],
space_order=param_runtime['space_order'],
parameters=param_runtime['parameters'],
model=param_runtime['model'],
gradient=param_runtime['gradient'],
# The model needs its own credentials and container, not just paths that
# happen to name one: they are what lets a worker open the model, and what
# tells the run it has storage every node can reach. Without them the
# staged model and the compiled operators go to a local directory the
# workers cannot see.
creds=creds,
container_name=container,
)
print('Shots available:', len(dataset))# NBVAL_SKIP# Submit distributed gradient computation via Daskshot_ids = [0, 2, 4, 6]shots = [dataset[i] for i in shot_ids]scheduler = DaskScheduler(client)misfit, grads = physics.objective(shots, scheduler=scheduler)stacked = next(iter(grads.values()))print('Average misfit:', misfit)client.close()cluster.close()