test_memcpy_op.py 8.9 KB
Newer Older
J
JZ-LIANG 已提交
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
#   Copyright (c) 2018 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.

from __future__ import print_function

import op_test
import numpy as np
import unittest
import paddle
import paddle.fluid.core as core
from paddle.fluid.op import Operator
import paddle.fluid as fluid
from paddle.fluid import compiler, Program, program_guard
from paddle.fluid.backward import append_backward


class TestMemcpy_FillConstant(unittest.TestCase):
29

J
JZ-LIANG 已提交
30 31 32 33 34 35 36 37 38 39 40 41
    def get_prog(self):
        paddle.enable_static()
        main_program = Program()
        with program_guard(main_program):
            pinned_var_name = "tensor@Pinned"
            gpu_var_name = "tensor@GPU"
            pinned_var = main_program.global_block().create_var(
                name=pinned_var_name,
                shape=[10, 10],
                dtype='float32',
                persistable=False,
                stop_gradient=True)
42 43 44 45 46 47 48 49 50 51 52 53 54
            gpu_var = main_program.global_block().create_var(name=gpu_var_name,
                                                             shape=[10, 10],
                                                             dtype='float32',
                                                             persistable=False,
                                                             stop_gradient=True)
            main_program.global_block().append_op(type="fill_constant",
                                                  outputs={"Out": gpu_var_name},
                                                  attrs={
                                                      "shape": [10, 10],
                                                      "dtype": gpu_var.dtype,
                                                      "value": 1.0,
                                                      "place_type": 1
                                                  })
J
JZ-LIANG 已提交
55 56 57 58 59 60 61 62 63 64 65
            main_program.global_block().append_op(
                type="fill_constant",
                outputs={"Out": pinned_var_name},
                attrs={
                    "shape": [10, 10],
                    "dtype": gpu_var.dtype,
                    "value": 0.0,
                    "place_type": 2
                })
        return main_program, gpu_var, pinned_var

66
    def test_gpu_copy_to_pinned(self):
J
JZ-LIANG 已提交
67
        main_program, gpu_var, pinned_var = self.get_prog()
68 69 70 71
        main_program.global_block().append_op(type='memcpy',
                                              inputs={'X': gpu_var},
                                              outputs={'Out': pinned_var},
                                              attrs={'dst_place_type': 2})
J
JZ-LIANG 已提交
72 73 74 75 76
        place = fluid.CUDAPlace(0)
        exe = fluid.Executor(place)
        gpu_, pinned_ = exe.run(main_program,
                                feed={},
                                fetch_list=[gpu_var.name, pinned_var.name])
77 78
        np.testing.assert_allclose(gpu_, pinned_, rtol=1e-05)
        np.testing.assert_allclose(pinned_, np.ones((10, 10)), rtol=1e-05)
J
JZ-LIANG 已提交
79

80
    def test_pinned_copy_gpu(self):
J
JZ-LIANG 已提交
81
        main_program, gpu_var, pinned_var = self.get_prog()
82 83 84 85
        main_program.global_block().append_op(type='memcpy',
                                              inputs={'X': pinned_var},
                                              outputs={'Out': gpu_var},
                                              attrs={'dst_place_type': 1})
J
JZ-LIANG 已提交
86 87 88 89 90
        place = fluid.CUDAPlace(0)
        exe = fluid.Executor(place)
        gpu_, pinned_ = exe.run(main_program,
                                feed={},
                                fetch_list=[gpu_var.name, pinned_var.name])
91 92
        np.testing.assert_allclose(gpu_, pinned_, rtol=1e-05)
        np.testing.assert_allclose(gpu_, np.zeros((10, 10)), rtol=1e-05)
J
JZ-LIANG 已提交
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    def test_hip_copy_bool_value(self):
        if core.is_compiled_with_rocm():
            paddle.enable_static()
            main_program = Program()
            with program_guard(main_program):
                pinned_var_name = "tensor@Pinned"
                gpu_var_name = "tensor@GPU"
                pinned_var = main_program.global_block().create_var(
                    name=pinned_var_name,
                    shape=[1],
                    dtype='bool',
                    persistable=False,
                    stop_gradient=True)
                gpu_var = main_program.global_block().create_var(
                    name=gpu_var_name,
                    shape=[1],
                    dtype='bool',
                    persistable=False,
                    stop_gradient=True)
                main_program.global_block().append_op(
                    type="fill_constant",
                    outputs={"Out": gpu_var_name},
                    attrs={
                        "shape": [1],
                        "dtype": gpu_var.dtype,
                        "value": False,
                        "place_type": 1
                    })
                main_program.global_block().append_op(
                    type="fill_constant",
                    outputs={"Out": pinned_var_name},
                    attrs={
                        "shape": [1],
                        "dtype": gpu_var.dtype,
                        "value": True,
                        "place_type": 2
                    })

132 133 134 135
            main_program.global_block().append_op(type='memcpy',
                                                  inputs={'X': pinned_var},
                                                  outputs={'Out': gpu_var},
                                                  attrs={'dst_place_type': 1})
136 137 138 139 140 141
            place = fluid.CUDAPlace(0)
            exe = fluid.Executor(place)
            gpu_, pinned_ = exe.run(main_program,
                                    feed={},
                                    fetch_list=[gpu_var.name, pinned_var.name])
            expect_value = np.array([1]).astype('bool')
142
            np.testing.assert_array_equal(gpu_, expect_value)
143 144 145
        else:
            pass

J
JZ-LIANG 已提交
146 147

class TestMemcpyOPError(unittest.TestCase):
148

J
JZ-LIANG 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    def get_prog(self):
        paddle.enable_static()
        main_program = Program()
        with program_guard(main_program):
            pinned_var = main_program.global_block().create_var(
                name="tensor@Pinned_0",
                shape=[10, 10],
                dtype='float32',
                persistable=False,
                stop_gradient=True)
            main_program.global_block().append_op(
                type="fill_constant",
                outputs={"Out": "tensor@Pinned_0"},
                attrs={
                    "shape": [10, 10],
                    "dtype": pinned_var.dtype,
                    "value": 0.0,
                    "place_type": 2
                })
        return main_program, pinned_var

    def test_SELECTED_ROWS(self):
        main_program, pinned_var = self.get_prog()
        selected_row_var = main_program.global_block().create_var( \
            name="selected_row_0", dtype="float32", persistable=False, \
            type=fluid.core.VarDesc.VarType.SELECTED_ROWS, stop_gradient=True)
175 176 177 178 179 180 181 182 183 184
        main_program.global_block().append_op(type="fill_constant",
                                              outputs={"Out": selected_row_var},
                                              attrs={
                                                  "shape":
                                                  selected_row_var.shape,
                                                  "dtype":
                                                  selected_row_var.dtype,
                                                  "value": 1.0,
                                                  "place_type": 1
                                              })
185 186 187 188 189 190
        with self.assertRaises(RuntimeError):
            main_program.global_block().append_op(
                type='memcpy',
                inputs={'X': selected_row_var},
                outputs={'Out': pinned_var},
                attrs={'dst_place_type': 2})
J
JZ-LIANG 已提交
191 192 193 194 195 196 197 198
            place = fluid.CUDAPlace(0)
            exe = fluid.Executor(place)
            selected_row_var_, pinned_ = exe.run(
                main_program,
                feed={},
                fetch_list=[selected_row_var.name, pinned_var.name])


199
class TestMemcpyApi(unittest.TestCase):
200

201 202 203
    def test_api(self):
        a = paddle.ones([1024, 1024])
        b = paddle.tensor.creation._memcpy(a, paddle.CUDAPinnedPlace())
204
        self.assertEqual(b.place.__repr__(), "Place(gpu_pinned)")
205
        np.testing.assert_array_equal(a.numpy(), b.numpy())
206 207


J
JZ-LIANG 已提交
208 209 210
if __name__ == '__main__':
    paddle.enable_static()
    unittest.main()