-
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
Add openvino export configs and support chatglm #454
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,23 @@ | ||
# Copyright 2022 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. | ||
|
||
from .__main__ import main_export | ||
from .base import init_model_configs | ||
from .convert import export, export_models, export_pytorch_via_onnx | ||
from .model_configs import * | ||
|
||
|
||
init_model_configs() | ||
|
||
__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,38 @@ | ||
# Copyright 2022 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. | ||
from copy import deepcopy | ||
from typing import Callable, Type | ||
|
||
from optimum.exporters.tasks import TasksManager | ||
from optimum.utils.normalized_config import NormalizedConfigManager | ||
|
||
|
||
def init_model_configs(): | ||
suppored_models = TasksManager._SUPPORTED_MODEL_TYPE | ||
for model, export_configs in suppored_models.items(): | ||
if "onnx" not in export_configs: | ||
continue | ||
TasksManager._SUPPORTED_MODEL_TYPE[model]["openvino"] = deepcopy( | ||
TasksManager._SUPPORTED_MODEL_TYPE[model]["onnx"] | ||
) | ||
|
||
|
||
def register_normalized_config(model_type: str) -> Callable[[Type], Type]: | ||
def decorator(config_cls: Type) -> Type: | ||
if model_type in NormalizedConfigManager._conf: | ||
return config_cls | ||
NormalizedConfigManager._conf[model_type] = config_cls | ||
return config_cls | ||
|
||
return decorator |
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,87 @@ | ||
# Copyright 2022 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. | ||
|
||
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. can be moved to |
||
from typing import Optional, Tuple | ||
|
||
import torch | ||
|
||
from optimum.utils import ( | ||
DEFAULT_DUMMY_SHAPES, | ||
DummyPastKeyValuesGenerator, | ||
DummyTextInputGenerator, | ||
NormalizedTextConfig, | ||
) | ||
|
||
|
||
class ChatGLN2DummyTextInputGenerator(DummyTextInputGenerator): | ||
SUPPORTED_INPUT_NAMES = { | ||
"input_ids", | ||
"attention_mask", | ||
"token_type_ids", | ||
"position_ids", | ||
} | ||
|
||
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"): | ||
input = super().generate(input_name, framework, int_dtype, float_dtype) | ||
if input_name == "attention_mask": | ||
input = torch.ones((input.shape[0], input.shape[1] + 1), dtype=input.dtype) | ||
# input[0] = 0 | ||
if input_name == "position_ids": | ||
input = torch.range(0, input.shape[1] + 1, dtype=input.dtype).repeat(1, 1) | ||
# input[0] = 0 | ||
return input | ||
|
||
|
||
class ChatGLM2DummyPastKeyValuesGenerator(DummyPastKeyValuesGenerator): | ||
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. I think we can move it to optimum directly as it's not specific to OpenVINO |
||
def __init__( | ||
self, | ||
task: str, | ||
normalized_config: NormalizedTextConfig, | ||
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"], | ||
sequence_length: int = DEFAULT_DUMMY_SHAPES["sequence_length"], | ||
random_batch_size_range: Optional[Tuple[int, int]] = None, | ||
random_sequence_length_range: Optional[Tuple[int, int]] = None, | ||
**kwargs, | ||
): | ||
super().__init__( | ||
task=task, | ||
normalized_config=normalized_config, | ||
batch_size=batch_size, | ||
sequence_length=sequence_length, | ||
random_batch_size_range=random_batch_size_range, | ||
random_sequence_length_range=random_sequence_length_range, | ||
) | ||
self.multi_query_group_num = normalized_config.multi_query_group_num | ||
self.head_dim = self.hidden_size // self.num_attention_heads | ||
|
||
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"): | ||
past_key_shape = ( | ||
self.sequence_length, | ||
self.batch_size, | ||
self.multi_query_group_num, | ||
self.head_dim, | ||
) | ||
past_value_shape = ( | ||
self.sequence_length, | ||
self.batch_size, | ||
self.multi_query_group_num, | ||
self.head_dim, | ||
) | ||
return [ | ||
( | ||
self.random_float_tensor(past_key_shape, framework=framework, dtype=float_dtype), | ||
self.random_float_tensor(past_value_shape, framework=framework, dtype=float_dtype), | ||
) | ||
for _ in range(self.num_layers) | ||
] |
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.
here I would prefer we keep the onnx config so that we don't duplicate code as I'm unsure why we would need it for now, is there a specific reason for that @eaidova ?
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.
the main idea of this pr to allow override configs for onnx for openvino.
I see 2 reasons for that:
We do not duplicate code, (for majority cases configs mapping filled in runtime, just reusing the same onnx config, but if we have own one for openvino specifc, we will use own) just allow overriding some export configs if it is applicable.