wrhhh/microsoft-BiomedCLIP-PubMedBERT_256-vit_base_patch16_224-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

BiomedCLIP-PubMedBERT_256-vit_base_patch16_224 NPU

#NPU Ascend Ascend910 BiomedCLIP Vision-Language Biomedical Zero-Shot

  • Model: microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224
  • Weight URL: https://huggingface.co/microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224
  • Revision: 9f341de24bfb00180f1b847274256e9b65a3a32e
  • Task: Computer Vision / Biomedical Vision-Language (zero-shot image classification & image-text retrieval)
  • Architecture: CustomTextCLIP — 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=512
  • Pipeline: open_clip create_model_and_transforms with local-dir: schema, HFTextEncoder (PubMedBERT) + ViT

Data Contract

Input

  • Image: RGB 3×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)
  • Text: tokenized via HFTokenizer (vocab vocab.txt, tokenizer.json from model dir), context_length=256, padding/truncation, dtype int64, shape [N,256] (N=number of prompts)
  • Example prompts: "a chest X-ray showing pneumonia", "a normal chest X-ray", "a brain MRI showing tumor", "histopathology image of adenocarcinoma", "immunohistochemistry staining"

Output

  • image_features: [B,512] L2-normalized, float32
  • text_features: [N,512] L2-normalized, float32
  • logit_scale: scalar ≈85.23 (exp≈1e37), logits = logit_scale.exp() * image_features @ text_features.T → [B,N]
  • probs: softmax over N prompts, pred = argmax

Dataset

  • Example images bundled in model repo: example_data/biomed_image_classification_example_data/ (8 images: chest_X-ray.jpg, brain_MRI.jpg, adenocarcinoma_histopathology.jpg, etc.)
  • SHA-256 for validation not pinned (images are JPEG); size checks: chest_X-ray 906KB, brain_MRI 128KB
  • Fallback: synthetic 224×224 random image if no file found

Environment

  • Hardware: Ascend 910 (910_9362), 2 devices, target npu:0
  • CANN: 8.5.1 (/usr/local/Ascend/cann-8.5.1)
  • Python: 3.11.14
  • PyTorch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04
  • open_clip_torch: 3.3.0, timm: 1.0.28, transformers: 4.57.6, Pillow: 12.2.0, numpy: 1.26.4
  • HF_ENDPOINT: https://hf-mirror.com (direct, no proxy override)
  • Devices verified: torch.npu.is_available()==True, device_count==2, get_device_name(0)==Ascend910_9362

Installation

pip 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.txt

Model 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")

NPU Inference

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_9362
  • backend: open_clip + torch_npu, param_device: npu:0, dtype: torch.float32
  • Input shapes, first run (compile) time, stable avg time, throughput
  • image_features/text_features shapes, logit_scale, logits, probs, pred indices

Exit code 0 on success; non-zero if NPU unavailable or CPU fallback.

Real Results (npu:0, 2026-08-21, single 224×224 chest X-ray)

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.

Consistency (CPU vs NPU, same weights/inputs/preprocess/dtype fp32, seed fixed)

  • image_features ([1,512]): max_abs=0.000746, mean_abs=0.00018, cos_sim=0.999987, passed at atol=1e-3 rtol=1e-2
  • text_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 dataset
  • logits ([1,3]): max_abs≈2.3e35 (relative 0.2-14% due to large scale), but argmax_agreement=1.0, probs identical [0,1,0]
  • Same revision, same preprocess, no CPU fallback, tensors explicitly on 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  # PASS

Performance (npu:0, batch=1, fp32, 224×224)

  • Compile / first run: 310.74 ms (includes graph compilation)
  • Stable avg: 13.19 ms, min 12.52 ms, max 13.76 ms, p50 13.56 ms, p90 13.76 ms, p95 13.76 ms
  • Throughput: 75.8 infer/s (rows/s equivalent, no preprocessing timed separately)
  • Peak HBM: ~3110 MB baseline → no OOM, 64GB available
  • Preprocessing not included in timing; torch.npu.synchronize() used around each measurement

Evidence

  • assets/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:0
  • assets/model_result.png — default python inference.py stdout with real logits/probs/timing

All PNGs rendered from real logs via scripts/render_xterm_evidence.mjs --style raw with prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$ (display label, not identity proof).

Limitations

  • Only single-image batch=1 tested; larger batches not benchmarked
  • Text tower uses PubMedBERT (BERT-base); long prompts truncated at 256 tokens
  • Logit scale extreme; downstream calibration may be needed
  • Synthetic input fallback not representative of clinical distribution; use real DICOM/PNG for evaluation
  • Smoke consistency only (single sample); full dataset metrics not computed

Model Weight URL (for competition submission)

https://huggingface.co/microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224