"""Perplexity scorer: measure main pass vs span detection cost.""" import sys, os sys.path.insert(0, os.path.dirname(__file__)) os.environ['TRANSFORMERS_NO_ADVISORY_WARNINGS'] = '1' import logging logging.getLogger('transformers').setLevel(logging.ERROR) logging.getLogger('app').setLevel(logging.ERROR) import warnings; warnings.filterwarnings('ignore') import time, math import torch print("Loading PerplexityScorer...", flush=True) t_load = time.perf_counter() from app.core.perplexity_scorer import PerplexityScorer scorer = PerplexityScorer() print(f"Loaded in {(time.perf_counter()-t_load)*1000:.0f}ms\n", flush=True) sentences = [ "Furthermore, the implementation of sophisticated machine learning algorithms demonstrates unprecedented capabilities in facilitating proactive interventions.", "The integration of these technologies into clinical workflows represents a significant paradigm shift.", "The system actually works pretty well for detecting patterns that doctors might miss.", "Consequently, nations worldwide have begun implementing comprehensive mitigation strategies.", ] # Measure ONLY the mean perplexity (single forward pass) print("=== Single-pass mean perplexity only ===") for sent in sentences: t0 = time.perf_counter() ppl = scorer._compute_mean_perplexity(sent) ms = (time.perf_counter()-t0)*1000 cls = scorer._classify(ppl) print(f" ppl={ppl:6.1f} {cls:<14} {ms:5.0f}ms {sent[:55]}") print() # Measure score_sentence WITH span detection (current default) print("=== score_sentence WITH span detection (current) ===") for sent in sentences: t0 = time.perf_counter() r = scorer.score_sentence(sent) ms = (time.perf_counter()-t0)*1000 print(f" ppl={r.mean_perplexity:6.1f} {r.classification:<14} {ms:5.0f}ms spans={len(r.low_perplexity_spans)}") print() print("Span detection is the bottleneck. We'll disable it by default.")