def testAnnotateDumpedTensorsGivesCorrectResult(self): source_annotation = source_utils.annotate_source( self.dump, self.curr_file_path, do_dumped_tensors=True) # Note: Constant Tensors u_init and v_init may not get dumped due to # constant-folding. self.assertIn(self.u.name, source_annotation[self.u_line_number]) self.assertIn(self.v.name, source_annotation[self.v_line_number]) self.assertIn(self.w.name, source_annotation[self.w_line_number]) self.assertNotIn(self.u.op.name, source_annotation[self.u_line_number]) self.assertNotIn(self.v.op.name, source_annotation[self.v_line_number]) self.assertNotIn(self.w.op.name, source_annotation[self.w_line_number]) self.assertIn(self.u.name, source_annotation[self.helper_line_number]) self.assertIn(self.v.name, source_annotation[self.helper_line_number]) self.assertIn(self.w.name, source_annotation[self.helper_line_number])
def testAnnotateWholeValidSourceFileGivesCorrectResult(self): source_annotation = source_utils.annotate_source(self.dump, self.curr_file_path) self.assertIn(self.u_init.op.name, source_annotation[self.u_init_line_number]) self.assertIn(self.u.op.name, source_annotation[self.u_line_number]) self.assertIn(self.v_init.op.name, source_annotation[self.v_init_line_number]) self.assertIn(self.v.op.name, source_annotation[self.v_line_number]) self.assertIn(self.w.op.name, source_annotation[self.w_line_number]) # In the non-stack-top (default) mode, the helper line should be annotated # with all the ops as well. self.assertIn(self.u_init.op.name, source_annotation[self.helper_line_number]) self.assertIn(self.u.op.name, source_annotation[self.helper_line_number]) self.assertIn(self.v_init.op.name, source_annotation[self.helper_line_number]) self.assertIn(self.v.op.name, source_annotation[self.helper_line_number]) self.assertIn(self.w.op.name, source_annotation[self.helper_line_number])
def testAnnotateSubsetOfLinesGivesCorrectResult(self): source_annotation = source_utils.annotate_source( self.dump, self.curr_file_path, min_line=self.u_line_number, max_line=self.u_line_number + 1) self.assertIn(self.u.op.name, source_annotation[self.u_line_number]) self.assertNotIn(self.v_line_number, source_annotation)
def testAnnotateSubsetOfLinesGivesCorrectResult(self): source_annotation = source_utils.annotate_source( self.dump, self.curr_file_path, min_line=self.u_line_number, max_line=self.u_line_number + 1) self.assertIn(self.u.op.name, source_annotation[self.u_line_number]) self.assertNotIn(self.v_line_number, source_annotation)
def print_source(self, args, screen_info=None): """Print the content of a source file.""" del screen_info # Unused. parsed = self._arg_parsers["print_source"].parse_args(args) source_annotation = source_utils.annotate_source( self._debug_dump, parsed.source_file_path, do_dumped_tensors=parsed.tensors, min_line=parsed.line_begin) with open(parsed.source_file_path, "rU") as f: source_text = f.read() source_lines = source_text.split("\n") num_lines = len(source_lines) line_num_width = int(np.ceil(np.log10(num_lines))) + 3 labeled_source_lines = [] if parsed.line_begin > 1: labeled_source_lines.append( RL("(... Omitted %d source lines ...)" % (parsed.line_begin - 1), "bold")) for i, line in enumerate(source_lines[parsed.line_begin - 1:]): annotated_line = RL("L%d" % (i + parsed.line_begin), "yellow") annotated_line += " " * (line_num_width - len(annotated_line)) annotated_line += line labeled_source_lines.append(annotated_line) if i + parsed.line_begin in source_annotation: sorted_elements = sorted(source_annotation[i + parsed.line_begin]) for k, element in enumerate(sorted_elements): if k >= parsed.max_elements_per_line: labeled_source_lines.append( " (... Omitted %d of %d %s ...)" % ( len(sorted_elements) - parsed.max_elements_per_line, len(sorted_elements), "tensor(s)" if parsed.tensors else "op(s)")) break label = RL(" " * 4) if self._debug_dump.debug_watch_keys( debug_data.get_node_name(element)): attribute = debugger_cli_common.MenuItem("", "pt %s" % element) else: attribute = "blue" label += RL(element, attribute) labeled_source_lines.append(label) output = debugger_cli_common.rich_text_lines_from_rich_line_list( labeled_source_lines) _add_main_menu(output, node_name=None) return output
def testCallingAnnotateSourceOnUnrelatedSourceFileDoesNotError(self): # Create an unrelated source file. unrelated_source_path = tempfile.mktemp() with open(unrelated_source_path, "wt") as source_file: source_file.write("print('hello, world')\n") self.assertEqual( {}, source_utils.annotate_source(self.dump, unrelated_source_path)) # Clean up unrelated source file. os.remove(unrelated_source_path)
def testCallingAnnotateSourceOnUnrelatedSourceFileDoesNotError(self): # Create an unrelated source file. unrelated_source_path = tempfile.mktemp() with open(unrelated_source_path, "wt") as source_file: source_file.write("print('hello, world')\n") self.assertEqual({}, source_utils.annotate_source(self.dump, unrelated_source_path)) # Clean up unrelated source file. os.remove(unrelated_source_path)
def testAnnotateWithStackTopGivesCorrectResult(self): source_annotation = source_utils.annotate_source( self.dump, self.curr_file_path, file_stack_top=True) self.assertIn(self.u_init.op.name, source_annotation[self.u_init_line_number]) self.assertIn(self.u.op.name, source_annotation[self.u_line_number]) self.assertIn(self.v_init.op.name, source_annotation[self.v_init_line_number]) self.assertIn(self.v.op.name, source_annotation[self.v_line_number]) self.assertIn(self.w.op.name, source_annotation[self.w_line_number]) # In the stack-top mode, the helper line should not have been annotated. self.assertNotIn(self.helper_line_number, source_annotation)
def testAnnotateWithStackTopGivesCorrectResult(self): source_annotation = source_utils.annotate_source( self.dump, self.curr_file_path, file_stack_top=True) self.assertIn(self.u_init.op.name, source_annotation[self.u_init_line_number]) self.assertIn(self.u.op.name, source_annotation[self.u_line_number]) self.assertIn(self.v_init.op.name, source_annotation[self.v_init_line_number]) self.assertIn(self.v.op.name, source_annotation[self.v_line_number]) self.assertIn(self.w.op.name, source_annotation[self.w_line_number]) # In the stack-top mode, the helper line should not have been annotated. self.assertNotIn(self.helper_line_number, source_annotation)
def testAnnotateDumpedTensorsGivesCorrectResult(self): source_annotation = source_utils.annotate_source( self.dump, self.curr_file_path, do_dumped_tensors=True) # Note: Constant Tensors u_init and v_init may not get dumped due to # constant-folding. self.assertIn(self.u.name, source_annotation[self.u_line_number]) self.assertIn(self.v.name, source_annotation[self.v_line_number]) self.assertIn(self.w.name, source_annotation[self.w_line_number]) self.assertNotIn(self.u.op.name, source_annotation[self.u_line_number]) self.assertNotIn(self.v.op.name, source_annotation[self.v_line_number]) self.assertNotIn(self.w.op.name, source_annotation[self.w_line_number]) self.assertIn(self.u.name, source_annotation[self.helper_line_number]) self.assertIn(self.v.name, source_annotation[self.helper_line_number]) self.assertIn(self.w.name, source_annotation[self.helper_line_number])
def testAnnotateWholeValidSourceFileGivesCorrectResult(self): source_annotation = source_utils.annotate_source(self.dump, self.curr_file_path) self.assertIn(self.u_init.op.name, source_annotation[self.u_init_line_number]) self.assertIn(self.u.op.name, source_annotation[self.u_line_number]) self.assertIn(self.v_init.op.name, source_annotation[self.v_init_line_number]) self.assertIn(self.v.op.name, source_annotation[self.v_line_number]) self.assertIn(self.w.op.name, source_annotation[self.w_line_number]) # In the non-stack-top (default) mode, the helper line should be annotated # with all the ops as well. self.assertIn(self.u_init.op.name, source_annotation[self.helper_line_number]) self.assertIn(self.u.op.name, source_annotation[self.helper_line_number]) self.assertIn(self.v_init.op.name, source_annotation[self.helper_line_number]) self.assertIn(self.v.op.name, source_annotation[self.helper_line_number]) self.assertIn(self.w.op.name, source_annotation[self.helper_line_number])
def testCallingAnnotateSourceWithoutPythonGraphRaisesException(self): self.dump.set_python_graph(None) with self.assertRaises(ValueError): source_utils.annotate_source(self.dump, self.curr_file_path)
def testCallingAnnotateSourceWithoutPythonGraphRaisesException(self): self.dump.set_python_graph(None) with self.assertRaises(ValueError): source_utils.annotate_source(self.dump, self.curr_file_path)