|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import os |
| 8 | +from datetime import datetime, timedelta |
| 9 | + |
| 10 | +import boto3.session |
| 11 | + |
| 12 | +from airflow import DAG |
| 13 | +from airflow.operators.bash import BashOperator |
| 14 | +from airflow.operators.python import PythonOperator |
| 15 | +from airflow.providers.amazon.aws.hooks.batch_waiters import BatchWaitersHook |
| 16 | + |
| 17 | +REGION = "us-west-2" |
| 18 | +JOB_QUEUE = "torchx-gpu" |
| 19 | +ECR_URL = os.environ["ECR_URL"] |
| 20 | + |
| 21 | + |
| 22 | +default_args = { |
| 23 | + "depends_on_past": False, |
| 24 | + |
| 25 | + "email_on_failure": False, |
| 26 | + "email_on_retry": False, |
| 27 | + "retries": 0, |
| 28 | + "retry_delay": timedelta(minutes=5), |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +dag = DAG( |
| 33 | + "train_charnn", |
| 34 | + default_args=default_args, |
| 35 | + description="A DAG to train charnn in AWS Batch", |
| 36 | + schedule_interval="@daily", |
| 37 | + catchup=False, |
| 38 | + start_date=datetime(2022, 8, 1), |
| 39 | + tags=["aws_batch"], |
| 40 | +) |
| 41 | + |
| 42 | + |
| 43 | +# This example uses torchx CLI with BashOperator. |
| 44 | +# We can also use PythonOperator to achive it. |
| 45 | +train = BashOperator( |
| 46 | + task_id="train", |
| 47 | + bash_command=f"""AWS_DEFAULT_REGION=$REGION \ |
| 48 | + torchx run --workspace '' -s aws_batch \ |
| 49 | + -cfg queue={JOB_QUEUE},image_repo={ECR_URL}/charnn dist.ddp \ |
| 50 | + --script charnn/main.py --image {ECR_URL}/charnn:latest \ |
| 51 | + --cpu 8 --gpu 2 -j 1x2 --memMB 20480 2>&1 \ |
| 52 | + | grep -Eo aws_batch://torchx/{JOB_QUEUE}:main-[a-z0-9]+""", |
| 53 | + env={ |
| 54 | + "REGION": REGION, |
| 55 | + "JOB_QUEUE": JOB_QUEUE, |
| 56 | + "ECR_URL": ECR_URL, |
| 57 | + }, |
| 58 | + append_env=True, |
| 59 | + dag=dag, |
| 60 | + do_xcom_push=True, |
| 61 | +) |
| 62 | + |
| 63 | + |
| 64 | +def wait_for_batch_job(**context) -> bool: |
| 65 | + session = boto3.session.Session() |
| 66 | + client = session.client("batch", region_name=REGION) |
| 67 | + # XComs are a mechanism that let Tasks talk to each other |
| 68 | + # Learn more from https://airflow.apache.org/docs/apache-airflow/stable/concepts/xcoms.html |
| 69 | + job = context["ti"].xcom_pull(task_ids="train") |
| 70 | + job_desc = job.split("/")[-1] |
| 71 | + queue_name, job_name = job_desc.split(":") |
| 72 | + job_id = client.list_jobs( |
| 73 | + jobQueue=queue_name, |
| 74 | + filters=[{"name": "JOB_NAME", "values": [job_name]}], |
| 75 | + )["jobSummaryList"][0]["jobId"] |
| 76 | + waiter = BatchWaitersHook(region_name=REGION) |
| 77 | + try: |
| 78 | + waiter.wait_for_job(job_id) |
| 79 | + return True |
| 80 | + except Exception: |
| 81 | + return False |
| 82 | + |
| 83 | + |
| 84 | +wait_for_job = PythonOperator( |
| 85 | + task_id="wait_for_job", |
| 86 | + python_callable=wait_for_batch_job, |
| 87 | + dag=dag, |
| 88 | +) |
| 89 | + |
| 90 | + |
| 91 | +parse_output = BashOperator( |
| 92 | + task_id="parse_output", |
| 93 | + bash_command="echo {{ ti.xcom_pull(task_ids='wait_for_job') }}", |
| 94 | + dag=dag, |
| 95 | +) |
| 96 | + |
| 97 | + |
| 98 | +train >> wait_for_job >> parse_output |
0 commit comments