ar12c commited on
Commit
40bb089
·
verified ·
1 Parent(s): 19c7a81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -9
app.py CHANGED
@@ -233,6 +233,7 @@ def train_model(
233
  combined_ids = combined_ids[:HP["max_length"]]
234
  sequence_len = HP["max_length"]
235
 
 
236
  input_tensor = torch.tensor([combined_ids[:-1]], dtype=torch.long, device=device).unsqueeze(0)
237
  target_tensor = torch.tensor([combined_ids[1:]], dtype=torch.long, device=device).unsqueeze(0)
238
 
@@ -337,19 +338,18 @@ def generate_response(prompt: str) -> Iterator[str]:
337
 
338
  logits = gpt_model(input_tensor, mask=mask, key_padding_mask=padmask)
339
 
340
- # Extract last logits and flatten to 1D (vector of vocab size)
341
- logits = logits[:, -1, :].squeeze(0) / temperature # Shape: [N_vocab]
 
342
 
343
  # --- STRONG N-GRAM REPETITION PENALTY ---
344
- # Penalize tokens that have appeared in the last 10 steps (N-gram)
345
  PENALTY_WINDOW = 10
346
 
347
  if L > 1:
348
  # Get the sequence of the last PENALTY_WINDOW tokens generated by the model
349
- # Note: We need to exclude the system token at index 0 from being penalized
350
  history_ids = input_tensor[0, max(1, L - PENALTY_WINDOW):].tolist()
351
 
352
- # Check for immediate repetition of the last few tokens (N-gram check)
353
  for gen_id in history_ids:
354
  # Apply a logit penalty (push probability towards zero)
355
  if gen_id < logits.size(-1):
@@ -360,19 +360,17 @@ def generate_response(prompt: str) -> Iterator[str]:
360
  if t_id != -1 and t_id < logits.size(-1):
361
  logits[t_id] = -float('inf')
362
 
363
- # --- Top-K Sampling (FIXED DIMENSION) ---
364
- # Now working with 1D logits tensor [N_vocab]
365
  if top_k > 0:
366
  # Top-K filtering: keep only the top k tokens
367
  v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
368
 
369
  # Zero out the logits for tokens below the k-th smallest value
370
- # CRITICAL FIX: The comparison must happen against the k-th element of the 1D tensor v
371
  logits[logits < v[-1]] = -float('inf')
372
  # ----------------------
373
 
374
  # Softmax on the penalized logits
375
- probs = F.softmax(logits, dim=-1).unsqueeze(0) # Unsqueeze to restore batch dim for multinomial
376
 
377
  # Runtime safety checks
378
  if probs.size(-1) < gpt_model.embedding.num_embeddings:
 
233
  combined_ids = combined_ids[:HP["max_length"]]
234
  sequence_len = HP["max_length"]
235
 
236
+ # Ensure tensors are created with the correct shape (unsqueeze added back for consistency)
237
  input_tensor = torch.tensor([combined_ids[:-1]], dtype=torch.long, device=device).unsqueeze(0)
238
  target_tensor = torch.tensor([combined_ids[1:]], dtype=torch.long, device=device).unsqueeze(0)
239
 
 
338
 
339
  logits = gpt_model(input_tensor, mask=mask, key_padding_mask=padmask)
340
 
341
+ # --- CRITICAL FIX: Ensure logits is 1D for processing ---
342
+ # Shape: [1, 1, N_vocab] -> Reduce to [N_vocab]
343
+ logits = logits[:, -1, :].squeeze(0) / temperature
344
 
345
  # --- STRONG N-GRAM REPETITION PENALTY ---
 
346
  PENALTY_WINDOW = 10
347
 
348
  if L > 1:
349
  # Get the sequence of the last PENALTY_WINDOW tokens generated by the model
 
350
  history_ids = input_tensor[0, max(1, L - PENALTY_WINDOW):].tolist()
351
 
352
+ # Penalize tokens that have appeared immediately before (N-gram check)
353
  for gen_id in history_ids:
354
  # Apply a logit penalty (push probability towards zero)
355
  if gen_id < logits.size(-1):
 
360
  if t_id != -1 and t_id < logits.size(-1):
361
  logits[t_id] = -float('inf')
362
 
363
+ # --- Top-K Sampling (operates on 1D logits) ---
 
364
  if top_k > 0:
365
  # Top-K filtering: keep only the top k tokens
366
  v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
367
 
368
  # Zero out the logits for tokens below the k-th smallest value
 
369
  logits[logits < v[-1]] = -float('inf')
370
  # ----------------------
371
 
372
  # Softmax on the penalized logits
373
+ probs = F.softmax(logits, dim=-1).unsqueeze(0) # Unsqueeze to add batch dim for multinomial
374
 
375
  # Runtime safety checks
376
  if probs.size(-1) < gpt_model.embedding.num_embeddings: