|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Articles d’actualité sur les crypto-monnaies
llama.cpp : écriture d'un programme d'inférence C++ simple pour les modèles GGUF LLM
Jan 14, 2025 at 03:04 am
Explorer les composants internes de llama.cpp et un flux de programme de discussion de base
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.
Ce didacticiel vous guidera tout au long du processus de création d'un programme C++ simple qui effectue des inférences sur les modèles GGUF LLM à l'aide du framework llama.cpp. Nous aborderons les étapes essentielles impliquées dans le chargement du modèle, la réalisation de l'inférence et l'affichage des résultats. Le code de ce tutoriel peut être trouvé ici.
Prerequisites
Conditions préalables
To follow along with this tutorial, you will need the following:
Pour suivre ce tutoriel, vous aurez besoin des éléments suivants :
A Linux-based operating system (native or WSL)
Un système d'exploitation basé sur Linux (natif ou WSL)
CMake installed
CMake installé
GNU/clang toolchain installed
Chaîne d'outils GNU/clang installée
Step 1: Setting Up the Project
Étape 1 : Mise en place du projet
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.
Commençons par mettre en place notre projet. Nous allons construire un programme C/C++ qui utilise llama.cpp pour effectuer des inférences sur les modèles GGUF LLM.
Create a new project directory, let's call it smol_chat.
Créez un nouveau répertoire de projet, appelons-le 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.
Dans le répertoire du projet, clonons le référentiel llama.cpp dans un sous-répertoire appelé externals. Cela nous donnera accès au code source et aux en-têtes de llama.cpp.
mkdir -p externals
mkdir -p externes
cd externals
CD externes
git clone https://github.com/georgigerganov/llama.cpp.git
clone git https://github.com/georgigerganov/llama.cpp.git
cd ..
cd..
Step 2: Configuring CMake
Étape 2 : configuration de 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.
Maintenant, configurons notre projet pour utiliser CMake. Cela nous permettra de compiler et de lier facilement notre code C/C++ avec la bibliothèque llama.cpp.
Create a CMakeLists.txt file in the project directory.
Créez un fichier CMakeLists.txt dans le répertoire du projet.
In the CMakeLists.txt file, add the following code:
Dans le fichier CMakeLists.txt, ajoutez le code suivant :
cmake_minimum_required(VERSION 3.10)
project(smol_chat)
projet (smol_chat)
set(CMAKE_CXX_STANDARD 20)
ensemble(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
ensemble (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)
target_link_libraries(smol_chat lama.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.
Ce code spécifie la version minimale de CMake, définit la norme C++ et l'indicateur standard, ajoute un exécutable nommé smol_chat, inclut les en-têtes du répertoire source actuel et lie la bibliothèque partagée llama.cpp à notre exécutable.
Step 3: Defining the LLM Interface
Étape 3 : Définition de l'interface 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.
Ensuite, définissons une classe C++ qui gérera les interactions de haut niveau avec le LLM. Cette classe fera abstraction des appels de fonction lama.cpp de bas niveau et fournira une interface pratique pour effectuer des inférences.
In the project directory, create a header file called LLMInference.h.
Dans le répertoire du projet, créez un fichier d'en-tête appelé LLMInference.h.
In LLMInference.h, declare the following class:
Dans LLMInference.h, déclarez la classe suivante :
class LLMInference {
classe LLMInférence {
public:
publique:
LLMInference(const std::string& model_path);
LLMInference(const std::string& model_path);
~LLMInference();
~LLMInférence();
void startCompletion(const std::string& query);
void startCompletion(const std::string& query);
std::string completeNext();
std::string completeNext();
private:
privé:
llama_model llama_model_;
flame_model flame_model_;
llama_context llama_context_;
lama_context lama_context_;
llama_sampler llama_sampler_;
call_sampler call_sampler_;
std::vector
std :: vecteur _messages ;
std::vector
std :: vecteur _formattedMessages ;
std::vector
std :: vecteur _tokens ;
llama_batch batch_;
lama_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.
Cette classe possède un constructeur public qui prend le chemin d'accès au modèle GGUF LLM comme argument et un destructeur qui désalloue tous les objets alloués dynamiquement. Il possède également deux fonctions membres publiques : startCompletion, qui lance le processus d'achèvement pour une requête donnée, et completeNext, qui récupère le jeton suivant dans la séquence de réponse du LLM.
Step 4: Implementing LLM Inference Functions
Étape 4 : Implémentation des fonctions d'inférence LLM
Now, let's define the implementation for the LLMInference class in a file called LLMInference.cpp.
Définissons maintenant l'implémentation de la classe LLMInference dans un fichier appelé LLMInference.cpp.
In LLMInference.cpp, include the necessary headers and implement the class methods as follows:
Dans LLMInference.cpp, incluez les en-têtes nécessaires et implémentez les méthodes de classe comme suit :
#include "LLMInference.h"
#include "LLMInference.h"
#include "common.h"
#include "commun.h"
#include
#inclure
#include
#inclure
#include
#inclure
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() {
LLMInférence::~LLMInférence() {
for (auto& msg : _messages) {
pour (auto& msg : _messages) {
std::free(msg.content);
std :: free (msg.content);
}
}
llama_free_model(&llama_model_);
llama_free_model(&llama_model_);
llama_free_context(&llama_context_);
llama_free_context(&llama_context_);
}
void LLMInference::startCompletion(const std::string& query)
void LLMInference :: startCompletion (const std :: string & requête)
Clause de non-responsabilité: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.
-
- Remittix (RTX) gagne du terrain alors que les investisseurs de Dogecoin (DOGE) recherchent de nouveaux projets de cryptographie
- Feb 01, 2025 at 07:40 pm
- Le marché des crypto-monnaies bourdonne d'activité cette semaine, alors qu'Ethereum (ETH) s'approche de la barre des 5 000 $, et les investisseurs de Dogecoin (DOGE) tournent leur attention vers un nouveau projet crypto: Remittix (RTX).