test_mkldnn_conv3d_op.py 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 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 auto_scan_test import MkldnnAutoScanTest, SkipReasons
from program_config import TensorConfig, ProgramConfig, OpConfig
import numpy as np
import paddle.inference as paddle_infer
from functools import partial
from typing import Optional, List, Callable, Dict, Any, Set
import unittest

import hypothesis
from hypothesis import given, settings, seed, example, assume
import hypothesis.strategies as st


class TestMkldnnConv3dOp(MkldnnAutoScanTest):
29

30 31 32 33
    def is_program_valid(self, program_config: ProgramConfig) -> bool:
        return True

    def sample_program_configs(self, *args, **kwargs):
34

35 36
        def generate_input(*args, **kwargs):
            if kwargs["data_format"] == "NCDHW":
37 38
                return np.random.random([kwargs["batch_size"], 48, 64, 32,
                                         64]).astype(np.float32)
39
            else:
40 41
                return np.random.random([kwargs["batch_size"], 64, 32, 64,
                                         48]).astype(np.float32)
42 43

        def generate_weight(*args, **kwargs):
44 45 46
            return np.random.random([16,
                                     int(48 / kwargs["groups"]), 3, 3,
                                     3]).astype(np.float32)
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        conv3d_op = OpConfig(type="conv3d",
                             inputs={
                                 "Input": ["input_data"],
                                 "Filter": ["conv_weight"]
                             },
                             outputs={"Output": ["conv_output"]},
                             attrs={
                                 "data_format": kwargs["data_format"],
                                 "dilations": kwargs["dilations"],
                                 "padding_algorithm":
                                 kwargs["padding_algorithm"],
                                 "groups": kwargs["groups"],
                                 "paddings": kwargs["paddings"],
                                 "strides": kwargs["strides"],
                                 "is_test": True
                             })
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

        program_config = ProgramConfig(
            ops=[conv3d_op],
            weights={
                "conv_weight":
                TensorConfig(data_gen=partial(generate_weight, *args, **kwargs))
            },
            inputs={
                "input_data":
                TensorConfig(data_gen=partial(generate_input, *args, **kwargs))
            },
            outputs=["conv_output"])

        yield program_config

    def sample_predictor_configs(self, program_config):
        config = self.create_inference_config(use_mkldnn=True)
        yield config, (1e-5, 1e-5)

    @given(
        data_format=st.sampled_from(["NCDHW", "NDHWC"]),
        dilations=st.sampled_from([[1, 2, 1]]),
        padding_algorithm=st.sampled_from(["EXPLICIT"]),
        groups=st.sampled_from([2]),
        paddings=st.sampled_from([[0, 3, 2]]),
        strides=st.sampled_from([[1, 2, 1]]),
90 91
        batch_size=st.integers(min_value=1, max_value=4),
    )
92 93 94 95 96 97
    def test(self, *args, **kwargs):
        self.run_test(*args, **kwargs)


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