m
mengsihan/qox-mba-simplifier-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

MBA Simplifier — Ascend NPU (qox/mba-simplifier)

Simplifies Mixed Boolean-Arithmetic (MBA) expressions on Huawei Ascend 910 NPU. This is an NPU adaptation of qox/mba-simplifier with explicit npu:0 execution.

Architecture

Three-layer pipeline (first hit wins):

  1. BFS lookup — 18,189 minimal 8-bit expressions (bfs_table_6.json)
  2. Symbolic rewriting — 40+ MBA identities (pure Python, fixpoint)
  3. Neural Transformer — 2.22M params, d_model=256, n_heads=8, n_layers=4 — runs on npu:0 via torch_npu

Base weights: mba_classifier.safetensors (8.5MB) + config.json/char_vocab.json/rule_vocab.json from original model.

NPU vs CPU Consistency

MetricValue
CPU vs NPU cosine1.000000 (all samples)
max_diff<5e-06 (fp32)
Top-1 rule agreement100% (10/10)
Pipeline correctness100% (10/10 cases)

Validated on Ascend910_9362 with torch.npu.is_available()=True, next(model.parameters()).device=npu:0, input_ids.device=npu:0, logits.device=npu:0.

Performance (Ascend 910)

  • Steady-state NPU latency: ~42.5 ms / expr (23.5 expr/s) for neural classifier forward (batch=1, max_len=320, synchronized with torch.npu.synchronize())
  • First compile ~42-206 ms (includes operator compilation)
  • BFS warm latency ~2 ms; cold ~380 ms (table load)
  • Symbolic fallback is CPU-bound but verified equivalent.

Usage

pip install -r requirements.txt  # torch, torch_npu, safetensors, numpy
python inference.py "(x & y) + (x | y)" --device npu:0
# also:
python inference.py "~(~x & ~y)" --bits 8
from inference import simplify, DEVICE
print(DEVICE)  # npu:0 on Ascend, cpu otherwise
result = simplify("(x & y) + (x | y)")
print(result["simplified"])  # "x + y"
print(result["strategy"])    # "bfs"
print(result["device"])      # "npu:0"

Input / Output Examples

InputOutputStrategy
`(x & y) + (xy)`x + y
`(xy) - (x & y)`x ^ y
~(~x)xbfs
x ^ x0bfs
`x & (xy)`x

NPU Notes

  • Model and all tensors are explicitly moved to npu:0 (model.to(torch.device("npu:0")), input_ids.to("npu:0")).
  • torch.npu.synchronize() is called before/after timing.
  • TransformerEncoderLayer falls back to CPU for aten::_transformer_encoder_layer_fwd (documented), but surrounding ops remain on NPU and logits.device==npu:0 is proven.

Evidence

workflow npu device call model result

Original Model

  • Hub: https://huggingface.co/qox/mba-simplifier
  • License: MIT
  • Files: bfs_table_6.json, mba_classifier.safetensors, config.json, char_vocab.json, rule_vocab.json