def parse_files(self, filenames, events_data): """ Load summary file and parse file content. Args: filenames (list[str]): File name list. events_data (EventsData): The container of event data. """ self._events_data = events_data summary_files = self.filter_files(filenames) summary_files = self.sort_files(summary_files) for filename in summary_files: if self._latest_filename and \ (self._compare_summary_file(self._latest_filename, filename)): continue file_path = FileHandler.join(self._summary_dir, filename) if filename != self._latest_filename: self._summary_file_handler = FileHandler(file_path, 'rb') self._latest_filename = filename self._latest_file_size = 0 new_size = FileHandler.file_stat(file_path).size if new_size == self._latest_file_size: continue self._latest_file_size = new_size try: self._load_single_file(self._summary_file_handler) except UnknownError as ex: logger.warning("Parse summary file failed, detail: %r," "file path: %s.", str(ex), file_path)
def phase_pb_file(file_path: str) -> Union[MSGraph, None]: """ Parse pb file to graph Args: file_path (str): The file path of pb file. Returns: MSGraph, if load pb file and build graph success, will return the graph, else return None. """ if not CONFIG.VERBOSE: logger.setLevel(logging.ERROR) logger.info("Start to load graph from pb file, file path: %s.", file_path) model_proto = anf_ir_pb2.ModelProto() try: model_proto.ParseFromString(FileHandler(file_path).read()) except ParseError: logger.warning("The given file is not a valid pb file, file path: %s.", file_path) return None graph = MSGraph() try: graph.build_graph(model_proto.graph) except Exception as ex: logger.error("Build graph failed, file path: %s.", file_path) logger.exception(ex) raise UnknownError(str(ex)) logger.info("Build graph success, file path: %s.", file_path) return graph
def parse(self): """Load summary file and parse file content.""" summary_file_handler = FileHandler(self.summary_file, 'rb') self._file_size = os.path.getsize(self.summary_file) # when current parsed size bigger than self._process_info, print process self._process_info = self._file_size // INFO_INTERVAL parse_summary_logger.info("Loading %s.", self.summary_file) result = self._load(summary_file_handler) if result: warning = '' scalar_path = FileHandler.join(self._output, "scalar.csv") image_path = FileHandler.join(self._output, IMAGE) if not self._image_check: warning = warning + " The summary file contains no image." else: parse_summary_logger.info("Images are written in %s.", image_path) if not self._scalar_check: warning = warning + " The summary file contains no scalar value." else: parse_summary_logger.info("Writing scalar data into %s.", scalar_path) self._scalar_writer.write() if warning: parse_summary_logger.warning(warning) parse_summary_logger.info("Finished loading %s.", self.summary_file)
def _parse_pb_file(self, filename): """ Parse pb file and write content to `EventsData`. Args: filename (str): The file path of pb file. """ file_path = FileHandler.join(self._summary_dir, filename) logger.info("Start to load graph from pb file, file path: %s.", file_path) filehandler = FileHandler(file_path) model_proto = anf_ir_pb2.ModelProto() try: model_proto.ParseFromString(filehandler.read()) except ParseError: logger.warning( "The given file is not a valid pb file, file path: %s.", file_path) return graph = MSGraph() graph.build_graph(model_proto.graph) tensor_event = TensorEvent(wall_time=FileHandler.file_stat(file_path), step=0, tag=filename, plugin_name=PluginNameEnum.GRAPH.value, value=graph) self._events_data.add_tensor_event(tensor_event)
def parse_files(self, executor, filenames, events_data): """ Load summary file and parse file content. Args: executor (Executor): The executor instance. filenames (list[str]): File name list. events_data (EventsData): The container of event data. Returns: bool, True if all the summary files are finished loading. """ summary_files = self.filter_files(filenames) summary_files = self.sort_files(summary_files) if self._latest_filename in summary_files: index = summary_files.index(self._latest_filename) summary_files = summary_files[index:] for filename in summary_files: file_path = FileHandler.join(self._summary_dir, filename) if filename != self._latest_filename: self._summary_file_handler = FileHandler(file_path, 'rb') self._latest_filename = filename self._latest_file_size = 0 new_size = FileHandler.file_stat(file_path).size if new_size == self._latest_file_size: continue try: if not self._load_single_file(self._summary_file_handler, executor, events_data): self._latest_file_size = self._summary_file_handler.offset else: self._latest_file_size = new_size # Wait for data in this file to be processed to avoid loading multiple files at the same time. logger.debug("Parse summary file offset %d, file path: %s.", self._latest_file_size, file_path) return False except UnknownError as ex: logger.warning( "Parse summary file failed, detail: %r," "file path: %s.", str(ex), file_path) return True
def _parse_pb_file(summary_dir, filename): """ Parse pb file and write content to `EventsData`. Args: filename (str): The file path of pb file. Returns: TensorEvent, if load pb file and build graph success, will return tensor event, else return None. """ file_path = FileHandler.join(summary_dir, filename) logger.info("Start to load graph from pb file, file path: %s.", file_path) filehandler = FileHandler(file_path) model_proto = anf_ir_pb2.ModelProto() try: model_proto.ParseFromString(filehandler.read()) except ParseError: logger.warning( "The given file is not a valid pb file, file path: %s.", file_path) return None graph = MSGraph() try: graph.build_graph(model_proto.graph) except Exception as ex: # Normally, there are no exceptions, and it is only possible for users on the MindSpore side # to dump other non-default graphs. logger.error("Build graph failed, file path: %s.", file_path) logger.exception(ex) raise UnknownError(str(ex)) tensor_event = TensorEvent( wall_time=FileHandler.file_stat(file_path).mtime, step=0, tag=filename, plugin_name=PluginNameEnum.GRAPH.value, value=graph, filename=filename) logger.info("Build graph success, file path: %s.", file_path) return tensor_event
def parse_files(self, executor, filenames, events_data): """ Load summary file and parse file content. Args: executor (Executor): The executor instance. filenames (list[str]): File name list. events_data (EventsData): The container of event data. """ self._events_data = events_data summary_files = self.filter_files(filenames) summary_files = self.sort_files(summary_files) for filename in summary_files: if self._latest_filename and \ (self._compare_summary_file(self._latest_filename, filename)): continue file_path = FileHandler.join(self._summary_dir, filename) if filename != self._latest_filename: self._summary_file_handler = FileHandler(file_path, 'rb') self._latest_filename = filename self._latest_file_size = 0 new_size = FileHandler.file_stat(file_path).size if new_size == self._latest_file_size: continue self._latest_file_size = new_size try: self._load_single_file(self._summary_file_handler, executor) # Wait for data in this file to be processed to avoid loading multiple files at the same time. executor.wait_all_tasks_finish() except UnknownError as ex: logger.warning( "Parse summary file failed, detail: %r," "file path: %s.", str(ex), file_path)
def list_events(self, filenames): """ Load summary file and parse file content. Args: filenames (list[str]): File name list. Returns: tuple, the elements of the tuple are: - file_changed (bool): True if the latest file is changed. - is_end (bool): True if all the summary files are finished loading. - event_data (dict): Event data where keys are explanation field. """ summary_files = self.sort_files(filenames) is_end = False file_changed = False event_data = {} filename = summary_files[-1] file_path = FileHandler.join(self._summary_dir, filename) if filename != self._latest_filename: self._summary_file_handler = FileHandler(file_path, 'rb') self._latest_filename = filename self._latest_offset = 0 file_changed = True new_size = FileHandler.file_stat(file_path).size if new_size == self._latest_offset: is_end = True return file_changed, is_end, event_data while True: start_offset = self._summary_file_handler.offset try: event_str = self.event_load(self._summary_file_handler) if event_str is None: self._summary_file_handler.reset_offset(start_offset) is_end = True return file_changed, is_end, event_data if len(event_str) > MAX_EVENT_STRING: logger.warning( "file_path: %s, event string: %d exceeds %d and drop it.", self._summary_file_handler.file_path, len(event_str), MAX_EVENT_STRING) continue field_list, tensor_value_list = self._event_decode(event_str) for field, tensor_value in zip(field_list, tensor_value_list): event_data[field] = tensor_value logger.debug("Parse summary file offset %d, file path: %s.", self._summary_file_handler.offset, file_path) return file_changed, is_end, event_data except exceptions.CRCLengthFailedError as ex: self._summary_file_handler.reset_offset(start_offset) is_end = True logger.warning( "Check crc failed and reset offset, file_path=%s, offset=%s. Detail: %r.", self._summary_file_handler.file_path, self._summary_file_handler.offset, str(ex)) return file_changed, is_end, event_data except Exception as ex: # Note: If an unknown error occurs, we will set the offset to the end of this file, # which is equivalent to stopping parsing this file. We do not delete the current job # and retain the data that has been successfully parsed. self._summary_file_handler.reset_offset(new_size) # Notice: If the current job is the latest one in the loader pool and the job is deleted, # the job goes into an infinite cycle of load-fail-delete-reload-load-fail-delete. # We need to prevent this infinite loop. logger.error( "Parse summary file failed, will set offset to the file end. file_path: %s, " "offset: %d, detail: %s.", file_path, self._summary_file_handler.offset, str(ex)) logger.exception(ex) raise UnknownError(str(ex)) finally: self._latest_offset = self._summary_file_handler.offset
def parse_explain(self, filenames): """ Load summary file and parse file content. Args: filenames (list[str]): File name list. Returns: bool, True if all the summary files are finished loading. """ summary_files = self.sort_files(filenames) is_end = False is_clean = False event_data = {} filename = summary_files[-1] file_path = FileHandler.join(self._summary_dir, filename) if filename != self._latest_filename: self._summary_file_handler = FileHandler(file_path, 'rb') self._latest_filename = filename self._latest_file_size = 0 is_clean = True new_size = FileHandler.file_stat(file_path).size if new_size == self._latest_file_size: is_end = True return is_clean, is_end, event_data while True: start_offset = self._summary_file_handler.offset try: event_str = self.event_load(self._summary_file_handler) if event_str is None: self._summary_file_handler.reset_offset(start_offset) is_end = True return is_clean, is_end, event_data if len(event_str) > MAX_EVENT_STRING: logger.warning( "file_path: %s, event string: %d exceeds %d and drop it.", self._summary_file_handler.file_path, len(event_str), MAX_EVENT_STRING) continue field_list, tensor_value_list = self._event_decode(event_str) for field, tensor_value in zip(field_list, tensor_value_list): event_data[field] = tensor_value logger.info("Parse summary file offset %d, file path: %s.", self._summary_file_handler.offset, file_path) return is_clean, is_end, event_data except (exceptions.CRCFailedError, exceptions.CRCLengthFailedError) as ex: self._summary_file_handler.reset_offset(start_offset) is_end = True logger.warning( "Check crc failed and ignore this file, file_path=%s, offset=%s. Detail: %r.", self._summary_file_handler.file_path, self._summary_file_handler.offset, str(ex)) return is_clean, is_end, event_data except (OSError, DecodeError, exceptions.MindInsightException) as ex: is_end = True logger.warning( "Parse log file fail, and ignore this file, detail: %r," "file path: %s.", str(ex), self._summary_file_handler.file_path) return is_clean, is_end, event_data except Exception as ex: logger.exception(ex) raise UnknownError(str(ex))