# llama.cpp quantize

> **About this page**
>
> - Task definition: `swaleio/llamacpp-quantize`
> - Summary: Quantizes a GGUF model file to a smaller preset (e.g. Q4_K_M) with llama.cpp's llama-quantize.
> - Category: Machine learning & AI
> - Latest version: b12855, published 2026-11-30
> - Page: https://hub.swale.io/swaleio/llamacpp-quantize
> - Versions: https://hub.swale.io/swaleio/llamacpp-quantize/versions, where each version's page has a Markdown copy at its address with `.md` added
> - 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 quantize
description: Quantizes a GGUF model file to a smaller preset (e.g. Q4_K_M) with llama.cpp's llama-quantize.
category: machine-learning
inputs:
  input_gguf:
    description: Absolute path inside the container of the source .gguf file, typically the outfile of a llamacpp-convert task. Point it at the mounted workspace (e.g. ${{env.WORKFLOW_STORAGE}}/model-f16.gguf), or any other container-local path.
    required: true
  output_gguf:
    description: Absolute path inside the container for the quantized .gguf file. Point it at the mounted workspace (e.g. ${{env.WORKFLOW_STORAGE}}/model-q4_k_m.gguf) to share it with later tasks, or any other container-local path.
    required: true
  preset:
    description: Quantization preset understood by llama-quantize, e.g. Q4_K_M, Q5_K_M, Q8_0.
    required: true
exec:
  # ghcr.io/ggml-org/llama.cpp:full-b12855
  image: ghcr.io/ggml-org/llama.cpp@sha256:e39fcf42662a2b8238df7dc57a7ef9d9ac97dd5f341c9b081255ea169d469a0e
  args:
    - "--quantize"
    - ${{inputs.input_gguf}}
    - ${{inputs.output_gguf}}
    - ${{inputs.preset}}
```

# llama.cpp quantize

Quantizes an existing GGUF file down to a smaller preset using llama.cpp's
`llama-quantize`, trading a little quality for a much smaller and faster model.
The image's ENTRYPOINT is llama.cpp's `/app/tools.sh` dispatcher, which runs
**exactly one mode per invocation**: the leading `--quantize` argument execs
`llama-quantize` with the remaining arguments. That one-mode-per-invocation
dispatch is why quantizing and converting are two separate tasks —
`llamacpp-quantize` and `llamacpp-convert` — rather than one; chain them, as in
the example below.

This task declares no outputs: it runs the vendor image unmodified, so there is
no wrapper to append to `$WORKFLOW_TASK_OUTPUT` — and none is needed, because
the consumer already knows where the result lands: it supplied `output_gguf`.
Pass the same path to the next task.

## Inputs

| Input | Required | Default | Description |
|-------|----------|---------|-------------|
| `input_gguf` | yes | — | Absolute path of the source `.gguf` file — typically the `outfile` of a `llamacpp-convert` task (e.g. `${{env.WORKFLOW_STORAGE}}/model-f16.gguf`). |
| `output_gguf` | yes | — | Absolute path for the quantized `.gguf` file. Point it at the mounted workspace (e.g. `${{env.WORKFLOW_STORAGE}}/model-q4_k_m.gguf`) to share it with later tasks, or any other container-local path. |
| `preset` | yes | — | Quantization preset, e.g. `Q4_K_M`, `Q5_K_M`, `Q8_0`. |

All three inputs are required. `input_gguf` and `output_gguf` are the paths you
choose; `preset` has no default because `llama-quantize` has none either — it
refuses to guess a quantization type, and neither does this task. The preset is
the decision the task exists to make: every value is a different
quality-per-byte trade, none of them a neutral setting. `Q4_K_M` is the usual
choice, `Q5_K_M` trades size for quality, and `Q8_0` is near-lossless but
largest.

## Compute

**CPU.** `llama-quantize` is a multi-threaded CPU program — more cores make it
faster, and a GPU adds nothing. Budget disk rather than VRAM: the workspace
must hold the source and quantized files side by side (a `Q4_K_M` file is
roughly a third the size of its 16-bit source).

## Example

Download a model from the Hugging Face Hub, convert it to a 16-bit GGUF with
`llamacpp-convert`, then quantize it to `Q4_K_M`. Each task lists its
predecessor in `start_on` so the steps run in order, and the files are
exchanged through the shared workspace paths the consumer chose.

```yaml
name: llama.cpp quantize example
compute_type: cpu-4
entry_point: main

blocks:
  main:
    tasks:
      weights:
        name: Fetch weights
        uses: swaleio/hf-download
        args:
          repo: Qwen/Qwen2.5-1.5B-Instruct
          include: "*.safetensors,*.json,tokenizer.*"
          dest: ${{env.WORKFLOW_STORAGE}}/qwen-hf
      convert:
        name: Convert to GGUF
        uses: swaleio/llamacpp-convert
        start_on:
          - weights
        args:
          model_dir: ${{env.WORKFLOW_STORAGE}}/qwen-hf
          outfile: ${{env.WORKFLOW_STORAGE}}/qwen-16bit.gguf
      quantize:
        name: Quantize
        uses: swaleio/llamacpp-quantize
        start_on:
          - convert
        args:
          input_gguf: ${{env.WORKFLOW_STORAGE}}/qwen-16bit.gguf
          output_gguf: ${{env.WORKFLOW_STORAGE}}/qwen-q4_k_m.gguf
          preset: Q4_K_M
```

Downstream tasks read the quantized model at `${{env.WORKFLOW_STORAGE}}/qwen-q4_k_m.gguf`
— the path the workflow supplied, so no output lookup is needed.

---

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).
