"csrc/includes/ds_kernel_utils.h" did not exist on "1b2721adcd96656bb1f27d1f2f60947567b2d505"
flash_causal_lm.py 93.4 KB
Newer Older
1
from contextlib import nullcontext
2
import math
3
import os
4
import time
5
6
7
import torch
import torch.distributed

8
9
import numpy as np

10
from loguru import logger
11
12
from dataclasses import dataclass
from opentelemetry import trace
13
14
15
16
17
18
from transformers import (
    PreTrainedTokenizerBase,
    AutoConfig,
    AutoTokenizer,
    GenerationConfig,
)
19
20
21
22
23
24
25
26
27
28
29
from typing import (
    Any,
    ContextManager,
    Iterable,
    Optional,
    Tuple,
    List,
    Type,
    Dict,
    Union,
)
fxmarty's avatar
fxmarty committed
30

drbh's avatar
drbh committed
31
from text_generation_server.adapters import AdapterBatchData, AdapterBatchMetadata
fxmarty's avatar
fxmarty committed
32
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
Daniël de Kok's avatar
Daniël de Kok committed
33
from text_generation_server.utils.chunks import concat_text_chunks
Nicolas Patry's avatar
Nicolas Patry committed
34
from text_generation_server.utils.import_utils import SYSTEM
OlivierDehaene's avatar
OlivierDehaene committed
35
from text_generation_server.models import Model
36
from text_generation_server.utils.log import log_master
37
38
39
40
from text_generation_server.utils.prefill_chunking import (
    get_support_chunking,
    get_max_prefill_tokens,
)
41
from text_generation_server.utils.tokens import batch_top_tokens
Nicolas Patry's avatar
Nicolas Patry committed
42
from text_generation_server.utils.speculate import get_speculate
43
44
45
46
47
from text_generation_server.utils import (
    initialize_torch_distributed,
    weight_files,
    Weights,
)
48
49
from text_generation_server.models.types import (
    Batch,
Nicolas Patry's avatar
Nicolas Patry committed
50
    Tokens,
51
52
53
54
    Generation,
    GeneratedText,
)
from text_generation_server.pb import generate_pb2
Nicolas Patry's avatar
Nicolas Patry committed
55
56
from text_generation_server.models.globals import (
    MEM_POOL,
57
    ATTENTION,
58
    BLOCK_SIZE,
Nicolas Patry's avatar
Nicolas Patry committed
59
    CUDA_GRAPHS,
60
    TGI_WIGGLE_ROOM,
Nicolas Patry's avatar
Nicolas Patry committed
61
62
    get_adapter_to_index,
)
63
from text_generation_server.layers.attention import KVCache, Seqlen
64
from text_generation_server.utils import StoppingCriteria, HeterogeneousNextTokenChooser
65
from text_generation_server.utils.dist import MEMORY_FRACTION
66
from text_generation_server.utils.quantization import get_loader
drbh's avatar
drbh committed
67
from text_generation_server.utils.segments import SegmentConcatBuilder, find_segments
68

Nicolas Patry's avatar
Nicolas Patry committed
69
from text_generation_server.utils.import_utils import (
Nicolas Patry's avatar
Nicolas Patry committed
70
71
72
    empty_cache,
    synchronize,
    get_free_memory,
Nicolas Patry's avatar
Nicolas Patry committed
73
)
74
75
76
77
78
79
80
81
from text_generation_server.models.metadata_kernels import (
    has_triton,
    copy_next_input_ids_inplace,
    block_tables_to_ragged,
    block_tables_to_padded,
    prepare_position_slot_ids,
    slots_filtering,
)
Nicolas Patry's avatar
Nicolas Patry committed
82

Nicolas Patry's avatar
Nicolas Patry committed
83
84
tracer = trace.get_tracer(__name__)

85
86
87
88
# Will be set in init
SLIDING_WINDOW: Optional[int] = None


89
90
91
92
def small_power_of_2(n: int):
    return 1 << ((n - 1).bit_length() - 1)


93
94
95
96
97
98
99
100
101
def set_sliding_window(sliding_window: int):
    global SLIDING_WINDOW
    SLIDING_WINDOW = sliding_window


def get_sliding_windows() -> int:
    global SLIDING_WINDOW
    return SLIDING_WINDOW

102

103
104
105
106
107
108
109
def init_cpu_threads_env(rank_id: int, world_size: int):
    import importlib.util

    if importlib.util.find_spec("numa") is not None:
        import numa
        import psutil

110
        nodes = numa.info.get_max_node() + 1
111
112
113
114
115
116
117
118
        rank_per_node = math.ceil(world_size / nodes)
        num_cpus_per_nodes = int(psutil.cpu_count(logical=False) / nodes)
        node_id = int(rank_id / rank_per_node)
        rank_offset_per_node = rank_id % rank_per_node
        if os.getenv("OMP_NUM_THREADS") is None:
            num_cpus_per_rank = max(int(num_cpus_per_nodes / rank_per_node), 1)
        else:
            num_cpus_per_rank = int(os.getenv("OMP_NUM_THREADS"))
119
120
        if len(numa.memory.get_membind_nodes()) == nodes:
            numa.memory.set_membind_nodes((node_id))
121
        torch.set_num_threads(num_cpus_per_rank)
122
        if len(numa.schedule.get_affinitive_cpus(0)) == psutil.cpu_count(logical=True):
123
            cpu_start = num_cpus_per_rank * rank_offset_per_node
124
            numa.schedule.run_on_cpus(
125
                0,
126
127
128
129
130
                *(
                    numa.info.node_to_cpus(node_id)[
                        cpu_start : cpu_start + num_cpus_per_rank
                    ]
                ),
131
            )
132
133
134
        logger.info(
            f"affinity={numa.schedule.get_affinitive_cpus(0)}, membind = {numa.memory.get_membind_nodes()}"
        )
135
136


137
138
139
140
@dataclass
class FlashCausalLMBatch(Batch):
    batch_id: int
    requests: List[generate_pb2.Request]
141
142
    # request id -> idx in list mapping
    requests_idx_mapping: Dict[int, int]
143
144

    # Decoder values
145
146
147
148
149
    # Can be a list for easy filtering
    # If `input_ids` is a list, it needs to be materialized to a tensor first
    input_ids: Union[torch.Tensor, List[List[int]]]
    # Will be set by `generate_token` and reset after each prefill forward before staying set in decode
    position_ids: Optional[torch.Tensor]
150
    speculative_ids: Optional[torch.Tensor]
151

152
153
    # Set when creating the batch
    # tensor of indices of the currently used slots, length = \sum_{i=0}^{b} s_i in prefill, length = b in decode
154
155
    # Will be set by `generate_token` and reset after each prefill forward before staying set in decode
    slot_indices: Optional[torch.Tensor]
156
157

    # list of length b of list of length s_i // block_size
158
    block_tables: List[List[int]]
159
    # tensor of size [b, max_total_seqlen // block_size] holding the paged attention block tables for all sequences
160
    block_tables_tensor: torch.Tensor
161
    # tensor of length \sum_{i=0}^{b} max_s_i  holding the paged attention slots for all sequences
162
163
164
165
    slots: torch.Tensor
    # list of length b + 1  containing the cumulative sequence slot lengths of the sequences in the batch
    # used for filtering
    cu_slots: torch.Tensor
166

167
168
169
170
171
172
173
    max_input_length: int
    max_current_length: int

    # Whether this batch contains at least one request that is prefilling
    prefilling: bool
    # Whether each request is prefilling
    prefilling_mask: List[bool]
174

175
    # Prefill metadata tensors to efficiently compute logprobs
176
    # tensor of length b + 1  containing the cumulative sequence lengths of the sequences in the batch, only used in prefill
177
178
179
180
181
    cu_seqlen_prefill: Optional[torch.Tensor]
    # Prefill cache indices is used to slice into the kv tensor before caching it into the paged attention buffers
    # as we only keep SLIDING_WINDOW values instead of the whole tensor
    prefill_cache_indices: Optional[torch.Tensor]
    # Will be set by `generate_token` and reset after each prefill forward
182
    prefill_head_indices: Optional[torch.Tensor]
183
    # Will be set by `generate_token` and reset after each prefill forward
184
    prefill_next_token_indices: Optional[torch.tensor]
185
    # Will be set by `generate_token` and reset after each prefill forward
186
    prefill_cu_outlens: Optional[List[int]]
187
188
    # Will be set by `generate_token` and reset after each prefill forward
    prefill_logprob_tokens: List[Optional[Tokens]]
Nicolas Patry's avatar
Nicolas Patry committed
189

190
191
    # All tokens
    all_input_ids: List[List[int]]
192
    all_input_ids_tensor: torch.Tensor
193
194
195

    # Lengths of all generations present in the batch
    input_lengths: List[int]
196
197
198
199
200
201
202
203
    # size [b], containing the number of blocks that can be retrieved from the cache
    cache_lengths: List[int]
    prompt_lengths: List[int]
    # Will be set by `generate_token` and reset after each prefill forward before staying set in decode
    input_lengths_tensor: Optional[torch.Tensor]
    cache_lengths_tensor: Optional[torch.Tensor]
    prompt_lengths_tensor: torch.Tensor

204
205
    prefix_offsets: List[Optional[int]]
    read_offsets: List[Optional[int]]
206
207

    # Generation helpers
208
    next_token_chooser: HeterogeneousNextTokenChooser
209
    stopping_criterias: List[StoppingCriteria]
Nicolas Patry's avatar
Nicolas Patry committed
210
211
    top_n_tokens: List[int]
    top_n_tokens_tensor: torch.Tensor
212

drbh's avatar
drbh committed
213
    # Adapter metadata for each request
214
215
    # Will be set by `generate_token` and reset after each prefill forward before staying set in decode
    adapter_meta: Optional[AdapterBatchMetadata]
drbh's avatar
drbh committed
216

217
    # Number of blocks in this batch
218
    num_blocks: int
219
220
    # Maximum number of blocks
    max_blocks: int
221

222
223
    def to_pb(self) -> generate_pb2.CachedBatch:
        return generate_pb2.CachedBatch(
224
            id=self.batch_id,
225
            request_ids=[r.id for r in self.requests],
226
            size=len(self),
227
            max_tokens=self.num_blocks * BLOCK_SIZE,
228
229
230
231
232
            current_tokens=(
                sum([len(i) for i in self.input_ids])
                if isinstance(self.input_ids, list)
                else len(self.input_ids)
            ),
233
234
235
        )

    @classmethod
Daniël de Kok's avatar
Daniël de Kok committed
236
237
238
    def batch_tokenized_inputs(
        cls, requests: Iterable[generate_pb2.Request], tokenizer
    ):
239
240
241
        max_length = 0
        all_input_ids = []
        batch_size = 0
242
        for r in requests:
243
244
245
246
247
248
249
250
251
252
253
            batch_size += 1
            inputs = concat_text_chunks(r.input_chunks.chunks)
            input_ids = tokenizer(
                inputs,
                truncation=True,
                max_length=r.truncate,
                add_special_tokens=r.add_special_tokens,
            )["input_ids"]
            max_length = max(max_length, len(input_ids))
            all_input_ids.append(input_ids)
        return all_input_ids
254

drbh's avatar
drbh committed
255
256
257
258
259
260
261
262
263
    @classmethod
    def from_tokenized(
        cls,
        pb: generate_pb2.Batch,
        tokenizer: PreTrainedTokenizerBase,
        batch_tokenized_inputs,
        dtype: torch.dtype,
        device: torch.device,
    ) -> "FlashCausalLMBatch":
264
        speculate = get_speculate()
265

266
        cache_lengths = []
267
        input_lengths = []
268
        prompt_lengths = []
269
270
        prefix_offsets = []
        read_offsets = []
271
        all_input_ids = []
272
        all_postfix_ids = []
273
        requests_idx_mapping = {}
274
275
        slots = []
        cu_slots = [0]
276

277
        next_token_chooser_parameters = []
278
        stopping_criterias = []
Nicolas Patry's avatar
Nicolas Patry committed
279
        top_n_tokens = []
280

281
        num_blocks = 0
282
283
        max_input_length = 0
        max_current_length = 0
284
        max_length = 0
285
        max_blocks = 0
286

287
        cu_blocks = [0]
288
        block_tables = []
289
        block_tables_ragged = []
290

291
        # Parse batch
292
293
294
        for i, (r, tokenized_input) in enumerate(
            zip(pb.requests, batch_tokenized_inputs)
        ):
295
296
297
            # request id -> idx in list mapping
            requests_idx_mapping[r.id] = i

298
299
300
301
            prompt_length = len(tokenized_input)
            prompt_lengths.append(prompt_length)

            cache_length = r.cache_len
Nicolas Patry's avatar
Nicolas Patry committed
302

303
            assert (
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
                cache_length <= prompt_length
            ), f"Prefix {cache_length} vs input {prompt_length}"
            if cache_length == prompt_length:
                assert False, "unreachable"

            # `chunk_len` is an optional field in the protobuf
            # It is only set if the model support chunking
            if r.HasField("chunk_len"):
                input_length = r.chunk_len

                if cache_length + input_length < prompt_length:
                    # FIXME: speculate is not supported for context chunking at the moment
                    assert speculate == 0
                    assert get_support_chunking()
                    assert input_length > 0

                postfix_ids = tokenized_input[
                    cache_length : cache_length + input_length
                ]
                assert (
                    len(postfix_ids) == input_length
                ), "Rust and Python tokenizers are not aligned"
            else:
                # Use all the remaining ids
                postfix_ids = tokenized_input[cache_length:]
                input_length = len(postfix_ids)

331
            input_lengths.append(input_length)
332

333
334
            prefix_offsets.append(prompt_length - 5)
            read_offsets.append(prompt_length)
335

336
            all_postfix_ids.append(postfix_ids)
337
            all_input_ids.append(tokenized_input)
338

339
            next_token_chooser_parameters.append(r.parameters)
340

341
342
343
            stopping_criteria = StoppingCriteria.from_pb(
                r.stopping_parameters, tokenizer
            )
344
            max_new_tokens = stopping_criteria.max_new_tokens
345
            stopping_criterias.append(stopping_criteria)
Nicolas Patry's avatar
Nicolas Patry committed
346
            top_n_tokens.append(r.top_n_tokens)
347

348
349
            # Paged attention
            # Remove one as the first token des not have a past
Nicolas Patry's avatar
Nicolas Patry committed
350
            speculative_length = get_speculate()
drbh's avatar
drbh committed
351
            speculative_length = 0 if speculative_length is None else speculative_length
Nicolas Patry's avatar
Nicolas Patry committed
352
353

            # Tokens that need to be mapped to blocks.
354
            block_tokens = prompt_length + max_new_tokens - 1 + speculative_length
355
356
357

            # blocks and slots can be empty (for example in warmup)
            if not r.blocks:
Nicolas Patry's avatar
Nicolas Patry committed
358
                needed_blocks = math.ceil(block_tokens / BLOCK_SIZE)
359
360
361
                request_blocks = [
                    b for b in range(num_blocks, num_blocks + needed_blocks)
                ]
362
363
364
365
366
                request_slots = [
                    s
                    for b in request_blocks
                    for s in range(b * BLOCK_SIZE, (b + 1) * BLOCK_SIZE)
                ]
367
368
            else:
                request_blocks = r.blocks
369
                request_slots = r.slots
370
371

            block_tables.append(request_blocks)
372
373
374
375
376
            block_tables_ragged.extend(request_blocks)
            cu_blocks.append(len(block_tables_ragged))

            slots.extend(request_slots)
            cu_slots.append(len(slots))
Nicolas Patry's avatar
Nicolas Patry committed
377

378
            cache_lengths.append(cache_length)
379
            num_blocks += len(request_blocks)
380

381
            # Update
382
            max_blocks = max(max_blocks, len(request_blocks))
383
384
            max_input_length = max(max_input_length, input_length)
            max_current_length = max(max_current_length, cache_length + input_length)
OlivierDehaene's avatar
OlivierDehaene committed
385
            max_length = max(
386
387
                max_length,
                prompt_length + max_new_tokens + speculative_length,
OlivierDehaene's avatar
OlivierDehaene committed
388
            )
389
390

        next_token_chooser = HeterogeneousNextTokenChooser.from_pb(
drbh's avatar
drbh committed
391
            next_token_chooser_parameters, dtype, device, tokenizer
392
393
394
395
396
397
398
399
        )

        # Padded all_input_ids_tensor
        all_input_ids_tensor = np.zeros(
            (len(all_input_ids), max_length), dtype=np.int64
        )
        for i, input_ids in enumerate(all_input_ids):
            all_input_ids_tensor[i, : len(input_ids)] = input_ids
400

401
402
403
404
405
        # Create tensors on device
        all_input_ids_tensor = torch.tensor(
            all_input_ids_tensor, dtype=torch.int64, device=device
        )

Nicolas Patry's avatar
Nicolas Patry committed
406
407
408
        top_n_tokens_tensor = torch.tensor(
            top_n_tokens, device=device, dtype=torch.int64
        )
409

410
411
        block_tables_ragged = torch.tensor(
            block_tables_ragged, device=device, dtype=torch.int32
412
        )
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
        cu_blocks = torch.tensor(cu_blocks, device=device, dtype=torch.int64)
        block_tables_tensor = torch.empty(
            (len(block_tables), max_blocks),
            device=device,
            dtype=torch.int32,
        )

        # If the device supports Triton, we can use a fused kernel
        if has_triton():
            block_tables_to_padded(
                max_blocks, cu_blocks, block_tables_tensor, block_tables_ragged
            )
        else:
            for i, request_blocks in enumerate(block_tables):
                block_tables_tensor[i, : len(request_blocks)] = torch.tensor(
                    request_blocks
                )

431
432
433
        prompt_lengths_tensor = torch.tensor(
            prompt_lengths, dtype=torch.int32, device=device
        )
434

435
436
437
        slots = torch.tensor(slots, dtype=torch.int64, device=device)
        cu_slots = torch.tensor(cu_slots, dtype=torch.int64)

438
439
440
        return cls(
            batch_id=pb.id,
            requests=pb.requests,
441
            requests_idx_mapping=requests_idx_mapping,
442
            input_ids=all_postfix_ids,
443
444
            block_tables=block_tables,
            block_tables_tensor=block_tables_tensor,
445
446
447
448
449
450
            cache_lengths=cache_lengths,
            max_input_length=max_input_length,
            max_current_length=max_current_length,
            prefilling=True,
            prefilling_mask=[True] * len(pb.requests),
            prefill_logprob_tokens=[None] * len(pb.requests),
451
            input_lengths=input_lengths,
452
            prompt_lengths=prompt_lengths,
453
454
            prefix_offsets=prefix_offsets,
            read_offsets=read_offsets,
455
            all_input_ids=all_input_ids,
456
457
            all_input_ids_tensor=all_input_ids_tensor,
            next_token_chooser=next_token_chooser,
458
            stopping_criterias=stopping_criterias,
Nicolas Patry's avatar
Nicolas Patry committed
459
460
            top_n_tokens=top_n_tokens,
            top_n_tokens_tensor=top_n_tokens_tensor,
461
            num_blocks=num_blocks,
462
            max_blocks=max_blocks,
Nicolas Patry's avatar
Nicolas Patry committed
463
            speculative_ids=None,
464
465
466
467
468
469
            prompt_lengths_tensor=prompt_lengths_tensor,
            # These values will be set by `FlashCausalLMBatch.prepare_for_prefill`
            position_ids=None,
            cu_seqlen_prefill=None,
            prefill_cache_indices=None,
            slot_indices=None,
470
471
            slots=slots,
            cu_slots=cu_slots,
472
473
474
475
476
477
            prefill_head_indices=None,
            prefill_next_token_indices=None,
            prefill_cu_outlens=None,
            cache_lengths_tensor=None,
            input_lengths_tensor=None,
            adapter_meta=None,
478
479
        )

480
481
482
483
484
485
486
487
    @classmethod
    def from_pb(
        cls,
        pb: generate_pb2.Batch,
        tokenizer: PreTrainedTokenizerBase,
        dtype: torch.dtype,
        device: torch.device,
    ) -> "FlashCausalLMBatch":
488
        assert len(pb.requests) > 0
489
490
491
        batch_tokenized_inputs = cls.batch_tokenized_inputs(pb.requests, tokenizer)
        return cls.from_tokenized(pb, tokenizer, batch_tokenized_inputs, dtype, device)

492
    @tracer.start_as_current_span("filter")
493
494
    def filter(self, request_ids: List[int]) -> "FlashCausalLMBatch":
        if len(request_ids) == 0:
495
496
            raise ValueError("Batch must have at least one request")
        # We assume that if len(requests) == len(self) then the requests are the same
497
        if len(request_ids) == len(self):
498
499
            return self

500
        device = self.block_tables_tensor.device
501

502
503
504
        # New values after filtering
        requests_idx_mapping = {}

505
506
507
        # Used to index into tensors
        indices = []

508
509
510
511
512
        if not has_triton():
            # slots to keep after filtering
            slot_filtering_indices = torch.zeros(
                self.slots.shape[0], dtype=torch.bool, device=device
            )
513

514
        # Create on CPU to only move to GPU once instead of at every copy
515
        slot_indices = torch.empty(len(request_ids), dtype=torch.int64)
516
517
        max_input_length = 0
        max_current_length = 0
518

519
        requests = []
520
        block_tables = []
521
        all_input_ids = []
522
        input_ids = []
523

524
        prompt_lengths = []
525
        input_lengths = []
526
        cache_lengths = []
527
528
        prefix_offsets = []
        read_offsets = []
529
        cu_slots = [0]
530

531
532
533
        prefilling_mask = []
        prefill_logprob_tokens = []

534
        stopping_criterias = []
Nicolas Patry's avatar
Nicolas Patry committed
535
        top_n_tokens = []
drbh's avatar
drbh committed
536
        adapter_set = set()
537

538
        num_blocks = 0
539
        max_blocks = 0
540
541
        max_slots = 0
        cumulative_slot_tokens = 0
542

543
544
        for i, request_id in enumerate(request_ids):
            idx = self.requests_idx_mapping[request_id]
545
            indices.append(idx)
546
547
548
            requests_idx_mapping[request_id] = i

            requests.append(self.requests[idx])
549

550
551
552
553
            # Prefilling
            request_prefilling = self.prefilling_mask[idx]
            prefilling_mask.append(request_prefilling)

554
555
            # Get length
            request_input_length = self.input_lengths[idx]
556
557
558
559
560
            request_cache_length = self.cache_lengths[idx]
            max_input_length = max(max_input_length, request_input_length)
            max_current_length = max(
                max_current_length, request_cache_length + request_input_length
            )
561

562
563
            all_input_ids.append(self.all_input_ids[idx])

564
            prompt_lengths.append(self.prompt_lengths[idx])
565
            input_lengths.append(request_input_length)
566
            cache_lengths.append(request_cache_length)
567
568
            prefix_offsets.append(self.prefix_offsets[idx])
            read_offsets.append(self.read_offsets[idx])
569

570
571
            stopping_criteria = self.stopping_criterias[idx]
            stopping_criterias.append(stopping_criteria)
572

Nicolas Patry's avatar
Nicolas Patry committed
573
            top_n_tokens.append(self.top_n_tokens[idx])
574
            prefill_logprob_tokens.append(self.prefill_logprob_tokens[idx])
Nicolas Patry's avatar
Nicolas Patry committed
575

Nicolas Patry's avatar
Nicolas Patry committed
576
577
            ADAPTER_TO_INDEX = get_adapter_to_index()
            adapter_index = ADAPTER_TO_INDEX.get(self.requests[idx].adapter_id, 0)
drbh's avatar
drbh committed
578
579
            adapter_set.add(adapter_index)

580
            request_block_table = self.block_tables[idx]
581
            num_blocks += len(request_block_table)
582
583
            block_tables.append(request_block_table)

584
585
586
587
588
589
590
591
592
593
            start_slot = self.cu_slots[idx]
            end_slot = self.cu_slots[idx + 1]
            slot_length = end_slot - start_slot

            if not has_triton():
                # Set slice
                slot_filtering_indices[start_slot:end_slot] = True

            cu_slots.append(cumulative_slot_tokens + slot_length)

594
595
596
597
598
599
            # Input ids if the request was part of a prefilling batch
            # If the batch was decoding we can index into the tensor directly later
            if self.prefilling:
                input_ids.append(self.input_ids[idx])
            else:
                # Copy to tensor (CPU)
600
                slot_indices[i] = cumulative_slot_tokens + request_cache_length
601

602
            cumulative_slot_tokens += slot_length
603
            max_blocks = max(max_blocks, len(request_block_table))
604
            max_slots = max(max_slots, slot_length)
605

606
        all_input_ids_tensor = self.all_input_ids_tensor[indices]
607
        block_tables_tensor = self.block_tables_tensor[indices]
608
        next_token_chooser = self.next_token_chooser.filter(indices)
Nicolas Patry's avatar
Nicolas Patry committed
609
        top_n_tokens_tensor = self.top_n_tokens_tensor[indices]
OlivierDehaene's avatar
OlivierDehaene committed
610
611
612
        speculative_ids = (
            self.speculative_ids[indices] if self.speculative_ids is not None else None
        )
613
614
        prompt_lengths_tensor = self.prompt_lengths_tensor[indices]

615
616
617
618
619
620
621
622
623
624
625
626
        cu_slots = torch.tensor(cu_slots, dtype=torch.int64)

        if not has_triton():
            slots = self.slots[slot_filtering_indices]
        else:
            slots = self.slots.new_empty(cumulative_slot_tokens)
            gpu_cu_slots = cu_slots.to(device)
            slots_indexing_start = self.cu_slots.to(device)[indices]
            slots_filtering(
                max_slots, self.slots, slots, gpu_cu_slots, slots_indexing_start
            )

627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
        if self.prefilling:
            # These values will be set by `FlashCausalLMBatch.prepare_for_prefill`
            position_ids = None
            slot_indices = None
            cache_lengths_tensor = None
            input_lengths_tensor = None
            adapter_meta = None
        else:
            # Index into tensors
            input_ids = self.input_ids[indices]
            position_ids = self.position_ids[indices]
            adapter_indices = self.adapter_meta.adapter_indices[indices]
            input_lengths_tensor = self.input_lengths_tensor[indices]
            cache_lengths_tensor = self.cache_lengths_tensor[indices]

            # Move to GPU now that we have the whole tensor
            slot_indices = slot_indices.to(device)

            adapter_segments, adapter_segment_indices = find_segments(adapter_indices)
            adapter_segments = torch.tensor(
                adapter_segments, dtype=torch.int32, device=device
            )
            adapter_meta = AdapterBatchMetadata(
                adapter_indices=adapter_indices,
                adapter_set=adapter_set,
                adapter_segments=adapter_segments,
                segment_indices=adapter_segment_indices,
            )
drbh's avatar
drbh committed
655

656
        return type(self)(
657
658
659
660
661
            batch_id=self.batch_id,
            requests=requests,
            requests_idx_mapping=requests_idx_mapping,
            input_ids=input_ids,
            position_ids=position_ids,
662
            cu_seqlen_prefill=None,
663
            prefill_cache_indices=None,
664
665
666
667
            slot_indices=slot_indices,
            block_tables=block_tables,
            block_tables_tensor=block_tables_tensor,
            slots=slots,
668
            cu_slots=cu_slots,
669
670
671
672
            max_input_length=max_input_length,
            max_current_length=max_current_length,
            prefilling=self.prefilling,
            prefilling_mask=prefilling_mask,
673
674
675
            prefill_head_indices=None,
            prefill_next_token_indices=None,
            prefill_cu_outlens=None,
676
677
678
            prefill_logprob_tokens=prefill_logprob_tokens,
            prompt_lengths=prompt_lengths,
            prompt_lengths_tensor=prompt_lengths_tensor,
679
            input_lengths=input_lengths,
680
            input_lengths_tensor=input_lengths_tensor,
681
682
            cache_lengths=cache_lengths,
            cache_lengths_tensor=cache_lengths_tensor,
683
684
            prefix_offsets=prefix_offsets,
            read_offsets=read_offsets,
685
686
            all_input_ids=all_input_ids,
            all_input_ids_tensor=all_input_ids_tensor,
687
            next_token_chooser=next_token_chooser,
688
            stopping_criterias=stopping_criterias,
Nicolas Patry's avatar
Nicolas Patry committed
689
690
            top_n_tokens=top_n_tokens,
            top_n_tokens_tensor=top_n_tokens_tensor,
691
            num_blocks=num_blocks,
692
            max_blocks=max_blocks,
Nicolas Patry's avatar
Nicolas Patry committed
693
            speculative_ids=speculative_ids,
694
            adapter_meta=adapter_meta,
695
696
697
698
699
700
701
702
703
        )

    @classmethod
    @tracer.start_as_current_span("concatenate")
    def concatenate(cls, batches: List["FlashCausalLMBatch"]) -> "FlashCausalLMBatch":
        # Batch attributes
        requests = []
        requests_idx_mapping = {}

704
        prefilling = False
705
        num_blocks = 0
706
707
708
709
        total_batch_size = 0
        total_slots = 0
        max_blocks = 0
        max_length = 0
710
711
        max_input_length = 0
        max_current_length = 0
712
713
        for b in batches:
            total_batch_size += len(b)
714
            max_blocks = max(max_blocks, b.max_blocks)
715
            total_slots += len(b.slots)
716
            num_blocks += b.num_blocks
OlivierDehaene's avatar
OlivierDehaene committed
717
718
719
            speculative_length = (
                b.speculative_ids.shape[1] if b.speculative_ids is not None else 0
            )
720
721
            max_input_length = max(max_input_length, b.max_input_length)
            max_current_length = max(max_current_length, b.max_current_length)
722
723
724
            max_length = max(
                max_length,
                max(
725
                    prompt_length
726
                    + stopping_criteria.max_new_tokens
Nicolas Patry's avatar
Nicolas Patry committed
727
                    + speculative_length
728
729
                    for prompt_length, stopping_criteria in zip(
                        b.prompt_lengths, b.stopping_criterias
730
731
732
                    )
                ),
            )
733
734
            prefilling = prefilling or b.prefilling

735
736
        slots = batches[0].slots.new_empty(total_slots)
        cu_slots = torch.zeros(total_batch_size + 1, dtype=torch.int64)
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
        if prefilling:
            input_ids = []
            # These values will be set by `FlashCausalLMBatch.prepare_for_prefill`
            position_ids = None
            slot_indices = None
            cache_lengths_tensor = None
            input_lengths_tensor = None
            adapter_meta = None
            adapter_segment_builder = None
        else:
            input_ids = batches[0].input_ids.new_empty(total_batch_size)
            position_ids = batches[0].position_ids.new_empty(total_batch_size)
            slot_indices = batches[0].slot_indices.new_empty(total_batch_size)
            input_lengths_tensor = batches[0].input_lengths_tensor.new_empty(
                total_batch_size
            )
            cache_lengths_tensor = batches[0].cache_lengths_tensor.new_empty(
                total_batch_size
            )
            total_indices_size = sum(
                b.adapter_meta.adapter_indices.shape[0] for b in batches
            )
            adapter_indices = batches[0].adapter_meta.adapter_indices.new_empty(
                total_indices_size
            )
            adapter_segment_builder = SegmentConcatBuilder()
            adapter_set = set()
764

765
        prompt_lengths_tensor = batches[0].prompt_lengths_tensor.new_empty(
766
767
768
769
770
771
772
            total_batch_size
        )
        block_tables_tensor = batches[0].block_tables_tensor.new_zeros(
            (total_batch_size, max_blocks)
        )
        all_input_ids_tensor = batches[0].all_input_ids_tensor.new_zeros(
            (total_batch_size, max_length)
773
        )
Nicolas Patry's avatar
Nicolas Patry committed
774
775
776
        top_n_tokens_tensor = batches[0].top_n_tokens_tensor.new_zeros(
            total_batch_size,
        )
777

778
        block_tables = []
779
        cache_lengths = []
780
781
        all_input_ids = []

782
        prompt_lengths = []
783
        input_lengths = []
784
785
        prefix_offsets = []
        read_offsets = []
786

787
788
        prefill_logprob_tokens = []

789
        next_token_chooser_parameters = []
790
        fsm_grammar_states = []
791
        stopping_criterias = []
Nicolas Patry's avatar
Nicolas Patry committed
792
        top_n_tokens = []
793
        prefilling_mask = []
794

795
        # Cumulative length
796
        cumulative_batch_size = 0
797
        cumulative_slots = 0
drbh's avatar
drbh committed
798
        cumulative_adapter_indices_size = 0
799
800
801

        for i, batch in enumerate(batches):
            requests.extend(batch.requests)
802
803
804
805
806
807
808
809

            if i == 0:
                requests_idx_mapping = batch.requests_idx_mapping
            else:
                # We need to offset the mapping for each batch by the cumulative batch size
                for k, v in batch.requests_idx_mapping.items():
                    requests_idx_mapping[k] = v + cumulative_batch_size

810
811
812
813
            start_index = cumulative_batch_size
            end_index = cumulative_batch_size + len(batch)

            # Copy tensors (GPU)
Nicolas Patry's avatar
Nicolas Patry committed
814
            top_n_tokens_tensor[start_index:end_index] = batch.top_n_tokens_tensor
815
816
817
            all_input_ids_tensor[
                start_index:end_index, : batch.all_input_ids_tensor.shape[1]
            ] = batch.all_input_ids_tensor[:, :max_length]
818

819
820
821
            block_tables_tensor[
                start_index:end_index, : batch.block_tables_tensor.shape[1]
            ] = batch.block_tables_tensor[:, :max_blocks]
822
            prompt_lengths_tensor[start_index:end_index] = batch.prompt_lengths_tensor
823

824
825
826
827
828
829
            slots_start_index = cumulative_slots
            slots_end_index = cumulative_slots + len(batch.slots)
            slots[slots_start_index:slots_end_index] = batch.slots
            cu_slots[start_index + 1 : end_index + 1] = (
                batch.cu_slots[1:] + cumulative_slots
            )
830

831
            if not prefilling:
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
                input_ids[start_index:end_index] = batch.input_ids
                position_ids[start_index:end_index] = batch.position_ids
                slot_indices[start_index:end_index] = (
                    batch.slot_indices + cumulative_slots
                )
                input_lengths_tensor[start_index:end_index] = batch.input_lengths_tensor
                cache_lengths_tensor[start_index:end_index] = batch.cache_lengths_tensor

                # Copy over adapter indices
                adapter_start_index = cumulative_adapter_indices_size
                adapter_end_index = (
                    cumulative_adapter_indices_size
                    + batch.adapter_meta.adapter_indices.shape[0]
                )
                adapter_indices[adapter_start_index:adapter_end_index] = (
                    batch.adapter_meta.adapter_indices
                )
                cumulative_adapter_indices_size = adapter_end_index
                adapter_set.update(batch.adapter_meta.adapter_set)
                adapter_segment_builder.concat(
                    batch.adapter_meta.adapter_segments,
                    batch.adapter_meta.segment_indices,
                )
            else:
                if isinstance(batch.input_ids, torch.Tensor):
                    batch.input_ids = batch.input_ids.view(-1, 1).tolist()
                input_ids.extend(batch.input_ids)
859

860
            prefilling_mask.extend(batch.prefilling_mask)
861
            block_tables.extend(batch.block_tables)
862
            cache_lengths.extend(batch.cache_lengths)
863
864
            all_input_ids.extend(batch.all_input_ids)

865
            prompt_lengths.extend(batch.prompt_lengths)
866
            input_lengths.extend(batch.input_lengths)
867
868
            prefix_offsets.extend(batch.prefix_offsets)
            read_offsets.extend(batch.read_offsets)
869

870
871
            prefill_logprob_tokens.extend(batch.prefill_logprob_tokens)

872
            next_token_chooser_parameters.extend([r.parameters for r in batch.requests])
873
            fsm_grammar_states.extend(batch.next_token_chooser.fsm_grammar_states)
874
875
            stopping_criterias.extend(batch.stopping_criterias)

Nicolas Patry's avatar
Nicolas Patry committed
876
877
            top_n_tokens.extend(batch.top_n_tokens)

878
            # Update
879
            cumulative_slots += len(batch.slots)
880
            cumulative_batch_size += len(batch)
881

882
        next_token_chooser = HeterogeneousNextTokenChooser.from_pb(
883
884
885
            next_token_chooser_parameters,
            dtype=batches[0].next_token_chooser.dtype,
            device=batches[0].next_token_chooser.device,
drbh's avatar
drbh committed
886
            tokenizer=batches[0].next_token_chooser.tokenizer,
887
            fsm_grammar_states=fsm_grammar_states,
888
889
        )

OlivierDehaene's avatar
OlivierDehaene committed
890
891
892
893
894
        speculative_ids = (
            torch.cat([b.speculative_ids for b in batches], dim=0)
            if batches[0].speculative_ids is not None
            else None
        )
Nicolas Patry's avatar
Nicolas Patry committed
895

896
897
898
899
900
901
902
903
        if adapter_segment_builder is not None:
            adapter_segments, adapter_segment_indices = adapter_segment_builder.build()
            adapter_meta = AdapterBatchMetadata(
                adapter_indices=adapter_indices,
                adapter_set=adapter_set,
                adapter_segments=adapter_segments,
                segment_indices=adapter_segment_indices,
            )
drbh's avatar
drbh committed
904

905
        return cls(
906
907
            batch_id=batches[0].batch_id,
            requests=requests,
908
            requests_idx_mapping=requests_idx_mapping,
909
910
            input_ids=input_ids,
            position_ids=position_ids,
911
            cu_seqlen_prefill=None,
912
            prefill_cache_indices=None,
913
914
915
            slot_indices=slot_indices,
            block_tables=block_tables,
            block_tables_tensor=block_tables_tensor,
916
917
            cache_lengths=cache_lengths,
            cache_lengths_tensor=cache_lengths_tensor,
918
            slots=slots,
919
            cu_slots=cu_slots,
920
921
922
923
            max_input_length=max_input_length,
            max_current_length=max_current_length,
            prefilling=prefilling,
            prefilling_mask=prefilling_mask,
924
925
926
            prefill_head_indices=None,
            prefill_next_token_indices=None,
            prefill_cu_outlens=None,
927
928
929
            prefill_logprob_tokens=prefill_logprob_tokens,
            prompt_lengths=prompt_lengths,
            prompt_lengths_tensor=prompt_lengths_tensor,
930
            input_lengths=input_lengths,
931
            input_lengths_tensor=input_lengths_tensor,
932
933
            prefix_offsets=prefix_offsets,
            read_offsets=read_offsets,
934
935
            all_input_ids=all_input_ids,
            all_input_ids_tensor=all_input_ids_tensor,
936
            next_token_chooser=next_token_chooser,
937
            stopping_criterias=stopping_criterias,
Nicolas Patry's avatar
Nicolas Patry committed
938
939
            top_n_tokens=top_n_tokens,
            top_n_tokens_tensor=top_n_tokens_tensor,
940
            num_blocks=num_blocks,
941
            max_blocks=max_blocks,
OlivierDehaene's avatar
OlivierDehaene committed
942
            speculative_ids=speculative_ids,
943
944
945
946
947
948
949
950
951
            adapter_meta=adapter_meta,
        )

    def prepare_for_prefill(self):
        # Prepare values if we need to continue prefilling
        # Speculation must be ignored while we prefill even with chunking
        # it simplifies everything
        assert self.speculative_ids is None

952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
        device = self.block_tables_tensor.device

        if isinstance(self.input_ids, list):
            if len(self) > 1:
                input_ids = np.concatenate(self.input_ids, dtype=np.int64)
            else:
                input_ids = self.input_ids[0]
            self.input_ids = torch.tensor(input_ids, dtype=torch.int64, device=device)

        self.input_lengths_tensor = torch.tensor(
            self.input_lengths, dtype=torch.int32, device=device
        )
        self.cu_seqlen_prefill = torch.nn.functional.pad(
            torch.cumsum(self.input_lengths_tensor, dim=0), (1, 0)
        ).to(torch.int32)
        self.cache_lengths_tensor = torch.tensor(
            self.cache_lengths, dtype=torch.int32, device=device
        )

        # If the device supports Triton, we can use a fused kernel
        if has_triton():
            self.position_ids = torch.empty(
                len(self.input_ids), dtype=torch.int32, device=device
            )
            self.slot_indices = torch.empty(
                len(self.input_ids), dtype=torch.int64, device=device
            )
            cu_slots_gpu = self.cu_slots.to(device)

            prepare_position_slot_ids(
                self.max_input_length,
                self.cache_lengths_tensor,
                self.cu_seqlen_prefill,
                cu_slots_gpu,
                self.position_ids,
                self.slot_indices,
            )

990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
        sliding_window = get_sliding_windows()
        position_ids = []
        slot_indices = []
        prefill_cache_indices = []
        all_prefill_logprobs = True
        no_prefill_logprobs = True
        prefill_cu_outlens = [0]

        # Cumulative length
        cumulative_length = 0
        cumulative_slot_tokens = 0
        prefill_out_cumulative_length = 0

        adapter_indices_list = []
        adapter_set = set()

        for i, (
            r,
            cache_length,
            input_length,
            prompt_length,
            request_prefilling,
            blocks,
        ) in enumerate(
            zip(
                self.requests,
                self.cache_lengths,
                self.input_lengths,
                self.prompt_lengths,
                self.prefilling_mask,
                self.block_tables,
            )
        ):
            next_chunk_length = input_length

1025
1026
1027
1028
1029
1030
            if not has_triton():
                # Position ids
                request_position_ids = torch.arange(
                    cache_length, cache_length + input_length, dtype=torch.int32
                )
                position_ids.append(request_position_ids)
1031

1032
1033
1034
1035
1036
1037
1038
1039
                if not r.slots:
                    request_slots = [
                        s
                        for b in blocks
                        for s in range(b * BLOCK_SIZE, (b + 1) * BLOCK_SIZE)
                    ]
                else:
                    request_slots = r.slots
1040

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
                request_slot_indices = torch.arange(
                    cache_length + cumulative_slot_tokens,
                    cache_length + cumulative_slot_tokens + input_length,
                    dtype=torch.int64,
                )

                slot_indices.append(request_slot_indices)

                # Update
                cumulative_slot_tokens += len(request_slots)
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076

            # Create tensor to slice into the kv tensor in prefill
            if sliding_window is not None:
                request_prefill_cache_indices = torch.arange(
                    cumulative_length + max(0, input_length - sliding_window),
                    cumulative_length + input_length,
                    dtype=torch.int64,
                )

            # Prefill logprobs is ignored if the request is done prefilling
            prefill_logprobs = r.prefill_logprobs and request_prefilling

            all_prefill_logprobs = all_prefill_logprobs and prefill_logprobs
            no_prefill_logprobs = no_prefill_logprobs and not prefill_logprobs

            if prefill_logprobs:
                prefill_cu_outlens.append(prefill_out_cumulative_length + input_length)
                prefill_out_cumulative_length += input_length
            else:
                prefill_cu_outlens.append(prefill_out_cumulative_length + 1)
                prefill_out_cumulative_length += 1

            if sliding_window is not None:
                prefill_cache_indices.append(request_prefill_cache_indices)

            ADAPTER_TO_INDEX = get_adapter_to_index()
1077
1078
1079
1080
1081
1082
            if ADAPTER_TO_INDEX:
                adapter_index = ADAPTER_TO_INDEX.get(r.adapter_id, 0)
                adapter_indices_list.append(
                    torch.full((next_chunk_length,), adapter_index)
                )
                adapter_set.add(adapter_index)
1083
1084
1085
1086

            # Update
            cumulative_length += next_chunk_length

1087
1088
1089
        if not all_prefill_logprobs and not no_prefill_logprobs:
            prefill_head_indices = []
            prefill_next_token_indices = []
1090

1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
            # Cumulative length
            cumulative_length = 0
            prefill_out_cumulative_length = 0

            for i, (
                r,
                input_length,
                request_prefilling,
            ) in enumerate(
                zip(
                    self.requests,
                    self.input_lengths,
                    self.prefilling_mask,
                )
            ):
                # Prefill logprobs is ignored if the request is done prefilling
                prefill_logprobs = r.prefill_logprobs and request_prefilling

                if prefill_logprobs:
                    prefill_head_indices.append(
                        torch.arange(
                            cumulative_length,
                            cumulative_length + input_length,
                            dtype=torch.int64,
                        )
                    )
                    prefill_next_token_indices.append(
                        prefill_out_cumulative_length + input_length - 1
                    )
                    prefill_out_cumulative_length += input_length
                else:
                    prefill_head_indices.append(
                        torch.tensor(
                            [cumulative_length + input_length - 1],
                            dtype=torch.int64,
                        )
                    )
                    prefill_next_token_indices.append(prefill_out_cumulative_length)
                    prefill_out_cumulative_length += 1

                # Update
                cumulative_length += input_length
1133
1134

        if len(self) > 1:
1135
1136
1137
1138
            if position_ids:
                position_ids = torch.cat(position_ids)
            if slot_indices:
                slot_indices = torch.cat(slot_indices)
1139
1140
1141
            if sliding_window is not None:
                prefill_cache_indices = torch.cat(prefill_cache_indices)
        else:
1142
1143
1144
1145
            if position_ids:
                position_ids = position_ids[0]
            if slot_indices:
                slot_indices = slot_indices[0]
1146
1147
1148
            if sliding_window is not None:
                prefill_cache_indices = prefill_cache_indices[0]

1149
1150
1151
1152
        if not has_triton():
            self.position_ids = position_ids.to(device)
            self.slot_indices = slot_indices.to(device)

1153
1154
1155
1156
1157
1158
1159
        self.prefill_cu_outlens = prefill_cu_outlens
        self.prefill_cache_indices = (
            prefill_cache_indices.to(device) if sliding_window is not None else None
        )

        if all_prefill_logprobs:
            prefill_head_indices = None
1160
            prefill_next_token_indices = self.cu_seqlen_prefill[1:] - 1
1161
        elif no_prefill_logprobs:
1162
            prefill_head_indices = self.cu_seqlen_prefill[1:] - 1
1163
1164
1165
1166
1167
1168
1169
1170
1171
            prefill_next_token_indices = None
        else:
            prefill_head_indices = torch.cat(prefill_head_indices).to(device)
            prefill_next_token_indices = torch.tensor(
                prefill_next_token_indices, dtype=torch.int64, device=device
            )

        self.prefill_head_indices = prefill_head_indices
        self.prefill_next_token_indices = prefill_next_token_indices
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182

        if adapter_set:
            adapter_indices = torch.cat(adapter_indices_list).to(
                dtype=torch.int64, device=device
            )
            adapter_segments, adapter_segment_indices = find_segments(adapter_indices)
        else:
            adapter_indices = torch.zeros_like(self.input_ids)
            adapter_segments = [0, len(adapter_indices)]
            adapter_segment_indices = [len(adapter_indices) - 1]

1183
1184
1185
        adapter_segments = torch.tensor(
            adapter_segments, dtype=torch.int32, device=device
        )
1186

1187
1188
1189
1190
1191
        self.adapter_meta = AdapterBatchMetadata(
            adapter_indices=adapter_indices,
            adapter_set=adapter_set,
            adapter_segments=adapter_segments,
            segment_indices=adapter_segment_indices,
1192
1193
1194
1195
1196
1197
        )

    def __len__(self):
        return len(self.requests)


1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
ADAPTER_LAYERS = [
    "q_proj",
    "k_proj",
    "v_proj",
    "o_proj",
    "gate_proj",
    "up_proj",
    "down_proj",
]
ROW_PARALLEL = {"o_proj", "down_proj", "lm_head"}


1210
1211
1212
class FlashCausalLM(Model):
    def __init__(
        self,
drbh's avatar
drbh committed
1213
        model_id: str,
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
        model_class,
        revision: Optional[str] = None,
        quantize: Optional[str] = None,
        speculator: Optional[str] = None,
        dtype: Optional[torch.dtype] = None,
        trust_remote_code: bool = False,
        lora_adapter_ids: Optional[list] = [],
        tokenizer_class: PreTrainedTokenizerBase = AutoTokenizer,
        config_class: PreTrainedTokenizerBase = AutoConfig,
        default_dtype=torch.float16,
        aliases=None,
        # Used for Santacoder override of config
1226
1227
1228
        num_kv_heads: Optional[int] = None,
        # Deepseek V2 uses different QK and V dims.
        head_size: Optional[int] = None,
1229
        skip_special_tokens: bool = True,
1230
        kv_cache_dtype: Optional[torch.dtype] = None,
1231
        support_chunking: bool = True,
1232
    ):
Nicolas Patry's avatar
Nicolas Patry committed
1233
        self.quantize = quantize
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
        self.process_group, rank, world_size = initialize_torch_distributed()
        if torch.cuda.is_available():
            device = torch.device(f"cuda:{rank}")
            dtype = default_dtype if dtype is None else dtype
        elif SYSTEM == "ipex":
            if hasattr(torch, "xpu") and torch.xpu.is_available():
                device = torch.device(f"xpu:{rank}")
                dtype = default_dtype if dtype is None else dtype
            else:
                device = torch.device("cpu")
                dtype = torch.bfloat16 if dtype is None else dtype
1245
                init_cpu_threads_env(rank_id=rank, world_size=world_size)
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
        else:
            raise NotImplementedError(f"{model_class} is only available on GPU")

        tokenizer = tokenizer_class.from_pretrained(
            model_id,
            revision=revision,
            padding_side="left",
            truncation_side="left",
            trust_remote_code=trust_remote_code,
        )
        try:
            generation_config = GenerationConfig.from_pretrained(
                model_id, revision=revision, trust_remote_code=trust_remote_code
            )
            if isinstance(generation_config.eos_token_id, (list, set)):
                # TODO Huge hack
                tokenizer._eos_token_ids = set(generation_config.eos_token_id)
        except Exception:
            pass

        config = config_class.from_pretrained(
            model_id, revision=revision, trust_remote_code=trust_remote_code
        )
        config.quantize = quantize
        config.speculator = speculator

        torch.distributed.barrier(group=self.process_group)

1274
        weights_loader = get_loader(quantize, model_id, revision)
1275
1276
        filenames = weight_files(model_id, revision=revision, extension=".safetensors")
        weights = Weights(
1277
1278
1279
1280
1281
1282
            filenames,
            device,
            dtype,
            process_group=self.process_group,
            aliases=aliases,
            weights_loader=weights_loader,
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
        )

        prefix = ""
        model = model_class(prefix, config, weights)
        torch.distributed.barrier(group=self.process_group)

        # VLM models define the config we care about in their text_config
        text_config = getattr(config, "text_config", None)
        if text_config is not None:
            config = text_config
1293
1294
1295
1296
1297
1298

        if getattr(config, "sliding_window", None) is not None:
            set_sliding_window(config.sliding_window)
        else:
            config.sliding_window = None

1299
        self.num_layers = config.num_hidden_layers
1300
        self.num_heads = config.num_attention_heads // self.process_group.size()
1301
1302
        # Validation is done in the model itself
        if num_kv_heads is None:
1303
1304
            num_kv_heads = getattr(config, "num_key_value_heads", None)
            # GPT-2 workaround
1305
            if num_kv_heads is None:
1306
1307
1308
                num_kv_heads = getattr(config, "n_head", None)
        if num_kv_heads is None:
            raise ValueError("Cannot get the number of key/value heads")
1309
1310
1311
1312
1313
1314
        self.num_kv_heads = (
            num_kv_heads // self.process_group.size()
            if num_kv_heads > 1
            else num_kv_heads
        )
        assert self.num_kv_heads > 0
1315
1316

        if head_size is None:
Nicolas Patry's avatar
Nicolas Patry committed
1317
1318
1319
1320
1321
1322
            # Some models use GQA and different sizes for o_proj
            # and q_proj, that allows for that.
            if hasattr(config, "head_dim"):
                self.head_size = config.head_dim
            else:
                self.head_size = config.hidden_size // config.num_attention_heads
1323
1324
        else:
            self.head_size = head_size
1325

1326
        self.cuda_graphs = {}
1327
        self.kv_cache = []
1328
        self.kv_cache_dtype = dtype if kv_cache_dtype is None else kv_cache_dtype
1329

1330
        if ATTENTION == "flashinfer":
Nicolas Patry's avatar
Nicolas Patry committed
1331
            from text_generation_server.layers.attention.flashinfer import (
1332
1333
                create_prefill_state,
                create_decode_state,
Nicolas Patry's avatar
Nicolas Patry committed
1334
                create_prefill_with_paged_kv_state,
1335
1336
1337
            )

            self.prefill_state = create_prefill_state(device=device)
Nicolas Patry's avatar
Nicolas Patry committed
1338
1339
1340
            self.prefill_with_paged_kv_state = create_prefill_with_paged_kv_state(
                device=device
            )
1341

Nicolas Patry's avatar
Nicolas Patry committed
1342
1343
1344
1345
1346
            self.decode_state = create_decode_state(
                device=device,
                num_heads=self.num_heads,
                num_kv_heads=self.num_kv_heads,
            )
1347

1348
        super().__init__(
drbh's avatar
drbh committed
1349
            model_id=model_id,
1350
            model=model,
1351
1352
1353
1354
            tokenizer=tokenizer,
            requires_padding=False,
            dtype=dtype,
            device=device,
1355
1356
            rank=rank,
            world_size=world_size,
1357
            sliding_window=config.sliding_window,
1358
            support_chunking=support_chunking,
1359
1360
1361
1362
1363
1364
        )

    @property
    def batch_type(self) -> Type[FlashCausalLMBatch]:
        return FlashCausalLMBatch

1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
    def max_past(self) -> int:
        return getattr(self.model, "max_past", None)

    def init_kv_cache(
        self,
        num_blocks: int,
        num_layers: int,
        num_heads: int,
        head_size: int,
        dtype: torch.dtype,
        device: torch.device,
    ):
        self.kv_cache = []
        empty_cache()
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
        self.kv_cache = [
            KVCache(
                num_blocks=num_blocks,
                num_heads=num_heads,
                head_size=head_size,
                dtype=dtype,
                device=device,
            )
            for _ in range(num_layers)
        ]
1389

1390
1391
1392
    def cuda_graph_warmup(self, bs: int, max_s: int, max_bt: int):
        input_ids = torch.zeros(bs, dtype=torch.int64, device=self.device)
        position_ids = torch.zeros(bs, dtype=torch.int32, device=self.device)
1393
        slots = torch.arange(bs, dtype=torch.int64, device=self.device)
Nicolas Patry's avatar
Nicolas Patry committed
1394
        input_lengths = [max_s] * bs
1395
        cache_lengths = [0] * bs
Nicolas Patry's avatar
Nicolas Patry committed
1396
1397
        input_lengths_tensor = (
            torch.ones(bs, dtype=torch.int32, device=self.device) * max_s
1398
        )
1399
        cache_lengths_tensor = torch.zeros(bs, dtype=torch.int32, device=self.device)
Nicolas Patry's avatar
Nicolas Patry committed
1400
1401
1402
1403
1404
1405
1406
1407
1408
        block_tables = torch.arange(
            max_bt, dtype=torch.int32, device=self.device
        ).repeat(bs)
        block_tables = block_tables.reshape((bs, max_bt))

        if ATTENTION == "flashinfer":
            block_tables = block_tables_to_ragged(
                block_tables=block_tables,
                input_lengths=input_lengths,
1409
                cache_lengths=cache_lengths,
1410
1411
1412
                input_lengths_tensor=input_lengths_tensor,
                cache_lengths_tensor=cache_lengths_tensor,
                max_current_length=max_s,
Nicolas Patry's avatar
Nicolas Patry committed
1413
1414
            )
            from text_generation_server.layers.attention.flashinfer import (
1415
1416
1417
1418
1419
1420
1421
1422
1423
                create_decode_state_cuda_graphs,
            )

            block_tables_ptr = torch.zeros(
                bs + 1, dtype=torch.int32, device=self.device
            )
            last_page_len = torch.ones(bs, dtype=torch.int32, device=self.device)
            state = create_decode_state_cuda_graphs(
                device=input_ids.device,
Nicolas Patry's avatar
Nicolas Patry committed
1424
                block_tables=block_tables,
1425
1426
1427
1428
1429
1430
1431
1432
                block_tables_ptr=block_tables_ptr,
                last_page_len=last_page_len,
                num_heads=self.num_heads,
                num_kv_heads=self.num_kv_heads,
            )
        else:
            state = None

drbh's avatar
drbh committed
1433
1434
1435
1436
1437
1438
1439
1440
        if (
            hasattr(self.model, "config")
            and hasattr(self.model.config, "model_type")
            and self.model.config.model_type == "qwen2_vl"
        ):
            if position_ids.dim() == 1:
                position_ids = self.model.get_position_ids(input_ids)

1441
1442
1443
1444
1445
1446
1447
1448
        graph = torch.cuda.CUDAGraph()
        self.cuda_graphs[bs] = {
            "input_ids": input_ids,
            "position_ids": position_ids,
            "kv_cache": self.kv_cache,
            "block_tables": block_tables,
            "slots": slots,
            "input_lengths": input_lengths_tensor,
1449
            "cache_lengths": cache_lengths_tensor,
1450
1451
1452
1453
            "state": state,
            "graph": graph,
        }

1454
1455
        torch.cuda.synchronize()
        # Run once outside to warmup
1456
        with self._forward_context(
1457
            block_tables=block_tables,
1458
            cu_seqlen_prefill=None,
Nicolas Patry's avatar
Nicolas Patry committed
1459
            input_lengths_tensor=input_lengths_tensor,
1460
            state=state,
1461
            cache_lengths_tensor=cache_lengths_tensor,
1462
        ):
1463
1464
            seqlen = Seqlen(
                input_lengths=input_lengths_tensor,
1465
                cache_lengths=cache_lengths_tensor,
1466
1467
1468
1469
                cu_seqlen_q=None,
                max_q=1,
                max_k=max_s,
            )
1470
            self.model.forward(
1471
1472
1473
                input_ids=input_ids,
                position_ids=position_ids,
                cu_seqlen_prefill=None,
1474
                kv_cache=self.kv_cache,
1475
1476
                block_tables=block_tables,
                slots=slots,
1477
                seqlen=seqlen,
1478
                max_s=max_s,
1479
                prefill_cache_indices=None,
1480
1481
                lm_head_indices=None,
            )
1482
            del seqlen
1483
1484
1485
1486

            torch.cuda.synchronize()

            with torch.cuda.graph(graph, pool=MEM_POOL):
1487
1488
                seqlen = Seqlen(
                    input_lengths=input_lengths_tensor,
1489
                    cache_lengths=cache_lengths_tensor,
1490
1491
1492
1493
                    cu_seqlen_q=None,
                    max_q=1,
                    max_k=max_s,
                )
1494
1495
1496
1497
1498
1499
1500
                logits, speculative_logits = self.model.forward(
                    input_ids=input_ids,
                    position_ids=position_ids,
                    cu_seqlen_prefill=None,
                    kv_cache=self.kv_cache,
                    block_tables=block_tables,
                    slots=slots,
1501
                    seqlen=seqlen,
1502
1503
1504
1505
1506
1507
                    max_s=max_s,
                    prefill_cache_indices=None,
                    lm_head_indices=None,
                )
                self.cuda_graphs[bs]["logits"] = logits
                self.cuda_graphs[bs]["speculative_logits"] = speculative_logits
1508
1509
        torch.cuda.synchronize()

1510
1511
1512
1513
1514
1515
    def warmup(
        self,
        batch: FlashCausalLMBatch,
        max_input_tokens: Optional[int],
        max_total_tokens: Optional[int],
    ):
1516
        # The warmup batch is the biggest batch we could ever receive
1517
        self.kv_cache = []
Nicolas Patry's avatar
Nicolas Patry committed
1518
1519
        empty_cache()

1520
1521
1522
1523
1524
1525
        # Inspired by the original implementation in [vllm](https://github.com/vllm-project/vllm)
        # Calculate the number of blocks that can be allocated with the free memory
        dtype_size = torch.tensor([], dtype=self.kv_cache_dtype).element_size()
        cache_block_size = BLOCK_SIZE * self.num_kv_heads * self.head_size
        total_cache_size = self.num_layers * cache_block_size * 2 * dtype_size

1526
        try:
1527
1528
            self.init_kv_cache(
                batch.num_blocks,
1529
1530
1531
                self.num_layers,
                self.num_kv_heads,
                self.head_size,
1532
                self.kv_cache_dtype,
1533
1534
                self.device,
            )
1535
            max_bt = batch.max_blocks
1536
            max_s = max_bt * BLOCK_SIZE
1537
            batch_num_blocks = batch.num_blocks
fxmarty's avatar
fxmarty committed
1538
1539
1540

            if SYSTEM == "rocm" and os.environ.get("PYTORCH_TUNABLEOP_ENABLED", False):
                torch.cuda.tunable.tuning_enable(False)
1541
            _, _batch, _ = self.generate_token(batch)
OlivierDehaene's avatar
OlivierDehaene committed
1542
        except torch.cuda.OutOfMemoryError as e:
1543
            raise RuntimeError(
1544
                f"Not enough memory to handle {batch.to_pb().current_tokens} prefill tokens. "
1545
                f"You need to decrease `--max-batch-prefill-tokens`"
1546
            ) from e
1547

Nicolas Patry's avatar
Nicolas Patry committed
1548
        synchronize(self.device)
1549

Nicolas Patry's avatar
Nicolas Patry committed
1550
        free_memory = get_free_memory(self.device, MEMORY_FRACTION)
1551
1552

        num_blocks = (
1553
            # Leave 5% for some wiggle room
1554
            int((free_memory * TGI_WIGGLE_ROOM) // total_cache_size)
1555
            # Add batch.num_blocks as we allocated it above, so it is included in the peak memory.
drbh's avatar
drbh committed
1556
            + batch_num_blocks
1557
1558
        )

1559
        log_master(logger.info, f"KV-cache blocks: {num_blocks}, size: {BLOCK_SIZE}")
1560
1561
1562
1563
1564
1565
1566
1567
1568
        if max_total_tokens is None:
            if get_support_chunking():
                model_max_length = self.tokenizer.model_max_length
                max_input_tokens = (
                    min((num_blocks * BLOCK_SIZE - 1), model_max_length)
                    if max_input_tokens is None
                    else max_input_tokens
                )
                max_total_tokens = num_blocks * BLOCK_SIZE
1569

1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
            else:
                max_total_tokens = sum(batch.cache_lengths)
                max_input_tokens = (
                    max_total_tokens - 1
                    if max_input_tokens is None
                    else max_input_tokens
                )

        del _batch, batch
        self.kv_cache = []
        empty_cache()
1581

1582
        self.init_kv_cache(
1583
1584
1585
1586
            num_blocks,
            self.num_layers,
            self.num_kv_heads,
            self.head_size,
1587
            self.kv_cache_dtype,
1588
1589
1590
            self.device,
        )

fxmarty's avatar
fxmarty committed
1591
1592
1593
1594
1595
        if SYSTEM == "rocm":
            if (
                os.environ.get("PYTORCH_TUNABLEOP_ENABLED") is None
                or os.environ.get("PYTORCH_TUNABLEOP_ENABLED") == "1"
            ):
1596
1597
                torch.cuda.tunable.enable()

fxmarty's avatar
fxmarty committed
1598
1599
1600
1601
1602
1603
1604
1605
                if os.environ.get("PYTORCH_TUNABLEOP_TUNING") != "0":
                    torch.cuda.tunable.tuning_enable(True)

                if os.environ.get("PYTORCH_TUNABLEOP_SEQLENS") is not None:
                    tuning_sequences = [
                        int(val)
                        for val in os.environ["PYTORCH_TUNABLEOP_SEQLENS"].split(",")
                    ]
1606
                elif CUDA_GRAPHS is not None:
fxmarty's avatar
fxmarty committed
1607
                    tuning_sequences = CUDA_GRAPHS
1608
                else:
1609
                    tuning_sequences = [1, 2, 3, 4, 5, 6, 7]
fxmarty's avatar
fxmarty committed
1610
1611
1612

                tunableop_filepath = os.path.join(
                    HUGGINGFACE_HUB_CACHE,
drbh's avatar
drbh committed
1613
                    f"tunableop_{self.model_id.replace('/', '-')}_tp{self.world_size}_rank{self.rank}.csv",
fxmarty's avatar
fxmarty committed
1614
1615
                )

1616
1617
                log_master(
                    logger.info,
1618
1619
1620
1621
1622
                    f"PyTorch TunableOp is enabled. The warmup may take several minutes, picking the ROCm optimal matrix multiplication kernel for the target lengths {', '.join([str(seqlen) for seqlen in tuning_sequences])}, with typical 5-8% latency improvement for small sequence lengths. The picked GEMMs are saved in the file {tunableop_filepath}. To disable TunableOp, please launch TGI with `PYTORCH_TUNABLEOP_ENABLED=0`.",
                )

                torch.cuda.tunable.set_filename(
                    tunableop_filepath, insert_device_ordinal=False
fxmarty's avatar
fxmarty committed
1623
1624
1625
                )

                if os.path.isfile(tunableop_filepath):
1626
1627
1628
                    log_master(
                        logger.info,
                        f"The file {tunableop_filepath} already exists and will be reused.",
fxmarty's avatar
fxmarty committed
1629
1630
1631
1632
1633
1634
                    )
                    torch.cuda.tunable.read_file(tunableop_filepath)

                os.makedirs(HUGGINGFACE_HUB_CACHE, exist_ok=True)

                for seqlen in tuning_sequences:
1635
                    log_master(logger.info, f"Warming up TunableOp for seqlen={seqlen}")
fxmarty's avatar
fxmarty committed
1636
1637
                    self.tunableop_warmup(seqlen)
                    torch.cuda.tunable.write_file(tunableop_filepath)
1638
1639
                if os.environ.get("PYTORCH_TUNABLEOP_TUNING_AFTER_WARMUP") != "1":
                    torch.cuda.tunable.tuning_enable(False)
fxmarty's avatar
fxmarty committed
1640
            else:
1641
1642
1643
                log_master(
                    logger.info,
                    "PyTorch ROCm TunableOp (https://github.com/pytorch/pytorch/tree/main/aten/src/ATen/cuda/tunable) is disabled. TunableOp brings an additional 5-8% latency improvement for small sequence lengths but requires a warmup. If necessary, please use the environment variable PYTORCH_TUNABLEOP_ENABLED=1 to enable TunableOp.",
fxmarty's avatar
fxmarty committed
1644
1645
                )

1646
        if CUDA_GRAPHS:
1647
            try:
1648
1649
1650
                log_master(
                    logger.info, f"Cuda Graphs are enabled for sizes {CUDA_GRAPHS}"
                )
1651
                # Warmup cuda graphs
1652
                for bs in CUDA_GRAPHS:
1653
1654
                    if self.speculate is None or self.speculate + 1 <= bs:
                        self.cuda_graph_warmup(bs, max_s, max_bt)
OlivierDehaene's avatar
OlivierDehaene committed
1655
            except torch.cuda.OutOfMemoryError:
1656
                logger.exception("Decode cuda graph warmup failed")
1657
        else:
1658
1659
1660
            log_master(
                logger.info, f"Cuda Graphs are disabled (CUDA_GRAPHS={CUDA_GRAPHS})."
            )
1661

1662
1663
1664
        assert max_input_tokens is not None
        assert max_total_tokens is not None
        return int(num_blocks * BLOCK_SIZE), max_input_tokens, max_total_tokens
1665

fxmarty's avatar
fxmarty committed
1666
1667
1668
1669
1670
    def tunableop_warmup(self, seqlen: int):
        input_ids = torch.zeros(seqlen, dtype=torch.int64, device=self.device)
        position_ids = torch.zeros(seqlen, dtype=torch.int32, device=self.device)
        slots = torch.arange(seqlen, dtype=torch.int64, device=self.device)

fxmarty's avatar
fxmarty committed
1671
1672
        # Dummy value, some models (starcoder2) don't accept `None`.
        input_lengths = torch.ones(seqlen, dtype=torch.int32, device=self.device)
1673
1674
1675
        cache_lengths_tensor = torch.zeros(
            seqlen, dtype=torch.int32, device=self.device
        )
1676
1677
1678
        cu_seqlen_prefill = torch.tensor(
            [0, seqlen], device=self.device, dtype=torch.int32
        )
1679
        max_s = seqlen
1680
1681
        seqlen = Seqlen(
            input_lengths=input_lengths,
1682
            cache_lengths=cache_lengths_tensor,
1683
1684
1685
1686
            cu_seqlen_q=cu_seqlen_prefill,
            max_q=1,
            max_k=seqlen,
        )
fxmarty's avatar
fxmarty committed
1687

fxmarty's avatar
fxmarty committed
1688
1689
1690
1691
        # We pass a `cu_seqlen_prefill` in order not to have to deal with paged attention cache allocation/deallocation.
        self.model.forward(
            input_ids=input_ids,
            position_ids=position_ids,
1692
            cu_seqlen_prefill=cu_seqlen_prefill,
1693
            kv_cache=self.kv_cache,
fxmarty's avatar
fxmarty committed
1694
            block_tables=None,
1695
            seqlen=seqlen,
fxmarty's avatar
fxmarty committed
1696
            slots=slots,
1697
            max_s=max_s,
fxmarty's avatar
fxmarty committed
1698
            lm_head_indices=None,
1699
            prefill_cache_indices=None,
fxmarty's avatar
fxmarty committed
1700
1701
        )

1702
    def forward(
drbh's avatar
drbh committed
1703
        self, batch: FlashCausalLMBatch, adapter_data: AdapterBatchData
1704
    ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
1705
        # Model Forward
Nicolas Patry's avatar
Nicolas Patry committed
1706
        if batch.speculative_ids is not None:
OlivierDehaene's avatar
OlivierDehaene committed
1707
1708
1709
            input_ids = batch.input_ids
            position_ids = batch.position_ids
            cu_seqlen_prefill = batch.cu_seqlen_prefill
1710
            kv_cache = self.kv_cache
OlivierDehaene's avatar
OlivierDehaene committed
1711
1712
1713
            block_tables = batch.block_tables_tensor
            slots = batch.slots[batch.slot_indices]
            input_lengths = batch.input_lengths_tensor
1714
            max_s = batch.max_current_length
OlivierDehaene's avatar
OlivierDehaene committed
1715
            lm_head_indices = batch.prefill_head_indices
Nicolas Patry's avatar
Nicolas Patry committed
1716
1717
1718

            speculative_ids = batch.speculative_ids

OlivierDehaene's avatar
OlivierDehaene committed
1719
            B, speculative_length = speculative_ids.shape
Nicolas Patry's avatar
Nicolas Patry committed
1720
            new_length = speculative_length + 1
OlivierDehaene's avatar
OlivierDehaene committed
1721
1722
1723
            new_input_ids = torch.cat(
                [input_ids.unsqueeze(-1), speculative_ids], dim=1
            ).reshape(-1)
Nicolas Patry's avatar
Nicolas Patry committed
1724
1725
            arange = torch.arange(new_length, device=position_ids.device).unsqueeze(0)
            arange_int = arange.to(dtype=torch.int32)
OlivierDehaene's avatar
OlivierDehaene committed
1726
1727
1728
            new_position_ids = (
                position_ids.unsqueeze(-1).expand(B, new_length) + arange
            ).view(-1)
Nicolas Patry's avatar
Nicolas Patry committed
1729
            slots = (slots.unsqueeze(-1).expand(B, new_length) + arange_int).view(-1)
OlivierDehaene's avatar
OlivierDehaene committed
1730
1731
1732
            input_lengths = (
                input_lengths.unsqueeze(-1).expand(B, new_length) + arange_int
            ).view(-1)
1733
1734
            cache_lengths_tensor = (
                batch.cache_lengths_tensor.unsqueeze(-1).expand(B, new_length)
Nicolas Patry's avatar
Nicolas Patry committed
1735
            ).reshape(-1)
Nicolas Patry's avatar
Nicolas Patry committed
1736
1737

            # Add Copy the block tables for all members
OlivierDehaene's avatar
OlivierDehaene committed
1738
1739
1740
1741
1742
1743
            block_tables = (
                block_tables.unsqueeze(1)
                .expand(B, new_length, -1)
                .reshape(B * new_length, -1)
                .contiguous()
            )
Nicolas Patry's avatar
Nicolas Patry committed
1744
1745
1746
1747
1748
            max_s = max_s + speculative_length

            input_ids = new_input_ids
            position_ids = new_position_ids
        else:
OlivierDehaene's avatar
OlivierDehaene committed
1749
1750
1751
            input_ids = batch.input_ids
            position_ids = batch.position_ids
            cu_seqlen_prefill = batch.cu_seqlen_prefill
1752
            kv_cache = self.kv_cache
OlivierDehaene's avatar
OlivierDehaene committed
1753
1754
1755
            block_tables = batch.block_tables_tensor
            slots = batch.slots[batch.slot_indices]
            input_lengths = batch.input_lengths_tensor
1756
1757
            cache_lengths_tensor = batch.cache_lengths_tensor
            max_s = batch.max_current_length
OlivierDehaene's avatar
OlivierDehaene committed
1758
            lm_head_indices = batch.prefill_head_indices
Nicolas Patry's avatar
Nicolas Patry committed
1759

1760
1761
1762
1763
1764
1765
        if cu_seqlen_prefill is None and self.max_past() is not None:
            # In decode, not prefill, we're actually overwriting the KV-cache
            # in a circular buffer mode.
            # This makes sure the max_s for the decode pass is correct.
            max_s = min(self.max_past(), max_s)

1766
        bs = input_ids.shape[0]
OlivierDehaene's avatar
OlivierDehaene committed
1767
1768
1769
1770
1771
1772
1773
1774
        sorted_padded_bs = sorted([k for k in self.cuda_graphs.keys() if k >= bs])
        if sorted_padded_bs:
            # Get associated cuda graph
            cuda_graph = self.cuda_graphs[sorted_padded_bs[0]]
        else:
            cuda_graph = None

        if cu_seqlen_prefill is not None or cuda_graph is None:
1775
            if ATTENTION == "flashinfer":
Nicolas Patry's avatar
Nicolas Patry committed
1776
1777
1778
                block_tables = block_tables_to_ragged(
                    block_tables=block_tables,
                    input_lengths=batch.input_lengths,
1779
                    cache_lengths=batch.cache_lengths,
1780
1781
1782
                    input_lengths_tensor=batch.input_lengths_tensor,
                    cache_lengths_tensor=batch.cache_lengths_tensor,
                    max_current_length=batch.max_current_length,
Nicolas Patry's avatar
Nicolas Patry committed
1783
                )
1784
            with self._forward_context(
1785
                block_tables=block_tables,
1786
                cu_seqlen_prefill=cu_seqlen_prefill,
1787
                input_lengths_tensor=input_lengths,
1788
                cache_lengths_tensor=cache_lengths_tensor,
1789
            ):
1790
1791
                seqlen = Seqlen(
                    input_lengths=input_lengths,
1792
                    cache_lengths=cache_lengths_tensor,
1793
                    cu_seqlen_q=cu_seqlen_prefill,
1794
1795
                    max_q=batch.max_input_length,
                    max_k=batch.max_current_length,
1796
                )
1797
1798
1799
1800
1801
1802
1803
                logits, speculative_logits = self.model.forward(
                    input_ids=input_ids,
                    position_ids=position_ids,
                    cu_seqlen_prefill=cu_seqlen_prefill,
                    kv_cache=kv_cache,
                    block_tables=block_tables,
                    slots=slots,
1804
                    seqlen=seqlen,
1805
1806
1807
1808
1809
1810
1811
1812
                    max_s=max_s,
                    prefill_cache_indices=batch.prefill_cache_indices,
                    lm_head_indices=lm_head_indices,
                    adapter_data=adapter_data,
                )
                if batch.prefill_cache_indices is not None:
                    batch.prefill_cache_indices = None
                return logits, speculative_logits
1813
1814
1815
1816

        # Copy inputs to the static inputs of the cuda graph
        # Static inputs are potentially padded
        cuda_graph["input_ids"][: input_ids.shape[0]] = input_ids
drbh's avatar
drbh committed
1817
        cuda_graph["position_ids"][: position_ids.shape[-1]] = position_ids
Nicolas Patry's avatar
Nicolas Patry committed
1818
1819
1820
1821
        if ATTENTION == "flashinfer":
            block_tables = block_tables_to_ragged(
                block_tables=block_tables,
                input_lengths=batch.input_lengths,
1822
                cache_lengths=batch.cache_lengths,
1823
1824
1825
                input_lengths_tensor=batch.input_lengths_tensor,
                cache_lengths_tensor=batch.cache_lengths_tensor,
                max_current_length=batch.max_current_length,
Nicolas Patry's avatar
Nicolas Patry committed
1826
            )
1827
            # assert block_tables.shape[0] >= slots.shape[0]
Nicolas Patry's avatar
Nicolas Patry committed
1828
1829
1830
1831
1832
            cuda_graph["block_tables"][: block_tables.shape[0]] = block_tables
        else:
            cuda_graph["block_tables"][
                : block_tables.shape[0], : block_tables.shape[1]
            ] = block_tables
1833
1834
1835
1836

        # XXX: This is working only because block 0 is reserved for the healthcheck
        # so it doesn't matter if we override it with bogus values.
        cuda_graph["slots"].fill_(0)
1837
1838
        cuda_graph["slots"][: slots.shape[0]] = slots
        cuda_graph["input_lengths"].zero_()
1839
        cuda_graph["input_lengths"][: input_lengths.shape[0]] = input_lengths
1840
1841
1842
1843
        cuda_graph["cache_lengths"].zero_()
        cuda_graph["cache_lengths"][
            : cache_lengths_tensor.shape[0]
        ] = cache_lengths_tensor
1844

1845
        with self._forward_context(
Nicolas Patry's avatar
Nicolas Patry committed
1846
            block_tables=cuda_graph["block_tables"],
1847
            cu_seqlen_prefill=None,
Nicolas Patry's avatar
Nicolas Patry committed
1848
            input_lengths_tensor=cuda_graph["input_lengths"],
1849
            cache_lengths_tensor=cuda_graph["cache_lengths"],
1850
            state=cuda_graph["state"],
1851
1852
1853
1854
        ):
            # Replay the graph
            cuda_graph["graph"].replay()

1855
        # Slice output to the correct shape
1856
1857
1858
1859
1860
1861
1862
        speculative_logits = (
            cuda_graph["speculative_logits"][:bs]
            if cuda_graph["speculative_logits"] is not None
            else None
        )
        logits = cuda_graph["logits"][:bs]
        return logits, speculative_logits
1863
1864
1865
1866

    @tracer.start_as_current_span("generate_token")
    def generate_token(
        self, batch: FlashCausalLMBatch
1867
1868
    ) -> Tuple[List[Generation], Optional[FlashCausalLMBatch], Tuple[int, int]]:
        start = time.time_ns()
1869
1870
1871
1872
        prefill = batch.prefilling
        if prefill:
            batch.prepare_for_prefill()

1873
        prefill_logprobs = batch.prefill_next_token_indices is not None
1874

drbh's avatar
drbh committed
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
        # Update adapter indices for speculative tokens (if present)
        adapter_meta = batch.adapter_meta
        if batch.speculative_ids is not None:
            B, speculative_length = batch.speculative_ids.shape
            new_length = speculative_length + 1
            adapter_indices = (
                adapter_meta.adapter_indices.unsqueeze(-1)
                .expand(B, new_length)
                .reshape(-1)
            )
            adapter_segments = adapter_meta.adapter_segments * new_length
            adapter_meta = AdapterBatchMetadata(
                adapter_indices=adapter_indices,
                adapter_set=adapter_meta.adapter_set,
                adapter_segments=adapter_segments,
                segment_indices=adapter_meta.segment_indices,
            )

        # Assign pointers to adapter weights
        # TODO(travis): don't update this if indices haven't changed
        adapter_data = AdapterBatchData.from_meta(
            adapter_meta,
            self.layer_to_adapter_weights,
            prefill,
            batch.prefill_head_indices,
        )

        out, speculative_logits = self.forward(batch, adapter_data)
1903

1904
1905
        if prefill:
            next_token_logits = (
1906
                out[batch.prefill_next_token_indices] if prefill_logprobs else out
1907
            )
Nicolas Patry's avatar
Nicolas Patry committed
1908
1909
            if speculative_logits is not None:
                speculative_logits = (
OlivierDehaene's avatar
OlivierDehaene committed
1910
1911
1912
                    speculative_logits[batch.prefill_next_token_indices]
                    if prefill_logprobs
                    else speculative_logits
Nicolas Patry's avatar
Nicolas Patry committed
1913
                )
1914
1915
1916
1917
            if len(batch) > 1 and prefill_logprobs:
                # We create the prefill_tokens_indices tensor that will be used to gather prefill logprobs
                # When batch == 1, we will just use the batch.input_ids values directly
                prefill_tokens_indices = batch.input_ids.new_zeros(len(out))
1918
        else:
1919
            prefill_logprobs = None
1920
1921
            next_token_logits = out

1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
        finished_prefilling = True
        next_chunk_lengths = []
        current_prefilling_mask = batch.prefilling_mask
        if prefill:
            if get_support_chunking():
                next_prefilling_mask = []
                # Budget in tokens for the next batch
                # We remove (len(batch) - 1) to always have enough space for at least a single decode
                # for the remaining requests -1 because the first request does not need to be removed from the budget
                # (ex: you have one request in the batch, you want it to take the full budget not budget -1)
                batch_budget = get_max_prefill_tokens() - (len(batch) - 1)
                # We reverse to prioritize older requests
                # zip() is not reversible so reverse the underlying lists instead
                for cache_length, input_length, prompt_length in zip(
                    reversed(batch.cache_lengths),
                    reversed(batch.input_lengths),
                    reversed(batch.prompt_lengths),
                ):
                    remaining_prefill_tokens = max(
                        prompt_length - cache_length - input_length, 0
                    )
                    if remaining_prefill_tokens > 0:
                        next_chunk_length = max(
                            min(remaining_prefill_tokens, batch_budget), 1
                        )
                        batch_budget -= next_chunk_length
                        finished_prefilling = False
                        next_prefilling_mask.append(True)
                    else:
                        # FIXME: use true number of accepted tokens instead of 1
                        # Since speculation will be turned off, this is always true
                        next_chunk_length = 1
                        next_prefilling_mask.append(False)
                    next_chunk_lengths.append(next_chunk_length)

                # Reverse back the obtained values²
                next_chunk_lengths.reverse()
                next_prefilling_mask.reverse()
            else:
                # The model does not support chunking
                # We know we only do a single prefill
                finished_prefilling = True
                next_prefilling_mask = [False] * len(batch)

            batch.prefilling = not finished_prefilling
            batch.prefilling_mask = next_prefilling_mask

Nicolas Patry's avatar
Nicolas Patry committed
1969
        speculate = get_speculate()
OlivierDehaene's avatar
OlivierDehaene committed
1970
1971
1972
1973
1974
1975
1976
        (
            next_input_ids,
            next_token_logprobs,
            logprobs,
            accepted_ids,
            speculative_ids,
        ) = batch.next_token_chooser(
1977
            batch.all_input_ids_tensor[:, : batch.max_current_length],
OlivierDehaene's avatar
OlivierDehaene committed
1978
            next_token_logits,
Nicolas Patry's avatar
Nicolas Patry committed
1979
            speculate,
OlivierDehaene's avatar
OlivierDehaene committed
1980
1981
            batch.speculative_ids,
            speculative_logits,
1982
1983
        )

Nicolas Patry's avatar
Nicolas Patry committed
1984
        batch_top_token_ids, batch_top_token_logprobs = batch_top_tokens(
Nicolas Patry's avatar
Nicolas Patry committed
1985
            batch.top_n_tokens, batch.top_n_tokens_tensor, logprobs, accepted_ids
Nicolas Patry's avatar
Nicolas Patry committed
1986
1987
        )

1988
1989
1990
        # Since we are done prefilling, all the tensors that were concatenating values for all the requests
        # instantly become of shape [BATCH_SIZE]
        if prefill and finished_prefilling:
1991
            indices = batch.cu_seqlen_prefill[1:] - 1
drbh's avatar
drbh committed
1992
            batch.position_ids = batch.position_ids[(..., indices)]
1993
1994
1995
1996
            batch.slot_indices = batch.slot_indices[indices]
            batch.adapter_meta.adapter_indices = batch.adapter_meta.adapter_indices[
                indices
            ]
1997

1998
        # Zipped iterator
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
        iterator = zip(
            batch.requests,
            batch.prompt_lengths,
            batch.cache_lengths,
            batch.input_lengths,
            batch.all_input_ids,
            accepted_ids,
            current_prefilling_mask,
            batch.prefilling_mask,
        )
2009

2010
2011
2012
2013
        # We do two for loops as the first one can run completely asynchronously from the GPU while for the second
        # one, we need to first do a GPU <-> CPU sync
        # It is faster if we delay this sync for the maximum amount of time

2014
        # For each member of the batch
2015
        # Cumulative length
2016
2017
2018
        cu_accepted_ids = torch.nn.functional.pad(
            torch.cumsum(accepted_ids, dim=0), (1, 0)
        )
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
        cumulative_length = 0
        for i, (
            request,
            prompt_length,
            cache_length,
            input_length,
            all_input_ids,
            n_accepted_ids,
            request_was_prefilling,
            request_is_prefilling,
        ) in enumerate(iterator):
            # Used to gather prefill logprobs
            # Copy batch.all_input_ids_tensor to prefill_token_indices
            if request.prefill_logprobs and request_was_prefilling:
                # Indexing metadata
                out_start_index = batch.prefill_cu_outlens[i]
                out_end_index = batch.prefill_cu_outlens[i + 1]
2036

2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
                # Logprobs generated by the model are for the next token
                # So we need to translate the id tensor by 1
                ids = batch.all_input_ids_tensor[
                    i, cache_length + 1 : cache_length + input_length + 1
                ]
                if len(batch) > 1:
                    prefill_tokens_indices[out_start_index:out_end_index] = ids
                else:
                    # Set prefill_tokens_indices to the correct slice
                    prefill_tokens_indices = ids

2048
2049
            # If the device does not support triton, we copy one by one
            if not request_is_prefilling and not has_triton():
2050
                # Only save tokens if we are done prefilling for this request
2051
2052
2053
2054
2055
2056
2057
                batch.all_input_ids_tensor[
                    i,
                    batch.cache_lengths_tensor[i]
                    + batch.input_lengths[i] : batch.cache_lengths_tensor[i]
                    + batch.input_lengths[i]
                    + accepted_ids[i],
                ] = next_input_ids[cu_accepted_ids[i] : cu_accepted_ids[i + 1]]
2058
2059
            cumulative_length += input_length

2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
        # If the device support triton, we can use a fused kernel
        if has_triton():
            copy_next_input_ids_inplace(
                speculate + 1,
                batch.all_input_ids_tensor,
                batch.cache_lengths_tensor,
                batch.input_lengths_tensor,
                batch.prompt_lengths_tensor,
                next_input_ids,
                cu_accepted_ids,
            )

drbh's avatar
drbh committed
2072
        # Update values
2073
2074
        # These values can be updated without a GPU -> CPU sync
        if not prefill or (prefill and finished_prefilling):
2075
            batch.input_ids = next_input_ids[cu_accepted_ids[1:] - 1]
2076
            batch.speculative_ids = speculative_ids
2077
2078
2079
            batch.position_ids += accepted_ids
            batch.cache_lengths_tensor += batch.input_lengths_tensor + accepted_ids - 1
            batch.input_lengths_tensor = torch.ones_like(batch.input_lengths_tensor)
2080
            batch.slot_indices += accepted_ids
2081

2082
        if prefill and prefill_logprobs:
2083
2084
2085
            # Get prefill logprobs with inplace softmax (avoid copying the `out` tensor (max_batch_prefill_tokens * vocab_size))
            torch.log_softmax(out, -1, out=out)
            prefill_logprobs_tensor = out
2086
2087
2088
2089
2090
2091
            prefill_logprobs = torch.gather(
                prefill_logprobs_tensor, 1, prefill_tokens_indices.view(-1, 1)
            )
            # GPU <-> CPU sync
            prefill_logprobs = prefill_logprobs.view(-1).tolist()

2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
        # Does a GPU <-> CPU sync internally
        if prefill and finished_prefilling:
            # adjust segment lengths to account for all request lengths being 1 during decoding
            adapter_segments, _ = find_segments(batch.adapter_meta.adapter_indices)
            batch.adapter_meta.adapter_segments = torch.tensor(
                adapter_segments,
                dtype=torch.int32,
                device=batch.adapter_meta.adapter_segments.device,
            )

2102
2103
        # GPU <-> CPU sync
        next_token_logprobs = next_token_logprobs.tolist()
Nicolas Patry's avatar
Nicolas Patry committed
2104
        next_token_ids = next_input_ids.tolist()
2105
        accepted_ids = accepted_ids.tolist()
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146

        # Update values if we need to continue prefilling
        # This represents the `else` case of the `Update values` if above
        # but since this require the `next_token_ids` to be on CPU, it is better to do it here
        if prefill and not finished_prefilling:
            # Speculation must be ignored while we prefill even with chunking
            # it simplifies everything
            assert batch.speculative_ids is None

            all_postfix_ids = []
            for i, (
                request_prefilling,
                next_token_id,
                all_input_ids,
                cache_length,
                input_length,
                next_chunk_length,
            ) in enumerate(
                zip(
                    batch.prefilling_mask,
                    next_token_ids,
                    batch.all_input_ids,
                    batch.cache_lengths,
                    batch.input_lengths,
                    next_chunk_lengths,
                )
            ):
                if request_prefilling:
                    next_cache_length = cache_length + input_length
                    # Get new prompt IDs to prefill
                    postfix_ids = all_input_ids[
                        next_cache_length : next_cache_length + next_chunk_length
                    ]
                else:
                    # This request is done prefilling, the new id is the one selected the sampling method
                    postfix_ids = [next_token_id]

                all_postfix_ids.append(postfix_ids)

            batch.input_ids = all_postfix_ids

2147
        start_decode = time.time_ns()
2148

2149
2150
2151
2152
        # Results
        generations: List[Generation] = []
        stopped = True

2153
2154
2155
        # Zipped iterator
        iterator = zip(
            batch.requests,
2156
2157
            batch.prompt_lengths,
            batch.cache_lengths,
2158
            batch.input_lengths,
2159
2160
            batch.prefix_offsets,
            batch.read_offsets,
2161
2162
            batch.stopping_criterias,
            batch.all_input_ids,
2163
2164
            batch.next_token_chooser.do_sample,
            batch.next_token_chooser.seeds,
Nicolas Patry's avatar
Nicolas Patry committed
2165
            batch.top_n_tokens,
2166
2167
            current_prefilling_mask,
            batch.prefilling_mask,
Nicolas Patry's avatar
Nicolas Patry committed
2168
            accepted_ids,
Nicolas Patry's avatar
Nicolas Patry committed
2169
2170
            batch_top_token_ids,
            batch_top_token_logprobs,
2171
2172
        )

2173
2174
        # Reset max_input_length
        batch.max_input_length = 0
2175
        # For each member of the batch
Nicolas Patry's avatar
Nicolas Patry committed
2176
        index = 0
2177
2178
        for i, (
            request,
2179
2180
            prompt_length,
            cache_length,
2181
            input_length,
2182
2183
            prefix_offset,
            read_offset,
2184
2185
            stopping_criteria,
            all_input_ids,
2186
2187
            do_sample,
            seed,
Nicolas Patry's avatar
Nicolas Patry committed
2188
            top_n_tokens,
2189
2190
            request_was_prefilling,
            request_is_prefilling,
Nicolas Patry's avatar
Nicolas Patry committed
2191
            n_accepted_ids,
Nicolas Patry's avatar
Nicolas Patry committed
2192
2193
            top_token_ids,
            top_token_logprobs,
2194
        ) in enumerate(iterator):
2195
2196
2197
2198
2199
            # Compute logprobs first as, even though we might skip the token,
            # it can still be required to compute the logprobs
            # modulo on request.id as it is robust to batch.filter whereas the index in the batch is not and we need
            # this state to be stable
            if request.id % self.world_size == self.rank:
2200
                # Prefill
2201
                if request_was_prefilling and request.prefill_logprobs:
2202
2203
                    out_start_index = batch.prefill_cu_outlens[i]
                    out_end_index = batch.prefill_cu_outlens[i + 1]
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
                    if not request_is_prefilling:
                        # The request is dones prefilling, meaning that we started generating new tokens
                        # The last logprob is a logprob for a generated token that was not part of the prompt
                        # We need to remove it
                        out_end_index -= 1

                    request_prefill_logprobs = prefill_logprobs[
                        out_start_index:out_end_index
                    ]
                    # Logprobs generated by the model are for the next token
                    # So we need to translate the id tensor by 1
                    prefill_token_ids = all_input_ids[
                        cache_length + 1 : cache_length + input_length + 1
                    ]

                    past_prefill_logprob_tokens = batch.prefill_logprob_tokens[i]

                    if past_prefill_logprob_tokens is None:
                        # add nan for cached prompt tokens/first token
                        request_prefill_logprobs = [float("nan")] * (
                            cache_length + 1
                        ) + request_prefill_logprobs
                        prefill_token_ids = (
                            all_input_ids[: cache_length + 1] + prefill_token_ids
                        )
2229

2230
                    prefill_texts = self.tokenizer.batch_decode(
2231
                        prefill_token_ids,
2232
2233
2234
                        clean_up_tokenization_spaces=False,
                        skip_special_tokens=False,
                    )
Nicolas Patry's avatar
Nicolas Patry committed
2235

2236
2237
                    prefill_logprob_tokens = Tokens(
                        prefill_token_ids,
OlivierDehaene's avatar
OlivierDehaene committed
2238
2239
2240
                        request_prefill_logprobs,
                        prefill_texts,
                        is_special=[],
2241
                    )
2242
2243
2244
2245
2246
2247
                    if past_prefill_logprob_tokens is not None:
                        prefill_logprob_tokens = (
                            past_prefill_logprob_tokens + prefill_logprob_tokens
                        )

                    batch.prefill_logprob_tokens[i] = prefill_logprob_tokens
2248
                else:
2249
2250
2251
2252
2253
2254
2255
2256
                    batch.prefill_logprob_tokens[i] = None

            # If it is, the tokens we decoded should be ignored
            if request_is_prefilling:
                # Make sure that we do not stop as even though this request did not create a token, it is still
                # processing
                stopped = False
                new_input_length = next_chunk_lengths[i]
2257
                new_cache_length = cache_length + input_length
2258
            else:
2259
2260
                new_input_length = 1
                new_cache_length = cache_length + input_length + n_accepted_ids - 1
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
                # Append next token to all tokens
                next_token_texts = []
                left = 0

                if n_accepted_ids > 1:
                    log_master(logger.debug, f"speculated ids {n_accepted_ids - 1}")

                current_stopped = False
                for j in range(index, index + n_accepted_ids):
                    # Generated token
                    next_token_id = next_token_ids[j]
                    all_input_ids.append(next_token_id)
                    next_token_text, prefix_offset, read_offset = self.decode_token(
                        all_input_ids,
                        prefix_offset,
                        read_offset,
                    )
                    next_token_texts.append(next_token_text)

                    stop, reason = stopping_criteria(
                        next_token_id,
                        next_token_text,
                    )

                    if stop:
                        left = index + n_accepted_ids - j - 1
                        current_stopped = True
                        break
                    else:
                        current_stopped = False
                stopped = stopped and current_stopped

                _next_token_ids = next_token_ids[index : index + n_accepted_ids - left]
                _next_token_logprobs = next_token_logprobs[
                    index : index + n_accepted_ids - left
                ]

                # Shard generations
                # All generations will be appended in the rust sharded client
                if request.id % self.world_size == self.rank:
                    if stop:
                        # Decode generated tokens
                        output_text, _, _ = self.decode_token(
                            all_input_ids,
                            prefix_offset=len(all_input_ids)
                            - stopping_criteria.current_tokens
                            - 1,
                            read_offset=len(all_input_ids)
                            - stopping_criteria.current_tokens,
                            skip_special_tokens=True,
Nicolas Patry's avatar
Nicolas Patry committed
2311
                        )
2312
2313
2314
2315
2316
                        generated_text = GeneratedText(
                            output_text,
                            stopping_criteria.current_tokens,
                            reason,
                            seed if do_sample else None,
Nicolas Patry's avatar
Nicolas Patry committed
2317
                        )
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
                    else:
                        generated_text = None

                    if top_n_tokens > 0:
                        all_top_tokens = []
                        for top_token_ids, top_token_logprobs in zip(
                            top_token_ids, top_token_logprobs
                        ):
                            toptoken_texts = self.tokenizer.batch_decode(
                                top_token_ids,
                                clean_up_tokenization_spaces=False,
                                skip_special_tokens=False,
                            )
                            special_toptokens = [
                                token_id in self.all_special_ids
                                for token_id in top_token_ids
                            ]
                            top_tokens = Tokens(
                                top_token_ids,
                                top_token_logprobs,
                                toptoken_texts,
                                special_toptokens,
                            )
                            all_top_tokens.append(top_tokens)
                        top_tokens = all_top_tokens
                    else:
                        top_tokens = None

                    generation = Generation(
                        request.id,
                        batch.prefill_logprob_tokens[i],
                        Tokens(
                            _next_token_ids,
                            _next_token_logprobs,
                            next_token_texts,
                            [nid in self.all_special_ids for nid in _next_token_ids],
                        ),
                        generated_text,
                        top_tokens,
                    )
2358

2359
                    generations.append(generation)
2360

2361
2362
2363
2364
2365
2366
2367
2368
                # accept each new token for this specific request since we may
                # have more than one new token per request with speculative decoding
                for next_token_id in _next_token_ids:
                    batch.next_token_chooser = (
                        batch.next_token_chooser.advance_grammar_single(
                            i, next_token_id
                        )
                    )
drbh's avatar
drbh committed
2369

2370
            # Update values
2371
            index += n_accepted_ids
2372
2373
2374
2375
            batch.cache_lengths[i] = new_cache_length
            batch.max_input_length = max(batch.max_input_length, new_input_length)
            batch.input_lengths[i] = new_input_length
            current_length = new_cache_length + new_input_length
2376
2377
            batch.max_current_length = max(batch.max_current_length, current_length)

2378
2379
            batch.prefix_offsets[i] = prefix_offset
            batch.read_offsets[i] = read_offset
2380
2381
            batch.all_input_ids[i] = all_input_ids

2382
2383
        if stopped:
            # No need to return a batch if we know that all requests stopped
2384
2385
2386
            forward_ns = start_decode - start
            decode_ns = time.time_ns() - start_decode
            return generations, None, (forward_ns, decode_ns)
2387

2388
2389
2390
2391
2392
2393
2394
        if prefill and finished_prefilling:
            # We do not need prefill tensors anymore
            batch.cu_seqlen_prefill = None
            batch.prefill_cache_indices = None
            batch.prefill_cu_outlens = None
            batch.prefill_head_indices = None
            batch.prefill_next_token_indices = None
2395

2396
2397
2398
        forward_ns = start_decode - start
        decode_ns = time.time_ns() - start_decode
        return generations, batch, (forward_ns, decode_ns)
2399
2400
2401
2402
2403
2404

    def _forward_context(
        self,
        *,
        block_tables: torch.Tensor,
        cu_seqlen_prefill: Optional[torch.Tensor],
Nicolas Patry's avatar
Nicolas Patry committed
2405
        input_lengths_tensor: torch.Tensor,
2406
        cache_lengths_tensor: torch.Tensor,
2407
2408
        state: Optional[Any] = None,
    ) -> ContextManager:
2409
        if ATTENTION != "flashinfer":
2410
2411
            return nullcontext()

Nicolas Patry's avatar
Nicolas Patry committed
2412
        from text_generation_server.layers.attention.flashinfer import (
2413
            use_decode_state,
Nicolas Patry's avatar
Nicolas Patry committed
2414
            use_prefill_with_paged_kv_state,
2415
2416
2417
        )

        if cu_seqlen_prefill is not None:
Nicolas Patry's avatar
Nicolas Patry committed
2418
2419
2420
2421
2422
            return use_prefill_with_paged_kv_state(
                state=(
                    state if state is not None else self.prefill_with_paged_kv_state
                ),
                block_tables=block_tables,
2423
                cu_seqlens=cu_seqlen_prefill,
2424
                input_lengths=input_lengths_tensor + cache_lengths_tensor,
2425
2426
2427
                num_heads=self.num_heads,
                num_kv_heads=self.num_kv_heads,
                head_size=self.head_size,
Nicolas Patry's avatar
Nicolas Patry committed
2428
                page_size=BLOCK_SIZE,
2429
2430
                dtype=self.dtype,
                window_left=self.sliding_window,
2431
2432
            )
        else:
Nicolas Patry's avatar
Nicolas Patry committed
2433
            assert input_lengths_tensor is not None
2434
2435
            return use_decode_state(
                state=state if state is not None else self.decode_state,
2436
                input_lengths=input_lengths_tensor + cache_lengths_tensor,
Nicolas Patry's avatar
Nicolas Patry committed
2437
                block_tables=block_tables,
2438
2439
2440
2441
                num_heads=self.num_heads,
                num_kv_heads=self.num_kv_heads,
                head_size=self.head_size,
                page_size=BLOCK_SIZE,
2442
                kv_cache_dtype=self.kv_cache_dtype,
2443
2444
                dtype=self.dtype,
                window_left=self.sliding_window,
2445
            )