2023-01-20 04:24:39 -07:00
|
|
|
import torch
|
|
|
|
import torch.distributed
|
|
|
|
|
2023-02-14 05:02:16 -07:00
|
|
|
from typing import Optional, List
|
2023-01-20 04:24:39 -07:00
|
|
|
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
2023-03-07 10:52:22 -07:00
|
|
|
from text_generation_server.models import CausalLM
|
2023-01-20 04:24:39 -07:00
|
|
|
|
|
|
|
FIM_PREFIX = "<fim-prefix>"
|
|
|
|
FIM_MIDDLE = "<fim-middle>"
|
|
|
|
FIM_SUFFIX = "<fim-suffix>"
|
|
|
|
FIM_PAD = "<fim-pad>"
|
|
|
|
EOD = "<|endoftext|>"
|
|
|
|
|
|
|
|
|
|
|
|
class SantaCoder(CausalLM):
|
2023-05-12 06:46:41 -06:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
model_id: str,
|
|
|
|
revision: Optional[str] = None,
|
|
|
|
quantize: Optional[str] = None,
|
2023-06-30 12:30:09 -06:00
|
|
|
dtype: Optional[torch.dtype] = None,
|
2023-05-23 12:40:39 -06:00
|
|
|
trust_remote_code: bool = False,
|
2023-05-12 06:46:41 -06:00
|
|
|
):
|
2023-01-20 04:24:39 -07:00
|
|
|
if torch.cuda.is_available():
|
|
|
|
device = torch.device("cuda")
|
2023-06-30 12:30:09 -06:00
|
|
|
dtype = torch.float16 if dtype is None else dtype
|
2023-01-20 04:24:39 -07:00
|
|
|
else:
|
|
|
|
if quantize:
|
|
|
|
raise ValueError("quantization is not available on CPU")
|
|
|
|
|
|
|
|
device = torch.device("cpu")
|
2023-09-19 09:19:28 -06:00
|
|
|
dtype = torch.float32 if dtype is None else dtype
|
2023-01-20 04:24:39 -07:00
|
|
|
|
2023-01-31 10:53:56 -07:00
|
|
|
tokenizer = AutoTokenizer.from_pretrained(
|
2023-05-23 12:40:39 -06:00
|
|
|
model_id,
|
|
|
|
revision=revision,
|
|
|
|
padding_side="left",
|
|
|
|
truncation_side="left",
|
|
|
|
trust_remote_code=trust_remote_code,
|
2023-01-31 10:53:56 -07:00
|
|
|
)
|
2023-01-20 04:24:39 -07:00
|
|
|
tokenizer.add_special_tokens(
|
|
|
|
{
|
|
|
|
"additional_special_tokens": [
|
|
|
|
EOD,
|
|
|
|
FIM_PREFIX,
|
|
|
|
FIM_MIDDLE,
|
|
|
|
FIM_SUFFIX,
|
|
|
|
FIM_PAD,
|
|
|
|
],
|
|
|
|
"pad_token": EOD,
|
|
|
|
}
|
|
|
|
)
|
2023-07-21 03:27:31 -06:00
|
|
|
with device:
|
|
|
|
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
model_id,
|
|
|
|
revision=revision,
|
|
|
|
torch_dtype=dtype,
|
|
|
|
load_in_8bit=quantize == "bitsandbytes",
|
|
|
|
trust_remote_code=trust_remote_code,
|
|
|
|
)
|
2023-01-20 04:24:39 -07:00
|
|
|
|
|
|
|
super(CausalLM, self).__init__(
|
2023-05-16 15:23:27 -06:00
|
|
|
model=model,
|
2023-04-21 07:36:29 -06:00
|
|
|
tokenizer=tokenizer,
|
|
|
|
requires_padding=True,
|
|
|
|
dtype=dtype,
|
|
|
|
device=device,
|
2023-01-20 04:24:39 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
def decode(self, generated_ids: List[int]) -> str:
|
|
|
|
# Do not skip special tokens as they are used for custom parsing rules of the generated text
|
|
|
|
return self.tokenizer.decode(
|
2023-05-03 02:10:34 -06:00
|
|
|
generated_ids, skip_special_tokens=False, clean_up_tokenization_spaces=False
|
2023-01-20 04:24:39 -07:00
|
|
|
)
|