class_docstr: Manage Embedding Weights on CPU and CUDA memory uses a software cache.
  CPU maintains the entire original weight.
  CUDA maintains a fraction of the weights used in the upcoming computation. The row number in CUDA is controlled by `cuda_row_num`.
  During training, GPU needs to transmit embedding rows between CPU and GPU.
  Args:
      weight (torch.Tensor): the weight of the Embedding layer.
      cuda_row_num (int, optional): the number of rows cached in CUDA memory. Defaults to 0.
      buffer_size (int, optional): the number of rows in a data transmitter buffer. Defaults to 50_000.
      pin_weight (bool, optional): use pin memory to store the cpu weight. If set `True`, the cpu memory usage will increase largely. Defaults to False.
      evict_strategy (EvictionStrategy, optional): the eviction strategy. There are two options.
      `EvictionStrategy.LFU`: use the least frequently used cache.
      `EvictionStrategy.DATASET`: use the stats collected from the target dataset. It usually leads to less cpu-gpu communication volume.
      Defaults to EvictionStrategy.DATASET.
class_docstr_tok: ["Manage","Embedding","Weights","on","CPU","and","CUDA","memory","uses","a","software","cache",".","CPU","maintains","the","entire","original","weight",".","CUDA","maintains","a","fraction","of","the","weights","used","in","the","upcoming","computation",".","The","row","number","in","CUDA","is","controlled","by","`","cuda_row_num","`",".","During","training",",","GPU","needs","to","transmit","embedding","rows","between","CPU","and","GPU",".","Args",":","weight","(","torch.Tensor",")",":","the","weight","of","the","Embedding","layer",".","cuda_row_num","(","int",",","optional",")",":","the","number","of","rows","cached","in","CUDA","memory",".","Defaults","to","0.","buffer_size","(","int",",","optional",")",":","the","number","of","rows","in","a","data","transmitter","buffer",".","Defaults","to","50_000",".","pin_weight","(","bool",",","optional",")",":","use","pin","memory","to","store","the","cpu","weight",".","If","set","`","True","`",",","the","cpu","memory","usage","will","increase","largely",".","Defaults","to","False",".","evict_strategy","(","EvictionStrategy",",","optional",")",":","the","eviction","strategy",".","There","are","two","options",".","`","EvictionStrategy.LFU","`",":","use","the","least","frequently","used","cache",".","`","EvictionStrategy.DATASET","`",":","use","the","stats","collected","from","the","target","dataset",".","It","usually","leads","to","less","cpu-gpu","communication","volume",".","Defaults","to","EvictionStrategy.DATASET","."]
docstr: reorder
  reorder the weight according to ids' frequency in dataset before training.
  Execute only once before training, also known as warmup phase.
  
  Note:
      If you would like to use the DATASET as the eviction strategy, you must call this function.
  Note:
      If you are use the LFU as the eviction strategy, you can skip this function. If you still use this function. It will initialize
      The frequency in LFU cache using the dataset statistics.
  Args:
      ids_freq_mapping (List[int]): a list, whose offset is id number, value is freq. if None then not reorder the cpu weight.
      warmup_ratio (float): the amount of chunks preloaded in cuda cache
docstr_tok: ["reorder","reorder","the","weight","according","to","ids","'","frequency","in","dataset","before","training",".","Execute","only","once","before","training",",","also","known","as","warmup","phase",".","Note",":","If","you","would","like","to","use","the","DATASET","as","the","eviction","strategy",",","you","must","call","this","function",".","Note",":","If","you","are","use","the","LFU","as","the","eviction","strategy",",","you","can","skip","this","function",".","If","you","still","use","this","function",".","It","will","initialize","The","frequency","in","LFU","cache","using","the","dataset","statistics",".","Args",":","ids_freq_mapping","(","List","[","int","]",")",":","a","list",",","whose","offset","is","id","number",",","value","is","freq",".","if","None","then","not","reorder","the","cpu","weight",".","warmup_ratio","(","float",")",":","the","amount","of","chunks","preloaded","in","cuda","cache"]
code: def reorder(
      self,
      ids_freq_mapping: Optional[List[int]] = None,
      warmup_ratio=0.7,
  ):
      """reorder
      reorder the weight according to ids' frequency in dataset before training.
      Execute only once before training, also known as warmup phase.
  
      Note:
          If you would like to use the DATASET as the eviction strategy, you must call this function.
      Note:
          If you are use the LFU as the eviction strategy, you can skip this function. If you still use this function. It will initialize
          The frequency in LFU cache using the dataset statistics.
      Args:
          ids_freq_mapping (List[int]): a list, whose offset is id number, value is freq. if None then not reorder the cpu weight.
          warmup_ratio (float): the amount of chunks preloaded in cuda cache
      """
      # reorder phase: reorder the cpu weight according to their freq stats in the target dataset.
      # reorder only works for DATASET eviction strategy.
  
      if ids_freq_mapping is not None and not isinstance(
          ids_freq_mapping, torch.Tensor
      ):
          ids_freq_mapping = torch.tensor(ids_freq_mapping)
  
      if self._evict_strategy == EvictionStrategy.DATASET:
          if ids_freq_mapping is not None:
              tmp_idx = torch.argsort(ids_freq_mapping, descending=True)
              sorted_idx = torch.argsort(tmp_idx)
              self.idx_map.data.copy_(sorted_idx)
  
      # warmup phase: copy #preload_row_num rows from cpu to gpu.
      preload_row_num = min(
          int(np.ceil(self.cuda_row_num * warmup_ratio)),
          self.num_embeddings,
      )
      if preload_row_num > 0:
          with Timer() as timer:
              # extract rows from cpu weight
              if (
                  self._evict_strategy == EvictionStrategy.LFU
                  and ids_freq_mapping is not None
              ):
                  freq_value, preload_cpu_ids = torch.topk(
                      ids_freq_mapping,
                      preload_row_num,
                      dim=0,
                      largest=True,
                  )
                  preload_cuda_row_idxs = torch.arange(
                      preload_row_num
                  ).cuda()
              else:
                  preload_cpu_ids = torch.arange(preload_row_num)
                  preload_cuda_row_idxs = preload_cpu_ids.cuda()
              if self.buffer_size > 0:
                  self.limit_buff_index_copyer.index_copy(
                      0,
                      src_index=preload_cpu_ids,
                      tgt_index=preload_cuda_row_idxs,
                      src=self.weight.view(self.num_embeddings, -1),
                      tgt=self.cuda_cached_weight.view(
                          self.cuda_row_num, -1
                      ),
                  )
              else:
                  preload_rows = (
                      self.weight.view(self.num_embeddings, -1)
                      .index_select(0, preload_cpu_ids)
                      .cuda()
                  )
                  self.cuda_cached_weight.view(
                      self.cuda_row_num, -1
                  ).index_copy_(0, preload_cuda_row_idxs, preload_rows)
  
              # update auxiliary info
              self.cached_idx_map[
                  preload_cuda_row_idxs
              ] = preload_cpu_ids.cuda()
              self.inverted_cached_idx[
                  preload_cpu_ids
              ] = preload_cuda_row_idxs
              self._cuda_available_row_num -= preload_row_num
  
              if self._evict_strategy == EvictionStrategy.LFU:
                  # if the ids_freq_mapping is not None, we initialize the embedding row's freq value in LFU as its freq in dataset.
                  if ids_freq_mapping is None:
                      self.freq_cnter.index_fill_(
                          0, preload_cuda_row_idxs, 0
                      )
                  else:
                      self.freq_cnter[
                          preload_cuda_row_idxs
                      ] = freq_value.cuda()
  
          print(f"Cache warmup finished cost {timer.elapsed} sec.")
  
imports: import sys
  from contextlib import contextmanager
  from enum import Enum
  from typing import List, Optional
  import numpy
  import torch
  from contexttimer import Timer
  from torch.profiler import record_function
  from .copyer import LimitBuffIndexCopyer
label_desc: Use image node_id 8 for calling the CachedParamMgr obj's underlying member method code with example usage: obj.reorder(ids_freq_mapping, warmup_ratio) without return types