HaS-820m 是腾讯安全玄武实验室(Tencent Security Xuanwu Lab)发布的 Hide-and-Seek 隐私保护模型。它以 BLOOM-1.1B 为底模,经词表裁剪与专项微调后得到约 821.7M 参数(hidden=1536,n_layer=24,n_head=16,vocab=46145)。模型提供两类互补的隐私 NLP 能力:
本次适配将模型部署于 昇腾 Ascend910 NPU,通过 vLLM-Ascend 0.18.0 提供 OpenAI 兼容的服务化推理接口(/v1/completions)。适配核心解决了两处兼容性问题:tied embedding 权重显式声明,以及 BLOOM head_dim=96 超出 Ascend fused attention 算子支持集(64/128/192)导致的推理失败,后者通过注入 SDPA + causal mask + ALiBi 的兼容实现解决。
| 项目 | 实测值 |
|---|---|
| 昇腾处理器 | Ascend910(Ascend910_9362) |
| CANN | 8.5.1 |
| 操作系统 | Linux 5.10.0 aarch64 |
| Python | 3.11.14 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| vLLM | 0.18.0(源码) |
| vLLM-Ascend | 0.18.0 |
| transformers | 4.57.6 |
| 模型权重 | 1,501,777,697 字节(fp16,293 张量,821.7M 参数) |
该模型在 GitCode 与 ModelScope 均未同步,需从 HuggingFace 镜像拉取:
export HF_ENDPOINT=https://hf-mirror.com
huggingface-cli download SecurityXuanwuLab/HaS-820m --local-dir /opt/atomgit/models/SecurityXuanwuLab/HaS-820m由于 config.json 未显式声明 tie_word_embeddings(transformers 默认 True),且 checkpoint 内含一份与 word_embeddings 完全相同的 lm_head.weight 副本,vLLM 加载会报 KeyError。需按如下方式修复:
import json, torch
d = '/opt/atomgit/models/SecurityXuanwuLab/HaS-820m'
c = json.load(open(f'{d}/config.json')); c['tie_word_embeddings'] = True
json.dump(c, open(f'{d}/config.json', 'w'), indent=2, ensure_ascii=False)
sd = torch.load(f'{d}/pytorch_model.bin', map_location='cpu', weights_only=True)
sd.pop('lm_head.weight', None)
torch.save(sd, f'{d}/pytorch_model.bin')BLOOM 的 head_dim = 1536 / 16 = 96,而 Ascend aclnnFusedInferAttentionScoreV3 仅支持 head_dim ∈ {64, 128, 192}。将仓库内 sitecustomize.py 置于 PYTHONPATH 首部,使其在 EngineCore 进程内将 AscendAttentionBackendImpl.forward_fused_infer_attention 替换为基于 torch.nn.functional.scaled_dot_product_attention 的实现(显式 causal mask + ALiBi 相对位置偏置)。
export ASCEND_RT_VISIBLE_DEVICES=1
export PYTHONPATH=/path/to/patch:$PYTHONPATH
vllm serve /opt/atomgit/models/SecurityXuanwuLab/HaS-820m \
--served-model-name HaS-820m \
--dtype bfloat16 --tensor-parallel-size 1 \
--max-model-len 1024 --max-num-seqs 2 \
--gpu-memory-utilization 0.1 --port 8031 \
--enforce-eager --trust-remote-code \
--no-enable-prefix-caching端口按空闲情况选用;若
8031被其他任务占用,改换其他端口即可,无需改动其他配置。
curl -sf http://127.0.0.1:8031/v1/models
# 应返回包含 "id": "HaS-820m" 的模型列表服务就绪后,用 hide 模板发起一次脱敏请求:
curl -s http://127.0.0.1:8031/v1/completions -H 'Content-Type: application/json' \
-d '{"model":"HaS-820m",
"prompt":"<s>Paraphrase the text:张伟用苹果(iPhone 13)换了一箱好吃的苹果。\n\n",
"max_tokens":64,"temperature":0}'实测返回(精简):
{
"text": "李华用华为(Mate 20)换了一箱好吃的橙子。",
"usage": {"prompt_tokens": 23, "completion_tokens": 17, "total_tokens": 40}
}可见输入中的人名「张伟」与设备「iPhone 13」分别被匿名化为「李华」与「华为(Mate 20)」,符合 hide 任务预期。也可使用仓库内 inference.py:
python3 inference.py --task hide --input "张伟用苹果(iPhone 13)换了一箱好吃的苹果。"以下数据在共享 NPU 环境实测,同一时刻可能有其他任务占用设备;单卡、单并发、bfloat16、--enforce-eager 模式:
| 任务 | prompt 长度 | completion 长度 | 耗时 | 说明 |
|---|---|---|---|---|
| hide 脱敏 | 23 tokens | 17 tokens | ~0.59s | 首次含算子编译,后续更短 |
| seek 还原 | 257 tokens | 21 tokens | ~0.64s | 长上下文摘要还原 |
| 文本补全 | 11 tokens | 64 tokens | ~1.83s | 通用生成 |
模型加载后 NPU HBM 占用约 15.8 GB(含 vLLM 运行时与 KV cache),推理时 chip AICore 使用率实测 12%(npu-smi info 观测)。
张伟用苹果(iPhone 13)换了一箱好吃的苹果。,输出 李华用华为(Mate 20)换了一箱好吃的橙子。,与官方 README 参考输出完全一致(实体替换到位,句法通顺)。2016苏州吴中·太湖经贸合作洽谈会成功举办,各大媒体广泛报道。,与参考摘要一致。0.0002(bf16,head_dim=96),属于 bf16 正常舍入范围。sitecustomize.py patch;否则报 RuntimeError 561002。npu_fusion_attention 替代:该算子实测忽略 TND 布局下的 causal mask(输出与 full attention 一致),会破坏 BLOOM 因果性,输出不可靠。true 并删除 checkpoint 冗余的 lm_head.weight,否则 vLLM 加载报 KeyError。npu-smi info 确认空闲卡;模型加载约需 15 GB HBM。--port,切勿强占他人服务端口。/opt/atomgit 配额有限,运行时缓存(HF_HOME 等)建议重定向到 /tmp。# hide 脱敏
python3 inference.py --task hide --input "张伟用苹果(iPhone 13)换了一箱好吃的苹果。"
# seek 还原
python3 inference.py --task seek \
--hidden "李华用华为(Mate 20)换了一箱好吃的橙子。" \
--summary "李华用华为手机换了一箱橙子。" \
--original "张伟用苹果(iPhone 13)换了一箱好吃的苹果。"