Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RylanSchaeffer/8198233a445ab3373aff01b038cc4b8b to your computer and use it in GitHub Desktop.
Save RylanSchaeffer/8198233a445ab3373aff01b038cc4b8b to your computer and use it in GitHub Desktop.
Llava Vicuna7B RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:1 and cuda:0! (when checking argument for argument weight in method wrapper_CUDA__native_layer_norm)
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
from accelerate import Accelerator
import dataclasses
from enum import auto, Enum
import torch
from typing import Any, Dict, List, Optional, Tuple
from llava.model.builder import load_pretrained_model
from llava.mm_utils import get_model_name_from_path
# Model Constants
IGNORE_INDEX = -100
IMAGE_TOKEN_INDEX = -200
DEFAULT_IMAGE_TOKEN = "<image>"
DEFAULT_IMAGE_PATCH_TOKEN = "<im_patch>"
DEFAULT_IM_START_TOKEN = "<im_start>"
DEFAULT_IM_END_TOKEN = "<im_end>"
def prepare_text_prompt(user_prompt):
qs = DEFAULT_IMAGE_TOKEN + "\n" + user_prompt
conv = conv_llava_llama_2.copy()
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
return prompt
def normalize_images(images: torch.Tensor) -> torch.Tensor:
mean = torch.tensor([0.48145466, 0.4578275, 0.40821073]).to(images.device)
std = torch.tensor([0.26862954, 0.26130258, 0.27577711]).to(images.device)
images = images - mean[None, :, None, None]
images = images / std[None, :, None, None]
return images
def tokenizer_image_token(
prompt, tokenizer, image_token_index=IMAGE_TOKEN_INDEX, return_tensors=None
):
prompt_chunks = [tokenizer(chunk).input_ids for chunk in prompt.split("<image>")]
def insert_separator(X, sep):
return [ele for sublist in zip(X, [sep] * len(X)) for ele in sublist][:-1]
input_ids = []
offset = 0
if (
len(prompt_chunks) > 0
and len(prompt_chunks[0]) > 0
and prompt_chunks[0][0] == tokenizer.bos_token_id
):
offset = 1
input_ids.append(prompt_chunks[0][0])
for x in insert_separator(prompt_chunks, [image_token_index] * (offset + 1)):
input_ids.extend(x[offset:])
if return_tensors is not None:
if return_tensors == "pt":
return torch.tensor(input_ids, dtype=torch.long)
raise ValueError(f"Unsupported tensor type: {return_tensors}")
return input_ids
class SeparatorStyle(Enum):
"""Different separator style."""
SINGLE = auto()
TWO = auto()
MPT = auto()
PLAIN = auto()
LLAMA_2 = auto()
@dataclasses.dataclass
class Conversation:
"""A class that keeps all conversation history."""
system: str
roles: List[str]
messages: List[List[str]]
offset: int
sep_style: SeparatorStyle = SeparatorStyle.SINGLE
sep: str = "###"
sep2: str = None
version: str = "Unknown"
skip_next: bool = False
def get_prompt(self):
messages = self.messages
if len(messages) > 0 and type(messages[0][1]) is tuple:
messages = self.messages.copy()
init_role, init_msg = messages[0].copy()
init_msg = init_msg[0].replace("<image>", "").strip()
if "mmtag" in self.version:
messages[0] = (init_role, init_msg)
messages.insert(0, (self.roles[0], "<Image><image></Image>"))
messages.insert(1, (self.roles[1], "Received."))
else:
messages[0] = (init_role, "<image>\n" + init_msg)
if self.sep_style == SeparatorStyle.SINGLE:
ret = self.system + self.sep
for role, message in messages:
if message:
if type(message) is tuple:
message, _, _ = message
ret += role + ": " + message + self.sep
else:
ret += role + ":"
elif self.sep_style == SeparatorStyle.TWO:
seps = [self.sep, self.sep2]
ret = self.system + seps[0]
for i, (role, message) in enumerate(messages):
if message:
if type(message) is tuple:
message, _, _ = message
ret += role + ": " + message + seps[i % 2]
else:
ret += role + ":"
elif self.sep_style == SeparatorStyle.MPT:
ret = self.system + self.sep
for role, message in messages:
if message:
if type(message) is tuple:
message, _, _ = message
ret += role + message + self.sep
else:
ret += role
elif self.sep_style == SeparatorStyle.LLAMA_2:
wrap_sys = lambda msg: f"<<SYS>>\n{msg}\n<</SYS>>\n\n"
wrap_inst = lambda msg: f"[INST] {msg} [/INST]"
ret = ""
for i, (role, message) in enumerate(messages):
if i == 0:
assert message, "first message should not be none"
assert role == self.roles[0], "first message should come from user"
if message:
if type(message) is tuple:
message, _, _ = message
if i == 0:
message = wrap_sys(self.system) + message
if i % 2 == 0:
message = wrap_inst(message)
ret += self.sep + message
else:
ret += " " + message + " " + self.sep2
else:
ret += ""
ret = ret.lstrip(self.sep)
elif self.sep_style == SeparatorStyle.PLAIN:
seps = [self.sep, self.sep2]
ret = self.system
for i, (role, message) in enumerate(messages):
if message:
if type(message) is tuple:
message, _, _ = message
ret += message + seps[i % 2]
else:
ret += ""
else:
raise ValueError(f"Invalid style: {self.sep_style}")
return ret
def append_message(self, role, message):
self.messages.append([role, message])
def get_images(self, return_pil=False):
images = []
for i, (role, msg) in enumerate(self.messages[self.offset :]):
if i % 2 == 0:
if type(msg) is tuple:
import base64
from io import BytesIO
from PIL import Image
msg, image, image_process_mode = msg
if image_process_mode == "Pad":
def expand2square(pil_img, background_color=(122, 116, 104)):
width, height = pil_img.size
if width == height:
return pil_img
elif width > height:
result = Image.new(
pil_img.mode, (width, width), background_color
)
result.paste(pil_img, (0, (width - height) // 2))
return result
else:
result = Image.new(
pil_img.mode, (height, height), background_color
)
result.paste(pil_img, ((height - width) // 2, 0))
return result
image = expand2square(image)
elif image_process_mode == "Crop":
pass
elif image_process_mode == "Resize":
image = image.resize((336, 336))
else:
raise ValueError(
f"Invalid image_process_mode: {image_process_mode}"
)
max_hw, min_hw = max(image.size), min(image.size)
aspect_ratio = max_hw / min_hw
max_len, min_len = 800, 400
shortest_edge = int(min(max_len / aspect_ratio, min_len, min_hw))
longest_edge = int(shortest_edge * aspect_ratio)
W, H = image.size
if H > W:
H, W = longest_edge, shortest_edge
else:
H, W = shortest_edge, longest_edge
image = image.resize((W, H))
if return_pil:
images.append(image)
else:
buffered = BytesIO()
image.save(buffered, format="PNG")
img_b64_str = base64.b64encode(buffered.getvalue()).decode()
images.append(img_b64_str)
return images
def to_gradio_chatbot(self):
ret = []
for i, (role, msg) in enumerate(self.messages[self.offset :]):
if i % 2 == 0:
if type(msg) is tuple:
import base64
from io import BytesIO
msg, image, image_process_mode = msg
max_hw, min_hw = max(image.size), min(image.size)
aspect_ratio = max_hw / min_hw
max_len, min_len = 800, 400
shortest_edge = int(min(max_len / aspect_ratio, min_len, min_hw))
longest_edge = int(shortest_edge * aspect_ratio)
W, H = image.size
if H > W:
H, W = longest_edge, shortest_edge
else:
H, W = shortest_edge, longest_edge
image = image.resize((W, H))
buffered = BytesIO()
image.save(buffered, format="JPEG")
img_b64_str = base64.b64encode(buffered.getvalue()).decode()
img_str = f'<img src="data:image/png;base64,{img_b64_str}" alt="user upload image" />'
ret.append([img_str, None])
msg = msg.replace("<image>", "").strip()
if len(msg) > 0:
ret.append([msg, None])
else:
ret.append([msg, None])
else:
ret[-1][-1] = msg
return ret
def copy(self):
return Conversation(
system=self.system,
roles=self.roles,
messages=[[x, y] for x, y in self.messages],
offset=self.offset,
sep_style=self.sep_style,
sep=self.sep,
sep2=self.sep2,
version=self.version,
)
def dict(self):
if len(self.get_images()) > 0:
return {
"system": self.system,
"roles": self.roles,
"messages": [
[x, y[0] if type(y) is tuple else y] for x, y in self.messages
],
"offset": self.offset,
"sep": self.sep,
"sep2": self.sep2,
}
return {
"system": self.system,
"roles": self.roles,
"messages": self.messages,
"offset": self.offset,
"sep": self.sep,
"sep2": self.sep2,
}
conv_vicuna_v0 = Conversation(
system="A chat between a curious human and an artificial intelligence assistant. "
"The assistant gives helpful, detailed, and polite answers to the human's questions.",
roles=("Human", "Assistant"),
messages=(
(
"Human",
"What are the key differences between renewable and non-renewable energy sources?",
),
(
"Assistant",
"Renewable energy sources are those that can be replenished naturally in a relatively "
"short amount of time, such as solar, wind, hydro, geothermal, and biomass. "
"Non-renewable energy sources, on the other hand, are finite and will eventually be "
"depleted, such as coal, oil, and natural gas. Here are some key differences between "
"renewable and non-renewable energy sources:\n"
"1. Availability: Renewable energy sources are virtually inexhaustible, while non-renewable "
"energy sources are finite and will eventually run out.\n"
"2. Environmental impact: Renewable energy sources have a much lower environmental impact "
"than non-renewable sources, which can lead to air and water pollution, greenhouse gas emissions, "
"and other negative effects.\n"
"3. Cost: Renewable energy sources can be more expensive to initially set up, but they typically "
"have lower operational costs than non-renewable sources.\n"
"4. Reliability: Renewable energy sources are often more reliable and can be used in more remote "
"locations than non-renewable sources.\n"
"5. Flexibility: Renewable energy sources are often more flexible and can be adapted to different "
"situations and needs, while non-renewable sources are more rigid and inflexible.\n"
"6. Sustainability: Renewable energy sources are more sustainable over the long term, while "
"non-renewable sources are not, and their depletion can lead to economic and social instability.\n",
),
),
offset=2,
sep_style=SeparatorStyle.SINGLE,
sep="###",
)
conv_vicuna_v1 = Conversation(
system="A chat between a curious user and an artificial intelligence assistant. "
"The assistant gives helpful, detailed, and polite answers to the user's questions.",
roles=("USER", "ASSISTANT"),
version="v1",
messages=(),
offset=0,
sep_style=SeparatorStyle.TWO,
sep=" ",
sep2="</s>",
)
conv_llama_2 = Conversation(
system="""You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.""",
roles=("USER", "ASSISTANT"),
version="llama_v2",
messages=(),
offset=0,
sep_style=SeparatorStyle.LLAMA_2,
sep="<s>",
sep2="</s>",
)
conv_llava_llama_2 = Conversation(
system="You are a helpful language and vision assistant. "
"You are able to understand the visual content that the user provides, "
"and assist the user with a variety of tasks using natural language.",
roles=("USER", "ASSISTANT"),
version="llama_v2",
messages=(),
offset=0,
sep_style=SeparatorStyle.LLAMA_2,
sep="<s>",
sep2="</s>",
)
conv_mpt = Conversation(
system="""<|im_start|>system
A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.""",
roles=("<|im_start|>user\n", "<|im_start|>assistant\n"),
version="mpt",
messages=(),
offset=0,
sep_style=SeparatorStyle.MPT,
sep="<|im_end|>",
)
conv_llava_plain = Conversation(
system="",
roles=("", ""),
messages=(),
offset=0,
sep_style=SeparatorStyle.PLAIN,
sep="\n",
)
conv_llava_v0 = Conversation(
system="A chat between a curious human and an artificial intelligence assistant. "
"The assistant gives helpful, detailed, and polite answers to the human's questions.",
roles=("Human", "Assistant"),
messages=(("Human", "Hi!"), ("Assistant", "Hi there! How can I help you today?")),
offset=2,
sep_style=SeparatorStyle.SINGLE,
sep="###",
)
conv_llava_v0_mmtag = Conversation(
system="A chat between a curious user and an artificial intelligence assistant. "
"The assistant is able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language."
"The visual content will be provided with the following format: <Image>visual content</Image>.",
roles=("Human", "Assistant"),
messages=(),
offset=0,
sep_style=SeparatorStyle.SINGLE,
sep="###",
version="v0_mmtag",
)
conv_llava_v1 = Conversation(
system="A chat between a curious human and an artificial intelligence assistant. "
"The assistant gives helpful, detailed, and polite answers to the human's questions.",
roles=("USER", "ASSISTANT"),
version="v1",
messages=(),
offset=0,
sep_style=SeparatorStyle.TWO,
sep=" ",
sep2="</s>",
)
conv_llava_v1_mmtag = Conversation(
system="A chat between a curious user and an artificial intelligence assistant. "
"The assistant is able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language."
"The visual content will be provided with the following format: <Image>visual content</Image>.",
roles=("USER", "ASSISTANT"),
messages=(),
offset=0,
sep_style=SeparatorStyle.TWO,
sep=" ",
sep2="</s>",
version="v1_mmtag",
)
default_conversation = conv_vicuna_v0
conversation_templates = {
"default": conv_vicuna_v0,
"v0": conv_vicuna_v0,
"v1": conv_vicuna_v1,
"vicuna_v1": conv_vicuna_v1,
"llama_2": conv_llama_2,
"v0_plain": conv_llava_plain,
"llava_v0": conv_llava_v0,
"v0_mmtag": conv_llava_v0_mmtag,
"llava_v1": conv_llava_v1,
"v1_mmtag": conv_llava_v1_mmtag,
"llava_llama_2": conv_llava_llama_2,
"mpt": conv_mpt,
}
class LlavaVisionLanguageModel(torch.nn.Module):
def __init__(
self,
huggingface_name: str = "llava-hf/llava-1.5-7b-hf",
generation_kwargs: Dict[str, Any] = None,
accelerator: Optional[Accelerator] = None,
):
super(LlavaVisionLanguageModel, self).__init__()
self.huggingface_name = huggingface_name
self.generation_kwargs = generation_kwargs
if self.huggingface_name == "llava-hf/llava-1.5-7b-hf":
self.conv_template_name = "vicuna_v1"
elif self.huggingface_name == "liuhaotian/llava-v1.6-34b":
# https://github.com/haotian-liu/LLaVA/issues/1078
self.conv_template_name = "chatml_direct"
elif self.huggingface_name == "liuhaotian/llava-v1.6-vicuna-7b":
self.conv_template_name = "vicuna_v1"
elif self.huggingface_name == "liuhaotian/llava-v1.6-vicuna-13b":
self.conv_template_name = "vicuna_v1"
else:
self.conv_template_name = "default"
self.conv_template = conversation_templates[self.conv_template_name]
# self.device = torch.device(
# f"cuda:{self.gpu_id}" if torch.cuda.is_available() else "cpu"
# )
self.accelerator = accelerator
if accelerator is not None:
self.device = accelerator.device
else:
self.device = (
torch.device("cuda")
if torch.cuda.is_available()
else torch.device("cpu")
)
(
self.tokenizer,
self.model,
self.image_processor,
self.context_len,
) = load_pretrained_model(
model_path=self.huggingface_name,
model_base=None,
model_name=get_model_name_from_path(self.huggingface_name),
# device_map={"device_map": self.gpu_id},
)
self.text_prompt_template = prepare_text_prompt("")
print(self.text_prompt_template)
def convert_prompts_and_maybe_targets_to_input_ids_and_attention_mask(
self,
prompts: List[str],
targets: Optional[List[str]] = None,
) -> Dict[str, torch.Tensor]:
if targets is None:
targets = [None for _ in range(len(prompts))]
prompts_with_image_tokens = [
DEFAULT_IM_START_TOKEN
+ DEFAULT_IMAGE_TOKEN
+ DEFAULT_IM_END_TOKEN
+ "\n"
+ prompt
for prompt in prompts
]
templated_prompts = []
for prompt, target in zip(prompts_with_image_tokens, targets):
conv = self.conv_template.copy()
conv.append_message(conv.roles[0], prompt)
conv.append_message(conv.roles[1], target)
templated_prompt = conv.get_prompt()
templated_prompts.append(templated_prompt)
input_ids_list: List[List[int]] = [
tokenizer_image_token(
templated_prompt,
self.tokenizer,
IMAGE_TOKEN_INDEX,
)
for templated_prompt in templated_prompts
]
# Pad all input_ids to be the same length using the tokenizer's padding token.
attention_mask = []
max_length = max([len(input_ids) for input_ids in input_ids_list])
for idx, input_ids in enumerate(input_ids_list):
padding_length = max_length - len(input_ids)
attention_mask.append(
[1 for _ in range(max_length - padding_length)]
+ [0 for _ in range(padding_length)]
)
input_ids.extend(
[self.tokenizer.pad_token_id for _ in range(padding_length)]
)
input_ids = torch.tensor(input_ids_list)
attention_mask = torch.tensor(attention_mask)
results = {
"input_ids": input_ids,
"attention_mask": attention_mask,
}
if targets[0] is not None:
labels = input_ids.clone()
last_nonpadding_indices = torch.argmin((labels != 0).float(), axis=1)
# Find the last non-zero token. Then set labels to ignore for anything
# before and before the targets (plus two).
tokenized_labels = self.tokenizer(targets).input_ids
for batch_idx, (last_nonpadding_idx, tokenized_label) in enumerate(
zip(last_nonpadding_indices, tokenized_labels)
):
target_start_idx = last_nonpadding_idx - len(tokenized_label) - 1
labels[batch_idx, :target_start_idx] = IGNORE_INDEX
# Also mask out the padding tokens.
labels[labels == 0] = IGNORE_INDEX
results["labels"] = labels
return results
@torch.inference_mode()
def generate(self, images: torch.Tensor, prompts: List[str]) -> List[str]:
# Based on https://github.com/haotian-liu/LLaVA/blob/main/llava/eval/run_llava.py#L50
# and also based on https://github.com/haotian-liu/LLaVA/blob/main/llava/eval/model_vqa.py.
image_pixel_values = self.image_processor(
images, do_rescale=False, return_tensors="pt"
)["pixel_values"].to(self.device)
input_ids = (
self.convert_prompts_and_maybe_targets_to_input_ids_and_attention_mask(
prompts=prompts,
targets=None,
)["input_ids"].to(self.device)
)
self.model = self.model.to(self.device)
generated_ids = self.model.generate(
input_ids.to(self.device),
images=image_pixel_values.half().to(self.device),
do_sample=True if self.generation_kwargs["temperature"] > 0 else False,
cache_position=None,
**self.generation_kwargs,
)
model_generations = self.tokenizer.batch_decode(
generated_ids, skip_special_tokens=True
)
return model_generations
accelerator = Accelerator()
llava_vicuna_7b_vlm = LlavaVisionLanguageModel(
huggingface_name="liuhaotian/llava-v1.5-7b",
accelerator=accelerator,
generation_kwargs={
"temperature": 0.1,
"top_p": 0.9,
"max_new_tokens": 100,
"min_new_tokens": 5,
},
)
llava_vicuna_7b_vlm = accelerator.prepare(llava_vicuna_7b_vlm)
images = torch.rand(1, 3, 336, 336)
prompts = ["What is this image?"]
generated_text = llava_vicuna_7b_vlm.generate(images=images, prompts=prompts)
print("Llava Vicuna 7B Generated text: ", generated_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment