MCPcopy Create free account
hub / github.com/LMCache/LMCache / _per_layer_paged_shape

Function _per_layer_paged_shape

lmcache/python_ops_fallback.py:815–853  ·  view source on GitHub ↗

Return the logical shape of a single per-layer paged buffer tensor. Args: engine_kv_format: The format enum that describes how K/V tokens are laid out. nb: Number of blocks in the paged buffer (``shape_desc.nb``). bs: Tokens per block / block size (``shape_desc.bs``).

(
    engine_kv_format: EngineKVFormat,
    nb: int,
    bs: int,
    nh: int,
    hs: int,
)

Source from the content-addressed store, hash-verified

813
814
815def _per_layer_paged_shape(
816 engine_kv_format: EngineKVFormat,
817 nb: int,
818 bs: int,
819 nh: int,
820 hs: int,
821) -> tuple[int, ...]:
822 """Return the logical shape of a single per-layer paged buffer tensor.
823
824 Args:
825 engine_kv_format: The format enum that describes how K/V tokens are laid out.
826 nb: Number of blocks in the paged buffer (``shape_desc.nb``).
827 bs: Tokens per block / block size (``shape_desc.bs``).
828 nh: Number of attention heads (``shape_desc.nh``).
829 hs: Per-head hidden size (``shape_desc.hs``).
830
831 Returns:
832 A tuple representing the shape needed to reconstruct one layer's tensor
833 from a raw pointer via :func:`_tensor_from_ptr`.
834 """
835 fmt = int(engine_kv_format)
836 if fmt == int(EngineKVFormat.NL_X_NBBS_ONE_HS):
837 return (nb * bs, 1, hs)
838 if fmt == int(EngineKVFormat.NL_X_NB_BS_HS):
839 return (nb, bs, hs)
840 if fmt == int(EngineKVFormat.NL_X_TWO_NB_NH_BS_HS):
841 return (2, nb, nh, bs, hs)
842 if fmt == int(EngineKVFormat.NL_X_NB_TWO_NH_BS_HS):
843 return (nb, 2, nh, bs, hs)
844 if fmt == int(EngineKVFormat.NL_X_NB_NH_BS_TWO_HS):
845 # vLLM CPU blocks-first fused KV: K and V interleaved at the
846 # second-to-last dim so each layer is [NB, NH, BS, 2, HS].
847 return (nb, nh, bs, 2, hs)
848 if fmt == int(EngineKVFormat.NL_X_TWO_NB_BS_NH_HS):
849 return (2, nb, bs, nh, hs)
850 if fmt == int(EngineKVFormat.NL_X_NB_NH_BS_TWO_HS):
851 return (nb, nh, bs, 2, hs)
852 # Covers NL_X_NB_TWO_BS_NH_HS and any future NHD variants.
853 return (nb, 2, bs, nh, hs)
854
855
856def _infer_kv_dtype(

Callers 1

_normalize_paged_layersFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected