# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

from __future__ import print_function

import unittest
import numpy as np
from inference_pass_test import InferencePassTest
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.core import PassVersionChecker
from paddle.fluid.core import AnalysisConfig


class TRTReduceMeanTest(InferencePassTest):
    def setUp(self):
        with fluid.program_guard(self.main_program, self.startup_program):
            data = fluid.data(
                name="data", shape=[-1, 3, -1, -1], dtype="float32")
            reduce_mean = fluid.layers.reduce_mean(
                data, dim=[2, -1], keep_dim=True)
            out = fluid.layers.batch_norm(reduce_mean, is_test=True)

        self.feeds = {
            "data": np.random.random([3, 3, 56, 56]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TRTReduceMeanTest.TensorRTParam(
            1 << 30, 32, 1, AnalysisConfig.Precision.Float32, False, False)
        self.fetch_list = [out]
        self.dynamic_shape_params = TRTReduceMeanTest.DynamicShapeParam({
            'data': [1, 3, 16, 16]
        }, {'data': [3, 3, 56, 56]}, {'data': [3, 3, 56, 56]}, False)

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True)
            self.assertTrue(
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


class TRTReduceMeanAllNoBatchTest(InferencePassTest):
    def setUp(self):
        with fluid.program_guard(self.main_program, self.startup_program):
            data = fluid.data(
                name="data", shape=[-1, 3, -1, -1], dtype="float32")
            reduce_mean = fluid.layers.reduce_mean(data, keep_dim=True)
            out = fluid.layers.batch_norm(reduce_mean, is_test=True)

        self.feeds = {
            "data": np.random.random([3, 3, 56, 56]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TRTReduceMeanAllNoBatchTest.TensorRTParam(
            1 << 30, 32, 1, AnalysisConfig.Precision.Float32, False, False)
        self.fetch_list = [out]
        self.dynamic_shape_params = TRTReduceMeanAllNoBatchTest.DynamicShapeParam(
            {
                'data': [1, 3, 16, 16]
            }, {'data': [3, 3, 56, 56]}, {'data': [3, 3, 56, 56]}, False)

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True)
            self.assertTrue(
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


class TRTReduceMeanTestFP16(InferencePassTest):
    def setUp(self):
        with fluid.program_guard(self.main_program, self.startup_program):
            data = fluid.data(
                name="data", shape=[-1, 3, -1, -1], dtype="float32")
            reduce_mean = fluid.layers.reduce_mean(
                data, dim=[2, -1], keep_dim=True)
            out = fluid.layers.batch_norm(reduce_mean, is_test=True)

        self.feeds = {
            "data": np.random.random([3, 3, 56, 56]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TRTReduceMeanTestFP16.TensorRTParam(
            1 << 30, 32, 1, AnalysisConfig.Precision.Half, False, False)
        self.fetch_list = [out]
        self.dynamic_shape_params = TRTReduceMeanTestFP16.DynamicShapeParam({
            'data': [1, 3, 16, 16]
        }, {'data': [3, 3, 56, 56]}, {'data': [3, 3, 56, 56]}, False)

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True)
            self.assertTrue(
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


class TRTReduceMeanAllTest(InferencePassTest):
    def setUp(self):
        with fluid.program_guard(self.main_program, self.startup_program):
            data = fluid.data(
                name="data", shape=[-1, 3, 56, 56], dtype="float32")
            reduce_mean = fluid.layers.reduce_mean(data, keep_dim=True)
            out = fluid.layers.batch_norm(reduce_mean, is_test=True)

        self.feeds = {
            "data": np.random.random([3, 3, 56, 56]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TRTReduceMeanAllTest.TensorRTParam(
            1 << 30, 32, 1, AnalysisConfig.Precision.Float32, False, False)
        self.fetch_list = [out]
        self.dynamic_shape_params = TRTReduceMeanAllTest.DynamicShapeParam({
            'data': [1, 3, 56, 56]
        }, {'data': [3, 3, 56, 56]}, {'data': [3, 3, 56, 56]}, False)

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True)
            self.assertTrue(
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


class TRTReduceMeanTestStatic(InferencePassTest):
    def setUp(self):
        with fluid.program_guard(self.main_program, self.startup_program):
            data = fluid.data(
                name="data", shape=[3, 3, 56, 56], dtype="float32")
            reduce_mean = fluid.layers.reduce_mean(
                data, dim=[2, -1], keep_dim=True)
            out = fluid.layers.batch_norm(reduce_mean, is_test=True)

        self.feeds = {
            "data": np.random.random([3, 3, 56, 56]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TRTReduceMeanTestStatic.TensorRTParam(
            1 << 30, 32, 1, AnalysisConfig.Precision.Float32, False, False)
        self.fetch_list = [out]

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True)
            self.assertTrue(
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


class TRTReduceMeanStaticAllTest(InferencePassTest):
    def setUp(self):
        with fluid.program_guard(self.main_program, self.startup_program):
            data = fluid.data(
                name="data", shape=[4, 3, 56, 56], dtype="float32")
            reduce_mean = fluid.layers.reduce_mean(data, keep_dim=True)
            out = fluid.layers.batch_norm(reduce_mean, is_test=True)

        self.feeds = {
            "data": np.random.random([4, 3, 56, 56]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TRTReduceMeanStaticAllTest.TensorRTParam(
            1 << 30, 32, 1, AnalysisConfig.Precision.Float32, False, False)
        self.fetch_list = [out]

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True)
            self.assertTrue(
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


class TRTReduceMeanStaticFP16(InferencePassTest):
    def setUp(self):
        with fluid.program_guard(self.main_program, self.startup_program):
            data = fluid.data(
                name="data", shape=[4, 3, 56, 56], dtype="float32")
            reduce_mean = fluid.layers.reduce_mean(data, keep_dim=True)
            out = fluid.layers.batch_norm(reduce_mean, is_test=True)

        self.feeds = {
            "data": np.random.random([4, 3, 56, 56]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TRTReduceMeanStaticFP16.TensorRTParam(
            1 << 30, 32, 1, AnalysisConfig.Precision.Half, False, False)
        self.fetch_list = [out]

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True)
            self.assertTrue(
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


class TRTReduceMeanFP16Static(InferencePassTest):
    def setUp(self):
        with fluid.program_guard(self.main_program, self.startup_program):
            data = fluid.data(
                name="data", shape=[4, 3, 56, 56], dtype="float32")
            reduce_mean = fluid.layers.reduce_mean(data, keep_dim=True)
            out = fluid.layers.batch_norm(reduce_mean, is_test=True)

        self.feeds = {
            "data": np.random.random([4, 3, 56, 56]).astype("float32"),
        }
        self.enable_trt = True
        self.trt_parameters = TRTReduceMeanFP16Static.TensorRTParam(
            1 << 30, 32, 1, AnalysisConfig.Precision.Half, True, False)
        self.fetch_list = [out]

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True)
            self.assertTrue(
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass'))


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

[(235, 'unittest.main', 'unittest.main', False, 'import unittest\n'), (47, 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', True, 'import paddle.fluid.core as core\n'), (75, 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', True, 'import paddle.fluid.core as core\n'), (103, 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', True, 'import paddle.fluid.core as core\n'), (130, 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', True, 'import paddle.fluid.core as core\n'), (155, 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', True, 'import paddle.fluid.core as core\n'), (179, 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', True, 'import paddle.fluid.core as core\n'), (203, 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', True, 'import paddle.fluid.core as core\n'), (227, 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', True, 'import paddle.fluid.core as core\n'), (28, 'paddle.fluid.program_guard', 'fluid.program_guard', True, 'import paddle.fluid as fluid\n'), (29, 'paddle.fluid.data', 'fluid.data', True, 'import paddle.fluid as fluid\n'), (31, 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', True, 'import paddle.fluid as fluid\n'), (33, 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', True, 'import paddle.fluid as fluid\n'), (56, 'paddle.fluid.program_guard', 'fluid.program_guard', True, 'import paddle.fluid as fluid\n'), (57, 'paddle.fluid.data', 'fluid.data', True, 'import paddle.fluid as fluid\n'), (59, 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', True, 'import paddle.fluid as fluid\n'), (60, 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', True, 'import paddle.fluid as fluid\n'), (84, 'paddle.fluid.program_guard', 'fluid.program_guard', True, 'import paddle.fluid as fluid\n'), (85, 'paddle.fluid.data', 'fluid.data', True, 'import paddle.fluid as fluid\n'), (87, 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', True, 'import paddle.fluid as fluid\n'), (89, 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', True, 'import paddle.fluid as fluid\n'), (112, 'paddle.fluid.program_guard', 'fluid.program_guard', True, 'import paddle.fluid as fluid\n'), (113, 'paddle.fluid.data', 'fluid.data', True, 'import paddle.fluid as fluid\n'), (115, 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', True, 'import paddle.fluid as fluid\n'), (116, 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', True, 'import paddle.fluid as fluid\n'), (139, 'paddle.fluid.program_guard', 'fluid.program_guard', True, 'import paddle.fluid as fluid\n'), (140, 'paddle.fluid.data', 'fluid.data', True, 'import paddle.fluid as fluid\n'), (142, 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', True, 'import paddle.fluid as fluid\n'), (144, 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', True, 'import paddle.fluid as fluid\n'), (164, 'paddle.fluid.program_guard', 'fluid.program_guard', True, 'import paddle.fluid as fluid\n'), (165, 'paddle.fluid.data', 'fluid.data', True, 'import paddle.fluid as fluid\n'), (167, 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', True, 'import paddle.fluid as fluid\n'), (168, 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', True, 'import paddle.fluid as fluid\n'), (188, 'paddle.fluid.program_guard', 'fluid.program_guard', True, 'import paddle.fluid as fluid\n'), (189, 'paddle.fluid.data', 'fluid.data', True, 'import paddle.fluid as fluid\n'), (191, 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', True, 'import paddle.fluid as fluid\n'), (192, 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', True, 'import paddle.fluid as fluid\n'), (212, 'paddle.fluid.program_guard', 'fluid.program_guard', True, 'import paddle.fluid as fluid\n'), (213, 'paddle.fluid.data', 'fluid.data', True, 'import paddle.fluid as fluid\n'), (215, 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', True, 'import paddle.fluid as fluid\n'), (216, 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', True, 'import paddle.fluid as fluid\n'), (51, 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', False, 'from paddle.fluid.core import PassVersionChecker\n'), (79, 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', False, 'from paddle.fluid.core import PassVersionChecker\n'), (107, 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', False, 'from paddle.fluid.core import PassVersionChecker\n'), (134, 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', False, 'from paddle.fluid.core import PassVersionChecker\n'), (159, 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', False, 'from paddle.fluid.core import PassVersionChecker\n'), (183, 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', False, 'from paddle.fluid.core import PassVersionChecker\n'), (207, 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', False, 'from paddle.fluid.core import PassVersionChecker\n'), (231, 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', False, 'from paddle.fluid.core import PassVersionChecker\n'), (36, 'numpy.random.random', 'np.random.random', True, 'import numpy as np\n'), (63, 'numpy.random.random', 'np.random.random', True, 'import numpy as np\n'), (92, 'numpy.random.random', 'np.random.random', True, 'import numpy as np\n'), (119, 'numpy.random.random', 'np.random.random', True, 'import numpy as np\n'), (147, 'numpy.random.random', 'np.random.random', True, 'import numpy as np\n'), (171, 'numpy.random.random', 'np.random.random', True, 'import numpy as np\n'), (195, 'numpy.random.random', 'np.random.random', True, 'import numpy as np\n'), (219, 'numpy.random.random', 'np.random.random', True, 'import numpy as np\n')]