char-fork/llama.cpp

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

Masashi Yoshimuraggml-virtgpu: Fix some build commands (#20341)05039967d

char/aliceai-t5-moe
6.0 KiB222 linesraw

Development and Testing

Development

Code Generation

The backend uses code generation from YAML configuration:

# Regenerate protocol code
cd ggml-virtgpu/
python regenerate_remoting.py

Adding New Operations

  1. Add function definition to ggmlremoting_functions.yaml
  2. Regenerate code with regenerate_remoting.py
  3. Implement guest-side forwarding in virtgpu-forward-*.cpp
  4. Implement host-side handling in backend-dispatched-*.cpp

Testing

This document provides instructions for building and testing the GGML-VirtGPU backend on macOS with containers.

Prerequisites

The testing setup requires:

  • macOS host system
  • Container runtime with libkrun provider (podman machine)
  • Access to development patchset for VirglRenderer

Required Patchsets

The backend requires patches that are currently under review:

  • Virglrenderer APIR upstream PR: https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1590 (for reference)
  • MacOS Virglrenderer (for krunkit): https://gitlab.freedesktop.org/kpouget/virglrenderer/-/tree/main-macos
  • Linux Virglrenderer (for krun): https://gitlab.freedesktop.org/kpouget/virglrenderer/-/tree/main-linux

Build Instructions

1. Build ggml-virtgpu-backend (Host-side, macOS)

# Build the backend that runs natively on macOS
mkdir llama.cpp
cd llama.cpp
git clone https://github.com/ggml-org/llama.cpp.git src
cd src

LLAMA_MAC_BUILD=$PWD/build/ggml-virtgpu-backend

cmake -S . -B $LLAMA_MAC_BUILD \
      -DGGML_NATIVE=OFF \
      -DLLAMA_CURL=ON \
      -DGGML_VIRTGPU=ON \
      -DGGML_VIRTGPU_BACKEND=ONLY \
      -DGGML_METAL=ON

TARGETS="ggml-metal"
cmake --build $LLAMA_MAC_BUILD --parallel 8 --target $TARGETS

# Build additional tools for native benchmarking
EXTRA_TARGETS="llama-run llama-bench"
cmake --build $LLAMA_MAC_BUILD --parallel 8 --target $EXTRA_TARGETS

2. Build virglrenderer (Host-side, macOS)

# Build virglrenderer with APIR support
mkdir virglrenderer
cd virglrenderer
git clone https://gitlab.freedesktop.org/kpouget/virglrenderer -b main-macos src
cd src

VIRGL_BUILD_DIR=$PWD/build

# -Dvenus=true and VIRGL_ROUTE_VENUS_TO_APIR=1 route the APIR requests via the Venus backend, for easier testing without a patched hypervisor

meson setup $VIRGL_BUILD_DIR \
      -Dvenus=true \
      -Dapir=true

ninja -C $VIRGL_BUILD_DIR

3. Build ggml-virtgpu (Guest-side, Linux)

Option A: Build from a script:

# Inside a Linux container
mkdir llama.cpp
git clone https://github.com/ggml-org/llama.cpp.git src
cd src

LLAMA_LINUX_BUILD=$PWD/build-virtgpu

cmake -S . -B $LLAMA_LINUX_BUILD \
      -DGGML_VIRTGPU=ON

ninja -C $LLAMA_LINUX_BUILD

Option B: Build container image with frontend:

cat << EOF > remoting.containerfile
FROM quay.io/fedora/fedora:43
USER 0

WORKDIR /app/remoting

ARG LLAMA_CPP_REPO="https://github.com/ggml-org/llama.cpp.git"
ARG LLAMA_CPP_VERSION="master"
ARG LLAMA_CPP_CMAKE_FLAGS="-DGGML_VIRTGPU=ON"
ARG LLAMA_CPP_CMAKE_BUILD_FLAGS="--parallel 4"

RUN dnf install -y git cmake gcc gcc-c++ libcurl-devel libdrm-devel

RUN git clone "\${LLAMA_CPP_REPO}" src \\
 && git -C src fetch origin \${LLAMA_CPP_VERSION} \\
 && git -C src reset --hard FETCH_HEAD

RUN mkdir -p build \\
 && cd src \\
 && set -o pipefail \\
 && cmake -S . -B ../build \${LLAMA_CPP_CMAKE_FLAGS} \\
 && cmake --build ../build/ \${LLAMA_CPP_CMAKE_BUILD_FLAGS}

ENTRYPOINT ["/app/remoting/src/build/bin/llama-server"]
EOF

mkdir -p empty_dir
podman build -f remoting.containerfile ./empty_dir -t localhost/llama-cpp.virtgpu

Environment Setup

Set krunkit Environment Variables

# Define the base directories (adapt these paths to your system)
VIRGL_BUILD_DIR=$HOME/remoting/virglrenderer/build
LLAMA_MAC_BUILD=$HOME/remoting/llama.cpp/build-backend

# For krunkit to load the custom virglrenderer library
export DYLD_LIBRARY_PATH=$VIRGL_BUILD_DIR/src

# For Virglrenderer to load the ggml-remotingbackend library
export VIRGL_APIR_BACKEND_LIBRARY="$LLAMA_MAC_BUILD/bin/libggml-virtgpu-backend.dylib"

# For llama.cpp remotingbackend to load the ggml-metal backend
export APIR_LLAMA_CPP_GGML_LIBRARY_PATH="$LLAMA_MAC_BUILD/bin/libggml-metal.dylib"
export APIR_LLAMA_CPP_GGML_LIBRARY_REG=ggml_backend_metal_reg

Launch Container Environment

# Set container provider to libkrun
export CONTAINERS_MACHINE_PROVIDER=libkrun
podman machine start

Verify Environment

Confirm that krunkit is using the correct virglrenderer library:

lsof -c krunkit | grep virglrenderer
# Expected output:
# krunkit 50574 user  txt  REG  1,14  2273912  10849442 ($VIRGL_BUILD_DIR/src)/libvirglrenderer.1.dylib

Running Tests

Launch Test Container

# Optional model caching
mkdir -p models
PODMAN_CACHE_ARGS="-v models:/models --user root:root --cgroupns host --security-opt label=disable -w /models"

podman run $PODMAN_CACHE_ARGS -it --rm --device /dev/dri localhost/llama-cpp.virtgpu

Test llama.cpp in Container

# Run performance benchmark
/app/remoting/build/bin/llama-bench -m ./llama3.2

Expected output (performance may vary):

| model                          |       size |     params | backend    | ngl |          test |                  t/s |
| ------------------------------ | ---------: | ---------: | ---------- | --: | ------------: | -------------------: |
| llama 3B Q4_K - Medium         |   1.87 GiB |     3.21 B | ggml-virtgpu |  99 |         pp512 |        991.30 ± 0.66 |
| llama 3B Q4_K - Medium         |   1.87 GiB |     3.21 B | ggml-virtgpu |  99 |         tg128 |         85.71 ± 0.11 |

Troubleshooting

SSH Environment Variable Issues

⚠️ Warning: Setting DYLD_LIBRARY_PATH from SSH doesn't work on macOS. Here is a workaround:

Workaround 1: Replace system library

VIRGL_BUILD_DIR=$HOME/remoting/virglrenderer/build  # ⚠️ adapt to your system
BREW_VIRGL_DIR=/opt/homebrew/Cellar/virglrenderer/0.10.4d/lib
VIRGL_LIB=libvirglrenderer.1.dylib

cd $BREW_VIRGL_DIR
mv $VIRGL_LIB ${VIRGL_LIB}.orig
ln -s $VIRGL_BUILD_DIR/src/$VIRGL_LIB
1# Development and Testing
2
3## Development
4
5### Code Generation
6
7The backend uses code generation from YAML configuration:
8
9```bash
10# Regenerate protocol code
11cd ggml-virtgpu/
12python regenerate_remoting.py
13```
14
15### Adding New Operations
16
171. Add function definition to `ggmlremoting_functions.yaml`
182. Regenerate code with `regenerate_remoting.py`
193. Implement guest-side forwarding in `virtgpu-forward-*.cpp`
204. Implement host-side handling in `backend-dispatched-*.cpp`
21
22## Testing
23
24This document provides instructions for building and testing the GGML-VirtGPU backend on macOS with containers.
25
26### Prerequisites
27
28The testing setup requires:
29
30- macOS host system
31- Container runtime with `libkrun` provider (podman machine)
32- Access to development patchset for VirglRenderer
33
34### Required Patchsets
35
36The backend requires patches that are currently under review:
37
38- **Virglrenderer APIR upstream PR**: https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1590 (for reference)
39- **MacOS Virglrenderer (for krunkit)**: https://gitlab.freedesktop.org/kpouget/virglrenderer/-/tree/main-macos
40- **Linux Virglrenderer (for krun)**: https://gitlab.freedesktop.org/kpouget/virglrenderer/-/tree/main-linux
41
42### Build Instructions
43
44#### 1. Build ggml-virtgpu-backend (Host-side, macOS)
45
46```bash
47# Build the backend that runs natively on macOS
48mkdir llama.cpp
49cd llama.cpp
50git clone https://github.com/ggml-org/llama.cpp.git src
51cd src
52
53LLAMA_MAC_BUILD=$PWD/build/ggml-virtgpu-backend
54
55cmake -S . -B $LLAMA_MAC_BUILD \
56      -DGGML_NATIVE=OFF \
57      -DLLAMA_CURL=ON \
58      -DGGML_VIRTGPU=ON \
59      -DGGML_VIRTGPU_BACKEND=ONLY \
60      -DGGML_METAL=ON
61
62TARGETS="ggml-metal"
63cmake --build $LLAMA_MAC_BUILD --parallel 8 --target $TARGETS
64
65# Build additional tools for native benchmarking
66EXTRA_TARGETS="llama-run llama-bench"
67cmake --build $LLAMA_MAC_BUILD --parallel 8 --target $EXTRA_TARGETS
68```
69
70#### 2. Build virglrenderer (Host-side, macOS)
71
72```bash
73# Build virglrenderer with APIR support
74mkdir virglrenderer
75cd virglrenderer
76git clone https://gitlab.freedesktop.org/kpouget/virglrenderer -b main-macos src
77cd src
78
79VIRGL_BUILD_DIR=$PWD/build
80
81# -Dvenus=true and VIRGL_ROUTE_VENUS_TO_APIR=1 route the APIR requests via the Venus backend, for easier testing without a patched hypervisor
82
83meson setup $VIRGL_BUILD_DIR \
84      -Dvenus=true \
85      -Dapir=true
86
87ninja -C $VIRGL_BUILD_DIR
88```
89
90#### 3. Build ggml-virtgpu (Guest-side, Linux)
91
92Option A: Build from a script:
93
94```bash
95# Inside a Linux container
96mkdir llama.cpp
97git clone https://github.com/ggml-org/llama.cpp.git src
98cd src
99
100LLAMA_LINUX_BUILD=$PWD/build-virtgpu
101
102cmake -S . -B $LLAMA_LINUX_BUILD \
103      -DGGML_VIRTGPU=ON
104
105ninja -C $LLAMA_LINUX_BUILD
106```
107
108Option B: Build container image with frontend:
109
110```bash
111cat << EOF > remoting.containerfile
112FROM quay.io/fedora/fedora:43
113USER 0
114
115WORKDIR /app/remoting
116
117ARG LLAMA_CPP_REPO="https://github.com/ggml-org/llama.cpp.git"
118ARG LLAMA_CPP_VERSION="master"
119ARG LLAMA_CPP_CMAKE_FLAGS="-DGGML_VIRTGPU=ON"
120ARG LLAMA_CPP_CMAKE_BUILD_FLAGS="--parallel 4"
121
122RUN dnf install -y git cmake gcc gcc-c++ libcurl-devel libdrm-devel
123
124RUN git clone "\${LLAMA_CPP_REPO}" src \\
125 && git -C src fetch origin \${LLAMA_CPP_VERSION} \\
126 && git -C src reset --hard FETCH_HEAD
127
128RUN mkdir -p build \\
129 && cd src \\
130 && set -o pipefail \\
131 && cmake -S . -B ../build \${LLAMA_CPP_CMAKE_FLAGS} \\
132 && cmake --build ../build/ \${LLAMA_CPP_CMAKE_BUILD_FLAGS}
133
134ENTRYPOINT ["/app/remoting/src/build/bin/llama-server"]
135EOF
136
137mkdir -p empty_dir
138podman build -f remoting.containerfile ./empty_dir -t localhost/llama-cpp.virtgpu
139```
140
141### Environment Setup
142
143#### Set krunkit Environment Variables
144
145```bash
146# Define the base directories (adapt these paths to your system)
147VIRGL_BUILD_DIR=$HOME/remoting/virglrenderer/build
148LLAMA_MAC_BUILD=$HOME/remoting/llama.cpp/build-backend
149
150# For krunkit to load the custom virglrenderer library
151export DYLD_LIBRARY_PATH=$VIRGL_BUILD_DIR/src
152
153# For Virglrenderer to load the ggml-remotingbackend library
154export VIRGL_APIR_BACKEND_LIBRARY="$LLAMA_MAC_BUILD/bin/libggml-virtgpu-backend.dylib"
155
156# For llama.cpp remotingbackend to load the ggml-metal backend
157export APIR_LLAMA_CPP_GGML_LIBRARY_PATH="$LLAMA_MAC_BUILD/bin/libggml-metal.dylib"
158export APIR_LLAMA_CPP_GGML_LIBRARY_REG=ggml_backend_metal_reg
159```
160
161#### Launch Container Environment
162
163```bash
164# Set container provider to libkrun
165export CONTAINERS_MACHINE_PROVIDER=libkrun
166podman machine start
167```
168
169#### Verify Environment
170
171Confirm that krunkit is using the correct virglrenderer library:
172
173```bash
174lsof -c krunkit | grep virglrenderer
175# Expected output:
176# krunkit 50574 user  txt  REG  1,14  2273912  10849442 ($VIRGL_BUILD_DIR/src)/libvirglrenderer.1.dylib
177```
178
179### Running Tests
180
181#### Launch Test Container
182
183```bash
184# Optional model caching
185mkdir -p models
186PODMAN_CACHE_ARGS="-v models:/models --user root:root --cgroupns host --security-opt label=disable -w /models"
187
188podman run $PODMAN_CACHE_ARGS -it --rm --device /dev/dri localhost/llama-cpp.virtgpu
189```
190
191#### Test llama.cpp in Container
192
193```bash
194
195# Run performance benchmark
196/app/remoting/build/bin/llama-bench -m ./llama3.2
197```
198
199Expected output (performance may vary):
200```
201| model                          |       size |     params | backend    | ngl |          test |                  t/s |
202| ------------------------------ | ---------: | ---------: | ---------- | --: | ------------: | -------------------: |
203| llama 3B Q4_K - Medium         |   1.87 GiB |     3.21 B | ggml-virtgpu |  99 |         pp512 |        991.30 ± 0.66 |
204| llama 3B Q4_K - Medium         |   1.87 GiB |     3.21 B | ggml-virtgpu |  99 |         tg128 |         85.71 ± 0.11 |
205```
206
207### Troubleshooting
208
209#### SSH Environment Variable Issues
210
211⚠️ **Warning**: Setting `DYLD_LIBRARY_PATH` from SSH doesn't work on macOS. Here is a workaround:
212
213**Workaround 1: Replace system library**
214```bash
215VIRGL_BUILD_DIR=$HOME/remoting/virglrenderer/build  # ⚠️ adapt to your system
216BREW_VIRGL_DIR=/opt/homebrew/Cellar/virglrenderer/0.10.4d/lib
217VIRGL_LIB=libvirglrenderer.1.dylib
218
219cd $BREW_VIRGL_DIR
220mv $VIRGL_LIB ${VIRGL_LIB}.orig
221ln -s $VIRGL_BUILD_DIR/src/$VIRGL_LIB
222```