-
Notifications
You must be signed in to change notification settings - Fork 115
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
Stateful models by default #486
Closed
Closed
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
d41110c
Allow loading of stateful models (no patching yet)
slyalin bcb7cac
Stateful models support
slyalin d4c165b
Fix forward for chatglm
slyalin a62dc6f
Passing stateful as a dedicated parameter
slyalin 403adb5
Fixed possibly misaligned types in ShapeOf Concat sub-expression
slyalin 6097bfd
Merge remote-tracking branch 'origin/main' into stateful
slyalin c54a466
Fixed critical typo in infer_request invocation
slyalin 6151bec
Merge remote-tracking branch 'origin/main' into stateful
slyalin 0e5aefc
Apply bettertransfomer when model is converted in stateful mode
slyalin 5087f92
Correct default value handling for stateful flag
slyalin 5ae6d5c
Apply bettertransformer under try-except to avoid crashes when model …
slyalin ddb182e
Added --stateful option in optimum-cli
slyalin 8e9a7e0
Merge remote-tracking branch 'origin/main' into stateful
slyalin a51ab27
Raise if too old version of opevino is used ans stateful=True
slyalin 6df798a
Fix openvino version check to be compatible with openvino-nightly
slyalin fa33784
Merged from recent main branch
slyalin aa299e6
Fix for bloom family
slyalin 70ff804
Allow loading of stateful models (no patching yet)
slyalin bf45cc7
Stateful models support
slyalin fafc040
Fix forward for chatglm
slyalin e4585fe
Passing stateful as a dedicated parameter
slyalin a5c48c4
Fixed possibly misaligned types in ShapeOf Concat sub-expression
slyalin 3ce4fe9
Fixed critical typo in infer_request invocation
slyalin 00bd0b3
Apply bettertransfomer when model is converted in stateful mode
slyalin 489799a
Correct default value handling for stateful flag
slyalin 312f24f
Apply bettertransformer under try-except to avoid crashes when model …
slyalin 4bca339
Added --stateful option in optimum-cli
slyalin a5f8558
Raise if too old version of opevino is used ans stateful=True
slyalin 2763639
Fix openvino version check to be compatible with openvino-nightly
slyalin f5da152
Fix for bloom family
slyalin da62d99
Fix general code style and appliy renaming suggestions
eaidova 33f86dc
fix version checking if openvino not in site-packages
eaidova 7d15415
use reset_stateif available
eaidova 0dfbb63
remove input patch in bettertransformer apply
eaidova 09bbe7b
add tests
eaidova e70a50d
add type hints and update doc strings
eaidova a57c86a
added more tests
eaidova 67deae2
Merge remote-tracking branch 'origin/main' into ea/stateful
slyalin 89ba0cb
Fixed outdated signature of InferRequest wrapper to fix one of the qu…
slyalin a85960a
Merge remote-tracking branch 'slyalin/stateful' into stateful
slyalin dcfcc2a
Merge remote-tracking branch 'origin/main' into stateful
slyalin df0e727
Switch to stateful model by default
slyalin 9992419
Merge remote-tracking branch 'origin/main' into stateful
slyalin 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 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 patch_stateful, raise_if_openvino_is_too_old | ||
|
||
|
||
__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
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=Flase" | ||
) | ||
# 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 |
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
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.
should we also check task before applying patch? for making this transformation, the model should have with_past in task type and if I right understand current changes are targeted only for text-generation-with-past, there is also seq2seq models with decoder with past as @AlexKoff88 already mentioned in comment, where this transformation should be applied on only one from 3 models
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.
As it is implemented now, it is a user responsibility to set stateful=True only for right models. To set it by default there should be more adjustments in the code, which I cannot provide in this PR. Part of the models will not be supported. I am just not so familiar with the code base to provide necessary changes to enable by default. We need someone who can do that.