How alibaba-damo-academy/clinfusion works
Purpose and Entry Points
ClinFusion is a Python medical multimodal LLM package for text, 2D medical images, and 3D NIfTI volumes. The operational entry points are inference/evaluation launch scripts, which drive the evaluation coordinator at eval/Evaluation/main.py and generation rollout at eval/InferenceEngine/rollout.py. Inputs and outputs are JSONL, preserving source fields and adding model generations.
Multimodal Model Assembly
The ClinFusion-specific model implementation adapts Qwen VL architectures with multiple medical vision encoders:
• Qwen3-VL multi-encoder model: custom_model/modeling_multi_enc_qwen3_vl.py
• Qwen2.5-VL variant: custom_model/modeling_multi_enc_qwen2_5_vl.py
• model loading/registration: custom_model/loader.py
• shared encoder fusion utilities: custom_model/multi_enc_utils.py
The repository expects externally downloaded Qwen base-model and ClinFusion checkpoint weights rather than containing weights itself.
Vision Encoding and Medical Input Processing
The visual pipeline combines external DINOv2 and OpenCLIP/ConvNeXt encoders with an in-repo medical ViT. custom_model/openclip_encoder.py wraps the OpenCLIP encoder; the medical ViT is constructed through custom_model/medical_vit/create_medical_vit.py and implemented in custom_model/medical_vit/mae_v2.py. Image/volume transformation and multimodal processor behavior are defined in custom_model/medical_image_preprocessing.py and custom_model/image_processing_multi_enc_qwen2_vl.py.
A key modality boundary is NIfTI-to-2D-slice conversion before model processing, allowing 3D CT/MRI inputs to enter the vision-language generation path.
Inference Engine and Model Abstraction
eval/InferenceEngine/rollout.py orchestrates distributed/local generation over JSONL records. Model selection is isolated behind wrapper resolution in eval/InferenceEngine/models/get_wrapper.py. Supported routes include ClinFusion/custom local checkpoints (eval/InferenceEngine/models/custom.py), Qwen VL (eval/InferenceEngine/models/qwenvl.py), Hulu-Med, and API-backed models (eval/InferenceEngine/models/api.py).
Custom checkpoints implement the adapter contract in custom_model/medevalkit_adapter_qwen3_vl.py; this separates evaluation infrastructure from model-specific loading, preprocessing, and generation APIs.
Evaluation Pipeline
eval/Evaluation/main.py loads a YAML-selected dataset, model wrapper, evaluator, and aggregator. Evaluator branches represent major task classes:
• general 2D/text tasks: eval/Evaluation/evaluators/evaluator_general.py
• volumetric tasks: eval/Evaluation/evaluators/evaluator_3d.py
• instruction-following tasks: eval/Evaluation/evaluators/evaluator_if.py
Their outputs feed pluggable metric/report aggregation in eval/Evaluation/aggregators/aggregator.py, eval/Evaluation/aggregators/aggregator_if.py, and eval/Evaluation/aggregators/aggregator_staging.py.
Instruction-Following Tagging Boundary
Instruction-following evaluation has a dedicated prompt/tag pipeline: eval/tagging/prepare_prompts.py prepares tagged prompts, while eval/tagging/parse_and_validate_tags.py parses and validates generated tags. This separates structured compliance/factualness assessment from normal generation and task scoring.
Runtime, Infrastructure, and External Services
The primary runtime is Python 3.11 with PyTorch/CUDA and a prebuilt Flash-Attention wheel, provisioned by install_from_scratch.sh and requirements.txt. Local execution supports single- and multi-node GPU workers through launch scripts such as eval/tools/launch_inference.sh and eval/tools/launch_eval.sh. External dependencies include Hugging Face model hosting/downloads, DINOv2, OpenCLIP ConvNeXt, Qwen VL model weights, and configurable API model endpoints/credentials via eval/InferenceEngine/models/api.py.
Main entry points: rollout.py Generation rollout · get_wrapper.py Wrapper resolution · medevalkit_adapter_qwen3_vl.py ClinFusion adapter · main.py Evaluation coordinator
Components 20
Runtime & Entry Points
- GPU launch scripts · Launch local or distributed inference and evaluation.
eval/tools/launch_inference.sh
Evaluation System
- Evaluation coordinator · Selects datasets, wrappers, evaluators, and aggregators from YAML.
eval/Evaluation/main.py - Task evaluators · General, volumetric, and instruction-following task evaluation.
eval/Evaluation/evaluators/evaluator_general.py - Metrics & reports · Produces task-specific metric and report outputs.
eval/Evaluation/aggregators/aggregator.py - Tagged prompt preparation
eval/tagging/prepare_prompts.py - Tag parsing & validation · Separates structured compliance and factualness assessment.
eval/tagging/parse_and_validate_tags.py
Inference Engine
- Generation rollout · Runs local or distributed generation while preserving source records.
eval/InferenceEngine/rollout.py - Wrapper resolution · Selects custom, Qwen VL, Hulu-Med, or API model routes.
eval/InferenceEngine/models/get_wrapper.py - Custom checkpoint wrapper · Bridges evaluation infrastructure to ClinFusion loading and generation.
eval/InferenceEngine/models/custom.py - API model wrapper · Uses configured endpoint credentials for API-backed models.
eval/InferenceEngine/models/api.py
ClinFusion Model
- ClinFusion adapter · Provides model-specific loading, preprocessing, and generation APIs.
custom_model/medevalkit_adapter_qwen3_vl.py - Model loader & registration
custom_model/loader.py - Multi-encoder Qwen VL · ClinFusion adapts Qwen VL with fused medical vision encoders.
custom_model/modeling_multi_enc_qwen3_vl.py - Encoder fusion utilities
custom_model/multi_enc_utils.py
Medical Vision Pipeline
- Medical input processor · Transforms images and converts NIfTI volumes into 2D slices.
custom_model/medical_image_preprocessing.py - Multi-encoder processor
custom_model/image_processing_multi_enc_qwen2_vl.py - OpenCLIP / ConvNeXt encoder
custom_model/openclip_encoder.py - Medical ViT · Constructed medical image encoder used alongside external encoders.
custom_model/medical_vit/create_medical_vit.py
Other components
- External models & weights · Hugging Face Qwen and ClinFusion checkpoints plus DINOv2 and OpenCLIP weights.
- JSONL datasets & generations · Input records are preserved and augmented with generations.
Connections 25
- GPU launch scripts → Evaluation coordinator: launches
- GPU launch scripts → Generation rollout: launches
- Evaluation coordinator → Wrapper resolution: selects model
- Evaluation coordinator → Task evaluators: dispatches tasks
- Task evaluators → Metrics & reports: scores feed
- Task evaluators → Tagged prompt preparation: instruction tasks
- Tagged prompt preparation → Generation rollout: tagged requests
- Generation rollout → Tag parsing & validation: generated tags
- Tag parsing & validation → Metrics & reports: validated assessment
- JSONL datasets & generations → Generation rollout: input records
- Generation rollout → JSONL datasets & generations: augmented outputs
- Generation rollout → Wrapper resolution: resolves model
- Wrapper resolution → Custom checkpoint wrapper: local ClinFusion route
- Wrapper resolution → API model wrapper: API route
- Custom checkpoint wrapper → ClinFusion adapter: uses
- ClinFusion adapter → Model loader & registration: loads
- Model loader & registration → Multi-encoder Qwen VL: registers
- Medical input processor → Multi-encoder processor: prepared slices and images
- Multi-encoder processor → ClinFusion adapter: multimodal inputs
- ClinFusion adapter → Medical input processor: preprocesses
- Multi-encoder Qwen VL → Encoder fusion utilities: fuses encoders
- Encoder fusion utilities → OpenCLIP / ConvNeXt encoder: uses features
- Encoder fusion utilities → Medical ViT: uses features
- External models & weights → Model loader & registration: base and checkpoint weights
- External models & weights → OpenCLIP / ConvNeXt encoder: encoder weights