ai-edge-torch-nightly 0.4.0.dev20250329__py3-none-any.whl → 0.4.0.dev20250331__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. ai_edge_torch/generative/examples/amd_llama_135m/convert_to_tflite.py +7 -43
  2. ai_edge_torch/generative/examples/deepseek/convert_to_tflite.py +7 -42
  3. ai_edge_torch/generative/examples/gemma/convert_gemma1_to_tflite.py +7 -45
  4. ai_edge_torch/generative/examples/gemma/convert_gemma2_to_tflite.py +7 -44
  5. ai_edge_torch/generative/examples/gemma3/convert_gemma3_to_tflite.py +10 -45
  6. ai_edge_torch/generative/examples/gemma3/verify_gemma3.py +90 -0
  7. ai_edge_torch/generative/examples/gemma3/verify_util.py +247 -0
  8. ai_edge_torch/generative/examples/llama/convert_to_tflite.py +9 -43
  9. ai_edge_torch/generative/examples/openelm/convert_to_tflite.py +7 -44
  10. ai_edge_torch/generative/examples/paligemma/convert_to_tflite.py +8 -39
  11. ai_edge_torch/generative/examples/phi/convert_phi3_to_tflite.py +7 -44
  12. ai_edge_torch/generative/examples/phi/convert_phi4_to_tflite.py +7 -44
  13. ai_edge_torch/generative/examples/phi/convert_to_tflite.py +7 -42
  14. ai_edge_torch/generative/examples/qwen/convert_to_tflite.py +8 -45
  15. ai_edge_torch/generative/examples/qwen_vl/convert_to_tflite.py +8 -39
  16. ai_edge_torch/generative/examples/smollm/convert_to_tflite.py +8 -43
  17. ai_edge_torch/generative/examples/smollm/convert_v2_to_tflite.py +8 -43
  18. ai_edge_torch/generative/examples/tiny_llama/convert_to_tflite.py +7 -44
  19. ai_edge_torch/generative/utilities/converter.py +45 -0
  20. ai_edge_torch/version.py +1 -1
  21. {ai_edge_torch_nightly-0.4.0.dev20250329.dist-info → ai_edge_torch_nightly-0.4.0.dev20250331.dist-info}/METADATA +1 -1
  22. {ai_edge_torch_nightly-0.4.0.dev20250329.dist-info → ai_edge_torch_nightly-0.4.0.dev20250331.dist-info}/RECORD +25 -23
  23. {ai_edge_torch_nightly-0.4.0.dev20250329.dist-info → ai_edge_torch_nightly-0.4.0.dev20250331.dist-info}/LICENSE +0 -0
  24. {ai_edge_torch_nightly-0.4.0.dev20250329.dist-info → ai_edge_torch_nightly-0.4.0.dev20250331.dist-info}/WHEEL +0 -0
  25. {ai_edge_torch_nightly-0.4.0.dev20250329.dist-info → ai_edge_torch_nightly-0.4.0.dev20250331.dist-info}/top_level.txt +0 -0
@@ -16,61 +16,25 @@
16
16
  """Example of converting AMD-Llama-135m model to multi-signature tflite model."""
17
17
 
18
18
  import os
19
- import pathlib
20
-
21
19
  from absl import app
22
20
  from absl import flags
23
21
  from ai_edge_torch.generative.examples.amd_llama_135m import amd_llama_135m
24
22
  from ai_edge_torch.generative.utilities import converter
25
23
  from ai_edge_torch.generative.utilities.model_builder import ExportConfig
26
24
 
27
- _CHECKPOINT_PATH = flags.DEFINE_string(
28
- 'checkpoint_path',
29
- os.path.join(pathlib.Path.home(), 'Downloads/llm_data/amd-llama-135m'),
30
- 'The path to the model checkpoint, or directory holding the checkpoint.',
31
- )
32
- _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
33
- 'kv_cache_max_len',
34
- 1280,
35
- 'The maximum size of KV cache buffer, including both prefill and decode.',
36
- )
37
- _OUTPUT_PATH = flags.DEFINE_string(
38
- 'output_path',
39
- '/tmp/',
40
- 'The path to export the tflite model.',
41
- )
42
- _OUTPUT_NAME_PREFIX = flags.DEFINE_string(
43
- 'output_name_prefix',
44
- 'amd_llama',
45
- 'The prefix of the output tflite model name.',
46
- )
47
- _PREFILL_SEQ_LENS = flags.DEFINE_multi_integer(
48
- 'prefill_seq_lens',
49
- (8, 64, 128, 256, 512, 1024),
50
- 'List of the maximum sizes of prefill input tensors.',
51
- )
52
- _QUANTIZE = flags.DEFINE_bool(
53
- 'quantize',
54
- True,
55
- 'Whether the model should be quantized.',
56
- )
57
- _LORA_RANKS = flags.DEFINE_multi_integer(
58
- 'lora_ranks',
59
- None,
60
- 'If set, the model will be converted with the provided list of LoRA ranks.',
61
- )
25
+ flags = converter.define_conversion_flags("amd-llama-135m")
62
26
 
63
27
  def main(_):
64
28
  pytorch_model = amd_llama_135m.build_model(
65
- _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
29
+ flags.FLAGS.checkpoint_path, kv_cache_max_len=flags.FLAGS.kv_cache_max_len
66
30
  )
67
31
  converter.convert_to_tflite(
68
32
  pytorch_model,
69
- output_path=_OUTPUT_PATH.value,
70
- output_name_prefix=_OUTPUT_NAME_PREFIX.value,
71
- prefill_seq_len=_PREFILL_SEQ_LENS.value,
72
- quantize=_QUANTIZE.value,
73
- lora_ranks=_LORA_RANKS.value,
33
+ output_path=flags.FLAGS.output_path,
34
+ output_name_prefix=flags.FLAGS.output_name_prefix,
35
+ prefill_seq_len=flags.FLAGS.prefill_seq_lens,
36
+ quantize=flags.FLAGS.quantize,
37
+ lora_ranks=flags.FLAGS.lora_ranks,
74
38
  export_config=ExportConfig(),
75
39
  )
76
40
 
@@ -24,54 +24,19 @@ from ai_edge_torch.generative.examples.deepseek import deepseek
24
24
  from ai_edge_torch.generative.utilities import converter
25
25
  from ai_edge_torch.generative.utilities.model_builder import ExportConfig
26
26
 
27
- _CHECKPOINT_PATH = flags.DEFINE_string(
28
- 'checkpoint_path',
29
- os.path.join(pathlib.Path.home(), 'Downloads/llm_data/deepseek'),
30
- 'The path to the model checkpoint, or directory holding the checkpoint.',
31
- )
32
- _OUTPUT_PATH = flags.DEFINE_string(
33
- 'output_path',
34
- '/tmp/',
35
- 'The path to export the tflite model.',
36
- )
37
- _OUTPUT_NAME_PREFIX = flags.DEFINE_string(
38
- 'output_name_prefix',
39
- 'deepseek',
40
- 'The prefix of the output tflite model name.',
41
- )
42
- _PREFILL_SEQ_LENS = flags.DEFINE_multi_integer(
43
- 'prefill_seq_lens',
44
- (8, 64, 128, 256, 512, 1024),
45
- 'List of the maximum sizes of prefill input tensors.',
46
- )
47
- _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
48
- 'kv_cache_max_len',
49
- 1280,
50
- 'The maximum size of KV cache buffer, including both prefill and decode.',
51
- )
52
- _QUANTIZE = flags.DEFINE_bool(
53
- 'quantize',
54
- True,
55
- 'Whether the model should be quantized.',
56
- )
57
- _LORA_RANKS = flags.DEFINE_multi_integer(
58
- 'lora_ranks',
59
- None,
60
- 'If set, the model will be converted with the provided list of LoRA ranks.',
61
- )
62
-
27
+ flags = converter.define_conversion_flags("deepseek")
63
28
 
64
29
  def main(_):
65
30
  pytorch_model = deepseek.build_model(
66
- _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
31
+ flags.FLAGS.checkpoint_path, kv_cache_max_len=flags.FLAGS.kv_cache_max_len
67
32
  )
68
33
  converter.convert_to_tflite(
69
34
  pytorch_model,
70
- output_path=_OUTPUT_PATH.value,
71
- output_name_prefix=_OUTPUT_NAME_PREFIX.value,
72
- prefill_seq_len=_PREFILL_SEQ_LENS.value,
73
- quantize=_QUANTIZE.value,
74
- lora_ranks=_LORA_RANKS.value,
35
+ output_path=flags.FLAGS.output_path,
36
+ output_name_prefix=flags.FLAGS.output_name_prefix,
37
+ prefill_seq_len=flags.FLAGS.prefill_seq_lens,
38
+ quantize=flags.FLAGS.quantize,
39
+ lora_ranks=flags.FLAGS.lora_ranks,
75
40
  export_config=ExportConfig(),
76
41
  )
77
42
 
@@ -16,62 +16,24 @@
16
16
  """Example of converting a Gemma1 model to multi-signature tflite model."""
17
17
 
18
18
  import os
19
- import pathlib
20
-
21
19
  from absl import app
22
- from absl import flags
23
20
  from ai_edge_torch.generative.examples.gemma import gemma1
24
21
  from ai_edge_torch.generative.utilities import converter
25
22
  from ai_edge_torch.generative.utilities.model_builder import ExportConfig
26
23
 
27
- _CHECKPOINT_PATH = flags.DEFINE_string(
28
- 'checkpoint_path',
29
- os.path.join(pathlib.Path.home(), 'Downloads/llm_data/gemma-2b'),
30
- 'The path to the model checkpoint, or directory holding the checkpoint.',
31
- )
32
- _OUTPUT_PATH = flags.DEFINE_string(
33
- 'output_path',
34
- '/tmp/',
35
- 'The path to export the tflite model.',
36
- )
37
- _OUTPUT_NAME_PREFIX = flags.DEFINE_string(
38
- 'output_name_prefix',
39
- 'gemma',
40
- 'The prefix of the output tflite model name.',
41
- )
42
- _PREFILL_SEQ_LENS = flags.DEFINE_multi_integer(
43
- 'prefill_seq_lens',
44
- (8, 64, 128, 256, 512, 1024),
45
- 'List of the maximum sizes of prefill input tensors.',
46
- )
47
- _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
48
- 'kv_cache_max_len',
49
- 1280,
50
- 'The maximum size of KV cache buffer, including both prefill and decode.',
51
- )
52
- _QUANTIZE = flags.DEFINE_bool(
53
- 'quantize',
54
- True,
55
- 'Whether the model should be quantized.',
56
- )
57
- _LORA_RANKS = flags.DEFINE_multi_integer(
58
- 'lora_ranks',
59
- None,
60
- 'If set, the model will be converted with the provided list of LoRA ranks.',
61
- )
62
-
24
+ flags = converter.define_conversion_flags("gemma-2b")
63
25
 
64
26
  def main(_):
65
27
  pytorch_model = gemma1.build_2b_model(
66
- _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
28
+ flags.FLAGS.checkpoint_path, kv_cache_max_len=flags.FLAGS.kv_cache_max_len
67
29
  )
68
30
  converter.convert_to_tflite(
69
31
  pytorch_model,
70
- output_path=_OUTPUT_PATH.value,
71
- output_name_prefix=_OUTPUT_NAME_PREFIX.value,
72
- prefill_seq_len=_PREFILL_SEQ_LENS.value,
73
- quantize=_QUANTIZE.value,
74
- lora_ranks=_LORA_RANKS.value,
32
+ output_path=flags.FLAGS.output_path,
33
+ output_name_prefix=flags.FLAGS.output_name_prefix,
34
+ prefill_seq_len=flags.FLAGS.prefill_seq_lens,
35
+ quantize=flags.FLAGS.quantize,
36
+ lora_ranks=flags.FLAGS.lora_ranks,
75
37
  export_config=ExportConfig(),
76
38
  )
77
39
 
@@ -16,62 +16,25 @@
16
16
  """Example of converting a Gemma2 model to multi-signature tflite model."""
17
17
 
18
18
  import os
19
- import pathlib
20
-
21
19
  from absl import app
22
20
  from absl import flags
23
21
  from ai_edge_torch.generative.examples.gemma import gemma2
24
22
  from ai_edge_torch.generative.utilities import converter
25
23
  from ai_edge_torch.generative.utilities.model_builder import ExportConfig
26
24
 
27
- _CHECKPOINT_PATH = flags.DEFINE_string(
28
- 'checkpoint_path',
29
- os.path.join(pathlib.Path.home(), 'Downloads/llm_data/gemma2-2b'),
30
- 'The path to the model checkpoint, or directory holding the checkpoint.',
31
- )
32
- _OUTPUT_PATH = flags.DEFINE_string(
33
- 'output_path',
34
- '/tmp/',
35
- 'The path to export the tflite model.',
36
- )
37
- _OUTPUT_NAME_PREFIX = flags.DEFINE_string(
38
- 'output_name_prefix',
39
- 'gemma2',
40
- 'The prefix of the output tflite model name.',
41
- )
42
- _PREFILL_SEQ_LENS = flags.DEFINE_multi_integer(
43
- 'prefill_seq_lens',
44
- (8, 64, 128, 256, 512, 1024),
45
- 'List of the maximum sizes of prefill input tensors.',
46
- )
47
- _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
48
- 'kv_cache_max_len',
49
- 1280,
50
- 'The maximum size of KV cache buffer, including both prefill and decode.',
51
- )
52
- _QUANTIZE = flags.DEFINE_bool(
53
- 'quantize',
54
- True,
55
- 'Whether the model should be quantized.',
56
- )
57
- _LORA_RANKS = flags.DEFINE_multi_integer(
58
- 'lora_ranks',
59
- None,
60
- 'If set, the model will be converted with the provided list of LoRA ranks.',
61
- )
62
-
25
+ flags = converter.define_conversion_flags("gemma2-2b")
63
26
 
64
27
  def main(_):
65
28
  pytorch_model = gemma2.build_2b_model(
66
- _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
29
+ flags.FLAGS.checkpoint_path, kv_cache_max_len=flags.FLAGS.kv_cache_max_len
67
30
  )
68
31
  converter.convert_to_tflite(
69
32
  pytorch_model,
70
- output_path=_OUTPUT_PATH.value,
71
- output_name_prefix=_OUTPUT_NAME_PREFIX.value,
72
- prefill_seq_len=_PREFILL_SEQ_LENS.value,
73
- quantize=_QUANTIZE.value,
74
- lora_ranks=_LORA_RANKS.value,
33
+ output_path=flags.FLAGS.output_path,
34
+ output_name_prefix=flags.FLAGS.output_name_prefix,
35
+ prefill_seq_len=flags.FLAGS.prefill_seq_lens,
36
+ quantize=flags.FLAGS.quantize,
37
+ lora_ranks=flags.FLAGS.lora_ranks,
75
38
  export_config=ExportConfig(),
76
39
  )
77
40
 
@@ -16,8 +16,6 @@
16
16
  """Example of converting a Gemma3 model to multi-signature tflite model."""
17
17
 
18
18
  import os
19
- import pathlib
20
-
21
19
  from absl import app
22
20
  from absl import flags
23
21
  from ai_edge_torch.generative.examples.gemma3 import gemma3
@@ -26,48 +24,14 @@ from ai_edge_torch.generative.utilities import converter
26
24
  from ai_edge_torch.generative.utilities.model_builder import ExportConfig
27
25
  import torch
28
26
 
27
+ flags = converter.define_conversion_flags('gemma3-1b')
28
+
29
29
  _MODEL_SIZE = flags.DEFINE_string(
30
30
  'model_size',
31
31
  '1b',
32
32
  'The size of the model to convert.',
33
33
  )
34
34
 
35
- _CHECKPOINT_PATH = flags.DEFINE_string(
36
- 'checkpoint_path',
37
- os.path.join(pathlib.Path.home(), 'Downloads/llm_data/gemma3-1b'),
38
- 'The path to the model checkpoint, or directory holding the checkpoint.',
39
- )
40
- _OUTPUT_PATH = flags.DEFINE_string(
41
- 'output_path',
42
- '/tmp/',
43
- 'The path to export the tflite model.',
44
- )
45
- _OUTPUT_NAME_PREFIX = flags.DEFINE_string(
46
- 'output_name_prefix',
47
- 'gemma3',
48
- 'The prefix of the output tflite model name.',
49
- )
50
- _PREFILL_SEQ_LENS = flags.DEFINE_multi_integer(
51
- 'prefill_seq_lens',
52
- (32, 64, 128, 256, 512, 1024),
53
- 'List of the maximum sizes of prefill input tensors.',
54
- )
55
- _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
56
- 'kv_cache_max_len',
57
- 2048,
58
- 'The maximum size of KV cache buffer, including both prefill and decode.',
59
- )
60
- _QUANTIZE = flags.DEFINE_bool(
61
- 'quantize',
62
- False,
63
- 'Whether the model should be quantized.',
64
- )
65
- _LORA_RANKS = flags.DEFINE_multi_integer(
66
- 'lora_ranks',
67
- None,
68
- 'If set, the model will be converted with the provided list of LoRA ranks.',
69
- )
70
-
71
35
 
72
36
  def _create_mask(mask_len, kv_cache_max_len):
73
37
  mask = torch.full(
@@ -101,21 +65,22 @@ def _create_export_config(
101
65
  def main(_):
102
66
  if _MODEL_SIZE.value == '1b':
103
67
  pytorch_model = gemma3.build_model_1b(
104
- _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
68
+ flags.FLAGS.checkpoint_path,
69
+ kv_cache_max_len=flags.FLAGS.kv_cache_max_len,
105
70
  )
106
71
  config = pytorch_model.config
107
72
  else:
108
73
  raise ValueError(f'Unsupported model size: {_MODEL_SIZE.value}')
109
74
  converter.convert_to_tflite(
110
75
  pytorch_model,
111
- output_path=_OUTPUT_PATH.value,
112
- output_name_prefix=_OUTPUT_NAME_PREFIX.value,
113
- prefill_seq_len=_PREFILL_SEQ_LENS.value,
114
- quantize=_QUANTIZE.value,
76
+ output_path=flags.FLAGS.output_path,
77
+ output_name_prefix=flags.FLAGS.output_name_prefix,
78
+ prefill_seq_len=flags.FLAGS.prefill_seq_lens,
79
+ quantize=flags.FLAGS.quantize,
115
80
  config=config,
116
- lora_ranks=_LORA_RANKS.value,
81
+ lora_ranks=flags.FLAGS.lora_ranks,
117
82
  export_config=_create_export_config(
118
- _PREFILL_SEQ_LENS.value, _KV_CACHE_MAX_LEN.value
83
+ flags.FLAGS.prefill_seq_lens, flags.FLAGS.kv_cache_max_len
119
84
  ),
120
85
  )
121
86
 
@@ -0,0 +1,90 @@
1
+ # Copyright 2025 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 Gemma3 model."""
17
+
18
+ import glob
19
+ import logging
20
+ import os
21
+ from absl import app
22
+ from absl import flags
23
+ from ai_edge_torch.generative.examples.gemma3 import verify_util
24
+ import kagglehub
25
+
26
+
27
+ _PROMPTS = flags.DEFINE_multi_string(
28
+ "prompts",
29
+ "What is the meaning of life?",
30
+ "The input prompts to generate answers.",
31
+ )
32
+ _MAX_NEW_TOKENS = flags.DEFINE_integer(
33
+ "max_new_tokens",
34
+ 30,
35
+ "The maximum size of the generated tokens.",
36
+ )
37
+ _CHECKPOINT = flags.DEFINE_string(
38
+ "checkpoint",
39
+ "",
40
+ "The checkpoint to verify.",
41
+ )
42
+ _VARIANT = flags.DEFINE_string(
43
+ "variant",
44
+ "1b",
45
+ "The variant of the model to verify.",
46
+ )
47
+ _WEIGHT_FILENAME = flags.DEFINE_string(
48
+ "weight_filename",
49
+ None,
50
+ "The weightfilename of the model to verify.",
51
+ )
52
+
53
+
54
+ def find_first_ckpt(folder):
55
+ """Finds the first .ckpt file in a folder."""
56
+ ckpt_files = sorted(glob.glob(os.path.join(folder, "*.ckpt")))
57
+ return os.path.basename(ckpt_files[0]) if ckpt_files else None
58
+
59
+
60
+ def main(_):
61
+ if _CHECKPOINT.value:
62
+ checkpoint = _CHECKPOINT.value
63
+ else:
64
+ checkpoint = kagglehub.model_download(
65
+ "google/gemma-3/pyTorch/gemma-3-1b-it"
66
+ )
67
+
68
+ # If the weight filename is not specified, use the first checkpoint.
69
+ if _WEIGHT_FILENAME.value is None:
70
+ weight_filename = find_first_ckpt(checkpoint)
71
+ logging.info(
72
+ "NOTE: using the first weight file `%s` from `%s`",
73
+ weight_filename,
74
+ checkpoint,
75
+ )
76
+ else:
77
+ weight_filename = _WEIGHT_FILENAME.value
78
+
79
+ # Verify the reauthored model by comparing the outputs with the original one.
80
+ verify_util.verify_gemma3(
81
+ checkpoint,
82
+ _PROMPTS.value,
83
+ _MAX_NEW_TOKENS.value,
84
+ _VARIANT.value,
85
+ weight_filename,
86
+ )
87
+
88
+
89
+ if __name__ == "__main__":
90
+ app.run(main)