This repository contains a from-scratch, educational PyTorch implementation of Llama 3.2 text models with minimal code dependencies. The implementation is optimized for readability and intended for learning and research purposes.
The model weights included here are PyTorch state dicts converted from the official weights provided by Meta. For original weights, usage terms, and license information, please refer to the original model repositories linked below:
Please refer to these repositories above for more information about the models and license information.
Usage
The section below explain how the model weights can be used via the from-scratch implementation provided in the model.py and tokenizer.py files.
Alternatively, you can also modify and run the generate_example.py file via:
python generate_example.py
which uses the Llama 3.2 1B Instruct model by default and prints:
Time: 4.12 sec
Max memory allocated: 2.91 GB
Output text:
Llamas are herbivores, which means they primarily eat plants. Their diet consists mainly of:
1. Grasses: Llamas love to graze on various types of grasses, including tall grasses and grassy meadows.
2. Hay: Llamas also eat hay, which is a dry, compressed form of grass or other plants.
3. Alfalfa: Alfalfa is a legume that is commonly used as a hay substitute in llama feed.
4. Other plants: Llamas will also eat other plants, such as clover, dandelions, and wild grasses.
It's worth noting that the specific diet of llamas can vary depending on factors such as the breed,
1) Setup
The only dependencies are torch, tiktoken, and blobfile, which can be installed as follows:
pip install torch tiktoken blobfile
Optionally, you can install the llms-from-scratch PyPI package if you prefer not to have the model.py and tokenizer.py files in your local directory:
When using the Llama 3.2 1B Instruct model, the output should look similar to the one shown below:
Time: 4.12 sec
Max memory allocated: 2.91 GB
Output text:
Llamas are herbivores, which means they primarily eat plants. Their diet consists mainly of:
1. Grasses: Llamas love to graze on various types of grasses, including tall grasses and grassy meadows.
2. Hay: Llamas also eat hay, which is a dry, compressed form of grass or other plants.
3. Alfalfa: Alfalfa is a legume that is commonly used as a hay substitute in llama feed.
4. Other plants: Llamas will also eat other plants, such as clover, dandelions, and wild grasses.
It's worth noting that the specific diet of llamas can vary depending on factors such as the breed,
Pro tip
Replace
model.to(device)
with
python
1model = torch.compile(model)2model.to(device)
for a 4x speed-up (after the first generate call).