# llama.cpp convert

> **About this page**
>
> - Task definition version: `swaleio/llamacpp-convert@b12855-r1`
> - Summary: Converts a Hugging Face model directory into a single GGUF file with llama.cpp.
> - Published: 2026-11-30, the latest version
> - Page: https://hub.swale.io/swaleio/llamacpp-convert/v/b12855-r1
> - Latest version's copy: https://hub.swale.io/swaleio/llamacpp-convert.md
> - Versions: https://hub.swale.io/swaleio/llamacpp-convert/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 convert
description: Converts a Hugging Face model directory into a single GGUF file with llama.cpp.
category: machine-learning
inputs:
  model_dir:
    description: Absolute path inside the container of the Hugging Face format model directory to convert. Point it at the mounted workspace (e.g. ${{env.WORKFLOW_STORAGE}}/model-hf) where an earlier task placed the model, or any other container-local path.
    required: true
  outfile:
    description: Absolute path inside the container for the converted .gguf file. Point it at the mounted workspace (e.g. ${{env.WORKFLOW_STORAGE}}/model-f16.gguf) to share it with later tasks, or any other container-local path.
    required: true
  outtype:
    description: "GGUF tensor type to write: f16, bf16, or q8_0. Defaults to auto, which writes bf16 for a bf16 model and f16 otherwise, keeps the FP8 weights of an FP8 checkpoint scaled per tensor or per output channel, and fails on one with block scales: set bf16 for that."
    default: "auto"
exec:
  # ghcr.io/ggml-org/llama.cpp:full-b12855
  image: ghcr.io/ggml-org/llama.cpp@sha256:e39fcf42662a2b8238df7dc57a7ef9d9ac97dd5f341c9b081255ea169d469a0e
  args:
    - "--convert"
    - ${{inputs.model_dir}}
    - "--outfile"
    - ${{inputs.outfile}}
    - "--outtype"
    - ${{inputs.outtype}}
```

# llama.cpp convert

Converts a Hugging Face format model directory (`config.json`, tokenizer files,
`*.safetensors`) into a single GGUF file that llama.cpp-based runtimes can load.
The image's ENTRYPOINT is llama.cpp's `/app/tools.sh` dispatcher, which runs
**exactly one mode per invocation**: the leading `--convert` argument execs
`convert_hf_to_gguf.py` with the remaining arguments. That one-mode-per-invocation
dispatch is why converting and quantizing are two separate tasks —
`llamacpp-convert` and `llamacpp-quantize` — 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 `outfile`. Pass
the same path to the next task.

## Inputs

| Input | Required | Default | Description |
|-------|----------|---------|-------------|
| `model_dir` | yes | — | Absolute path of the Hugging Face format model directory to convert. Point it at the mounted workspace (e.g. `${{env.WORKFLOW_STORAGE}}/model-hf`) where an earlier task placed the model, or any other container-local path. |
| `outfile` | yes | — | Absolute path for the converted `.gguf` file. Point it at the mounted workspace (e.g. `${{env.WORKFLOW_STORAGE}}/model-f16.gguf`) to share it with later tasks, or any other container-local path. |
| `outtype` | no | `auto` | GGUF tensor type: `f16`, `bf16`, or `q8_0`. The default `auto` writes `bf16` for a bf16 model and `f16` otherwise, keeps the FP8 weights of an FP8 checkpoint scaled per tensor or per output channel, and fails on one with block scales: set `bf16` for that. |

`model_dir` and `outfile` are required — they are the paths you choose and the
next task reads. `outtype` is optional and defaults to `auto`, which is what
`convert_hf_to_gguf.py` itself does when the flag is omitted: it reads the
checkpoint's own tensor dtype and writes `bf16` for a bf16 model, `f16`
otherwise. Pass it explicitly to override that heuristic — `f16` for a fixed
full-precision baseline whatever the source is, `bf16` to force bfloat16, or
`q8_0` to get an 8-bit file directly from conversion.

FP8 checkpoints depend on how their weights are scaled:

- **One scale per tensor or per output channel:** `auto` keeps the weights in
  FP8 (`F8_E4M3`). A checkpoint scaled per tensor converts only with `auto`.
  Older llama.cpp builds cannot load the result, or the files quantized from
  it, correctly.
- **Block scales**, as in Qwen's `-FP8` models: `auto` fails with an
  "unsupported block/group scale" error. Set `outtype` to `bf16`, which writes
  the weights as bfloat16. The `--fp8-as-q8` the error suggests is not one of
  this task's inputs.

## Models saved by Transformers 5

The converter runs Transformers 4.57.6, the version llama.cpp's own converter
requires. A model saved by Transformers 5, such as one fine-tuned with
`trl-sft` or saved by the `transformers` task, lists `extra_special_tokens` in
`tokenizer_config.json` in a form Transformers 4 cannot read, and conversion
fails with `AttributeError: 'list' object has no attribute 'keys'`.

Delete that key before converting. The tokens it lists are also in
`tokenizer.json`, so the converted model keeps them. A `python-3-13` step
between the task that saved the model and `convert` does it:

```yaml
name: llama.cpp convert - Transformers 5 model
compute_type: cpu-4
entry_point: main

blocks:
  main:
    tasks:
      train:
        name: Fine-tune
        uses: swaleio/transformers
        compute_type: a100-1   # any GPU compute type whose VRAM fits the model
        args:
          script: |
            # Fine-tune a model and save it, with its tokenizer, to
            # $WORKFLOW_STORAGE/model-hf using save_pretrained.
            ...
      fix_tokenizer:
        name: Fix tokenizer config
        uses: swaleio/python-3-13
        start_on:
          - train
        args:
          script: |
            import json, os
            path = os.path.join(os.environ["WORKFLOW_STORAGE"], "model-hf", "tokenizer_config.json")
            with open(path) as f:
                config = json.load(f)
            if isinstance(config.get("extra_special_tokens"), list):
                del config["extra_special_tokens"]
                with open(path, "w") as f:
                    json.dump(config, f, indent=2, ensure_ascii=False)
      convert:
        name: Convert to GGUF
        uses: swaleio/llamacpp-convert
        start_on:
          - fix_tokenizer
        args:
          model_dir: ${{env.WORKFLOW_STORAGE}}/model-hf
          outfile: ${{env.WORKFLOW_STORAGE}}/model-16bit.gguf
```

A model downloaded from the Hub that was saved by Transformers 4 converts
without this step.

## Compute

**CPU.** Conversion is CPU- and RAM-bound — no CUDA is involved, so a GPU
compute type adds nothing. Budget disk rather than VRAM: the workspace must
hold the source directory and the converted file side by side (a 16-bit GGUF
is roughly 2 bytes per parameter).

## Example

Download a model from the Hugging Face Hub, convert it to a 16-bit GGUF, then
quantize it with `llamacpp-quantize`. The `convert` step leaves `outtype` unset,
so the converted file keeps the checkpoint's own dtype. 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 convert 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).
