توقف تطوير AutogptQ. يرجى التبديل إلى GPTQModel كبديل إسقاط.
حزمة قياس LLM سهلة الاستخدام مع واجهات برمجة التطبيقات سهلة الاستخدام ، استنادًا إلى خوارزمية GPTQ (تقدير الوزن فقط).
use_marlin=True عند تحميل النماذج.auto-gptq ، لذلك يمكن أن يكون تشغيل نماذج GPTQ وتدريب الآن أكثر متاحة للجميع! شاهد هذه المدونة ومواردها لمزيد من التفاصيل!لمزيد من التاريخ ، يرجى اللجوء إلى هنا
يتم إنشاء النتيجة باستخدام هذا البرنامج النصي ، حجم الدفعة من الإدخال هو 1 ، استراتيجية فك الشفرة هي البحث عن الشعاع وفرض النموذج لإنشاء 512 رمزًا ، مقياس السرعة هو الرموز/S (أكبر ، كان ذلك أفضل).
يتم تحميل النموذج الكمي باستخدام الإعداد الذي يمكن أن يكتسب أسرع سرعة استنتاج.
| نموذج | GPU | num_beams | FP16 | GPTQ-int4 |
|---|---|---|---|---|
| لاما -7 ب | 1xa100-40g | 1 | 18.87 | 25.53 |
| لاما -7 ب | 1xa100-40g | 4 | 68.79 | 91.30 |
| موس مون 16 ب | 1xa100-40g | 1 | 12.48 | 15.25 |
| موس مون 16 ب | 1xa100-40g | 4 | أووم | 42.67 |
| موس مون 16 ب | 2xa100-40g | 1 | 06.83 | 06.78 |
| موس مون 16 ب | 2xa100-40g | 4 | 13.10 | 10.80 |
| GPT-J 6B | 1xrtx3060-12g | 1 | أووم | 29.55 |
| GPT-J 6B | 1xrtx3060-12g | 4 | أووم | 47.36 |
لمقارنة الحيرة ، يمكنك اللجوء إلى هنا وهنا
AutogptQ متاح على Linux و Windows فقط. يمكنك تثبيت أحدث إصدار مستقر من AutogptQ من PIP مع عجلات تم إنشاؤها مسبقًا:
| إصدار منصة | تثبيت | بنيت ضد بيتورش |
|---|---|---|
| كودا 11.8 | pip install auto-gptq --no-build-isolation --extra-index-url https://huggingface.github.io/autogptq-index/whl/cu118/ | 2.2.1+Cu118 |
| كودا 12.1 | pip install auto-gptq --no-build-isolation | 2.2.1+CU121 |
| ROCM 5.7 | pip install auto-gptq --no-build-isolation --extra-index-url https://huggingface.github.io/autogptq-index/whl/rocm573/ | 2.2.1+ROCM5.7 |
يمكن تثبيت AutoGPTQ مع تبعية Triton مع pip install auto-gptq[triton] --no-build-isolation من أجل أن تكون قادرًا على استخدام الواجهة الخلفية Triton (تدعم حاليًا فقط Linux ، No 3 بتات).
بالنسبة إلى AutogptQ الأقدم ، يرجى الرجوع إلى جدول تثبيت الإصدارات السابقة.
على أنظمة NVIDIA ، لا تدعم AutoGPTQ ماكسويل أو وحدة معالجة الرسومات المنخفضة.
استنساخ رمز المصدر:
git clone https://github.com/PanQiWei/AutoGPTQ.git && cd AutoGPTQ هناك حاجة إلى بعض الحزم من أجل البناء من المصدر: pip install numpy gekko pandas .
ثم ، قم بتثبيت محليًا من المصدر:
pip install -vvv --no-build-isolation -e . يمكنك تعيين BUILD_CUDA_EXT=0 لتعطيل مبنى امتداد Pytorch ، ولكن هذا يتم تثبيته بشدة حيث أن AutogptQ ثم يعود إلى تطبيق بطيء Python.
كملاذ أخير ، إذا فشل الأمر أعلاه ، يمكنك تجربة python setup.py install .
للتثبيت من Source for AMD GPUs دعم ROCM ، يرجى تحديد متغير بيئة ROCM_VERSION . مثال:
ROCM_VERSION=5.6 pip install -vvv --no-build-isolation -e . يمكن تسريع التجميع عن طريق تحديد متغير PYTORCH_ROCM_ARCH (مرجع) من أجل الإنشاء لجهاز هدف واحد ، على سبيل المثال gfx90a لأجهزة سلسلة MI200.
بالنسبة لأنظمة ROCM ، فإن الحزم rocsparse-dev و hipsparse-dev و rocthrust-dev و rocblas-dev و hipblas-dev مطلوبة للبناء.
إشعار: تأكد من أنك في الالتزام 65C2E15 أو أحدث
للتثبيت من Source لـ Intel Gaudi 2 HPU ، قم بتعيين BUILD_CUDA_EXT=0 متغير البيئة لتعطيل بناء امتداد Cuda Pytorch. مثال:
BUILD_CUDA_EXT=0 pip install -vvv --no-build-isolation -e .لاحظ أن Intel Gaudi 2 تستخدم نواة محسّنة عند الاستدلال ، وتتطلب
BUILD_CUDA_EXT=0على آلات غير CUDA.
تحذير: هذا مجرد عرض لاستخدام واجهات برمجة التطبيقات الأساسية في AutogptQ ، والتي تستخدم عينة واحدة فقط لتوظيف نموذج صغير ، قد لا تكون جودة النموذج الكمي باستخدام هذه العينات الصغيرة جيدة.
فيما يلي مثال على أبسط استخدام لـ auto_gptq لتوسيع نطاق نموذج واستدلال بعد القياس الكمي:
from transformers import AutoTokenizer , TextGenerationPipeline
from auto_gptq import AutoGPTQForCausalLM , BaseQuantizeConfig
import logging
logging . basicConfig (
format = "%(asctime)s %(levelname)s [%(name)s] %(message)s" , level = logging . INFO , datefmt = "%Y-%m-%d %H:%M:%S"
)
pretrained_model_dir = "facebook/opt-125m"
quantized_model_dir = "opt-125m-4bit"
tokenizer = AutoTokenizer . from_pretrained ( pretrained_model_dir , use_fast = True )
examples = [
tokenizer (
"auto-gptq is an easy-to-use model quantization library with user-friendly apis, based on GPTQ algorithm."
)
]
quantize_config = BaseQuantizeConfig (
bits = 4 , # quantize model to 4-bit
group_size = 128 , # it is recommended to set the value to 128
desc_act = False , # set to False can significantly speed up inference but the perplexity may slightly bad
)
# load un-quantized model, by default, the model will always be loaded into CPU memory
model = AutoGPTQForCausalLM . from_pretrained ( pretrained_model_dir , quantize_config )
# quantize model, the examples should be list of dict whose keys can only be "input_ids" and "attention_mask"
model . quantize ( examples )
# save quantized model
model . save_quantized ( quantized_model_dir )
# save quantized model using safetensors
model . save_quantized ( quantized_model_dir , use_safetensors = True )
# push quantized model to Hugging Face Hub.
# to use use_auth_token=True, Login first via huggingface-cli login.
# or pass explcit token with: use_auth_token="hf_xxxxxxx"
# (uncomment the following three lines to enable this feature)
# repo_id = f"YourUserName/{quantized_model_dir}"
# commit_message = f"AutoGPTQ model for {pretrained_model_dir}: {quantize_config.bits}bits, gr{quantize_config.group_size}, desc_act={quantize_config.desc_act}"
# model.push_to_hub(repo_id, commit_message=commit_message, use_auth_token=True)
# alternatively you can save and push at the same time
# (uncomment the following three lines to enable this feature)
# repo_id = f"YourUserName/{quantized_model_dir}"
# commit_message = f"AutoGPTQ model for {pretrained_model_dir}: {quantize_config.bits}bits, gr{quantize_config.group_size}, desc_act={quantize_config.desc_act}"
# model.push_to_hub(repo_id, save_dir=quantized_model_dir, use_safetensors=True, commit_message=commit_message, use_auth_token=True)
# load quantized model to the first GPU
model = AutoGPTQForCausalLM . from_quantized ( quantized_model_dir , device = "cuda:0" )
# download quantized model from Hugging Face Hub and load to the first GPU
# model = AutoGPTQForCausalLM.from_quantized(repo_id, device="cuda:0", use_safetensors=True, use_triton=False)
# inference with model.generate
print ( tokenizer . decode ( model . generate ( ** tokenizer ( "auto_gptq is" , return_tensors = "pt" ). to ( model . device ))[ 0 ]))
# or you can also use pipeline
pipeline = TextGenerationPipeline ( model = model , tokenizer = tokenizer )
print ( pipeline ( "auto-gptq is" )[ 0 ][ "generated_text" ])للحصول على المزيد من الميزات المتقدمة من كمية النموذج ، يرجى الإشارة إلى هذا البرنامج النصي
from auto_gptq . modeling import BaseGPTQForCausalLM
class OPTGPTQForCausalLM ( BaseGPTQForCausalLM ):
# chained attribute name of transformer layer block
layers_block_name = "model.decoder.layers"
# chained attribute names of other nn modules that in the same level as the transformer layer block
outside_layer_modules = [
"model.decoder.embed_tokens" , "model.decoder.embed_positions" , "model.decoder.project_out" ,
"model.decoder.project_in" , "model.decoder.final_layer_norm"
]
# chained attribute names of linear layers in transformer layer module
# normally, there are four sub lists, for each one the modules in it can be seen as one operation,
# and the order should be the order when they are truly executed, in this case (and usually in most cases),
# they are: attention q_k_v projection, attention output projection, MLP project input, MLP project output
inside_layer_modules = [
[ "self_attn.k_proj" , "self_attn.v_proj" , "self_attn.q_proj" ],
[ "self_attn.out_proj" ],
[ "fc1" ],
[ "fc2" ]
] بعد ذلك ، يمكنك استخدام OPTGPTQForCausalLM.from_pretrained وطرق أخرى كما هو موضح في الأساس.
يمكنك استخدام المهام المحددة في auto_gptq.eval_tasks لتقييم أداء النموذج على مهمة محددة لأسفل قبل وبعد القياس الكمي.
تدعم المهام المحددة مسبقًا جميع نماذج اللغة السببية التي تم تنفيذها؟ المحولات وفي هذا المشروع.
from functools import partial
import datasets
from transformers import AutoTokenizer , AutoModelForCausalLM , GenerationConfig
from auto_gptq import AutoGPTQForCausalLM , BaseQuantizeConfig
from auto_gptq . eval_tasks import SequenceClassificationTask
MODEL = "EleutherAI/gpt-j-6b"
DATASET = "cardiffnlp/tweet_sentiment_multilingual"
TEMPLATE = "Question:What's the sentiment of the given text? Choices are {labels}. n Text: {text} n Answer:"
ID2LABEL = {
0 : "negative" ,
1 : "neutral" ,
2 : "positive"
}
LABELS = list ( ID2LABEL . values ())
def ds_refactor_fn ( samples ):
text_data = samples [ "text" ]
label_data = samples [ "label" ]
new_samples = { "prompt" : [], "label" : []}
for text , label in zip ( text_data , label_data ):
prompt = TEMPLATE . format ( labels = LABELS , text = text )
new_samples [ "prompt" ]. append ( prompt )
new_samples [ "label" ]. append ( ID2LABEL [ label ])
return new_samples
# model = AutoModelForCausalLM.from_pretrained(MODEL).eval().half().to("cuda:0")
model = AutoGPTQForCausalLM . from_pretrained ( MODEL , BaseQuantizeConfig ())
tokenizer = AutoTokenizer . from_pretrained ( MODEL )
task = SequenceClassificationTask (
model = model ,
tokenizer = tokenizer ,
classes = LABELS ,
data_name_or_path = DATASET ,
prompt_col_name = "prompt" ,
label_col_name = "label" ,
** {
"num_samples" : 1000 , # how many samples will be sampled to evaluation
"sample_max_len" : 1024 , # max tokens for each sample
"block_max_len" : 2048 , # max tokens for each data block
# function to load dataset, one must only accept data_name_or_path as input
# and return datasets.Dataset
"load_fn" : partial ( datasets . load_dataset , name = "english" ),
# function to preprocess dataset, which is used for datasets.Dataset.map,
# must return Dict[str, list] with only two keys: [prompt_col_name, label_col_name]
"preprocess_fn" : ds_refactor_fn ,
# truncate label when sample's length exceed sample_max_len
"truncate_prompt" : False
}
)
# note that max_new_tokens will be automatically specified internally based on given classes
print ( task . run ())
# self-consistency
print (
task . run (
generation_config = GenerationConfig (
num_beams = 3 ,
num_return_sequences = 3 ,
do_sample = True
)
)
) توفر البرامج التعليمية إرشادات خطوة بخطوة لدمج auto_gptq مع مشروعك الخاص وبعض مبادئ أفضل الممارسات.
أمثلة توفر الكثير من البرامج النصية على سبيل المثال لاستخدام auto_gptq بطرق مختلفة.
يمكنك استخدام
model.config.model_typeللمقارنة مع الجدول أدناه للتحقق مما إذا كان النموذج الذي تستخدمه مدعومًا بواسطةauto_gptq.على سبيل المثال ، Model_type of
WizardLMوvicunaوgpt4allكلهاllama، وبالتالي يتم دعمها جميعًا بواسطةauto_gptq.
| نوع النموذج | الكمية | الاستدلال | Peft-Lora | Peft-ada-lora | peft-adaption_prompt |
|---|---|---|---|---|---|
| يزدهر | ✅ | ✅ | ✅ | ✅ | |
| GPT2 | ✅ | ✅ | ✅ | ✅ | |
| GPT_NEOX | ✅ | ✅ | ✅ | ✅ | required هذا الفرع peft |
| GPTJ | ✅ | ✅ | ✅ | ✅ | required هذا الفرع peft |
| لاما | ✅ | ✅ | ✅ | ✅ | ✅ |
| طحلب | ✅ | ✅ | ✅ | ✅ | required هذا الفرع peft |
| OPT | ✅ | ✅ | ✅ | ✅ | |
| GPT_BIGCODE | ✅ | ✅ | ✅ | ✅ | |
| Codegen | ✅ | ✅ | ✅ | ✅ | |
| Falcon (reforedwebmodel/refloveWeb) | ✅ | ✅ | ✅ | ✅ |
حاليًا ، يدعم auto_gptq : LanguageModelingTask و SequenceClassificationTask و TextSummarizationTask ؛ المزيد من المهام ستأتي قريبًا!
يمكن إجراء الاختبارات مع:
pytest tests/ -s
الافتراضات الافتراضية لاستخدام exllamav2 int4*fp16 kernel لضرب المصفوفة.
Marlin هو kernel int4 * fp16 محسّن تم اقتراحه مؤخرًا على https://github.com/ist-daslab/marlin. تم دمج هذا في AutogptQ عند تحميل نموذج باستخدام use_marlin=True . هذه النواة متوفرة فقط على الأجهزة ذات إمكانية حساب 8.0 أو 8.6 (وحدات معالجة الرسومات Ampere).