Skip to content
B4K2Public

About

Research-oriented Small Language Model built from scratch in PyTorch featuring MLA, MoE, Attention Residuals, and the Muon optimizer.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

CodeMind

A research-oriented Small Language Model (SLM) built entirely from scratch in PyTorch to explore modern transformer architectures under real-world compute constraints.

Python PyTorch License


Overview

CodeMind is an experimental language model project focused on understanding and implementing modern LLM architectures from first principles rather than fine-tuning existing models.

Instead of modifying LLaMA or Mistral, this repository implements an entire transformer stack from scratch, integrating recent architectural ideas proposed by DeepSeek, Moonshot AI, and Muon into a unified training framework.

The project serves both as

  • a research platform for experimenting with new architectures
  • a production-quality implementation of a modern language model
  • a learning resource for understanding large-scale model training

Features

Multi-Head Latent Attention (MLA)

Implements DeepSeek's latent KV-cache compression mechanism for reducing inference memory while preserving attention quality.

Mixture of Experts (MoE)

  • 4 Routed Experts
  • Top-2 Routing
  • 1 Shared Expert
  • Gradient-compatible load balancing

Block Attention Residuals

Implementation inspired by Kimi (Moonshot AI), replacing additive residual connections with attention-based aggregation across transformer blocks.

Multi-Token Prediction

Predicts multiple future tokens during training to improve latent representations and enable speculative decoding research.

Hybrid Muon + AdamW Optimizer

Instead of using a single optimizer:

  • Muon handles large weight matrices
  • AdamW optimizes embeddings, norms, and other 1D parameters

Repository Structure

CodeMind/
│
├── config/
├── model/
│   ├── attention/
│   ├── moe/
│   ├── transformer/
│   └── codemind.py
│
├── tokenizer/
├── dataset/
├── training/
├── inference/
├── scripts/
│
├── checkpoints/
├── assets/
├── technical_report.pdf
└── README.md

Model Architecture

CodeMind combines multiple modern research ideas into one architecture.

Embedding
      │
RoPE
      │
Multi-Head Latent Attention
      │
Block Attention Residual
      │
Mixture of Experts
      │
Multi-Token Prediction Head
      │
Language Modeling Head

Training

The initial pretraining checkpoint was trained completely from scratch.

Item Value
Parameters ~1B
Active Parameters ~400M
Hardware NVIDIA A40 48GB
Dataset GitHub + OpenWebMath + FineWeb-Edu
Tokens 147 Million
Precision bfloat16
Training Time ~21 Hours

Results

The model successfully converged without instability despite combining several modern architectural components.

Highlights:

  • Stable MoE routing
  • Stable Muon optimization
  • Successful MLA implementation
  • ~1940 Tokens/sec
  • Training Loss: 10.5 → 3.1

Training curves are available below.


Technical Report

A complete write-up of the implementation and training methodology is available.

📄 Technical Report

👉 Read the Technical Report


Hugging Face Model

The pretrained checkpoint is available on Hugging Face.

🤗 https://huggingface.co/B4K2xx/CodeMind-1B-v0.1


Research

CodeMind is only the beginning.

Current research directions include

  • Scaling MoE at small model sizes
  • Muon vs AdamW optimization
  • Long-context training
  • Efficient KV-cache compression
  • Alternative attention mechanisms
  • Instruction tuning
  • Reinforcement Learning
  • Distributed training

Acknowledgements

This project draws inspiration from

  • DeepSeek
  • Moonshot AI (Kimi)
  • Muon Optimizer
  • PyTorch
  • Hugging Face

About

Research-oriented Small Language Model built from scratch in PyTorch featuring MLA, MoE, Attention Residuals, and the Muon optimizer.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages