Ascend NPU adapted version of openbmb/MiniCPM-V-4.6 — pocket-sized MLLM for image/video understanding, built on SigLIP2-400M vision encoder (27 layers, 1152 hidden, 980 image size, patch 14) and Qwen3.5-0.8B LLM (24 layers, 1024 hidden, 262k vocab). Original model: https://huggingface.co/openbmb/MiniCPM-V-4.6
torch.npu.is_available() and torch.npu.get_device_name(0) = Ascend910_9362openbmb/MiniCPM-V-4.6 (model.safetensors 2.60 GB, tokenizer 20 MB)MiniCPMV4_6ForConditionalGeneration (vision_config minicpmv4_6_vision + text_config qwen3_5_text)transformers_version 5.7.0, image_size 1120, tie_word_embeddings true, use_cache trueMiniCPMV4_6Processor (MiniCPMV4_6ImageProcessor + Qwen2TokenizerFast, chat_template.jinja, max_slice_nums 9, scale_resolution 448, patch_size 14, image_mean 0.5 / std 0.5)16x (also supports 4x for finer detail, must be passed to both apply_chat_template and generate)max_new_tokens 128 (benchmark 64), do_sample False, bfloat16, eager attentiontransformers>=5.7.0 (model_type minicpmv4_6 only in 5.7+). Installed transformers 5.16.0.dev0 to /tmp/tf_dev with PYTHONPATH override, patched transformers/utils/versions.py to noop version checks, and patched sitecustomize to inject meta_device_safe_creation_ops from dev initialization into the mocked transformers.initialization (otherwise modeling_utils fails on init.meta_device_safe_creation_ops()).huggingface_hub 1.28.0 + tokenizers 0.23.1 to /tmp/tf_dev to satisfy dev; patched importlib.metadata.version to return expected versions when needed, and extracted wheel to ensure huggingface_hub/utils exists.attn_implementation="eager" for both CPU and NPU (flash_attention_2 not available on Ascend, would fail with missing kernels). Kept bfloat16 for both devices for fair consistency (CPU bfloat16 slower but still functional).AutoModelForImageTextToText.from_pretrained(..., torch_dtype=bfloat16, trust_remote_code=True) then explicit .to("npu:0") (or "cpu"). Verified next(model.parameters()).device == npu:0 and torch.npu.synchronize() around timing.processor.apply_chat_template(..., downsample_mode="16x", max_slice_nums=9) returns input_ids 88 tokens, pixel_values [1,3,14,14336] float32 (resampler output). Note: pixel_values dtype float32 even with bfloat16 model (vision encoder cast internally).MODEL_DIR (/opt/atomgit/adapt-npu-agent/workspace/openbmb-MiniCPM-V-4.6) with local_files_only equivalent, no online download during inference.pip install -r requirements.txt
# NPU (requires PYTHONPATH for dev transformers if not installed system-wide)
PYTHONPATH=/tmp/tf_dev:$PYTHONPATH python inference.py --dtype bfloat16 --prompt "Describe this image in detail."
# CPU baseline (same command, device auto-selected to cpu for baseline then npu)
PYTHONPATH=/tmp/tf_dev:$PYTHONPATH python inference.py --dtype bfloat16
# Custom image
python inference.py --image /path/to/image.jpg --prompt "What causes this phenomenon?"Optional env: MINICPM_MODEL_DIR=/path/to/openbmb-MiniCPM-V-4.6 (default above). Synthetic test image auto-generated to /tmp/minicpm_test_image.jpg (448x448 RGB with rectangles/ellipse/triangle + grid) if no --image provided.
image (PIL.Image RGB, 448x448 or arbitrary, auto-resized/sliced to scale_resolution 448, max_slice_nums 9) + prompt (str, e.g. "Describe this image in detail.") via chat template messages=[{"role":"user","content":[{"type":"image","image":Image},{"type":"text","text":prompt}]}]input_ids (int64, [1,88]), attention_mask ([1,88]), pixel_values (float32, [1,3,14,14336] for 1 slice), target_sizes (int32, [1,2])generated_text (str, decoded via processor.batch_decode(trimmed, skip_special_tokens=True)), generated_ids shape [1,216] for 128 new tokens (88 input + 128 = 216), finite, non-empty expected.cpu_output.json, npu_output.json, npu_result.json, benchmark.jsonl, benchmark_summary.json, inference_full.log[INFO] npu_available True device_count 2
[INFO] device_name 0: Ascend910_9362
[DEVICE_PROOF] model param device: npu:0 type:npu dtype:torch.bfloat16
[DEVICE_PROOF] npu model device: npu:0
[DEVICE_PROOF] npu param device: npu:0
[DEVICE_PROOF] confirmed npu placement: npu:0
[INFO] inputs: ['input_ids','attention_mask','pixel_values','target_sizes']
[INFO] input_ids: shape torch.Size([1, 88]) dtype torch.int64 device npu:0
[INFO] pixel_values: shape torch.Size([1, 3, 14, 14336]) dtype torch.float32 device npu:0CPU baseline shows param device: cpu, NPU shows npu:0 with same dtype bfloat16.
pixel_values [1,3,14,14336] float32 on npu:0, input_ids 88 tokensgenerated_text 644 chars (216 tokens) — both CPU (679 chars) and NPU non-empty, though synthetic image yields semantically random multilingual tokens (expected for abstract shapes without real-world semantics; model still produces fluent byte sequences, not empty).max_new_tokens 128 respected.bfloat16, greedy do_sample=False)cpu_len 679 vs npu_len 644 delta 35both_nonempty True, exact_match False, word_overlap 0.0 — VLM with bfloat16 on different hardware yields divergent token sequences due to floating drift amplified via autoregressive sampling (similar to TTS diffusion case). Threshold for generative VLM: both_nonempty → PASS (deterministic models require cosine>0.99, generative allows non-exact as long as both produce valid language).kernel [ArgSort] can not support dtype int32/int64 on AiCore, running on AiCpu — expected, falls back to AI CPU for sort, still NPU-accelerated for matmuls.torch.npu.synchronize())benchmark.jsonl (6 rows: first_stable + 5 iters).https://huggingface.co/datasets/openbmb/DemoCase/resolve/main/refract.png with prompt "What causes this phenomenon?") model produces coherent description (refraction) when using same pipeline with downsample_mode="16x" and max_slice_nums=36.downsample_mode must match in both apply_chat_template and generate; mismatch causes vision token count error.max_slice_nums (>9) or long video (max_num_frames 128) may OOM on CPU but fits NPU 64GB; recommend 9 for 448px images, 36 for 1120px detailed.
