BeeLlama.cpp Documentation

repository·main·Indexed 21 days ago

https://github.com/anbeeld/beellama.cpp

A performance-optimized fork of llama.cpp designed to maximize speed and context capacity for local GGUF inference. It features advanced KV-cache quantization, speculative decoding optimizations, and a C++ implementation of the Jinja template engine for chat templates with input marking for security against token injection. The project includes specialized build and execution guides for Snapdragon-based Android devices and Windows on Snapdragon, supporting CPU, Adreno GPU (OpenCL), and Hexagon NPU backends.

Tokens
190.8K
Snippets
571
Records
804
Agent score
73%

What's inside BeeLlama.cpp

  1. What is GBNF and how to use it

    main

    GBNF (GGML BNF) is a format used to constrain model outputs in llama.cpp by defining formal grammars. It allows you to force models to follow specific structures, such as valid JSON, specific notation (like chess moves), or restricted character sets (like emojis).

    GBNF grammars can be applied via:

    • llama-cli and llama-completion using the --grammar or --grammar-file flags.
    • llama-server completion endpoints by passing a grammar field in the request body.

    To test a grammar file with the CLI, use:

    ./llama-cli -m <model> --grammar-file grammars/some-grammar.gbnf -p 'Some prompt'
  2. Overview of the PEG Parser for Model Output

    main

    The common library provides a PEG (Parsing Expression Grammar) parser implementation specifically designed for parsing model output. It supports partial parsing of streaming input, built-in JSON parsing, and AST (Abstract Syntax Tree) generation using 'tagged' nodes.

    There are two main types of parsers:

    • common_peg_*: General-purpose PEG parser types.
    • common_chat_peg_*: Specialized helpers optimized for model output (e.g., handling reasoning content, tool calls, and chat formats).
  3. Overview of the LLaMA.cpp HTTP Server

    main

    The LLaMA.cpp HTTP Server is a fast, lightweight, pure C/C++ server built using httplib, nlohmann::json, and llama.cpp. It provides a set of REST APIs and a web UI to interact with Large Language Models (LLMs).

    Key capabilities include:

    • Inference: Supports F16 and quantized models on both GPU and CPU.
    • API Compatibility: Provides routes compatible with the OpenAI API (chat completions, responses, embeddings) and the Anthropic Messages API.
    • Advanced Inference: Supports parallel decoding for multi-user environments, continuous batching, speculative decoding, and reranking.
    • Advanced Features: Supports multimodal inputs, schema-constrained JSON responses, function calling/tool use, and prefilling of assistant messages (similar to the Claude API).
    • Monitoring: Includes endpoints for monitoring server status.
  4. Explore the llama-ui project structure

    main

    The source code is organized as follows:

    • src/lib/components/: UI components (subdivided into app/ and ui/).
    • src/lib/hooks/: Svelte hooks.
    • src/lib/stores/: State management logic.
    • src/lib/services/: API and database services.
    • src/lib/types/: TypeScript interfaces.
    • src/lib/utils/: Utility functions.
    • src/routes/: SvelteKit routes.
    • src/styles/: Global styles.
    • static/: Static assets.
    • tests/: Test files.
    • docs/: Architecture diagrams (high-level and feature-specific flows).
    • .storybook/: Storybook configuration.
  5. Use llama-bench for performance testing

    main

    llama-bench is a performance testing tool for llama.cpp used to measure the speed of prompt processing, text generation, and both combined. It calculates average tokens per second (t/s) and standard deviation across multiple repetitions.

    Test Types

    • Prompt processing (pp): Measures processing a prompt in batches using -p.
    • Text generation (tg): Measures generating a sequence of tokens using -n.
    • Prompt processing + text generation (pg): Measures both using -pg.

    Key Features

    • Repetitions: Use -r <n> to repeat each test $n$ times (default is 5) and average the results.
    • Context Depth: Use -d <n> to run tests at a specific context depth by prefilling the KV cache with $n$ tokens.
    • Batching/Multi-test: You can specify multiple values for parameters by separating them with commas (e.g., -n 16,32) or by repeating the flag (e.g., -n 16 -n 32).
    • Exclusions: Note that measurements do not include time for tokenization or sampling.
    NOTE

    Measurements do not include tokenization and sampling times.

  6. Web UI Features and Capabilities

    main

    The Web UI provides a comprehensive interface for model interaction, including:

    • Chat interface: Supports streaming responses.
    • Multi-model support (ROUTER mode): Allows switching between models with automatic loading upon selection.
    • Modality validation: Automatically checks if the selected model supports specific attachments like images or audio.
    • Conversation management: Supports branching, regeneration, and editing while preserving history.
    • Attachment support: Handles images, audio, and PDFs (using vision or text fallbacks).
    • Configurable parameters: Syncs parameters like temperature and top_p with server defaults.
    • Theming: Supports both dark and light modes.
  7. What is libmtmd and mtmd-cli?

    main

    libmtmd

    libmtmd is the modern replacement for the original llava.cpp implementation. It is designed to provide a unified interface for various multimodal models, supporting multiple input types (text, audio, and images) while handling complex model-specific chat templates. Its API is inspired by the Processor class in the Hugging Face transformers library.

    mtmd-cli

    mtmd-cli is the consolidated command-line tool powered by libmtmd. It replaces various model-specific binaries (like qwen2vl-cli, minicpmv-cli, and gemma3-cli) into a single, unified interface for interacting with multimodal models.

  8. Overview of the GGML RPC backend

    main

    The ggml-rpc-server allows you to expose ggml devices (like GPUs or CPUs) on a remote host. The RPC backend in llama.cpp communicates with one or more ggml-rpc-server instances via TCP to offload computations, enabling distributed LLM inference across multiple machines.

    IMPORTANT

    This is a proof-of-concept. The functionality is fragile and insecure. Never run the RPC server on an open network or in a sensitive environment!

  9. Overview of the GGML-VirtGPU Backend

    main

    The GGML-VirtGPU backend allows GGML applications (like llama.cpp) to run machine learning computations on host hardware while the application itself is running inside a virtual machine (Guest VM). It achieves this by using host-guest shared memory for zero-copy data transfer and virtio-gpu hypercalls to trigger remote execution.

    The architecture is split into two parts:

    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 (e.g., Metal, Vulkan, CUDA, or CPU).
  10. How multimodal support works in llama.cpp

    main

    Multimodal support in llama.cpp functions by encoding images into embeddings using a separate model component, which are then fed into the language model.

    To run a multimodal model, you typically need two distinct GGUF files:

    1. The standard language model file: The core LLM.
    2. A multimodal projector (mmproj) file: A specific component that handles image encoding and projection.

    This separation allows for independent development of vision architectures (like Vision Transformers) without modifying the core libllama library.

  11. Understand KVarN cache behavior and constraints

    main

    KVarN (Key-Value Quantization with Residuals) differs from standard quantized caches in several ways:

    Key Characteristics

    • Intrinsic Suffix: KVarN always resolves at least a 128-token exact suffix. Positive requests for tail tokens round upward to 128.
    • Eager Writing: Completed compressed records are written eagerly even while tokens coexist in the exact suffix.
    • Memory/RAM Management:
      • Unified KVarN: Requires an exclusive structured stream for RAM save/restore. Contention during save results in no cache entry; contention during restore results in a cache miss.
      • Non-unified KVarN: Multi-slot RAM caching and group-aligned historical reuse are unconditional.
    • Compatibility: BeeLlama v0.3.x sessions and v11 KVarN state are incompatible with v0.4.0 cache type IDs. Restore will fail closed; there is no migration shim.
  12. Understand JSON_NATIVE tool calling format

    main

    In JSON_NATIVE mode, the entire tool call (function name, arguments, and values) is contained within a JSON structure. The parser detects this when the function name appears inside a JSON context (e.g., preceded by { or :).

    Examples:

    Standard OpenAI-style:

    <tool_call>
    {"name": "get_weather", "arguments": {"location": "Paris", "unit": "celsius"}}
    </tool_call>

    Mistral Nemo with array wrapper:

    [TOOL_CALLS]
    [{"name": "calculate", "arguments": {"expr": "2+2"}}]

    Function name as JSON key (Apertus style):

    {"get_weather": {"location": "Paris"}}