gemma.cpp 是 Gemma 模型的輕量純 C++ 推論執行階段實作。
如要進一步瞭解 Gemma,請參閱模型資訊卡。Kaggle 提供模型權重,包括 gemma.cpp 特定構件。
這項專案的適用對象
現代化的 LLM 推論引擎是複雜的系統,通常具備自訂功能,可延伸至傳統的類神經網路執行階段之外。隨著共同設計高層級演算法和低階運算,將可帶來研究和創新的機會。不過,部署導向的 C++ 推論執行階段並非專為實驗而設計,與以 Python 為中心的機器學習研究架構之間存在著缺口,後者會透過編譯作業簡化低階運算作業。
gemma.cpp 提供了極簡實作 Gemma 2B 和 7B 模型的簡易實作方式,其重點在於簡單明瞭,而非完整的通用性。從垂直整合的 C++ 模型實作中獲得啟發,例如 ggml、llama.c 和 llama.rs。
gemma.cpp 主要針對實驗和研究用途,特別是透過 Google Highway 程式庫使用可攜式 SIMD 探索 CPU 推論和推論演算法的設計空間。其設計是相當簡單地嵌入其他專案中,只需要最低程度的依附元件,還可以透過小型約 2K 個 LoC 核心實作 (以及約 4K 個 LoC 的支援公用程式) 輕鬆修改。
針對實際工作環境導向的邊緣部署,我們建議使用 JAX、Keras、PyTorch 和 Transformer 等成熟的 Python 架構進行標準部署路徑 (這裡的所有模型變化版本)。
不論社群大小,我們都歡迎社群貢獻心力。本專案遵循 Google 的開放原始碼社群規範。
快速入門導覽課程
如要完成本快速入門導覽課程,您必須複製或下載 gemma.cpp。
系統需求
開始之前,請先安裝好:
- CMake
- Clang C++ 編譯器
tar
:用於從 Kaggle 擷取封存。
步驟 1:從 Kaggle 取得模型權重和符記化工具
前往 Kaggle 的 gemma 模型頁面,然後選取「模型變化版本」
Gemma C++
. On this tab, the
Variation` 下拉式選單包含下列選項。請注意,bfloat16 權重選項較為保真,8 位元切換浮點權重則能加快推論速度。
20 億個指令經過調整 (it
) 和預先訓練的模型 (pt
):
模型名稱 | 說明 |
---|---|
2b-it |
20 億個參數指令調整模型,bfloat16 |
2b-it-sfp |
20 億個參數指令調整模型、8 位元交換浮點 |
2b-pt |
20 億個參數預先訓練模型 bfloat16 |
2b-pt-sfp |
20 億個參數預先訓練模型、8 位元交換浮點 |
70 億個指令經過調整 (it
) 和預先訓練的模型 (pt
):
模型名稱 | 說明 |
---|---|
7b-it |
70 億個參數指令調整模型,bfloat16 |
7b-it-sfp |
70 億個參數指令調整模型、8 位元交換浮點 |
7b-pt |
70 億個參數預先訓練模型 bfloat16 |
7b-pt-sfp |
70 億個參數預先訓練模型、8 位元交換浮點 |
注意:建議從 2b-it-sfp
著手,以便開始運作。
步驟 2:擷取檔案
填寫同意聲明表單後,下載作業應繼續擷取 tar 封存檔案 archive.tar.gz
。從 archive.tar.gz
擷取檔案 (這可能需要幾分鐘的時間):
tar -xf archive.tar.gz
這樣做應會產生含有模型權重的檔案,例如 2b-it-sfp.sbs
和權杖化工具檔案 (tokenizer.spm
)。建議您將這些檔案移至方便的目錄位置 (例如此存放區中的 build/ 目錄)。
步驟 3:建立
建構系統使用 CMake。如要建構 Gemma 推論執行階段,請建立建構目錄,並使用頂層專案目錄中的 cmake
產生建構檔案:
(cd build && cmake ..)
然後執行 make
建構 ./gemma
執行檔:
cd build
make -j [number of parallel threads to use] gemma
例如:make -j 8 gemma
。如果成功,build/
目錄中應該會出現 gemma
執行檔。
步驟 4:執行
您現在可以從 build/
目錄內執行 gemma
。
gemma
含有下列必要引數:
引數 | 說明 | 範例值 |
---|---|---|
--model |
模型類型。 | 2b-it 、2b-pt 、7b-it 、7b-pt ... (請參閱上文) |
--compressed_weights |
壓縮後的權重檔案。 | 2b-it-sfp.sbs 、... (請參閱上文) |
--tokenizer |
權杖化工具檔案名稱。 | tokenizer.spm |
gemma
的叫用方式如下:
./gemma \
--tokenizer [tokenizer file] \
--compressed_weights [compressed weights file] \
--model [2b-it or 2b-pt or 7b-it or 7b-pt]
下列設定的叫用範例:
- 壓縮權重檔案
2b-it-sfp.sbs
(2B 指令調整模型、8 位元切換浮點)。 - 權杖化工具檔案
tokenizer.spm
。
./gemma \
--tokenizer tokenizer.spm \
--compressed_weights 2b-it-sfp.sbs \
--model 2b-it
使用方式
gemma
有不同的使用模式,由詳細標記控管。
所有使用模式都是互動式模式,會在以換行符號輸入時觸發文字。
詳細程度 | 使用模式 | 詳細說明 |
---|---|---|
--verbosity 0 |
最低旗標 | 只顯示產生輸出內容。適用於 CLI 工具。 |
--verbosity 1 |
預設 | 面向使用者的標準終端機 UI。 |
--verbosity 2 |
明細 | 顯示其他開發人員和偵錯資訊。 |
互動式終端機應用程式
根據預設,詳細程度設為 1,當使用必要引數叫用 gemma
時,就會顯示終端機式互動式介面:
$ ./gemma [...]
__ _ ___ _ __ ___ _ __ ___ __ _ ___ _ __ _ __
/ _` |/ _ \ '_ ` _ \| '_ ` _ \ / _` | / __| '_ \| '_ \
| (_| | __/ | | | | | | | | | | (_| || (__| |_) | |_) |
\__, |\___|_| |_| |_|_| |_| |_|\__,_(_)___| .__/| .__/
__/ | | | | |
|___/ |_| |_|
tokenizer : tokenizer.spm
compressed_weights : 2b-it-sfp.sbs
model : 2b-it
weights : [no path specified]
max_tokens : 3072
max_generated_tokens : 2048
*Usage*
Enter an instruction and press enter (%Q quits).
*Examples*
- Write an email to grandma thanking her for the cookies.
- What are some historical attractions to visit around Massachusetts?
- Compute the nth fibonacci number in javascript.
- Write a standup comedy bit about WebGPU programming.
> What are some outdoorsy places to visit around Boston?
[ Reading prompt ] .....................
**Boston Harbor and Islands:**
* **Boston Harbor Islands National and State Park:** Explore pristine beaches, wildlife, and maritime history.
* **Charles River Esplanade:** Enjoy scenic views of the harbor and city skyline.
* **Boston Harbor Cruise Company:** Take a relaxing harbor cruise and admire the city from a different perspective.
* **Seaport Village:** Visit a charming waterfront area with shops, restaurants, and a seaport museum.
**Forest and Nature:**
* **Forest Park:** Hike through a scenic forest with diverse wildlife.
* **Quabbin Reservoir:** Enjoy boating, fishing, and hiking in a scenic setting.
* **Mount Forest:** Explore a mountain with breathtaking views of the city and surrounding landscape.
...
做為指令列工具使用
如要將 gemma
執行檔做為指令列工具使用,建議為包含完整引數的 gemma.cpp 建立別名:
alias gemma2b="~/gemma.cpp/build/gemma -- --tokenizer
~/gemma.cpp/build/tokenizer.spm --compressed_weights
~/gemma.cpp/build/2b-it-sfp.sbs --model 2b-it --verbosity 0"
將上述路徑替換為您下載後的模型和權杖化工具路徑。
以下示範如何使用截斷的輸入提示 gemma
(使用上述定義的 gemma2b
別名):
cat configs.h | tail -35 | tr '\n' ' ' | xargs -0 echo "What does this C++ code
do: " | gemma2b
注意:gemma.cpp 的 CLI 使用方式仍處於實驗階段,且應將背景資訊長度限制納入考量。
上述指令的輸出內容應會如下所示:
$ cat configs.h | tail -35 | tr '\n' ' ' | xargs -0 echo "What does this C++ code do: " | gemma2b
[ Reading prompt ] ......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
The code defines two C++ structs, `ConfigGemma7B` and `ConfigGemma2B`, which are used for configuring a deep learning model.
**ConfigGemma7B**:
* `seq_len`: Stores the length of the sequence to be processed. It's set to 7168.
* `vocab_size`: Stores the size of the vocabulary, which is 256128.
* `n_layers`: Number of layers in the deep learning model. It's set to 28.
* `dim_model`: Dimension of the model's internal representation. It's set to 3072.
* `dim_ffw_hidden`: Dimension of the feedforward and recurrent layers' hidden representations. It's set to 16 * 3072 / 2.
**ConfigGemma2B**:
* `seq_len`: Stores the length of the sequence to be processed. It's also set to 7168.
* `vocab_size`: Size of the vocabulary, which is 256128.
* `n_layers`: Number of layers in the deep learning model. It's set to 18.
* `dim_model`: Dimension of the model's internal representation. It's set to 2048.
* `dim_ffw_hidden`: Dimension of the feedforward and recurrent layers' hidden representations. It's set to 16 * 2048 / 2.
These structs are used to configure a deep learning model with specific parameters for either Gemma7B or Gemma2B architecture.