Modal images explained
Quick answer
Modal images are managed using the
modal Python package, which allows you to run GPU-accelerated image generation or processing tasks in serverless containers. You define a @app.function with GPU support and invoke it with image prompts or data to generate or manipulate images efficiently.PREREQUISITES
Python 3.8+Modal account and CLI installedpip install modalBasic knowledge of Python async functions
Setup
Install the modal package and configure your environment with your Modal account. This enables running GPU-backed image generation functions in the cloud.
pip install modal output
Collecting modal Downloading modal-1.x.x-py3-none-any.whl (xx kB) Installing collected packages: modal Successfully installed modal-1.x.x
Step by step
Define a Modal app with a GPU-enabled function that runs an image generation model (e.g., Stable Diffusion). Call this function with a prompt to generate an image and save it locally.
import modal
app = modal.App("image-gen-app")
@app.function(gpu="A10G", image=modal.Image.debian_slim().pip_install("torch", "diffusers", "transformers"))
def generate_image(prompt: str) -> str:
from diffusers import StableDiffusionPipeline
import torch
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
)
pipe = pipe.to("cuda")
image = pipe(prompt).images[0]
output_path = "/tmp/output.png"
image.save(output_path)
return output_path
if __name__ == "__main__":
with app.run():
path = generate_image.call("A futuristic cityscape at sunset")
print(f"Image saved to: {path}") output
Image saved to: /tmp/output.png
Common variations
- Use different GPU types by changing
gpu="A10G"to other supported GPUs. - Run image generation asynchronously with
async defandawaitcalls. - Use other image models by installing their dependencies and adjusting the pipeline.
import modal
app = modal.App("async-image-gen")
@app.function(gpu="A10G", image=modal.Image.debian_slim().pip_install("torch", "diffusers", "transformers"))
async def generate_image_async(prompt: str) -> str:
from diffusers import StableDiffusionPipeline
import torch
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
)
pipe = pipe.to("cuda")
image = pipe(prompt).images[0]
output_path = "/tmp/async_output.png"
image.save(output_path)
return output_path
if __name__ == "__main__":
with app.run():
import asyncio
path = asyncio.run(generate_image_async.call("A serene mountain lake"))
print(f"Async image saved to: {path}") output
Async image saved to: /tmp/async_output.png
Troubleshooting
- If you see
CUDA out of memory, reduce batch size or use a smaller GPU. - Ensure your Modal CLI is logged in with
modal loginbefore running. - Check that all dependencies are installed in the
modal.Imagedefinition.
Key Takeaways
- Use
modal.Appand@app.function(gpu=...)to run GPU-backed image tasks serverlessly. - Install necessary ML libraries inside the Modal image with
pip_installfor smooth execution. - Invoke Modal functions synchronously or asynchronously depending on your application needs.
- Troubleshoot GPU memory errors by adjusting GPU type or dependencies in your Modal function.
- Modal abstracts infrastructure, letting you focus on image generation code in Python.