2022-10-28 11:24:00 -06:00
|
|
|
import torch
|
|
|
|
import torch.distributed
|
|
|
|
|
2023-06-08 06:51:52 -06:00
|
|
|
from typing import Optional, Type
|
2022-10-28 11:24:00 -06:00
|
|
|
|
2023-01-20 04:24:39 -07:00
|
|
|
from transformers import (
|
|
|
|
PreTrainedTokenizerBase,
|
|
|
|
)
|
2022-10-28 11:24:00 -06:00
|
|
|
|
2023-03-07 10:52:22 -07:00
|
|
|
from text_generation_server.models import CausalLM
|
|
|
|
from text_generation_server.models.causal_lm import CausalLMBatch
|
|
|
|
from text_generation_server.pb import generate_pb2
|
2022-10-28 11:24:00 -06:00
|
|
|
|
|
|
|
|
2022-12-08 10:49:33 -07:00
|
|
|
class BloomCausalLMBatch(CausalLMBatch):
|
|
|
|
@classmethod
|
|
|
|
def from_pb(
|
2023-01-20 04:24:39 -07:00
|
|
|
cls,
|
|
|
|
pb: generate_pb2.Batch,
|
|
|
|
tokenizer: PreTrainedTokenizerBase,
|
2023-05-26 04:30:27 -06:00
|
|
|
dtype: torch.dtype,
|
2023-01-20 04:24:39 -07:00
|
|
|
device: torch.device,
|
2022-12-08 10:49:33 -07:00
|
|
|
) -> "CausalLMBatch":
|
2023-06-08 06:51:52 -06:00
|
|
|
batch = super().from_pb(pb=pb, tokenizer=tokenizer, dtype=dtype, device=device)
|
2022-12-08 10:49:33 -07:00
|
|
|
batch.keys_head_dim_last = False
|
|
|
|
return batch
|
|
|
|
|
|
|
|
|
2023-06-08 06:51:52 -06:00
|
|
|
class BLOOMSharded(CausalLM):
|
|
|
|
@property
|
|
|
|
def batch_type(self) -> Type[CausalLMBatch]:
|
|
|
|
return BloomCausalLMBatch
|
2022-10-28 11:24:00 -06:00
|
|
|
|
2023-01-30 07:36:16 -07:00
|
|
|
def forward(
|
|
|
|
self, input_ids, attention_mask, position_ids, past_key_values: Optional = None
|
|
|
|
):
|
2024-02-26 11:49:28 -07:00
|
|
|
outputs, speculative_logits = self.model.forward(
|
2022-10-28 11:24:00 -06:00
|
|
|
input_ids=input_ids,
|
|
|
|
attention_mask=attention_mask,
|
2023-01-20 07:35:22 -07:00
|
|
|
position_ids=position_ids,
|
2022-10-28 11:24:00 -06:00
|
|
|
past_key_values=past_key_values,
|
|
|
|
use_cache=True,
|
|
|
|
)
|
|
|
|
|
2023-06-08 06:51:52 -06:00
|
|
|
logits = outputs.logits
|
2024-02-26 11:49:28 -07:00
|
|
|
return logits, speculative_logits, outputs.past_key_values
|