6300: Context Window Optimization - Practice¶
Exercises¶
Exercise 1: Long Context Benchmarking¶
Benchmark a causal LM across increasing context lengths: feed random token inputs at each length, measure peak GPU memory per forward pass, and plot memory against context length.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 2: Context Compression via Summarization¶
Implement context compression: when an input exceeds a word budget, split it into word-sized chunks, replace the older half with LLM summaries (prompt an instruction-tuned model - the task-specific summarization pipeline is retired in transformers 5.x), keep the recent chunks verbatim, and return the compressed context.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 3: Sliding Window Attention¶
Implement sliding-window self-attention as an nn.Module: project QKV, build the causal mask, then restrict each position's attention to a bounded window of preceding tokens, and verify the module forward-passes a long test sequence.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 4: Context Chunking Strategies¶
Write a context chunking function with three strategies - fixed word windows with overlap, semantic packing by paragraph, and recursive splitting - plus the recursive splitter behind the third mode, which falls through separators from coarsest to finest and hard-cuts on characters as a last resort.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 5: Context Relevance Scoring¶
Score context chunks by embedding similarity to the query, rank them, and assemble an optimal context window: keep selecting ranked chunks until a token budget is exhausted.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 6: LongContext LLM (Llama-2-Long style)¶
Wire up long-context generation: concatenate a large document set into one prompt, tokenize with truncation at the model's 32K-token ceiling, generate the response, and strip the prompt scaffolding so only the generated answer returns.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Exercise 7: Context Distillation¶
Build a context-distillation pipeline: have a long-context teacher answer each query over the full context, compress the contexts, and prepare student training examples that pair the compressed context, the query, and the teacher's answer.
Attempt it yourself first — the full worked answer is in the Answer Key at the end of this file.
Answer Key¶
Full solutions for every exercise above. Work through your own attempt before comparing.
Exercise 1: Long Context Benchmarking¶
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import matplotlib.pyplot as plt
def benchmark_context_length(model, tokenizer, lengths=None):
"""Benchmark model performance at different context lengths."""
if lengths is None:
lengths = [512, 1024, 2048, 4096, 8192]
results = []
device = next(model.parameters()).device
for length in lengths:
print(f"Testing length {length}...")
# Create input of specific length
input_ids = torch.randint(
0, tokenizer.vocab_size,
(1, length),
device=device
)
# Measure memory
torch.cuda.reset_peak_memory_stats()
start_mem = torch.cuda.memory_allocated() / 1024**3
# Forward pass
with torch.no_grad():
outputs = model(input_ids)
peak_mem = torch.cuda.max_memory_allocated() / 1024**3
memory_used = peak_mem - start_mem
results.append({
"length": length,
"memory_gb": memory_used,
"peak_memory_gb": peak_mem,
})
print(f" Memory used: {memory_used:.2f} GB")
return results
# Run benchmark
if __name__ == "__main__":
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-8B", device_map="auto")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-8B")
results = benchmark_context_length(model, tokenizer)
# Plot results
lengths = [r["length"] for r in results]
memories = [r["memory_gb"] for r in results]
plt.figure(figsize=(10, 6))
plt.plot(lengths, memories, marker='o', linewidth=2)
plt.xlabel("Context Length (tokens)")
plt.ylabel("Memory Usage (GB)")
plt.title("Memory Usage vs Context Length")
plt.grid(True)
plt.savefig("context_memory_benchmark.png")
print(f"\nResults:")
for r in results:
print(f" {r['length']:5d} tokens: {r['memory_gb']:.2f} GB")
# Expected output:
# - Memory usage scales linearly with context length
# - 512 tokens: ~2 GB
# - 4096 tokens: ~8 GB
# - 8192 tokens: ~16 GB (may exceed GPU memory)
Exercise 2: Context Compression via Summarization¶
from transformers import pipeline, GenerationConfig
# Load an instruct model - transformers 5.x retired the task-specific
# "summarization" pipeline; prompting an instruction-tuned LLM is the
# modern replacement (SmolLM2-1.7B-Instruct runs fine on CPU)
print("Loading summarization model...")
summarizer = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-1.7B-Instruct")
def summarize_text(text):
"""Summarize a passage with an instruction-following LLM."""
messages = [{"role": "user",
"content": f"Summarize the following text in 2-3 sentences:\n\n{text}"}]
result = summarizer(messages, generation_config=GenerationConfig(max_new_tokens=120))
return result[0]["generated_text"][-1]["content"].strip()
def compress_context(long_context, target_length=512):
"""Compress long context by summarizing older parts."""
# Everything is measured in words here: target_length, chunk_size
# and the split below share one unit, so a "512" budget really is
# 512 words (the old version compared a word count against
# character slices).
words = long_context.split()
if len(words) <= target_length:
return long_context
# Split into word-based chunks
chunk_size = 256
chunks = [
" ".join(words[i:i+chunk_size])
for i in range(0, len(words), chunk_size)
]
# Summarize older chunks, keep recent ones
num_summarize = len(chunks) // 2
compressed = ""
for i, chunk in enumerate(chunks):
if i < num_summarize:
# Summarize older chunks
summary = summarize_text(chunk)
compressed += f"[Summary] {summary} "
else:
# Keep recent chunks
compressed += chunk + " "
return compressed
# Test compression
long_text = " ".join(["This is a sentence about machine learning and artificial intelligence."] * 100)
print(f"Original: {len(long_text)} chars")
compressed = compress_context(long_text, target_length=500)
print(f"Compressed: {len(compressed)} chars")
print(f"Compression ratio: {len(compressed)/len(long_text):.1%}")
print(f"\nCompressed text preview:")
print(compressed[:300] + "...")
# Expected output:
# - Original text compressed significantly
# - Older content summarized, recent content preserved
# - Compression ratio: roughly 40-70%, depending on how much the
# older half condenses
Exercise 3: Sliding Window Attention¶
import torch
import torch.nn as nn
class SlidingWindowAttention(nn.Module):
def __init__(self, d_model, n_heads, window_size=512):
super().__init__()
self.window_size = window_size
self.n_heads = n_heads
self.head_dim = d_model // n_heads
self.qkv_proj = nn.Linear(d_model, 3 * d_model)
self.out_proj = nn.Linear(d_model, d_model)
def forward(self, x):
batch_size, seq_len, _ = x.shape
# Project QKV
qkv = self.qkv_proj(x)
q, k, v = qkv.chunk(3, dim=-1)
q = q.view(batch_size, seq_len, self.n_heads, self.head_dim).transpose(1, 2)
k = k.view(batch_size, seq_len, self.n_heads, self.head_dim).transpose(1, 2)
v = v.view(batch_size, seq_len, self.n_heads, self.head_dim).transpose(1, 2)
# Apply sliding window mask
scores = torch.matmul(q, k.transpose(-2, -1)) / (self.head_dim ** 0.5)
# Create sliding window mask
mask = torch.triu(torch.ones(seq_len, seq_len), diagonal=1)
mask = mask.masked_fill(mask == 1, float('-inf'))
# Limit to window size
for i in range(seq_len):
window_start = max(0, i - self.window_size)
mask[i, :window_start] = float('-inf')
scores = scores + mask.to(scores.device)
# Attention
attn = torch.softmax(scores, dim=-1)
output = torch.matmul(attn, v)
# Reshape and project
output = output.transpose(1, 2).contiguous().view(batch_size, seq_len, -1)
return self.out_proj(output)
# Test
if __name__ == "__main__":
d_model = 512
n_heads = 8
seq_len = 2048
batch_size = 2
model = SlidingWindowAttention(d_model, n_heads, window_size=256)
x = torch.randn(batch_size, seq_len, d_model)
output = model(x)
print(f"Input shape: {x.shape}")
print(f"Output shape: {output.shape}")
print(f"Sliding window attention working!")
# Expected output:
# - Each position only attends within its sliding window (semantics)
# - Note: this naive implementation still builds the full seq_len x
# seq_len score matrix (plus a Python loop for the window mask) -
# the O(n * window_size) savings only materialize in production
# kernels that exploit the sparsity (FlashAttention, Mistral's
# sliding-window attention)
# - Enables processing of much longer sequences
Exercise 4: Context Chunking Strategies¶
def chunk_context(context, chunk_size=512, overlap=50, strategy="fixed"):
"""Chunk context for processing."""
if strategy == "fixed":
# Fixed-size chunks
tokens = context.split()
chunks = []
for i in range(0, len(tokens), chunk_size - overlap):
chunk = " ".join(tokens[i:i+chunk_size])
chunks.append(chunk)
return chunks
elif strategy == "semantic":
# Semantic chunking (by paragraphs/sentences)
import re
paragraphs = context.split("\n\n")
chunks = []
current_chunk = ""
for para in paragraphs:
if len(current_chunk) + len(para) < chunk_size:
current_chunk += para + "\n\n"
else:
if current_chunk:
chunks.append(current_chunk.strip())
current_chunk = para + "\n\n"
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
elif strategy == "recursive":
# Recursive character splitting
separators = ["\n\n", "\n", ". ", " ", ""]
return _recursive_split(context, chunk_size, separators)
def _recursive_split(text, chunk_size, separators):
"""Split text by the coarsest separator that appears, packing the
pieces greedily; pieces still over budget fall through to the next
(finer) separator."""
if len(text) <= chunk_size:
return [text]
for i, sep in enumerate(separators):
if not sep:
# Last resort: no separator left ("".split would raise
# ValueError), so cut on fixed character boundaries.
return [text[j:j+chunk_size] for j in range(0, len(text), chunk_size)]
if sep not in text:
continue
rest = separators[i+1:]
chunks = []
current = ""
for part in text.split(sep):
if len(part) > chunk_size and rest:
# Flush what we have, then recurse into the oversized
# piece with finer separators.
if current:
chunks.append(current.strip())
current = ""
chunks.extend(_recursive_split(part, chunk_size, rest))
continue
if len(current) + len(part) + len(sep) <= chunk_size:
current += part + sep
else:
if current:
chunks.append(current.strip())
current = part + sep
if current:
chunks.append(current.strip())
return chunks
return [text]
# Test
long_text = """
Machine learning is a subset of artificial intelligence.
It focuses on building systems that can learn from data.
Deep learning uses neural networks with multiple layers.
These networks can model complex patterns in data.
NLP helps computers understand human language.
""" * 20
print("Testing chunking strategies:")
print(f"Original text: {len(long_text)} chars\n")
for strategy in ["fixed", "semantic", "recursive"]:
chunks = chunk_context(long_text, chunk_size=200, overlap=30, strategy=strategy)
print(f"{strategy.capitalize()}: {len(chunks)} chunks")
print(f" First chunk: {chunks[0][:80]}...")
print(f" Avg size: {sum(len(c) for c in chunks)//len(chunks)} chars\n")
# Expected output:
# - Fixed: Uniform chunks, may split mid-sentence
# - Semantic: Preserves paragraph boundaries
# - Recursive: Intelligent splitting, most readable
Exercise 5: Context Relevance Scoring¶
from sentence_transformers import SentenceTransformer
def score_context_relevance(query, context_chunks, embedder):
"""Score each chunk by relevance to query."""
# Embed query and chunks
query_emb = embedder.encode([query])
chunk_embs = embedder.encode(context_chunks)
# Compute similarity
from sklearn.metrics.pairwise import cosine_similarity
similarities = cosine_similarity(chunk_embs, query_emb).flatten()
# Rank chunks
ranked_chunks = [
{"chunk": chunk, "score": float(sim)}
for chunk, sim in zip(context_chunks, similarities)
]
ranked_chunks.sort(key=lambda x: x["score"], reverse=True)
return ranked_chunks
def build_optimal_context(query, full_context, embedder, max_tokens=4096):
"""Build context with most relevant chunks."""
# Chunk context
chunks = chunk_context(full_context, chunk_size=512, overlap=50)
# Score and rank
ranked = score_context_relevance(query, chunks, embedder)
# Select top chunks within budget
selected = []
total_tokens = 0
for item in ranked:
chunk_tokens = len(item["chunk"].split())
if total_tokens + chunk_tokens <= max_tokens:
selected.append(item)
total_tokens += chunk_tokens
else:
break
# Order by original position (optional)
return selected
# Test
embedder = SentenceTransformer('all-MiniLM-L6-v2')
query = "neural network architectures"
full_context = """
Machine learning models learn patterns from data.
Neural networks are a type of machine learning model.
Deep learning uses neural networks with multiple layers.
Transformers use self-attention mechanisms.
CNNs are good for image processing.
RNNs process sequential data.
Attention mechanisms help models focus on relevant parts.
""" * 5
selected = build_optimal_context(query, full_context, embedder, max_tokens=200)
print(f"Query: {query}\n")
print(f"Selected {len(selected)} relevant chunks:")
for i, item in enumerate(selected, 1):
print(f"{i}. [Score: {item['score']:.3f}] {item['chunk'][:60]}...")
# Expected output:
# - Chunks about neural networks and transformers ranked highest
# - CNN/RNN chunks ranked lower for this query
# - Most relevant content prioritized
Exercise 6: LongContext LLM (Llama-2-Long style)¶
from transformers import AutoModelForCausalLM, AutoTokenizer
# Use long context model
model = AutoModelForCausalLM.from_pretrained(
"togethercomputer/Llama-2-7B-32K", # 32K context
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("togethercomputer/Llama-2-7B-32K")
def generate_with_long_context(model, tokenizer, prompt, documents):
"""Generate with long context window."""
# Concatenate all documents
full_context = "\n\n".join(documents)
# Format prompt
formatted_prompt = f"""Context:
{full_context}
Question: {prompt}
Answer:"""
# Truncate if needed (for non-long models)
inputs = tokenizer(
formatted_prompt,
return_tensors="pt",
truncation=True,
max_length=32000, # Model's max
)
# Generate
outputs = model.generate(
**inputs,
max_new_tokens=500,
temperature=0.7,
do_sample=True,
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract just the answer part
if "Answer:" in response:
response = response.split("Answer:")[-1].strip()
return response
# Test
documents = [
"Document 1: Machine learning is about learning from data.",
"Document 2: Neural networks are biologically inspired.",
"Document 3: Transformers use attention mechanisms.",
] * 100 # Create a very long context
prompt = "What are the key differences between machine learning and neural networks?"
print(f"Total context length: {sum(len(d) for d in documents)} chars")
print(f"Question: {prompt}\n")
# answer = generate_with_long_context(model, tokenizer, prompt, documents)
# print(f"Answer: {answer}")
print("Long context generation ready!")
# Expected output:
# - Model can handle 32K token context
# - Maintains coherence across long documents
# - Answers based on information distributed across context
Exercise 7: Context Distillation¶
def distill_context(teacher_model, teacher_tokenizer, long_contexts, queries):
"""Distill knowledge from long-context to shorter-context model."""
# Generate answers with teacher (long context)
print("Generating teacher answers...")
teacher_answers = []
for context, query in zip(long_contexts, queries):
prompt = f"Context: {context}\n\nQuestion: {query}\nAnswer:"
# Use teacher with full context. The tokenizer is passed in
# separately - HF models don't carry a .tokenizer attribute.
inputs = teacher_tokenizer(prompt, return_tensors="pt", truncation=False)
outputs = teacher_model.generate(**inputs, max_new_tokens=200)
answer = teacher_tokenizer.decode(outputs[0], skip_special_tokens=True)
teacher_answers.append(answer)
# Compress contexts for student
print("Compressing contexts for student...")
compressed_contexts = []
for context in long_contexts:
compressed = compress_context(context, target_length=512)
compressed_contexts.append(compressed)
# Train student on compressed contexts + teacher answers
print("Training student model...")
training_data = []
for comp_context, teacher_answer, query in zip(compressed_contexts, teacher_answers, queries):
prompt = f"Context: {comp_context}\n\nQuestion: {query}\nAnswer: {teacher_answer}"
training_data.append(prompt)
# Student learns from compressed context
# (In practice, you would fine-tune here)
print(f"Prepared {len(training_data)} training examples")
return {
"teacher_answers": teacher_answers,
"compressed_contexts": compressed_contexts,
"training_data": training_data,
}
# Test
teacher_model = None # Your long-context teacher model
teacher_tokenizer = None # Its tokenizer
long_contexts = ["...long context..."] * 3
queries = ["What is X?", "Explain Y", "Compare Z and W"]
# results = distill_context(teacher_model, teacher_tokenizer, long_contexts, queries)
print("Context distillation framework ready!")
print("Student will learn to answer using compressed contexts")
# Expected output:
# - Teacher generates answers from full context
# - Contexts compressed for student
# - Student learns to mimic teacher with less context
# - Enables efficient deployment without sacrificing much quality