mirror of
https://github.com/huggingface/text-generation-inference.git
synced 2025-04-22 15:32:08 +00:00
# What does this PR do? Lifting check_unitialized. <!-- Congratulations! You've made it this far! You're not quite done yet though. Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution. Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change. Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost. --> <!-- Remove if not applicable --> Fixes # (issue) ## Before submitting - [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case). - [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests), Pull Request section? - [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link to it if that's the case. - [ ] Did you make sure to update the documentation with your changes? Here are the [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation). - [ ] Did you write any new necessary tests? ## Who can review? Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR. <!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @ @OlivierDehaene OR @Narsil -->
113 lines
3.7 KiB
Python
113 lines
3.7 KiB
Python
import torch
|
||
|
||
from abc import ABC, abstractmethod
|
||
from typing import List, Tuple, Optional, TypeVar, Type
|
||
from transformers import PreTrainedTokenizerBase
|
||
|
||
from text_generation_server.models.types import Batch, GeneratedText
|
||
from text_generation_server.pb.generate_pb2 import InfoResponse
|
||
|
||
B = TypeVar("B", bound=Batch)
|
||
|
||
|
||
class Model(ABC):
|
||
def __init__(
|
||
self,
|
||
tokenizer: PreTrainedTokenizerBase,
|
||
requires_padding: bool,
|
||
dtype: torch.dtype,
|
||
device: torch.device,
|
||
decode_buffer: int = 3,
|
||
rank: int = 0,
|
||
world_size: int = 1,
|
||
):
|
||
if decode_buffer < 1:
|
||
raise ValueError("decode_buffer must be >= 1")
|
||
|
||
self.tokenizer = tokenizer
|
||
self.all_special_ids = set(tokenizer.all_special_ids)
|
||
self.requires_padding = requires_padding
|
||
self.dtype = dtype
|
||
self.device = device
|
||
self.decode_buffer = decode_buffer
|
||
self.rank = rank
|
||
self.world_size = world_size
|
||
self.check_initialized()
|
||
|
||
@property
|
||
def info(self) -> InfoResponse:
|
||
return InfoResponse(
|
||
requires_padding=self.requires_padding,
|
||
dtype=str(self.dtype),
|
||
device_type=self.device.type,
|
||
)
|
||
|
||
@property
|
||
@abstractmethod
|
||
def batch_type(self) -> Type[B]:
|
||
raise NotImplementedError
|
||
|
||
@abstractmethod
|
||
def generate_token(self, batch: B) -> Tuple[List[GeneratedText], Optional[B]]:
|
||
raise NotImplementedError
|
||
|
||
def decode_token(
|
||
self,
|
||
all_input_ids: List[int],
|
||
offset: Optional[int] = None,
|
||
token_offset: Optional[int] = None,
|
||
) -> Tuple[str, Optional[int], Optional[int]]:
|
||
"""Hack to hopefully support generate_stream for the maximum number of tokenizers"""
|
||
if all_input_ids[-1] in self.all_special_ids:
|
||
return (
|
||
self.tokenizer.decode(all_input_ids[-1], skip_special_tokens=False),
|
||
None,
|
||
None,
|
||
)
|
||
|
||
if token_offset is None:
|
||
token_offset = len(all_input_ids) - self.decode_buffer
|
||
# left token buffer
|
||
if self.decode_buffer > 1:
|
||
# Decode token_offset token minus last one and token_offset tokens
|
||
raw_texts = self.tokenizer.batch_decode(
|
||
[all_input_ids[token_offset:-1], all_input_ids[token_offset:]],
|
||
skip_special_tokens=False,
|
||
)
|
||
|
||
# default offset is only the last token
|
||
offset = len(raw_texts[0])
|
||
sequence_text = raw_texts[1]
|
||
else:
|
||
# Only decode the last token without using a token buffer
|
||
sequence_text = self.tokenizer.decode(
|
||
all_input_ids[-1], skip_special_tokens=False
|
||
)
|
||
# no offset in this case
|
||
offset = 0
|
||
else:
|
||
assert offset is not None
|
||
sequence_text = self.tokenizer.decode(
|
||
all_input_ids[token_offset:],
|
||
skip_special_tokens=False,
|
||
)
|
||
|
||
# get text
|
||
token_text = sequence_text[offset:]
|
||
|
||
# if text is utf-8
|
||
if token_text and token_text[-1] != "<EFBFBD>":
|
||
return token_text, None, None
|
||
else:
|
||
return "", offset, token_offset
|
||
|
||
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}"
|
||
)
|