File size: 17,752 Bytes
f50d964 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
from __future__ import annotations
import configparser
import pathlib
import typing
import os
import torch
import transformers
from torch.nn.utils.rnn import pad_sequence
from .config import LYRA_BAICHUAN_PARAM, LIB_SO_PATH
from .model import BaichuanModel
from .tokenization_baichuan import BaichuanTokenizer
class lyraBaichuan7B:
def __init__(self, model_path, tokenizer_path=None, dtype='fp16', memopt_mode=0, arch='Ampere', cuda_version=12) -> None:
self.model_path = model_path
self.tokenizer_path = tokenizer_path
self.dtype = dtype
self.memopt_mode = memopt_mode
self.arch = arch
self.cuda_version = cuda_version
self.model, self.tokenizer = self.load_model_and_tokenizer()
print("Got model and tokenizer")
def load_model_and_tokenizer(self):
if self.tokenizer_path is None:
tokenizer_path = self.model_path
else:
tokenizer_path = self.tokenizer_path
print(f'Loading tokenizer from {tokenizer_path}')
tokenizer = BaichuanTokenizer.from_pretrained(tokenizer_path)
checkpoint_path = pathlib.Path(self.model_path)
config_path = checkpoint_path / 'config.ini'
if config_path.exists():
# Read model params from config.
cfg = configparser.ConfigParser()
cfg.read(config_path)
model_name = 'baichuan'
inference_data_type = self.dtype
if inference_data_type == None:
inference_data_type = cfg.get(model_name, "weight_data_type")
model_args = dict(
head_num=cfg.getint(model_name, 'head_num'),
size_per_head=cfg.getint(model_name, "size_per_head"),
inter_size=cfg.getint(model_name, 'inter_size'),
layer_num=cfg.getint(model_name, "num_layer"),
rotary_embedding_dim=cfg.getint(model_name, 'rotary_embedding'),
layernorm_eps=cfg.getfloat(model_name, 'layernorm_eps'),
vocab_size=cfg.getint(model_name, "vocab_size"),
start_id=cfg.getint(model_name, "start_id"),
end_id=cfg.getint(model_name, "end_id"),
weights_data_type=cfg.get(model_name, "weight_data_type"),
tensor_para_size=cfg.getint(model_name, "tensor_para_size"),
inference_data_type=inference_data_type)
else:
inference_data_type = self.dtype
if inference_data_type == None:
inference_data_type = LYRA_BAICHUAN_PARAM.weights_data_type
model_args = dict(head_num=LYRA_BAICHUAN_PARAM.num_heads,
size_per_head=LYRA_BAICHUAN_PARAM.size_per_head,
inter_size=LYRA_BAICHUAN_PARAM.inter_size,
layer_num=LYRA_BAICHUAN_PARAM.num_layers,
rotary_embedding_dim=LYRA_BAICHUAN_PARAM.rotary_embedding,
layernorm_eps=LYRA_BAICHUAN_PARAM.layernorm_eps,
vocab_size=LYRA_BAICHUAN_PARAM.vocab_size,
start_id=LYRA_BAICHUAN_PARAM.start_id or tokenizer.bos_token_id,
end_id=LYRA_BAICHUAN_PARAM.end_id or tokenizer.eos_token_id,
weights_data_type=LYRA_BAICHUAN_PARAM.weights_data_type,
tensor_para_size=LYRA_BAICHUAN_PARAM.tensor_para_size,
inference_data_type=inference_data_type)
# update common parameters
# Load the C++ model into Pytorch model.
sm = "sm80"
if self.arch == "Ampere":
sm = "sm80"
elif self.arch == "Volta":
sm = "sm70"
else:
raise Exception(f"unsupported arch: {self.arch}")
cu = 'cu11'
if self.cuda_version == 11:
cu = 'cu11'
elif self.cuda_version == 12:
cu = 'cu12'
else:
raise Exception(f"unsupported cuda version: {self.cuda_version}")
lib_path = pathlib.Path(__file__).parent / "ftlib" / f"libth_transformer_{sm}_{cu}.so"
model_args.update(dict(
lib_path=lib_path,
model_path=os.path.join(self.model_path, "1-gpu-fp16.bin"),
max_seq_len=0, # for position seq embedding
pipeline_para_size=LYRA_BAICHUAN_PARAM.pipeline_para_size,
use_gptj_residual=LYRA_BAICHUAN_PARAM.use_gptj_residual,
memopt_mode=self.memopt_mode
))
print('[FT][INFO] Load Our FT Highly Optimized Baichuan-7B model')
for k, v in model_args.items():
print(f' - {k.ljust(25, ".")}: {v}')
# Check sanity and consistency between the model and tokenizer.
checklist = ['head_num', 'size_per_head', 'vocab_size', 'layer_num',
'tensor_para_size', 'tensor_para_size', 'weights_data_type']
if None in [model_args[k] for k in checklist]:
none_params = [p for p in checklist if model_args[p] is None]
print(f'[FT][WARNING] Found None parameters {none_params}. They must '
f'be provided either by config file or CLI arguments.')
if model_args['start_id'] != tokenizer.bos_token_id:
print('[FT][WARNING] Given start_id is not matched with the bos token '
'id of the pretrained tokenizer.')
if model_args['end_id'] not in (tokenizer.pad_token_id, tokenizer.eos_token_id):
print('[FT][WARNING] Given end_id is not matched with neither pad '
'token id nor eos token id of the pretrained tokenizer.')
print(f'Loading model from {self.model_path}')
model = BaichuanModel(**model_args)
return model, tokenizer
def generate(self, prompts: typing.List[str] | str,
output_length: int = 512,
beam_width: int = 1,
top_k: typing.Optional[torch.IntTensor] = 1,
top_p: typing.Optional[torch.FloatTensor] = 1.0,
beam_search_diversity_rate: typing.Optional[torch.FloatTensor] = 0.0,
temperature: typing.Optional[torch.FloatTensor] = 1.0,
len_penalty: typing.Optional[torch.FloatTensor] = 0.0,
repetition_penalty: typing.Optional[torch.FloatTensor] = 1.0,
presence_penalty: typing.Optional[torch.FloatTensor] = None,
min_length: typing.Optional[torch.IntTensor] = None,
bad_words_list: typing.Optional[torch.IntTensor] = None,
do_sample: bool = False,
return_output_length: bool = False,
return_cum_log_probs: int = 0):
#
if isinstance(prompts, str):
prompts = [prompts, ]
inputs = prompts
batch_size = len(inputs)
ones_int = torch.ones(size=[batch_size], dtype=torch.int32)
ones_float = torch.ones(size=[batch_size], dtype=torch.float32)
# we must encode the raw prompt text one by one in order to compute the length of the original text.
input_token_ids = [self.tokenizer(text, return_tensors="pt").input_ids.int().squeeze() for text in inputs]
input_lengths = torch.IntTensor([len(ids) for ids in input_token_ids])
# after got the length of each input text tokens. we can batchfy the input list to a tensor. padding the right.
input_token_ids = pad_sequence(input_token_ids, batch_first=True, padding_value=self.tokenizer.eos_token_id)
random_seed = None
if do_sample:
random_seed = torch.randint(0, 262144, (batch_size,), dtype=torch.long)
outputs = self.model(start_ids=input_token_ids,
start_lengths=input_lengths,
output_len=output_length,
beam_width=beam_width,
top_k=top_k * ones_int,
top_p=top_p * ones_float,
beam_search_diversity_rate=beam_search_diversity_rate * ones_float,
temperature=temperature * ones_float,
len_penalty=len_penalty * ones_float,
repetition_penalty=repetition_penalty * ones_float,
random_seed=random_seed,
return_output_length=return_output_length,
return_cum_log_probs=return_cum_log_probs)
if return_cum_log_probs > 0:
outputs = outputs[0] # output_token_ids.
# Slice the generated token ids of the 1st beam result.
# output = input tokens + generated tokens.
output_token_ids = [out[0, length:].cpu()
for out, length in zip(outputs, input_lengths)]
output_texts = self.tokenizer.batch_decode(
output_token_ids, skip_special_tokens=True)
return output_texts
class lyraBaichuan13B:
def __init__(self, model_path, tokenizer_path=None, dtype='fp16', memopt_mode=0, arch='Ampere', cuda_version=12) -> None:
self.model_path = model_path
self.tokenizer_path = tokenizer_path
self.dtype = dtype
self.memopt_mode = memopt_mode
self.arch = arch
self.cuda_version = cuda_version
self.model, self.tokenizer = self.load_model_and_tokenizer()
print("Got model and tokenizer")
def load_model_and_tokenizer(self):
if self.tokenizer_path is None:
tokenizer_path = self.model_path
else:
tokenizer_path = self.tokenizer_path
print(f'Loading tokenizer from {tokenizer_path}')
tokenizer = BaichuanTokenizer.from_pretrained(tokenizer_path)
checkpoint_path = pathlib.Path(self.model_path)
config_path = checkpoint_path / 'config.ini'
if config_path.exists():
# Read model params from config.
cfg = configparser.ConfigParser()
cfg.read(config_path)
model_name = 'baichuan'
inference_data_type = self.dtype
if inference_data_type == None:
inference_data_type = cfg.get(model_name, "weight_data_type")
model_args = dict(
head_num=cfg.getint(model_name, 'head_num'),
size_per_head=cfg.getint(model_name, "size_per_head"),
inter_size=cfg.getint(model_name, 'inter_size'),
layer_num=cfg.getint(model_name, "num_layer"),
rotary_embedding_dim=0,
layernorm_eps=cfg.getfloat(model_name, 'layernorm_eps'),
vocab_size=cfg.getint(model_name, "vocab_size"),
start_id=cfg.getint(model_name, "start_id"),
end_id=cfg.getint(model_name, "end_id"),
weights_data_type=cfg.get(model_name, "weight_data_type"),
tensor_para_size=cfg.getint(model_name, "tensor_para_size"),
inference_data_type=inference_data_type)
else:
inference_data_type = self.dtype
if inference_data_type == None:
inference_data_type = LYRA_BAICHUAN_PARAM.weights_data_type
model_args = dict(head_num=LYRA_BAICHUAN_PARAM.num_heads,
size_per_head=LYRA_BAICHUAN_PARAM.size_per_head,
inter_size=LYRA_BAICHUAN_PARAM.inter_size,
layer_num=LYRA_BAICHUAN_PARAM.num_layers,
rotary_embedding_dim=0,
layernorm_eps=LYRA_BAICHUAN_PARAM.layernorm_eps,
vocab_size=LYRA_BAICHUAN_PARAM.vocab_size,
start_id=LYRA_BAICHUAN_PARAM.start_id or tokenizer.bos_token_id,
end_id=LYRA_BAICHUAN_PARAM.end_id or tokenizer.eos_token_id,
weights_data_type=LYRA_BAICHUAN_PARAM.weights_data_type,
tensor_para_size=LYRA_BAICHUAN_PARAM.tensor_para_size,
inference_data_type=inference_data_type)
# update common parameters
# Load the C++ model into Pytorch model.
sm = "sm80"
if self.arch == "Ampere":
sm = "sm80"
elif self.arch == "Volta":
sm = "sm70"
else:
raise Exception(f"unsupported arch: {self.arch}")
cu = 'cu11'
if self.cuda_version == 11:
cu = 'cu11'
elif self.cuda_version == 12:
cu = 'cu12'
else:
raise Exception(f"unsupported cuda version: {self.cuda_version}")
lib_path = pathlib.Path(__file__).parent / "ftlib" / f"libth_transformer_{sm}_{cu}.so"
model_args.update(dict(
lib_path=lib_path,
model_path=os.path.join(self.model_path, "1-gpu-fp16.bin"),
max_seq_len=0, # for position seq embedding
pipeline_para_size=LYRA_BAICHUAN_PARAM.pipeline_para_size,
use_gptj_residual=LYRA_BAICHUAN_PARAM.use_gptj_residual,
memopt_mode=self.memopt_mode
))
print('[FT][INFO] Load Our FT Highly Optimized Baichuan-13B model')
for k, v in model_args.items():
print(f' - {k.ljust(25, ".")}: {v}')
# Check sanity and consistency between the model and tokenizer.
checklist = ['head_num', 'size_per_head', 'vocab_size', 'layer_num',
'tensor_para_size', 'tensor_para_size', 'weights_data_type']
if None in [model_args[k] for k in checklist]:
none_params = [p for p in checklist if model_args[p] is None]
print(f'[FT][WARNING] Found None parameters {none_params}. They must '
f'be provided either by config file or CLI arguments.')
if model_args['start_id'] != tokenizer.bos_token_id:
print('[FT][WARNING] Given start_id is not matched with the bos token '
'id of the pretrained tokenizer.')
if model_args['end_id'] not in (tokenizer.pad_token_id, tokenizer.eos_token_id):
print('[FT][WARNING] Given end_id is not matched with neither pad '
'token id nor eos token id of the pretrained tokenizer.')
print(f'Loading model from {self.model_path}')
model = BaichuanModel(**model_args)
return model, tokenizer
def generate(self, prompts: typing.List[str] | str,
output_length: int = 512,
beam_width: int = 1,
top_k: typing.Optional[torch.IntTensor] = 1,
top_p: typing.Optional[torch.FloatTensor] = 1.0,
beam_search_diversity_rate: typing.Optional[torch.FloatTensor] = 0.0,
temperature: typing.Optional[torch.FloatTensor] = 1.0,
len_penalty: typing.Optional[torch.FloatTensor] = 0.0,
repetition_penalty: typing.Optional[torch.FloatTensor] = 1.0,
presence_penalty: typing.Optional[torch.FloatTensor] = None,
min_length: typing.Optional[torch.IntTensor] = None,
bad_words_list: typing.Optional[torch.IntTensor] = None,
do_sample: bool = False,
return_output_length: bool = False,
return_cum_log_probs: int = 0):
#
if isinstance(prompts, str):
prompts = [prompts, ]
inputs = prompts
batch_size = len(inputs)
ones_int = torch.ones(size=[batch_size], dtype=torch.int32)
ones_float = torch.ones(size=[batch_size], dtype=torch.float32)
# we must encode the raw prompt text one by one in order to compute the length of the original text.
input_token_ids = [self.tokenizer(text, return_tensors="pt").input_ids.int().squeeze() for text in inputs]
input_lengths = torch.IntTensor([len(ids) for ids in input_token_ids])
# after got the length of each input text tokens. we can batchfy the input list to a tensor. padding the right.
input_token_ids = pad_sequence(input_token_ids, batch_first=True, padding_value=self.tokenizer.eos_token_id)
random_seed = None
if do_sample:
random_seed = torch.randint(0, 262144, (batch_size,), dtype=torch.long)
outputs = self.model(start_ids=input_token_ids,
start_lengths=input_lengths,
output_len=output_length,
beam_width=beam_width,
top_k=top_k * ones_int,
top_p=top_p * ones_float,
beam_search_diversity_rate=beam_search_diversity_rate * ones_float,
temperature=temperature * ones_float,
len_penalty=len_penalty * ones_float,
repetition_penalty=repetition_penalty * ones_float,
random_seed=random_seed,
return_output_length=return_output_length,
return_cum_log_probs=return_cum_log_probs)
if return_cum_log_probs > 0:
outputs = outputs[0] # output_token_ids.
# Slice the generated token ids of the 1st beam result.
# output = input tokens + generated tokens.
output_token_ids = [out[0, length:].cpu()
for out, length in zip(outputs, input_lengths)]
output_texts = self.tokenizer.batch_decode(
output_token_ids, skip_special_tokens=True)
return output_texts
|