• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Common values and methods for TensorFlow Debugger."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import collections
21import json
22
23GRPC_URL_PREFIX = "grpc://"
24
25# A key for a Session.run() call.
26RunKey = collections.namedtuple("RunKey", ["feed_names", "fetch_names"])
27
28
29def get_graph_element_name(elem):
30  """Obtain the name or string representation of a graph element.
31
32  If the graph element has the attribute "name", return name. Otherwise, return
33  a __str__ representation of the graph element. Certain graph elements, such as
34  `SparseTensor`s, do not have the attribute "name".
35
36  Args:
37    elem: The graph element in question.
38
39  Returns:
40    If the attribute 'name' is available, return the name. Otherwise, return
41    str(fetch).
42  """
43
44  return elem.name if hasattr(elem, "name") else str(elem)
45
46
47def get_flattened_names(feeds_or_fetches):
48  """Get a flattened list of the names in run() call feeds or fetches.
49
50  Args:
51    feeds_or_fetches: Feeds or fetches of the `Session.run()` call. It maybe
52      a Tensor, an Operation or a Variable. It may also be nested lists, tuples
53      or dicts. See doc of `Session.run()` for more details.
54
55  Returns:
56    (list of str) A flattened list of fetch names from `feeds_or_fetches`.
57  """
58
59  lines = []
60  if isinstance(feeds_or_fetches, (list, tuple)):
61    for item in feeds_or_fetches:
62      lines.extend(get_flattened_names(item))
63  elif isinstance(feeds_or_fetches, dict):
64    for key in feeds_or_fetches:
65      lines.extend(get_flattened_names(feeds_or_fetches[key]))
66  else:
67    # This ought to be a Tensor, an Operation or a Variable, for which the name
68    # attribute should be available. (Bottom-out condition of the recursion.)
69    lines.append(get_graph_element_name(feeds_or_fetches))
70
71  return lines
72
73
74def get_run_key(feed_dict, fetches):
75  """Summarize the names of feeds and fetches as a RunKey JSON string.
76
77  Args:
78    feed_dict: The feed_dict given to the `Session.run()` call.
79    fetches: The fetches from the `Session.run()` call.
80
81  Returns:
82    A JSON Array consisting of two items. They first items is a flattened
83    Array of the names of the feeds. The second item is a flattened Array of
84    the names of the fetches.
85  """
86  return json.dumps(RunKey(get_flattened_names(feed_dict),
87                           get_flattened_names(fetches)))
88