시가총액: $3.4772T -2.320%
거래량(24시간): $122.7994B 22.170%
  • 시가총액: $3.4772T -2.320%
  • 거래량(24시간): $122.7994B 22.170%
  • 공포와 탐욕 지수:
  • 시가총액: $3.4772T -2.320%
Cryptos
주제
Cryptospedia
소식
CryptosTopics
비디오
Top News
Cryptos
주제
Cryptospedia
소식
CryptosTopics
비디오
bitcoin
bitcoin

$102418.358867 USD

-1.97%

ethereum
ethereum

$3298.096549 USD

1.21%

xrp
xrp

$3.048127 USD

-1.30%

tether
tether

$0.999866 USD

-0.01%

solana
solana

$231.464380 USD

-2.61%

bnb
bnb

$675.655067 USD

-0.56%

usd-coin
usd-coin

$0.999928 USD

-0.01%

dogecoin
dogecoin

$0.327988 USD

-0.25%

cardano
cardano

$0.945324 USD

-1.12%

tron
tron

$0.256233 USD

0.65%

chainlink
chainlink

$25.471085 USD

1.61%

avalanche
avalanche

$34.603954 USD

-1.17%

stellar
stellar

$0.416369 USD

-2.01%

sui
sui

$4.058447 USD

-3.89%

toncoin
toncoin

$4.893106 USD

1.10%

암호화폐 뉴스 기사

llama.cpp: GGUF LLM 모델을 위한 간단한 C++ 추론 프로그램 작성

2025/01/14 03:04

llama.cpp 내부 및 기본 채팅 프로그램 흐름 살펴보기

llama.cpp: GGUF LLM 모델을 위한 간단한 C++ 추론 프로그램 작성

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

자식 클론 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)

설정(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& model_path);

~LLMInference();

~LLMInference();

void startCompletion(const std::string& query);

void startCompletion(const std::string& 쿼리);

std::string completeNext();

std::string 완료다음();

private:

사적인:

llama_model llama_model_;

불꽃_모델 불꽃_모델_;

llama_context llama_context_;

llama_context llama_context_;

llama_sampler llama_sampler_;

call_sampler call_sampler_;

std::vector _messages;

표준::벡터 _messages;

std::vector _formattedMessages;

표준::벡터 _formattedMessages;

std::vector _tokens;

표준::벡터 _tokens;

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 모델에 대한 경로를 인수로 사용하는 공개 생성자와 동적으로 할당된 개체를 할당 해제하는 소멸자가 있습니다. 또한 주어진 쿼리에 대한 완료 프로세스를 시작하는 startCompletion과 LLM의 응답 시퀀스에서 다음 토큰을 가져오는 CompleteNext라는 두 가지 공개 멤버 함수도 있습니다.

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) {

(자동& 메시지: _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& 쿼리)

부인 성명: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.

2025年02月01日 에 게재된 다른 기사