test_device_guard.py 8.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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
16
import warnings
17

Z
Zhang Ting 已提交
18
import paddle
19 20
import paddle.fluid.core as core

21 22
paddle.enable_static()

23 24

def execute(main_program, startup_program):
Z
Zhang Ting 已提交
25 26
    if paddle.is_compiled_with_cuda():
        place = paddle.CUDAPlace(0)
27
    else:
Z
Zhang Ting 已提交
28 29
        place = paddle.CPUPlace()
    exe = paddle.static.Executor(place)
30 31 32 33
    exe.run(startup_program)
    exe.run(main_program)


34 35 36 37 38 39 40 41
def get_vaild_warning_num(warning, w):
    num = 0
    for i in range(len(w)):
        if warning in str(w[i].message):
            num += 1
    return num


42 43
class TestDeviceGuard(unittest.TestCase):
    def test_device_guard(self):
Z
Zhang Ting 已提交
44 45 46
        main_program = paddle.static.Program()
        startup_program = paddle.static.Program()
        with paddle.static.program_guard(main_program, startup_program):
47 48 49 50 51 52
            data1 = paddle.full(
                shape=[1, 3, 8, 8], fill_value=0.5, dtype='float32'
            )
            data2 = paddle.full(
                shape=[1, 3, 5, 5], fill_value=0.5, dtype='float32'
            )
Z
Zhang Ting 已提交
53 54 55 56
            shape = paddle.shape(data2)
            with paddle.static.device_guard("cpu"):
                shape = paddle.slice(shape, axes=[0], starts=[0], ends=[4])
                with paddle.static.device_guard("gpu"):
57
                    out = paddle.crop(data1, shape=shape)
58 59 60 61 62 63 64 65 66 67 68
        # check if the device attr is set correctly
        all_ops = main_program.global_block().ops
        device_attr_name = core.op_proto_and_checker_maker.kOpDeviceAttrName()
        for op in all_ops:
            if op.type == 'slice':
                self.assertEqual(op.desc.attr(device_attr_name), "cpu")
            if op.type == 'crop_tensor':
                self.assertEqual(op.desc.attr(device_attr_name), "gpu")

        execute(main_program, startup_program)

69
    def test_device_guard_with_id(self):
Z
Zhang Ting 已提交
70 71 72
        main_program = paddle.static.Program()
        startup_program = paddle.static.Program()
        with paddle.static.program_guard(main_program, startup_program):
73 74 75 76 77 78
            data1 = paddle.full(
                shape=[1, 3, 8, 8], fill_value=0.5, dtype='float32'
            )
            data2 = paddle.full(
                shape=[1, 3, 5, 5], fill_value=0.5, dtype='float32'
            )
Z
Zhang Ting 已提交
79 80 81 82
            shape = paddle.shape(data2)
            with paddle.static.device_guard("cpu"):
                shape = paddle.slice(shape, axes=[0], starts=[0], ends=[4])
                with paddle.static.device_guard("gpu:1"):
83
                    out = paddle.crop(data1, shape=shape)
84 85 86 87 88 89 90 91 92 93 94
        # check if the device attr is set correctly
        all_ops = main_program.global_block().ops
        device_attr_name = core.op_proto_and_checker_maker.kOpDeviceAttrName()
        for op in all_ops:
            if op.type == 'slice':
                self.assertEqual(op.desc.attr(device_attr_name), "cpu")
            if op.type == 'crop_tensor':
                self.assertEqual(op.desc.attr(device_attr_name), "gpu:1")

        execute(main_program, startup_program)

95
    def test_cpu_only_op(self):
Z
Zhang Ting 已提交
96 97 98
        main_program = paddle.static.Program()
        startup_program = paddle.static.Program()
        with paddle.static.program_guard(main_program, startup_program):
99 100 101 102 103 104
            x = paddle.full(
                shape=[2, 255, 13, 13], fill_value=0.3, dtype='float32'
            )
            gt_box = paddle.full(
                shape=[2, 6, 4], fill_value=0.5, dtype='float32'
            )
Z
Zhang Ting 已提交
105
            gt_label = paddle.full(shape=[2, 6], fill_value=1.0, dtype='int32')
106 107 108
            gt_score = paddle.full(
                shape=[2, 6], fill_value=0.5, dtype='float32'
            )
109
            anchors = [
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
                10,
                13,
                16,
                30,
                33,
                23,
                30,
                61,
                62,
                45,
                59,
                119,
                116,
                90,
                156,
                198,
                373,
                326,
128 129
            ]
            anchor_mask = [0, 1, 2]
Z
Zhang Ting 已提交
130
            with paddle.static.device_guard("gpu"):
131 132
                # yolo_loss only has cpu kernel, so its cpu kernel will be executed
                loss = paddle.vision.ops.yolo_loss(
133 134 135 136 137 138 139 140 141 142
                    x=x,
                    gt_box=gt_box,
                    gt_label=gt_label,
                    gt_score=gt_score,
                    anchors=anchors,
                    anchor_mask=anchor_mask,
                    class_num=80,
                    ignore_thresh=0.7,
                    downsample_ratio=32,
                )
143 144 145 146

        execute(main_program, startup_program)

    def test_without_kernel_op(self):
Z
Zhang Ting 已提交
147 148 149 150 151 152
        main_program = paddle.static.Program()
        startup_program = paddle.static.Program()
        with paddle.static.program_guard(main_program, startup_program):
            i = paddle.full(shape=[1], dtype='int64', fill_value=0)
            loop_len = paddle.full(shape=[1], dtype='int64', fill_value=10)
            cond = paddle.less_than(x=i, y=loop_len)
153 154 155

            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter("always")
Z
Zhang Ting 已提交
156
                with paddle.static.device_guard("cpu"):
157
                    while_op = paddle.static.nn.control_flow.While(cond=cond)
158
                    with while_op.block():
Z
Zhang Ting 已提交
159
                        i = paddle.increment(x=i, value=1)
L
LiYuRio 已提交
160
                        paddle.assign(paddle.less_than(x=i, y=loop_len), cond)
161

162 163 164 165
        warning = "The Op(while) is not support to set device."
        warning_num = get_vaild_warning_num(warning, w)
        assert warning_num == 1

166 167 168 169 170 171 172 173 174 175
        all_ops = main_program.global_block().ops
        device_attr_name = core.op_proto_and_checker_maker.kOpDeviceAttrName()
        for op in all_ops:
            if op.type == 'while':
                self.assertEqual(op.desc.attr(device_attr_name), "")

        execute(main_program, startup_program)

    def test_error(self):
        def device_attr():
Z
Zhang Ting 已提交
176 177
            with paddle.static.device_guard("cpu1"):
                out = paddle.full(shape=[1], fill_value=0.2, dtype='float32')
178

179
        def device_attr2():
Z
Zhang Ting 已提交
180 181
            with paddle.static.device_guard("cpu:1"):
                out = paddle.full(shape=[1], fill_value=0.2, dtype='float32')
182

183
        self.assertRaises(ValueError, device_attr)
184
        self.assertRaises(ValueError, device_attr2)
185

186 187
    # check if op_descs have op_device attr
    def test_op_descs_device_attr(self):
Z
Zhang Ting 已提交
188 189 190
        main_program = paddle.static.Program()
        startup_program = paddle.static.Program()
        with paddle.static.program_guard(main_program, startup_program):
191 192 193 194 195 196
            data1 = paddle.static.data(
                name="data_1", shape=[4, 2], dtype="float32"
            )
            label = paddle.static.data(
                name="label", shape=[4, 1], dtype="int64"
            )
Z
Zhang Ting 已提交
197 198 199 200
            fc1 = paddle.static.nn.fc(x=data1, size=10)
            fc2 = paddle.static.nn.fc(x=fc1, size=10)
            with paddle.static.device_guard("gpu"):
                out = paddle.nn.functional.softmax_with_cross_entropy(
201 202
                    logits=fc1 + fc2, label=label
                )
Z
Zhang Ting 已提交
203 204
                loss = paddle.mean(out)
                opt = paddle.optimizer.SGD(0.1)
205 206 207 208 209 210
                opt.minimize(loss)

        all_ops = main_program.global_block().ops
        device_attr_name = core.op_proto_and_checker_maker.kOpDeviceAttrName()
        for op in all_ops:
            self.assertEqual(True, op.desc.has_attr(device_attr_name))
211 212
            # fill_constant(backward op) is append to mean op, which should have
            # the same op_device value as mean op
213 214 215
            if op.desc == 'fill_constant':
                self.assertEqual(op.desc.attr(device_attr_name), "gpu")

216 217 218

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