| name | code-writer |
| description | Generate implementation code scaffolding from paper descriptions when no source code exists. |
Code-Writer Skill
Purpose
When a paper has no source code, this skill generates a complete project scaffolding with model, training, data loading, and evaluation code โ ready to fill in and run.
When to Use
- Paper has no associated GitHub repository
- User says "implement this paper" or "write code for this model"
- After
paper-parser has extracted the paper content but paper-finder found no code
- When reproducing a paper from scratch
Prerequisites
- Paper content (from
paper-parser JSON/MD, or user description)
formula2code skill (for converting paper equations to code)
code-analyzer skill (for analyzing reference implementations)
paper-finder skill (for searching reference code)
Architecture
Paper Content (JSON/MD/description)
โ
โผ
Phase 0: Reference Code Discovery โ KEY INNOVATION
โ "Papers don't exist in a vacuum โ find code for what they build on"
โ โ paper-finder: search for base methods' implementations
โ โ code-analyzer: analyze found repos for reusable components
โผ
Phase 1: Paper Info Extraction
โ โ extractors/paper_info.py: title, abstract, sections
โ โ extractors/architecture.py: model components, layer types
โ โ extractors/equations.py: LaTeX formulas
โ โ extractors/experiment.py: hyperparameters, datasets, metrics
โผ
Phase 2: Code Generation (with reference code guidance)
โ โ generate.py: fill templates with extracted info
โ โ formula2code: convert equations to loss/model code
โผ
Phase 3: Output
project/
โโโ configs/default.yaml # Hyperparameters from paper
โโโ src/
โ โโโ model.py # Architecture skeleton with TODOs
โ โโโ train.py # Training loop (standard PyTorch)
โ โโโ data.py # Data loading skeleton
โ โโโ evaluate.py # (generated when metrics detected)
โโโ references/
โ โโโ search_plan.md # What code to search for
โโโ implementation_checklist.md # Step-by-step guide
โโโ requirements.txt
โโโ README.md
Usage
Quick Start
python code-writer/generate.py --paper workspace/<paper>/paper_content.json -o workspace/<paper>/implementation/
python code-writer/generate.py --markdown workspace/<paper>/paper.md -o workspace/<paper>/implementation/
python code-writer/generate.py --describe "A transformer-based model for image classification with self-attention" -o project/
Agent Workflow โ How to Implement a Paper from Scratch
This is the most important section. Follow this methodology step by step.
Step 0: Reference Code Discovery (BEFORE writing any code)
The paper you're implementing is NOT invented from nothing. It builds on existing methods. Find their code first.
-
Read the paper (from paper-parser output) and identify:
- What base methods/architectures it uses (e.g., "extends ResNet", "uses Transformer encoder")
- Which cited papers are most important (usually the "baseline" they compare against)
-
Run generate.py to get the scaffold AND the references/search_plan.md
-
Execute the search plan using paper-finder:
paper-finder: search "ResNet PyTorch implementation" on GitHub
paper-finder: search base paper on Papers With Code
-
Clone & analyze the most relevant reference repo:
git clone <reference_repo> workspace/<paper>/references/<name>
code-analyzer: analyze workspace/<paper>/references/<name>
-
Study the reference code โ pay attention to:
- How they implement the specific layers you need
- Their training loop structure
- Data preprocessing pipeline
- Loss function implementation
Step 1: Data Pipeline (FIRST!)
Why first? Because wrong data = nothing else matters.
- Read the paper's experiment section for dataset details
- Implement
src/data.py with actual data loading
- Verify:
print(next(iter(loader))) should match expected shapes
- Check normalization: [0,1] or [-1,1]? ImageNet stats?
Step 2: Model Components (Bottom-Up)
Why bottom-up? Build small, test small, then compose.
- Implement each component as a separate
nn.Module
- For each component:
block = MyBlock()
x = torch.randn(2, 64, 32, 32)
print(block(x).shape)
- Use
formula2code for any mathematical operations:
python formula2code/convert.py "<latex from paper>" --to pytorch -v
Step 3: Assemble Model
- Connect components in the main model class
- Verify:
model(dummy_input).shape should match paper's output description
- Check parameter count against paper (if reported)
Step 4: Loss Function
- Use
formula2code to convert the paper's loss equation
- If it's a standard loss (CE, MSE, etc.), use PyTorch built-in
- Verify: loss should be a scalar
Step 5: Training Loop
- The scaffold already generates a working loop โ customize it:
- Add learning rate scheduler (check paper for warmup/cosine)
- Add gradient clipping (if mentioned)
- Add evaluation loop
- Critical test: Overfit on 5 samples
python src/train.py --epochs 1000 --batch-size 5
Step 6: Evaluate & Compare
- Implement evaluation metrics from the paper
- Compare against paper's reported results (Table 1)
Common Pitfalls & Debugging Guide
| Problem | Symptom | Fix |
|---|
| Wrong normalization | Training doesn't converge | Check if paper uses [0,1] or [-1,1] |
| Missing weight init | Poor performance | Check paper appendix for init method |
| Wrong norm layer | NaN losses | BatchNorm vs LayerNorm matters! |
| No LR schedule | Can't match paper results | Add warmup + cosine decay |
| Wrong loss function | Loss doesn't decrease | Verify with formula2code |
| Dimension mismatch | Runtime crash | Test each component with dummy tensors |
Dependencies
pip install -r code-writer/requirements.txt
Examples
See code-writer/examples/ for runnable demos:
| Example | What it Shows |
|---|
01_generate_from_description.py | Full project generation from paper-like data |
02_extractors_demo.py | Each extractor running independently |
Quick Example: Architecture Detection
from extractors.architecture import extract_architecture
sections = [{'title': 'Method', 'content': 'We use a U-Net with self-attention...'}]
arch = extract_architecture(sections)
Quick Example: Hyperparameter Extraction
from extractors.experiment import extract_experiment
sections = [{'title': 'Experiments', 'content': 'Adam optimizer lr 1e-4, batch 64, 200 epochs on CIFAR-10'}]
exp = extract_experiment(sections)
Quick Example: Reference Code Discovery
from extractors.reference_finder import find_reference_code
paper_info = {'title': 'My DDPM Paper', 'abstract': 'diffusion model using U-Net...', ...}
refs = find_reference_code(paper_info)
๐ Reference URLs (for agent self-help)
When stuck, the agent should fetch these URLs for guidance:
| Topic | URL |
|---|
| Jinja2 template syntax | https://jinja.palletsprojects.com/en/3.1.x/templates/ |
| PyYAML documentation | https://pyyaml.org/wiki/PyYAMLDocumentation |
| PyTorch model best practices | https://pytorch.org/tutorials/beginner/introyt/modelsyt_tutorial.html |
| PyTorch training loop | https://pytorch.org/tutorials/beginner/introyt/trainingyt.html |
| PyTorch data loading | https://pytorch.org/tutorials/beginner/basics/data_tutorial.html |
| Papers With Code methods | https://paperswithcode.com/methods |
| torchvision datasets | https://pytorch.org/vision/stable/datasets.html |
| Hugging Face datasets | https://huggingface.co/docs/datasets/ |
| Common training recipes | https://github.com/pytorch/examples |
Template Customization Help
When modifying Jinja2 templates:
# Fetch Jinja2 template syntax docs:
fetch_url("https://jinja.palletsprojects.com/en/3.1.x/templates/")
Dataset Implementation Help
When implementing data loading:
# Fetch PyTorch data loading tutorial:
fetch_url("https://pytorch.org/tutorials/beginner/basics/data_tutorial.html")
# Fetch torchvision available datasets:
fetch_url("https://pytorch.org/vision/stable/datasets.html")