Build A Large Language Model From Scratch Github Jun 2026
llm-from-scratch/ │ ├── data/ # Data handling modules │ ├── __init__.py │ ├── dataset.py # PyTorch Dataset class for text chunking │ └── tokenizer.py # BPE or Character-level tokenizer implementation │ ├── model/ # Core architecture │ ├── __init__.py │ ├── attention.py # Multi-head attention and Causal masking │ ├── feed_forward.py # FFN layers │ ├── transformer_block.py # Single Transformer Block composition │ └── gpt.py # The main GPT Model class (nn.Module) │ ├── config/ # Configuration files │ └── config.yaml # Hyperparameters (n_layer, n_head, lr, batch_size) │ ├── engine/ # Training and Inference logic │ ├── __init__.py │ ├── trainer.py # Training loop, optimization, checkpointing │ └── generator.py # Text generation and sampling strategies │ ├── scripts/ # Entry points │ ├── train.py # CLI script to start training │ └── inference.py # CLI script to generate text │ ├── requirements.txt # Dependencies (torch, numpy, tiktoken, etc.) └── README.md # Project documentation
@torch.inference_mode() def generate(self, idx, max_new_tokens, temperature=1.0, top_k=None): for _ in range(max_new_tokens): idx_cond = idx[:, -self.max_seq_len:] logits = self(idx_cond)[:, -1, :] / temperature if top_k is not None: v, _ = torch.topk(logits, min(top_k, logits.size(-1))) logits[logits < v[:, [-1]]] = -float('Inf') build a large language model from scratch github
# Train model device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) criterion = torch.nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) -self.max_seq_len:] logits = self(idx_cond)[: