Skip to main content

Fine-tune a model then serve it with vLLM

Source on GitHub: the workflows/train-then-serve folder, with its README and its workflow file.

Fine-tunes a small chat model and serves it right away through vLLM's OpenAI-compatible server, inside the same run. A smoke test waits for the server, asks one question, and saves the answer; when it is done the platform stops the server. Add your own consumers of the server, and list them in the server's terminate_on.

StepTask definitionCompute
Write the training configswaleio/bashcpu-4
Fine-tuneswaleio/trl-sfta100-1
Write the smoke-test requestswaleio/duckdbcpu-4
Serve the fine-tuned model, as a runnerswaleio/vllm-servea100-1
Wait for the server, checking every 5 secondsswaleio/http-requestcpu-4
Ask the questionswaleio/http-requestcpu-4

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

Every step is its own task.

Inputs​

InputDefaultDescription
modelQwen/Qwen2.5-0.5B-InstructModel to fine-tune.
datasetHuggingFaceH4/ultrafeedback_binarizedHugging Face dataset with a messages column.
splittrain_sft[:5000]Split, or slice of one, to train on.
questiona question about fine-tuningWhat the smoke test asks.

Training reads the secret hf_token.

Output​

The fine-tuned model in model/ on the workspace, and the server's answer in answer.json.

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: Fine-tune a model then serve it with vLLM
description: Fine-tunes a small chat model with TRL, serves the result through vLLM's OpenAI-compatible server, and checks it answers.
compute_type: cpu-4

inputs:
model:
description: Hugging Face repo id of the model to fine-tune.
default: Qwen/Qwen2.5-0.5B-Instruct
dataset:
description: Hugging Face dataset with a messages column of chat turns.
default: HuggingFaceH4/ultrafeedback_binarized
split:
description: Split of the dataset to train on.
default: train_sft[:5000]
question:
description: The question the smoke test asks the served model.
default: In one sentence, what is supervised fine-tuning?

entry_point: main

blocks:
main:
tasks:
write_config:
name: Write training config
uses: swaleio/bash
args:
script: |
cat > "$WORKFLOW_STORAGE/sft.yaml" <<EOF
model_name_or_path: ${{inputs.model}}
datasets:
- path: ${{inputs.dataset}}
split: ${{inputs.split}}
columns: [messages]
learning_rate: 1.0e-5
num_train_epochs: 1
per_device_train_batch_size: 8
gradient_accumulation_steps: 2
max_length: 1024
bf16: true
report_to: none
save_strategy: "no"
output_dir: $WORKFLOW_STORAGE/model
EOF

train:
name: Fine-tune
uses: swaleio/trl-sft
compute_type: a100-1
start_on:
- write_config
args:
config: ${{env.WORKFLOW_STORAGE}}/sft.yaml
token: ${{secrets.hf_token}}

write_request:
name: Write the smoke-test request
uses: swaleio/duckdb
args:
sql: |
COPY (SELECT '${{env.WORKFLOW_STORAGE}}/model' AS model,
[{'role': 'user', 'content': $$${{inputs.question}}$$}] AS messages,
128 AS max_tokens)
TO '${{env.WORKFLOW_STORAGE}}/request.json' (FORMAT json);

serve:
name: Serve the fine-tuned model
uses: swaleio/vllm-serve
compute_type: a100-1
start_on:
- train
terminate_on:
- ask
args:
model: ${{env.WORKFLOW_STORAGE}}/model

wait_ready:
name: Wait for the server
uses: swaleio/http-request
start_on:
- serve
args:
url: http://${{tasks.serve.ip-address}}:8000/health
retry: "360"
retry_delay: "5"
expected_status: "200"

ask:
name: Ask a question
uses: swaleio/http-request
start_on:
- wait_ready
- write_request
args:
url: http://${{tasks.serve.ip-address}}:8000/v1/chat/completions
method: POST
headers: |
Content-Type: application/json
body_file: ${{env.WORKFLOW_STORAGE}}/request.json
expected_status: "200"
response_path: ${{env.WORKFLOW_STORAGE}}/answer.json

Licenses of the defaults​