ai-edge-torch-nightly 0.5.0.dev20250519__py3-none-any.whl → 0.6.0.dev20250521__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ai_edge_torch/generative/layers/einsum.py +37 -0
- ai_edge_torch/generative/layers/einsum_test.py +31 -0
- ai_edge_torch/generative/utilities/converter.py +5 -5
- ai_edge_torch/generative/utilities/export_config.py +5 -0
- ai_edge_torch/generative/utilities/loader.py +3 -3
- ai_edge_torch/version.py +1 -1
- {ai_edge_torch_nightly-0.5.0.dev20250519.dist-info → ai_edge_torch_nightly-0.6.0.dev20250521.dist-info}/METADATA +1 -1
- {ai_edge_torch_nightly-0.5.0.dev20250519.dist-info → ai_edge_torch_nightly-0.6.0.dev20250521.dist-info}/RECORD +11 -9
- {ai_edge_torch_nightly-0.5.0.dev20250519.dist-info → ai_edge_torch_nightly-0.6.0.dev20250521.dist-info}/LICENSE +0 -0
- {ai_edge_torch_nightly-0.5.0.dev20250519.dist-info → ai_edge_torch_nightly-0.6.0.dev20250521.dist-info}/WHEEL +0 -0
- {ai_edge_torch_nightly-0.5.0.dev20250519.dist-info → ai_edge_torch_nightly-0.6.0.dev20250521.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
# Copyright 2025 The AI Edge Torch Authors.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
# Einsum layer implementation.
|
16
|
+
|
17
|
+
from typing import Sequence
|
18
|
+
import torch
|
19
|
+
from torch import nn
|
20
|
+
|
21
|
+
|
22
|
+
class Einsum(nn.Module):
|
23
|
+
"""Einsum layer wrapping over torch.einsum."""
|
24
|
+
|
25
|
+
def __init__(self, shape: Sequence[int], einsum_str: str):
|
26
|
+
super().__init__()
|
27
|
+
self.shape = shape
|
28
|
+
self.einsum_str = einsum_str
|
29
|
+
self.w = nn.Parameter(
|
30
|
+
torch.empty(shape, dtype=torch.float32),
|
31
|
+
requires_grad=False,
|
32
|
+
)
|
33
|
+
self.einsum_fn = lambda x: torch.einsum(einsum_str, x, self.w)
|
34
|
+
|
35
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
36
|
+
"""Forward pass of the Einsum layer."""
|
37
|
+
return self.einsum_fn(x)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Copyright 2025 The AI Edge Torch Authors.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
|
16
|
+
from ai_edge_torch.generative.layers import einsum
|
17
|
+
import torch
|
18
|
+
from absl.testing import absltest as googletest
|
19
|
+
|
20
|
+
|
21
|
+
class FeedForwardTest(googletest.TestCase):
|
22
|
+
|
23
|
+
def test_einsum(self):
|
24
|
+
einsum_layer = einsum.Einsum(shape=(5, 10), einsum_str="btf,fd->btd")
|
25
|
+
x = torch.ones((1, 8, 5))
|
26
|
+
out = einsum_layer(x)
|
27
|
+
self.assertEqual(out.shape, (1, 8, 10))
|
28
|
+
|
29
|
+
|
30
|
+
if __name__ == "__main__":
|
31
|
+
googletest.main()
|
@@ -350,10 +350,10 @@ def _export_helper(
|
|
350
350
|
)
|
351
351
|
|
352
352
|
prefill_masks = None
|
353
|
-
if
|
353
|
+
if export_config.mask_as_input:
|
354
354
|
prefill_masks = _build_mask(
|
355
|
-
|
356
|
-
|
355
|
+
prefill_seq_lens,
|
356
|
+
config.kv_cache_max_len,
|
357
357
|
config.causal_mask_value,
|
358
358
|
)
|
359
359
|
if not isinstance(prefill_masks, list):
|
@@ -424,7 +424,7 @@ def _export_helper(
|
|
424
424
|
'input_pos': decode_input_pos,
|
425
425
|
'kv_cache': decode_kv,
|
426
426
|
}
|
427
|
-
if
|
427
|
+
if export_config.mask_as_input:
|
428
428
|
# Note that the decode mask is not a correct causal mask, but it is okay
|
429
429
|
# for the conversion purpose because only the shape matters in conversion.
|
430
430
|
# A correct causal mask of decode for a given token position of decode, it
|
@@ -433,7 +433,7 @@ def _export_helper(
|
|
433
433
|
# torch.triu(mask, diagonal=decode_position).unsqueeze(0).unsqueeze(0)
|
434
434
|
#
|
435
435
|
sample_kwargs['mask'] = _build_mask(
|
436
|
-
1,
|
436
|
+
1, config.kv_cache_max_len, config.causal_mask_value
|
437
437
|
)
|
438
438
|
if lora is not None:
|
439
439
|
sample_kwargs['lora'] = lora
|
@@ -43,6 +43,9 @@ class ExportConfig:
|
|
43
43
|
kvcache_cls: type = kv_utils.KVCache
|
44
44
|
# The batch size of the decode signature.
|
45
45
|
decode_batch_size: int = 1
|
46
|
+
# If true, the mask will be passed in as input. Otherwise, mask will be
|
47
|
+
# built by the model internally.
|
48
|
+
mask_as_input: bool = False
|
46
49
|
|
47
50
|
|
48
51
|
def get_from_flags() -> ExportConfig:
|
@@ -51,5 +54,7 @@ def get_from_flags() -> ExportConfig:
|
|
51
54
|
|
52
55
|
if flags.FLAGS.transpose_kv_cache:
|
53
56
|
export_config.kvcache_layout = kv_utils.KV_LAYOUT_TRANSPOSED
|
57
|
+
if flags.FLAGS.mask_as_input:
|
58
|
+
export_config.mask_as_input = flags.FLAGS.mask_as_input
|
54
59
|
|
55
60
|
return export_config
|
@@ -19,8 +19,8 @@ import os
|
|
19
19
|
from typing import Callable, Dict, List, Optional, Tuple
|
20
20
|
|
21
21
|
from ai_edge_torch.generative.layers import model_config
|
22
|
-
import safetensors
|
23
22
|
from safetensors import safe_open
|
23
|
+
from safetensors.torch import load_file
|
24
24
|
import torch
|
25
25
|
|
26
26
|
|
@@ -47,7 +47,7 @@ def get_custom_loader(
|
|
47
47
|
|
48
48
|
if checkpoint_format:
|
49
49
|
if checkpoint_format == "safetensors":
|
50
|
-
return
|
50
|
+
return load_file
|
51
51
|
if checkpoint_format == "pt":
|
52
52
|
return lambda path: torch.load(path, weights_only=True)
|
53
53
|
raise ValueError(f"Unsupported checkpoint format: {checkpoint_format}")
|
@@ -55,7 +55,7 @@ def get_custom_loader(
|
|
55
55
|
if os.path.splitext(checkpoint_path)[1] in [".bin", ".pt", ".ckpt"]:
|
56
56
|
return lambda path: torch.load(path, weights_only=True)
|
57
57
|
if checkpoint_path.endswith(".safetensors"):
|
58
|
-
return
|
58
|
+
return load_file
|
59
59
|
raise ValueError(f"Unsupported checkpoint format: {checkpoint_path}")
|
60
60
|
|
61
61
|
|
ai_edge_torch/version.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: ai-edge-torch-nightly
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.6.0.dev20250521
|
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,7 +2,7 @@ ai_edge_torch/__init__.py,sha256=lemyLCNoGYRnJsmDuGZu7qOqLbLqG6CGDFtu3ue1syU,129
|
|
2
2
|
ai_edge_torch/_config.py,sha256=AiqhbcheF7j_ozIGDLC89k1we95aVgFDa-tR6h7UI0s,2529
|
3
3
|
ai_edge_torch/conftest.py,sha256=r0GTrhMRhlmOGrrkvumHN8hkmyug6WvF60vWq8wRIBI,758
|
4
4
|
ai_edge_torch/model.py,sha256=wxjSFq_rBSxSqbUE8E8EJTCkgvgaRLjq_ZuAM-IZpCU,5606
|
5
|
-
ai_edge_torch/version.py,sha256=
|
5
|
+
ai_edge_torch/version.py,sha256=lmyCstaeVZjTAbBP4s9Z02tpX00ynyLPsymBY2tCe4A,706
|
6
6
|
ai_edge_torch/_convert/__init__.py,sha256=hHLluseD2R0Hh4W6XZRIXY_dRQeYudjsrKGf6LZz65g,671
|
7
7
|
ai_edge_torch/_convert/conversion.py,sha256=iQk3R-pLq4c1nfLqPB4xTRj78gghxPGzJCJtILLdg5o,6123
|
8
8
|
ai_edge_torch/_convert/conversion_utils.py,sha256=Sr8qXVcTwc-ZnZmK7yxVrIOOp1S_vNrwzC0zUvLTI2o,2160
|
@@ -171,6 +171,8 @@ ai_edge_torch/generative/layers/attention_test.py,sha256=9v8v96TLyFPdqxEylU1JOAe
|
|
171
171
|
ai_edge_torch/generative/layers/attention_utils.py,sha256=zBVwlBUTs-nStIKCZG0ks5ra7tsqc9ShfakFJKH5rds,7344
|
172
172
|
ai_edge_torch/generative/layers/attention_utils_test.py,sha256=22gQ1gcRPkwqFG3_p82GZfRKVE3udEssSy58wNOqv0w,2431
|
173
173
|
ai_edge_torch/generative/layers/builder.py,sha256=LXGuSHIx6QZAzLFm7aJvlzoMPgQwbXLFchGEKYwOOUA,5090
|
174
|
+
ai_edge_torch/generative/layers/einsum.py,sha256=EsZSWNVWUs0-1plp4TBnhP4ZhaRDBa2VlDO6hWpUAqU,1288
|
175
|
+
ai_edge_torch/generative/layers/einsum_test.py,sha256=ltIE773bvvNLv_9aLQxFwe1MgQ762sez0c5E2tejxuA,1079
|
174
176
|
ai_edge_torch/generative/layers/feed_forward.py,sha256=hdICat-8gW7-vxDAevJQ8NQ-mynllPiqLdXQMF6JMnc,4189
|
175
177
|
ai_edge_torch/generative/layers/feed_forward_test.py,sha256=8ZGy79BBpsyS6yKKDEKrDt249G5Mz-8VKWW7_WHx0u4,1655
|
176
178
|
ai_edge_torch/generative/layers/kv_cache.py,sha256=b-7shzDaKexmvQF7P3SiAmIz4ZofjYWv3m5u71GojsA,10460
|
@@ -203,9 +205,9 @@ ai_edge_torch/generative/test/test_model_conversion_large.py,sha256=vQWmpzMkJ2hP
|
|
203
205
|
ai_edge_torch/generative/test/test_quantize.py,sha256=kKJ01wscTC2t_Ylr7huO5gNKES01gm3dT1gx52z15PA,7356
|
204
206
|
ai_edge_torch/generative/test/utils.py,sha256=tF6aCfAGJnc9dmzCnZCEOuKNVimfWOqscv9og0DDLHU,2656
|
205
207
|
ai_edge_torch/generative/utilities/__init__.py,sha256=-_jxnnFnCgnTU4oTm4MnRsvL5lqhomBNdFBbqfmfHPo,720
|
206
|
-
ai_edge_torch/generative/utilities/converter.py,sha256=
|
207
|
-
ai_edge_torch/generative/utilities/export_config.py,sha256=
|
208
|
-
ai_edge_torch/generative/utilities/loader.py,sha256=
|
208
|
+
ai_edge_torch/generative/utilities/converter.py,sha256=DuoPb8Uhbxa32uUvr6grV5lssmUJdx298QwYz8cG_1Y,15512
|
209
|
+
ai_edge_torch/generative/utilities/export_config.py,sha256=qjkEbjcvi2AgQikZS5qfgR95Z5z9pm07KX-RN5ibfNE,2280
|
210
|
+
ai_edge_torch/generative/utilities/loader.py,sha256=oGgEc2tHRsVqSN3mgvcngXQrpV0a7cwTpJ3LmMVnyF0,15954
|
209
211
|
ai_edge_torch/generative/utilities/model_builder.py,sha256=tBfOcsI_NcneggHqkCSydYN3ZgmkzPc6nW0AJrA81wI,6461
|
210
212
|
ai_edge_torch/generative/utilities/moonshine_loader.py,sha256=_RpFabSqtGH5PHiP3_1f6QfO14qMADUxr_HGRlVDFB0,4891
|
211
213
|
ai_edge_torch/generative/utilities/stable_diffusion_loader.py,sha256=dqPD9qRXEWtU3ombslOC-BE2l_dMwHoCNu7NsIJhsso,36158
|
@@ -262,8 +264,8 @@ ai_edge_torch/testing/__init__.py,sha256=_yGgvnBZWb7T3IN3mc4x1sS4vM96HZwM8pwIcPG
|
|
262
264
|
ai_edge_torch/testing/export.py,sha256=k5mGDGzwc23Z4zaIVDs8CNh-oOt64gsf9MS9NjhbPy4,3293
|
263
265
|
ai_edge_torch/testing/model_coverage/__init__.py,sha256=5P8J6Zk5YYtDvTBucFvB9NGSRI7Gw_24WnrbhXgycEE,765
|
264
266
|
ai_edge_torch/testing/model_coverage/model_coverage.py,sha256=UPB448aMDUyC0HNYVqio2rcJPnDN0tBQMP08J6vPYew,4718
|
265
|
-
ai_edge_torch_nightly-0.
|
266
|
-
ai_edge_torch_nightly-0.
|
267
|
-
ai_edge_torch_nightly-0.
|
268
|
-
ai_edge_torch_nightly-0.
|
269
|
-
ai_edge_torch_nightly-0.
|
267
|
+
ai_edge_torch_nightly-0.6.0.dev20250521.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
268
|
+
ai_edge_torch_nightly-0.6.0.dev20250521.dist-info/METADATA,sha256=_UC8q7Xe3xMUCwKKbF4CJ5hewK9PLIJ26ksKCAeWjik,2074
|
269
|
+
ai_edge_torch_nightly-0.6.0.dev20250521.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
270
|
+
ai_edge_torch_nightly-0.6.0.dev20250521.dist-info/top_level.txt,sha256=5KXRaF2hwkApYxf7Y8y_tVb9aulGTlbOoNdbx1aKRkE,14
|
271
|
+
ai_edge_torch_nightly-0.6.0.dev20250521.dist-info/RECORD,,
|
File without changes
|
File without changes
|