test_assign_value_op.py 4.0 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

X
xuwei06 已提交
15
import unittest
16

W
wanghuancoder 已提交
17
import eager_op_test
18 19
import numpy as np

20
import paddle
21 22
from paddle import fluid
from paddle.fluid import framework
X
xuwei06 已提交
23

24

W
wanghuancoder 已提交
25 26 27 28 29 30 31
def assign_value_wrapper(
    shape=[], dtype=fluid.core.VarDesc.VarType.FP32, values=0.0
):
    tensor = paddle.Tensor()
    return paddle._C_ops.assign_value_(
        tensor, shape, dtype, values, framework._current_expected_place()
    )
X
xuwei06 已提交
32

W
wanghuancoder 已提交
33 34

class TestAssignValueOp(eager_op_test.OpTest):
X
xuwei06 已提交
35 36
    def setUp(self):
        self.op_type = "assign_value"
W
wanghuancoder 已提交
37
        self.python_api = assign_value_wrapper
X
xuwei06 已提交
38
        self.inputs = {}
39 40 41 42
        self.attrs = {}
        self.init_data()
        self.attrs["shape"] = self.value.shape
        self.attrs["dtype"] = framework.convert_np_dtype_to_dtype_(
43 44
            self.value.dtype
        )
45 46 47
        self.outputs = {"Out": self.value}

    def init_data(self):
48
        self.value = np.random.random(size=(2, 5)).astype(np.float32)
49
        self.attrs["fp32_values"] = [float(v) for v in self.value.flat]
X
xuwei06 已提交
50 51

    def test_forward(self):
52
        self.check_output(check_cinn=True)
X
xuwei06 已提交
53

54 55 56

class TestAssignValueOp2(TestAssignValueOp):
    def init_data(self):
57
        self.value = np.random.random(size=(2, 5)).astype(np.int32)
58 59 60 61 62
        self.attrs["int32_values"] = [int(v) for v in self.value.flat]


class TestAssignValueOp3(TestAssignValueOp):
    def init_data(self):
63
        self.value = np.random.random(size=(2, 5)).astype(np.int64)
64 65 66
        self.attrs["int64_values"] = [int(v) for v in self.value.flat]


67 68
class TestAssignValueOp4(TestAssignValueOp):
    def init_data(self):
69
        self.value = np.random.choice(a=[False, True], size=(2, 5)).astype(
R
Ryan 已提交
70
            np.bool_
71
        )
W
wanghuancoder 已提交
72
        self.attrs["bool_values"] = [int(v) for v in self.value.flat]
73 74


75 76
class TestAssignApi(unittest.TestCase):
    def setUp(self):
W
wanghuancoder 已提交
77 78 79 80 81 82 83 84 85 86
        with eager_op_test.paddle_static_guard():
            self.init_dtype()
            self.value = (-100 + 200 * np.random.random(size=(2, 5))).astype(
                self.dtype
            )
            self.place = (
                fluid.CUDAPlace(0)
                if fluid.is_compiled_with_cuda()
                else fluid.CPUPlace()
            )
87 88 89 90

    def init_dtype(self):
        self.dtype = "float32"

X
xuwei06 已提交
91
    def test_assign(self):
W
wanghuancoder 已提交
92 93 94 95 96
        with eager_op_test.paddle_static_guard():
            main_program = fluid.Program()
            with fluid.program_guard(main_program):
                x = paddle.tensor.create_tensor(dtype=self.dtype)
                paddle.assign(self.value, output=x)
97

W
wanghuancoder 已提交
98 99 100 101
            exe = fluid.Executor(self.place)
            [fetched_x] = exe.run(main_program, feed={}, fetch_list=[x])
            np.testing.assert_array_equal(fetched_x, self.value)
            self.assertEqual(fetched_x.dtype, self.value.dtype)
102 103 104 105 106 107 108 109 110 111


class TestAssignApi2(TestAssignApi):
    def init_dtype(self):
        self.dtype = "int32"


class TestAssignApi3(TestAssignApi):
    def init_dtype(self):
        self.dtype = "int64"
X
xuwei06 已提交
112 113


114 115
class TestAssignApi4(TestAssignApi):
    def setUp(self):
W
wanghuancoder 已提交
116 117 118 119 120 121 122 123 124 125
        with eager_op_test.paddle_static_guard():
            self.init_dtype()
            self.value = np.random.choice(a=[False, True], size=(2, 5)).astype(
                np.bool_
            )
            self.place = (
                fluid.CUDAPlace(0)
                if fluid.is_compiled_with_cuda()
                else fluid.CPUPlace()
            )
126 127 128 129 130

    def init_dtype(self):
        self.dtype = "bool"


X
xuwei06 已提交
131 132
if __name__ == '__main__':
    unittest.main()