This repository was archived by the owner on Jun 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 192
[Text Generation][V2] End-to-end tests #1402
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
596ef17
initial commit
dbogunowicz 03385ec
initial commit
dbogunowicz f19ee10
Merge remote-tracking branch 'origin/feature/damian/v2/fix_max_length…
dbogunowicz 1af45d9
its working now
dbogunowicz 804264d
beautification
dbogunowicz eff57a1
thank you Dipika <3
dbogunowicz 3d0c6bf
ready to review
dbogunowicz e116e88
Merge branch 'v2' into feature/damian/v2/e2e
dbogunowicz 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
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
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,13 @@ | ||
| # Copyright (c) 2021 - present / Neuralmagic, Inc. 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. |
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,6 @@ | ||
| cadence: "nightly" | ||
| model_path: "zoo:nlg/text_generation/codegen_mono-350m/pytorch/huggingface/bigpython_bigquery_thepile/base-none" | ||
| torch_model_name: "salesforce/codegen-350m-mono" | ||
| prompt: "\ndef Fibonacci(n):\n # Check if input is 0 then it will\n # print incorrect input" | ||
| precision: 0.0001 | ||
| internal_kv_cache: [True, False] |
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,6 @@ | ||
| cadence: "commit" | ||
| model_path: "hf:mgoin/TinyStories-1M-ds" | ||
| torch_model_name: "roneneldan/TinyStories-1M" | ||
| prompt: "Didn't know what time it was, the lights were low\n I leaned back on my radio" | ||
| precision: 0.001 | ||
| internal_kv_cache: [True, False] |
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,6 @@ | ||
| cadence: "nightly" | ||
| model_path: "zoo:nlg/text_generation/opt-1.3b/pytorch/huggingface/opt_pretrain/base-none" | ||
| torch_model_name: "facebook/opt-1.3b" | ||
| prompt: "Didn't know what time it was, the lights were low\n I leaned back on my radio" | ||
| precision: 0.0001 | ||
| internal_kv_cache: [True, False] |
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,137 @@ | ||
| # Copyright (c) 2021 - present / Neuralmagic, Inc. 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 | ||
| import os | ||
| from typing import Any, Dict, List, Tuple, Union | ||
|
|
||
| import numpy | ||
| import yaml | ||
| from transformers import AutoModelForCausalLM, AutoTokenizer | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| class TorchGroundTruthSource: | ||
| """ | ||
| An object that generates ground truth logits and | ||
| cache states from a prompt. This object can | ||
| generate tokens in an autoregressive manner, and thus | ||
| will output: | ||
| - prompt logits, | ||
| - generated logits, | ||
| - prompt cache state, | ||
| - generated sequence | ||
| """ | ||
|
|
||
| def __init__(self, num_tokens_to_generate: int, model_name: str): | ||
|
|
||
| self.model = AutoModelForCausalLM.from_pretrained(model_name) | ||
| self.tokenizer = self._create_tokenizer(model_name) | ||
|
|
||
| self.num_tokens_to_generate = num_tokens_to_generate | ||
|
|
||
| def tokenize(self, prompt: str): | ||
| return self.tokenizer(prompt, return_tensors="pt") | ||
|
|
||
| def __call__( | ||
| self, prompt: str | ||
| ) -> Tuple[numpy.ndarray, numpy.ndarray, List[numpy.ndarray], str]: | ||
| # afaik it is not possible to get 'past_key_values' from | ||
| # the generate method, so we have to run the model twice | ||
| out = self.model.generate( | ||
| self.tokenize(prompt).input_ids, | ||
| max_new_tokens=self.num_tokens_to_generate, | ||
| output_scores=True, | ||
| return_dict_in_generate=True, | ||
| use_cache=True, | ||
| ) | ||
| generated_text = self.tokenizer.decode( | ||
| out.sequences[0], skip_special_tokens=True | ||
| ) | ||
| generated_logits = numpy.concatenate( | ||
| [[score.numpy() for score in out.scores]] | ||
| ).transpose( | ||
| 1, 0, 2 | ||
| ) # (1, num_tokens_to_generate, vocab_size) | ||
|
|
||
| out = self.model(**self.tokenize(prompt)) | ||
| prompt_logits = out.logits.detach().numpy()[ | ||
| :, :-1, : | ||
| ] # (1, prompt_length, vocab_size) | ||
| prompt_cache = [ | ||
| entry.detach().numpy() | ||
| for key_value_tuple in out.past_key_values | ||
| for entry in key_value_tuple | ||
| ] # List[(1, num_heads, past_length, head_dim)] | ||
|
|
||
| return generated_logits, prompt_logits, prompt_cache, generated_text | ||
|
|
||
| @staticmethod | ||
| def _create_tokenizer(model_name): | ||
| tokenizer = AutoTokenizer.from_pretrained(model_name) | ||
| tokenizer.padding_side = "left" | ||
| if tokenizer.pad_token is None: | ||
| tokenizer.pad_token = tokenizer.eos_token | ||
|
|
||
| return tokenizer | ||
|
|
||
|
|
||
| def parse_params(configs_directory: str) -> List[Dict[str, Any]]: | ||
| # parses the config file provided | ||
| assert os.path.isdir( | ||
| configs_directory | ||
| ), f"Config_directory {configs_directory} is not a directory" | ||
|
|
||
| config_dicts = [] | ||
| for file in os.listdir(configs_directory): | ||
| if file.endswith(".yaml"): | ||
| config_path = os.path.join(configs_directory, file) | ||
| # reads the yaml file | ||
| with open(config_path, "r") as f: | ||
| config = yaml.safe_load(f) | ||
|
|
||
| cadence = os.environ.get("CADENCE", "commit") | ||
| expected_cadence = config["cadence"] | ||
|
|
||
| if not isinstance(expected_cadence, list): | ||
| expected_cadence = [expected_cadence] | ||
| if cadence in expected_cadence: | ||
| config_dicts.append(config) | ||
| else: | ||
| logging.info( | ||
| f"Skipping testing model: {config['model_path']} " | ||
| f"for cadence: {config['cadence']}" | ||
| ) | ||
| else: | ||
| raise FileNotFoundError( | ||
| f"Could not find a yaml file in {configs_directory}" | ||
| ) | ||
| return config_dicts | ||
|
|
||
|
|
||
| def validate_internal_kv_cache( | ||
| internal_kv_cache, available_kv_cache_types: Union[str, List[str]] | ||
| ) -> bool: | ||
| if internal_kv_cache and True not in available_kv_cache_types: | ||
| pytest.skip( | ||
| "The tests for running the pipeline with " | ||
| "internal kv cache management are disabled." | ||
| ) | ||
| if not internal_kv_cache and False not in available_kv_cache_types: | ||
| pytest.skip( | ||
| "The tests for running the pipeline with " | ||
| "external kv cache management are disabled." | ||
| ) | ||
| return internal_kv_cache |
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.
Uh oh!
There was an error while loading. Please reload this page.