怹
gcw_uXfX1fA6/PaddleOCR-VL-20260519
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PaddleOCR-VL Ascend NPU Adaptation

#NPU

This repository contains the Ascend NPU adaptation for PaddleOCR-VL, a vision-language model for OCR and document understanding tasks.

ItemDetail
ModelPaddleOCR-VL
Original FrameworkPaddlePaddle
Adaptation FrameworkPyTorch / Transformers
NPU BackendAscend CANN + torch_npu
TaskVision-Language / OCR / Document Understanding
StatusAdapted & Verified

Table of Contents

  • Environment Requirements
  • Installation
  • Model Weights
  • Quick Start
  • Accuracy Evaluation
  • Performance Benchmark
  • FAQ

Environment Requirements

ComponentVersion (Tested)
OSLinux (aarch64)
Python3.11
CANN Toolkit8.5.1
PyTorch2.9.0+cpu
torch_npu2.9.0.post1
transformers>= 4.35.0

Verify NPU Environment

npu-smi info
python -c "import torch; import torch_npu; print(torch.npu.is_available())"

Installation

# 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__)"

Model Weights

Option A: Download from ModelScope (Recommended for China)

pip install modelscope
python -c "from modelscope import snapshot_download; snapshot_download('paddlepaddle/PaddleOCR-VL', cache_dir='./PaddleOCR-VL-weights')"

Option B: Download from HuggingFace

pip install huggingface-hub
huggingface-cli download paddlepaddle/PaddleOCR-VL --local-dir ./PaddleOCR-VL-weights

Option C: Manual Download

Visit the model page and download the following files into ./PaddleOCR-VL-weights/:

  • config.json
  • tokenizer.json
  • tokenizer_config.json
  • model.safetensors or pytorch_model.bin
  • preprocessor_config.json

Quick Start

Single Image Inference

python 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 float32

Important: 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".

Parameters

ParameterDescriptionDefault
--model_pathPath to model directory(required)
--imageInput image path(s)(required)
--promptText prompt with image placeholdersSee above
--deviceDevice: cpu / cuda / npunpu
--dtypeData type: float16 / bfloat16 / float32float32

Accuracy Evaluation

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

Pass Criteria

MetricThresholdActual ResultStatus
MRE (eps=1e-3)< 1.00%0.6408%PASS
Top-1 Token Accuracy>= 99.0%99.52%PASS
Cosine Similarity> 0.9990.999919PASS

Conclusion: PASS — NPU inference is numerically aligned with CPU baseline.

Why eps=1e-3 for MRE?

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.

Why Top-1 Token Accuracy is Primary

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.


Performance Benchmark

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

Metrics

MetricDescription
Avg LatencyAverage end-to-end forward-pass time
P50 / P99Latency percentiles
First TokenTime to first forward pass completion
ThroughputLogits elements generated per second
SpeedupNPU 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.


FAQ

Q1: RuntimeError: NPU out of memory

Solution:

  • Reduce image resolution or batch size
  • Use float16 instead of float32
  • Ensure no other processes are using the NPU: npu-smi info

Q2: ImportError: torch_npu

Solution:

# 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])")

Q3: "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.

Q4: Model outputs differ between CPU and NPU

Solution:

  • Use float32 for accuracy comparison (float16 has expected minor numerical differences)
  • Ensure torch.npu.synchronize() is called before measuring time
  • The provided eval_accuracy.py already handles these edge cases correctly

Q5: Slow NPU inference on first run

Solution:

  • First run includes graph compilation; subsequent runs are significantly faster
  • The benchmark script includes automatic warmup rounds

Q6: ValueError: Unrecognized configuration class PaddleOCRVLConfig for AutoModelForVision2Seq

Solution: 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.

Q7: How to add #NPU tag for AtomGit submission?

See SUBMISSION.md for detailed instructions.


License

This adaptation follows the license of the original PaddleOCR-VL model.

Acknowledgements

  • Original model: paddlepaddle/PaddleOCR-VL
  • Ascend NPU toolchain: torch_npu

#NPU