code: def build_knn_index_package(args):
      """
      Top-level function that builds a kNN index package (kNN index and config file) from an existing state and word
      store.
      """
      state_store_filename = get_state_store_path(args.input_dir)
      word_store_filename = get_word_store_path(args.input_dir)
      config_filename = get_config_path(args.input_dir)
      utils.check_condition(
          os.path.exists(state_store_filename),
          f"Input file {state_store_filename} not found!",
      )
      utils.check_condition(
          os.path.exists(word_store_filename),
          f"Input file {word_store_filename} not found!",
      )
      utils.check_condition(
          os.path.exists(config_filename),
          f"Config file {config_filename} not found!",
      )
      utils.check_import_faiss()
  
      setup_main_logger(
          file_logging=False,
          console=not args.quiet,
          level=args.loglevel,
      )  # pylint: disable=no-member
      utils.log_basic_info(args)
  
      config = KNNConfig.load(config_filename)
      if args.index_type is not None:
          config.index_type = args.index_type
      if args.train_data_size is not None:
          config.train_data_size = args.train_data_size
  
      keys = np.memmap(
          state_store_filename,
          dtype=config.state_data_type,
          mode="r",
          shape=(config.index_size, config.dimension),
      )
      builder = FaissIndexBuilder(
          config, not args.use_cpu, args.device_id
      )
      train_sample = None
      if args.train_data_input_file is not None:
          train_sample = np.memmap(
              args.train_data_input_file,
              dtype=config.state_data_type,
              mode="r",
              shape=(config.index_size, config.dimension),
          )
      index = builder.build_faiss_index(keys, train_sample)
  
      if not args.use_cpu:
          index_cpu = faiss.index_gpu_to_cpu(
              index
          )  # pylint: disable=no-member
      else:
          index_cpu = index
  
      if not args.output_dir:
          args.output_dir = args.input_dir
      elif args.output_dir != args.input_dir:
          shutil.copy(
              word_store_filename,
              os.path.join(args.output_dir, C.KNN_WORD_DATA_STORE_NAME),
          )
  
      if not os.path.exists(args.output_dir):
          os.mkdir(args.output_dir)
  
      faiss.write_index(
          index_cpu, os.path.join(args.output_dir, C.KNN_INDEX_NAME)
      )
      config.save(os.path.join(args.output_dir, C.KNN_CONFIG_NAME))
  
imports: from dataclasses import dataclass
  import logging
  import numpy
  import os
  import shutil
  from typing import Optional
  from .None import arguments
  from sockeye import config, utils, constants
  from sockeye.log import setup_main_logger
label_desc: Use image node_id 4 for calling a global function with example usage: build_knn_index_package(args) without return types