Skip to content

Commit 311f328

Browse files
authored
Merge pull request #602 from SrivastavaKshitij/fix_test_and_doc
Test enhancement and doc correction for QAT
2 parents eb07e6d + 4393e4f commit 311f328

File tree

3 files changed

+59
-33
lines changed

3 files changed

+59
-33
lines changed

examples/contrib/quantization_aware_training/README.md

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,9 @@ RUN add-apt-repository ppa:git-core/ppa && \
2525
2626
RUN pip install termcolor graphviz
2727
28-
## If you have followed instructions on main README.md file to install torch2trt using scripts/build_contrib.sh
29-
## You dont require rest of the steps
30-
31-
RUN git clone https://github.com/NVIDIA/TensorRT.git /sw/TensorRT/
32-
33-
##Make sure that patch file is under the same folder where dockerfile is being called
34-
35-
ADD pytorch_nvidia_quantization.patch /sw/TensorRT
36-
37-
RUN cd /sw/TensorRT/ && \
38-
git sparse-checkout init --cone && \
39-
git sparse-checkout set /tools/pytorch-quantization/ && \
40-
git apply --reject --whitespace=fix pytorch_nvidia_quantization.patch && \
41-
cd tools/pytorch-quantization/ && \
42-
python setup.py install
43-
44-
RUN git clone https://github.com/NVIDIA-AI-IOT/torch2trt.git /sw/TensorRT/ && \
45-
cd /sw/TensorRT/ && \
46-
git fetch origin pull/514/head:PR514 && \
47-
git checkout PR514 && \
48-
python setup.py install --plugins
28+
RUN git clone https://github.com/NVIDIA-AI-IOT/torch2trt.git /sw/torch2trt/ && \
29+
cd /sw/torch2trt/scripts && \
30+
bash build_contrib.sh
4931
5032
```
5133

torch2trt/converters/interpolate.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,34 +92,36 @@ def convert_interpolate_trt7(ctx):
9292

9393

9494
class Interpolate(torch.nn.Module):
95-
def __init__(self, size, mode, align_corners):
95+
def __init__(self, size=None,scale_factor=None, mode=None, align_corners=None):
9696
super(Interpolate, self).__init__()
97+
## Use either size or scale factor.
9798
self.size = size
99+
self.scale_factor = scale_factor
98100
self.mode = mode
99101
self.align_corners = align_corners
100102

101103
def forward(self, x):
102-
return F.interpolate(x, self.size, mode=self.mode, align_corners=self.align_corners)
104+
return F.interpolate(x, size=self.size, scale_factor=self.scale_factor,mode=self.mode, align_corners=self.align_corners)
103105

104106

105107
@add_module_test(torch.float32, torch.device('cuda'), [(1, 10, 112, 112)], enabled=trt_version() < '7.1' and has_interpolate_plugin())
106108
def test_interpolate_nearest():
107-
return Interpolate((224, 224), 'nearest', None)
109+
return Interpolate(size=(224, 224), mode='nearest', align_corners=None)
108110

109111

110112
@add_module_test(torch.float32, torch.device('cuda'), [(1, 10, 112, 112)], enabled=trt_version() < '7.1' and has_interpolate_plugin())
111113
def test_interpolate_bilinear():
112-
return Interpolate((224, 224), 'bilinear', False)
114+
return Interpolate(size=(224, 224), mode= 'bilinear', align_corners=False)
113115

114116

115117
@add_module_test(torch.float32, torch.device('cuda'), [(1, 10, 112, 112)], enabled=trt_version() < '7.1' and has_interpolate_plugin())
116118
def test_interpolate_bicubic():
117-
return Interpolate((224, 224), 'bicubic', False)
119+
return Interpolate(size=(224, 224), mode='bicubic',align_corners= False)
118120

119121

120122
@add_module_test(torch.float32, torch.device('cuda'), [(1, 10, 112, 112)], enabled=trt_version() < '7.1' and has_interpolate_plugin())
121123
def test_interpolate_area():
122-
return Interpolate((56, 56), 'area', None)
124+
return Interpolate(size=(56, 56), mode='area',align_corners= None)
123125

124126
@add_module_test(torch.float32, torch.device('cuda'), [(1, 10, 112, 112)], enabled=trt_version() < '7.1' and has_interpolate_plugin())
125127
def test_upsample_scale_factor2():
@@ -135,7 +137,11 @@ def test_bilinear_mode():
135137

136138
@add_module_test(torch.float32, torch.device('cuda'), [(1,3,12,12)], enabled=trt_version() >= '7.1')
137139
def test_align_corner():
138-
return torch.nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
140+
return torch.nn.Upsample(scale_factor=2.0, mode="bilinear", align_corners=True)
141+
142+
@add_module_test(torch.float32, torch.device('cuda'), [(1,3,12,12)], enabled=trt_version() >= '7.1')
143+
def test_align_corner_functional():
144+
return Interpolate(scale_factor=2.0, mode="bilinear", align_corners=True)
139145

140146
@add_module_test(torch.float32, torch.device('cuda'), [(1,5,13,13)], enabled=trt_version() >= '7.1')
141147
def test_bilinear_mode_odd_input_shape():

torch2trt/test.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66
import runpy
77
import traceback
88
from termcolor import colored
9+
import math
10+
import numpy as np
11+
12+
def pSNR(model_op,trt_op):
13+
#model_op = model_op.cpu().detach().numpy().flatten()
14+
#trt_op = trt_op.cpu().detach().numpy().flatten()
15+
16+
# Calculating Mean Squared Error
17+
mse = np.sum(np.square(model_op - trt_op)) / len(model_op)
18+
# Calcuating peak signal to noise ratio
19+
try:
20+
psnr_db = 20 * math.log10(np.max(abs(model_op))) - 10 * math.log10(mse)
21+
except:
22+
psnr_db = np.nan
23+
return mse,psnr_db
24+
925

1026

1127
def run(self):
@@ -49,6 +65,24 @@ def run(self):
4965

5066
if max_error_i > max_error:
5167
max_error = max_error_i
68+
69+
## calculate peak signal to noise ratio
70+
assert(len(outputs) == len(outputs_trt))
71+
72+
## Check if output is boolean
73+
# if yes, then dont calculate psnr
74+
if outputs[0].dtype == torch.bool:
75+
mse = np.nan
76+
psnr_db = np.nan
77+
else:
78+
model_op = []
79+
trt_op = []
80+
for i in range(len(outputs)):
81+
model_op.extend(outputs[i].detach().cpu().numpy().flatten())
82+
trt_op.extend(outputs_trt[i].detach().cpu().numpy().flatten())
83+
model_op = np.array(model_op)
84+
trt_op = np.array(trt_op)
85+
mse,psnr_db = pSNR(model_op,trt_op)
5286

5387
# benchmark pytorch throughput
5488
torch.cuda.current_stream().synchronize()
@@ -90,7 +124,7 @@ def run(self):
90124

91125
ms_trt = 1000.0 * (t1 - t0) / 50.0
92126

93-
return max_error, fps, fps_trt, ms, ms_trt
127+
return max_error,psnr_db,mse, fps, fps_trt, ms, ms_trt
94128

95129

96130
if __name__ == '__main__':
@@ -106,7 +140,7 @@ def run(self):
106140
for include in args.include:
107141
runpy.run_module(include)
108142

109-
num_tests, num_success, num_tolerance, num_error = 0, 0, 0, 0
143+
num_tests, num_success, num_tolerance, num_error, num_tolerance_psnr = 0, 0, 0, 0, 0
110144
for test in MODULE_TESTS:
111145

112146
# filter by module name
@@ -120,14 +154,17 @@ def run(self):
120154
if args.use_onnx:
121155
test.torch2trt_kwargs.update({'use_onnx': True})
122156

123-
max_error, fps, fps_trt, ms, ms_trt = run(test)
157+
max_error,psnr_db,mse, fps, fps_trt, ms, ms_trt = run(test)
124158

125159
# write entry
126-
line = '| %s | %s | %s | %s | %.2E | %.3g | %.3g | %.3g | %.3g |' % (name, test.dtype.__repr__().split('.')[-1], str(test.input_shapes), str(test.torch2trt_kwargs), max_error, fps, fps_trt, ms, ms_trt)
160+
line = '| %70s | %s | %25s | %s | %.2E | %.2f | %.2E | %.3g | %.3g | %.3g | %.3g |' % (name, test.dtype.__repr__().split('.')[-1], str(test.input_shapes), str(test.torch2trt_kwargs), max_error,psnr_db,mse, fps, fps_trt, ms, ms_trt)
127161

128162
if args.tolerance >= 0 and max_error > args.tolerance:
129163
print(colored(line, 'yellow'))
130164
num_tolerance += 1
165+
elif psnr_db < 100:
166+
print(colored(line, 'magenta'))
167+
num_tolerance_psnr +=1
131168
else:
132169
print(line)
133170
num_success += 1
@@ -144,4 +181,5 @@ def run(self):
144181
print('NUM_TESTS: %d' % num_tests)
145182
print('NUM_SUCCESSFUL_CONVERSION: %d' % num_success)
146183
print('NUM_FAILED_CONVERSION: %d' % num_error)
147-
print('NUM_ABOVE_TOLERANCE: %d' % num_tolerance)
184+
print('NUM_ABOVE_TOLERANCE: %d' % num_tolerance)
185+
print('NUM_pSNR_TOLERANCE: %d' %num_tolerance_psnr)

0 commit comments

Comments
 (0)