Skip to main content

modal-gpu-retry

modal-gpu-retry: change modal.App to modal_gpu_retry.App and add retries=[...]

Install

pip install modal-gpu-retry

What it does

Modal's native retries= reruns a failed job on the same hardware, which doesn't help with OOM errors since the rerun will just run out of memory again. modal-gpu-retry reuses the same retries argument but takes a list of GPUs and escalates to the next one on each failure.

@app.function(gpu="L40S", retries=["A100", "H100"])

If the job OOMs on the L40S it reruns on the A100, then the H100.

On OOM, each retry escalates to the next GPU: L40S to A100 to H100

Usage

To integrate with your existing Modal scripts, there are exactly two changes needed to be made. Modify the app initialization to use modal_gpu_retry instead of modal:

app = modal.App("my-evals")             # before
app = modal_gpu_retry.App("my-evals")   # after

and give the decorator a list-valued retries=[...] containing the fallback GPUs:

@app.function(gpu="L40S", image=image)                            # before
@app.function(gpu="L40S", retries=["A100", "H100"], image=image)  # after

Your code should look like this now:

import modal
import modal_gpu_retry

app = modal_gpu_retry.App("my-evals")
image = modal.Image.debian_slim().pip_install("torch")

@app.function(gpu="L40S", retries=["A100", "H100"], image=image)
def run_eval(config):
    ...  # if this OOMs on L40S, it runs again on A100, then H100

@app.local_entrypoint()
def main():
    results = list(run_eval.map(configs))

You can run it as normal with the Modal CLI, for example modal run evals.py works just fine.

The retries= value decides the behavior:

  • retries=3: native Modal (rerun the same GPU).
  • retries=["A100", "H100"]: escalate to a bigger GPU each time.
  • retries=[]: same as retries=0.

Exhausted jobs come back in place

If a job fails on every GPU, you get a GPURetryExhausted in the results instead of an exception, so one bad job doesn't kill the batch:

results = list(run_eval.map(configs))
dead = [c for c, r in zip(configs, results, strict=True)
        if isinstance(r, modal_gpu_retry.GPURetryExhausted)]

Detached runs

.remote, .map, and .starmap run the retry loop in your process, so it stops if you disconnect. modal run --detach doesn't help: it keeps the app alive but the loop still runs locally. For escalation that survives a disconnect, modal deploy your app and use .spawn_map, which runs the loop in a CPU orchestrator on Modal:

handle = run_eval.spawn_map(configs)
results = handle.get()   # later, or from a different process

Reconnect from anywhere with modal_gpu_retry.GPURetryCall.from_id(call_id).

Notes

  • A few modal run CLI patterns behave unexpectedly, such as targeting the wrapped function directly. See the examples README.
  • Your class/function appears in the Modal dashboard under a _mgr_real_ prefix; that's how the wrapper keeps your call sites unchanged without breaking how Modal loads your class in the container.
  • It works on @app.cls too, and .remote, .map, and .starmap keep their usual call sites.
  • You don't need to modify your image because the library will install itself on the image you pass into modal_gpu_retry.App via image= automatically.

License

MIT. This is a community wrapper around the modal SDK and isn't affiliated with Modal.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modal_gpu_retry-0.0.4.tar.gz (259.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modal_gpu_retry-0.0.4-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 files

0.0.3

2 files

0.0.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page