Spaces:
Sleeping
Sleeping
Commit ·
f722db4
1
Parent(s): ce736c4
Add BOS token handling in entropy computation and update notes
Browse files
app.py
CHANGED
|
@@ -46,6 +46,16 @@ def compute_entropy(code: str):
|
|
| 46 |
if attention_mask is not None:
|
| 47 |
attention_mask = attention_mask.to(device)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# Need at least 2 tokens to compute next-token NLL
|
| 50 |
if input_ids.shape[1] < 2:
|
| 51 |
return "Input is too short to compute token-level entropy.", None
|
|
@@ -117,7 +127,7 @@ The table shows each token's NLL and probability under the model.
|
|
| 117 |
gr.Markdown(
|
| 118 |
"""
|
| 119 |
Notes:
|
| 120 |
-
- NLL is computed for next-token prediction
|
| 121 |
- Large inputs may take time to process depending on hardware.
|
| 122 |
"""
|
| 123 |
)
|
|
|
|
| 46 |
if attention_mask is not None:
|
| 47 |
attention_mask = attention_mask.to(device)
|
| 48 |
|
| 49 |
+
# Prepend BOS if not already present so the first real token gets a predicted probability
|
| 50 |
+
bos_id = TOKENIZER.bos_token_id
|
| 51 |
+
if bos_id is not None and input_ids[0, 0].item() != bos_id:
|
| 52 |
+
bos_tensor = torch.full((1, 1), bos_id, dtype=input_ids.dtype, device=device)
|
| 53 |
+
input_ids = torch.cat([bos_tensor, input_ids], dim=1)
|
| 54 |
+
if attention_mask is not None:
|
| 55 |
+
attention_mask = torch.cat(
|
| 56 |
+
[torch.ones(1, 1, dtype=attention_mask.dtype, device=device), attention_mask], dim=1
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
# Need at least 2 tokens to compute next-token NLL
|
| 60 |
if input_ids.shape[1] < 2:
|
| 61 |
return "Input is too short to compute token-level entropy.", None
|
|
|
|
| 127 |
gr.Markdown(
|
| 128 |
"""
|
| 129 |
Notes:
|
| 130 |
+
- NLL is computed for next-token prediction; a BOS token is prepended if needed so all tokens are included.
|
| 131 |
- Large inputs may take time to process depending on hardware.
|
| 132 |
"""
|
| 133 |
)
|