Distilling Large Language Models into Small Models: A Guide to Capability Transfer and Safety Alignment

    This guide explains how to transfer reasoning abilities from large language models into small models for local, edge, and cost-sensitive deployments. It covers data construction, supervised fine-tuning, lightweight reinforcement learning, capability evaluation, and safety behavior checks after fine-tuning.

    1. Background: Why Small Models Need Specialized Reasoning Ability

    In local deployments, edge devices, and cost-sensitive applications, models with around one billion parameters remain the mainstream choice. These models use less memory and respond faster. However, general pretrained weights often perform poorly on multi-step math derivations and science and engineering problems. Developers typically have two options. They can use a larger model and accept higher inference costs, or they can transfer the reasoning ability of a large model into a small one. The second option is known as knowledge distillation.

    This article focuses on the second option. It follows the engineering workflow in order: data construction, supervised fine-tuning, lightweight reinforcement learning, capability evaluation, and safety behavior checks after fine-tuning. The last step is the easiest to overlook, but it directly determines whether a model can be deployed safely.

    2. The Basic Idea of Knowledge Distillation

    Knowledge distillation usually means training a smaller student model to imitate the outputs of a stronger teacher model. In large language model scenarios, the most common form is sequence-level distillation. The teacher model generates complete answers or reasoning processes for a set of tasks. The student model then uses these texts as training targets for fine-tuning.

    Keeping the reasoning process is more valuable than keeping only the final answer. A complete sample usually contains four types of information:

    • Problem understanding: how the model identifies conditions, goals, and constraints.
    • Solution strategy: which method to choose and where to start.
    • Step-by-step derivation: the intermediate conclusions at each step and their justifications.
    • Result verification: whether the final answer is consistent with the problem conditions.

    The student model learns a habit of breaking down problems, not just the answers. For this reason, the quality of distillation data usually matters more than the quantity.

    3. Key Stages of Data Construction

    3.1 Domain Coverage and Balance

    Training data should cover the main subfields of the target task. For math and science and engineering, these subfields might include algebra, geometry, probability, mechanics, and circuits. Each subfield should have a target proportion. If one subfield has too many samples, the model may overfit to it, and performance in other subfields will decline.

    3.2 Correctness Filtering

    Teacher models also make mistakes, so generated samples need filtering. Common methods include:

    1. Comparing results with reference answers and discarding samples whose results do not match.
    2. For problems that can be checked programmatically, running verification scripts to test intermediate results.
    3. Using an independent judge model or manual spot checks to assess whether the reasoning chain is sound.

    3.3 Deduplication and Format Standardization

    Large batches of generated samples often contain near-duplicates. Text similarity or hash fingerprints can be used to remove them. For format, it is recommended to standardize each sample into fixed fields such as question, reasoning process, and final answer, so that training scripts can process them easily. An example is shown below:

    1. {"id": "math_0001", "domain": "algebra", "question": "...", "reasoning": "...", "answer": "...", "verified": true}

    3.4 Data Safety Review

    Training data may contain inappropriate content or sensitive information. Content review and privacy cleaning should be completed before data is added to the training set. This step is often underestimated, but it sets the baseline for the model’s future behavior.

    4. Key Points for Supervised Fine-Tuning

    The goal of supervised fine-tuning (SFT) is to make the student model reproduce the reasoning style and output format of the samples as closely as possible when given an input. The following points matter during implementation.

    4.1 Training Objective

    Loss is usually computed only on the response part. The question serves only as context and does not contribute to gradients. This way, the model does not spend computing resources reproducing the question itself.

    4.2 Hyperparameters and Overfitting

    The learning rate is generally small, and the number of training epochs should be limited. If the validation loss starts to rise, training should stop. Small models have limited capacity. Overtraining causes them to memorize samples instead of learning the method.

    4.3 Training Workflow Example

    1. # Illustrative code: shows the workflow structure only.
    2. # Replace the functions and parameters with those of your actual environment.
    3. config = {
    4. "base_model": "local_checkpoint_path",
    5. "train_file": "distill_data.jsonl",
    6. "learning_rate": 1e-5,
    7. "epochs": 2,
    8. "max_seq_len": 4096,
    9. "loss_on": "response_only",
    10. }
    11. model, tokenizer = load_model(config["base_model"])
    12. train_set = load_jsonl(config["train_file"])
    13. for epoch in range(config["epochs"]):
    14. for batch in make_batches(train_set, tokenizer, config["max_seq_len"]):
    15. loss = compute_loss(model, batch, mask_prompt=True)
    16. loss.backward()
    17. optimizer_step(model)
    18. evaluate_on_dev(model)

    5. The Role of Lightweight Reinforcement Learning

    Supervised fine-tuning teaches the model to imitate. Reinforcement learning further optimizes the model against verifiable goals. For math and science and engineering tasks, answers can usually be judged automatically, which makes rule-based reward signals possible.

    A common approach works as follows:

    • Sample multiple responses for each question.
    • Assign scores based on whether the answer is correct and whether the format meets requirements.
    • Use the relative advantage within each group to update the policy, increasing the probability of generating high-scoring responses.

    The update step size for reinforcement learning should stay conservative. If the reward is poorly designed, the model may learn shortcuts, such as shortening the reasoning process while keeping the correct format. After each training round, the reasoning content should be checked to confirm it has actually improved, not just the score.

    6. Designing Capability Metrics

    Evaluation needs to cover several dimensions, because a single metric can hide problems. At least the following four categories are recommended:

    1. Accuracy: the proportion of correct final answers on a held-out test set.
    2. Format compliance: whether outputs follow the agreed structure so that downstream systems can parse them.
    3. Inference cost: average generation length and response time, which directly affect deployment costs.
    4. Generalization: performance on similar question types that were not included in training.

    The test set must be kept separate from the training data. If data leakage occurs, accuracy will be overestimated, and later decisions may be wrong.

    7. Safety Behavior Checks After Fine-Tuning

    This section is the focus of the article. The model’s capabilities in specialized domains