code: def build_index(cfg: DictConfig, model: object):
      """
      Builds faiss index from index dataset specified in the config.
  
      Args:
          cfg (DictConfig): Config file specifying index parameters
          model (object): Encoder model
      """
  
      # Get index dataset embeddings
      # PCA model exists and index embeddings have already been PCAed, no need to re-extract/PCA them
      if (
          cfg.apply_pca
          and os.path.isfile(cfg.pca.pca_save_name)
          and os.path.isfile(cfg.pca_embeddings_save_name)
      ):
          logging.info("Loading reduced dimensionality embeddings")
          embeddings = h5py.File(cfg.pca_embeddings_save_name, "r")
          embeddings = embeddings[cfg.index_ds.name][:]
  
      elif os.path.isfile(cfg.embedding_save_name):
          logging.info(
              "Loading previously extracted index dataset embeddings"
          )
          embeddings = h5py.File(cfg.embedding_save_name, "r")
          embeddings = embeddings[cfg.index_ds.name][:]
  
      else:
          logging.info("Encoding index dataset, this may take a while")
          index_dataloader = model.setup_dataloader(
              cfg.index_ds, is_index_data=True
          )
          embeddings, concept_ids = get_index_embeddings(
              cfg, index_dataloader, model
          )
  
      # Create pca model to reduce dimensionality of index dataset and decrease memory footprint
      if cfg.apply_pca:
          # Need to train PCA model and apply PCA transformation with newly trained model
          if not os.path.isfile(cfg.pca.pca_save_name):
              logging.info(
                  "Fitting PCA model for embedding dimensionality reduction"
              )
              pca_train_set = random.sample(
                  list(embeddings),
                  k=int(len(embeddings) * cfg.pca.sample_fraction),
              )
              pca = PCA(n_components=cfg.pca.output_dim)
              pca.fit(pca_train_set)
              pkl.dump(pca, open(cfg.pca.pca_save_name, "wb"))
              embeddings = reduce_embedding_dim(pca, embeddings, cfg)
  
          # PCA model already trained, just need to reduce dimensionality of all embeddings
          elif not os.path.isfile(cfg.pca_embeddings_save_name):
              pca = pkl.load(open(cfg.pca.pca_save_name, "rb"))
              embeddings = reduce_embedding_dim(pca, embeddings, cfg)
  
      # Build faiss index from embeddings
      logging.info(
          f"Training index with embedding dim size {cfg.dims} using {faiss.get_num_gpus()} gpus"
      )
      quantizer = faiss.IndexFlatL2(cfg.dims)
      index = faiss.IndexIVFFlat(quantizer, cfg.dims, cfg.nlist)
      index = faiss.index_cpu_to_all_gpus(index)
      index.train(embeddings)
  
      logging.info("Adding dataset embeddings to index")
      for i in tqdm(
          range(0, embeddings.shape[0], cfg.index_batch_size)
      ):
          index.add(embeddings[i : i + cfg.index_batch_size])
  
      logging.info("Saving index")
      faiss.write_index(
          faiss.index_gpu_to_cpu(index), cfg.index_save_name
      )
      logging.info("Index built and saved")
  
file_path: NeMo/examples/nlp/entity_linking/build_index.py
imports: import os
  import pickle
  import random
  from argparse import ArgumentParser
  import h5py
  import numpy
  import torch
  from omegaconf import DictConfig, OmegaConf
  from sklearn.decomposition import PCA
  from tqdm import tqdm
  from nemo.collections.nlp.models import EntityLinkingModel
  from nemo.utils import logging
label_desc: Use image node_id 1 for calling a global function with example usage: build_index(cfg, model) without return types