WorkingDirectory icon
Clone icon
ModalCLI icon

GPU-accelerated Python script with Modal

Clone a Git repo and launch a GPU-accelerated Python script on Modal's serverless cloud with Kestra. Declarative YAML, retries, and full run lineage.

Categories
CoreData
id: gpu-modal
namespace: company.team

tasks:
  - id: python_repository
    type: io.kestra.plugin.core.flow.WorkingDirectory
    tasks:
      - id: clone_repository
        type: io.kestra.plugin.git.Clone
        branch: main
        url: https://github.com/kestra-io/scripts

      - id: modal
        type: io.kestra.plugin.modal.cli.ModalCLI
        commands:
          - modal run modal/gpu.py
        docker:
          image: ghcr.io/kestra-io/modal:latest
        env:
          MODAL_TOKEN_ID: "{{ secret('MODAL_TOKEN_ID') }}"
          MODAL_TOKEN_SECRET: "{{ secret('MODAL_TOKEN_SECRET') }}"

Run a GPU-accelerated Python script on Modal's serverless cloud without managing any GPU infrastructure yourself. This blueprint clones a Git repository, uses the Modal CLI inside a prebuilt container to launch a Python script, and lets Modal provision the GPU on demand. It solves a common machine learning and data science problem: you need GPU compute for inference, training, or batch jobs, but you do not want to keep an expensive GPU instance running or stitch together shell scripts and cron entries to trigger it.

How it works

  1. A io.kestra.plugin.core.flow.WorkingDirectory task creates a shared workspace so the cloned code and the Modal CLI run in the same directory.
  2. io.kestra.plugin.git.Clone checks out the main branch of https://github.com/kestra-io/scripts, which contains the modal/gpu.py example script.
  3. io.kestra.plugin.modal.cli.ModalCLI runs modal run modal/gpu.py from inside the ghcr.io/kestra-io/modal:latest Docker image. Modal then provisions a GPU-enabled server in its cloud, executes the script, and tears the resources down when the run finishes.
  4. Authentication is passed through the MODAL_TOKEN_ID and MODAL_TOKEN_SECRET environment variables, both sourced from Kestra secrets.

What you get

  • On-demand GPU execution with no idle GPU costs between runs.
  • A reproducible container image (ghcr.io/kestra-io/modal:latest) with the Modal CLI preinstalled.
  • Code pulled fresh from Git on every run, so the script stays in sync with your repository.
  • Full run lineage, logs, and outputs captured in the Kestra UI.

Who it's for

  • ML engineers running GPU inference or training jobs on a schedule or on demand.
  • Data scientists who want serverless GPUs without provisioning VMs.
  • Platform teams standardizing how GPU workloads are triggered and audited.

Why orchestrate this with Kestra

Modal handles the GPU provisioning, but it does not orchestrate the surrounding workflow. Kestra adds event and schedule triggers so a GPU job can fire when upstream data lands or on a cron, automatic retries when a transient cloud error occurs, run lineage across the Git clone and the Modal step, and a single declarative YAML definition that lives in version control alongside the rest of your pipelines. You get one place to see logs, replay failed runs, and chain the GPU step into larger data flows.

Prerequisites

  • A Modal account (sign up at modal.com).
  • A Kestra instance that can pull the ghcr.io/kestra-io/modal:latest image.

Secrets

  • MODAL_TOKEN_ID: your Modal API token ID.
  • MODAL_TOKEN_SECRET: your Modal API token secret.

Quick start

  1. Create a Modal account and generate an API token (token ID and secret).
  2. Store MODAL_TOKEN_ID and MODAL_TOKEN_SECRET as Kestra secrets.
  3. Add this blueprint to your namespace and execute it.
  4. Watch the logs to confirm Modal provisioned a GPU and the script ran.

How to extend

  • Point io.kestra.plugin.git.Clone at your own repository and swap modal run modal/gpu.py for your script.
  • Add a Schedule or flow trigger to run the GPU job automatically.
  • Pass inputs into the script as CLI arguments or environment variables.
  • Chain a downstream task to store model outputs in object storage or a database.

Links

Orchestrate with Kestra
Orchestrate Git with Kestra
Share this Blueprint
See How

New to Kestra?

Use blueprints to kickstart your first workflows.