For pipelines that support Python based execution you can directly use the TorchX API. TorchX is designed to be easily integrated in to other applications via the programmatic API. No special Airflow integrations are needed.

With TorchX, you can use Airflow for the pipeline orchestration and run your PyTorch application (i.e. distributed training) on a remote GPU cluster.

import datetime
import pendulum

from airflow.utils.state import DagRunState, TaskInstanceState
from airflow.utils.types import DagRunType
from airflow.models.dag import DAG
from airflow.decorators import task

DATA_INTERVAL_START = pendulum.datetime(2021, 9, 13, tz="UTC")
DATA_INTERVAL_END = DATA_INTERVAL_START + datetime.timedelta(days=1)

To launch a TorchX job from Airflow you can create a Airflow Python task to import the runner, launch the job and wait for it to complete. If you’re running on a remote cluster you may need to use the virtualenv task to install the torchx package.

def run_torchx(message):
    """This is a function that will run within the DAG execution"""
    from torchx.runner import get_runner
    with get_runner() as runner:
        # Run the component on the local_cwd scheduler.
        app_id = runner.run_component(
            ["echo", message],

        # Wait for the the job to complete
        status = runner.wait(app_id, wait_interval=1)

        # Raise_for_status will raise an exception if the job didn't succeed

        # Finally we can print all of the log lines from the TorchX job so it
        # will show up in the workflow logs.
        for line in runner.log_lines(app_id, "sh", k=0):
            print(line, end="")

Once we have the task defined we can put it into a Airflow DAG and run it like normal.

from torchx.schedulers.ids import make_unique

with DAG(
) as dag:
    run_job = run_torchx("Hello, TorchX!")

dagrun = dag.create_dagrun(
ti = dagrun.get_task_instance(task_id="hello_torchx")
ti.task = dag.get_task(task_id="hello_torchx")
assert ti.state == TaskInstanceState.SUCCESS
/tmp/ipykernel_4040/ RemovedInAirflow3Warning: Param `schedule_interval` is deprecated and will be removed in a future release. Please use `schedule` instead.
[2023-10-19T01:33:50.915+0000] {} INFO - Dependencies all met for dep_context=non-requeueable deps ti=<TaskInstance: example_python_operator-kw25q9fcp19sdd.hello_torchx manual__2021-09-13T00:00:00+00:00 [None]>
[2023-10-19T01:33:50.920+0000] {} INFO - Dependencies all met for dep_context=requeueable deps ti=<TaskInstance: example_python_operator-kw25q9fcp19sdd.hello_torchx manual__2021-09-13T00:00:00+00:00 [None]>
[2023-10-19T01:33:50.921+0000] {} INFO - Starting attempt 1 of 1
[2023-10-19T01:33:50.922+0000] {} WARNING - cannot record queued_duration for task hello_torchx because previous state change time has not been saved
[2023-10-19T01:33:50.931+0000] {} INFO - Executing <Task(_PythonDecoratedOperator): hello_torchx> on 2021-09-13 00:00:00+00:00
[2023-10-19T01:33:51.168+0000] {} INFO - Exporting env vars: AIRFLOW_CTX_DAG_OWNER='airflow' AIRFLOW_CTX_DAG_ID='example_python_operator-kw25q9fcp19sdd' AIRFLOW_CTX_TASK_ID='hello_torchx' AIRFLOW_CTX_EXECUTION_DATE='2021-09-13T00:00:00+00:00' AIRFLOW_CTX_TRY_NUMBER='1' AIRFLOW_CTX_DAG_RUN_ID='manual__2021-09-13T00:00:00+00:00'
[2023-10-19T01:33:51.787+0000] {} INFO - Tracker configurations: {}
[2023-10-19T01:33:51.791+0000] {} INFO - Log directory not set in scheduler cfg. Creating a temporary log dir that will be deleted on exit. To preserve log directory set the `log_dir` cfg option
[2023-10-19T01:33:51.791+0000] {} INFO - Log directory is: /tmp/torchx_okvtgkb3
Hello, TorchX!
[2023-10-19T01:33:51.898+0000] {} INFO - Done. Returned value was: None
[2023-10-19T01:33:51.903+0000] {} INFO - Marking task as SUCCESS. dag_id=example_python_operator-kw25q9fcp19sdd, task_id=hello_torchx, execution_date=20210913T000000, start_date=20231019T013350, end_date=20231019T013351

If all goes well you should see Hello, TorchX! printed above.

Next Steps


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources