- Add enhanced README with project structure and quick start guide - Initialize repository with DCNv4 CUDA extension (PyTorch module) - Include classification, detection, and segmentation subdirectories - Reference upstream OpenGVLab DCNv4 implementation Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
370 lines
14 KiB
Python
370 lines
14 KiB
Python
# --------------------------------------------------------
|
|
# DCNv4
|
|
# Copyright (c) 2024 OpenGVLab
|
|
# Licensed under The MIT License [see LICENSE for details]
|
|
# --------------------------------------------------------
|
|
|
|
import torch
|
|
from torch.cuda.amp import custom_bwd, custom_fwd
|
|
from torch.autograd.function import Function, once_differentiable
|
|
from mmcv.utils import ext_loader
|
|
ext_module = ext_loader.load_ext(
|
|
'_ext', ['ms_deform_attn_backward', 'ms_deform_attn_forward'])
|
|
|
|
class MultiScaleDeformableAttnFunction_fp16(Function):
|
|
|
|
@staticmethod
|
|
@custom_fwd(cast_inputs=torch.float16)
|
|
def forward(ctx, value, value_spatial_shapes, value_level_start_index,
|
|
sampling_locations, attention_weights, im2col_step):
|
|
"""GPU version of multi-scale deformable attention.
|
|
|
|
Args:
|
|
value (Tensor): The value has shape
|
|
(bs, num_keys, mum_heads, embed_dims//num_heads)
|
|
value_spatial_shapes (Tensor): Spatial shape of
|
|
each feature map, has shape (num_levels, 2),
|
|
last dimension 2 represent (h, w)
|
|
sampling_locations (Tensor): The location of sampling points,
|
|
has shape
|
|
(bs ,num_queries, num_heads, num_levels, num_points, 2),
|
|
the last dimension 2 represent (x, y).
|
|
attention_weights (Tensor): The weight of sampling points used
|
|
when calculate the attention, has shape
|
|
(bs ,num_queries, num_heads, num_levels, num_points),
|
|
im2col_step (Tensor): The step used in image to column.
|
|
|
|
Returns:
|
|
Tensor: has shape (bs, num_queries, embed_dims)
|
|
"""
|
|
ctx.im2col_step = im2col_step
|
|
output = ext_module.ms_deform_attn_forward(
|
|
value,
|
|
value_spatial_shapes,
|
|
value_level_start_index,
|
|
sampling_locations,
|
|
attention_weights,
|
|
im2col_step=ctx.im2col_step)
|
|
ctx.save_for_backward(value, value_spatial_shapes,
|
|
value_level_start_index, sampling_locations,
|
|
attention_weights)
|
|
return output
|
|
|
|
@staticmethod
|
|
@once_differentiable
|
|
@custom_bwd
|
|
def backward(ctx, grad_output):
|
|
"""GPU version of backward function.
|
|
|
|
Args:
|
|
grad_output (Tensor): Gradient
|
|
of output tensor of forward.
|
|
|
|
Returns:
|
|
Tuple[Tensor]: Gradient
|
|
of input tensors in forward.
|
|
"""
|
|
value, value_spatial_shapes, value_level_start_index, \
|
|
sampling_locations, attention_weights = ctx.saved_tensors
|
|
grad_value = torch.zeros_like(value)
|
|
grad_sampling_loc = torch.zeros_like(sampling_locations)
|
|
grad_attn_weight = torch.zeros_like(attention_weights)
|
|
|
|
ext_module.ms_deform_attn_backward(
|
|
value,
|
|
value_spatial_shapes,
|
|
value_level_start_index,
|
|
sampling_locations,
|
|
attention_weights,
|
|
grad_output.contiguous(),
|
|
grad_value,
|
|
grad_sampling_loc,
|
|
grad_attn_weight,
|
|
im2col_step=ctx.im2col_step)
|
|
|
|
return grad_value, None, None, \
|
|
grad_sampling_loc, grad_attn_weight, None
|
|
|
|
|
|
|
|
shm_size_dict = {
|
|
"8.0": 163000,
|
|
"8.6": 99000,
|
|
"8.7": 163000,
|
|
"8.9": 99000,
|
|
"9.0": 227000,
|
|
"7.5": 64000,
|
|
"7.0": 96000,
|
|
}
|
|
|
|
cuda_capability = f"{torch.cuda.get_device_properties(0).major}.{torch.cuda.get_device_properties(0).minor}"
|
|
|
|
if cuda_capability not in shm_size_dict:
|
|
raise NotImplementedError
|
|
|
|
shm_size_cap = shm_size_dict[cuda_capability]
|
|
|
|
|
|
class MultiScaleDeformableAttnFunction_fp32_old(Function):
|
|
|
|
@staticmethod
|
|
@custom_fwd(cast_inputs=torch.float32)
|
|
def forward(ctx, value, value_spatial_shapes, value_level_start_index,
|
|
sampling_locations, attention_weights, im2col_step):
|
|
"""GPU version of multi-scale deformable attention.
|
|
|
|
Args:
|
|
value (Tensor): The value has shape
|
|
(bs, num_keys, mum_heads, embed_dims//num_heads)
|
|
value_spatial_shapes (Tensor): Spatial shape of
|
|
each feature map, has shape (num_levels, 2),
|
|
last dimension 2 represent (h, w)
|
|
sampling_locations (Tensor): The location of sampling points,
|
|
has shape
|
|
(bs ,num_queries, num_heads, num_levels, num_points, 2),
|
|
the last dimension 2 represent (x, y).
|
|
attention_weights (Tensor): The weight of sampling points used
|
|
when calculate the attention, has shape
|
|
(bs ,num_queries, num_heads, num_levels, num_points),
|
|
im2col_step (Tensor): The step used in image to column.
|
|
|
|
Returns:
|
|
Tensor: has shape (bs, num_queries, embed_dims)
|
|
"""
|
|
|
|
ctx.im2col_step = im2col_step
|
|
output = ext_module.ms_deform_attn_forward(
|
|
value,
|
|
value_spatial_shapes,
|
|
value_level_start_index,
|
|
sampling_locations,
|
|
attention_weights,
|
|
im2col_step=ctx.im2col_step)
|
|
ctx.save_for_backward(value, value_spatial_shapes,
|
|
value_level_start_index, sampling_locations,
|
|
attention_weights)
|
|
return output
|
|
|
|
@staticmethod
|
|
@once_differentiable
|
|
@custom_bwd
|
|
def backward(ctx, grad_output):
|
|
"""GPU version of backward function.
|
|
|
|
Args:
|
|
grad_output (Tensor): Gradient
|
|
of output tensor of forward.
|
|
|
|
Returns:
|
|
Tuple[Tensor]: Gradient
|
|
of input tensors in forward.
|
|
"""
|
|
value, value_spatial_shapes, value_level_start_index, \
|
|
sampling_locations, attention_weights = ctx.saved_tensors
|
|
grad_value = torch.zeros_like(value)
|
|
grad_sampling_loc = torch.zeros_like(sampling_locations)
|
|
grad_attn_weight = torch.zeros_like(attention_weights)
|
|
|
|
ext_module.ms_deform_attn_backward(
|
|
value,
|
|
value_spatial_shapes,
|
|
value_level_start_index,
|
|
sampling_locations,
|
|
attention_weights,
|
|
grad_output.contiguous(),
|
|
grad_value,
|
|
grad_sampling_loc,
|
|
grad_attn_weight,
|
|
im2col_step=ctx.im2col_step)
|
|
|
|
return grad_value, None, None, \
|
|
grad_sampling_loc, grad_attn_weight, None
|
|
|
|
|
|
# Copyright (c) OpenMMLab. All rights reserved.
|
|
import math
|
|
import warnings
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
import torch.nn.functional as F
|
|
from torch.autograd.function import Function, once_differentiable
|
|
|
|
from mmcv import deprecated_api_warning
|
|
from mmcv.cnn import constant_init, xavier_init
|
|
from mmcv.cnn.bricks.registry import ATTENTION
|
|
from mmcv.runner import BaseModule
|
|
|
|
ext_module = ext_loader.load_ext(
|
|
'_ext', ['ms_deform_attn_backward', 'ms_deform_attn_forward'])
|
|
import functools
|
|
import time
|
|
from collections import defaultdict
|
|
import torch
|
|
from mmcv.ops import MultiScaleDeformableAttention
|
|
@ATTENTION.register_module()
|
|
class FlashMultiScaleDeformableAttention(MultiScaleDeformableAttention):
|
|
"""An attention module used in Deformable-Detr.
|
|
|
|
`Deformable DETR: Deformable Transformers for End-to-End Object Detection.
|
|
<https://arxiv.org/pdf/2010.04159.pdf>`_.
|
|
|
|
Args:
|
|
embed_dims (int): The embedding dimension of Attention.
|
|
Default: 256.
|
|
num_heads (int): Parallel attention heads. Default: 64.
|
|
num_levels (int): The number of feature map used in
|
|
Attention. Default: 4.
|
|
num_points (int): The number of sampling points for
|
|
each query in each head. Default: 4.
|
|
im2col_step (int): The step used in image_to_column.
|
|
Default: 64.
|
|
dropout (float): A Dropout layer on `inp_identity`.
|
|
Default: 0.1.
|
|
batch_first (bool): Key, Query and Value are shape of
|
|
(batch, n, embed_dim)
|
|
or (n, batch, embed_dim). Default to False.
|
|
norm_cfg (dict): Config dict for normalization layer.
|
|
Default: None.
|
|
init_cfg (obj:`mmcv.ConfigDict`): The Config for initialization.
|
|
Default: None.
|
|
"""
|
|
|
|
def __init__(self,
|
|
use_flash=False,
|
|
use_softmax=True,
|
|
**kwargs
|
|
):
|
|
super().__init__(**kwargs)
|
|
|
|
self.use_flash = use_flash
|
|
self.use_softmax = use_softmax
|
|
|
|
@deprecated_api_warning({'residual': 'identity'},
|
|
cls_name='FlashMultiScaleDeformableAttention')
|
|
# @run_time('ms_attention')
|
|
def forward(self,
|
|
query,
|
|
key=None,
|
|
value=None,
|
|
identity=None,
|
|
query_pos=None,
|
|
key_padding_mask=None,
|
|
reference_points=None,
|
|
spatial_shapes=None,
|
|
level_start_index=None,
|
|
**kwargs):
|
|
"""Forward Function of MultiScaleDeformAttention.
|
|
|
|
Args:
|
|
query (torch.Tensor): Query of Transformer with shape
|
|
(num_query, bs, embed_dims).
|
|
key (torch.Tensor): The key tensor with shape
|
|
`(num_key, bs, embed_dims)`.
|
|
value (torch.Tensor): The value tensor with shape
|
|
`(num_key, bs, embed_dims)`.
|
|
identity (torch.Tensor): The tensor used for addition, with the
|
|
same shape as `query`. Default None. If None,
|
|
`query` will be used.
|
|
query_pos (torch.Tensor): The positional encoding for `query`.
|
|
Default: None.
|
|
key_pos (torch.Tensor): The positional encoding for `key`. Default
|
|
None.
|
|
reference_points (torch.Tensor): The normalized reference
|
|
points with shape (bs, num_query, num_levels, 2),
|
|
all elements is range in [0, 1], top-left (0,0),
|
|
bottom-right (1, 1), including padding area.
|
|
or (N, Length_{query}, num_levels, 4), add
|
|
additional two dimensions is (w, h) to
|
|
form reference boxes.
|
|
key_padding_mask (torch.Tensor): ByteTensor for `query`, with
|
|
shape [bs, num_key].
|
|
spatial_shapes (torch.Tensor): Spatial shape of features in
|
|
different levels. With shape (num_levels, 2),
|
|
last dimension represents (h, w).
|
|
level_start_index (torch.Tensor): The start index of each level.
|
|
A tensor has shape ``(num_levels, )`` and can be represented
|
|
as [0, h_0*w_0, h_0*w_0+h_1*w_1, ...].
|
|
|
|
Returns:
|
|
torch.Tensor: forwarded results with shape
|
|
[num_query, bs, embed_dims].
|
|
"""
|
|
|
|
if value is None:
|
|
value = query
|
|
|
|
if identity is None:
|
|
identity = query
|
|
if query_pos is not None:
|
|
query = query + query_pos
|
|
if not self.batch_first:
|
|
# change to (bs, num_query ,embed_dims)
|
|
query = query.permute(1, 0, 2)
|
|
value = value.permute(1, 0, 2)
|
|
|
|
bs, num_query, _ = query.shape
|
|
bs, num_value, _ = value.shape
|
|
assert (spatial_shapes[:, 0] * spatial_shapes[:, 1]).sum() == num_value
|
|
|
|
value = self.value_proj(value)
|
|
if key_padding_mask is not None:
|
|
value = value.masked_fill(key_padding_mask[..., None], 0.0)
|
|
value = value.view(bs, num_value, self.num_heads, -1)
|
|
sampling_offsets = self.sampling_offsets(query).view(
|
|
bs, num_query, self.num_heads, self.num_levels, self.num_points, 2)
|
|
attention_weights = self.attention_weights(query).view(
|
|
bs, num_query, self.num_heads, self.num_levels * self.num_points)
|
|
|
|
if not self.use_flash:
|
|
if self.use_softmax:
|
|
attention_weights = attention_weights.softmax(-1)
|
|
attention_weights = attention_weights.view(bs, num_query,
|
|
self.num_heads,
|
|
self.num_levels,
|
|
self.num_points)
|
|
|
|
else:
|
|
attention_weights = attention_weights.view(bs, num_query,
|
|
self.num_heads,
|
|
self.num_levels,
|
|
self.num_points, 1)
|
|
|
|
if reference_points.shape[-1] == 2:
|
|
offset_normalizer = torch.stack(
|
|
[spatial_shapes[..., 1], spatial_shapes[..., 0]], -1)
|
|
sampling_locations = reference_points[:, :, None, :, None, :] \
|
|
+ sampling_offsets \
|
|
/ offset_normalizer[None, None, None, :, None, :]
|
|
elif reference_points.shape[-1] == 4:
|
|
sampling_locations = reference_points[:, :, None, :, None, :2] \
|
|
+ sampling_offsets / self.num_points \
|
|
* reference_points[:, :, None, :, None, 2:] \
|
|
* 0.5
|
|
else:
|
|
raise ValueError(
|
|
f'Last dim of reference_points must be'
|
|
f' 2 or 4, but get {reference_points.shape[-1]} instead.')
|
|
sampling_locations = sampling_locations.to(sampling_offsets.dtype)
|
|
if torch.cuda.is_available() and value.is_cuda:
|
|
if self.use_flash:
|
|
assert False
|
|
else:
|
|
MultiScaleDeformableAttnFunction = MultiScaleDeformableAttnFunction_fp32_old
|
|
|
|
output = MultiScaleDeformableAttnFunction.apply(
|
|
value, spatial_shapes, level_start_index, sampling_locations,
|
|
attention_weights, self.im2col_step)
|
|
|
|
else:
|
|
output = multi_scale_deformable_attn_pytorch(
|
|
value, spatial_shapes, sampling_locations, attention_weights)
|
|
|
|
output = self.output_proj(output)
|
|
|
|
if not self.batch_first:
|
|
# (num_query, bs ,embed_dims)
|
|
output = output.permute(1, 0, 2)
|
|
|
|
return self.dropout(output) + identity
|