CodeRepairLM is a compact decoder-only Transformer language model designed to automatically repair buggy Python code.
Unlike projects that simply fine-tune an existing code LLM, CodeRepairLM implements the core language-modeling pipeline from scratch: a Python-aware tokenizer, causal self-attention, Transformer blocks, LayerNorm, GELU, autoregressive generation, training loop, evaluation framework, execution-based validation, and iterative repair.
The project is intentionally small enough to understand and run locally while demonstrating the complete architecture behind an autoregressive code-repair system.
Given buggy Python code and optional debugging context:
Buggy Code
+
Bug Description
+
Error Message
+
Unit Tests
|
v
CodeRepairLM
|
v
Generated Repair
|
v
Syntax Check
|
v
Execution
|
v
Unit Tests
|
v
Final Repair
The model is trained using next-token language modeling:
where
| Area | Implementation |
|---|---|
| Language | Python |
| Framework | PyTorch |
| Model | Decoder-only Transformer |
| Architecture | GPT-style causal LM |
| Tokenizer | Custom Python-aware tokenizer |
| Attention | Multi-head causal self-attention |
| Normalization | Custom LayerNorm |
| Activation | Custom GELU |
| Feed Forward | Custom implementation |
| Generation | Autoregressive decoding |
| Objective | Next-token cross-entropy |
| Optimizer | AdamW |
| Scheduler | Cosine Annealing |
| Evaluation | Text + syntax + execution + tests |
| UI | Streamlit |
| Dataset | Synthetic Python repair examples |
| Execution | Temporary-directory subprocess execution |
| Pretrained Model | None |
| Hugging Face Transformers | Not used |
nn.Transformer |
Not used |
nn.MultiheadAttention |
Not used |
The project deliberately avoids high-level Transformer implementations.
The Transformer computation is explicitly implemented using tensor operations and basic neural-network primitives.
- Python-aware tokenization
- vocabulary construction
- causal attention
- Q/K/V projections
- attention scaling
- causal masking
- multi-head reshaping
- attention aggregation
- LayerNorm
- GELU
- feed-forward network
- residual connections
- Transformer blocks
- language-model head
- autoregressive generation
- training loop
- evaluation pipeline
- edit-distance calculation
- syntax validation
- execution-based validation
- iterative repair loop
PyTorch is used for low-level tensor computation, automatic differentiation, and standard primitives such as Linear, Embedding, and Dropout.
No pretrained Transformer or high-level Transformer wrapper is used.
The core neural network follows:
Input Token IDs
|
v
+-----------------------+
| Token Embedding |
+-----------------------+
|
+ <---- Positional Embedding
|
v
+-----------------------+
| Transformer Block |
| |
| LayerNorm |
| | |
| v |
| Causal Self-Attn |
| | |
| Residual Add |
| | |
| LayerNorm |
| | |
| v |
| Feed Forward |
| | |
| GELU |
| | |
| Residual Add |
+-----------------------+
|
v
Repeat L times
|
v
+-----------------------+
| Final LayerNorm |
+-----------------------+
|
v
+-----------------------+
| LM Head |
+-----------------------+
|
v
Vocabulary Logits
|
v
Next Token
The default model is intentionally small.
| Hyperparameter | Value |
|---|---|
| Vocabulary size | 512 |
| Model dimension | 64 |
| Attention heads | 4 |
| Transformer layers | 2 |
| Feed-forward dimension | 128 |
| Maximum sequence length | 128 |
| Dropout | 0.1 |
| Batch size | 4 |
| Epochs | 5 |
| Learning rate | 0.0005 |
| Weight decay | 0.01 |
| Optimizer | AdamW |
| Scheduler | Cosine Annealing |
| Maximum generation length | 128 |
| Maximum repair iterations | 3 |
The small architecture is intentional: the objective is transparency and understanding rather than maximizing parameter count.
Let the vocabulary size be
The learned embedding matrix is:
For token
Because self-attention does not inherently encode token order, learned positional embeddings are added:
where:
For hidden states
Attention scores are calculated as:
where
A causal mask prevents access to future tokens:
The attention matrix becomes:
The attention output is:
For
The outputs are concatenated:
and projected:
The implementation explicitly performs the head splitting, attention calculation, causal masking, concatenation, and output projection.
For hidden vector
Normalize:
Apply learned parameters:
The repository contains a custom LayerNorm implementation.
The feed-forward network uses GELU:
This activation is implemented directly rather than relying on a Transformer wrapper.
Each Transformer block contains a position-wise feed-forward network:
Architecture:
Hidden Dimension
|
v
Linear(d_model -> ff_dim)
|
v
GELU
|
v
Linear(ff_dim -> d_model)
|
v
Output
The project uses a pre-LayerNorm Transformer formulation.
Attention:
Feed-forward:
Therefore one Transformer block can be summarized as:
After the final Transformer block:
The hidden representation is projected to vocabulary space:
where:
The next-token probability distribution is:
The model generates one token at a time.
Given:
the next-token distribution is:
where
Then:
The generated token is appended to the context and generation continues until <EOS> or the maximum generation length.
CodeRepairLM contains a custom Python-aware tokenizer.
The tokenizer builds:
mappings and handles Python syntax elements including:
- keywords
- identifiers
- literals
- operators
- punctuation
- brackets
- strings
- comments
- newlines
- indentation-related tokens
Example:
def add(a, b):
return a + bis represented as a sequence of Python-aware tokens rather than characters.
| Token | Purpose |
|---|---|
<PAD> |
Sequence padding |
<UNK> |
Unknown token |
<BOS> |
Beginning of sequence |
<EOS> |
End of sequence |
<MASK> |
Reserved special token |
Implementation:
code_repair_lm/tokenizer.py
Training examples combine debugging information into a structured sequence:
BUG:
<buggy Python code>
DESC:
<description>
ERR:
<error message>
TESTS:
<unit tests>
FIX:
<corrected Python code>
The model therefore learns:
This formulation allows the model to use both source-code context and debugging feedback.
CodeRepairLM is trained using autoregressive next-token prediction.
For target sequence:
the objective is:
This is standard causal language-model cross-entropy.
Padding tokens are excluded from the loss.
For target token
For the complete sequence:
The model minimizes this loss through backpropagation.
The project uses AdamW.
First moment:
Second moment:
Bias correction:
Parameter update:
where:
-
$\eta$ is the learning rate -
$\lambda$ is weight decay
The learning rate follows cosine annealing:
This gradually decreases the learning rate throughout training.
The training loop monitors gradient magnitude:
Gradients are clipped using:
with a maximum gradient norm of
This prevents unusually large gradients from destabilizing optimization.
The current implementation uses a small synthetic Python code-repair dataset.
Each repair example contains:
buggy_code
bug_description
error_message
unit_tests
fixed_code
The dataset contains structured examples covering scenarios such as:
- variable-name errors
- logical errors
- edge cases
- input validation
- recursive functions
- list-processing bugs
- common Python implementation mistakes
The dataset is intentionally small because this repository is primarily a from-scratch architecture and research prototype.
The data interface is modular and can be replaced with a larger real-world repair corpus.
Code generation is evaluated at multiple levels.
This layered evaluation is important because textual similarity alone does not establish that a program has actually been repaired.
Lower is better.
Lower perplexity indicates better next-token modeling of the validation corpus.
This measures token-level generation accuracy.
The generated repair must exactly match the reference solution.
The implementation calculates Levenshtein distance.
Lower is better.
Generated Python is checked using Python's compiler:
compile(code, "<repair>", "exec")The metric is:
This captures runtime correctness beyond syntax.
This is particularly important for program-repair systems because functional correctness matters more than textual similarity.
The current implementation uses the repository's repair-success evaluation criteria.
The latest successful training run produced the following measured results:
| Metric | Result |
|---|---|
| Training Loss | 6.2248 |
| Validation Loss | 6.1315 |
| Perplexity | 460.1169 |
| Token Accuracy | 2.04% |
| Exact Match Accuracy | 0.00% |
| Mean Edit Distance | 375.0 |
| Syntax Validity Rate | 0.00% |
| Executable-Code Rate | 0.00% |
| Unit-Test Pass Rate | 0.00% |
| Repair Success Rate | 0.00% |
| Epoch | Training Loss | Validation Loss | Perplexity |
|---|---|---|---|
| 1 | 6.3800 | 6.2155 | 500.4449 |
| 2 | 6.3316 | 6.1775 | 481.8091 |
| 3 | 6.2841 | 6.1501 | 468.7534 |
| 4 | 6.2420 | 6.1355 | 461.9841 |
| 5 | 6.2248 | 6.1315 | 460.1169 |
The validation loss decreased consistently during training:
and validation perplexity decreased from:
This indicates that the model is learning the token distribution of the training domain.
However, the generation-based repair metrics remain poor.
That is expected for the current tiny synthetic dataset and small model.
The experiment demonstrates that:
A model can reduce cross-entropy and perplexity while still generating:
- syntactically invalid code
- incomplete repairs
- incorrect identifiers
- incorrect control flow
- code that executes but fails tests
Therefore, a practical code-repair model must be evaluated at multiple levels:
Token Prediction
|
v
Text Similarity
|
v
Syntax Validity
|
v
Runtime Execution
|
v
Unit Tests
|
v
Functional Correctness
This is one of the key motivations for including execution-based evaluation in CodeRepairLM.
The repository also contains an iterative repair loop.
Each iteration can use the observed failure information to construct the next repair attempt.
The system tracks:
- candidate repair
- compilation result
- execution result
- stdout
- stderr
- exception information
- test result
- iteration number
- final pass/fail status
Implementation:
code_repair_lm/sandbox.py
The project includes an interactive Streamlit application.
The interface accepts:
| Input | Description |
|---|---|
| Buggy Code | Python code containing the defect |
| Bug Description | Optional natural-language explanation |
| Error Message | Optional traceback/compiler information |
| Unit Tests | Optional validation tests |
The UI displays:
- generated repair
- code diff
- repair iterations
- execution results
- test results
- final validation status
Run:
streamlit run app.pyCodeRepairLM/
│
├── app.py
├── train.py
├── evaluate.py
├── requirements.txt
├── pyproject.toml
│
├── configs/
│ └── default.json
│
├── code_repair_lm/
│ ├── __init__.py
│ ├── config.py
│ ├── data.py
│ ├── evaluation.py
│ ├── model.py
│ ├── sandbox.py
│ ├── streamlit_app.py
│ ├── tokenizer.py
│ └── training.py
│
├── tests/
│ └── test_core.py
│
├── checkpoints/
│
├── artifacts/
│ ├── final_metrics.json
│ └── evaluation_metrics.json
│
└── README.md
Core Transformer implementation.
GELU
LayerNorm
CausalSelfAttention
FeedForward
TransformerBlock
CodeRepairLM
Autoregressive Generation
Custom Python-aware tokenizer.
Vocabulary Construction
Tokenization
Encoding
Decoding
Special Tokens
Token <-> ID Mapping
Dataset representation.
RepairExample
Synthetic Repair Corpus
Bug Metadata
Repair Context
Training infrastructure.
Seed Initialization
Batching
Loss Calculation
Backpropagation
Gradient Clipping
AdamW
Cosine Scheduler
Validation
Checkpointing
Evaluation framework.
Validation Loss
Perplexity
Token Accuracy
Exact Match
Edit Distance
Syntax Validation
Executable-Code Rate
Unit-Test Pass Rate
Repair Success
Execution and iterative repair.
Temporary Runtime
Python Compilation
Subprocess Execution
Unit-Test Execution
Output Capture
Exception Capture
Repair Iteration
Candidate Tracking
Interactive code-repair interface.
CodeRepairLM treats repair as a conditional generation problem:
A decoder-only architecture naturally supports this formulation.
Context + Previous Tokens
|
v
Causal Transformer
|
v
Next Token
Advantages:
- simple architecture
- naturally autoregressive
- directly supports code generation
- one Transformer stack
- straightforward training objective
- easy to implement from scratch
An encoder-only model is better suited to:
- classification
- representation learning
- bug detection
- embedding generation
It does not naturally generate an arbitrary repaired program.
An encoder-decoder architecture would explicitly separate:
Buggy Code + Context
|
v
Encoder
|
v
Representation
|
Cross Attention
|
v
Decoder
|
v
Fixed Code
This is also a valid design for code repair, but it introduces an additional Transformer stack and cross-attention mechanism.
For this project, decoder-only provides a compact architecture while still demonstrating the fundamental mechanics of an autoregressive language model.
Python was selected deliberately because it provides a practical environment for:
- rapid model experimentation
- code parsing
- syntax validation
- subprocess execution
- unit-test execution
- Streamlit deployment
- ML experimentation with PyTorch
It also allows the generated output to be validated directly using Python's own compiler and testing infrastructure.
The prototype uses temporary directories and subprocess execution to evaluate generated Python programs.
The execution flow is:
Generated Code
|
v
Temporary Directory
|
v
Compilation
|
v
Subprocess Execution
|
v
Capture stdout/stderr
|
v
Unit Tests
The current implementation is suitable for controlled experimentation.
It should not be considered a hardened security sandbox for arbitrary hostile code. A production deployment would require stronger isolation such as:
- containers or microVMs
- CPU limits
- memory limits
- execution timeouts
- filesystem restrictions
- network isolation
- process restrictions
- resource quotas
Clone the repository:
git clone https://github.com/Arjun-08/CodeRepairLM_tiny-LLM.git
cd CodeRepairLM_tiny-LLMInstall dependencies:
pip install -r requirements.txtRun:
python train.pyThe training process reports:
Epoch
Batch
Training Loss
Gradient Norm
Learning Rate
Validation Loss
Perplexity
Checkpoint Events
Runtime
Training artifacts are stored under:
checkpoints/
artifacts/
Run:
python evaluate.pyEvaluation results are written to:
artifacts/evaluation_metrics.json
Start the UI:
streamlit run app.pyThe browser interface allows you to submit buggy Python code and inspect the model's generated repair and validation results.
Run the project tests:
python -m pytest -qThe test suite covers the core project functionality.
The training pipeline supports deterministic experiment setup through a fixed seed.
Default:
{
"seed": 1337
}Model and training configuration are stored in:
configs/default.json
Checkpoints and evaluation artifacts are written to dedicated directories.
This repository should be viewed as a research and learning prototype, not a production-grade code-repair model.
The current dataset is very small and synthetic.
The default model contains only:
with:
Therefore its representational capacity is intentionally limited.
The default context window is only:
which limits the size of programs and debugging context that can be processed.
The custom tokenizer is Python-aware but does not provide the compression and vocabulary efficiency of modern BPE or SentencePiece tokenizers.
The current generation metrics are weak because the model is trained on a tiny corpus.
The execution environment is designed for experimentation rather than security-critical arbitrary-code execution.
- larger synthetic repair corpus
- GitHub bug-fix commits
- real Python repair datasets
- repository-level repair examples
- bug-type stratification
- repository-level train/test separation
- larger Transformer
- longer context
- improved tokenizer
- more layers
- more attention heads
- embedding weight tying
- learning-rate warmup
- label smoothing
- beam search
- top-k sampling
- top-p sampling
- compiler-feedback conditioning
- test-feedback conditioning
- execution-guided decoding
- multi-candidate generation
- candidate ranking
- best-of-$k$ repair
- confidence estimation
- repair-by-iteration analysis
- pass@k
- functional correctness
- repair success by bug type
- repair success by iteration
- inference latency
- parameter count
- memory footprint
- ablation studies
CodeRepairLM demonstrates the complete lifecycle of a small language model:
DATA
|
v
Custom Tokenizer
|
v
Token Embeddings
|
v
Causal Self-Attention
|
v
Transformer Blocks
|
v
Language Model
|
v
Next-Token Training
|
v
Autoregressive
Generation
|
v
Generated Repair
|
+----------+----------+
| | |
v v v
Syntax Execution Unit Tests
Check Check Check
| | |
+----------+----------+
|
v
Repair Decision
The project therefore covers both language-model fundamentals and software-engineering-oriented evaluation.
The central design principle of CodeRepairLM is:
A code-repair model should not be judged only by whether its output resembles a reference solution.
The more meaningful hierarchy is:
This project is an intentionally transparent implementation of that idea.
Working research prototype
The complete end-to-end pipeline is implemented:
The current experimental results establish a baseline for the architecture. The next major improvement is expanding the repair corpus and increasing model capacity so that improvements in language modeling translate into measurable functional repair performance.