示例#1
0
  def dump_xmodel(self):
    if self.quant_mode < 2:
      return

    compiler = CompilerFactory.get_compiler("xmodel")
    xmodel_dir = os.path.join(self._output_dir, "xmodel")
    generic_utils.mkdir_if_not_exist(xmodel_dir)

    compile_args = []
    if self.lstm:
      for direction, graph in self._cell_graphs:
        compile_args.append((graph, {'direction': direction}))
    else:
      compile_args.append((graph, {}))

    for graph, attr_kwargs in compile_args:
      for node in graph.nodes:
        # TODO(yuwang): Set out tensor shape in parser.
        # Maybe specify shape for all tensors?
        # Input shape must be specified for xgraph shape inference.
        if node.op.type == OpTypes.INPUT:
          node.out_tensors[0].shape = node.op.attr['shape']
      try:
        compiler.do_compile(
            graph,
            os.path.join(xmodel_dir, graph.name),
            quant_config_info=self.quant_config,
            graph_attr_kwargs=attr_kwargs)
      except Exception as e:
        print('[ERROR] Failed to dump xmodel: {}'.format(e))
        return

    print('[INFO] Successfully convert nndct graph to xmodel!')
示例#2
0
  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.")
示例#3
0
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)
示例#4
0
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
        try:
            compiler = CompilerFactory.get_compiler("xmodel")
            NndctScreenLogger().info("=>Converting to xmodel ...")
            compiler.do_compile(nndct_graph=quantizer.Nndctgraph,
                                quant_config_info=quantizer.quant_config,
                                output_file_name=os.path.join(
                                    output_dir, quantizer.Nndctgraph.name))

        except AddXopError as e:
            NndctScreenLogger().error(
                f"Failed convert nndct graph to xmodel({str(e)}).")
        else:
            NndctScreenLogger().info(
                f"=>Successfully convert to xmodel.({compiler.xmodel_file})")

        # dump data for accuracy checkvim
        if deploy_check:
            NndctScreenLogger().info("=>Dumping checking data...")
            update_nndct_blob_data(quantizer.quant_model, quantizer.Nndctgraph)
            checker = DeployChecker(output_dir_name=output_dir)
            checker.dump_nodes_output(
                quantizer.Nndctgraph,
                quantizer.quant_config,
                round_method=quantizer.quant_opt['round_method'])

            set_outputs_recorder_status(quantizer.quant_model, False)
            NndctScreenLogger().info(
                f"=>Finsh dumping data.({checker.dump_folder})")
示例#5
0
文件: base.py 项目: Xilinx/Vitis-AI
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)