-
Notifications
You must be signed in to change notification settings - Fork 177
optimum openvino #467
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
Open
michaelfeil
wants to merge
6
commits into
main
Choose a base branch
from
merge-git-checkout-main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−188
Open
optimum openvino #467
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,15 +6,18 @@ | |
|
|
||
| import numpy as np | ||
|
|
||
| from infinity_emb._optional_imports import CHECK_ONNXRUNTIME, CHECK_TRANSFORMERS | ||
| from infinity_emb._optional_imports import ( | ||
| CHECK_ONNXRUNTIME, | ||
| CHECK_TRANSFORMERS, | ||
| CHECK_OPTIMUM_INTEL, | ||
| ) | ||
| from infinity_emb.args import EngineArgs | ||
| from infinity_emb.primitives import EmbeddingReturnType, PoolingMethod | ||
| from infinity_emb.transformer.abstract import BaseEmbedder | ||
| from infinity_emb.transformer.quantization.interface import quant_embedding_decorator | ||
| from infinity_emb.transformer.utils_optimum import ( | ||
| cls_token_pooling, | ||
| device_to_onnx, | ||
| get_onnx_files, | ||
| mean_pooling, | ||
| normalize, | ||
| optimize_model, | ||
|
|
@@ -25,43 +28,79 @@ | |
| from optimum.onnxruntime import ( # type: ignore[import-untyped] | ||
| ORTModelForFeatureExtraction, | ||
| ) | ||
| from infinity_emb.transformer.utils_optimum import get_onnx_files | ||
|
|
||
| except (ImportError, RuntimeError, Exception) as ex: | ||
| CHECK_ONNXRUNTIME.mark_dirty(ex) | ||
|
|
||
|
|
||
| if CHECK_OPTIMUM_INTEL.is_available: | ||
| try: | ||
| from optimum.intel import OVModelForFeatureExtraction # type: ignore[import-untyped] | ||
| from infinity_emb.transformer.utils_optimum import get_openvino_files | ||
|
|
||
| except (ImportError, RuntimeError, Exception) as ex: | ||
| CHECK_OPTIMUM_INTEL.mark_dirty(ex) | ||
|
|
||
|
|
||
| if CHECK_TRANSFORMERS.is_available: | ||
| from transformers import AutoConfig, AutoTokenizer # type: ignore[import-untyped] | ||
|
|
||
|
|
||
| class OptimumEmbedder(BaseEmbedder): | ||
| def __init__(self, *, engine_args: EngineArgs): | ||
| CHECK_ONNXRUNTIME.mark_required() | ||
| provider = device_to_onnx(engine_args.device) | ||
|
|
||
| onnx_file = get_onnx_files( | ||
| model_name_or_path=engine_args.model_name_or_path, | ||
| revision=engine_args.revision, | ||
| use_auth_token=True, | ||
| prefer_quantized=("cpu" in provider.lower() or "openvino" in provider.lower()), | ||
| ) | ||
| if provider == "OpenVINOExecutionProvider": | ||
| CHECK_OPTIMUM_INTEL.mark_required() | ||
| filename = "" | ||
| try: | ||
| openvino_file = get_openvino_files( | ||
| model_name_or_path=engine_args.model_name_or_path, | ||
| revision=engine_args.revision, | ||
| use_auth_token=True, | ||
| ) | ||
| filename = openvino_file.as_posix() | ||
| except Exception as e: # show error then let the optimum intel compress on the fly | ||
| print(str(e)) | ||
|
Comment on lines
+64
to
+65
Contributor
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: Silently printing errors and continuing is dangerous. Consider logging the error and/or raising a more specific exception if OpenVINO file loading fails. |
||
|
|
||
| self.model = optimize_model( | ||
| model_name_or_path=engine_args.model_name_or_path, | ||
| revision=engine_args.revision, | ||
| trust_remote_code=engine_args.trust_remote_code, | ||
| execution_provider=provider, | ||
| file_name=filename, | ||
| optimize_model=not os.environ.get( | ||
| "INFINITY_ONNX_DISABLE_OPTIMIZE", False | ||
| ), # TODO: make this env variable public | ||
| model_class=OVModelForFeatureExtraction, | ||
| ) | ||
|
|
||
| else: | ||
| CHECK_ONNXRUNTIME.mark_required() | ||
| onnx_file = get_onnx_files( | ||
| model_name_or_path=engine_args.model_name_or_path, | ||
| revision=engine_args.revision, | ||
| use_auth_token=True, | ||
| prefer_quantized="cpu" in provider.lower(), | ||
| ) | ||
| self.model = optimize_model( | ||
| model_name_or_path=engine_args.model_name_or_path, | ||
| revision=engine_args.revision, | ||
| trust_remote_code=engine_args.trust_remote_code, | ||
| execution_provider=provider, | ||
| file_name=onnx_file.as_posix(), | ||
| optimize_model=not os.environ.get( | ||
| "INFINITY_ONNX_DISABLE_OPTIMIZE", False | ||
| ), # TODO: make this env variable public | ||
| model_class=ORTModelForFeatureExtraction, | ||
| ) | ||
| self.model.use_io_binding = False | ||
|
|
||
| self.pooling = ( | ||
| mean_pooling if engine_args.pooling_method == PoolingMethod.mean else cls_token_pooling | ||
| ) | ||
|
|
||
| self.model = optimize_model( | ||
| model_name_or_path=engine_args.model_name_or_path, | ||
| revision=engine_args.revision, | ||
| trust_remote_code=engine_args.trust_remote_code, | ||
| execution_provider=provider, | ||
| file_name=onnx_file.as_posix(), | ||
| optimize_model=not os.environ.get( | ||
| "INFINITY_ONNX_DISABLE_OPTIMIZE", False | ||
| ), # TODO: make this env variable public | ||
| model_class=ORTModelForFeatureExtraction, | ||
| ) | ||
| self.model.use_io_binding = False | ||
|
|
||
| self.tokenizer = AutoTokenizer.from_pretrained( | ||
| engine_args.model_name_or_path, | ||
| revision=engine_args.revision, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: Empty filename could cause issues if exception occurs. Initialize with None instead to make the failure case more explicit.