test_bitcast_convert_op.py 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2023 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.

import unittest
16 17 18
from struct import pack, unpack

import numpy as np
19
from cinn.common import *
20 21
from cinn.frontend import *
from op_test import OpTest, OpTestTool
22

23
import paddle
24 25


26 27 28
@OpTestTool.skip_if(
    not is_compiled_with_cuda(), "x86 test will be skipped due to timeout."
)
29 30 31 32 33 34 35 36 37 38
class TestBitcastConvertOp(OpTest):
    def setUp(self):
        self.init_case()

    # input[(3, 1), int32] --> output[(3, 1, 4), uint8]
    def init_case(self):
        data = np.random.random([3, 1]).astype(np.int32)
        packed = pack(data.size * 'i', *data.flatten())
        self.inputs = {"x": data}
        self.outputs = {
39 40 41 42
            "y": np.array(unpack('12B', packed), dtype='uint8').reshape(
                (3, 1, 4)
            ),
            "output_type": "uint8",
43 44 45 46 47 48 49 50 51 52
        }

    def build_paddle_program(self, target):
        y = paddle.to_tensor(self.outputs["y"], stop_gradient=False)
        self.paddle_outputs = [y]

    def build_cinn_program(self, target):
        builder = NetBuilder("bitcast_convert")
        x = builder.create_input(
            self.nptype2cinntype(self.inputs["x"].dtype),
53 54 55
            self.inputs["x"].shape,
            "x",
        )
56 57
        out = builder.bitcast_convert(x, self.outputs["output_type"])
        prog = builder.build()
58
        res = self.get_cinn_output(prog, target, [x], [self.inputs["x"]], [out])
59 60 61 62 63 64 65 66 67 68 69 70 71
        self.cinn_outputs = [res[0]]

    def test_check_results(self):
        self.check_outputs_and_grads()


class TestBitcastConvertCase1(TestBitcastConvertOp):
    # input[(4, 2), int16] --> output[(4), int32]
    def init_case(self):
        data = np.random.random([4, 2]).astype(np.int16)
        packed = pack(data.size * 'h', *data.flatten())
        self.inputs = {"x": data}
        self.outputs = {
72
            "y": np.array(unpack('4i', packed), dtype='int32').reshape(4),
73
            "output_type": "int32",
74 75 76 77 78 79 80 81 82 83
        }


class TestBitcastConvertCase2(TestBitcastConvertOp):
    # input[(4, 3, 2), float32] --> output[(4, 3), float64]
    def init_case(self):
        data = np.random.random([4, 3, 2]).astype(np.float32)
        packed = pack(data.size * 'f', *data.flatten())
        self.inputs = {"x": data}
        self.outputs = {
84 85 86 87
            "y": np.array(unpack('12d', packed), dtype='float64').reshape(
                (4, 3)
            ),
            "output_type": "float64",
88 89 90 91 92 93 94 95 96 97
        }


class TestBitcastConvertCase3(TestBitcastConvertOp):
    # input[(4, 3, 2), float32] --> output[(4, 3, 2, 2), uint16]
    def init_case(self):
        data = np.random.random([4, 3, 2]).astype(np.float32)
        packed = pack(data.size * 'f', *data.flatten())
        self.inputs = {"x": data}
        self.outputs = {
98 99 100 101
            "y": np.array(unpack('48H', packed), dtype='uint16').reshape(
                (4, 3, 2, 2)
            ),
            "output_type": "uint16",
102 103 104 105 106
        }


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