test_transfer_dtype_op.py 3.2 KB
Newer Older
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
#
3 4 5
# 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
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 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
import numpy as np

import paddle
import paddle.fluid.core as core
from op_test import OpTest, convert_uint16_to_float, convert_float_to_uint16


class TestTransferDtypeOpFp32ToFp64(OpTest):
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    def setUp(self):
        ipt = np.random.random(size=[10, 10])
        self.inputs = {'X': ipt.astype('float32')}
        self.outputs = {'Out': ipt.astype('float64')}
        self.attrs = {
            'out_dtype': int(core.VarDesc.VarType.FP64),
            'in_dtype': int(core.VarDesc.VarType.FP32)
        }
        self.op_type = 'transfer_dtype'

    def test_check_output(self):
        self.check_output()


class TestTransferDtypeOpFp16ToFp32(OpTest):
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    def setUp(self):
        ipt = np.random.random(size=[10, 10])
        self.inputs = {'X': ipt.astype('float16')}
        self.outputs = {'Out': ipt.astype('float32')}
        self.attrs = {
            'out_dtype': int(core.VarDesc.VarType.FP32),
            'in_dtype': int(core.VarDesc.VarType.FP16)
        }
        self.op_type = 'transfer_dtype'

    def test_check_output(self):
        self.check_output(atol=1e-3)


class TestTransferDtypeOpFp32ToFp16(OpTest):
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    def setUp(self):
        ipt = np.random.random(size=[10, 10])
        self.inputs = {'X': ipt.astype('float32')}
        self.outputs = {'Out': ipt.astype('float16')}
        self.attrs = {
            'out_dtype': int(core.VarDesc.VarType.FP16),
            'in_dtype': int(core.VarDesc.VarType.FP32)
        }
        self.op_type = 'transfer_dtype'

    def test_check_output(self):
        self.check_output(atol=1e-3)


class TestTransferDtypeOpBf16ToFp32(OpTest):
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    def setUp(self):
        ipt = np.array(np.random.randint(10, size=[10, 10])).astype('uint16')
        self.inputs = {'X': ipt}
        self.outputs = {'Out': convert_uint16_to_float(ipt)}
        self.attrs = {
            'out_dtype': int(core.VarDesc.VarType.FP32),
            'in_dtype': int(core.VarDesc.VarType.BF16)
        }
        self.op_type = 'transfer_dtype'

    def test_check_output(self):
        self.check_output()


class TestTransferDtypeFp32ToBf16(OpTest):
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    def setUp(self):
        ipt = np.random.random(size=[10, 10]).astype('float32')
        self.inputs = {'X': ipt}
        self.outputs = {'Out': convert_float_to_uint16(ipt)}
        self.attrs = {
            'out_dtype': int(core.VarDesc.VarType.BF16),
            'in_dtype': int(core.VarDesc.VarType.FP32)
        }
        self.op_type = 'transfer_dtype'

    def test_check_output(self):
        self.check_output()


if __name__ == '__main__':
    paddle.enable_static()
    unittest.main()