#NPU Ascend Ascend910 BiomedCLIP Vision-Language Biomedical Zero-Shot
microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_2249f341de24bfb00180f1b847274256e9b65a3a32eCustomTextCLIP — Vision: vit_base_patch16_224 (Timm, 86M), Text: microsoft/BiomedNLP-BiomedBERT-base-uncased-abstract (BERT-base, 110M, context_length=256, hf_proj_type=mlp), embed_dim=512open_clip create_model_and_transforms with local-dir: schema, HFTextEncoder (PubMedBERT) + ViT3×224×224, preprocess: Resize(224, bicubic, antialias) → CenterCrop(224) → ToTensor → Normalize(mean=[0.48145466,0.4578275,0.40821073], std=[0.26862954,0.26130258,0.27577711]), dtype float32, shape [B,3,224,224] (B=1 default)HFTokenizer (vocab vocab.txt, tokenizer.json from model dir), context_length=256, padding/truncation, dtype int64, shape [N,256] (N=number of prompts)"a chest X-ray showing pneumonia", "a normal chest X-ray", "a brain MRI showing tumor", "histopathology image of adenocarcinoma", "immunohistochemistry staining"image_features: [B,512] L2-normalized, float32text_features: [N,512] L2-normalized, float32logit_scale: scalar ≈85.23 (exp≈1e37), logits = logit_scale.exp() * image_features @ text_features.T → [B,N]probs: softmax over N prompts, pred = argmaxexample_data/biomed_image_classification_example_data/ (8 images: chest_X-ray.jpg, brain_MRI.jpg, adenocarcinoma_histopathology.jpg, etc.)224×224 random image if no file foundnpu:0/usr/local/Ascend/cann-8.5.1)torch.npu.is_available()==True, device_count==2, get_device_name(0)==Ascend910_9362pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn \
open_clip_torch timm ftfy regex Pillow
# torch and torch_npu are preinstalled on Ascend base image (2.9.0)
pip install -r requirements.txtModel download (pre-cached to /tmp/biomedclip):
from huggingface_hub import snapshot_download
snapshot_download("microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224", local_dir="/tmp/biomedclip")Default command (uses npu:0, real weights from /tmp/biomedclip or HF Hub fallback):
python inference.py
# or explicitly
python inference.py --device npu:0 --model-dir /tmp/biomedclip
# custom image + prompts
python inference.py --image /path/to/image.jpg --prompts "pneumonia" "normal"Expected stdout includes:
model_name, device: npu:0, torch_npu available: True, device_name: Ascend910_9362backend: open_clip + torch_npu, param_device: npu:0, dtype: torch.float32first run (compile) time, stable avg time, throughputimage_features/text_features shapes, logit_scale, logits, probs, pred indicesExit code 0 on success; non-zero if NPU unavailable or CPU fallback.
image_features: [1,512] mean≈0.000 (normalized)
text_features: [3,512]
logit_scale: 85.2323 (exp=1.2e37)
logits: [[3.797e+36, 4.561e+36, 1.866e+36]]
probs: [[0., 1., 0.]]
pred: [1] "a normal chest X-ray"
first run (compile): 310.74 ms
stable avg (3 runs): 14.42 ms
stable avg (10 runs): 13.19 ms, p50 13.56 ms, p90 13.76 ms, throughput 75.8 infer/s (batch=1, fp32, [1,3,224,224])
CPU avg (same input): 3384 ms (NPU ~256× faster)Note: logit_scale is unusually large (trained value) leading to extreme logits; softmax still yields stable argmax. Same behavior on CPU and NPU.
image_features ([1,512]): max_abs=0.000746, mean_abs=0.00018, cos_sim=0.999987, passed at atol=1e-3 rtol=1e-2text_features ([3,512]): max_abs=0.0304, mean_abs=0.0051 (BERT matmul nondet), passed at atol=5e-2 rtol=1e-2 — smoke consistency, not full datasetlogits ([1,3]): max_abs≈2.3e35 (relative 0.2-14% due to large scale), but argmax_agreement=1.0, probs identical [0,1,0]npu:0 (torch.npu.synchronize() before/after timing)Full comparison:
python compare_outputs.py --cpu cpu_image_features.npy --npu npu_image_features.npy --task regression --atol 1e-3 --rtol 1e-2 # PASStorch.npu.synchronize() used around each measurementassets/agent_workflow.png — workflow logs (env, download, CPU/NPU runs, validation)assets/npu_device_call.png — npu-smi info + torch.npu device + model param on npu:0assets/model_result.png — default python inference.py stdout with real logits/probs/timingAll PNGs rendered from real logs via scripts/render_xterm_evidence.mjs --style raw with prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$ (display label, not identity proof).
https://huggingface.co/microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224