--- library_name: transformers license: mit pipeline_tag: text-generation base_model: openai-community/gpt2 tags: - gpt2 - code-generation - causal-lm - transformers - trainer - codeparrot - educational - huggingface-course --- # CodeParrot GPT-2 — Trainer Experiment A **GPT-2-style causal language model for code generation**, trained as part of my hands-on study of language-model training with the Hugging Face ecosystem. This checkpoint represents the **`Trainer` implementation** of the experiment. A companion repository reproduces the same learning workflow using a lower-level **Accelerate-based training loop**: ➡️ [`rIsHu009/codeparrot-gpt2-accelerate`](https://huggingface.co/rIsHu009/codeparrot-gpt2-accelerate) > This repository is preserved as an educational training artifact rather than presented as a production code-generation model. ## Experiment The goal of this project was to explore the end-to-end workflow behind training a causal language model on source code: ```text source code ↓ code-oriented BPE tokenizer ↓ GPT-2 causal language model ↓ Hugging Face Trainer ↓ next-token prediction ``` The companion Accelerate checkpoint provides a useful contrast between Hugging Face's high-level `Trainer` abstraction and a more explicit training workflow. ## Model Architecture | Property | Value | |---|---:| | Architecture | `GPT2LMHeadModel` | | Task | Causal Language Modeling / Text Generation | | Transformer layers | 12 | | Hidden size | 768 | | Attention heads | 12 | | Vocabulary size | 50,000 | | Training context length | 128 tokens | | Maximum position embeddings | 1,024 | | Weight precision | FP32 | | Activation | GELU New | The model configuration follows the GPT-2 architecture while using a code-oriented tokenizer and a shortened training context. ## Tokenizer The repository includes a **50,000-token Byte-Level BPE tokenizer** adapted for source-code text. Its vocabulary contains common programming patterns and tokens such as: ```text def return self None value file data error config ``` This allows the tokenizer to represent common code structures more efficiently than treating the experiment as ordinary natural-language generation. The special `<|endoftext|>` token is used for sequence boundaries and padding in the Trainer workflow. ## Training Configuration The preserved Trainer metadata records the following configuration: | Hyperparameter | Value | |---|---:| | Learning rate | `5e-4` | | Train batch size | `64` | | Evaluation batch size | `64` | | Gradient accumulation | `8` | | Effective train batch size | `512` | | Seed | `42` | | Optimizer | AdamW | | LR scheduler | Cosine | | Warmup steps | `1000` | | Epochs | `1` | | Mixed precision | Native AMP | ### Historical software environment - Transformers `4.51.3` - PyTorch `2.6.0+cu124` - Datasets `3.6.0` - Tokenizers `0.21.1` ## Quick Start ```python from transformers import AutoTokenizer, AutoModelForCausalLM MODEL_ID = "rIsHu009/codeparrot-gpt2-trainer" tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) model = AutoModelForCausalLM.from_pretrained(MODEL_ID) prompt = """def fibonacci(n): if n <= 1: """ inputs = tokenizer( prompt, return_tensors="pt", ) outputs = model.generate( **inputs, max_new_tokens=80, do_sample=True, temperature=0.8, top_p=0.95, ) print( tokenizer.decode( outputs[0], skip_special_tokens=True, ) ) ``` ## Trainer vs Accelerate This checkpoint is useful together with its companion experiment: | Repository | Training approach | |---|---| | **`codeparrot-gpt2-trainer`** | Hugging Face `Trainer` | | [`codeparrot-gpt2-accelerate`](https://huggingface.co/rIsHu009/codeparrot-gpt2-accelerate) | Accelerate/manual training workflow | The pair is retained to document two levels of abstraction for Transformer training. `Trainer` handles much of the training infrastructure automatically, while an Accelerate-oriented workflow exposes more of the optimization and training loop directly. ## Intended Use This checkpoint is primarily useful for: - learning causal language modeling; - experimenting with code generation; - studying code-oriented tokenization; - learning the Hugging Face `Trainer`; - comparing `Trainer` with Accelerate; - experimenting with GPT-2 architecture and generation; - educational inference demonstrations. ## Evaluation No verified benchmark results were preserved with this checkpoint. For that reason, this model card intentionally does **not** report unsupported code-generation accuracy, pass@k, perplexity, or other performance claims. The repository should be treated as a reproducible learning checkpoint rather than a benchmarked code model. ## Limitations This is a relatively small educational code-generation model. Generated code may: - be syntactically incorrect; - contain logical errors; - reference nonexistent APIs; - reproduce undesirable patterns from training data; - fail to complete complex programming tasks; - contain insecure or inefficient code. Generated programs should always be reviewed and tested before execution. ## Learning Context This experiment was created while studying Hugging Face language-model training workflows. The broader learning progression on this profile includes: - Transformer encoders; - question answering; - sequence classification; - named-entity recognition; - machine translation; - causal language modeling; - tokenizer training; - `Trainer`; - Accelerate; - dataset processing. ## Repository Contents The repository includes: - GPT-2 model configuration; - `model.safetensors`; - custom tokenizer files; - generation configuration; - Trainer metadata; - historical training arguments; - TensorBoard training artifacts. ## Companion Model ### Accelerate implementation [`rIsHu009/codeparrot-gpt2-accelerate`](https://huggingface.co/rIsHu009/codeparrot-gpt2-accelerate) The companion checkpoint represents the same learning direction using an Accelerate-oriented training workflow rather than the high-level Trainer abstraction. ## Author **Naman Chanana** - Hugging Face: `rIsHu009` - GitHub: `Rishy-09` --- *Educational code-language-model artifact preserved as part of an ongoing study of LLM training and the Hugging Face ecosystem.*