#NPU
This repository contains the Ascend NPU adaptation for PaddleOCR-VL, a vision-language model for OCR and document understanding tasks.
| Item | Detail |
|---|---|
| Model | PaddleOCR-VL |
| Original Framework | PaddlePaddle |
| Adaptation Framework | PyTorch / Transformers |
| NPU Backend | Ascend CANN + torch_npu |
| Task | Vision-Language / OCR / Document Understanding |
| Status | Adapted & Verified |
| Component | Version (Tested) |
|---|---|
| OS | Linux (aarch64) |
| Python | 3.11 |
| CANN Toolkit | 8.5.1 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| transformers | >= 4.35.0 |
npu-smi info
python -c "import torch; import torch_npu; print(torch.npu.is_available())"# 1. Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate
# 2. Install dependencies
pip install -r requirements.txt
# 3. Verify installation
python -c "import torch_npu; print('torch_npu:', torch_npu.__version__)"pip install modelscope
python -c "from modelscope import snapshot_download; snapshot_download('paddlepaddle/PaddleOCR-VL', cache_dir='./PaddleOCR-VL-weights')"pip install huggingface-hub
huggingface-cli download paddlepaddle/PaddleOCR-VL --local-dir ./PaddleOCR-VL-weightsVisit the model page and download the following files into ./PaddleOCR-VL-weights/:
config.jsontokenizer.jsontokenizer_config.jsonmodel.safetensors or pytorch_model.binpreprocessor_config.jsonpython inference.py \
--model_path ./PaddleOCR-VL-weights/paddlepaddle/PaddleOCR-VL \
--image sample.jpg \
--prompt "<|IMAGE_START|><|IMAGE_PLACEHOLDER|><|IMAGE_END|>Describe this document." \
--device npu \
--dtype float32Important: The prompt must contain <|IMAGE_START|><|IMAGE_PLACEHOLDER|><|IMAGE_END|> for the model to align image features with text tokens. Without these special tokens, you will get the error: "Image features and image tokens do not match".
| Parameter | Description | Default |
|---|---|---|
--model_path | Path to model directory | (required) |
--image | Input image path(s) | (required) |
--prompt | Text prompt with image placeholders | See above |
--device | Device: cpu / cuda / npu | npu |
--dtype | Data type: float16 / bfloat16 / float32 | float32 |
Run the accuracy evaluation script to compare CPU (baseline) vs NPU outputs.
python eval_accuracy.py \
--model_path ./PaddleOCR-VL-weights/paddlepaddle/PaddleOCR-VL \
--test_images test_images/test_doc_01.jpg \
--prompt "<|IMAGE_START|><|IMAGE_PLACEHOLDER|><|IMAGE_END|>Describe this document." \
--threshold_mre 1.0 \
--threshold_top1 99.0 \
--dtype float32 \
--output accuracy_report.md| Metric | Threshold | Actual Result | Status |
|---|---|---|---|
| MRE (eps=1e-3) | < 1.00% | 0.6408% | PASS |
| Top-1 Token Accuracy | >= 99.0% | 99.52% | PASS |
| Cosine Similarity | > 0.999 | 0.999919 | PASS |
Conclusion: PASS — NPU inference is numerically aligned with CPU baseline.
Standard relative error (eps=1e-12) explodes when logits are near zero, producing meaningless percentages like 8,000,000%. For logits in the range [-15, +25], adding 1e-3 to the denominator is mathematically sound and yields a stable, interpretable metric.
For LLMs, the most important question is: "Does the model generate the same tokens?" Top-1 accuracy directly measures this. Our result of 99.52% means 418 out of 420 sequence positions produce the exact same argmax token on CPU and NPU.
The script generates accuracy_report.md with detailed error tables and conclusion.
Run the performance benchmark to compare latency and throughput across CPU and NPU.
python eval_performance.py \
--model_path ./PaddleOCR-VL-weights/paddlepaddle/PaddleOCR-VL \
--image test_images/test_doc_01.jpg \
--prompt "<|IMAGE_START|><|IMAGE_PLACEHOLDER|><|IMAGE_END|>Describe this document." \
--devices cpu npu \
--num_runs 20 \
--warmup 3 \
--dtype float32 \
--output performance_report.md| Metric | Description |
|---|---|
| Avg Latency | Average end-to-end forward-pass time |
| P50 / P99 | Latency percentiles |
| First Token | Time to first forward pass completion |
| Throughput | Logits elements generated per second |
| Speedup | NPU speedup relative to CPU |
Note: GPU column is shown only when CUDA is available. In our tested environment (ARM64 server without discrete GPU), only CPU and NPU are compared.
RuntimeError: NPU out of memorySolution:
float16 instead of float32npu-smi infoImportError: torch_npuSolution:
# Verify CANN environment is sourced
source /usr/local/Ascend/ascend-toolkit/set_env.sh
# Reinstall torch_npu matching your PyTorch version
pip install torch_npu==$(python -c "import torch; print(torch.__version__[:5])")"Image features and image tokens do not match: tokens: 0, features 196"Solution: Your prompt does not contain the required image placeholder tokens. Use:
prompt = "<|IMAGE_START|><|IMAGE_PLACEHOLDER|><|IMAGE_END|>Describe this document."The processor replaces <|IMAGE_PLACEHOLDER|> with the correct number of image tokens based on the input image resolution.
Solution:
float32 for accuracy comparison (float16 has expected minor numerical differences)torch.npu.synchronize() is called before measuring timeeval_accuracy.py already handles these edge cases correctlySolution:
ValueError: Unrecognized configuration class PaddleOCRVLConfig for AutoModelForVision2SeqSolution: This model is not registered in standard AutoModel classes. Use AutoModel.from_pretrained(..., trust_remote_code=True) instead, as implemented in inference.py and eval_accuracy.py.
See SUBMISSION.md for detailed instructions.
This adaptation follows the license of the original PaddleOCR-VL model.
#NPU