英特尔权重量化技术
使用 Intel Extension for Transformers Pipelines 对 Huggingface 模型进行仅权重量化
Hugging Face 模型可以通过 WeightOnlyQuantPipeline 类使用仅权重量化在本地运行。
Hugging Face 模型中心 托管了超过 12 万个模型、2 万份数据集和 5 万个演示应用程序(Spaces),所有资源均为开源且公开可用,提供了一个在线平台,人们可以在其中轻松协作并共同构建机器学习项目。
这些可以通过此本地管道包装类从LangChain调用。
要使用,您应该安装了 transformers Python 软件包,以及 pytorch、intel-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}))
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 支持的数据类型
我们支持将权重量化为以下数据类型以进行存储(weight_dtype 在 WeightOnlyQuantConfig 中):
- int8: 使用 8 位数据类型。
- int4_fullrange: 使用 int4 范围的 -8 值,与标准 int4 范围 [-7,7] 相比。
- 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 位数据类型进行计算。
支持的算法矩阵
英特尔扩展库 for-transformers 支持的量化算法(WeightOnlyQuantConfig 中的算法):
| 算法 | PyTorch | LLM 运行时 |
|---|---|---|
| RTN | ✔ | ✔ |
| AWQ | ✔ | stay tuned |
| TEQ | ✔ | stay tuned |
RTN: 一种我们可以非常直观理解的量化方法。它不需要额外的数据集,是一种非常快速的量化方法。一般来说,RTN会将权重转换为均匀分布的整数数据类型,但是一些算法(如Qlora)提出了非均匀的NF4数据类型,并证明了其理论上的最优性。
AWQ: 证明了仅保护1%的显著权重即可大幅降低量化误差。显著权重通道是通过观察每个通道的激活值和权重分布来选择的。在量化前,显著权重还会乘以一个较大的缩放因子后再进行量化,以实现保护。
TEQ: 一种可训练的等效变换,可在仅权重量化中保持FP32精度。它受AWQ启发,同时提供了一种新的解决方案,用于搜索激活值与权重之间每通道缩放因子的最优值。