File size: 11,955 Bytes
c645013
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared live prediction for local and Hugging Face FALCON models."""

from __future__ import annotations

import json
import os
from collections.abc import Collection, Mapping
from pathlib import Path
from typing import Any

import numpy as np
import torch
from PIL import Image

from .capabilities import SafetyCapabilities
from .grounding import (
    PanopticPrediction,
    SegmentationPrediction,
    resolve_panoptic,
    resolve_segmentation,
    select_panoptic_category,
)
from .model import FalconModel


def _candidate_relations_from_answer(text: str) -> list[dict[str, str]] | None:
    """Extract only explicit observable geometry from a structured model answer.

    Safety examples are trained to emit a compact JSON object.  Risk and
    physical links may be unavailable, so this parser never derives either
    from geometry or component presence.
    """

    try:
        payload = json.loads(text)
    except (json.JSONDecodeError, TypeError):
        return None
    if not isinstance(payload, dict) or "candidate_relations" not in payload:
        return None
    raw = payload["candidate_relations"]
    if not isinstance(raw, list):
        raise ValueError("generated candidate_relations must be an array")
    horizontal_values = {
        "left_of",
        "right_of",
        "horizontally_aligned",
        "left of",
        "right of",
        "horizontally aligned",
    }
    vertical_values = {"above", "below", "vertically_aligned", "vertically aligned"}
    parsed: list[dict[str, str]] = []
    for index, relation in enumerate(raw):
        if not isinstance(relation, dict):
            raise ValueError(
                f"generated candidate_relations[{index}] must be an object"
            )
        if relation.get("connection_observed") not in (None, False):
            raise ValueError(
                f"generated candidate_relations[{index}] asserts an unavailable "
                "physical connection"
            )
        source = relation.get("src_component")
        target = relation.get("dst_component")
        horizontal = relation.get("horizontal_relation")
        vertical = relation.get("vertical_relation")
        if not all(
            isinstance(value, str) and value.strip() for value in (source, target)
        ):
            raise ValueError(
                f"generated candidate_relations[{index}] requires component names"
            )
        if horizontal is not None and horizontal not in horizontal_values:
            raise ValueError(
                f"generated candidate_relations[{index}] has unsupported "
                "horizontal geometry"
            )
        if vertical is not None and vertical not in vertical_values:
            raise ValueError(
                f"generated candidate_relations[{index}] has unsupported vertical geometry"
            )
        if horizontal is None and vertical is None:
            raise ValueError(
                f"generated candidate_relations[{index}] requires at least one "
                "supported geometry axis"
            )
        parsed_relation = {
            "src_component": source.strip().casefold(),
            "dst_component": target.strip().casefold(),
        }
        if horizontal is not None:
            parsed_relation["horizontal_relation"] = (
                str(horizontal).replace(" ", "_").casefold()
            )
        if vertical is not None:
            parsed_relation["vertical_relation"] = (
                str(vertical).replace(" ", "_").casefold()
            )
        parsed.append(parsed_relation)
    return parsed


def _safety_prediction(safety: Any) -> dict[str, Any]:
    prediction = getattr(safety, "prediction", None)
    if callable(prediction):
        value = prediction(0)
        if not isinstance(value, Mapping):
            raise TypeError("SafetyOutput.prediction() must return a mapping")
        return dict(value)

    # Compatibility for lightweight callers/tests predating SafetyOutput's
    # serializer.  If capabilities are provided they are still honored; real
    # checkpoint-backed models always take the method path above.
    capabilities = getattr(safety, "capabilities", SafetyCapabilities())
    if not isinstance(capabilities, SafetyCapabilities):
        raise TypeError("safety.capabilities must be SafetyCapabilities")
    risk = (
        float(safety.risk_probability[0].detach().float().cpu())
        if capabilities.risk
        else None
    )
    presence = [
        float(value.detach().float().cpu()) if enabled else None
        for value, enabled in zip(
            safety.presence_probabilities[0], capabilities.presence, strict=True
        )
    ]
    links = [
        float(value.detach().float().cpu()) if enabled else None
        for value, enabled in zip(
            safety.link_probabilities[0], capabilities.links, strict=True
        )
    ]
    return {
        "risk": risk,
        "presence": presence,
        "links": links,
        "capabilities": capabilities.as_dict(),
    }


def _generate(
    model: FalconModel,
    *,
    image: Any,
    prompt: str,
    max_new_tokens: int = 128,
    temperature: float = 0.0,
) -> tuple[dict[str, Any], Any]:
    """Run generation once and retain the exact selected detector proposals."""

    if max_new_tokens < 1:
        raise ValueError("max_new_tokens must be positive")
    if temperature < 0:
        raise ValueError("temperature cannot be negative")
    if isinstance(image, str | os.PathLike):
        # Transformers image processors consume decoded pixels, not path
        # strings.  Detach from the file handle once here so RF-DETR, DINO, and
        # native-size bookkeeping all see the exact same RGB image object.
        with Image.open(Path(image).expanduser()) as opened:
            image = opened.convert("RGB").copy()
    formatted_prompt = f"USER: {prompt}\nASSISTANT:"
    encoded = model.prepare_multimodal_inputs(images=image, prompts=formatted_prompt)
    # We call the language model directly in order to preserve the exact
    # detector output for grounding resolution, so mirror FalconModel.generate's
    # context check before allocating the generation cache.
    model._validate_context_budget(
        encoded.prefix,
        additional_tokens=max_new_tokens,
    )
    generation_kwargs: dict[str, Any] = {
        "max_new_tokens": max_new_tokens,
        "do_sample": temperature > 0,
        "eos_token_id": model.tokenizer.eos_token_id,
        "pad_token_id": model.tokenizer.pad_token_id,
    }
    if temperature > 0:
        generation_kwargs["temperature"] = temperature
    generated = model.language_model.generate(
        inputs_embeds=encoded.prefix.inputs_embeds,
        attention_mask=encoded.prefix.attention_mask,
        **generation_kwargs,
    )
    text = model.tokenizer.batch_decode(generated, skip_special_tokens=True)[0].strip()
    candidate_relations = _candidate_relations_from_answer(text)
    structured_safety = _safety_prediction(encoded.safety)
    structured_safety["inference_ablation"] = {
        "name": getattr(model, "inference_ssa_ablation", "none"),
        "scope": "diagnostic",
        "paper_trained_ablation_claim": False,
    }
    if candidate_relations is not None:
        structured_safety["candidate_relations"] = candidate_relations
    return {
        "prompt": prompt,
        "answer": text,
        "safety": structured_safety,
    }, encoded


def _resolved_region_payload(
    model: FalconModel,
    text: str,
    detector_output: Any,
) -> tuple[dict[str, Any], np.ndarray]:
    resolved = model.resolve_region_references(text, detector_output)
    region_ids = [region.proposal_index for region in resolved]
    boxes = (
        torch.stack([region.box_xyxy for region in resolved]).float().cpu()
        if resolved
        else torch.empty(0, 4)
    )
    scores = [
        None if region.score is None else float(region.score.float().cpu())
        for region in resolved
    ]
    if resolved:
        binary_masks = []
        for region in resolved:
            mask = region.mask.detach()
            if mask.dtype == torch.bool:
                binary = mask
            elif detector_output.masks_are_logits:
                binary = mask >= 0.0
            else:
                binary = mask >= model.config.mask_probability_threshold
            binary_masks.append(binary.to(dtype=torch.bool))
        masks = torch.stack(binary_masks).cpu().numpy()
    else:
        image_sizes = getattr(detector_output, "image_sizes", None)
        if image_sizes is None:
            raise ValueError(
                "detector output lacks native image size for empty prediction"
            )
        sizes = torch.as_tensor(image_sizes).detach().cpu()
        if sizes.shape != (1, 2):
            raise ValueError("single-image inference requires one detector image size")
        height, width = (int(value) for value in sizes[0].tolist())
        if height < 1 or width < 1:
            raise ValueError("detector output contains an invalid native image size")
        masks = np.empty((0, height, width), dtype=bool)
    return {
        "region_ids": region_ids,
        "boxes_xyxy": boxes.tolist(),
        "scores": scores,
    }, masks


@torch.inference_mode()
def predict(
    model: FalconModel,
    *,
    image: Any,
    prompt: str,
    max_new_tokens: int = 128,
    temperature: float = 0.0,
) -> tuple[dict[str, Any], np.ndarray]:
    """Run one live detector→Falcon→LLM pass without any cache dependency."""

    result, encoded = _generate(
        model,
        image=image,
        prompt=prompt,
        max_new_tokens=max_new_tokens,
        temperature=temperature,
    )
    regions, masks = _resolved_region_payload(
        model,
        result["answer"],
        encoded.detector_output,
    )
    result.update(regions)
    return result, masks


@torch.inference_mode()
def predict_segmentation(
    model: FalconModel,
    *,
    image: Any,
    prompt: str,
    max_new_tokens: int = 128,
    temperature: float = 0.0,
) -> tuple[dict[str, Any], SegmentationPrediction]:
    """Generate and strictly resolve ``<SEG>`` transport against live proposals."""

    result, encoded = _generate(
        model,
        image=image,
        prompt=prompt,
        max_new_tokens=max_new_tokens,
        temperature=temperature,
    )
    prediction = resolve_segmentation(
        result["answer"],
        encoded.detector_output,
        max_regions=model.config.max_regions,
    )
    result["region_ids"] = list(prediction.region_indices)
    return result, prediction


@torch.inference_mode()
def predict_panoptic(
    model: FalconModel,
    *,
    image: Any,
    prompt: str,
    category_ids: Collection[int],
    query_category_id: int | None = None,
    max_new_tokens: int = 128,
    temperature: float = 0.0,
) -> tuple[dict[str, Any], PanopticPrediction]:
    """Generate panoptic instances, optionally selecting a queried predicted class."""

    if query_category_id is not None and (
        isinstance(query_category_id, bool)
        or not isinstance(query_category_id, int)
        or query_category_id not in category_ids
    ):
        raise ValueError("query_category_id must belong to the manifest category IDs")

    result, encoded = _generate(
        model,
        image=image,
        prompt=prompt,
        max_new_tokens=max_new_tokens,
        temperature=temperature,
    )
    prediction = resolve_panoptic(
        result["answer"],
        encoded.detector_output,
        category_ids=category_ids,
        max_regions=model.config.max_regions,
    )
    if query_category_id is not None:
        prediction = select_panoptic_category(prediction, query_category_id)
    result["region_ids"] = [instance.region_index for instance in prediction.instances]
    result["segments_info"] = [dict(segment) for segment in prediction.segments_info]
    return result, prediction