国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

使用Llama.cpp在CPU上快速的運行LLM

這篇具有很好參考價值的文章主要介紹了使用Llama.cpp在CPU上快速的運行LLM。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

大型語言模型(llm)正變得越來越流行,但是它需要很多的資源,尤其時GPU。在這篇文章中,我們將介紹如何使用Python中的llama.cpp庫在高性能的cpu上運行l(wèi)lm。

使用Llama.cpp在CPU上快速的運行LLM,llama,深度學(xué)習(xí),人工智能,神經(jīng)網(wǎng)絡(luò),大語言模型

大型語言模型(llm)正變得越來越流行,但是它們的運行在計算上是非常消耗資源的。有很多研究人員正在為改進這個缺點而努力,比如HuggingFace開發(fā)出支持4位和8位的模型加載。但它們也需要GPU才能工作。雖然可以在直接在cpu上運行這些llm,但CPU的性能還無法滿足現(xiàn)有的需求。而Georgi Gerganov最近的工作使llm在高性能cpu上運行成為可能。這要歸功于他的llama.cpp庫,該庫為各種llm提供了高速推理。

原始的llama.cpp庫側(cè)重于在shell中本地運行模型。這并沒有為用戶提供很大的靈活性,并且使用戶很難利用大量的python庫來構(gòu)建應(yīng)用程序。而最近LangChain的發(fā)展使得我可以可以在python中使用llama.cpp。

在這篇文章中,我們將介紹如何在Python中使用llama-cpp-python包使用llama.cpp庫。我們還將介紹如何使用LLaMA -cpp-python庫來運行Vicuna LLM。

llama- pcp -python

 pip install llama-cpp-python

更詳細的安裝說明,請參閱llama- pcp -python文檔:https://github.com/abetlen/llama-cpp-python#installation-from-pypi-recommended。

使用LLM和llama-cpp-python

只要語言模型轉(zhuǎn)換為GGML格式,就可以被llama.cpp加載和使用。而大多數(shù)流行的LLM都有可用的GGML版本。

需要注意的重要一點是,在將原始llm轉(zhuǎn)換為GGML格式時,它們就已被量化過了。量化的好處是在不顯著降低性能的情況下,減少運行這些大型模型所需的內(nèi)存。例如,在不到4GB的RAM中可以加載大小為13GB的70億個參數(shù)模型。

在本文中,我們使用GGML版本的Vicuna-7B,該模型可從HuggingFace下載:https://huggingface.co/CRD716/ggml-vicuna-1.1-quantized。

下載GGML文件并加載LLM

可以使用以下代碼下載模型。該代碼還在嘗試下載文件之前檢查該文件是否已經(jīng)存在。

 import os
 import urllib.request
 
 
 def download_file(file_link, filename):
     # Checks if the file already exists before downloading
     if not os.path.isfile(filename):
         urllib.request.urlretrieve(file_link, filename)
         print("File downloaded successfully.")
     else:
         print("File already exists.")
 
 # Dowloading GGML model from HuggingFace
 ggml_model_path = "https://huggingface.co/CRD716/ggml-vicuna-1.1-quantized/resolve/main/ggml-vicuna-7b-1.1-q4_1.bin"
 filename = "ggml-vicuna-7b-1.1-q4_1.bin"
 
 download_file(ggml_model_path, filename)

下一步是加載模型:

 from llama_cpp import Llama
 
 llm = Llama(model_path="ggml-vicuna-7b-1.1-q4_1.bin", n_ctx=512, n_batch=126)

在加載模型時,應(yīng)該設(shè)置兩個重要參數(shù)。

n_ctx:用于設(shè)置模型的最大上下文大小。默認值是512個token。

上下文大小是輸入提示符中的令牌數(shù)量和模型可以生成的令牌最大數(shù)量的總和。具有較小上下文大小的模型生成文本的速度比具有較大上下文大小的模型快得多。

n_batch:用于設(shè)置在生成文本時要批處理的提示令牌的最大數(shù)量。默認值是512個token。

應(yīng)該仔細設(shè)置n_batch參數(shù)。降低n_batch有助于加速多線程cpu上的文本生成。但是太少可能會導(dǎo)致文本生成明顯惡化。

使用LLM生成文本

下面的代碼編寫了一個簡單的包裝器函數(shù)來使用LLM生成文本。

 def generate_text(
     prompt="Who is the CEO of Apple?",
     max_tokens=256,
     temperature=0.1,
     top_p=0.5,
     echo=False,
     stop=["#"],
 ):
     output = llm(
         prompt,
         max_tokens=max_tokens,
         temperature=temperature,
         top_p=top_p,
         echo=echo,
         stop=stop,
     )
     output_text = output["choices"][0]["text"].strip()
     return output_text

llm對象有幾個重要的參數(shù):

prompt:模型的輸入提示。該文本被標記并傳遞給模型。

max_tokens:該參數(shù)用于設(shè)置模型可以生成的令牌的最大數(shù)量。此參數(shù)控制文本生成的長度。默認值是128個token。

temperature:溫度,介于0和1之間。較高的值(如0.8)將使輸出更加隨機,而較低的值(如0.2)將使輸出更加集中和確定。缺省值為1。

top_p:溫度采樣的替代方案,稱為核采樣,其中模型考慮具有top_p概率質(zhì)量的標記的結(jié)果。所以0.1意味著只考慮包含前10%概率質(zhì)量的標記。

echo: 用于控制模型是否返回(回顯)生成文本開頭的模型提示符。

stop:用于停止文本生成的字符串列表。如果模型遇到任何字符串,文本生成將在該標記處停止。用于控制模型幻覺,防止模型產(chǎn)生不必要的文本。

llm對象返回如下形式的字典對象:

 {
   "id": "xxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",  # text generation id 
   "object": "text_completion",              # object name
   "created": 1679561337,                    # time stamp
   "model": "./models/7B/ggml-model.bin",    # model path
   "choices": [
     {
       "text": "Q: Name the planets in the solar system? A: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune and Pluto.", # generated text
       "index": 0,
       "logprobs": None,
       "finish_reason": "stop"
     }
   ],
   "usage": {
     "prompt_tokens": 14,       # Number of tokens present in the prompt
     "completion_tokens": 28,   # Number of tokens present in the generated text
     "total_tokens": 42
   }
 }

可以使用output"choices"[“text”]從字典對象中提取生成的文本。

使用Vicuna-7B生成文本的示例代碼

 import os
 import urllib.request
 from llama_cpp import Llama
 
 
 def download_file(file_link, filename):
     # Checks if the file already exists before downloading
     if not os.path.isfile(filename):
         urllib.request.urlretrieve(file_link, filename)
         print("File downloaded successfully.")
     else:
         print("File already exists.")
 
 
 # Dowloading GGML model from HuggingFace
 ggml_model_path = "https://huggingface.co/CRD716/ggml-vicuna-1.1-quantized/resolve/main/ggml-vicuna-7b-1.1-q4_1.bin"
 filename = "ggml-vicuna-7b-1.1-q4_1.bin"
 
 download_file(ggml_model_path, filename)
 
 
 llm = Llama(model_path="ggml-vicuna-7b-1.1-q4_1.bin", n_ctx=512, n_batch=126)
 
 
 def generate_text(
     prompt="Who is the CEO of Apple?",
     max_tokens=256,
     temperature=0.1,
     top_p=0.5,
     echo=False,
     stop=["#"],
 ):
     output = llm(
         prompt,
         max_tokens=max_tokens,
         temperature=temperature,
         top_p=top_p,
         echo=echo,
         stop=stop,
     )
     output_text = output["choices"][0]["text"].strip()
     return output_text
 
 
 generate_text(
     "Compose an engaging travel blog post about a recent trip to Hawaii, highlighting cultural experiences and must-see attractions.",
     max_tokens=356,
 )

生成的文本如下:

 Hawaii is a state located in the United States of America that is known for its beautiful beaches, lush landscapes, and rich culture. It is made up of six islands: Oahu, Maui, Kauai, Lanai, Molokai, and Hawaii (also known as the Big Island). Each island has its own unique attractions and experiences to offer visitors.
 One of the most interesting cultural experiences in Hawaii is visiting a traditional Hawaiian village or ahupuaa. An ahupuaa is a system of land use that was used by ancient Hawaiians to manage their resources sustainably. It consists of a coastal area, a freshwater stream, and the surrounding uplands and forests. Visitors can learn about this traditional way of life at the Polynesian Cultural Center in Oahu or by visiting a traditional Hawaiian village on one of the other islands.
 Another must-see attraction in Hawaii is the Pearl Harbor Memorial. This historic site commemorates the attack on Pearl Harbor on December 7, 1941, which led to the United States' entry into World War II. Visitors can see the USS Arizona Memorial, a memorial that sits above the sunken battleship USS Arizona and provides an overview of the attack. They can also visit other museums and exhibits on the site to learn more about this important event in American history.
 Hawaii is also known for its beautiful beaches and crystal clear waters, which are perfect for swimming, snorkeling, and sunbathing.

總結(jié)

在這篇文章中,我們介紹了如何在Python中使用llama.cpp庫和llama-cpp-python包。這些工具支持基于cpu的llm高性能執(zhí)行。

Llama.cpp幾乎每天都在更新。推理的速度越來越快,社區(qū)定期增加對新模型的支持。在Llama.cpp有一個“convert.py”可以幫你將自己的Pytorch模型轉(zhuǎn)換為ggml格式。

llama.cpp庫和llama-cpp-python包為在cpu上高效運行l(wèi)lm提供了健壯的解決方案。如果您有興趣將llm合并到您的應(yīng)用程序中,我建議深入的研究一下這個包。

本文源代碼:

https://avoid.overfit.cn/post/257997272e4a454cae9d01556332d6a0

本文作者:Ashwin Mathur文章來源地址http://www.zghlxwxcb.cn/news/detail-564583.html

到了這里,關(guān)于使用Llama.cpp在CPU上快速的運行LLM的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 使用go-llama.cpp 運行 yi-01-6b大模型,使用本地CPU運行,速度挺快的

    使用go-llama.cpp 運行 yi-01-6b大模型,使用本地CPU運行,速度挺快的

    https://github.com/ggerganov/llama.cpp LaMA.cpp 項目是開發(fā)者 Georgi Gerganov 基于 Meta 釋出的 LLaMA 模型(簡易 Python 代碼示例)手擼的純 C/C++ 版本,用于模型推理。所謂推理,即是給輸入-跑模型-得輸出的模型運行過程。 那么,純 C/C++ 版本有何優(yōu)勢呢? 無需任何額外依賴,相比 Python 代碼

    2024年02月20日
    瀏覽(19)
  • 基于llama.cpp學(xué)習(xí)開源LLM本地部署

    目錄 前言 一、llama.cpp是什么? 二、使用步驟 1.下載編譯llama.cpp 2. 普通編譯 3. BLAS編譯 3.1、OpenBLAS 編譯 CPU版 3.2?cuBLAS 編譯GPU版本 4. 模型量化 4.1、模型文件下載:

    2024年01月21日
    瀏覽(30)
  • LLM大模型推理加速實戰(zhàn):vllm、fastllm與llama.cpp使用指南

    隨著人工智能技術(shù)的飛速發(fā)展,大型語言模型(LLM)在諸如自然語言處理、智能問答、文本生成等領(lǐng)域的應(yīng)用越來越廣泛。然而,LLM模型往往具有龐大的參數(shù)規(guī)模,導(dǎo)致推理過程計算量大、耗時長,成為了制約其實際應(yīng)用的關(guān)鍵因素。為了解決這個問題,一系列大模型推理加

    2024年04月13日
    瀏覽(28)
  • [NLP] 使用Llama.cpp和LangChain在CPU上使用大模型-RAG

    [NLP] 使用Llama.cpp和LangChain在CPU上使用大模型-RAG

    下面是構(gòu)建這個應(yīng)用程序時將使用的軟件工具: 1.Llama-cpp-python ?下載llama-cpp, llama-cpp-python [NLP] Llama2模型運行在Mac機器-CSDN博客 2、LangChain LangChain是一個提供了一組廣泛的集成和數(shù)據(jù)連接器,允許我們鏈接和編排不同的模塊。可以常見聊天機器人、數(shù)據(jù)分析和文檔問答等應(yīng)用。

    2024年02月04日
    瀏覽(20)
  • 大模型在cpu上使用llama_cpp部署無法加載模型的問題

    大模型在cpu上使用llama_cpp部署無法加載模型的問題

    錯誤:gguf_init_from_file: invalid magic characters \\\'tjgg\\\'等,也就是無法加載模型 因為最新版的llama-cpp-python不支持ggml文件格式了 解決方案: 1、降低版本(最簡單): pip install llama-cpp-python==0.1.78 2、直接下載對應(yīng)GGUF的模型 3、利用llama.cpp內(nèi)部轉(zhuǎn)換函數(shù)進行轉(zhuǎn)換 參考出處:TheBloke/Llam

    2024年01月20日
    瀏覽(26)
  • llama.cpp一種在本地CPU上部署的量化模型(超低配推理llama)

    llama.cpp一種在本地CPU上部署的量化模型(超低配推理llama)

    前不久,Meta前腳發(fā)布完開源大語言模型LLaMA, 隨后就被網(wǎng)友“泄漏”,直接放了一個磁力鏈接下載鏈接。 然而那些手頭沒有頂級顯卡的朋友們,就只能看看而已了 但是 Georgi Gerganov 開源了一個項目llama.cpp ggerganov/llama.cpp: Port of Facebook’s LLaMA model in C/C++ (github.com) 次項目的牛逼

    2023年04月23日
    瀏覽(21)
  • 【大模型】大模型 CPU 推理之 llama.cpp

    【大模型】大模型 CPU 推理之 llama.cpp

    描述 The main goal of llama.cpp is to enable LLM inference with minimal setup and state-of-the-art performance on a wide variety of hardware - locally and in the cloud. Plain C/C++ implementation without any dependencies Apple silicon is a first-class citizen - optimized via ARM NEON, Accelerate and Metal frameworks AVX, AVX2 and AVX512 support for x86 arc

    2024年04月14日
    瀏覽(23)
  • 在本地使用CPU運行Llama 2模型來實現(xiàn)文檔Q&A

    在本地使用CPU運行Llama 2模型來實現(xiàn)文檔Q&A

    第三方商業(yè)大型語言模型(LLM)提供商,如OpenAI的GPT4,通過簡單的API調(diào)用使LLM的使用更加容易。然而,由于數(shù)據(jù)隱私和合規(guī)等各種原因,我們可能仍需要在企業(yè)內(nèi)部部署或私有模型推理。 開源LLM的普及讓我們私有化部署大語言模型稱為可能,從而減少了對這些第三方提供商

    2024年02月13日
    瀏覽(18)
  • 使用GGML和LangChain在CPU上運行量化的llama2

    使用GGML和LangChain在CPU上運行量化的llama2

    Meta AI 在本周二發(fā)布了最新一代開源大模型 Llama 2。對比于今年 2 月發(fā)布的 Llama 1,訓(xùn)練所用的 token 翻了一倍,已經(jīng)達到了 2 萬億,對于使用大模型最重要的上下文長度限制,Llama 2 也翻了一倍。 在本文,我們將緊跟趨勢介紹如何在本地CPU推理上運行量化版本的開源Llama 2。 我

    2024年02月16日
    瀏覽(21)
  • [llama懶人包]ChatGPT本地下位替代llama-7b,支持全平臺顯卡/CPU運行

    [llama懶人包]ChatGPT本地下位替代llama-7b,支持全平臺顯卡/CPU運行

    LLAMA的懶人包: 鏈接: https://pan.baidu.com/s/1xOw8-eP8QB--u6y644_UPg?pwd=0l08? 提取碼:0l08 模型來源:elinas/llama-7b-hf-transformers-4.29 模型來源(LoRA):ymcui/Chinese-LLaMA-Alpaca 侵權(quán)請通知作者刪除 也可以進我的群下載哦:904511841 下面是llama的輸入樣例 ? 自我介紹一下llama 您好,我是llama。

    2024年02月12日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包