g
gcw_coj3XaOd/qox_mba_simplifier
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

MBA Simplifier (qox/mba-simplifier) - 昇腾 NPU 推理部署

1. 模型简介

模型名称: qox/mba-simplifier

模型链接: HuggingFace

模型描述: 简化 Mixed Boolean-Arithmetic (MBA) 表达式的神经符号系统。结合 BFS 查表、符号重写和可选的神经引导搜索,用于反混淆代码中的 MBA 表达式。

模型架构: 自定义 TransformerEncoder (MBA Rule Classifier, 2.2M params)

参数规模: 2,224,474 (2.2M)

输入规格:

  • MBA 表达式字符串(如 (x & y) + (x | y))
  • 字符级 tokenization,最大长度 320
  • 支持变量:x, y(8-bit);支持更多变量(32-bit symbolic only)

输出规格:

  • 简化后的 MBA 表达式字符串
  • 简化策略(bfs / symbolic / neural)
  • 缩减比例 (0.0-1.0)
  • 推理延迟 (ms)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11(需 structural pattern matching)
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
safetensors>= 0.4.0安全权重加载
numpy>= 1.21.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch>=2.1.0 torch-npu>=2.1.0 safetensors>=0.4.0 numpy>=1.21.0

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

git clone https://huggingface.co/qox/mba-simplifier
cd mba-simplifier

方式二:AtomGit 镜像

git clone https://gitcode.com/gcw_coj3XaOd/qox_mba_simplifier
cd qox_mba_simplifier

3.3 运行推理

# 基础推理(BFS + 符号引擎,无需神经模型)
python inference.py "(x & y) + (x | y)"

# 启用神经引导推理(NPU 加速)
python inference.py --model-path mba_classifier.safetensors "complex_expression"

# 32-bit 模式
python inference.py --bits 32 "(x ^ y) + (x & y)"

3.4 推理参数说明

参数类型默认值说明
expr (positional)str必填MBA 表达式
--bitsint8位宽:8 或 32
--modelstrNone神经模型路径
--model-pathstrNone--model 的别名

4. 推理成功日志

4.1 单条推理日志

BFS: 18,189 entries (284ms)
Input:      (x & y) + (x | y)
Simplified: x + y
Reduction:  57% (bfs)
Latency:    291.7 ms
[状态] SUCCESS

4.2 批量推理统计

=== 推理测试汇总 ===
  (x & y) + (x | y) → x + y      (bfs, 57% reduction)
  (x | y) - (x & y) → x ^ y      (bfs, 57% reduction)
  x ^ x → 0                      (bfs, 67% reduction)
  ~(~x) → x                      (bfs, 67% reduction)
  (x + y) - 2*(x & y) → x ^ y    (bfs, 67% reduction)
  x & (x | y) → x                (bfs, 80% reduction)
  -(~x) - 1 → x                  (symbolic, 67% reduction)
  x * 1 + y * 0 → x              (bfs, 50% reduction)

  总测试数: 8
  成功简化: 8
  简化率: 100%
  平均延迟: ~292ms (cold), ~2ms (warm)

5. 测试样例及输出结果

样例 1:基本 MBA 恒等式

输入: (x & y) + (x | y) 运行命令:

python inference.py "(x & y) + (x | y)"

输出:

BFS: 18,189 entries (284ms)
Input:      (x & y) + (x | y)
Simplified: x + y
Reduction:  57% (bfs)
Latency:    291.7 ms

样例 2:XOR 变换

输入: (x | y) - (x & y) 运行命令:

python inference.py "(x | y) - (x & y)"

输出:

BFS: 18,189 entries (285ms)
Input:      (x | y) - (x & y)
Simplified: x ^ y
Reduction:  57% (bfs)
Latency:    293.4 ms

样例 3:自消除

输入: x ^ x 运行命令:

python inference.py "x ^ x"

输出:

BFS: 18,189 entries (286ms)
Input:      x ^ x
Simplified: 0
Reduction:  67% (bfs)
Latency:    291.5 ms

样例 4:De Morgan

输入: ~(~x) 运行命令:

python inference.py "~(~x)"

输出:

BFS: 18,189 entries (285ms)
Input:      ~(~x)
Simplified: x
Reduction:  67% (bfs)
Latency:    288.8 ms

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: MBA-Blast 论文数据集(62 个 8-bit 2-var 表达式)

评测指标:

指标结果说明
正确率100%Oracle 验证(512 点随机采样)
简化率100%所有表达式均可简化
平均缩减77%AST 树大小缩减
延迟6.5ms平均每表达式

评测命令:

python inference.py "(x & y) + (x | y)"
python inference.py "(x | y) - (x & y)"
python inference.py "x ^ x"
python inference.py "~(~x)"
python inference.py "(x + y) - 2*(x & y)"
python inference.py "x & (x | y)"
python inference.py "-(~x) - 1"
python inference.py "x * 1 + y * 0"

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 卡数: 2
  • CANN 版本: 8.5.1
  • 算子验证: Embedding, Linear, LayerNorm, ReLU, GELU, Softmax, Matmul, TransformerEncoder, Cross-Attention, Dropout — 全部兼容
  • 模型加载: 神经分类器可在 NPU 上运行(TransformerEncoder 推理)

9. 已知问题

  • BFS 表仅支持 8-bit 和最多 2 个变量(x, y)的表达式
  • 神经模型仅训练于 8-bit 数据,32-bit 模式仅使用符号引擎
  • 首次推理 BFS 表加载约 280ms,后续调用约 2ms(LRU 缓存)