Published: March 31, 2026

Deep Dive: How Gollek Orchestrates GGUF with llama.cpp and Project Panama

In our previous post, we discussed why we chose Java for AI. Today, we’re peeling back the layers of the GGUF Runner—the engine responsible for running quantized models like Llama 3, Qwen, and Mistral at native speeds within the Java Virtual Machine.

Located in gollek/plugins/runner/gguf, this runner isn’t just a simple wrapper. It’s a sophisticated orchestration layer that bridges the high-level Java world with the optimized C++ kernels of llama.cpp.


1. The Architecture of a Modern Runner

The GGUF runner is built as a modular system where each component has a specific responsibility:

  • LlamaCppBinding: The heart of the integration. It uses the JDK 21+ Foreign Function & Memory (FFM) API (Project Panama) to call llama.cpp directly, bypassing the overhead and complexity of JNI.
  • LlamaCppModelInitializer: Manages the complex lifecycle of loading GGUF binaries, calculating GPU layer offloading, and initializing the compute context.
  • LlamaCppKVCacheManager: Handles the KV (Key-Value) cache, ensuring that multi-turn conversations are efficient by reusing previous computations.
  • LlamaCppTokenSampler: Implements advanced sampling strategies (Temperature, Top-K, Top-P, Min-P, and Penalties) directly in the inference loop.

2. Goodbye JNI, Hello Project Panama

Traditionally, Java-to-C++ communication required JNI (Java Native Interface), which involved writing “glue” code in C, managing complex headers, and dealing with significant call overhead.

Gollek uses Project Panama to define these bindings purely in Java. Here’s a look at how we define the llama_decode function in LlamaCppBinding.java:

// Definition of the native function signature
private final MethodHandle llama_decode = Linker.nativeLinker().downcallHandle(
    symbolLookup.find("llama_decode").get(),
    FunctionDescriptor.of(ValueLayout.JAVA_INT, 
        ValueLayout.ADDRESS, // context
        LLAMA_BATCH_LAYOUT   // batch data
    )
);

By using MemorySegment and Arena, we manage native memory with safety and performance that was previously impossible in Java. This allows for zero-copy data transfer between Java and the C++ engine.


3. Beyond Text: Multimodal LLaVA Support

One of the most powerful features of Gollek’s GGUF runner is its Multimodal Support. In GGUFMultimodalProcessor.java, we’ve implemented support for LLaVA (Large Language-and-Vision Assistant) models.

When you send an image to Gollek, the runner:

  1. Processes the image using a dedicated vision encoder.
  2. Projects the image embeddings into the same space as the text tokens.
  3. Injects these “visual tokens” into the llama.cpp context.
  4. Generates a text response based on both the image and your prompt.

All of this happens through the same high-performance Panama bindings, allowing Java developers to build vision-aware AI applications without leaving the ecosystem.


4. Intelligent GPU Offloading

GGUF models are famous for their ability to run on both CPU and GPU. Gollek’s LlamaCppModelInitializer dynamically calculates how many layers can fit into your VRAM:

// Snippet of the logic in ModelInitializer
int activeGpuLayers = calculateOptimalGpuLayers(modelSize, availableVram);
llamaCppBinding.setModelParam(modelParams, "n_gpu_layers", activeGpuLayers);

This ensures that whether you’re on a Mac with Metal, a Linux box with NVIDIA CUDA, or a laptop with only a CPU, Gollek automatically scales to provide the best possible performance.


5. Performance Metrics

Thanks to the FFM API and the efficiency of llama.cpp, the overhead of calling into native code from Java is negligible. On a modern Apple M2 Max, the GGUF runner achieves:

Model Speed (Tokens/sec) Latency (First Token)
Llama 3 8B (Q4_K_M) ~45-55 t/s < 150ms
Qwen 2.5 7B (Q8_0) ~35-40 t/s < 200ms

Conclusion

The GGUF runner in Gollek represents a new era of Java engineering. By leveraging the latest JDK features and the best-in-class native engines, we’ve created a platform that offers Python-like flexibility with C++ performance, all while maintaining the Java type safety and stability that enterprises demand.

Ready to dive deeper? Check out the source code in gollek/plugins/runner/gguf or start building your first multimodal app today!