Adaptation of the neural rewrite-rule classifier from qox/mba-simplifier
to Huawei Ascend 910 NPUs via torch_npu. The model is a character-level Transformer
encoder that reads a Mixed Boolean-Arithmetic (MBA) expression and predicts which of
90 symbolic rewrite rules should be applied next — the neural guidance stage of an
MBA deobfuscation pipeline.
| Item | Value |
|---|---|
| Source | https://huggingface.co/qox/mba-simplifier (revision: main) |
| Task | NLP — char-level text classification (rewrite-rule prediction, 90 classes) |
| Architecture | Embedding(41×256) + learned positional embedding(320) + 4-layer norm-first TransformerEncoder (8 heads, d_ff=512) + LayerNorm + Linear(256→90), mean-pool over non-pad chars |
| Parameters | 2.22M (fp32, mba_classifier.safetensors) |
| Input | MBA expression string, char-tokenized with char_vocab.json, padded/truncated to 320 |
| Output | 90 logits over rewrite rules (rule_vocab.json), e.g. comm_^, fold_not, self_xor_zero |
npu:0; every tensor taking
part in compute (input ids, hidden states, logits) lives on the NPU.aten::_transformer_encoder_layer_fwd) is not implemented in torch_npu and under
eval()+no_grad() it silently falls back to CPU. The adapter calls
torch.backends.mha.set_fastpath_enabled(False) and iterates encoder layers
explicitly so attention/linear/LayerNorm/GELU run as real NPU kernels. No
npu_cpu_fallback warnings remain during inference.8 real MBA expressions from the official model card; identical weights, inputs, preprocessing and dtype for CPU baseline and NPU:
| Metric | Result | Gate |
|---|---|---|
| max abs logit diff (NPU vs CPU) | 4.77e-06 | <= 1e-3 |
| min cosine similarity | 0.99999994 | >= 0.999 |
| argmax / top-5 rule agreement | 8/8 exact | required |
| first-call latency (incl. compile) | ~3.5 ms | recorded |
| stable latency | ~3.0 ms/sentence | torch.npu.synchronize()d |
| throughput | ~336 sentences/s | batch=1 |
Device proofs printed at runtime: parameter device, input device and output device
are all npu:0.
Real terminal logs from this adaptation session:



pip install -r requirements.txt
# full validation: CPU baseline vs npu:0 consistency + performance
python inference.py # downloads weights to ./model_cache on first run
python inference.py --model-dir /path/to/mba-simplifier # use local weights
# classify one expression: prints top-5 predicted rewrite rules
python inference.py --predict "(x & y) ^ (x | y)"Exit code is non-zero unless every NPU gate passes (device placement, max diff, cosine, argmax agreement).
| File | Description |
|---|---|
inference.py | Standalone Ascend NPU adapter/validation + prediction CLI |
requirements.txt | torch, torch-npu (aarch64), safetensors, numpy |
Weights are not redistributed here; inference.py fetches them from the public
mirror (or uses --model-dir). BFS lookup table and the symbolic rewriting engine
of the original pipeline are pure-Python deterministic components that do not
require hardware acceleration; only the neural classifier is adapted here.