Take a prefix view of a larger cache. The original cache object remains of identical size and valid after the shrinked alias has been used. This function is useful when a cache was allocated for a larger batch size than what is necessary. Args: cache: the
(cache: list[LayerCache], length: int)
| 344 | |
| 345 | |
| 346 | def cache_prefix(cache: list[LayerCache], length: int) -> list[LayerCache]: |
| 347 | """ |
| 348 | Take a prefix view of a larger cache. |
| 349 | |
| 350 | The original cache object remains of identical size and valid |
| 351 | after the shrinked alias has been used. This function is useful |
| 352 | when a cache was allocated for a larger batch size than what is |
| 353 | necessary. |
| 354 | |
| 355 | Args: |
| 356 | cache: the cache to take a view in. |
| 357 | length (int): the desired length |
| 358 | |
| 359 | Returns: |
| 360 | A view in the input cache object. |
| 361 | """ |
| 362 | |
| 363 | if len(cache) > 0: |
| 364 | assert cache[0][0].shape[1] >= length |
| 365 | |
| 366 | return [(ck[:, :length], cv[:, :length]) for ck, cv in cache] |
nothing calls this directly
no outgoing calls
no test coverage detected