Spaces:
Running
on
T4
Running
on
T4
Changed running test code inline to running it in separate script
Browse files
app.py
CHANGED
@@ -1,14 +1,8 @@
|
|
1 |
-
from __future__ import absolute_import
|
2 |
-
from __future__ import print_function
|
3 |
-
from __future__ import division
|
4 |
-
from torch.autograd import gradcheck
|
5 |
import spaces
|
6 |
import gradio as gr
|
7 |
import copy
|
8 |
import random
|
9 |
import torch
|
10 |
-
import torch.nn as nn
|
11 |
-
import time
|
12 |
import PIL
|
13 |
from PIL import Image, ImageDraw, ImageFont
|
14 |
import torchvision.transforms.functional as F
|
@@ -46,7 +40,12 @@ subprocess.run(
|
|
46 |
"pip install MultiScaleDeformableAttention-1.0-cp310-cp310-linux_x86_64.whl"
|
47 |
)
|
48 |
)
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
class AppSteps(Enum):
|
52 |
JUST_TEXT = 1
|
@@ -56,74 +55,6 @@ class AppSteps(Enum):
|
|
56 |
|
57 |
CONF_THRESH = 0.23
|
58 |
|
59 |
-
@spaces.GPU
|
60 |
-
def check_ms_deform_install():
|
61 |
-
|
62 |
-
N, M, D = 1, 2, 2
|
63 |
-
Lq, L, P = 2, 2, 2
|
64 |
-
shapes = torch.as_tensor([(6, 4), (3, 2)], dtype=torch.long).cuda()
|
65 |
-
level_start_index = torch.cat((shapes.new_zeros((1, )), shapes.prod(1).cumsum(0)[:-1]))
|
66 |
-
S = sum([(H*W).item() for H, W in shapes])
|
67 |
-
|
68 |
-
|
69 |
-
torch.manual_seed(3)
|
70 |
-
|
71 |
-
|
72 |
-
@torch.no_grad()
|
73 |
-
def check_forward_equal_with_pytorch_double():
|
74 |
-
value = torch.rand(N, S, M, D).cuda() * 0.01
|
75 |
-
sampling_locations = torch.rand(N, Lq, M, L, P, 2).cuda()
|
76 |
-
attention_weights = torch.rand(N, Lq, M, L, P).cuda() + 1e-5
|
77 |
-
attention_weights /= attention_weights.sum(-1, keepdim=True).sum(-2, keepdim=True)
|
78 |
-
im2col_step = 2
|
79 |
-
output_pytorch = ms_deform_attn_core_pytorch(value.double(), shapes, sampling_locations.double(), attention_weights.double()).detach().cpu()
|
80 |
-
output_cuda = MSDeformAttnFunction.apply(value.double(), shapes, level_start_index, sampling_locations.double(), attention_weights.double(), im2col_step).detach().cpu()
|
81 |
-
fwdok = torch.allclose(output_cuda, output_pytorch)
|
82 |
-
max_abs_err = (output_cuda - output_pytorch).abs().max()
|
83 |
-
max_rel_err = ((output_cuda - output_pytorch).abs() / output_pytorch.abs()).max()
|
84 |
-
|
85 |
-
print(f'* {fwdok} check_forward_equal_with_pytorch_double: max_abs_err {max_abs_err:.2e} max_rel_err {max_rel_err:.2e}')
|
86 |
-
|
87 |
-
|
88 |
-
@torch.no_grad()
|
89 |
-
def check_forward_equal_with_pytorch_float():
|
90 |
-
value = torch.rand(N, S, M, D).cuda() * 0.01
|
91 |
-
sampling_locations = torch.rand(N, Lq, M, L, P, 2).cuda()
|
92 |
-
attention_weights = torch.rand(N, Lq, M, L, P).cuda() + 1e-5
|
93 |
-
attention_weights /= attention_weights.sum(-1, keepdim=True).sum(-2, keepdim=True)
|
94 |
-
im2col_step = 2
|
95 |
-
output_pytorch = ms_deform_attn_core_pytorch(value, shapes, sampling_locations, attention_weights).detach().cpu()
|
96 |
-
output_cuda = MSDeformAttnFunction.apply(value, shapes, level_start_index, sampling_locations, attention_weights, im2col_step).detach().cpu()
|
97 |
-
fwdok = torch.allclose(output_cuda, output_pytorch, rtol=1e-2, atol=1e-3)
|
98 |
-
max_abs_err = (output_cuda - output_pytorch).abs().max()
|
99 |
-
max_rel_err = ((output_cuda - output_pytorch).abs() / output_pytorch.abs()).max()
|
100 |
-
|
101 |
-
print(f'* {fwdok} check_forward_equal_with_pytorch_float: max_abs_err {max_abs_err:.2e} max_rel_err {max_rel_err:.2e}')
|
102 |
-
|
103 |
-
|
104 |
-
def check_gradient_numerical(channels=4, grad_value=True, grad_sampling_loc=True, grad_attn_weight=True):
|
105 |
-
|
106 |
-
value = torch.rand(N, S, M, channels).cuda() * 0.01
|
107 |
-
sampling_locations = torch.rand(N, Lq, M, L, P, 2).cuda()
|
108 |
-
attention_weights = torch.rand(N, Lq, M, L, P).cuda() + 1e-5
|
109 |
-
attention_weights /= attention_weights.sum(-1, keepdim=True).sum(-2, keepdim=True)
|
110 |
-
im2col_step = 2
|
111 |
-
func = MSDeformAttnFunction.apply
|
112 |
-
|
113 |
-
value.requires_grad = grad_value
|
114 |
-
sampling_locations.requires_grad = grad_sampling_loc
|
115 |
-
attention_weights.requires_grad = grad_attn_weight
|
116 |
-
|
117 |
-
gradok = gradcheck(func, (value.double(), shapes, level_start_index, sampling_locations.double(), attention_weights.double(), im2col_step))
|
118 |
-
|
119 |
-
print(f'* {gradok} check_gradient_numerical(D={channels})')
|
120 |
-
|
121 |
-
check_forward_equal_with_pytorch_double()
|
122 |
-
check_forward_equal_with_pytorch_float()
|
123 |
-
|
124 |
-
for channels in [30, 32, 64, 71]:
|
125 |
-
check_gradient_numerical(channels, True, True, True)
|
126 |
-
|
127 |
# MODEL:
|
128 |
def get_args_parser():
|
129 |
"""
|
|
|
|
|
|
|
|
|
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import copy
|
4 |
import random
|
5 |
import torch
|
|
|
|
|
6 |
import PIL
|
7 |
from PIL import Image, ImageDraw, ImageFont
|
8 |
import torchvision.transforms.functional as F
|
|
|
40 |
"pip install MultiScaleDeformableAttention-1.0-cp310-cp310-linux_x86_64.whl"
|
41 |
)
|
42 |
)
|
43 |
+
|
44 |
+
subprocess.run(
|
45 |
+
shlex.split(
|
46 |
+
"python test.py"
|
47 |
+
)
|
48 |
+
)
|
49 |
|
50 |
class AppSteps(Enum):
|
51 |
JUST_TEXT = 1
|
|
|
55 |
|
56 |
CONF_THRESH = 0.23
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# MODEL:
|
59 |
def get_args_parser():
|
60 |
"""
|