嘗試在macbook上部署LLaMA-2的中文模型的詳細(xì)過(guò)程。
(1)環(huán)境準(zhǔn)備
MacBook?Pro(M2 Max/32G);
VMware Fusion Player 版本 13.5.1 (23298085);
Ubuntu 22.04.2 LTS;
給linux虛擬機(jī)分配8*core CPU 16G RAM。
我這里用的是16bit的量化模型,至少需要13G內(nèi)存,如果4bit的只需要3.8G內(nèi)存,當(dāng)然上述不包含系統(tǒng)本身需要的內(nèi)存。
(2)環(huán)境依賴
sudo apt update
sudo apt-get install gcc g++ python3 python3-pip
python3 -m pip install torch numpy sentencepiece
(3)拉取llama.cpp工具并進(jìn)行構(gòu)建
?在目錄/home/zhangzk下:
git clone https://github.com/ggerganov/llama.cpp.git
#安裝依賴,llama.cpp 項(xiàng)目下帶有 requirements.txt 文件
pip install -r requirements.txt
#構(gòu)建llama.cpp
cd llama.cpp/
make -j8
(4)下載LLAMA2中文模型
下載LLama2的中文模型:GitHub - ymcui/Chinese-LLaMA-Alpaca-2: 中文LLaMA-2 & Alpaca-2大模型二期項(xiàng)目 + 64K超長(zhǎng)上下文模型 (Chinese LLaMA-2 & Alpaca-2 LLMs with 64K long context models)
這里下載 Chinese-Alpace-2-7B的指令模型,模型文件12.9G。
百度網(wǎng)盤那叫一個(gè)慢啊,沒(méi)有會(huì)員能讓你等死,還是梯子和GOOGLE網(wǎng)盤配合才叫一個(gè)快啊,幾分鐘的事。
把模型文件(共9個(gè)文件)都下載到 /home/zhangzk/llama.cpp/models/chinese-alpaca-2-7b-hf目錄下。
(5)量化模型
在目錄llama.cpp下執(zhí)行:
#轉(zhuǎn)換模型
python3 convert.py ./models/chinese-alpaca-2-7b-hf/
#16位量化
./quantize ./models/chinese-alpaca-2-7b-hf/ggml-model-f16.gguf ./models/chinese_7b_f16.gguf f16
上述兩步執(zhí)行完會(huì)生成新文件llama.cpp/models/chinese_7b_f16.gguf
(6)啟動(dòng)模型
把Chinese-LLaMA-Alpaca-2/scripts/llama-cpp/chat.sh復(fù)制到llama.cpp目錄下。
chmod +x chat.sh
在目錄llama.cpp下執(zhí)行即可看到模型輸出了:
./chat.sh models/chinese_7b_f16.gguf '中國(guó)北京有哪些著名的景點(diǎn)?'
附1:QA例子
[INST] >
You are a helpful assistant. 你是一個(gè)樂(lè)于助人的助手。
>
中國(guó)北京有哪些著名的景點(diǎn)?
[/INST] 北京是中國(guó)的首都,擁有豐富的歷史文化遺產(chǎn)和自然景觀資源,以下是一些著名景點(diǎn):
- 故宮博物院:中國(guó)古代皇宮建筑群,是明清兩代皇帝的居所,保存了大量的文物和藝術(shù)品。
- 天壇公園:古代祭祀天地之場(chǎng)所,是中國(guó)現(xiàn)存規(guī)模最大、保存最完整的祭天文化遺產(chǎn)之一。
- 頤和園:中國(guó)傳統(tǒng)皇家園林,被譽(yù)為"皇家園林博物館",以其精美的建筑、湖泊和山水景觀而聞名。
- 北京長(zhǎng)城:中國(guó)古代防御工程的代表,是世界文化遺產(chǎn),也是世界上最長(zhǎng)的城墻。
- 北海公園:古代皇家園林之一,以碧波蕩漾、荷花盛開、古樹參天為特色,是中國(guó)最大的人工湖泊和島嶼園林。
- 圓明園:清代皇家園林,以其精美的建筑、精美的花園和珍貴文物而聞名于世,曾經(jīng)是世界上最大的皇家園林。
- 北京鳥巢:2008年北京奧運(yùn)會(huì)主體育場(chǎng),是一座現(xiàn)代化體育場(chǎng)館,也是北京市區(qū)的一個(gè)地標(biāo)性建筑。
- 天安門廣場(chǎng):中國(guó)最大的城市廣場(chǎng)之一,是中國(guó)政治和歷史的重要場(chǎng)所,也是游客必去的地方。
- 王府井大街:北京的商業(yè)中心,擁有各種購(gòu)物、餐飲和娛樂(lè)設(shè)施,是游客體驗(yàn)北京文化的好地方。
- 北京大學(xué):中國(guó)的著名高等學(xué)府,以其美麗的校園建筑和悠久的歷史而聞名于世。
附2: 查看quantize 提供各種精度的量化。
zhangzk@test-llm:~/llama.cpp$ ./quantize --help
usage: ./quantize [--help] [--allow-requantize] [--leave-output-tensor] [--pure] [--imatrix] [--include-weights] [--exclude-weights] model-f32.gguf [model-quant.gguf] type [nthreads]
--allow-requantize: Allows requantizing tensors that have already been quantized. Warning: This can severely reduce quality compared to quantizing from 16bit or 32bit
--leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing
--pure: Disable k-quant mixtures and quantize all tensors to the same type
--imatrix file_name: use data in file_name as importance matrix for quant optimizations
--include-weights tensor_name: use importance matrix for this/these tensor(s)
--exclude-weights tensor_name: use importance matrix for this/these tensor(s)
Note: --include-weights and --exclude-weights cannot be used together
Allowed quantization types:
2 or Q4_0 : 3.56G, +0.2166 ppl @ LLaMA-v1-7B
3 or Q4_1 : 3.90G, +0.1585 ppl @ LLaMA-v1-7B
8 or Q5_0 : 4.33G, +0.0683 ppl @ LLaMA-v1-7B
9 or Q5_1 : 4.70G, +0.0349 ppl @ LLaMA-v1-7B
19 or IQ2_XXS : 2.06 bpw quantization
20 or IQ2_XS : 2.31 bpw quantization
28 or IQ2_S : 2.5 bpw quantization
29 or IQ2_M : 2.7 bpw quantization
24 or IQ1_S : 1.56 bpw quantization
10 or Q2_K : 2.63G, +0.6717 ppl @ LLaMA-v1-7B
21 or Q2_K_S : 2.16G, +9.0634 ppl @ LLaMA-v1-7B
23 or IQ3_XXS : 3.06 bpw quantization
26 or IQ3_S : 3.44 bpw quantization
27 or IQ3_M : 3.66 bpw quantization mix
12 or Q3_K : alias for Q3_K_M
22 or IQ3_XS : 3.3 bpw quantization
11 or Q3_K_S : 2.75G, +0.5551 ppl @ LLaMA-v1-7B
12 or Q3_K_M : 3.07G, +0.2496 ppl @ LLaMA-v1-7B
13 or Q3_K_L : 3.35G, +0.1764 ppl @ LLaMA-v1-7B
25 or IQ4_NL : 4.50 bpw non-linear quantization
30 or IQ4_XS : 4.25 bpw non-linear quantization
15 or Q4_K : alias for Q4_K_M
14 or Q4_K_S : 3.59G, +0.0992 ppl @ LLaMA-v1-7B
15 or Q4_K_M : 3.80G, +0.0532 ppl @ LLaMA-v1-7B
17 or Q5_K : alias for Q5_K_M
16 or Q5_K_S : 4.33G, +0.0400 ppl @ LLaMA-v1-7B
17 or Q5_K_M : 4.45G, +0.0122 ppl @ LLaMA-v1-7B
18 or Q6_K : 5.15G, +0.0008 ppl @ LLaMA-v1-7B
7 or Q8_0 : 6.70G, +0.0004 ppl @ LLaMA-v1-7B
1 or F16 : 13.00G @ 7B
0 or F32 : 26.00G @ 7B文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-844818.html
COPY : only copy tensors, no quantizing文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-844818.html
到了這里,關(guān)于筆記本電腦上部署LLaMA-2中文模型的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!