def dump_xmodel(self, deploy_check=False): """ `dump xmodel for LSTM cell` """ quantizer = GLOBAL_MAP.get_ele(NNDCT_KEYS.QUANTIZER) if quantizer and quantizer.quant_mode > 1: compiler = CompilerFactory.get_compiler("xmodel") xmodel_dir = os.path.join(self._export_folder, "xmodel") create_work_dir(xmodel_dir) for info in self._modules_info.values(): for l_num, layer_graph in enumerate(info["layers_graph"]): for lstm_direction, graph in layer_graph.items(): try: compiler.do_compile( nndct_graph=graph, quant_config_info=quantizer.quant_config, output_file_name=os.path.join(xmodel_dir, graph.name), graph_attr_kwargs={"direction": lstm_direction}) except Exception as e: print( f"[NNDCT_ERROR]:failed convert nndct graph to xmodel({str(e)})." ) else: print("[NNDCT_NOTE]:Successfully convert nndct graph to xmodel!") if deploy_check: print("[NNDCT_NOTE]: Dumping checking data...") checker = DeployChecker( output_dir_name=self._export_folder, data_format="txt") # get timestep output for name, info in self._layers_info.items(): cell = info["cell_module"] layer = info["layer_module"] graph = info["graph"] if layer.input is None: warnings.warn( f"[NNDCT_WARNING]: Provide inputs for '{name}' when do deploy checking", RuntimeWarning) continue set_outputs_recorder_status(cell, True) layer(layer.input, layer.initial_state, layer.batch_lengths) for timestep in range(layer.input.size()[1]): enable_dump_weight = True if timestep == 0 else False update_nndct_blob_data(cell, graph, timestep) checker.update_dump_folder(f"{graph.name}/frame_{timestep}") checker.dump_nodes_output( graph, quantizer.quant_config, round_method=quantizer.quant_opt['round_method'], enable_dump_weight=enable_dump_weight) set_outputs_recorder_status(cell, False) print("[NNDCT_NOTE]: Finsh dumping data.")
def dump_xmodel(output_dir="quantize_result", deploy_check=False): r"""converts module to xmodel for deployment compilation only works when quantm model = 2. The xmodel and some checking data will be generated under work dir. Args: deploy_check(bool): if true, can dump blobs and parameters of model for deployment verification Returns: None """ quantizer = GLOBAL_MAP.get_ele(NNDCT_KEYS.QUANTIZER) if quantizer and quantizer.quant_mode > 1: nndct_utils.create_work_dir(output_dir) # compile to xmodel compiler = CompilerFactory.get_compiler("xmodel") NndctScreenLogger().info("=>Converting to xmodel ...") deploy_graphs = get_deploy_graph_list(quantizer.quant_model, quantizer.Nndctgraph) depoly_infos = compiler.get_deloy_graph_infos(quantizer, deploy_graphs) for depoly_info in depoly_infos: try: compiler.do_compile(depoly_info.dev_graph, quant_config_info=depoly_info.quant_info, output_file_name=os.path.join( output_dir, depoly_info.dev_graph.name)) except AddXopError as e: NndctScreenLogger().error( f"Failed convert graph '{depoly_info.dev_graph.name}' to xmodel({str(e)})." ) # dump data for accuracy check if deploy_check: NndctScreenLogger().info( f"=>Dumping '{depoly_info.dev_graph.name}'' checking data..." ) checker = DeployChecker(output_dir_name=output_dir) checker.update_dump_folder(f"{depoly_info.dev_graph.name}") checker.dump_nodes_output( depoly_info.dev_graph, depoly_info.quant_info, round_method=quantizer.quant_opt['round_method'], select_batch=False) NndctScreenLogger().info( f"=>Finsh dumping data.({checker.dump_folder})") set_outputs_recorder_status(quantizer.quant_model, False)
def dump_node_outputs(graph, node_outputs, sub_dir): checker = DeployChecker( output_dir_name=self._output_dir, data_format="txt") checker.update_dump_folder(sub_dir) for node in graph.nodes: outputs = node_outputs[node.name] assert len(node.out_tensors) == len(outputs) for index, data in enumerate(outputs): node.out_tensors[index].from_ndarray(data) tensor_utils.tf_blob_to_nndct(node.out_tensors[index]) enable_dump_weight = True checker.dump_nodes_output( graph, self.quant_config, round_method=self.quant_opt['round_method'], enable_dump_weight=enable_dump_weight)
def dump_xmodel(output_dir="quantize_result", deploy_check=False, lstm_app=False): r"""converts module to xmodel for deployment compilation only works when quantm model = 2. The xmodel and some checking data will be generated under work dir. Args: deploy_check(bool): if true, can dump blobs and parameters of model for deployment verification Returns: None """ quantizer = GLOBAL_MAP.get_ele(NNDCT_KEYS.QUANTIZER) if quantizer and quantizer.quant_mode > 1: nndct_utils.create_work_dir(output_dir) # compile to xmodel compiler = CompilerFactory.get_compiler("xmodel") NndctScreenLogger().info("=>Converting to xmodel ...") deploy_graphs = get_deploy_graph_list(quantizer.quant_model, quantizer.Nndctgraph) #depoly_infos = compiler.get_deloy_graph_infos(quantizer, deploy_graphs) xmodel_depoly_infos, dump_deploy_infos = compiler.get_xmodel_and_dump_infos(quantizer, deploy_graphs) if not lstm_app: for node in xmodel_depoly_infos[0].dev_graph.nodes: error_out = False if node.op.type not in [NNDCT_OP.INPUT, NNDCT_OP.QUANT_STUB]: continue for i, tensor in enumerate(node.out_tensors): if tensor.shape and tensor.shape[0] != 1: NndctScreenLogger().error(f"Batch size must be 1 when exporting xmodel.") error_out = True break if error_out: break for depoly_info in dump_deploy_infos: # dump data for accuracy check if deploy_check: NndctScreenLogger().info(f"=>Dumping '{depoly_info.dev_graph.name}'' checking data...") if lstm_app: checker = DeployChecker(output_dir_name=output_dir, data_format='txt') checker.update_dump_folder(f"{depoly_info.dev_graph.name}/frame_0") select_batch = True else: checker = DeployChecker(output_dir_name=output_dir) checker.update_dump_folder(f"{depoly_info.dev_graph.name}") select_batch = False checker.dump_nodes_output( depoly_info.dev_graph, depoly_info.quant_info, round_method=quantizer.quant_opt['round_method'], select_batch=select_batch) NndctScreenLogger().info(f"=>Finsh dumping data.({checker.dump_folder})") for depoly_info in xmodel_depoly_infos: try: xgraph = compiler.do_compile( depoly_info.dev_graph, quant_config_info=depoly_info.quant_info, output_file_name=os.path.join(output_dir, depoly_info.dev_graph.name)) except AddXopError as e: NndctScreenLogger().error(f"Failed convert graph '{depoly_info.dev_graph.name}' to xmodel.") raise e compiler.verify_xmodel(depoly_info.dev_graph, xgraph) set_outputs_recorder_status(quantizer.quant_model, False)