test_op_transform.py 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/env python3

# Copyright (c) 2021 CINN 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.

17
import logging
18
import math
19 20 21
import os
import unittest

22
import cinn
23 24
import numpy as np
from cinn import common, framework, frontend, ir, lang, runtime
25 26
from cinn.poly import create_stages
from test_utils import SingleOpTester
27

28 29
import paddle
import paddle.static as static
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"


def matmul_util(inputs_data, input_shape, trans_a, trans_b, alpha):
    main_program = static.Program()
    paddle.enable_static()
    with static.program_guard(main_program, static.Program()):
        [input_x, input_y] = inputs_data
        x = static.data(name='x', shape=input_shape[0], dtype='float32')
        y = static.data(name='y', shape=input_shape[1], dtype='float32')
        output = paddle.matmul(x, y, trans_a, trans_b)
        output = paddle.scale(output, scale=alpha)
        exe = static.Executor(paddle.CPUPlace())
        exe.run(static.default_startup_program())
45
        (res,) = exe.run(
46
            static.default_main_program(),
47 48 49
            feed={'x': input_x, 'y': input_y},
            fetch_list=[output],
        )
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        return res


class OpTest_matmul_0(SingleOpTester):
    def init_testcase(self):
        self.input_shape = [[100, 32], [32, 100]]
        self.output_shape = [[100, 100], [100, 100]]
        self.trans_a = False
        self.trans_b = False
        self.alpha = 1.0
        self.attrs = framework.NodeAttr()
        self.attrs.set_attr("trans_a", self.trans_a)
        self.attrs.set_attr("trans_b", self.trans_b)
        self.attrs.set_attr("alpha", self.alpha)

    def create_target_data(self, inputs_data, attrs):
66 67 68 69 70 71 72
        return matmul_util(
            inputs_data,
            self.input_shape,
            self.trans_a,
            self.trans_b,
            self.alpha,
        )
73 74 75

    def test_op(self):
        self.init_testcase()
76 77 78
        self.to_test_op(
            self.input_shape, self.output_shape, "matmul", self.attrs, 0
        )
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93


class OpTest_matmul_1(SingleOpTester):
    def init_testcase(self):
        self.input_shape = [[100, 32], [100, 32]]
        self.output_shape = [[100, 100], [2, 32, 50]]
        self.trans_a = False
        self.trans_b = True
        self.alpha = 2.0
        self.attrs = framework.NodeAttr()
        self.attrs.set_attr("trans_a", self.trans_a)
        self.attrs.set_attr("trans_b", self.trans_b)
        self.attrs.set_attr("alpha", self.alpha)

    def create_target_data(self, inputs_data, attrs):
94 95 96 97 98 99 100
        return matmul_util(
            inputs_data,
            self.input_shape,
            self.trans_a,
            self.trans_b,
            self.alpha,
        )
101 102 103

    def test_op(self):
        self.init_testcase()
104 105 106
        self.to_test_op(
            self.input_shape, self.output_shape, "matmul", self.attrs, 0
        )
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121


class OpTest_matmul_2(SingleOpTester):
    def init_testcase(self):
        self.input_shape = [[2, 3, 100, 32], [2, 3, 100, 32]]
        self.output_shape = [[2, 3, 100, 100], [2, 3, 2, 100, 16]]
        self.trans_a = False
        self.trans_b = True
        self.alpha = 2.0
        self.attrs = framework.NodeAttr()
        self.attrs.set_attr("trans_a", self.trans_a)
        self.attrs.set_attr("trans_b", self.trans_b)
        self.attrs.set_attr("alpha", self.alpha)

    def create_target_data(self, inputs_data, attrs):
122 123 124 125 126 127 128
        return matmul_util(
            inputs_data,
            self.input_shape,
            self.trans_a,
            self.trans_b,
            self.alpha,
        )
129 130 131

    def test_op(self):
        self.init_testcase()
132 133 134
        self.to_test_op(
            self.input_shape, self.output_shape, "matmul", self.attrs, 0
        )
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149


class OpTest_matmul_3(SingleOpTester):
    def init_testcase(self):
        self.input_shape = [[32, 100], [32, 100]]
        self.output_shape = [[100, 100], [2, 100, 16]]
        self.trans_a = True
        self.trans_b = False
        self.alpha = 2.0
        self.attrs = framework.NodeAttr()
        self.attrs.set_attr("trans_a", self.trans_a)
        self.attrs.set_attr("trans_b", self.trans_b)
        self.attrs.set_attr("alpha", self.alpha)

    def create_target_data(self, inputs_data, attrs):
150 151 152 153 154 155 156
        return matmul_util(
            inputs_data,
            self.input_shape,
            self.trans_a,
            self.trans_b,
            self.alpha,
        )
157 158 159

    def test_op(self):
        self.init_testcase()
160 161 162
        self.to_test_op(
            self.input_shape, self.output_shape, "matmul", self.attrs, 0
        )
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177


class OpTest_matmul_4(SingleOpTester):
    def init_testcase(self):
        self.input_shape = [[32, 100], [100]]
        self.output_shape = [[32], [2, 100, 16]]
        self.trans_a = False
        self.trans_b = False
        self.alpha = 2.0
        self.attrs = framework.NodeAttr()
        self.attrs.set_attr("trans_a", self.trans_a)
        self.attrs.set_attr("trans_b", self.trans_b)
        self.attrs.set_attr("alpha", self.alpha)

    def create_target_data(self, inputs_data, attrs):
178 179 180 181 182 183 184
        return matmul_util(
            inputs_data,
            self.input_shape,
            self.trans_a,
            self.trans_b,
            self.alpha,
        )
185 186 187

    def test_op(self):
        self.init_testcase()
188 189 190
        self.to_test_op(
            self.input_shape, self.output_shape, "matmul", self.attrs, 0
        )
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205


class OpTest_matmul_5(SingleOpTester):
    def init_testcase(self):
        self.input_shape = [[100], [100]]
        self.output_shape = [[1], [1, 100, 1]]
        self.trans_a = False
        self.trans_b = False
        self.alpha = 2.0
        self.attrs = framework.NodeAttr()
        self.attrs.set_attr("trans_a", self.trans_a)
        self.attrs.set_attr("trans_b", self.trans_b)
        self.attrs.set_attr("alpha", self.alpha)

    def create_target_data(self, inputs_data, attrs):
206 207 208 209 210 211 212
        return matmul_util(
            inputs_data,
            self.input_shape,
            self.trans_a,
            self.trans_b,
            self.alpha,
        )
213 214 215

    def test_op(self):
        self.init_testcase()
216 217 218
        self.to_test_op(
            self.input_shape, self.output_shape, "matmul", self.attrs, 0
        )
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233


class OpTest_matmul_6(SingleOpTester):
    def init_testcase(self):
        self.input_shape = [[32, 1], [1, 100]]
        self.output_shape = [[32, 100], [2, 1, 50]]
        self.trans_a = False
        self.trans_b = False
        self.alpha = 2.0
        self.attrs = framework.NodeAttr()
        self.attrs.set_attr("trans_a", self.trans_a)
        self.attrs.set_attr("trans_b", self.trans_b)
        self.attrs.set_attr("alpha", self.alpha)

    def create_target_data(self, inputs_data, attrs):
234 235 236 237 238 239 240
        return matmul_util(
            inputs_data,
            self.input_shape,
            self.trans_a,
            self.trans_b,
            self.alpha,
        )
241 242 243

    def test_op(self):
        self.init_testcase()
244 245 246
        self.to_test_op(
            self.input_shape, self.output_shape, "matmul", self.attrs, 0
        )
247 248 249 250


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