Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dy2stat] Fix to_tensor Bug Reported from QA #32701

Merged
merged 3 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ def __init__(self, wrapper_root):
self.root = wrapper_root.node
self.class_node_dict = {}

self.name_to_tensor_shape = {}

def transform(self):
to_tensor_transformer = ToTensorTransformer(self.root)
to_tensor_transformer.transform()
self.visit(self.root)

return self.wrapper_root

def visit_Assign(self, node):
Expand All @@ -62,11 +63,6 @@ def visit_Expr(self, node):

def _visit_Call(self, node):
assert isinstance(node, gast.Call)
# Replace API `to_variable` with `fluid.layers.assign`
if is_to_variable(node):
node = to_assign_node(node)
return node

func_name = astor.to_source(gast.gast_to_ast(node.func))

if self._is_dygraph_forward(func_name):
Expand Down Expand Up @@ -102,6 +98,29 @@ def _update_class_node_dict(self, node):
return False


class ToTensorTransformer(gast.NodeTransformer):
"""
Class to transform paddle.to_tensor and paddle.to_variable to paddle.assign
"""

def __init__(self, node):
assert isinstance(
node, gast.AST
), "Input non-gast.AST node for the initialization of ToTensorTransformer."
self.root = node

def transform(self):
self.visit(self.root)
return self.root

def visit_Call(self, node):
assert isinstance(node, gast.Call)
if is_to_variable(node):
node = to_assign_node(node)
self.generic_visit(node)
return node


def is_to_variable(node):
assert isinstance(node, gast.Call)
api_name = utils.ast_to_source_code(node.func).strip()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ def dyfunc_int_to_tensor(x):


def dyfunc_float_to_tensor(x):
res = paddle.to_tensor(2.0)
return res
return paddle.to_tensor(2.0)


def dyfunc_bool_to_tensor(x):
res = paddle.to_tensor(True)
return res
return paddle.to_tensor(True)


class TestDygraphBasicApi_ToVariable(unittest.TestCase):
Expand Down