Spaces:
Running
on
Zero
Running
on
Zero
Use num_to_noise with confidence guided noising
Browse files
app.py
CHANGED
|
@@ -74,20 +74,36 @@ 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, eot_weight):
|
| 78 |
noised = input_ids.copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
mixed_probs = token_probabilities.copy()
|
| 80 |
mixed_probs[eot_token_id] *= eot_weight
|
| 81 |
mixed_probs /= mixed_probs.sum()
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
idx = answer_start + i
|
| 87 |
-
noised[idx] = rng.choice(np.arange(vocab_size), p=mixed_probs)
|
| 88 |
|
| 89 |
return noised
|
| 90 |
|
|
|
|
| 91 |
@spaces.GPU
|
| 92 |
def generate_diffusion_text(input_ids, answer_start):
|
| 93 |
with torch.no_grad():
|
|
@@ -160,7 +176,7 @@ def diffusion_chat(question, eot_weight, max_it, sharpness, use_confidence_noisi
|
|
| 160 |
|
| 161 |
threshold = get_noising_schedule(i, max_it, sharpness=sharpness)
|
| 162 |
if use_confidence_noising:
|
| 163 |
-
current_tokens = confidence_guided_noising(generated_tokens, answer_start, confidences, eot_weight)
|
| 164 |
else:
|
| 165 |
current_tokens = noisify_answer(generated_tokens, answer_start, threshold=threshold, eot_weight=eot_weight)
|
| 166 |
|
|
|
|
| 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)
|
| 81 |
+
|
| 82 |
+
if num_to_noise == 0:
|
| 83 |
+
return noised
|
| 84 |
+
|
| 85 |
+
# Use 1 - confidence as sampling weights
|
| 86 |
+
weights = 1.0 - np.array(confidences[answer_start:])
|
| 87 |
+
weights /= weights.sum()
|
| 88 |
+
|
| 89 |
+
indices = rng.choice(
|
| 90 |
+
np.arange(answer_start, len(input_ids)),
|
| 91 |
+
size=num_to_noise,
|
| 92 |
+
replace=False,
|
| 93 |
+
p=weights
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
mixed_probs = token_probabilities.copy()
|
| 97 |
mixed_probs[eot_token_id] *= eot_weight
|
| 98 |
mixed_probs /= mixed_probs.sum()
|
| 99 |
|
| 100 |
+
noise = rng.choice(np.arange(vocab_size), size=num_to_noise, p=mixed_probs)
|
| 101 |
+
for idx, val in zip(indices, noise):
|
| 102 |
+
noised[idx] = val
|
|
|
|
|
|
|
| 103 |
|
| 104 |
return noised
|
| 105 |
|
| 106 |
+
|
| 107 |
@spaces.GPU
|
| 108 |
def generate_diffusion_text(input_ids, answer_start):
|
| 109 |
with torch.no_grad():
|
|
|
|
| 176 |
|
| 177 |
threshold = get_noising_schedule(i, max_it, sharpness=sharpness)
|
| 178 |
if use_confidence_noising:
|
| 179 |
+
current_tokens = confidence_guided_noising(generated_tokens, answer_start, confidences, threshold, eot_weight)
|
| 180 |
else:
|
| 181 |
current_tokens = noisify_answer(generated_tokens, answer_start, threshold=threshold, eot_weight=eot_weight)
|
| 182 |
|