893. Pick the biggest model that fits on a phone
Why it matters On-device AI (phones, laptops) is limited by RAM and memory bandwidth, not FLOPs. Picking a model for an app is this calculation.
Pick a model to run on a device with limited RAM and memory bandwidth.
For each candidate model:
- weight bytes: params_b * 1e9 * bits / 8
- KV cache bytes for the context: context * 2 * layers * kv_dim * 2 (keys and values in fp16)
- it fits if weight bytes + KV bytes <= (ram_gb - reserve_gb) * 1e9
- decode speed (memory-bound estimate): bandwidth_gbs * 1e9 / weight bytes tokens per second; it is fast enough if this is >= min_tok_s
Write pick_edge_model(models, ram_gb, reserve_gb, bandwidth_gbs, min_tok_s, context). Among models that fit and are fast enough, pick the one with the most parameters; ties go to more bits, then to the alphabetically first name.
Inputs: models is a list of dicts with keys name, params_b, bits, layers, kv_dim. The other inputs are numbers.
Return a dict {"name": str, "size_gb": weight bytes / 1e9 rounded to 2 decimals, "tok_s": rounded to 1 decimal}, or None if no model qualifies.
def pick_edge_model(models, ram_gb, reserve_gb, bandwidth_gbs, min_tok_s, context)Example 1
Input: pick_edge_model([{'name': 'gemma-2b-q4', 'params_b': 2.6, 'bits': 4, 'layers': 26, 'kv_dim': 1024}, {'name': 'llama-8b-q4', 'params_b': 8, 'bits': 4, 'layers': 32, 'kv_dim': 1024}, {'name': 'qwen-3b-q8', 'params_b': 3, 'bits': 8, 'layers': 36, 'kv_dim': 256}], 8, 3, 50, 10, 4096)
Output: {'name': 'llama-8b-q4', 'size_gb': 4.0, 'tok_s': 12.5}
Example 2
Input: models = [{'name': 'llama-8b-q4', 'params_b': 8, 'bits': 4, 'layers': 32, 'kv_dim': 1024}], ram_gb = 12, reserve_gb = 3, bandwidth_gbs = 30, min_tok_s = 10, context = 4096
Output: None
Example 3
Input: models = [{'name': 'big', 'params_b': 1.5, 'bits': 16, 'layers': 28, 'kv_dim': 256}, {'name': 'small', 'params_b': 0.5, 'bits': 16, 'layers': 24, 'kv_dim': 128}], ram_gb = 6, reserve_gb = 2, bandwidth_gbs = 60, min_tok_s = 10, context = 65536
Output: {'name': 'small', 'size_gb': 1.0, 'tok_s': 60.0}
Constraints
- Python 3 standard library. Code runs in your browser.
- Your code must pass all 5 tests to be accepted.
Hint 1
Compute weight bytes and KV bytes per model.
Hint 2
Filter by memory and by speed.
Hint 3
Pick with the key (-params_b, -bits, name).
Common mistakes
- Forgetting the KV cache in the memory check.
- Using bits instead of bytes for the size.
- Reserving memory twice.
Understand the idea
Decoding reads every weight once per token, so tokens per second is roughly memory bandwidth divided by model size. RAM must hold the weights plus the KV cache for the context, plus what the OS and app need. In the example the 8B 4-bit model needs 4 GB of weights and 0.54 GB of KV cache, which fits in the 5 GB available, and decodes at about 12.5 tokens per second, so it beats the smaller models. With only 30 GB/s of bandwidth it would drop to 7.5 tokens per second and the 3B 8-bit model would win instead.
Shapes and data flow
List of dicts in, dict or None out.