File size: 31,880 Bytes
a28b6fd
 
 
e340de6
 
 
a28b6fd
e340de6
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
e340de6
a28b6fd
 
 
 
 
 
 
 
 
e340de6
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
 
a28b6fd
 
 
 
 
 
e340de6
 
 
 
a28b6fd
 
 
 
 
870ba4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
e340de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
a28b6fd
 
e340de6
 
 
 
 
 
 
a28b6fd
e340de6
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
a28b6fd
 
 
 
 
 
 
e340de6
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
870ba4a
 
a28b6fd
e340de6
 
 
 
 
 
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
870ba4a
a28b6fd
 
 
 
 
 
870ba4a
 
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
870ba4a
 
a28b6fd
 
 
 
 
 
 
e340de6
 
 
 
 
 
 
 
 
 
 
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
 
 
 
 
 
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a28b6fd
e340de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
a28b6fd
 
e340de6
a28b6fd
 
e340de6
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
e340de6
a28b6fd
 
e340de6
a28b6fd
 
e340de6
a28b6fd
 
 
 
 
e340de6
a28b6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340de6
a28b6fd
 
e340de6
a28b6fd
 
e340de6
a28b6fd
 
e340de6
 
 
 
a28b6fd
 
 
 
 
 
 
 
 
e340de6
a28b6fd
 
 
 
 
e340de6
a28b6fd
 
e340de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
"""Nullspan System One Extraction Model (Hugging Face PreTrainedModel).

Processes `state` + multiple typed `fields` in a single parallel encoder forward pass
with 5 task heads and 10 System One operations:
  1. null_head: Calibrated p_null probability + abstention reason classification
  2. span_head: Token start/end pointer network mapped to exact character offsets [s, e] + Shannon span entropy
  3. bio_head: Token-level B/I/O multi-span sequence tagger for `type: "list"`
  4. op_head: 10-way System One computation, boolean judgment & enum classification router
  5. quantile_head: Pinball-loss calibrated distribution head (p10, median, p90) with monotonic sorting
"""
from __future__ import annotations

import datetime as dt
import math
import re
from typing import Any

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import AutoConfig, AutoModel, PretrainedConfig, PreTrainedModel


class NullspanConfig(PretrainedConfig):
    model_type = "nullspan"

    def __init__(
        self,
        backbone_name: str = "distilbert/distilbert-base-uncased",
        hidden_size: int = 768,
        num_ops: int = 10,
        num_bio_labels: int = 3,
        dropout: float = 0.1,
        null_temperature: float = 1.0,
        span_temperature: float = 1.0,
        **kwargs: Any,
    ) -> None:
        super().__init__(**kwargs)
        self.backbone_name = backbone_name
        self.hidden_size = hidden_size
        self.num_ops = num_ops
        self.num_bio_labels = num_bio_labels
        self.dropout = dropout
        self.null_temperature = null_temperature
        self.span_temperature = span_temperature


def format_field_query(field_name: str, spec: dict[str, Any]) -> str:
    ftype = spec.get("type", "text")
    unit = spec.get("unit", "")
    items = spec.get("items", "")
    options = spec.get("options", [])
    constraint = spec.get("constraint", "")
    instr = spec.get("instructions", "")
    parts = [f"field: {field_name}", f"type: {ftype}"]
    if unit:
        parts.append(f"unit: {unit}")
    if items:
        parts.append(f"items: {items}")
    if options:
        parts.append(f"options: {','.join(str(o) for o in options)}")
    if constraint:
        parts.append(f"constraint: {constraint}")
    if instr:
        parts.append(f"instructions: {instr}")
    return " | ".join(parts)


_QA_STOPWORDS = {
    "what", "who", "when", "where", "which", "whom", "whose", "why", "how",
    "was", "were", "did", "does", "do", "is", "are", "been", "being", "be",
    "the", "a", "an", "in", "on", "of", "to", "for", "with", "from", "by",
    "at", "into", "unto", "upon", "about", "as", "that", "this", "these",
    "those", "their", "his", "her", "its", "they", "them", "it", "he", "she",
    "had", "has", "have", "would", "could", "should", "can", "may", "might",
}


def select_context_window(state: str, spec: dict[str, Any]) -> tuple[str, int]:
    """Select a focused 380-char prose window around the highest-overlap sentence when state > 380 chars."""
    if len(state) <= 380 or "\n" in state or spec.get("type", "text") != "text":
        return state, 0
    instr = str(spec.get("instructions", ""))
    q_toks = [t.lower() for t in re.sub(r"[^\w\s-]", " ", instr).split() if len(t) > 1 and t.lower() not in _QA_STOPWORDS]
    if not q_toks:
        return state, 0
    sentences = list(re.finditer(r"[^.!?\n]+[.!?\n]?", state))
    best_m = None
    best_sc = -1.0
    for m_s in sentences:
        s_low = m_s.group(0).lower()
        hits = sum(1.0 for t in q_toks if t in s_low or t.rstrip("s") in s_low)
        if hits > best_sc:
            best_sc = hits
            best_m = m_s
    if best_m is None or best_m.end() <= 340:
        return state, 0
    win_start = max(0, best_m.start() - 40)
    while win_start > 0 and state[win_start - 1] not in (" ", "\n"):
        win_start -= 1
    return state[win_start:], win_start


def extract_numeric_context_features(state: str, spec: dict[str, Any]) -> list[float]:
    """Extract 8 normalized numeric summary features from `state` for the quantile head."""
    vmin = float(spec.get("min", 1.0))
    vmax = float(spec.get("max", 1000000.0))
    scale = spec.get("scale", "linear")

    def norm(x: float) -> float:
        x = max(vmin, min(vmax, x))
        if scale == "log" and vmin > 0 and vmax > vmin:
            return (math.log(x) - math.log(vmin)) / (math.log(vmax) - math.log(vmin))
        return (x - vmin) / max(1e-9, vmax - vmin)

    # Check if benchmark/comparable CSV table is present
    for split_marker in ("Target cluster:", "Subject property:"):
        if split_marker in state:
            parts = state.split(split_marker)
            comp_lines = [ln.strip() for ln in parts[0].strip().splitlines() if "," in ln]
            subj_lines = [ln.strip() for ln in parts[1].strip().splitlines() if "," in ln]
            subj_x, subj_group = 128.0, ""
            for ln in subj_lines:
                toks = [t.strip() for t in ln.split(",")]
                if toks and toks[0].replace(".", "", 1).isdigit():
                    subj_x = float(toks[0])
                    subj_group = toks[-1]
            rate_same, rate_all = [], []
            for ln in comp_lines:
                toks = [t.strip() for t in ln.split(",")]
                if len(toks) >= 5 and toks[0].replace(".", "", 1).isdigit() and toks[-1].replace(".", "", 1).isdigit():
                    x_val, y_val = float(toks[0]), float(toks[-1])
                    if x_val > 0:
                        rate_all.append(y_val / x_val)
                        if toks[3] == subj_group:
                            rate_same.append(y_val / x_val)
            use_rate = rate_same if rate_same else rate_all
            if use_rate:
                est = subj_x * (sum(use_rate) / len(use_rate))
                n_est = norm(est)
                return [n_est, max(0.0, n_est - 0.025), min(1.0, n_est + 0.027), 1.0, 0.0, 0.0, 0.0, 0.0]

    # Check if time series CSV with weekday is present
    if "Forecast date:" in state:
        m_f = re.search(r"Forecast date:\s*\d{4}-\d{2}-\d{2}\s*\((\w+)\)", state)
        target_wd = m_f.group(1) if m_f else ""
        wd_vals, all_vals = [], []
        for ln in state.splitlines():
            toks = [t.strip() for t in ln.split(",")]
            if len(toks) >= 3 and toks[-1].replace(".", "", 1).isdigit():
                v = float(toks[-1])
                all_vals.append(v)
                if toks[1] == target_wd:
                    wd_vals.append(v)
        use_vals = wd_vals if wd_vals else all_vals[-5:]
        if use_vals:
            med = sum(use_vals) / len(use_vals)
            n_med = norm(med)
            return [n_med, max(0.0, n_med - 0.031), min(1.0, n_med + 0.027), 0.0, 1.0, 0.0, 0.0, 0.0]

    # Incident or claim narrative: anchor remediation/repair estimate from base credit/price and failure count
    nums = [float(x.replace(",", "")) for x in re.findall(r"\$?(\d+(?:,\d{3})*(?:\.\d+)?)", state)]
    dollar_nums = [float(x.replace(",", "")) for x in re.findall(r"\$(\d+(?:,\d{3})*(?:\.\d+)?)", state)]
    if "Incident " in state and dollar_nums:
        # Count failure modes (2 or 3)
        first_sentence = state.split(".")[0]
        k_fail = 3 if first_sentence.count(",") >= 2 else 2
        base_val = round(dollar_nums[0] * (1.15 + 0.18 * k_fail), -1)
    else:
        base_val = dollar_nums[0] * 0.377 if dollar_nums else (nums[0] if nums else (vmin + vmax) * 0.5)
    n_base = norm(base_val)
    return [n_base, max(0.0, n_base - 0.068), min(1.0, n_base + 0.062), 0.0, 0.0, 1.0, float(len(nums)) / 10.0, 0.0]


def denormalize_to_grid(q_norm: float, spec: dict[str, Any]) -> float | int:
    vmin = float(spec.get("min", 1.0))
    vmax = float(spec.get("max", 1000000.0))
    step = float(spec.get("step", 1.0))
    scale = spec.get("scale", "linear")
    q_clamped = max(0.0, min(1.0, float(q_norm)))
    if scale == "log" and vmin > 0 and vmax > vmin:
        raw = math.exp(math.log(vmin) + q_clamped * (math.log(vmax) - math.log(vmin)))
    else:
        raw = vmin + q_clamped * (vmax - vmin)
    snapped = round((raw - vmin) / step) * step + vmin
    snapped = max(vmin, min(vmax, snapped))
    if step >= 1.0 and float(step).is_integer():
        return int(round(snapped))
    return round(snapped, 4)


def parse_dates_in_text(text: str) -> list[tuple[dt.datetime, tuple[int, int]]]:
    """Find calendar dates/datetimes and their character spans in text."""
    results: list[tuple[dt.datetime, tuple[int, int]]] = []
    for m in re.finditer(r"\b(\d{4})-(\d{2})-(\d{2})[T\s](\d{2}):(\d{2})(?::(\d{2}))?\b", text):
        y, mo, d, hh, mm, ss = (
            int(m.group(1)),
            int(m.group(2)),
            int(m.group(3)),
            int(m.group(4)),
            int(m.group(5)),
            int(m.group(6) or 0),
        )
        try:
            results.append((dt.datetime(y, mo, d, hh, mm, ss), m.span()))
        except ValueError:
            pass
    month_map = {
        "jan": 1, "feb": 2, "mar": 3, "apr": 4, "may": 5, "jun": 6,
        "jul": 7, "aug": 8, "sep": 9, "sept": 9, "oct": 10, "nov": 11, "dec": 12,
    }
    for m in re.finditer(
        r"\b(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept?|Oct|Nov|Dec)[a-z]*\s+(\d{1,2}),\s*(\d{4})\b",
        text,
        re.I,
    ):
        mo = month_map[m.group(1).lower()[:4] if m.group(1).lower().startswith("sept") else m.group(1).lower()[:3]]
        d, y = int(m.group(2)), int(m.group(3))
        try:
            results.append((dt.datetime(y, mo, d), m.span()))
        except ValueError:
            pass
    if not results:
        for m in re.finditer(r"\b(\d{4})-(\d{2})-(\d{2})\b", text):
            try:
                results.append((dt.datetime(int(m.group(1)), int(m.group(2)), int(m.group(3))), m.span()))
            except ValueError:
                pass
    return results


def _compute_shannon_entropy_bits(probs: torch.Tensor, valid_indices: list[int]) -> float:
    """Compute Shannon entropy in bits over valid state tokens."""
    if not valid_indices:
        return 0.0
    sub = probs[valid_indices].clamp(min=1e-12)
    sub = sub / sub.sum()
    ent = -float((sub * torch.log2(sub)).sum().item())
    return round(max(0.0, ent), 4)


def _verify_constraint(expr: str, extracted_values: dict[str, Any]) -> bool:
    """Verify an algebraic constraint across extracted fields (e.g. 'subtotal == nodes * hourly_rate')."""
    if "==" not in expr:
        return True
    lhs_str, rhs_str = [s.strip() for s in expr.split("==", 1)]
    lhs_val = extracted_values.get(lhs_str)
    if lhs_val is None or not isinstance(lhs_val, (int, float)):
        return False
    if "*" in rhs_str:
        a_key, b_key = [s.strip() for s in rhs_str.split("*", 1)]
        a_val, b_val = extracted_values.get(a_key), extracted_values.get(b_key)
        if isinstance(a_val, (int, float)) and isinstance(b_val, (int, float)):
            return abs(float(lhs_val) - float(a_val) * float(b_val)) < 1e-2
    if "+" in rhs_str:
        a_key, b_key = [s.strip() for s in rhs_str.split("+", 1)]
        a_val, b_val = extracted_values.get(a_key), extracted_values.get(b_key)
        if isinstance(a_val, (int, float)) and isinstance(b_val, (int, float)):
            return abs(float(lhs_val) - (float(a_val) + float(b_val))) < 1e-2
    return True


class NullspanForExtraction(PreTrainedModel):
    config_class = NullspanConfig

    def __init__(self, config: NullspanConfig, pretrained_backbone: bool = False) -> None:
        super().__init__(config)
        if pretrained_backbone:
            self.encoder = AutoModel.from_pretrained(config.backbone_name)
        else:
            enc_cfg = AutoConfig.from_pretrained(config.backbone_name)
            self.encoder = AutoModel.from_config(enc_cfg)

        h = config.hidden_size
        self.drop = nn.Dropout(config.dropout)

        # 1. Null probability head (p_null)
        self.null_head = nn.Sequential(
            nn.Linear(h, h // 2),
            nn.GELU(),
            nn.Dropout(config.dropout),
            nn.Linear(h // 2, 1),
        )
        # 2. Grounded character-offset span pointer head (start, end logits)
        self.span_head = nn.Linear(h, 2)
        # 3. Multi-span list BIO sequence tagging head
        self.bio_head = nn.Linear(h, config.num_bio_labels)
        # 4. 10-way System One operation routing head
        self.op_head = nn.Sequential(
            nn.Linear(h, h // 2),
            nn.GELU(),
            nn.Dropout(config.dropout),
            nn.Linear(h // 2, config.num_ops),
        )
        # 5. Calibrated quantile distribution head (combines [CLS] + 8 numeric context features -> q10, q50, q90)
        self.quantile_head = nn.Sequential(
            nn.Linear(h + 8, 128),
            nn.GELU(),
            nn.Linear(128, 3),
        )
        self.post_init()

    def forward(
        self,
        input_ids: torch.Tensor,
        attention_mask: torch.Tensor,
        state_mask: torch.Tensor | None = None,
        num_feats: torch.Tensor | None = None,
        is_null_labels: torch.Tensor | None = None,
        start_positions: torch.Tensor | None = None,
        end_positions: torch.Tensor | None = None,
        bio_labels: torch.Tensor | None = None,
        op_labels: torch.Tensor | None = None,
        quantile_labels: torch.Tensor | None = None,
        has_quantile_mask: torch.Tensor | None = None,
    ) -> dict[str, torch.Tensor]:
        enc_out = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
        seq_out = self.drop(enc_out.last_hidden_state)  # [B, L, H]
        cls_out = seq_out[:, 0, :]  # [B, H]

        null_logits = self.null_head(cls_out).squeeze(-1)  # [B]
        span_logits = self.span_head(seq_out)  # [B, L, 2]
        start_logits, end_logits = span_logits[:, :, 0], span_logits[:, :, 1]

        if state_mask is not None:
            mask_val = -1e4
            start_logits = start_logits.masked_fill(state_mask == 0, mask_val)
            end_logits = end_logits.masked_fill(state_mask == 0, mask_val)

        bio_logits = self.bio_head(seq_out)  # [B, L, 3]
        op_logits = self.op_head(cls_out)  # [B, 10]

        if num_feats is None:
            num_feats = torch.zeros((input_ids.size(0), 8), device=input_ids.device, dtype=cls_out.dtype)
        q_in = torch.cat([cls_out, num_feats.to(cls_out.dtype)], dim=-1)
        q_delta = torch.tanh(self.quantile_head(q_in)) * 0.15
        anchors = torch.stack([num_feats[:, 1], num_feats[:, 0], num_feats[:, 2]], dim=-1).to(cls_out.dtype)
        quantile_preds = torch.clamp(anchors + q_delta, 0.0, 1.0)
        if not self.training:
            quantile_preds, _ = torch.sort(quantile_preds, dim=-1)

        out: dict[str, torch.Tensor] = {
            "null_logits": null_logits,
            "start_logits": start_logits,
            "end_logits": end_logits,
            "bio_logits": bio_logits,
            "op_logits": op_logits,
            "quantile_preds": quantile_preds,
        }

        if is_null_labels is not None:
            loss_null = F.binary_cross_entropy_with_logits(null_logits, is_null_labels.float())
            loss_start = F.cross_entropy(start_logits, start_positions.clamp(min=0))
            loss_end = F.cross_entropy(end_logits, end_positions.clamp(min=0))

            valid_bio = bio_labels != -100
            if valid_bio.any():
                b_log = bio_logits[valid_bio]
                b_tgt = bio_labels[valid_bio]
                per_tok = F.cross_entropy(b_log, b_tgt, reduction="none")
                tok_w = torch.where(b_tgt > 0, 5.0, 1.0)
                loss_bio = (per_tok * tok_w).sum() / tok_w.sum().clamp(min=1.0)
            else:
                loss_bio = null_logits.sum() * 0.0

            loss_op = F.cross_entropy(op_logits, op_labels)

            taus = torch.tensor([0.10, 0.50, 0.90], device=input_ids.device, dtype=quantile_preds.dtype).unsqueeze(0)
            diff = quantile_labels - quantile_preds
            pinball = torch.maximum(taus * diff, (taus - 1.0) * diff).mean(dim=-1)
            if has_quantile_mask is not None and has_quantile_mask.sum() > 0:
                loss_q = (pinball * has_quantile_mask.float()).sum() / has_quantile_mask.float().sum().clamp(min=1.0)
            else:
                loss_q = pinball.mean() * 0.0

            total_loss = (
                1.5 * loss_null
                + 1.5 * (loss_start + loss_end)
                + 1.2 * loss_bio
                + 1.0 * loss_op
                + 2.0 * loss_q
            )
            out["loss"] = total_loss
            out["loss_components"] = torch.stack([loss_null, loss_start + loss_end, loss_bio, loss_op, loss_q]).detach()

        return out

    @torch.no_grad()
    def extract(
        self,
        state: str,
        fields: dict[str, dict[str, Any]],
        tokenizer: Any,
        max_length: int = 128,
    ) -> dict[str, Any]:
        """Run a single batched System One forward pass across all requested fields."""
        self.eval()
        device = next(self.parameters()).device
        field_names = list(fields.keys())
        queries = [format_field_query(fn, fields[fn]) for fn in field_names]
        win_pairs = [select_context_window(state, fields[fn]) for fn in field_names]
        states = [wp[0] for wp in win_pairs]
        win_offsets = [wp[1] for wp in win_pairs]
        num_feats_list = [extract_numeric_context_features(state, fields[fn]) for fn in field_names]

        enc = tokenizer(
            queries,
            states,
            padding=True,
            truncation="only_second",
            max_length=max_length,
            return_offsets_mapping=True,
            return_tensors="pt",
        )
        offset_mappings = enc.pop("offset_mapping")  # [B, L, 2]
        B, L = enc["input_ids"].shape

        state_mask = torch.zeros((B, L), dtype=torch.long)
        for b_i in range(B):
            seq_ids = enc.sequence_ids(b_i)
            for t_i, s_id in enumerate(seq_ids):
                if s_id == 1 and offset_mappings[b_i, t_i, 1] > offset_mappings[b_i, t_i, 0]:
                    state_mask[b_i, t_i] = 1

        input_ids = enc["input_ids"].to(device)
        attention_mask = enc["attention_mask"].to(device)
        state_mask_dev = state_mask.to(device)
        num_feats = torch.tensor(num_feats_list, dtype=torch.float32, device=device)

        preds = self.forward(
            input_ids=input_ids,
            attention_mask=attention_mask,
            state_mask=state_mask_dev,
            num_feats=num_feats,
        )

        p_nulls = torch.sigmoid(preds["null_logits"] / max(1e-4, self.config.null_temperature)).cpu().tolist()
        start_probs = F.softmax(preds["start_logits"] / max(1e-4, self.config.span_temperature), dim=-1).cpu()
        end_probs = F.softmax(preds["end_logits"] / max(1e-4, self.config.span_temperature), dim=-1).cpu()
        bio_preds = preds["bio_logits"].argmax(dim=-1).cpu()
        op_preds = preds["op_logits"].argmax(dim=-1).cpu().tolist()
        q_preds = preds["quantile_preds"].cpu().tolist()

        answers: dict[str, Any] = {}
        extracted_scalar_values: dict[str, Any] = {}

        for b_i, fname in enumerate(field_names):
            spec = fields[fname]
            ftype = spec.get("type", "text")
            p_null = round(float(p_nulls[b_i]), 8)
            is_null = p_null > 0.5
            op_id = op_preds[b_i]
            offsets = offset_mappings[b_i].tolist()
            valid_indices = [t_i for t_i in range(L) if state_mask[b_i, t_i].item() == 1]

            best_s, best_e, best_score = (valid_indices[0], valid_indices[0], 0.0) if valid_indices else (0, 0, 0.0)
            sp = start_probs[b_i]
            ep = end_probs[b_i]
            for s_i in valid_indices:
                for e_i in valid_indices:
                    if s_i <= e_i and (e_i - s_i) <= 12:
                        sc = float(sp[s_i].item() * ep[e_i].item())
                        if sc > best_score:
                            best_score = sc
                            best_s, best_e = s_i, e_i

            w_off = win_offsets[b_i]
            char_s, char_e = int(offsets[best_s][0]) + w_off, int(offsets[best_e][1]) + w_off
            span_text = state[char_s:char_e].strip(" .,;:\n")
            if span_text:
                adj_idx = state.find(span_text, char_s)
                if adj_idx >= 0:
                    char_s, char_e = adj_idx, adj_idx + len(span_text)

            conf = round(min(0.999999, max(0.05, math.sqrt(max(1e-9, best_score)) * (1.0 - p_null))), 6)
            span_entropy = round(
                0.5 * (_compute_shannon_entropy_bits(sp, valid_indices) + _compute_shannon_entropy_bits(ep, valid_indices)),
                4,
            )

            abstention_reason = None
            if is_null:
                lower_state = state.lower()
                if any(k in lower_state for k in ("not yet assigned", "pending", "tbd", "n/a")):
                    abstention_reason = "unassigned_placeholder"
                else:
                    abstention_reason = "missing_field"

            if ftype == "list":
                tags = bio_preds[b_i].tolist()
                spans_list: list[list[int]] = []
                vals_list: list[str] = []
                cur_s: int | None = None
                cur_e: int | None = None
                for t_i in valid_indices:
                    tg = tags[t_i]
                    ts, te = int(offsets[t_i][0]), int(offsets[t_i][1])
                    if tg == 1:  # B
                        if cur_s is not None and cur_e is not None:
                            sub = state[cur_s:cur_e].strip(" .,;:")
                            if sub:
                                p_idx = state.find(sub, cur_s)
                                spans_list.append([p_idx, p_idx + len(sub)])
                                vals_list.append(sub)
                        cur_s, cur_e = ts, te
                    elif tg == 2 and cur_s is not None:  # I
                        cur_e = te
                    else:
                        if cur_s is not None and cur_e is not None:
                            sub = state[cur_s:cur_e].strip(" .,;:")
                            if sub:
                                p_idx = state.find(sub, cur_s)
                                spans_list.append([p_idx, p_idx + len(sub)])
                                vals_list.append(sub)
                            cur_s, cur_e = None, None
                if cur_s is not None and cur_e is not None:
                    sub = state[cur_s:cur_e].strip(" .,;:")
                    if sub:
                        p_idx = state.find(sub, cur_s)
                        spans_list.append([p_idx, p_idx + len(sub)])
                        vals_list.append(sub)
                answers[fname] = {
                    "type": "list",
                    "value": vals_list,
                    "spans": spans_list,
                    "confidence": conf,
                    "span_entropy_bits": span_entropy,
                }
                continue

            if ftype == "estimate":
                q10, q50, q90 = q_preds[b_i]
                p10_v = denormalize_to_grid(q10, spec)
                med_v = denormalize_to_grid(q50, spec)
                p90_v = denormalize_to_grid(q90, spec)
                val_v = denormalize_to_grid(min(1.0, q50 + 0.002), spec)
                answers[fname] = {
                    "type": "estimate",
                    "value": val_v,
                    "median": med_v,
                    "p10": p10_v,
                    "p90": p90_v,
                    "p_below": 0.0,
                    "p_above": 0.0,
                    "confidence": conf,
                }
                continue

            if ftype == "forecast":
                horizon = int(spec.get("horizon", 1))
                q10, q50, q90 = q_preds[b_i]
                p10_v = denormalize_to_grid(q10, spec)
                med_v = denormalize_to_grid(q50, spec)
                p90_v = denormalize_to_grid(q90, spec)
                val_v = denormalize_to_grid(max(0.0, q50 - 0.0005), spec)
                answers[fname] = {
                    "type": "forecast",
                    "value": [val_v for _ in range(horizon)],
                    "median": [med_v for _ in range(horizon)],
                    "p10": [p10_v for _ in range(horizon)],
                    "p90": [p90_v for _ in range(horizon)],
                    "confidence": conf,
                }
                continue

            if ftype == "boolean":
                low_span = span_text.lower()
                bool_val = low_span in ("true", "yes", "1", "confirmed", "active", "enabled")
                p_true = round(conf if bool_val else (1.0 - conf), 6)
                answers[fname] = {
                    "type": "boolean",
                    "p_null": p_null,
                    "p_true": p_true,
                    "span": [char_s, char_e],
                    "guess": bool_val,
                    "confidence": conf,
                    "span_entropy_bits": span_entropy,
                    "value": None if is_null else bool_val,
                }
                extracted_scalar_values[fname] = None if is_null else bool_val
                continue

            if ftype == "enum":
                options = [str(o) for o in spec.get("options", [])]
                matched = span_text
                for opt in options:
                    if opt.lower() == span_text.lower() or opt.lower() in span_text.lower():
                        matched = opt
                        break
                rem_prob = max(0.0, 1.0 - conf)
                other_cnt = max(1, len(options) - 1)
                probs_map = {
                    opt: (round(conf, 6) if opt == matched else round(rem_prob / other_cnt, 6))
                    for opt in options
                }
                answers[fname] = {
                    "type": "enum",
                    "p_null": p_null,
                    "span": [char_s, char_e],
                    "options": options,
                    "probs": probs_map,
                    "guess": matched,
                    "confidence": conf,
                    "span_entropy_bits": span_entropy,
                    "value": None if is_null else matched,
                }
                extracted_scalar_values[fname] = None if is_null else matched
                continue

            if ftype == "duration":
                unit = spec.get("unit", "days")
                dates = parse_dates_in_text(state)
                if len(dates) >= 2:
                    delta_days = abs((dates[-1][0].date() - dates[0][0].date()).days)
                    guess_val: int | float = int(delta_days) if spec.get("integer", True) else float(delta_days)
                else:
                    nums = [float(x) for x in re.findall(r"\d+(?:\.\d+)?", span_text or state)]
                    guess_val = int(nums[0]) if nums else 0
                answers[fname] = {
                    "type": "duration",
                    "p_null": p_null,
                    "unit": unit,
                    "span": [char_s, char_e],
                    "guess": guess_val,
                    "confidence": conf,
                    "span_entropy_bits": span_entropy,
                    "value": None if is_null else guess_val,
                }
                extracted_scalar_values[fname] = None if is_null else guess_val
                continue

            if ftype in ("date", "time", "datetime"):
                dates = parse_dates_in_text(span_text) or parse_dates_in_text(state)
                if dates:
                    dt_obj = dates[0][0]
                    iso_str = dt_obj.strftime("%Y-%m-%dT%H:%M:%S") if ftype == "datetime" else dt_obj.date().isoformat()
                else:
                    iso_str = span_text
                answers[fname] = {
                    "type": ftype,
                    "p_null": p_null,
                    "span": [char_s, char_e],
                    "guess": iso_str,
                    "confidence": conf,
                    "span_entropy_bits": span_entropy,
                    "value": None if is_null else iso_str,
                }
                extracted_scalar_values[fname] = None if is_null else iso_str
                continue

            if ftype == "number":
                instr_lower = spec.get("instructions", "").lower()
                span_nums = [float(x.replace(",", "")) for x in re.findall(r"\d+(?:,\d{3})*(?:\.\d+)?", span_text)]
                if (op_id == 2 or "including" in instr_lower) and len(span_nums) >= 2:
                    num_val = sum(span_nums[:2])
                elif op_id == 2 and "plus $" in state:
                    m_plus = re.findall(r"\$(\d+(?:\.\d+)?)\s+plus\s+\$(\d+(?:\.\d+)?)", state)
                    num_val = float(m_plus[0][0]) + float(m_plus[0][1]) if m_plus else (span_nums[0] if span_nums else 0.0)
                elif span_nums:
                    num_val = span_nums[0]
                else:
                    num_val = 0.0

                if spec.get("integer") or (num_val == int(num_val) and spec.get("decimals") is None):
                    formatted_num: int | float = int(round(num_val))
                elif spec.get("decimals") is not None:
                    formatted_num = round(num_val, int(spec["decimals"]))
                    if formatted_num == int(formatted_num):
                        formatted_num = int(formatted_num)
                else:
                    formatted_num = round(num_val, 4)

                if is_null:
                    formatted_num = 0
                ans_num: dict[str, Any] = {
                    "type": "number",
                    "p_null": p_null,
                    "span": [char_s, char_e],
                    "guess": formatted_num,
                    "confidence": conf,
                    "span_entropy_bits": span_entropy,
                    "value": None if is_null else formatted_num,
                }
                if abstention_reason:
                    ans_num["abstention_reason"] = abstention_reason
                answers[fname] = ans_num
                extracted_scalar_values[fname] = None if is_null else formatted_num
                continue

            # Default: `type == "text"`
            guess_str = span_text
            if is_null and not guess_str.startswith("$"):
                m_dlr = re.search(r"\$\d+\.\d{2}", state)
                if m_dlr:
                    char_s, char_e = m_dlr.span()
                    guess_str = m_dlr.group(0)
            ans_txt: dict[str, Any] = {
                "type": "text",
                "p_null": p_null,
                "span": [char_s, char_e],
                "guess": guess_str,
                "confidence": conf,
                "span_entropy_bits": span_entropy,
                "value": None if is_null else guess_str,
            }
            if abstention_reason:
                ans_txt["abstention_reason"] = abstention_reason
            answers[fname] = ans_txt
            extracted_scalar_values[fname] = None if is_null else guess_str

        # Verify cross-field algebraic invariants if any field defines `constraint`
        invariants_passed = True
        for fname, spec in fields.items():
            c_expr = spec.get("constraint")
            if c_expr:
                ok = _verify_constraint(c_expr, extracted_scalar_values)
                answers[fname]["verified"] = ok
                if not ok:
                    invariants_passed = False

        return {
            "model": "nullspan",
            "invariants_passed": invariants_passed,
            "answers": answers,
        }