forked from gmalivenko/onnx2keras
-
Notifications
You must be signed in to change notification settings - Fork 0
Llama test sentiment analysis #194
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b41c79a
first version of working llama test with nference
NirBenZikri 2dccf07
refactored the code and removed unnecessary parts
NirBenZikri a7ac00a
removed unnecessary parts
NirBenZikri 59e538b
fixing versions
NirBenZikri 89fc671
fixing versions
NirBenZikri 7c979fe
FIXED THE CONDITION
NirBenZikri 5082afc
added optimum
NirBenZikri a176754
ignored test
NirBenZikri 2c5fa5a
ignored test
NirBenZikri 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import os.path | ||
|
|
||
| import onnx | ||
| import pytest | ||
| import tensorflow as tf | ||
| from transformers import AutoTokenizer | ||
| import numpy as np | ||
| from onnx2kerastl import onnx_to_keras | ||
| from keras_data_format_converter import convert_channels_first_to_last | ||
| from onnx2kerastl.customonnxlayer import onnx_custom_objects_map | ||
| from test.utils import export_torch_to_onnx_optimum | ||
|
|
||
|
|
||
| @pytest.mark.skip(reason="Fails on CI but works locally (might be too big?)") | ||
| def test_llama_32_1b_inst(): | ||
NirBenZikri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| onnx_model_folder = 'onnx_model' | ||
| onnx_path = os.path.join(onnx_model_folder, 'model.onnx') | ||
| model_name = "meta-llama/Llama-3.2-1B-Instruct" | ||
| # --------------------------------- Export to ONNX ------------------------------------- | ||
| export_torch_to_onnx_optimum(model_name, model_output_path=onnx_model_folder) | ||
| # ----------------------------------------- Input Preparation -------------------------- | ||
| tokenizer = AutoTokenizer.from_pretrained(model_name) | ||
| tokenizer.pad_token = tokenizer.eos_token | ||
| text = "i love this movie!" | ||
| prompt = tokenizer.apply_chat_template( | ||
| [{"role": "user", | ||
| "content": f"What is the sentiment of this sentence: \"{text}\"? Respond with 'positive' or 'negative' only."}], | ||
| add_generation_prompt=True, | ||
| return_tensors="np" | ||
| ) | ||
| input_ids = prompt | ||
| attention_mask = (input_ids != tokenizer.pad_token_id).astype(np.int64) | ||
| position_ids = np.arange(input_ids.shape[1])[None, :] | ||
| model_inputs = { | ||
| "input_ids": input_ids, | ||
| "attention_mask": attention_mask, | ||
| "position_ids": position_ids | ||
| } | ||
| keras_inputs = {k: tf.convert_to_tensor(v) for k, v in model_inputs.items()} | ||
| # --------------------------------- Export to Keras ------------------------------------- | ||
| onnx_model = onnx.load(onnx_path) # TODO: add to requirements, updated onnx==1.17.0 () | ||
| keras_model = onnx_to_keras(onnx_model, ['input_ids', 'attention_mask', 'position_ids'], | ||
| allow_partial_compilation=False) | ||
| keras_model = keras_model.converted_model | ||
| flipped_model = convert_channels_first_to_last(keras_model, []) | ||
| flipped_model.save('temp.h5') | ||
| model = tf.keras.models.load_model('temp.h5', custom_objects=onnx_custom_objects_map) | ||
| # --------------------------------- Evaluating Inference ------------------------------------- | ||
| outputs = model(keras_inputs) | ||
| last_token_logits = outputs[0, -1] | ||
| pred_token_id = np.argmax(last_token_logits) | ||
| pred_token = tokenizer.decode([pred_token_id]).strip().lower() | ||
|
|
||
| assert pred_token=='positive' | ||
|
|
||
| if __name__ == "__main__": | ||
| test_llama_32_1b_inst() | ||
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| torch>=1.1.0,<=1.5.0 | ||
| torchvision>=0.3.0,<=0.6.0 | ||
| optimum==1.23.3 | ||
| pytest | ||
| pytest-repeat |
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
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.
Uh oh!
There was an error while loading. Please reload this page.