char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Kevin Pougetggml-virtgpu: improve the reliability of the code (#19846)ffaafde16

char/aliceai-t5-moe
6.1 KiB182 linesraw

GGML-VirtGPU Backend

The GGML-VirtGPU backend enables GGML applications to run machine learning computations on host hardware while the application itself runs inside a virtual machine. It uses host-guest shared memory to efficiently share data buffers between the two sides.

This backend relies on the virtio-gpu, and VirglRenderer API Remoting (APIR) component. The backend is split into two libraries:

  • a GGML implementation (the "remoting frontend"), running in the guest and interacting with the virtgpu device
  • a VirglRenderer APIR compatible library (the "remoting backend"), running in the host and interacting with Virglrenderer and an actual GGML device backend.

OS support

OSStatusBackendCI testingNotes
MacOS 14Supportedggml-metalXWorking when compiled on MacOS 14
MacOS 15Supportedggml-metalXWorking when compiled on MacOS 14 or MacOS 15
MacOS 26Not tested
LinuxUnder developmentggml-vulkannot workingWorking locally, CI running into deadlocks

Architecture Overview

The GGML-VirtGPU backend consists of three main components:

graph TD
    %% Nodes

 subgraph GuestVM ["Guest VM - Frontend"]
        App([GGML Application<br/>llama.cpp, etc.])

        direction TB
        Interface[GGML Backend Interface]
        Comm["GGML-VirtGPU<br/>(hypercalls + shared mem)"]

        App --> Interface
        Interface --> Comm
    end

    API[virtio-gpu / virglrenderer API]

    subgraph HostSystem [Host System - Backend]
        direction TB
        Dispatcher[GGML-VirtGPU-Backend]
        BackendLib[GGML Backend library<br/>Metal / Vulkan / CPU / ...]

        Dispatcher --> BackendLib
    end

    %% Connections
    Comm --> API
    API --> HostSystem

Key Components

  1. Guest-side Frontend (ggml-virtgpu/): Implements the GGML backend interface and forwards operations to the host
  2. Host-side Backend (ggml-virtgpu/backend/): Receives forwarded operations and executes them on actual hardware backends
  3. Communication Layer: Uses virtio-gpu hypercalls and shared memory for efficient data transfer

Features

  • Dynamic backend loading on the host side (CPU, CUDA, Metal, etc.)
  • Zero-copy data transfer via host-guest shared memory pages

Communication Protocol

Hypercalls and Shared Memory

The backend uses two primary communication mechanisms:

  1. Hypercalls (DRM_IOCTL_VIRTGPU_EXECBUFFER): Trigger remote execution from guest to host
  2. Shared Memory Pages: Zero-copy data transfer for tensors and parameters

Shared Memory Layout

Each connection uses two shared memory buffers:

  • Data Buffer (24 MiB): For command/response data and tensor transfers
  • Reply Buffer (16 KiB): For command replies and status information
  • Data Buffers: Dynamically allocated host-guest shared buffers served as GGML buffers.

APIR Protocol

The Virglrender API Remoting protocol defines three command types:

  • HANDSHAKE: Protocol version negotiation and capability discovery
  • LOADLIBRARY: Dynamic loading of backend libraries on the host
  • FORWARD: API function call forwarding

Binary Serialization

Commands and data are serialized using a custom binary protocol with:

  • Fixed-size encoding for basic types
  • Variable-length arrays with size prefixes
  • Buffer bounds checking
  • Error recovery mechanisms

Supported Operations

Device Operations

  • Device enumeration and capability queries
  • Memory information (total/free)
  • Backend type detection

Buffer Operations

  • Buffer allocation and deallocation
  • Tensor data transfer (host ↔ guest)
  • Memory copying and clearing

Computation Operations

  • Graph execution forwarding

Build Requirements

Guest-side Dependencies

  • libdrm for DRM/virtio-gpu communication
  • C++20 compatible compiler
  • CMake 3.14+

Host-side Dependencies

  • virglrenderer with APIR support (pending upstream review)
  • Target backend libraries (libggml-metal, libggml-vulkan, etc.)

Configuration

Environment Variables

  • GGML_VIRTGPU_BACKEND_LIBRARY: Path to the host-side backend library
  • GGML_VIRTGPU_DEBUG: Enable debug logging

Build Options

  • GGML_VIRTGPU: Enable the VirtGPU backend (ON or OFF, default: OFF)
  • GGML_VIRTGPU_BACKEND: Build the host-side backend component (ON, OFF or ONLY, default: OFF)

System Requirements

  • VM with virtio-gpu support
  • VirglRenderer with APIR patches
  • Compatible backend libraries on host

Limitations

  • VM-specific: Only works in virtual machines with virtio-gpu support
  • Host dependency: Requires properly configured host-side backend
  • Latency: Small overhead from VM escaping for each operation
  • Shared-memory size: with the libkrun hypervisor, the RAM + VRAM addressable memory is limited to 64 GB. So the maximum GPU memory will be 64GB - RAM, regardless of the hardware VRAM size.
  • This work is pending upstream changes in the VirglRenderer project.

    • The backend can be tested with Virglrenderer compiled from source using this PR: https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1590
  • This work is pending changes in the VMM/hypervisor running the virtual machine, which need to know how to route the newly introduced APIR capset.

    • The environment variable VIRGL_ROUTE_VENUS_TO_APIR=1 allows using the Venus capset, until the relevant hypervisors have been patched. However, setting this flag breaks the Vulkan/Venus normal behavior.
    • The environment variable GGML_REMOTING_USE_APIR_CAPSET tells the ggml-virtgpu backend to use the APIR capset. This will become the default when the relevant hypervisors have been patched.
  • This work focused on improving the performance of llama.cpp running on MacOS containers, and is mainly tested on this platform. The linux support (via krun) is in progress.

See Also

1# GGML-VirtGPU Backend
2
3The GGML-VirtGPU backend enables GGML applications to run machine
4learning computations on host hardware while the application itself
5runs inside a virtual machine.  It uses host-guest shared memory to
6efficiently share data buffers between the two sides.
7
8This backend relies on the virtio-gpu, and VirglRenderer API Remoting
9(APIR) component. The backend is split into two libraries:
10- a GGML implementation (the "remoting frontend"), running in the
11  guest and interacting with the virtgpu device
12- a VirglRenderer APIR compatible library (the "remoting backend"),
13  running in the host and interacting with Virglrenderer and an actual
14  GGML device backend.
15
16## OS support
17
18| OS       | Status            | Backend     | CI testing  | Notes
19| -------- | ----------------- | ----------- | ----------- | -----
20| MacOS 14 | Supported         | ggml-metal  | X           | Working when compiled on MacOS 14
21| MacOS 15 | Supported         | ggml-metal  | X           | Working when compiled on MacOS 14 or MacOS 15
22| MacOS 26 | Not tested        |             |             |
23| Linux    | Under development | ggml-vulkan | not working | Working locally, CI running into deadlocks
24
25
26## Architecture Overview
27
28The GGML-VirtGPU backend consists of three main components:
29
30```mermaid
31graph TD
32    %% Nodes
33
34 subgraph GuestVM ["Guest VM - Frontend"]
35        App([GGML Application<br/>llama.cpp, etc.])
36
37        direction TB
38        Interface[GGML Backend Interface]
39        Comm["GGML-VirtGPU<br/>(hypercalls + shared mem)"]
40
41        App --> Interface
42        Interface --> Comm
43    end
44
45    API[virtio-gpu / virglrenderer API]
46
47    subgraph HostSystem [Host System - Backend]
48        direction TB
49        Dispatcher[GGML-VirtGPU-Backend]
50        BackendLib[GGML Backend library<br/>Metal / Vulkan / CPU / ...]
51
52        Dispatcher --> BackendLib
53    end
54
55    %% Connections
56    Comm --> API
57    API --> HostSystem
58```
59
60### Key Components
61
621. **Guest-side Frontend** (`ggml-virtgpu/`): Implements the GGML backend interface and forwards operations to the host
632. **Host-side Backend** (`ggml-virtgpu/backend/`): Receives forwarded operations and executes them on actual hardware backends
643. **Communication Layer**: Uses virtio-gpu hypercalls and shared memory for efficient data transfer
65
66## Features
67
68- **Dynamic backend loading** on the host side (CPU, CUDA, Metal, etc.)
69- **Zero-copy data transfer** via host-guest shared memory pages
70
71## Communication Protocol
72
73### Hypercalls and Shared Memory
74
75The backend uses two primary communication mechanisms:
76
771. **Hypercalls (`DRM_IOCTL_VIRTGPU_EXECBUFFER`)**: Trigger remote execution from guest to host
782. **Shared Memory Pages**: Zero-copy data transfer for tensors and parameters
79
80#### Shared Memory Layout
81
82Each connection uses two shared memory buffers:
83
84- **Data Buffer** (24 MiB): For command/response data and tensor transfers
85- **Reply Buffer** (16 KiB): For command replies and status information
86- **Data Buffers**: Dynamically allocated host-guest shared buffers
87  served as GGML buffers.
88
89### APIR Protocol
90
91The Virglrender API Remoting protocol defines three command types:
92
93- `HANDSHAKE`: Protocol version negotiation and capability discovery
94- `LOADLIBRARY`: Dynamic loading of backend libraries on the host
95- `FORWARD`: API function call forwarding
96
97### Binary Serialization
98
99Commands and data are serialized using a custom binary protocol with:
100
101- Fixed-size encoding for basic types
102- Variable-length arrays with size prefixes
103- Buffer bounds checking
104- Error recovery mechanisms
105
106## Supported Operations
107
108### Device Operations
109- Device enumeration and capability queries
110- Memory information (total/free)
111- Backend type detection
112
113### Buffer Operations
114- Buffer allocation and deallocation
115- Tensor data transfer (host ↔ guest)
116- Memory copying and clearing
117
118### Computation Operations
119- Graph execution forwarding
120
121## Build Requirements
122
123### Guest-side Dependencies
124- `libdrm` for DRM/virtio-gpu communication
125- C++20 compatible compiler
126- CMake 3.14+
127
128### Host-side Dependencies
129- virglrenderer with APIR support (pending upstream review)
130- Target backend libraries (libggml-metal, libggml-vulkan, etc.)
131
132## Configuration
133
134### Environment Variables
135
136- `GGML_VIRTGPU_BACKEND_LIBRARY`: Path to the host-side backend library
137- `GGML_VIRTGPU_DEBUG`: Enable debug logging
138
139### Build Options
140
141- `GGML_VIRTGPU`: Enable the VirtGPU backend (`ON` or `OFF`, default: `OFF`)
142- `GGML_VIRTGPU_BACKEND`: Build the host-side backend component (`ON`, `OFF` or `ONLY`, default: `OFF`)
143
144### System Requirements
145
146- VM with virtio-gpu support
147- VirglRenderer with APIR patches
148- Compatible backend libraries on host
149
150## Limitations
151
152- **VM-specific**: Only works in virtual machines with virtio-gpu support
153- **Host dependency**: Requires properly configured host-side backend
154- **Latency**: Small overhead from VM escaping for each operation
155- **Shared-memory size**: with the `libkrun` hypervisor, the RAM + VRAM
156  addressable memory is limited to 64 GB. So the maximum GPU memory
157  will be `64GB - RAM`, regardless of the hardware VRAM size.
158
159* This work is pending upstream changes in the VirglRenderer
160  project.
161  * The backend can be tested with Virglrenderer compiled from source
162  using this PR:
163  https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1590
164* This work is pending changes in the VMM/hypervisor running the
165  virtual machine, which need to know how to route the newly
166  introduced APIR capset.
167  * The environment variable `VIRGL_ROUTE_VENUS_TO_APIR=1` allows
168    using the Venus capset, until the relevant hypervisors have been
169    patched. However, setting this flag breaks the Vulkan/Venus normal
170    behavior.
171  * The environment variable `GGML_REMOTING_USE_APIR_CAPSET` tells the
172    `ggml-virtgpu` backend to use the APIR capset. This will become
173    the default when the relevant hypervisors have been patched.
174
175* This work focused on improving the performance of llama.cpp running
176  on MacOS containers, and is mainly tested on this platform. The
177  linux support (via `krun`) is in progress.
178
179## See Also
180
181- [Development and Testing](VirtGPU/development.md)
182- [Backend configuration](VirtGPU/configuration.md)