Python implementation of reshape_and_cache_back_flash. Operation: Flat Tensor (Source) -> Paged Attention Cache (Destination) Logic: 1. Extract the specific layer's data from key_value. 2. Move it to the GPU (if it's on CPU). 3. Reshape it to match the
(
key_value: torch.Tensor,
# Source: [2, num_layer, num_tokens, num_heads * head_size]
# (Can be on CPU/Pinned Memory or GPU)
key_cache: torch.Tensor,
# Destination: [num_blocks, block_size, num_heads, head_size]
# (Must be on GPU)
value_cache: torch.Tensor, # Destination: (Must be on GPU)
slot_mapping: torch.Tensor, # Indices: [num_tokens]
layer_idx: int,
)
| 1981 | |
| 1982 | |
| 1983 | def reshape_and_cache_back_flash( |
| 1984 | key_value: torch.Tensor, |
| 1985 | # Source: [2, num_layer, num_tokens, num_heads * head_size] |
| 1986 | # (Can be on CPU/Pinned Memory or GPU) |
| 1987 | key_cache: torch.Tensor, |
| 1988 | # Destination: [num_blocks, block_size, num_heads, head_size] |
| 1989 | # (Must be on GPU) |
| 1990 | value_cache: torch.Tensor, # Destination: (Must be on GPU) |
| 1991 | slot_mapping: torch.Tensor, # Indices: [num_tokens] |
| 1992 | layer_idx: int, |
| 1993 | ): |
| 1994 | """ |
| 1995 | Python implementation of reshape_and_cache_back_flash. |
| 1996 | |
| 1997 | Operation: |
| 1998 | Flat Tensor (Source) -> Paged Attention Cache (Destination) |
| 1999 | |
| 2000 | Logic: |
| 2001 | 1. Extract the specific layer's data from key_value. |
| 2002 | 2. Move it to the GPU (if it's on CPU). |
| 2003 | 3. Reshape it to match the cache's head structure. |
| 2004 | 4. Scatter (write) it into the non-contiguous cache blocks using slot_mapping. |
| 2005 | """ |
| 2006 | |
| 2007 | # 1. Setup Device & Dimensions |
| 2008 | # The cache is on the GPU, so all indices and source data must eventually be there. |
| 2009 | device = key_cache.device |
| 2010 | |
| 2011 | block_size = key_cache.size(1) |
| 2012 | num_heads = key_cache.size(2) |
| 2013 | head_size = key_cache.size(3) |
| 2014 | |
| 2015 | # 2. Prepare Indices |
| 2016 | # slot_mapping might be on CPU, must move to GPU for indexing. |
| 2017 | slot_mapping = slot_mapping.to(device=device, dtype=torch.long) |
| 2018 | |
| 2019 | # Calculate physical block indices and offsets |
| 2020 | block_indices = torch.div(slot_mapping, block_size, rounding_mode="floor") |
| 2021 | block_offsets = slot_mapping % block_size |
| 2022 | |
| 2023 | # 3. Process Source Data (Key) |
| 2024 | # Step A: Slice the specific layer from the source tensor |
| 2025 | # Source shape: [2, num_layers, num_tokens, hidden_dim] -> [num_tokens, hidden_dim] |
| 2026 | k_src_flat = key_value[0, layer_idx] |
| 2027 | v_src_flat = key_value[1, layer_idx] |
| 2028 | |
| 2029 | # Step B: Reshape & Move to GPU |
| 2030 | # .to(device) handles the CPU -> GPU transfer if key_value is in pinned memory. |
| 2031 | # View shape: [num_tokens, num_heads, head_size] |
| 2032 | k_src = k_src_flat.to(device).view(-1, num_heads, head_size) |
| 2033 | v_src = v_src_flat.to(device).view(-1, num_heads, head_size) |
| 2034 | |
| 2035 | # 4. Write to Cache (Scatter) |
| 2036 | # Using Advanced Indexing to write data into specific blocks/offsets |
| 2037 | key_cache[block_indices, block_offsets] = k_src |
| 2038 | value_cache[block_indices, block_offsets] = v_src |
| 2039 | |
| 2040 |