def _counts_summary(counts, skip_zeros=True, total_count=None): """Format values as a two-row table.""" if skip_zeros: counts = [(count_key, count_val) for count_key, count_val in counts if count_val] max_common_len = 0 for count_key, count_val in counts: count_val_str = str(count_val) common_len = max(len(count_key) + 1, len(count_val_str) + 1) max_common_len = max(common_len, max_common_len) key_line = ui_common.RichLine("|") val_line = ui_common.RichLine("|") for count_key, count_val in counts: count_val_str = str(count_val) key_line += _pad_string_to_length(count_key, max_common_len) val_line += _pad_string_to_length(count_val_str, max_common_len) key_line += " |" val_line += " |" if total_count is not None: total_key_str = "total" total_val_str = str(total_count) max_common_len = max(len(total_key_str) + 1, len(total_val_str)) total_key_str = _pad_string_to_length(total_key_str, max_common_len) total_val_str = _pad_string_to_length(total_val_str, max_common_len) key_line += total_key_str + " |" val_line += total_val_str + " |" return ui_common.rich_text_lines_frm_line_list([key_line, val_line])
def _recommend_command(command, description, indent=2, create_link=False): """Generate a RichTextLines object that describes a recommended command. Parameters ---------- command: (str) The command to recommend. description: (str) A description of what the command does. indent: (int) How many spaces to indent in the beginning. create_link: (bool) Whether a command link is to be applied to the command string. Returns: (RichTextLines) Formatted text (with font attributes) for recommending the command. """ indent_str = " " * indent if create_link: font_attr = [ui_common.MenuItem("", command), "bold"] else: font_attr = "bold" lines = [ RL(indent_str) + RL(command, font_attr) + ":", indent_str + " " + description ] return ui_common.rich_text_lines_frm_line_list(lines)
def get_error_intro(tvm_error): """Generate formatted intro for TVM run-time error. Parameters ---------- tvm_error : errors.OpError TVM run-time error object. Returns ------- out : object RichTextLines Formatted intro message about the run-time OpError, with sample commands for debugging. """ op_name = tvm_error.op.name intro_lines = [ "--------------------------------------", RL("!!! An error occurred during the run !!!", "blink"), "", "You may use the following commands to debug:", ] out = ui_common.rich_text_lines_frm_line_list(intro_lines) out.extend( _recommend_command("nd -a -d -t %s" % op_name, "Inspect information about the failing op.", create_link=True)) out.extend( _recommend_command("gi -r %s" % op_name, "List inputs to the failing op, recursively.", create_link=True)) out.extend( _recommend_command( "lg", "List all graphnodes dumped during the failing run() call.", create_link=True)) more_lines = [ "", "Op name: " + op_name, "Error type: " + str(type(tvm_error)), "", "Details:", str(tvm_error), "", "WARNING: Using client GraphDef due to the error, instead of " "executor GraphDefs.", "--------------------------------------", "", ] out.extend(ui_common.RichTextLines(more_lines)) return out
def error(msg): """Generate a RichTextLines output for error. Parameters ---------- msg: (str) The error message. Returns: (ui_common.RichTextLines) A representation of the error message for screen output. """ return ui_common.rich_text_lines_frm_line_list( [RL("ERROR: " + msg, COLOR_RED)])
def summarize(self, highlight=None): """Get a text summary of the config. Parameters ---------- highlight: A property name to highlight in the output. Returns: A `RichTextLines` output. """ lines = [RL("Command-line configuration:", "bold"), RL("")] for name, val in self._config.items(): highlight_attr = "bold" if name == highlight else None line = RL(" ") line += RL(name, ["underline", highlight_attr]) line += RL(": ") line += RL(str(val), font_attr=highlight_attr) lines.append(line) return ui_common.rich_text_lines_frm_line_list(lines)
def get_run_start_intro(graph_node_count, outputs, input_dict, is_callable_runner=False): """Generate formatted intro for run-start UI. Parameters ---------- run_call_count: (int) Run call counter. outputs: Outputs of the `GraphRuntime.run()` call. See doc of `GraphRuntime.run()` for more details. input_dict: Inputs to the `GraphRuntime.run()` call. See doc of `GraphRuntime.run()` for more details. tensor_filters: (dict) A dict from tensor-filter name to tensor-filter callable. is_callable_runner: (bool) whether a runner returned by GraphRuntime.make_callable is being run. Returns: (RichTextLines) Formatted intro message about the `GraphRuntime.run()` call. """ output_lines = common.get_flattened_names(outputs) if not input_dict: input_dict_lines = [ui_common.RichLine(" (Empty)")] else: input_dict_lines = [] for input_key in input_dict: input_key_name = common.get_graph_element_name(input_key) input_dict_line = ui_common.RichLine(" ") input_dict_line += ui_common.RichLine( input_key_name, ui_common.MenuItem(None, "pi '%s'" % input_key_name)) # Surround the name string with quotes, because input_key_name may contain # spaces in some cases, e.g., SparseTensors. input_dict_lines.append(input_dict_line) input_dict_lines = ui_common.rich_text_lines_frm_line_list( input_dict_lines) out = ui_common.RichTextLines(_HORIZONTAL_BAR) out.append("") out.append("") out.append(" Choose any of the below option to continue...") out.append(" ---------------------------------------------") out.extend( _recommend_command("run", "Run the NNVM graph with debug", create_link=True)) out.extend( _recommend_command("run -nodebug", "Run the NNVM graph without debug", create_link=True)) out.append("") out.append(_HORIZONTAL_BAR) if is_callable_runner: out.append( " Running a runner returned by GraphRuntime.make_callable()") else: out.append("") out.append(" TVM Graph details") # % run_call_count) out.append(" -----------------") out.append("") out.append(" Node count:") out.append(" " + str(graph_node_count)) out.append("") out.append(" Input(s):") out.extend(input_dict_lines) out.append("") out.append(" Output(s):") out.extend( ui_common.RichTextLines([" " + line for line in output_lines])) out.append("") out.append(_HORIZONTAL_BAR) # Make main menu for the run-start intro. menu = ui_common.Menu() menu.append(ui_common.MenuItem("run", "run")) out.annotations[ui_common.MAIN_MENU_KEY] = menu return out
def format_tensor(tensor, tensor_name, np_printoptions, print_all=False, tensor_slicing=None, highlight_options=None, include_numeric_summary=False, write_path=None): """Generate formatted str to represent a tensor or its slices. Parameters ---------- tensor: (numpy ndarray) The tensor value. tensor_name: (str) Name of the tensor, e.g., the tensor's debug watch key. np_printoptions: (dict) Numpy tensor formatting options. print_all: (bool) Whether the tensor is to be displayed in its entirety, instead of printing ellipses, even if its number of elements exceeds the default numpy display threshold. (Note: Even if this is set to true, the screen output can still be cut off by the UI frontend if it consist of more lines than the frontend can handle.) tensor_slicing: (str or None) Slicing of the tensor, e.g., "[:, 1]". If None, no slicing will be performed on the tensor. highlight_options: (tensor_data.HighlightOptions) options to highlight elements of the tensor. See the doc of tensor_data.format_tensor() for more details. include_numeric_summary: Whether a text summary of the numeric values (if applicable) will be included. write_path: A path to save the tensor value (after any slicing) to (optional). `numpy.save()` is used to save the value. Returns: An instance of `ui_common.RichTextLines` representing the (potentially sliced) tensor. """ if tensor_slicing: # Validate the indexing. value = command_parser.evaluate_tensor_slice(tensor, tensor_slicing) sliced_name = tensor_name + tensor_slicing else: value = tensor sliced_name = tensor_name auxiliary_message = None if write_path: with open(write_path, "wb") as output_file: np.save(output_file, value) line = ui_common.RichLine("Saved value to: ") line += ui_common.RichLine(write_path, font_attr="bold") line += " (%sB)" % bytes_to_readable_str(os.stat(write_path).st_size) auxiliary_message = ui_common.rich_text_lines_frm_line_list( [line, ui_common.RichLine("")]) if print_all: np_printoptions["threshold"] = value.size else: np_printoptions["threshold"] = DEFAULT_NDARRAY_DISPLAY_THRESHOLD return tensor_data.format_tensor( value, sliced_name, include_metadata=True, include_numeric_summary=include_numeric_summary, auxiliary_message=auxiliary_message, np_printoptions=np_printoptions, highlight_options=highlight_options)
def render(self, max_length, backward_command, forward_command, home_command, help_command, exit_command): """Render the rich text content of the single-line navigation bar. Parameters ---------- max_length : int Maximum length of the navigation bar, in characters. backward_command : str command for going backward. Used to construct the shortcut menu item. forward_command : str Command for going forward. Used to construct the shortcut menu item. Returns ------- output : Object ui_common.RichTextLines The navigation bar text with attributes. """ output = RL("| ", NAVIGATION_MENU_COLOR_ATTR) output += RL( HOME_TEXT, (ui_common.MenuItem( None, home_command, custom_color=NAVIGATION_MENU_COLOR_ATTR) if self.can_go_home() else NAVIGATION_MENU_COLOR_ATTR)) output += RL(" | ", NAVIGATION_MENU_COLOR_ATTR) output += RL(self.BACK_ARROW_TEXT, (ui_common.MenuItem( None, backward_command, custom_color=NAVIGATION_MENU_COLOR_ATTR) if self.can_go_back() else NAVIGATION_MENU_COLOR_ATTR)) output += RL(" ", NAVIGATION_MENU_COLOR_ATTR) output += RL( self.FORWARD_ARROW_TEXT, (ui_common.MenuItem( None, forward_command, custom_color=NAVIGATION_MENU_COLOR_ATTR) if self.can_go_forward() else NAVIGATION_MENU_COLOR_ATTR)) output_end = RL("| ", NAVIGATION_MENU_COLOR_ATTR) output_end += RL( HELP_TEXT, (ui_common.MenuItem( None, help_command, custom_color=NAVIGATION_MENU_COLOR_ATTR) if self.can_go_help() else NAVIGATION_MENU_COLOR_ATTR)) output_end += RL(" | ", NAVIGATION_MENU_COLOR_ATTR) output_end += RL( EXIT_TEXT, ui_common.MenuItem(None, exit_command, custom_color=NAVIGATION_MENU_COLOR_ATTR)) output_end += RL(" |", NAVIGATION_MENU_COLOR_ATTR) output_middle = RL("", NAVIGATION_MENU_COLOR_ATTR) if (len(output) + len(output_end)) < max_length: space_need_size = max_length - len(output + output_end) if space_need_size > len(TVM_DBG_BOX_BOTTOM): space_need_size -= len(TVM_DBG_BOX_BOTTOM) space_middle_size = int(space_need_size / 2) empty_line = (" " * (space_middle_size - (0 if space_need_size % 2 else 1)) + TVM_DBG_BOX_BOTTOM + " " * space_middle_size) output_middle = RL(empty_line, NAVIGATION_MENU_COLOR_ATTR) else: empty_line = "-" * space_need_size output_middle = RL(empty_line, NAVIGATION_MENU_COLOR_ATTR) return ui_common.rich_text_lines_frm_line_list( [output + output_middle + output_end], additional_attr=curses.A_BOLD)