RL environment where an LLM agent has to fit a model into limited GPU memory while actually serving users. The agent inspects the model architecture, builds a VRAM calculator, picks a quantization strategy, and writes a serving solution - all under a hard VRAM budget.
Given a model + VRAM budget, the agent needs to serve N concurrent users with M context tokens while hitting quality and throughput targets. This means understanding:
- Model architectures (dense, MoE, hybrid attention, Mamba, MLA - they all have different KV cache behavior)
- KV cache scaling (per-token x layers x users, this is what actually kills you in production)
- Quantization trade-offs (int4/int8/fp16 for both weights and KV cache)
- Serving frameworks (vLLM continuous batching vs raw transformers)
- Constraints like throughput targets, banned quantization methods, no CPU offloading
Quality
/\
/ \ More quantization = fits in VRAM + faster
/ \ but degrades output quality
/ \
/________\
VRAM Speed
Model fit - weights have to fit in the VRAM budget after quantization. Agent picks fp16/int8/int4 and the method (bitsandbytes, GPTQ, AWQ).
KV cache for concurrent users - each user gets their own KV cache: KV_total = per_token * context_length * num_users * num_kv_layers. With 32 users at 32K context, KV cache dwarfs model weights. Agent decides whether to quantize KV, reduce context, or use vLLM's paged attention.
Throughput - model has to generate tokens fast enough per user. With many concurrent users, sequential generation is way too slow - vLLM continuous batching is basically required. Judge measures per-user throughput under load.
Each task hits a different sweet spot in this space. The agent can't just apply a default quantization recipe.
Tested: with 4 concurrent users, raw transformers gets 3.9 tok/s per user (fails 5 tok/s target), vLLM gets 8.7 tok/s (passes).
Produces 38,000+ unique tasks from combinations of:
| Dimension | Values | Why it matters |
|---|---|---|
| Models | 12 (Qwen3.5, Gemma4, Nemotron, GLM, GPT-OSS) | Different architectures |
| VRAM budgets | 2-40 GB | How tight the constraint is |
| Context lengths | 1K to 131K tokens | KV cache pressure |
| Concurrent users | 1, 4, 8, 16, 32 | KV cache multiplier |
| Throughput targets | 5, 20, 50 tokens/sec | Speed vs quality |
| Quantization constraints | any, no-4bit, no-bitsandbytes | Forces different approaches |
| CPU offload | allowed or not | GPU-only vs hybrid |
Trivially easy and impossible configs get filtered out automatically.
python vram_environment/task_generator.py --count-only # just show stats
python vram_environment/task_generator.py --curated # representative subset
python vram_environment/task_generator.py --all # everythingBased on compression ratio (fp16 total / VRAM budget):
| Difficulty | Ratio | ~Count | Meaning |
|---|---|---|---|
| Easy | 1.0-1.5x | ~4K | Light quantization enough |
| Medium | 1.5-2.5x | ~12K | Need aggressive quantization |
| Hard | 2.5-4.0x | ~12K | KV cache optimization critical |
| Expert | 4.0x+ | ~12K | Multi-technique creative solutions |
Constraints (no-4bit, no-bitsandbytes, no CPU offload) multiply the effective difficulty.
- Inspect model architecture via
AutoConfig.from_pretrained() - Write
/solution/vram_calculator.pywithestimate_vram()function - Plan optimization strategy using the calculator
- Implement
/solution/serve.pywithload_and_configure()function
Objective: Configure
Qwen/Qwen3.5-2Bto run on a GPU with only 4.0 GB VRAM.Must serve 4 concurrent users, each with up to 4,096 tokens context.
Constraints: no bitsandbytes, no CPU offloading, min 20 tok/s per user.
{
"task_id": "string",
"model_key": "string",
"model_id": "HuggingFace model ID",
"vram_budget_gb": 4.0,
"target_context_length": 4096,
"min_quality_score": 0.5,
"use_case": "description",
"eval_dataset": "standard",
"num_eval_samples": 100,
"max_concurrent_users": 4,
"throughput_target": "low|medium|high",
"quant_constraint": "any|no-4bit|no-bitsandbytes",
"allow_cpu_offload": true
}7 dimensions, continuous 0.0-1.0 score:
| Component | Weight | What it checks |
|---|---|---|
| Calculator | 10% | estimate_vram() works? Reports kv_cache_layers? |
| Constraints | 10% | Followed quant/offload rules? Hard fail on violation |
| Loading/VRAM | 10% | Model loads within budget? Good utilization (30-90%)? |
| Context | 10% | Processes target length? Finds needle-in-haystack? |
| Concurrent | 20% | N users without OOM? All get valid responses? |
| Quality | 20% | MCQ accuracy on 100 questions from ARC, MMLU, HellaSwag |
| Throughput | 20% | Per-user throughput under concurrent load |
Pass threshold: >= 0.5
Judge reads the source code (strips comments/docstrings first) and checks for banned patterns:
- no-bitsandbytes: fails if
BitsAndBytesConfig,load_in_4bit,load_in_8bitfound - no-4bit: fails if
4bit,nf4,int4patterns found - no CPU offload: fails if CPU offloading detected
Uses torch.cuda.set_per_process_memory_fraction() before any CUDA op in a subprocess. Creates a hard memory limit - allocations beyond budget cause OutOfMemoryError, same as running on a smaller GPU.
- Needle-in-haystack uses random content each run
- Execution-based - model actually runs on GPU, no faking it
- Source code analysis (strips comments) catches banned patterns before execution
- 7 scoring dimensions so you can't just optimize one
- Handles chat templates and strips thinking tokens from reasoning models
from vllm import LLM
model = LLM(
model="Qwen/Qwen3.5-2B",
quantization="bitsandbytes",
max_model_len=4096,
max_num_seqs=4,
gpu_memory_utilization=0.9,
)See examples/serve_vllm.py for a working example.
LangGraph ReAct agent with tools (Python REPL, file I/O, shell). Solves tasks autonomously.
python vram_environment/agent_runner.py \
--task-config '<json>' \
--model claude-sonnet-4-6 \
--solution-dir /tmp/solution \
--judgeThe RL loop: agent writes solution -> judge scores it -> reward signal.
python vram_environment/judge.py --solution examples/serve.py \
--config '{"task_id":"test","model_key":"qwen3.5-0.8b","model_id":"Qwen/Qwen3.5-0.8B","vram_budget_gb":4.0,"target_context_length":4096,"min_quality_score":0.3,"use_case":"Chatbot","eval_dataset":"standard","num_eval_samples":100,"max_concurrent_users":1,"throughput_target":"low","quant_constraint":"any","allow_cpu_offload":true}'cd vram_environment && docker build -t vram-env .
./vram_environment/run_sandbox.sh ./examples '<task_config_json>'Sandbox: --network=none, --read-only, --cap-drop=ALL, --no-new-privileges, 16GB memory limit, non-root, HF cache mounted read-only, 600s timeout.
vram_environment/
task_generator.py # generates 38K+ tasks
judge.py # 7-dimension scoring
reference_solution.py # minimal working solution
agent_runner.py # LangChain/LangGraph agent
eval_datasets.py # ARC, MMLU, HellaSwag loader
Dockerfile # production container (CUDA 12.6)
run_sandbox.sh # isolated sandbox runner
examples/
serve.py # Qwen3.5-0.8B int4
serve_2b.py # Qwen3.5-2B int4
serve_gemma4.py # Gemma4-E2B int4
serve_vllm.py # Qwen3.5-0.8B via vLLM
vram_calculator.py # VRAM estimation reference
| Family | Models | Architecture | KV Cache |
|---|---|---|---|
| Qwen3.5 | 0.8B, 2B, 4B, 9B | Hybrid DeltaNet | ~25% layers have KV cache |
| Qwen3.5 MoE | 35B-A3B | DeltaNet + 256 experts | 10/40 KV layers |
| Gemma4 | E2B, E4B | Hybrid sliding/full attn | Sliding window + full (5:1) |
| Gemma4 | 26B-A4B | MoE + hybrid attn | Same pattern |
| Gemma4 | 31B | Dense + hybrid attn | 60 layers, 58GB fp16 |
| Nemotron | Cascade-2-30B-A3B | Mamba2 + Transformer MoE | ~8 attention layers |
| GLM-4.7 | Flash | MoE + MLA | Multi-head Latent Attention |
| GPT-OSS | 20B | Hybrid attention MoE | 12 sliding + 12 full |
All passing on L40S (48GB):
| Test | Model | VRAM | Score | Notes |
|---|---|---|---|---|
| easy_single_user | Qwen3.5-0.8B | 4GB | 0.91 | basic |
| med_4_users | Qwen3.5-0.8B | 4GB | 0.78 | transformers backend |
| vllm_4_users | Qwen3.5-0.8B | 4GB | 0.87 | vLLM batching |
| high_throughput | Qwen3.5-0.8B | 4GB | 0.71 | 50 tok/s target |
| 2b_tight | Qwen3.5-2B | 4GB | 0.98 | tight VRAM |
| gemma4_12gb | Gemma4-E2B | 12GB | 0.98 | |
| constraint_fail | Qwen3.5-0.8B | 4GB | 0.10 | expected fail (no-bnb) |
| missing_file | - | - | 0.00 | expected fail |
vLLM (0.87) beats transformers (0.78) on concurrent tasks - continuous batching matters.
Add a ModelSpec to the dict in task_generator.py. Get values from AutoConfig.from_pretrained() - watch for text_config on multimodal models and layer_types on hybrid attention models.
"model-key": ModelSpec(
model_id="org/model-name",
family="family-name",
params_b=7.0,
weights_fp16_gb=14.0,
num_layers=32,
num_q_heads=32,
num_kv_heads=8,
head_dim=128,
hidden_size=4096,
max_position_embeddings=131072,
is_moe=False,
kv_cache_layers=None, # None = all layers have KV cache
)