# Sharing a GPU

> [!NOTE]
>
> **In one minute**
>
> - **Passthrough** gives a whole GPU to one VM: full speed, no sharing.
> - **vGPU** lets several VMs share one GPU through the vendor's software, taking turns (**time-sliced**) or each on its own slice of the chip (**MIG-backed**).
> - **MIG** cuts one GPU into isolated instances with their own memory and cores.
> - Choose by what you need most: performance, isolation, or many small users.

**Before you start.** [Why GPUs](<https://ai.ozarland.com/hardware/why-gpus/index.md>). The vendor statements are marked DOC; the rest is background.

## Four ways, side by side

| Way | What a VM gets | Shared? | Isolation | Notes |
| --- | --- | --- | --- | --- |
| **Passthrough** | The whole physical GPU | No | Complete | Simplest; the guest uses the normal driver |
| **Time-sliced vGPU** | A virtual GPU with a fixed share of the memory; compute shared in turns | Yes | Memory separated; compute shared, so a busy neighbour slows you | Needs the vendor's host software and a licence |
| **MIG** | A GPU instance: its own memory and cores | Yes | Hardware-separated | On MIG-capable data-centre GPUs; used inside a VM through MIG-backed vGPU |
| **MIG-backed vGPU** | A virtual GPU on one MIG instance | Yes | Hardware-separated | Combines vGPU management with MIG isolation |

There is also **time-slicing in containers**: several containers share one GPU that is already inside a server or a VM, with no isolation at all. It is a Kubernetes setting, not a way to give a GPU to VMs ([step 4](<https://ai.ozarland.com/run/index.md>)).

## Passthrough

The hypervisor hands the physical device to the VM through the IOMMU (the unit that lets a device read and write only the memory of the VM it belongs to). The guest installs the normal driver and sees a real GPU. Nothing is shared, and the host cannot use the GPU while the VM has it.

## vGPU

The vendor's software on the host (NVIDIA calls it the Virtual GPU Manager) presents several **virtual GPUs** from one physical GPU. Each vGPU **type** (or profile) fixes how much GPU memory it gets; the number of vGPUs a GPU can host follows from it. NVIDIA describes two partitioning modes DOC ([NVIDIA vGPU for Compute](<https://docs.nvidia.com/ai-enterprise/release-8/latest/infra-software/vgpu/overview.html>)):

- **Time-sliced:** the VMs take turns on the GPU's cores.
- **MIG-backed:** each VM owns the cores and engines of its GPU instance and runs in parallel with the others.

NVIDIA's vGPU for compute workloads is licensed through NVIDIA AI Enterprise DOC ([NVIDIA vGPU for Compute](<https://docs.nvidia.com/ai-enterprise/release-8/latest/infra-software/vgpu/overview.html>)), and a guest that gets no licence within 20 minutes runs with degraded performance DOC ([NVIDIA vGPU licensing](<https://docs.nvidia.com/vgpu/21.0/grid-licensing-user-guide/intro-to-grid-licensing.html>)). The licence server is a dependency of every vGPU instance.

## MIG

Multi-Instance GPU cuts one GPU into instances, each with its own memory, cache and cores. An H100 SXM can be cut into up to 7 instances of 10 GB DOC ([NVIDIA H100](<https://www.nvidia.com/en-us/data-center/h100/>)). One instance cannot slow down another, which makes MIG the strongest way to share a GPU between tenants.

## Choosing

| If you need... | Prefer |
| --- | --- |
| Full performance, a large model, or several linked GPUs in one VM | Passthrough |
| Many small users (notebooks, small models) and live migration | vGPU |
| Several tenants on one GPU with strict isolation | MIG-backed vGPU |
| Several containers of one team inside one VM | Passthrough or vGPU to the VM, then time-slicing in Kubernetes |

## Common mistakes

- **"vGPU gives each VM a fraction of the speed."** A time-sliced vGPU can use the whole GPU when its neighbours are idle, and much less when they are busy. Its share of memory, however, is fixed.
- **"MIG and vGPU are competitors."** For VMs, MIG is used *through* vGPU (MIG-backed vGPU).
- **"Passthrough needs no vendor software."** It needs no host software, but the guest still needs the vendor's driver and, for some uses, a licence.

## Check yourself

> [!NOTE]
>
> **Two tenants must share one GPU, and one must never slow the other down. Which way?**
>
> MIG-backed vGPU: each tenant's VM gets a hardware-separated MIG instance.

> [!NOTE]
>
> **What happens to a vGPU guest that cannot reach the licence server?**
>
> After 20 minutes without a licence, its performance is degraded, according to NVIDIA's licensing guide.

> [!NOTE]
>
> **Is time-slicing in Kubernetes a way to share a GPU between VMs?**
>
> No. It shares a GPU already given to one server or VM between its containers, with no isolation.

## Go further

- Next page: [The software stack](<https://ai.ozarland.com/hardware/software-stack/index.md>).
- How OpenStack gives each of these to an instance: [step 3, GPUs in OpenStack](<https://ai.ozarland.com/gpus/index.md>).
