Local FastAPI to Serverless GPU Endpoint: Migration Guide

Connor Blier
Founding GTM

Two stacked server-rack boxes connected by a short vertical link, each box showing a row of small circular status lights and side connector pins, symbolizing a transition from one hosted server unit to another.

Local FastAPI to Serverless GPU Endpoint: Migration Guide

Moving from a local FastAPI model server to a serverless GPU endpoint rarely means rewriting your model code. The real work is relocating startup, taming cold starts and model-weight loading, and re-architecting for scale-to-zero billing. Your handler logic mostly survives the move intact.

Your local FastAPI GPU server already encodes the hard decisions: which model, which precision, how you batch, what the request contract looks like. Migrating that server to a serverless GPU endpoint is mostly about relocating startup work and re-architecting for cold starts and scale-to-zero billing, not rewriting model code. This guide walks an ML engineer through the move, section by section.

The starting point: what your local FastAPI model server actually does

A typical local server does four things. At process start it loads model weights into GPU memory. It holds a warm CUDA context and a loaded model object in a global. It exposes one or more routes that accept a request, run inference, and return a response. And it keeps running whether or not anyone is calling it.

That last point is the whole economic difference. Locally, you pay for the box 24/7 and the one-time model load is invisible because it happened at boot and never again. Serverless inverts both facts: you only pay while a request is executing, and the model load can happen again every time the platform spins a fresh worker. Everything else in the migration follows from those two inversions.

Why "FastAPI serverless" usually isn't GPU serverless

Search for "deploy FastAPI serverless" and you mostly get CPU function platforms. That is a trap for GPU workloads.

Where Lambda-style migration breaks

The classic serverless path is a function-as-a-service endpoint that scales to zero. AWS SageMaker Serverless Inference, for instance, "scales your endpoint down to 0, helping you to minimize your costs" during idle periods (SageMaker docs). But the same documentation lists GPUs among its feature exclusions, so a GPU model simply cannot run there. Lift-and-shift of your FastAPI app into a generic serverless runtime breaks the moment it needs CUDA.

What serverless GPU means

Serverless GPU is the version that keeps the accelerator. As Beam describes it, you "run code that automatically spins up a GPU container when needed, executes your job, and then spins it down. You pay only for the execution time" (Beam). You still ship a container with CUDA and PyTorch inside; you just stop owning the server. If you want the conceptual grounding first, our serverless computing and serverless GPU platform explainers cover the model in depth.

The core challenge: cold starts and model-weight loading

Why model load dominates

On a local server the model load is a one-time cost. On serverless it recurs every time the platform starts a new worker from cold. Spheron puts the penalty at "a 40-90 second penalty the first time a pod has to load a large model from scratch" (Spheron). That is the single biggest behavioural change you will feel, and it is dominated by moving weights from storage into VRAM.

Mitigations

Several levers help, and most are things you can do in your own container:

  • Shrink the weights. Quantization cuts both memory and load time. Spheron notes INT4 (AWQ/GPTQ) runs at "~0.5 bytes/param, 4 GB for 7B" against FP16's "2 bytes/param, 14 GB for 7B." Our walkthrough of 4-bit LLM inference on H100 shows the trade-offs.

  • Stream the weights. With safetensors you can overlap disk reads and GPU transfers; Spheron reports this "saves 20-30 seconds on a 70B model." Where the weights live matters too, which we cover in loading model weights from object storage.

  • Snapshot the whole process. Rather than re-running the load, restore a memory snapshot. In our testing, restoring from a 9 GiB checkpoint reduced startup to 2.25 seconds from S3 and 9 seconds from local NVMe, versus a roughly 50-second full vLLM cold start on a g5.12xlarge (Cerebrium).

  • Pre-warm when latency is non-negotiable. Keeping a minimum replica warm eliminates the cold start at the cost of a persistent reservation.

Container build choices feed into this as well. We measured machine boot time drop from 2 to 7 minutes down to under 30 seconds after reworking how custom images are built (Cerebrium).

Choosing a serverless GPU platform

Don't start from a price table. As Northflank advises, "Compare the execution model before comparing the price" (Northflank). Evaluate against your workload:

  • Deployment model. Do you ship a container and own the runtime, or hand over a model name? RunPod lets you "deploy any containerized model as an autoscaling GPU endpoint," while Modal lets you define GPU requirements "right in line with application code."

  • Cold-start behaviour. Scale-to-zero only pays off if scale-up is fast. Vendors range widely in claimed starts.

  • Billing granularity. Per-second billing is common; Northflank lists an H100 80GB at "$2.74/hour" and Modal's H100 component at "approximately $3.95/hour." For reference, Cerebrium's own CPU rate is $0.000026 per second.

For head-to-head framing see Modal vs RunPod on cost and cold starts and our Baseten alternatives breakdown of production criteria.

The migration, step by step

  1. Isolate model load from request handling. Separate the one-time setup (load weights, build the model, warm CUDA) from the per-request path. Serverless platforms treat these as distinct lifecycle hooks, and keeping them separate is what lets snapshotting and pre-warming work.

  2. Port the handler. Your inference function usually moves nearly verbatim. Keep the same input/output contract so clients don't change. If you need HTTP-native behaviour, ASGI support lets a FastAPI-style app run as-is.

  3. Containerize dependencies. As RunPod notes, the container "needs to include CUDA drivers, PyTorch, and our FastAPI code." Pin versions, keep the image lean, and front-load heavy downloads into the build so they are not paid per cold start.

  4. Configure scaling and billing. Decide min and max replicas, concurrency per worker, and whether to keep a warm pool. This is where your throughput strategy matters: continuous batching can deliver "up to 23x throughput improvement" (Anyscale), and tuning it is covered in vLLM server parameters.

Validate, compare, and cut over

Before sending real traffic, run a validation checklist: confirm parity of outputs against the local server on a fixed prompt set; measure cold-start and warm latency separately; verify TTFT under load, since "time to first token (TTFT) is the slice of latency users feel first" (Mirantis); and check autoscaling under a burst.

Cut over gradually. A canary release that shifts a small traffic slice first, with an automatic rollback path, de-risks the move; our guide on canary rollout and rollback for a serverless GPU endpoint details the mechanics. Teams that get this right see large wins: Creatium's GPU cold-start times "dropped from several minutes to roughly 10 seconds" after migrating (Cerebrium).

The model code you debugged locally is the part that travels best. Spend your migration budget on the two things serverless actually changes: startup and scale.

Frequently asked questions

Do I have to rewrite my model code to go serverless?
Usually no. The handler and model logic port nearly verbatim. The work concentrates in splitting one-time model loading from per-request handling and configuring scaling and billing.
Can I just deploy my FastAPI app to a generic serverless platform?
Not for GPU workloads. Generic function platforms like AWS SageMaker Serverless Inference exclude GPUs, so you need a serverless GPU platform that runs your CUDA container.
Why is my first request so slow on serverless GPU?
Cold starts. Loading large model weights into VRAM can add 40-90 seconds the first time a worker starts. Quantization, weight streaming, memory snapshots, and pre-warming all reduce it.
How much faster can cold starts get?
In our testing, restoring a 9 GiB checkpoint from a memory snapshot cut startup to 2.25 seconds from S3 and 9 seconds from local NVMe, versus roughly 50 seconds for a full vLLM cold start.
How should I compare serverless GPU providers?
Compare the execution model and cold-start behaviour before price. Then weigh per-second billing rates and whether you can keep a warm pool for latency-critical traffic.

Get started with Cerebrium

Deploy AI models on serverless GPUs in minutes, with no infrastructure to manage. Start for free and pay only for the compute you use.

Sign up free

Sources

  1. beam.cloud
    “run code that automatically spins up a GPU container when needed, executes your job, and then spins it down. You pay only for the execution time”

    Definition of serverless GPU used to explain what the migration target is.

  2. docs.aws.amazon.com
    “During times when there are no requests, Serverless Inference scales your endpoint down to 0, helping you to minimize your costs.”

    Illustrates scale-to-zero on a generic serverless inference service.

  3. docs.aws.amazon.com
    “Serverless Inference, including GPUs, AWS marketplace model packages, private Docker registries, Multi-Model Endpoints, VPC”

    Shows GPUs are excluded from SageMaker Serverless Inference, so a generic path breaks for GPU models.

  4. spheron.network
    “a 40-90 second penalty the first time a pod has to load a large model from scratch”

    Quantifies the cold-start penalty dominated by model loading.

  5. spheron.network
    “FP16: 2 bytes/param, 14 GB for 7B; FP8: 1 byte/param, 7 GB for 7B; INT4 (AWQ/GPTQ): ~0.5 bytes/param, 4 GB for 7B”

    Supports quantization as a cold-start and memory mitigation.

  6. spheron.network
    “At 3-4 GB/s on local NVMe, streaming overlap saves 20-30 seconds on a 70B model.”

    Supports weight streaming as a cold-start mitigation.

  7. cerebrium.ai
    “Restoring from a 9 GiB checkpoint reduced startup to 2.25 seconds from S3 and 9 seconds from local NVMe.”

    First-hand Cerebrium snapshot benchmark for cold-start mitigation.

  8. cerebrium.ai
    “From the moment we requested a machine to the moment it was ready to run user containers, the process could take anywhere from 2 to 7 minutes.”

    First-hand baseline boot time behind the container-build improvement.

  9. cerebrium.ai
    “GPU cold-start times dropped from several minutes to roughly 10 seconds, allowing users to begin interacting with AI-powered learning experiences almost instantly.”

    First-hand customer migration result after cutover.

  10. northflank.com
    “Compare the execution model before comparing the price.”

    Supports the platform-selection advice to evaluate execution model first.

  11. northflank.com
    “GPU plans billed by the second; H100 80 GB: $2.74/hour, including allocated CPU, RAM, and ephemeral storage”

    Example per-second GPU pricing for platform comparison.

  12. northflank.com
    “GPU, CPU, and memory usage; H100 SXM GPU component: $0.001097/second, approximately $3.95/hour”

    Example per-second GPU pricing for platform comparison.

  13. runpod.io
    “Deploy any containerized model as an autoscaling GPU endpoint without managing servers.”

    Illustrates the container-ship deployment model.

  14. modal.com
    “Notice that GPU requirements for the function are defined right in line with application code.”

    Illustrates an in-code GPU configuration deployment model.

  15. runpod.io
    “The container needs to include **CUDA drivers**, **PyTorch**, and our FastAPI code.”

    Supports the containerize-dependencies migration step.

  16. anyscale.com
    “Up to 23x throughput improvement using continuous batching and continuous batching-specific memory optimizations (using vLLM).”

    Supports configuring throughput via continuous batching.

  17. mirantis.com
    “In streaming setups, time to first token (TTFT) is the slice of latency users feel first.”

    Supports the validation step on measuring TTFT.

  18. cerebrium.ai
    “Pay-per-use at $0.000026 per second for a basic CPU application”

    Cerebrium's own per-second rate used as a billing reference point.


Related resources

See all
A microphone icon with sound waves on either side, representing a timer-like capture of a spoken voice, drawn as a simple black line mark.
Voice Cloning Inference: Cost & Latency Per Request
A symmetrical network diagram of small circular nodes connected by straight lines radiating from a central node, forming a hexagonal mesh pattern suggesting a distributed computing cluster.
Anyscale Alternatives for Ray Serve Model Serving
A microphone with its diaphragm shown as concentric rounded bars, flanked on each side by two short vertical signal bars representing streaming audio waveforms.
Streaming STT Latency on Serverless GPU: A Budget