Views
No views yet
huggingface_hub:1from huggingface_hub import hf_hub_download
2
3# Download Qwen2-VL model trained on M3CoT
4qwen_m3cot_path = hf_hub_download("ModalityDance/IVTLR_QWEN_M3COT", "model.pth")
5
6# Download Qwen2-VL model trained on ScienceQA
7qwen_sqa_path = hf_hub_download("ModalityDance/IVTLR_QWEN_SQA", "model.pth")image and text with your own input.1from transformers import AutoTokenizer, AutoProcessor, Qwen2VLForConditionalGeneration
2from qwen_ivtlr import IVTLR
3from qwen_vl_utils import process_vision_info
4from peft import LoraConfig, get_peft_model
5from huggingface_hub import hf_hub_download
6import torch
7
8device = "cuda" if torch.cuda.is_available() else "cpu"
9
10# Download model
11checkpoint_path = hf_hub_download("ModalityDance/IVTLR_QWEN_M3COT", "model.pth")
12
13# Load processor and tokenizer
14processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")
15tokenizer = AutoTokenizer.from_pretrained(
16 "Qwen/Qwen2-VL-7B-Instruct",
17 use_fast=False,
18 trust_remote_code=True,
19 padding_side="right"
20)
21tokenizer.add_special_tokens({
22 "additional_special_tokens": ["<|start-latent|>", "<|end-latent|>", "<|latent|>"]
23})
24
25# Load base model with LoRA
26base_model = Qwen2VLForConditionalGeneration.from_pretrained(
27 "Qwen/Qwen2-VL-7B-Instruct",
28 device_map="cuda",
29 torch_dtype=torch.bfloat16,
30 trust_remote_code=True,
31 attn_implementation="eager"
32)
33base_model.resize_token_embeddings(len(tokenizer))
34processor.tokenizer = tokenizer
35
36lora_config = LoraConfig(
37 task_type="CAUSAL_LM",
38 target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
39 r=64, lora_alpha=16, lora_dropout=0.05, bias="none", inference_mode=False
40)
41base_model = get_peft_model(base_model, lora_config)
42
43# Create IVTLR model
44latent_id = tokenizer.convert_tokens_to_ids("<|latent|>")
45start_id = tokenizer.convert_tokens_to_ids("<|start-latent|>")
46end_id = tokenizer.convert_tokens_to_ids("<|end-latent|>")
47image_token_id = tokenizer.convert_tokens_to_ids(processor.image_token)
48visual_start_id = tokenizer.convert_tokens_to_ids("<|vision_start|>")
49visual_end_id = tokenizer.convert_tokens_to_ids("<|vision_end|>")
50
51model = IVTLR(
52 base_model,
53 latent_token_id=latent_id,
54 start_latent_id=start_id,
55 end_latent_id=end_id,
56 eos_token_id=tokenizer.eos_token_id,
57 image_token_id=image_token_id,
58 visual_start_id=visual_start_id,
59 visual_end_id=visual_end_id
60)
61
62# Load checkpoint
63state_dict = torch.load(checkpoint_path, map_location="cpu")
64if any(k.startswith("module.") for k in state_dict.keys()):
65 state_dict = {k.replace("module.", ""): v for k, v in state_dict.items()}
66model.load_state_dict(state_dict, strict=True)
67model = model.to(device)
68model.eval()
69
70# ============ Inference ============
71# Replace with your own image and text
72image = "your_image.jpg" # PIL Image or path to image
73text = "Your question here"
74
75messages = [{
76 "role": "user",
77 "content": [
78 {"type": "image", "image": image, "resized_height": 280, "resized_width": 280},
79 {"type": "text", "text": text}
80 ]
81}]
82
83prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
84prompt = prompt + "<|latent|>" * 3 # Add latent tokens
85
86image_inputs, video_inputs = process_vision_info(messages)
87inputs = processor(
88 text=[prompt],
89 images=image_inputs,
90 videos=video_inputs,
91 padding=True,
92 return_tensors="pt"
93).to(device)
94
95with torch.no_grad():
96 outputs = model.generate(
97 input_ids=inputs["input_ids"],
98 attention_mask=inputs["attention_mask"],
99 pixel_values=inputs["pixel_values"],
100 image_grid_thw=inputs["image_grid_thw"],
101 max_new_tokens=512
102 )
103
104response = processor.decode(outputs[0], skip_special_tokens=True)
105print(response)1@article{chen2025reasoning,
2 title={Reasoning in the dark: Interleaved vision-text reasoning in latent space},
3 author={Chen, Chao and Ma, Zhixin and Li, Yongqi and Hu, Yupeng and Wei, Yinwei and Li, Wenjie and Nie, Liqiang},
4 journal={arXiv preprint arXiv:2510.12603},
5 year={2025}
6}