BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / LLM inference and serving2 of 36 in topic

428. Work out how many tokens fit in GPU memory for the KV cache

EasyLLM inference and serving
Asked atNVIDIA

Serving capacity is often set by KV cache memory, not by compute. Every token stores one key and one value vector per layer per key/value head.

Write kv_cache_bytes(tokens, layers, kv_heads, head_dim, bytes_per_value=2): 2 (key and value) x tokens x layers x kv_heads x head_dim x bytes_per_value.

Write max_tokens(free_bytes, layers, kv_heads, head_dim, bytes_per_value=2): the largest whole number of tokens whose cache fits in free_bytes.

Functiondef kv_cache_bytes(tokens, layers, kv_heads, head_dim, bytes_per_value=2)

Example 1

32 layers, 8 KV heads, head_dim 128, 2 bytes each is 131,072 bytes (128 KiB) per token.

Example 2

Input: tokens = 1, layers = 32, kv_heads = 8, head_dim = 128

Output: 131072

Example 3

Input: tokens = 4096, layers = 32, kv_heads = 8, head_dim = 128

Output: 4096 * 131072

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Your code must pass all 5 tests to be accepted.
Hint 1

Everything is linear in the token count, so first work out the bytes needed for a single token.

Hint 2

kv_cache_bytes is the product 2 x tokens x layers x kv_heads x head_dim x bytes_per_value, and max_tokens divides free_bytes by the bytes for one token.

Hint 3

Use integer floor division // in max_tokens so you get the largest whole number of tokens that fits; you can reuse kv_cache_bytes(1, ...) as the per-token cost.

Understand the idea

Every token an LLM keeps in context stores a key vector and a value vector in every layer for every key/value head. So the memory per token is a simple product, and dividing free memory by it tells you how many tokens fit. Like working out how many boxes fit on a shelf when you know each box size. Worked example from the prompt: 2 (key and value) x 32 layers x 8 KV heads x 128 head_dim x 2 bytes = 131,072 bytes per token. With 1 GiB free (1,073,741,824 bytes) you can hold 1,073,741,824 // 131,072 = 8,192 tokens. Smaller case: 2 layers, 1 KV head, head_dim 4, 2 bytes gives 2x2x1x4x2 = 32 bytes per token; with 100 bytes free, 100 // 32 = 3 tokens.

bytes = 2 x tokens x layers x kv_heads x head_dim x bytes_per_value; max tokens = free // per-token bytes

Serving teams use this to set batch sizes and context limits.

Topics
LLM inference and servingLLM architecture track
Companies1
  • NVIDIA the topic appears in interview reports NVIDIA Interview Prep: Questions, Process & Study Plan

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Trim a chat history to fit the context windowEasy
  • Compute TTFT, TPOT and throughput from request timestampsEasy
  • Price an API request with cached input tokensEasy
  • Size concurrency with Little's lawEasy
All LLM inference and serving problems
Report
428 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
7
8
def kv_cache_bytes(tokens, layers, kv_heads, head_dim, bytes_per_value=2):
# your code here
pass
​
def max_tokens(free_bytes, layers, kv_heads, head_dim, bytes_per_value=2):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 5 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS