aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/autograph/converters/name_scopes.py
diff options
context:
space:
mode:
authorGravatar Dan Moldovan <mdan@google.com>2018-09-11 16:20:49 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-09-11 16:32:19 -0700
commit668c079f4e6020131978b7a812c3b92eea9c47b9 (patch)
tree269836fd98f37b3a099e6b4cceeb3256416705fa /tensorflow/python/autograph/converters/name_scopes.py
parentefd9e0d073a6632f7632f7fe43ae4364cc2c834b (diff)
Move AutoGraph to core. This CL moves the entirety of the code base, keeping the frontend autograph module in contrib for backward compatibility. Certain files, like notebooks and the readme file may be referenced from the outside, so a copy of those is kept as well. In addition, the notebooks subdirectory of examples is also kept in contrib because the extension the build file relies on is not available in the PIP package.
PiperOrigin-RevId: 212543067
Diffstat (limited to 'tensorflow/python/autograph/converters/name_scopes.py')
-rw-r--r--tensorflow/python/autograph/converters/name_scopes.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/tensorflow/python/autograph/converters/name_scopes.py b/tensorflow/python/autograph/converters/name_scopes.py
new file mode 100644
index 0000000000..a9c55ccff0
--- /dev/null
+++ b/tensorflow/python/autograph/converters/name_scopes.py
@@ -0,0 +1,74 @@
+# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
+#
+# 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
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# 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.
+# ==============================================================================
+"""Wraps a function body with a `name_scope` of the function name."""
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import gast
+
+from tensorflow.python.autograph.core import converter
+from tensorflow.python.autograph.pyct import templates
+
+
+class FunctionNameScopeTransformer(converter.Base):
+ """Wrap a function body with a `name_scope` of the function name."""
+
+ def _name_for_current_scope(self):
+ innermost = self.enclosing_entities[-1]
+ if len(self.enclosing_entities) > 1:
+ parent = self.enclosing_entities[-2]
+ if isinstance(parent, gast.ClassDef):
+ # Methods also take the name of their class.
+ name = '%s/%s' % (parent.name, innermost.name)
+ else:
+ name = innermost.name
+ else:
+ name = innermost.name
+
+ # Sanitize the name.
+ # See https://www.tensorflow.org/api_docs/python/tf/Graph#name_scope
+ # TensorFlow doesn't like leading underscores at the top level.
+ while name[0] == '_':
+ name = name[1:]
+ return name
+
+ def visit_FunctionDef(self, node):
+ node = self.generic_visit(node)
+
+ unscoped_body = []
+ scoped_body = node.body
+ if scoped_body:
+ first = scoped_body[0]
+ if isinstance(first, gast.Expr) and isinstance(first.value, gast.Str):
+ # Skip any docstring.
+ unscoped_body = scoped_body[:1]
+ scoped_body = scoped_body[1:]
+
+ template = """
+ with tf.name_scope(scope_name):
+ body
+ """
+ scoped_body = templates.replace(
+ template,
+ scope_name=gast.Str(self._name_for_current_scope()),
+ body=scoped_body)
+ node.body = unscoped_body + scoped_body
+ return node
+
+
+def transform(node, ctx):
+ return FunctionNameScopeTransformer(ctx).visit(node)