Update pipe.py
Browse files
pipe.py
CHANGED
|
@@ -76,58 +76,73 @@ class SmilesDiffusionPipe(Pipeline):
|
|
| 76 |
model.eval()
|
| 77 |
with torch.no_grad():
|
| 78 |
sequences = torch.tensor(input_ids, dtype=torch.long, device=self.device).unsqueeze(0)
|
| 79 |
-
|
| 80 |
total_tokens = sequences.size(1)
|
| 81 |
original_num_masked = (sequences == voc.vocab['[MASK]']).sum().item()
|
| 82 |
print(f"Original number of masked tokens: {original_num_masked}")
|
| 83 |
-
|
| 84 |
if total_tokens == 0:
|
| 85 |
raise ValueError("Encoded SMILES has no tokens.")
|
| 86 |
if original_num_masked == 0:
|
| 87 |
print("No masked tokens found. Returning original SMILES.")
|
| 88 |
return self.decode(input_ids)
|
| 89 |
-
|
| 90 |
filled_tokens_confidence = {}
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
for step in range(steps):
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
logits = model(sequences, t=t)
|
| 95 |
probs = F.softmax(logits, dim=-1)
|
| 96 |
-
|
| 97 |
mask_indices = (sequences == voc.vocab['[MASK]']).nonzero(as_tuple=True)[1]
|
|
|
|
| 98 |
if len(mask_indices) == 0:
|
| 99 |
print("All tokens filled.")
|
| 100 |
break
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
for idx in mask_indices:
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
sorted_filled_tokens = sorted(
|
| 122 |
-
((idx, conf) for idx, conf in filled_tokens_confidence.items()
|
| 123 |
key=lambda x: x[0]
|
| 124 |
)
|
| 125 |
-
|
| 126 |
for idx, confidence in sorted_filled_tokens:
|
| 127 |
print(f"Final confidence for token at index {idx}: {confidence:.3f}")
|
| 128 |
-
|
|
|
|
| 129 |
decoded = self.decode([
|
| 130 |
-
|
| 131 |
-
if
|
| 132 |
]).replace(" ", "")
|
|
|
|
| 133 |
return decoded
|
|
|
|
| 76 |
model.eval()
|
| 77 |
with torch.no_grad():
|
| 78 |
sequences = torch.tensor(input_ids, dtype=torch.long, device=self.device).unsqueeze(0)
|
| 79 |
+
|
| 80 |
total_tokens = sequences.size(1)
|
| 81 |
original_num_masked = (sequences == voc.vocab['[MASK]']).sum().item()
|
| 82 |
print(f"Original number of masked tokens: {original_num_masked}")
|
| 83 |
+
|
| 84 |
if total_tokens == 0:
|
| 85 |
raise ValueError("Encoded SMILES has no tokens.")
|
| 86 |
if original_num_masked == 0:
|
| 87 |
print("No masked tokens found. Returning original SMILES.")
|
| 88 |
return self.decode(input_ids)
|
| 89 |
+
|
| 90 |
filled_tokens_confidence = {}
|
| 91 |
+
|
| 92 |
+
# -------------------------------
|
| 93 |
+
# Iterative filling: one token per step
|
| 94 |
+
# -------------------------------
|
| 95 |
for step in range(steps):
|
| 96 |
+
|
| 97 |
+
# Time step based on % masked
|
| 98 |
+
t = torch.tensor([(sequences == voc.vocab['[MASK]']).sum().item() / total_tokens],
|
| 99 |
+
device=self.device)
|
| 100 |
+
|
| 101 |
logits = model(sequences, t=t)
|
| 102 |
probs = F.softmax(logits, dim=-1)
|
| 103 |
+
|
| 104 |
mask_indices = (sequences == voc.vocab['[MASK]']).nonzero(as_tuple=True)[1]
|
| 105 |
+
|
| 106 |
if len(mask_indices) == 0:
|
| 107 |
print("All tokens filled.")
|
| 108 |
break
|
| 109 |
+
|
| 110 |
+
# For each masked position, get highest probability among all tokens
|
| 111 |
+
candidate_confs = []
|
| 112 |
+
candidate_samples = []
|
| 113 |
+
|
| 114 |
for idx in mask_indices:
|
| 115 |
+
p = probs[0, idx]
|
| 116 |
+
|
| 117 |
+
# sample using multinomial
|
| 118 |
+
sampled_token = self.multinomial_sample(p)
|
| 119 |
+
confidence = p[sampled_token].item()
|
| 120 |
+
|
| 121 |
+
candidate_confs.append(confidence)
|
| 122 |
+
candidate_samples.append((idx.item(), sampled_token, confidence))
|
| 123 |
+
|
| 124 |
+
# Pick the token with highest confidence
|
| 125 |
+
chosen_idx, chosen_token, chosen_conf = max(candidate_samples, key=lambda x: x[2])
|
| 126 |
+
|
| 127 |
+
# Fill it permanently
|
| 128 |
+
sequences[0, chosen_idx] = chosen_token
|
| 129 |
+
filled_tokens_confidence[chosen_idx] = chosen_conf
|
| 130 |
+
|
| 131 |
+
print(f"[Step {step+1}] Filled index {chosen_idx} with token {chosen_token} (conf={chosen_conf:.4f})")
|
| 132 |
+
|
| 133 |
+
# Print final per-token confidence summary
|
| 134 |
sorted_filled_tokens = sorted(
|
| 135 |
+
((idx, conf) for idx, conf in filled_tokens_confidence.items()),
|
| 136 |
key=lambda x: x[0]
|
| 137 |
)
|
| 138 |
+
|
| 139 |
for idx, confidence in sorted_filled_tokens:
|
| 140 |
print(f"Final confidence for token at index {idx}: {confidence:.3f}")
|
| 141 |
+
|
| 142 |
+
# Decode without <s> or </s>
|
| 143 |
decoded = self.decode([
|
| 144 |
+
t for t in sequences[0].tolist()
|
| 145 |
+
if t not in (voc.vocab.get('<s>'), voc.vocab.get('</s>'))
|
| 146 |
]).replace(" ", "")
|
| 147 |
+
|
| 148 |
return decoded
|