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
@@ -14,19 +14,22 @@
|
|
14
14
|
# ==============================================================================
|
15
15
|
# Common normalization layers.
|
16
16
|
|
17
|
+
from ai_edge_torch.hlfb import StableHLOCompositeBuilder
|
17
18
|
import torch
|
19
|
+
from torch import nn
|
20
|
+
import torch.nn.functional as F
|
18
21
|
|
19
22
|
|
20
23
|
# Implementation for RMSNorm from: https://arxiv.org/abs/1910.07467
|
21
24
|
class RMSNorm(torch.nn.Module):
|
22
25
|
|
23
26
|
def __init__(self, dim: int, eps: float = 1e-6, zero_centered_gamma=False):
|
24
|
-
"""
|
25
|
-
Initialize the RMSNorm layer.
|
27
|
+
"""Initialize the RMSNorm layer.
|
26
28
|
|
27
29
|
Args:
|
28
30
|
dim (int): dimension of the input tensor.
|
29
|
-
eps (float): A small float value to ensure numerical stability (default:
|
31
|
+
eps (float): A small float value to ensure numerical stability (default:
|
32
|
+
1e-6).
|
30
33
|
"""
|
31
34
|
super().__init__()
|
32
35
|
self.eps = eps
|
@@ -34,8 +37,7 @@ class RMSNorm(torch.nn.Module):
|
|
34
37
|
self.zero_centered_gamma = zero_centered_gamma
|
35
38
|
|
36
39
|
def _norm(self, x):
|
37
|
-
"""
|
38
|
-
Apply RMSNorm normalization.
|
40
|
+
"""Apply RMSNorm normalization.
|
39
41
|
|
40
42
|
Args:
|
41
43
|
x (torch.Tensor): input tensor.
|
@@ -46,8 +48,7 @@ class RMSNorm(torch.nn.Module):
|
|
46
48
|
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
47
49
|
|
48
50
|
def forward(self, x):
|
49
|
-
"""
|
50
|
-
Running the forward pass of RMSNorm layer.
|
51
|
+
"""Running the forward pass of RMSNorm layer.
|
51
52
|
|
52
53
|
Args:
|
53
54
|
x (torch.Tensor): input tensor.
|
@@ -60,3 +61,180 @@ class RMSNorm(torch.nn.Module):
|
|
60
61
|
return output * (1 + self.weight)
|
61
62
|
else:
|
62
63
|
return output * self.weight
|
64
|
+
|
65
|
+
|
66
|
+
class GroupNorm(torch.nn.Module):
|
67
|
+
|
68
|
+
def __init__(
|
69
|
+
self,
|
70
|
+
group_num: int,
|
71
|
+
dim: int,
|
72
|
+
eps: float = 1e-5,
|
73
|
+
enable_hlfb: bool = False,
|
74
|
+
):
|
75
|
+
"""Initialize the GroupNorm layer.
|
76
|
+
|
77
|
+
Args:
|
78
|
+
group_num (int): Number of groups to separate the channels into.
|
79
|
+
dim (int): Dimension of the input tensor.
|
80
|
+
eps (float): A small float value to ensure numerical stability (default:
|
81
|
+
1e-5).
|
82
|
+
enable_hlfb (bool): Whether to convert this normalization into a single
|
83
|
+
op.
|
84
|
+
"""
|
85
|
+
super().__init__()
|
86
|
+
self.enable_hlfb = enable_hlfb
|
87
|
+
self.group_num = group_num
|
88
|
+
self.eps = eps
|
89
|
+
self.weight = torch.nn.Parameter(torch.ones(dim))
|
90
|
+
self.bias = torch.nn.Parameter(torch.ones(dim))
|
91
|
+
|
92
|
+
def forward(self, x):
|
93
|
+
"""Running the forward pass of GroupNorm layer.
|
94
|
+
|
95
|
+
Args:
|
96
|
+
x (torch.Tensor): input tensor.
|
97
|
+
|
98
|
+
Returns:
|
99
|
+
torch.Tensor: output tensor after applying GroupNorm.
|
100
|
+
"""
|
101
|
+
if self.enable_hlfb:
|
102
|
+
return group_norm_with_hlfb(
|
103
|
+
x,
|
104
|
+
self.weight,
|
105
|
+
self.bias,
|
106
|
+
self.group_num,
|
107
|
+
self.eps,
|
108
|
+
)
|
109
|
+
else:
|
110
|
+
return F.group_norm(x, self.group_num, self.weight, self.bias, self.eps)
|
111
|
+
|
112
|
+
|
113
|
+
class LayerNorm(torch.nn.Module):
|
114
|
+
|
115
|
+
def __init__(
|
116
|
+
self,
|
117
|
+
dim: int,
|
118
|
+
eps: float = 1e-5,
|
119
|
+
enable_hlfb: bool = False,
|
120
|
+
use_input_shape: bool = True,
|
121
|
+
):
|
122
|
+
"""Initialize the LayerNorm layer.
|
123
|
+
|
124
|
+
Args:
|
125
|
+
dim (int): dimension of the input tensor.
|
126
|
+
eps (float): A small float value to ensure numerical stability (default:
|
127
|
+
1e-6).
|
128
|
+
enable_hlfb (bool): Whether to convert this normalization into a single
|
129
|
+
op.
|
130
|
+
use_input_shape (bool): Whether to use the input shape to determine the
|
131
|
+
dimension of normalization (default: True).
|
132
|
+
"""
|
133
|
+
super().__init__()
|
134
|
+
self.enable_hlfb = enable_hlfb
|
135
|
+
self.use_input_shape = use_input_shape
|
136
|
+
self.eps = eps
|
137
|
+
self.weight = torch.nn.Parameter(torch.ones(dim))
|
138
|
+
self.bias = torch.nn.Parameter(torch.ones(dim))
|
139
|
+
|
140
|
+
def forward(self, x):
|
141
|
+
"""Running the forward pass of LayerNorm layer.
|
142
|
+
|
143
|
+
Args:
|
144
|
+
x (torch.Tensor): input tensor.
|
145
|
+
|
146
|
+
Returns:
|
147
|
+
torch.Tensor: output tensor after applying LayerNorm.
|
148
|
+
"""
|
149
|
+
if self.enable_hlfb:
|
150
|
+
return layer_norm_with_hlfb(
|
151
|
+
x, self.weight, self.bias, self.eps, self.use_input_shape
|
152
|
+
)
|
153
|
+
|
154
|
+
if self.use_input_shape:
|
155
|
+
normalized_shape = x.shape
|
156
|
+
weight = self.weight.broadcast_to(x.shape)
|
157
|
+
bias = self.bias.broadcast_to(x.shape)
|
158
|
+
else:
|
159
|
+
normalized_shape = self.weight.shape
|
160
|
+
weight = self.weight
|
161
|
+
bias = self.bias
|
162
|
+
return F.layer_norm(x, normalized_shape, weight, bias, self.eps)
|
163
|
+
|
164
|
+
|
165
|
+
def group_norm_with_hlfb(
|
166
|
+
x: torch.Tensor,
|
167
|
+
w: torch.Tensor,
|
168
|
+
b: torch.Tensor,
|
169
|
+
num_groups: int,
|
170
|
+
eps: float,
|
171
|
+
):
|
172
|
+
"""Group Normalization with high-level function boundary enabled.
|
173
|
+
|
174
|
+
Args:
|
175
|
+
x (torch.Tensor): Input tensor for Group Normalization, with BCHW shape.
|
176
|
+
w (torch.Tensor): The weight tensor for the normalization.
|
177
|
+
b (torch.Tensor): The bias tensor for the normalization.
|
178
|
+
num_groups (int): Number of groups to separate the channels into.
|
179
|
+
eps (float): A small float value to ensure numerical stability.
|
180
|
+
|
181
|
+
Returns:
|
182
|
+
The output tensor of Group Normalization.
|
183
|
+
"""
|
184
|
+
x = torch.permute(x, (0, 2, 3, 1))
|
185
|
+
|
186
|
+
# TODO: b/366544750 - Change "reduction_axes" field as an array, rather than
|
187
|
+
# int32 when the bug is fixed.
|
188
|
+
builder = StableHLOCompositeBuilder(
|
189
|
+
name="odml.group_norm",
|
190
|
+
attr={
|
191
|
+
"num_groups": num_groups,
|
192
|
+
"epsilon": eps,
|
193
|
+
"reduction_axes": 3,
|
194
|
+
"channel_axis": 3,
|
195
|
+
},
|
196
|
+
)
|
197
|
+
x, w, b = builder.mark_inputs(x, w, b)
|
198
|
+
x = torch.permute(x, (0, 3, 1, 2))
|
199
|
+
y = F.group_norm(x, num_groups, weight=w, bias=b, eps=eps)
|
200
|
+
y = torch.permute(y, (0, 2, 3, 1))
|
201
|
+
y = builder.mark_outputs(y)
|
202
|
+
|
203
|
+
y = torch.permute(y, (0, 3, 1, 2))
|
204
|
+
return y
|
205
|
+
|
206
|
+
|
207
|
+
def layer_norm_with_hlfb(
|
208
|
+
x: torch.Tensor,
|
209
|
+
w: torch.Tensor,
|
210
|
+
b: torch.Tensor,
|
211
|
+
eps: float,
|
212
|
+
use_input_shape: bool,
|
213
|
+
):
|
214
|
+
"""Layer Normalization with high-level function boundary enabled.
|
215
|
+
|
216
|
+
Args:
|
217
|
+
x (torch.Tensor): Input tensor for Layer Normalization, with BCHW shape.
|
218
|
+
w (torch.Tensor): The weight tensor for the normalization.
|
219
|
+
b (torch.Tensor): The bias tensor for the normalization.
|
220
|
+
eps (float): A small float value to ensure numerical stability.
|
221
|
+
use_input_shape (bool): Whether to use the input shape to determine the
|
222
|
+
dimension of normalization.
|
223
|
+
|
224
|
+
Returns:
|
225
|
+
The output tensor of Layer Normalization.
|
226
|
+
"""
|
227
|
+
builder = StableHLOCompositeBuilder(
|
228
|
+
name="odml.group_norm",
|
229
|
+
attr={"num_groups": 1, "epsilon": eps, "channel_axis": 1},
|
230
|
+
)
|
231
|
+
x, w, b = builder.mark_inputs(x, w, b)
|
232
|
+
if use_input_shape:
|
233
|
+
normalized_shape = x.shape
|
234
|
+
w = w.broadcast_to(x.shape)
|
235
|
+
b = b.broadcast_to(x.shape)
|
236
|
+
else:
|
237
|
+
normalized_shape = w.shape
|
238
|
+
y = F.layer_norm(x, normalized_shape, w, b, eps=eps)
|
239
|
+
y = builder.mark_outputs(y)
|
240
|
+
return y
|
@@ -16,13 +16,15 @@
|
|
16
16
|
import torch
|
17
17
|
|
18
18
|
|
19
|
-
def apply_rope(
|
19
|
+
def apply_rope(
|
20
|
+
x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor
|
21
|
+
) -> torch.Tensor:
|
20
22
|
"""Computes rotary positional embedding.
|
21
23
|
|
22
24
|
Args:
|
23
|
-
x
|
24
|
-
cos
|
25
|
-
sin
|
25
|
+
x: the input tensor.
|
26
|
+
cos: cosine value for the rope.
|
27
|
+
sin: sin value for the rope.
|
26
28
|
|
27
29
|
Returns:
|
28
30
|
output tensor of RoPE.
|
@@ -17,11 +17,10 @@
|
|
17
17
|
import math
|
18
18
|
from typing import Optional
|
19
19
|
|
20
|
+
from ai_edge_torch.hlfb import StableHLOCompositeBuilder
|
20
21
|
import torch
|
21
22
|
import torch.nn.functional as F
|
22
23
|
|
23
|
-
from ai_edge_torch.hlfb import StableHLOCompositeBuilder
|
24
|
-
|
25
24
|
|
26
25
|
def scaled_dot_product_attention(
|
27
26
|
q: torch.Tensor,
|
@@ -30,6 +29,7 @@ def scaled_dot_product_attention(
|
|
30
29
|
head_size: int,
|
31
30
|
mask: Optional[torch.Tensor] = None,
|
32
31
|
scale: Optional[float] = None,
|
32
|
+
softcap: Optional[float] = None,
|
33
33
|
):
|
34
34
|
"""Scaled dot product attention.
|
35
35
|
|
@@ -54,15 +54,26 @@ def scaled_dot_product_attention(
|
|
54
54
|
# Handle the GQA case, where q.shape[1] % k.shape[1] == 0.
|
55
55
|
k = k.repeat_interleave(q.shape[1] // k.shape[1], dim=1)
|
56
56
|
v = v.repeat_interleave(q.shape[1] // v.shape[1], dim=1)
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
57
|
+
if softcap is None:
|
58
|
+
y = F.scaled_dot_product_attention(
|
59
|
+
q,
|
60
|
+
k,
|
61
|
+
v,
|
62
|
+
attn_mask=mask,
|
63
|
+
dropout_p=0.0,
|
64
|
+
is_causal=mask is None,
|
65
|
+
scale=scale,
|
66
|
+
)
|
67
|
+
else:
|
68
|
+
q.mul_(scale)
|
69
|
+
scores = q @ k.transpose(-1, -2)
|
70
|
+
scores = scores / softcap
|
71
|
+
scores = torch.tanh(scores)
|
72
|
+
scores = scores * softcap
|
73
|
+
scores = scores + mask
|
74
|
+
out = F.softmax(scores.float(), dim=-1).type_as(q)
|
75
|
+
y = torch.matmul(out, v)
|
76
|
+
|
66
77
|
return y.transpose(1, 2)
|
67
78
|
|
68
79
|
|
@@ -73,6 +84,7 @@ def scaled_dot_product_attention_with_hlfb(
|
|
73
84
|
head_size: int,
|
74
85
|
mask: Optional[torch.Tensor] = None,
|
75
86
|
scale: Optional[float] = None,
|
87
|
+
softcap: Optional[float] = None,
|
76
88
|
):
|
77
89
|
"""Scaled dot product attention with high-level function boundary enabled.
|
78
90
|
|
@@ -90,8 +102,13 @@ def scaled_dot_product_attention_with_hlfb(
|
|
90
102
|
if scale is None:
|
91
103
|
scale = 1.0 / math.sqrt(head_size)
|
92
104
|
|
105
|
+
attrs = {"scale": scale}
|
106
|
+
|
107
|
+
if softcap is not None:
|
108
|
+
attrs["logit_cap"] = softcap
|
109
|
+
|
93
110
|
builder = StableHLOCompositeBuilder(
|
94
|
-
name="odml.scaled_dot_product_attention", attr=
|
111
|
+
name="odml.scaled_dot_product_attention", attr=attrs
|
95
112
|
)
|
96
113
|
q, k, v, mask = builder.mark_inputs(q, k, v, mask)
|
97
114
|
|
@@ -102,15 +119,25 @@ def scaled_dot_product_attention_with_hlfb(
|
|
102
119
|
# Handle the GQA case, where q.shape[1] % k.shape[1] == 0.
|
103
120
|
k = k.repeat_interleave(q.shape[1] // k.shape[1], dim=1)
|
104
121
|
v = v.repeat_interleave(q.shape[1] // v.shape[1], dim=1)
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
122
|
+
if softcap is None:
|
123
|
+
y = F.scaled_dot_product_attention(
|
124
|
+
q,
|
125
|
+
k,
|
126
|
+
v,
|
127
|
+
attn_mask=mask,
|
128
|
+
dropout_p=0.0,
|
129
|
+
is_causal=mask is None,
|
130
|
+
scale=scale,
|
131
|
+
)
|
132
|
+
else:
|
133
|
+
q.mul_(scale)
|
134
|
+
scores = q @ k.transpose(-1, -2)
|
135
|
+
scores = scores / softcap
|
136
|
+
scores = torch.tanh(scores)
|
137
|
+
scores = scores * softcap
|
138
|
+
scores = scores + mask
|
139
|
+
out = F.softmax(scores.float(), dim=-1).type_as(q)
|
140
|
+
y = torch.matmul(out, v)
|
114
141
|
|
115
142
|
result = y.transpose(1, 2)
|
116
143
|
result = builder.mark_outputs(result)
|