Files
composable-evidence-states-…/losses/matching_loss.py
T
2026-10-01 22:44:07 +08:00

100 lines
4.1 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
from itertools import permutations
import torch
import torch.nn.functional as F
from data.schema import NULL_TYPE, RULE_TYPE
@dataclass
class MatchingOutput:
loss: torch.Tensor
assignments: list[tuple[torch.Tensor, torch.Tensor]]
def _small_linear_assignment(cost: torch.Tensor) -> tuple[list[int], list[int]]:
"""Exact assignment for the MVP's at-most-six slots without a SciPy dependency."""
num_slots, num_facts = cost.shape
if num_facts > num_slots:
raise ValueError("The number of facts cannot exceed the number of slots.")
cpu_cost = cost.detach().cpu()
best_slots: tuple[int, ...] | None = None
best_cost = float("inf")
for slots in permutations(range(num_slots), num_facts):
current = sum(float(cpu_cost[slot, fact]) for fact, slot in enumerate(slots))
if current < best_cost:
best_cost = current
best_slots = slots
return list(best_slots or ()), list(range(num_facts))
def hungarian_matching_loss(
predictions: dict[str, torch.Tensor],
targets: list[torch.Tensor],
type_weight: float = 0.5,
) -> MatchingOutput:
subject_logp = predictions["subject_logits"].log_softmax(-1)
predicate_logp = predictions["predicate_logits"].log_softmax(-1)
object_logp = predictions["object_logits"].log_softmax(-1)
type_logp = predictions["type_logits"].log_softmax(-1)
consequent_predicate_logp = predictions["consequent_predicate_logits"].log_softmax(-1)
consequent_object_logp = predictions["consequent_object_logits"].log_softmax(-1)
device = subject_logp.device
total = subject_logp.sum() * 0.0
assignments = []
for batch_index, facts in enumerate(targets):
facts = facts.to(device)
num_slots = subject_logp.shape[1]
if len(facts):
cost = -(
type_logp[batch_index, :, facts[:, 0]]
+ subject_logp[batch_index, :, facts[:, 1]]
+ predicate_logp[batch_index, :, facts[:, 2]]
+ object_logp[batch_index, :, facts[:, 3]]
)
rule_columns = facts[:, 0] == RULE_TYPE
if rule_columns.any():
cost[:, rule_columns] -= (
consequent_predicate_logp[batch_index, :, facts[rule_columns, 4]]
+ consequent_object_logp[batch_index, :, facts[rule_columns, 5]]
)
slot_index, fact_index = _small_linear_assignment(cost)
slots = torch.as_tensor(slot_index, dtype=torch.long, device=device)
matched = torch.as_tensor(fact_index, dtype=torch.long, device=device)
selected = facts[matched]
total = total + F.cross_entropy(
predictions["subject_logits"][batch_index, slots], selected[:, 1]
)
total = total + F.cross_entropy(
predictions["predicate_logits"][batch_index, slots], selected[:, 2]
)
total = total + F.cross_entropy(
predictions["object_logits"][batch_index, slots], selected[:, 3]
)
selected_rules = selected[:, 0] == RULE_TYPE
if selected_rules.any():
total = total + F.cross_entropy(
predictions["consequent_predicate_logits"][batch_index, slots[selected_rules]],
selected[selected_rules, 4],
)
total = total + F.cross_entropy(
predictions["consequent_object_logits"][batch_index, slots[selected_rules]],
selected[selected_rules, 5],
)
else:
slots = torch.empty(0, dtype=torch.long, device=device)
matched = torch.empty(0, dtype=torch.long, device=device)
type_target = torch.full((num_slots,), NULL_TYPE, dtype=torch.long, device=device)
if len(slots):
type_target[slots] = facts[matched, 0]
total = total + type_weight * F.cross_entropy(
predictions["type_logits"][batch_index], type_target
)
assignments.append((slots, matched))
return MatchingOutput(total / len(targets), assignments)