1# Copyright 2016 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"""Tests for TensorFlow Debugger (tfdbg) Utilities.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20import numpy as np 21 22from tensorflow.core.protobuf import config_pb2 23from tensorflow.python.client import session 24from tensorflow.python.debug.lib import debug_utils 25from tensorflow.python.framework import constant_op 26from tensorflow.python.framework import test_util 27from tensorflow.python.ops import math_ops 28# Import resource_variable_ops for the variables-to-tensor implicit conversion. 29from tensorflow.python.ops import resource_variable_ops # pylint: disable=unused-import 30from tensorflow.python.ops import variables 31from tensorflow.python.platform import googletest 32 33 34class DebugUtilsTest(test_util.TensorFlowTestCase): 35 36 @classmethod 37 def setUpClass(cls): 38 cls._sess = session.Session() 39 with cls._sess: 40 cls._a_init_val = np.array([[5.0, 3.0], [-1.0, 0.0]]) 41 cls._b_init_val = np.array([[2.0], [-1.0]]) 42 cls._c_val = np.array([[-4.0], [np.nan]]) 43 44 cls._a_init = constant_op.constant( 45 cls._a_init_val, shape=[2, 2], name="a1_init") 46 cls._b_init = constant_op.constant( 47 cls._b_init_val, shape=[2, 1], name="b_init") 48 49 cls._a = variables.Variable(cls._a_init, name="a1") 50 cls._b = variables.Variable(cls._b_init, name="b") 51 cls._c = constant_op.constant(cls._c_val, shape=[2, 1], name="c") 52 53 # Matrix product of a and b. 54 cls._p = math_ops.matmul(cls._a, cls._b, name="p1") 55 56 # Sum of two vectors. 57 cls._s = math_ops.add(cls._p, cls._c, name="s") 58 59 cls._graph = cls._sess.graph 60 61 # These are all the expected nodes in the graph: 62 # Two variables (a, b), each with four nodes (Variable, init, Assign, 63 # read). 64 # One constant (c). 65 # One add operation and one matmul operation. 66 cls._expected_num_nodes = 4 * 2 + 1 + 1 + 1 67 68 def setUp(self): 69 self._run_options = config_pb2.RunOptions() 70 71 def _verify_watches(self, watch_opts, expected_output_slot, 72 expected_debug_ops, expected_debug_urls): 73 """Verify a list of debug tensor watches. 74 75 This requires all watches in the watch list have exactly the same 76 output_slot, debug_ops and debug_urls. 77 78 Args: 79 watch_opts: Repeated protobuf field of DebugTensorWatch. 80 expected_output_slot: Expected output slot index, as an integer. 81 expected_debug_ops: Expected debug ops, as a list of strings. 82 expected_debug_urls: Expected debug URLs, as a list of strings. 83 84 Returns: 85 List of node names from the list of debug tensor watches. 86 """ 87 node_names = [] 88 for watch in watch_opts: 89 node_names.append(watch.node_name) 90 91 self.assertEqual(expected_output_slot, watch.output_slot) 92 self.assertEqual(expected_debug_ops, watch.debug_ops) 93 self.assertEqual(expected_debug_urls, watch.debug_urls) 94 95 return node_names 96 97 def testAddDebugTensorWatches_defaultDebugOp(self): 98 debug_utils.add_debug_tensor_watch( 99 self._run_options, "foo/node_a", 1, debug_urls="file:///tmp/tfdbg_1") 100 debug_utils.add_debug_tensor_watch( 101 self._run_options, "foo/node_b", 0, debug_urls="file:///tmp/tfdbg_2") 102 103 debug_watch_opts = self._run_options.debug_options.debug_tensor_watch_opts 104 self.assertEqual(2, len(debug_watch_opts)) 105 106 watch_0 = debug_watch_opts[0] 107 watch_1 = debug_watch_opts[1] 108 109 self.assertEqual("foo/node_a", watch_0.node_name) 110 self.assertEqual(1, watch_0.output_slot) 111 self.assertEqual("foo/node_b", watch_1.node_name) 112 self.assertEqual(0, watch_1.output_slot) 113 # Verify default debug op name. 114 self.assertEqual(["DebugIdentity"], watch_0.debug_ops) 115 self.assertEqual(["DebugIdentity"], watch_1.debug_ops) 116 117 # Verify debug URLs. 118 self.assertEqual(["file:///tmp/tfdbg_1"], watch_0.debug_urls) 119 self.assertEqual(["file:///tmp/tfdbg_2"], watch_1.debug_urls) 120 121 def testAddDebugTensorWatches_explicitDebugOp(self): 122 debug_utils.add_debug_tensor_watch( 123 self._run_options, 124 "foo/node_a", 125 0, 126 debug_ops="DebugNanCount", 127 debug_urls="file:///tmp/tfdbg_1") 128 129 debug_watch_opts = self._run_options.debug_options.debug_tensor_watch_opts 130 self.assertEqual(1, len(debug_watch_opts)) 131 132 watch_0 = debug_watch_opts[0] 133 134 self.assertEqual("foo/node_a", watch_0.node_name) 135 self.assertEqual(0, watch_0.output_slot) 136 137 # Verify default debug op name. 138 self.assertEqual(["DebugNanCount"], watch_0.debug_ops) 139 140 # Verify debug URLs. 141 self.assertEqual(["file:///tmp/tfdbg_1"], watch_0.debug_urls) 142 143 def testAddDebugTensorWatches_multipleDebugOps(self): 144 debug_utils.add_debug_tensor_watch( 145 self._run_options, 146 "foo/node_a", 147 0, 148 debug_ops=["DebugNanCount", "DebugIdentity"], 149 debug_urls="file:///tmp/tfdbg_1") 150 151 debug_watch_opts = self._run_options.debug_options.debug_tensor_watch_opts 152 self.assertEqual(1, len(debug_watch_opts)) 153 154 watch_0 = debug_watch_opts[0] 155 156 self.assertEqual("foo/node_a", watch_0.node_name) 157 self.assertEqual(0, watch_0.output_slot) 158 159 # Verify default debug op name. 160 self.assertEqual(["DebugNanCount", "DebugIdentity"], watch_0.debug_ops) 161 162 # Verify debug URLs. 163 self.assertEqual(["file:///tmp/tfdbg_1"], watch_0.debug_urls) 164 165 def testAddDebugTensorWatches_multipleURLs(self): 166 debug_utils.add_debug_tensor_watch( 167 self._run_options, 168 "foo/node_a", 169 0, 170 debug_ops="DebugNanCount", 171 debug_urls=["file:///tmp/tfdbg_1", "file:///tmp/tfdbg_2"]) 172 173 debug_watch_opts = self._run_options.debug_options.debug_tensor_watch_opts 174 self.assertEqual(1, len(debug_watch_opts)) 175 176 watch_0 = debug_watch_opts[0] 177 178 self.assertEqual("foo/node_a", watch_0.node_name) 179 self.assertEqual(0, watch_0.output_slot) 180 181 # Verify default debug op name. 182 self.assertEqual(["DebugNanCount"], watch_0.debug_ops) 183 184 # Verify debug URLs. 185 self.assertEqual(["file:///tmp/tfdbg_1", "file:///tmp/tfdbg_2"], 186 watch_0.debug_urls) 187 188 def testWatchGraph_allNodes(self): 189 debug_utils.watch_graph( 190 self._run_options, 191 self._graph, 192 debug_ops=["DebugIdentity", "DebugNanCount"], 193 debug_urls="file:///tmp/tfdbg_1") 194 195 debug_watch_opts = self._run_options.debug_options.debug_tensor_watch_opts 196 self.assertEqual(self._expected_num_nodes, len(debug_watch_opts)) 197 198 # Verify that each of the nodes in the graph with output tensors in the 199 # graph have debug tensor watch. 200 node_names = self._verify_watches(debug_watch_opts, 0, 201 ["DebugIdentity", "DebugNanCount"], 202 ["file:///tmp/tfdbg_1"]) 203 204 # Verify the node names. 205 self.assertTrue("a1_init" in node_names) 206 self.assertTrue("a1" in node_names) 207 self.assertTrue("a1/Assign" in node_names) 208 self.assertTrue("a1/read" in node_names) 209 210 self.assertTrue("b_init" in node_names) 211 self.assertTrue("b" in node_names) 212 self.assertTrue("b/Assign" in node_names) 213 self.assertTrue("b/read" in node_names) 214 215 self.assertTrue("c" in node_names) 216 self.assertTrue("p1" in node_names) 217 self.assertTrue("s" in node_names) 218 219 def testWatchGraph_nodeNameWhitelist(self): 220 debug_utils.watch_graph( 221 self._run_options, 222 self._graph, 223 debug_urls="file:///tmp/tfdbg_1", 224 node_name_regex_whitelist="(a1$|a1_init$|a1/.*|p1$)") 225 226 node_names = self._verify_watches( 227 self._run_options.debug_options.debug_tensor_watch_opts, 0, 228 ["DebugIdentity"], ["file:///tmp/tfdbg_1"]) 229 self.assertEqual( 230 sorted(["a1_init", "a1", "a1/Assign", "a1/read", "p1"]), 231 sorted(node_names)) 232 233 def testWatchGraph_opTypeWhitelist(self): 234 debug_utils.watch_graph( 235 self._run_options, 236 self._graph, 237 debug_urls="file:///tmp/tfdbg_1", 238 op_type_regex_whitelist="(Variable|MatMul)") 239 240 node_names = self._verify_watches( 241 self._run_options.debug_options.debug_tensor_watch_opts, 0, 242 ["DebugIdentity"], ["file:///tmp/tfdbg_1"]) 243 self.assertEqual(sorted(["a1", "b", "p1"]), sorted(node_names)) 244 245 def testWatchGraph_nodeNameAndOpTypeWhitelists(self): 246 debug_utils.watch_graph( 247 self._run_options, 248 self._graph, 249 debug_urls="file:///tmp/tfdbg_1", 250 node_name_regex_whitelist="([a-z]+1$)", 251 op_type_regex_whitelist="(MatMul)") 252 253 node_names = self._verify_watches( 254 self._run_options.debug_options.debug_tensor_watch_opts, 0, 255 ["DebugIdentity"], ["file:///tmp/tfdbg_1"]) 256 self.assertEqual(["p1"], node_names) 257 258 def testWatchGraph_tensorDTypeWhitelist(self): 259 debug_utils.watch_graph( 260 self._run_options, 261 self._graph, 262 debug_urls="file:///tmp/tfdbg_1", 263 tensor_dtype_regex_whitelist=".*_ref") 264 265 node_names = self._verify_watches( 266 self._run_options.debug_options.debug_tensor_watch_opts, 0, 267 ["DebugIdentity"], ["file:///tmp/tfdbg_1"]) 268 self.assertItemsEqual(["a1", "a1/Assign", "b", "b/Assign"], node_names) 269 270 def testWatchGraph_nodeNameAndTensorDTypeWhitelists(self): 271 debug_utils.watch_graph( 272 self._run_options, 273 self._graph, 274 debug_urls="file:///tmp/tfdbg_1", 275 node_name_regex_whitelist="^a.*", 276 tensor_dtype_regex_whitelist=".*_ref") 277 278 node_names = self._verify_watches( 279 self._run_options.debug_options.debug_tensor_watch_opts, 0, 280 ["DebugIdentity"], ["file:///tmp/tfdbg_1"]) 281 self.assertItemsEqual(["a1", "a1/Assign"], node_names) 282 283 def testWatchGraph_nodeNameBlacklist(self): 284 debug_utils.watch_graph_with_blacklists( 285 self._run_options, 286 self._graph, 287 debug_urls="file:///tmp/tfdbg_1", 288 node_name_regex_blacklist="(a1$|a1_init$|a1/.*|p1$)") 289 290 node_names = self._verify_watches( 291 self._run_options.debug_options.debug_tensor_watch_opts, 0, 292 ["DebugIdentity"], ["file:///tmp/tfdbg_1"]) 293 self.assertEqual( 294 sorted(["b_init", "b", "b/Assign", "b/read", "c", "s"]), 295 sorted(node_names)) 296 297 def testWatchGraph_opTypeBlacklist(self): 298 debug_utils.watch_graph_with_blacklists( 299 self._run_options, 300 self._graph, 301 debug_urls="file:///tmp/tfdbg_1", 302 op_type_regex_blacklist="(Variable|Identity|Assign|Const)") 303 304 node_names = self._verify_watches( 305 self._run_options.debug_options.debug_tensor_watch_opts, 0, 306 ["DebugIdentity"], ["file:///tmp/tfdbg_1"]) 307 self.assertEqual(sorted(["p1", "s"]), sorted(node_names)) 308 309 def testWatchGraph_nodeNameAndOpTypeBlacklists(self): 310 debug_utils.watch_graph_with_blacklists( 311 self._run_options, 312 self._graph, 313 debug_urls="file:///tmp/tfdbg_1", 314 node_name_regex_blacklist="p1$", 315 op_type_regex_blacklist="(Variable|Identity|Assign|Const)") 316 317 node_names = self._verify_watches( 318 self._run_options.debug_options.debug_tensor_watch_opts, 0, 319 ["DebugIdentity"], ["file:///tmp/tfdbg_1"]) 320 self.assertEqual(["s"], node_names) 321 322 def testWatchGraph_tensorDTypeBlacklists(self): 323 debug_utils.watch_graph_with_blacklists( 324 self._run_options, 325 self._graph, 326 debug_urls="file:///tmp/tfdbg_1", 327 tensor_dtype_regex_blacklist=".*_ref") 328 329 node_names = self._verify_watches( 330 self._run_options.debug_options.debug_tensor_watch_opts, 0, 331 ["DebugIdentity"], ["file:///tmp/tfdbg_1"]) 332 self.assertNotIn("a1", node_names) 333 self.assertNotIn("a1/Assign", node_names) 334 self.assertNotIn("b", node_names) 335 self.assertNotIn("b/Assign", node_names) 336 self.assertIn("s", node_names) 337 338 def testWatchGraph_nodeNameAndTensorDTypeBlacklists(self): 339 debug_utils.watch_graph_with_blacklists( 340 self._run_options, 341 self._graph, 342 debug_urls="file:///tmp/tfdbg_1", 343 node_name_regex_blacklist="^s$", 344 tensor_dtype_regex_blacklist=".*_ref") 345 346 node_names = self._verify_watches( 347 self._run_options.debug_options.debug_tensor_watch_opts, 0, 348 ["DebugIdentity"], ["file:///tmp/tfdbg_1"]) 349 self.assertNotIn("a1", node_names) 350 self.assertNotIn("a1/Assign", node_names) 351 self.assertNotIn("b", node_names) 352 self.assertNotIn("b/Assign", node_names) 353 self.assertNotIn("s", node_names) 354 355 356if __name__ == "__main__": 357 googletest.main() 358