GitHub – karpathy/llama2.c: Inference Llama 2 in one file of pure C

1周前发布 6 00

Inference Llama 2 in one file of pure C. Contribute to karpathy/llama2.c development by creating an account on GitHub.

所在地:
中国
语言:
zh
收录时间:
2025-10-06
其他站点:
GitHub – karpathy/llama2.c: Inference Llama 2 in one file of pure CGitHub – karpathy/llama2.c: Inference Llama 2 in one file of pure C

llama2.c是GitHub上由karpathy开发的开源项目,专注于用纯C实现Llama 2大模型的推理,并配套PyTorch训练代码,核心目标是“极简、易 hack、教育性”,适合小参数模型(100M1B)的训练与边缘部署。截至目前,仓库获18.8k星标、2.4k分叉,采用MIT许可证。

一、核心定位
项目是Llama 2架构的“全栈解决方案”:用PyTorch训练模型,用纯C(约700行代码)实现推理,强调无依赖、可读性,避免复杂框架。相比llama.cpp,它更轻量化,适合学习Llama 2的底层逻辑,或快速适配小场景需求(如MCU、手机、浏览器)。

二、核心功能模块
1. 纯C推理:极简高效的模型运行
基础推理:通过`run.c`文件实现Llama 2的fp32精度推理,支持两类模型:
自定义小模型:比如在TinyStories数据集上训练的15M、42M、110M参数模型(预训练模型托管在Hugging Face的`tinyllamas`仓库,如`stories15M.bin`),可快速生成连贯故事(如“Lily的茶话会”)。
Meta Llama 2模型:需用`export.py`将Meta的Llama 2 checkpoint(如7B模型)转换为llama2.c格式(约26GB),支持Base和Chat模型(用`m chat`启动对话),还部分支持Code Llama(需调整tokenizer)。
量化优化:通过`runq.c`实现int8量化(llama.cpp的Q8_0格式),将模型体积缩小4倍(如7B模型从26GB降到6.7GB),推理速度提升3倍(float32约4.6 tok/s → int8约14 tok/s),且精度损失极小。

2. PyTorch训练:从数据到模型的全流程
数据集支持:默认适配[TinyStories](https://huggingface.co/datasets/roneneldan/TinyStories)(儿童故事数据集),通过`tinystories.py`下载、预token化数据。
模型训练:用`train.py`训练Llama 2架构模型,可调整核心参数(如`dim`、`n_layers`、`n_heads`、`batch_size`、`learning_rate`),支持多GPU分布式训练(DDP)。例如训练110M参数模型:`dim=768`、`n_layers=12`、`n_heads=12`,总 batch size 131k tokens,约24小时完成。
自定义tokenizer:支持训练小 vocab 的tokenizer(如4096个词),通过`tinystories.py train_vocab`生成,可减小模型参数(token嵌入表更小)、提升推理速度(序列更短)。

3. 模型生态:丰富的预训练与适配
预训练小模型:提供多尺度预训练模型(260K、15M、42M、110M参数),覆盖不同复杂度需求,比如110M参数模型相当于GPT1大小,在TinyStories上有较好表现。
第三方模型支持:兼容Meta Llama 2(7B及以下)、Code Llama(部分支持),以及Hugging Face上所有Llama 2架构的模型(通过`export.py`转换)。

三、性能与适配
编译优化:Makefile提供多类编译选项:
`make run`:默认`O3`优化,平衡速度与兼容性。
`make runfast`:`Ofast`更激进优化,提升速度(可能牺牲部分标准合规性)。
`make runomp`:用Clang+OpenMP编译,支持多线程(如`OMP_NUM_THREADS=4`),大幅提升大模型推理速度。
跨平台支持:
Windows:用`build_msvc.bat`(Visual Studio命令行)编译。
Centos/Mac:用`rungnu`/`runomp`目标,适配GCC/Clang。

四、测试与社区
测试保障:用`pytest`测试Python(`sample.py`)与C(`run.c`)推理的一致性,`test.c`文件提供C语言单元测试,确保代码正确性。
社区生态:
Discord频道:llama2c(链接:https://discord.gg/3zy8kqD9Cp),用于讨论问题。
多语言forks:已有Rust、Go、Java、JavaScript、Zig等20+语言的移植版,覆盖Android、WebAssembly、Flutter等场景。

五、快速上手示例
1. 克隆仓库:`git clone https://github.com/karpathy/llama2.c.git`
2. 下载预训练模型:`wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin`
3. 编译推理代码:`make run`
4. 生成故事:`./run stories15M.bin`(将输出连贯的儿童故事)

该项目适合想学习Llama 2底层逻辑、快速开发小模型应用,或在边缘设备部署轻量化大模型的开发者,是“从训练到推理”的极简实践案例。

相关导航

GitHub – Rudrabha/Wav2Lip: This repository contains the codes of “A Lip Sync Expert Is All You Need for Speech to Lip Generation In the Wild”, published at ACM Multimedia 2020. For HD commercial model, please try out Sync Labs

GitHub – Rudrabha/Wav2Lip: This repository contains the codes of “A Lip Sync Expert Is All You Need for Speech to Lip Generation In the Wild”, published at ACM Multimedia 2020. For HD commercial model, please try out Sync Labs

This repository contains the codes of "A Lip Sync Expert Is All You Need for Speech to Lip Generation In the Wild", published at ACM Multimedia 2020. For HD commercial model, please try out Sync Labs - GitHub - Rudrabha/Wav2Lip: This repository contains the codes of "A Lip Sync Expert Is All You Need for Speech to Lip Generation In the Wild", published at ACM Multimedia 2020. For HD commercial model, please try out Sync Labs

暂无评论

您必须登录才能参与评论!
立即登录
none
暂无评论...