parameters: (self, save_model_dir)
docstring: Convert all presistable vars from fp32 to fp16.
          Note that, this api only changes the data type of variables in
          __params__ file, and the __model__ file remains unchanged. 
  
          Args:
              save_model_dir(str): The path to save the fp16 model.
docstring_summary: Convert all presistable vars from fp32 to fp16.
          Note that, this api only changes the data type of variables in
          __params__ file, and the __model__ file remains unchanged.
docstring_tokens: Convert all presistable vars from fp32 to fp16 . Note that this api only changes the data type of variables in __params__ file and the __model__ file remains unchanged .
function: def convert_weight_to_fp16(self, save_model_dir):
          """
          Convert all presistable vars from fp32 to fp16.
          Note that, this api only changes the data type of variables in
          __params__ file, and the __model__ file remains unchanged. 
  
          Args:
              save_model_dir(str): The path to save the fp16 model.
          """
  
          # Load model
          place = core.CPUPlace()
          exe = Executor(place)
          scope = global_scope()
          [infer_program, feed_list, fetch_list] = \
              io.load_inference_model(dirname=self._model_dir,
                                      executor=exe,
                                      model_filename=self._model_filename,
                                      params_filename=self._params_filename)
  
          # Clone and save fp16 weights
          save_program = framework.Program()
          save_block = save_program.global_block()
          save_var_map = {}
  
          for var in infer_program.list_vars():
              if (var.type == core.VarDesc.VarType.RAW) or \
                  (not var.persistable) or (var.name in ['feed', 'fetch']) \
                  or (var.dtype != core.VarDesc.VarType.FP32):
                  continue
  
              #new_var = _clone_var_to_block_(var, save_block)
              new_var = save_block._clone_variable(var)
              if self._params_filename is not None:
                  save_var_map[new_var.name] = new_var
              else:
                  save_file_path = os.path.join(
                      os.path.normpath(save_model_dir), new_var.name)
                  save_block.append_op(
                      type='save',
                      inputs={'X': [new_var]},
                      outputs={},
                      attrs={
                          'file_path': os.path.normpath(save_file_path),
                          'save_as_fp16': True
                      })
  
          if self._params_filename is not None:
              save_var_list = []
              for name in sorted(save_var_map.keys()):
                  save_var_list.append(save_var_map[name])
  
              saved_params_var = save_block.create_var(
                  type=core.VarDesc.VarType.RAW,
                  name=unique_name.generate("saved_params"))
              saved_params_var.desc.set_persistable(True)
  
              save_path = os.path.join(
                  os.path.normpath(save_model_dir), self._params_filename)
              save_block.append_op(
                  type='save_combine',
                  inputs={'X': save_var_list},
                  outputs={'Y': saved_params_var},
                  attrs={'file_path': save_path,
                         'save_as_fp16': True})
  
          save_program._sync_with_cpp()
          exe.run(save_program)
  
          # Copy model
          model_filename = "__model__" if self._model_filename is None \
                      else self._model_filename
          src_model = os.path.join(self._model_dir, model_filename)
          dest_model = os.path.join(save_model_dir, model_filename)
          shutil.copyfile(src_model, dest_model)
function_tokens: def convert_weight_to_fp16 ( self , save_model_dir ) : # Load model place = core . CPUPlace ( ) exe = Executor ( place ) scope = global_scope ( ) [ infer_program , feed_list , fetch_list ] = io . load_inference_model ( dirname = self . _model_dir , executor = exe , model_filename = self . _model_filename , params_filename = self . _params_filename ) # Clone and save fp16 weights save_program = framework . Program ( ) save_block = save_program . global_block ( ) save_var_map = { } for var in infer_program . list_vars ( ) : if ( var . type == core . VarDesc . VarType . RAW ) or ( not var . persistable ) or ( var . name in [ 'feed' , 'fetch' ] ) or ( var . dtype != core . VarDesc . VarType . FP32 ) : continue #new_var = _clone_var_to_block_(var, save_block) new_var = save_block . _clone_variable ( var ) if self . _params_filename is not None : save_var_map [ new_var . name ] = new_var else : save_file_path = os . path . join ( os . path . normpath ( save_model_dir ) , new_var . name ) save_block . append_op ( type = 'save' , inputs = { 'X' : [ new_var ] } , outputs = { } , attrs = { 'file_path' : os . path . normpath ( save_file_path ) , 'save_as_fp16' : True } ) if self . _params_filename is not None : save_var_list = [ ] for name in sorted ( save_var_map . keys ( ) ) : save_var_list . append ( save_var_map [ name ] ) saved_params_var = save_block . create_var ( type = core . VarDesc . VarType . RAW , name = unique_name . generate ( "saved_params" ) ) saved_params_var . desc . set_persistable ( True ) save_path = os . path . join ( os . path . normpath ( save_model_dir ) , self . _params_filename ) save_block . append_op ( type = 'save_combine' , inputs = { 'X' : save_var_list } , outputs = { 'Y' : saved_params_var } , attrs = { 'file_path' : save_path , 'save_as_fp16' : True } ) save_program . _sync_with_cpp ( ) exe . run ( save_program ) # Copy model model_filename = "__model__" if self . _model_filename is None else self . _model_filename src_model = os . path . join ( self . _model_dir , model_filename ) dest_model = os . path . join ( save_model_dir , model_filename ) shutil . copyfile ( src_model , dest_model )