llama2.mojo

tairovMIT活跃维护

用单个 Mojo 文件实现 Llama 2 推理,借助 SIMD 和并行化把大模型跑起来,适合研究推理底层实现和性能优化。

Inference Llama 2 in one file of pure 🔥

暂无版本2,129star139fork同步于 2026-11-07 07:31 UTC

怎么上手

由 README 提炼,执行前请确认
uv venv --python 3.13 ~/.modular-venv
uv pip install --python ~/.modular-venv/bin/python mojo "max[all]"
source ~/.modular-venv/bin/activate
wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin
mojo llama2.mojo stories15M.bin -s 100 -n 256 -t 0.5 -i "Once upon a time"
硬件要求按 README 说明

README 未说明显存要求;基准测试均在 CPU 上完成,包括 Apple M1 Max、Intel Xeon Skylake(AVX-512)和 i7-8700,未提及 GPU 支持

支持 Apple Silicon
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

用单文件纯 Mojo 实现 Llama 2 推理,在 CPU 上跑出比 llama2.c 和 llama.cpp 更快的速度

用单个 Mojo 文件实现 Llama 2 推理,借助 SIMD 和并行化把大模型跑起来,适合研究推理底层实现和性能优化。

核心能力

  • 单文件 llama2.mojo 实现完整 Llama 2 推理
  • 用 Mojo 的 SIMD 与向量化原语加速矩阵运算
  • 支持多线程并行推理,用 -j 指定 worker 数
  • 支持 stories 260K~110M 与 TinyLlama-1.1B
  • 提供 -s/-n/-t/-i/-z/-j/-pc 命令行参数

容易踩的坑

需要 Mojo 1.1.0 与 MAX 26.6.0,旧的 modular 包已废弃;链接 Mojo 可执行文件需要主机安装 C 编译器(如 gcc);模型权重和 tokenizer.bin 需自行从 Hugging Face 下载,TinyLlama 还要额外用 -z 指定 tokenizer 路径

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    llama2.🔥

    Have you ever wanted to inference a baby Llama 2 model in pure Mojo? No? Well, now you can!

    supported version: Mojo 1.1.0 (MAX 26.6.0)

    With the release of Mojo, I was inspired to take my Python port of llama2.py and transition it to Mojo. The result? A version that leverages Mojo's SIMD & vectorization primitives, boosting the Python performance by nearly 250x. Impressively, after few native improvements the Mojo version outperforms the original llama2.c by 30% in multi-threaded inference. As well as it outperforms llama.cpp on baby-llama inference on CPU by 20%. This showcases the potential of hardware-level optimizations through Mojo's advanced features.

    supported models

    At the moment, the following models were successfully executed via llama2.mojo:

    Models
    stories 260K, 15M, 42M, 110M
    Tinyllama-1.1B-Chat-v0.2

    extensive benchmark on Apple M1 Max

    mojo vs 6 programming languages

    benchmark (updated)

    Last benchmark run: 2026-09-12, Mojo 1.0.0, persistent worker pool build (PR #102).

    Mac M1 Max (8 workers)

    Modelllama2.c (OMP/parallelized)llama2.mojo (parallelized)llama.cpp (CPU, 6 threads)llama2.py
    stories15M.bin730 tok/s1283 tok/s890 tok/s38 tok/s (pypi)
    stories42M.bin270 tok/s506 tok/s420 tok/s-
    stories110M.bin102 tok/s218 tok/s187 tok/s-
    TinyLlama-1.1B-23 tok/s--

    Ubuntu 26.04, 4 vCPU Intel Xeon (Skylake, AVX-512) VM, Mojo 1.0.0

    Modelllama2.c (OMP, 4 threads)llama2.mojo (-j 4)llama2.mojo (-j 1)llama2.c (1 thread)
    stories15M.bin292 tok/s327 tok/s154 tok/s105 tok/s
    stories42M.bin117 tok/s150 tok/s54 tok/s40 tok/s
    stories110M.bin53 tok/s66 tok/s22 tok/s16 tok/s
    TinyLlama-1.1B5.2 tok/s6.8 tok/s-1.7 tok/s

    Ubuntu 20.04, Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz, 6 cores, 12 threads

    Modelllama2.c (OMP/parallelized)llama2.mojo (parallelized)llama2.mojo (naive matmul)llama2.py
    stories15M.bin435 tok/s440 tok/s67.26 tok/s1.3 tok/s
    stories110M.bin64 tok/s63 tok/s9.20 tok/s-
    TinyLlama-1.1B7.25 tok/s7.25 tok/s--

    prerequisites

    Make sure you have installed and configured mojo on your environment.

    Install the Mojo 1.1 compiler and MAX (which provides parallelize) with uv:

    uv venv --python 3.13 ~/.modular-venv
    uv pip install --python ~/.modular-venv/bin/python mojo "max[all]"
    source ~/.modular-venv/bin/activate
    mojo --version   # Mojo 1.1.0
    

    The old modular package was retired in Modular 26.6.

    Linking a Mojo executable needs a C compiler on the host (apt-get install gcc on Debian/Ubuntu).

    feel the 🔥 magic

    First, navigate to the folder when you keep your projects and clone this repository to this folder:

    git clone https://github.com/tairov/llama2.mojo.git
    

    Then, open the repository folder:

    cd llama2.mojo
    

    Now, let's download the model

    wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin
    

    Then, just run the Mojo

    mojo llama2.mojo stories15M.bin -s 100 -n 256 -t 0.5 -i "Once upon a time"
    

    Command-line options:

    • -s <int> - random seed (default: current time in milliseconds)
    • -n <int> - number of steps to run for (default: 256, 0 = max_seq_len)
    • -t <float> - temperature in [0,1.0] (default: 0.9)
    • -i <string> - input prompt
    • -z <string> - tokenizer path (default: tokenizer.bin)
    • -j <int> - number of parallel workers (default: number of performance cores; values are clamped to that count).
    • -pc <int> - print config (0 or 1)

    example output

    num parallel workers: 4  SIMD width: 16
    Total bytes read: 60816028 Estimated checkpoint size:  57 MB
    n layers: 6 | vocab size: 32000
    Once upon a time, there was a little girl named Lily. She loved to play with her toys and her friends. One day, Lily's mom asked her to help make dinner. Lily was happy to help and got out the plates and forks.
    While they were cooking, Lily's mom asked her to help clean the kitchen. Lily was happy to help and started to pick up the plates. Suddenly, she saw a shiny knife on the counter. She picked it up and showed it to her mom.
    "Be careful with that knife, Lily. It's sharp and can hurt you," her mom warned.
    Lily was careful and put the knife away. She finished cooking and felt proud of herself. Her mom hugged her and said, "You are such a good helper, Lily. Thank you for helping me."
    achieved tok/s:  148.95267649340573
    

    citing llama2.🔥

    If you use or discuss llama2.mojo in your academic research, please cite the project to help spread awareness:

    @misc{llama2.mojo,
      author = {Aydyn Tairov}, 
      title = {Inference Llama2 in one file of pure Mojo},
      year = {2023},
      month = {09},
      howpublished = {\url{https://github.com/tairov/llama2.mojo}},
      note = {Llama2 Mojo, MIT License}
    }
    

    We kindly request that you include a link to the GitHub repository in published papers. This will allow interested readers to easily find the latest updates and extensions to the project.

    llama2.mojo aims to encourage academic research on efficient implementations of transformer architectures, the llama model, and applications of the mojo programming language. Citing the project helps growth of the knowledge community around these topics. We appreciate your support through referencing llama2.mojo!

    in research & industry

    Our project has been used in academic. If you’ve published a paper or article that uses this project, please send a PR, so we can feature it here

    play with Tinyllama-1.1B-Chat-v0.2

    The TinyLlama is a 1.1B Llama model trained on 3 trillion tokens. This compactness allows it to cater to a multitude of applications demanding a restricted computation and memory footprint. This is also the reason why we select it as the first model to support.

    First, navigate to the folder when you keep your projects and clone this repository to this folder:

    git clone https://github.com/tairov/llama2.mojo.git
    

    Then, open the repository folder:

    cd llama2.mojo
    

    Now, let's download the model and the tokenizer

    wget https://huggingface.co/kirp/TinyLlama-1.1B-Chat-v0.2-bin/resolve/main/tok_tl-chat.bin
    wget https://huggingface.co/kirp/TinyLlama-1.1B-Chat-v0.2-bin/resolve/main/tl-chat.bin
    

    Then, just run the Mojo

    mojo llama2.mojo tl-chat.bin \
        -z tok_tl-chat.bin \
        -n 256 -t 0 -s 100 -i "<|im_start|>user\nGive me a python function to generate Fibonacci sequence<|im_end|>\n<|im_start|>assistant\n"
    

    example output

    num hardware threads:  6
    SIMD vector width:  16
    checkpoint size:  4400767004 [ 4196 MB ]
    n layers:  22
    vocab size:  32003
    <|im_start|>user
    Give me a python function to generate Fibonacci sequence<|im_end|>
    <|im_start|>assistant
    Sure, here's a Python function that generates the Fibonacci sequence:
    
    def fibonacci(n):
        if n <= 0:
            return 0
        elif n == 1:
            return 1
        else:
            return fibonacci(n-1) + fibonacci(n-2)
    
    This function takes an integer n as a parameter and returns the next Fibonacci number. It uses a recursive approach to calculate the Fibonacci numbers, starting from 0 and working up. The function returns the value it found at the current level of the recursion, which can be either 0 or a Fibonacci number.
    

    license

    MIT