-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprobe_morph_real.py
More file actions
68 lines (55 loc) · 2.25 KB
/
Copy pathprobe_morph_real.py
File metadata and controls
68 lines (55 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Probe 2: dictabert-morph predict() on REAL knesset lines, batch 32 — find why kept=0."""
from __future__ import annotations
import modal
datasets_volume = modal.Volume.from_name("rababa-datasets", create_if_missing=True)
image = (
modal.Image.debian_slim(python_version="3.11")
.pip_install("torch==2.5.1", "transformers==4.38.0", "huggingface_hub>=0.24")
)
app = modal.App("probe-morph-real")
@app.function(
image=image,
gpu="A10G",
timeout=10 * 60,
volumes={"/datasets": datasets_volume},
secrets=[modal.Secret.from_name("huggingface")],
)
def probe() -> None:
import json
from transformers import AutoModel, AutoTokenizer
datasets_volume.reload()
srcs = []
with open("/datasets/hebrew-phonikud/pairs/train.jsonl", encoding="utf-8") as f:
for line in f:
if len(srcs) >= 64:
break
row = json.loads(line)
src = row["src"].strip()
if src and 10 <= len(src) <= 300:
srcs.append(src)
m = "dicta-il/dictabert-morph"
tok = AutoTokenizer.from_pretrained(m)
model = AutoModel.from_pretrained(m, trust_remote_code=True)
model.eval()
for bi, bs in ((0, 2), (0, 32)):
batch = srcs[bi : bi + bs]
results = model.predict(batch, tok)
print(f"### batch_size={len(batch)} -> {len(results)} results", flush=True)
ok = 0
for i, res in enumerate(results):
toks = (res.get("tokens") or []) if isinstance(res, dict) else []
n_text = len((res.get("text") or "").split()) if isinstance(res, dict) else -1
if toks and len(toks) == n_text:
ok += 1
if i < 3:
print(f"[{i}] type={type(res).__name__} keys={list(res.keys()) if isinstance(res, dict) else '?'}", flush=True)
print(f" n_toks={len(toks)} n_text_split={n_text}", flush=True)
print(f" text={repr(res.get('text'))[:120]}", flush=True)
if toks:
print(f" tok0={repr(toks[0])[:220]}", flush=True)
else:
print(f" RES_REPR={repr(res)[:400]}", flush=True)
print(f"### ok={ok}/{len(results)}", flush=True)
@app.local_entrypoint()
def main():
probe.remote()