gcw_6a3w2yXz/qox-mba-simplifier-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

qox/mba-simplifier on Ascend NPU

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.

Model summary

ItemValue
Sourcehttps://huggingface.co/qox/mba-simplifier (revision: main)
TaskNLP — char-level text classification (rewrite-rule prediction, 90 classes)
ArchitectureEmbedding(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
Parameters2.22M (fp32, mba_classifier.safetensors)
InputMBA expression string, char-tokenized with char_vocab.json, padded/truncated to 320
Output90 logits over rewrite rules (rule_vocab.json), e.g. comm_^, fold_not, self_xor_zero

NPU adaptation notes

  • Weights are loaded once from safetensors and moved to npu:0; every tensor taking part in compute (input ids, hidden states, logits) lives on the NPU.
  • CPU-fallback fix: PyTorch's fused fast path (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.

Validation results (Ascend910_9362, npu:0, fp32)

8 real MBA expressions from the official model card; identical weights, inputs, preprocessing and dtype for CPU baseline and NPU:

MetricResultGate
max abs logit diff (NPU vs CPU)4.77e-06<= 1e-3
min cosine similarity0.99999994>= 0.999
argmax / top-5 rule agreement8/8 exactrequired
first-call latency (incl. compile)~3.5 msrecorded
stable latency~3.0 ms/sentencetorch.npu.synchronize()d
throughput~336 sentences/sbatch=1

Device proofs printed at runtime: parameter device, input device and output device are all npu:0.

Evidence

Real terminal logs from this adaptation session:

agent workflow

NPU device call

model result

Usage

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).

Files

FileDescription
inference.pyStandalone Ascend NPU adapter/validation + prediction CLI
requirements.txttorch, 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.