ai-edge-torch-nightly 0.3.0.dev20240919__py3-none-any.whl → 0.3.0.dev20240920__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (21) hide show
  1. ai_edge_torch/_convert/test/test_convert.py +7 -3
  2. ai_edge_torch/generative/examples/gemma/convert_gemma2_to_tflite.py +6 -4
  3. ai_edge_torch/generative/examples/gemma/convert_to_tflite.py +6 -4
  4. ai_edge_torch/generative/examples/openelm/convert_to_tflite.py +6 -4
  5. ai_edge_torch/generative/examples/openelm/verify.py +5 -3
  6. ai_edge_torch/generative/examples/phi/convert_to_tflite.py +6 -4
  7. ai_edge_torch/generative/examples/phi/verify.py +13 -3
  8. ai_edge_torch/generative/examples/smollm/convert_to_tflite.py +6 -4
  9. ai_edge_torch/generative/examples/smollm/verify.py +4 -3
  10. ai_edge_torch/generative/examples/tiny_llama/convert_to_tflite.py +6 -4
  11. ai_edge_torch/generative/examples/tiny_llama/verify.py +5 -4
  12. ai_edge_torch/generative/test/test_model_conversion.py +1 -1
  13. ai_edge_torch/generative/test/test_model_conversion_large.py +1 -1
  14. ai_edge_torch/generative/utilities/verifier.py +56 -7
  15. ai_edge_torch/model.py +7 -4
  16. ai_edge_torch/version.py +1 -1
  17. {ai_edge_torch_nightly-0.3.0.dev20240919.dist-info → ai_edge_torch_nightly-0.3.0.dev20240920.dist-info}/METADATA +1 -1
  18. {ai_edge_torch_nightly-0.3.0.dev20240919.dist-info → ai_edge_torch_nightly-0.3.0.dev20240920.dist-info}/RECORD +21 -21
  19. {ai_edge_torch_nightly-0.3.0.dev20240919.dist-info → ai_edge_torch_nightly-0.3.0.dev20240920.dist-info}/LICENSE +0 -0
  20. {ai_edge_torch_nightly-0.3.0.dev20240919.dist-info → ai_edge_torch_nightly-0.3.0.dev20240920.dist-info}/WHEEL +0 -0
  21. {ai_edge_torch_nightly-0.3.0.dev20240919.dist-info → ai_edge_torch_nightly-0.3.0.dev20240920.dist-info}/top_level.txt +0 -0
@@ -23,12 +23,12 @@ from ai_edge_torch import config
23
23
  from ai_edge_torch._convert import conversion_utils
24
24
  from ai_edge_torch.testing import model_coverage
25
25
  import numpy as np
26
- import tensorflow as tf
27
26
  import torch
28
27
  from torch import nn
29
28
  import torchvision
30
29
 
31
30
  from absl.testing import absltest as googletest
31
+ from ai_edge_litert import interpreter as tfl_interpreter # pylint: disable=g-direct-tensorflow-import
32
32
 
33
33
 
34
34
  @dataclasses.dataclass
@@ -466,7 +466,9 @@ class TestConvert(googletest.TestCase):
466
466
  np.testing.assert_almost_equal(edge_output["y_data_2_0"], args[1])
467
467
  np.testing.assert_almost_equal(edge_output["y_data_2_1"], args[2])
468
468
 
469
- interpreter = tf.lite.Interpreter(model_content=edge_model._tflite_model)
469
+ interpreter = tfl_interpreter.Interpreter(
470
+ model_content=edge_model._tflite_model
471
+ )
470
472
  runner = interpreter.get_signature_runner("serving_default")
471
473
  output_details = runner.get_output_details()
472
474
  self.assertIn("x", output_details.keys())
@@ -477,7 +479,9 @@ class TestConvert(googletest.TestCase):
477
479
  def _compare_tflite_torch_args_kwargs(self, model, args, kwargs, flat_inputs):
478
480
  model.eval()
479
481
  edge_model = ai_edge_torch.convert(model, args, kwargs)
480
- interpreter = tf.lite.Interpreter(model_content=edge_model._tflite_model)
482
+ interpreter = tfl_interpreter.Interpreter(
483
+ model_content=edge_model._tflite_model
484
+ )
481
485
  runner = interpreter.get_signature_runner("serving_default")
482
486
  input_details = runner.get_input_details()
483
487
  self.assertEqual(input_details.keys(), flat_inputs.keys())
@@ -30,17 +30,17 @@ _CHECKPOINT_PATH = flags.DEFINE_string(
30
30
  )
31
31
  _TFLITE_PATH = flags.DEFINE_string(
32
32
  'tflite_path',
33
- '/tmp/gemma2_q8_seq512_ekv1024.tflite',
33
+ '/tmp/',
34
34
  'The tflite file path to export.',
35
35
  )
36
36
  _PREFILL_SEQ_LEN = flags.DEFINE_integer(
37
37
  'prefill_seq_len',
38
- 512,
38
+ 1024,
39
39
  'The maximum size of prefill input tensor.',
40
40
  )
41
41
  _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
42
42
  'kv_cache_max_len',
43
- 1024,
43
+ 1280,
44
44
  'The maximum size of KV cache buffer, including both prefill and decode.',
45
45
  )
46
46
  _QUANTIZE = flags.DEFINE_bool(
@@ -54,9 +54,11 @@ def main(_):
54
54
  pytorch_model = gemma2.build_2b_model(
55
55
  _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
56
56
  )
57
+ quant_suffix = 'q8' if _QUANTIZE.value else 'f32'
58
+ output_filename = f'gemma2_{quant_suffix}_seq{_PREFILL_SEQ_LEN.value}_ekv{_KV_CACHE_MAX_LEN.value}.tflite'
57
59
  converter.convert_to_tflite(
58
60
  pytorch_model,
59
- tflite_path=_TFLITE_PATH.value,
61
+ tflite_path=os.path.join(_TFLITE_PATH.value, output_filename),
60
62
  prefill_seq_len=_PREFILL_SEQ_LEN.value,
61
63
  quantize=_QUANTIZE.value,
62
64
  )
@@ -30,17 +30,17 @@ _CHECKPOINT_PATH = flags.DEFINE_string(
30
30
  )
31
31
  _TFLITE_PATH = flags.DEFINE_string(
32
32
  'tflite_path',
33
- '/tmp/gemma_q8_seq512_ekv1024.tflite',
33
+ '/tmp/',
34
34
  'The tflite file path to export.',
35
35
  )
36
36
  _PREFILL_SEQ_LEN = flags.DEFINE_integer(
37
37
  'prefill_seq_len',
38
- 512,
38
+ 1024,
39
39
  'The maximum size of prefill input tensor.',
40
40
  )
41
41
  _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
42
42
  'kv_cache_max_len',
43
- 1024,
43
+ 1280,
44
44
  'The maximum size of KV cache buffer, including both prefill and decode.',
45
45
  )
46
46
  _QUANTIZE = flags.DEFINE_bool(
@@ -54,9 +54,11 @@ def main(_):
54
54
  pytorch_model = gemma.build_2b_model(
55
55
  _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
56
56
  )
57
+ quant_suffix = 'q8' if _QUANTIZE.value else 'f32'
58
+ output_filename = f'gemma_{quant_suffix}_seq{_PREFILL_SEQ_LEN.value}_ekv{_KV_CACHE_MAX_LEN.value}.tflite'
57
59
  converter.convert_to_tflite(
58
60
  pytorch_model,
59
- tflite_path=_TFLITE_PATH.value,
61
+ tflite_path=os.path.join(_TFLITE_PATH.value, output_filename),
60
62
  prefill_seq_len=_PREFILL_SEQ_LEN.value,
61
63
  quantize=_QUANTIZE.value,
62
64
  )
@@ -30,17 +30,17 @@ _CHECKPOINT_PATH = flags.DEFINE_string(
30
30
  )
31
31
  _TFLITE_PATH = flags.DEFINE_string(
32
32
  'tflite_path',
33
- '/tmp/openelm_q8_seq512_ekv1024.tflite',
33
+ '/tmp/',
34
34
  'The tflite file path to export.',
35
35
  )
36
36
  _PREFILL_SEQ_LEN = flags.DEFINE_integer(
37
37
  'prefill_seq_len',
38
- 512,
38
+ 1024,
39
39
  'The maximum size of prefill input tensor.',
40
40
  )
41
41
  _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
42
42
  'kv_cache_max_len',
43
- 1024,
43
+ 1280,
44
44
  'The maximum size of KV cache buffer, including both prefill and decode.',
45
45
  )
46
46
  _QUANTIZE = flags.DEFINE_bool(
@@ -54,9 +54,11 @@ def main(_):
54
54
  pytorch_model = openelm.build_model(
55
55
  _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
56
56
  )
57
+ quant_suffix = 'q8' if _QUANTIZE.value else 'f32'
58
+ output_filename = f'openelm_{quant_suffix}_seq{_PREFILL_SEQ_LEN.value}_ekv{_KV_CACHE_MAX_LEN.value}.tflite'
57
59
  converter.convert_to_tflite(
58
60
  pytorch_model,
59
- tflite_path=_TFLITE_PATH.value,
61
+ tflite_path=os.path.join(_TFLITE_PATH.value, output_filename),
60
62
  prefill_seq_len=_PREFILL_SEQ_LEN.value,
61
63
  quantize=_QUANTIZE.value,
62
64
  )
@@ -33,8 +33,10 @@ _PROMPTS = flags.DEFINE_multi_string(
33
33
  def main(_):
34
34
  checkpoint = "apple/OpenELM-3B"
35
35
  verifier.log_msg("Loading the original model from", checkpoint)
36
- original_model = transformers.AutoModelForCausalLM.from_pretrained(
37
- checkpoint, trust_remote_code=True
36
+ wrapper_model = verifier.ModelWrapper(
37
+ model=transformers.AutoModelForCausalLM.from_pretrained(
38
+ checkpoint, trust_remote_code=True
39
+ ),
38
40
  )
39
41
 
40
42
  # Locate the cached dir.
@@ -50,7 +52,7 @@ def main(_):
50
52
  tokenizer = transformers.AutoTokenizer.from_pretrained(tokenizer_checkpoint)
51
53
 
52
54
  verifier.verify_reauthored_model(
53
- original_model=original_model,
55
+ original_model=wrapper_model,
54
56
  reauthored_model=reauthored_model,
55
57
  tokenizer=tokenizer,
56
58
  prompts=_PROMPTS.value,
@@ -30,17 +30,17 @@ _CHECKPOINT_PATH = flags.DEFINE_string(
30
30
  )
31
31
  _TFLITE_PATH = flags.DEFINE_string(
32
32
  'tflite_path',
33
- '/tmp/phi2_q8_seq512_ekv1024.tflite',
33
+ '/tmp/',
34
34
  'The tflite file path to export.',
35
35
  )
36
36
  _PREFILL_SEQ_LEN = flags.DEFINE_integer(
37
37
  'prefill_seq_len',
38
- 512,
38
+ 1024,
39
39
  'The maximum size of prefill input tensor.',
40
40
  )
41
41
  _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
42
42
  'kv_cache_max_len',
43
- 1024,
43
+ 1280,
44
44
  'The maximum size of KV cache buffer, including both prefill and decode.',
45
45
  )
46
46
  _QUANTIZE = flags.DEFINE_bool(
@@ -54,9 +54,11 @@ def main(_):
54
54
  pytorch_model = phi2.build_model(
55
55
  _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
56
56
  )
57
+ quant_suffix = 'q8' if _QUANTIZE.value else 'f32'
58
+ output_filename = f'phi2_{quant_suffix}_seq{_PREFILL_SEQ_LEN.value}_ekv{_KV_CACHE_MAX_LEN.value}.tflite'
57
59
  converter.convert_to_tflite(
58
60
  pytorch_model,
59
- tflite_path=_TFLITE_PATH.value,
61
+ tflite_path=os.path.join(_TFLITE_PATH.value, output_filename),
60
62
  prefill_seq_len=_PREFILL_SEQ_LEN.value,
61
63
  quantize=_QUANTIZE.value,
62
64
  )
@@ -24,15 +24,25 @@ import transformers
24
24
 
25
25
  _PROMPTS = flags.DEFINE_multi_string(
26
26
  "prompts",
27
- "What is the meaning of life?",
27
+ "Instruct: Write an email about the weather Output:",
28
28
  "The input prompts to generate answers.",
29
29
  )
30
30
 
31
+ _MAX_NEW_TOKENS = flags.DEFINE_integer(
32
+ "max_new_tokens",
33
+ 30,
34
+ "The maximum size of the generated tokens.",
35
+ )
31
36
 
32
37
  def main(_):
33
38
  checkpoint = kagglehub.model_download("Microsoft/phi/transformers/2")
34
39
  verifier.log_msg("Loading the original model from", checkpoint)
35
- original_model = transformers.AutoModelForCausalLM.from_pretrained(checkpoint)
40
+ generation_config = transformers.GenerationConfig.from_pretrained(checkpoint)
41
+ generation_config.max_new_tokens = _MAX_NEW_TOKENS.value
42
+ wrapper_model = verifier.ModelWrapper(
43
+ model=transformers.AutoModelForCausalLM.from_pretrained(checkpoint),
44
+ hf_generation_config=generation_config,
45
+ )
36
46
 
37
47
  verifier.log_msg("Building the reauthored model from", checkpoint)
38
48
  reauthored_model = phi2.build_model(checkpoint)
@@ -41,7 +51,7 @@ def main(_):
41
51
  tokenizer = transformers.AutoTokenizer.from_pretrained(checkpoint)
42
52
 
43
53
  verifier.verify_reauthored_model(
44
- original_model=original_model,
54
+ original_model=wrapper_model,
45
55
  reauthored_model=reauthored_model,
46
56
  tokenizer=tokenizer,
47
57
  prompts=_PROMPTS.value,
@@ -30,17 +30,17 @@ _CHECKPOINT_PATH = flags.DEFINE_string(
30
30
  )
31
31
  _TFLITE_PATH = flags.DEFINE_string(
32
32
  'tflite_path',
33
- '/tmp/smollm_q8_seq512_ekv1024.tflite',
33
+ '/tmp/',
34
34
  'The tflite file path to export.',
35
35
  )
36
36
  _PREFILL_SEQ_LEN = flags.DEFINE_integer(
37
37
  'prefill_seq_len',
38
- 512,
38
+ 1024,
39
39
  'The maximum size of prefill input tensor.',
40
40
  )
41
41
  _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
42
42
  'kv_cache_max_len',
43
- 1024,
43
+ 1280,
44
44
  'The maximum size of KV cache buffer, including both prefill and decode.',
45
45
  )
46
46
  _QUANTIZE = flags.DEFINE_bool(
@@ -54,9 +54,11 @@ def main(_):
54
54
  pytorch_model = smollm.build_model(
55
55
  _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
56
56
  )
57
+ quant_suffix = 'q8' if _QUANTIZE.value else 'f32'
58
+ output_filename = f'smollm_{quant_suffix}_seq{_PREFILL_SEQ_LEN.value}_ekv{_KV_CACHE_MAX_LEN.value}.tflite'
57
59
  converter.convert_to_tflite(
58
60
  pytorch_model,
59
- tflite_path=_TFLITE_PATH.value,
61
+ tflite_path=os.path.join(_TFLITE_PATH.value, output_filename),
60
62
  prefill_seq_len=_PREFILL_SEQ_LEN.value,
61
63
  quantize=_QUANTIZE.value,
62
64
  )
@@ -33,8 +33,9 @@ _PROMPTS = flags.DEFINE_multi_string(
33
33
  def main(_):
34
34
  checkpoint = "HuggingFaceTB/SmolLM-135M"
35
35
  verifier.log_msg("Loading the original model from", checkpoint)
36
- original_model = transformers.AutoModelForCausalLM.from_pretrained(checkpoint)
37
-
36
+ wrapper_model = verifier.ModelWrapper(
37
+ model=transformers.AutoModelForCausalLM.from_pretrained(checkpoint),
38
+ )
38
39
  # Locate the cached dir.
39
40
  cached_config_file = transformers.utils.cached_file(
40
41
  checkpoint, transformers.utils.CONFIG_NAME
@@ -47,7 +48,7 @@ def main(_):
47
48
  tokenizer = transformers.AutoTokenizer.from_pretrained(checkpoint)
48
49
 
49
50
  verifier.verify_reauthored_model(
50
- original_model=original_model,
51
+ original_model=wrapper_model,
51
52
  reauthored_model=reauthored_model,
52
53
  tokenizer=tokenizer,
53
54
  prompts=_PROMPTS.value,
@@ -30,17 +30,17 @@ _CHECKPOINT_PATH = flags.DEFINE_string(
30
30
  )
31
31
  _TFLITE_PATH = flags.DEFINE_string(
32
32
  'tflite_path',
33
- '/tmp/tiny_llama_q8_seq512_ekv1024.tflite',
33
+ '/tmp/',
34
34
  'The tflite file path to export.',
35
35
  )
36
36
  _PREFILL_SEQ_LEN = flags.DEFINE_integer(
37
37
  'prefill_seq_len',
38
- 512,
38
+ 1024,
39
39
  'The maximum size of prefill input tensor.',
40
40
  )
41
41
  _KV_CACHE_MAX_LEN = flags.DEFINE_integer(
42
42
  'kv_cache_max_len',
43
- 1024,
43
+ 1280,
44
44
  'The maximum size of KV cache buffer, including both prefill and decode.',
45
45
  )
46
46
  _QUANTIZE = flags.DEFINE_bool(
@@ -54,9 +54,11 @@ def main(_):
54
54
  pytorch_model = tiny_llama.build_model(
55
55
  _CHECKPOINT_PATH.value, kv_cache_max_len=_KV_CACHE_MAX_LEN.value
56
56
  )
57
+ quant_suffix = 'q8' if _QUANTIZE.value else 'f32'
58
+ output_filename = f'tinyllama_{quant_suffix}_seq{_PREFILL_SEQ_LEN.value}_ekv{_KV_CACHE_MAX_LEN.value}.tflite'
57
59
  converter.convert_to_tflite(
58
60
  pytorch_model,
59
- tflite_path=_TFLITE_PATH.value,
61
+ tflite_path=os.path.join(_TFLITE_PATH.value, output_filename),
60
62
  prefill_seq_len=_PREFILL_SEQ_LEN.value,
61
63
  quantize=_QUANTIZE.value,
62
64
  )
@@ -33,10 +33,11 @@ _PROMPTS = flags.DEFINE_multi_string(
33
33
  def main(_):
34
34
  checkpoint = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
35
35
  verifier.log_msg("Loading the original model from", checkpoint)
36
- original_model = transformers.AutoModelForCausalLM.from_pretrained(
37
- checkpoint, trust_remote_code=True
36
+ wrapper_model = verifier.ModelWrapper(
37
+ model=transformers.AutoModelForCausalLM.from_pretrained(
38
+ checkpoint, trust_remote_code=True
39
+ ),
38
40
  )
39
-
40
41
  # Locate the cached dir.
41
42
  cached_config_file = transformers.utils.cached_file(
42
43
  checkpoint, transformers.utils.CONFIG_NAME
@@ -49,7 +50,7 @@ def main(_):
49
50
  tokenizer = transformers.AutoTokenizer.from_pretrained(checkpoint)
50
51
 
51
52
  verifier.verify_reauthored_model(
52
- original_model=original_model,
53
+ original_model=wrapper_model,
53
54
  reauthored_model=reauthored_model,
54
55
  tokenizer=tokenizer,
55
56
  prompts=_PROMPTS.value,
@@ -25,7 +25,7 @@ import numpy as np
25
25
  import torch
26
26
 
27
27
  from absl.testing import absltest as googletest
28
- from tensorflow.lite.python import interpreter
28
+ from ai_edge_litert import interpreter
29
29
 
30
30
 
31
31
  class TestModelConversion(googletest.TestCase):
@@ -28,7 +28,7 @@ import numpy as np
28
28
  import torch
29
29
 
30
30
  from absl.testing import absltest as googletest
31
- from tensorflow.lite.python import interpreter
31
+ from ai_edge_litert import interpreter
32
32
 
33
33
 
34
34
  class TestModelConversion(googletest.TestCase):
@@ -16,17 +16,66 @@
16
16
  """Common utility functions to verify the reauthored models."""
17
17
 
18
18
  import datetime
19
- from typing import List
19
+ from typing import List, Optional, Union
20
20
 
21
21
  from ai_edge_torch.generative.layers import kv_cache as kv_utils
22
22
  import numpy as np
23
23
  import torch
24
+ import transformers
24
25
 
25
26
 
26
27
  def log_msg(*args):
27
28
  print("[%s]" % datetime.datetime.now(), *args)
28
29
 
29
30
 
31
+ class ModelWrapper(torch.nn.Module):
32
+ """A wrapper for the model to be verified, this could be a HuggingFace model
33
+
34
+ or a regular PyTorch model.
35
+ """
36
+
37
+ def __init__(
38
+ self,
39
+ model: torch.nn.Module,
40
+ model_format: str = "huggingface",
41
+ hf_generation_config: Optional[transformers.GenerationConfig] = None,
42
+ ):
43
+ """Initializes the wrapper.
44
+
45
+ Args:
46
+ model (torch.nn.Module): The original model. This could be a model built
47
+ from HuggingFace transformers, or a regular PyTorch model.
48
+ model_format (str): The format of the model. It should be either
49
+ "huggingface" or "pytorch".
50
+ hf_generation_config (transformers.GenerationConfig): The HuggingFace
51
+ generation config. This config will only be used if the underlying model
52
+ is built from HuggingFace transformers.
53
+ """
54
+ super().__init__()
55
+ self.model = model
56
+ self.model_format = model_format
57
+ self.hf_generation_config = hf_generation_config
58
+
59
+ def generate(
60
+ self, inputs: torch.Tensor
61
+ ) -> Union[transformers.utils.ModelOutput, torch.LongTensor]:
62
+ if self.model_format == "huggingface":
63
+ return self.model.generate(
64
+ inputs=inputs, generation_config=self.hf_generation_config
65
+ )
66
+ else:
67
+ raise NotImplementedError(
68
+ "generate() is not implemented for model format: %s"
69
+ % self.model_format
70
+ )
71
+
72
+ def forward(
73
+ self,
74
+ inputs: torch.Tensor,
75
+ ):
76
+ return self.model.forward(inputs)
77
+
78
+
30
79
  def forward(
31
80
  model: torch.nn.Module,
32
81
  tokens: torch.Tensor,
@@ -75,7 +124,7 @@ def generate(
75
124
 
76
125
 
77
126
  def verify_with_input_ids(
78
- original_model: torch.nn.Module,
127
+ original_model: ModelWrapper,
79
128
  reauthored_model: torch.nn.Module,
80
129
  input_ids: torch.Tensor = torch.from_numpy(np.array([[1, 2, 3, 4]])).int(),
81
130
  kv_cache_max_len: int = 1024,
@@ -87,7 +136,7 @@ def verify_with_input_ids(
87
136
  It compares only one outputs from the original and the reauthored model.
88
137
 
89
138
  Args:
90
- original_model (torch.nn.Module): The original model.
139
+ original_model (ModelWrapper): The original model.
91
140
  reauthored_model (torch.nn.Module): The model reauthored with ai_edge_torch
92
141
  Generative API.
93
142
  input_ids (torch.Tensor): The input token IDs to forward.
@@ -119,7 +168,7 @@ def verify_with_input_ids(
119
168
 
120
169
 
121
170
  def verify_model_with_prompts(
122
- original_model: torch.nn.Module,
171
+ original_model: ModelWrapper,
123
172
  reauthored_model: torch.nn.Module,
124
173
  tokenizer: torch.nn.Module,
125
174
  prompts: str,
@@ -130,7 +179,7 @@ def verify_model_with_prompts(
130
179
  original and the reauthored model.
131
180
 
132
181
  Args:
133
- original_model (torch.nn.Module): The original model.
182
+ original_model (ModelWrapper): The original model.
134
183
  reauthored_model (torch.nn.Module): The model reauthored with ai_edge_torch
135
184
  Generative API.
136
185
  tokenizer (torch.nn.Module): The tokenizer.
@@ -156,7 +205,7 @@ def verify_model_with_prompts(
156
205
 
157
206
 
158
207
  def verify_reauthored_model(
159
- original_model: torch.nn.Module,
208
+ original_model: ModelWrapper,
160
209
  reauthored_model: torch.nn.Module,
161
210
  tokenizer: torch.nn.Module,
162
211
  prompts: List[str],
@@ -174,7 +223,7 @@ def verify_reauthored_model(
174
223
  It prints out "PASS" or "FAILED" to the console.
175
224
 
176
225
  Args:
177
- original_model (torch.nn.Module): The original model.
226
+ original_model (ModelWrapper): The original model.
178
227
  reauthored_model (torch.nn.Module): The model reauthored with ai_edge_torch
179
228
  Generative API.
180
229
  tokenizer (torch.nn.Module): The tokenizer.
ai_edge_torch/model.py CHANGED
@@ -27,6 +27,8 @@ from typing import Callable
27
27
  import numpy.typing as npt
28
28
  import tensorflow as tf
29
29
 
30
+ from ai_edge_litert import interpreter as tfl_interpreter # pylint: disable=g-direct-tensorflow-import
31
+
30
32
  DEFAULT_SIGNATURE_NAME = tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY
31
33
 
32
34
 
@@ -65,7 +67,7 @@ class TfLiteModel(Model):
65
67
  tflite_model: A TFlite serialized object.
66
68
  """
67
69
  self._tflite_model = tflite_model
68
- self._interpreter_builder = lambda: tf.lite.Interpreter(
70
+ self._interpreter_builder = lambda: tfl_interpreter.Interpreter(
69
71
  model_content=self._tflite_model,
70
72
  experimental_default_delegate_latest_features=True,
71
73
  )
@@ -75,12 +77,13 @@ class TfLiteModel(Model):
75
77
  return self._tflite_model
76
78
 
77
79
  def set_interpreter_builder(
78
- self, builder: Callable[[], tf.lite.Interpreter]
80
+ self, builder: Callable[[], tfl_interpreter.Interpreter]
79
81
  ) -> None:
80
82
  """Sets a custom interpreter builder.
81
83
 
82
84
  Args:
83
- builder: A function that returns a `tf.lite.Interpreter` or its subclass.
85
+ builder: A function that returns a `tfl_interpreter.Interpreter` or its
86
+ subclass.
84
87
  """
85
88
  self._interpreter_builder = builder
86
89
 
@@ -166,7 +169,7 @@ class TfLiteModel(Model):
166
169
 
167
170
  # Check if this is indeed a tflite model:
168
171
  try:
169
- interpreter = tf.lite.Interpreter(model_content=model_content)
172
+ interpreter = tfl_interpreter.Interpreter(model_content=model_content)
170
173
  interpreter.get_signature_list()
171
174
  except:
172
175
  return None
ai_edge_torch/version.py CHANGED
@@ -13,4 +13,4 @@
13
13
  # limitations under the License.
14
14
  # ==============================================================================
15
15
 
16
- __version__ = "0.3.0.dev20240919"
16
+ __version__ = "0.3.0.dev20240920"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ai-edge-torch-nightly
3
- Version: 0.3.0.dev20240919
3
+ Version: 0.3.0.dev20240920
4
4
  Summary: Supporting PyTorch models with the Google AI Edge TFLite runtime.
5
5
  Home-page: https://github.com/google-ai-edge/ai-edge-torch
6
6
  Keywords: On-Device ML,AI,Google,TFLite,PyTorch,LLMs,GenAI
@@ -2,8 +2,8 @@ ai_edge_torch/__init__.py,sha256=48qP37uHT90YPs4eIUQxCiWVwqGEX3idCUs6mQKvX1U,116
2
2
  ai_edge_torch/config.py,sha256=FMWeCH2b7HYILBvaI1iZNnYCO4WAhDOwBZBmIE-xrF0,909
3
3
  ai_edge_torch/conftest.py,sha256=r0GTrhMRhlmOGrrkvumHN8hkmyug6WvF60vWq8wRIBI,758
4
4
  ai_edge_torch/fx_pass_base.py,sha256=D86Gw3pIRcpnTebUPKlnPbPGJae1S6Fw4DZZ3ZkD0zw,3730
5
- ai_edge_torch/model.py,sha256=NYV6Mkaje_ditIEI_s_7nLP_-8i4kbGM8nRzieVkbUI,5397
6
- ai_edge_torch/version.py,sha256=N5hYc9s2RU44J1_oe0UfJhTFo0d4JvMlKvxNlYtK0GI,706
5
+ ai_edge_torch/model.py,sha256=N-pNpTxzhaFGhWhnSGd70lBzb9VlEhTOq5mddU7bvvI,5542
6
+ ai_edge_torch/version.py,sha256=-oH0R07HZpydzqltOWclHB1dbcc4VycTlZcnDYtS89g,706
7
7
  ai_edge_torch/_convert/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
8
8
  ai_edge_torch/_convert/conversion.py,sha256=5uPwHhmc6kwiIz-CqaiHDejf2SOWMHrb-rYEHm69wKc,3801
9
9
  ai_edge_torch/_convert/conversion_utils.py,sha256=Sr8qXVcTwc-ZnZmK7yxVrIOOp1S_vNrwzC0zUvLTI2o,2160
@@ -25,7 +25,7 @@ ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/layout_partitio
25
25
  ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/layout_partitioners/greedy.py,sha256=L_x8BrF7UDah-SYl-pG11I6CIckdU9kBTUHcmwW4cts,2420
26
26
  ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/layout_partitioners/min_cut.py,sha256=mzfL9cf0qBnpmxM_OlMQFvQsEZV2B_Mia9yEJV4J7rI,7135
27
27
  ai_edge_torch/_convert/test/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
28
- ai_edge_torch/_convert/test/test_convert.py,sha256=FSufFZEeTLBpUnzE1Iy-LvNN0mhDynWMNg7Mei8RpLQ,14973
28
+ ai_edge_torch/_convert/test/test_convert.py,sha256=40QRxQFNeSRr4dLXJkzG-wKUlvJtsfv62cdvRrmBv5w,15097
29
29
  ai_edge_torch/_convert/test/test_convert_composites.py,sha256=BCIODgxMI_3MxMLfNWYMGjcz-al-J3z5eDHCiZJXNwY,7992
30
30
  ai_edge_torch/_convert/test/test_convert_multisig.py,sha256=6_C2R9--KyNR7_oezZIAfyTSR97tOeEWy4XGcbSxBDE,5778
31
31
  ai_edge_torch/_convert/test/test_to_channel_last_io.py,sha256=1o-gUiwzIuO67FNAJ8DeyKv8fVUeZVNNNwofNVDjYeU,3024
@@ -39,22 +39,22 @@ ai_edge_torch/experimental/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrK
39
39
  ai_edge_torch/generative/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
40
40
  ai_edge_torch/generative/examples/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
41
41
  ai_edge_torch/generative/examples/gemma/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
42
- ai_edge_torch/generative/examples/gemma/convert_gemma2_to_tflite.py,sha256=09VbyWErOMP9BXGwZpwvqzN5RaOqRigsELfxNRVeWns,2024
43
- ai_edge_torch/generative/examples/gemma/convert_to_tflite.py,sha256=qJKQu6lKuSVhn8JR7KUeInq0u6yqgxEi7hfKCrZrIqY,2019
42
+ ai_edge_torch/generative/examples/gemma/convert_gemma2_to_tflite.py,sha256=RZDs6oY-NLYrPNtfuJDweIHzGUL2kzpIc3AW_1p8gGg,2186
43
+ ai_edge_torch/generative/examples/gemma/convert_to_tflite.py,sha256=t8Qg10obnEzeoMeyHnZhyNBN7G85SGy-au8Y8nehq8E,2181
44
44
  ai_edge_torch/generative/examples/gemma/gemma.py,sha256=hjpSPzEjPHuxwRJ-vHHtCCf2PSTnm30Mp0ajYYtDivo,7489
45
45
  ai_edge_torch/generative/examples/gemma/gemma2.py,sha256=gCLOti-4xHunjphNBbx9St6faRteSakm8Oex6R1Xek0,10272
46
46
  ai_edge_torch/generative/examples/openelm/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
47
- ai_edge_torch/generative/examples/openelm/convert_to_tflite.py,sha256=HnqP3te1Qvy4SKaaqPrsG05eojiKDJShp4H3jPC9tYg,2023
47
+ ai_edge_torch/generative/examples/openelm/convert_to_tflite.py,sha256=85FVEt6cKFP2UzCLC78tAkbwGlGhAArtG7Wa75NxJik,2185
48
48
  ai_edge_torch/generative/examples/openelm/openelm.py,sha256=gGkHELNrt4xqnu11fCh3sJbZ7OsPyvoiF1J1aKCs5r8,7532
49
- ai_edge_torch/generative/examples/openelm/verify.py,sha256=2qFdyLfcefdA3s1KQ-ZGWo4XReMXkEQAvpUEyJE5iqM,2057
49
+ ai_edge_torch/generative/examples/openelm/verify.py,sha256=BvK4c8jodQBy2l3NnvCjlBB0qaA7EYwPNKklvFR4k_o,2103
50
50
  ai_edge_torch/generative/examples/phi/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
51
- ai_edge_torch/generative/examples/phi/convert_to_tflite.py,sha256=viIkbAgknE3zxavTZtib87cMIG2_-jJXtxJPcmB2pGQ,2007
51
+ ai_edge_torch/generative/examples/phi/convert_to_tflite.py,sha256=3go690yX6PFeXMdpY7y4JZorAwxX0HT_b_pKZieauvk,2169
52
52
  ai_edge_torch/generative/examples/phi/phi2.py,sha256=YwAszA53aOjvaMJ5wua2-5rP79N21Un_Y5yBCfFSYNU,6189
53
- ai_edge_torch/generative/examples/phi/verify.py,sha256=R9BjOArnn-3svoIApmP1NwO47n8KIFikOF0_MEgTOa4,1770
53
+ ai_edge_torch/generative/examples/phi/verify.py,sha256=5bKONolW8JIsQAzMHIvh_OSytoJVVJqDZEcxjhciFnI,2136
54
54
  ai_edge_torch/generative/examples/smollm/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
55
- ai_edge_torch/generative/examples/smollm/convert_to_tflite.py,sha256=86hvBleyFXWmwy3Ke5J7x7WcCtG20D2kiBNrodE0R4w,2017
55
+ ai_edge_torch/generative/examples/smollm/convert_to_tflite.py,sha256=zPrDTDeRVWFi9DS32uNi-RLpzOStFOk5MhNla4ixeew,2179
56
56
  ai_edge_torch/generative/examples/smollm/smollm.py,sha256=hyhMk-b5762Q2xmjdD47g85dcbBSNJXNPIsifm1DRto,3239
57
- ai_edge_torch/generative/examples/smollm/verify.py,sha256=JzidfVMMFDXzDdwn7ToDPuMo6eaoENNZGpEzX3f61Jk,1976
57
+ ai_edge_torch/generative/examples/smollm/verify.py,sha256=wsoy3CaHZhrdJjkJJYir7xxxwgCvLprMnh8QxT0hEkc,2013
58
58
  ai_edge_torch/generative/examples/stable_diffusion/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
59
59
  ai_edge_torch/generative/examples/stable_diffusion/attention.py,sha256=kDWG6MlIGa89zC5KSRcJlw2c4ITuw8KcchtfmF55f4g,3545
60
60
  ai_edge_torch/generative/examples/stable_diffusion/clip.py,sha256=tL6w2dr6VP66IXjSKo9StDNP-wl0RO3fh6dIliiYlFA,4656
@@ -78,9 +78,9 @@ ai_edge_torch/generative/examples/test_models/__init__.py,sha256=hHLluseD2R0Hh4W
78
78
  ai_edge_torch/generative/examples/test_models/toy_model.py,sha256=QyLeCqDnk71WvvFH68g9UeF-HytonSk1ItGF9dc7Zj8,5854
79
79
  ai_edge_torch/generative/examples/test_models/toy_model_with_kv_cache.py,sha256=e_Kqm5dStSrNE9_aIYC-vYJRsqLn-hJVkmR4QjYqZI0,5913
80
80
  ai_edge_torch/generative/examples/tiny_llama/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
81
- ai_edge_torch/generative/examples/tiny_llama/convert_to_tflite.py,sha256=Yg5G1LePoryeTib35lqICqaDW6foLUzSRgwJ2FlklIw,2040
81
+ ai_edge_torch/generative/examples/tiny_llama/convert_to_tflite.py,sha256=ekxd8efjMgEvauUu3PidWOC-DszPHn5sqU753F7sJIM,2201
82
82
  ai_edge_torch/generative/examples/tiny_llama/tiny_llama.py,sha256=tlWpa7Aun3u3w5b-9EBtW7olhmSf8W-tn5bKUIwC-ys,6044
83
- ai_edge_torch/generative/examples/tiny_llama/verify.py,sha256=jld5PlGOQXMIWc1WoDYL_1nnsoVzRfrg-WgnsxRgaEU,2041
83
+ ai_edge_torch/generative/examples/tiny_llama/verify.py,sha256=27oBf706_AKX7amfp2THF9J0G3AUEEecGaXv025idKA,2086
84
84
  ai_edge_torch/generative/fx_passes/__init__.py,sha256=jrzCB3ZyY_t5jJM1e2Czdt3DjAIL43R0_a-T-I7wOzw,1155
85
85
  ai_edge_torch/generative/fx_passes/remove_sdpa_zero_mask_pass.py,sha256=hhxSQvkDMv0isZJhmuLiod66ZODaJ8uSPSVTJVHBabQ,1931
86
86
  ai_edge_torch/generative/layers/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
@@ -107,8 +107,8 @@ ai_edge_torch/generative/quantize/supported_schemes.py,sha256=FjdycEOvxRgBmQdZVu
107
107
  ai_edge_torch/generative/test/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
108
108
  ai_edge_torch/generative/test/test_kv_cache.py,sha256=W6Bh0gYDzmwb0j9HdD5_D7Z7FPToP2HSyFrmwIXuFqo,3793
109
109
  ai_edge_torch/generative/test/test_loader.py,sha256=8y74ChO3CZCfEi1eCf3-w47kRgAI4qPYCXpi8rTQXMA,3378
110
- ai_edge_torch/generative/test/test_model_conversion.py,sha256=DBlqxW2IT-dZYzEfOMAp86Wtqiu6kgSWZ9BKZR1Clrw,5467
111
- ai_edge_torch/generative/test/test_model_conversion_large.py,sha256=dUYFarOldejqbMpa0j0vIDvXlWPAancuI8di3XkGxm8,4498
110
+ ai_edge_torch/generative/test/test_model_conversion.py,sha256=s-EVLOQGjIeVtgNI8Ggs37pkRdErAliT6NhrrFigPOE,5459
111
+ ai_edge_torch/generative/test/test_model_conversion_large.py,sha256=PtePuBqVMLjxq2cDIIXXqaz7zsn3R19oilFyIVJRFi8,4490
112
112
  ai_edge_torch/generative/test/test_quantize.py,sha256=8geJhKwYBU20m0mdGPD1BUFwQ0lZKNtCB04SOLO18y4,5980
113
113
  ai_edge_torch/generative/test/utils.py,sha256=YvEhO2HIj1LkBs5du1UxY-cGRW9HMyAYsOUhgsTrTpA,1796
114
114
  ai_edge_torch/generative/utilities/__init__.py,sha256=-_jxnnFnCgnTU4oTm4MnRsvL5lqhomBNdFBbqfmfHPo,720
@@ -116,7 +116,7 @@ ai_edge_torch/generative/utilities/converter.py,sha256=MQUg2ZLmfk_2csWmQWKD_II0b
116
116
  ai_edge_torch/generative/utilities/loader.py,sha256=b9iotIhVDX-Zc9XjIDUaLxnV395AyBnkQe3dV5YA7Co,13297
117
117
  ai_edge_torch/generative/utilities/stable_diffusion_loader.py,sha256=dqPD9qRXEWtU3ombslOC-BE2l_dMwHoCNu7NsIJhsso,36158
118
118
  ai_edge_torch/generative/utilities/t5_loader.py,sha256=tEsfy8-ymzbbjOIc-oesXF3yGyyWtJgFXn2s7VOavt8,16961
119
- ai_edge_torch/generative/utilities/verifier.py,sha256=QAv1uJdI5o1yfphr_DpzxhZswKa4VG3JZUpqbCCWKMk,7114
119
+ ai_edge_torch/generative/utilities/verifier.py,sha256=7DoYtkilz4wjWnXfdydIGNgTG1udZIydFxdbpIcKbMQ,8625
120
120
  ai_edge_torch/hlfb/__init__.py,sha256=sH4um75na-O8tzxN6chFyp6Y4xnexsE7kUQpZySv6dE,735
121
121
  ai_edge_torch/hlfb/mark_pattern/__init__.py,sha256=cjTprggj_cuktSCm7-A25e7Shop3k63ylp7sdZmtZ8o,4790
122
122
  ai_edge_torch/hlfb/mark_pattern/passes.py,sha256=pjkKcI1nHECPluAt87cFBrt1DP0f3ge7rHq1NhCkBIE,1936
@@ -163,8 +163,8 @@ ai_edge_torch/quantize/quant_config.py,sha256=U0KisSW-uZkoMJcy-ZP9W57p3tsa594fr9
163
163
  ai_edge_torch/testing/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
164
164
  ai_edge_torch/testing/model_coverage/__init__.py,sha256=5P8J6Zk5YYtDvTBucFvB9NGSRI7Gw_24WnrbhXgycEE,765
165
165
  ai_edge_torch/testing/model_coverage/model_coverage.py,sha256=UPB448aMDUyC0HNYVqio2rcJPnDN0tBQMP08J6vPYew,4718
166
- ai_edge_torch_nightly-0.3.0.dev20240919.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
167
- ai_edge_torch_nightly-0.3.0.dev20240919.dist-info/METADATA,sha256=NkHYIOMz-5DNKJuSQ8wE-3Nz1R6a9YZ59M-Nq8sAnJg,1859
168
- ai_edge_torch_nightly-0.3.0.dev20240919.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
169
- ai_edge_torch_nightly-0.3.0.dev20240919.dist-info/top_level.txt,sha256=5KXRaF2hwkApYxf7Y8y_tVb9aulGTlbOoNdbx1aKRkE,14
170
- ai_edge_torch_nightly-0.3.0.dev20240919.dist-info/RECORD,,
166
+ ai_edge_torch_nightly-0.3.0.dev20240920.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
167
+ ai_edge_torch_nightly-0.3.0.dev20240920.dist-info/METADATA,sha256=m60oD-H8W2EMVolDGw02tMYcKDrotaTaLtsZwzr_Kyk,1859
168
+ ai_edge_torch_nightly-0.3.0.dev20240920.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
169
+ ai_edge_torch_nightly-0.3.0.dev20240920.dist-info/top_level.txt,sha256=5KXRaF2hwkApYxf7Y8y_tVb9aulGTlbOoNdbx1aKRkE,14
170
+ ai_edge_torch_nightly-0.3.0.dev20240920.dist-info/RECORD,,