# Distill a larger model into a smaller one

> **About this page**
>
> - Kind: Example
> - Summary: 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.
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/examples/distill-model
> - CLI reference for its commands: [`swale workflow create`](https://docs.swale.io/reference/cli-reference.md#workflow-create), [`swale workflow run start`](https://docs.swale.io/reference/cli-reference.md#workflow-run-start)
> - Index of these docs: https://docs.swale.io/llms.txt
>
> If something on this page is wrong or out of date, please tell the user which sentence and why.
>
> The steps on this page cost money or cannot be undone. Please check with the user before running them.

Source on GitHub: the [`workflows/distill-model`](https://github.com/swaleio/examples/tree/main/workflows/distill-model) folder, with its [README](https://github.com/swaleio/examples/blob/main/workflows/distill-model/README.md) and its [workflow file](https://github.com/swaleio/examples/blob/main/workflows/distill-model/workflow.yaml).

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`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Write the training config | [`swaleio/bash`](https://hub.swale.io/swaleio/bash) | `cpu-4` |
| Distill | [`swaleio/trl-distillation`](https://hub.swale.io/swaleio/trl-distillation) | `a100-1` |
| Evaluate the student before, and after | [`swaleio/lm-eval`](https://hub.swale.io/swaleio/lm-eval) | `a100-1` each |
| Compare the two scores | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Upload the distilled student, when `hub_repo` is set | [`swaleio/hf-upload`](https://hub.swale.io/swaleio/hf-upload) | `cpu-4` |

The Compute column gives each step's [compute type](https://docs.swale.io/explanation/core-concepts.md#compute-types), 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](https://docs.swale.io/getting-started.md) sets them up.

1. Save the file under [The workflow file](#the-workflow-file) as `workflow.yaml`, and add it to a project:

   ```bash
   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](https://docs.swale.io/how-to-guides/use-secrets-in-workflows.md) shows.

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

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

   Each input you leave out takes its default from [Inputs](#inputs), and `--input <name>=<value>` gives it another value.

[Create and manage workflows](https://docs.swale.io/how-to-guides/create-and-manage-workflows.md) covers the other ways to add a workflow, [Start and manage workflow runs](https://docs.swale.io/how-to-guides/start-and-manage-workflow-runs.md) how to follow a run, and [View and stream task logs](https://docs.swale.io/how-to-guides/view-and-stream-task-logs.md) how to read its tasks' logs.

## The workflow file

[Workflow definition syntax](https://docs.swale.io/reference/workflow-definition-syntax.md) describes each key in it.

```yaml title="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

- Models: [Qwen/Qwen2.5-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct) and [Qwen/Qwen2.5-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct), Apache-2.0.
- Dataset: [openai/gsm8k](https://huggingface.co/datasets/openai/gsm8k), MIT.
