vllm_causallms.py 16.6 KB
Newer Older
1
2
3
4
5
6
import copy
from importlib.util import find_spec
from typing import List, Literal, Optional, Tuple, Union

from tqdm import tqdm

baberabb's avatar
baberabb committed
7
8
9
from lm_eval.api.instance import Instance
from lm_eval.api.model import LM
from lm_eval.api.registry import register_model
10
11
12
13
14
15
16
from lm_eval.utils import (
    Collator,
    divide,
    eval_logger,
    get_rolling_token_windows,
    make_disjoint_window,
)
17

Hailey Schoelkopf's avatar
Hailey Schoelkopf committed
18

19
try:
20
    import ray
baberabb's avatar
baberabb committed
21
    from ray.util.multiprocessing import Pool
22
    from vllm import LLM, SamplingParams
baberabb's avatar
baberabb committed
23
    from vllm.transformers_utils.tokenizer import get_tokenizer
24
25
except ModuleNotFoundError:
    pass
Hailey Schoelkopf's avatar
Hailey Schoelkopf committed
26

27
eval_logger = eval_logger
baberabb's avatar
baberabb committed
28

baberabb's avatar
baberabb committed
29

baberabb's avatar
baberabb committed
30
# adapted from https://github.com/vllm-project/vllm/issues/367#issuecomment-1788341727
31
32
33
def run_inference_one_model(
    model_args: dict, sampling_params, requests: List[List[int]]
):
baberabb's avatar
baberabb committed
34
35
36
37
    llm = LLM(**model_args)
    return llm.generate(prompt_token_ids=requests, sampling_params=sampling_params)


baberabb's avatar
baberabb committed
38
39
40
41
42
43
44
45
46
47
@register_model("vllm")
class VLLM(LM):
    _DEFAULT_MAX_LENGTH = 2048

    def __init__(
        self,
        pretrained="gpt2",
        dtype: Literal["float16", "bfloat16", "float32", "auto"] = "auto",
        revision: Optional[str] = None,
        trust_remote_code: Optional[bool] = False,
baberabb's avatar
baberabb committed
48
        tokenizer: Optional[str] = None,
baberabb's avatar
baberabb committed
49
        tokenizer_mode: Literal["auto", "slow"] = "auto",
baberabb's avatar
baberabb committed
50
        tokenizer_revision: Optional[str] = None,
baberabb's avatar
baberabb committed
51
        tensor_parallel_size: int = 1,
52
        quantization: Optional[str] = None,
baberabb's avatar
baberabb committed
53
54
        max_gen_toks: int = 256,
        swap_space: int = 4,
baberabb's avatar
baberabb committed
55
        batch_size: Union[str, int] = 1,
baberabb's avatar
baberabb committed
56
        max_batch_size=None,
baberabb's avatar
baberabb committed
57
        max_length: int = None,
58
        max_model_len: int = None,
baberabb's avatar
baberabb committed
59
        seed: int = 1234,
60
        gpu_memory_utilization: float = 0.9,
baberabb's avatar
baberabb committed
61
        device: str = "cuda",
62
        data_parallel_size: int = 1,
baberabb's avatar
baberabb committed
63
64
    ):
        super().__init__()
65

66
        if not find_spec("vllm"):
67
            raise Exception(
68
69
                "attempted to use 'vllm' LM type, but package `vllm` is not installed. "
                "Please install vllm via `pip install lm-eval[vllm]` or `pip install -e .[vllm]`"
Hailey Schoelkopf's avatar
Hailey Schoelkopf committed
70
71
            )

baberabb's avatar
baberabb committed
72
        assert "cuda" in device or device is None, "vLLM only supports CUDA"
73
74
75
76
77
        assert (
            max_length is None or max_model_len is None
        ), "Either max_length or max_model_len may be provided, but not both"

        self._max_length = max_model_len if max_model_len is not None else max_length
baberabb's avatar
baberabb committed
78
        self.tensor_parallel_size = int(tensor_parallel_size)
79
        self.data_parallel_size = int(data_parallel_size)
baberabb's avatar
baberabb committed
80
81
82
83
84
        self.model_args = {
            "model": pretrained,
            "gpu_memory_utilization": float(gpu_memory_utilization),
            "revision": revision,
            "dtype": dtype,
baberabb's avatar
baberabb committed
85
            "tokenizer": tokenizer,
baberabb's avatar
baberabb committed
86
            "tokenizer_mode": tokenizer_mode,
baberabb's avatar
baberabb committed
87
            "tokenizer_revision": tokenizer_revision,
baberabb's avatar
baberabb committed
88
89
            "trust_remote_code": trust_remote_code,
            "tensor_parallel_size": int(tensor_parallel_size),
90
            "max_model_len": int(self._max_length) if self._max_length else None,
baberabb's avatar
baberabb committed
91
92
93
94
            "swap_space": int(swap_space),
            "quantization": quantization,
            "seed": int(seed),
        }
95
96
97
98
99
        self.batch_size = (
            "auto"
            if isinstance(batch_size, str) and "auto" in batch_size
            else batch_size
        )
100
        if self.data_parallel_size <= 1:
baberabb's avatar
baberabb committed
101
            self.model = LLM(**self.model_args)
baberabb's avatar
baberabb committed
102
103
        else:
            self.model_args["worker_use_ray"] = True
104
105
106
107
108
109
110
111
            self.batch_size = "auto"
            eval_logger.info("Manual batching is not compatible with data parallelism.")

            from transformers import AutoConfig

            self._config = AutoConfig.from_pretrained(
                pretrained, trust_remote_code=trust_remote_code, revision=revision
            )
baberabb's avatar
nits  
baberabb committed
112
113
114
115
116
117
        self.tokenizer = get_tokenizer(
            tokenizer if tokenizer else pretrained,
            tokenizer_mode=tokenizer_mode,
            trust_remote_code=trust_remote_code,
            tokenizer_revision=tokenizer_revision,
        )
118

baberabb's avatar
baberabb committed
119
120
121
122
123
124
125
126
127
128
129
        self._max_gen_toks = max_gen_toks

    @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):
        if self._max_length:  # if max length manually set, return it
            return self._max_length
130
131
132
133
134
135
136
137
138
139
140
141
        if self.data_parallel_size <= 1:
            return self.model.llm_engine.model_config.max_model_len
        else:
            seqlen_config_attrs = ("n_positions", "max_position_embeddings", "n_ctx")
            for attr in seqlen_config_attrs:
                if hasattr(self._config, attr):
                    return getattr(self._config, attr)
            if hasattr(self.tokenizer, "model_max_length"):
                if self.tokenizer.model_max_length == 1000000000000000019884624838656:
                    return self._DEFAULT_MAX_LENGTH
                return self.tokenizer.model_max_length
            return self._DEFAULT_MAX_LENGTH
baberabb's avatar
baberabb committed
142
143
144
145
146

    @property
    def max_gen_toks(self):
        return self._max_gen_toks

baberabb's avatar
baberabb committed
147
148
149
150
151
152
153
    def tok_encode(
        self,
        string: str,
        left_truncate_len=None,
        add_special_tokens=False,
        truncation=False,
    ):
baberabb's avatar
baberabb committed
154
        """ """
baberabb's avatar
baberabb committed
155
156
157
        encoding = self.tokenizer.encode(
            string, add_special_tokens=add_special_tokens, truncation=truncation
        )
baberabb's avatar
baberabb committed
158
159
160
161
162
163
164
165
166

        # 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 _model_generate(
        self,
baberabb's avatar
baberabb committed
167
        requests: List[List[int]] = None,
baberabb's avatar
baberabb committed
168
169
170
171
172
173
        generate: bool = False,
        max_tokens: int = None,
        stop: Optional[List[str]] = None,
        **kwargs,
    ):
        if generate:
174
            kwargs = self.modify_gen_kwargs(kwargs)
baberabb's avatar
baberabb committed
175
            sampling_params = SamplingParams(max_tokens=max_tokens, stop=stop, **kwargs)
baberabb's avatar
baberabb committed
176
        else:
baberabb's avatar
baberabb committed
177
            sampling_params = SamplingParams(
178
                temperature=0, prompt_logprobs=1, max_tokens=1
baberabb's avatar
baberabb committed
179
            )
180
        if self.data_parallel_size > 1:
181
            requests = [list(x) for x in divide(requests, self.data_parallel_size)]
baberabb's avatar
baberabb committed
182
            inputs = [(self.model_args, sampling_params, req) for req in requests]
baberabb's avatar
baberabb committed
183

184
            with Pool(self.data_parallel_size) as pool:
baberabb's avatar
baberabb committed
185
                results = pool.starmap(run_inference_one_model, inputs)
186
187
            # Invoke ray.shutdown() to prevent hang-ups if subsequent calls required.
            ray.shutdown()
baberabb's avatar
baberabb committed
188
189
190
191
192
193
            # flatten results
            return [item for sublist in results for item in sublist]

        outputs = self.model.generate(
            prompt_token_ids=requests,
            sampling_params=sampling_params,
194
            use_tqdm=True if self.batch_size == "auto" else False,
baberabb's avatar
baberabb committed
195
        )
baberabb's avatar
baberabb committed
196
197
        return outputs

baberabb's avatar
baberabb committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    def _encode_pair(
        self, context: str, continuation: str
    ) -> Tuple[List[int], List[int]]:
        n_spaces = len(context) - len(context.rstrip())
        if n_spaces > 0:
            continuation = context[-n_spaces:] + continuation
            context = context[:-n_spaces]

        whole_enc = self.tok_encode(context + continuation, add_special_tokens=False)
        context_enc = self.tok_encode(context, add_special_tokens=False)

        context_enc_len = len(context_enc)
        continuation_enc = whole_enc[context_enc_len:]
        return context_enc, continuation_enc

baberabb's avatar
baberabb committed
213
    def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]:
baberabb's avatar
baberabb committed
214
215
216
217
        new_reqs = []
        for context, continuation in [req.args for req in requests]:
            if context == "":
                # end of text as context
218
219
220
                context_enc, continuation_enc = (
                    [self.eot_token_id],
                    self.tok_encode(continuation),
baberabb's avatar
baberabb committed
221
                )
baberabb's avatar
baberabb committed
222
            else:
baberabb's avatar
baberabb committed
223
                context_enc, continuation_enc = self._encode_pair(context, continuation)
baberabb's avatar
baberabb committed
224
225
226
227
228

            new_reqs.append(((context, continuation), context_enc, continuation_enc))

        return self._loglikelihood_tokens(new_reqs)

baberabb's avatar
baberabb committed
229
    def loglikelihood_rolling(self, requests: List[Instance]) -> List[float]:
baberabb's avatar
baberabb committed
230
231
232
233
234
        loglikelihoods = []

        for (string,) in tqdm([req.args for req in requests]):
            rolling_token_windows = list(
                map(
235
236
                    make_disjoint_window,
                    get_rolling_token_windows(
baberabb's avatar
baberabb committed
237
238
                        token_list=self.tok_encode(string),
                        prefix_token=self.eot_token_id,
baberabb's avatar
baberabb committed
239
                        max_seq_len=self.max_length - 1,
baberabb's avatar
baberabb committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
                        context_len=1,
                    ),
                )
            )

            rolling_token_windows = [(None,) + x for x in rolling_token_windows]

            string_nll = self._loglikelihood_tokens(
                rolling_token_windows,
            )

            # discard is_greedy
            string_nll = [x[0] for x in string_nll]

            string_nll = sum(string_nll)
            loglikelihoods.append(string_nll)
        return loglikelihoods

    def generate_until(self, requests: List[Instance]) -> List[str]:
259
        res = []
baberabb's avatar
baberabb committed
260
261
262

        # batch tokenize contexts
        context, all_gen_kwargs = zip(*(req.args for req in requests))
263
        context_encoding = self.tokenizer(context, add_special_tokens=False).input_ids
baberabb's avatar
baberabb committed
264
265
266
        requests = [
            ((a, b), c) for a, b, c in zip(context, context_encoding, all_gen_kwargs)
        ]
baberabb's avatar
baberabb committed
267
268
269
270
271
272
273
274

        def _collate_gen(_requests):
            # 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
275
            return -len(_requests[0][1]), _requests[0][0]
baberabb's avatar
baberabb committed
276
277
278
279

        # 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.
280
281
282
283
        re_ords = Collator(requests, _collate_gen, grouping=True)
        chunks = re_ords.get_batched(
            n=int(self.batch_size) if self.batch_size != "auto" else 0, batch_fn=None
        )
baberabb's avatar
baberabb committed
284
285
286

        pbar = tqdm(total=len(requests), disable=(self.rank != 0))
        # for each different set of kwargs, we execute all requests, by batch.
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
        for chunk in chunks:
            context_and_encoding, all_gen_kwargs = zip(*chunk)
            context, context_encoding = zip(*context_and_encoding)
            # 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]
            # unpack our keyword arguments.
            until = None
            if isinstance(gen_kwargs, dict):
                kwargs = copy.deepcopy(gen_kwargs)  # edge case for repeats > 1
                if "until" in kwargs.keys():
                    until = kwargs.pop("until")
                    if isinstance(until, str):
                        until = [until]
                    elif not isinstance(until, list):
                        raise ValueError(
                            f"Expected `kwargs['until']` to be of type Union[str,list] but got {until}"
                        )
            else:
                raise ValueError(
                    f"Expected `kwargs` to be of type `dict` but got {gen_kwargs}"
baberabb's avatar
baberabb committed
308
                )
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
            if not until:
                until = [self.tokenizer.decode(self.eot_token_id)]
            if "max_gen_toks" in kwargs.keys():
                max_gen_toks = kwargs.pop("max_gen_toks")
            else:
                max_gen_toks = self.max_gen_toks

            # set the max length in tokens of inputs ("context_enc")
            # max len for inputs = max length, minus room to generate the max new tokens
            max_ctx_len = self.max_length - max_gen_toks
            context_encoding = [x[-max_ctx_len:] for x in context_encoding]

            # perform batched generation
            cont = self._model_generate(
                requests=context_encoding,
                generate=True,
                max_tokens=max_gen_toks,
                stop=until,
                **kwargs,
            )
baberabb's avatar
baberabb committed
329

330
331
332
333
334
335
336
337
            # cache generations
            for output, context in zip(cont, context):
                generated_text = output.outputs[0].text
                res.append(generated_text)
                self.cache_hook.add_partial(
                    "generate_until", (context, gen_kwargs), generated_text
                )
                pbar.update(1)
baberabb's avatar
baberabb committed
338
339

        pbar.close()
340
341
        # reorder all group of results back to original unsorted form
        return re_ords.get_original(res)
baberabb's avatar
baberabb committed
342
343

    def _loglikelihood_tokens(
baberabb's avatar
baberabb committed
344
345
346
        self,
        requests: List[Tuple[Tuple[str, str], List[int], List[int]]],
        disable_tqdm: bool = False,
baberabb's avatar
baberabb committed
347
348
349
350
351
352
353
    ) -> List[Tuple[float, bool]]:
        res = []

        def _collate(x):
            toks = x[1] + x[2]
            return -len(toks), tuple(toks)

354
355
356
357
        # Reorder requests by length and batch
        re_ord = Collator(requests, sort_fn=_collate)
        chunks = re_ord.get_batched(
            n=int(self.batch_size) if self.batch_size != "auto" else 0, batch_fn=None
baberabb's avatar
baberabb committed
358
        )
359

baberabb's avatar
baberabb committed
360
        pbar = tqdm(total=len(requests), disable=disable_tqdm)
baberabb's avatar
baberabb committed
361
        for chunk in chunks:
362
            inputs = []
baberabb's avatar
baberabb committed
363
364
365
366
367
368
369
            ctxlens = []
            for cache_key, context_enc, continuation_enc in chunk:
                inp = (context_enc + continuation_enc)[-(self.max_length) :]
                ctxlen = len(context_enc) - max(
                    0, len(context_enc) + len(continuation_enc) - (self.max_length)
                )

370
                inputs.append(inp)
baberabb's avatar
baberabb committed
371
372
                ctxlens.append(ctxlen)

373
            outputs = self._model_generate(requests=inputs, generate=False)
baberabb's avatar
baberabb committed
374

375
376
            for output, ctxlen, (cache_key, _, _), inp in zip(
                outputs, ctxlens, chunk, inputs
baberabb's avatar
baberabb committed
377
378
            ):
                answer = self._parse_logprobs(
379
380
381
                    tokens=inp,
                    outputs=output,
                    ctxlen=ctxlen,
baberabb's avatar
baberabb committed
382
383
384
385
386
387
388
                )

                res.append(answer)

                # partial caching
                if cache_key is not None:
                    self.cache_hook.add_partial("loglikelihood", cache_key, answer)
389
                pbar.update(1)
baberabb's avatar
baberabb committed
390
391
392
393
        pbar.close()
        return re_ord.get_original(res)

    @staticmethod
baberabb's avatar
baberabb committed
394
    def _parse_logprobs(tokens: List, outputs, ctxlen: int) -> Tuple[float, bool]:
baberabb's avatar
baberabb committed
395
396
397
        """Process logprobs and tokens.

        :param tokens: list
398
            Input tokens (potentially left-truncated)
baberabb's avatar
bugfix  
baberabb committed
399
        :param outputs: RequestOutput
400
            Contains prompt_logprobs
baberabb's avatar
baberabb committed
401
402
403
404
405
406
407
408
409
        :param ctxlen: int
            Length of context (so we can slice them away and only keep the predictions)
        :return:
            continuation_logprobs: float
                Log probabilities of continuation tokens
            is_greedy: bool
                Whether argmax matches given continuation exactly
        """

410
        # The first entry of prompt_logprobs is None because the model has no previous tokens to condition on.
baberabb's avatar
bugfix  
baberabb committed
411
412
        continuation_logprobs_dicts = outputs.prompt_logprobs

baberabb's avatar
baberabb committed
413
        # Calculate continuation_logprobs
414
        # assume ctxlen always >= 1
baberabb's avatar
baberabb committed
415
        continuation_logprobs = sum(
baberabb's avatar
baberabb committed
416
            logprob_dict.get(token)
baberabb's avatar
baberabb committed
417
            for token, logprob_dict in zip(
baberabb's avatar
bugfix  
baberabb committed
418
                tokens[ctxlen:], continuation_logprobs_dicts[ctxlen:]
baberabb's avatar
baberabb committed
419
420
421
422
423
            )
        )

        # Determine if is_greedy
        is_greedy = True
baberabb's avatar
baberabb committed
424
425
426
        for token, logprob_dict in zip(
            tokens[ctxlen:], continuation_logprobs_dicts[ctxlen:]
        ):
baberabb's avatar
bugfix  
baberabb committed
427
428
429
430
431
432
            # Get the token with the maximum log probability from the logprob_dict
            if logprob_dict:  # Ensure the logprob_dict is not None
                top_token = max(logprob_dict, key=logprob_dict.get)
                if top_token != token:
                    is_greedy = False
                    break
baberabb's avatar
baberabb committed
433
434

        return continuation_logprobs, is_greedy
435
436
437
438

    @staticmethod
    def modify_gen_kwargs(kwargs: dict) -> dict:
        # sampling_params
439
440
        do_sample = kwargs.pop("do_sample", None)
        if do_sample is False or "temperature" not in kwargs:
441
442
443
444
445
446
447
            kwargs["temperature"] = 0.0
        # hf defaults
        kwargs["skip_special_tokens"] = kwargs.get("skip_special_tokens", False)
        kwargs["spaces_between_special_tokens"] = kwargs.get(
            "spaces_between_special_tokens", False
        )
        return kwargs