fastLLaMa 、生産環境で大規模な言語モデル(LLM)の展開に関連する課題に取り組むように設計された実験的な高性能フレームワークです。
C ++ライブラリであるllama.cppにユーザーフレンドリーなPythonインターフェイスを提供し、開発者がカスタムワークフローを作成し、適応可能なロギングを実装し、セッション間のシームレスにコンテキストを切り替えることができます。このフレームワークは、最適化されたコールドブート時間、NVIDIA GPUのINT4サポート、モデルアーティファクト管理、複数のプログラミング言語サポートなどの機能の導入に焦点を当てている継続的な開発に焦点を当てているため、大規模なLLMSの動作効率を向上させることを目的としています。
___ __ _ _ __ __
| | '___ ___ _| |_ | | | | ___ | ___
| |-<_> |<_-< | | | |_ | |_ <_> || |<_> |
|_| <___|/__/ |_| |___||___|<___||_|_|_|<___|
.+*+-.
-%#--
:=***%*++=.
:+=+**####%+
++=+*%#
.*+++==-
::--:. .**++=::
#%##*++=...... =*+==-::
.@@@*@%*==-==-==---:::::------::==*+==--::
%@@@@+--====+===---=---==+=======+++----:
.%@@*++*##***+===-=====++++++*++*+====++.
:@@%*##%@@%#*%#+==++++++=++***==-=+==+=-
%@%%%%%@%#+=*%*##%%%@###**++++==--==++
#@%%@%@@##**%@@@%#%%%%**++*++=====-=*-
-@@@@@@@%*#%@@@@@@@%%%%#+*%#++++++=*+.
+@@@@@%%*-#@@@@@@@@@@@%%@%**#*#+=-.
#%%###%: ..+#%@@@@%%@@@@%#+-
:***#*- ... *@@@%*+:
=***= -@%##**.
:#*++ -@#-:*=.
=##- .%*..##
+*- *: +-
:+- :+ =.
=-. *+ =-
:-:- =-- :::
aio_readのサポート。 io_uringを試します。 cmake
Linuxの場合:
sudo apt-get -y install cmake
OS Xの場合:
brew install cmake
Windows用
cmake-*。ダウンロードページからexeインストーラーをダウンロードして実行します。
GCC 11以上
最小C ++ 17
Python 3.x
PIPの使用を介してfastLLaMaをインストールします
pip install git+https://github.com/PotatoSpudowski/fastLLaMa.git@mainFastllamaをインポートするには、実行するだけです
from fastllama import Model MODEL_PATH = "./models/7B/ggml-model-q4_0.bin"
model = Model (
path = MODEL_PATH , #path to model
num_threads = 8 , #number of threads to use
n_ctx = 512 , #context size of model
last_n_size = 64 , #size of last n tokens (used for repetition penalty) (Optional)
seed = 0 , #seed for random number generator (Optional)
n_batch = 128 , #batch size (Optional)
use_mmap = False , #use mmap to load model (Optional)
) prompt = """Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision.
User: Hello, Bob.
Bob: Hello. How may I help you today?
User: Please tell me the largest city in Europe.
Bob: Sure. The largest city in Europe is Moscow, the capital of Russia.
User: """
res = model . ingest ( prompt , is_system_prompt = True ) #ingest model with prompt def stream_token ( x : str ) -> None :
"""
This function is called by the library to stream tokens
"""
print ( x , end = '' , flush = True )
res = model . generate (
num_tokens = 100 ,
top_p = 0.95 , #top p sampling (Optional)
temp = 0.8 , #temperature (Optional)
repeat_penalty = 1.0 , #repetition penalty (Optional)
streaming_fn = stream_token , #streaming function
stop_words = [ "User:" , " n " ] #stop generation when this word is encountered (Optional)
) model = Model (
path = MODEL_PATH , #path to model
num_threads = 8 , #number of threads to use
n_ctx = 512 , #context size of model
last_n_size = 64 , #size of last n tokens (used for repetition penalty) (Optional)
seed = 0 , #seed for random number generator (Optional)
n_batch = 128 , #batch size (Optional)
load_parallel = True
)セッションをキャッシュするには、 save_stateメソッドを使用できます。
res = model . save_state ( "./models/fast_llama.bin" )セッションをロードするには、 load_stateメソッドを使用します。
res = model . load_state ( "./models/fast_llama.bin" )セッションをリセットするには、 resetメソッドを使用します。
model . reset ()ランタイム中にLORAアダプターを取り付けるには、 attach_loraメソッドを使用してください。
LORA_ADAPTER_PATH = "./models/ALPACA-7B-ADAPTER/ggml-adapter-model.bin"
model . attach_lora ( LORA_ADAPTER_PATH )注:LORAアダプターを取り付けた後、モデルの状態をリセットすることをお勧めします。
ランタイム中にロラアダプターを取り外すには、 detach_loraメソッドを使用します。
model . detach_lora ()困惑を抑えるには、 perplexity法を使用します。
with open ( "test.txt" , "r" ) as f :
data = f . read ( 8000 )
total_perplexity = model . perplexity ( data )
print ( f"Total Perplexity: { total_perplexity :.4f } " )モデルの埋め込みを取得するには、 get_embeddingsメソッドを使用します。
embeddings = model . get_embeddings ()モデルのロジットを取得するには、 get_logitsメソッドを使用します。
logits = model . get_logits () from fastLLaMa import Logger
class MyLogger ( Logger ):
def __init__ ( self ):
super (). __init__ ()
self . file = open ( "logs.log" , "w" )
def log_info ( self , func_name : str , message : str ) -> None :
#Modify this to do whatever you want when you see info logs
print ( f"[Info]: Func(' { func_name } ') { message } " , flush = True , end = '' , file = self . file )
pass
def log_err ( self , func_name : str , message : str ) -> None :
#Modify this to do whatever you want when you see error logs
print ( f"[Error]: Func(' { func_name } ') { message } " , flush = True , end = '' , file = self . file )
def log_warn ( self , func_name : str , message : str ) -> None :
#Modify this to do whatever you want when you see warning logs
print ( f"[Warn]: Func(' { func_name } ') { message } " , flush = True , end = '' , file = self . file )さらに明確にするには、 examples/python/フォルダーを確認してください。
# obtain the original LLaMA model weights and place them in ./models
ls ./models
65B 30B 13B 7B tokenizer_checklist.chk tokenizer.model
# convert the 7B model to ggml FP16 format
# python [PythonFile] [ModelPath] [Floattype] [Vocab Only] [SplitType]
python3 scripts/convert-pth-to-ggml.py models/7B/ 1 0
# quantize the model to 4-bits
./build/src/quantize models/7B/ggml-model-f16.bin models/7B/ggml-model-q4_0.bin 2
# run the inference
# Run the scripts from the root dir of the project for now!
python ./examples/python/example.py # Before running this command
# You need to provide the HF model paths here
python ./scripts/export-from-huggingface.py
# Alternatively you can just download the ggml models from huggingface directly and run them!
python3 ./scripts/convert-pth-to-ggml.py models/ALPACA-LORA-7B 1 0
./build/src/quantize models/ALPACA-LORA-7B/ggml-model-f16.bin models/ALPACA-LORA-7B/alpaca-lora-q4_0.bin 2
python ./examples/python/example-alpaca.py # Download lora adapters and paste them inside models folder
# https://huggingface.co/tloen/alpaca-lora-7b
python scripts/convert-lora-to-ggml.py models/ALPACA-7B-ADAPTER/ -t fp32
# Change -t to fp16 to use fp16 weights
# Inorder to use LoRA adapters without caching, pass the --no-cache flag
# - Only supported for fp32 adapter weights
python examples/python/example-lora-adapter.py
# Make sure to set paths correctly for the base model and adapter inside the example
# Commands:
# load_lora: Attaches the adapter to the base model
# unload_lora: Deattaches the adapter (Deattach for fp16 is yet to be added!)
# reset: Resets the model stateWebSocketサーバーとWebUIを実行するには、それぞれのブランチの指示に従ってください。
モデルは現在メモリに完全にロードされているため、それらを保存するには適切なディスクスペースとそれらをロードするのに十分なRAMが必要になります。現時点では、メモリとディスクの要件は同じです。
| モデルサイズ | オリジナルサイズ | 量子化サイズ(4ビット) |
|---|---|---|
| 7b | 13 GB | 3.9 GB |
| 13b | 24 GB | 7.8 GB |
| 30b | 60 GB | 19.5 GB |
| 65b | 120 GB | 38.5 GB |
情報:実行時間には、推論中に余分なメモリが必要になる場合があります!
(モデルの初期化中に使用されるハイパーパーメーターに依存します)