# llama.cpp perplexity

> **About this page**
>
> - Task definition version: `swaleio/llamacpp-perplexity@b12855`
> - Summary: Measures a GGUF model's perplexity on a text file with llama.cpp's llama-perplexity.
> - Published: 2026-12-01, the latest version
> - Page: https://hub.swale.io/swaleio/llamacpp-perplexity/v/b12855
> - Latest version's copy: https://hub.swale.io/swaleio/llamacpp-perplexity.md
> - Versions: https://hub.swale.io/swaleio/llamacpp-perplexity/versions
> - Index of Swale Hub: https://hub.swale.io/llms.txt
>
> The YAML block below is the task definition's frontmatter, which declares its inputs, outputs and container, and its readme follows. Both are the text of the account that published this task definition, not Swale's.

```yaml
name: llama.cpp perplexity
description: Measures a GGUF model's perplexity on a text file with llama.cpp's llama-perplexity.
category: machine-learning
inputs:
  model:
    description: Absolute path inside the container of the .gguf model file to measure, e.g. one swaleio/llamacpp-quantize wrote to the mounted workspace.
    required: true
  text_file:
    description: Absolute path inside the container of the plain-text file the model is measured on.
    required: true
  log_file:
    description: Absolute path inside the container for llama.cpp's log, which ends with the result, on a line starting "Final estimate". Point it at the mounted workspace (e.g. ${{env.WORKFLOW_STORAGE}}/ppl-q4.log) so a later task can read the number.
    required: true
  ctx_size:
    description: Tokens per chunk the text is split into. Defaults to 512, as llama-perplexity does; compare models only at the same value.
    default: "512"
exec:
  # ghcr.io/ggml-org/llama.cpp:full-cuda13-b12855
  image: ghcr.io/ggml-org/llama.cpp@sha256:07798987d96bcdc082656662aa6603b76498f5816ae0cf68290658bc11d416e3
  args:
    - "--perplexity"
    - "--model"
    - ${{inputs.model}}
    - "--file"
    - ${{inputs.text_file}}
    - "--ctx-size"
    - ${{inputs.ctx_size}}
    - "--log-file"
    - ${{inputs.log_file}}
```

# llama.cpp perplexity

Runs `llama-perplexity` from [llama.cpp](https://github.com/ggml-org/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](https://huggingface.co/datasets/openai/gsm8k) test split (MIT),
which a `duckdb` task writes out as plain text:

```yaml
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](https://docs.swale.io/reference/workflow-definition-syntax)
in the [Swale documentation](https://docs.swale.io).

Licensed under the [MIT License](https://github.com/swaleio/task-definitions/blob/main/LICENSE).
