跳到主要內容
Open In ColabOpen on GitHub

Intel 權重僅量化

搭配 Intel Extension for Transformers Pipelines 的 Huggingface 模型權重僅量化

Hugging Face 模型可以透過 WeightOnlyQuantPipeline 類別,以權重僅量化在本地端執行。

Hugging Face 模型中心 託管超過 12 萬個模型、2 萬個資料集和 5 萬個示範應用程式 (Spaces),全部都是開放原始碼且公開可用,在一個線上平台上,人們可以輕鬆協作並共同建構 ML。

這些可以透過這個本地端管線包裝器類別從 LangChain 呼叫。

若要使用,您應該已安裝 transformers python 套件,以及 pytorchintel-extension-for-transformers

%pip install transformers --quiet
%pip install intel-extension-for-transformers

模型載入

可以透過使用 from_model_id 方法指定模型參數來載入模型。模型參數包含 intel_extension_for_transformers 中的 WeightOnlyQuantConfig 類別。

from intel_extension_for_transformers.transformers import WeightOnlyQuantConfig
from langchain_community.llms.weight_only_quantization import WeightOnlyQuantPipeline

conf = WeightOnlyQuantConfig(weight_dtype="nf4")
hf = WeightOnlyQuantPipeline.from_model_id(
model_id="google/flan-t5-large",
task="text2text-generation",
quantization_config=conf,
pipeline_kwargs={"max_new_tokens": 10},
)

它們也可以透過直接傳入現有的 transformers 管線來載入

from intel_extension_for_transformers.transformers import AutoModelForSeq2SeqLM
from transformers import AutoTokenizer, pipeline

model_id = "google/flan-t5-large"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
pipe = pipeline(
"text2text-generation", model=model, tokenizer=tokenizer, max_new_tokens=10
)
hf = WeightOnlyQuantPipeline(pipeline=pipe)

建立鏈

將模型載入到記憶體後,您可以將其與提示詞組合以形成鏈。

from langchain_core.prompts import PromptTemplate

template = """Question: {question}

Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)

chain = prompt | hf

question = "What is electroencephalography?"

print(chain.invoke({"question": question}))
API 參考:PromptTemplate

CPU 推論

現在 intel-extension-for-transformers 僅支援 CPU 裝置推論。即將支援 intel GPU。在具有 CPU 的機器上執行時,您可以指定 device="cpu"device=-1 參數,將模型放在 CPU 裝置上。預設值為 -1,用於 CPU 推論。

conf = WeightOnlyQuantConfig(weight_dtype="nf4")
llm = WeightOnlyQuantPipeline.from_model_id(
model_id="google/flan-t5-large",
task="text2text-generation",
quantization_config=conf,
pipeline_kwargs={"max_new_tokens": 10},
)

template = """Question: {question}

Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)

chain = prompt | llm

question = "What is electroencephalography?"

print(chain.invoke({"question": question}))

批次 CPU 推論

您也可以在批次模式下於 CPU 上執行推論。

conf = WeightOnlyQuantConfig(weight_dtype="nf4")
llm = WeightOnlyQuantPipeline.from_model_id(
model_id="google/flan-t5-large",
task="text2text-generation",
quantization_config=conf,
pipeline_kwargs={"max_new_tokens": 10},
)

chain = prompt | llm.bind(stop=["\n\n"])

questions = []
for i in range(4):
questions.append({"question": f"What is the number {i} in french?"})

answers = chain.batch(questions)
for answer in answers:
print(answer)

Intel-extension-for-transformers 支援的資料類型

我們支援將權重量化為以下資料類型以進行儲存(WeightOnlyQuantConfig 中的 weight_dtype)

  • int8:使用 8 位元資料類型。
  • int4_fullrange:與一般的 int4 範圍 [-7,7] 相比,使用 int4 範圍的 -8 值。
  • int4_clip:裁剪並保留 int4 範圍內的值,將其他值設定為零。
  • nf4:使用標準化浮點 4 位元資料類型。
  • fp4_e2m1:使用常規浮點 4 位元資料類型。「e2」表示 2 個位元用於指數,「m1」表示 1 個位元用於尾數。

雖然這些技術以 4 或 8 位元儲存權重,但計算仍然以 float32、bfloat16 或 int8 進行(WeightOnlyQuantConfig 中的 compute_dtype)

  • fp32:使用 float32 資料類型進行計算。
  • bf16:使用 bfloat16 資料類型進行計算。
  • int8:使用 8 位元資料類型進行計算。

支援的演算法矩陣

intel-extension-for-transformers 中支援的量化演算法(WeightOnlyQuantConfig 中的 algorithm)

演算法PyTorchLLM 執行階段
RTN
AWQ敬請期待
TEQ敬請期待

RTN: 一種我們可以非常直觀地思考的量化方法。它不需要額外的資料集,而且是一種非常快速的量化方法。一般來說,RTN 會將權重轉換為均勻分佈的整數資料類型,但有些演算法(例如 Qlora)提出了非均勻的 NF4 資料類型,並證明了其理論上的最佳性。

AWQ: 證明僅保護 1% 的顯著權重可以大大減少量化誤差。顯著權重通道是透過觀察每個通道的激活和權重分佈來選擇的。顯著權重也會在量化之前乘以大的比例因子以進行保留,然後再進行量化。

TEQ: 一種可訓練的等效轉換,可在僅權重量化中保留 FP32 精度。它受到 AWQ 的啟發,同時為搜尋激活和權重之間最佳的每通道縮放因子提供了新的解決方案。


此頁面是否對您有幫助?