detrium.ops.cuda_ops

Custom CUDA operator wrappers for D-FINE / RT-DETRv4 with graceful PyTorch fallback, per-operator enable/disable switches (YAML + env), and build-cache staleness detection.

register_cuda_op(name: str, aliases: Sequence[str]=()) -> None

Register a canonical CUDA operator name and optional aliases.

名称类型默认值
namestr—
aliasesSequence[str]()
compute_cuda_ext_build_signature(repo_root: Path=_REPO_ROOT, setup_py: Optional[Path]=None) -> Dict[str, str]

Compute a deterministic source + ABI build signature for `_detrium_cuda_ext`.

名称类型默认值
repo_rootPath_REPO_ROOT
setup_pyOptional[Path]None
is_cuda_ext_binary_up_to_date(repo_root: Path=_REPO_ROOT, setup_py: Optional[Path]=None) -> bool

Return True iff `_detrium_cuda_ext` binary exists in cache and matches current csrc/ABI.

名称类型默认值
repo_rootPath_REPO_ROOT
setup_pyOptional[Path]None
should_use_cuda_op(op_name: str, precondition_ok: bool=True, reason: str='') -> bool

Central per-operator dispatch gate supporting `auto`, `off`, and `strict` modes.

名称类型默认值
op_namestr—
precondition_okboolTrue
reasonstr''
ensure_dfine_cuda_ops(verbose: bool=True, setup_py: Optional[Path]=None) -> bool

Ensure `_detrium_cuda_ext` is compiled into the out-of-tree cache, up-to-date, and loaded. Degrades cleanly to the pure-PyTorch fallback on CPU/MPS, missing nvcc, or opt-out.

名称类型默认值
verboseboolTrue
setup_pyOptional[Path]None
can_use_cuda_ms_deform_attn_v2(value: torch.Tensor, spatial_shapes: Sequence[Sequence[int]], num_points_list: Sequence[int], method: str='default', sampling_locations: Optional[torch.Tensor]=None, attention_weights: Optional[torch.Tensor]=None, value_shape: str='default') -> bool
名称类型默认值
valuetorch.Tensor—
spatial_shapesSequence[Sequence[int]]—
num_points_listSequence[int]—
methodstr'default'
sampling_locationsOptional[torch.Tensor]None
attention_weightsOptional[torch.Tensor]None
value_shapestr'default'
ms_deform_attn_v2_cuda(value: torch.Tensor, spatial_shapes: Sequence[Sequence[int]], sampling_locations: torch.Tensor, attention_weights: torch.Tensor, num_points_list: Sequence[int]) -> torch.Tensor

value: [B, S, M, D] sampling_locations: [B, Lq, M, P_total, 2] in [0, 1] attention_weights: [B, Lq, M, P_total]

名称类型默认值
valuetorch.Tensor—
spatial_shapesSequence[Sequence[int]]—
sampling_locationstorch.Tensor—
attention_weightstorch.Tensor—
num_points_listSequence[int]—
can_use_cuda_ms_deform_attn_v2_fused(value: torch.Tensor, spatial_shapes: Sequence[Sequence[int]], num_points_list: Sequence[int], sampling_offsets: torch.Tensor, attention_logits: torch.Tensor, reference_points: torch.Tensor, method: str='default', value_shape: str='default') -> bool
名称类型默认值
valuetorch.Tensor—
spatial_shapesSequence[Sequence[int]]—
num_points_listSequence[int]—
sampling_offsetstorch.Tensor—
attention_logitstorch.Tensor—
reference_pointstorch.Tensor—
methodstr'default'
value_shapestr'default'
ms_deform_attn_v2_fused_cuda(value: torch.Tensor, spatial_shapes: Sequence[Sequence[int]], sampling_offsets: torch.Tensor, attention_logits: torch.Tensor, reference_points: torch.Tensor, num_points_list: Sequence[int], offset_scale: float=0.5) -> torch.Tensor

Fused MSDeformAttn v2 entrypoint combining 4D reference-point offset scaling, FP32 softmax over attention logits, and bilinear sampling. value: [B, S, M, D] sampling_offsets: [B, Lq, M, P_total, 2] attention_logits: [B, Lq, M, P_total] reference_points: [B, Lq, 1, 4]

名称类型默认值
valuetorch.Tensor—
spatial_shapesSequence[Sequence[int]]—
sampling_offsetstorch.Tensor—
attention_logitstorch.Tensor—
reference_pointstorch.Tensor—
num_points_listSequence[int]—
offset_scalefloat0.5
batched_hungarian_cuda(cost: torch.Tensor, gt_mask: torch.Tensor) -> torch.Tensor

Solve P independent [N, M] exact min-cost injective assignments on GPU. Args: cost: [P, N, M] floating-point tensor on CUDA. gt_mask: [P, M] bool tensor on CUDA indicating active GTs per subproblem. Returns: gt_q: [P, M] int64 tensor on CUDA with matched query indices in [0, N-1] (0 for inactive/padded GTs).

名称类型默认值
costtorch.Tensor—
gt_masktorch.Tensor—
can_use_cuda_fused_focal_hungarian(boxes_all: torch.Tensor, tgt_boxes: torch.Tensor, tgt_labels: torch.Tensor, gt_mask: torch.Tensor, logits_cls: Optional[torch.Tensor]=None, logits_enc: Optional[torch.Tensor]=None) -> bool
名称类型默认值
boxes_alltorch.Tensor—
tgt_boxestorch.Tensor—
tgt_labelstorch.Tensor—
gt_masktorch.Tensor—
logits_clsOptional[torch.Tensor]None
logits_encOptional[torch.Tensor]None
fused_focal_hungarian_cuda(boxes_all: torch.Tensor, logits_cls: torch.Tensor, logits_enc: torch.Tensor, tgt_boxes: torch.Tensor, tgt_labels: torch.Tensor, gt_mask: torch.Tensor, alpha: float, gamma: float, w_bbox: float, w_cls: float, w_giou: float, enc_class_agnostic: bool) -> torch.Tensor

Single-kernel fused Focal/L1/GIoU cost (ragged active GTs + unique-class dedup) and exact Super-Sink + Vickrey/GS-ARR GPU Hungarian solver.

名称类型默认值
boxes_alltorch.Tensor—
logits_clstorch.Tensor—
logits_enctorch.Tensor—
tgt_boxestorch.Tensor—
tgt_labelstorch.Tensor—
gt_masktorch.Tensor—
alphafloat—
gammafloat—
w_bboxfloat—
w_clsfloat—
w_gioufloat—
enc_class_agnosticbool—