-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add rank endpoint (#694)
- Loading branch information
Showing
6 changed files
with
164 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
def convert_float_to_float16(model_path: str, output_model_path: str): | ||
import onnx | ||
from onnxmltools.utils.float16_converter import ( | ||
convert_float_to_float16_model_path, | ||
) | ||
|
||
new_onnx_model = convert_float_to_float16_model_path(model_path) | ||
|
||
onnx.save(new_onnx_model, output_model_path) | ||
|
||
# Alternate approach | ||
# from onnx import load_model | ||
# from onnxruntime.transformers import optimizer, onnx_model | ||
# | ||
# # optimized_model = optimizer.optimize_model(model_path, model_type='bert') | ||
# | ||
# model = load_model(model_path) | ||
# optimized_model = onnx_model.OnnxModel(model) | ||
# | ||
# if hasattr(optimized_model, 'convert_float32_to_float16'): | ||
# optimized_model.convert_float_to_float16() | ||
# else: | ||
# optimized_model.convert_model_float32_to_float16() | ||
# | ||
# self._textual_path = f'{self._textual_path[:-5]}_optimized.onnx' | ||
# optimized_model.save_model_to_file(output_model_path) | ||
|
||
|
||
def quantize(model_path: str, output_model_path: str): | ||
""" | ||
Quantize the weights of the model from float32 to in8 to allow very efficient inference on modern CPU | ||
Uses unsigned ints for activation values, signed ints for weights, per | ||
https://onnxruntime.ai/docs/performance/quantization.html#data-type-selection | ||
it is faster on most CPU architectures | ||
Args: | ||
onnx_model_path: Path to location the exported ONNX model is stored | ||
Returns: The Path generated for the quantized | ||
""" | ||
from onnxruntime.quantization import quantize_dynamic, QuantType | ||
|
||
quantize_dynamic( | ||
model_input=model_path, | ||
model_output=output_model_path, | ||
per_channel=True, | ||
reduce_range=True, # should be the same as per_channel | ||
activation_type=QuantType.QUInt8, | ||
weight_type=QuantType.QInt8, # per docs, signed is faster on most CPUs | ||
optimize_model=True, | ||
op_types_to_quantize=["MatMul", "Attention", "Mul", "Add"], | ||
extra_options={"WeightSymmetric": False, "MatMulConstBOnly": True}, | ||
) # op_types_to_quantize=['MatMul', 'Relu', 'Add', 'Mul' ], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import os | ||
|
||
import pytest | ||
from clip_server.executors.clip_torch import CLIPEncoder | ||
from docarray import DocumentArray, Document | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_torch_executor_rank_img2texts(): | ||
ce = CLIPEncoder() | ||
|
||
da = DocumentArray.from_files( | ||
f'{os.path.dirname(os.path.abspath(__file__))}/**/*.jpg' | ||
) | ||
for d in da: | ||
d.chunks.append(Document(text='hello, world!')) | ||
d.chunks.append(Document(text='goodbye, world!')) | ||
|
||
await ce.rank(da) | ||
print(da['@c', 'scores__clip-rank__value']) | ||
for d in da: | ||
for c in d.chunks: | ||
assert c.scores['clip-rank'].value is not None | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_torch_executor_rank_text2imgs(): | ||
ce = CLIPEncoder() | ||
db = DocumentArray( | ||
[Document(text='hello, world!'), Document(text='goodbye, world!')] | ||
) | ||
for d in db: | ||
d.chunks.extend( | ||
DocumentArray.from_files( | ||
f'{os.path.dirname(os.path.abspath(__file__))}/**/*.jpg' | ||
) | ||
) | ||
await ce.rank(db) | ||
print(db['@c', 'scores__clip-rank__value']) | ||
for d in db: | ||
for c in d.chunks: | ||
assert c.scores['clip-rank'].value is not None |