利用網(wǎng)站空間做代理seo推廣一年要多少錢
文章目錄
- 簡介
- Github
- 文檔
- 克隆源碼
- 英文模型
- 編譯運(yùn)行
- 中文模型(280M)
- main函數(shù)
簡介
llama2.c是一個極簡的Llama 2 LLM全棧工具,使用一個簡單的 700 行 C 文件 ( run.c ) 對其進(jìn)行推理。llama2.c涉及LLM微調(diào)、模型構(gòu)建、推理端末部署(量化、硬件加速)等眾多方面,是學(xué)習(xí)研究Open LLM的很好切入點(diǎn)。
Github
- https://github.com/karpathy/llama2.c
文檔
- https://llama.meta.com/
克隆源碼
git clone https://github.com/karpathy/llama2.c.git
英文模型
- https://huggingface.co/datasets/roneneldan/TinyStories
# 15M參數(shù)模型
wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin
# 42M參數(shù)模型
wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories42M.bin
# 110M參數(shù)模型
wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories110M.bin
編譯運(yùn)行
make run
# 15M參數(shù)模型
./run stories15M.bin
# 42M參數(shù)模型,運(yùn)行并輸入提示詞
./run stories42M.bin -i "One day, Lily met a Shoggoth"
中文模型(280M)
- https://huggingface.co/flyingfishinwater/chinese-baby-llama2
# 下載模型
git clone https://huggingface.co/flyingfishinwater/chinese-baby-llama2
- 安裝 python 相關(guān)依賴
pip3 install numpy
pip3 install torch torchvision torchaudio
pip3 install transformers
- 將模型hf格式轉(zhuǎn)換為bin格式
# 將hf模型文件轉(zhuǎn)換成.bin文件
python export.py ./chinese-baby-llama2.bin --hf ./chinese-baby-llama2
- 修改 llama2.c/run.c
// 將 main() 中的 tokenizer.bin 改為 chinese-baby-llama2 目錄下的tokenizer.bin
char *tokenizer_path = "chinese-baby-llama2/tokenizer.bin";
- 編譯 c
make run
- 運(yùn)行并輸入提示詞
./run chinese-baby-llama2.bin -i "今天是武林大會,我是武林盟主"
main函數(shù)
- 默認(rèn)參數(shù)設(shè)置: 定義了一些默認(rèn)參數(shù)值,例如模型路徑、分詞器路徑、溫度、top-p 值、步數(shù)等。
- 命令行參數(shù)解析: 通過檢查命令行參數(shù),更新默認(rèn)參數(shù)值。命令行參數(shù)的格式為 flag value,例如 -t 0.5 表示設(shè)置溫度為 0.5。
- 參數(shù)驗(yàn)證和覆蓋: 對解析后的參數(shù)進(jìn)行驗(yàn)證和覆蓋。例如,確保隨機(jī)數(shù)種子大于 0、溫度在合理范圍內(nèi)、步數(shù)為非負(fù)數(shù)等。
- 構(gòu)建 Transformer 模型: 使用給定的模型文件構(gòu)建 Transformer 模型,并根據(jù)需要調(diào)整步數(shù)。
- 構(gòu)建 Tokenizer: 使用給定的分詞器文件構(gòu)建 Tokenizer。
- 構(gòu)建 Sampler: 構(gòu)建 Sampler,并設(shè)置相應(yīng)的參數(shù),如詞匯表大小、溫度、top-p 值等。
- 執(zhí)行功能: 根據(jù)模式選擇執(zhí)行生成或者聊天功能。如果模式是 generate,則執(zhí)行生成功能;如果是 chat,則執(zhí)行聊天功能。
- 內(nèi)存和文件句柄清理: 釋放動態(tài)分配的內(nèi)存和關(guān)閉文件句柄,確保程序執(zhí)行結(jié)束時資源被正確釋放。