Python and NumPy

LLM coding practice

50 hands-on problems covering the core algorithms behind LLM inference: numerical stability, BPE and GQA attention, sampling and speculative decoding, paged KV memory and INT8/INT4 quantization, and continuous-batching schedulers.

Read every problem statement, function signature, and worked example below without signing in. Sign in on a desktop browser (1024px or wider) to run Python and NumPy tests in your browser and track completed problems.

Sign in to solve problems

Tensor and numerical foundations

10 problems

Production inference engines depend on numerically stable reductions, normalization layers, and batched projections so mixed-precision forward passes never overflow or lose rank.

  1. 01 Stable Vocabulary SoftmaxEasy
  2. 02 Normalize Retrieval EmbeddingsEasy
  3. 03 Batched Linear ProjectionEasy
  4. 04 RMSNormMedium
  5. 05 LayerNormMedium
  6. 06 Token Cross Entropy with Ignored LabelsMedium
  7. 07 Project and Split Packed QKVMedium
  8. 08 SwiGLU Feed-Forward LayerMedium
  9. 09 RMSNorm Backward PassHard
  10. 10 Gradients for Tied Token EmbeddingsHard

Tokenization and attention

10 problems

Every serving request begins with byte-level BPE tokenization and spends its prefill and decode compute inside causal, grouped-query, or tiled online-softmax attention kernels.

  1. 11 Byte Tokenization with EOSEasy
  2. 12 Masked Mean PoolingEasy
  3. 13 Causal Masks with Cached PositionsEasy
  4. 14 Ranked BPE MergesMedium
  5. 15 Incremental UTF-8 DecodingMedium
  6. 16 Rotary Position EmbeddingsMedium
  7. 17 Grouped-Query AttentionMedium
  8. 18 ALiBi Attention BiasMedium
  9. 19 Cached Transformer Decoder BlockHard
  10. 20 Attention with Online SoftmaxHard

Decoding and sampling

10 problems

Autoregressive servers run decoding rules on every generated token: temperature scaling, top-k and nucleus filtering, repetition penalties, grammar FSMs, and speculative draft verification.

  1. 21 Greedy Decoding with EOSEasy
  2. 22 Completed Stop SequencesEasy
  3. 23 Sign-Aware Repetition PenaltyEasy
  4. 24 Temperature and Top-K DistributionMedium
  5. 25 Nucleus Sampling CandidatesMedium
  6. 26 Contrastive Search RerankingMedium
  7. 27 Repeated N-Gram BlockingMedium
  8. 28 Finite-State Constrained DecodingMedium
  9. 29 Beam Search with Finished HypothesesHard
  10. 30 Speculative Acceptance and CorrectionHard

KV memory and quantization

10 problems

On serving GPUs, KV-cache capacity and weight precision set maximum concurrency and decode bandwidth. Engines page KV blocks, share prefixes with copy-on-write, and quantize tensors to INT8, FP8, and INT4.

  1. 31 Grouped-Query KV StorageEasy
  2. 32 Token-to-Page Address TranslationEasy
  3. 33 Per-Channel INT8 DequantizationEasy
  4. 34 KV Append and RollbackMedium
  5. 35 Attention Sinks and Sliding WindowsMedium
  6. 36 Per-Channel INT8 QuantizationMedium
  7. 37 Affine-Quantized Matrix MultiplicationMedium
  8. 38 Signed INT4 PackingMedium
  9. 39 Copy-on-Write KV PagesHard
  10. 40 Packed Groupwise INT4 MultiplicationHard

Batching and serving algorithms

10 problems

Modern LLM servers schedule work at every step: admitting requests under token budgets, chunking long prefills, reusing cached prefixes, and routing traffic to KV-warm workers.

  1. 41 Last Real Token LogitsEasy
  2. 42 TTFT and Decode LatencyEasy
  3. 43 Stable Length-Bucketed BatchesEasy
  4. 44 Deficit Round-Robin SchedulingMedium
  5. 45 Token-Budgeted Chunked PrefillMedium
  6. 46 Longest-Prefix LRU CacheMedium
  7. 47 KV-Aware Request RoutingMedium
  8. 48 Sliding Token-Budget AdmissionMedium
  9. 49 Paged Continuous BatchingHard
  10. 50 Minimum-Cost Padded Batch PartitionHard

Study the concepts alongside the code

Pair these implementations with the free Inference Engineering Foundations course, check real model configs in the compatibility directory, or size memory in the GPU memory calculator.