Deploy Generative AI Models on OCI GPUs Guide
In this article
Deploying a generative AI model on Oracle Cloud Infrastructure (OCI) GPUs involves provisioning high-performance compute instances, configuring the NVIDIA driver stack, and setting up a model serving framework to expose the model via an API. This process allows you to leverage A100 or H100 GPUs to run Large Language Models (LLMs) or diffusion models with low latency and high throughput, moving from a local development environment to a scalable cloud production environment.
What You'll Need
- An active OCI Tenancy with appropriate service limits for GPU shapes (e.g., VM.GPU.A10.1 or BM.GPU.H100.8).
- A Virtual Cloud Network (VCN) with a public subnet and configured security lists to allow traffic on your chosen API ports (e.g., 8000 or 8080).
- An SSH key pair for accessing the compute instance.
- The model weights (e.g., from Hugging Face) or a container image containing the model.
- Basic familiarity with Linux command-line operations and Python environments.
Step-by-Step
- Request GPU Service Limits: OCI GPU resources are often restricted by default to prevent accidental spending. Navigate to Governance & Administration > Limits, Quotas and Usage. Search for "GPU" and request a limit increase for the specific shape you intend to use in your home region. This request may take several business days to be approved by Oracle support.
- Create the Compute Instance: Go to Compute > Instances > Create Instance. Select the GPU shape required for your model size. For smaller models (7B parameters), a single A10 GPU may suffice; for larger models or training, select the H100 or A100 clusters. Choose an Ubuntu 22.04 or Oracle Linux 8 image. Ensure you attach a block volume large enough to hold both the OS and the model weights, as generative models often require hundreds of gigabytes of disk space.
- Configure Networking and Security: In the VCN settings, edit the Security List for your public subnet. Add an Ingress Rule to allow TCP traffic on the port your model server will use (typically port 8000 for FastAPI or vLLM). Without this, you will be able to run the model internally but will be unable to send requests to it from outside the OCI network.
-
Install NVIDIA Drivers and CUDA: Once you SSH into the instance, you must install the GPU drivers. If using Oracle Linux, you can use the
oci-utilspackage to automate driver installation. On Ubuntu, use the NVIDIA official repositories. Runnvidia-smito verify that the GPU is recognized and that the driver version is compatible with the CUDA version required by your AI framework (e.g., PyTorch 2.0+ usually requires CUDA 11.8 or 12.1). -
Set Up the Python Environment: Install Miniconda or Anaconda to manage dependencies. Create a dedicated environment to avoid conflicts with system libraries:
conda create -n genai python=3.10 -yandconda activate genai. Install the necessary libraries, such astorch,transformers,accelerate, andbitsandbytesfor quantization. -
Download the Model Weights: Use the Hugging Face Hub library to download your model. For efficiency, use a script to download the model to the attached block volume rather than the boot volume. If the model is gated (like Llama 3), ensure you have run
huggingface-cli loginand provided your access token. -
Deploy the Inference Server: Instead of a basic Python script, use a high-performance serving engine like vLLM or Text Generation Inference (TGI). For vLLM, run:
python -m vllm.entrypoints.openai.api_server --model /path/to/model --gpu-memory-utilization 0.9. This creates an OpenAI-compatible API endpoint that handles continuous batching and PagedAttention, significantly increasing the number of tokens generated per second. - Implement a Reverse Proxy: For production deployments, do not expose the model server directly. Install Nginx on the instance to act as a reverse proxy. Configure Nginx to forward requests from port 80 or 443 to port 8000. This allows you to implement SSL termination, rate limiting, and better logging of incoming requests.
Common Mistakes
One of the most frequent errors is neglecting the storage requirements. Many users provision a default 50GB boot volume, only to find that the model weights and the Docker layers for CUDA exceed this limit during the first download, causing the system to crash or enter a read-only state. Always provision a separate block volume for model storage.
Another common oversight is failing to configure the OCI Security List. Users often spend hours debugging "Connection Refused" errors in their application, only to realize the OCI cloud firewall is blocking the port, even if the local ufw or iptables on the Linux VM is open.
Finally, many developers attempt to load models in full precision (FP32) on GPUs with limited VRAM. This leads to immediate "Out of Memory" (OOM) errors. Using 4-bit or 8-bit quantization via bitsandbytes or loading the model in BF16 is essential for fitting larger models into the available GPU memory.
Tips
To maximize performance, use BF16 (Bfloat16) precision if your GPU supports it (A100/H100). BF16 provides a better dynamic range than FP16, which reduces the likelihood of numerical instability and "NaN" outputs during long text generation sequences.
Implement Model Quantization (such as AWQ or GPTQ) to reduce the VRAM footprint. This allows you to serve larger models on smaller, cheaper GPU instances without a significant drop in perplexity or reasoning capability.
Use OCI Object Storage to store your model checkpoints. Instead of downloading from Hugging Face every time you recreate an instance, you can mount an Object Storage bucket or use the OCI CLI to sync weights to the local disk, which is significantly faster and more reliable for large-scale deployments.
Troubleshooting
Issue: CUDA Out of Memory (OOM)
If you encounter an OOM error during model loading, first check nvidia-smi to ensure no other processes are consuming VRAM. If the GPU is empty, reduce the max_model_len in your serving engine or increase the quantization level (e.g., move from 8-bit to 4-bit). If using vLLM, lower the gpu_memory_utilization parameter to leave more headroom for the KV cache.
Issue: Slow Token Generation (Low Throughput)
Check if the CPU is bottlenecking the GPU. Ensure that you are using a compute shape with enough OCPUs to feed the GPU. Additionally, verify that you are using a serving framework that supports continuous batching. If you are using a simple transformers pipeline, you will only process one request at a time, which is inefficient for multiple users.
Issue: SSH Connection Timeout
If you cannot access your instance after creation, verify that the VCN's ingress rules allow SSH (Port 22) from your specific IP address. Also, check the instance console logs in the OCI dashboard to see if the VM is stuck in a boot loop due to a kernel panic or driver conflict.
Issue: Model Weights Corrupted during Download
If the model fails to load with a "tensor mismatch" or "unexpected EOF" error, the download was likely interrupted. Clear the Hugging Face cache directory (usually ~/.cache/huggingface) and restart the download using a tool like wget or the huggingface-cli with resume capabilities enabled.
Frequently Asked Questions
Which GPU shape is best for Llama 3 70B?
For a 70B model, you typically need at least two A100 (80GB) GPUs or a single H100 instance to fit the model in 16-bit precision. If using 4-bit quantization, you may be able to fit it on fewer GPUs, but multi-GPU setups are recommended for acceptable inference speeds.
Can I use Spot Instances for AI deployment?
Yes, OCI Preemptible Instances can significantly reduce costs. However, they can be reclaimed by Oracle at any time. This is suitable for asynchronous batch processing or development, but not for production APIs that require high availability.
How do I handle model versioning on OCI?
The best practice is to store different model versions in OCI Object Storage buckets. You can then use a deployment script to pull the specific version tag required for your environment, ensuring consistency across staging and production.
Do I need to install CUDA manually?
While some OCI images come with pre-installed drivers, it is generally safer to install the specific CUDA Toolkit version that matches your PyTorch or TensorFlow version to avoid "version mismatch" errors during runtime.
How do I scale my deployment to multiple GPUs?
Use a framework like vLLM or DeepSpeed that supports tensor parallelism. By setting the tensor-parallel-size to the number of GPUs available on your instance, the model weights are split across the cards, allowing for faster computation and larger model capacity.