test_multi_out_jit.py 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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.

import os
16
import subprocess
17 18
import unittest
import numpy as np
19 20 21

import paddle
from paddle.utils.cpp_extension import load
22 23
from paddle.utils.cpp_extension import load, get_build_directory
from paddle.utils.cpp_extension.extension_utils import run_cmd
24 25
from utils import paddle_includes, extra_compile_args

Z
Zhou Wei 已提交
26 27
# Because Windows don't use docker, the shared lib already exists in the 
# cache dir, it will not be compiled again unless the shared lib is removed.
28 29 30
file = '{}\\multi_out_jit\\multi_out_jit.pyd'.format(get_build_directory())
if os.name == 'nt' and os.path.isfile(file):
    cmd = 'del {}'.format(file)
Z
Zhou Wei 已提交
31
    run_cmd(cmd, True)
32

33
# Compile and load custom op Just-In-Time.
34 35 36
multi_out_module = load(
    name='multi_out_jit',
    sources=['multi_out_test_op.cc'],
37
    extra_include_paths=paddle_includes,  # add for Coverage CI
Z
Zhou Wei 已提交
38 39
    extra_cflags=extra_compile_args,  # add for Coverage CI
    verbose=True)
40 41


42 43
class TestMultiOutputDtypes(unittest.TestCase):
    def setUp(self):
44
        self.custom_op = multi_out_module.multi_out
45
        self.dtypes = ['float32', 'float64']
46
        self.devices = ['cpu']
47

48 49 50
    def run_static(self, device, dtype):
        paddle.set_device(device)
        x_data = np.random.uniform(-1, 1, [4, 8]).astype(dtype)
51

52 53 54
        with paddle.static.scope_guard(paddle.static.Scope()):
            with paddle.static.program_guard(paddle.static.Program()):
                x = paddle.static.data(name='X', shape=[None, 8], dtype=dtype)
55 56
                outs = self.custom_op(x)

57 58 59 60 61 62 63
                exe = paddle.static.Executor()
                exe.run(paddle.static.default_startup_program())
                res = exe.run(paddle.static.default_main_program(),
                              feed={'X': x_data},
                              fetch_list=outs)

                return res
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    def check_multi_outputs(self, outs, is_dynamic=False):
        out, zero_float64, one_int32 = outs
        if is_dynamic:
            zero_float64 = zero_float64.numpy()
            one_int32 = one_int32.numpy()
        # Fake_float64
        self.assertTrue('float64' in str(zero_float64.dtype))
        self.assertTrue(
            np.array_equal(zero_float64, np.zeros([4, 8]).astype('float64')))
        # ZFake_int32
        self.assertTrue('int32' in str(one_int32.dtype))
        self.assertTrue(
            np.array_equal(one_int32, np.ones([4, 8]).astype('int32')))

79 80 81 82 83 84 85
    def test_static(self):
        paddle.enable_static()
        for device in self.devices:
            for dtype in self.dtypes:
                res = self.run_static(device, dtype)
                self.check_multi_outputs(res)
        paddle.disable_static()
86

87 88 89 90 91 92
    def test_dynamic(self):
        for device in self.devices:
            for dtype in self.dtypes:
                paddle.set_device(device)
                x_data = np.random.uniform(-1, 1, [4, 8]).astype(dtype)
                x = paddle.to_tensor(x_data)
93 94
                outs = self.custom_op(x)

95 96
                self.assertTrue(len(outs) == 3)
                self.check_multi_outputs(outs, True)
97 98


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