File size: 10,360 Bytes
2997d61 0b4c4af 2997d61 |
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 |
import numpy as np
import sys
import torch
from typing import List, Tuple, Union
from stripedhyena.model import StripedHyena
from stripedhyena.sample import sample
from stripedhyena.tokenizer import CharLevelTokenizer
from .scoring import logits_to_logprobs, prepare_batch
class Generator:
'''
Adapted from https://github.com/togethercomputer/stripedhyena.
Modifications include:
- `generate()` accepts and returns the recurrent cache state, letting the user
keep track of it across sampling runs.
- Able to sample with long token prompts in which the cache is initialized with
recurrent teacher forcing.
'''
def __init__(
self,
model: StripedHyena,
tokenizer: CharLevelTokenizer,
top_k: int = 50,
top_p: float = 0.7,
temperature: float = 1.,
):
self.model = model
self.tokenizer = tokenizer
self.top_k = top_k
self.top_p = top_p
self.temperature = temperature
self.untils = ['\n\n']
def generate(
self,
device: str,
input_string: str = None,
input_ids: torch.tensor = None,
num_tokens: int = 32,
cached_generation: bool = True,
force_prompt_threshold: int = 128,
print_generation: bool = True,
verbose: bool = False,
skip_special_tokens: bool = False,
stop_at_eos: bool = True,
max_seqlen: int = None,
inference_params_dict: dict = None,
) -> Tuple[torch.tensor, torch.tensor, dict]:
"""
A version of the generate() method that enables passing in and that returns the
`inference_params_dict` for replaying cached sampling from a given state.
"""
if isinstance(self.tokenizer.eos, int):
eos_token_ids = torch.LongTensor([self.tokenizer.eos]).to(device)
else:
# is a tensor
eos_token_ids = self.tokenizer.tokenize(self.tokenizer.eos).to(device)
if input_ids is None:
input = self.tokenizer.tokenize(input_string)
if isinstance(input, list):
input = torch.LongTensor(input).unsqueeze(0).to(device)
# is a tensor
else:
input = input.unsqueeze(0).to(device)
else:
input = input_ids
x = input
if max_seqlen is not None:
x = x[:, -max_seqlen :]
num_tokens = int(num_tokens)
batch_size = x.shape[0]
prompt_length = x.shape[1]
prompt_forcing = prompt_length > force_prompt_threshold
if prompt_forcing:
forced_prompt_length = prompt_length - force_prompt_threshold
x_force = x[:, force_prompt_threshold:]
x = x[:, :force_prompt_threshold]
else:
forced_prompt_length = 0
generation = torch.empty(
x.shape[0],
num_tokens,
dtype=torch.long,
device=x.device,
)
scores = torch.empty(
x.shape[0],
num_tokens,
self.tokenizer.vocab_size,
dtype=torch.float,
device=x.device,
)
# Initialize prefilled to False by default
prefilled = False
if inference_params_dict is not None:
cached_generation = True
prefilled = True
# Ensure that the cached data is loaded on the correct device.
for key, data in inference_params_dict['mha'].key_value_memory_dict.items():
inference_params_dict['mha'].key_value_memory_dict[key] = data.to(x.device)
for key, data in inference_params_dict['hyena'].fir_state_dict.items():
inference_params_dict['hyena'].fir_state_dict[key] = data.to(x.device)
for key, data in inference_params_dict['hyena'].state_dict.items():
inference_params_dict['hyena'].state_dict[key] = data.to(x.device)
elif cached_generation:
inference_params_dict = self.model.initialize_inference_params()
inference_params_dict['mha'].max_batch_size = batch_size
inference_params_dict['hyena'].max_batch_size = batch_size
prefilled = False
if verbose:
mem_after_tok = torch.cuda.memory_allocated(device=x.device) / 1e9
print(f'Memory after tokenization: {mem_after_tok} GB')
print('Starting generation...')
if input_string is not None:
print('Prompt: ' + input_string)
else:
print(f'Prompt ids: {input_ids} {input_ids.shape}')
for i in range(forced_prompt_length + num_tokens):
if prefilled:
post_prefill = True
else:
post_prefill = cached_generation and i > 0
# prefill then process only the last token
if post_prefill:
x = x[:, -1:]
seqlen_offset = inference_params_dict['mha'].seqlen_offset
if seqlen_offset == 0:
seqlen_offset = input.shape[-1]
inference_params_dict['hyena'].seqlen_offset = seqlen_offset
inference_params_dict['mha'].seqlen_offset = seqlen_offset
else:
inference_params_dict['mha'].seqlen_offset += 1
inference_params_dict['hyena'].seqlen_offset += 1
# do forward pass with no gradient
with torch.inference_mode():
logits, inference_params_dict = self.model(
x,
inference_params_dict=inference_params_dict,
)
last_logits = logits[:, -1]
if prompt_forcing and i < forced_prompt_length:
new_idx = x_force[:, i]
else:
new_idx = sample(
last_logits,
top_k=self.top_k,
top_p=self.top_p,
temperature=self.temperature,
)
if stop_at_eos and (generation[0, -2:] == eos_token_ids).all():
print('Stopping generation at EOS')
if print_generation and verbose and batch_size == 1:
print(
f'{self.tokenizer.detokenize([new_idx.item()])}',
end=' ',
)
if prompt_forcing:
if i >= forced_prompt_length:
scores[:, i - forced_prompt_length] = last_logits
generation[:, i - forced_prompt_length] = new_idx
else:
scores[:, i] = last_logits
generation[:, i] = new_idx
if post_prefill:
x = new_idx[:, None]
else:
x = torch.cat([x, new_idx[:, None]], dim=-1)
if verbose:
y = self.tokenizer.detokenize_batch(generation[:, : i + 1])
for until in self.untils:
if until in y:
y = y.split(until)[0]
break
print(f'\nInput: {input_string}, Output: {y}')
mem_end = torch.cuda.memory_allocated(device=x.device) / 1e9
print(f'Memory after generation: {mem_end} GB')
return generation[:, : i + 1], scores[:, : i + 1], inference_params_dict
def generate(
prompt_seqs: List[str],
model: StripedHyena,
tokenizer: CharLevelTokenizer,
n_tokens: int = 100,
temperature: float = 0.,
top_k: int = 1,
top_p: float = 1.,
batched: bool = True,
prepend_bos: bool = False,
cached_generation: bool = False,
force_prompt_threshold: int = 128,
verbose: int = 1,
device: str = 'cuda:0',
**kwargs,
) -> Tuple[List[str], List[float]]:
"""
Performs generation from a list of prompts.
If all prompts are the same length, this can do batched generation.
Also supports cached generation for efficient sampling.
"""
model.eval()
g = Generator(
model,
tokenizer,
top_k=top_k,
top_p=top_p,
temperature=temperature,
)
uniform_lengths = all(len(s) == len(prompt_seqs[0]) for s in prompt_seqs)
if batched and uniform_lengths:
input_ids_list = [
prepare_batch(
prompt_seqs,
tokenizer,
prepend_bos=prepend_bos,
device=device,
)[0]
]
else:
if verbose:
if not uniform_lengths:
sys.stderr.write('Note: Prompts are of different lengths.\n')
sys.stderr.write('Note: Will not do batched generation.\n')
input_ids_list = [
prepare_batch(
[ prompt_seq ],
tokenizer,
prepend_bos=prepend_bos,
device=device,
)[0]
for prompt_seq in prompt_seqs
]
generated_seqs, generated_scores = [], []
for input_ids in input_ids_list:
batch_size = input_ids.shape[0]
output_ids, logits, _ = g.generate(
input_ids=input_ids,
num_tokens=n_tokens,
cached_generation=cached_generation,
force_prompt_threshold=force_prompt_threshold,
device=device,
print_generation=(verbose > 1),
verbose=(verbose > 1),
stop_at_eos=False,
)
if verbose > 1:
print('input_ids.shape', input_ids.shape)
print('output_ids.shape', output_ids.shape)
print('logits.shape', logits.shape)
generated_seqs_batch = list(tokenizer.detokenize_batch(output_ids))
assert len(generated_seqs_batch) == batch_size
generated_seqs += generated_seqs_batch
logprobs = logits_to_logprobs(logits, output_ids)
logprobs = logprobs.float().cpu().numpy()
generated_scores += [ np.mean(logprobs[idx]) for idx in range(batch_size) ]
assert len(generated_seqs) == len(generated_scores) == len(prompt_seqs)
if verbose:
for seq, score, prompt in zip(generated_seqs, generated_scores, prompt_seqs):
print(f'Prompt: "{prompt}",\tOutput: "{seq}",\tScore: {score}')
return generated_seqs, generated_scores
|