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.
| Step | Task definition | Compute |
|---|---|---|
| Turn GSM8K questions into chat prompts | swaleio/duckdb | cpu-4 |
| Write the training config | swaleio/bash | cpu-4 |
| Distill | swaleio/trl-distillation | a100-1 |
| Evaluate the student before, and after | swaleio/lm-eval | a100-1 each |
| Compare the two scores | swaleio/duckdb | cpu-4 |
Upload the distilled student, when hub_repo is set | swaleio/hf-upload | cpu-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
| Input | Default | Description |
|---|---|---|
student | Qwen/Qwen2.5-0.5B-Instruct | Student model; all of its weights are trained. |
teacher | Qwen/Qwen2.5-1.5B-Instruct | Teacher model; it must share the student's vocabulary. |
train_prompts | 4000 | GSM8K training questions to distill on. |
hub_repo | empty | Hugging 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.
-
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.yamlThe command prints
Workflow created:followed by the workflow's ID. -
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. -
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.
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
- Models: Qwen/Qwen2.5-0.5B-Instruct and Qwen/Qwen2.5-1.5B-Instruct, Apache-2.0.
- Dataset: openai/gsm8k, MIT.