ai-edge-torch-nightly 0.3.0.dev20250114__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- ai_edge_torch/__init__.py +32 -0
- ai_edge_torch/_config.py +69 -0
- ai_edge_torch/_convert/__init__.py +14 -0
- ai_edge_torch/_convert/conversion.py +153 -0
- ai_edge_torch/_convert/conversion_utils.py +64 -0
- ai_edge_torch/_convert/converter.py +270 -0
- ai_edge_torch/_convert/fx_passes/__init__.py +23 -0
- ai_edge_torch/_convert/fx_passes/build_aten_composite_pass.py +288 -0
- ai_edge_torch/_convert/fx_passes/build_interpolate_composite_pass.py +131 -0
- ai_edge_torch/_convert/fx_passes/inject_mlir_debuginfo_pass.py +73 -0
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/__init__.py +16 -0
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/layout_check.py +258 -0
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/layout_mark.py +50 -0
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/layout_partitioners/__init__.py +18 -0
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/layout_partitioners/greedy.py +68 -0
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/layout_partitioners/min_cut.py +216 -0
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/layout_rewrite.py +449 -0
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/op_func_registry.py +30 -0
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/pass_body.py +303 -0
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/utils.py +64 -0
- ai_edge_torch/_convert/fx_passes/remove_non_user_outputs_pass.py +52 -0
- ai_edge_torch/_convert/signature.py +66 -0
- ai_edge_torch/_convert/test/__init__.py +14 -0
- ai_edge_torch/_convert/test/test_convert.py +558 -0
- ai_edge_torch/_convert/test/test_convert_composites.py +234 -0
- ai_edge_torch/_convert/test/test_convert_multisig.py +189 -0
- ai_edge_torch/_convert/test/test_to_channel_last_io.py +96 -0
- ai_edge_torch/_convert/to_channel_last_io.py +92 -0
- ai_edge_torch/conftest.py +20 -0
- ai_edge_torch/debug/__init__.py +17 -0
- ai_edge_torch/debug/culprit.py +496 -0
- ai_edge_torch/debug/test/__init__.py +14 -0
- ai_edge_torch/debug/test/test_culprit.py +140 -0
- ai_edge_torch/debug/test/test_search_model.py +51 -0
- ai_edge_torch/debug/utils.py +59 -0
- ai_edge_torch/experimental/__init__.py +14 -0
- ai_edge_torch/fx_pass_base.py +110 -0
- ai_edge_torch/generative/__init__.py +14 -0
- ai_edge_torch/generative/examples/__init__.py +14 -0
- ai_edge_torch/generative/examples/amd_llama_135m/__init__.py +14 -0
- ai_edge_torch/generative/examples/amd_llama_135m/amd_llama_135m.py +87 -0
- ai_edge_torch/generative/examples/amd_llama_135m/convert_to_tflite.py +70 -0
- ai_edge_torch/generative/examples/amd_llama_135m/verify.py +72 -0
- ai_edge_torch/generative/examples/gemma/__init__.py +14 -0
- ai_edge_torch/generative/examples/gemma/convert_gemma1_to_tflite.py +80 -0
- ai_edge_torch/generative/examples/gemma/convert_gemma2_to_tflite.py +80 -0
- ai_edge_torch/generative/examples/gemma/gemma1.py +107 -0
- ai_edge_torch/generative/examples/gemma/gemma2.py +295 -0
- ai_edge_torch/generative/examples/gemma/verify_gemma1.py +56 -0
- ai_edge_torch/generative/examples/gemma/verify_gemma2.py +43 -0
- ai_edge_torch/generative/examples/gemma/verify_util.py +157 -0
- ai_edge_torch/generative/examples/llama/__init__.py +14 -0
- ai_edge_torch/generative/examples/llama/convert_to_tflite.py +91 -0
- ai_edge_torch/generative/examples/llama/llama.py +196 -0
- ai_edge_torch/generative/examples/llama/verify.py +88 -0
- ai_edge_torch/generative/examples/moonshine/__init__.py +14 -0
- ai_edge_torch/generative/examples/moonshine/convert_moonshine_to_tflite.py +50 -0
- ai_edge_torch/generative/examples/moonshine/moonshine.py +103 -0
- ai_edge_torch/generative/examples/openelm/__init__.py +14 -0
- ai_edge_torch/generative/examples/openelm/convert_to_tflite.py +80 -0
- ai_edge_torch/generative/examples/openelm/openelm.py +127 -0
- ai_edge_torch/generative/examples/openelm/verify.py +71 -0
- ai_edge_torch/generative/examples/paligemma/__init__.py +14 -0
- ai_edge_torch/generative/examples/paligemma/convert_to_tflite.py +95 -0
- ai_edge_torch/generative/examples/paligemma/decoder.py +151 -0
- ai_edge_torch/generative/examples/paligemma/decoder2.py +177 -0
- ai_edge_torch/generative/examples/paligemma/image_encoder.py +160 -0
- ai_edge_torch/generative/examples/paligemma/paligemma.py +179 -0
- ai_edge_torch/generative/examples/paligemma/verify.py +161 -0
- ai_edge_torch/generative/examples/paligemma/verify_decoder.py +75 -0
- ai_edge_torch/generative/examples/paligemma/verify_decoder2.py +72 -0
- ai_edge_torch/generative/examples/paligemma/verify_image_encoder.py +99 -0
- ai_edge_torch/generative/examples/phi/__init__.py +14 -0
- ai_edge_torch/generative/examples/phi/convert_phi3_to_tflite.py +80 -0
- ai_edge_torch/generative/examples/phi/convert_to_tflite.py +80 -0
- ai_edge_torch/generative/examples/phi/phi2.py +107 -0
- ai_edge_torch/generative/examples/phi/phi3.py +219 -0
- ai_edge_torch/generative/examples/phi/verify.py +64 -0
- ai_edge_torch/generative/examples/phi/verify_phi3.py +69 -0
- ai_edge_torch/generative/examples/qwen/__init__.py +14 -0
- ai_edge_torch/generative/examples/qwen/convert_to_tflite.py +93 -0
- ai_edge_torch/generative/examples/qwen/qwen.py +134 -0
- ai_edge_torch/generative/examples/qwen/verify.py +88 -0
- ai_edge_torch/generative/examples/smollm/__init__.py +14 -0
- ai_edge_torch/generative/examples/smollm/convert_to_tflite.py +80 -0
- ai_edge_torch/generative/examples/smollm/convert_v2_to_tflite.py +71 -0
- ai_edge_torch/generative/examples/smollm/smollm.py +125 -0
- ai_edge_torch/generative/examples/smollm/verify.py +86 -0
- ai_edge_torch/generative/examples/stable_diffusion/__init__.py +14 -0
- ai_edge_torch/generative/examples/stable_diffusion/attention.py +108 -0
- ai_edge_torch/generative/examples/stable_diffusion/clip.py +185 -0
- ai_edge_torch/generative/examples/stable_diffusion/convert_to_tflite.py +173 -0
- ai_edge_torch/generative/examples/stable_diffusion/decoder.py +398 -0
- ai_edge_torch/generative/examples/stable_diffusion/diffusion.py +749 -0
- ai_edge_torch/generative/examples/stable_diffusion/encoder.py +119 -0
- ai_edge_torch/generative/examples/stable_diffusion/pipeline.py +254 -0
- ai_edge_torch/generative/examples/stable_diffusion/samplers/__init__.py +19 -0
- ai_edge_torch/generative/examples/stable_diffusion/samplers/k_euler.py +62 -0
- ai_edge_torch/generative/examples/stable_diffusion/samplers/k_euler_ancestral.py +66 -0
- ai_edge_torch/generative/examples/stable_diffusion/samplers/k_lms.py +74 -0
- ai_edge_torch/generative/examples/stable_diffusion/samplers/sampler.py +39 -0
- ai_edge_torch/generative/examples/stable_diffusion/tokenizer.py +111 -0
- ai_edge_torch/generative/examples/stable_diffusion/util.py +77 -0
- ai_edge_torch/generative/examples/t5/__init__.py +14 -0
- ai_edge_torch/generative/examples/t5/convert_to_tflite.py +138 -0
- ai_edge_torch/generative/examples/t5/t5.py +655 -0
- ai_edge_torch/generative/examples/t5/t5_attention.py +246 -0
- ai_edge_torch/generative/examples/test_models/__init__.py +14 -0
- ai_edge_torch/generative/examples/test_models/convert_toy_model.py +105 -0
- ai_edge_torch/generative/examples/test_models/toy_model.py +156 -0
- ai_edge_torch/generative/examples/test_models/toy_model_with_kv_cache.py +138 -0
- ai_edge_torch/generative/examples/tiny_llama/__init__.py +14 -0
- ai_edge_torch/generative/examples/tiny_llama/convert_to_tflite.py +80 -0
- ai_edge_torch/generative/examples/tiny_llama/tiny_llama.py +88 -0
- ai_edge_torch/generative/examples/tiny_llama/verify.py +72 -0
- ai_edge_torch/generative/fx_passes/__init__.py +30 -0
- ai_edge_torch/generative/fx_passes/remove_sdpa_zero_mask_pass.py +50 -0
- ai_edge_torch/generative/layers/__init__.py +14 -0
- ai_edge_torch/generative/layers/attention.py +399 -0
- ai_edge_torch/generative/layers/attention_utils.py +210 -0
- ai_edge_torch/generative/layers/builder.py +160 -0
- ai_edge_torch/generative/layers/feed_forward.py +120 -0
- ai_edge_torch/generative/layers/kv_cache.py +204 -0
- ai_edge_torch/generative/layers/lora.py +557 -0
- ai_edge_torch/generative/layers/model_config.py +238 -0
- ai_edge_torch/generative/layers/normalization.py +222 -0
- ai_edge_torch/generative/layers/rotary_position_embedding.py +94 -0
- ai_edge_torch/generative/layers/scaled_dot_product_attention.py +144 -0
- ai_edge_torch/generative/layers/unet/__init__.py +14 -0
- ai_edge_torch/generative/layers/unet/blocks_2d.py +806 -0
- ai_edge_torch/generative/layers/unet/builder.py +50 -0
- ai_edge_torch/generative/layers/unet/model_config.py +282 -0
- ai_edge_torch/generative/quantize/__init__.py +14 -0
- ai_edge_torch/generative/quantize/example.py +47 -0
- ai_edge_torch/generative/quantize/quant_attrs.py +68 -0
- ai_edge_torch/generative/quantize/quant_recipe.py +154 -0
- ai_edge_torch/generative/quantize/quant_recipe_utils.py +62 -0
- ai_edge_torch/generative/quantize/quant_recipes.py +56 -0
- ai_edge_torch/generative/quantize/supported_schemes.py +32 -0
- ai_edge_torch/generative/test/__init__.py +14 -0
- ai_edge_torch/generative/test/test_custom_dus.py +107 -0
- ai_edge_torch/generative/test/test_kv_cache.py +120 -0
- ai_edge_torch/generative/test/test_loader.py +83 -0
- ai_edge_torch/generative/test/test_lora.py +147 -0
- ai_edge_torch/generative/test/test_model_conversion.py +191 -0
- ai_edge_torch/generative/test/test_model_conversion_large.py +362 -0
- ai_edge_torch/generative/test/test_quantize.py +183 -0
- ai_edge_torch/generative/test/utils.py +82 -0
- ai_edge_torch/generative/utilities/__init__.py +15 -0
- ai_edge_torch/generative/utilities/converter.py +215 -0
- ai_edge_torch/generative/utilities/dynamic_update_slice.py +56 -0
- ai_edge_torch/generative/utilities/loader.py +398 -0
- ai_edge_torch/generative/utilities/model_builder.py +180 -0
- ai_edge_torch/generative/utilities/moonshine_loader.py +154 -0
- ai_edge_torch/generative/utilities/stable_diffusion_loader.py +1032 -0
- ai_edge_torch/generative/utilities/t5_loader.py +512 -0
- ai_edge_torch/generative/utilities/transformers_verifier.py +42 -0
- ai_edge_torch/generative/utilities/verifier.py +335 -0
- ai_edge_torch/hlfb/__init__.py +16 -0
- ai_edge_torch/hlfb/mark_pattern/__init__.py +153 -0
- ai_edge_torch/hlfb/mark_pattern/fx_utils.py +69 -0
- ai_edge_torch/hlfb/mark_pattern/pattern.py +288 -0
- ai_edge_torch/hlfb/test/__init__.py +14 -0
- ai_edge_torch/hlfb/test/test_mark_pattern.py +185 -0
- ai_edge_torch/lowertools/__init__.py +18 -0
- ai_edge_torch/lowertools/_shim.py +86 -0
- ai_edge_torch/lowertools/common_utils.py +142 -0
- ai_edge_torch/lowertools/odml_torch_utils.py +260 -0
- ai_edge_torch/lowertools/test_utils.py +62 -0
- ai_edge_torch/lowertools/torch_xla_utils.py +301 -0
- ai_edge_torch/lowertools/translate_recipe.py +163 -0
- ai_edge_torch/model.py +177 -0
- ai_edge_torch/odml_torch/__init__.py +20 -0
- ai_edge_torch/odml_torch/_torch_future.py +88 -0
- ai_edge_torch/odml_torch/_torch_library.py +19 -0
- ai_edge_torch/odml_torch/composite/__init__.py +16 -0
- ai_edge_torch/odml_torch/composite/mark_tensor.py +120 -0
- ai_edge_torch/odml_torch/composite/stablehlo_composite_builder.py +106 -0
- ai_edge_torch/odml_torch/debuginfo/__init__.py +16 -0
- ai_edge_torch/odml_torch/debuginfo/_build.py +43 -0
- ai_edge_torch/odml_torch/debuginfo/_op_polyfill.py +55 -0
- ai_edge_torch/odml_torch/export.py +403 -0
- ai_edge_torch/odml_torch/export_utils.py +157 -0
- ai_edge_torch/odml_torch/jax_bridge/__init__.py +18 -0
- ai_edge_torch/odml_torch/jax_bridge/_wrap.py +180 -0
- ai_edge_torch/odml_torch/jax_bridge/utils.py +75 -0
- ai_edge_torch/odml_torch/lowerings/__init__.py +27 -0
- ai_edge_torch/odml_torch/lowerings/_basic.py +294 -0
- ai_edge_torch/odml_torch/lowerings/_batch_norm.py +65 -0
- ai_edge_torch/odml_torch/lowerings/_convolution.py +243 -0
- ai_edge_torch/odml_torch/lowerings/_jax_lowerings.py +285 -0
- ai_edge_torch/odml_torch/lowerings/_layer_norm.py +87 -0
- ai_edge_torch/odml_torch/lowerings/_quantized_decomposed.py +177 -0
- ai_edge_torch/odml_torch/lowerings/_rand.py +142 -0
- ai_edge_torch/odml_torch/lowerings/context.py +42 -0
- ai_edge_torch/odml_torch/lowerings/decomp.py +69 -0
- ai_edge_torch/odml_torch/lowerings/registry.py +65 -0
- ai_edge_torch/odml_torch/lowerings/utils.py +201 -0
- ai_edge_torch/odml_torch/passes/__init__.py +38 -0
- ai_edge_torch/odml_torch/tf_integration.py +156 -0
- ai_edge_torch/quantize/__init__.py +16 -0
- ai_edge_torch/quantize/pt2e_quantizer.py +466 -0
- ai_edge_torch/quantize/pt2e_quantizer_utils.py +1061 -0
- ai_edge_torch/quantize/quant_config.py +85 -0
- ai_edge_torch/testing/__init__.py +14 -0
- ai_edge_torch/testing/model_coverage/__init__.py +16 -0
- ai_edge_torch/testing/model_coverage/model_coverage.py +145 -0
- ai_edge_torch/version.py +16 -0
- ai_edge_torch_nightly-0.3.0.dev20250114.dist-info/LICENSE +202 -0
- ai_edge_torch_nightly-0.3.0.dev20250114.dist-info/METADATA +44 -0
- ai_edge_torch_nightly-0.3.0.dev20250114.dist-info/RECORD +213 -0
- ai_edge_torch_nightly-0.3.0.dev20250114.dist-info/WHEEL +5 -0
- ai_edge_torch_nightly-0.3.0.dev20250114.dist-info/top_level.txt +1 -0
@@ -0,0 +1,191 @@
|
|
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
|
+
"""Testing model conversion for a few gen-ai models."""
|
17
|
+
|
18
|
+
import ai_edge_torch
|
19
|
+
from ai_edge_torch.generative.examples.test_models import toy_model_with_kv_cache
|
20
|
+
from ai_edge_torch.generative.examples.tiny_llama import tiny_llama
|
21
|
+
from ai_edge_torch.generative.layers import kv_cache
|
22
|
+
from ai_edge_torch.generative.test import utils as test_utils
|
23
|
+
import numpy as np
|
24
|
+
import torch
|
25
|
+
|
26
|
+
from absl.testing import absltest as googletest
|
27
|
+
from ai_edge_litert import interpreter
|
28
|
+
|
29
|
+
|
30
|
+
class TestModelConversion(googletest.TestCase):
|
31
|
+
"""Unit tests that check for model conversion and correctness."""
|
32
|
+
|
33
|
+
def setUp(self):
|
34
|
+
super().setUp()
|
35
|
+
# Builder function for an Interpreter that supports custom ops.
|
36
|
+
self._interpreter_builder = (
|
37
|
+
lambda tflite_model: lambda: interpreter.InterpreterWithCustomOps(
|
38
|
+
custom_op_registerers=["GenAIOpsRegisterer"],
|
39
|
+
model_content=tflite_model,
|
40
|
+
experimental_default_delegate_latest_features=True,
|
41
|
+
)
|
42
|
+
)
|
43
|
+
|
44
|
+
def _get_params(self, enable_hlfb: bool):
|
45
|
+
"""Returns a model, edge model and the kwargs to use for testing."""
|
46
|
+
config = toy_model_with_kv_cache.get_model_config()
|
47
|
+
config.enable_hlfb = enable_hlfb
|
48
|
+
pytorch_model = toy_model_with_kv_cache.ToyModelWithKVCache(config).eval()
|
49
|
+
tokens, input_pos = torch.tensor([[1]], dtype=torch.int), torch.tensor(
|
50
|
+
[10], dtype=torch.int
|
51
|
+
)
|
52
|
+
kv = kv_cache.KVCache.from_model_config(config)
|
53
|
+
kwargs = {
|
54
|
+
"tokens": tokens,
|
55
|
+
"input_pos": input_pos,
|
56
|
+
"kv_cache": kv,
|
57
|
+
}
|
58
|
+
|
59
|
+
edge_model = ai_edge_torch.convert(
|
60
|
+
pytorch_model,
|
61
|
+
sample_kwargs=kwargs,
|
62
|
+
)
|
63
|
+
edge_model.set_interpreter_builder(
|
64
|
+
self._interpreter_builder(edge_model.tflite_model())
|
65
|
+
)
|
66
|
+
return pytorch_model, edge_model, kwargs
|
67
|
+
|
68
|
+
def _test_model_with_kv_cache(self, enable_hlfb: bool):
|
69
|
+
pytorch_model, edge_model, kwargs = self._get_params(enable_hlfb)
|
70
|
+
|
71
|
+
self.assertTrue(
|
72
|
+
test_utils.compare_tflite_torch(
|
73
|
+
edge_model,
|
74
|
+
pytorch_model,
|
75
|
+
kwargs["tokens"],
|
76
|
+
kwargs["input_pos"],
|
77
|
+
kwargs["kv_cache"],
|
78
|
+
signature_name="serving_default",
|
79
|
+
atol=1e-5,
|
80
|
+
rtol=1e-5,
|
81
|
+
)
|
82
|
+
)
|
83
|
+
|
84
|
+
@googletest.skipIf(
|
85
|
+
ai_edge_torch.config.in_oss,
|
86
|
+
reason="tests with custom ops are not supported in oss",
|
87
|
+
)
|
88
|
+
def test_toy_model_with_kv_cache(self):
|
89
|
+
self._test_model_with_kv_cache(enable_hlfb=False)
|
90
|
+
|
91
|
+
@googletest.skipIf(
|
92
|
+
ai_edge_torch.config.in_oss,
|
93
|
+
reason="tests with custom ops are not supported in oss",
|
94
|
+
)
|
95
|
+
def test_toy_model_with_kv_cache_with_hlfb(self):
|
96
|
+
self._test_model_with_kv_cache(enable_hlfb=True)
|
97
|
+
|
98
|
+
@googletest.skipIf(
|
99
|
+
ai_edge_torch.config.in_oss,
|
100
|
+
reason="tests with custom ops are not supported in oss",
|
101
|
+
)
|
102
|
+
def test_toy_model_has_dus_op(self):
|
103
|
+
"""Tests that the model has the dynamic update slice op."""
|
104
|
+
_, edge_model, _ = self._get_params(enable_hlfb=True)
|
105
|
+
interpreter_ = interpreter.InterpreterWithCustomOps(
|
106
|
+
custom_op_registerers=["GenAIOpsRegisterer"],
|
107
|
+
model_content=edge_model.tflite_model(),
|
108
|
+
experimental_default_delegate_latest_features=True,
|
109
|
+
)
|
110
|
+
|
111
|
+
# pylint: disable=protected-access
|
112
|
+
op_names = [op["op_name"] for op in interpreter_._get_ops_details()]
|
113
|
+
self.assertIn("DYNAMIC_UPDATE_SLICE", op_names)
|
114
|
+
|
115
|
+
def _test_multisig_model(self, config, pytorch_model, atol, rtol):
|
116
|
+
# prefill
|
117
|
+
seq_len = 10
|
118
|
+
prefill_tokens = torch.zeros((1, seq_len), dtype=torch.int, device="cpu")
|
119
|
+
prompt_token = torch.from_numpy(np.array([1, 2, 3, 4]))
|
120
|
+
prefill_tokens[0, : len(prompt_token)] = prompt_token
|
121
|
+
prefill_input_pos = torch.arange(0, seq_len, dtype=torch.int)
|
122
|
+
|
123
|
+
# decode
|
124
|
+
decode_token = torch.tensor([[1]], dtype=torch.int)
|
125
|
+
decode_input_pos = torch.tensor([5], dtype=torch.int)
|
126
|
+
|
127
|
+
kv = kv_cache.KVCache.from_model_config(config)
|
128
|
+
|
129
|
+
edge_model = (
|
130
|
+
ai_edge_torch.signature(
|
131
|
+
"prefill",
|
132
|
+
pytorch_model,
|
133
|
+
sample_kwargs={
|
134
|
+
"tokens": prefill_tokens,
|
135
|
+
"input_pos": prefill_input_pos,
|
136
|
+
"kv_cache": kv,
|
137
|
+
},
|
138
|
+
)
|
139
|
+
.signature(
|
140
|
+
"decode",
|
141
|
+
pytorch_model,
|
142
|
+
sample_kwargs={
|
143
|
+
"tokens": decode_token,
|
144
|
+
"input_pos": decode_input_pos,
|
145
|
+
"kv_cache": kv,
|
146
|
+
},
|
147
|
+
)
|
148
|
+
.convert()
|
149
|
+
)
|
150
|
+
edge_model.set_interpreter_builder(
|
151
|
+
self._interpreter_builder(edge_model.tflite_model())
|
152
|
+
)
|
153
|
+
|
154
|
+
self.assertTrue(
|
155
|
+
test_utils.compare_tflite_torch(
|
156
|
+
edge_model,
|
157
|
+
pytorch_model,
|
158
|
+
prefill_tokens,
|
159
|
+
prefill_input_pos,
|
160
|
+
kv,
|
161
|
+
signature_name="prefill",
|
162
|
+
atol=atol,
|
163
|
+
rtol=atol,
|
164
|
+
)
|
165
|
+
)
|
166
|
+
|
167
|
+
self.assertTrue(
|
168
|
+
test_utils.compare_tflite_torch(
|
169
|
+
edge_model,
|
170
|
+
pytorch_model,
|
171
|
+
decode_token,
|
172
|
+
decode_input_pos,
|
173
|
+
kv,
|
174
|
+
signature_name="decode",
|
175
|
+
atol=atol,
|
176
|
+
rtol=atol,
|
177
|
+
)
|
178
|
+
)
|
179
|
+
|
180
|
+
@googletest.skipIf(
|
181
|
+
ai_edge_torch.config.in_oss,
|
182
|
+
reason="tests with custom ops are not supported in oss",
|
183
|
+
)
|
184
|
+
def test_tiny_llama_multisig(self):
|
185
|
+
config = tiny_llama.get_fake_model_config()
|
186
|
+
pytorch_model = tiny_llama.TinyLlama(config).eval()
|
187
|
+
self._test_multisig_model(config, pytorch_model, atol=1e-5, rtol=1e-5)
|
188
|
+
|
189
|
+
|
190
|
+
if __name__ == "__main__":
|
191
|
+
googletest.main()
|
@@ -0,0 +1,362 @@
|
|
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
|
+
"""Testing model conversion for a few gen-ai models."""
|
17
|
+
|
18
|
+
import ai_edge_torch
|
19
|
+
from ai_edge_torch.generative.examples.amd_llama_135m import amd_llama_135m
|
20
|
+
from ai_edge_torch.generative.examples.gemma import gemma1
|
21
|
+
from ai_edge_torch.generative.examples.gemma import gemma2
|
22
|
+
from ai_edge_torch.generative.examples.llama import llama
|
23
|
+
from ai_edge_torch.generative.examples.openelm import openelm
|
24
|
+
from ai_edge_torch.generative.examples.paligemma import decoder
|
25
|
+
from ai_edge_torch.generative.examples.paligemma import decoder2
|
26
|
+
from ai_edge_torch.generative.examples.paligemma import paligemma
|
27
|
+
from ai_edge_torch.generative.examples.phi import phi2
|
28
|
+
from ai_edge_torch.generative.examples.phi import phi3
|
29
|
+
from ai_edge_torch.generative.examples.qwen import qwen
|
30
|
+
from ai_edge_torch.generative.examples.smollm import smollm
|
31
|
+
from ai_edge_torch.generative.examples.stable_diffusion import clip as sd_clip
|
32
|
+
from ai_edge_torch.generative.examples.stable_diffusion import decoder as sd_decoder
|
33
|
+
from ai_edge_torch.generative.examples.stable_diffusion import diffusion as sd_diffusion
|
34
|
+
from ai_edge_torch.generative.layers import kv_cache
|
35
|
+
from ai_edge_torch.generative.test import utils as test_utils
|
36
|
+
import numpy as np
|
37
|
+
import torch
|
38
|
+
|
39
|
+
from absl.testing import absltest as googletest
|
40
|
+
from ai_edge_litert import interpreter
|
41
|
+
|
42
|
+
|
43
|
+
class TestModelConversion(googletest.TestCase):
|
44
|
+
"""Unit tests that check for model conversion and correctness."""
|
45
|
+
|
46
|
+
def setUp(self):
|
47
|
+
super().setUp()
|
48
|
+
# Builder function for an Interpreter that supports custom ops.
|
49
|
+
self._interpreter_builder = (
|
50
|
+
lambda tflite_model: lambda: interpreter.InterpreterWithCustomOps(
|
51
|
+
custom_op_registerers=["GenAIOpsRegisterer"],
|
52
|
+
model_content=tflite_model,
|
53
|
+
experimental_default_delegate_latest_features=True,
|
54
|
+
)
|
55
|
+
)
|
56
|
+
# Default cache_size_limit, 8 is hit and aborts often when the tests are
|
57
|
+
# running all together. Doubles it to avoid abortion.
|
58
|
+
torch._dynamo.config.cache_size_limit = 16
|
59
|
+
np.random.seed(1234) # Make np.random deterministic.
|
60
|
+
|
61
|
+
def _test_model(self, config, model, signature_name, atol, rtol):
|
62
|
+
seq_len = 10
|
63
|
+
tokens = torch.zeros((1, seq_len), dtype=torch.int, device="cpu")
|
64
|
+
input_pos = torch.arange(0, seq_len, dtype=torch.int)
|
65
|
+
kv = kv_cache.KVCache.from_model_config(config)
|
66
|
+
|
67
|
+
edge_model = ai_edge_torch.signature(
|
68
|
+
signature_name,
|
69
|
+
model,
|
70
|
+
sample_kwargs={
|
71
|
+
"tokens": tokens,
|
72
|
+
"input_pos": input_pos,
|
73
|
+
"kv_cache": kv,
|
74
|
+
},
|
75
|
+
).convert()
|
76
|
+
edge_model.set_interpreter_builder(
|
77
|
+
self._interpreter_builder(edge_model.tflite_model())
|
78
|
+
)
|
79
|
+
|
80
|
+
tokens = torch.arange(1, seq_len + 1, dtype=torch.int).unsqueeze(0)
|
81
|
+
self.assertTrue(
|
82
|
+
test_utils.compare_tflite_torch(
|
83
|
+
edge_model,
|
84
|
+
model,
|
85
|
+
tokens,
|
86
|
+
input_pos,
|
87
|
+
kv,
|
88
|
+
signature_name=signature_name,
|
89
|
+
atol=atol,
|
90
|
+
rtol=rtol,
|
91
|
+
)
|
92
|
+
)
|
93
|
+
|
94
|
+
@googletest.skipIf(
|
95
|
+
ai_edge_torch.config.in_oss,
|
96
|
+
reason="tests with custom ops are not supported in oss",
|
97
|
+
)
|
98
|
+
def test_gemma1(self):
|
99
|
+
config = gemma1.get_fake_model_config()
|
100
|
+
pytorch_model = gemma1.Gemma1(config).eval()
|
101
|
+
self._test_model(config, pytorch_model, "prefill", atol=1e-3, rtol=1e-5)
|
102
|
+
|
103
|
+
@googletest.skipIf(
|
104
|
+
ai_edge_torch.config.in_oss,
|
105
|
+
reason="tests with custom ops are not supported in oss",
|
106
|
+
)
|
107
|
+
def test_gemma2(self):
|
108
|
+
config = gemma2.get_fake_model_config()
|
109
|
+
pytorch_model = gemma2.Gemma2(config).eval()
|
110
|
+
self._test_model(config, pytorch_model, "prefill", atol=1e-4, rtol=1e-5)
|
111
|
+
|
112
|
+
@googletest.skipIf(
|
113
|
+
ai_edge_torch.config.in_oss,
|
114
|
+
reason="tests with custom ops are not supported in oss",
|
115
|
+
)
|
116
|
+
def test_llama(self):
|
117
|
+
config = llama.get_fake_model_config()
|
118
|
+
pytorch_model = llama.Llama(config).eval()
|
119
|
+
self._test_model(config, pytorch_model, "prefill", atol=1e-3, rtol=1e-5)
|
120
|
+
|
121
|
+
@googletest.skipIf(
|
122
|
+
ai_edge_torch.config.in_oss,
|
123
|
+
reason="tests with custom ops are not supported in oss",
|
124
|
+
)
|
125
|
+
def test_phi2(self):
|
126
|
+
config = phi2.get_fake_model_config()
|
127
|
+
pytorch_model = phi2.Phi2(config).eval()
|
128
|
+
# Phi-2 logits are very big, so we need a larger absolute tolerance.
|
129
|
+
self._test_model(config, pytorch_model, "prefill", atol=1e-3, rtol=1e-5)
|
130
|
+
|
131
|
+
@googletest.skipIf(
|
132
|
+
ai_edge_torch.config.in_oss,
|
133
|
+
reason="tests with custom ops are not supported in oss",
|
134
|
+
)
|
135
|
+
def test_phi3(self):
|
136
|
+
config = phi3.get_fake_model_config()
|
137
|
+
pytorch_model = phi3.Phi3_5Mini(config).eval()
|
138
|
+
self._test_model(config, pytorch_model, "prefill", atol=1e-5, rtol=1e-5)
|
139
|
+
|
140
|
+
@googletest.skipIf(
|
141
|
+
ai_edge_torch.config.in_oss,
|
142
|
+
reason="tests with custom ops are not supported in oss",
|
143
|
+
)
|
144
|
+
def test_smollm(self):
|
145
|
+
config = smollm.get_fake_model_config()
|
146
|
+
pytorch_model = smollm.SmolLM(config).eval()
|
147
|
+
self._test_model(config, pytorch_model, "prefill", atol=1e-4, rtol=1e-5)
|
148
|
+
|
149
|
+
@googletest.skipIf(
|
150
|
+
ai_edge_torch.config.in_oss,
|
151
|
+
reason="tests with custom ops are not supported in oss",
|
152
|
+
)
|
153
|
+
|
154
|
+
def test_smollm2(self):
|
155
|
+
config = smollm.get_fake_model_config_v2()
|
156
|
+
pytorch_model = smollm.SmolLM2(config).eval()
|
157
|
+
self._test_model(config, pytorch_model, "prefill", atol=1e-4, rtol=1e-5)
|
158
|
+
@googletest.skipIf(
|
159
|
+
ai_edge_torch.config.in_oss,
|
160
|
+
reason="tests with custom ops are not supported in oss",
|
161
|
+
)
|
162
|
+
|
163
|
+
def test_openelm(self):
|
164
|
+
config = openelm.get_fake_model_config()
|
165
|
+
pytorch_model = openelm.OpenELM(config).eval()
|
166
|
+
self._test_model(config, pytorch_model, "prefill", atol=1e-4, rtol=1e-5)
|
167
|
+
|
168
|
+
@googletest.skipIf(
|
169
|
+
ai_edge_torch.config.in_oss,
|
170
|
+
reason="tests with custom ops are not supported in oss",
|
171
|
+
)
|
172
|
+
def test_qwen(self):
|
173
|
+
config = qwen.get_fake_model_config()
|
174
|
+
pytorch_model = qwen.Qwen(config).eval()
|
175
|
+
self._test_model(config, pytorch_model, "prefill", atol=1e-3, rtol=1e-5)
|
176
|
+
|
177
|
+
@googletest.skipIf(
|
178
|
+
ai_edge_torch.config.in_oss,
|
179
|
+
reason="tests with custom ops are not supported in oss",
|
180
|
+
)
|
181
|
+
def test_amd_llama_135m(self):
|
182
|
+
config = amd_llama_135m.get_fake_model_config()
|
183
|
+
pytorch_model = amd_llama_135m.AmdLlama(config).eval()
|
184
|
+
self._test_model(config, pytorch_model, "prefill", atol=1e-5, rtol=1e-5)
|
185
|
+
|
186
|
+
def _test_paligemma_model(self, decoder_class, decoder_config, atol, rtol):
|
187
|
+
config = paligemma.get_fake_model_config(decoder_config)
|
188
|
+
pytorch_model = paligemma.PaliGemma(config, decoder_class).eval()
|
189
|
+
|
190
|
+
image_embedding_config = config.image_encoder_config.image_embedding
|
191
|
+
num_patches = (
|
192
|
+
image_embedding_config.image_size // image_embedding_config.patch_size
|
193
|
+
) ** 2
|
194
|
+
|
195
|
+
# Make sure the token size is longer than the number of image patches.
|
196
|
+
seq_len = num_patches + 10
|
197
|
+
tokens = torch.zeros((1, seq_len), dtype=torch.int, device="cpu")
|
198
|
+
input_pos = torch.arange(0, seq_len, dtype=torch.int)
|
199
|
+
kv = kv_cache.KVCache.from_model_config(config.decoder_config)
|
200
|
+
pixel_values = torch.zeros((1, 3, 8, 8), dtype=torch.float32, device="cpu")
|
201
|
+
|
202
|
+
edge_model = ai_edge_torch.signature(
|
203
|
+
"prefill_pixel",
|
204
|
+
pytorch_model,
|
205
|
+
sample_kwargs={
|
206
|
+
"tokens": tokens,
|
207
|
+
"input_pos": input_pos,
|
208
|
+
"kv_cache": kv,
|
209
|
+
"pixel_values": pixel_values,
|
210
|
+
},
|
211
|
+
).convert()
|
212
|
+
edge_model.set_interpreter_builder(
|
213
|
+
self._interpreter_builder(edge_model.tflite_model())
|
214
|
+
)
|
215
|
+
|
216
|
+
tokens = torch.arange(1, seq_len + 1, dtype=torch.int).unsqueeze(0)
|
217
|
+
self.assertTrue(
|
218
|
+
test_utils.compare_tflite_torch(
|
219
|
+
edge_model,
|
220
|
+
pytorch_model,
|
221
|
+
tokens,
|
222
|
+
input_pos,
|
223
|
+
kv,
|
224
|
+
pixel_values=pixel_values,
|
225
|
+
signature_name="prefill_pixel",
|
226
|
+
atol=atol,
|
227
|
+
rtol=rtol,
|
228
|
+
)
|
229
|
+
)
|
230
|
+
|
231
|
+
@googletest.skipIf(
|
232
|
+
ai_edge_torch.config.in_oss,
|
233
|
+
reason="tests with custom ops are not supported in oss",
|
234
|
+
)
|
235
|
+
def disabled_test_paligemma1(self):
|
236
|
+
self._test_paligemma_model(
|
237
|
+
decoder.Decoder, decoder.get_fake_decoder_config, atol=1e-3, rtol=1e-5
|
238
|
+
)
|
239
|
+
|
240
|
+
@googletest.skipIf(
|
241
|
+
ai_edge_torch.config.in_oss,
|
242
|
+
reason="tests with custom ops are not supported in oss",
|
243
|
+
)
|
244
|
+
def disabled_test_paligemma2(self):
|
245
|
+
self._test_paligemma_model(
|
246
|
+
decoder2.Decoder2,
|
247
|
+
decoder2.get_fake_decoder2_config,
|
248
|
+
atol=1e-3,
|
249
|
+
rtol=1e-5,
|
250
|
+
)
|
251
|
+
|
252
|
+
@googletest.skipIf(
|
253
|
+
ai_edge_torch.config.in_oss,
|
254
|
+
reason="tests with custom ops are not supported in oss",
|
255
|
+
)
|
256
|
+
def test_stable_diffusion_clip(self):
|
257
|
+
config = sd_clip.get_fake_model_config()
|
258
|
+
prompt_tokens = torch.from_numpy(
|
259
|
+
np.array([[1, 2, 3, 4, 5, 6]], dtype=np.int32)
|
260
|
+
)
|
261
|
+
|
262
|
+
pytorch_model = sd_clip.CLIP(config).eval()
|
263
|
+
torch_output = pytorch_model(prompt_tokens)
|
264
|
+
|
265
|
+
edge_model = ai_edge_torch.signature(
|
266
|
+
"encode", pytorch_model, (prompt_tokens,)
|
267
|
+
).convert()
|
268
|
+
edge_model.set_interpreter_builder(
|
269
|
+
self._interpreter_builder(edge_model.tflite_model())
|
270
|
+
)
|
271
|
+
edge_output = edge_model(
|
272
|
+
prompt_tokens.numpy(),
|
273
|
+
signature_name="encode",
|
274
|
+
)
|
275
|
+
self.assertTrue(
|
276
|
+
test_utils.compare_logits(
|
277
|
+
edge_output,
|
278
|
+
torch_output.detach().numpy(),
|
279
|
+
atol=1e-4,
|
280
|
+
rtol=1e-5,
|
281
|
+
)
|
282
|
+
)
|
283
|
+
|
284
|
+
@googletest.skipIf(
|
285
|
+
ai_edge_torch.config.in_oss,
|
286
|
+
reason="tests with custom ops are not supported in oss",
|
287
|
+
)
|
288
|
+
def test_stable_diffusion_diffusion(self):
|
289
|
+
config = sd_diffusion.get_fake_model_config(2)
|
290
|
+
# Reduce stddev(scale) of input values to avoid too big output logits which
|
291
|
+
# fails comparisons with reasonable tolerances.
|
292
|
+
latents = torch.from_numpy(
|
293
|
+
np.random.normal(size=(2, 4, 8, 8), scale=0.1).astype(np.float32)
|
294
|
+
)
|
295
|
+
context = torch.from_numpy(
|
296
|
+
np.random.normal(size=(2, 4, 4), scale=0.1).astype(np.float32)
|
297
|
+
)
|
298
|
+
time_embedding = torch.from_numpy(
|
299
|
+
np.random.normal(size=(2, 2), scale=0.1).astype(np.float32)
|
300
|
+
)
|
301
|
+
|
302
|
+
pytorch_model = sd_diffusion.Diffusion(config).eval()
|
303
|
+
torch_output = pytorch_model(latents, context, time_embedding)
|
304
|
+
|
305
|
+
edge_model = ai_edge_torch.signature(
|
306
|
+
"diffusion", pytorch_model, (latents, context, time_embedding)
|
307
|
+
).convert()
|
308
|
+
edge_model.set_interpreter_builder(
|
309
|
+
self._interpreter_builder(edge_model.tflite_model())
|
310
|
+
)
|
311
|
+
edge_output = edge_model(
|
312
|
+
latents.numpy(),
|
313
|
+
context.numpy(),
|
314
|
+
time_embedding.numpy(),
|
315
|
+
signature_name="diffusion",
|
316
|
+
)
|
317
|
+
self.assertTrue(
|
318
|
+
test_utils.compare_logits(
|
319
|
+
edge_output,
|
320
|
+
torch_output.detach().numpy(),
|
321
|
+
atol=1e-4,
|
322
|
+
rtol=1e-5,
|
323
|
+
)
|
324
|
+
)
|
325
|
+
|
326
|
+
@googletest.skipIf(
|
327
|
+
ai_edge_torch.config.in_oss,
|
328
|
+
reason="tests with custom ops are not supported in oss",
|
329
|
+
)
|
330
|
+
def test_stable_diffusion_decoder(self):
|
331
|
+
config = sd_decoder.get_fake_model_config()
|
332
|
+
# Reduce stddev(scale) of input values to avoid too big output logits which
|
333
|
+
# fails comparisons with reasonable tolerances.
|
334
|
+
latents = torch.from_numpy(
|
335
|
+
np.random.normal(size=(1, 4, 64, 64), scale=0.1).astype(np.float32)
|
336
|
+
)
|
337
|
+
|
338
|
+
pytorch_model = sd_decoder.Decoder(config).eval()
|
339
|
+
torch_output = pytorch_model(latents)
|
340
|
+
|
341
|
+
edge_model = ai_edge_torch.signature(
|
342
|
+
"decode", pytorch_model, (latents,)
|
343
|
+
).convert()
|
344
|
+
edge_model.set_interpreter_builder(
|
345
|
+
self._interpreter_builder(edge_model.tflite_model())
|
346
|
+
)
|
347
|
+
edge_output = edge_model(
|
348
|
+
latents.numpy(),
|
349
|
+
signature_name="decode",
|
350
|
+
)
|
351
|
+
self.assertTrue(
|
352
|
+
test_utils.compare_logits(
|
353
|
+
edge_output,
|
354
|
+
torch_output.detach().numpy(),
|
355
|
+
atol=1e-3,
|
356
|
+
rtol=1e-5,
|
357
|
+
)
|
358
|
+
)
|
359
|
+
|
360
|
+
|
361
|
+
if __name__ == "__main__":
|
362
|
+
googletest.main()
|