detrium.ops.matching_cost

Decoupled, modular, Triton-fused DETR matching cost computation with PyTorch fallback. Separates matching cost construction (`[H_tot * B, N, M]`) from the exact GPU Hungarian solver (`batched_hungarian_cuda`), enabling effortless customization of classification, box-regression, IoU-family, or arbitrary user-defined cost terms without touching CUDA Hungarian solver internals, while avoiding PyTorch eager's multi-kernel HBM traffic.

triton_detr_cost_matrix(boxes_all: torch.Tensor, logits_cls: Optional[torch.Tensor], logits_enc: Optional[torch.Tensor], tgt_boxes: torch.Tensor, tgt_labels: torch.Tensor, gt_mask: torch.Tensor, *, cls_cost_type: str='focal', bbox_cost_type: str='l1', iou_cost_type: str='giou', alpha: float=0.25, gamma: float=2.0, w_bbox: float=5.0, w_cls: float=2.0, w_giou: float=2.0, smooth_l1_beta: float=1.0, enc_class_agnostic: bool=False, zero_masked: bool=False) -> torch.Tensor

Compute DETR matching cost matrix `C` of shape `[H_tot * B, N, M]` in a single Triton kernel. Decoupled from the Hungarian solver: outputs a standard contiguous `[P, N, M]` float32 tensor directly consumable by `batched_hungarian_cuda(C, gt_mask_flat)` or any custom post-processing.

名称类型默认值
boxes_alltorch.Tensor—
logits_clsOptional[torch.Tensor]—
logits_encOptional[torch.Tensor]—
tgt_boxestorch.Tensor—
tgt_labelstorch.Tensor—
gt_masktorch.Tensor—
cls_cost_typestr'focal'
bbox_cost_typestr'l1'
iou_cost_typestr'giou'
alphafloat0.25
gammafloat2.0
w_bboxfloat5.0
w_clsfloat2.0
w_gioufloat2.0
smooth_l1_betafloat1.0
enc_class_agnosticboolFalse
zero_maskedboolFalse
pytorch_detr_cost_matrix(boxes_all: torch.Tensor, logits_cls: Optional[torch.Tensor], logits_enc: Optional[torch.Tensor], tgt_boxes: torch.Tensor, tgt_labels: torch.Tensor, gt_mask: torch.Tensor, *, cls_cost_type: str='focal', bbox_cost_type: str='l1', iou_cost_type: str='giou', alpha: float=0.25, gamma: float=2.0, w_bbox: float=5.0, w_cls: float=2.0, w_giou: float=2.0, smooth_l1_beta: float=1.0, enc_class_agnostic: bool=False, zero_masked: bool=False) -> torch.Tensor

Pure PyTorch reference implementation matching `triton_detr_cost_matrix` exactly. Used automatically on CPU, when Triton is unavailable, or when `cost_mode='eager'`.

名称类型默认值
boxes_alltorch.Tensor—
logits_clsOptional[torch.Tensor]—
logits_encOptional[torch.Tensor]—
tgt_boxestorch.Tensor—
tgt_labelstorch.Tensor—
gt_masktorch.Tensor—
cls_cost_typestr'focal'
bbox_cost_typestr'l1'
iou_cost_typestr'giou'
alphafloat0.25
gammafloat2.0
w_bboxfloat5.0
w_clsfloat2.0
w_gioufloat2.0
smooth_l1_betafloat1.0
enc_class_agnosticboolFalse
zero_maskedboolFalse
compute_detr_cost_matrix(boxes_all: torch.Tensor, logits_cls: Optional[torch.Tensor], logits_enc: Optional[torch.Tensor], tgt_boxes: torch.Tensor, tgt_labels: torch.Tensor, gt_mask: torch.Tensor, *, cls_cost_type: str='focal', bbox_cost_type: str='l1', iou_cost_type: str='giou', alpha: float=0.25, gamma: float=2.0, w_bbox: float=5.0, w_cls: float=2.0, w_giou: float=2.0, smooth_l1_beta: float=1.0, enc_class_agnostic: bool=False, zero_masked: bool=False, use_triton: bool=True, extra_cost_fn: Optional[Callable]=None) -> torch.Tensor

Unified decoupled DETR cost-matrix builder (`[H_tot * B, N, M]`). Dispatches to the single-kernel Triton implementation on CUDA when available and `use_triton=True`, falling back transparently to exact PyTorch otherwise. If `extra_cost_fn` is provided, it is invoked with `(boxes_all, logits_cls, logits_enc, tgt_boxes, tgt_labels, gt_mask, enc_class_agnostic)` and its `[P, N, M]` result is added to the returned cost matrix.

名称类型默认值
boxes_alltorch.Tensor—
logits_clsOptional[torch.Tensor]—
logits_encOptional[torch.Tensor]—
tgt_boxestorch.Tensor—
tgt_labelstorch.Tensor—
gt_masktorch.Tensor—
cls_cost_typestr'focal'
bbox_cost_typestr'l1'
iou_cost_typestr'giou'
alphafloat0.25
gammafloat2.0
w_bboxfloat5.0
w_clsfloat2.0
w_gioufloat2.0
smooth_l1_betafloat1.0
enc_class_agnosticboolFalse
zero_maskedboolFalse
use_tritonboolTrue
extra_cost_fnOptional[Callable]None