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