Skip to main content

Quantize a model and pick the best preset

Source on GitHub: the workflows/quantize-and-pick folder, with its README and its workflow file.

Finds the smallest quantized version of a model that still holds up. The model is converted to a 16-bit GGUF file once; then each quantization preset is made, measured for perplexity on the same text and benchmarked for speed in its own tasks, all presets at the same time. A last step compares every preset's size and perplexity with the 16-bit file's and keeps the smallest one whose perplexity rose by no more than max_increase percent.

StepTask definitionCompute
Fetch the modelswaleio/hf-downloadcpu-4
Write the measuring textswaleio/duckdbcpu-4
Make the output foldersswaleio/bashcpu-4
Convert to a 16-bit GGUF fileswaleio/llamacpp-convertcpu-4
Measure the 16-bit file's perplexityswaleio/llamacpp-perplexitya100-1
For each preset: quantizeswaleio/llamacpp-quantizecpu-4
For each preset: measure perplexity, and benchmark speedswaleio/llamacpp-perplexity, swaleio/llamacpp-bencha100-1 each
Compare sizes and perplexities, pick the presetswaleio/duckdbcpu-4
Stage the chosen fileswaleio/bashcpu-4
Upload it, when hub_repo is setswaleio/hf-uploadcpu-4

The Compute column gives each step's compute type, the compute its tasks are allocated.

Every step is its own task. The pick reads perplexity from the log file llamacpp-perplexity writes. llama-bench prints its speeds only to its task log, so they are there to read but do not take part in the pick.

Inputs​

InputDefaultDescription
modelQwen/Qwen2.5-1.5B-InstructModel to quantize.
presetsQ3_K_M, Q4_K_M, Q5_K_M, Q6_K, Q8_0llama.cpp presets to try, as a JSON array.
max_increase5Largest perplexity increase over the 16-bit file the chosen preset may have, in percent.
hub_repoemptyHugging Face model repo to upload the chosen file to.

The download and the upload read the secret hf_token.

Output​

Every GGUF file in gguf/ on the workspace, the perplexity logs in logs/, and the chosen file in release/. The comparison table is in the pick task's log.

Run it​

You need the CLI, logged in to your account, and a project to add the workflow to; Getting started sets them up.

  1. Save the file under The workflow file as workflow.yaml, and add it to a project:

    swale workflow create --project-name my-project --file workflow.yaml

    The command prints Workflow created: followed by the workflow's ID.

  2. The workflow reads the secret hf_token. Create it as a project secret, or as an account secret that allows the project, as Use secrets in workflows shows.

  3. Start a run with the workflow's ID:

    swale workflow run start --project-name my-project --workflow-id <workflow-id>

    Each input you leave out takes its default from Inputs, and --input <name>=<value> gives it another value.

Create and manage workflows covers the other ways to add a workflow, Start and manage workflow runs how to follow a run, and View and stream task logs how to read its tasks' logs.

The workflow file​

Workflow definition syntax describes each key in it.

workflow.yaml
name: Quantize a model and pick the best preset
description: Converts a model to GGUF, quantizes it to several presets in parallel, measures their perplexity and speed, and uploads the smallest within a perplexity budget.
compute_type: cpu-4

inputs:
model:
description: Hugging Face repo id of the model to quantize.
default: Qwen/Qwen2.5-1.5B-Instruct
presets:
description: llama.cpp quantization presets to try, as a JSON array.
default: '["Q3_K_M", "Q4_K_M", "Q5_K_M", "Q6_K", "Q8_0"]'
max_increase:
description: Largest perplexity increase over the 16-bit file that the chosen preset may have, in percent.
default: "5"
hub_repo:
description: Hugging Face model repo to upload the chosen file to, as namespace/name. Leave empty to keep it on the workspace only.
default: ""

entry_point: main

blocks:
main:
tasks:
fetch:
name: Fetch the model
uses: swaleio/hf-download
args:
repo: ${{inputs.model}}
dest: ${{env.WORKFLOW_STORAGE}}/hf
token: ${{secrets.hf_token}}

text:
name: Write the measuring text
uses: swaleio/duckdb
args:
sql: |
COPY (SELECT question || ' ' || answer
FROM 'hf://datasets/openai/gsm8k/main/test-*.parquet' LIMIT 300)
TO '${{env.WORKFLOW_STORAGE}}/measure.txt' (FORMAT csv, HEADER false, QUOTE '', DELIMITER '\t');

make_dirs:
name: Make output folders
uses: swaleio/bash
args:
script: mkdir -p "$WORKFLOW_STORAGE/gguf" "$WORKFLOW_STORAGE/logs" "$WORKFLOW_STORAGE/release"

convert:
name: Convert to a 16-bit GGUF file
uses: swaleio/llamacpp-convert
start_on:
- fetch
- make_dirs
args:
model_dir: ${{env.WORKFLOW_STORAGE}}/hf
outfile: ${{env.WORKFLOW_STORAGE}}/gguf/model-16bit.gguf

measure_16bit:
name: Measure the 16-bit file
uses: swaleio/llamacpp-perplexity
compute_type: a100-1
start_on:
- convert
- text
args:
model: ${{env.WORKFLOW_STORAGE}}/gguf/model-16bit.gguf
text_file: ${{env.WORKFLOW_STORAGE}}/measure.txt
log_file: ${{env.WORKFLOW_STORAGE}}/logs/16bit.log

each_preset:
name: Quantize and measure each preset
start_on:
- convert
- text
for_each:
items: ${{inputs.presets}}
block: one_preset

pick:
name: Pick the preset
uses: swaleio/duckdb
start_on:
- measure_16bit
- each_preset
args:
sql: |
-- llama-perplexity ends its log with "Final estimate: PPL = <number> +/- <error>".
CREATE TABLE measured AS
SELECT regexp_extract(filename, '/([^/]+)\.log$', 1) AS preset,
regexp_extract(content, 'Final estimate: PPL = ([0-9.]+)', 1)::DOUBLE AS perplexity
FROM read_text('${{env.WORKFLOW_STORAGE}}/logs/*.log');
CREATE TABLE sizes AS
SELECT regexp_extract(filename, 'model-([^/]+)\.gguf$', 1) AS preset, round(size / 1e6) AS megabytes
FROM read_blob('${{env.WORKFLOW_STORAGE}}/gguf/*.gguf');
CREATE TABLE compared AS
SELECT m.preset, s.megabytes, m.perplexity,
round(100 * (m.perplexity / (SELECT perplexity FROM measured WHERE preset = '16bit') - 1), 2) AS increase_percent
FROM measured m JOIN sizes s USING (preset)
ORDER BY s.megabytes;
SELECT * FROM compared;
CREATE TABLE chosen AS
SELECT preset FROM compared
WHERE preset <> '16bit' AND increase_percent <= ${{inputs.max_increase}}
ORDER BY megabytes LIMIT 1;
SELECT preset AS chosen FROM chosen;
COPY (SELECT 'model-' || preset || '.gguf' FROM chosen)
TO '${{env.WORKFLOW_STORAGE}}/chosen.txt' (FORMAT csv, HEADER false, QUOTE '');

stage:
name: Stage the chosen file
uses: swaleio/bash
start_on:
- pick
args:
script: |
set -euo pipefail
chosen=$(cat "$WORKFLOW_STORAGE/chosen.txt")
[ -n "$chosen" ] || { echo "no preset is within ${{inputs.max_increase}}% of the 16-bit perplexity" >&2; exit 1; }
cp "$WORKFLOW_STORAGE/gguf/$chosen" "$WORKFLOW_STORAGE/release/"
echo "chosen: $chosen"

publish:
name: Upload when asked
start_on:
- stage
if:
condition: ${{inputs.hub_repo}}
true: upload

one_preset:
tasks:
quantize:
name: Quantize
uses: swaleio/llamacpp-quantize
args:
input_gguf: ${{env.WORKFLOW_STORAGE}}/gguf/model-16bit.gguf
output_gguf: ${{env.WORKFLOW_STORAGE}}/gguf/model-${{tasks.each_preset.for_each.item}}.gguf
preset: ${{tasks.each_preset.for_each.item}}
measure:
name: Measure perplexity
uses: swaleio/llamacpp-perplexity
compute_type: a100-1
start_on:
- quantize
args:
model: ${{env.WORKFLOW_STORAGE}}/gguf/model-${{tasks.each_preset.for_each.item}}.gguf
text_file: ${{env.WORKFLOW_STORAGE}}/measure.txt
log_file: ${{env.WORKFLOW_STORAGE}}/logs/${{tasks.each_preset.for_each.item}}.log
bench:
name: Benchmark speed
uses: swaleio/llamacpp-bench
compute_type: a100-1
start_on:
- quantize
args:
model: ${{env.WORKFLOW_STORAGE}}/gguf/model-${{tasks.each_preset.for_each.item}}.gguf

upload:
tasks:
upload_gguf:
name: Upload to the Hugging Face Hub
uses: swaleio/hf-upload
args:
repo: ${{inputs.hub_repo}}
source: ${{env.WORKFLOW_STORAGE}}/release
token: ${{secrets.hf_token}}

Licenses of the defaults​