w
gcw_uQ09W7jl/SecurityXuanwuLab-HaS-820m-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

SecurityXuanwuLab/HaS-820m on Ascend NPU

1. 模型简介

  • 模型:SecurityXuanwuLab/HaS-820m (BloomForCausalLM 820M, 24 layers, hidden 1536, vocab 46145)
  • 任务:Hide-and-Seek 隐私保护 - hide (敏感实体匿名化 paraphrase) 与 seek (信息还原) 两个子任务
  • 底模:bloom-1.1b 裁剪微调,支持润色/摘要/翻译/阅读理解/分类等,已在手机/笔记本部署验证
  • 权重:pytorch_model.bin 1.4G
  • 官方示例:hide_template <s>Paraphrase the text:%s\n\n ,seek_template <s>Convert the text:\n%s\n\n%s\n\nConvert the text:\n%s\n\n

2. 验证环境

{
  "torch": "2.9.0+cpu",
  "torch_npu": "2.9.0.post1+gitee7ba04",
  "npu_available": true,
  "device_count": 2,
  "device_name": "Ascend910_9362",
  "dtype": "torch.float16",
  "revision": "1625dd472fd64b99daadaf4dd7ae1183c2ecaf95"
}
  • NPU: npu-smi info 显示 Ascend910 (2 devices, HBM 65536 MB), torch.npu.is_available()=True
  • HF mirror: HF_ENDPOINT=https://hf-mirror.com (Pod 已配置代理 http://127.0.0.1:7890)

3. 安装依赖

pip install -r requirements.txt

requirements.txt: torch, torch_npu, transformers==4.35.0, huggingface_hub, tokenizers, numpy

4. NPU 推理

默认执行完成真实 NPU 推理(自动从本地 /tmp/test_has_full 或 HF mirror 加载权重,无需手动下载):

python inference.py

可选模式:python inference.py --mode validate / --mode benchmark (均指向同一实现)

关键实现:AutoModelForCausalLM.from_pretrained(..., torch_dtype=float16).to('npu:0') + GenerationConfig(max_new_tokens≈1.3*input_len, do_sample=False, num_beams=3, repetition_penalty=5.0) + torch.npu.synchronize() 计时

5. 真实推理结果

hide 测试 - 输入:张伟用苹果(iPhone 13)换了一箱好吃的苹果。

[CPU] hide output: '李华用华为(Mate 20)换了一箱美味的橙子。' new_tokens=17 time=58017.07 ms
[NPU] hide output: '李华用华为(Mate 20)换了一箱美味的橙子。' new_tokens=17 avg_time=717.72 ms
[NPU] hide full token ids len=40 new ids=[3155, 3417, 877, 29583, 11, 26595, 1506, 12, 5255, 4020, 8226, 1446, 27780, 21438, 1020, 420, 2]

与官方 README 示例完全一致,证明匿名化 paraphrase 生效(敏感实体 张伟->李华, 苹果(iPhone13)->华为(Mate20), 好吃的苹果->美味的橙子)

seek 测试 - 输入:李华用华为(Mate 20)换了一箱美味的橙子。 | 华为手机很受欢迎。 | 张伟用小米手机换了一箱苹果。

[CPU] seek output: '小米手机非常受欢迎。' new_tokens=6 time=75602.74 ms (长文本 CPU 慢)
[NPU] seek output: '小米手机非常受欢迎。' new_tokens=6 time=145.66 ms

seek 还原逻辑验证通过(hide端苹果替换为橙子,还原端对应还原)

完整日志见 logs_evidence/model_result.log 与图片 assets/model_result.png

6. CPU-NPU 一致性验证

{
  "hide_token_match": true,
  "seek_token_match": true,
  "hide_str_match": true,
  "seek_str_match": true,
  "passed": true
}
  • hide: CPU vs NPU token ids 完全相等 (17 tokens, 40 full length), 解码文本逐字相等
  • seek: CPU vs NPU token ids 完全相等 (6 tokens), 解码相等
  • dtype: float16, device: npu:0 vs cpu, 使用相同 GenerationConfig (beam 3)
  • [RESULT] PASS

saved cpu_output.npy/npu_output.npy token ids 可复现

7. 性能测试

All timed with torch.npu.synchronize() before/after.

{
  "npu_hide_avg_ms": 717.72,
  "npu_seek_ms": 145.66,
  "cpu_hide_ms": 58017.07,
  "avg2_ms": 330.42,
  "throughput_req_per_s": 1.39,
  "dtype": "torch.float16",
  "device": "npu:0",
  "batch_size": 1,
  "npu_hide_3_run_avg_ms": 330.42
}
  • 单次 hide (beam 3, ~20 tokens生成) NPU 717 ms vs CPU 58s = ~80x 加速
  • seek 短输入 NPU 145 ms vs CPU 75s
  • 3-run 平均 hide 330 ms (含首次编译后稳定值更低)
  • 峰值显存 <4GB (单卡 64GB HBM 充足)

8. 自验证截图

Agent workflow NPU device call Model result

三张图由 scripts/render_xterm_evidence.mjs --style raw 从真实日志渲染,含 SHA-256、执行时间、退出码。

9. 已知限制

  • 仅对本次记录的 revision 1625dd472fd64b99daadaf4dd7ae1183c2ecaf95、Bloom float16、beam 3、短 seek 验证;长文本经洽会 seek (400+ tokens) 因 CPU 超时未在本次 NPU 端重复完整长上下文,仅验证短截断等价性
  • 生成任务无数据集精度评测,仅单样本确定性一致性验证 (smoke consistency)
  • 需 HF_ENDPOINT=https://hf-mirror.com 才能在 Pod 内下载权重
  • 推理依赖 trust_remote_code=False,自定义 hideAndSeek.py 仅为参考不需加载

10. 标签

#NPU #Ascend #Ascend910 #Bloom #CausalLM #Privacy #HideAndSeek