Upload eval.py with huggingface_hub
Browse files
eval.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import argparse
|
| 2 |
-
import time
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
import torch
|
|
@@ -28,19 +27,31 @@ def muat(path):
|
|
| 28 |
|
| 29 |
|
| 30 |
@torch.no_grad()
|
| 31 |
-
def nats_per_token(model, ids, block_size, device):
|
| 32 |
-
|
| 33 |
-
total_tok = 0
|
| 34 |
for i in range(0, max(0, len(ids) - 1), block_size):
|
| 35 |
potongan = ids[i : i + block_size + 1]
|
| 36 |
-
if len(potongan)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
logits, _ = model(x)
|
| 41 |
-
logp = torch.log_softmax(logits
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
return total_nll / max(1, total_tok), total_tok
|
| 45 |
|
| 46 |
|
|
@@ -53,6 +64,7 @@ def main():
|
|
| 53 |
ap.add_argument("--device", default="cpu", choices=["cpu", "cuda"])
|
| 54 |
ap.add_argument("--guard", default=None, help="kamus opsional untuk metrik rasio ejaan")
|
| 55 |
ap.add_argument("--seed", type=int, default=42)
|
|
|
|
| 56 |
args = ap.parse_args()
|
| 57 |
|
| 58 |
teks = read_clean(args.test)
|
|
@@ -71,22 +83,23 @@ def main():
|
|
| 71 |
for path in args.ckpt:
|
| 72 |
model, tokenizer, meta = muat(path)
|
| 73 |
ids = tokenizer.encode(teks)
|
| 74 |
-
npt, n_tok = nats_per_token(
|
|
|
|
|
|
|
| 75 |
kompresi = n_karakter / max(1, n_tok)
|
| 76 |
npc = npt / kompresi
|
| 77 |
rasio = ""
|
| 78 |
if wordset:
|
| 79 |
-
t0 = time.time()
|
| 80 |
out = model.generate(
|
| 81 |
torch.tensor([[0]], dtype=torch.long, device=args.device),
|
| 82 |
120,
|
| 83 |
temperature=0.8,
|
| 84 |
top_k=40,
|
| 85 |
)
|
| 86 |
-
del t0
|
| 87 |
teks_out = tokenizer.decode(out[0].tolist())
|
| 88 |
rasio = f"{word_known_ratio(teks_out, wordset, prefiks, sufiks):6.0%}"
|
| 89 |
-
|
|
|
|
| 90 |
print(f"{nama:44s} {npt:9.3f} {npc:8.3f} {rasio:>7s}")
|
| 91 |
baris.append({"ckpt": str(path), "nats_per_token": round(npt, 4), "nats_per_char": round(npc, 4)})
|
| 92 |
|
|
|
|
| 1 |
import argparse
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
import torch
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
@torch.no_grad()
|
| 30 |
+
def nats_per_token(model, ids, block_size, device, batch_size=32):
|
| 31 |
+
jendela = []
|
|
|
|
| 32 |
for i in range(0, max(0, len(ids) - 1), block_size):
|
| 33 |
potongan = ids[i : i + block_size + 1]
|
| 34 |
+
if len(potongan) >= 2:
|
| 35 |
+
jendela.append(potongan)
|
| 36 |
+
total_nll = 0.0
|
| 37 |
+
total_tok = 0
|
| 38 |
+
for k in range(0, len(jendela), batch_size):
|
| 39 |
+
kelompok = jendela[k : k + batch_size]
|
| 40 |
+
L = max(len(w) - 1 for w in kelompok)
|
| 41 |
+
x = torch.zeros(len(kelompok), L, dtype=torch.long)
|
| 42 |
+
y = torch.zeros(len(kelompok), L, dtype=torch.long)
|
| 43 |
+
mask = torch.zeros(len(kelompok), L, dtype=torch.bool)
|
| 44 |
+
for r, w in enumerate(kelompok):
|
| 45 |
+
n = len(w) - 1
|
| 46 |
+
x[r, :n] = torch.tensor(w[:-1], dtype=torch.long)
|
| 47 |
+
y[r, :n] = torch.tensor(w[1:], dtype=torch.long)
|
| 48 |
+
mask[r, :n] = True
|
| 49 |
+
x, y, mask = x.to(device), y.to(device), mask.to(device)
|
| 50 |
logits, _ = model(x)
|
| 51 |
+
logp = torch.log_softmax(logits.float(), dim=-1)
|
| 52 |
+
nll = -logp.gather(2, y.unsqueeze(2)).squeeze(2)
|
| 53 |
+
total_nll += float(nll[mask].sum())
|
| 54 |
+
total_tok += int(mask.sum())
|
| 55 |
return total_nll / max(1, total_tok), total_tok
|
| 56 |
|
| 57 |
|
|
|
|
| 64 |
ap.add_argument("--device", default="cpu", choices=["cpu", "cuda"])
|
| 65 |
ap.add_argument("--guard", default=None, help="kamus opsional untuk metrik rasio ejaan")
|
| 66 |
ap.add_argument("--seed", type=int, default=42)
|
| 67 |
+
ap.add_argument("--batch-size", type=int, default=32)
|
| 68 |
args = ap.parse_args()
|
| 69 |
|
| 70 |
teks = read_clean(args.test)
|
|
|
|
| 83 |
for path in args.ckpt:
|
| 84 |
model, tokenizer, meta = muat(path)
|
| 85 |
ids = tokenizer.encode(teks)
|
| 86 |
+
npt, n_tok = nats_per_token(
|
| 87 |
+
model, ids, meta["config"]["block_size"], args.device, args.batch_size
|
| 88 |
+
)
|
| 89 |
kompresi = n_karakter / max(1, n_tok)
|
| 90 |
npc = npt / kompresi
|
| 91 |
rasio = ""
|
| 92 |
if wordset:
|
|
|
|
| 93 |
out = model.generate(
|
| 94 |
torch.tensor([[0]], dtype=torch.long, device=args.device),
|
| 95 |
120,
|
| 96 |
temperature=0.8,
|
| 97 |
top_k=40,
|
| 98 |
)
|
|
|
|
| 99 |
teks_out = tokenizer.decode(out[0].tolist())
|
| 100 |
rasio = f"{word_known_ratio(teks_out, wordset, prefiks, sufiks):6.0%}"
|
| 101 |
+
bagian = str(Path(path)).replace("\\", "/").split("/")
|
| 102 |
+
nama = "/".join(bagian[-3:-1] + [bagian[-1]]) if len(bagian) >= 3 else bagian[-1]
|
| 103 |
print(f"{nama:44s} {npt:9.3f} {npc:8.3f} {rasio:>7s}")
|
| 104 |
baris.append({"ckpt": str(path), "nats_per_token": round(npt, 4), "nats_per_char": round(npc, 4)})
|
| 105 |
|