Spaces:
Running on Zero

Ruurd commited on
Commit
eca0863
·
1 Parent(s): ae08b25

adjustable noise clipping

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -74,7 +74,7 @@ def noisify_answer(input_ids, answer_start, threshold=1.0, eot_weight=1.0):
74
  return noised
75
 
76
  # Add new noising function
77
- def confidence_guided_noising(input_ids, answer_start, confidences, threshold, eot_weight):
78
  noised = input_ids.copy()
79
  answer_len = len(input_ids) - answer_start
80
  num_to_noise = int(threshold * answer_len)
@@ -84,7 +84,7 @@ def confidence_guided_noising(input_ids, answer_start, confidences, threshold, e
84
 
85
  # Avoid zero-probability weights for selection
86
  raw_weights = 1.0 - np.array(confidences[answer_start:])
87
- raw_weights = np.clip(raw_weights, 0.01, None) # avoid 0s
88
  weights = raw_weights / raw_weights.sum()
89
 
90
  if num_to_noise > len(weights):
@@ -100,7 +100,7 @@ def confidence_guided_noising(input_ids, answer_start, confidences, threshold, e
100
  # Avoid zero-probability for token sampling
101
  mixed_probs = token_probabilities.copy()
102
  mixed_probs[eot_token_id] *= eot_weight
103
- mixed_probs = np.clip(mixed_probs, 1e-5, None) # fix for EOT weight near 0
104
  mixed_probs /= mixed_probs.sum()
105
 
106
  noise = rng.choice(np.arange(vocab_size), size=num_to_noise, p=mixed_probs)
@@ -129,7 +129,7 @@ def generate_diffusion_text(input_ids, answer_start):
129
 
130
 
131
  # Modify diffusion_chat to use new noising conditionally
132
- def diffusion_chat(question, eot_weight, max_it, sharpness, use_confidence_noising):
133
  placeholder = "What do you know about the city of New York?"
134
  if question.strip() == "":
135
  question = placeholder
@@ -184,7 +184,7 @@ def diffusion_chat(question, eot_weight, max_it, sharpness, use_confidence_noisi
184
 
185
  threshold = get_noising_schedule(i, max_it, sharpness=sharpness)
186
  if use_confidence_noising:
187
- current_tokens = confidence_guided_noising(generated_tokens, answer_start, confidences, threshold, eot_weight)
188
  else:
189
  current_tokens = noisify_answer(generated_tokens, answer_start, threshold=threshold, eot_weight=eot_weight)
190
 
@@ -209,6 +209,7 @@ demo = gr.Interface(
209
  gr.Slider(0, 1, value=0.4, step=0.05, label="↓ = longer answers (EOT weight)"),
210
  gr.Slider(1, 512, value=64, step=1, label="↑ = more iterations"),
211
  gr.Slider(1.0, 20.0, value=5.0, step=0.5, label="↓ = more noising (sharpness)"),
 
212
  gr.Checkbox(value=False, label="Use confidence-guided noising") # ✅ NEW
213
  ],
214
  outputs=[gr.HTML(label="Diffusion Output")],
 
74
  return noised
75
 
76
  # Add new noising function
77
+ def confidence_guided_noising(input_ids, answer_start, confidences, threshold, eot_weight, noise_clipping):
78
  noised = input_ids.copy()
79
  answer_len = len(input_ids) - answer_start
80
  num_to_noise = int(threshold * answer_len)
 
84
 
85
  # Avoid zero-probability weights for selection
86
  raw_weights = 1.0 - np.array(confidences[answer_start:])
87
+ raw_weights = np.clip(raw_weights, noise_clipping, None) # avoid 0s
88
  weights = raw_weights / raw_weights.sum()
89
 
90
  if num_to_noise > len(weights):
 
100
  # Avoid zero-probability for token sampling
101
  mixed_probs = token_probabilities.copy()
102
  mixed_probs[eot_token_id] *= eot_weight
103
+ # mixed_probs = np.clip(mixed_probs, 1e-6, None) # fix for EOT weight near 0
104
  mixed_probs /= mixed_probs.sum()
105
 
106
  noise = rng.choice(np.arange(vocab_size), size=num_to_noise, p=mixed_probs)
 
129
 
130
 
131
  # Modify diffusion_chat to use new noising conditionally
132
+ def diffusion_chat(question, eot_weight, max_it, sharpness, noise_clipping, use_confidence_noising):
133
  placeholder = "What do you know about the city of New York?"
134
  if question.strip() == "":
135
  question = placeholder
 
184
 
185
  threshold = get_noising_schedule(i, max_it, sharpness=sharpness)
186
  if use_confidence_noising:
187
+ current_tokens = confidence_guided_noising(generated_tokens, answer_start, confidences, threshold, eot_weight, noise_clipping)
188
  else:
189
  current_tokens = noisify_answer(generated_tokens, answer_start, threshold=threshold, eot_weight=eot_weight)
190
 
 
209
  gr.Slider(0, 1, value=0.4, step=0.05, label="↓ = longer answers (EOT weight)"),
210
  gr.Slider(1, 512, value=64, step=1, label="↑ = more iterations"),
211
  gr.Slider(1.0, 20.0, value=5.0, step=0.5, label="↓ = more noising (sharpness)"),
212
+ gr.Slider(0.01, 1.0, value=0.05, step=0.01, label="↑ = more confidence guidance (noise clipping)"),
213
  gr.Checkbox(value=False, label="Use confidence-guided noising") # ✅ NEW
214
  ],
215
  outputs=[gr.HTML(label="Diffusion Output")],