You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
5.4 KiB
155 lines
5.4 KiB
7 years ago
|
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||
7 years ago
|
#
|
||
7 years ago
|
# 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
|
||
7 years ago
|
#
|
||
7 years ago
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
7 years ago
|
#
|
||
7 years ago
|
# 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.
|
||
|
|
||
7 years ago
|
from __future__ import print_function
|
||
7 years ago
|
import unittest
|
||
7 years ago
|
|
||
7 years ago
|
from paddle.v2.fluid.framework import Program, default_main_program, program_guard, grad_var_name
|
||
7 years ago
|
import paddle.v2.fluid.layers as layers
|
||
7 years ago
|
|
||
7 years ago
|
main_program = default_main_program()
|
||
|
|
||
7 years ago
|
|
||
|
class TestProgram(unittest.TestCase):
|
||
|
def test_program(self):
|
||
7 years ago
|
b = main_program.current_block()
|
||
7 years ago
|
self.assertEqual(-1, b.parent_idx)
|
||
|
self.assertEqual(0, b.idx)
|
||
|
|
||
7 years ago
|
b = main_program.create_block()
|
||
7 years ago
|
self.assertEqual(1, b.idx)
|
||
|
self.assertEqual(0, b.parent_idx)
|
||
|
|
||
7 years ago
|
b = main_program.create_block()
|
||
7 years ago
|
self.assertEqual(2, b.idx)
|
||
|
self.assertEqual(1, b.parent_idx)
|
||
|
|
||
7 years ago
|
main_program.rollback()
|
||
7 years ago
|
|
||
7 years ago
|
b = main_program.current_block()
|
||
7 years ago
|
self.assertEqual(1, b.idx)
|
||
|
self.assertEqual(0, b.parent_idx)
|
||
|
|
||
7 years ago
|
b = main_program.create_block()
|
||
7 years ago
|
self.assertEqual(3, b.idx)
|
||
|
self.assertEqual(1, b.parent_idx)
|
||
|
|
||
7 years ago
|
main_program.rollback()
|
||
|
b = main_program.current_block()
|
||
7 years ago
|
self.assertEqual(1, b.idx)
|
||
|
self.assertEqual(0, b.parent_idx)
|
||
|
|
||
7 years ago
|
def test_program_clone(self):
|
||
|
prog = Program()
|
||
|
|
||
|
x = prog.global_block().create_var(
|
||
|
name='X', shape=[1000, 784], dtype='float32')
|
||
|
|
||
|
y = prog.global_block().create_var(
|
||
|
name='Y', shape=[784, 100], dtype='float32')
|
||
|
out = prog.global_block().create_var(name='Out', dtype='float32')
|
||
|
prog.global_block().append_op(
|
||
|
type="mul", inputs={'X': [x],
|
||
|
'Y': [y]}, outputs={'Out': [out]})
|
||
|
|
||
|
# FIXME(yuyang18): We manual compare the output string, since the order
|
||
|
# of variable could be changed.
|
||
7 years ago
|
print(prog)
|
||
|
print(prog.clone())
|
||
7 years ago
|
|
||
7 years ago
|
def test_parse_program_from_string(self):
|
||
|
prog = Program()
|
||
|
|
||
|
x = prog.global_block().create_var(
|
||
|
name='X', shape=[1000, 784], dtype='float32')
|
||
|
|
||
|
y = prog.global_block().create_var(
|
||
|
name='Y', shape=[784, 100], dtype='float32')
|
||
|
out = prog.global_block().create_var(name='Out', dtype='float32')
|
||
|
prog.global_block().append_op(
|
||
|
type="mul", inputs={'X': [x],
|
||
|
'Y': [y]}, outputs={'Out': [out]})
|
||
|
|
||
|
binary_str = prog.desc.serialize_to_string()
|
||
|
prog_restored = Program.parse_from_string(binary_str)
|
||
|
|
||
7 years ago
|
print(prog)
|
||
|
print(prog_restored)
|
||
7 years ago
|
|
||
7 years ago
|
def test_append_backward(self):
|
||
7 years ago
|
prog = Program()
|
||
7 years ago
|
block = prog.global_block()
|
||
|
|
||
7 years ago
|
mul_x = block.create_var(
|
||
7 years ago
|
dtype="float32", shape=[5, 10], lod_level=0, name="mul.x")
|
||
|
mul_y = block.create_var(
|
||
|
dtype="float32", shape=[10, 8], lod_level=0, name="mul.y")
|
||
|
mul_out = block.create_var(
|
||
|
dtype="float32", shape=[5, 8], lod_level=0, name="mul.out")
|
||
|
mul_op = block.append_op(
|
||
|
type="mul",
|
||
|
inputs={"X": [mul_x],
|
||
|
"Y": mul_y},
|
||
|
outputs={"Out": [mul_out]},
|
||
|
attrs={"x_num_col_dims": 1})
|
||
7 years ago
|
|
||
|
add_y = block.create_var(
|
||
|
dtype="float32", shape=[5, 8], lod_level=0, name="add.y")
|
||
|
add_out = block.create_var(
|
||
|
dtype="float32", shape=[5, 8], lod_level=0, name="add.out")
|
||
|
add_op = block.append_op(
|
||
|
type="elementwise_add",
|
||
|
inputs={"X": mul_out,
|
||
|
"Y": add_y},
|
||
|
outputs={"Out": add_out},
|
||
|
attrs={"x_num_col_dims": 1})
|
||
7 years ago
|
mean_out = block.create_var(
|
||
|
dtype="float32", shape=[1], lod_level=0, name="mean.out")
|
||
|
block.append_op(
|
||
|
type="mean", inputs={"X": add_out}, outputs={"Out": mean_out})
|
||
7 years ago
|
|
||
7 years ago
|
self.assertEqual(mul_op.idx, 0)
|
||
|
self.assertEqual(add_op.idx, 1)
|
||
7 years ago
|
param_to_grad = prog.append_backward(mean_out, set())
|
||
7 years ago
|
|
||
7 years ago
|
for var_name in ("mul.x", "mul.y", "mul.out", "add.y", "add.out",
|
||
|
"mean.out"):
|
||
7 years ago
|
self.assertEqual(param_to_grad[var_name][0],
|
||
|
grad_var_name(var_name))
|
||
7 years ago
|
self.assertEqual(param_to_grad[var_name][1], 0)
|
||
|
|
||
|
expect_ops = [
|
||
7 years ago
|
"mul", "elementwise_add", "mean", "fill_constant", "mean_grad",
|
||
|
"elementwise_add_grad", "mul_grad"
|
||
7 years ago
|
]
|
||
|
actual_ops = []
|
||
|
for op in block.ops:
|
||
|
actual_ops.append(op.type)
|
||
|
self.assertEqual(actual_ops, expect_ops)
|
||
7 years ago
|
|
||
7 years ago
|
def test_program_clone_with_parameter(self):
|
||
|
main_program = Program()
|
||
|
startup_program = Program()
|
||
7 years ago
|
with program_guard(main_program, startup_program):
|
||
|
d = layers.data(name='x', shape=[784], dtype='float32')
|
||
|
hidden = layers.fc(input=d, size=100)
|
||
|
layers.fc(input=hidden, size=100)
|
||
7 years ago
|
|
||
|
new_program = main_program.clone()
|
||
|
self.assertNotEqual(0, len(new_program.blocks[0].all_parameters()))
|
||
|
|
||
7 years ago
|
|
||
|
if __name__ == '__main__':
|
||
|
unittest.main()
|