llava.py 19.7 KB
Newer Older
Ashvin Nihalani's avatar
Ashvin Nihalani committed
1
2
3
4
5
6
7
import logging
import warnings
from typing import List, Optional, Tuple, Union

import torch
from accelerate import Accelerator, DistributedType
from accelerate.state import AcceleratorState
Ashvin Nihalani's avatar
Ashvin Nihalani committed
8
from tqdm import tqdm
Ashvin Nihalani's avatar
Ashvin Nihalani committed
9

Ashvin Nihalani's avatar
Ashvin Nihalani committed
10
11
12
13
from lm_eval.api.instance import Instance
from lm_eval.api.model import LM
from lm_eval.api.registry import register_model
from lm_eval.models.utils import Collator
Ashvin Nihalani's avatar
Ashvin Nihalani committed
14
15


Ashvin Nihalani's avatar
Ashvin Nihalani committed
16
17
18
19
20
warnings.filterwarnings("ignore")

eval_logger = logging.getLogger("lm-eval")

try:
Ashvin Nihalani's avatar
Ashvin Nihalani committed
21
22
23
24
25
26
27
28
29
30
    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,
    )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
31
32
33
34
35
36
37
38
39
40
41
42
    from llava.model.builder import load_pretrained_model
except ImportError:
    eval_logger.error("LLaVA is not installed. Please install LLaVA to use this model.")


@register_model("llava")
class Llava(LM):
    """
    Llava Model
    """

    def __init__(
Ashvin Nihalani's avatar
Ashvin Nihalani committed
43
44
45
46
47
48
49
50
51
52
53
54
55
        self,
        pretrained: str = "liuhaotian/llava-v1.5-7b",
        truncation: Optional[bool] = True,
        device: Optional[str] = "cuda",
        dtype: Optional[Union[str, torch.dtype]] = "auto",
        batch_size: Optional[Union[int, str]] = 1,
        trust_remote_code: Optional[bool] = False,
        revision=None,
        use_flash_attention_2=False,
        conv_template="vicuna_v1",
        use_cache=True,
        truncate_context=False,  # whether to truncate the context in generation, set it False for LLaVA-1.6
        **kwargs,
Ashvin Nihalani's avatar
Ashvin Nihalani committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    ) -> None:
        super().__init__()
        # Do not use kwargs for now
        assert kwargs == {}, f"Unexpected kwargs: {kwargs}"

        accelerator = Accelerator()
        if accelerator.num_processes > 1:
            self._device = torch.device(f"cuda:{accelerator.local_process_index}")
        else:
            self._device = device
        (
            self._tokenizer,
            self._model,
            self._image_processor,
            self._max_length,
Ashvin Nihalani's avatar
Ashvin Nihalani committed
71
72
73
74
75
76
        ) = load_pretrained_model(
            pretrained,
            None,
            get_model_name_from_path(pretrained),
            device_map=self._device,
        )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
77
78
79
80
81
82
83
84
85
86
        self._config = self._model.config
        self.model.eval()
        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:
Ashvin Nihalani's avatar
Ashvin Nihalani committed
87
88
89
90
91
            assert accelerator.distributed_type in [
                DistributedType.FSDP,
                DistributedType.MULTI_GPU,
                DistributedType.DEEPSPEED,
            ], "Unsupported distributed type provided. Only DDP and FSDP are supported."
Ashvin Nihalani's avatar
Ashvin Nihalani committed
92
93
94
95
96
97
            # 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,
Ashvin Nihalani's avatar
Ashvin Nihalani committed
98
99
                    "train_batch_size": self.batch_size_per_gpu
                    * accelerator.num_processes,
Ashvin Nihalani's avatar
Ashvin Nihalani committed
100
                }
Ashvin Nihalani's avatar
Ashvin Nihalani committed
101
102
103
                AcceleratorState().deepspeed_plugin.deepspeed_config_process(
                    must_match=True, **kwargs
                )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
104
                eval_logger.info(
Ashvin Nihalani's avatar
Ashvin Nihalani committed
105
106
107
108
109
110
                    "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
            ):
Ashvin Nihalani's avatar
Ashvin Nihalani committed
111
112
                self._model = accelerator.prepare(self.model)
            else:
Ashvin Nihalani's avatar
Ashvin Nihalani committed
113
114
115
                self._model = accelerator.prepare_model(
                    self.model, evaluation_mode=True
                )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
116
117
            self.accelerator = accelerator
            if self.accelerator.is_local_main_process:
Ashvin Nihalani's avatar
Ashvin Nihalani committed
118
119
120
                eval_logger.info(
                    f"Using {accelerator.num_processes} devices with data parallelism"
                )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
            self._rank = self.accelerator.local_process_index
            self._world_size = self.accelerator.num_processes
        else:
            self.model.to(self._device)
            self._rank = 0
            self._word_size = 1

    @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]
Ashvin Nihalani's avatar
Ashvin Nihalani committed
157
158
159
        input_ids = torch.nn.utils.rnn.pad_sequence(
            input_ids, batch_first=batch_first, padding_value=padding_value
        )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
        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

Ashvin Nihalani's avatar
Ashvin Nihalani committed
180
181
182
    def tok_encode(
        self, string: str, left_truncate_len=None, add_special_tokens=None
    ) -> List[int]:
Ashvin Nihalani's avatar
Ashvin Nihalani committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
        """ """
        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):
        return self.tokenizer.decode(tokens)

    def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]:
        # TODO
        res = []
Ashvin Nihalani's avatar
Ashvin Nihalani committed
197
198
199
        pbar = tqdm(
            total=len(requests), disable=(self.rank != 0), desc="Model Responding"
        )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
200

lintangsutawika's avatar
lintangsutawika committed
201
202
        print(requests[0])
        import sys; sys.exit()
Ashvin Nihalani's avatar
Ashvin Nihalani committed
203
204
205
        for contexts, doc_to_target, doc_to_visual, doc, task in [
            reg.args for reg in requests
        ]:
Ashvin Nihalani's avatar
Ashvin Nihalani committed
206
            # encode, pad, and truncate contexts for this batch
Ashvin Nihalani's avatar
Ashvin Nihalani committed
207
            if isinstance(doc_to_target, str):
Ashvin Nihalani's avatar
Ashvin Nihalani committed
208
209
210
211
212
213
214
                continuation = doc_to_target
            else:
                continuation = doc_to_target(doc)
            visuals = [doc_to_visual(doc)]
            visuals = self.flatten(visuals)
            if visuals:
                image = process_images(visuals, self._image_processor, self._config)
Ashvin Nihalani's avatar
Ashvin Nihalani committed
215
                if isinstance(image, list):
Ashvin Nihalani's avatar
Ashvin Nihalani committed
216
217
218
219
                    image = [
                        _image.to(dtype=torch.float16, device=self.device)
                        for _image in image
                    ]
Ashvin Nihalani's avatar
Ashvin Nihalani committed
220
221
222
223
224
225
226
                else:
                    image = image.to(dtype=torch.float16, device=self.device)
            else:
                image = None

            prompts_input = contexts[0]

Ashvin Nihalani's avatar
Ashvin Nihalani committed
227
228
229
230
231
            if (
                image is not None
                and len(image) != 0
                and DEFAULT_IMAGE_TOKEN not in prompts_input
            ):
Ashvin Nihalani's avatar
Ashvin Nihalani committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
                """
                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]

            conv = conv_templates[self.conv_template].copy()
            conv.append_message(conv.roles[0], prompts_input)
            conv.append_message(conv.roles[1], None)
            prompt = conv.get_prompt()
Ashvin Nihalani's avatar
Ashvin Nihalani committed
246
247
248
249
250
251
252
            contxt_id = (
                tokenizer_image_token(
                    prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt"
                )
                .unsqueeze(0)
                .to(self.device)
            )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
253
254
255
256
            # Add the answer of the second role
            conv.messages[1][1] = continuation

            prompt = conv.get_prompt()
Ashvin Nihalani's avatar
Ashvin Nihalani committed
257
258
259
260
261
262
263
            input_ids = (
                tokenizer_image_token(
                    prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt"
                )
                .unsqueeze(0)
                .to(self.device)
            )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
264
265
266
267
            labels = input_ids.clone()
            # Context part no need to calculate for loss
            labels[0, : contxt_id.shape[1]] = -100
            with torch.inference_mode():
Ashvin Nihalani's avatar
Ashvin Nihalani committed
268
269
270
                outputs = self.model(
                    input_ids=input_ids, labels=labels, images=image, use_cache=True
                )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
271
272
273
274
            loss = outputs["loss"]
            # loss = torch.exp(loss)
            logits = outputs["logits"]
            greedy_tokens = logits.argmax(dim=-1)
Ashvin Nihalani's avatar
Ashvin Nihalani committed
275
276
277
278
            cont_toks = input_ids[:, contxt_id.shape[1] :]  # [1, seq]
            greedy_tokens = greedy_tokens[
                :, contxt_id.shape[1] : input_ids.shape[1]
            ]  # [1, seq]
Ashvin Nihalani's avatar
Ashvin Nihalani committed
279
280
281
282
283
284
            max_equal = (greedy_tokens == cont_toks).all()
            res.append((float(loss.item()), bool(max_equal)))
            pbar.update(1)
        pbar.close()
        return res

Ashvin Nihalani's avatar
Ashvin Nihalani committed
285
286
287
    def loglikelihood_rolling(
        self, requests: List[Instance]
    ) -> List[Tuple[float, bool]]:
Ashvin Nihalani's avatar
Ashvin Nihalani committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
        raise NotImplementedError()

    def flatten(self, input):
        new_list = []
        for i in input:
            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 = Collator(
            [reg.arguments for reg in requests],
            sort_fn=_collate,
            group_by="gen_kwargs",
            group_fn=lambda x: x[1],
        )
        chunks = re_ords.get_batched(n=self.batch_size)
Ashvin Nihalani's avatar
Ashvin Nihalani committed
320
321
322
323
324
        num_iters = (
            len(requests) // self.batch_size
            if len(requests) % self.batch_size == 0
            else len(requests) // self.batch_size + 1
        )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
325
326
        pbar = tqdm(total=num_iters, disable=(self.rank != 0), desc="Model Responding")
        for chunk in chunks:
lintangsutawika's avatar
lintangsutawika committed
327
328
            contexts, all_gen_kwargs, visuals = zip(*chunk)
            # task = task[0]
Ashvin Nihalani's avatar
Ashvin Nihalani committed
329

lintangsutawika's avatar
lintangsutawika committed
330
            # visuals = [doc_to_visual[0](doc[0])]
Ashvin Nihalani's avatar
Ashvin Nihalani committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
            visuals = self.flatten(visuals)
            # 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(
Ashvin Nihalani's avatar
Ashvin Nihalani committed
346
347
                        f"Expected `gen_kwargs['until']` to be of type Union[str,list] but got {type(until)}"
                    )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
348

Ashvin Nihalani's avatar
Ashvin Nihalani committed
349
350
351
352
            if (
                "image_aspect_ratio" in gen_kwargs.keys()
                and "image_aspect_ratio" not in self._config.__dict__
            ):
Ashvin Nihalani's avatar
Ashvin Nihalani committed
353
354
                # 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")
Ashvin Nihalani's avatar
Ashvin Nihalani committed
355
356
357
                eval_logger.info(
                    f"Setting image aspect ratio: {self._config.image_aspect_ratio}"
                )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
358
359
            # encode, pad, and truncate contexts for this batch
            if visuals:
Ashvin Nihalani's avatar
Ashvin Nihalani committed
360
361
362
                image_tensor = process_images(
                    visuals, self._image_processor, self._config
                )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
363
                if isinstance(image_tensor, list):
Ashvin Nihalani's avatar
Ashvin Nihalani committed
364
365
366
367
                    image_tensor = [
                        _image.to(dtype=torch.float16, device=self.device)
                        for _image in image_tensor
                    ]
Ashvin Nihalani's avatar
Ashvin Nihalani committed
368
                else:
Ashvin Nihalani's avatar
Ashvin Nihalani committed
369
370
371
                    image_tensor = image_tensor.to(
                        dtype=torch.float16, device=self.device
                    )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
372
373
374
375
376
377
378
379
            else:
                image_tensor = None

            # prompts_input = contexts[0]

            question_input = []

            for visual, context in zip(visuals, contexts):
Ashvin Nihalani's avatar
Ashvin Nihalani committed
380
381
382
383
384
                if (
                    image_tensor is not None
                    and len(image_tensor) != 0
                    and DEFAULT_IMAGE_TOKEN not in context
                ):
Ashvin Nihalani's avatar
Ashvin Nihalani committed
385
386
387
388
389
390
                    """
                    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.
                    """
Ashvin Nihalani's avatar
Ashvin Nihalani committed
391
392
393
394
395
                    image_tokens = (
                        [DEFAULT_IMAGE_TOKEN] * len(visual)
                        if isinstance(visual, list)
                        else [DEFAULT_IMAGE_TOKEN]
                    )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
                    image_tokens = " ".join(image_tokens)
                    question = image_tokens + "\n" + context
                else:
                    question = context

                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()
                question_input.append(prompt_question)

            # The above for loop has bugs. When there is no visuals, e.g. pure text,
            # there will be no for loop execute resulting in an empty question_input (because no visuals)
            # Scenario 1 won't even be execute
            if len(visuals) == 0:
                for context in contexts:
                    question = context
                    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()
                    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
Ashvin Nihalani's avatar
Ashvin Nihalani committed
421
422
423
            gen_kwargs["image_sizes"] = [
                visuals[idx].size for idx in range(len(visuals))
            ]
Ashvin Nihalani's avatar
Ashvin Nihalani committed
424
425
426
427
428
429
430
431
432
            if "max_gen_toks" not in gen_kwargs:
                gen_kwargs["max_gen_toks"] = 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

Ashvin Nihalani's avatar
Ashvin Nihalani committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
            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)
Ashvin Nihalani's avatar
Ashvin Nihalani committed
447
448
449
450
451
452
453
454
455
            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: pay 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,
Ashvin Nihalani's avatar
Ashvin Nihalani committed
456
                    image_sizes=gen_kwargs["image_sizes"],
Ashvin Nihalani's avatar
Ashvin Nihalani committed
457
458
459
460
461
462
463
                    do_sample=gen_kwargs["do_sample"],
                    temperature=gen_kwargs["temperature"],
                    top_p=gen_kwargs["top_p"],
                    num_beams=gen_kwargs["num_beams"],
                    max_new_tokens=gen_kwargs["max_gen_toks"],
                    use_cache=self.use_cache,
                )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
464
465
466
                text_outputs = self.tokenizer.batch_decode(
                    cont, skip_special_tokens=True
                )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
            except Exception as 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)
Ashvin Nihalani's avatar
Ashvin Nihalani committed
485
486
487
            self.cache_hook.add_partial(
                "generate_until", (context, gen_kwargs), text_outputs
            )
Ashvin Nihalani's avatar
Ashvin Nihalani committed
488
489
490
491
492
            pbar.update(1)
            # reorder this group of results back to original unsorted form
        res = re_ords.get_original(res)

        pbar.close()
Ashvin Nihalani's avatar
Ashvin Nihalani committed
493
        return res