commit dc732117406ac44aa6d152cd81c22fdb0c18dd7f Author: ModelHub XC Date: Fri Aug 28 20:54:19 2026 +0800 初始化项目,由ModelHub XC社区提供模型 Model: Harsha901/qwen2.5-coder-3b-distilled-from-14b-merged Source: Original Platform diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..52373fe --- /dev/null +++ b/.gitattributes @@ -0,0 +1,36 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text +tokenizer.json filter=lfs diff=lfs merge=lfs -text diff --git a/README.md b/README.md new file mode 100644 index 0000000..2b1317b --- /dev/null +++ b/README.md @@ -0,0 +1,167 @@ +--- +language: +- en +license: apache-2.0 +base_model: Qwen/Qwen2.5-Coder-3B-Instruct +tags: +- qwen2.5 +- code +- knowledge-distillation +- gkd +- qlora +- python +- causal-lm +datasets: +- iamtarun/python_code_instructions_18k_alpaca +pipeline_tag: text-generation +--- + +# Qwen2.5-Coder-3B — GKD Distilled from 14B + +A **merged** (LoRA-free) version of `Qwen2.5-Coder-3B-Instruct` whose weights have been updated via **Generalized Knowledge Distillation (GKD)** from `Qwen2.5-Coder-14B-Instruct` as the teacher. + +The LoRA adapter was trained with TRL's `DistillationTrainer` on a single NVIDIA A100 80 GB (Google Colab) for ~5 hours, then merged back into the base weights so the model loads exactly like the original 3B — no adapter plumbing required. + +--- + +## Evaluation — HumanEval pass@1 + +Greedy decoding, 164 Python programming tasks. + +| Model | Passed | Total | pass@1 | +|---|---:|---:|---:| +| Base `Qwen2.5-Coder-3B-Instruct` | 133 | 164 | 81.1% | +| **This model (distilled, 300 steps)** | **137** | **164** | **83.5%** | +| Δ | +4 | — | **+2.44 pp** | + +**Task-level breakdown:** + +| Outcome | Count | +|---|---:| +| Both pass | 126 | +| Only distilled passes (gained) | 11 | +| Only base passes (lost) | 7 | +| Neither passes | 20 | +| **Net change** | **+4** | + +**Gained tasks (11):** HumanEval/26, /64, /75, /89, /93, /95, /110, /123, /124, /125, /135 — reasoning-heavy string manipulation, cipher encoding, and date parsing problems. + +**Lost tasks (7):** HumanEval/10, /46, /99, /103, /141, /154, /159 — precise numeric/sequence edge cases (bankers rounding, fib4 base cases, cyclic rotation). + +> Note: HumanEval pass@1 with greedy decoding has ~±2pp noise. The +2.44pp delta is at the edge of statistical significance for a 300-step run; longer training is expected to widen the gap. + +--- + +## Training Details + +### Setup + +| Component | Detail | +|---|---| +| **Teacher** | `Qwen/Qwen2.5-Coder-14B-Instruct` — frozen, loaded in 4-bit NF4 | +| **Student** | `Qwen/Qwen2.5-Coder-3B-Instruct` + LoRA (r=16, α=32) | +| **Distillation** | TRL `DistillationTrainer`, GKD with λ=0.25, β=0.5 | +| **Dataset** | `iamtarun/python_code_instructions_18k_alpaca` — 17,681 train / 931 eval | +| **Hardware** | Google Colab A100 80 GB | +| **Wall time** | ~5 hours | + +### GKD Loss + +``` +L = λ · L_on_policy + (1−λ) · L_teacher_forced + where L = β · KL(student ∥ teacher) + (1−β) · KL(teacher ∥ student) +``` + +- **λ=0.25** — 25% on-policy (student-generated) sequences, 75% teacher-forced; keeps training stable at low step counts while reducing exposure bias. +- **β=0.5** — symmetric Jensen–Shannon divergence between teacher and student logits. + +### Hyperparameters + +| Hyperparameter | Value | +|---|---| +| LoRA rank (r) | 16 | +| LoRA alpha | 32 | +| LoRA dropout | 0.05 | +| Target modules | q/k/v/o_proj, gate/up/down_proj | +| Optimizer | paged_adamw_8bit | +| Learning rate | 1.5e-4 | +| LR schedule | Cosine with 30-step warmup | +| Max steps | 300 | +| Per-device batch size | 16 | +| Gradient accumulation | 2 (effective batch = 32) | +| Max sequence length | 1024 | +| Precision | bf16 | +| Quantization (both models) | 4-bit NF4, double quant, bf16 compute | + +--- + +## Usage + +This is a standard causal-LM checkpoint — load it exactly like the base model. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer +import torch + +model_id = "Harsha901/qwen2.5-coder-3b-distilled-from-14b-merged" + +tokenizer = AutoTokenizer.from_pretrained(model_id) +model = AutoModelForCausalLM.from_pretrained( + model_id, + torch_dtype=torch.bfloat16, + device_map="auto", +) + +messages = [ + {"role": "user", "content": "Write a Python function that checks if a number is prime."} +] + +text = tokenizer.apply_chat_template( + messages, + tokenize=False, + add_generation_prompt=True, +) +inputs = tokenizer([text], return_tensors="pt").to(model.device) + +with torch.no_grad(): + output = model.generate( + **inputs, + max_new_tokens=512, + do_sample=False, + ) + +print(tokenizer.decode(output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)) +``` + +### Memory requirements + +| Precision | Approx. VRAM | +|---|---| +| bf16 (this checkpoint) | ~6.5 GB | +| 4-bit NF4 (bitsandbytes) | ~2.5 GB | + +The model runs comfortably on a 24 GB consumer GPU (RTX 3090/4090) in full bf16 precision. + +--- + +## Limitations and Future Work + +- **Only 300 steps trained** — the model has seen ~9,600 examples (~0.54 epochs of the training set). Longer training (1,000–2,000 steps or 3 full epochs) is expected to improve further. +- **Regressions on arithmetic edge cases** — the 7 lost tasks suggest slight distribution shift away from precise numeric corner cases; increasing LoRA rank or adding targeted examples may help. +- **Training dataset is Python-only** — generalisation to other languages is untested. +- **Distillation dataset differs from eval benchmark** — HumanEval is held-out; the training set is `python_code_instructions_18k_alpaca`, which covers general Python instruction-following rather than competitive algorithmic problems. + +Planned next steps: +1. Train for 1,000+ steps and track HumanEval every 50 steps. +2. Tune λ toward 0.5–0.75 for more on-policy exposure. +3. Benchmark inference latency and VRAM vs. the 14B teacher to quantify serving cost reduction. + +--- + +## Related Resources + +- **LoRA adapter (pre-merge):** [Harsha901/qwen2.5-coder-3b-distilled-from-14b](https://huggingface.co/Harsha901/qwen2.5-coder-3b-distilled-from-14b) +- **Base student model:** [Qwen/Qwen2.5-Coder-3B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-3B-Instruct) +- **Teacher model:** [Qwen/Qwen2.5-Coder-14B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-14B-Instruct) +- **Training dataset:** [iamtarun/python_code_instructions_18k_alpaca](https://huggingface.co/datasets/iamtarun/python_code_instructions_18k_alpaca) +- **TRL DistillationTrainer docs:** [huggingface.co/docs/trl](https://huggingface.co/docs/trl) diff --git a/chat_template.jinja b/chat_template.jinja new file mode 100644 index 0000000..bdf7919 --- /dev/null +++ b/chat_template.jinja @@ -0,0 +1,54 @@ +{%- if tools %} + {{- '<|im_start|>system\n' }} + {%- if messages[0]['role'] == 'system' %} + {{- messages[0]['content'] }} + {%- else %} + {{- 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.' }} + {%- endif %} + {{- "\n\n# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within XML tags:\n" }} + {%- for tool in tools %} + {{- "\n" }} + {{- tool | tojson }} + {%- endfor %} + {{- "\n\n\nFor each function call, return a json object with function name and arguments within XML tags:\n\n{\"name\": , \"arguments\": }\n<|im_end|>\n" }} +{%- else %} + {%- if messages[0]['role'] == 'system' %} + {{- '<|im_start|>system\n' + messages[0]['content'] + '<|im_end|>\n' }} + {%- else %} + {{- '<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n' }} + {%- endif %} +{%- endif %} +{%- for message in messages %} + {%- if (message.role == "user") or (message.role == "system" and not loop.first) or (message.role == "assistant" and not message.tool_calls) %} + {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }} + {%- elif message.role == "assistant" %} + {{- '<|im_start|>' + message.role }} + {%- if message.content %} + {{- '\n' + message.content }} + {%- endif %} + {%- for tool_call in message.tool_calls %} + {%- if tool_call.function is defined %} + {%- set tool_call = tool_call.function %} + {%- endif %} + {{- '\n\n{"name": "' }} + {{- tool_call.name }} + {{- '", "arguments": ' }} + {{- tool_call.arguments | tojson }} + {{- '}\n' }} + {%- endfor %} + {{- '<|im_end|>\n' }} + {%- elif message.role == "tool" %} + {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") %} + {{- '<|im_start|>user' }} + {%- endif %} + {{- '\n\n' }} + {{- message.content }} + {{- '\n' }} + {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %} + {{- '<|im_end|>\n' }} + {%- endif %} + {%- endif %} +{%- endfor %} +{%- if add_generation_prompt %} + {{- '<|im_start|>assistant\n' }} +{%- endif %} diff --git a/config.json b/config.json new file mode 100644 index 0000000..e3b74ee --- /dev/null +++ b/config.json @@ -0,0 +1,69 @@ +{ + "architectures": [ + "Qwen2ForCausalLM" + ], + "attention_dropout": 0.0, + "bos_token_id": 151643, + "dtype": "bfloat16", + "eos_token_id": 151645, + "hidden_act": "silu", + "hidden_size": 2048, + "initializer_range": 0.02, + "intermediate_size": 11008, + "layer_types": [ + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention", + "full_attention" + ], + "max_position_embeddings": 32768, + "max_window_layers": 36, + "model_type": "qwen2", + "num_attention_heads": 16, + "num_hidden_layers": 36, + "num_key_value_heads": 2, + "pad_token_id": null, + "rms_norm_eps": 1e-06, + "rope_parameters": { + "rope_theta": 1000000.0, + "rope_type": "default" + }, + "sliding_window": null, + "tie_word_embeddings": true, + "transformers_version": "5.0.0", + "use_cache": true, + "use_sliding_window": false, + "vocab_size": 151936 +} diff --git a/generation_config.json b/generation_config.json new file mode 100644 index 0000000..a3498e8 --- /dev/null +++ b/generation_config.json @@ -0,0 +1,14 @@ +{ + "bos_token_id": 151643, + "do_sample": true, + "eos_token_id": [ + 151645, + 151643 + ], + "pad_token_id": 151643, + "repetition_penalty": 1.05, + "temperature": 0.7, + "top_k": 20, + "top_p": 0.8, + "transformers_version": "5.0.0" +} diff --git a/model.safetensors b/model.safetensors new file mode 100644 index 0000000..a586e43 --- /dev/null +++ b/model.safetensors @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cd941c776f4840e8ea421e58cf1e088f495a35d4fce826857f109eea14343f5d +size 6171927112 diff --git a/tokenizer.json b/tokenizer.json new file mode 100644 index 0000000..34510ff --- /dev/null +++ b/tokenizer.json @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3fd169731d2cbde95e10bf356d66d5997fd885dd8dbb6fb4684da3f23b2585d8 +size 11421892 diff --git a/tokenizer_config.json b/tokenizer_config.json new file mode 100644 index 0000000..e4fb8c3 --- /dev/null +++ b/tokenizer_config.json @@ -0,0 +1,29 @@ +{ + "add_prefix_space": false, + "backend": "tokenizers", + "bos_token": null, + "clean_up_tokenization_spaces": false, + "eos_token": "<|im_end|>", + "errors": "replace", + "extra_special_tokens": [ + "<|im_start|>", + "<|im_end|>", + "<|object_ref_start|>", + "<|object_ref_end|>", + "<|box_start|>", + "<|box_end|>", + "<|quad_start|>", + "<|quad_end|>", + "<|vision_start|>", + "<|vision_end|>", + "<|vision_pad|>", + "<|image_pad|>", + "<|video_pad|>" + ], + "is_local": false, + "model_max_length": 32768, + "pad_token": "<|endoftext|>", + "split_special_tokens": false, + "tokenizer_class": "Qwen2Tokenizer", + "unk_token": null +}