diff --git a/.gitignore b/.gitignore index a254579f2..3b58c84f8 100755 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,6 @@ temp profile_default/ ipython_config.py logs/ -scripts/ wandb/ SimSun.ttf submissions/ @@ -30,6 +29,7 @@ cache_dir ckpt pretrained/ LLaVA/ +LLaVA-NeXT/ *logs *.isorted temp/ @@ -41,6 +41,5 @@ Video-MME/ VATEX/ lmms_eval/tasks/vatex/__pycache__/utils.cpython-310.pyc lmms_eval/tasks/mlvu/__pycache__/utils.cpython-310.pyc - -scripts/ -.env \ No newline at end of file +.env +lm-evaluation-harness/ diff --git a/lmms-eval.reqs b/lmms-eval.reqs new file mode 100644 index 000000000..6830faa7d --- /dev/null +++ b/lmms-eval.reqs @@ -0,0 +1,185 @@ +accelerate 1.2.0 +aiofiles 23.2.1 +aiohappyeyeballs 2.4.4 +aiohttp 3.11.10 +aiosignal 1.3.1 +altair 5.5.0 +annotated-types 0.7.0 +anyio 4.7.0 +async-timeout 5.0.1 +attrs 24.2.0 +av 14.0.1 +bitsandbytes 0.45.0 +black 24.1.0 +certifi 2024.8.30 +cfgv 3.4.0 +chardet 5.2.0 +charset-normalizer 3.4.0 +click 8.1.7 +colorama 0.4.6 +contourpy 1.3.1 +cycler 0.12.1 +DataProperty 1.0.1 +datasets 2.20.0 +decord 0.6.0 +dill 0.3.7 +distlib 0.3.9 +distro 1.9.0 +docker-pycreds 0.4.0 +einops 0.6.1 +einops-exts 0.0.4 +et_xmlfile 2.0.0 +evaluate 0.4.3 +exceptiongroup 1.2.2 +fastapi 0.115.6 +ffmpy 0.4.0 +filelock 3.16.1 +fonttools 4.55.2 +frozenlist 1.5.0 +fsspec 2023.10.0 +ftfy 6.3.1 +gitdb 4.0.11 +GitPython 3.1.43 +gradio 4.16.0 +gradio_client 0.8.1 +h11 0.14.0 +hf_transfer 0.1.8 +httpcore 0.16.3 +httpx 0.24.0 +huggingface-hub 0.26.5 +identify 2.6.3 +idna 3.10 +importlib_resources 6.4.5 +isort 5.13.2 +Jinja2 3.1.4 +jiter 0.8.2 +joblib 1.4.2 +jsonlines 4.0.0 +jsonschema 4.23.0 +jsonschema-specifications 2024.10.1 +kiwisolver 1.4.7 +latex2mathml 3.77.0 +llava 1.2.2.post1 /lustre/fshomisc/home/rech/genrce01/ued79zb/repos/LLaVA +lmms_eval 0.3.0 /lustre/fshomisc/home/rech/genrce01/ued79zb/repos/lmms-eval +loguru 0.7.3 +lxml 5.3.0 +markdown-it-py 3.0.0 +markdown2 2.5.1 +MarkupSafe 2.1.5 +matplotlib 3.9.3 +mbstrdecoder 1.1.3 +mdurl 0.1.2 +mpmath 1.3.0 +multidict 6.1.0 +multiprocess 0.70.15 +mypy-extensions 1.0.0 +narwhals 1.17.0 +networkx 3.4.2 +nltk 3.9.1 +nodeenv 1.9.1 +numexpr 2.10.2 +numpy 1.26.4 +nvidia-cublas-cu12 12.1.3.1 +nvidia-cuda-cupti-cu12 12.1.105 +nvidia-cuda-nvrtc-cu12 12.1.105 +nvidia-cuda-runtime-cu12 12.1.105 +nvidia-cudnn-cu12 8.9.2.26 +nvidia-cufft-cu12 11.0.2.54 +nvidia-curand-cu12 10.3.2.106 +nvidia-cusolver-cu12 11.4.5.107 +nvidia-cusparse-cu12 12.1.0.106 +nvidia-nccl-cu12 2.18.1 +nvidia-nvjitlink-cu12 12.4.127 +nvidia-nvtx-cu12 12.1.105 +openai 1.57.2 +opencv-python-headless 4.10.0.84 +openpyxl 3.1.5 +orjson 3.10.12 +packaging 24.2 +pandas 2.2.3 +pathspec 0.12.1 +pathvalidate 3.2.1 +peft 0.14.0 +pillow 10.4.0 +pip 24.2 +platformdirs 4.3.6 +portalocker 3.0.0 +pre_commit 4.0.1 +propcache 0.2.1 +protobuf 3.20.0 +psutil 6.1.0 +pyarrow 18.1.0 +pyarrow-hotfix 0.6 +pybind11 2.13.6 +pycocoevalcap 1.2 +pycocotools 2.0.8 +pydantic 2.10.3 +pydantic_core 2.27.1 +pydub 0.25.1 +Pygments 2.18.0 +pyparsing 3.2.0 +pytablewriter 1.2.0 +python-dateutil 2.9.0.post0 +python-multipart 0.0.19 +pytz 2024.2 +PyYAML 6.0.2 +referencing 0.35.1 +regex 2024.11.6 +requests 2.32.3 +rfc3986 1.5.0 +rich 13.9.4 +rpds-py 0.22.3 +ruff 0.8.2 +sacrebleu 2.4.3 +safetensors 0.4.5 +scikit-learn 1.2.2 +scipy 1.14.1 +semantic-version 2.10.0 +sentence-transformers 3.3.1 +sentencepiece 0.1.99 +sentry-sdk 2.19.2 +setproctitle 1.3.4 +setuptools 75.1.0 +shellingham 1.5.4 +shortuuid 1.0.13 +six 1.17.0 +smmap 5.0.1 +sniffio 1.3.1 +sqlitedict 2.1.0 +starlette 0.41.3 +svgwrite 1.4.3 +sympy 1.13.1 +tabledata 1.3.3 +tabulate 0.9.0 +tcolorpy 0.1.6 +tenacity 8.3.0 +threadpoolctl 3.5.0 +tiktoken 0.8.0 +timm 0.6.13 +tokenizers 0.21.0 +tomli 2.2.1 +tomlkit 0.12.0 +torch 2.1.2 +torchvision 0.16.2 +tqdm 4.67.1 +tqdm-multiprocess 0.0.11 +transformers 4.47.0 +transformers-stream-generator 0.0.5 +triton 2.1.0 +typepy 1.3.2 +typer 0.15.1 +typing_extensions 4.12.2 +tzdata 2024.2 +urllib3 2.2.3 +uvicorn 0.32.1 +virtualenv 20.28.0 +wandb 0.19.0 +wavedrom 2.0.3.post3 +wcwidth 0.2.13 +websockets 11.0.3 +wheel 0.44.0 +xxhash 3.5.0 +yarl 1.18.3 +yt-dlp 2024.12.6 +zss 1.2.0 +zstandard 0.23.0 \ No newline at end of file diff --git a/lmms_eval/evaluator.py b/lmms_eval/evaluator.py index 8776eed1a..ce7cb1499 100755 --- a/lmms_eval/evaluator.py +++ b/lmms_eval/evaluator.py @@ -168,8 +168,9 @@ def simple_evaluate( if task_manager is None: task_manager = TaskManager(verbosity, model_name=model) + # FOR DEBUG PURPOSES -> COMMENT THIS task_dict = get_task_dict(tasks, task_manager) - + if isinstance(model, str): if model_args is None: model_args = "" @@ -590,7 +591,7 @@ def evaluate( num_fewshot, higher_is_better, ) = consolidate_results(eval_tasks) - + ### Calculate group metrics ### if bool(results): results, versions, show_group_table, *_ = consolidate_group_results(results, versions, task_dict) diff --git a/lmms_eval/models/__init__.py b/lmms_eval/models/__init__.py index ed13bda7b..29a40a8d6 100644 --- a/lmms_eval/models/__init__.py +++ b/lmms_eval/models/__init__.py @@ -28,6 +28,8 @@ "llama_vid": "LLaMAVid", "llama_vision": "LlamaVision", "llava": "Llava", + "llava_next": "LlavaNext", + "llava_v6": "Llava_v6", "llava_hf": "LlavaHf", "llava_onevision": "Llava_OneVision", "llava_onevision_moviechat": "Llava_OneVision_MovieChat", @@ -65,6 +67,14 @@ "vllm": "VLLM", "xcomposer2_4KHD": "XComposer2_4KHD", "xcomposer2d5": "XComposer2D5", + "oryx": "Oryx", + "llama_vision": "LlamaVision", + "aria": "Aria", + "pangea": "Pangea", + "pixtral": "Pixtral", + "pixtral_v6": "Pixtral_v6", + "aya": "Aya", + "aya_v6": "Aya_v6", "egogpt": "EgoGPT", "internvideo2_5": "InternVideo2_5", "videochat_flash": "VideoChat_Flash", diff --git a/lmms_eval/models/aya.py b/lmms_eval/models/aya.py new file mode 100644 index 000000000..48d9c1924 --- /dev/null +++ b/lmms_eval/models/aya.py @@ -0,0 +1,320 @@ +import warnings +from typing import List, Optional, Tuple, Union +import torch +from accelerate import Accelerator, DistributedType +from accelerate.state import AcceleratorState +from tqdm import tqdm +from lmms_eval.api.instance import Instance +from lmms_eval.api.model import lmms +from lmms_eval.api.registry import register_model + +import torch.nn.functional as F +import numpy as np + +import io +import base64 + +from transformers import AutoProcessor, AutoModelForImageTextToText + +warnings.filterwarnings("ignore") +from loguru import logger as eval_logger + +DEFAULT_IMAGE_TOKEN = "" + +@register_model("aya") +class Aya(lmms): + """ + Custom AYA model implementation using Hugging Face Transformers and remote code. + + Example usage: + + accelerate launch --num_processes=8 -m lmms_eval \ + --model aya \ + --model_args pretrained=CohereForAI/aya-vision-8b \ + --tasks mme \ + --batch_size 1 \ + --output_path ./logs/ \ + --log_samples + """ + + def __init__( + self, + pretrained: str = "CohereForAI/aya-vision-8b", + device: str = "cuda", + dtype: Optional[Union[str, torch.dtype]] = "bfloat16", + batch_size: int = 1, + add_system_prompt: Optional[str] = None, + device_map: str = "", + use_cache: bool = True, + **kwargs, + ) -> None: + super().__init__() + assert kwargs == {}, f"Unexpected kwargs: {kwargs}" + + accelerator = Accelerator() + if accelerator.num_processes > 1: + self._device = torch.device(f"cuda:{accelerator.local_process_index}") + self.device_map = f"cuda:{accelerator.local_process_index}" + elif accelerator.num_processes == 1 and device_map == "auto": + self._device = torch.device(device) + self.device_map = device_map + else: + self._device = torch.device(f"cuda:{accelerator.local_process_index}") + self.device_map = f"cuda:{accelerator.local_process_index}" + + if isinstance(dtype, str) and dtype != "auto": + dtype = getattr(torch, dtype) + + self._processor = AutoProcessor.from_pretrained(pretrained) + self._model = AutoModelForImageTextToText.from_pretrained(pretrained, device_map="auto", torch_dtype=torch.float16) + self.batch_size_per_gpu = int(batch_size) + self.use_cache = use_cache + self.add_system_prompt = add_system_prompt + # Handle distributed setup + if accelerator.num_processes > 1: + assert accelerator.distributed_type in [DistributedType.FSDP, DistributedType.MULTI_GPU, DistributedType.DEEPSPEED] + + if accelerator.distributed_type == DistributedType.DEEPSPEED: + kwargs = { + "train_micro_batch_size_per_gpu": self.batch_size_per_gpu, + "train_batch_size": self.batch_size_per_gpu * accelerator.num_processes, + } + AcceleratorState().deepspeed_plugin.deepspeed_config_process(must_match=True, **kwargs) + eval_logger.info("Using DeepSpeed - ensure zero stage is set to 0 in accelerate config") + + if accelerator.distributed_type in [DistributedType.FSDP, DistributedType.DEEPSPEED]: + self._model = accelerator.prepare(self._model) + else: + self._model = accelerator.prepare_model(self._model, evaluation_mode=True) + + self.accelerator = accelerator + if self.accelerator.is_local_main_process: + eval_logger.info(f"Using {accelerator.num_processes} devices with data parallelism") + self._rank = self.accelerator.local_process_index + self._world_size = self.accelerator.num_processes + else: + if device_map == "auto": + eval_logger.info("Using pipeline parallelism") + else: + eval_logger.info(f"Using single device: {self._device}") + self._rank = 0 + self._world_size = 1 + + @property + def config(self): + return self._config + + @property + def tokenizer(self): + return self._tokenizer + + @property + def model(self): + if hasattr(self, "accelerator"): + return self.accelerator.unwrap_model(self._model) + else: + return self._model + + @property + def eot_token_id(self): + return self.tokenizer.eos_token_id + + @property + def max_length(self): + return self._max_length + + @property + def batch_size(self): + return self.batch_size_per_gpu + + @property + def device(self): + return self._device + + @property + def rank(self): + return self._rank + + @property + def world_size(self): + return self._world_size + + def tok_encode(self, string: str, left_truncate_len=None, add_special_tokens=None) -> List[int]: + """Tokenize a string.""" + add_special_tokens = False if add_special_tokens is None else add_special_tokens + encoding = self.tokenizer.encode(string, add_special_tokens=add_special_tokens) + if left_truncate_len: + encoding = encoding[-left_truncate_len:] + return encoding + + def tok_decode(self, tokens): + """Decode a sequence of tokens to a string.""" + return self.tokenizer.decode(tokens) + + def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]: + res = [] + pbar = tqdm(total=len(requests), disable=(self.rank != 0), desc="Model Responding") + for contexts, doc_to_target, doc_to_visual, doc_id, task, split in [reg.args for reg in requests]: + # encode, pad, and truncate contexts for this batch + if type(doc_to_target) == str: + continuation = doc_to_target + else: + continuation = doc_to_target(self.task_dict[task][split][doc_id]) + visuals = [doc_to_visual(self.task_dict[task][split][doc_id])] + + if visuals != [None]: + visuals = self.flatten(visuals) + image_sizes = [[visual.size[0], visual.size[1]] for visual in visuals] + image_urls = [self.get_image_url(v) for v in visuals] + else: + image_urls = None + + prompts_input = contexts[0] if isinstance(contexts, list) else contexts + + # create chat object + message = [ + {"role": "user", + "content": [ + {"type": "text", "text": prompts_input + }]}] + if image_urls is not None: + message[0]["content"] += [{"type": "image", "url": image_url} for image_url in image_urls] + + if self.add_system_prompt is not None: + message.insert(0, {"role": "system", + "content": [{"type": "text", "text": self.add_system_prompt}]}) + + # Here we haven't appended the continuation yet, so we can use the original message to get the logits of the full input + context_id = self._processor.apply_chat_template(message, padding=True, add_generation_prompt=False, tokenize=True, return_dict=True, return_tensors="pt").to(self._model.device) + + # Now we append the continuation to the message to get the full input + # NOTE: We are stripping the answer here so that there is no leading space in the answer + message.append({"role": "assistant", "content": [{"type": "text", "text": continuation.strip()}]}) + input = self._processor.apply_chat_template(message, padding=True, add_generation_prompt=False, tokenize=True, return_dict=True, return_tensors="pt").to(self._model.device) + labels = input["input_ids"].clone() + + + # NOTE: The following solution is just a workaround for the fact that we need to mask the extra tokens in the labels + """ + Context part no need to calculate for loss. That's why we set the labels to -100 for the initial context part + The answer part is with the following format: + '<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_RESPONSE|>ANSWER<|END_RESPONSE|><|END_OF_TURN_TOKEN|>' + So, we have 3 tokens before the answer (<|START_OF_TURN_TOKEN|>, <|CHATBOT_TOKEN|>, <|START_RESPONSE|>) + and 2 tokens after the answer (<|END_RESPONSE|>, <|END_OF_TURN_TOKEN|>) + We need to set the labels for the answer part to -100, so that they are ignored in the loss calculation + """ + labels[0, : context_id["input_ids"].shape[1]+3] = -100 + labels[0, -2:] = -100 # last token is also ignored + try: + # NOTE: We have to set `add_generation_prompt` to False here, because we are not generating text, but rather calculating the log likelihood of the continuation. + with torch.inference_mode(): + outputs = self._model(**input, labels=labels, add_generation_prompt=False) + loss = outputs["loss"] + logits = outputs["logits"] + greedy_tokens = logits.argmax(dim=-1) + cont_toks = input["input_ids"][:, context_id["input_ids"].shape[1] :] # [1, seq] + greedy_tokens = greedy_tokens[:, context_id["input_ids"].shape[1] : input["input_ids"].shape[1]] # [1, seq] + max_equal = (greedy_tokens == cont_toks).all() + result = (float(loss.item()), bool(max_equal)) + except Exception as e: + eval_logger.error(f"Error {e} in generating") + result = "" + res.append(result) + pbar.update(1) + pbar.close() + return res + + def flatten(self, input): + """Flatten a nested list.""" + new_list = [] + for i in input: + for j in i: + new_list.append(j) + return new_list + + + def get_image_url(self, image_bytes: bytes) -> str: + buffer = io.BytesIO() + image_bytes.save(buffer, format="JPEG") + img_bytes = buffer.getvalue() + base64_image = base64.b64encode(img_bytes).decode('utf-8') + data_url = f"data:image/jpeg;base64,{base64_image}" + return data_url + + def generate_until(self, requests: List[Instance]) -> List[str]: + """Generate text based on the given requests.""" + res = [] + + def batch_requests(requests, batch_size): + for i in range(0, len(requests), batch_size): + yield [x.arguments for x in requests[i:i + batch_size]] + + chunks = batch_requests(requests, self.batch_size) + + num_iters = len(requests) // self.batch_size if len(requests) % self.batch_size == 0 else len(requests) // self.batch_size + 1 + + pbar = tqdm(total=num_iters, disable=(self.rank != 0), desc="Model Responding") + for chunk in chunks: + contexts, all_gen_kwargs, doc_to_visuals, doc_id, tasks, splits = zip(*chunk) + visuals = [doc_to_visual(self.task_dict[task][split][ids]) for ids, task, split, doc_to_visual in zip(doc_id, tasks, splits, doc_to_visuals)] + + # Use the generation kwargs from the first request in the batch + gen_kwargs = all_gen_kwargs[0] + + # Set default generation parameters if not provided + if "max_new_tokens" not in gen_kwargs: + gen_kwargs["max_new_tokens"] = 8192 + if "temperature" not in gen_kwargs: + gen_kwargs["temperature"] = 0 + if "do_sample" not in gen_kwargs: + gen_kwargs["do_sample"] = False + + assert self.batch_size_per_gpu == 1, "Do not support batch_size_per_gpu > 1 for now" + context = contexts[0] + visual = visuals[0] + + # TODO: handle multiple images / understand the `visuals` object + if not isinstance(visual, list): + visual = [visual] + + # vLLM does not work with bytes, so we need to convert it to a data url + image_urls = [self.get_image_url(v) for v in visual] + + # image_url = self.get_image_url(visual) + + # Pixtral expects inputs in a different format, and doesn't work with tokens added in the middle of the prompt. + context = context.replace(DEFAULT_IMAGE_TOKEN, "") + + # create chat object + message = [ + {"role": "user", + "content": [ + {"type": "text", "text": context + }] + + + [{"type": "image", "url": image_url} for image_url in image_urls] + }] + + if self.add_system_prompt is not None: + message.insert(0, {"role": "system", + "content": [{"type": "text", "text": self.add_system_prompt}]}) + + try: + inputs = self._processor.apply_chat_template(message, padding=True, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(self._model.device) + result = self._model.generate(**inputs, + max_new_tokens=gen_kwargs["max_new_tokens"], + do_sample=gen_kwargs["do_sample"], + temperature=gen_kwargs["temperature"], + ) + result = self._processor.tokenizer.decode(result[0][inputs.input_ids.shape[1]:], skip_special_tokens=True) + except Exception as e: + eval_logger.error(f"Error {e} in generating") + result = "" + res.append(result) + pbar.update(1) + pbar.close() + return res + + def generate_until_multi_round(self, requests) -> List[str]: + """Not implemented for AYA model.""" + raise NotImplementedError("Multi-round generation is not implemented for AYA model") \ No newline at end of file diff --git a/lmms_eval/models/aya_v6.py b/lmms_eval/models/aya_v6.py new file mode 100644 index 000000000..744880c2e --- /dev/null +++ b/lmms_eval/models/aya_v6.py @@ -0,0 +1,34 @@ +from lmms_eval.models.aya import Aya +import warnings +from typing import Optional, Union +import torch + +from lmms_eval.api.model import lmms +from lmms_eval.api.registry import register_model + + +warnings.filterwarnings("ignore") + +DEFAULT_IMAGE_TOKEN = "" + +@register_model("aya_v6") +class Aya_v6(Aya): + def __init__( + self, + pretrained: str = "CohereForAI/aya-vision-8b", + device: str = "cuda", + dtype: Optional[Union[str, torch.dtype]] = "bfloat16", + batch_size: int = 1, + add_system_prompt: Optional[str] = None, + device_map: str = "", + use_cache: bool = True, + **kwargs, + ) -> None: + super().__init__( + pretrained = pretrained, + device = device, + dtype = dtype, + batch_size = batch_size, + add_system_prompt = add_system_prompt, + device_map = device_map, + use_cache = use_cache,) \ No newline at end of file diff --git a/lmms_eval/models/gemini_api.py b/lmms_eval/models/gemini_api.py index 1d5a77069..d46247c9c 100644 --- a/lmms_eval/models/gemini_api.py +++ b/lmms_eval/models/gemini_api.py @@ -11,22 +11,21 @@ from loguru import logger as eval_logger from PIL import Image from tqdm import tqdm - +import io +import base64 from lmms_eval.api.instance import Instance from lmms_eval.api.model import lmms from lmms_eval.api.registry import register_model -try: - import google.generativeai as genai - from google.generativeai.types import HarmBlockThreshold, HarmCategory +from google import genai +from google.genai import types +# import google.generativeai as genai +# from google.generativeai.types import HarmBlockThreshold, HarmCategory - NUM_SECONDS_TO_SLEEP = 30 - GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") - genai.configure(api_key=GOOGLE_API_KEY) +NUM_SECONDS_TO_SLEEP = 30 +GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") +# genai.configure(api_key=GOOGLE_API_KEY) -except Exception as e: - eval_logger.error(f"Error importing generativeai: {str(e)}") - genai = None try: import soundfile as sf @@ -39,10 +38,11 @@ class GeminiAPI(lmms): def __init__( self, model_version: str = "gemini-1.5-pro", - # modality: str = "image", + modality: str = "image", timeout: int = 120, continual_mode: bool = True, response_persistent_folder: str = "./logs/gemini_persistent_folder", + system_prompt: str = None, interleave: bool = False, # We will cache the Gemini API response in this path and use it for future requests **kwargs, @@ -50,7 +50,8 @@ def __init__( super().__init__() self.model_version = model_version self.timeout = timeout - self.model = genai.GenerativeModel(model_version) + # self.model = genai.GenerativeModel(model_version) + self.model = genai.Client(api_key=GOOGLE_API_KEY) self.continual_mode = continual_mode self.response_persistent_file = "" self.interleave = interleave @@ -62,13 +63,13 @@ def __init__( os.makedirs(self.response_persistent_folder) self.response_persistent_file = os.path.join(self.response_persistent_folder, f"{self.model_version}_response.json") - if os.path.exists(self.response_persistent_file): - with open(self.response_persistent_file, "r") as f: - self.response_cache = json.load(f) - self.cache_mode = "resume" - else: - self.response_cache = {} - self.cache_mode = "start" + # if os.path.exists(self.response_persistent_file): + # with open(self.response_persistent_file, "r") as f: + # self.response_cache = json.load(f) + # self.cache_mode = "resume" + # else: + # self.response_cache = {} + self.cache_mode = "start" accelerator = Accelerator() if accelerator.num_processes > 1: @@ -86,9 +87,10 @@ def __init__( self.device = self.accelerator.device - # self.modality = modality + self.modality = modality self.video_pool = [] + self.system_prompt = system_prompt def free_video(self): for video in self.video_pool: @@ -159,27 +161,15 @@ def get_uuid(task, split, doc_id): return f"{task}___{split}___{doc_id}" for contexts, gen_kwargs, doc_to_visual, doc_id, task, split in [reg.args for reg in requests]: - if self.continual_mode and self.cache_mode == "resume": - doc_uuid = get_uuid(task, split, doc_id) - if doc_uuid in self.response_cache: - content = self.response_cache[doc_uuid] - if content: - res.append(content) - pbar.update(1) - continue + if "max_new_tokens" not in gen_kwargs: gen_kwargs["max_new_tokens"] = 1024 if "temperature" not in gen_kwargs: gen_kwargs["temperature"] = 0 - config = genai.GenerationConfig( - max_output_tokens=gen_kwargs["max_new_tokens"], - temperature=gen_kwargs["temperature"], - ) - visuals = [doc_to_visual(self.task_dict[task][split][doc_id])] - visuals = self.flatten(visuals) + visuals = self.convert_modality(visuals) if self.interleave: @@ -189,24 +179,21 @@ def get_uuid(task, split, doc_id): for attempt in range(5): try: - content = self.model.generate_content( - message, - generation_config=config, - safety_settings={ - HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE, - HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE, - HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE, - HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE, - }, - ) - content = content.text + response = self.model.models.generate_content( + model=self.model_version, + config=types.GenerateContentConfig( + system_instruction=self.system_prompt, + ), + contents=message + ) + results = response.text break except Exception as e: eval_logger.info(f"Attempt {attempt + 1} failed with error: {str(e)}") if isinstance(e, ValueError): try: - eval_logger.info(f"Prompt feed_back: {content.prompt_feedback}") - content = "" + eval_logger.info(f"Prompt feed_back: {results.prompt_feedback}") + results = "" break except Exception: pass @@ -215,7 +202,7 @@ def get_uuid(task, split, doc_id): else: # If this was the last attempt, log and return empty eval_logger.error(f"All 5 attempts failed. Last error message: {str(e)}") content = "" - res.append(content) + res.append(results) pbar.update(1) self.free_video() diff --git a/lmms_eval/models/llava.py b/lmms_eval/models/llava.py index 479ba1024..28040fa88 100755 --- a/lmms_eval/models/llava.py +++ b/lmms_eval/models/llava.py @@ -1,5 +1,4 @@ import torch - torch.backends.cuda.matmul.allow_tf32 = True @@ -23,6 +22,13 @@ from loguru import logger as eval_logger +from transformers import ( + AutoConfig, + AutoProcessor, + LlavaForConditionalGeneration, + LlavaNextForConditionalGeneration, +) + try: from llava.constants import DEFAULT_IMAGE_TOKEN, IMAGE_TOKEN_INDEX from llava.conversation import conv_templates @@ -31,7 +37,6 @@ process_images, tokenizer_image_token, ) - from llava.model.builder import load_pretrained_model except Exception as e: eval_logger.debug("LLaVA is not installed. Please install LLaVA to use this model.\nError: %s" % e) @@ -60,17 +65,20 @@ def __init__( model_name=None, attn_implementation=best_fit_attn_implementation, device_map="cuda:0", - conv_template="vicuna_v1", + conv_template="qwen_2", + revision="main", + trust_remote_code: Optional[bool] = False, + dtype: Optional[Union[str, torch.dtype]] = "auto", use_cache=True, tie_weights: bool = True, truncate_context=False, # whether to truncate the context in generation, set it False for LLaVA-1.6 customized_config=None, # ends in json + add_system_prompt=None, **kwargs, ) -> None: super().__init__() # Do not use kwargs for now assert kwargs == {}, f"Unexpected kwargs: {kwargs}" - accelerator_kwargs = InitProcessGroupKwargs(timeout=timedelta(weeks=52)) accelerator = Accelerator(kwargs_handlers=[accelerator_kwargs]) self.accelerator = accelerator @@ -93,14 +101,17 @@ def __init__( llava_model_args["attn_implementation"] = attn_implementation if "use_flash_attention_2" in kwargs: llava_model_args["use_flash_attention_2"] = kwargs["use_flash_attention_2"] - model_name = model_name if model_name is not None else get_model_name_from_path(pretrained) + try: # Try to load the model with the multimodal argument - self._tokenizer, self._model, self._image_processor, self._max_length = load_pretrained_model(pretrained, None, model_name, device_map=self.device_map, **llava_model_args) - except TypeError: - # for older versions of LLaVA that don't have multimodal argument - llava_model_args.pop("multimodal", None) - self._tokenizer, self._model, self._image_processor, self._max_length = load_pretrained_model(pretrained, None, model_name, device_map=self.device_map, **llava_model_args) + self._model = LlavaNextForConditionalGeneration.from_pretrained(pretrained, revision=revision, torch_dtype=dtype, device_map=self.device_map, trust_remote_code=trust_remote_code, attn_implementation=attn_implementation) + self.pretrained = pretrained + self._image_processor = AutoProcessor.from_pretrained(pretrained, revision=revision, trust_remote_code=trust_remote_code) + self._image_processor.tokenizer.padding_side = "left" + self._tokenizer = self._image_processor.tokenizer + self._config = self._model.config + except: + print("Error loading model with multimodal argument.") self._config = self._model.config self.model.eval() if tie_weights: @@ -143,7 +154,7 @@ def __init__( self.model.to(self._device) self._rank = 0 self._world_size = 1 - + self.add_system_prompt = add_system_prompt @property def config(self): # return the associated transformers.AutoConfig for the given pretrained model. @@ -213,7 +224,6 @@ def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]: # TODO res = [] pbar = tqdm(total=len(requests), disable=(self.rank != 0), desc="Model Responding") - for contexts, doc_to_target, doc_to_visual, doc_id, task, split in [reg.args for reg in requests]: # encode, pad, and truncate contexts for this batch if type(doc_to_target) == str: @@ -226,9 +236,9 @@ def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]: if visuals: image = process_images(visuals, self._image_processor, self._config) if type(image) is list: - image = [_image.to(dtype=torch.float16, device=self.device) for _image in image] + image = [_image.to(dtype=torch.bfloat16, device=self.device) for _image in image] else: - image = image.to(dtype=torch.float16, device=self.device) + image = image.to(dtype=torch.bfloat16, device=self.device) else: image = None @@ -250,19 +260,28 @@ def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]: conv = copy.deepcopy(conv_templates[self.conv_template]) else: conv = conv_templates[self.conv_template].copy() + if self.add_system_prompt and conv.system: + conv.system = self.add_system_prompt conv.append_message(conv.roles[0], prompts_input) conv.append_message(conv.roles[1], None) prompt = conv.get_prompt() + pad_token_id = self.tokenizer.pad_token_id if self.tokenizer.pad_token_id is not None else self.tokenizer.eos_token_id contxt_id = tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(self.device) # Add the answer of the second role - conv.messages[1][1] = continuation + # NOTE: We are stripping the answer here so that there is no leading space in the answer + conv.messages[1][1] = continuation.strip() prompt = conv.get_prompt() input_ids = tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(self.device) labels = input_ids.clone() # Context part no need to calculate for loss labels[0, : contxt_id.shape[1]] = -100 + + # NOTE: We mask the last two tokens in the labels, which are the EOS and the new line tokens. + # This is to avoid calculating loss on these tokens and have a more comparable loss to the other models. + labels[0, -2:] = -100 + with torch.inference_mode(): outputs = self.model(input_ids=input_ids, labels=labels, images=image, use_cache=True, image_sizes=image_sizes) loss = outputs["loss"] @@ -289,7 +308,6 @@ def flatten(self, input): def generate_until(self, requests: List[Instance]) -> List[str]: res = [] - def _collate(x): # the negative sign on len(toks) sorts descending - this has a few advantages: # - time estimates will always be over not underestimates, which is more useful for planning @@ -307,6 +325,7 @@ def _collate(x): chunks = re_ords.get_batched(n=self.batch_size, batch_fn=None) num_iters = len(requests) // self.batch_size if len(requests) % self.batch_size == 0 else len(requests) // self.batch_size + 1 pbar = tqdm(total=num_iters, disable=(self.rank != 0), desc="Model Responding") + # breakpoint() for chunk in chunks: contexts, all_gen_kwargs, doc_to_visual, doc_id, task, split = zip(*chunk) task = task[0] @@ -336,9 +355,9 @@ def _collate(x): if flattened_visuals: image_tensor = process_images(flattened_visuals, self._image_processor, self._config) if type(image_tensor) is list: - image_tensor = [_image.to(dtype=torch.float16, device=self.device) for _image in image_tensor] + image_tensor = [_image.to(dtype=torch.bfloat16, device=self.device) for _image in image_tensor] else: - image_tensor = image_tensor.to(dtype=torch.float16, device=self.device) + image_tensor = image_tensor.to(dtype=torch.bfloat16, device=self.device) else: image_tensor = None @@ -359,16 +378,16 @@ def _collate(x): question = image_tokens + "\n" + context else: question = context - # This is much safer for llama3, as we now have some object type in it - if "llama_3" in self.conv_template: - conv = copy.deepcopy(conv_templates[self.conv_template]) + + if isinstance(question, list): + conv = [ {"role": "user", "content": qq} for qq in question ], else: - conv = conv_templates[self.conv_template].copy() - conv.append_message(conv.roles[0], question) - conv.append_message(conv.roles[1], None) - prompt_question = conv.get_prompt() + conv = [{"role": "user", "content": question}] + if self.add_system_prompt: + conv.insert(0, {"role": "system", "content": self.add_system_prompt}) + prompt_question = self.tokenizer.apply_chat_template(conv, tokenize=False, add_generation_prompt=True) question_input.append(prompt_question) - + # input_ids = tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(self.device) # preconfigure gen_kwargs with defaults gen_kwargs["image_sizes"] = [flattened_visuals[idx].size for idx in range(len(flattened_visuals))] @@ -425,7 +444,6 @@ def _collate(x): pbar.update(1) # reorder this group of results back to original unsorted form res = re_ords.get_original(res) - pbar.close() return res diff --git a/lmms_eval/models/llava_hf.py b/lmms_eval/models/llava_hf.py index 8151e941d..802038ac6 100644 --- a/lmms_eval/models/llava_hf.py +++ b/lmms_eval/models/llava_hf.py @@ -70,10 +70,12 @@ def __init__( batch_size: int = 1, trust_remote_code: Optional[bool] = False, attn_implementation: Optional[str] = None, - device_map: str = "", + device_map: str = "auto", chat_template: Optional[str] = None, use_cache: bool = True, max_frames_num: Optional[int] = 32, + add_system_prompt: str = None, + add_bos_token: bool = False, **kwargs, ) -> None: super().__init__() @@ -202,11 +204,13 @@ def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]: else: continuation = doc_to_target(self.task_dict[task][split][doc_id]) visuals = [doc_to_visual(self.task_dict[task][split][doc_id])] - visuals = self.flatten(visuals) - - image_tokens = [DEFAULT_IMAGE_TOKEN] * len(visuals) - image_tokens = " ".join(image_tokens) - context = f"{image_tokens}\n{context}" + if visuals != [None]: + visuals = self.flatten(visuals) + image_tokens = [DEFAULT_IMAGE_TOKEN] * len(visuals) + image_tokens = " ".join(image_tokens) + context = f"{image_tokens}\n{context}" + else: + visuals = None # Apply chat template messages = [{"role": "user", "content": context}, {"role": "assistant", "content": continuation}] if self.chat_template is not None: @@ -220,14 +224,14 @@ def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]: self.tokenizer.chat_template = VICUNA_CHAT_TEMPLATE prompt = self.tokenizer.apply_chat_template(messages[:-1], tokenize=False, add_generation_prompt=True) prompt_and_continuation = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False) - formatted_contexts = [prompt] formatted_continuation = [prompt_and_continuation] + model_inputs_wo_continuation = self._image_processor(text=formatted_contexts, images=visuals, return_tensors="pt").to(self._device, self.model.dtype) model_inputs = self._image_processor(text=formatted_continuation, images=visuals, return_tensors="pt").to(self._device, self.model.dtype) labels = model_inputs["input_ids"].clone() - contxt_id = self._image_processor(text=formatted_contexts, return_tensors="pt")["input_ids"] - labels[:, : contxt_id.shape[1]] = -100 - + image_context_shape = model_inputs_wo_continuation["input_ids"].shape + labels[:, : image_context_shape[1]] = -100 + # labels[0, -1:] = -100 # last token is also ignored if self.accelerator.is_main_process and doc_id % 100 == 0: eval_logger.debug(f"Prompt for doc ID {doc_id}:\n\n{formatted_contexts[0]}\n") eval_logger.debug(f"Prompt and continuation for doc ID {doc_id}:\n\n{formatted_continuation[0]}\n") @@ -237,8 +241,8 @@ def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]: loss = outputs["loss"] logits = outputs["logits"] greedy_tokens = logits.argmax(dim=-1) - cont_toks = model_inputs["input_ids"][:, contxt_id.shape[1] :] # [1, seq] - greedy_tokens = greedy_tokens[:, contxt_id.shape[1] : model_inputs["input_ids"].shape[1]] # [1, seq] + cont_toks = model_inputs["input_ids"][:, image_context_shape[1] :] # [1, seq] + greedy_tokens = greedy_tokens[:, image_context_shape[1] : model_inputs["input_ids"].shape[1]] # [1, seq] max_equal = (greedy_tokens == cont_toks).all() res.append((float(loss.item()), bool(max_equal))) pbar.update(1) @@ -319,6 +323,8 @@ def _collate(x): image_tokens = [DEFAULT_IMAGE_TOKEN] * len(visuals) elif task_type == "video": image_tokens = [DEFAULT_VIDEO_TOKEN] * len(visuals) + elif task_type == "text": + image_tokens = [] image_tokens = " ".join(image_tokens) context = f"{image_tokens}\n{context}" # Apply chat template @@ -347,7 +353,8 @@ def _collate(x): inputs = self._image_processor(images=visuals, text=text, return_tensors="pt").to(self._device, self.model.dtype) elif task_type == "video": inputs = self._image_processor(videos=visuals, text=text, return_tensors="pt").to(self._device, self.model.dtype) - + elif task_type == "text": + inputs = self._image_processor(text=text, return_tensors="pt").to(self._device, self.model.dtype) gen_kwargs["image_sizes"] = [visuals[idx].size for idx in range(len(visuals))] if "max_new_tokens" not in gen_kwargs: gen_kwargs["max_new_tokens"] = 1024 @@ -358,7 +365,8 @@ def _collate(x): if "num_beams" not in gen_kwargs: gen_kwargs["num_beams"] = 1 try: - cont = self.model.generate( + # breakpoint() + outputs = self.model.generate( **inputs, do_sample=True if gen_kwargs["temperature"] > 0 else False, temperature=gen_kwargs["temperature"], @@ -367,13 +375,13 @@ def _collate(x): max_new_tokens=gen_kwargs["max_new_tokens"], use_cache=self.use_cache, pad_token_id=self.eot_token_id, - eos_token_id=self.eot_token_id, - ) - cont = cont[:, inputs["input_ids"].shape[-1] :] + eos_token_id=self.eot_token_id) + + outputs = outputs[:, inputs["input_ids"].shape[-1] :] except Exception as e: eval_logger.error(f"Error {e} in generating") - cont = "" - text_outputs = self.tokenizer.batch_decode(cont, skip_special_tokens=True)[0] + outputs = "" + text_outputs = self.tokenizer.batch_decode(outputs, skip_special_tokens=True)[0] if self.accelerator.is_main_process and doc_id[0] % 100 == 0: eval_logger.debug(f"Generated text for doc ID {doc_id[0]}:\n\n{text_outputs}\n") @@ -387,4 +395,4 @@ def _collate(x): return res def generate_until_multi_round(self, requests) -> List[str]: - raise NotImplementedError("TODO: Implement multi-round generation for LLaVAHF") + raise NotImplementedError("TODO: Implement multi-round generation for LLaVAHF") \ No newline at end of file diff --git a/lmms_eval/models/llava_next.py b/lmms_eval/models/llava_next.py new file mode 100755 index 000000000..3e941d4a1 --- /dev/null +++ b/lmms_eval/models/llava_next.py @@ -0,0 +1,439 @@ +import torch +torch.backends.cuda.matmul.allow_tf32 = True + + +import copy +import warnings +from datetime import timedelta +from typing import List, Optional, Tuple, Union + +from accelerate import Accelerator, DistributedType, InitProcessGroupKwargs +from accelerate.state import AcceleratorState +from packaging import version +from tqdm import tqdm + +from lmms_eval import utils +from lmms_eval.api.instance import Instance +from lmms_eval.api.model import lmms +from lmms_eval.api.registry import register_model +from lmms_eval.utils import stop_sequences_criteria + +warnings.filterwarnings("ignore") + +from loguru import logger as eval_logger + +try: + from llava.constants import DEFAULT_IMAGE_TOKEN, IMAGE_TOKEN_INDEX + from llava.conversation import conv_templates + from llava.mm_utils import ( + get_model_name_from_path, + process_images, + tokenizer_image_token, + ) + from llava.model.builder import load_pretrained_model +except Exception as e: + eval_logger.debug("LLaVA is not installed. Please install LLaVA to use this model.\nError: %s" % e) + +# inference implementation for attention, can be "sdpa", "eager", "flash_attention_2". Seems FA2 is not effective during inference: https://discuss.huggingface.co/t/flash-attention-has-no-effect-on-inference/73453/5 +# if is_flash_attn_2_available: +# best_fit_attn_implementation = "flash_attention_2" # flash_attn has a bug that says: ERROR Error query and key must have the same dtype in generating + +if version.parse(torch.__version__) >= version.parse("2.1.2"): + best_fit_attn_implementation = "sdpa" +else: + best_fit_attn_implementation = "eager" + + +@register_model("llava_next") +class LlavaNext(lmms): + """ + Llava Model + """ + + def __init__( + self, + pretrained: str = "liuhaotian/llava-v1.5-7b", + truncation: Optional[bool] = True, + device: Optional[str] = "cuda:0", + batch_size: Optional[Union[int, str]] = 1, + model_name=None, + attn_implementation=best_fit_attn_implementation, + device_map="cuda:0", + conv_template="qwen_2", + use_cache=True, + tie_weights: bool = True, + truncate_context=False, # whether to truncate the context in generation, set it False for LLaVA-1.6 + customized_config=None, # ends in json + add_system_prompt=None, + **kwargs, + ) -> None: + super().__init__() + # Do not use kwargs for now + assert kwargs == {}, f"Unexpected kwargs: {kwargs}" + accelerator_kwargs = InitProcessGroupKwargs(timeout=timedelta(weeks=52)) + accelerator = Accelerator(kwargs_handlers=[accelerator_kwargs]) + self.accelerator = accelerator + if accelerator.num_processes > 1: + self._device = torch.device(f"cuda:{accelerator.local_process_index}") + self.device_map = f"cuda:{accelerator.local_process_index}" + elif accelerator.num_processes == 1 and device_map == "auto": + self._device = torch.device(device) + self.device_map = device_map + else: + self._device = torch.device(f"cuda:{accelerator.local_process_index}") + self.device_map = f"cuda:{accelerator.local_process_index}" + + llava_model_args = { + "multimodal": True, + } + if customized_config is not None: + llava_model_args["customized_config"] = customized_config + if attn_implementation is not None: + llava_model_args["attn_implementation"] = attn_implementation + if "use_flash_attention_2" in kwargs: + llava_model_args["use_flash_attention_2"] = kwargs["use_flash_attention_2"] + model_name = model_name if model_name is not None else get_model_name_from_path(pretrained) + try: + # Try to load the model with the multimodal argument + self._tokenizer, self._model, self._image_processor, self._max_length = load_pretrained_model(pretrained, None, model_name, device_map=self.device_map, torch_dtype="bfloat16", **llava_model_args) + except TypeError: + # for older versions of LLaVA that don't have multimodal argument + llava_model_args.pop("multimodal", None) + self._tokenizer, self._model, self._image_processor, self._max_length = load_pretrained_model(pretrained, None, model_name, device_map=self.device_map, torch_dtype="bfloat16", **llava_model_args) + self._config = self._model.config + self.model.eval() + if tie_weights: + self.model.tie_weights() + + self.truncation = truncation + self.batch_size_per_gpu = int(batch_size) + self.conv_template = conv_template + self.use_cache = use_cache + self.truncate_context = truncate_context + # assert self.batch_size_per_gpu == 1, "Llava currently does not support batched generation. See https://github.com/haotian-liu/LLaVA/issues/754. HF Llava also has this issue." + if accelerator.num_processes > 1: + assert accelerator.distributed_type in [DistributedType.FSDP, DistributedType.MULTI_GPU, DistributedType.DEEPSPEED], "Unsupported distributed type provided. Only DDP and FSDP are supported." + # If you want to use DistributedType.DEEPSPEED, you have to run accelerate config before using the model + # Also, you have to select zero stage 0 (equivalent to DDP) in order to make the prepare model works + # I tried to set different parameters in the kwargs to let default zero 2 stage works, but it didn't work. + if accelerator.distributed_type == DistributedType.DEEPSPEED: + kwargs = { + "train_micro_batch_size_per_gpu": self.batch_size_per_gpu, + "train_batch_size": self.batch_size_per_gpu * accelerator.num_processes, + } + AcceleratorState().deepspeed_plugin.deepspeed_config_process(must_match=True, **kwargs) + eval_logger.info("Detected that you are using DistributedType.DEEPSPEED. Make sure you run `accelerate config` and set zero stage to 0") + + if accelerator.distributed_type == DistributedType.FSDP or accelerator.distributed_type == DistributedType.DEEPSPEED: + self._model = accelerator.prepare(self.model) + else: + self._model = accelerator.prepare_model(self.model, evaluation_mode=True) + self.accelerator = accelerator + if self.accelerator.is_local_main_process: + eval_logger.info(f"Using {accelerator.num_processes} devices with data parallelism") + self._rank = self.accelerator.local_process_index + self._world_size = self.accelerator.num_processes + elif accelerator.num_processes == 1 and device_map == "auto": + eval_logger.info(f"Using {accelerator.num_processes} devices with tensor parallelism") + self._rank = 0 + self._world_size = 1 + else: + eval_logger.info(f"Using single device: {self._device}") + self.model.to(self._device) + self._rank = 0 + self._world_size = 1 + self.add_system_prompt = add_system_prompt + @property + def config(self): + # return the associated transformers.AutoConfig for the given pretrained model. + return self._config + + @property + def tokenizer(self): + return self._tokenizer + + @property + def model(self): + # returns the model, unwrapping it if using Accelerate + if hasattr(self, "accelerator"): + return self.accelerator.unwrap_model(self._model) + else: + return self._model + + @property + def eot_token_id(self): + # we use EOT because end of *text* is more accurate for what we're doing than end of *sentence* + return self.tokenizer.eos_token_id + + @property + def max_length(self): + return self._max_length + + def pad_sequence(self, input_ids, batch_first, padding_value): + if self.tokenizer.padding_side == "left": + input_ids = [torch.flip(_input_ids, [0]) for _input_ids in input_ids] + input_ids = torch.nn.utils.rnn.pad_sequence(input_ids, batch_first=batch_first, padding_value=padding_value) + if self.tokenizer.padding_side == "left": + input_ids = torch.flip(input_ids, [1]) + return input_ids + + @property + def batch_size(self): + return self.batch_size_per_gpu + + @property + def device(self): + return self._device + + @property + def rank(self): + return self._rank + + @property + def world_size(self): + return self._world_size + + def tok_encode(self, string: str, left_truncate_len=None, add_special_tokens=None) -> List[int]: + """ """ + add_special_tokens = False if add_special_tokens is None else add_special_tokens + encoding = self.tokenizer.encode(string, add_special_tokens=add_special_tokens) + # left-truncate the encoded context to be at most `left_truncate_len` tokens long + if left_truncate_len: + encoding = encoding[-left_truncate_len:] + return encoding + + def tok_decode(self, tokens): + try: + return self.tokenizer.decode(tokens) + except: + return self.tokenizer.decode([tokens]) + + def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]: + # TODO + res = [] + pbar = tqdm(total=len(requests), disable=(self.rank != 0), desc="Model Responding") + for contexts, doc_to_target, doc_to_visual, doc_id, task, split in [reg.args for reg in requests]: + # encode, pad, and truncate contexts for this batch + if type(doc_to_target) == str: + continuation = doc_to_target + else: + continuation = doc_to_target(self.task_dict[task][split][doc_id]) + visuals = [doc_to_visual(self.task_dict[task][split][doc_id])] + visuals = self.flatten(visuals) + image_sizes = [[visual.size[0], visual.size[1]] for visual in visuals] + if visuals: + image = process_images(visuals, self._image_processor, self._config) + if type(image) is list: + image = [_image.to(dtype=torch.bfloat16, device=self.device) for _image in image] + else: + image = image.to(dtype=torch.bfloat16, device=self.device) + else: + image = None + + prompts_input = contexts[0] if isinstance(contexts, list) else contexts + + if image is not None and len(image) != 0 and DEFAULT_IMAGE_TOKEN not in prompts_input: + """ + Three senarios: + 1. No image, and there for, no image token should be added. + 2. image token is already specified in the context, so we don't need to add it. + 3. image token is not specified in the context and there is image inputs, so we need to add it. In this case, we add the image token at the beginning of the context and add a new line. + """ + image_tokens = [DEFAULT_IMAGE_TOKEN] * len(visuals) + image_tokens = " ".join(image_tokens) + prompts_input = image_tokens + "\n" + (contexts[0] if isinstance(contexts, list) else contexts) + + # This is much safer for llama3, as we now have some object type in it + if "llama_3" in self.conv_template: + conv = copy.deepcopy(conv_templates[self.conv_template]) + else: + conv = conv_templates[self.conv_template].copy() + if self.add_system_prompt and conv.system: + conv.system = self.add_system_prompt + conv.append_message(conv.roles[0], prompts_input) + conv.append_message(conv.roles[1], None) + prompt = conv.get_prompt() + + pad_token_id = self.tokenizer.pad_token_id if self.tokenizer.pad_token_id is not None else self.tokenizer.eos_token_id + contxt_id = tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(self.device) + # Add the answer of the second role + # NOTE: We are stripping the answer here so that there is no leading space in the answer + conv.messages[1][1] = continuation.strip() + + prompt = conv.get_prompt() + input_ids = tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(self.device) + labels = input_ids.clone() + # Context part no need to calculate for loss + labels[0, : contxt_id.shape[1]] = -100 + + # NOTE: We mask the last two tokens in the labels, which are the EOS and the new line tokens. + # This is to avoid calculating loss on these tokens and have a more comparable loss to the other models. + labels[0, -2:] = -100 + + with torch.inference_mode(): + outputs = self.model(input_ids=input_ids, labels=labels, images=image, use_cache=True, image_sizes=image_sizes) + loss = outputs["loss"] + # loss = torch.exp(loss) + logits = outputs["logits"] + greedy_tokens = logits.argmax(dim=-1) + cont_toks = input_ids[:, contxt_id.shape[1] :] # [1, seq] + greedy_tokens = greedy_tokens[:, contxt_id.shape[1] : input_ids.shape[1]] # [1, seq] + max_equal = (greedy_tokens == cont_toks).all() + res.append((float(loss.item()), bool(max_equal))) + pbar.update(1) + pbar.close() + return res + + def flatten(self, input): + if not input or any(i is None for i in input): + return [] + new_list = [] + for i in input: + if i: + for j in i: + new_list.append(j) + return new_list + + def generate_until(self, requests: List[Instance]) -> List[str]: + res = [] + def _collate(x): + # the negative sign on len(toks) sorts descending - this has a few advantages: + # - time estimates will always be over not underestimates, which is more useful for planning + # - to know the size of a batch when going through the list, you know the first one is always the batch + # padded context length. this is useful to simplify the batching logic and more importantly to make + # automatic adaptive batches much much easier to implement + # - any OOMs will happen right away rather than near the end + toks = self.tok_encode(x[0]) + return -len(toks), x[0] + + # we group requests by their generation_kwargs, + # so that we don't try to execute e.g. greedy sampling and temp=0.8 sampling + # in the same batch. + re_ords = utils.Collator([reg.args for reg in requests], _collate, grouping=True) + chunks = re_ords.get_batched(n=self.batch_size, batch_fn=None) + num_iters = len(requests) // self.batch_size if len(requests) % self.batch_size == 0 else len(requests) // self.batch_size + 1 + pbar = tqdm(total=num_iters, disable=(self.rank != 0), desc="Model Responding") + # breakpoint() + for chunk in chunks: + contexts, all_gen_kwargs, doc_to_visual, doc_id, task, split = zip(*chunk) + task = task[0] + split = split[0] + batched_visuals = [doc_to_visual[0](self.task_dict[task][split][ids]) for ids in doc_id] # [B, N] + flattened_visuals = self.flatten(batched_visuals) # [B*N] + # we assume all gen kwargs in the batch are the same + # this is safe to assume because the `grouper` object ensures it. + gen_kwargs = all_gen_kwargs[0] + + # Set default values for until and max_new_tokens + until = [self.tok_decode(self.eot_token_id)] + + # Update values from gen_kwargs if present + if "until" in gen_kwargs: + until = gen_kwargs.pop("until") + if isinstance(until, str): + until = [until] + elif not isinstance(until, list): + raise ValueError(f"Expected `gen_kwargs['until']` to be of type Union[str,list] but got {type(until)}") + + if "image_aspect_ratio" in gen_kwargs.keys() and "image_aspect_ratio" not in self._config.__dict__: + # here we should pop it out of gen_kwargs so that it doesn't get passed to the model for next step of generation + self._config.image_aspect_ratio = gen_kwargs.pop("image_aspect_ratio") + eval_logger.info(f"Setting image aspect ratio: {self._config.image_aspect_ratio}") + # encode, pad, and truncate contexts for this batch + if flattened_visuals: + image_tensor = process_images(flattened_visuals, self._image_processor, self._config) + if type(image_tensor) is list: + image_tensor = [_image.to(dtype=torch.bfloat16, device=self.device) for _image in image_tensor] + else: + image_tensor = image_tensor.to(dtype=torch.bfloat16, device=self.device) + else: + image_tensor = None + + # prompts_input = contexts[0] + + question_input = [] + + for visual, context in zip(batched_visuals, contexts): + if image_tensor is not None and len(image_tensor) != 0 and DEFAULT_IMAGE_TOKEN not in context: + """ + Three senarios: + 1. No image, and there for, no image token should be added. + 2. image token is already specified in the context, so we don't need to add it. + 3. image token is not specified in the context and there is image inputs, so we need to add it. In this case, we add the image token at the beginning of the context and add a new line. + """ + image_tokens = [DEFAULT_IMAGE_TOKEN] * len(visual) if isinstance(visual, list) else [DEFAULT_IMAGE_TOKEN] + image_tokens = " ".join(image_tokens) + question = image_tokens + "\n" + context + else: + question = context + + if isinstance(question, list): + conv = [ {"role": "user", "content": qq} for qq in question ], + else: + conv = [{"role": "user", "content": question}] + if self.add_system_prompt: + conv.insert(0, {"role": "system", "content": self.add_system_prompt}) + prompt_question = self._tokenizer.apply_chat_template(conv, tokenize=False, add_generation_prompt=True) + question_input.append(prompt_question) + + # input_ids = tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to(self.device) + # preconfigure gen_kwargs with defaults + gen_kwargs["image_sizes"] = [flattened_visuals[idx].size for idx in range(len(flattened_visuals))] + if "max_new_tokens" not in gen_kwargs: + gen_kwargs["max_new_tokens"] = 1024 + if "temperature" not in gen_kwargs: + gen_kwargs["temperature"] = 0 + if "top_p" not in gen_kwargs: + gen_kwargs["top_p"] = None + if "num_beams" not in gen_kwargs: + gen_kwargs["num_beams"] = 1 + + input_ids_list = [tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt") for prompt in question_input] + pad_token_ids = self.tokenizer.pad_token_id if self.tokenizer.pad_token_id is not None else self.tokenizer.eos_token_id + input_ids = self.pad_sequence(input_ids_list, batch_first=True, padding_value=pad_token_ids).to(self.device) + attention_masks = input_ids.ne(pad_token_ids).to(self.device) + # These steps are not in LLaVA's original code, but are necessary for generation to work + # TODO: attention to this major generation step... + try: + cont = self.model.generate( + input_ids, + attention_mask=attention_masks, + pad_token_id=pad_token_ids, + images=image_tensor, + image_sizes=gen_kwargs["image_sizes"], + do_sample=True if gen_kwargs["temperature"] > 0 else False, + temperature=gen_kwargs["temperature"], + top_p=gen_kwargs["top_p"], + num_beams=gen_kwargs["num_beams"], + max_new_tokens=gen_kwargs["max_new_tokens"], + use_cache=self.use_cache, + ) + text_outputs = self.tokenizer.batch_decode(cont, skip_special_tokens=True) + except Exception as e: + raise e + eval_logger.error(f"Error {e} in generating") + cont = "" + text_outputs = [""] + + # cont_toks_list = cont.tolist() + # for cont_toks, context in zip(cont_toks_list, contexts): + # discard context + left-padding toks if using causal decoder-only LMM + # if self.truncate_context: + # cont_toks = cont_toks[input_ids.shape[1] :] + # use secondary stop seqs to cut off should-have-been-stopped content post-hoc + # if self.truncate_context: + # for term in until: + # if len(term) > 0: + # # ignore '' separator, + # # for seq2seq case where self.tok_decode(self.eot_token_id) = '' + # text_outputs = text_outputs.split(term)[0] + res.extend(text_outputs) + self.cache_hook.add_partial("generate_until", (context, gen_kwargs), text_outputs) + pbar.update(1) + # reorder this group of results back to original unsorted form + res = re_ords.get_original(res) + pbar.close() + return res + + def generate_until_multi_round(self, requests) -> List[str]: + raise NotImplementedError("TODO: Implement multi-round generation for LLaVA") diff --git a/lmms_eval/models/llava_v6.py b/lmms_eval/models/llava_v6.py new file mode 100755 index 000000000..ed115d3fe --- /dev/null +++ b/lmms_eval/models/llava_v6.py @@ -0,0 +1,72 @@ +from lmms_eval.models.llava_hf import LlavaHf +import warnings +from typing import List, Optional, Tuple, Union + +import numpy as np +import PIL +import torch +from transformers import ( + LlavaForConditionalGeneration, + LlavaNextForConditionalGeneration, +) + +from lmms_eval.api.registry import register_model + +warnings.filterwarnings("ignore") + +from loguru import logger as eval_logger + +DEFAULT_IMAGE_TOKEN = "" +DEFAULT_VIDEO_TOKEN = "