# 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, 4, 235, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((47, 11, 47, 39), 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', ({}, {}), '()', True, 'import paddle.fluid.core as core\n'), ((75, 11, 75, 39), 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', ({}, {}), '()', True, 'import paddle.fluid.core as core\n'), ((103, 11, 103, 39), 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', ({}, {}), '()', True, 'import paddle.fluid.core as core\n'), ((130, 11, 130, 39), 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', ({}, {}), '()', True, 'import paddle.fluid.core as core\n'), ((155, 11, 155, 39), 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', ({}, {}), '()', True, 'import paddle.fluid.core as core\n'), ((179, 11, 179, 39), 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', ({}, {}), '()', True, 'import paddle.fluid.core as core\n'), ((203, 11, 203, 39), 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', ({}, {}), '()', True, 'import paddle.fluid.core as core\n'), ((227, 11, 227, 39), 'paddle.fluid.core.is_compiled_with_cuda', 'core.is_compiled_with_cuda', ({}, {}), '()', True, 'import paddle.fluid.core as core\n'), ((28, 13, 28, 73), 'paddle.fluid.program_guard', 'fluid.program_guard', ({(28, 33, 28, 50): 'self.main_program', (28, 52, 28, 72): 'self.startup_program'}, {}), '(self.main_program, self.startup_program)', True, 'import paddle.fluid as fluid\n'), ((29, 19, 30, 68), 'paddle.fluid.data', 'fluid.data', (), '', True, 'import paddle.fluid as fluid\n'), ((31, 26, 32, 49), 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', (), '', True, 'import paddle.fluid as fluid\n'), ((33, 18, 33, 68), 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', (), '', True, 'import paddle.fluid as fluid\n'), ((56, 13, 56, 73), 'paddle.fluid.program_guard', 'fluid.program_guard', ({(56, 33, 56, 50): 'self.main_program', (56, 52, 56, 72): 'self.startup_program'}, {}), '(self.main_program, self.startup_program)', True, 'import paddle.fluid as fluid\n'), ((57, 19, 58, 68), 'paddle.fluid.data', 'fluid.data', (), '', True, 'import paddle.fluid as fluid\n'), ((59, 26, 59, 71), 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', (), '', True, 'import paddle.fluid as fluid\n'), ((60, 18, 60, 68), 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', (), '', True, 'import paddle.fluid as fluid\n'), ((84, 13, 84, 73), 'paddle.fluid.program_guard', 'fluid.program_guard', ({(84, 33, 84, 50): 'self.main_program', (84, 52, 84, 72): 'self.startup_program'}, {}), '(self.main_program, self.startup_program)', True, 'import paddle.fluid as fluid\n'), ((85, 19, 86, 68), 'paddle.fluid.data', 'fluid.data', (), '', True, 'import paddle.fluid as fluid\n'), ((87, 26, 88, 49), 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', (), '', True, 'import paddle.fluid as fluid\n'), ((89, 18, 89, 68), 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', (), '', True, 'import paddle.fluid as fluid\n'), ((112, 13, 112, 73), 'paddle.fluid.program_guard', 'fluid.program_guard', ({(112, 33, 112, 50): 'self.main_program', (112, 52, 112, 72): 'self.startup_program'}, {}), '(self.main_program, self.startup_program)', True, 'import paddle.fluid as fluid\n'), ((113, 19, 114, 68), 'paddle.fluid.data', 'fluid.data', (), '', True, 'import paddle.fluid as fluid\n'), ((115, 26, 115, 71), 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', (), '', True, 'import paddle.fluid as fluid\n'), ((116, 18, 116, 68), 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', (), '', True, 'import paddle.fluid as fluid\n'), ((139, 13, 139, 73), 'paddle.fluid.program_guard', 'fluid.program_guard', ({(139, 33, 139, 50): 'self.main_program', (139, 52, 139, 72): 'self.startup_program'}, {}), '(self.main_program, self.startup_program)', True, 'import paddle.fluid as fluid\n'), ((140, 19, 141, 67), 'paddle.fluid.data', 'fluid.data', (), '', True, 'import paddle.fluid as fluid\n'), ((142, 26, 143, 49), 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', (), '', True, 'import paddle.fluid as fluid\n'), ((144, 18, 144, 68), 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', (), '', True, 'import paddle.fluid as fluid\n'), ((164, 13, 164, 73), 'paddle.fluid.program_guard', 'fluid.program_guard', ({(164, 33, 164, 50): 'self.main_program', (164, 52, 164, 72): 'self.startup_program'}, {}), '(self.main_program, self.startup_program)', True, 'import paddle.fluid as fluid\n'), ((165, 19, 166, 67), 'paddle.fluid.data', 'fluid.data', (), '', True, 'import paddle.fluid as fluid\n'), ((167, 26, 167, 71), 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', (), '', True, 'import paddle.fluid as fluid\n'), ((168, 18, 168, 68), 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', (), '', True, 'import paddle.fluid as fluid\n'), ((188, 13, 188, 73), 'paddle.fluid.program_guard', 'fluid.program_guard', ({(188, 33, 188, 50): 'self.main_program', (188, 52, 188, 72): 'self.startup_program'}, {}), '(self.main_program, self.startup_program)', True, 'import paddle.fluid as fluid\n'), ((189, 19, 190, 67), 'paddle.fluid.data', 'fluid.data', (), '', True, 'import paddle.fluid as fluid\n'), ((191, 26, 191, 71), 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', (), '', True, 'import paddle.fluid as fluid\n'), ((192, 18, 192, 68), 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', (), '', True, 'import paddle.fluid as fluid\n'), ((212, 13, 212, 73), 'paddle.fluid.program_guard', 'fluid.program_guard', ({(212, 33, 212, 50): 'self.main_program', (212, 52, 212, 72): 'self.startup_program'}, {}), '(self.main_program, self.startup_program)', True, 'import paddle.fluid as fluid\n'), ((213, 19, 214, 67), 'paddle.fluid.data', 'fluid.data', (), '', True, 'import paddle.fluid as fluid\n'), ((215, 26, 215, 71), 'paddle.fluid.layers.reduce_mean', 'fluid.layers.reduce_mean', (), '', True, 'import paddle.fluid as fluid\n'), ((216, 18, 216, 68), 'paddle.fluid.layers.batch_norm', 'fluid.layers.batch_norm', (), '', True, 'import paddle.fluid as fluid\n'), ((51, 16, 51, 73), 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', ({(51, 48, 51, 72): '"""tensorrt_subgraph_pass"""'}, {}), "('tensorrt_subgraph_pass')", False, 'from paddle.fluid.core import PassVersionChecker\n'), ((79, 16, 79, 73), 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', ({(79, 48, 79, 72): '"""tensorrt_subgraph_pass"""'}, {}), "('tensorrt_subgraph_pass')", False, 'from paddle.fluid.core import PassVersionChecker\n'), ((107, 16, 107, 73), 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', ({(107, 48, 107, 72): '"""tensorrt_subgraph_pass"""'}, {}), "('tensorrt_subgraph_pass')", False, 'from paddle.fluid.core import PassVersionChecker\n'), ((134, 16, 134, 73), 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', ({(134, 48, 134, 72): '"""tensorrt_subgraph_pass"""'}, {}), "('tensorrt_subgraph_pass')", False, 'from paddle.fluid.core import PassVersionChecker\n'), ((159, 16, 159, 73), 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', ({(159, 48, 159, 72): '"""tensorrt_subgraph_pass"""'}, {}), "('tensorrt_subgraph_pass')", False, 'from paddle.fluid.core import PassVersionChecker\n'), ((183, 16, 183, 73), 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', ({(183, 48, 183, 72): '"""tensorrt_subgraph_pass"""'}, {}), "('tensorrt_subgraph_pass')", False, 'from paddle.fluid.core import PassVersionChecker\n'), ((207, 16, 207, 73), 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', ({(207, 48, 207, 72): '"""tensorrt_subgraph_pass"""'}, {}), "('tensorrt_subgraph_pass')", False, 'from paddle.fluid.core import PassVersionChecker\n'), ((231, 16, 231, 73), 'paddle.fluid.core.PassVersionChecker.IsCompatible', 'PassVersionChecker.IsCompatible', ({(231, 48, 231, 72): '"""tensorrt_subgraph_pass"""'}, {}), "('tensorrt_subgraph_pass')", False, 'from paddle.fluid.core import PassVersionChecker\n'), ((36, 20, 36, 52), 'numpy.random.random', 'np.random.random', ({(36, 37, 36, 51): '[3, 3, 56, 56]'}, {}), '([3, 3, 56, 56])', True, 'import numpy as np\n'), ((63, 20, 63, 52), 'numpy.random.random', 'np.random.random', ({(63, 37, 63, 51): '[3, 3, 56, 56]'}, {}), '([3, 3, 56, 56])', True, 'import numpy as np\n'), ((92, 20, 92, 52), 'numpy.random.random', 'np.random.random', ({(92, 37, 92, 51): '[3, 3, 56, 56]'}, {}), '([3, 3, 56, 56])', True, 'import numpy as np\n'), ((119, 20, 119, 52), 'numpy.random.random', 'np.random.random', ({(119, 37, 119, 51): '[3, 3, 56, 56]'}, {}), '([3, 3, 56, 56])', True, 'import numpy as np\n'), ((147, 20, 147, 52), 'numpy.random.random', 'np.random.random', ({(147, 37, 147, 51): '[3, 3, 56, 56]'}, {}), '([3, 3, 56, 56])', True, 'import numpy as np\n'), ((171, 20, 171, 52), 'numpy.random.random', 'np.random.random', ({(171, 37, 171, 51): '[4, 3, 56, 56]'}, {}), '([4, 3, 56, 56])', True, 'import numpy as np\n'), ((195, 20, 195, 52), 'numpy.random.random', 'np.random.random', ({(195, 37, 195, 51): '[4, 3, 56, 56]'}, {}), '([4, 3, 56, 56])', True, 'import numpy as np\n'), ((219, 20, 219, 52), 'numpy.random.random', 'np.random.random', ({(219, 37, 219, 51): '[4, 3, 56, 56]'}, {}), '([4, 3, 56, 56])', True, 'import numpy as np\n')]