Commit ·
4157798
1
Parent(s): 836e65d
Optimization fix for 500 error
Browse files- .gitignore +2 -0
- app.py +16 -13
.gitignore
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
venv
|
| 2 |
diffusion_model_best2.pth
|
| 3 |
diffusion_model.pth
|
|
|
|
|
|
|
|
|
| 1 |
venv
|
| 2 |
diffusion_model_best2.pth
|
| 3 |
diffusion_model.pth
|
| 4 |
+
__pycache__/
|
| 5 |
+
*.pyc
|
app.py
CHANGED
|
@@ -63,8 +63,7 @@ def generate_step_by_step(history):
|
|
| 63 |
|
| 64 |
prompt = history[-1][0]
|
| 65 |
|
| 66 |
-
steps =
|
| 67 |
-
# Lower temperature restricts the model from making massive unpredicted jumps
|
| 68 |
temp = 0.2
|
| 69 |
top_k = 10
|
| 70 |
|
|
@@ -101,6 +100,10 @@ def generate_step_by_step(history):
|
|
| 101 |
|
| 102 |
current_seq = seq_tensor.squeeze(0).clone()
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
for step in range(1, steps + 1):
|
| 105 |
t_val = max(1.0 - step / steps, 0.05)
|
| 106 |
t = torch.tensor([t_val], device=device)
|
|
@@ -111,19 +114,19 @@ def generate_step_by_step(history):
|
|
| 111 |
|
| 112 |
response_logits = logits[0, mask_indices]
|
| 113 |
|
| 114 |
-
#
|
| 115 |
if step > 1:
|
| 116 |
prev_pred = current_seq[mask_indices]
|
| 117 |
-
counts
|
| 118 |
-
|
| 119 |
-
for tok_id in
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
penalty = 5.0 ** (counts[
|
| 123 |
-
response_logits[:,
|
| 124 |
-
response_logits[:,
|
| 125 |
-
response_logits[:,
|
| 126 |
-
response_logits[:,
|
| 127 |
)
|
| 128 |
|
| 129 |
if top_k > 0:
|
|
|
|
| 63 |
|
| 64 |
prompt = history[-1][0]
|
| 65 |
|
| 66 |
+
steps = 20
|
|
|
|
| 67 |
temp = 0.2
|
| 68 |
top_k = 10
|
| 69 |
|
|
|
|
| 100 |
|
| 101 |
current_seq = seq_tensor.squeeze(0).clone()
|
| 102 |
|
| 103 |
+
# Pre-calculate penalty indices once
|
| 104 |
+
vocab_size = model.fc_out.out_features
|
| 105 |
+
penalty_indices = torch.tensor([i for i in range(vocab_size) if i not in special_ids], device=device)
|
| 106 |
+
|
| 107 |
for step in range(1, steps + 1):
|
| 108 |
t_val = max(1.0 - step / steps, 0.05)
|
| 109 |
t = torch.tensor([t_val], device=device)
|
|
|
|
| 114 |
|
| 115 |
response_logits = logits[0, mask_indices]
|
| 116 |
|
| 117 |
+
# Optimized Vectorized Repetition Penalty
|
| 118 |
if step > 1:
|
| 119 |
prev_pred = current_seq[mask_indices]
|
| 120 |
+
# Get counts only for tokens present in context to avoid giant loops
|
| 121 |
+
unique_tokens, counts = torch.unique(prev_pred, return_counts=True)
|
| 122 |
+
for i, tok_id in enumerate(unique_tokens):
|
| 123 |
+
t_id = tok_id.item()
|
| 124 |
+
if t_id not in special_ids and counts[i] > 1:
|
| 125 |
+
penalty = 5.0 ** (counts[i].item() - 1)
|
| 126 |
+
response_logits[:, t_id] = torch.where(
|
| 127 |
+
response_logits[:, t_id] < 0,
|
| 128 |
+
response_logits[:, t_id] * penalty,
|
| 129 |
+
response_logits[:, t_id] / penalty
|
| 130 |
)
|
| 131 |
|
| 132 |
if top_k > 0:
|