2022-02-24 09:24:25 +08:00
|
|
|
# Copyright (c) OpenMMLab. All rights reserved.
|
2021-04-23 16:35:15 +08:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
|
|
|
|
from mmcv.ops.multi_scale_deform_attn import (
|
2021-06-11 18:09:31 +08:00
|
|
|
MultiScaleDeformableAttention, MultiScaleDeformableAttnFunction,
|
|
|
|
multi_scale_deformable_attn_pytorch)
|
2022-11-16 14:08:04 +08:00
|
|
|
from mmcv.utils import IS_CUDA_AVAILABLE, IS_MLU_AVAILABLE
|
2021-04-23 16:35:15 +08:00
|
|
|
|
2021-05-25 13:13:05 +08:00
|
|
|
_USING_PARROTS = True
|
|
|
|
try:
|
|
|
|
from parrots.autograd import gradcheck
|
|
|
|
except ImportError:
|
|
|
|
from torch.autograd import gradcheck
|
|
|
|
_USING_PARROTS = False
|
|
|
|
|
2021-04-23 16:35:15 +08:00
|
|
|
|
2022-11-16 14:08:04 +08:00
|
|
|
@pytest.mark.parametrize('device', [
|
2021-10-14 20:50:38 +08:00
|
|
|
'cpu',
|
|
|
|
pytest.param(
|
|
|
|
'cuda:0',
|
|
|
|
marks=pytest.mark.skipif(
|
2022-11-16 14:08:04 +08:00
|
|
|
not IS_CUDA_AVAILABLE, reason='requires CUDA support')),
|
|
|
|
pytest.param(
|
|
|
|
'mlu',
|
|
|
|
marks=pytest.mark.skipif(
|
|
|
|
not IS_MLU_AVAILABLE, reason='requires MLU support'))
|
2021-10-14 20:50:38 +08:00
|
|
|
])
|
2022-11-16 14:08:04 +08:00
|
|
|
def test_multiscale_deformable_attention(device):
|
2021-10-14 20:50:38 +08:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
# embed_dims must be divisible by num_heads,
|
|
|
|
MultiScaleDeformableAttention(
|
|
|
|
embed_dims=256,
|
|
|
|
num_heads=7,
|
|
|
|
)
|
2022-11-16 14:08:04 +08:00
|
|
|
device = torch.device(device)
|
2021-10-14 20:50:38 +08:00
|
|
|
msda = MultiScaleDeformableAttention(
|
|
|
|
embed_dims=3, num_levels=2, num_heads=3)
|
|
|
|
msda.init_weights()
|
|
|
|
num_query = 5
|
|
|
|
bs = 1
|
|
|
|
embed_dims = 3
|
|
|
|
query = torch.rand(num_query, bs, embed_dims).to(device)
|
|
|
|
key = torch.rand(num_query, bs, embed_dims).to(device)
|
|
|
|
spatial_shapes = torch.Tensor([[2, 2], [1, 1]]).long().to(device)
|
|
|
|
level_start_index = torch.Tensor([0, 4]).long().to(device)
|
|
|
|
reference_points = torch.rand(bs, num_query, 2, 2).to(device)
|
|
|
|
msda.to(device)
|
|
|
|
msda(
|
|
|
|
query,
|
|
|
|
key,
|
|
|
|
key,
|
|
|
|
reference_points=reference_points,
|
|
|
|
spatial_shapes=spatial_shapes,
|
|
|
|
level_start_index=level_start_index)
|
2022-12-11 17:48:21 +08:00
|
|
|
|
|
|
|
# test with value_proj_ratio
|
|
|
|
embed_dims = 6
|
|
|
|
value_proj_ratio = 0.5
|
|
|
|
query = torch.rand(num_query, bs, embed_dims).to(device)
|
|
|
|
key = torch.rand(num_query, bs, embed_dims).to(device)
|
|
|
|
msda = MultiScaleDeformableAttention(
|
|
|
|
embed_dims=embed_dims,
|
|
|
|
num_levels=2,
|
|
|
|
num_heads=3,
|
|
|
|
value_proj_ratio=value_proj_ratio)
|
|
|
|
msda.init_weights()
|
|
|
|
msda.to(device)
|
|
|
|
msda(
|
|
|
|
query,
|
|
|
|
key,
|
|
|
|
key,
|
|
|
|
reference_points=reference_points,
|
|
|
|
spatial_shapes=spatial_shapes,
|
|
|
|
level_start_index=level_start_index)
|
2021-10-14 20:50:38 +08:00
|
|
|
|
|
|
|
|
2021-04-23 16:35:15 +08:00
|
|
|
def test_forward_multi_scale_deformable_attn_pytorch():
|
|
|
|
N, M, D = 1, 2, 2
|
|
|
|
Lq, L, P = 2, 2, 2
|
|
|
|
shapes = torch.as_tensor([(6, 4), (3, 2)], dtype=torch.long)
|
2022-05-18 11:47:14 +08:00
|
|
|
S = sum((H * W).item() for H, W in shapes)
|
2021-04-23 16:35:15 +08:00
|
|
|
|
|
|
|
torch.manual_seed(3)
|
|
|
|
value = torch.rand(N, S, M, D) * 0.01
|
|
|
|
sampling_locations = torch.rand(N, Lq, M, L, P, 2)
|
|
|
|
attention_weights = torch.rand(N, Lq, M, L, P) + 1e-5
|
|
|
|
attention_weights /= attention_weights.sum(
|
|
|
|
-1, keepdim=True).sum(
|
|
|
|
-2, keepdim=True)
|
|
|
|
|
|
|
|
multi_scale_deformable_attn_pytorch(value.double(), shapes,
|
|
|
|
sampling_locations.double(),
|
|
|
|
attention_weights.double()).detach()
|
|
|
|
|
|
|
|
|
2022-11-16 14:08:04 +08:00
|
|
|
@pytest.mark.skipif(not IS_CUDA_AVAILABLE, reason='requires CUDA support')
|
2021-04-23 16:35:15 +08:00
|
|
|
def test_forward_equal_with_pytorch_double():
|
|
|
|
N, M, D = 1, 2, 2
|
|
|
|
Lq, L, P = 2, 2, 2
|
2022-11-16 14:08:04 +08:00
|
|
|
shapes = torch.as_tensor([(6, 4), (3, 2)], dtype=torch.long)
|
2021-04-23 16:35:15 +08:00
|
|
|
level_start_index = torch.cat((shapes.new_zeros(
|
|
|
|
(1, )), shapes.prod(1).cumsum(0)[:-1]))
|
2022-05-18 11:47:14 +08:00
|
|
|
S = sum((H * W).item() for H, W in shapes)
|
2021-04-23 16:35:15 +08:00
|
|
|
|
|
|
|
torch.manual_seed(3)
|
2022-11-16 14:08:04 +08:00
|
|
|
value = torch.rand(N, S, M, D) * 0.01
|
|
|
|
sampling_locations = torch.rand(N, Lq, M, L, P, 2)
|
|
|
|
attention_weights = torch.rand(N, Lq, M, L, P) + 1e-5
|
2021-04-23 16:35:15 +08:00
|
|
|
attention_weights /= attention_weights.sum(
|
|
|
|
-1, keepdim=True).sum(
|
|
|
|
-2, keepdim=True)
|
|
|
|
im2col_step = 2
|
|
|
|
output_pytorch = multi_scale_deformable_attn_pytorch(
|
|
|
|
value.double(), shapes, sampling_locations.double(),
|
|
|
|
attention_weights.double()).detach().cpu()
|
|
|
|
|
|
|
|
output_cuda = MultiScaleDeformableAttnFunction.apply(
|
2022-11-16 14:08:04 +08:00
|
|
|
value.cuda().double(), shapes.cuda(), level_start_index.cuda(),
|
|
|
|
sampling_locations.cuda().double(),
|
|
|
|
attention_weights.cuda().double(), im2col_step).detach().cpu()
|
2021-04-23 16:35:15 +08:00
|
|
|
assert torch.allclose(output_cuda, output_pytorch)
|
|
|
|
max_abs_err = (output_cuda - output_pytorch).abs().max()
|
|
|
|
max_rel_err = ((output_cuda - output_pytorch).abs() /
|
|
|
|
output_pytorch.abs()).max()
|
|
|
|
assert max_abs_err < 1e-18
|
|
|
|
assert max_rel_err < 1e-15
|
|
|
|
|
|
|
|
|
2022-11-16 14:08:04 +08:00
|
|
|
@pytest.mark.parametrize('device', [
|
|
|
|
pytest.param(
|
|
|
|
'cuda',
|
|
|
|
marks=pytest.mark.skipif(
|
|
|
|
not IS_CUDA_AVAILABLE, reason='requires CUDA support')),
|
|
|
|
pytest.param(
|
|
|
|
'mlu',
|
|
|
|
marks=pytest.mark.skipif(
|
|
|
|
not IS_MLU_AVAILABLE, reason='requires MLU support'))
|
|
|
|
])
|
|
|
|
def test_forward_equal_with_pytorch_float(device):
|
2021-04-23 16:35:15 +08:00
|
|
|
N, M, D = 1, 2, 2
|
|
|
|
Lq, L, P = 2, 2, 2
|
2022-11-16 14:08:04 +08:00
|
|
|
shapes = torch.as_tensor([(6, 4), (3, 2)], dtype=torch.long)
|
2021-04-23 16:35:15 +08:00
|
|
|
level_start_index = torch.cat((shapes.new_zeros(
|
|
|
|
(1, )), shapes.prod(1).cumsum(0)[:-1]))
|
2022-05-18 11:47:14 +08:00
|
|
|
S = sum((H * W).item() for H, W in shapes)
|
2021-04-23 16:35:15 +08:00
|
|
|
|
|
|
|
torch.manual_seed(3)
|
2022-11-16 14:08:04 +08:00
|
|
|
value = torch.rand(N, S, M, D) * 0.01
|
|
|
|
sampling_locations = torch.rand(N, Lq, M, L, P, 2)
|
|
|
|
attention_weights = torch.rand(N, Lq, M, L, P) + 1e-5
|
2021-04-23 16:35:15 +08:00
|
|
|
attention_weights /= attention_weights.sum(
|
|
|
|
-1, keepdim=True).sum(
|
|
|
|
-2, keepdim=True)
|
|
|
|
im2col_step = 2
|
|
|
|
output_pytorch = multi_scale_deformable_attn_pytorch(
|
|
|
|
value, shapes, sampling_locations, attention_weights).detach().cpu()
|
|
|
|
|
2022-11-16 14:08:04 +08:00
|
|
|
output_device = MultiScaleDeformableAttnFunction.apply(
|
|
|
|
value.to(device), shapes.to(device), level_start_index.to(device),
|
|
|
|
sampling_locations.to(device), attention_weights.to(device),
|
|
|
|
im2col_step).detach().cpu()
|
|
|
|
assert torch.allclose(output_device, output_pytorch, rtol=1e-2, atol=1e-3)
|
|
|
|
max_abs_err = (output_device - output_pytorch).abs().max()
|
|
|
|
max_rel_err = ((output_device - output_pytorch).abs() /
|
2021-04-23 16:35:15 +08:00
|
|
|
output_pytorch.abs()).max()
|
|
|
|
assert max_abs_err < 1e-9
|
|
|
|
assert max_rel_err < 1e-6
|
|
|
|
|
|
|
|
|
2022-11-16 14:08:04 +08:00
|
|
|
@pytest.mark.parametrize('device', [
|
|
|
|
pytest.param(
|
|
|
|
'cuda',
|
|
|
|
marks=pytest.mark.skipif(
|
|
|
|
not IS_CUDA_AVAILABLE, reason='requires CUDA support')),
|
|
|
|
pytest.param(
|
|
|
|
'mlu',
|
|
|
|
marks=pytest.mark.skipif(
|
|
|
|
not IS_MLU_AVAILABLE, reason='requires MLU support'))
|
|
|
|
])
|
|
|
|
@pytest.mark.parametrize('dtype', [
|
|
|
|
torch.float,
|
|
|
|
pytest.param(
|
|
|
|
torch.double,
|
|
|
|
marks=pytest.mark.skipif(
|
|
|
|
IS_MLU_AVAILABLE,
|
|
|
|
reason='MLU does not support for 64-bit floating point')),
|
|
|
|
torch.half
|
|
|
|
])
|
2021-06-11 18:09:31 +08:00
|
|
|
@pytest.mark.parametrize('channels', [
|
|
|
|
4,
|
|
|
|
30,
|
|
|
|
32,
|
|
|
|
64,
|
|
|
|
71,
|
|
|
|
1025,
|
|
|
|
])
|
2021-04-23 16:35:15 +08:00
|
|
|
def test_gradient_numerical(channels,
|
2022-11-16 14:08:04 +08:00
|
|
|
device,
|
|
|
|
dtype,
|
2021-04-23 16:35:15 +08:00
|
|
|
grad_value=True,
|
|
|
|
grad_sampling_loc=True,
|
|
|
|
grad_attn_weight=True):
|
|
|
|
|
|
|
|
N, M, _ = 1, 2, 2
|
|
|
|
Lq, L, P = 2, 2, 2
|
2022-11-16 14:08:04 +08:00
|
|
|
shapes = torch.as_tensor([(3, 2), (2, 1)], dtype=torch.long).to(device)
|
2021-04-23 16:35:15 +08:00
|
|
|
level_start_index = torch.cat((shapes.new_zeros(
|
|
|
|
(1, )), shapes.prod(1).cumsum(0)[:-1]))
|
2022-05-18 11:47:14 +08:00
|
|
|
S = sum((H * W).item() for H, W in shapes)
|
2021-04-23 16:35:15 +08:00
|
|
|
|
2022-11-16 14:08:04 +08:00
|
|
|
value = torch.rand(N, S, M, channels).to(device) * 0.01
|
|
|
|
sampling_locations = torch.rand(N, Lq, M, L, P, 2).to(device)
|
|
|
|
attention_weights = torch.rand(N, Lq, M, L, P).to(device) + 1e-5
|
2021-04-23 16:35:15 +08:00
|
|
|
attention_weights /= attention_weights.sum(
|
|
|
|
-1, keepdim=True).sum(
|
|
|
|
-2, keepdim=True)
|
|
|
|
im2col_step = 2
|
|
|
|
|
|
|
|
func = MultiScaleDeformableAttnFunction.apply
|
|
|
|
|
|
|
|
value.requires_grad = grad_value
|
|
|
|
sampling_locations.requires_grad = grad_sampling_loc
|
|
|
|
attention_weights.requires_grad = grad_attn_weight
|
2022-11-16 14:08:04 +08:00
|
|
|
if device == 'cuda':
|
|
|
|
dtype = torch.double
|
|
|
|
eps = 1e-6
|
|
|
|
elif device == 'mlu':
|
|
|
|
dtype = torch.float
|
|
|
|
eps = 1e-4
|
2021-05-25 13:13:05 +08:00
|
|
|
if _USING_PARROTS:
|
|
|
|
assert gradcheck(
|
2022-11-16 14:08:04 +08:00
|
|
|
func, (value.to(dtype), shapes, level_start_index,
|
|
|
|
sampling_locations.to(dtype), attention_weights.to(dtype),
|
2021-05-25 13:13:05 +08:00
|
|
|
im2col_step),
|
2022-11-16 14:08:04 +08:00
|
|
|
no_grads=[shapes, level_start_index],
|
|
|
|
eps=eps)
|
2021-05-25 13:13:05 +08:00
|
|
|
else:
|
2022-11-16 14:08:04 +08:00
|
|
|
assert gradcheck(
|
|
|
|
func, (value.to(dtype), shapes, level_start_index,
|
|
|
|
sampling_locations.to(dtype), attention_weights.to(dtype),
|
|
|
|
im2col_step),
|
|
|
|
eps=eps,
|
|
|
|
atol=1e-2)
|