ritikraj2425 commited on
Commit
4157798
·
1 Parent(s): 836e65d

Optimization fix for 500 error

Browse files
Files changed (2) hide show
  1. .gitignore +2 -0
  2. 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 = 40
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
- # Enhanced Subword Spatial Repetition Penalty
115
  if step > 1:
116
  prev_pred = current_seq[mask_indices]
117
- counts = torch.bincount(prev_pred, minlength=model.fc_out.out_features)
118
- rep_mask = counts > 1
119
- for tok_id in range(len(counts)):
120
- if rep_mask[tok_id] and tok_id not in special_ids:
121
- # Devastating penalty for tokens that loop more than once globally
122
- penalty = 5.0 ** (counts[tok_id].item() - 1)
123
- response_logits[:, tok_id] = torch.where(
124
- response_logits[:, tok_id] < 0,
125
- response_logits[:, tok_id] * penalty,
126
- response_logits[:, tok_id] / penalty
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: