code: def distill_record_file(in_file, out_file):
      """
      Pick the best entries from a record file and store them to another file.
      This function distills the useful log entries from a large log file.
      If out_file already exists, the best entries from both
      in_file and out_file will be saved.
  
      Parameters
      ----------
      in_file: str
          The filename of input
      out_file: str or file
          The filename of output
      """
      # pylint: disable=import-outside-toplevel
      from .dispatcher import ApplyHistoryBest
  
      context = load_records(in_file)
  
      dirname = os.path.dirname(os.path.abspath(out_file))
      if not os.path.exists(dirname):
          os.makedirs(dirname)
  
      if os.path.isfile(out_file):
          out_context = load_records(out_file)
          context = itertools.chain(context, out_context)
  
      def measure_input_str_key(inp):
          return _ffi_api.SerializeMeasureInput(inp)
  
      # Dict[target key,
      #   Dict[workload hash,
      #     Dict[workload args, (cost, (MeasureInput, MeasureResult))]]]
      # Full type: Dict[str, Dict[str, Dict[Tuple, Tuple[float, Tuple[Measureinput, MeasureResult]]]]]
      best_records = {}
  
      for inp, res in context:
          if res.error_no != 0:
              continue
  
          # Keep the best record for each target and workload.
          costs = [
              x.value
              for x in res.costs
              if isinstance(x, tvm.tir.expr.FloatImm)
          ]
          cost = np.mean(costs)
          for k in inp.task.target.keys:
              (
                  entry,
                  _,
                  workload_args,
              ) = ApplyHistoryBest.get_workload_entry(
                  best_records, k, inp.task.workload_key
              )
              if (
                  workload_args not in entry
                  or cost < entry[workload_args][0]
              ):
                  entry[workload_args] = (cost, (inp, res))
  
      # Remove duplications by multiple target keys.
      out_records = {}
      for target_entry in best_records.values():
          for workload_entry in target_entry.values():
              for _, (inp, res) in workload_entry.values():
                  out_records[measure_input_str_key(inp)] = (inp, res)
  
      inputs = []
      results = []
      for inp, res in out_records.values():
          inputs.append(inp)
          results.append(res)
  
      # create a new file and save the best records
      open(out_file, "w")
      save_records(out_file, inputs, results)
      logger.info(
          "Extract %d best records from %s to %s",
          len(inputs),
          in_file,
          out_file,
      )
  
imports: import argparse
  import logging
  import os
  import itertools
  import numpy
  import tvm._ffi
  from tvm.runtime import Object
  from .measure import MeasureErrorNo, MeasureCallback
  from .utils import calc_workload_dis_factor, decode_workload_key
  from .None import _ffi_api
label_desc: Use image node_id 6 for calling a global function with example usage: distill_record_file(in_file, out_file) and returns: _ffi_api