From 594dc0f6b2fe5ef7b884d7d69d94cb6f0d62d5a8 Mon Sep 17 00:00:00 2001 From: agcl Date: Fri, 27 Nov 2026 20:48:09 +0800 Subject: [PATCH] =?UTF-8?q?models:=20=E6=96=B0=E5=A2=9E=20Qwen3-8B-guard?= =?UTF-8?q?=20LoRA=20=E9=80=82=E9=85=8D=E5=99=A8=EF=BC=88LFS=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A6000 上 8005 端口服务的 Qwen3-8B-guard-same 对应的 PEFT 适配器: r=16 / alpha=32 / 7 个投影层,可训练参数 43,646,976(0.5301%); adapter_model.safetensors 174,655,536 字节,md5 307b3d8a1a7d5d420b9ef43620f70492。 只提交增量(175MB),不含 16GB 基座与合并权重;模型说明.md 记录训练参数、 评测口径、合并脚本,并注明它不能直接作为 response_guard 的守卫后端 (输出是拒答文本而非 Safety/Categories/Refusal)。 --- .gitattributes | 1 + models/qwen3-8b-guard-lora/README.md | 207 ++++++++++++++++++ .../qwen3-8b-guard-lora/adapter_config.json | 51 +++++ .../adapter_model.safetensors | 3 + models/qwen3-8b-guard-lora/模型说明.md | 69 ++++++ 5 files changed, 331 insertions(+) create mode 100644 models/qwen3-8b-guard-lora/README.md create mode 100644 models/qwen3-8b-guard-lora/adapter_config.json create mode 100644 models/qwen3-8b-guard-lora/adapter_model.safetensors create mode 100644 models/qwen3-8b-guard-lora/模型说明.md diff --git a/.gitattributes b/.gitattributes index ba6b5ff..41d2428 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,4 @@ * text=auto eol=lf models/guard_model/*.safetensors filter=lfs diff=lfs merge=lfs -text models/guard_model/tokenizer.json filter=lfs diff=lfs merge=lfs -text +models/qwen3-8b-guard-lora/*.safetensors filter=lfs diff=lfs merge=lfs -text diff --git a/models/qwen3-8b-guard-lora/README.md b/models/qwen3-8b-guard-lora/README.md new file mode 100644 index 0000000..386fb59 --- /dev/null +++ b/models/qwen3-8b-guard-lora/README.md @@ -0,0 +1,207 @@ +--- +base_model: /home/chenleixu/models/Qwen3-8B +library_name: peft +pipeline_tag: text-generation +tags: +- base_model:adapter:/home/chenleixu/models/Qwen3-8B +- lora +- transformers +--- + +# Model Card for Model ID + + + + + +## Model Details + +### Model Description + + + + + +- **Developed by:** [More Information Needed] +- **Funded by [optional]:** [More Information Needed] +- **Shared by [optional]:** [More Information Needed] +- **Model type:** [More Information Needed] +- **Language(s) (NLP):** [More Information Needed] +- **License:** [More Information Needed] +- **Finetuned from model [optional]:** [More Information Needed] + +### Model Sources [optional] + + + +- **Repository:** [More Information Needed] +- **Paper [optional]:** [More Information Needed] +- **Demo [optional]:** [More Information Needed] + +## Uses + + + +### Direct Use + + + +[More Information Needed] + +### Downstream Use [optional] + + + +[More Information Needed] + +### Out-of-Scope Use + + + +[More Information Needed] + +## Bias, Risks, and Limitations + + + +[More Information Needed] + +### Recommendations + + + +Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations. + +## How to Get Started with the Model + +Use the code below to get started with the model. + +[More Information Needed] + +## Training Details + +### Training Data + + + +[More Information Needed] + +### Training Procedure + + + +#### Preprocessing [optional] + +[More Information Needed] + + +#### Training Hyperparameters + +- **Training regime:** [More Information Needed] + +#### Speeds, Sizes, Times [optional] + + + +[More Information Needed] + +## Evaluation + + + +### Testing Data, Factors & Metrics + +#### Testing Data + + + +[More Information Needed] + +#### Factors + + + +[More Information Needed] + +#### Metrics + + + +[More Information Needed] + +### Results + +[More Information Needed] + +#### Summary + + + +## Model Examination [optional] + + + +[More Information Needed] + +## Environmental Impact + + + +Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700). + +- **Hardware Type:** [More Information Needed] +- **Hours used:** [More Information Needed] +- **Cloud Provider:** [More Information Needed] +- **Compute Region:** [More Information Needed] +- **Carbon Emitted:** [More Information Needed] + +## Technical Specifications [optional] + +### Model Architecture and Objective + +[More Information Needed] + +### Compute Infrastructure + +[More Information Needed] + +#### Hardware + +[More Information Needed] + +#### Software + +[More Information Needed] + +## Citation [optional] + + + +**BibTeX:** + +[More Information Needed] + +**APA:** + +[More Information Needed] + +## Glossary [optional] + + + +[More Information Needed] + +## More Information [optional] + +[More Information Needed] + +## Model Card Authors [optional] + +[More Information Needed] + +## Model Card Contact + +[More Information Needed] +### Framework versions + +- PEFT 0.21.3 \ No newline at end of file diff --git a/models/qwen3-8b-guard-lora/adapter_config.json b/models/qwen3-8b-guard-lora/adapter_config.json new file mode 100644 index 0000000..0332b59 --- /dev/null +++ b/models/qwen3-8b-guard-lora/adapter_config.json @@ -0,0 +1,51 @@ +{ + "alora_invocation_tokens": null, + "alpha_pattern": {}, + "arrow_config": null, + "auto_mapping": null, + "base_model_name_or_path": "/home/chenleixu/models/Qwen3-8B", + "bias": "none", + "corda_config": null, + "ensure_weight_tying": false, + "eva_config": null, + "exclude_modules": null, + "fan_in_fan_out": false, + "inference_mode": true, + "init_lora_weights": true, + "kasa_config": null, + "layer_replication": null, + "layers_pattern": null, + "layers_to_transform": null, + "loftq_config": {}, + "lora_alpha": 32, + "lora_bias": false, + "lora_dropout": 0.05, + "lora_ga_config": null, + "megatron_config": null, + "megatron_core": "megatron.core", + "modules_to_save": null, + "monteclora_config": null, + "peft_type": "LORA", + "peft_version": "0.21.3", + "qalora_group_size": 16, + "r": 16, + "rank_pattern": {}, + "revision": null, + "target_modules": [ + "down_proj", + "o_proj", + "v_proj", + "q_proj", + "up_proj", + "k_proj", + "gate_proj" + ], + "target_parameters": null, + "task_type": "CAUSAL_LM", + "trainable_token_indices": null, + "use_bdlora": null, + "use_dora": false, + "use_qalora": false, + "use_rslora": false, + "velora_config": null +} \ No newline at end of file diff --git a/models/qwen3-8b-guard-lora/adapter_model.safetensors b/models/qwen3-8b-guard-lora/adapter_model.safetensors new file mode 100644 index 0000000..ec5aca5 --- /dev/null +++ b/models/qwen3-8b-guard-lora/adapter_model.safetensors @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:86adb4a9182d9ed1704be60ce79b6db751653f6c8c6f7c128785af5c9c40932d +size 174655536 diff --git a/models/qwen3-8b-guard-lora/模型说明.md b/models/qwen3-8b-guard-lora/模型说明.md new file mode 100644 index 0000000..7c561a7 --- /dev/null +++ b/models/qwen3-8b-guard-lora/模型说明.md @@ -0,0 +1,69 @@ +# Qwen3-8B-guard LoRA(same 变体) + +内容安全判定用的 LoRA 适配器,基座为 Qwen3-8B。本目录只放**适配器增量**(175MB), +不含基座权重;基座请自行从 Qwen3-8B 官方仓库获取。 + +## 文件 + +| 文件 | 说明 | +|---|---| +| `adapter_model.safetensors` | LoRA 权重,174,655,536 字节,md5 `307b3d8a1a7d5d420b9ef43620f70492` | +| `adapter_config.json` | PEFT 配置,`r=16`、`lora_alpha=32`、7 个投影层 | +| `README.md` | PEFT 自动生成的 Model Card 模板(未填写) | + +## 训练参数 + +- 基座:`Qwen3-8B`(bf16) +- LoRA:`r=16`、`alpha=32`、`target_modules = q/k/v/o/gate/up/down_proj`,可训练参数 43,646,976(占 0.5301%) +- 数据:1658 条攻击样本 + 200 条良性样本 × 4 次重复,2 epoch +- 耗时:单卡 A6000,1267 s(约 21 分钟) +- 变体:`same`(保持基座原有的拒答行为);另有 `unseat` 变体(解除拒答),不在此目录 + +## 评测口径(历史记录,非本目录复现) + +| 指标 | 值 | 口径 | +|---|---|---| +| AUC | 0.9989 | 同一分布留出集,n_pos 398 / n_neg 200 | +| 阈值 | 0.99999 | 按该阈值取 P(unsafe) | +| 召回 / 误拦 | 94.97% / 0.00% | 同一阈值下 | +| vLLM 复验 | 94.47%~94.72%,误拦 0~1 条 | 阈值 0.99999,延迟 0.08~0.11 s | + +结果随阈值与机器负载浮动,重跑请以实际输出为准。 + +## 与 Qwen3-8B 合并 + +```python +from peft import PeftModel +from transformers import AutoModelForCausalLM, AutoTokenizer + +base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-8B", torch_dtype="bfloat16") +model = PeftModel.from_pretrained(base, "./qwen3-8b-guard-lora") +model = model.merge_and_unload() +model.save_pretrained("./Qwen3-8B-guard-same") + +tok = AutoTokenizer.from_pretrained("Qwen/Qwen3-8B") +tok.save_pretrained("./Qwen3-8B-guard-same") +``` + +合并后约 16GB(5 个 safetensors 分片),即 A6000 上 `8005` 端口服务的 `Qwen3-8B-guard-same`。 + +## 注意:不能直接当 response_guard 的守卫后端 + +宿主的 `parse_guard_output` 要求模型输出 + +``` +Safety: Safe | Unsafe | Controversial +Categories: <固定枚举> +Refusal: Yes | No +``` + +这个适配器训练成的是**拒答/打分**形态:给它一段待判文本,它会像助手一样回答 +(例如“我不能提供任何机密文件……”),不产生 `Safety:` 行。直接接到 +`response_guard.lua` 的 `host.llm.call` 上,每次判定都会因 `InvalidModelOutput` +fail-closed 成 503。 + +要用它做判定,需要二选一: + +1. 走打分路径:取 `P(unsafe)` 的 logprob,按阈值(如 0.99999)映射成 safety 标签, + 在宿主侧新增一个 adapter,而不是复用生成式契约; +2. 换用原生按 `Safety:/Categories:/Refusal:` 输出的 Qwen3Guard-Gen(0.6B/4B/8B)。