Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,7 +34,7 @@ DEFAULTS = {
|
|
| 34 |
"n_layers": 12,
|
| 35 |
"dropout": 0.1,
|
| 36 |
"max_length": 1024,
|
| 37 |
-
"k_sampling":
|
| 38 |
"LEARNING_RATE": 1e-6,
|
| 39 |
}
|
| 40 |
|
|
@@ -233,8 +233,8 @@ 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)
|
| 237 |
-
target_tensor = torch.tensor([combined_ids[1:]], dtype=torch.long, device=device)
|
| 238 |
|
| 239 |
|
| 240 |
# 3. Forward Pass
|
|
@@ -321,18 +321,21 @@ def generate_response(prompt: str) -> Iterator[str]:
|
|
| 321 |
|
| 322 |
# --- Inference Parameters ---
|
| 323 |
temperature = 0.8
|
| 324 |
-
top_k = HP.get("k_sampling",
|
| 325 |
# ----------------------------
|
| 326 |
|
| 327 |
eos_id = voc.word2index[voc.EOS]
|
| 328 |
-
last_id = -1 # Track last generated ID for penalty
|
| 329 |
|
| 330 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
olm_tokens = [voc.word2index.get(t, -1) for t in ['o', 'l', 'm', 'olm'] if t in voc.word2index]
|
| 332 |
-
|
| 333 |
-
|
| 334 |
with torch.no_grad():
|
| 335 |
-
for
|
| 336 |
L = input_tensor.size(1)
|
| 337 |
mask = make_bool_causal_mask(L, device)
|
| 338 |
padmask = (input_tensor == voc.word2index[voc.PAD])
|
|
@@ -340,17 +343,25 @@ def generate_response(prompt: str) -> Iterator[str]:
|
|
| 340 |
logits = gpt_model(input_tensor, mask=mask, key_padding_mask=padmask)
|
| 341 |
logits = logits[:, -1, :] / temperature
|
| 342 |
|
| 343 |
-
# --- REPETITION PENALTY
|
| 344 |
-
|
| 345 |
-
|
| 346 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
for t_id in olm_tokens:
|
| 348 |
if t_id != -1:
|
| 349 |
-
logits[0, t_id] = -float('inf')
|
| 350 |
|
| 351 |
# --- Top-K Sampling ---
|
| 352 |
if top_k > 0:
|
| 353 |
-
# Top-K filtering: keep only the top k tokens
|
| 354 |
v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
|
| 355 |
logits[logits < v[:, [-1]]] = -float('inf')
|
| 356 |
# ----------------------
|
|
@@ -363,8 +374,6 @@ def generate_response(prompt: str) -> Iterator[str]:
|
|
| 363 |
|
| 364 |
next_id = torch.multinomial(probs, 1).item()
|
| 365 |
|
| 366 |
-
last_id = next_id # Update last generated ID
|
| 367 |
-
|
| 368 |
if next_id == eos_id:
|
| 369 |
break
|
| 370 |
|
|
|
|
| 34 |
"n_layers": 12,
|
| 35 |
"dropout": 0.1,
|
| 36 |
"max_length": 1024,
|
| 37 |
+
"k_sampling": 200, # INCREASED to 200 to help break loops
|
| 38 |
"LEARNING_RATE": 1e-6,
|
| 39 |
}
|
| 40 |
|
|
|
|
| 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 |
|
| 239 |
|
| 240 |
# 3. Forward Pass
|
|
|
|
| 321 |
|
| 322 |
# --- Inference Parameters ---
|
| 323 |
temperature = 0.8
|
| 324 |
+
top_k = HP.get("k_sampling", 200) # Using 200 to widen token variety
|
| 325 |
# ----------------------------
|
| 326 |
|
| 327 |
eos_id = voc.word2index[voc.EOS]
|
|
|
|
| 328 |
|
| 329 |
+
# --- Repetition Penalty Setup ---
|
| 330 |
+
# Find all tokens already generated in the input sequence
|
| 331 |
+
# Use a dictionary for fast lookup of IDs recently generated
|
| 332 |
+
generated_id_set = {}
|
| 333 |
+
|
| 334 |
+
# Get the index for "olm" tokens for strong penalty
|
| 335 |
olm_tokens = [voc.word2index.get(t, -1) for t in ['o', 'l', 'm', 'olm'] if t in voc.word2index]
|
| 336 |
+
|
|
|
|
| 337 |
with torch.no_grad():
|
| 338 |
+
for i in range(HP["max_length"]):
|
| 339 |
L = input_tensor.size(1)
|
| 340 |
mask = make_bool_causal_mask(L, device)
|
| 341 |
padmask = (input_tensor == voc.word2index[voc.PAD])
|
|
|
|
| 343 |
logits = gpt_model(input_tensor, mask=mask, key_padding_mask=padmask)
|
| 344 |
logits = logits[:, -1, :] / temperature
|
| 345 |
|
| 346 |
+
# --- STRONG N-GRAM REPETITION PENALTY ---
|
| 347 |
+
# Penalize tokens that have appeared in the last 10 steps (N-gram)
|
| 348 |
+
PENALTY_WINDOW = 10
|
| 349 |
|
| 350 |
+
if L > 1:
|
| 351 |
+
# Get the sequence of the last PENALTY_WINDOW tokens generated by the model
|
| 352 |
+
history_ids = input_tensor[0, -min(L - 1, PENALTY_WINDOW):].tolist()
|
| 353 |
+
|
| 354 |
+
# Check for immediate repetition of the last few tokens (N-gram check)
|
| 355 |
+
for gen_id in history_ids:
|
| 356 |
+
logits[0, gen_id] = -float('inf')
|
| 357 |
+
|
| 358 |
+
# Strong penalty against spamming OLM name
|
| 359 |
for t_id in olm_tokens:
|
| 360 |
if t_id != -1:
|
| 361 |
+
logits[0, t_id] = -float('inf')
|
| 362 |
|
| 363 |
# --- Top-K Sampling ---
|
| 364 |
if top_k > 0:
|
|
|
|
| 365 |
v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
|
| 366 |
logits[logits < v[:, [-1]]] = -float('inf')
|
| 367 |
# ----------------------
|
|
|
|
| 374 |
|
| 375 |
next_id = torch.multinomial(probs, 1).item()
|
| 376 |
|
|
|
|
|
|
|
| 377 |
if next_id == eos_id:
|
| 378 |
break
|
| 379 |
|