test_print_op.py 5.5 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.

Y
Yan Chunwei 已提交
15
import unittest
16 17 18 19 20

import numpy as np

from op_test import OpTest
import paddle
21
import paddle.fluid as fluid
22
import paddle.fluid.layers as layers
23
from paddle.fluid import core
24
from paddle.fluid.framework import switch_main_program
25
from simple_nets import simple_fc_net, init_data
26 27 28
from paddle.static import Program, program_guard

paddle.enable_static()
Y
yangyaming 已提交
29 30 31


class TestPrintOpCPU(unittest.TestCase):
32

Y
yangyaming 已提交
33
    def setUp(self):
34 35
        self.place = paddle.CPUPlace()
        self.x_tensor = fluid.core.LoDTensor()
Y
yangyaming 已提交
36 37
        tensor_np = np.random.random(size=(2, 3)).astype('float32')
        self.x_tensor.set(tensor_np, self.place)
38
        self.x_tensor.set_recursive_sequence_lengths([[1, 1]])
Y
Yan Chunwei 已提交
39

Y
yangyaming 已提交
40 41 42
    def build_network(self, only_forward, **kargs):
        x = layers.data('x', shape=[3], dtype='float32', lod_level=1)
        x.stop_gradient = False
43 44 45
        paddle.static.Print(input=x, **kargs)
        loss = paddle.mean(x)
        paddle.static.append_backward(loss=loss)
Y
yangyaming 已提交
46
        return loss
Y
Yan Chunwei 已提交
47

Y
yangyaming 已提交
48 49 50
    def test_forward(self):
        switch_main_program(Program())
        printed = self.build_network(True, print_phase='forward')
51
        exe = paddle.static.Executor(self.place)
Y
yangyaming 已提交
52 53 54
        outs = exe.run(feed={'x': self.x_tensor},
                       fetch_list=[printed],
                       return_numpy=False)
Y
Yan Chunwei 已提交
55

Y
yangyaming 已提交
56 57 58
    def test_backward(self):
        switch_main_program(Program())
        loss = self.build_network(False, print_phase='backward')
59
        exe = paddle.static.Executor(self.place)
Y
yangyaming 已提交
60 61 62
        outs = exe.run(feed={'x': self.x_tensor},
                       fetch_list=[loss],
                       return_numpy=False)
Y
Yan Chunwei 已提交
63

64 65 66 67 68 69 70 71
    def test_all_parameters(self):
        x = layers.data('x', shape=[3], dtype='float32', lod_level=1)
        x.stop_gradient = False

        for print_tensor_name in [True, False]:
            for print_tensor_type in [True, False]:
                for print_tensor_shape in [True, False]:
                    for print_tensor_lod in [True, False]:
72
                        paddle.static.Print(
73 74 75 76
                            input=x,
                            print_tensor_name=print_tensor_name,
                            print_tensor_type=print_tensor_type,
                            print_tensor_shape=print_tensor_shape,
77 78
                            print_tensor_lod=print_tensor_lod,
                        )
79 80 81
        loss = paddle.mean(x)
        paddle.static.append_backward(loss=loss)
        exe = paddle.static.Executor(self.place)
82 83 84 85
        outs = exe.run(feed={'x': self.x_tensor},
                       fetch_list=[loss],
                       return_numpy=False)

86 87 88
    def test_no_summarize(self):
        switch_main_program(Program())
        printed = self.build_network(True, summarize=-1, print_phase='forward')
89
        exe = paddle.static.Executor(self.place)
90 91 92 93
        outs = exe.run(feed={'x': self.x_tensor},
                       fetch_list=[printed],
                       return_numpy=False)

Y
Yan Chunwei 已提交
94

95
class TestPrintOpError(unittest.TestCase):
96

97 98 99
    def test_errors(self):
        with program_guard(Program(), Program()):
            # The input type of Print_op must be Variable.
100 101
            x1 = fluid.create_lod_tensor(np.array([[-1]]), [[1]],
                                         paddle.CPUPlace())
102
            self.assertRaises(TypeError, paddle.static.Print, x1)
103
            # The input dtype of Print_op must be float32, float64, int32_t, int64_t or bool.
104 105
            x2 = paddle.static.data(name='x2', shape=[4], dtype="float16")
            self.assertRaises(TypeError, paddle.static.Print, x2)
106 107


108 109
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
Y
yangyaming 已提交
110
class TestPrintOpGPU(TestPrintOpCPU):
111

Y
yangyaming 已提交
112
    def setUp(self):
113 114
        self.place = paddle.CUDAPlace(0)
        self.x_tensor = fluid.core.LoDTensor()
Y
yangyaming 已提交
115 116
        tensor_np = np.random.random(size=(2, 3)).astype('float32')
        self.x_tensor.set(tensor_np, self.place)
117
        self.x_tensor.set_recursive_sequence_lengths([[1, 1]])
Y
Yan Chunwei 已提交
118 119


120
class TestPrintOpBackward(unittest.TestCase):
121

122
    def check_backward(self, use_cuda):
123 124
        main = paddle.static.Program()
        startup = paddle.static.Program()
125

126
        with program_guard(main, startup):
127
            loss = simple_fc_net()
128 129
            loss = paddle.static.Print(loss)
            paddle.optimizer.Adam().minimize(loss)
130 131 132 133

        print_ops = [op for op in main.blocks[0].ops if op.type == u'print']
        assert len(print_ops) == 2, "The number of print op should be 2"

134 135
        place = paddle.CUDAPlace(0) if use_cuda else paddle.CPUPlace()
        exe = paddle.static.Executor(place)
136 137
        exe.run(startup)

138
        binary = paddle.static.CompiledProgram(main).with_data_parallel(
139 140 141 142 143 144 145
            loss_name=loss.name)

        img, label = init_data()
        feed_dict = {"image": img, "label": label}
        exe.run(binary, feed_dict)

    def test_fw_bw(self):
146
        if paddle.is_compiled_with_cuda():
147 148 149 150
            self.check_backward(use_cuda=True)
        self.check_backward(use_cuda=False)


Y
Yan Chunwei 已提交
151 152
if __name__ == '__main__':
    unittest.main()