ai-edge-torch-nightly 0.3.0.dev20240924__py3-none-any.whl → 0.3.0.dev20240928__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- ai_edge_torch/generative/examples/gemma/gemma1.py +2 -6
- ai_edge_torch/generative/examples/gemma/gemma2.py +2 -10
- ai_edge_torch/generative/examples/gemma/verify_gemma1.py +3 -2
- ai_edge_torch/generative/examples/gemma/verify_gemma2.py +3 -2
- ai_edge_torch/generative/examples/gemma/verify_util.py +15 -25
- ai_edge_torch/generative/examples/llama/__init__.py +14 -0
- ai_edge_torch/generative/examples/llama/convert_3b_to_tflite.py +68 -0
- ai_edge_torch/generative/examples/llama/convert_to_tflite.py +68 -0
- ai_edge_torch/generative/examples/llama/llama.py +204 -0
- ai_edge_torch/generative/examples/llama/verify.py +73 -0
- ai_edge_torch/generative/examples/llama/verify_3b.py +73 -0
- ai_edge_torch/generative/examples/openelm/openelm.py +2 -6
- ai_edge_torch/generative/examples/openelm/verify.py +19 -11
- ai_edge_torch/generative/examples/phi/convert_phi3_to_tflite.py +68 -0
- ai_edge_torch/generative/examples/phi/phi2.py +2 -6
- ai_edge_torch/generative/examples/phi/phi3.py +279 -0
- ai_edge_torch/generative/examples/phi/verify.py +13 -13
- ai_edge_torch/generative/examples/phi/verify_phi3.py +69 -0
- ai_edge_torch/generative/examples/smollm/smollm.py +1 -0
- ai_edge_torch/generative/examples/smollm/verify.py +19 -9
- ai_edge_torch/generative/examples/stable_diffusion/clip.py +54 -1
- ai_edge_torch/generative/examples/stable_diffusion/decoder.py +58 -0
- ai_edge_torch/generative/examples/stable_diffusion/diffusion.py +71 -1
- ai_edge_torch/generative/examples/t5/t5.py +0 -2
- ai_edge_torch/generative/examples/test_models/convert_toy_model.py +105 -0
- ai_edge_torch/generative/examples/test_models/toy_model.py +7 -41
- ai_edge_torch/generative/examples/test_models/toy_model_with_kv_cache.py +5 -61
- ai_edge_torch/generative/examples/tiny_llama/tiny_llama.py +2 -6
- ai_edge_torch/generative/examples/tiny_llama/verify.py +20 -10
- ai_edge_torch/generative/layers/model_config.py +2 -0
- ai_edge_torch/generative/layers/normalization.py +2 -2
- ai_edge_torch/generative/layers/unet/blocks_2d.py +2 -2
- ai_edge_torch/generative/test/test_model_conversion_large.py +129 -0
- ai_edge_torch/generative/utilities/transformers_verifier.py +42 -0
- ai_edge_torch/generative/utilities/verifier.py +130 -114
- ai_edge_torch/version.py +1 -1
- {ai_edge_torch_nightly-0.3.0.dev20240924.dist-info → ai_edge_torch_nightly-0.3.0.dev20240928.dist-info}/METADATA +1 -1
- {ai_edge_torch_nightly-0.3.0.dev20240924.dist-info → ai_edge_torch_nightly-0.3.0.dev20240928.dist-info}/RECORD +41 -30
- {ai_edge_torch_nightly-0.3.0.dev20240924.dist-info → ai_edge_torch_nightly-0.3.0.dev20240928.dist-info}/LICENSE +0 -0
- {ai_edge_torch_nightly-0.3.0.dev20240924.dist-info → ai_edge_torch_nightly-0.3.0.dev20240928.dist-info}/WHEEL +0 -0
- {ai_edge_torch_nightly-0.3.0.dev20240924.dist-info → ai_edge_torch_nightly-0.3.0.dev20240928.dist-info}/top_level.txt +0 -0
@@ -69,15 +69,10 @@ class Gemma(nn.Module):
|
|
69
69
|
self.rope_cache = attn_utils.build_rope_cache(
|
70
70
|
size=config.kv_cache_max,
|
71
71
|
dim=int(attn_config.rotary_percentage * attn_config.head_dim),
|
72
|
-
base=
|
73
|
-
condense_ratio=1,
|
74
|
-
dtype=torch.float32,
|
75
|
-
device=torch.device("cpu"),
|
72
|
+
base=attn_config.rotary_base,
|
76
73
|
)
|
77
74
|
self.mask_cache = attn_utils.build_causal_mask_cache(
|
78
75
|
size=config.kv_cache_max,
|
79
|
-
dtype=torch.float32,
|
80
|
-
device=torch.device("cpu"),
|
81
76
|
)
|
82
77
|
self.config = config
|
83
78
|
|
@@ -135,6 +130,7 @@ def get_model_config_2b(kv_cache_max_len: int = 1024) -> cfg.ModelConfig:
|
|
135
130
|
num_heads=8,
|
136
131
|
head_dim=256,
|
137
132
|
num_query_groups=1,
|
133
|
+
rotary_base=10000,
|
138
134
|
rotary_percentage=1.0,
|
139
135
|
)
|
140
136
|
ff_config = cfg.FeedForwardConfig(
|
@@ -16,7 +16,6 @@
|
|
16
16
|
"""Example of building a Gemma2 model."""
|
17
17
|
|
18
18
|
import os
|
19
|
-
import pathlib
|
20
19
|
from typing import Optional, Tuple
|
21
20
|
|
22
21
|
from ai_edge_torch.generative.layers import attention
|
@@ -25,7 +24,6 @@ from ai_edge_torch.generative.layers import kv_cache as kv_utils
|
|
25
24
|
import ai_edge_torch.generative.layers.attention_utils as attn_utils
|
26
25
|
import ai_edge_torch.generative.layers.model_config as cfg
|
27
26
|
import ai_edge_torch.generative.utilities.loader as loading_utils
|
28
|
-
import numpy as np
|
29
27
|
import torch
|
30
28
|
from torch import nn
|
31
29
|
|
@@ -111,21 +109,14 @@ class Gemma2(nn.Module):
|
|
111
109
|
self.rope_cache = attn_utils.build_rope_cache(
|
112
110
|
size=config.kv_cache_max,
|
113
111
|
dim=int(attn_config.rotary_percentage * attn_config.head_dim),
|
114
|
-
base=
|
115
|
-
condense_ratio=1,
|
116
|
-
dtype=torch.float32,
|
117
|
-
device=torch.device("cpu"),
|
112
|
+
base=attn_config.rotary_base,
|
118
113
|
)
|
119
114
|
self.mask_cache = attn_utils.build_causal_mask_cache(
|
120
115
|
size=config.kv_cache_max,
|
121
|
-
dtype=torch.float32,
|
122
|
-
device=torch.device("cpu"),
|
123
116
|
)
|
124
117
|
self.sliding_window_mask_cache = attn_utils.build_sliding_window_mask_cache(
|
125
118
|
size=config.kv_cache_max,
|
126
119
|
window_size=attn_config.sliding_window_size,
|
127
|
-
dtype=torch.float32,
|
128
|
-
device=torch.device("cpu"),
|
129
120
|
)
|
130
121
|
self.config = config
|
131
122
|
|
@@ -210,6 +201,7 @@ def get_model_config_2b(kv_cache_max_len: int = 1024) -> cfg.ModelConfig:
|
|
210
201
|
num_heads=8,
|
211
202
|
head_dim=256,
|
212
203
|
num_query_groups=4,
|
204
|
+
rotary_base=10000,
|
213
205
|
rotary_percentage=1.0,
|
214
206
|
qkv_transpose_before_split=True,
|
215
207
|
logit_softcap=50.0,
|
@@ -15,13 +15,14 @@
|
|
15
15
|
|
16
16
|
"""Verifies the reauthored Gemma1 model."""
|
17
17
|
|
18
|
+
import logging
|
18
19
|
from absl import app
|
19
20
|
from absl import flags
|
20
21
|
from ai_edge_torch.generative.examples.gemma import gemma1
|
21
22
|
from ai_edge_torch.generative.examples.gemma import verify_util
|
22
|
-
from ai_edge_torch.generative.utilities import verifier
|
23
23
|
import kagglehub
|
24
24
|
|
25
|
+
|
25
26
|
_PROMPTS = flags.DEFINE_multi_string(
|
26
27
|
"prompts",
|
27
28
|
"What is the meaning of life?",
|
@@ -37,7 +38,7 @@ _MAX_NEW_TOKENS = flags.DEFINE_integer(
|
|
37
38
|
def main(_):
|
38
39
|
checkpoint = kagglehub.model_download("google/gemma/pyTorch/2b-it")
|
39
40
|
|
40
|
-
|
41
|
+
logging.info("Building the reauthored model from: %s", checkpoint)
|
41
42
|
reauthored_model = gemma1.build_2b_model(checkpoint)
|
42
43
|
|
43
44
|
verify_util.verify_reauthored_gemma_model(
|
@@ -15,13 +15,14 @@
|
|
15
15
|
|
16
16
|
"""Verifies the reauthored Gemma2 model."""
|
17
17
|
|
18
|
+
import logging
|
18
19
|
from absl import app
|
19
20
|
from absl import flags
|
20
21
|
from ai_edge_torch.generative.examples.gemma import gemma2
|
21
22
|
from ai_edge_torch.generative.examples.gemma import verify_util
|
22
|
-
from ai_edge_torch.generative.utilities import verifier
|
23
23
|
import kagglehub
|
24
24
|
|
25
|
+
|
25
26
|
_PROMPTS = flags.DEFINE_multi_string(
|
26
27
|
"prompts",
|
27
28
|
"What is the meaning of life?",
|
@@ -37,7 +38,7 @@ _MAX_NEW_TOKENS = flags.DEFINE_integer(
|
|
37
38
|
def main(_):
|
38
39
|
checkpoint = kagglehub.model_download("google/gemma-2/pyTorch/gemma-2-2b-it")
|
39
40
|
|
40
|
-
|
41
|
+
logging.info("Building the reauthored model from: %s", checkpoint)
|
41
42
|
reauthored_model = gemma2.build_2b_model(checkpoint)
|
42
43
|
|
43
44
|
verify_util.verify_reauthored_gemma_model(
|
@@ -15,7 +15,7 @@
|
|
15
15
|
|
16
16
|
"""Utility functions to verify the reauthored Gemma model."""
|
17
17
|
|
18
|
-
import
|
18
|
+
import logging
|
19
19
|
import os
|
20
20
|
from typing import List, Tuple
|
21
21
|
|
@@ -26,26 +26,17 @@ from gemma import model as gemma_model
|
|
26
26
|
import torch
|
27
27
|
|
28
28
|
|
29
|
-
@dataclasses.dataclass
|
30
|
-
class _Output:
|
31
|
-
logits: torch.Tensor
|
32
|
-
|
33
|
-
|
34
29
|
class GemmaWrapper(verifier.ModelWrapper):
|
35
30
|
"""Gemma model wrapper for verification.
|
36
31
|
|
37
32
|
Verifier calls model.forward() with maxium sequence length (1024) expecting
|
38
|
-
the output
|
39
|
-
|
33
|
+
the output is logits while Gemma gets the input tokens with the actual length
|
34
|
+
and returns logits in a tuple.
|
40
35
|
|
41
36
|
Verifier runs tokenizer before model.generate() while Gemma runs the tokenizer
|
42
37
|
inside model.generate().
|
43
38
|
"""
|
44
39
|
|
45
|
-
def __init__(self, model: torch.nn.Module, max_new_tokens: int):
|
46
|
-
super().__init__(model)
|
47
|
-
self.max_new_tokens = max_new_tokens
|
48
|
-
|
49
40
|
def _get_actual_input_len(self, tokens: torch.Tensor) -> int:
|
50
41
|
for i in range(tokens.shape[1]):
|
51
42
|
if tokens[0, i] == 0:
|
@@ -62,7 +53,7 @@ class GemmaWrapper(verifier.ModelWrapper):
|
|
62
53
|
(cache.clone(), cache.clone()) for _ in range(config.num_hidden_layers)
|
63
54
|
]
|
64
55
|
|
65
|
-
def forward(self, tokens: torch.Tensor) ->
|
56
|
+
def forward(self, tokens: torch.Tensor) -> torch.Tensor:
|
66
57
|
"""Forwards the model after reducing input tokens to the actual length."""
|
67
58
|
actual_input_len = self._get_actual_input_len(tokens)
|
68
59
|
input_pos = torch.arange(0, actual_input_len, dtype=torch.long)
|
@@ -78,28 +69,26 @@ class GemmaWrapper(verifier.ModelWrapper):
|
|
78
69
|
top_ps=torch.tensor([1.0], dtype=torch.float),
|
79
70
|
top_ks=torch.tensor([1], dtype=torch.long),
|
80
71
|
)
|
81
|
-
return
|
72
|
+
return logits
|
82
73
|
|
83
|
-
def generate(
|
74
|
+
def generate(
|
75
|
+
self, tokens: torch.Tensor, max_new_tokens: int
|
76
|
+
) -> torch.IntTensor:
|
84
77
|
"""Generates the response after decoding the tokens into a string."""
|
85
78
|
prompts = self.model.tokenizer.decode(tokens[0].tolist())
|
86
79
|
response = self.model.generate(
|
87
|
-
prompts, device="cpu", output_len=
|
80
|
+
prompts, device="cpu", output_len=max_new_tokens, top_k=1
|
88
81
|
)
|
89
82
|
return torch.tensor([self.model.tokenizer.encode(prompts + response)])
|
90
83
|
|
91
84
|
|
92
|
-
class
|
85
|
+
class GemmaTokenizerWrapper(verifier.TokenizerWrapper):
|
93
86
|
"""Tokenizer wrapper for verification.
|
94
87
|
|
95
88
|
Verifier expects the tokenizer to handle tokens in torch.Tensor while Gemma
|
96
89
|
tokenizer expects tokens in a list.
|
97
90
|
"""
|
98
91
|
|
99
|
-
def __init__(self, tokenizer: torch.nn.Module):
|
100
|
-
super().__init__()
|
101
|
-
self.tokenizer = tokenizer
|
102
|
-
|
103
92
|
def encode(self, text: str, **_) -> torch.Tensor:
|
104
93
|
"""Adds one more dimension to the output of the tokenizer."""
|
105
94
|
return torch.tensor([self.tokenizer.encode(text)])
|
@@ -127,15 +116,16 @@ def verify_reauthored_gemma_model(
|
|
127
116
|
# Use float32 to be compatible with the reauthored model.
|
128
117
|
config.dtype = torch.float32
|
129
118
|
|
130
|
-
|
119
|
+
logging.info("Loading the original model from: %s", checkpoint)
|
131
120
|
original_model = gemma_model.GemmaForCausalLM(config).eval()
|
132
121
|
original_model.load_weights(os.path.join(checkpoint, weight_filename))
|
133
122
|
|
134
123
|
verifier.verify_reauthored_model(
|
135
|
-
original_model=GemmaWrapper(original_model
|
136
|
-
reauthored_model=reauthored_model,
|
137
|
-
tokenizer=
|
124
|
+
original_model=GemmaWrapper(original_model),
|
125
|
+
reauthored_model=verifier.ReauthoredModelWrapper(reauthored_model),
|
126
|
+
tokenizer=GemmaTokenizerWrapper(original_model.tokenizer),
|
138
127
|
generate_prompts=generate_prompts,
|
128
|
+
max_new_tokens=max_new_tokens,
|
139
129
|
forward_input_ids=forward_input_ids,
|
140
130
|
rtol=rtol,
|
141
131
|
atol=atol,
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
|
16
|
+
"""Example of converting Llama 3.2 3B model to multi-signature tflite model."""
|
17
|
+
|
18
|
+
import os
|
19
|
+
import pathlib
|
20
|
+
|
21
|
+
from absl import app
|
22
|
+
from absl import flags
|
23
|
+
from ai_edge_torch.generative.examples.llama import llama
|
24
|
+
from ai_edge_torch.generative.utilities import converter
|
25
|
+
|
26
|
+
_CHECKPOINT_PATH = flags.DEFINE_string(
|
27
|
+
'checkpoint_path',
|
28
|
+
os.path.join(pathlib.Path.home(), 'Downloads/llm_data/llama'),
|
29
|
+
'The path to the model checkpoint, or directory holding the checkpoint.',
|
30
|
+
)
|
31
|
+
_TFLITE_PATH = flags.DEFINE_string(
|
32
|
+
'tflite_path',
|
33
|
+
'/tmp/',
|
34
|
+
'The tflite file path to export.',
|
35
|
+
)
|
36
|
+
_PREFILL_SEQ_LEN = flags.DEFINE_integer(
|
37
|
+
'prefill_seq_len',
|
38
|
+
1024,
|
39
|
+
'The maximum size of prefill input tensor.',
|
40
|
+
)
|
41
|
+
_KV_CACHE_MAX_LEN = flags.DEFINE_integer(
|
42
|
+
'kv_cache_max_len',
|
43
|
+
1280,
|
44
|
+
'The maximum size of KV cache buffer, including both prefill and decode.',
|
45
|
+
)
|
46
|
+
_QUANTIZE = flags.DEFINE_bool(
|
47
|
+
'quantize',
|
48
|
+
True,
|
49
|
+
'Whether the model should be quantized.',
|
50
|
+
)
|
51
|
+
|
52
|
+
|
53
|
+
def main(_):
|
54
|
+
pytorch_model = llama.build_3b_model(
|
55
|
+
_CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
|
56
|
+
)
|
57
|
+
quant_suffix = 'q8' if _QUANTIZE.value else 'f32'
|
58
|
+
output_filename = f'llama_3b_{quant_suffix}_seq{_PREFILL_SEQ_LEN.value}_ekv{_KV_CACHE_MAX_LEN.value}.tflite'
|
59
|
+
converter.convert_to_tflite(
|
60
|
+
pytorch_model,
|
61
|
+
tflite_path=os.path.join(_TFLITE_PATH.value, output_filename),
|
62
|
+
prefill_seq_len=_PREFILL_SEQ_LEN.value,
|
63
|
+
quantize=_QUANTIZE.value,
|
64
|
+
)
|
65
|
+
|
66
|
+
|
67
|
+
if __name__ == '__main__':
|
68
|
+
app.run(main)
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
|
16
|
+
"""Example of converting Llama 3.2 1B model to multi-signature tflite model."""
|
17
|
+
|
18
|
+
import os
|
19
|
+
import pathlib
|
20
|
+
|
21
|
+
from absl import app
|
22
|
+
from absl import flags
|
23
|
+
from ai_edge_torch.generative.examples.llama import llama
|
24
|
+
from ai_edge_torch.generative.utilities import converter
|
25
|
+
|
26
|
+
_CHECKPOINT_PATH = flags.DEFINE_string(
|
27
|
+
'checkpoint_path',
|
28
|
+
os.path.join(pathlib.Path.home(), 'Downloads/llm_data/llama'),
|
29
|
+
'The path to the model checkpoint, or directory holding the checkpoint.',
|
30
|
+
)
|
31
|
+
_TFLITE_PATH = flags.DEFINE_string(
|
32
|
+
'tflite_path',
|
33
|
+
'/tmp/',
|
34
|
+
'The tflite file path to export.',
|
35
|
+
)
|
36
|
+
_PREFILL_SEQ_LEN = flags.DEFINE_integer(
|
37
|
+
'prefill_seq_len',
|
38
|
+
1024,
|
39
|
+
'The maximum size of prefill input tensor.',
|
40
|
+
)
|
41
|
+
_KV_CACHE_MAX_LEN = flags.DEFINE_integer(
|
42
|
+
'kv_cache_max_len',
|
43
|
+
1280,
|
44
|
+
'The maximum size of KV cache buffer, including both prefill and decode.',
|
45
|
+
)
|
46
|
+
_QUANTIZE = flags.DEFINE_bool(
|
47
|
+
'quantize',
|
48
|
+
True,
|
49
|
+
'Whether the model should be quantized.',
|
50
|
+
)
|
51
|
+
|
52
|
+
|
53
|
+
def main(_):
|
54
|
+
pytorch_model = llama.build_model(
|
55
|
+
_CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
|
56
|
+
)
|
57
|
+
quant_suffix = 'q8' if _QUANTIZE.value else 'f32'
|
58
|
+
output_filename = f'llama_{quant_suffix}_seq{_PREFILL_SEQ_LEN.value}_ekv{_KV_CACHE_MAX_LEN.value}.tflite'
|
59
|
+
converter.convert_to_tflite(
|
60
|
+
pytorch_model,
|
61
|
+
tflite_path=os.path.join(_TFLITE_PATH.value, output_filename),
|
62
|
+
prefill_seq_len=_PREFILL_SEQ_LEN.value,
|
63
|
+
quantize=_QUANTIZE.value,
|
64
|
+
)
|
65
|
+
|
66
|
+
|
67
|
+
if __name__ == '__main__':
|
68
|
+
app.run(main)
|
@@ -0,0 +1,204 @@
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
|
16
|
+
"""Example of building Llama 3.2 models."""
|
17
|
+
|
18
|
+
import copy
|
19
|
+
import math
|
20
|
+
from typing import Tuple
|
21
|
+
|
22
|
+
from ai_edge_torch.generative.examples.tiny_llama import tiny_llama
|
23
|
+
import ai_edge_torch.generative.layers.model_config as cfg
|
24
|
+
import ai_edge_torch.generative.utilities.loader as loading_utils
|
25
|
+
import torch
|
26
|
+
from torch import nn
|
27
|
+
|
28
|
+
TENSOR_NAMES = copy.copy(tiny_llama.TENSOR_NAMES)
|
29
|
+
# SmolLM re-uses the embedding as the head projection layer.
|
30
|
+
TENSOR_NAMES.lm_head = None
|
31
|
+
|
32
|
+
|
33
|
+
def _build_llama3_rope_cache(
|
34
|
+
size: int,
|
35
|
+
dim: int,
|
36
|
+
base: int,
|
37
|
+
condense_ratio: int,
|
38
|
+
dtype: torch.dtype,
|
39
|
+
device: torch.device,
|
40
|
+
factor: float,
|
41
|
+
low_freq_factor: float,
|
42
|
+
high_freq_factor: float,
|
43
|
+
max_seq_len: int,
|
44
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
45
|
+
"""Precomputes Rotary Positional Embeddings for Llama 3.2 model.
|
46
|
+
|
47
|
+
It's a modified version of attn_utils.build_rope_cache with additional
|
48
|
+
arguments for Llama 3.2 model. It precomputes Rotary Positional Embedding Sin
|
49
|
+
and Cos values with scaling factors for quick lookup during the inference.
|
50
|
+
|
51
|
+
Reference:
|
52
|
+
https://github.com/huggingface/transformers/blob/main/src/transformers/modeling_rope_utils.py#L307
|
53
|
+
|
54
|
+
Args:
|
55
|
+
size (int): The size of the built cache.
|
56
|
+
dim (int): Each sequence's dimmension.
|
57
|
+
base (int, optional): Rope base value.
|
58
|
+
condense_ratio (int, optional): The ratio by which sequence indicies are
|
59
|
+
condensed.
|
60
|
+
dtype (torch.dtype, optional): Output tensor's data type.
|
61
|
+
device (torch.device, optional): Output tensor's data type.
|
62
|
+
factor (float): Factor to scale theta down for tokens in long range in the
|
63
|
+
sequence.
|
64
|
+
low_freq_factor (float): Factor to determine if tokens are in long range
|
65
|
+
in the sequence.
|
66
|
+
high_freq_factor (float): Factor to determine if tokens are in short range
|
67
|
+
in the sequence.
|
68
|
+
max_seq_len (int): The original token sequence length before extending
|
69
|
+
ROPE to support longer sequence.
|
70
|
+
|
71
|
+
Returns:
|
72
|
+
Tuple[torch.Tensor, torch.Tensor]: Rope's Cosine and Sine waves.
|
73
|
+
"""
|
74
|
+
theta = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
|
75
|
+
low_freq_wavelen = max_seq_len / low_freq_factor
|
76
|
+
high_freq_wavelen = max_seq_len / high_freq_factor
|
77
|
+
wavelen = 2 * math.pi / theta
|
78
|
+
# wavelen < high_freq_wavelen: do nothing
|
79
|
+
# wavelen > low_freq_wavelen: divide by factor
|
80
|
+
theta = torch.where(wavelen > low_freq_wavelen, theta / factor, theta)
|
81
|
+
# otherwise: interpolate between the two, using a smooth factor
|
82
|
+
smooth_factor = (max_seq_len / wavelen - low_freq_factor) / (
|
83
|
+
high_freq_factor - low_freq_factor
|
84
|
+
)
|
85
|
+
smoothed_theta = (1 - smooth_factor) * theta / factor + smooth_factor * theta
|
86
|
+
is_medium = ~(wavelen < high_freq_wavelen) * ~(wavelen > low_freq_wavelen)
|
87
|
+
theta = torch.where(is_medium, smoothed_theta, theta)
|
88
|
+
|
89
|
+
seq_idx = torch.arange(size) / condense_ratio
|
90
|
+
idx_theta = torch.outer(seq_idx, theta)
|
91
|
+
cos = torch.cos(idx_theta).to(dtype=dtype, device=device)
|
92
|
+
sin = torch.sin(idx_theta).to(dtype=dtype, device=device)
|
93
|
+
return cos, sin
|
94
|
+
|
95
|
+
|
96
|
+
class Llama(tiny_llama.TinyLlama):
|
97
|
+
"""A Llama model built from the Edge Generative API layers.
|
98
|
+
|
99
|
+
Llama 3.2 shares the same architecture as TinyLlama except ROPE calculation.
|
100
|
+
"""
|
101
|
+
|
102
|
+
def __init__(self, config: cfg.ModelConfig):
|
103
|
+
super().__init__(config)
|
104
|
+
# Llama 3.2 re-uses the embedding as the head projection layer.
|
105
|
+
self.lm_head.weight.data = self.tok_embedding.weight.data
|
106
|
+
# Llama has only one block config.
|
107
|
+
attn_config = self.config.block_config(0).attn_config
|
108
|
+
self.rope_cache = _build_llama3_rope_cache(
|
109
|
+
size=self.config.kv_cache_max,
|
110
|
+
dim=int(attn_config.rotary_percentage * attn_config.head_dim),
|
111
|
+
base=attn_config.rotary_base,
|
112
|
+
condense_ratio=1,
|
113
|
+
dtype=torch.float32,
|
114
|
+
device=torch.device("cpu"),
|
115
|
+
factor=32.0,
|
116
|
+
low_freq_factor=1.0,
|
117
|
+
high_freq_factor=4.0,
|
118
|
+
max_seq_len=self.config.max_seq_len,
|
119
|
+
)
|
120
|
+
|
121
|
+
|
122
|
+
def get_model_config(kv_cache_max_len: int = 1024) -> cfg.ModelConfig:
|
123
|
+
"""Returns the model config for a Llama 3.2-1B model.
|
124
|
+
|
125
|
+
Args:
|
126
|
+
kv_cache_max_len (int): The maximum sequence length of the KV cache. Default
|
127
|
+
is 1024.
|
128
|
+
|
129
|
+
Returns:
|
130
|
+
The model config for a SmolLM model.
|
131
|
+
"""
|
132
|
+
attn_config = cfg.AttentionConfig(
|
133
|
+
num_heads=32,
|
134
|
+
head_dim=64,
|
135
|
+
num_query_groups=8,
|
136
|
+
rotary_base=500000,
|
137
|
+
rotary_percentage=1.0,
|
138
|
+
)
|
139
|
+
ff_config = cfg.FeedForwardConfig(
|
140
|
+
type=cfg.FeedForwardType.GATED,
|
141
|
+
activation=cfg.ActivationConfig(cfg.ActivationType.SILU),
|
142
|
+
intermediate_size=8192,
|
143
|
+
)
|
144
|
+
norm_config = cfg.NormalizationConfig(type=cfg.NormalizationType.RMS_NORM)
|
145
|
+
block_config = cfg.TransformerBlockConfig(
|
146
|
+
attn_config=attn_config,
|
147
|
+
ff_config=ff_config,
|
148
|
+
pre_attention_norm_config=norm_config,
|
149
|
+
post_attention_norm_config=norm_config,
|
150
|
+
)
|
151
|
+
config = cfg.ModelConfig(
|
152
|
+
vocab_size=128256,
|
153
|
+
num_layers=16,
|
154
|
+
max_seq_len=8192,
|
155
|
+
embedding_dim=2048,
|
156
|
+
kv_cache_max_len=kv_cache_max_len,
|
157
|
+
block_configs=block_config,
|
158
|
+
final_norm_config=norm_config,
|
159
|
+
enable_hlfb=True,
|
160
|
+
)
|
161
|
+
return config
|
162
|
+
|
163
|
+
|
164
|
+
def get_3b_model_config(kv_cache_max_len: int = 1024) -> cfg.ModelConfig:
|
165
|
+
"""Returns the model config for a Llama 3.2-3B model."""
|
166
|
+
config = get_model_config(kv_cache_max_len)
|
167
|
+
# Llama 3.2 has only one block config.
|
168
|
+
attn_config = config.block_config(0).attn_config
|
169
|
+
attn_config.num_heads = 24
|
170
|
+
attn_config.head_dim = 128
|
171
|
+
config.num_layers = 28
|
172
|
+
config.embedding_dim = 3072
|
173
|
+
return config
|
174
|
+
|
175
|
+
|
176
|
+
def get_fake_model_config(**kwargs) -> cfg.ModelConfig:
|
177
|
+
config = get_model_config(**kwargs)
|
178
|
+
config.vocab_size = 128
|
179
|
+
config.num_layers = 2
|
180
|
+
# SmolLM has only one block config.
|
181
|
+
config.block_config(0).ff_config.intermediate_size = 64
|
182
|
+
return config
|
183
|
+
|
184
|
+
|
185
|
+
def build_model(checkpoint_path: str, **kwargs) -> nn.Module:
|
186
|
+
config = get_model_config(**kwargs)
|
187
|
+
model = Llama(config)
|
188
|
+
loader = loading_utils.ModelLoader(checkpoint_path, TENSOR_NAMES)
|
189
|
+
# Since embedding and lm-head use the same weight, we need to set strict
|
190
|
+
# to False.
|
191
|
+
loader.load(model, strict=False)
|
192
|
+
model.eval()
|
193
|
+
return model
|
194
|
+
|
195
|
+
|
196
|
+
def build_3b_model(checkpoint_path: str, **kwargs) -> nn.Module:
|
197
|
+
config = get_3b_model_config(**kwargs)
|
198
|
+
model = Llama(config)
|
199
|
+
loader = loading_utils.ModelLoader(checkpoint_path, TENSOR_NAMES)
|
200
|
+
# Since embedding and lm-head use the same weight, we need to set strict
|
201
|
+
# to False.
|
202
|
+
loader.load(model, strict=False)
|
203
|
+
model.eval()
|
204
|
+
return model
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
|
16
|
+
"""Verifies the reauthored Llama 3.2-1B model."""
|
17
|
+
|
18
|
+
import logging
|
19
|
+
import pathlib
|
20
|
+
|
21
|
+
from absl import app
|
22
|
+
from absl import flags
|
23
|
+
from ai_edge_torch.generative.examples.llama import llama
|
24
|
+
from ai_edge_torch.generative.utilities import transformers_verifier
|
25
|
+
from ai_edge_torch.generative.utilities import verifier
|
26
|
+
import transformers
|
27
|
+
|
28
|
+
|
29
|
+
_PROMPTS = flags.DEFINE_multi_string(
|
30
|
+
"prompts",
|
31
|
+
"What is the meaning of life?",
|
32
|
+
"The input prompts to generate answers.",
|
33
|
+
)
|
34
|
+
_MAX_NEW_TOKENS = flags.DEFINE_integer(
|
35
|
+
"max_new_tokens",
|
36
|
+
30,
|
37
|
+
"The maximum size of the generated tokens.",
|
38
|
+
)
|
39
|
+
|
40
|
+
|
41
|
+
def main(_):
|
42
|
+
checkpoint = "meta-llama/Llama-3.2-1B-Instruct"
|
43
|
+
logging.info("Loading the original model from: %s", checkpoint)
|
44
|
+
original_model = transformers.AutoModelForCausalLM.from_pretrained(checkpoint)
|
45
|
+
|
46
|
+
# Locate the cached dir.
|
47
|
+
cached_config_file = transformers.utils.cached_file(
|
48
|
+
checkpoint, transformers.utils.CONFIG_NAME
|
49
|
+
)
|
50
|
+
reauthored_checkpoint = pathlib.Path(cached_config_file).parent
|
51
|
+
logging.info("Building the reauthored model from: %s", reauthored_checkpoint)
|
52
|
+
reauthored_model = llama.build_model(reauthored_checkpoint)
|
53
|
+
|
54
|
+
logging.info("Loading the tokenizer from: %s", checkpoint)
|
55
|
+
# Llama tokenizer_config.json sets a fast tokenizer class explicitly,
|
56
|
+
# "PreTrainedTokenizerFast". It works only when the fast tokenizer is
|
57
|
+
# available.
|
58
|
+
tokenizer = transformers.AutoTokenizer.from_pretrained(checkpoint)
|
59
|
+
|
60
|
+
verifier.verify_reauthored_model(
|
61
|
+
original_model=transformers_verifier.TransformersModelWrapper(
|
62
|
+
original_model
|
63
|
+
),
|
64
|
+
reauthored_model=verifier.ReauthoredModelWrapper(reauthored_model),
|
65
|
+
tokenizer=verifier.TokenizerWrapper(tokenizer),
|
66
|
+
generate_prompts=_PROMPTS.value,
|
67
|
+
max_new_tokens=_MAX_NEW_TOKENS.value,
|
68
|
+
atol=1e-04,
|
69
|
+
)
|
70
|
+
|
71
|
+
|
72
|
+
if __name__ == "__main__":
|
73
|
+
app.run(main)
|