# Using GPUs

https://virtx.sh/docs/tutorials/using-gpus/

Give a sandbox the host's GPU through Vulkan, with the loader and driver that come with every sandbox.

A sandbox can use the host's GPU through Vulkan, on Linux, macOS and Windows hosts.

- **The host** needs a Vulkan loader.
- **The sandbox** already includes a Vulkan loader and the venus driver, which passes Vulkan calls to the host's GPU. Your image doesn't need either.

GPUs are not available on VirtX cloud yet. Use a local server or your own server.

## Create a GPU sandbox

Set `gpu` when you create a sandbox.
`gpu_memory_mib` (`gpuMemoryMib` in Node.js) sets how much GPU memory the sandbox may use, separate from its regular memory. The default is 8 GiB.
This example creates a GPU sandbox, installs `vulkaninfo`, and checks that the GPU is visible:

**Python**

```python
sandbox = await client.create_sandbox("alpine:latest", gpu=True)
await sandbox.exec(
    ["sh", "-c", "apk add --no-cache vulkan-tools && vulkaninfo --summary"]
).complete(print=True)
```

**Node.js**

```js
const sandbox = await client.createSandbox('alpine:latest', { gpu: true })
await sandbox
  .exec(['sh', '-c', 'apk add --no-cache vulkan-tools && vulkaninfo --summary'])
  .complete({ print: true })
```

**Rust**

```rust
let sandbox = client
    .create_sandbox(&SandboxCreateArgs::new("alpine:latest").gpu(true))
    .await?;
sandbox
    .exec(&SandboxExecArgs::new([
        "sh",
        "-c",
        "apk add --no-cache vulkan-tools && vulkaninfo --summary",
    ]))
    .complete(true)
    .await?;
```

In the sandbox, the GPU appears as `Virtio-GPU Venus (<the host's GPU>)`.
A server built without GPU support rejects a GPU sandbox instead of running it on the CPU. Passing `gpu_memory_mib` without `gpu` is also rejected.

## What the sandbox brings

Every sandbox has `/alib`, a read-only directory of libraries that VirtX provides. It contains the Vulkan loader and venus driver in two builds:

- `/alib/musl` for musl-based images, such as Alpine.
- `/alib/glibc` for images with glibc 2.28 or later, such as Debian and Ubuntu.

When a GPU sandbox starts, VirtX adds the matching directory (for the C library that `/bin/sh` uses) to `LD_LIBRARY_PATH`, after any paths the image sets.
VirtX also sets `VK_DRIVER_FILES` and `VK_ICD_FILENAMES` to venus, unless the image sets either one. This way a program finds the host's GPU whatever Vulkan drivers the image has installed.
A sandbox without a GPU gets neither setting, and uses the image's own Vulkan, if it has one.

In a GPU sandbox:

- **VirtX's driver replaces the image's Vulkan driver.** To use a different driver, set `VK_DRIVER_FILES` (or `VK_ICD_FILENAMES`) in the image.
- **Programs load the Vulkan loader from `/alib` before the image's,** whether they link it or open `libvulkan.so.1` by name.

## What the device offers

The device supports up to Vulkan 1.4, depending on the host's driver.
On a Mac with an Apple7 GPU or later (M1 and later), it also supports bfloat16 (`VK_KHR_shader_bfloat16`) and cooperative matrices (`VK_KHR_cooperative_matrix`) in 8x8 and 16x16 shapes.
If your image brings its own venus driver through `VK_DRIVER_FILES`, it needs Mesa 26.0 or later for these two extensions. Earlier versions of venus don't pass them through.

See [Working with images](https://virtx.sh/docs/tutorials/images/) for building the guest image, and [Running commands](https://virtx.sh/docs/tutorials/running-commands/) for reading command output.

## Troubleshooting

1. **`422`, `unsupported`, `this server was built without a GPU`**

   **Cause:** The server was built without GPU support.

   **Fix:** Use a server built with it.

2. **`400`, `invalid`, `gpu_memory_mib: a size is of a GPU, and this sandbox has none`**

   **Cause:** `gpu_memory_mib` without `gpu`.

   **Fix:** Turn `gpu` on too, or leave `gpu_memory_mib` out.

3. **`400`, `invalid`, `gpu_memory_mib: 0 is a machine nothing runs on`**

   **Cause:** `gpu_memory_mib` is `0`.

   **Fix:** Leave it out to get 8 GiB, or give a size in MiB.

## Next

- [Setting up sandboxes](https://virtx.sh/docs/tutorials/setting-sandboxes/): the other options a sandbox takes.
- [Running commands](https://virtx.sh/docs/tutorials/running-commands/): run the program that uses the GPU.
