ai-edge-torch-nightly 0.2.0.dev20240714__py3-none-any.whl → 0.3.0.dev20240926__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 +5 -4
- ai_edge_torch/_convert/conversion.py +112 -0
- ai_edge_torch/_convert/conversion_utils.py +64 -0
- ai_edge_torch/{convert → _convert}/converter.py +94 -48
- ai_edge_torch/_convert/fx_passes/__init__.py +22 -0
- ai_edge_torch/{convert → _convert}/fx_passes/build_aten_composite_pass.py +107 -44
- ai_edge_torch/{convert → _convert}/fx_passes/build_interpolate_composite_pass.py +23 -20
- ai_edge_torch/{convert → _convert}/fx_passes/inject_mlir_debuginfo_pass.py +5 -6
- ai_edge_torch/{convert → _convert}/fx_passes/optimize_layout_transposes_pass/__init__.py +1 -1
- ai_edge_torch/{convert → _convert}/fx_passes/optimize_layout_transposes_pass/layout_check.py +39 -9
- ai_edge_torch/{convert → _convert}/fx_passes/optimize_layout_transposes_pass/layout_mark.py +2 -0
- ai_edge_torch/{convert → _convert}/fx_passes/optimize_layout_transposes_pass/layout_partitioners/__init__.py +1 -0
- ai_edge_torch/{convert → _convert}/fx_passes/optimize_layout_transposes_pass/layout_partitioners/greedy.py +17 -8
- ai_edge_torch/{convert → _convert}/fx_passes/optimize_layout_transposes_pass/layout_partitioners/min_cut.py +9 -8
- ai_edge_torch/{convert → _convert}/fx_passes/optimize_layout_transposes_pass/layout_rewrite.py +31 -18
- ai_edge_torch/{convert → _convert}/fx_passes/optimize_layout_transposes_pass/op_func_registry.py +2 -2
- ai_edge_torch/{convert → _convert}/fx_passes/optimize_layout_transposes_pass/pass_body.py +34 -24
- ai_edge_torch/{convert → _convert}/fx_passes/optimize_layout_transposes_pass/utils.py +2 -0
- ai_edge_torch/_convert/signature.py +66 -0
- ai_edge_torch/_convert/test/test_convert.py +495 -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 → _convert}/test/test_to_channel_last_io.py +5 -5
- ai_edge_torch/{convert → _convert}/to_channel_last_io.py +10 -3
- ai_edge_torch/config.py +27 -0
- ai_edge_torch/conftest.py +20 -0
- ai_edge_torch/debug/culprit.py +72 -40
- ai_edge_torch/debug/test/test_culprit.py +7 -5
- ai_edge_torch/debug/test/test_search_model.py +8 -7
- ai_edge_torch/debug/utils.py +14 -3
- ai_edge_torch/fx_pass_base.py +101 -0
- ai_edge_torch/generative/examples/gemma/convert_gemma1_to_tflite.py +68 -0
- ai_edge_torch/generative/examples/gemma/convert_gemma2_to_tflite.py +68 -0
- ai_edge_torch/generative/examples/gemma/{gemma.py → gemma1.py} +69 -55
- ai_edge_torch/generative/examples/gemma/gemma2.py +267 -0
- ai_edge_torch/generative/examples/gemma/verify_gemma1.py +56 -0
- ai_edge_torch/generative/examples/gemma/verify_gemma2.py +57 -0
- ai_edge_torch/generative/examples/gemma/verify_util.py +143 -0
- ai_edge_torch/generative/examples/openelm/convert_to_tflite.py +68 -0
- ai_edge_torch/generative/examples/openelm/openelm.py +206 -0
- ai_edge_torch/generative/examples/openelm/verify.py +64 -0
- ai_edge_torch/generative/examples/phi/__init__.py +14 -0
- ai_edge_torch/generative/examples/phi/convert_phi3_to_tflite.py +68 -0
- ai_edge_torch/generative/examples/phi/convert_to_tflite.py +68 -0
- ai_edge_torch/generative/examples/{phi2 → phi}/phi2.py +70 -51
- ai_edge_torch/generative/examples/phi/phi3.py +286 -0
- ai_edge_torch/generative/examples/phi/verify.py +65 -0
- ai_edge_torch/generative/examples/phi/verify_phi3.py +70 -0
- ai_edge_torch/generative/examples/smollm/__init__.py +14 -0
- ai_edge_torch/generative/examples/smollm/convert_to_tflite.py +68 -0
- ai_edge_torch/generative/examples/smollm/smollm.py +101 -0
- ai_edge_torch/generative/examples/smollm/verify.py +62 -0
- ai_edge_torch/generative/examples/stable_diffusion/attention.py +3 -1
- ai_edge_torch/generative/examples/stable_diffusion/clip.py +83 -13
- ai_edge_torch/generative/examples/stable_diffusion/convert_to_tflite.py +27 -14
- ai_edge_torch/generative/examples/stable_diffusion/decoder.py +74 -9
- ai_edge_torch/generative/examples/stable_diffusion/diffusion.py +179 -37
- ai_edge_torch/generative/examples/stable_diffusion/encoder.py +4 -3
- ai_edge_torch/generative/examples/stable_diffusion/pipeline.py +83 -58
- ai_edge_torch/generative/examples/stable_diffusion/samplers/k_euler.py +4 -3
- ai_edge_torch/generative/examples/stable_diffusion/samplers/k_euler_ancestral.py +4 -3
- ai_edge_torch/generative/examples/stable_diffusion/samplers/k_lms.py +4 -3
- ai_edge_torch/generative/examples/stable_diffusion/samplers/sampler.py +1 -0
- ai_edge_torch/generative/examples/stable_diffusion/tokenizer.py +4 -1
- ai_edge_torch/generative/examples/stable_diffusion/util.py +9 -3
- ai_edge_torch/generative/examples/t5/convert_to_tflite.py +28 -25
- ai_edge_torch/generative/examples/t5/t5.py +208 -159
- ai_edge_torch/generative/examples/t5/t5_attention.py +45 -30
- ai_edge_torch/generative/examples/test_models/convert_toy_model.py +105 -0
- ai_edge_torch/generative/examples/test_models/toy_model.py +69 -41
- ai_edge_torch/generative/examples/test_models/toy_model_with_kv_cache.py +50 -64
- ai_edge_torch/generative/examples/tiny_llama/__init__.py +14 -0
- ai_edge_torch/generative/examples/tiny_llama/convert_to_tflite.py +41 -39
- ai_edge_torch/generative/examples/tiny_llama/tiny_llama.py +67 -54
- ai_edge_torch/generative/examples/tiny_llama/verify.py +64 -0
- ai_edge_torch/generative/fx_passes/__init__.py +4 -5
- ai_edge_torch/generative/fx_passes/remove_sdpa_zero_mask_pass.py +10 -7
- ai_edge_torch/generative/layers/attention.py +141 -102
- ai_edge_torch/generative/layers/attention_utils.py +53 -12
- ai_edge_torch/generative/layers/builder.py +37 -7
- ai_edge_torch/generative/layers/feed_forward.py +39 -14
- ai_edge_torch/generative/layers/kv_cache.py +162 -50
- ai_edge_torch/generative/layers/model_config.py +84 -30
- ai_edge_torch/generative/layers/normalization.py +185 -7
- ai_edge_torch/generative/layers/rotary_position_embedding.py +6 -4
- ai_edge_torch/generative/layers/scaled_dot_product_attention.py +48 -21
- ai_edge_torch/generative/layers/unet/blocks_2d.py +136 -77
- ai_edge_torch/generative/layers/unet/builder.py +7 -4
- ai_edge_torch/generative/layers/unet/model_config.py +17 -15
- ai_edge_torch/generative/quantize/example.py +7 -8
- ai_edge_torch/generative/quantize/quant_recipe.py +10 -7
- ai_edge_torch/generative/quantize/quant_recipe_utils.py +12 -1
- ai_edge_torch/generative/quantize/quant_recipes.py +8 -0
- ai_edge_torch/generative/test/test_kv_cache.py +120 -0
- ai_edge_torch/generative/test/{loader_test.py → test_loader.py} +9 -7
- ai_edge_torch/generative/test/test_model_conversion.py +124 -188
- ai_edge_torch/generative/test/test_model_conversion_large.py +251 -0
- ai_edge_torch/generative/test/test_quantize.py +76 -60
- ai_edge_torch/generative/test/utils.py +54 -0
- ai_edge_torch/generative/utilities/converter.py +82 -0
- ai_edge_torch/generative/utilities/loader.py +120 -57
- ai_edge_torch/generative/utilities/stable_diffusion_loader.py +165 -57
- ai_edge_torch/generative/utilities/t5_loader.py +110 -81
- ai_edge_torch/generative/utilities/verifier.py +247 -0
- ai_edge_torch/hlfb/__init__.py +1 -1
- ai_edge_torch/hlfb/mark_pattern/__init__.py +9 -7
- ai_edge_torch/hlfb/mark_pattern/passes.py +23 -3
- ai_edge_torch/hlfb/mark_pattern/pattern.py +39 -30
- ai_edge_torch/hlfb/test/test_mark_pattern.py +46 -20
- ai_edge_torch/hlfb/test/test_stablehlo_composite_builder.py +24 -11
- ai_edge_torch/lowertools/__init__.py +18 -0
- ai_edge_torch/lowertools/_shim.py +80 -0
- ai_edge_torch/lowertools/common_utils.py +142 -0
- ai_edge_torch/lowertools/odml_torch_utils.py +255 -0
- ai_edge_torch/lowertools/test_utils.py +60 -0
- ai_edge_torch/lowertools/torch_xla_utils.py +284 -0
- ai_edge_torch/{generative/quantize/ai_edge_quantizer_glue → lowertools}/translate_recipe.py +29 -14
- ai_edge_torch/model.py +53 -18
- ai_edge_torch/odml_torch/__init__.py +20 -0
- ai_edge_torch/odml_torch/_torch_future.py +61 -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 +357 -0
- ai_edge_torch/odml_torch/export_utils.py +168 -0
- ai_edge_torch/odml_torch/jax_bridge/__init__.py +15 -0
- ai_edge_torch/odml_torch/jax_bridge/_wrap.py +150 -0
- ai_edge_torch/odml_torch/jax_bridge/utils.py +75 -0
- ai_edge_torch/odml_torch/lowerings/__init__.py +25 -0
- ai_edge_torch/odml_torch/lowerings/_basic.py +258 -0
- ai_edge_torch/odml_torch/lowerings/_batch_norm.py +65 -0
- ai_edge_torch/odml_torch/lowerings/_convolution.py +241 -0
- ai_edge_torch/odml_torch/lowerings/_jax_lowerings.py +252 -0
- ai_edge_torch/odml_torch/lowerings/_layer_norm.py +78 -0
- ai_edge_torch/odml_torch/lowerings/context.py +42 -0
- ai_edge_torch/odml_torch/lowerings/registry.py +96 -0
- ai_edge_torch/odml_torch/lowerings/utils.py +185 -0
- ai_edge_torch/odml_torch/passes/__init__.py +38 -0
- ai_edge_torch/odml_torch/tf_integration.py +194 -0
- ai_edge_torch/quantize/pt2e_quantizer.py +52 -24
- ai_edge_torch/quantize/pt2e_quantizer_utils.py +43 -23
- ai_edge_torch/quantize/quant_config.py +13 -9
- ai_edge_torch/testing/model_coverage/model_coverage.py +29 -16
- ai_edge_torch/version.py +16 -0
- {ai_edge_torch_nightly-0.2.0.dev20240714.dist-info → ai_edge_torch_nightly-0.3.0.dev20240926.dist-info}/METADATA +7 -3
- ai_edge_torch_nightly-0.3.0.dev20240926.dist-info/RECORD +177 -0
- {ai_edge_torch_nightly-0.2.0.dev20240714.dist-info → ai_edge_torch_nightly-0.3.0.dev20240926.dist-info}/WHEEL +1 -1
- ai_edge_torch/convert/conversion.py +0 -117
- ai_edge_torch/convert/conversion_utils.py +0 -400
- ai_edge_torch/convert/fx_passes/__init__.py +0 -59
- ai_edge_torch/convert/fx_passes/_pass_base.py +0 -49
- ai_edge_torch/convert/fx_passes/canonicalize_pass.py +0 -37
- ai_edge_torch/convert/test/test_convert.py +0 -311
- ai_edge_torch/convert/test/test_convert_composites.py +0 -192
- ai_edge_torch/convert/test/test_convert_multisig.py +0 -139
- ai_edge_torch/generative/examples/gemma/convert_to_tflite.py +0 -66
- ai_edge_torch/generative/examples/phi2/convert_to_tflite.py +0 -64
- ai_edge_torch/generative/examples/test_models/toy_model_with_external_kv_cache.py +0 -161
- ai_edge_torch/generative/quantize/ai_edge_quantizer_glue/__init__.py +0 -0
- ai_edge_torch_nightly-0.2.0.dev20240714.dist-info/RECORD +0 -121
- /ai_edge_torch/{convert → _convert}/__init__.py +0 -0
- /ai_edge_torch/{convert → _convert}/test/__init__.py +0 -0
- /ai_edge_torch/generative/examples/{phi2 → openelm}/__init__.py +0 -0
- {ai_edge_torch_nightly-0.2.0.dev20240714.dist-info → ai_edge_torch_nightly-0.3.0.dev20240926.dist-info}/LICENSE +0 -0
- {ai_edge_torch_nightly-0.2.0.dev20240714.dist-info → ai_edge_torch_nightly-0.3.0.dev20240926.dist-info}/top_level.txt +0 -0
@@ -28,6 +28,7 @@ class SamplerInterface(abc.ABC):
|
|
28
28
|
@abc.abstractmethod
|
29
29
|
def set_strength(self, strength: float = 1) -> None:
|
30
30
|
"""Set the strength of initial step.
|
31
|
+
|
31
32
|
Conceptually, indicates how much to transform the reference `input_images`.
|
32
33
|
"""
|
33
34
|
return NotImplemented
|
@@ -27,7 +27,10 @@ def create_bytes_table() -> dict:
|
|
27
27
|
special_count = 0
|
28
28
|
for byte in range(256):
|
29
29
|
category = unicodedata.category(chr(byte))
|
30
|
-
if category[0] not in [
|
30
|
+
if category[0] not in [
|
31
|
+
'C',
|
32
|
+
'Z',
|
33
|
+
]: # ith character is NOT control char or space
|
31
34
|
table[byte] = chr(byte)
|
32
35
|
else: # ith character IS control char or space
|
33
36
|
table[byte] = chr(special_count + 256)
|
@@ -20,14 +20,20 @@ import torch
|
|
20
20
|
|
21
21
|
|
22
22
|
def get_time_embedding(timestep):
|
23
|
-
freqs = torch.pow(
|
23
|
+
freqs = torch.pow(
|
24
|
+
10000, -torch.arange(start=0, end=160, dtype=torch.float32) / 160
|
25
|
+
)
|
24
26
|
x = torch.tensor([timestep], dtype=torch.float32)[:, None] * freqs[None]
|
25
27
|
return torch.cat([torch.cos(x), torch.sin(x)], dim=-1)
|
26
28
|
|
27
29
|
|
28
|
-
def get_alphas_cumprod(
|
30
|
+
def get_alphas_cumprod(
|
31
|
+
beta_start=0.00085, beta_end=0.0120, n_training_steps=1000
|
32
|
+
):
|
29
33
|
betas = (
|
30
|
-
np.linspace(
|
34
|
+
np.linspace(
|
35
|
+
beta_start**0.5, beta_end**0.5, n_training_steps, dtype=np.float32
|
36
|
+
)
|
31
37
|
** 2
|
32
38
|
)
|
33
39
|
alphas = 1.0 - betas
|
@@ -16,12 +16,11 @@
|
|
16
16
|
import os
|
17
17
|
from pathlib import Path
|
18
18
|
|
19
|
-
import numpy as np
|
20
|
-
import torch
|
21
|
-
|
22
19
|
import ai_edge_torch
|
23
20
|
from ai_edge_torch.generative.examples.t5 import t5
|
24
21
|
from ai_edge_torch.generative.quantize import quant_recipes
|
22
|
+
import numpy as np
|
23
|
+
import torch
|
25
24
|
|
26
25
|
|
27
26
|
# TODO(haoliang): clean this up untile 2-sig model is validated e2e.
|
@@ -30,24 +29,24 @@ def convert_t5_to_tflite_singlesig(checkpoint_path: str):
|
|
30
29
|
|
31
30
|
# encoder
|
32
31
|
seq_len = 512
|
33
|
-
prefill_e_tokens = torch.full((1, seq_len), 0, dtype=torch.
|
32
|
+
prefill_e_tokens = torch.full((1, seq_len), 0, dtype=torch.int)
|
34
33
|
prompt_e_token = [1, 2, 3, 4, 5, 6]
|
35
34
|
prefill_e_tokens[0, : len(prompt_e_token)] = torch.tensor(
|
36
|
-
prompt_e_token, dtype=torch.
|
35
|
+
prompt_e_token, dtype=torch.int
|
37
36
|
)
|
38
|
-
prefill_e_input_pos = torch.arange(0, seq_len)
|
39
|
-
prefill_d_tokens = torch.full((1, seq_len), 0, dtype=torch.
|
37
|
+
prefill_e_input_pos = torch.arange(0, seq_len, dtype=torch.int)
|
38
|
+
prefill_d_tokens = torch.full((1, seq_len), 0, dtype=torch.int)
|
40
39
|
prompt_d_token = [1, 2, 3, 4, 5, 6]
|
41
40
|
prefill_d_tokens[0, : len(prompt_d_token)] = torch.tensor(
|
42
|
-
prompt_d_token, dtype=torch.
|
41
|
+
prompt_d_token, dtype=torch.int
|
43
42
|
)
|
44
|
-
prefill_d_input_pos = torch.arange(0, seq_len)
|
43
|
+
prefill_d_input_pos = torch.arange(0, seq_len, dtype=torch.int)
|
45
44
|
|
46
45
|
# decoder
|
47
|
-
decode_token = torch.tensor([[1]], dtype=torch.
|
48
|
-
decode_input_pos = torch.tensor([0], dtype=torch.
|
49
|
-
decode_d_token = torch.tensor([[1]], dtype=torch.
|
50
|
-
decode_d_input_pos = torch.tensor([0], dtype=torch.
|
46
|
+
decode_token = torch.tensor([[1]], dtype=torch.int)
|
47
|
+
decode_input_pos = torch.tensor([0], dtype=torch.int)
|
48
|
+
decode_d_token = torch.tensor([[1]], dtype=torch.int)
|
49
|
+
decode_d_input_pos = torch.tensor([0], dtype=torch.int)
|
51
50
|
|
52
51
|
# Pad mask for self attention only on "real" tokens.
|
53
52
|
# Pad with `-inf` for any tokens indices that aren't desired.
|
@@ -73,29 +72,33 @@ def convert_t5_to_tflite_multisig(checkpoint_path: str):
|
|
73
72
|
embedding_layer = torch.nn.Embedding(
|
74
73
|
config.vocab_size, config.embedding_dim, padding_idx=0
|
75
74
|
)
|
76
|
-
t5_encoder_model = t5.build_t5_encoder_model(
|
77
|
-
|
75
|
+
t5_encoder_model = t5.build_t5_encoder_model(
|
76
|
+
config, embedding_layer, checkpoint_path
|
77
|
+
)
|
78
|
+
t5_decoder_model = t5.build_t5_decoder_model(
|
79
|
+
config, embedding_layer, checkpoint_path
|
80
|
+
)
|
78
81
|
|
79
82
|
# encoder
|
80
83
|
seq_len = 512
|
81
|
-
prefill_e_tokens = torch.full((1, seq_len), 0, dtype=torch.
|
84
|
+
prefill_e_tokens = torch.full((1, seq_len), 0, dtype=torch.int)
|
82
85
|
prompt_e_token = [1, 2, 3, 4, 5, 6]
|
83
86
|
prefill_e_tokens[0, : len(prompt_e_token)] = torch.tensor(
|
84
|
-
prompt_e_token, dtype=torch.
|
87
|
+
prompt_e_token, dtype=torch.int
|
85
88
|
)
|
86
|
-
prefill_e_input_pos = torch.arange(0, seq_len)
|
87
|
-
prefill_d_tokens = torch.full((1, seq_len), 0, dtype=torch.
|
89
|
+
prefill_e_input_pos = torch.arange(0, seq_len, dtype=torch.int)
|
90
|
+
prefill_d_tokens = torch.full((1, seq_len), 0, dtype=torch.int)
|
88
91
|
prompt_d_token = [1, 2, 3, 4, 5, 6]
|
89
92
|
prefill_d_tokens[0, : len(prompt_d_token)] = torch.tensor(
|
90
|
-
prompt_d_token, dtype=torch.
|
93
|
+
prompt_d_token, dtype=torch.int
|
91
94
|
)
|
92
|
-
prefill_d_input_pos = torch.arange(0, seq_len)
|
95
|
+
prefill_d_input_pos = torch.arange(0, seq_len, dtype=torch.int)
|
93
96
|
|
94
97
|
# decoder
|
95
|
-
decode_token = torch.tensor([[1]], dtype=torch.
|
96
|
-
decode_input_pos = torch.tensor([0], dtype=torch.
|
97
|
-
decode_d_token = torch.tensor([[1]], dtype=torch.
|
98
|
-
decode_d_input_pos = torch.tensor([0], dtype=torch.
|
98
|
+
decode_token = torch.tensor([[1]], dtype=torch.int)
|
99
|
+
decode_input_pos = torch.tensor([0], dtype=torch.int)
|
100
|
+
decode_d_token = torch.tensor([[1]], dtype=torch.int)
|
101
|
+
decode_d_input_pos = torch.tensor([0], dtype=torch.int)
|
99
102
|
|
100
103
|
# Pad mask for self attention only on "real" tokens.
|
101
104
|
# Pad with `-inf` for any tokens indices that aren't desired.
|