Skip to main content

Distill a larger model into a smaller one

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

Teaches a small model to behave like a larger one of the same family on your prompts. The student answers math prompts and learns to match the teacher's word-by-word distribution over the answer; the student is scored on GSM8K's test split before and after.

StepTask definitionCompute
Turn GSM8K questions into chat promptsswaleio/duckdbcpu-4
Write the training configswaleio/bashcpu-4
Distillswaleio/trl-distillationa100-1
Evaluate the student before, and afterswaleio/lm-evala100-1 each
Compare the two scoresswaleio/duckdbcpu-4
Upload the distilled student, 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 student's first score is taken while it trains.

Inputs​

InputDefaultDescription
studentQwen/Qwen2.5-0.5B-InstructStudent model; all of its weights are trained.
teacherQwen/Qwen2.5-1.5B-InstructTeacher model; it must share the student's vocabulary.
train_prompts4000GSM8K training questions to distill on.
hub_repoemptyHugging Face model repo to upload the student to.

Training, the first evaluation and the upload read the secret hf_token.

Output​

The distilled student in student/ on the workspace, the scores in eval-before/ and eval-after/, and the comparison in the last 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: Distill a larger model into a smaller one
description: Distills a larger teacher into a smaller student of the same family on math prompts with TRL, and compares the student's GSM8K score before and after.
compute_type: cpu-4

inputs:
student:
description: Hugging Face repo id of the student model. All of its weights are trained.
default: Qwen/Qwen2.5-0.5B-Instruct
teacher:
description: Hugging Face repo id of the teacher model; it must share the student's vocabulary.
default: Qwen/Qwen2.5-1.5B-Instruct
train_prompts:
description: Number of GSM8K training questions to distill on.
default: "4000"
hub_repo:
description: Hugging Face model repo to upload the distilled student to, as namespace/name. Leave empty to keep it on the workspace only.
default: ""

entry_point: main

blocks:
main:
tasks:
prepare:
name: Prepare prompts
uses: swaleio/duckdb
args:
sql: |
COPY (
SELECT [{'role': 'user', 'content': question || '\nSolve it step by step.'}] AS prompt
FROM 'hf://datasets/openai/gsm8k/main/train-*.parquet'
LIMIT ${{inputs.train_prompts}}
) TO '${{env.WORKFLOW_STORAGE}}/distill-prompts.parquet' (FORMAT parquet);

write_config:
name: Write training config
uses: swaleio/bash
args:
script: |
cat > "$WORKFLOW_STORAGE/distillation.yaml" <<EOF
model_name_or_path: ${{inputs.student}}
teacher_model_name_or_path: ${{inputs.teacher}}
datasets:
- path: parquet
data_files: $WORKFLOW_STORAGE/distill-prompts.parquet
split: train
learning_rate: 1.0e-5
num_train_epochs: 1
per_device_train_batch_size: 8
gradient_accumulation_steps: 2
max_completion_length: 384
bf16: true
report_to: none
save_strategy: "no"
output_dir: $WORKFLOW_STORAGE/student
EOF

distill:
name: Distill
uses: swaleio/trl-distillation
compute_type: a100-1
start_on:
- prepare
- write_config
args:
config: ${{env.WORKFLOW_STORAGE}}/distillation.yaml
token: ${{secrets.hf_token}}

evaluate_before:
name: Evaluate the student before
uses: swaleio/lm-eval
compute_type: a100-1
args:
model_dir: ${{inputs.student}}
tasks: gsm8k
output_path: ${{env.WORKFLOW_STORAGE}}/eval-before
token: ${{secrets.hf_token}}

evaluate_after:
name: Evaluate the student after
uses: swaleio/lm-eval
compute_type: a100-1
start_on:
- distill
args:
model_dir: ${{env.WORKFLOW_STORAGE}}/student
tasks: gsm8k
output_path: ${{env.WORKFLOW_STORAGE}}/eval-after

compare:
name: Compare
uses: swaleio/duckdb
start_on:
- evaluate_before
- evaluate_after
args:
sql: |
SELECT CASE WHEN filename LIKE '%/eval-before/%' THEN 'before' ELSE 'after' END AS student,
round((results.gsm8k->>'$."exact_match,strict-match"')::DOUBLE, 4) AS strict_match,
round((results.gsm8k->>'$."exact_match,flexible-extract"')::DOUBLE, 4) AS flexible_extract
FROM read_json(['${{env.WORKFLOW_STORAGE}}/eval-before/*/results_*.json',
'${{env.WORKFLOW_STORAGE}}/eval-after/*/results_*.json'],
filename = true, columns = {'results': 'STRUCT(gsm8k JSON)'})
ORDER BY student DESC;

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

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

Licenses of the defaults​