Views
No views yet
| Module | Weight Shape Change | Affects Attention Calculation? |
|---|---|---|
| embed_tokens | [vocab_size+2, hidden_size] | ❌ No |
| lm_head | [vocab_size+2, hidden_size] | ❌ No |
| All attention layers | No shape change | ✅ Completely unchanged |
<image> or <pad>.model.resize_token_embeddings(config.text_config.vocab_size + 2, pad_shape)1from transformers import LlavaForConditionalGeneration, AutoProcessor
2import torch
3
4model_path = "chaoyinshe/llava-med-v1.5-mistral-7b-hf"
5
6model = LlavaForConditionalGeneration.from_pretrained(
7 model_path,
8 torch_dtype=torch.bfloat16,
9 attn_implementation="flash_attention_2", # requires FA2
10 device_map="auto" # multi-GPU ready
11)
12
13processor = AutoProcessor.from_pretrained(model_path)
14
15# Example inference
16messages = [
17 {
18 "role": "user",
19 "content": [
20 {"type": "image"},
21 {"type": "text", "text": "What is the main finding in this chest X-ray?"}
22 ]
23 }
24]
25
26prompt = processor.tokenizer.apply_chat_template(
27 messages, tokenize=False, add_generation_prompt=True
28)
29
30inputs = processor(
31 images=[image], text=prompt, return_tensors="pt"
32).to(model.device, torch.bfloat16)
33
34with torch.inference_mode():
35 out = model.generate(**inputs, max_new_tokens=256)
36
37print(processor.decode(out[0], skip_special_tokens=True))