-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v0 add autoquant #402
base: main
Are you sure you want to change the base?
v0 add autoquant #402
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import numpy as np | ||
import requests # type: ignore | ||
import torch.ao.quantization | ||
|
||
from infinity_emb._optional_imports import CHECK_SENTENCE_TRANSFORMERS, CHECK_TORCH | ||
from infinity_emb.env import MANAGER | ||
|
@@ -34,14 +35,22 @@ def quant_interface(model: Any, dtype: Dtype = Dtype.int8, device: Device = Devi | |
Defaults to Device.cpu. | ||
""" | ||
device_orig = model.device | ||
if device == Device.cpu and dtype in [Dtype.int8, Dtype.auto]: | ||
if dtype == Dtype.autoquant: | ||
import torchao # type: ignore | ||
|
||
model = torchao.autoquant(model) | ||
logger.info("using dtype=autoquant") | ||
elif device == Device.cpu and dtype in [Dtype.int8, Dtype.auto]: | ||
logger.info("using torch.quantization.quantize_dynamic()") | ||
# TODO: verify if cpu requires quantization with torch.quantization.quantize_dynamic() | ||
model = torch.quantization.quantize_dynamic( | ||
model.to("cpu"), # the original model | ||
{torch.nn.Linear}, # a set of layers to dynamically quantize | ||
dtype=torch.qint8, | ||
) | ||
model = torch.ao.quantization.quantize_dynamic( | ||
model, {torch.nn.Linear}, dtype=torch.qint8 | ||
) | ||
Comment on lines
46
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Two quantization methods are applied sequentially. This might lead to unexpected behavior or reduced model performance. Consider using only one method or clarify why both are necessary. |
||
elif device == Device.cuda and dtype in [Dtype.int8, Dtype.auto]: | ||
logger.info(f"using quantize() for {dtype.value}") | ||
quant_handler, state_dict = quantize(model, mode=dtype.value) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,11 @@ | |
import torch | ||
from transformers import AutoTokenizer, BertModel # type: ignore | ||
|
||
from infinity_emb.args import EngineArgs | ||
from infinity_emb.primitives import Device, Dtype | ||
from infinity_emb.transformer.embedder.sentence_transformer import ( | ||
SentenceTransformerPatched, | ||
) | ||
from infinity_emb.transformer.quantization.interface import quant_interface | ||
|
||
devices = [Device.cpu] | ||
|
@@ -49,3 +53,45 @@ def test_quantize_bert(device: Device, dtype: Dtype): | |
out_quant = model.forward(**tokens_encoded)["last_hidden_state"].mean(dim=1) | ||
|
||
assert torch.cosine_similarity(out_default, out_quant) > 0.95 | ||
|
||
|
||
def test_autoquant_quantization(): | ||
model_st = SentenceTransformerPatched( | ||
engine_args=EngineArgs( | ||
model_name_or_path="michaelfeil/bge-small-en-v1.5", | ||
dtype="autoquant", | ||
engine="torch", | ||
bettertransformer=False, | ||
) | ||
) | ||
model_default = SentenceTransformerPatched( | ||
engine_args=EngineArgs( | ||
model_name_or_path="michaelfeil/bge-small-en-v1.5", | ||
dtype="float32", | ||
engine="torch", | ||
bettertransformer=False, | ||
) | ||
) | ||
sentence = "This is a test sentence." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: This line is unused and can be removed. |
||
for sentence in [ | ||
"This is a test sentence.", | ||
"This is another sentence, that should be embedded. " * 10, | ||
"1", | ||
]: | ||
embedding_st = model_st.encode_post( | ||
model_st.encode_core(model_st.encode_pre([sentence])) | ||
) | ||
embedding_default = model_default.encode_post( | ||
model_default.encode_core(model_default.encode_pre([sentence])) | ||
) | ||
assert embedding_st.shape == embedding_default.shape | ||
|
||
# cosine similarity | ||
sim = torch.nn.functional.cosine_similarity( | ||
torch.tensor(embedding_st), torch.tensor(embedding_default) | ||
) | ||
assert sim > 0.95 | ||
|
||
|
||
if __name__ == "__main__": | ||
test_autoquant_quantization() | ||
Comment on lines
+96
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Running a single test function in main might not be ideal. Consider using a test runner or removing this block if not necessary. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: This import is unused in the current file. Consider removing it if not needed.