File size: 16,387 Bytes
3a464db | 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 | from typing import Callable, Optional, Union
import torch
import types
from transformers.utils import auto_docstring, logging
# Constants for Fast_dLLM model
FAST_DLLM_MASK_ID = 151665
FAST_DLLM_STOP_TOKEN = 151645
MASK_COLOR = 0.5
TOKEN_COLOR = -0.5
@auto_docstring
class Fast_dLLM_QwenForCausalLM:
@torch.no_grad()
def batch_sample(
self,
input_ids,
tokenizer,
block_size,
max_new_tokens,
small_block_size,
min_len,
seq_len,
mask_id=151665,
threshold=0.95,
stop_token=151645,
use_block_cache=False,
top_p=0.95,
temperature=0.0,
):
num_blocks = max_new_tokens // block_size + seq_len.max().item() // block_size
batch_size = input_ids.shape[0]
if min_len > block_size:
output = self.forward(input_ids=input_ids[:, :(min_len // block_size * block_size)], use_cache=True, update_past_key_values=True, block_size=block_size)
logits, past_key_values = output.logits, output.past_key_values
if min_len % block_size == 0:
predict_sample_idx = (seq_len == min_len)
predict_logits = logits[predict_sample_idx, -1:, :]
next_token = predict_logits.argmax(dim=-1)
if input_ids.shape[1] <= min_len:
input_ids = torch.cat([input_ids, next_token], dim=1)
else:
input_ids[predict_sample_idx, min_len] = next_token.squeeze(dim=-1)
else:
past_key_values = None
seq_block_idx = seq_len // block_size
finished_flag = torch.zeros((batch_size), device=self.device, dtype=torch.bool)
start_block_idx = min_len // block_size
num_small_blocks = block_size // small_block_size
sample_indices = torch.arange(batch_size, device=self.device)
finished_samples = {}
for block_idx in range(start_block_idx, num_blocks):
if finished_flag.all():
break
if (seq_block_idx == block_idx).all():
x_init = mask_id * torch.ones((input_ids.shape[0], block_size-input_ids.shape[1]%block_size), device=self.device, dtype=torch.long)
x_init = torch.cat([input_ids, x_init], dim=1)
input_ids = x_init
else:
x_init = input_ids[:, :(block_idx + 1)*block_size]
x_init[finished_flag, -block_size:] = tokenizer.pad_token_id
x_t = x_init.clone()
step = 0
block_past_key_values = None
while True:
mask_idx = (x_t[:, -block_size:] == mask_id)
if mask_idx.sum() == 0:
for sample_idx in range(x_t.shape[0]):
if finished_flag[sample_idx] and seq_len[sample_idx] < (block_idx + 1) * block_size:
stop_token_idx = (x_t[sample_idx, seq_len[sample_idx]:] == stop_token).nonzero()[0][0]
x_t[sample_idx, seq_len[sample_idx]+stop_token_idx+1:] = tokenizer.pad_token_id
if finished_flag.all():
break
output = self.forward(input_ids=x_t[:, -block_size:], use_cache=True, past_key_values=past_key_values, update_past_key_values=True, block_size=block_size)
logits, past_key_values = output.logits, output.past_key_values
next_token = logits[:, -1:, :].argmax(dim=-1)
next_token[finished_flag] = tokenizer.pad_token_id
x_t = torch.cat([x_t, next_token], dim=1)
step += 1
break
for small_block_idx in range(num_small_blocks):
small_block_start_idx = small_block_idx * small_block_size
small_block_end_idx = small_block_start_idx + small_block_size
start = -block_size + small_block_start_idx
end = None if block_size == small_block_end_idx else -block_size + small_block_end_idx
while True:
mask_idx = (x_t[:, -block_size:] == mask_id)
if mask_idx[:, start:end].sum() == 0:
break
if use_block_cache:
if block_past_key_values is None or (x_t[:, -block_size+small_block_start_idx] == mask_id).any():
output = self.forward(input_ids=x_t[:, -block_size:], use_cache=True, past_key_values=past_key_values, update_past_key_values=False, use_block_cache=True)
logits, block_past_key_values = output.logits, output.block_past_key_values
logits = torch.cat([logits[:, :1, :], logits[:, :-1, :]], dim=1)
logits = logits[:, start:end]
else:
logits = self.forward(input_ids=x_t[:,start:end], use_cache=True, past_key_values=past_key_values, update_past_key_values=False, use_block_cache=True, block_past_key_values=block_past_key_values, replace_position=small_block_start_idx).logits
logits = torch.cat([logits[:, :1, :], logits[:, :-1, :]], dim=1)
else:
logits = self.forward(input_ids=x_t[:, -block_size:], use_cache=True, past_key_values=past_key_values, update_past_key_values=False).logits
logits = torch.cat([logits[:, :1, :], logits[:, :-1, :]], dim=1)
logits = logits[:, start:end]
x_1, p_1t = self.sample_with_top_p(logits, top_p=top_p, temperature=temperature)
x1_p = torch.squeeze(torch.gather(p_1t, dim=-1, index=torch.unsqueeze(x_1, -1)), -1)
x1_p = torch.where(mask_idx[:, start:end], x1_p, -torch.inf)
unmask_idx = (x1_p > threshold)
max_prob_idx = x1_p.argmax(dim=-1)
unmask_idx[torch.arange(x_1.shape[0]), max_prob_idx] = True
unmask_idx = unmask_idx & mask_idx[:, start:end]
x_t[:, start:end][unmask_idx] = x_1[unmask_idx]
finished_row_flags = ((x_1 == stop_token) & unmask_idx).any(dim=1) # shape: [B]
finished_flag = finished_flag | finished_row_flags
step += 1
if input_ids.shape[1] == x_t.shape[1]:
input_ids = x_t
else:
input_ids[:, :(block_idx + 1)*block_size] = x_t[:, :-1]
if (seq_block_idx == block_idx).all():
input_ids = torch.cat([input_ids, x_t[:, -1:]], dim=1)
else:
if input_ids.shape[1] <= (block_idx + 1)*block_size:
input_ids = x_t
else:
input_ids[seq_block_idx == block_idx, (block_idx + 1)*block_size] = x_t[seq_block_idx == block_idx, (block_idx + 1)*block_size]
seq_block_idx[seq_block_idx == block_idx] = block_idx + 1
if finished_flag.any():
for sample_idx in range(x_t.shape[0]):
if finished_flag[sample_idx]:
original_idx = sample_indices[sample_idx].item()
finished_samples[original_idx] = x_t[sample_idx:sample_idx+1].clone().squeeze(dim=0)
sample_indices = sample_indices[~finished_flag]
input_ids = input_ids[~finished_flag]
seq_block_idx = seq_block_idx[~finished_flag]
seq_len = seq_len[~finished_flag]
x_t = x_t[~finished_flag]
for layer_id in range(len(past_key_values)):
past_key_values.key_cache[layer_id] = past_key_values.key_cache[layer_id][~finished_flag]
past_key_values.value_cache[layer_id] = past_key_values.value_cache[layer_id][~finished_flag]
finished_flag = finished_flag[~finished_flag]
# add not finished samples since max_new_tokens is reached
if len(finished_samples) < batch_size:
for sample_idx in range(x_t.shape[0]):
original_idx = sample_indices[sample_idx].item()
finished_samples[original_idx] = x_t[sample_idx:sample_idx+1].clone().squeeze(dim=0)
assert len(finished_samples) == batch_size
return finished_samples
@torch.no_grad()
def mdm_sample_with_visualization(
self,
input_ids,
tokenizer,
block_size=32,
max_new_tokens=1024,
mask_id=FAST_DLLM_MASK_ID,
threshold=0.95,
small_block_size=32,
stop_token=FAST_DLLM_STOP_TOKEN,
temperature=0.0,
top_p=0.95,
):
"""
MDM sampling function with visualization
with intermediate state output for Gradio visualization
"""
nfe = 0
self.model.bd_size = block_size
num_blocks = max_new_tokens // block_size
# Initialize state - show all positions as mask
initial_state = []
if input_ids.shape[1] > block_size:
output = self.forward(input_ids=input_ids[:, :(input_ids.shape[1] // block_size * block_size)], use_cache=True, update_past_key_values=True)
logits, past_key_values = output.logits, output.past_key_values
nfe += 1
if input_ids.shape[1] % block_size == 0:
next_token = logits[:, -1:, :].argmax(dim=-1)
input_ids = torch.cat([input_ids, next_token], dim=1)
else:
past_key_values = None
num_small_blocks = block_size // small_block_size
original_input_length = input_ids.shape[1]
for block_idx in range(num_blocks):
if stop_token in input_ids[:, original_input_length:]:
break
prompt_length = input_ids.shape[1]
# Use the length of the first block to initialize state
first_block_length = block_size - (input_ids.shape[1] % block_size)
if len(initial_state) == 0:
for i in range(first_block_length):
initial_state.append(("[MASK]", MASK_COLOR))
yield initial_state
else:
for i in range(first_block_length):
current_state.append(("[MASK]", MASK_COLOR))
yield current_state
# Initialize x_init as mask_id
x_init = mask_id * torch.ones((input_ids.shape[0], block_size-prompt_length%block_size), device=self.device, dtype=torch.long)
x_init = torch.cat([input_ids, x_init], dim=1)
x_t = x_init.clone()
block_past_key_values = None
step = 0
while True:
if stop_token in x_t[:, prompt_length:]:
stop_token_idx = (x_t[:, prompt_length:] == stop_token).nonzero()[0][1]
if (x_t[:, prompt_length:prompt_length+stop_token_idx] == mask_id).sum() == 0:
break
mask_idx = (x_t[:, -block_size:] == mask_id)
# Decode a complete block, update cache, and generate next token
if mask_idx.sum() == 0:
nfe += 1
output = self.forward(input_ids=x_t[:, -block_size:], use_cache=True, past_key_values=past_key_values, update_past_key_values=True)
logits, past_key_values = output.logits, output.past_key_values
next_token = logits[:, -1:, :].argmax(dim=-1)
x_t = torch.cat([x_t, next_token], dim=1)
token_text = tokenizer.decode([next_token[0].item()], skip_special_tokens=True)
# Handle special characters
token_text = token_text
current_state.append((token_text, TOKEN_COLOR))
yield current_state
break
for small_block_idx in range(num_small_blocks):
small_block_start_idx = small_block_idx * small_block_size
small_block_end_idx = small_block_start_idx + small_block_size
start = -block_size + small_block_start_idx
end = None if block_size == small_block_end_idx else -block_size + small_block_end_idx
while True:
mask_idx = (x_t[:, -block_size:] == mask_id)
if mask_idx[:, start:end].sum() == 0:
break
if stop_token in x_t[:, prompt_length:]:
stop_token_idx = (x_t[:, prompt_length:] == stop_token).nonzero()[0][1]
if (x_t[:, prompt_length:prompt_length+stop_token_idx] == mask_id).sum() == 0:
break
logits = self.forward(input_ids=x_t[:, -block_size:], use_cache=True, past_key_values=past_key_values, update_past_key_values=False).logits
logits = torch.cat([logits[:, :1, :], logits[:, :-1, :]], dim=1)
logits = logits[:, start:end]
step += 1
x_1, p_1t = self.sample_with_top_p(logits, top_p=top_p, temperature=temperature)
# Select tokens with probability greater than threshold in p_1t
x1_p = torch.squeeze(torch.gather(p_1t, dim=-1, index=torch.unsqueeze(x_1, -1)), -1)
x1_p = torch.where(mask_idx[:, small_block_start_idx:small_block_end_idx], x1_p, -torch.inf)
unmask_idx = (x1_p > threshold)
max_prob_idx = x1_p.argmax(dim=-1)
unmask_idx[torch.arange(x_1.shape[0]), max_prob_idx] = True
unmask_idx = unmask_idx & mask_idx[:, start:end]
x_t[:, start:end][unmask_idx] = x_1[unmask_idx]
# Generate visualization state
current_state = []
generated_tokens = x_t[0, original_input_length:]
# Display generated tokens
for i, token_id in enumerate(generated_tokens):
if token_id == mask_id:
current_state.append(("[MASK]", MASK_COLOR))
else:
token_text = tokenizer.decode([token_id.item()], skip_special_tokens=True)
# Handle special characters
token_text = token_text
current_state.append((token_text, TOKEN_COLOR))
yield current_state
input_ids = x_t
# Truncate stop_token
if stop_token in input_ids[:, original_input_length:]:
stop_token_idx = (input_ids[:, original_input_length:] == stop_token).nonzero()[0][1]
input_ids = input_ids[:, :stop_token_idx+original_input_length+1]
# Final state - display complete text
final_state = []
generated_tokens = input_ids[0, original_input_length:]
for token_id in generated_tokens:
token_text = tokenizer.decode([token_id.item()], skip_special_tokens=True)
token_text = token_text
final_state.append((token_text, TOKEN_COLOR))
# Final state doesn't need mask padding, only show actually generated tokens
yield final_state
# Return final text
final_text = tokenizer.decode(generated_tokens, skip_special_tokens=True)
yield final_text
def setup_model_with_custom_generation(model):
"""
Set up custom generation functions for the model
"""
# Add mdm_sample method with visualization
model.mdm_sample_with_visualization = types.MethodType(Fast_dLLM_QwenForCausalLM.mdm_sample_with_visualization, model)
return model
|