yalm

andrewkchan长期未更新

用 C++/CUDA 从零实现的 LLM 推理引擎,除 I/O 外不依赖任何第三方库,适合学习推理底层或做轻量部署。

Yet Another Language Model: LLM inference in C++/CUDA, no libraries except for I/O

暂无版本607star66fork同步于 2026-11-07 07:28 UTC

怎么上手

由 README 提炼,执行前请确认
pip install -r requirements.txt
python convert.py --dtype fp16 mistral-7b-instruct-fp16.yalm ../Mistral-7B-Instruct-v0.2/
make && ./build/main mistral-7b-instruct-fp16.yalm -i "What is a large language model?" -m c
硬件要求按 README 说明

需要 NVIDIA GPU,且 GPU 后端仅支持单卡、整个模型必须能放进显存;README 未给出具体显存数值。CPU 后端可运行,Mixtral-v0.1 仅测试过 CPU。

纯 CPU 可运行需要 CUDA
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

用 C++/CUDA 从零实现的 LLM 推理引擎,适合学习性能优化而非生产部署

用 C++/CUDA 从零实现的 LLM 推理引擎,除 I/O 外不依赖任何第三方库,适合学习推理底层或做轻量部署。

核心能力

  • 纯 C++/CUDA 实现,除权重读写外不依赖第三方库
  • 提供 CPU 与 CUDA 双后端,可切换设备运行
  • 支持 completion、perplexity、passkey 三种运行模式
  • 附带 attention、matmul、FFN 的 CPU/GPU 单元测试与内核基准
  • 提供内存带宽与单内核基准,便于用 ncu 做性能剖析

容易踩的坑

需先自行下载 HuggingFace 格式的 safetensor 权重并用 convert.py 转成 .yalm 文件才能运行;编译需要 C++20 编译器和含 nvcc 的 CUDA toolkit;仅支持 completion 等模式,未实现聊天界面,且只测试过 Mistral-v0.2、Mixtral-v0.1、Llama-3.2。

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

最近发版

0 个版本

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

用户点评

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

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

    README 原文

    未翻译

    yalm (Yet Another Language Model) is an LLM inference implementation in C++/CUDA, using no libraries except to load and save frozen LLM weights.

    • This project is intended as an educational exercise in performance engineering and LLM inference implementation.
    • The codebase therefore emphasizes documentation, whether external or in comments, scientific understanding of optimizations, and readability where possible.
    • It is not meant to be run in production. See limitations section at bottom.
    • See my blog post Fast LLM Inference From Scratch for more.

    Latest benchmarks with Mistral-7B-Instruct-v0.2 in FP16 with 4k context, on RTX 4090 + EPYC 7702P:

    EngineAvg. throughput (~120 tokens) tok/sAvg. throughput (~4800 tokens) tok/s
    huggingface transformers, GPU25.925.7
    llama.cpp, GPU61.058.8
    calm, GPU66.065.7
    yalm, GPU63.858.7

    Instructions

    yalm requires a computer with a C++20-compatible compiler and the CUDA toolkit (including nvcc) to be installed. You'll also need a directory containing LLM safetensor weights and configuration files in huggingface format, which you'll need to convert into a .yalm file. Follow the below to download Mistral-7B-v0.2, build yalm, and run it:

    # install git LFS
    curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
    sudo apt-get -y install git-lfs
    # download Mistral
    git clone [email protected]:mistralai/Mistral-7B-Instruct-v0.2
    # clone this repository
    git clone [email protected]:andrewkchan/yalm.git
    
    cd yalm
    pip install -r requirements.txt
    python convert.py --dtype fp16 mistral-7b-instruct-fp16.yalm ../Mistral-7B-Instruct-v0.2/
    make && ./build/main mistral-7b-instruct-fp16.yalm -i "What is a large language model?" -m c
    

    Usage

    See the CLI help documentation below for ./build/main:

    Usage:   main <checkpoint> [options]
    Example: main model.yalm -i "Q: What is the meaning of life?" -m c
    Options:
      -h Display this help message
      -d [cpu,cuda] which device to use (default - cuda)
      -m [completion,passkey,perplexity] which mode to run in (default - completion)
      -T <int> sliding window context length (0 - max)
    
    Perplexity mode options:
      Choose one:
        -i <string> input prompt
        -f <filepath> input file with prompt
    Completion mode options:
      -n <int>    number of steps to run for in completion mode, default 256. 0 = max_seq_len, -1 = infinite
      -t <float> temperature (default - 1.0)
      Choose one:
        -i <string> input prompt
        -f <filepath> input file with prompt
    Passkey mode options:
      -n <int>    number of junk lines to insert (default - 250)
      -l <int>    passkey position (-1 - random)
    

    Tests and benchmarks

    yalm comes with a basic test suite that checks implementations of attention, matrix multiplications, feedforward nets in the CPU and GPU backends. Build and run it like so:

    make test
    ./build/test
    

    The test binary also includes benchmarks for individual kernels (useful for profiling with ncu) and broader system tools such as 2 benchmarks to determine main memory bandwidth:

    # Memory benchmarks
    ./build/test -b
    ./build/test -b2
    
    # Kernel benchmarks
    ./build/test -k [matmul,mha,ffn]
    

    Limitations

    • Only completions may be performed (in addition to some testing modes like computing perplexity on a prompt or performing a passkey test). Chat interface has not been implemented.
    • An NVIDIA GPU is required.
    • The GPU backend only works with a single GPU and the entire model must fit into VRAM.
    • As of Dec 31, 2024 only the following models have been tested:
      • Mistral-v0.2
      • Mixtral-v0.1 (CPU only)
      • Llama-3.2

    Acknowledgements

    • calm - Much of my implementation is inspired by Arseny Kapoulkine’s inference engine. In a way, this project was kicked off by “understand calm and what makes it so fast.” I’ve tried to keep my code more readable for myself though, and as much as possible scientifically understanding optimizations, which means foregoing some advanced techniques used in calm like dynamic parallelism.
    • llama2.c - Parts of the CPU backend come from Andrej Karpathy’s excellent C implementation of Llama inference.