(
model,
tokenizer,
input: str,
gen_params: dict,
device: str = "cuda:0",
stream_interval: int = 2,
echo: bool = False,
stop_token_ids=[],
)
| 34 | |
| 35 | @torch.inference_mode() |
| 36 | def StreamGenerator( |
| 37 | model, |
| 38 | tokenizer, |
| 39 | input: str, |
| 40 | gen_params: dict, |
| 41 | device: str = "cuda:0", |
| 42 | stream_interval: int = 2, |
| 43 | echo: bool = False, |
| 44 | stop_token_ids=[], |
| 45 | ): |
| 46 | # print("inputaaa:", input) |
| 47 | input_ids = tokenizer(input).input_ids |
| 48 | input_echo_len = len(input_ids) |
| 49 | # print(input_ids) |
| 50 | output_ids = list(input_ids) |
| 51 | len_input = len(input) |
| 52 | |
| 53 | if gen_params.top_k <= 0: |
| 54 | top_k = gen_params.n_vocab |
| 55 | else: |
| 56 | top_k = gen_params.top_k |
| 57 | logits_processor = prepare_logits_processor( |
| 58 | gen_params.temp, gen_params.repeat_penalty, gen_params.top_p, top_k |
| 59 | ) |
| 60 | |
| 61 | past_key_values = out = None |
| 62 | stop_token_ids.append(tokenizer.eos_token_id) |
| 63 | max_new_tokens = gen_params.n_predict |
| 64 | start_pos = 0 |
| 65 | for i in range(max_new_tokens): |
| 66 | torch.cuda.synchronize() |
| 67 | t_st = time.time() |
| 68 | |
| 69 | if i == 0: |
| 70 | inputs = torch.as_tensor([input_ids], device=device) |
| 71 | else: |
| 72 | inputs = torch.as_tensor([[token]], device=device) |
| 73 | |
| 74 | if ( |
| 75 | "llama" not in model.__class__.__name__.lower() |
| 76 | and "mpt" not in model.__class__.__name__.lower() |
| 77 | and "falcon" not in model.__class__.__name__.lower() |
| 78 | # or model.__class__.__name__.lower() == "llamaforcausallm" |
| 79 | ): |
| 80 | if i == 0: # Context Stage |
| 81 | out = model(inputs, use_cache=True, past_key_values=past_key_values) |
| 82 | logits = out.logits |
| 83 | past_key_values = out.past_key_values |
| 84 | else: |
| 85 | out = model( |
| 86 | input_ids=inputs, |
| 87 | use_cache=True, |
| 88 | past_key_values=past_key_values, |
| 89 | ) |
| 90 | logits = out.logits |
| 91 | past_key_values = out.past_key_values |
| 92 | else: |
| 93 | # raise 1 |
nothing calls this directly
no test coverage detected