GitHub - rasbt/LLMs-from-scratch: Implement a ChatGPT-like LLM in PyTorch from scratch, step by step

GitHub - rasbt/LLMs-from-scratch: Implement a ChatGPT-like LLM in PyTorch from scratch, step by step

An open-source PyTorch guide by Sebastian Raschka to building a ChatGPT-like LLM from scratch, covering tokenization, attention, pretraining, and fine-tuning.

Overview

What is LLMs-from-scratch

LLMs-from-scratch is a comprehensive, open-source educational resource and code repository hosted on GitHub, created by machine learning expert Sebastian Raschka. The primary objective of this project is to demystify Large Language Models (LLMs) by guiding developers through the process of implementing a ChatGPT-like model entirely from scratch using PyTorch. Unlike high-level abstraction libraries such as Hugging Face Transformers, which often hide the underlying mechanics, this project exposes every component of the neural network architecture.

The repository serves as both a practical coding guide and a theoretical deep dive. It covers the full lifecycle of an LLM, starting with the construction of a Byte Pair Encoding (BPE) tokenizer, moving through the implementation of causal multi-head attention mechanisms and transformer blocks, and culminating in pretraining, finetuning, and instruction tuning phases. By providing step-by-step Jupyter notebooks and Python scripts, it allows users to understand the mathematical and engineering principles that power modern generative AI models. The project is licensed under the MIT License, making it freely accessible for educational and commercial experimentation.

For those interested in exploring similar developer tools and AI resources, you can browse our curated list of ToolSeekAI tools or view our AI rankings.

Key features

The LLMs-from-scratch repository distinguishes itself through its granular approach to model construction and its emphasis on foundational understanding. Key features include:

  • From-Scratch Implementation: Users build every critical component of an LLM using only PyTorch. This includes the tokenizer, attention layers, feed-forward networks, and training loops, ensuring no black-box dependencies obscure the learning process.
  • Step-by-Step Educational Structure: The project is organized into well-documented Jupyter notebooks that correspond to chapters in Sebastian Raschka’s book. Each stage builds upon the previous one, allowing learners to incrementally add complexity to their model.
  • Complete Training Pipeline: The guide covers the entire training spectrum. It begins with pretraining, where a GPT-like model is trained on a text corpus (such as "The Verdict") to learn language patterns. It proceeds to finetuning, adapting the pretrained model for specific downstream tasks like binary classification (e.g., spam detection). Finally, it addresses instruction tuning, where the model is fine-tuned on datasets like Alpaca to follow human instructions effectively.
  • Core Mechanism Implementation: The repository provides detailed implementations of essential LLM components, including causal multi-head attention, which is the core mechanism enabling GPT-style models to generate text sequentially while maintaining context.
  • Custom Tokenizer Construction: Rather than relying on pre-built tokenizers, users implement a Byte Pair Encoding (BPE) tokenizer from scratch. This feature highlights how raw text is converted into numerical tokens that neural networks can process.
  • Open-Source Accessibility: Hosted on GitHub with over 97k stars, the project benefits from a large community. It is fully open-source under the MIT license, encouraging modification and redistribution.

Use cases

LLMs-from-scratch is versatile and serves multiple audiences within the AI and software development communities:

  • Education and Learning: It is ideal for students, data scientists, and ML engineers who want to move beyond API usage and understand the inner workings of transformers. It serves as a hands-on project for AI/ML courses.
  • Research and Experimentation: Researchers can use the clean, minimal PyTorch implementation as a baseline for experimenting with novel architectural modifications, such as new attention mechanisms or training strategies, without the overhead of large frameworks.
  • Custom LLM Development: Developers aiming to build small-scale, domain-specific LLMs (e.g., for legal or medical texts) can use this guide to train models locally without relying on external APIs or expensive cloud inference services.
  • Prototyping: The modular nature of the code allows for rapid prototyping of ideas. Users can quickly test hypotheses about model behavior or data processing pipelines.

Pricing overview

The LLMs-from-scratch repository is completely free and open-source. There are no licensing fees, subscription costs, or paywalls associated with accessing the code, notebooks, or documentation. The MIT license permits unrestricted use, modification, and distribution.

However, users should note that while the software is free, the computational resources required to train these models are not. Pretraining and fine-tuning LLMs, even small ones, typically require significant GPU acceleration. If users do not have access to local high-performance hardware, they may incur costs by renting GPU instances from cloud providers such as AWS, Google Cloud, or Azure. These infrastructure costs are separate from the tool itself and depend on the scale of training and duration of compute time.

Who should use it

This tool is best suited for:

  • Machine Learning Engineers: Professionals seeking to deepen their technical expertise in transformer architectures and gain proficiency in PyTorch.
  • AI Researchers: Academics and industry researchers looking for a transparent, modifiable codebase to test new theories or compare against existing baselines.
  • Students: Individuals enrolled in AI or computer science programs who need a practical, project-based learning experience.
  • Developers: Software engineers interested in building custom, lightweight language models for specific applications where privacy or cost-efficiency is a priority.

For more information on how this tool compares to other AI development platforms, visit our tool comparisons.

Evaluation Context

Onboarding Flow: Getting started requires cloning the GitHub repository and installing the necessary Python dependencies, primarily PyTorch. The notebooks are self-contained, but users should ensure their environment supports the required CUDA versions if using GPUs.

Integration Considerations: As a standalone educational project, it does not integrate directly with enterprise MLOps pipelines out of the box. Users must manually export trained weights and integrate them into their own serving infrastructure.

Data Privacy: Since training occurs locally or on user-controlled cloud instances, data privacy is maintained. No user data is sent to third-party servers during the training process, making it suitable for sensitive domain applications.

Pricing Verification Checklist:

  • Is the software free? Yes (MIT License).
  • Are there hidden API costs? No.
  • Are compute costs included? No, users bear GPU/cloud costs.

FAQ

Q: Do I need prior experience with PyTorch? A: While basic Python knowledge is required, familiarity with PyTorch is beneficial. The notebooks provide explanations, but understanding tensor operations will help significantly.

Q: Can I use this to train a model as large as ChatGPT? A: No. This guide focuses on small-scale models for educational purposes. Training massive LLMs requires distributed computing infrastructure far beyond the scope of this repository.

Q: Is the code suitable for production use? A: The code is primarily educational. While functional, it lacks the robustness, optimization, and safety features required for direct deployment in production environments without significant modification.

Q: How long does it take to complete the tutorials? A: Depending on hardware speed and prior knowledge, completing all notebooks can take anywhere from a few days to several weeks.

Q: Does the project include pre-trained weights? A: The repository focuses on teaching the implementation. Users typically train their own models from scratch, though some appendices may reference weights for specific exercises.

Q: Is there a companion book? A: Yes, this repository is closely tied to Sebastian Raschka’s book "Build a Large Language Model (From Scratch)," which provides deeper theoretical context for the code.

Related tools and alternatives

View all alternatives
ChatGPT

Chatbots

ChatGPT

ChatGPT by OpenAI is a flagship AI assistant for drafting, reasoning, file analysis, and coding. Offering free and paid tiers with robust features, it serves individuals and enterprises seeking versatile, general-purpose AI workflows.

FeaturedFreeFreeProductivity
Claude

Chatbots

Claude

Claude by Anthropic is an advanced AI assistant specializing in long-document analysis, structured reasoning, and high-quality editorial writing. Ideal for researchers, writers, and teams needing precise, calm, and nuanced text synthesis.

FeaturedFreeFreeProductivity
Perplexity

Chatbots

Perplexity

Perplexity is an AI-powered search engine that combines real-time web browsing with conversational AI. It delivers cited, synthesized answers, making it ideal for researchers and knowledge workers seeking verified, source-backed information quickly.

FeaturedFreeFreeProductivity
Gemini

Chatbots

Gemini

Gemini is Google's flagship multimodal AI model, deeply integrated into Workspace and Search. It offers advanced reasoning, 1M token context, and seamless productivity tools for users within the Google ecosystem.

FeaturedFreeFreeProductivity
15 updates from Google I/O 2026: Powering the agentic web with new capabilities, tools, and features in Chrome  |  Blog  |  Chrome for Developers

Coding

15 updates from Google I/O 2026: Powering the agentic web with new capabilities, tools, and features in Chrome | Blog | Chrome for Developers

Chrome for Developers announces 15 updates from Google I/O 2026, focusing on the 'agentic web.' Key features include Gemini in Chrome, automated navigation, and enhanced AI capabilities for developers and users.

updatesfrom
About Lumo, the privacy-first AI assistant | Lumo by Proton

Chatbots

About Lumo, the privacy-first AI assistant | Lumo by Proton

Lumo by Proton is a privacy-first AI assistant offering zero-access encryption, open-source code, and GDPR compliance. It ensures conversations are never used for training or ads, available via web, iOS, and Android.

aboutlumo

Site Discovery

Explore more on ToolSeekAI

Keep moving through tools, use cases, models, news, and rankings to turn one visit into a complete AI discovery path.