-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathverify.py
72 lines (60 loc) · 2.37 KB
/
verify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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 TinyLlama-1.1B model."""
import logging
import pathlib
from absl import app
from absl import flags
from ai_edge_torch.generative.examples.tiny_llama import tiny_llama
from ai_edge_torch.generative.utilities import transformers_verifier
from ai_edge_torch.generative.utilities import verifier
import transformers
_PROMPTS = flags.DEFINE_multi_string(
"prompts",
"Show me the program to add 2 and 3.",
"The input prompts to generate answers.",
)
_MAX_NEW_TOKENS = flags.DEFINE_integer(
"max_new_tokens",
30,
"The maximum size of the generated tokens.",
)
def main(_):
checkpoint = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
logging.info("Loading the original model from: %s", 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
logging.info("Building the reauthored model from: %s", reauthored_checkpoint)
reauthored_model = tiny_llama.build_model(reauthored_checkpoint)
logging.info("Loading the tokenizer from: %s", checkpoint)
tokenizer = transformers.AutoTokenizer.from_pretrained(checkpoint)
verifier.verify_reauthored_model(
original_model=transformers_verifier.TransformersModelWrapper(
original_model
),
reauthored_model=verifier.ReauthoredModelWrapper(reauthored_model),
tokenizer=verifier.TokenizerWrapper(tokenizer),
generate_prompts=_PROMPTS.value,
max_new_tokens=_MAX_NEW_TOKENS.value,
atol=1e-04,
)
if __name__ == "__main__":
app.run(main)