def test_paddle_api_with_andOr(self): code_or = """ def foo(x): if 2 > 1 and fluid.layers.shape(x)[0] > 16 or x is not None : x = x + 1 return x """ code_and = """ def foo(x): if 2 > 1 and fluid.layers.shape(x)[0] > 16 and x is not None : x = x + 1 return x """ for code in [code_or, code_and]: code = textwrap.dedent(code) node = gast.parse(code) static_analysis_visitor = StaticAnalysisVisitor(node) test_node = node.body[0].body[0].test if_visitor = IfConditionVisitor(test_node, static_analysis_visitor) self.assertTrue(if_visitor.is_control_flow()) new_node, assign_nodes = if_visitor.transform() # Transformation result: # bool_tensor_0 = fluid.layers.fill_constant(shape=[1], dtype='bool', value=bool(2 > 1)) # bool_tensor_1 = fluid.layers.fill_constant(shape=[1], dtype='bool', value=bool(x is not None)) # logic_and_0 = fluid.layers.logical_and(x=bool_tensor_0, y=fluid.layers.shape(x)[0] > 16) # logic_and_1 = fluid.layers.logical_and(x=logic_and_0, y=bool_tensor_1) for code_and # logic_or_0= fluid.layers.logical_or(x=logic_and_0, y=bool_tensor_1) for code_and self.assertTrue(isinstance(new_node, gast.Name)) self.assertTrue(len(assign_nodes) == 4)
def test_if(self): node = gast.parse("x.numpy()[1] > 1") node_test = node.body[0].value if_visitor = IfConditionVisitor(node_test) self.assertTrue(if_visitor.is_control_flow()) # No transformation will be applied. new_node, assign_nodes = if_visitor.transform() self.assertTrue(len(assign_nodes) == 0)
def test_if_with_or(self): node = gast.parse("1 < fluid.layers.sum(x).numpy()[2] or x+y < 0") node_test = node.body[0].value if_visitor = IfConditionVisitor(node_test) self.assertTrue(if_visitor.is_control_flow()) # No transformation will be applied. new_node, assign_nodes = if_visitor.transform() self.assertTrue(isinstance(new_node, gast.Name)) self.assertTrue(len(assign_nodes) == 2)
def test_if_with_and(self): node = gast.parse("x and 1 < x.numpy()[1]") node_test = node.body[0].value if_visitor = IfConditionVisitor(node_test) self.assertTrue(if_visitor.is_control_flow()) # No transformation will be applied. new_node, assign_nodes = if_visitor.transform() self.assertTrue(isinstance(new_node, gast.Name)) self.assertTrue(len(assign_nodes) == 2)
def check_false_case(self, code): code = textwrap.dedent(code) node = gast.parse(code) node_test = node.body[0].value if_visitor = IfConditionVisitor(node_test) self.assertFalse(if_visitor.is_control_flow()) # No transformation will be applied. new_node, assign_nodes = if_visitor.transform() self.assertTrue(new_node == node_test) self.assertTrue(len(assign_nodes) == 0)
def test_is_None4(self): node = gast.parse("fluid.layers.sum(x) and 2>1") node_test = node.body[0].value if_visitor = IfConditionVisitor(node_test) self.assertTrue(if_visitor.is_control_flow()) # Transformation result: # bool_tensor_0 = fluid.layers.cast(x=fluid.layers.sum(x), dtype='bool') # bool_tensor_1 = fluid.layers.fill_constant(shape=[1], dtype='bool', value=bool(2 > 1)) # logic_and_0 = fluid.layers.logical_and(x=bool_tensor_0, y=bool_tensor_1) new_node, assign_nodes = if_visitor.transform() self.assertTrue(len(assign_nodes) == 3)
def test_paddle_api(self): code = """ def foo(x): if fluid.layers.shape(x)[0] > 16: x = x + 1 return x """ code = textwrap.dedent(code) node = gast.parse(code) static_analysis_visitor = StaticAnalysisVisitor(node) test_node = node.body[0].body[0].test if_visitor = IfConditionVisitor(test_node, static_analysis_visitor) self.assertTrue(if_visitor.is_control_flow()) # No transformation will be applied. new_node, assign_nodes = if_visitor.transform() self.assertTrue(new_node == test_node) self.assertTrue(len(assign_nodes) == 0)
def test_shape_with_andOr(self): code = """ def foo(x): batch_size = fluid.layers.shape(x) if x is not None and batch_size[0] > 16 or 2 > 1: x = x + 1 return x """ code = textwrap.dedent(code) node = gast.parse(code) static_analysis_visitor = StaticAnalysisVisitor(node) test_node = node.body[0].body[1].test if_visitor = IfConditionVisitor(test_node, static_analysis_visitor) self.assertTrue(if_visitor.is_control_flow()) new_node, assign_nodes = if_visitor.transform() # transformation result: # bool_tensor_0 = fluid.layers.fill_constant(shape=[1], dtype='bool', value=bool(x is not None)) # logic_and_0 = fluid.layers.logical_and(x=bool_tensor_0, y=batch_size[0] > 16) # bool_tensor_1 = fluid.layers.fill_constant(shape=[1], dtype='bool', value=bool(2 > 1)) # logic_or_0 = fluid.layers.logical_or(x=logic_and_0, y=bool_tensor_1) self.assertTrue(isinstance(new_node, gast.Name)) self.assertTrue(len(assign_nodes) == 4)
def test_raise_error(self): node = "a + b" with self.assertRaises(Exception) as e: self.assertRaises(TypeError, IfConditionVisitor(node)) self.assertTrue( "Type of input node should be gast.AST" in str(e.exception))