Skip to content
ImagineWorld

Optimizing GPU Resources for GenAI Applications in Kubernetes

node_idnode-book_kubernetes-for-generative-ai-solutions_chapter-10-optimizing-gpu-resources-for-genai-applications-in-kubernete
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num10
chapter_titleOptimizing GPU Resources for GenAI Applications in Kubernetes
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 10 — Optimizing GPU Resources for GenAI Applications in Kubernetes

Section titled “Chapter 10 — Optimizing GPU Resources for GenAI Applications in Kubernetes”

This chapter will cover strategies to maximize graphics processing unit (GPU) (https://aws.amazon.com/what-is/gpu/) efficiency in K8s when deploying GenAI applications as GPU instances are very expensive and often underutilized. We will also cover GPU resource management, scheduling best practices, and partitioning options such as Multi-Instance GPU (MIG) (https://www.nvidia.com/en-us/technologies/multi-instance-gpu/), Multi-Process Service (MPS) (https://docs.nvidia.com/deploy/mps/index.html), and GPU time-slicing (https://docs.nvidia.com/datacenter/cloud-native/gpu-operator/latest/gpu-sharing.html). Finally, we’ll discuss monitoring GPU performance, balancing workloads across nodes, and auto-scaling GPU resources to handle dynamic GenAI workloads effectively.

In this chapter, we’re going to cover the following main topics:

  • GPUs and custom accelerators
  • Allocating GPU resources in K8s
  • Understanding GPU utilization
  • Techniques for partitioning and sharing GPUs
  • Scaling and optimization considerations

In this chapter, we will be using the following tools, some of which require you to set up an account and create an access token:

Deploying GenAI workloads in K8s requires selecting the right hardware based on the computational requirements of the workload, such as training, inference, or microservices implementation. Compute options include CPUs, GPUs, or custom accelerators such as Inferentia (https://aws.amazon.com/ai/machine-learning/inferentia/) and Trainium (https://aws.amazon.com/ai/machine-learning/trainium/), as well as accelerators from AWS or tensor processing units (TPUs) (https://cloud.google.com/tpu) from Google Cloud.

CPUs are usually the default compute resource in K8s and are suitable for lightweight GenAI tasks, including small-scale inference, data preprocessing, exposing APIs, and implementing classical ML algorithms such as XGBoost for decision trees (https://xgboost.readthedocs.io/en/stable/). However, they are less efficient for tasks that require high parallelism and a very large number of matrix multiplications, such as training foundational models. K8s lets you define both CPU requests and limits, ensuring a fair and efficient allocation of resources among all workloads.

GPUs excel at massively parallel processing (MPP) because they feature thousands of cores and very high memory bandwidth, allowing them to handle the matrix multiplications and linear algebra computations that are central to deep learning far more efficiently than CPUs. However, GPUs are not recognized natively by K8s. Thanks to its extensible architecture, device vendors can develop device plugins (https://kubernetes.io/docs/concepts/extend-kubernetes/compute-storage-net/device-plugins/) that expose GPU resources to the K8s control plane. These plugins are typically deployed as DaemonSets (https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/) in the cluster, enabling the K8s scheduler to identify, allocate, and manage GPUs for containerized workloads properly. One of the most popular device plugins is the NVIDIA device plugin for Kubernetes (https://github.com/NVIDIA/k8s-device-plugin), which exposes NVIDIA GPUs attached to each K8s worker node and continuously tracks their health.

As an alternative to GPUs, many companies are investing in creating purpose-built accelerators tailored for AI/ML workloads. Examples include AWS Inferentia and Trainium, Google TPUs, field-programmable gate arrays (FPGAs) (https://www.arm.com/glossary/fpga), and various application-specific integrated circuits (ASICs) (https://www.arm.com/glossary/asic), each designed to excel at core operations such as matrix multiplication, utilizing transformer-based models, delivering higher performance, and ensuring lower energy consumption. Similar to GPUs, these accelerators integrate with K8s through custom device plugins provided by hardware vendors. These plugins discover, allocate, and monitor the specialized hardware resources attached to K8s worker nodes, enabling seamless scheduling and management alongside other compute resources.

Custom accelerators are particularly effective for large-scale training or low-latency inference. For example, AWS Trainium (https://aws.amazon.com/ai/machine-learning/trainium/) is a family of AI chips developed by AWS to enhance GenAI training by delivering high performance while reducing costs. The first-generation Trainium chips powered Amazon EC2 Trn1 instances (https://aws.amazon.com/ec2/instance-types/trn1/) and offer up to 50% lower training costs compared to comparable EC2 instances. The Trainium2 chips, featured in Amazon EC2 Trn2 instances and Trn2 UltraServers (https://aws.amazon.com/ec2/instance-types/trn2/), are the most powerful EC2 instances for training and inferencing of GenAI models with hundreds of billions to trillions of parameters. They provide up to four times the performance of their predecessors and 30% to 40% better price performance than EC2 P5e and P5en family instances.

Custom accelerators often rely on specialized hardware architectures and instruction sets that differ from general-purpose CPUs or GPUs. Because of this, AI/ML frameworks such as TensorFlow and PyTorch cannot natively translate high-level operations into low-level instructions that accelerators understand. The Neuron SDK (https://aws.amazon.com/ai/machine-learning/neuron/) is a software development kit designed by AWS to run and optimize AI/ML workloads efficiently on AWS’s custom AI accelerators, such as AWS Trainium and Inferentia. It includes a compiler, runtime, training and inference libraries, and profiling tools. Neuron supports customers throughout their end-to-end ML development life cycle, including building and deploying deep learning and AI models. The Neuron SDK provides seamless integration with popular ML frameworks such as PyTorch (https://pytorch.org/), TensorFlow (https://www.tensorflow.org/), and JAX (https://jax.readthedocs.io/) while supporting over 100,000 models, including those from Hugging Face. Customers, such as Databricks, have reported significant performance improvements and cost savings of up to 30% when using Trainium-powered instances (https://aws.amazon.com/ai/machine-learning/trainium/customers/).

Choosing between CPUs, GPUs, and accelerators requires balancing performance needs, workload intensity, and budget constraints to optimize resource utilization for GenAI workloads. With this overview of custom accelerators, let’s dive deeper into allocating GPU resources to GenAI applications in K8s.

To use GPU and custom accelerator resources in K8s, you must use the corresponding device plugins. For instance, the NVIDIA device plugin for Kubernetes makes NVIDIA GPUs recognizable and schedulable by the K8s cluster, while the Neuron device plugin does the same for AWS Trainium and Inferentia accelerators. This mechanism ensures that any custom accelerator is discovered, allocated, and managed properly within the K8s cluster.

Apart from installing the device plugin, you should also ensure that the respective GPU/accelerator drivers are present on the underlying operating system. AWS offers accelerated AMIs for NVIDIA and the Trainium and Inferentia accelerators, all of which you can use to launch K8s worker nodes. These AMIs include NVIDIA, Neuron drivers, nvidia-container-toolkit (https://github.com/NVIDIA/nvidia-container-toolkit), and others on top of the standard EKS-optimized AMI. Please refer to the AWS documentation at https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html#gpu-ami for more information.

A high-level architecture of the K8s device plugin framework is depicted in Figure 10*.1*. Let’s look at what steps are involved in this process:

  1. K8s device plugins are deployed as DaemonSets, ensuring that all (or some) nodes run a copy of the plugin Pod. Typically, these plugins are scheduled on worker nodes with specific labels (GPU, custom accelerator) to optimize resource utilization.
  2. Upon initialization, the device plugin performs various vendor-specific initialization tasks and ensures devices are in a ready state.
  3. The plugin registers itself with the kubelet and declares the custom resources it manages – for example, nvidia.com/gpu for NVIDIA GPUs and aws.amazon.com/neuroncore for AWS Trainium/Inferentia devices.
  4. After successful registration, the plugin provides the kubelet with the list of managed devices. The kubelet then performs a node status update to advertise these resources to the K8s API server. This can be verified by running the
    $ kubectl get node <replace-node-name> -o jsonpath='{.status.allocatable}' | jq .
    {
    "nvidia.com/gpu": "1",
    ...
  5. When a Pod is scheduled on the worker node, the kubelet notifies the device plugin of the container’s requirement (the number of GPUs) so that it can perform the necessary preparation tasks to allocate the requested resources.
  6. The device plugin continuously monitors the health of the devices it manages and updates the kubelet accordingly to ensure optimal resource availability.

Figure 10.1 – K8s device plugin architecture

  1. In our walkthrough in Chapter 5, we installed the NVIDIA device plugin using the eks-data-addons Terraform module, as
    module "eks_data_addons" {
    source = "aws-ia/eks-data-addons/aws"
    ...
    enable_nvidia_device_plugin = true
  2. It used the Terraform Helm provider to deploy the NVIDIA device plugin Helm chart (https://github.com/NVIDIA/k8s-device-plugin?tab=readme-ov-file#deployment-via-helm) in the EKS cluster. You can verify which Helm release and DeamonSet are being used by running the
    $ helm list -n nvidia-device-plugin
    NAME NAMESPACE CHART
    nvidia-device-plugin
    nvidia-device-plugin
    nvidia-device-plugin
    $ kubectl get ds -n nvidia-device-plugin --no-headers
    nvidia-device-plugin
    nvidia-device-plugin-gpu-feature-discovery
    nvidia-device-plugin-node-feature-discovery-worker
  3. The next step is to launch GPU worker nodes and register them with the cluster. This was covered in the Chapter 5 walkthrough, where we created a new EKS-managed node group called eks-gpu-mng using the G6 family of EC2 instances. Additionally, K8s taints were applied to these nodes to ensure that only Pods requiring GPU resources were scheduled
    module "eks" {
    source = "terraform-aws-modules/eks/aws"
    ...
    eks_managed_node_groups = {
    eks-gpu-mng = {
    instance_types = ["g6.2xlarge"]
    taints = {
    gpu = {
    key = "nvidia.com/gpu"
    value = "true"
    effect = "NO_SCHEDULE"
    ...
  4. GPU worker nodes can also be labeled so that advanced scheduling can be implemented – for example, we can use hardware-type=gpu to identify GPU-enabled nodes. This makes it possible to target specific nodes when scheduling workloads that require GPUs. We can do this by using the kubectl command or running the necessary Terraform code:
    $ kubectl label node <node-name> hardware-type=gpu
    We can also label the K8s worker nodes using Terraform, as shown in the following code snippet:
    ...
    eks-gpu-mng = {
    labels = {
    "hardware-type" = "gpu"
    }
    ...
  5. The next step is to allocate GPU resources for containerized workloads, such as GenAI training and inference workloads. To request GPUs, you need to specify resource requests and limits in your K8s deployment and Pod specifications. The following code snippet demonstrates how to do
    • Assign tolerations to the K8s Pod that match the node taints
      • Schedule the K8s Pods specifically on nodes labeled with hardware-type: gpu
      • Request one GPU resource using the nvidia.com/gpu
      apiVersion: v1
      kind: Pod
      metadata:
      name: gpu-demo-pod
      spec:
      tolerations:
      - key: "nvidia.com/gpu"
      operator: "Exists"
      effect: "NoSchedule"
      nodeSelector:
      hardware-type: gpu
      containers:
      - name: gpu-container
      image: nvidia/cuda
      resources:
      limits:
      nvidia.com/gpu: 1

When requesting GPU resources in K8s, you must always define them in the limits section of the specification, either alone or with matching request values; specifying only requests without limits is not allowed. Similarly, when using AWS Trainium/Inferentia accelerators, you can use the aws.amazon.com/neuroncore attribute to request the resources.

In this section, we started by looking at the K8s device plugin architecture and the steps involved in creating the worker nodes in an EKS cluster. We also looked at K8s scheduling techniques such as taints, tolerations, and node selectors, all of which we can use to schedule the GPU Pods to their respective nodes.

GPUs constitute a major cost of running GenAI workloads, so utilizing them effectively is paramount in achieving optimal performance and cost-efficiency. Without proper monitoring, underutilized GPUs can result in compute inefficiencies and increased operational expenses, while overutilized GPUs risk request throttling and potential application failures. In this section, we will explore solutions for monitoring GPU utilization while focusing on exporting metrics and leveraging them to implement efficient autoscaling strategies.

DCGM (https://developer.nvidia.com/dcgm) is a lightweight library and agent designed to simplify the management of NVIDIA GPUs, enabling users, developers, and system administrators to monitor and manage GPUs across clusters or data centers.

DCGM provides functionality such as GPU health diagnostics, behavior monitoring, configuration management, telemetry collection, and policy automation. It operates both as a standalone service through the NVIDIA host engine and as an embedded component within third-party management tools. Its key features include GPU health diagnostics, job-level telemetry, group-centric resource management for multiple GPUs or hosts, and automated management policies to enhance reliability and simplify administration tasks. DCGM can integrate seamlessly with K8s tools such as the NVIDIA GPU Operator, allowing for telemetry collection and health checks in containerized environments. With support for exporting metrics to systems such as Prometheus, DCGM also facilitates real-time visualization and analysis of GPU data in tools such as Grafana.

DCGM can be implemented in K8s clusters in the following ways:

  • NVIDIA GPU Operator: gpu-operator (https://github.com/NVIDIA/gpu-operator) leverages the operator pattern (https://kubernetes.io/docs/concepts/extend-kubernetes/operator/) within K8s to automate the process of managing all NVIDIA software components required for provisioning GPUs. These components include NVIDIA drivers (to enable CUDA), the K8s device plugin for GPUs, NVIDIA Container Runtime, automatic node labeling, DCGM-based monitoring, and more. This approach is ideal for environments where you want a fully automated solution for managing NVIDIA GPU resources, including installation, updates, and configuration.
  • DCGM-Exporter (https://github.com/NVIDIA/dcgm-exporter): Built on top of the NVIDIA DCGM framework, DCGM-Exporter collects and exposes a wide range of GPU performance and health metrics, such as utilization, memory usage, temperature, and power consumption, in a Prometheus-compatible format. This facilitates seamless integration with popular monitoring and visualization tools such as Prometheus and Grafana. DCGM-Exporter is well-suited for scenarios where the necessary GPU components are already installed, and you need to enable GPU monitoring without additional overhead.

Please refer to the NVIDIA documentation at https://docs.nvidia.com/datacenter/cloud-native/gpu-operator/latest/amazon-eks.html for detailed guidance on selecting the right approach based on your operational needs.

So far in our walkthrough, we have already installed the necessary components, such as the NVIDIA device plugin and relevant drivers. Therefore, we will use the second approach (DCGM-Exporter) to monitor the GPU’s health and utilization metrics. For the first approach, you can refer to the NVIDIA documentation at https://docs.nvidia.com/datacenter/cloud-native/gpu-operator/latest/getting-started.html#operator-install-guide for detailed setup instructions. To proceed, we will install DCGM-Exporter using the Helm provider in Terraform. Begin by downloading the aiml-addons.tf file from https://github.com/PacktPublishing/Kubernetes-for-Generative-AI-Solutions/blob/main/ch10/aiml-addons.tf.

The dcgm-exporter Helm chart will be deployed in the dcgm-exporter namespace from the https://nvidia.github.io/dcgm-exporter/ Helm repository, as shown in the following code snippet:

resource "helm_release" "dcgm_exporter" {
name = "dcgm-exporter"
repository = "https://nvidia.github.io/dcgm-exporter/"
chart = "dcgm-exporter"
namespace = "dcgm-exporter"
...

Execute the following commands to deploy the dcgm-exporter Helm chart in the EKS cluster and verify its installation using the following kubectl command. The output should confirm that dcgm-exporter has been deployed as a DaemonSet and its Pods are Running:

$ terraform init
$ terraform plan
$ terraform apply -auto-approve
$ kubectl get ds,pods -n dcgm-exporter
NAME DESIRED CURRENT READY
daemonset.apps/dcgm-exporter 2 2 2
NAME READY STATUS RESTARTS AGE
pod/dcgm-exporter-729qb 1/1 Running 0 84s
pod/dcgm-exporter-rtmtw 1/1 Running 0 84s

Now that DCGM-Exporter is functional, you can access the GPU’s health and utilization metrics by connecting to the DCGM-Exporter service. Execute the following commands to connect to the service locally and use a curl command to query the /metrics endpoint to view the GPU metrics:

$ kubectl port-forward svc/dcgm-exporter -n dcgm-exporter 9400:9400
Forwarding from 127.0.0.1:9400 -> 9400
Forwarding from [::1]:9400 -> 9400
$ curl http://localhost:9400/metrics
# HELP DCGM_FI_DEV_GPU_UTIL GPU utilization (in %).
# TYPE DCGM_FI_DEV_GPU_UTIL gauge
DCGM_FI_DEV_GPU_UTIL{gpu="0",UUID="GPU-173ced1d-4c1d-072d-6819-86522b018187",pci_bus_id="00000000:31:00.0",device="nvidia0",modelName="NVIDIA L4",Hostname="ip-10-0-34-196.us-west-2.compute.internal"} 0
# HELP DCGM_FI_DEV_MEM_COPY_UTIL Memory utilization (in %).
# TYPE DCGM_FI_DEV_MEM_COPY_UTIL gauge
DCGM_FI_DEV_MEM_COPY_UTIL{gpu="0",UUID="GPU-173ced1d-4c1d-072d-6819-86522b018187",pci_bus_id="00000000:31:00.0",device="nvidia0",modelName="NVIDIA L4",Hostname="ip-10-0-34-196.us-west-2.compute.internal"} 0
...

In Chapter 12, we will deploy and configure a Prometheus agent that will scrape these GPU metrics and visualize them using Grafana dashboards. Once the metrics are available in Prometheus, we can install the Prometheus adapter (https://github.com/kubernetes-sigs/prometheus-adapter) to create autoscaling policies, allowing GPU workloads to scale dynamically for optimal resource utilization.

In K8s, allocating a GPU to a Pod reserves that GPU exclusively for the Pod’s entire life cycle, even if the Pod is not actively using it. K8s does not support sharing GPUs among multiple Pods or assigning partial GPUs (< 1) per Pod by default. This design ensures isolation and avoids conflicts as most GPUs are not inherently designed to handle concurrent workloads without specialized software.

However, exclusive GPU allocation can lead to underutilization if a Pod does not fully utilize its assigned GPU. This challenge is especially pronounced in GenAI scenarios, where the following factors often come into play:

  • Varying model sizes (large versus small LLMs): While state-of-the-art (SOTA) LLMs may require an entire GPU or even multiple GPUs, there is a growing demand for smaller or distilled versions of these models. Depending on their size, these smaller LLMs may require a fraction of the GPU’s memory and compute capacity. With K8s’s default “whole GPU” allocation, even a smaller model that needs only part of a GPU will be allocated an entire device, leading to over-provisioning.
  • Dynamic workload patterns: GenAI workloads such as fine-tuning or training LLMs or running inference for complex diffusion-based models can create bursty GPU usage patterns. During training/fine-tuning/inference, GPU usage can spike to up to 100% for compute-intensive operations (matrix multiplications, backpropagation, etc.,) but will drop significantly in between epochs or data loading/processing steps. Because K8s does not support fractional GPU resource allocation by default, these peaks and valleys can lead to inefficient GPU utilization.
  • Scheduling complexity and fragmentation: K8s’s default scheduler lacks the intelligence of advanced GPU-sharing strategies. Even with techniques such as node affinity, taints, and tolerations, there is no out-of-the-box method to dynamically reassign underutilized GPUs to another Pod. Consequently, smaller LLMs might monopolize an entire GPU, even if they need a fraction of its capacity. As more Pods are deployed, multiple GPUs become partially utilized but fully reserved, causing fragmented GPU resources across the cluster.

We can implement a few approaches to address these utilization challenges, such as NVIDIA’s MIG, MPS, and GPU time-slicing options. In a case study on delivering video content using GPU-sharing techniques, up to a 95% improvement in price performance was achieved. For more detailed benchmarking data regarding this, please refer to the following AWS blog: https://aws.amazon.com/blogs/containers/delivering-video-content-with-fractional-gpus-in-containers-on-amazon-eks/.

In this section, we explored why monitoring GPU health and metrics is crucial and deployed the NVIDIA DCGM-Exporter add-on in our K8s cluster to track GPU performance and health metrics in real time. We also looked at the challenges of GPU utilization and the factors contributing to inefficiency in the GenAI space. Next, we will dive into GPU partitioning techniques that we can use to address these issues.

Techniques for partitioning and sharing GPUs

Section titled “Techniques for partitioning and sharing GPUs”

GPU partitioning and sharing techniques are often vendor-specific, meaning they may not be available for every accelerator out there. In this section, we will explore some of the most common approaches provided by NVIDIA for its GPUs, such as MIG, MPS, and time-slicing, and discuss how they can help improve GPU utilization for GenAI workloads.

MIG (https://docs.nvidia.com/datacenter/tesla/mig-user-guide/index.html) is a feature that was introduced in NVIDIA’s Ampere and later architectures (A100, H100, etc.) that allows a single physical GPU to be partitioned into multiple independent GPU instances. Each MIG instance has its own dedicated memory, compute cores, and other GPU resources, providing strict isolation between workloads. MIG minimizes interference among instances and ensures predictable performance, ultimately enabling more efficient and flexible use of GPU capacity.

The process of implementing MIG involves defining GPU instances that bundle a portion of the GPU’s memory and compute capacity. For example, on an NVIDIA A100 GPU with 40 GB of memory, you can configure up to seven instances each with 5 GB of dedicated memory and a corresponding share of compute resources; this can be seen in Figure 10*.2*. In this example, we can have multiple workloads, such as Jupyter notebooks, ML jobs, and more, that can run on separate GPU partitions, allowing for efficient utilization of the GPU instance:

Figure 10.2 – Multi-instance GPU in an A100 NVIDIA GPU

These instances are specified by MIG profiles (https://docs.nvidia.com/datacenter/tesla/mig-user-guide/index.html#supported-mig-profiles), which outline the size and shape of each instance – for example, a 1g.5gb profile allocates 5 GB of memory and a proportional share of GPU cores. These instances function as isolated GPU instances, each with guaranteed performance and minimal interference from others. The following table shows the MIG profiles for the latest NVIDIA H200 GPUs from the NVIDIA MIG user guide (https://docs.nvidia.com/datacenter/tesla/pdf/NVIDIA_MIG_User_Guide.pdf):

MIG ProfileGPU SlicesGPU Memory (GB)Number of Instances
1g.18gb1187
1g.18gb+me1181
1g.35gb1354
2g.35gb2353
3g.70gb3702
4g.70gb4701
7g.141gb71411

Table 10.1 – GPU instance profiles for a NVIDIA H200 GPU

In this table, GPU Slices represent the fraction of the GPU allocated to the MIG profile, GPU Memory (GB) represents the amount of memory allocated to that MIG instance in GB, and Number of Instances is the instance count that can be created for the given profile.

In K8s, you can use the NVIDIA GPU Operator to simplify the MIG setup that deploys MIG Manager to manage the MIG configuration and other essential configurations on the GPU nodes. Please refer to the NVIDIA documentation at https://docs.nvidia.com/datacenter/cloud-native/gpu-operator/latest/gpu-operator-mig.html for step-by-step instructions on setting up MIG on K8s clusters.

K8s identifies the individual MIG instances with a unique GPU instance ID and compute instance ID. These instances are exposed to K8s as extended resources (e.g., nvidia.com/mig-1g.18gb). The device plugin labels the node with the available MIG profiles and their quantities, enabling the K8s scheduler to match Pod resource requests to the appropriate MIG instance. For example, the following code snippet shows a K8s Pod requesting that nvidia.com/mig-1g.18gb be scheduled to a node that has an available instance of that specific configuration:

resources:
limits:
nvidia.com/mig-1g.18gb: 1

Now that we’ve learned how individual MIG instances are identified and allocated within K8s, let’s explore the different ways these partitions can be configured. Primarily, there are two strategies: single and mixed.

With the single MIG strategy (https://docs.nvidia.com/datacenter/cloud-native/gpu-operator/latest/gpu-operator-mig.html#example-single-mig-strategy), all GPU slices on a node are of the same size. For example, on a p5.48xlarge EC2 instance that features eight H100 GPUs, each with 80 GB of memory, you could create 56 slices of 1g.10gb, 24 slices of 2g.20gb, 16 slices of 3g.40gb, or even eight slices of 4g.40gb or 7g.80gb. This uniform approach is especially useful if multiple teams have similar GPU requirements. In such cases, you can allocate the same sized slice to each team, ensuring fair access and maximizing the utilization of the p5.48xlarge instance for workloads such as fine-tuning and inference, where the need for GPU capacity is consistent across tasks.

In contrast, the mixed MIG strategy (https://docs.nvidia.com/datacenter/cloud-native/gpu-operator/latest/gpu-operator-mig.html#example-mixed-mig-strategy) offers greater flexibility by allowing GPU slices of varying sizes to be created within each GPU on a node. For example, on a p5.24xlarge EC2 instance, which is equipped with eight NVIDIA H100 GPUs, you can combine different MIG slice configurations, such as 16 slices of 1g.10gb + 8 slices of 2g.20gb + 8 slices of 3g.40gb. This heterogeneous allocation is particularly beneficial for clusters that handle a wide range of workloads with diverse GPU requirements.

Consider an AI startup with three specialized workloads: an image recognition app, a natural language processing app, and a video analytics app. Using the mixed strategy, the startup can customize GPU allocations to match each app’s needs. For instance, the image recognition application might be assigned two 1g.10gb slices, the natural language processing application might utilize one 2g.20gb slice, and the video analytics application might benefit from one 3g.40gb slice, all operating concurrently on the same H100 GPU. This approach ensures that each application receives the appropriate level of GPU resources without overprovisioning, thereby maximizing the overall utilization and efficiency of the GPU resources.

MPS (https://docs.nvidia.com/deploy/mps/index.html) is a software feature that was designed to optimize GPU resource sharing across multiple processes. It enables multiple processes to submit work concurrently, reducing GPU idle time and improving resource utilization. As depicted in Figure 10*.3*, each process retains its own isolated GPU memory space, while compute resources are shared dynamically, allowing for low-latency scheduling and better performance for workloads that might not fully utilize GPU resources individually:

Figure 10.3 – NVIDIA MPS

In MPS, different processes running concurrently share GPU global memory, which introduces challenges that require careful consideration. Synchronization overhead can arise as processes or threads must be carefully managed to avoid race conditions or inconsistent states when accessing shared memory simultaneously. Additionally, resource contention may occur when multiple processes or threads compete for the same memory space, potentially creating performance bottlenecks. Balancing the use of shared global memory with private allocations adds complexity to programming, requiring careful planning and a deep understanding of CUDA (https://developer.nvidia.com/about-cuda) memory management to ensure both efficiency and correctness.

The memory isolation issue with MPS has been partially addressed, initially by the Volta architecture (https://docs.nvidia.com/deploy/mps/index.html#volta-mps). Volta GPUs introduced individual GPU address spaces for each client, ensuring that memory allocations by one process are not directly accessible to others, a significant improvement in memory isolation. Additionally, clients can submit work directly to the GPU without using a shared context, reducing contention and enabling finer resource allocation. Execution resource provisioning also ensures better control over GPU compute resources, preventing any single client from monopolizing them. However, limitations remain, with global memory bandwidth still being shared among all processes, potentially leading to performance degradation if one process overuses it. Additionally, MPS lacks fault isolation, meaning critical errors in one process can still disrupt others. While these improvements make MPS in Volta and newer GPUs more suitable for multi-process workloads with less stringent isolation requirements, NVIDA’s MIG remains the recommended solution when complete fault isolation is needed.

The NVIDIA device plugin for K8s does not currently support MPS partitioning, and it is tracked under this GitHub issue #443 (https://github.com/NVIDIA/k8s-device-plugin/issues/443). However, there is a forked version of the plugin at https://github.com/nebuly-ai/k8s-device-plugin that enables MPS support in K8s clusters. Refer to the following post by Medium for step-by-step instructions: https://medium.com/data-science/how-to-increase-gpu-utilization-in-kubernetes-with-nvidia-mps-e680d20c3181. Once MPS is enabled using this forked NVIDIA device plugin, multiple K8s Pods can share a single GPU. MPS dynamically manages compute resource allocation while maintaining memory isolation between Pods. This functionality enables fractional GPU requests in Pod resource definitions, allowing K8s to schedule multiple Pods on the same GPU efficiently.

For NVIDIA GPUs, time-slicing (https://docs.nvidia.com/datacenter/cloud-native/gpu-operator/latest/gpu-sharing.html) is another technique that allows multiple processes or applications to share GPU resources dynamically by dividing execution time into slices, enabling sequential access to the GPU. Figure 10*.4* illustrates how the GPU alternates between different processes over time, enabling them to share resources while each process typically retains its respective memory allocation:

Figure 10.4 – GPU time-slicing

Time-slicing supports workloads that would otherwise require exclusive access to GPUs, providing shared access but without the memory or fault isolation capability of NVIDIA’s MIG feature. The time-slicing feature is especially beneficial for older GPUs that do not support the MIG feature. It can also complement MIG by enabling multiple processes to share the resources of a single MIG partition. However, time-slicing may introduce latency as processes have to wait for their turn. This creates context-switching overhead, which involves saving and restoring the state of each process before switching to the next process.

Time-slicing is well suited for general-purpose, multi-process GPU usage and works effectively in virtualization, multi-tenant systems, and mixed workloads. However, for scenarios requiring strict resource isolation or high responsiveness, MIG might be a more appropriate solution.

Here is a high-level comparison of the GPU sharing techniques mentioned so far – that is, MIG, MPS, and time-slicing:

FeatureMIGMPSTime-Slicing
Resource isolationStrong (hardware-level)None or limited for Volta+ GPUsNone
PerformancePredictableImproved GPU utilization with added latencyDependent on workload characteristics
ScalabilityLimited by supported partition countHighHigh
OverheadMinimalLowHigher with context switching
Use vaseMulti-tenant, inferenceHPC, multi-process workloadsNon-critical tasks
CompatibilityAmpere+ GPUsAll CUDA-capable GPUsAll CUDA-capable GPUs

Table 10.2 – Comparison of NVIDIA GPU sharing techniques

The NVIDIA time-slicing feature in K8s enables GPU oversubscription, allowing multiple workloads to share a single GPU dynamically by interleaving their execution. This is achieved through the NVIDIA GPU Operator and enhanced configuration options in the NVIDIA device plugin for K8s. To enable the time-slicing feature in our EKS cluster setup, we need to create a ConfigMap and define time-slicing-config. In the following example, we are creating 10 replicas (virtual “time-sliced” GPUs) so that each K8s Pod requesting one nvidia.com/gpu resource will be allocated to one of these virtual GPUs and time-sliced on the underlying physical GPU:

apiVersion: v1
kind: ConfigMap
metadata:
name: time-slicing-config
namespace: nvidia-device-plugin
data:
any: |-
version: v1
flags:
migStrategy: none
sharing:
timeSlicing:
resources:
- name: nvidia.com/gpu
replicas: 10

Download this manifest file from https://github.com/PacktPublishing/Kubernetes-for-Generative-AI-Solutions/blob/main/ch10/nvidia-ts.yaml and execute the following commands to create a time-slicing-config ConfigMap:

$ kubectl apply -f nvidia-ts.yaml
configmap/time-slicing-config created

Now, update the NVIDIA K8s device plugin configuration so that it can use this ConfigMap in the Terraform code. Download the aiml-addons.tf file from https://github.com/PacktPublishing/Kubernetes-for-Generative-AI-Solutions/blob/main/ch10/aiml-addons.tf. This leverages the time-slicing-config ConfigMap to initialize the NVIDIA device plugin, as shown in the following code snippet:

module "eks_data_addons" {
source = "aws-ia/eks-data-addons/aws"
...
enable_nvidia_device_plugin = true
nvidia_device_plugin_helm_config = {
name = "nvidia-device-plugin"
values = [
...
config:
name: time-slicing-config
...

Execute the following command to apply the Terraform configuration:

$ terraform apply -auto-approve

The NVIDIA device plugin automatically reconciles the time-slicing configuration and updates the K8s node details accordingly. We can verify this by running the following command, which displays the GPU count as 10 on a g6.2xlarge EC2 instance. This indicates that one physical NVIDIA L4 GPU on the node has been virtualized into 10 replicas, making them available for the scheduler to allocate:

$ kubectl get nodes -o custom-columns=NAME:.metadata.name,INSTANCE:.metadata.labels."node\.kubernetes\.io/instance-type",GPUs:.status.allocatable."nvidia\.com/gpu"
NAME INSTANCE GPUs
ip-10-0-40-57.us-west-2.compute.internal g6.2xlarge 10

To demonstrate the use of NVIDIA time-slicing, let’s say we are deploying a small LLM such as Meta’s Llama-3.2-1B (https://huggingface.co/meta-llama/Llama-3.2-1B) parameter model in our application. This model requires 1.8 GB of GPU memory to load and perform inference. In a traditional setup, we would assign a single L4 GPU with 24 GB of memory to one K8s Pod, resulting in 92% of the GPU memory being underutilized. However, by creating 10 time-sliced replicas, we can deploy multiple instances of the Llama-3.2-1B parameter model on a single GPU.

We’ve already containerized the Llama-3.2-1B model into a Python FastAPI application and published it on Docker Hub. Using this image, we can deploy multiple copies of the model’s endpoint on the K8s node. Download the K8s deployment manifest from https://github.com/PacktPublishing/Kubernetes-for-Generative-AI-Solutions/blob/main/ch10/llama32-inf/llama32-deploy.yaml. Then, update the manifest with your Hugging Face token and execute the following commands to create a K8s deployment with five replicas of this model. You can verify that all replicas will run on the same node while sharing one physical GPU efficiently:

$ kubectl apply -f llama32-deploy.yaml
deployment/my-llama32-deployment created
$ kubectl get pods -o wide
<Displays 5 pods running on same node>

In this walkthrough, we applied the same time-slicing configuration across all nodes in the K8s cluster, irrespective of the GPU type. If you are using multiple GPU types (e.g., A100, L4, L40S, H100, etc.) in the same cluster, you can consider using a multiple node-specific configuration setup. In this approach, you define different time-slicing configurations for each GPU type in the ConfigMap and use node labels to specify which configuration should be applied to each GPU node. Please refer to the NVIDIA documentation at https://docs.nvidia.com/datacenter/cloud-native/gpu-operator/latest/gpu-sharing.html#applying-multiple-node-specific-configurations for a detailed walkthrough.

In this section, we learned about various NVIDIA GPU partitioning techniques (MIG, MPS, and time-slicing) and the differences among them. MIG provides strong isolation with minimal overhead and predictable performance, making it ideal for multi-tenant environments and inference workloads. On the other hand, MPS optimizes resource utilization by enabling multiple processes to share a single GPU concurrently. Finally, time-slicing allows multiple workloads to interleave on a single GPU by allocating compute resources in a round-robin fashion. It is compatible with all CUDA-capable GPUs, including older architectures, making it a versatile option. However, time-slicing introduces higher context-switching overhead and lacks both isolation and the efficiency improvements of MPS. In the next section, we will explore additional scaling and optimization considerations when using GPUs in K8s clusters.

When scaling K8s using GPU metrics, you can integrate NVIDIA DCGM to dynamically adjust workloads based on GPU health, utilization, and performance metrics. DCGM collects telemetry data, such as GPU utilization, memory usage, power consumption, and error rates, that is exposed through DCGM-Exporter for integration with Prometheus. Prometheus metrics are then consumed by autoscaling components such as Horizontal Pod Autoscaler (HPA) or Vertical Pod Autoscaler (VPA) to scale the K8s workloads.

For instance, in an ML training job that’s running on multiple nodes, DCGM-Exporter can track metrics such as nvidia_gpu_utilization, feeding them to Prometheus for analysis. In this instance, we can create an HPA policy that tracks GPU utilization and automatically increases the number of replicas (up to 10) when utilization exceeds 80%, thereby distributing processing across more GPUs:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: genai-training-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: genai-training
minReplicas: 1
maxReplicas: 10
metrics:
- type: Object
object:
metricName: DCGM_FI_DEV_GPU_UTIL
targetAverageValue: 80

Similarly, in an image recognition service, if the GPU utilization stays below 20% for 10 minutes, the autoscaler can reduce the number of Pods to save resources. DCGM also supports defining policies for error handling and health monitoring, ensuring K8s can reschedule Pods or drain nodes with unhealthy GPUs. For example, if DCGM detects memory errors or overheating, K8s can remove that node and migrate workloads to healthier nodes. These use cases demonstrate how GPU metrics enable fine-grained control over scaling to optimize resource usage.

However, GPU-based scaling also presents challenges. Metrics such as utilization or memory usage may not directly correlate with application performance, thereby requiring careful threshold tuning. Additionally, in environments using GPU time-slicing or MIG, workloads share resources, and DCGM metrics must account for such configurations to avoid over-scaling.

Continuously monitoring GPU metrics in large clusters can also add overhead, necessitating efficient integration with Prometheus and optimized alert rules. Scaling must also incorporate GPU health data, such as error rates or temperature, to avoid scheduling workloads on faulty hardware.

Best practices for GPU scaling include leveraging DCGM’s built-in policies for error detection and recovery, using custom Prometheus metrics that combine GPU and application-specific data, integrating the NVIDIA GPU Operator for seamless management, and regularly testing and fine-tuning scaling thresholds. By combining NVIDIA DCGM with K8s’s autoscaling capabilities, you can optimize GPU utilization, reduce costs, and enhance the performance of GPU-accelerated applications, ensuring scalability and reliability in resource-intensive environments.Bottom of Form

NVIDIA Inference Microservices (NIM) (https://developer.nvidia.com/nim) is a component of NVIDIA AI Enterprise that provides developers with GPU-accelerated inference microservices for deploying pretrained and customized AI models either on the cloud or in on-premises environments. These microservices are built on optimized inference engines from NVIDIA and the community, providing latency and throughput optimization for the specific combination of the foundation model and GPU system that’s detected at runtime. Additionally, NIM containers offer standard observability data feeds and built-in support for autoscaling on K8s with GPUs. Refer to the following AWS blog for a detailed walkthrough of deploying GenAI applications with NVIDIA NIM on Amazon EKS: https://aws.amazon.com/blogs/hpc/deploying-generative-ai-applications-with-nvidia-nims-on-amazon-eks/.

Each NIM container encapsulates a model, its runtime dependencies, and the inference engine. These containers are pre-configured for easy deployment and include APIs for interaction with applications. NIM microservices are deployed in a K8s cluster, enabling orchestration, scaling, and fault-tolerant management. Developers can use NIM to create custom AI workflows tailored to specific applications, such as RAG for chat-based question-answering or simulation pipelines for scientific research.

NIM’s architecture is designed to streamline the deployment of AI applications by offering prebuilt microservices that can be customized and scaled according to specific use cases. For instance, developers can deploy RAG pipelines for chat-based question-answering using NIM-hosted models available in the NVIDIA API catalog. These microservices are regularly updated so that they incorporate the latest advancements in AI models across various domains, including speech AI, data retrieval, digital biology, digital humans, simulation, and LLMs.

Developers interested in utilizing NIM can access it through the NVIDIA developer program, which offers free access for research, development, and testing purposes on up to 16 GPUs across any infrastructure – be it the cloud, a data center, or a personal workstation. For production deployments, NVIDIA AI Enterprise provides a comprehensive suite of tools, including NIM, that come with enterprise-grade security, support, and API stability (https://www.nvidia.com/en-us/data-center/products/ai-enterprise/).

Like CPU instances, GPU instances can be requested in the cloud as on-demand instances, reserved instances, or spot instances. However, the growing demand for GPUs has led to challenges in availability. In response, cloud providers are introducing innovative strategies to improve GPU accessibility and meet the needs of users more effectively.

For example, AWS has introduced Amazon EC2 Capacity Blocks for ML (https://aws.amazon.com/ec2/capacityblocks/), which allows you to reserve GPU instances in Amazon EC2 UltraClusters (https://aws.amazon.com/ec2/ultraclusters/) for future dates. At the time of writing, you can reserve capacity for durations ranging from 1 to 14 days, with the option to extend up to 6 months, and schedule start times up to 8 weeks in advance using Capacity Blocks. These blocks can be used for a wide range of ML workloads, from small-scale experiments to large-scale distributed training sessions, and are available for various instance types, including P5, P5e, P5en, P4d, Trn1, and Trn2, all powered by NVIDIA GPUs and AWS Trainium chips.

In this chapter, we covered options for optimizing GPU resources in K8s for GenAI applications. First, we described custom AI/ML accelerators, such as AWS Inferentia, Trainium, and Google TPUs. These specialized devices offer high performance and cost-efficiency for GenAI workloads, such as training LLMs or low-latency inference use cases. K8s supports these accelerators through device plugins, allowing them to be integrated into existing clusters seamlessly.

We also covered options to optimize GPU utilization in K8s. This is a critical step due to the high costs of GPU instances and their underutilization. This chapter highlighted various techniques you can implement to address the inefficient use of resources, such as MIG, MPS, and time-slicing.

MIG allows a single GPU to be partitioned into multiple isolated instances, providing a more granular and efficient allocation of resources. MPS, on the other hand, allows multiple processes to share GPU compute resources concurrently. Time-slicing further enables sequential access to the GPU by dividing execution time across different processes, a technique that’s beneficial for older GPUs that lack MIG support.

Finally, we covered GPU scaling practices within K8s and emphasized the role of NVIDIA DCGM, which can collect GPU telemetry data such as GPU utilization, memory usage, and power consumption. By integrating DCGM with Prometheus and K8s auto-scalers, you can dynamically scale workloads based on real-time GPU performance metrics.

NIM simplifies the deployment of AI models by providing pre-configured inference microservices optimized for specific GPU architectures. Cloud GPU availability remains a challenge, with demand often exceeding supply. In the next chapter, we will dive deeper into observability best practices for K8s.

Get This Book's PDF Version and Exclusive Extras