Downloads · 30 days
0
Harish241412/llama-3.2-1b-from-scratch
llama-3.2-1b-from-scratch is a machine learning model from Harish241412. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for pytorch.
A PyTorch implementation of Llama 3.2 1B built from scratch for educational and research purposes.
Downloads · 30 days
0
Access
Public
Updated Sep 12, 2026
Repo size
2.7 GB
Likes
0
Public
Click a slice to open those files.
.safetensors2.7 GB · 100%
From the Hugging Face model README
A PyTorch implementation of Llama 3.2 1B built from scratch for educational and research purposes.
The project reimplements the model architecture and inference pipeline from the ground up and loads the original Llama 3.2 1B weights into the custom implementation.
Educational implementation only.
| Property | Value |
|---|---|
| Base model | Llama 3.2 1B |
| Parameters | ~1.24B |
| Architecture | Decoder-only Transformer |
| Hidden size | 2048 |
| Layers | 16 |
| Attention heads | 32 |
| KV heads | 8 |
| Head dimension | 64 |
| Intermediate size | 8192 |
| Vocabulary size | 128,256 |
| Maximum context | 131,072 tokens |
| RoPE base | 500,000 |
| Weight dtype | bfloat16 |
The implementation includes a dynamic KV cache for autoregressive decoding.
Per-token decode latency was benchmarked on an RTX 3050:
| Context | No KV | KV | Speedup |
|---|---|---|---|
| 16 | 28.84 ms | 20.90 ms | 1.38× |
| 32 | 24.29 ms | 26.97 ms | 0.90× |
| 64 | 27.47 ms | 21.52 ms | 1.28× |
| 128 | 32.22 ms | 21.50 ms | 1.50× |
| 256 | 60.62 ms | 21.39 ms | 2.83× |
| 512 | 119.99 ms | 26.41 ms | 4.54× |
| 1024 | 268.96 ms | 19.44 ms | 13.83× |
The benefit of KV caching becomes increasingly significant as context length grows.
The scratch implementation was verified against the Hugging Face implementation using the original Llama 3.2 1B weights.
FP32 verification across five prompts produced:
2e-5Example:
Prompt:
The capital of France is
Next token:
Paris
Example greedy generation:
The capital of France is Paris, and the capital of the United States is
KV-cache and non-KV generation were also verified for consistency.
The model checkpoint is designed to be used with the custom PyTorch implementation available in the GitHub repository.
Clone the repository and install the required dependencies using uv:
git clone https://github.com/N-Harish/llama3.2-1b-scratch.git
cd llama3.2-1b-scratch
uv sync
The project uses uv for environment and dependency management.
Note: This is a from-scratch implementation of Llama 3.2 1B and is not intended to be loaded directly with the standard Hugging Face Transformers API. Use the implementation provided in the GitHub repository to run the model.
The project includes a custom Llama 3 tokenizer implementation using the original tokenizer vocabulary and configuration.
from llama3_2_1b_scratch.tokenizer import Llama3Tokenizer
tokenizer = Llama3Tokenizer(
"tokenizer.model",
"tokenizer_config.json",
)
ids = tokenizer.encode("The capital of France is")
text = tokenizer.decode(ids)
print(ids)
print(text)
The model weights and tokenizer files can be downloaded from this repository using the provided utility.
import torch
from llama3_2_1b_scratch.utils import download_model
from llama3_2_1b_scratch.model import LlamaForCausalLM
from llama3_2_1b_scratch.tokenizer import Llama3Tokenizer
local_dir = download_model(
output_dir="llama3-2-1b",
repo_id="Harish241412/llama-3.2-1b-from-scratch",
)
model = LlamaForCausalLM.from_pretrained(
local_dir / "llama3_2_1b_scratch_bf16.safetensors",
device="cuda" if torch.cuda.is_available() else "cpu",
dtype=torch.bfloat16,
)
tokenizer = Llama3Tokenizer(
local_dir / "tokenizer.model",
local_dir / "tokenizer_config.json",
)
prompt = "The capital of France is"
input_ids = torch.tensor(
[tokenizer.encode(prompt)],
dtype=torch.long,
device=next(model.parameters()).device,
)
generated_ids = model.generate(
input_ids,
max_new_tokens=50,
do_sample=False,
)
print(tokenizer.decode(generated_ids[0].tolist()))
The capital of France is Paris, and the capital of the United States is
The download utility saves the following files to the specified directory:
llama3_2_1b_scratch_bf16.safetensorstokenizer.modeltokenizer_config.jsonfrom_pretrained() loads the checkpoint from the local filesystem and does not perform model downloads itself.
The implementation supports greedy and stochastic generation.
generated_ids = model.generate(
input_ids,
max_new_tokens=50,
do_sample=True,
temperature=0.7,
top_p=0.9,
top_k=50,
)
For greedy decoding:
generated_ids = model.generate(
input_ids,
max_new_tokens=50,
do_sample=False,
)
The repository provides a BF16 checkpoint containing the Llama 3.2 1B weights mapped to the scratch implementation.
The checkpoint contains approximately 1.24B parameters.
This project is intended as a hands-on implementation for understanding:
This project is an educational implementation only and is intended for learning and experimentation with LLM architecture and inference.
It should not be considered an official implementation of Llama 3.2.