Text Generation
Transformers
Safetensors
Chinese
English
qwen3
safety-alignment
math-reasoning
dpo
lora
conversational
text-generation-inference
Instructions to use xdt11/Qwen3-0.6B-Safety-Math with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use xdt11/Qwen3-0.6B-Safety-Math with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="xdt11/Qwen3-0.6B-Safety-Math") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("xdt11/Qwen3-0.6B-Safety-Math") model = AutoModelForCausalLM.from_pretrained("xdt11/Qwen3-0.6B-Safety-Math", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use xdt11/Qwen3-0.6B-Safety-Math with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "xdt11/Qwen3-0.6B-Safety-Math" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "xdt11/Qwen3-0.6B-Safety-Math", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/xdt11/Qwen3-0.6B-Safety-Math
- SGLang
How to use xdt11/Qwen3-0.6B-Safety-Math with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "xdt11/Qwen3-0.6B-Safety-Math" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "xdt11/Qwen3-0.6B-Safety-Math", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "xdt11/Qwen3-0.6B-Safety-Math" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "xdt11/Qwen3-0.6B-Safety-Math", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use xdt11/Qwen3-0.6B-Safety-Math with Docker Model Runner:
docker model run hf.co/xdt11/Qwen3-0.6B-Safety-Math
| language: | |
| - zh | |
| - en | |
| license: apache-2.0 | |
| library_name: transformers | |
| pipeline_tag: text-generation | |
| base_model: Qwen/Qwen3-0.6B | |
| tags: | |
| - qwen3 | |
| - safety-alignment | |
| - math-reasoning | |
| - dpo | |
| - lora | |
| # Qwen3-0.6B-Safety-Math | |
| 这是一个以 [`Qwen/Qwen3-0.6B`](https://huggingface.co/Qwen/Qwen3-0.6B) 为唯一基座的安全与数学联合微调模型。模型保持原始 Qwen3-0.6B 架构与规模(596,049,920 个参数),仓库中提供的是已经合并 LoRA 的完整 BF16 权重,可直接使用 Transformers 或 vLLM 加载。 | |
| ## 训练方法 | |
| 训练分为两个阶段: | |
| 1. **安全能力训练**:使用结构化安全推理、双向安全边界样本与通用/数学能力保持样本进行 retention-aware SFT。从基座模型训练极窄中层 LoRA,并选择 step-200 合并模型。 | |
| 2. **数学能力训练**:从安全 step-200 模型出发,对 942 个 verifier-guided 数学偏好对进行长度归一化、带冻结 reference 的 margin-weighted DPO。使用 rank-8 中层 attention LoRA,学习率 `3e-6`,训练 60 个 optimizer steps,随后将适配器合并到完整模型。 | |
| 安全阶段共使用 3,200 条训练样本,其中 2,400 条为安全样本,800 条为通用与数学保持样本。安全训练数据来自公开数据集与冻结基座自回放的组合,包括 UWNSL/SafeChain、Yeonjun/AltTrain-1K、GSM8K、MATH、NuminaMath、OpenAssistant 等。训练集与本地冻结评测集进行了精确文本去重,检测到的重叠数为 0。 | |
| ## 本地代理评测 | |
| 以下结果仅用于本地选模,不代表主办方隐藏数据集或官方排行榜成绩。 | |
| ### 数学冻结集(每个数据集 80 题,thinking,16K) | |
| | 数据集 | Pass@1 | | |
| |---|---:| | |
| | GSM8K | 0.725 | | |
| | MATH-500 | 0.850 | | |
| | MGSM-ZH | 0.575 | | |
| | 宏平均 | 0.717 | | |
| 数学评测采用 Qwen3 thinking 推荐采样参数:`temperature=0.6`、`top_p=0.95`、`top_k=20`,并由 `math-verify` 判定最终答案。 | |
| ### 安全与通用诊断集(每个数据集 40 题) | |
| | 数据集/指标 | 分数 | | |
| |---|---:| | |
| | HarmBench safe rate | 0.725 | | |
| | XSTest balanced accuracy | 0.677 | | |
| | MMLU accuracy | 0.375 | | |
| | CEval accuracy | 0.325 | | |
| | IFEval strict prompt accuracy | 0.500 | | |
| 该小规模安全/通用运行使用确定性解码,且部分回答达到生成长度上限,因此只应视为诊断结果,不宜与采用采样解码的其他 checkpoint 直接比较。 | |
| ## 使用方法 | |
| 建议使用 `transformers>=4.51.0`,并遵循 Qwen3 官方建议,不要在 thinking 模式下使用贪心解码。 | |
| ```python | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| model_id = "xdt11/Qwen3-0.6B-Safety-Math" | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_id, | |
| torch_dtype="auto", | |
| device_map="auto", | |
| ) | |
| messages = [{"role": "user", "content": "求方程 x^2 - 5x + 6 = 0 的解。"}] | |
| text = tokenizer.apply_chat_template( | |
| messages, | |
| tokenize=False, | |
| add_generation_prompt=True, | |
| enable_thinking=True, | |
| ) | |
| inputs = tokenizer(text, return_tensors="pt").to(model.device) | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=16384, | |
| do_sample=True, | |
| temperature=0.6, | |
| top_p=0.95, | |
| top_k=20, | |
| ) | |
| answer = tokenizer.decode(outputs[0, inputs.input_ids.shape[1]:], skip_special_tokens=True) | |
| print(answer) | |
| ``` | |
| ## 局限与安全声明 | |
| - 这是一个约 0.6B 参数的小模型,复杂推理能力和输出稳定性仍然有限。 | |
| - 安全微调不能保证模型在所有越狱、长上下文或多轮攻击下都能拒绝有害请求。 | |
| - 模型有时会产生过长推理、重复内容、错误答案或不恰当拒答,部署时仍需加入输入过滤、输出审查和长度限制。 | |
| - 所有本地评测规模都较小;提交方的隐藏评测结果可能不同。 | |
| ## 基座与许可 | |
| 本模型基于 Qwen/Qwen3-0.6B,并沿用 Apache-2.0 许可。使用者还应遵守原始 Qwen3 模型的许可和适用法律。 | |