Downloads · 30 days
0
Mayank022/Gemma_3_270M_SLM_from_scratch
Gemma_3_270M_SLM_from_scratch is a machine learning model from Mayank022. Use it for the machine learning task on the model card, and read the license before you ship it in a product. The card lists the license as mit.
A complete PyTorch implementation of Google's Gemma 3 270M small language model, trained from scratch on the TinyStories dataset.
Downloads · 30 days
0
Access
Public
Updated Aug 26, 2025
Repo size
330 MB
Likes
0
Public
Click a slice to open those files.
.pt330 MB · 100%
From the Hugging Face model README
A complete PyTorch implementation of Google's Gemma 3 270M small language model, trained from scratch on the TinyStories dataset.

This repository contains implementation of the Gemma 3 270M architecture, featuring modern transformer optimizations including sliding window attention, rotary position embedding (RoPE), and root mean square normalization (RMSNorm). The model achieves efficient performance while maintaining the core capabilities of larger language models.
TRAINING_CONFIG = {
"max_iterations": 60000,
"batch_size": 32,
"block_size": 128,
"learning_rate": 1e-4,
"warmup_steps": 1000,
"min_lr": 5e-4,
"weight_decay": 0.1,
"gradient_accumulation_steps": 32,
"gradient_clip_norm": 0.5,
"eval_interval": 500,
"dtype": "bfloat16"
}
GEMMA3_CONFIG_270M = {
"vocab_size": 50257,
"context_length": 32768,
"emb_dim": 640,
"n_heads": 4,
"n_layers": 18,
"hidden_dim": 2048,
"head_dim": 256,
"qk_norm": True,
"n_kv_groups": 1,
"rope_local_base": 10000.0,
"rope_base": 1000000.0,
"sliding_window": 512,
"layer_types": [
# 15 sliding attention layers + 3 full attention layers
"sliding_attention", "sliding_attention", "sliding_attention",
"sliding_attention", "sliding_attention", "full_attention",
"sliding_attention", "sliding_attention", "sliding_attention",
"sliding_attention", "sliding_attention", "full_attention",
"sliding_attention", "sliding_attention", "sliding_attention",
"sliding_attention", "sliding_attention", "full_attention"
],
"dtype": torch.bfloat16,
"query_pre_attn_scalar": 256
}
pip install torch datasets tiktoken numpy tqdm matplotlib
# Load and tokenize dataset
from datasets import load_dataset
ds = load_dataset("roneneldan/TinyStories")
# Initialize model
model = Gemma3Model(GEMMA3_CONFIG_270M)
# Train for 60,000 iterations
# (See full training loop in the notebook)
# Load trained model
model = Gemma3Model(GEMMA3_CONFIG_270M)
model.load_state_dict(torch.load("best_model_params.pt"))
# Generate text
sentence = "Once upon a time there was a pumpkin."
context = torch.tensor(enc.encode_ordinary(sentence)).unsqueeze(0)
output = model.generate(context, max_new_tokens=200)
generated_text = enc.decode(output.squeeze().tolist())
The model was successfully trained for 60,000 iterations with:
├── gemma_3_270_m_slm_from_scratch.py # Main implementation
├── train.bin # Processed training data
├── validation.bin # Processed validation data
├── best_model_params.pt # Best model checkpoint
└── README.md # This file