py123d_detection.torch.models.bevformer.deformable_attention

Classes

MultiScaleDeformableAttention([embed_dims, ...])

Multi-scale deformable attention.

MultiScaleDeformableAttention3D([...])

Height-aware Multi-Scale Deformable Attention for BEVFormer SCA.

class py123d_detection.torch.models.bevformer.deformable_attention.MultiScaleDeformableAttention(embed_dims=256, num_heads=8, num_levels=1, num_points=4)[source]

Bases: Module

Multi-scale deformable attention.

Full attention over a feature map costs its area; this attends to a handful of learned locations instead. Each query predicts where to look, as offsets from its reference point, and how much to weight each of those samples. The features there are read by bilinear interpolation and summed.

Parameters:
  • embed_dims (int) – Feature dimension.

  • num_heads (int) – Attention heads.

  • num_levels (int) – Feature-map levels.

  • num_points (int) – Sampling points per head, per level.

forward(query, reference_points, value, spatial_shapes, level_start_index, key_padding_mask=None)[source]
Parameters:
  • query (Tensor) – (B, Q, C).

  • reference_points (Tensor) – (B, Q, num_levels, 2), normalised to [0, 1], one per query per level.

  • value (Tensor) – (B, S, C), every level’s features flattened and concatenated, so S = sum(H_l * W_l).

  • spatial_shapes (Tensor) – (num_levels, 2), each level’s (H, W).

  • level_start_index (Tensor) – (num_levels,), where each level begins in value.

  • key_padding_mask (Tensor) – (B, S), True at padded positions.

Return type:

Tensor

Returns:

(B, Q, C).

class py123d_detection.torch.models.bevformer.deformable_attention.MultiScaleDeformableAttention3D(embed_dims=256, num_heads=8, num_levels=1, num_points=8, num_Z_anchors=4)[source]

Bases: Module

Height-aware Multi-Scale Deformable Attention for BEVFormer SCA.

Unlike plain MultiScaleDeformableAttention, each BEV query carries num_Z_anchors 3D reference points (one per pillar height) that project to distinct 2D locations in each camera view. The num_points sampling offsets predicted per query are shared across all height anchors, so only the 2D reference location changes with height, and the sampled features are averaged across heights before weighted-summing across points. This matches MSDeformableAttention3D in external/BEVFormer/projects/mmdet3d_plugin/bevformer/modules/spatial_cross_attention.py.

Parameters:
  • embed_dims (int) – Feature dimension.

  • num_heads (int) – Attention heads.

  • num_levels (int) – Feature-map levels.

  • num_points (int) – Sampling points per query, per head, per level.

  • num_Z_anchors (int) – Height samples per pillar.

forward(query, reference_points, value, spatial_shapes, level_start_index, key_padding_mask=None)[source]
Parameters:
  • query (Tensor) – (B, Q, C).

  • reference_points (Tensor) – (B, Q, num_Z_anchors, 2), normalised to [0, 1], one per pillar height.

  • value (Tensor) – (B, S, C), every level’s features flattened and concatenated.

  • spatial_shapes (Tensor) – (num_levels, 2), each level’s (H, W).

  • level_start_index (Tensor) – (num_levels,), where each level begins in value.

  • key_padding_mask (Tensor) – (B, S), True at padded positions.

Return type:

Tensor

Returns:

(B, Q, C).