-
Notifications
You must be signed in to change notification settings - Fork 125
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
Convert tokenizers with openvino_tokenizers #500
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ | |||||
|
||||||
from requests.exceptions import ConnectionError as RequestsConnectionError | ||||||
from transformers import AutoConfig, AutoTokenizer | ||||||
from openvino import save_model | ||||||
|
||||||
from optimum.exporters import TasksManager | ||||||
from optimum.exporters.onnx import __main__ as optimum_main | ||||||
|
@@ -46,6 +47,24 @@ | |||||
logger = logging.getLogger(__name__) | ||||||
|
||||||
|
||||||
def tokenizer_export( | ||||||
tokenizer, | ||||||
output: Union[str, Path], | ||||||
suffix: Optional[str] = "" | ||||||
): | ||||||
try: | ||||||
from openvino_tokenizers import convert_tokenizer | ||||||
ov_tokenizer, ov_detokenizer = convert_tokenizer(tokenizer, with_detokenizer=True) | ||||||
if isinstance(output, str): | ||||||
output = Path(output) | ||||||
tokenizer_path = output.joinpath("openvino_tokenizer" + suffix + ".xml") | ||||||
detokenizer_path = output.joinpath("openvino_detokenizer" + suffix + ".xml") | ||||||
save_model(ov_tokenizer, tokenizer_path) | ||||||
save_model(ov_detokenizer, detokenizer_path) | ||||||
except Exception as exception: | ||||||
print("[ WARNING ] OpenVINO tokenizer/detokenizer models couldn't be exported because of exception:", exception) | ||||||
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.
Suggested change
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. Fixed. |
||||||
|
||||||
|
||||||
def main_export( | ||||||
model_name_or_path: str, | ||||||
output: Union[str, Path], | ||||||
|
@@ -328,6 +347,12 @@ class StoreAttr(object): | |||||
if generation_config is not None: | ||||||
generation_config.save_pretrained(output) | ||||||
maybe_save_preprocessors(model_name_or_path, output) | ||||||
try: | ||||||
# Avoid loding it for the second time if loaded before | ||||||
slyalin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) | ||||||
tokenizer_export(tokenizer, output) | ||||||
except: | ||||||
print("[ WARNING ] Could not load tokenizer using specified model ID or path. OpenVINO tokenizer/detokenizer models won't be generated.") | ||||||
|
||||||
if model.config.is_encoder_decoder and task.startswith("text-generation"): | ||||||
raise ValueError( | ||||||
|
@@ -358,10 +383,12 @@ class StoreAttr(object): | |||||
tokenizer = getattr(model, "tokenizer", None) | ||||||
if tokenizer is not None: | ||||||
tokenizer.save_pretrained(output.joinpath("tokenizer")) | ||||||
tokenizer_export(tokenizer, output) | ||||||
|
||||||
tokenizer_2 = getattr(model, "tokenizer_2", None) | ||||||
if tokenizer_2 is not None: | ||||||
tokenizer_2.save_pretrained(output.joinpath("tokenizer_2")) | ||||||
tokenizer_export(tokenizer, output, "_2") | ||||||
|
||||||
model.save_config(output) | ||||||
|
||||||
|
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.
I think it should be in convert.py file together with other conversion functions.
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.
Moved.