File size: 19,442 Bytes
35f0356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""TrialScreen browser application (Gradio).

Start:  python app/app.py            then open http://127.0.0.1:7860
Env vars:
  TS_MODEL    target model id            (default Qwen/Qwen2.5-1.5B-Instruct)
  TS_ADAPTER  LoRA adapter (Hub id or folder; default: local best_adapter if present)
  TS_DRAFT    draft model id             (default Qwen/Qwen2.5-0.5B-Instruct; 'none' disables)
  TS_DTYPE    float32 | bfloat16 | float16 (default: float32 on CPU, fp16/bf16 on GPU)
  TS_SHARE    1 = also create a public gradio.live link
"""
from __future__ import annotations
import html, json, os, sys, time, tempfile, traceback
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))

import gradio as gr
import pandas as pd
import torch

from trialscreen.data import split_sentences
from trialscreen.criteria import parse_criteria
from trialscreen.modeling import load_model, predict_one, prompt_ids, eos_ids, TARGET_ID, DRAFT_ID
from trialscreen.decoding import (ExplicitQwen2Decoder, HFCacheDecoder, greedy_cached, greedy_nocache,
                                  first_mismatch)
from trialscreen.speculative import speculative_greedy
from trialscreen.attention import reference_attention, tiled_attention, torch_sdpa
from trialscreen.explain import aggregate_trial
from trialscreen.report_export import html_report, provenance, DISCLAIMER

MAX_CRITERIA, MAX_NOTE_TOKENS, MAX_CRIT_CHARS = 40, 2500, 1500
EXAMPLES = json.load(open(ROOT / "app" / "examples.json"))
MODES = {"KV cache: explicit decoder (ours)": "explicit_cache",
         "KV cache: HF forward + DynamicCache": "hf_cache",
         "No cache: full recompute (reference)": "explicit_nocache",
         "Speculative: 0.5B draft + 1.5B target (ours)": "speculative"}

# ------------------------------------------------------------------ model loading
STATE = {"status": "loading"}


def _load():
    t0 = time.time()
    model_id = os.environ.get("TS_MODEL", TARGET_ID)
    local = ROOT / "checkpoints" / "qwen2.5-1.5b-trialscreen-lora" / "best_adapter"
    adapter = os.environ.get("TS_ADAPTER", str(local) if local.exists() else "")
    adapter = adapter if adapter and adapter.lower() != "none" else None
    dt = {"float32": torch.float32, "bfloat16": torch.bfloat16, "float16": torch.float16}.get(os.environ.get("TS_DTYPE", ""))
    tok, model = load_model(model_id, adapter=adapter, dtype=dt)
    draft_id = os.environ.get("TS_DRAFT", DRAFT_ID)
    draft = None
    if draft_id.lower() != "none":
        try:
            dtok, draft = load_model(draft_id, device=str(model.device), dtype=next(model.parameters()).dtype)
            if dtok.get_vocab() != tok.get_vocab():
                draft, STATE["draft_error"] = None, "draft tokenizer differs from target; speculative disabled"
        except Exception as e:
            STATE["draft_error"] = f"draft not loaded: {e}"
    STATE.update(tok=tok, model=model, draft=draft, model_id=model_id, adapter=adapter, draft_id=draft_id if draft else None,
                 device=str(model.device), dtype=str(next(model.parameters()).dtype), status="ready",
                 load_s=time.time() - t0)


def status_md():
    if STATE.get("status") != "ready":
        return "Model status: **loading**…"
    s = (f"Model: `{STATE['model_id']}` | adapter: `{STATE['adapter'] or 'none (base model)'}` | draft: "
         f"`{STATE['draft_id'] or 'disabled'}` | device: `{STATE['device']}` ({STATE['dtype']}) | loaded in {STATE['load_s']:.0f}s")
    if STATE.get("draft_error"):
        s += f"  \n⚠ {STATE['draft_error']}"
    if not STATE["adapter"]:
        s += "  \n⚠ No fine-tuned adapter found: running the BASE model. Set TS_ADAPTER to use the fine-tuned one."
    return s


# ------------------------------------------------------------------ screening
def _validate(note, criteria_text):
    if not note or len(note.strip()) < 20:
        raise gr.Error("Please paste a patient record (at least one full sentence).")
    if not criteria_text or not criteria_text.strip():
        raise gr.Error("Please enter the trial's eligibility criteria.")
    crits, problems = parse_criteria(criteria_text)
    if problems:
        raise gr.Error("Criteria format problem: " + "; ".join(problems[:3]) +
                       ". Use 'Inclusion criteria:' and 'Exclusion criteria:' headings with one criterion per line.")
    if not crits:
        raise gr.Error("No criteria found. Put each criterion on its own line under an 'Inclusion criteria:' or 'Exclusion criteria:' heading.")
    if len(crits) > MAX_CRITERIA:
        raise gr.Error(f"{len(crits)} criteria given; the limit is {MAX_CRITERIA} per run.")
    long = [c for _, c in crits if len(c) > MAX_CRIT_CHARS]
    if long:
        raise gr.Error(f"A criterion is longer than {MAX_CRIT_CHARS} characters; split it into separate lines.")
    sents = split_sentences(note)
    n_tok = len(STATE["tok"](note).input_ids)
    if n_tok > MAX_NOTE_TOKENS:
        raise gr.Error(f"The record has {n_tok} tokens; the limit is {MAX_NOTE_TOKENS}. Shorten it (no silent truncation).")
    return crits, sents


def _highlight(sents, cited):
    out = []
    for i, s in sents:
        if i in cited:
            out.append(f"<li value='{i}' style='background:#fff1b8;border-radius:4px'><b>[{i}]</b> {html.escape(s)}</li>")
        else:
            out.append(f"<li value='{i}'><b>[{i}]</b> {html.escape(s)}</li>")
    return "<ol start='0' style='padding-left:1.4em'>" + "".join(out) + "</ol>"


def screen(note, criteria_text, trial_name, mode_label, attn_backend, k, progress=gr.Progress()):
    if STATE.get("status") != "ready":
        raise gr.Error("The model is still loading. Wait for the status line to say it is ready.")
    crits, sents = _validate(note, criteria_text)
    mode = MODES[mode_label]
    if mode == "speculative" and STATE["draft"] is None:
        raise gr.Error("Speculative mode needs the draft model, which is not loaded. Choose another mode.")
    tok, model, draft = STATE["tok"], STATE["model"], STATE["draft"]
    tgt_dec = ExplicitQwen2Decoder(model, attn_backend) if mode != "hf_cache" else HFCacheDecoder(model)
    dr_dec = ExplicitQwen2Decoder(draft, attn_backend) if mode == "speculative" else None
    results, t0 = [], time.time()
    for j, (ctype, ctext) in enumerate(progress.tqdm(crits, desc="Screening criteria")):
        try:
            r = predict_one(tok, model, note, ctype, ctext, mode=mode, attn_backend=attn_backend,
                            draft_model=draft, k=int(k), decoder=tgt_dec, draft_decoder=dr_dec)
        except Exception as e:      # one bad criterion must not kill the whole run
            r = {"criterion_type": ctype, "criterion_text": ctext, "label": "invalid output", "evidence": [],
                 "evidence_text": [], "explanation": f"Error while running the model: {type(e).__name__}: {e}",
                 "raw_output": "", "valid_json": False, "parse_notes": [traceback.format_exc(limit=1)], "decode": {}}
        results.append(r)
    total = time.time() - t0
    verdict = aggregate_trial(results)
    df = pd.DataFrame([{"#": i + 1, "type": r["criterion_type"], "criterion": r["criterion_text"], "prediction": r["label"],
                        "evidence IDs": ", ".join(map(str, r["evidence"])) or "-",
                        "evidence text": " | ".join(r["evidence_text"]) or "-"} for i, r in enumerate(results)])
    cited = set(i for r in results for i in r["evidence"])
    icon = {"likely ineligible": "🔴", "potentially eligible": "🟢"}.get(verdict["verdict"], "🟡")
    missing = [r["criterion_text"] for r in results if r["label"] == "not enough information"]
    unsupported = [r["criterion_text"] for r in results if r["label"] not in ("not enough information", "invalid output") and not r["evidence"]]
    md = (f"### {icon} Preliminary outcome: **{verdict['verdict']}**\n"
          f"{verdict['n_criteria']} criteria · {verdict['n_blocking']} blocking · {verdict['n_unknown']} undetermined · "
          f"{total:.1f}s with *{mode_label}*  \n"
          f"<small>Rule-based aggregation of criterion predictions, not a ground-truth label. Rule: {verdict['rule']}</small>\n\n"
          "**Missing information** (record does not say enough):\n" + ("\n".join(f"- {m}" for m in missing) or "- none") +
          ("\n\n**Decisions without cited evidence** (check these):\n" + "\n".join(f"- {u}" for u in unsupported) if unsupported else ""))
    details = "".join(
        f"<details><summary><b>{i + 1}. [{r['criterion_type']}] {html.escape(r['label'])}</b> — {html.escape(r['criterion_text'][:120])}</summary>"
        f"<p>{html.escape(r['explanation'])}</p><p><small>raw model output: <code>{html.escape(r['raw_output'][:300])}</code>"
        f"{' · notes: ' + html.escape('; '.join(r['parse_notes'])) if r['parse_notes'] else ''}</small></p></details>"
        for i, r in enumerate(results))
    prov = provenance(note, crits, {"model": STATE["model_id"], "adapter": STATE["adapter"], "draft": STATE["draft_id"],
                                     "decoding": f"greedy, {mode}", "attention_backend": attn_backend,
                                     "lookahead_k": int(k) if mode == "speculative" else None, "device": STATE["device"],
                                     "dtype": STATE["dtype"], "runtime_s": round(total, 2)})
    tmp = Path(tempfile.mkdtemp())
    hp = tmp / "screening_report.html"; jp = tmp / "screening_report.json"
    hp.write_text(html_report(trial_name, sents, results, verdict, prov), encoding="utf-8")
    jp.write_text(json.dumps({"trial": trial_name, "verdict": verdict, "results": results, "patient_sentences": sents,
                              "provenance": prov, "notice": DISCLAIMER}, indent=1, default=str), encoding="utf-8")
    return md, df, _highlight(sents, cited), details, [str(hp), str(jp)]


def load_example(name):
    ex = next(e for e in EXAMPLES if e["name"] == name)
    return ex["note"], ex["criteria"], ex["trial_id"] or "Example trial"


# ------------------------------------------------------------------ live benchmark
def bench(note, criteria_text, max_new, k, which, progress=gr.Progress()):
    if STATE.get("status") != "ready":
        raise gr.Error("Model still loading.")
    tok, model, draft = STATE["tok"], STATE["model"], STATE["draft"]
    max_new = int(max_new)
    if which == "Free-text summary of the note (EOS ignored)":
        if not note or len(note.strip()) < 20:
            raise gr.Error("Paste a patient note first (Screening tab or below).")
        msgs = [{"role": "user", "content": "Summarise this patient's history in detail.\n\n" + note}]
        ids = tok(tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True), add_special_tokens=False).input_ids
        eos = ()
    else:
        crits, sents = _validate(note, criteria_text)
        ctype, ctext = crits[0]
        numbered = "\n".join(f"{i}. {s}" for i, s in sents)
        ids = prompt_ids(tok, numbered, ctype, ctext)
        eos = eos_ids(tok)
    ids = torch.tensor([ids], device=model.device)
    rows, ref = [], None
    runs = [("No cache (HF, reference)", lambda: greedy_nocache(HFCacheDecoder(model), ids, max_new, eos)),
            ("No cache (explicit)", lambda: greedy_nocache(ExplicitQwen2Decoder(model, "torch_sdpa"), ids, max_new, eos)),
            ("KV cache (explicit, ours)", lambda: greedy_cached(ExplicitQwen2Decoder(model, "torch_sdpa"), ids, max_new, eos)),
            ("KV cache (explicit + tiled attention)", lambda: greedy_cached(ExplicitQwen2Decoder(model, "tiled"), ids, max_new, eos)),
            ("KV cache (HF DynamicCache)", lambda: greedy_cached(HFCacheDecoder(model), ids, max_new, eos))]
    if draft is not None:
        runs.append((f"Speculative k={int(k)} (ours)", lambda: speculative_greedy(
            ExplicitQwen2Decoder(model, "torch_sdpa"), ExplicitQwen2Decoder(draft, "torch_sdpa"), ids, max_new, k=int(k), eos_ids=eos)))
    for name, fn in progress.tqdm(runs, desc="Running decoders"):
        st = fn()
        toks = st.tokens
        ref = toks if ref is None else ref
        d = st.as_dict()
        rows.append({"method": name, "new tokens": len(toks),
                     "prefill ms": round(d.get("prefill_s", 0) * 1000, 1),
                     "total ms": round(d["total_s"] * 1000, 1),
                     "tokens/s": round(len(toks) / d["total_s"], 1) if d["total_s"] else None,
                     "peak GPU MB": round(d["peak_mem_mb"], 0) if d.get("peak_mem_mb") else "n/a (CPU)",
                     "same tokens as reference": "yes" if first_mismatch(ref, toks) is None else f"no (first diff at {first_mismatch(ref, toks)})",
                     "draft acceptance": round(d["acceptance_rate"], 2) if "acceptance_rate" in d else "-"})
    df = pd.DataFrame(rows)
    base = df.loc[0, "total ms"]
    df["speedup vs no-cache"] = (base / df["total ms"]).round(2)
    text = tok.decode([t for t in ref if t not in eos], skip_special_tokens=True)
    return df, df[["method", "tokens/s"]], f"**Prompt tokens:** {ids.shape[1]} · **Reference output:**\n\n```\n{text[:1500]}\n```"


def attention_check(T, D, block):
    T, D, block = int(T), int(D), int(block)
    dev = STATE.get("device", "cpu")
    torch.manual_seed(0)
    rows = []
    for causal, tq in ((True, T), (True, 1), (False, T)):
        q = torch.randn(1, 4, tq, D, device=dev, dtype=torch.float64)
        k_, v_ = torch.randn(1, 4, T, D, device=dev, dtype=torch.float64), torch.randn(1, 4, T, D, device=dev, dtype=torch.float64)
        truth = reference_attention(q, k_, v_, causal)
        for name, fn in (("reference", reference_attention), ("tiled online-softmax", lambda a, b, c, cz: tiled_attention(a, b, c, cz, block_q=block, block_k=block)),
                         ("PyTorch SDPA", torch_sdpa)):
            qq, kk, vv = q.float(), k_.float(), v_.float()
            fn(qq, kk, vv, causal)
            if dev.startswith("cuda"): torch.cuda.synchronize()
            t0 = time.perf_counter(); out = fn(qq, kk, vv, causal)
            if dev.startswith("cuda"): torch.cuda.synchronize()
            ms = (time.perf_counter() - t0) * 1000
            err = (out.double() - truth).abs()
            rows.append({"mask": ("causal" if causal else "none") + (" (decode, 1 query)" if tq == 1 else ""),
                         "method": name, "max abs error vs float64": f"{float(err.max()):.2e}",
                         "mean abs error": f"{float(err.mean()):.2e}", "time ms (fp32)": round(ms, 2)})
    return pd.DataFrame(rows)


def saved_logs():
    out = []
    for f in sorted((ROOT / "results").glob("*.json")):
        out.append(f"- `{f.name}` ({f.stat().st_size // 1024} KB)")
    return "Benchmark logs from the full GPU runs (also used in the report):\n" + ("\n".join(out) or "- none yet")


# ------------------------------------------------------------------ UI
CSS = """.notice{background:#fff4e5;border:2px solid #8a5a00;border-radius:12px;padding:10px 14px;font-weight:600}"""

with gr.Blocks(title="TrialScreen: clinical trial eligibility screening (educational)") as demo:
    gr.Markdown("# TrialScreen\nCriterion-level clinical trial eligibility screening with evidence citations.")
    gr.HTML(f"<div class='notice'>⚠ {DISCLAIMER}</div>")
    status = gr.Markdown(status_md())
    with gr.Tab("Screen a patient"):
        with gr.Row():
            with gr.Column(scale=1):
                ex = gr.Dropdown([e["name"] for e in EXAMPLES], label="Load an example (validation split or hand-written)", value=None)
                trial = gr.Textbox(label="Trial name or ID", placeholder="e.g. NCT01234567")
                note = gr.Textbox(label="Patient record", lines=10,
                                  placeholder="Paste a clinical note. Numbered sentences ('0. ...') are kept; otherwise sentences are split and numbered automatically.")
                crit = gr.Textbox(label="Eligibility criteria", lines=10,
                                  placeholder="Inclusion criteria:\n- ...\nExclusion criteria:\n- ...")
                with gr.Row():
                    mode = gr.Dropdown(list(MODES), value=list(MODES)[0], label="Decoding mode")
                    attn = gr.Dropdown(["reference", "tiled", "torch_sdpa"], value="torch_sdpa", label="Attention backend (explicit decoder)")
                    kk = gr.Slider(1, 8, value=4, step=1, label="Draft lookahead k (speculative)")
                run = gr.Button("Screen patient", variant="primary")
            with gr.Column(scale=1):
                verdict = gr.Markdown()
                table = gr.Dataframe(label="Criterion-level results", wrap=True, interactive=False)
                files = gr.File(label="Download report (HTML + JSON with provenance)", file_count="multiple")
        with gr.Row():
            with gr.Column():
                gr.Markdown("**Patient record with cited sentences highlighted**")
                note_html = gr.HTML()
            with gr.Column():
                gr.Markdown("**Explanations per criterion** (built only from the cited sentences)")
                det = gr.HTML()
        ex.change(load_example, ex, [note, crit, trial])
        run.click(screen, [note, crit, trial, mode, attn, kk], [verdict, table, note_html, det, files])
    with gr.Tab("Inference benchmark"):
        gr.Markdown("Runs every decoding path **live** on the same prompt (from the Screening tab inputs) with greedy decoding, "
                    "checks that tokens match the uncached reference, and times them. Single runs: use the repeated, "
                    "warmed-up GPU benchmark scripts for report numbers.")
        with gr.Row():
            which = gr.Radio(["Screening prompt (first criterion)", "Free-text summary of the note (EOS ignored)"],
                             value="Free-text summary of the note (EOS ignored)", label="Workload")
            mx = gr.Slider(8, 256, value=64, step=8, label="Max new tokens")
            kb = gr.Slider(1, 8, value=4, step=1, label="Speculative lookahead k")
        bbtn = gr.Button("Run comparison", variant="primary")
        btab = gr.Dataframe(label="Decoding comparison", interactive=False, wrap=True)
        bplot = gr.BarPlot(x="method", y="tokens/s", label="Throughput (tokens/s)")
        bout = gr.Markdown()
        bbtn.click(bench, [note, crit, mx, kb, which], [btab, bplot, bout])
        gr.Markdown("### Attention check: reference vs tiled online-softmax vs PyTorch SDPA")
        with gr.Row():
            aT = gr.Slider(16, 2048, value=256, step=16, label="Sequence length T")
            aD = gr.Dropdown([32, 64, 128], value=64, label="Head dim")
            aB = gr.Dropdown([16, 32, 64, 128], value=64, label="Tile size")
        abtn = gr.Button("Run attention check")
        atab = gr.Dataframe(interactive=False)
        abtn.click(attention_check, [aT, aD, aB], atab)
        logs = gr.Markdown()
    with gr.Tab("About"):
        gr.Markdown(open(ROOT / "app" / "ABOUT.md").read())
    demo.load(lambda: (status_md(), saved_logs()), None, [status, logs])

if __name__ == "__main__":
    _load()
    demo.queue(default_concurrency_limit=1).launch(server_name=os.environ.get("TS_HOST", "0.0.0.0"),
                                                   server_port=int(os.environ.get("TS_PORT", 7860)),
                                                   share=os.environ.get("TS_SHARE") == "1", css=CSS)