2023-06-01 03:41:35 -06:00
|
|
|
|
import inspect
|
2022-11-04 07:22:47 -06:00
|
|
|
|
import torch
|
|
|
|
|
|
2022-11-03 09:07:54 -06:00
|
|
|
|
from abc import ABC, abstractmethod
|
2022-11-04 11:03:04 -06:00
|
|
|
|
from typing import List, Tuple, Optional, TypeVar, Type
|
2023-01-17 01:10:22 -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.types import Batch, GeneratedText
|
2023-04-21 07:36:29 -06:00
|
|
|
|
from text_generation_server.pb.generate_pb2 import InfoResponse
|
2022-10-28 11:24:00 -06:00
|
|
|
|
|
2022-11-04 11:03:04 -06:00
|
|
|
|
B = TypeVar("B", bound=Batch)
|
|
|
|
|
|
2022-10-28 11:24:00 -06:00
|
|
|
|
|
2022-11-03 09:07:54 -06:00
|
|
|
|
class Model(ABC):
|
2023-04-12 04:03:10 -06:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
2023-05-16 15:23:27 -06:00
|
|
|
|
model: torch.nn.Module,
|
2023-04-12 04:03:10 -06:00
|
|
|
|
tokenizer: PreTrainedTokenizerBase,
|
2023-04-21 07:36:29 -06:00
|
|
|
|
requires_padding: bool,
|
|
|
|
|
dtype: torch.dtype,
|
2023-04-12 04:03:10 -06:00
|
|
|
|
device: torch.device,
|
2023-05-10 07:48:21 -06:00
|
|
|
|
rank: int = 0,
|
|
|
|
|
world_size: int = 1,
|
2023-04-12 04:03:10 -06:00
|
|
|
|
):
|
2023-06-30 11:09:59 -06:00
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
|
torch.cuda.set_per_process_memory_fraction(1.0)
|
|
|
|
|
|
2023-05-16 15:23:27 -06:00
|
|
|
|
self.model = model.eval()
|
2022-11-04 07:22:47 -06:00
|
|
|
|
self.tokenizer = tokenizer
|
2023-02-24 07:55:57 -07:00
|
|
|
|
self.all_special_ids = set(tokenizer.all_special_ids)
|
2023-04-21 07:36:29 -06:00
|
|
|
|
self.requires_padding = requires_padding
|
|
|
|
|
self.dtype = dtype
|
2022-11-04 07:22:47 -06:00
|
|
|
|
self.device = device
|
2023-05-10 07:48:21 -06:00
|
|
|
|
self.rank = rank
|
|
|
|
|
self.world_size = world_size
|
2023-06-01 03:41:35 -06:00
|
|
|
|
|
|
|
|
|
self.has_position_ids = (
|
|
|
|
|
inspect.signature(model.forward).parameters.get("position_ids", None)
|
|
|
|
|
is not None
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-15 03:32:25 -06:00
|
|
|
|
self.check_initialized()
|
2022-11-04 07:22:47 -06:00
|
|
|
|
|
2023-04-21 07:36:29 -06:00
|
|
|
|
@property
|
|
|
|
|
def info(self) -> InfoResponse:
|
|
|
|
|
return InfoResponse(
|
|
|
|
|
requires_padding=self.requires_padding,
|
|
|
|
|
dtype=str(self.dtype),
|
|
|
|
|
device_type=self.device.type,
|
|
|
|
|
)
|
|
|
|
|
|
2022-11-04 11:03:04 -06:00
|
|
|
|
@property
|
2022-11-03 09:07:54 -06:00
|
|
|
|
@abstractmethod
|
2022-11-04 11:03:04 -06:00
|
|
|
|
def batch_type(self) -> Type[B]:
|
2022-11-03 09:07:54 -06:00
|
|
|
|
raise NotImplementedError
|
2022-10-28 11:24:00 -06:00
|
|
|
|
|
2022-11-04 11:03:04 -06:00
|
|
|
|
@abstractmethod
|
|
|
|
|
def generate_token(self, batch: B) -> Tuple[List[GeneratedText], Optional[B]]:
|
|
|
|
|
raise NotImplementedError
|
2023-03-06 05:22:58 -07:00
|
|
|
|
|
2023-07-19 01:31:25 -06:00
|
|
|
|
def warmup(self, batch: B) -> Optional[int]:
|
2023-06-30 11:09:59 -06:00
|
|
|
|
self.generate_token(batch)
|
2023-07-19 01:31:25 -06:00
|
|
|
|
return None
|
2023-06-30 11:09:59 -06:00
|
|
|
|
|
2023-04-11 08:38:22 -06:00
|
|
|
|
def decode_token(
|
|
|
|
|
self,
|
|
|
|
|
all_input_ids: List[int],
|
2023-05-16 15:23:27 -06:00
|
|
|
|
prefix_offset: int = 0,
|
|
|
|
|
read_offset: int = 0,
|
|
|
|
|
) -> Tuple[str, int, int]:
|
2023-03-06 05:22:58 -07:00
|
|
|
|
"""Hack to hopefully support generate_stream for the maximum number of tokenizers"""
|
2023-04-11 08:38:22 -06:00
|
|
|
|
|
2023-05-16 15:23:27 -06:00
|
|
|
|
# The prefix text is necessary only to defeat cleanup algorithms in the decode
|
|
|
|
|
# which decide to add a space or not depending on the surrounding ids.
|
|
|
|
|
prefix_text = self.tokenizer.decode(
|
|
|
|
|
all_input_ids[prefix_offset:read_offset], skip_special_tokens=False
|
|
|
|
|
)
|
|
|
|
|
new_text = self.tokenizer.decode(
|
|
|
|
|
all_input_ids[prefix_offset:], skip_special_tokens=False
|
|
|
|
|
)
|
2023-04-11 08:38:22 -06:00
|
|
|
|
|
2023-05-16 15:23:27 -06:00
|
|
|
|
if len(new_text) > len(prefix_text) and not new_text.endswith("<EFBFBD>"):
|
|
|
|
|
# utf-8 char at the end means it's a potential unfinished byte sequence
|
|
|
|
|
# from byte fallback tokenization.
|
|
|
|
|
# If it's in the middle, it's probably a real invalid id generated
|
|
|
|
|
# by the model
|
|
|
|
|
new_text = new_text[len(prefix_text) :]
|
|
|
|
|
return new_text, read_offset, len(all_input_ids)
|
2023-04-11 08:38:22 -06:00
|
|
|
|
else:
|
2023-05-16 15:23:27 -06:00
|
|
|
|
return "", prefix_offset, read_offset
|
2023-05-15 03:32:25 -06:00
|
|
|
|
|
|
|
|
|
def check_initialized(self):
|
|
|
|
|
uninitialized_parameters = []
|
|
|
|
|
for n, p in self.model.named_parameters():
|
|
|
|
|
if p.data.device == torch.device("meta"):
|
|
|
|
|
uninitialized_parameters.append(n)
|
|
|
|
|
if uninitialized_parameters:
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
f"found uninitialized parameters in model {self.__class__.__name__}: {uninitialized_parameters}"
|
|
|
|
|
)
|