-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify some examples re-authored from transformers package.
- Compare outputs from the re-authored model against ones from the original model on the fly * logits from an arbitrary input token * answer from a prompt string - Add a flag to change normalized shape - Update re-authored phi-2 model - Use absl.app to run verify.py files - Remove unnecessary lm_logits.pt files PiperOrigin-RevId: 676022650
- Loading branch information
1 parent
8593517
commit 586e9ff
Showing
17 changed files
with
478 additions
and
141 deletions.
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
Binary file not shown.
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,61 @@ | ||
# Copyright 2024 The AI Edge Torch Authors. | ||
# | ||
# 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. | ||
# ============================================================================== | ||
|
||
"""Verifies the reauthored OpenELM-3B model.""" | ||
|
||
import pathlib | ||
|
||
from absl import app | ||
from absl import flags | ||
from ai_edge_torch.generative.examples.openelm import openelm | ||
from ai_edge_torch.generative.utilities import verifier | ||
import transformers | ||
|
||
_PROMPTS = flags.DEFINE_multi_string( | ||
"prompts", | ||
"What is the meaning of life?", | ||
"The input prompts to generate answers.", | ||
) | ||
|
||
|
||
def main(_): | ||
checkpoint = "apple/OpenELM-3B" | ||
verifier.log_msg("Loading the original model from", checkpoint) | ||
original_model = transformers.AutoModelForCausalLM.from_pretrained( | ||
checkpoint, trust_remote_code=True | ||
) | ||
|
||
# Locate the cached dir. | ||
cached_config_file = transformers.utils.cached_file( | ||
checkpoint, transformers.utils.CONFIG_NAME | ||
) | ||
reauthored_checkpoint = pathlib.Path(cached_config_file).parent | ||
verifier.log_msg("Building the reauthored model from", reauthored_checkpoint) | ||
reauthored_model = openelm.build_model(reauthored_checkpoint) | ||
|
||
tokenizer_checkpoint = "meta-llama/Llama-2-7b-hf" | ||
verifier.log_msg("Loading the tokenizer from", tokenizer_checkpoint) | ||
tokenizer = transformers.AutoTokenizer.from_pretrained(tokenizer_checkpoint) | ||
|
||
verifier.verify_reauthored_model( | ||
original_model=original_model, | ||
reauthored_model=reauthored_model, | ||
tokenizer=tokenizer, | ||
prompts=_PROMPTS.value, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(main) |
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
Binary file not shown.
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,53 @@ | ||
# Copyright 2024 The AI Edge Torch Authors. | ||
# | ||
# 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. | ||
# ============================================================================== | ||
|
||
"""Verifies the reauthored Phi-2 model.""" | ||
|
||
from absl import app | ||
from absl import flags | ||
from ai_edge_torch.generative.examples.phi import phi2 | ||
from ai_edge_torch.generative.utilities import verifier | ||
import kagglehub | ||
import transformers | ||
|
||
_PROMPTS = flags.DEFINE_multi_string( | ||
"prompts", | ||
"What is the meaning of life?", | ||
"The input prompts to generate answers.", | ||
) | ||
|
||
|
||
def main(_): | ||
checkpoint = kagglehub.model_download("Microsoft/phi/transformers/2") | ||
verifier.log_msg("Loading the original model from", checkpoint) | ||
original_model = transformers.AutoModelForCausalLM.from_pretrained(checkpoint) | ||
|
||
verifier.log_msg("Building the reauthored model from", checkpoint) | ||
reauthored_model = phi2.build_model(checkpoint) | ||
|
||
verifier.log_msg("Loading the tokenizer from", checkpoint) | ||
tokenizer = transformers.AutoTokenizer.from_pretrained(checkpoint) | ||
|
||
verifier.verify_reauthored_model( | ||
original_model=original_model, | ||
reauthored_model=reauthored_model, | ||
tokenizer=tokenizer, | ||
prompts=_PROMPTS.value, | ||
atol=1e-03, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(main) |
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
Binary file not shown.
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,59 @@ | ||
# Copyright 2024 The AI Edge Torch Authors. | ||
# | ||
# 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. | ||
# ============================================================================== | ||
|
||
"""Verifies the reauthored SmolLM-135M model.""" | ||
|
||
import pathlib | ||
|
||
from absl import app | ||
from absl import flags | ||
from ai_edge_torch.generative.examples.smollm import smollm | ||
from ai_edge_torch.generative.utilities import verifier | ||
import transformers | ||
|
||
_PROMPTS = flags.DEFINE_multi_string( | ||
"prompts", | ||
"What is the meaning of life?", | ||
"The input prompts to generate answers.", | ||
) | ||
|
||
|
||
def main(_): | ||
checkpoint = "HuggingFaceTB/SmolLM-135M" | ||
verifier.log_msg("Loading the original model from", checkpoint) | ||
original_model = transformers.AutoModelForCausalLM.from_pretrained(checkpoint) | ||
|
||
# Locate the cached dir. | ||
cached_config_file = transformers.utils.cached_file( | ||
checkpoint, transformers.utils.CONFIG_NAME | ||
) | ||
reauthored_checkpoint = pathlib.Path(cached_config_file).parent | ||
verifier.log_msg("Building the reauthored model from", reauthored_checkpoint) | ||
reauthored_model = smollm.build_model(reauthored_checkpoint) | ||
|
||
verifier.log_msg("Loading the tokenizer from", checkpoint) | ||
tokenizer = transformers.AutoTokenizer.from_pretrained(checkpoint) | ||
|
||
verifier.verify_reauthored_model( | ||
original_model=original_model, | ||
reauthored_model=reauthored_model, | ||
tokenizer=tokenizer, | ||
prompts=_PROMPTS.value, | ||
atol=1e-04, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(main) |
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
Binary file removed
BIN
-126 KB
ai_edge_torch/generative/examples/tiny_llama/tiny_llama_lm_logits.pt
Binary file not shown.
Oops, something went wrong.