ALISY/mba-simplifier-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

MBA Simplifier — Ascend NPU (昇腾 NPU 适配版)

Simplifies Mixed Boolean-Arithmetic (MBA) expressions used in code obfuscation. Combines BFS lookup over a pre-computed expression table, symbolic rewriting (40+ rules), and a neural-guided search driven by a small Transformer classifier. Every output is oracle-verified correct.

This repo is the Ascend NPU-adapted version of qox/mba-simplifier:

  • GPU-independent: model and input tensors run on torch.device('npu:0') (Ascend910 ×2).
  • CLI + FastAPI dual-mode inference reusing one shared inference function.
  • Local weight cache priority: sidecars are read from model_weights/qox/mba-simplifier/ first, falling back to the repo root — no re-download.

Environment

  • Python 3.11 · torch 2.9.0+cpu · torch_npu · CANN 8.5.1 · Ascend910 (npu:0)
  • Set HF_ENDPOINT=https://hf-mirror.com if weights must be pulled from the mirror.

Quick Start

pip install -r requirements.txt

# CLI mode: --img accepts a text file OR the expression directly
python inference.py --img "(x & y) + (x | y)"
# → {"model": "mba-simplifier", "result": {"simplified": "x + y", ...}}

echo "(x & y) + (x | y)" > expr.txt
python inference.py --img expr.txt

# FastAPI mode
uvicorn inference:app --host 0.0.0.0 --port 8000
curl -s -F "file=@expr.txt" http://127.0.0.1:8000/predict
# → {"model":"mba-simplifier","result":{...}}

CLI output (JSON)

{
  "model": "mba-simplifier",
  "result": {
    "simplified": "x + y",
    "original": "(x & y) + (x | y)",
    "reduction": 0.571,
    "score": 0.571,
    "strategy": "bfs",
    "latency_ms": 393.5
  }
}

Python API

from inference import simplify

result = simplify("(x & y) + (x | y)")
print(result["simplified"])   # "x + y"
print(result["score"])        # 0.571
print(result["strategy"])     # "bfs"

Result fields

FieldTypeDescription
simplifiedstrSimplified infix expression
originalstrInput expression
reductionfloatTree-size reduction (0.0–1.0)
scorefloatAlias of reduction
strategystrbfs / symbolic / neural / none
latency_msfloatWall-clock time in milliseconds

Examples

InputOutputStrategy
(x & y) + (x | y)x + ybfs
(x | y) - (x & y)x ^ ybfs
(x + y) - 2 * (x & y)x ^ ybfs
(x & ~y) | (x & y)xbfs
~(~x & ~y)x | ysymbolic
x & (x | y)xsymbolic
(x + y) ^ (x + z)no simplificationneural (classifier on npu:0)

The neural Transformer classifier (vocab 41, d_model 256, 4 layers, 8 heads) runs on npu:0 and guides the beam search when BFS/symbolic do not fully reduce.

Files

FileDescription
inference.pyNPU-adapted standalone inference (CLI + FastAPI)
bfs_table_6.jsonPre-computed BFS table, 18K entries (1.7MB)
mba_classifier.safetensorsTransformer classifier, 2.2M params (8.9MB)
config.json / char_vocab.json / rule_vocab.jsonModel sidecars
model_weights/qox/mba-simplifier/Local weight cache (source of truth)
assets/Deployment / CLI / API screenshots

Limitations

  • BFS table: 8-bit only, variables x and y only; other cases fall through.
  • Bit width: 8 and 32 only.
  • Cold start: BFS table loads in ~390ms first call; NPU graph compile on first model forward adds latency on the first neural call.