|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llama.cpp の内部と基本的なチャット プログラム フローの探索
This tutorial will guide you through the process of building a simple C++ program that performs inference on GGUF LLM models using the llama.cpp framework. We will cover the essential steps involved in loading the model, performing inference, and displaying the results. The code for this tutorial can be found here.
このチュートリアルでは、llama.cpp フレームワークを使用して GGUF LLM モデルの推論を実行する単純な C++ プログラムを構築するプロセスを説明します。モデルのロード、推論の実行、結果の表示に関連する重要な手順について説明します。このチュートリアルのコードはここにあります。
Prerequisites
前提条件
To follow along with this tutorial, you will need the following:
このチュートリアルを進めるには、次のものが必要です。
A Linux-based operating system (native or WSL)
Linux ベースのオペレーティング システム (ネイティブまたは WSL)
CMake installed
CMakeがインストールされている
GNU/clang toolchain installed
GNU/clang ツールチェーンがインストールされている
Step 1: Setting Up the Project
ステップ 1: プロジェクトのセットアップ
Let's start by setting up our project. We will be building a C/C++ program that uses llama.cpp to perform inference on GGUF LLM models.
プロジェクトを設定することから始めましょう。 llama.cpp を使用して GGUF LLM モデルの推論を実行する C/C++ プログラムを構築します。
Create a new project directory, let's call it smol_chat.
新しいプロジェクト ディレクトリを作成し、smol_chat という名前にします。
Within the project directory, let's clone the llama.cpp repository into a subdirectory called externals. This will give us access to the llama.cpp source code and headers.
プロジェクト ディレクトリ内で、llama.cpp リポジトリのクローンを externals というサブディレクトリに作成しましょう。これにより、llama.cpp のソース コードとヘッダーにアクセスできるようになります。
mkdir -p externals
mkdir -p 外部
cd externals
CDの外装
git clone https://github.com/georgigerganov/llama.cpp.git
git クローン https://github.com/georgigerganov/llama.cpp.git
cd ..
CD ..
Step 2: Configuring CMake
ステップ 2: CMake の構成
Now, let's configure our project to use CMake. This will allow us to easily compile and link our C/C++ code with the llama.cpp library.
次に、CMake を使用するようにプロジェクトを構成しましょう。これにより、C/C++ コードを簡単にコンパイルし、llama.cpp ライブラリにリンクできるようになります。
Create a CMakeLists.txt file in the project directory.
プロジェクト ディレクトリに CMakeLists.txt ファイルを作成します。
In the CMakeLists.txt file, add the following code:
CMakeLists.txt ファイルに次のコードを追加します。
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(バージョン 3.10)
project(smol_chat)
プロジェクト(smol_chat)
set(CMAKE_CXX_STANDARD 20)
セット(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(smol_chat main.cpp)
target_include_directories(smol_chat PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(smol_chat llama.cpp)
This code specifies the minimum CMake version, sets the C++ standard and standard flag, adds an executable named smol_chat, includes headers from the current source directory, and links the llama.cpp shared library to our executable.
このコードは、CMake の最小バージョンを指定し、C++ 標準と標準フラグを設定し、smol_chat という名前の実行可能ファイルを追加し、現在のソース ディレクトリのヘッダーを組み込み、llama.cpp 共有ライブラリを実行可能ファイルにリンクします。
Step 3: Defining the LLM Interface
ステップ 3: LLM インターフェイスの定義
Next, let's define a C++ class that will handle the high-level interactions with the LLM. This class will abstract away the low-level llama.cpp function calls and provide a convenient interface for performing inference.
次に、LLM との高レベルの対話を処理する C++ クラスを定義しましょう。このクラスは、低レベルの llama.cpp 関数呼び出しを抽象化し、推論を実行するための便利なインターフェイスを提供します。
In the project directory, create a header file called LLMInference.h.
プロジェクト ディレクトリに、LLMInference.h というヘッダー ファイルを作成します。
In LLMInference.h, declare the following class:
LLMInference.h で、次のクラスを宣言します。
class LLMInference {
クラス LLMInference {
public:
公共:
LLMInference(const std::string& model_path);
LLMInference(const std::string& モデルパス);
~LLMInference();
~LLMInference();
void startCompletion(const std::string& query);
void startCompletion(const std::string& query);
std::string completeNext();
std::string completeNext();
private:
プライベート:
llama_model llama_model_;
炎_モデル 炎_モデル_;
llama_context llama_context_;
ラマ_コンテキスト ラマ_コンテキスト_;
llama_sampler llama_sampler_;
call_sampler call_sampler_;
std::vector
std::vector _messages;
std::vector
std::vector _formattedMessages;
std::vector
std::vector _トークン;
llama_batch batch_;
ラマ_バッチ バッチ_;
};
This class has a public constructor that takes the path to the GGUF LLM model as an argument and a destructor that deallocates any dynamically-allocated objects. It also has two public member functions: startCompletion, which initiates the completion process for a given query, and completeNext, which fetches the next token in the LLM's response sequence.
このクラスには、GGUF LLM モデルへのパスを引数として受け取るパブリック コンストラクターと、動的に割り当てられたオブジェクトの割り当てを解除するデストラクターがあります。また、2 つのパブリック メンバー関数もあります。startCompletion は、指定されたクエリの完了プロセスを開始し、completeNext は、LLM の応答シーケンス内の次のトークンをフェッチします。
Step 4: Implementing LLM Inference Functions
ステップ 4: LLM 推論関数の実装
Now, let's define the implementation for the LLMInference class in a file called LLMInference.cpp.
次に、LLMInference.cpp というファイルで LLMInference クラスの実装を定義しましょう。
In LLMInference.cpp, include the necessary headers and implement the class methods as follows:
LLMInference.cpp に必要なヘッダーを組み込み、次のようにクラス メソッドを実装します。
#include "LLMInference.h"
#include "LLMInference.h"
#include "common.h"
#include "common.h"
#include
#含む
#include
#含む
#include
#含む
LLMInference::LLMInference(const std::string& model_path) {
LLMInference::LLMInference(const std::string&model_path) {
llama_load_model_from_file(&llama_model_, model_path.c_str(), llama_model_default_params());
llama_load_model_from_file(&llama_model_, model_path.c_str(), llama_model_default_params());
llama_new_context_with_model(&llama_context_, &llama_model_);
llama_new_context_with_model(&llama_context_, &llama_model_);
llama_sampler_init_temp(&llama_sampler_, 0.8f);
llama_sampler_init_temp(&llama_sampler_, 0.8f);
llama_sampler_init_min_p(&llama_sampler_, 0.0f);
call_sampler_init_min_p(&call_sampler_, 0.0f);
}
LLMInference::~LLMInference() {
for (auto& msg : _messages) {
for (auto& msg : _messages) {
std::free(msg.content);
std::free(msg.content);
}
}
llama_free_model(&llama_model_);
ラマ_フリー_モデル(&ラマ_モデル_);
llama_free_context(&llama_context_);
ラマ_フリー_コンテキスト(&ラマ_コンテキスト_);
}
void LLMInference::startCompletion(const std::string& query)
void LLMInference::startCompletion(const std::string& query)
免責事項:info@kdj.com
The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!
If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.