|
|
|
@ -4,6 +4,12 @@ from torch.profiler import record_function
|
|
|
|
|
from typing import List, Optional
|
|
|
|
|
from contexttimer import Timer
|
|
|
|
|
from .copyer import LimitBuffIndexCopyer
|
|
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EvictionStrategy(Enum):
|
|
|
|
|
LFU = 1
|
|
|
|
|
DATASET = 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CachedParamMgr(torch.nn.Module):
|
|
|
|
@ -18,7 +24,8 @@ class CachedParamMgr(torch.nn.Module):
|
|
|
|
|
weight: torch.Tensor,
|
|
|
|
|
cuda_row_num: int = 0,
|
|
|
|
|
buffer_size: int = 50_000,
|
|
|
|
|
pin_weight=False) -> None:
|
|
|
|
|
pin_weight=False,
|
|
|
|
|
evict_strategy=EvictionStrategy.DATASET) -> None:
|
|
|
|
|
super(CachedParamMgr, self).__init__()
|
|
|
|
|
self.buffer_size = buffer_size
|
|
|
|
|
self.num_embeddings, self.embedding_dim = weight.shape
|
|
|
|
@ -38,6 +45,51 @@ class CachedParamMgr(torch.nn.Module):
|
|
|
|
|
self.input_id_percent_in_load_chunk = []
|
|
|
|
|
self._reset_comm_stats()
|
|
|
|
|
|
|
|
|
|
self._evict_strategy = evict_strategy
|
|
|
|
|
|
|
|
|
|
if self._evict_strategy == EvictionStrategy.LFU:
|
|
|
|
|
# cpu_row_idx -> frequency, freq of the cpu rows.
|
|
|
|
|
# evict the minimal freq value row in cuda cache.
|
|
|
|
|
self.register_buffer("freq_cnter",
|
|
|
|
|
torch.empty(self.num_embeddings, device=torch.cuda.current_device(),
|
|
|
|
|
dtype=torch.long).fill_(0),
|
|
|
|
|
persistent=False)
|
|
|
|
|
|
|
|
|
|
def _update_freq_cnter(self, cpu_row_idxs: torch.Tensor) -> None:
|
|
|
|
|
"""_update_freq_cnter
|
|
|
|
|
|
|
|
|
|
Update the frequency valude w.r.t. the cpu_row_ids in self.freq_cnter.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
cpu_row_idxs (torch.Tensor): a list of indices of cpu weight.
|
|
|
|
|
"""
|
|
|
|
|
if self._evict_strategy == EvictionStrategy.LFU:
|
|
|
|
|
self.freq_cnter[cpu_row_idxs] += 1
|
|
|
|
|
|
|
|
|
|
def _find_evict_gpu_idxs(self, evict_num: int) -> torch.Tensor:
|
|
|
|
|
"""_find_evict_gpu_idxs
|
|
|
|
|
|
|
|
|
|
Find the gpu idxs to be evicted, according to their freq.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
evict_num (int): how many rows has to be evicted
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
torch.Tensor: a list tensor (1D), contains the gpu_row_idxs.
|
|
|
|
|
"""
|
|
|
|
|
if self._evict_strategy == EvictionStrategy.LFU:
|
|
|
|
|
# find the minimal evict_num freq entries in cached_idx_map
|
|
|
|
|
evict_gpu_row_idxs = torch.argsort(self.freq_cnter[self.cached_idx_map])[:evict_num]
|
|
|
|
|
return self.cached_idx_map[evict_gpu_row_idxs]
|
|
|
|
|
elif self._evict_strategy == EvictionStrategy.DATASET:
|
|
|
|
|
# cached_idx_map itself implies the priority of eviction.
|
|
|
|
|
# The value of self.cached_idx_map represents cpu_row_idx.
|
|
|
|
|
# The larger it is, the less frequently it will appear in the dataset,
|
|
|
|
|
# and the higher its eviction priority will be.
|
|
|
|
|
return torch.argsort(self.cached_idx_map, descending=True)[:evict_num]
|
|
|
|
|
else:
|
|
|
|
|
raise TypeError
|
|
|
|
|
|
|
|
|
|
def _init_weight(self, weight):
|
|
|
|
|
if self.cuda_row_num > 0:
|
|
|
|
|
# Enable cache with introducing auxiliary data structures
|
|
|
|
@ -220,6 +272,10 @@ class CachedParamMgr(torch.nn.Module):
|
|
|
|
|
# new ids chunk_offset + offset_in_chunk
|
|
|
|
|
with record_function("(zhg) embed idx -> cache chunk id"):
|
|
|
|
|
gpu_row_idxs = self._id_to_cached_cuda_id(ids)
|
|
|
|
|
|
|
|
|
|
# update for LFU.
|
|
|
|
|
self._update_freq_cnter(cpu_row_idxs)
|
|
|
|
|
|
|
|
|
|
return gpu_row_idxs
|
|
|
|
|
|
|
|
|
|
def _reset_comm_stats(self):
|
|
|
|
@ -234,6 +290,7 @@ class CachedParamMgr(torch.nn.Module):
|
|
|
|
|
@torch.no_grad()
|
|
|
|
|
def _prepare_rows_on_cuda(self, cpu_row_idxs: torch.Tensor) -> None:
|
|
|
|
|
"""prepare rows in cpu_row_idxs on CUDA memory
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
cpu_row_idxs (torch.Tensor): the chunks to be placed on CUDA
|
|
|
|
|
"""
|
|
|
|
@ -245,7 +302,9 @@ class CachedParamMgr(torch.nn.Module):
|
|
|
|
|
invalid_idxs = torch.nonzero(mask_cpu_row_idx).squeeze(1)
|
|
|
|
|
|
|
|
|
|
self.cached_idx_map.index_fill_(0, invalid_idxs, -2)
|
|
|
|
|
evict_gpu_row_idxs = torch.argsort(self.cached_idx_map, descending=True)[:evict_num]
|
|
|
|
|
|
|
|
|
|
evict_gpu_row_idxs = self._find_evict_gpu_idxs(evict_num)
|
|
|
|
|
|
|
|
|
|
self.cached_idx_map.index_copy_(0, invalid_idxs, backup_idxs)
|
|
|
|
|
|
|
|
|
|
evict_info = self.cached_idx_map[evict_gpu_row_idxs]
|
|
|
|
@ -291,8 +350,16 @@ class CachedParamMgr(torch.nn.Module):
|
|
|
|
|
self._cpu_to_cuda_numel += weight_size
|
|
|
|
|
# print(f"admit embedding weight: {weight_size*self.elem_size_in_byte/1e6:.2f} MB")
|
|
|
|
|
|
|
|
|
|
def _find_free_cuda_row(self) -> int:
|
|
|
|
|
if self._cuda_available_row_num == 0:
|
|
|
|
|
return -1
|
|
|
|
|
candidates = torch.nonzero(self.cached_idx_map == -1).squeeze(1)
|
|
|
|
|
return candidates[0].item()
|
|
|
|
|
|
|
|
|
|
def _evict(self) -> int:
|
|
|
|
|
"""
|
|
|
|
|
deprecated
|
|
|
|
|
|
|
|
|
|
evict one chunk from cuda to cpu.
|
|
|
|
|
Returns:
|
|
|
|
|
(int) : the slot id be evicted.
|
|
|
|
@ -329,15 +396,11 @@ class CachedParamMgr(torch.nn.Module):
|
|
|
|
|
# self.num_write_back_history[-1] += 1
|
|
|
|
|
return max_cpu_row_idx
|
|
|
|
|
|
|
|
|
|
def _find_free_cuda_row(self) -> int:
|
|
|
|
|
if self._cuda_available_row_num == 0:
|
|
|
|
|
return -1
|
|
|
|
|
candidates = torch.nonzero(self.cached_idx_map == -1).squeeze(1)
|
|
|
|
|
return candidates[0].item()
|
|
|
|
|
|
|
|
|
|
@torch.no_grad()
|
|
|
|
|
def _admit(self, row_id: int):
|
|
|
|
|
"""
|
|
|
|
|
deprecated
|
|
|
|
|
|
|
|
|
|
move in row_id to CUDA
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|