Estimate KV-cache capacity and decode throughput using the supplied memory-bandwidth model. Let P be model parameter count, bp bytes per parameter, L layer count, S cached sequence length, Hkv KV-head count, D head width, and be bytes per KV element. Model storage Mmodel and per-request cache storage Mrequest are:
Mmodel=Pbp. Mrequest=2LSHkvDbe.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 M and reserved memory R, the largest fitting batch B is:
B=max(0,⌊MrequestM−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 W in bytes per second, step time T is:
T=WMmodel+BMrequest.Throughput is B/T, or zero when no request fits. The time formula still applies when B=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.
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}
The // operator gives an integer quotient rounded down.
max can enforce a nonnegative capacity.
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Estimate KV-cache capacity and decode throughput using the supplied memory-bandwidth model. Let P be model parameter count, bp bytes per parameter, L layer count, S cached sequence length, Hkv KV-head count, D head width, and be bytes per KV element. Model storage Mmodel and per-request cache storage Mrequest are:
Mmodel=Pbp. Mrequest=2LSHkvDbe.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 M and reserved memory R, the largest fitting batch B is:
B=max(0,⌊MrequestM−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 W in bytes per second, step time T is:
T=WMmodel+BMrequest.Throughput is B/T, or zero when no request fits. The time formula still applies when B=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.
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}
The // operator gives an integer quotient rounded down.
max can enforce a nonnegative capacity.
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number
Accepts: number