MCPcopy Create free account
hub / github.com/microsoft/BitNet / make_cache

Function make_cache

gpu/model.py:299–343  ·  view source on GitHub ↗

Allocate a cache to be used with the Transformer module. Args: args (ModelArgs): the model configuration. length (int): per layer cache size. It is usually budgeted as ``max_batch * max_seq`` device (torch.device, optional): the device on which

(
    args: ModelArgs,
    length: int,
    device: Optional[Union[str, torch.device]] = None,
    n_layers: Optional[int] = None,
    dtype: Optional[torch.dtype] = None,
)

Source from the content-addressed store, hash-verified

297
298
299def make_cache(
300 args: ModelArgs,
301 length: int,
302 device: Optional[Union[str, torch.device]] = None,
303 n_layers: Optional[int] = None,
304 dtype: Optional[torch.dtype] = None,
305) -> list[LayerCache]:
306 """
307 Allocate a cache to be used with the Transformer module.
308
309 Args:
310 args (ModelArgs): the model configuration.
311 length (int): per layer cache size.
312 It is usually budgeted as ``max_batch * max_seq``
313 device (torch.device, optional): the device on which
314 the cache should be allocated.
315 n_layers (int, optional): the number of layers to
316 allocate a cache for (defaults to the model
317 settings).
318 dtype (torch.dtype, optional): the dtype to use for
319 cache entries (defaults to the default dtype).
320
321 Returns:
322 The cache object to pass to ``Tranformer.forward``.
323 """
324
325 head_dim = args.dim // args.n_heads
326 n_kv_heads = args.n_kv_heads
327 if n_kv_heads is None:
328 n_kv_heads = args.n_heads
329 n_local_kv_heads = n_kv_heads
330
331 if n_layers is None:
332 n_layers = args.n_layers
333
334 shape = (1, length, n_local_kv_heads, 1, head_dim)
335 heads_per_group = args.n_heads // n_kv_heads
336 expansion = (-1, -1, -1, heads_per_group, -1)
337 return [
338 (
339 torch.zeros(shape, device=device, dtype=dtype).expand(expansion),
340 torch.zeros(shape, device=device, dtype=dtype).expand(expansion),
341 )
342 for _ in range(n_layers)
343 ]
344
345
346def cache_prefix(cache: list[LayerCache], length: int) -> list[LayerCache]:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected