llama.cpp perplexity

swaleio/llamacpp-perplexity

Category: Machine learning & AI Latest version b12855 · 2026-12-01 View versions

llama.cpp perplexity

Runs llama-perplexity from llama.cpp on a GGUF model and a text file: it splits the text into chunks of ctx_size tokens and reports how well the model predicts each next token. Lower is better. Measured on the same text and ctx_size, perplexity shows how much a quantization preset costs against the 16-bit model it came from.

The image is llama.cpp's CUDA 13 build, used as-is. Its entrypoint is llama.cpp's /app/tools.sh dispatcher, and the leading --perplexity argument runs llama-perplexity with the rest. The entrypoint can neither redirect the tool's output nor write a task output, so the result reaches later tasks through log_file: llama.cpp writes its log there, ending with a line such as Final estimate: PPL = <number> +/- <error>.

Inputs

Input Required Default Description
model yes — Absolute path of the .gguf model file.
text_file yes — Absolute path of the plain-text file to measure on. The text needs at least two chunks of ctx_size tokens.
log_file yes — Absolute path for llama.cpp's log, which ends with the Final estimate: PPL = … line. Point it at the mounted workspace (e.g. ${{env.WORKFLOW_STORAGE}}/ppl-q4.log) so a later task can read it.
ctx_size no 512 Tokens per chunk, llama-perplexity's own default.

Outputs

None. The result is the Final estimate line in log_file, a path the workflow chose; a later task reads the number from there.

Compute

GPU recommended. The image loads its CUDA backend when the compute type has a GPU and offloads the model to it, and falls back to its CPU backend otherwise. A small quantized model measures a short text on a CPU compute type; larger models and texts want a GPU.

Example

Fetch a 4-bit GGUF model and measure it on the questions and answers of the openai/gsm8k test split (MIT), which a duckdb task writes out as plain text:

name: llama.cpp perplexity example
compute_type: cpu-4
entry_point: main

blocks:
  main:
    tasks:
      model:
        name: Fetch model
        uses: swaleio/hf-download
        args:
          repo: Qwen/Qwen2.5-0.5B-Instruct-GGUF
          include: "qwen2.5-0.5b-instruct-q4_k_m.gguf"
          dest: ${{env.WORKFLOW_STORAGE}}/gguf
      text:
        name: Write text
        uses: swaleio/duckdb
        args:
          sql: |
            COPY (SELECT question || ' ' || answer
                  FROM 'hf://datasets/openai/gsm8k/main/test-*.parquet' LIMIT 300)
              TO '${{env.WORKFLOW_STORAGE}}/gsm8k.txt'
              (FORMAT csv, HEADER false, QUOTE '', DELIMITER '\t');
      measure:
        name: Measure perplexity
        uses: swaleio/llamacpp-perplexity
        start_on:
          - model
          - text
        args:
          model: ${{env.WORKFLOW_STORAGE}}/gguf/qwen2.5-0.5b-instruct-q4_k_m.gguf
          text_file: ${{env.WORKFLOW_STORAGE}}/gsm8k.txt
          log_file: ${{env.WORKFLOW_STORAGE}}/ppl-q4_k_m.log

Later tasks read the Final estimate line of ${{env.WORKFLOW_STORAGE}}/ppl-q4_k_m.log.


For the workflow syntax these examples use, see the workflow definition reference in the Swale documentation.

Licensed under the MIT License.