-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OPenVINO stateful model support (#493)
* Allow loading of stateful models (no patching yet) * Stateful models support * Fix forward for chatglm * Passing stateful as a dedicated parameter * Fixed possibly misaligned types in ShapeOf Concat sub-expression * Fixed critical typo in infer_request invocation * Apply bettertransfomer when model is converted in stateful mode * Correct default value handling for stateful flag * Apply bettertransformer under try-except to avoid crashes when model is not supported * Added --stateful option in optimum-cli * Raise if too old version of opevino is used ans stateful=True * Fix openvino version check to be compatible with openvino-nightly * Fix for bloom family * Allow loading of stateful models (no patching yet) * Stateful models support * Fix forward for chatglm * Passing stateful as a dedicated parameter * Fixed possibly misaligned types in ShapeOf Concat sub-expression * Fixed critical typo in infer_request invocation * Apply bettertransfomer when model is converted in stateful mode * Correct default value handling for stateful flag * Apply bettertransformer under try-except to avoid crashes when model is not supported * Added --stateful option in optimum-cli * Raise if too old version of opevino is used ans stateful=True * Fix openvino version check to be compatible with openvino-nightly * Fix for bloom family * Fix general code style and appliy renaming suggestions * fix version checking if openvino not in site-packages * use reset_stateif available * remove input patch in bettertransformer apply * add tests * add type hints and update doc strings * added more tests * Fixed outdated signature of InferRequest wrapper to fix one of the quantizer tests. * Switch to stateful model by default * Allow loading of stateful models (no patching yet) * Stateful models support * Fix forward for chatglm * Passing stateful as a dedicated parameter * Fixed possibly misaligned types in ShapeOf Concat sub-expression * Apply bettertransfomer when model is converted in stateful mode * Correct default value handling for stateful flag * Apply bettertransformer under try-except to avoid crashes when model is not supported * Added --stateful option in optimum-cli * Raise if too old version of opevino is used ans stateful=True * Fix openvino version check to be compatible with openvino-nightly * Fix for bloom family * Fix general code style and appliy renaming suggestions * fix version checking if openvino not in site-packages * use reset_stateif available * remove input patch in bettertransformer apply * add tests * add type hints and update doc strings * added more tests * Fixed outdated signature of InferRequest wrapper to fix one of the quantizer tests. * Stateful models support * Fix forward for chatglm * Passing stateful as a dedicated parameter * Apply bettertransfomer when model is converted in stateful mode * Raise if too old version of opevino is used ans stateful=True * Fix openvino version check to be compatible with openvino-nightly * Fix for bloom family * fix test and add beam_idx attribute * apply review comments * stateful by default fixes * less agressive stateful * ensure that task support stateful * remove debug print * Apply suggestions from code review Co-authored-by: Sergey Lyalin <[email protected]> * Apply suggestions from code review Co-authored-by: Helena Kloosterman <[email protected]> * update requirements and warning messages * Apply suggestions from code review Co-authored-by: Ella Charlaix <[email protected]> * fix cli export * Update optimum/exporters/openvino/__main__.py Co-authored-by: Ella Charlaix <[email protected]> --------- Co-authored-by: Sergey Lyalin <[email protected]> Co-authored-by: Helena Kloosterman <[email protected]> Co-authored-by: Ella Charlaix <[email protected]>
- Loading branch information
1 parent
133aa7d
commit 7f236c2
Showing
14 changed files
with
573 additions
and
91 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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from .__main__ import main_export | ||
from .convert import export, export_models, export_pytorch_via_onnx | ||
from .stateful import ensure_stateful_is_available, patch_stateful | ||
|
||
|
||
__all__ = ["main_export", "export", "export_models"] |
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,39 @@ | ||
# Copyright 2023 The HuggingFace Team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging as log | ||
|
||
from optimum.intel.utils.import_utils import is_torch_version | ||
|
||
|
||
def patch_model_with_bettertransformer(model): | ||
if is_torch_version("<", "2.0"): | ||
log.warn( | ||
"integration Scaled Dot Product Attention optimization supported only with torch > 2.0." | ||
"Usage model with stateful=True may be non-effective if model does not contain torch.functional.scaled_dot_product_attention" | ||
"It is recommended to upgrade PyTorch version for using stateful model or use stateful=False" | ||
) | ||
# model already has required SDPA implementation | ||
if getattr(model, "_supports_sdpa", False) and getattr(model.config, "_attn_implementation", "eager") == "sdpa": | ||
return model | ||
try: | ||
model = model.to_bettertransformer() | ||
except Exception as e: | ||
log.warn( | ||
f"Cannot apply model.to_bettertransformer because of the exception:\n{e}." | ||
" Usage model with stateful=True may be non-effective if model does not contain torch.functional.scaled_dot_product_attention" | ||
) | ||
return model | ||
|
||
return model |
Oops, something went wrong.