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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Density ensembles, zero-coordinate mixtures, and encoded-B smoothing."""

import argparse
import hashlib
import json
import re
import time
from pathlib import Path

import numpy as np
import torch
from torch.nn import functional as F

from src.inference import apply_policy
from src.io import context_batch, enforce_input_opens, read_input
from src.models.unet import decode_logits, probability_median

PLAIN = {
    "boundary_unet_v1",
    "boundary_unet_axial_v1",
    "boundary_unet_global_v1",
    "boundary_unet_spatial_v1",
}
WITH_ZERO = PLAIN | {"boundary_zero_atom_v1"}


@torch.inference_mode()
def predict_density_ensemble(
    row, prepared, net, cfg, device, batch_size, tta, progress=None
):
    if cfg.get("density_decoder") != "posterior_median":
        raise ValueError("Unsupported probability-mixture decision")
    packets = [p[:3] for p in prepared]
    H, A = packets[0][1:]
    if any(p[1:] != (H, A) for p in packets):
        raise ValueError("Inconsistent source grids")
    items = [p[0] for p in packets]
    pixels = (
        items[0]
        if len(items) == 1
        else (
            np.concatenate(items, 0)
            if isinstance(items[0], np.ndarray)
            else torch.cat(items, 0)
        )
    )
    family = row["device"]
    out = []
    weights = [c["weight"] for c in cfg["components"]]
    if (
        any(not np.isfinite(w) or w <= 0 for w in weights)
        or abs(sum(weights) - 1) > 1e-6
    ):
        raise ValueError("Invalid probability-mixture weights")
    for start in range(0, len(pixels), batch_size):
        end = min(start + batch_size, len(pixels))
        mixture = None
        for component, model in zip(cfg["components"], net.nets):
            child = component["config"]
            radius = child.get("context_radius", 0)
            if (
                child["architecture"] not in PLAIN
                or child.get("prediction_policy")
                or component.get("post_policy")
                or child.get("tta_horizontal", False)
            ):
                raise ValueError(
                    "Density mixture requires plain, unfiltered component logits"
                )
            x = context_batch(pixels, start, end, radius, device)
            with torch.autocast(
                device_type="cuda", dtype=torch.float16, enabled=device.type == "cuda"
            ):
                z = model(x, family)
                p = z.float().softmax(2)
                if tta:
                    p = (p + model(x.flip(-1), family).float().softmax(2).flip(-1)) / 2
            weighted = p * float(component["weight"])
            mixture = weighted if mixture is None else mixture + weighted
        if cfg.get("density_native_columns", False):
            mixture = F.interpolate(
                mixture,
                size=(mixture.shape[-2], A),
                mode="bilinear",
                align_corners=True,
            )
            mixture = mixture / mixture.sum(2, keepdim=True).clamp_min(1e-12)
        positions = probability_median(mixture)
        positions = F.interpolate(
            positions, size=A, mode="linear", align_corners=True
        ) * ((H - 1) / (cfg["height"] - 1))
        out.append(positions.clamp(0, H - 1).cpu().numpy().astype("float32"))
        if progress:
            progress(end, len(pixels))
    return np.concatenate(out, 0).transpose(1, 0, 2), np.asarray(
        cfg["boundary_keys"][family]
    )


@torch.inference_mode()
def predict_zero_density_ensemble(row, prepared, net, cfg, device, batch_size, tta):
    """Average unconditional densities and literal-zero atoms before deciding."""

    if cfg.get("density_decoder") != "joint_zero_positive_median" or not cfg.get(
        "density_native_columns"
    ):
        raise ValueError("Zero-density mixtures require a native-column joint median")
    packets = [p[:3] for p in prepared]
    H, A = packets[0][1:]
    if any(p[1:] != (H, A) for p in packets):
        raise ValueError("Inconsistent source grids")
    items = [p[0] for p in packets]
    pixels = (
        items[0]
        if len(items) == 1
        else (
            np.concatenate(items, 0)
            if isinstance(items[0], np.ndarray)
            else torch.cat(items, 0)
        )
    )
    family = row["device"]
    out = []
    weights = [c["weight"] for c in cfg["components"]]
    if (
        any(not np.isfinite(w) or w <= 0 for w in weights)
        or abs(sum(weights) - 1) > 1e-6
    ):
        raise ValueError("Invalid zero-density mixture weights")
    for start in range(0, len(pixels), batch_size):
        end = min(start + batch_size, len(pixels))
        total_density = None
        total_atom = None
        for component, model in zip(cfg["components"], net.nets):
            child = component["config"]
            radius = child.get("context_radius", 0)
            if (
                child["architecture"] not in WITH_ZERO
                or child.get("prediction_policy")
                or component.get("post_policy")
                or child.get("tta_horizontal", False)
            ):
                raise ValueError("Unsupported filtered zero-density component")
            x = context_batch(pixels, start, end, radius, device)

            def component_mass(image):
                result = model(image, family)
                if child["architecture"] == "boundary_zero_atom_v1":
                    logits, zero_logits = result
                    atom = F.interpolate(
                        zero_logits.float(), size=A, mode="linear", align_corners=True
                    ).sigmoid()
                else:
                    logits = result
                    atom = torch.zeros(
                        (len(image), logits.shape[1], A),
                        device=device,
                        dtype=torch.float32,
                    )
                probability = logits.float().softmax(2)
                probability = F.interpolate(
                    probability,
                    size=(probability.shape[-2], A),
                    mode="bilinear",
                    align_corners=True,
                )
                probability = probability / probability.sum(2, keepdim=True).clamp_min(
                    1e-12
                )
                return probability * (1 - atom[:, :, None, :]), atom

            with torch.autocast(
                device_type="cuda", dtype=torch.float16, enabled=device.type == "cuda"
            ):
                density, atom = component_mass(x)
                if tta:
                    flipped_density, flipped_atom = component_mass(x.flip(-1))
                    density = (density + flipped_density.flip(-1)) / 2
                    atom = (atom + flipped_atom.flip(-1)) / 2
            density = density * float(component["weight"])
            atom = atom * float(component["weight"])
            total_density = (
                density if total_density is None else total_density + density
            )
            total_atom = atom if total_atom is None else total_atom + atom
        positions = zero_mixture_median(total_density, total_atom) * (
            (H - 1) / (cfg["height"] - 1)
        )
        out.append(positions.clamp(0, H - 1).cpu().numpy().astype("float32"))
    return np.concatenate(out, 0).transpose(1, 0, 2), np.asarray(
        cfg["boundary_keys"][family]
    )


def zero_mixture_median(probability, zero_probability):
    """Median of a literal-zero atom and a conditional row-bin density."""
    p = probability.float()
    p = p / p.sum(-2, keepdim=True).clamp_min(1e-12)
    q = zero_probability.float()
    tau = ((0.5 - q) / (1 - q).clamp_min(1e-12)).clamp(0, 1).unsqueeze(-2)
    cdf = p.cumsum(-2)
    index = (cdf >= tau).to(torch.int64).argmax(-2, keepdim=True)
    previous = cdf.gather(-2, (index - 1).clamp_min(0))
    previous = torch.where(index == 0, torch.zeros_like(previous), previous)
    mass = p.gather(-2, index).clamp_min(1e-12)
    value = (
        (index.float() - 0.5 + ((tau - previous) / mass).clamp(0, 1))
        .clamp(0, p.shape[-2] - 1)
        .squeeze(-2)
    )
    return torch.where(q >= 0.5, torch.zeros_like(value), value)


@torch.inference_mode()
def predict_joint_zero_median(row, prepared, net, cfg, device, batch_size, tta):
    """Combine distributions before deciding, including native-column mapping."""
    packets = [p[:3] for p in prepared]
    H, A = packets[0][1:]
    family = row["device"]
    radius = cfg.get("context_radius", 0)
    if any(p[1:] != (H, A) for p in packets):
        raise ValueError("Inconsistent zero-mixture grids")
    arrays = [p[0] for p in packets]
    pixels = (
        arrays[0]
        if len(arrays) == 1
        else (
            np.concatenate(arrays, 0)
            if isinstance(arrays[0], np.ndarray)
            else torch.cat(arrays, 0)
        )
    )
    outs = []
    for start in range(0, len(pixels), batch_size):
        end = min(start + batch_size, len(pixels))
        x = context_batch(pixels, start, end, radius, device)
        with torch.autocast(
            device_type="cuda", dtype=torch.float16, enabled=device.type == "cuda"
        ):
            z, q = net(x, family)
            probability = z.float().softmax(2)
            q = F.interpolate(
                q.float(), size=A, mode="linear", align_corners=True
            ).sigmoid()
            if tta:
                zf, qf = net(x.flip(-1), family)
                probability = (probability + zf.float().softmax(2).flip(-1)) / 2
                q = (
                    q
                    + F.interpolate(
                        qf.float(), size=A, mode="linear", align_corners=True
                    )
                    .sigmoid()
                    .flip(-1)
                ) / 2
        probability = F.interpolate(
            probability,
            size=(probability.shape[-2], A),
            mode="bilinear",
            align_corners=True,
        )
        values = zero_mixture_median(probability, q) * ((H - 1) / (cfg["height"] - 1))
        outs.append(values.clamp(0, H - 1).cpu().numpy().astype("float32"))
    return np.concatenate(outs, 0).transpose(1, 0, 2), np.asarray(
        cfg["boundary_keys"][family]
    )


@torch.inference_mode()
def predict_zero_atom(row, prepared, net, cfg, device, batch_size, tta):
    decision = cfg.get("zero_decision", "threshold_positive")
    if decision == "mixture_median":
        return predict_joint_zero_median(
            row, prepared, net, cfg, device, batch_size, tta
        )
    if decision != "threshold_positive":
        raise ValueError("Unknown coordinate-zero decision")
    prepared = [p[:3] for p in prepared]
    H, A = prepared[0][1:]
    family = row["device"]
    radius = cfg.get("context_radius", 0)
    threshold = cfg.get("zero_threshold", 0.5)
    if not 0 < threshold < 1 or any(p[1:] != (H, A) for p in prepared):
        raise ValueError("Invalid zero-atom geometry/threshold")
    arrays = [p[0] for p in prepared]
    pixels = (
        arrays[0]
        if len(arrays) == 1
        else (
            np.concatenate(arrays, 0)
            if isinstance(arrays[0], np.ndarray)
            else torch.cat(arrays, 0)
        )
    )
    outs = []
    child = cfg["coarse_config"]

    def run(x):
        z, q = net(x, family)
        p = decode_logits(
            z,
            child.get("local_radius", 6),
            child.get("decode_method", "local_centroid"),
        )
        p = F.interpolate(p, size=A, mode="linear", align_corners=True) * (
            (H - 1) / (cfg["height"] - 1)
        )
        q = F.interpolate(
            q.float(), size=A, mode="linear", align_corners=True
        ).sigmoid()
        return p, q

    for start in range(0, len(pixels), batch_size):
        end = min(start + batch_size, len(pixels))
        x = context_batch(pixels, start, end, radius, device)
        with torch.autocast(
            device_type="cuda", dtype=torch.float16, enabled=device.type == "cuda"
        ):
            p, q = run(x)
            if tta:
                pf, qf = run(x.flip(-1))
                p = (p + pf.flip(-1)) / 2
                q = (q + qf.flip(-1)) / 2
        values = torch.where(q >= threshold, torch.zeros_like(p), p)
        outs.append(values.clamp(0, H - 1).cpu().numpy().astype("float32"))
    return np.concatenate(outs, 0).transpose(1, 0, 2), np.asarray(
        cfg["boundary_keys"][family]
    )


def kernel(sigma):
    sigma = float(sigma)
    if not np.isfinite(sigma) or not 0 <= sigma <= 2:
        raise ValueError("B density sigma must lie in [0,2]")
    radius = int(4 * sigma + 0.5)
    if radius == 0:
        return np.array([0], dtype="int64"), np.array([1.0], dtype="float64")
    offsets = np.arange(-radius, radius + 1, dtype="int64")
    weights = np.exp(-0.5 * (offsets / sigma) ** 2)
    weights /= weights.sum()
    return offsets, weights


def mix_b(probability, start, end, block_start, frames, sigma):
    """Central B frames from a halo block; replicate encoded-volume endpoints."""
    offsets, weights = kernel(sigma)
    result = None
    if len(offsets) == 1:
        return probability[start - block_start : end - block_start]
    for offset, weight in zip(offsets, weights):
        indices = np.clip(np.arange(start, end) + offset, 0, frames - 1) - block_start
        if indices.min() < 0 or indices.max() >= len(probability):
            raise ValueError("Probability halo is incomplete")
        part = probability.index_select(
            0, torch.as_tensor(indices, device=probability.device)
        )
        value = part * float(weight)
        result = value if result is None else result + value
    return result / result.sum(2, keepdim=True).clamp_min(1e-12)


@torch.inference_mode()
def predict_bprob(row, prepared, net, cfg, device, batch_size=64, progress=None):
    if (
        cfg["architecture"] not in PLAIN
        or cfg.get("inference_wrapper") != "b_probability_smooth_v1"
        or cfg.get("decode_method") != "posterior_median"
        or cfg.get("tta_horizontal", False)
    ):
        raise ValueError("Unsupported B-density model configuration")
    packets = [p[:3] for p in prepared]
    height, width = packets[0][1:]
    if any(p[1:] != (height, width) for p in packets):
        raise ValueError("Inconsistent structural source grids")
    arrays = [p[0] for p in packets]
    pixels = (
        arrays[0]
        if len(arrays) == 1
        else (
            np.concatenate(arrays, 0)
            if isinstance(arrays[0], np.ndarray)
            else torch.cat(arrays, 0)
        )
    )
    if not len(pixels):
        raise ValueError("Empty structural volume")
    sigma = cfg["density_b_sigma"]
    offsets, _ = kernel(sigma)
    halo = int(offsets[-1])
    context = cfg.get("context_radius", 0)
    family = row["device"]
    output = []
    if family not in cfg["boundary_keys"]:
        raise ValueError("Unsupported device family")
    for start in range(0, len(pixels), batch_size):
        end = min(start + batch_size, len(pixels))
        lo = max(0, start - halo)
        hi = min(len(pixels), end + halo)
        image = context_batch(pixels, lo, hi, context, device)
        with torch.autocast(
            device_type="cuda", dtype=torch.float16, enabled=device.type == "cuda"
        ):
            probability = net(image, family).float().softmax(2)
        probability = mix_b(probability, start, end, lo, len(pixels), sigma)
        positions = probability_median(probability)
        positions = F.interpolate(
            positions, size=width, mode="linear", align_corners=True
        ) * ((height - 1) / (cfg["height"] - 1))
        output.append(positions.clamp(0, height - 1).cpu().numpy().astype("float32"))
        if progress:
            progress(end, len(pixels))
    return np.concatenate(output, 0).transpose(1, 0, 2), np.asarray(
        cfg["boundary_keys"][family]
    )


def bprob_main():
    from src.inference.predict import (
        load_model,
        ordered_prefetch,
        ordered_process_prefetch,
        prepare_pixels,
    )

    p = argparse.ArgumentParser()
    for key in ["input-manifest", "data-root", "checkpoint", "output-dir"]:
        p.add_argument("--" + key, type=Path, required=True)
    p.add_argument("--batch-size", type=int, default=64)
    p.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu")
    p.add_argument("--threads", type=int, default=1)
    p.add_argument("--decode-workers", type=int, default=16)
    p.add_argument(
        "--decode-backend", choices=["threads", "processes"], default="processes"
    )
    a = p.parse_args()
    if a.batch_size < 1 or a.decode_workers < 0:
        raise ValueError("Invalid inference batch/worker count")
    torch.set_num_threads(a.threads)
    device = torch.device(a.device)
    net, cfg = load_model(a.checkpoint, device)
    offsets, weights = kernel(cfg["density_b_sigma"])
    rows = read_input(a.input_manifest)
    audit = enforce_input_opens(a.data_root, rows)
    a.output_dir.mkdir(parents=True, exist_ok=False)
    start = time.time()
    workers = max(1, min(a.decode_workers, len(rows)))
    stream = (
        (
            ordered_process_prefetch(rows, a.data_root, cfg, workers, audit)
            if a.decode_backend == "processes"
            else ordered_prefetch(rows, a.data_root, cfg, workers)
        )
        if a.decode_workers
        else ((row, prepare_pixels(row, a.data_root, cfg)) for row in rows)
    )
    for i, (row, prepared) in enumerate(stream):
        if not re.fullmatch("[A-Za-z0-9_-]{1,64}", row["case_id"]):
            raise ValueError("Unsafe case identifier")
        positions, keys = predict_bprob(row, prepared, net, cfg, device, a.batch_size)
        positions = apply_policy(positions, row["device"], cfg.get("prediction_policy"))
        height = prepared[0][1]
        if not np.isfinite(positions).all():
            raise ValueError("Nonfinite prediction")
        np.savez_compressed(
            a.output_dir / (row["case_id"] + ".npz"),
            positions=np.clip(positions, 0, height - 1).astype("float32"),
            boundary_keys=keys,
        )
        if (i + 1) % 10 == 0 or i + 1 == len(rows):
            print(
                json.dumps(
                    {
                        "predicted_cases": i + 1,
                        "total": len(rows),
                        "seconds": round(time.time() - start, 2),
                    }
                ),
                flush=True,
            )
    files = [
        "predict.py",
        "src/inference/density.py",
        "src/inference/predict.py",
        "src/io.py",
        "src/models/unet.py",
        "src/models/__init__.py",
        "src/inference/__init__.py",
        "src/protocol.py",
    ]
    root = Path(__file__).resolve().parents[2]
    receipt = {
        "cases": len(rows),
        "seconds": time.time() - start,
        "checkpoint": a.checkpoint.name,
        "config": cfg,
        "source_hashes": {
            f: hashlib.sha256((root / f).read_bytes()).hexdigest() for f in files
        },
        "torch": torch.__version__,
        "execution_device": str(device),
        "batch_size": a.batch_size,
        "threads": a.threads,
        "input_fields": ["case_id", "device", "oct_paths"],
        "segmentation_access": False,
        "input_open_audit": audit,
        "tta": False,
        "prediction_policy": cfg.get("prediction_policy"),
        "decode_workers": a.decode_workers,
        "decode_backend": a.decode_backend,
        "preprocessing_storage_dtype": "float16",
        "inference_wrapper": "b_probability_smooth_v1",
        "density_b_sigma": cfg["density_b_sigma"],
        "encoded_B_kernel_offsets": offsets.tolist(),
        "encoded_B_kernel_weights": weights.tolist(),
        "density_filter_precedes_median": True,
    }
    (a.output_dir / "inference-receipt.json").write_text(
        json.dumps(receipt, indent=2) + "\n"
    )