def test_from_tensor(self): x_bool = fluid.layers.fill_constant(shape=[1], dtype='bool', value=True) bool_spec = InputSpec.from_tensor(x_bool) self.assertEqual(bool_spec.dtype, x_bool.dtype) self.assertEqual(list(bool_spec.shape), list(x_bool.shape)) self.assertEqual(bool_spec.name, x_bool.name) bool_spec2 = InputSpec.from_tensor(x_bool, name='bool_spec') self.assertEqual(bool_spec2.name, bool_spec2.name)
def test_set_op_attrs(self): net = NetWithOpAttr(self.in_num, self.out_num) # set attrs net.linear._set_op_attrs(self.fc_attrs) net.bn._set_op_attrs({"bool_val": False}) # test overwrite behavior net.bn._set_op_attrs(self.bn_attrs) net.sub._set_op_attrs(self.sub_attrs) # assert hooks exist. self.assertEqual(len(net.linear._forward_pre_hooks), 1) self.assertEqual(len(net.linear._forward_post_hooks), 1) # to_static net = paddle.jit.to_static(net, input_spec=[InputSpec.from_tensor(self.x)]) # assert attrs have be set. self.check_op_attrs(net.forward.concrete_program.main_program) # assert hooks have be clean. self.assertEqual(len(net.linear._forward_pre_hooks), 0) self.assertEqual(len(net.linear._forward_post_hooks), 0)