test_op.py 5.09 KB
Newer Older
facebook-github-bot's avatar
facebook-github-bot committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
# ------------------------------------------------------------------------------------------------
# Deformable DETR
# Copyright (c) 2020 SenseTime. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 [see LICENSE for details]
# ------------------------------------------------------------------------------------------------
# Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0
# ------------------------------------------------------------------------------------------------

import io
import unittest
from functools import wraps

Yanghan Wang's avatar
Yanghan Wang committed
15
16
17
18
19
import torch
from detr.functions.ms_deform_attn_func import (
    MSDeformAttnFunction,
    ms_deform_attn_core_pytorch,
)
facebook-github-bot's avatar
facebook-github-bot committed
20
21
22
23
24
25
26
27
28
from torch.autograd import gradcheck

USE_CUDA = torch.cuda.device_count() > 0


N, M, D = 1, 2, 2
Lq, L, P = 2, 2, 2
if USE_CUDA:
    shapes = torch.as_tensor([(6, 4), (3, 2)], dtype=torch.long).cuda()
Yanghan Wang's avatar
Yanghan Wang committed
29
30
31
32
    level_start_index = torch.cat(
        (shapes.new_zeros((1,)), shapes.prod(1).cumsum(0)[:-1])
    )
    S = sum([(H * W).item() for H, W in shapes])
facebook-github-bot's avatar
facebook-github-bot committed
33
34
35

torch.manual_seed(3)

Yanghan Wang's avatar
Yanghan Wang committed
36

facebook-github-bot's avatar
facebook-github-bot committed
37
class Tester(unittest.TestCase):
Yanghan Wang's avatar
Yanghan Wang committed
38
    @unittest.skipIf(not USE_CUDA, "CI does not have gpu")
facebook-github-bot's avatar
facebook-github-bot committed
39
40
41
42
43
    @torch.no_grad()
    def test_forward_equal_with_pytorch_double(self):
        value = torch.rand(N, S, M, D).cuda() * 0.01
        sampling_locations = torch.rand(N, Lq, M, L, P, 2).cuda()
        attention_weights = torch.rand(N, Lq, M, L, P).cuda() + 1e-5
Yanghan Wang's avatar
Yanghan Wang committed
44
45
46
        attention_weights /= attention_weights.sum(-1, keepdim=True).sum(
            -2, keepdim=True
        )
facebook-github-bot's avatar
facebook-github-bot committed
47
        im2col_step = 2
Yanghan Wang's avatar
Yanghan Wang committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        output_pytorch = (
            ms_deform_attn_core_pytorch(
                value.double(),
                shapes,
                sampling_locations.double(),
                attention_weights.double(),
            )
            .detach()
            .cpu()
        )
        output_cuda = (
            MSDeformAttnFunction.apply(
                value.double(),
                shapes,
                level_start_index,
                sampling_locations.double(),
                attention_weights.double(),
                im2col_step,
            )
            .detach()
            .cpu()
        )
facebook-github-bot's avatar
facebook-github-bot committed
70
71
        fwdok = torch.allclose(output_cuda, output_pytorch)
        max_abs_err = (output_cuda - output_pytorch).abs().max()
Yanghan Wang's avatar
Yanghan Wang committed
72
73
74
        max_rel_err = (
            (output_cuda - output_pytorch).abs() / output_pytorch.abs()
        ).max()
facebook-github-bot's avatar
facebook-github-bot committed
75

Yanghan Wang's avatar
Yanghan Wang committed
76
77
78
        print(
            f"* {fwdok} test_forward_equal_with_pytorch_double: max_abs_err {max_abs_err:.2e} max_rel_err {max_rel_err:.2e}"
        )
facebook-github-bot's avatar
facebook-github-bot committed
79

Yanghan Wang's avatar
Yanghan Wang committed
80
    @unittest.skipIf(not USE_CUDA, "CI does not have gpu")
facebook-github-bot's avatar
facebook-github-bot committed
81
82
83
84
85
    @torch.no_grad()
    def test_forward_equal_with_pytorch_float(self):
        value = torch.rand(N, S, M, D).cuda() * 0.01
        sampling_locations = torch.rand(N, Lq, M, L, P, 2).cuda()
        attention_weights = torch.rand(N, Lq, M, L, P).cuda() + 1e-5
Yanghan Wang's avatar
Yanghan Wang committed
86
87
88
        attention_weights /= attention_weights.sum(-1, keepdim=True).sum(
            -2, keepdim=True
        )
facebook-github-bot's avatar
facebook-github-bot committed
89
        im2col_step = 2
Yanghan Wang's avatar
Yanghan Wang committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
        output_pytorch = (
            ms_deform_attn_core_pytorch(
                value, shapes, sampling_locations, attention_weights
            )
            .detach()
            .cpu()
        )
        output_cuda = (
            MSDeformAttnFunction.apply(
                value,
                shapes,
                level_start_index,
                sampling_locations,
                attention_weights,
                im2col_step,
            )
            .detach()
            .cpu()
        )
facebook-github-bot's avatar
facebook-github-bot committed
109
110
        fwdok = torch.allclose(output_cuda, output_pytorch, rtol=1e-2, atol=1e-3)
        max_abs_err = (output_cuda - output_pytorch).abs().max()
Yanghan Wang's avatar
Yanghan Wang committed
111
112
113
        max_rel_err = (
            (output_cuda - output_pytorch).abs() / output_pytorch.abs()
        ).max()
facebook-github-bot's avatar
facebook-github-bot committed
114

Yanghan Wang's avatar
Yanghan Wang committed
115
116
117
        print(
            f"* {fwdok} test_forward_equal_with_pytorch_float: max_abs_err {max_abs_err:.2e} max_rel_err {max_rel_err:.2e}"
        )
facebook-github-bot's avatar
facebook-github-bot committed
118

Yanghan Wang's avatar
Yanghan Wang committed
119
120
121
122
    @unittest.skipIf(not USE_CUDA, "CI does not have gpu")
    def test_gradient_numerical(
        self, channels=4, grad_value=True, grad_sampling_loc=True, grad_attn_weight=True
    ):
facebook-github-bot's avatar
facebook-github-bot committed
123
124
125
126

        value = torch.rand(N, S, M, channels).cuda() * 0.01
        sampling_locations = torch.rand(N, Lq, M, L, P, 2).cuda()
        attention_weights = torch.rand(N, Lq, M, L, P).cuda() + 1e-5
Yanghan Wang's avatar
Yanghan Wang committed
127
128
129
        attention_weights /= attention_weights.sum(-1, keepdim=True).sum(
            -2, keepdim=True
        )
facebook-github-bot's avatar
facebook-github-bot committed
130
131
132
133
134
135
136
        im2col_step = 2
        func = MSDeformAttnFunction.apply

        value.requires_grad = grad_value
        sampling_locations.requires_grad = grad_sampling_loc
        attention_weights.requires_grad = grad_attn_weight

Yanghan Wang's avatar
Yanghan Wang committed
137
138
139
140
141
142
143
144
145
146
147
        gradok = gradcheck(
            func,
            (
                value.double(),
                shapes,
                level_start_index,
                sampling_locations.double(),
                attention_weights.double(),
                im2col_step,
            ),
        )
facebook-github-bot's avatar
facebook-github-bot committed
148

Yanghan Wang's avatar
Yanghan Wang committed
149
        print(f"* {gradok} test_gradient_numerical(D={channels})")
facebook-github-bot's avatar
facebook-github-bot committed
150
151


Yanghan Wang's avatar
Yanghan Wang committed
152
if __name__ == "__main__":
facebook-github-bot's avatar
facebook-github-bot committed
153
    unittest.main()