Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -113,29 +113,48 @@ def encode_and_trace(text, selected_roles):
|
|
113 |
# container for summary text
|
114 |
report_lines = []
|
115 |
|
116 |
-
#
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
#
|
|
|
|
|
|
|
|
|
|
|
133 |
with torch.no_grad():
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
pool_lo, acc_lo = greedy_pool(lo_idx, "low")
|
141 |
pool_hi, acc_hi = greedy_pool(hi_idx, "high")
|
|
|
113 |
# container for summary text
|
114 |
report_lines = []
|
115 |
|
116 |
+
# ------------------------------------------------------------------
|
117 |
+
# Greedy pool helper (drop-in replacement)
|
118 |
+
# ------------------------------------------------------------------
|
119 |
+
def greedy_pool(index_list, which: str):
|
120 |
+
"""
|
121 |
+
index_list – indices (list[int]) to start pooling from
|
122 |
+
which – "low" → walk upward
|
123 |
+
"high" → walk downward
|
124 |
+
Returns (best_pool, best_acc)
|
125 |
+
"""
|
126 |
+
step = +1 if which == "low" else -1
|
127 |
+
best_pool, best_acc = [], 0.0
|
128 |
+
|
129 |
+
for i in range(0, len(index_list), 2): # 2 at a time
|
130 |
+
# current candidate indices to keep un-masked
|
131 |
+
cand = index_list[i : i + 2]
|
132 |
+
pool = best_pool + cand # grow pool
|
133 |
+
mask_flags = torch.ones_like(ids).bool() # mask *everything*
|
134 |
+
mask_flags[0, pool] = False # ...except pool
|
135 |
+
masked_ids = ids.masked_fill(~mask_flags, tokenizer.mask_token_id)
|
136 |
+
|
137 |
+
# ---------- second forward-pass on MASKED input ----------
|
138 |
with torch.no_grad():
|
139 |
+
x_m = emb_drop(emb_ln(embeddings(masked_ids)))
|
140 |
+
ext_m = full_model.bert.get_extended_attention_mask(mask, x_m.shape[:-1])
|
141 |
+
enc_m = encoder(x_m, attention_mask=ext_m) # (1,S,H)
|
142 |
+
logits = mlm_head(enc_m).squeeze(0) # (S,V)
|
143 |
+
# ---------------------------------------------------------
|
144 |
+
|
145 |
+
# accuracy of predicting original tokens only at *masked* positions
|
146 |
+
pred = logits.argmax(-1)
|
147 |
+
corr = (pred[mask_flags] == ids[mask_flags]).float().mean().item()
|
148 |
+
|
149 |
+
if corr > best_acc: # greedy improve
|
150 |
+
best_acc = corr
|
151 |
+
best_pool = pool
|
152 |
+
|
153 |
+
# stop early if we already exceed 0.50
|
154 |
+
if best_acc >= 0.50:
|
155 |
+
break
|
156 |
+
|
157 |
+
return best_pool, best_acc
|
158 |
|
159 |
pool_lo, acc_lo = greedy_pool(lo_idx, "low")
|
160 |
pool_hi, acc_hi = greedy_pool(hi_idx, "high")
|