TensorTonic
Study Plans
Study Plan
Plan KV-Cache Capacity
Language ModelsInference
Medium

Estimate KV-cache capacity and decode throughput using the supplied memory-bandwidth model. Let PPP be model parameter count, bpb_pbp​ bytes per parameter, LLL layer count, SSS cached sequence length, HkvH_{kv}Hkv​ KV-head count, DDD head width, and beb_ebe​ bytes per KV element. Model storage MmodelM_{\mathrm{model}}Mmodel​ and per-request cache storage MrequestM_{\mathrm{request}}Mrequest​ are:

Mmodel=Pbp.M_{\mathrm{model}}=Pb_p.Mmodel​=Pbp​. Mrequest=2LSHkvDbe.M_{\mathrm{request}}=2LSH_{kv}Db_e.Mrequest​=2LSHkv​Dbe​.

The factor two accounts for keys and values. The supplied query-head count describes the attention layout; cache storage depends on KV heads, not query heads. For total GPU memory MMM and reserved memory RRR, the largest fitting batch BBB is:

B=max⁡(0,⌊M−R−MmodelMrequest⌋).B=\max\left(0,\left\lfloor\frac{M-R-M_{\mathrm{model}}}{M_{\mathrm{request}}}\right\rfloor\right).B=max(0,⌊Mrequest​M−R−Mmodel​​⌋).

Floor brackets round down to a whole request. Assume each decode step reads the model once and every fitted request's complete cache. For bandwidth WWW in bytes per second, step time TTT is:

T=Mmodel+BMrequestW.T=\frac{M_{\mathrm{model}}+BM_{\mathrm{request}}}{W}.T=WMmodel​+BMrequest​​.

Throughput is B/TB/TB/T, or zero when no request fits. The time formula still applies when B=0B=0B=0. Return a Python dictionary with model_bytes, kv_bytes_per_request, max_batch_size as Python integers, and seconds_per_token and tokens_per_second as Python floats. seconds_per_token is the time for one batched decode step.

Loading visualization...

Examples

Input: model_parameters = 1000, bytes_per_parameter = 2, num_layers = 2, sequence_length = 4, num_attention_heads = 8, num_kv_heads = 2, head_dim = 4, bytes_per_element = 2, memory_per_gpu = 10000, reserved_memory = 1000, memory_bandwidth = 100000

Output: {"model_bytes":2000,"kv_bytes_per_request":256,"max_batch_size":27,"seconds_per_token":0.08912,"tokens_per_second":302.962298}

Explanation: After model and reserved memory, 7000 bytes hold 27 complete request caches of 256 bytes each.

Input: model_parameters = 10000, bytes_per_parameter = 2, num_layers = 2, sequence_length = 4, num_attention_heads = 8, num_kv_heads = 1, head_dim = 4, bytes_per_element = 2, memory_per_gpu = 10000, reserved_memory = 0, memory_bandwidth = 100000

Output: {"model_bytes":20000,"kv_bytes_per_request":128,"max_batch_size":0,"seconds_per_token":0.2,"tokens_per_second":0}

Input: model_parameters = 500, bytes_per_parameter = 1, num_layers = 4, sequence_length = 8, num_attention_heads = 16, num_kv_heads = 1, head_dim = 8, bytes_per_element = 2, memory_per_gpu = 20000, reserved_memory = 500, memory_bandwidth = 200000

Output: {"model_bytes":500,"kv_bytes_per_request":1024,"max_batch_size":18,"seconds_per_token":0.09466,"tokens_per_second":190.154236}

Hint 1

The // operator gives an integer quotient rounded down.

Hint 2

max can enforce a nonnegative capacity.

Requirements

  • Keep memory and batch counts exact using Python integer arithmetic.
  • Follow the supplied bandwidth model, including zero-capacity behavior.
  • Return the specified integer counts and floating-point rates.

Constraints

  • Every argument is a Python integer.
  • model_parameters and reserved_memory are nonnegative; all other size, count, capacity, and bandwidth arguments are positive.
  • num_attention_heads is divisible by num_kv_heads.
  • All byte arithmetic precedes floating-point rate calculations.
  • A zero fitting batch is valid.
  • Input integers, model_bytes, kv_bytes_per_request, and max_batch_size are at most 9,007,199,254,740,991.
Try Similar Problems
Decode Attention with a KV CacheHardPerform Exact Speculative SamplingHardFind Reusable Prefixes in a KV TrieMediumRoute Cache-Aware Prefill RequestsHardSimulate Continuous Decode BatchingHardTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCompute Temperature-Based Data MixturesMediumImplement Prefix Cache Matching and ReuseMediumBeam Search DecodingMediumText Generation SamplingMediumImplement Temperature, Top-k, and Top-p SamplingMedium

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

You must run your code first.

Plan KV-Cache Capacity

Language ModelsInference
Medium

Estimate KV-cache capacity and decode throughput using the supplied memory-bandwidth model. Let PPP be model parameter count, bpb_pbp​ bytes per parameter, LLL layer count, SSS cached sequence length, HkvH_{kv}Hkv​ KV-head count, DDD head width, and beb_ebe​ bytes per KV element. Model storage MmodelM_{\mathrm{model}}Mmodel​ and per-request cache storage MrequestM_{\mathrm{request}}Mrequest​ are:

Mmodel=Pbp.M_{\mathrm{model}}=Pb_p.Mmodel​=Pbp​. Mrequest=2LSHkvDbe.M_{\mathrm{request}}=2LSH_{kv}Db_e.Mrequest​=2LSHkv​Dbe​.

The factor two accounts for keys and values. The supplied query-head count describes the attention layout; cache storage depends on KV heads, not query heads. For total GPU memory MMM and reserved memory RRR, the largest fitting batch BBB is:

B=max⁡(0,⌊M−R−MmodelMrequest⌋).B=\max\left(0,\left\lfloor\frac{M-R-M_{\mathrm{model}}}{M_{\mathrm{request}}}\right\rfloor\right).B=max(0,⌊Mrequest​M−R−Mmodel​​⌋).

Floor brackets round down to a whole request. Assume each decode step reads the model once and every fitted request's complete cache. For bandwidth WWW in bytes per second, step time TTT is:

T=Mmodel+BMrequestW.T=\frac{M_{\mathrm{model}}+BM_{\mathrm{request}}}{W}.T=WMmodel​+BMrequest​​.

Throughput is B/TB/TB/T, or zero when no request fits. The time formula still applies when B=0B=0B=0. Return a Python dictionary with model_bytes, kv_bytes_per_request, max_batch_size as Python integers, and seconds_per_token and tokens_per_second as Python floats. seconds_per_token is the time for one batched decode step.

Loading visualization...

Examples

Input: model_parameters = 1000, bytes_per_parameter = 2, num_layers = 2, sequence_length = 4, num_attention_heads = 8, num_kv_heads = 2, head_dim = 4, bytes_per_element = 2, memory_per_gpu = 10000, reserved_memory = 1000, memory_bandwidth = 100000

Output: {"model_bytes":2000,"kv_bytes_per_request":256,"max_batch_size":27,"seconds_per_token":0.08912,"tokens_per_second":302.962298}

Explanation: After model and reserved memory, 7000 bytes hold 27 complete request caches of 256 bytes each.

Input: model_parameters = 10000, bytes_per_parameter = 2, num_layers = 2, sequence_length = 4, num_attention_heads = 8, num_kv_heads = 1, head_dim = 4, bytes_per_element = 2, memory_per_gpu = 10000, reserved_memory = 0, memory_bandwidth = 100000

Output: {"model_bytes":20000,"kv_bytes_per_request":128,"max_batch_size":0,"seconds_per_token":0.2,"tokens_per_second":0}

Input: model_parameters = 500, bytes_per_parameter = 1, num_layers = 4, sequence_length = 8, num_attention_heads = 16, num_kv_heads = 1, head_dim = 8, bytes_per_element = 2, memory_per_gpu = 20000, reserved_memory = 500, memory_bandwidth = 200000

Output: {"model_bytes":500,"kv_bytes_per_request":1024,"max_batch_size":18,"seconds_per_token":0.09466,"tokens_per_second":190.154236}

Hint 1

The // operator gives an integer quotient rounded down.

Hint 2

max can enforce a nonnegative capacity.

Requirements

  • Keep memory and batch counts exact using Python integer arithmetic.
  • Follow the supplied bandwidth model, including zero-capacity behavior.
  • Return the specified integer counts and floating-point rates.

Constraints

  • Every argument is a Python integer.
  • model_parameters and reserved_memory are nonnegative; all other size, count, capacity, and bandwidth arguments are positive.
  • num_attention_heads is divisible by num_kv_heads.
  • All byte arithmetic precedes floating-point rate calculations.
  • A zero fitting batch is valid.
  • Input integers, model_bytes, kv_bytes_per_request, and max_batch_size are at most 9,007,199,254,740,991.
Try Similar Problems
Decode Attention with a KV CacheHardPerform Exact Speculative SamplingHardFind Reusable Prefixes in a KV TrieMediumRoute Cache-Aware Prefill RequestsHardSimulate Continuous Decode BatchingHardTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCompute Temperature-Based Data MixturesMediumImplement Prefix Cache Matching and ReuseMediumBeam Search DecodingMediumText Generation SamplingMediumImplement Temperature, Top-k, and Top-p SamplingMedium

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

You must run your code first.