message: Avoid re-entering the default graph when building a function.
diff: @@ -5875,8 +5875,9 @@ def _get_graph_from_inputs(op_input_list, graph=None):
  The appropriate graph to use for the given inputs.
  """
  - if get_default_graph().building_function:
  - return get_default_graph()
  + current_default_graph = get_default_graph()
  + if current_default_graph.building_function:
  + return current_default_graph
  op_input_list = tuple(op_input_list) # Handle generators correctly
  if graph and not isinstance(graph, Graph):
  @@ -5909,7 +5910,7 @@ def _get_graph_from_inputs(op_input_list, graph=None):
  raise ValueError("%s is not from the passed-in graph." % graph_element)
  # 2. If all else fails, we use the default graph, which is always there.
  - return graph or get_default_graph()
  + return graph or current_default_graph
  @tf_export(v1=["GraphKeys"])
  @@ -6254,15 +6255,21 @@ class name_scope(object): # pylint: disable=invalid-name
  raise ValueError(
  "At least one of name (%s) and default_name (%s) must be provided."
  % (self._name, self._default_name))
  - if self._values is None:
  - self._values = []
  - if self._values:
  - g = _get_graph_from_inputs(self._values)
  +
  + g = get_default_graph()
  + if self._values and not g.building_function:
  + # Specialize based on the knowledge that `_get_graph_from_inputs()`
  + # ignores `inputs` when building a function.
  + g_from_inputs = _get_graph_from_inputs(self._values)
  + if g_from_inputs is not g:
  + g = g_from_inputs
  self._g_manager = g.as_default()
  self._g_manager.__enter__()
  else:
  - g = get_default_graph()
  self._g_manager = None
  + else:
  + self._g_manager = None
  +
  try:
  self._name_scope = g.name_scope(self._name)
  return self._name_scope.__enter__()
  