test_trt_matmul_quant_dequant.py 11.9 KB
Newer Older
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
16

17 18
import numpy as np
from quant_dequant_test import QuantDequantTest
19

20
import paddle
21
import paddle.nn.functional as F
22 23
from paddle import fluid
from paddle.fluid import core
24
from paddle.fluid.core import AnalysisConfig, PassVersionChecker
25 26 27 28 29 30 31


class TensorRTMatMulQuantDequantDims3Test(QuantDequantTest):
    def setUp(self):
        self.set_params()

        def network():
32
            self.data = paddle.static.data(
33 34
                name='data', shape=[1, 28, 28], dtype='float32'
            )
35 36 37
            self.label = paddle.static.data(
                name='label', shape=[1, 1], dtype='int64'
            )
K
kangguangli 已提交
38
            matmul_out = paddle.matmul(
39 40 41 42 43
                x=self.data,
                y=self.data,
                transpose_x=self.transpose_x,
                transpose_y=self.transpose_y,
            )
K
kangguangli 已提交
44
            matmul_out = paddle.scale(matmul_out, scale=self.alpha)
C
Charles-hit 已提交
45 46
            fc_out = paddle.static.nn.fc(
                x=matmul_out,
47 48 49
                size=10,
                num_flatten_dims=1,
                bias_attr=False,
C
Charles-hit 已提交
50
                activation=None,
51
            )
52
            result = F.relu(fc_out)
53 54 55 56 57 58
            loss = paddle.nn.functional.cross_entropy(
                input=result,
                label=self.label,
                reduction='none',
                use_softmax=False,
            )
59
            avg_loss = paddle.mean(loss)
60 61 62 63 64
            return avg_loss, result

        self.main_program.random_seed = 2
        self.startup_program.random_seed = 2
        self.test_main_program.random_seed = 2
65
        # self.test_startup_program.random_seed = 2
66 67 68 69 70 71
        with fluid.unique_name.guard():
            with fluid.program_guard(self.main_program, self.startup_program):
                self.loss, result = network()
                opt = fluid.optimizer.Adam(learning_rate=0.0001)
                opt.minimize(self.loss)
        with fluid.unique_name.guard():
72 73 74
            with fluid.program_guard(
                self.test_main_program, self.startup_program
            ):
75 76 77 78 79
                network()
        self.feeds = {"data": np.random.random([1, 28, 28]).astype("float32")}
        self.fetch_list = [result]
        self.enable_trt = True
        self.trt_parameters = TensorRTMatMulQuantDequantDims3Test.TensorRTParam(
80 81
            1 << 30, 32, 0, AnalysisConfig.Precision.Int8, False, False
        )
82 83 84 85 86 87 88 89
        self.dynamic_shape_params = (
            TensorRTMatMulQuantDequantDims3Test.DynamicShapeParam(
                {'data': [1, 28, 28]},
                {'data': [4, 28, 28]},
                {'data': [3, 28, 28]},
                False,
            )
        )
90 91 92 93 94 95 96 97 98
        self.activation_quantize_type = 'moving_average_abs_max'
        self.weight_quantize_type = 'channel_wise_abs_max'

    def set_params(self):
        self.transpose_x = False
        self.transpose_y = False
        self.alpha = 1.0

    def test_check_output(self):
99
        # self.quant_dequant()
100 101
        if core.is_compiled_with_cuda():
            use_gpu = True
102 103 104
            self.check_output_with_option(
                use_gpu, atol=1, flatten=False, rtol=1e-1
            )
105
            self.assertTrue(
106 107
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')
            )
108 109 110


class TensorRTMatMulQuantDequantDims3TransposeXTest(
111 112
    TensorRTMatMulQuantDequantDims3Test
):
113 114 115
    def set_params(self):
        self.transpose_x = True
        self.transpose_y = False
116
        self.alpha = 2.1
117 118 119


class TensorRTMatMulQuantDequantDims3TransposeYTest(
120 121
    TensorRTMatMulQuantDequantDims3Test
):
122 123 124
    def set_params(self):
        self.transpose_x = False
        self.transpose_y = True
125
        self.alpha = 3.9
126 127 128


class TensorRTMatMulQuantDequantDims3TransposeXYTest(
129 130
    TensorRTMatMulQuantDequantDims3Test
):
131 132 133
    def set_params(self):
        self.transpose_x = True
        self.transpose_y = True
134
        self.alpha = 8.4
135 136 137 138 139 140 141


class TensorRTMatMulQuantDequantDims4Test(QuantDequantTest):
    def setUp(self):
        self.set_params()

        def network():
142
            self.data = paddle.static.data(
143 144
                name='data', shape=[1, 28, 28], dtype='float32'
            )
145 146 147
            self.label = paddle.static.data(
                name='label', shape=[1, 1], dtype='int64'
            )
148
            reshape_out = paddle.reshape(self.data, shape=[0, 4, 14, 14])
K
kangguangli 已提交
149
            matmul_out = paddle.matmul(
150 151 152 153 154
                x=reshape_out,
                y=reshape_out,
                transpose_x=self.transpose_x,
                transpose_y=self.transpose_y,
            )
K
kangguangli 已提交
155
            matmul_out = paddle.scale(matmul_out, scale=self.alpha)
156
            out = paddle.static.nn.batch_norm(matmul_out, is_test=True)
C
Charles-hit 已提交
157 158
            fc_out = paddle.static.nn.fc(
                x=matmul_out,
159 160 161
                size=10,
                num_flatten_dims=1,
                bias_attr=False,
C
Charles-hit 已提交
162
                activation=None,
163
            )
164
            result = F.relu(fc_out)
165 166 167 168 169 170
            loss = paddle.nn.functional.cross_entropy(
                input=result,
                label=self.label,
                reduction='none',
                use_softmax=False,
            )
171
            avg_loss = paddle.mean(loss)
172 173 174 175 176
            return avg_loss, result

        self.main_program.random_seed = 2
        self.startup_program.random_seed = 2
        self.test_main_program.random_seed = 2
177
        # self.test_startup_program.random_seed = 2
178 179 180 181 182 183
        with fluid.unique_name.guard():
            with fluid.program_guard(self.main_program, self.startup_program):
                self.loss, result = network()
                opt = fluid.optimizer.Adam(learning_rate=0.0001)
                opt.minimize(self.loss)
        with fluid.unique_name.guard():
184 185 186
            with fluid.program_guard(
                self.test_main_program, self.startup_program
            ):
187 188 189 190 191
                network()
        self.feeds = {"data": np.random.random([1, 28, 28]).astype("float32")}
        self.fetch_list = [result]
        self.enable_trt = True
        self.trt_parameters = TensorRTMatMulQuantDequantDims4Test.TensorRTParam(
192 193
            1 << 30, 32, 0, AnalysisConfig.Precision.Int8, False, False
        )
194 195 196 197 198 199 200 201
        self.dynamic_shape_params = (
            TensorRTMatMulQuantDequantDims4Test.DynamicShapeParam(
                {'data': [1, 28, 28]},
                {'data': [4, 28, 28]},
                {'data': [3, 28, 28]},
                False,
            )
        )
202 203 204 205 206 207 208 209 210
        self.activation_quantize_type = 'moving_average_abs_max'
        self.weight_quantize_type = 'channel_wise_abs_max'

    def set_params(self):
        self.transpose_x = False
        self.transpose_y = False
        self.alpha = 1.0

    def test_check_output(self):
211
        # self.quant_dequant()
212 213
        if core.is_compiled_with_cuda():
            use_gpu = True
214 215 216
            self.check_output_with_option(
                use_gpu, atol=1, flatten=False, rtol=1e-1
            )
217
            self.assertTrue(
218 219
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')
            )
220 221 222


class TensorRTMatMulQuantDequantDims4TransposeXTest(
223 224
    TensorRTMatMulQuantDequantDims4Test
):
225 226 227
    def set_params(self):
        self.transpose_x = True
        self.transpose_y = False
228
        self.alpha = 3.2
229 230 231


class TensorRTMatMulQuantDequantDims4TransposeYTest(
232 233
    TensorRTMatMulQuantDequantDims4Test
):
234 235 236
    def set_params(self):
        self.transpose_x = False
        self.transpose_y = True
237
        self.alpha = 7.5
238 239 240


class TensorRTMatMulQuantDequantDims4TransposeXYTest(
241 242
    TensorRTMatMulQuantDequantDims4Test
):
243 244 245
    def set_params(self):
        self.transpose_x = True
        self.transpose_y = True
246
        self.alpha = 11.2
247 248


249 250 251 252 253
class TensorRTMatMulQuantDequantDims3DynamicTest(QuantDequantTest):
    def setUp(self):
        self.set_params()

        def network():
254
            self.data = paddle.static.data(
255 256
                name='data', shape=[-1, 28, 28], dtype='float32'
            )
257 258 259
            self.label = paddle.static.data(
                name='label', shape=[1, 1], dtype='int64'
            )
K
kangguangli 已提交
260
            matmul_out = paddle.matmul(
261 262 263 264 265
                x=self.data,
                y=self.data,
                transpose_x=self.transpose_x,
                transpose_y=self.transpose_y,
            )
K
kangguangli 已提交
266
            matmul_out = paddle.scale(matmul_out, scale=self.alpha)
267
            out = paddle.static.nn.batch_norm(matmul_out, is_test=True)
C
Charles-hit 已提交
268 269
            fc_out = paddle.static.nn.fc(
                x=matmul_out,
270 271 272
                size=10,
                num_flatten_dims=1,
                bias_attr=False,
C
Charles-hit 已提交
273
                activation=None,
274
            )
275
            result = F.relu(fc_out)
276 277 278 279 280 281
            loss = paddle.nn.functional.cross_entropy(
                input=result,
                label=self.label,
                reduction='none',
                use_softmax=False,
            )
282
            avg_loss = paddle.mean(loss)
283 284 285 286 287
            return avg_loss, result

        self.main_program.random_seed = 2
        self.startup_program.random_seed = 2
        self.test_main_program.random_seed = 2
288
        # self.test_startup_program.random_seed = 2
289 290 291 292 293 294
        with fluid.unique_name.guard():
            with fluid.program_guard(self.main_program, self.startup_program):
                self.loss, result = network()
                opt = fluid.optimizer.Adam(learning_rate=0.0001)
                opt.minimize(self.loss)
        with fluid.unique_name.guard():
295 296 297
            with fluid.program_guard(
                self.test_main_program, self.startup_program
            ):
298 299 300 301
                network()
        self.feeds = {"data": np.random.random([3, 28, 28]).astype("float32")}
        self.fetch_list = [result]
        self.enable_trt = True
302 303 304 305 306 307 308 309 310 311 312 313 314
        self.trt_parameters = (
            TensorRTMatMulQuantDequantDims3DynamicTest.TensorRTParam(
                1 << 30, 32, 0, AnalysisConfig.Precision.Int8, False, False
            )
        )
        self.dynamic_shape_params = (
            TensorRTMatMulQuantDequantDims3DynamicTest.DynamicShapeParam(
                {'data': [1, 28, 28]},
                {'data': [4, 28, 28]},
                {'data': [3, 28, 28]},
                False,
            )
        )
315 316 317
        self.activation_quantize_type = 'moving_average_abs_max'
        self.weight_quantize_type = 'channel_wise_abs_max'

318 319 320
    def set_params(self):
        self.transpose_x = False
        self.transpose_y = False
321 322 323
        self.alpha = 1.0

    def test_check_output(self):
324
        # self.quant_dequant()
325 326
        if core.is_compiled_with_cuda():
            use_gpu = True
327 328 329
            self.check_output_with_option(
                use_gpu, atol=1, flatten=False, rtol=1e-1
            )
330
            self.assertTrue(
331 332
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')
            )
333 334 335


class TensorRTMatMulQuantDequantDims4TransposeXDynamicTest(
336 337
    TensorRTMatMulQuantDequantDims3DynamicTest
):
338 339 340
    def set_params(self):
        self.transpose_x = True
        self.transpose_y = False
341 342 343
        self.alpha = 2.0


344
class TensorRTMatMulQuantDequantDims4TransposeYDynamicTest(
345 346
    TensorRTMatMulQuantDequantDims3DynamicTest
):
347 348 349 350 351 352 353
    def set_params(self):
        self.transpose_x = False
        self.transpose_y = True
        self.alpha = 2.2


class TensorRTMatMulQuantDequantDims4TransposeXYDynamicTest(
354 355
    TensorRTMatMulQuantDequantDims3DynamicTest
):
356 357 358 359 360 361
    def set_params(self):
        self.transpose_x = True
        self.transpose_y = True
        self.alpha = 7.8


362 363
if __name__ == "__main__":
    unittest.main()