test_tf32_cublas.py 2.1 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#   Copyright (c) 2020 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.

import unittest
import six
import numpy as np
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core


class TestTF32Switch(unittest.TestCase):
    def test_on_off(self):
        if core.is_compiled_with_cuda():
            place = fluid.CUDAPlace(0)
            self.assertTrue(core.get_cublas_switch())  # default
            core.set_cublas_switch(False)
            self.assertFalse(core.get_cublas_switch())  # turn off
            core.set_cublas_switch(True)
            self.assertTrue(core.get_cublas_switch())  # turn on

            core.set_cublas_switch(True)  # restore the switch
        else:
            pass


class TestTF32OnMatmul(unittest.TestCase):
    def test_dygraph_without_out(self):
        if core.is_compiled_with_cuda():
            place = fluid.CUDAPlace(0)
            core.set_cublas_switch(False)  # turn off
            with fluid.dygraph.guard(place):
                input_array1 = np.random.rand(4, 12, 64, 88).astype("float32")
                input_array2 = np.random.rand(4, 12, 88, 512).astype("float32")
                data1 = paddle.to_tensor(input_array1)
                data2 = paddle.to_tensor(input_array2)
                out = paddle.matmul(data1, data2)
                expected_result = np.matmul(input_array1, input_array2)
            self.assertTrue(np.allclose(expected_result, out.numpy(), 1e-03))
            core.set_cublas_switch(True)  # restore the switch
        else:
            pass


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