detrium.nn.matchers.matcher

Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved Modules to compute the matching cost and solve the corresponding LSAP. Copyright (c) 2024 The D-FINE Authors All Rights Reserved.

class HungarianMatcher(nn.Module)

This class computes an assignment between the targets and the predictions of the network For efficiency reasons, the targets don't include the no_object. Because of this, in general, there are more predictions than targets. In this case, we do a 1-to-1 matching of the best predictions, while the others are un-matched (and thus treated as non-objects).

名称类型默认值
weight_dict——
use_focal_loss—False
alpha—0.25
gamma—2.0
forward(self, outputs: Dict[str, torch.Tensor], targets, return_topk=False)

Performs the matching Params: outputs: This is a dict that contains at least these entries: "pred_logits": Tensor of dim [batch_size, num_queries, num_classes] with the classification logits "pred_boxes": Tensor of dim [batch_size, num_queries, 4] with the predicted box coordinates targets: This is a list of targets (len(targets) = batch_size), where each target is a dict containing: "labels": Tensor of dim [num_target_boxes] (where num_target_boxes is the number of ground-truth objects in the target) containing the class labels "boxes": Tensor of dim [num_target_boxes, 4] containing the target box coordinates Returns: A list of size batch_size, containing tuples of (index_i, index_j) where: - index_i is the indices of the selected predictions (in order) - index_j is the indices of the corresponding selected targets (in order) For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_target_boxes)

名称类型默认值
outputsDict[str, torch.Tensor]—
targets——
return_topk—False
class GPUAuctionMatcher(HungarianMatcher)

Zero-hyperparameter GPU Parallel Forward Auction Matcher. Solves all (head, image) rectangular assignment problems simultaneously on GPU via affine-invariant Forward epsilon-Auction (Bertsekas, 1988), eliminating CPU transfers, SciPy loops, and cross-batch cost blowups while preserving exact scale and column-shift invariance.

名称类型默认值
weight_dict——
use_focal_loss—False
alpha—0.25
gamma—2.0
auction_rounds—12
auction_eps—0.015
greedy_passes—2
disp_passes—4
forward_all_heads(self, cls_heads, enc_heads, enc_class_agnostic: bool, target_pack: dict)

Compute cost matrices and solve assignments for all decoder + encoder heads in one GPU pass. Returns gt_q of shape [H_cls + H_enc, B, M] (int64 in [0, N-1]).

名称类型默认值
cls_heads——
enc_heads——
enc_class_agnosticbool—
target_packdict—
forward(self, outputs: Dict[str, torch.Tensor], targets, return_topk=False)
名称类型默认值
outputsDict[str, torch.Tensor]—
targets——
return_topk—False
class BatchedHungarianMatcher(GPUAuctionMatcher)

Decoupled Exact Zero-Sync Batched GPU Hungarian Matcher. Separates matching cost computation (`compute_cost_matrix`, powered by a modular single-kernel Triton cost builder with transparent PyTorch fallback) from the exact GPU Hungarian solver (`batched_hungarian_cuda`, 8-warp register-resident 1-barrier Crouse Dijkstra + fused Pass-0 + Vickrey 2nd-price warm start). Cost terms are independently configurable (`cls_cost_type`, `bbox_cost_type`, `iou_cost_type`, `extra_cost_fn`, or by overriding `compute_cost_matrix`) without modifying the CUDA Hungarian solver. `cost_mode`: - `'triton'` (default): Decoupled Triton-fused cost matrix + `batched_hungarian_cuda` (automatically falls back to PyTorch cost on CPU / when Triton is unavailable). - `'fused'`: Coupled single-kernel `fused_focal_hungarian_cuda` fast path for standard `(focal, l1, giou)` cost without custom overrides. - `'eager'`: Pure PyTorch cost matrix + `batched_hungarian_cuda`. Can also be overridden via env var `DFINE_HUNGARIAN_COST_MODE`.

名称类型默认值
weight_dict——
use_focal_loss—False
alpha—0.25
gamma—2.0
auction_rounds—12
auction_eps—0.015
greedy_passes—2
disp_passes—4
cost_modestr'triton'
cls_cost_typeOptional[str]None
bbox_cost_typestr'l1'
iou_cost_typestr'giou'
smooth_l1_betafloat1.0
extra_cost_fnOptional[Callable]None
compute_cost_matrix(self, cls_heads, enc_heads, enc_class_agnostic: bool, target_pack: dict, boxes_all: Optional[torch.Tensor]=None, logits_cls: Optional[torch.Tensor]=None, logits_enc: Optional[torch.Tensor]=None, use_triton: bool=True, zero_masked: bool=False) -> torch.Tensor

Compute flattened matching cost matrix `C` of shape `[H_tot * B, N, M]` (float32). Subclasses can override this method to plug in arbitrary custom matching costs while continuing to use `batched_hungarian_cuda` untouched.

名称类型默认值
cls_heads——
enc_heads——
enc_class_agnosticbool—
target_packdict—
boxes_allOptional[torch.Tensor]None
logits_clsOptional[torch.Tensor]None
logits_encOptional[torch.Tensor]None
use_tritonboolTrue
zero_maskedboolFalse
forward_all_heads(self, cls_heads, enc_heads, enc_class_agnostic: bool, target_pack: dict, boxes_all: torch.Tensor=None, logits_cls: torch.Tensor=None, logits_enc: torch.Tensor=None)
名称类型默认值
cls_heads——
enc_heads——
enc_class_agnosticbool—
target_packdict—
boxes_alltorch.TensorNone
logits_clstorch.TensorNone
logits_enctorch.TensorNone