← All articles

Workflows · August 19, 2026 · 3 min read

GPU functions in Python: serverless ML in five lines

Learn how to deploy scalable ML models using serverless GPU functions in Python with Podstack, featuring per-second billing and zero-config infrastructure.

The transition from a local Jupyter notebook to a production-grade ML microservice is often where projects stall. Between configuring CUDA drivers, managing Docker images, and setting up autoscaling groups, the "infrastructure tax" can consume more time than the actual model engineering.

For ML engineers, the ideal workflow is simple: write a Python function, decorate it, and have it run on a high-end A100 or H100 without thinking about the underlying VM. This is the promise of serverless GPU functions.

In this post, we’ll look at how to deploy a model in five lines of code, turning standard Python into a scalable API endpoint.

The Shift to Functional GPU Compute

Traditional cloud infrastructure treats GPUs like virtual servers—you rent a box, SSH in, and keep it running. Serverless GPU compute treats the GPU like a utility.

With podstack, you define the hardware requirements directly in your code. When the function is called, the platform orchestrates a container, attaches the requested GPU, executes the logic, and spins down. You are billed only for the seconds the GPU was active. This is particularly effective for:

  • Batch inference jobs.
  • Image generation (Stable Diffusion).
  • LLM fine-tuning tasks.
  • Asynchronous video processing.

Deploying in Five Lines

To get started, ensure you have the Podstack API installed and your PODSTACK_API_KEY exported in your environment.

Podstack does not ship a command-line interface. Use the console, the Python package `podstack-sdk`, or the API at https://podstack.ai/api/v1 with a `ps_live_` key.

The core logic relies on defining a function that the platform can pick up and containerize automatically. Here is how you deploy a Hugging Face model for sentiment analysis in a serverless environment:

import podstack

@podstack.gpu(gpu="A10G", vram="24GB")
def analyze_sentiment(text: str):
    from transformers import pipeline
    classifier = pipeline("sentiment-analysis", device=0)
    return classifier(text)

# Deploy to an endpoint
endpoint = podstack.deploy(analyze_sentiment)
print(f"Service live at: {endpoint.url}")

In these five lines, we’ve defined the hardware (NVIDIA A10G), handled the environment isolation, and triggered a deployment. Podstack takes care of the cold-start optimization and the REST API wrapping.

Managing Dependencies and Environments

One of the biggest hurdles in GPU computing is the "it works on my machine" syndrome, usually caused by mismatched CUDA versions.

When using GPU functions, you can specify your requirements in a requirements.txt or directly in the decorator. The Podstack engine handles the heavy lifting of building a container image that includes the necessary drivers and libraries.

Practical Tip: To minimize cold-start times, try to load your models outside the function's global scope if using a persistent worker, or use Podstack’s "Volume" feature to cache model weights. This prevents the function from re-downloading gigabytes of data from Hugging Face every time a new instance spins up.

Scaling from One to One Thousand

The beauty of the serverless approach is the transition from "Job" to "Endpoint."

  1. Jobs: If you need to process a million images, you can map your function across a list of inputs. Podstack will parallelize these across as many GPUs as your quota allows.
  2. Endpoints: If you are building a user-facing app, the function acts as a standard HTTP endpoint. When traffic spikes, the platform spins up more instances; when traffic hits zero, your costs hit zero.

Monitoring and Optimization

Once your function is live, you can monitor performance via the API:

Podstack does not ship a command-line interface. Use the console, the Python package `podstack-sdk`, or the API at https://podstack.ai/api/v1 with a `ps_live_` key.

Keep an eye on VRAM utilization. If your model only uses 8GB of VRAM, don't pay for an A100. Downscaling to a T4 or an A10G can cut your costs by 70% without impacting latency for smaller models. Because Podstack bills per second, these optimizations show up on your invoice immediately.

Why Podstack?

Podstack was built to remove the friction between a Python script and a production GPU cluster. By providing on-demand GPUs billed by the second, it allows developers to focus on the model.predict() rather than the docker build.

Whether you are running a single inference job or building the backbone of a new AI startup, the workflow remains the same: write Python, specify your GPU, and deploy.

Ready to scale? Head over to your dashboard, grab your PODSTACK_API_KEY, and turn your Python functions into high-performance GPU endpoints today.

Try it on Podstack

Per-second GPUs and pay-per-token models. Add $5, get $10 on your first top-up.

Start building