ai-edge-torch-nightly 0.7.0.dev20250919__py3-none-any.whl → 0.7.0.dev20250920__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.
Potentially problematic release.
This version of ai-edge-torch-nightly might be problematic. Click here for more details.
- ai_edge_torch/generative/utilities/converter.py +54 -3
- ai_edge_torch/generative/utilities/litertlm_builder.py +129 -0
- ai_edge_torch/version.py +1 -1
- {ai_edge_torch_nightly-0.7.0.dev20250919.dist-info → ai_edge_torch_nightly-0.7.0.dev20250920.dist-info}/METADATA +1 -1
- {ai_edge_torch_nightly-0.7.0.dev20250919.dist-info → ai_edge_torch_nightly-0.7.0.dev20250920.dist-info}/RECORD +8 -7
- {ai_edge_torch_nightly-0.7.0.dev20250919.dist-info → ai_edge_torch_nightly-0.7.0.dev20250920.dist-info}/LICENSE +0 -0
- {ai_edge_torch_nightly-0.7.0.dev20250919.dist-info → ai_edge_torch_nightly-0.7.0.dev20250920.dist-info}/WHEEL +0 -0
- {ai_edge_torch_nightly-0.7.0.dev20250919.dist-info → ai_edge_torch_nightly-0.7.0.dev20250920.dist-info}/top_level.txt +0 -0
|
@@ -18,18 +18,20 @@
|
|
|
18
18
|
import enum
|
|
19
19
|
import os
|
|
20
20
|
import pathlib
|
|
21
|
-
|
|
21
|
+
import tempfile
|
|
22
|
+
from typing import Any, Optional, Union
|
|
22
23
|
from absl import flags
|
|
23
24
|
from ai_edge_torch._convert import converter as converter_utils
|
|
24
25
|
from ai_edge_torch.generative.layers import kv_cache as kv_utils
|
|
25
26
|
from ai_edge_torch.generative.layers import lora as lora_utils
|
|
26
27
|
import ai_edge_torch.generative.layers.model_config as cfg
|
|
27
28
|
from ai_edge_torch.generative.quantize import quant_recipes
|
|
28
|
-
from ai_edge_torch.generative.utilities import export_config
|
|
29
|
+
from ai_edge_torch.generative.utilities import export_config as export_config_lib
|
|
30
|
+
from ai_edge_torch.generative.utilities import litertlm_builder
|
|
29
31
|
from ai_edge_torch.quantize import quant_config as qcfg
|
|
30
32
|
import torch
|
|
31
33
|
|
|
32
|
-
ExportConfig =
|
|
34
|
+
ExportConfig = export_config_lib.ExportConfig
|
|
33
35
|
|
|
34
36
|
|
|
35
37
|
class ExportableModule(torch.nn.Module):
|
|
@@ -325,6 +327,7 @@ def convert_to_tflite(
|
|
|
325
327
|
loras,
|
|
326
328
|
export_config,
|
|
327
329
|
)
|
|
330
|
+
return output_file
|
|
328
331
|
|
|
329
332
|
|
|
330
333
|
def _export_helper(
|
|
@@ -457,3 +460,51 @@ def _export_helper(
|
|
|
457
460
|
quant_config=quant_config,
|
|
458
461
|
)
|
|
459
462
|
edge_model.export(output_file)
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def convert_to_litert(
|
|
466
|
+
pytorch_model: torch.nn.Module,
|
|
467
|
+
output_path: str,
|
|
468
|
+
output_name_prefix: str,
|
|
469
|
+
prefill_seq_len: Union[int, list[int]],
|
|
470
|
+
kv_cache_max_len: int,
|
|
471
|
+
pixel_values_size: torch.Size = None,
|
|
472
|
+
pixel_seq_len: int = 0,
|
|
473
|
+
quantize: str = 'dynamic_int8',
|
|
474
|
+
config: cfg.ModelConfig = None,
|
|
475
|
+
lora_ranks: Optional[list[int]] = None,
|
|
476
|
+
export_config: ExportConfig = None,
|
|
477
|
+
output_format: str = 'tflite',
|
|
478
|
+
**kwargs,
|
|
479
|
+
):
|
|
480
|
+
"""Converts a nn.Module model to multi-signature tflite model and pack it."""
|
|
481
|
+
with tempfile.TemporaryDirectory() as workdir:
|
|
482
|
+
if output_format == 'litertlm':
|
|
483
|
+
tflite_model_output_path = workdir
|
|
484
|
+
else:
|
|
485
|
+
tflite_model_output_path = output_path
|
|
486
|
+
tflite_model_path = convert_to_tflite(
|
|
487
|
+
pytorch_model,
|
|
488
|
+
tflite_model_output_path,
|
|
489
|
+
output_name_prefix,
|
|
490
|
+
prefill_seq_len,
|
|
491
|
+
kv_cache_max_len,
|
|
492
|
+
pixel_values_size,
|
|
493
|
+
pixel_seq_len,
|
|
494
|
+
quantize,
|
|
495
|
+
config,
|
|
496
|
+
lora_ranks,
|
|
497
|
+
export_config,
|
|
498
|
+
)
|
|
499
|
+
if output_format == 'litertlm':
|
|
500
|
+
tokenizer_model_path = kwargs.pop('tokenizer_model_path', None)
|
|
501
|
+
hf_tokenizer_model_path = kwargs.pop('hf_tokenizer_model_path', None)
|
|
502
|
+
litertlm_builder.build_litertlm(
|
|
503
|
+
tflite_model_path=tflite_model_path,
|
|
504
|
+
workdir=workdir,
|
|
505
|
+
output_path=output_path,
|
|
506
|
+
context_length=kv_cache_max_len,
|
|
507
|
+
tokenizer_model_path=tokenizer_model_path,
|
|
508
|
+
hf_tokenizer_model_path=hf_tokenizer_model_path,
|
|
509
|
+
**kwargs,
|
|
510
|
+
)
|
|
@@ -0,0 +1,129 @@
|
|
|
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
|
+
"""Utilities for building LiteRT-LM files."""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
import os
|
|
20
|
+
import pathlib
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
# pylint: disable=g-import-not-at-top
|
|
24
|
+
from ai_edge_litert.internal import llm_metadata_pb2
|
|
25
|
+
from ai_edge_litert.internal import litertlm_builder
|
|
26
|
+
# pylint: enable=g-import-not-at-top
|
|
27
|
+
|
|
28
|
+
_litertlm_builder_available = True
|
|
29
|
+
except ImportError:
|
|
30
|
+
llm_metadata_pb2 = None
|
|
31
|
+
litertlm_builder = None
|
|
32
|
+
_litertlm_builder_available = False
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def is_litertlm_builder_available() -> bool:
|
|
36
|
+
return _litertlm_builder_available
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def build_litertlm(
|
|
40
|
+
tflite_model_path: str,
|
|
41
|
+
workdir: str,
|
|
42
|
+
output_path: str,
|
|
43
|
+
context_length: int,
|
|
44
|
+
model_prompt_prefix: str | None,
|
|
45
|
+
model_prompt_suffix: str | None,
|
|
46
|
+
user_prompt_prefix: str | None,
|
|
47
|
+
user_prompt_suffix: str | None,
|
|
48
|
+
tokenizer_model_path: str | None,
|
|
49
|
+
hf_tokenizer_model_path: str | None,
|
|
50
|
+
start_token: str | None = None,
|
|
51
|
+
start_token_id: int | None = None,
|
|
52
|
+
stop_tokens: str | list[str] | None = None,
|
|
53
|
+
stop_token_ids: list[int] | None = None,
|
|
54
|
+
**kwargs,
|
|
55
|
+
):
|
|
56
|
+
"""Builds a LiteRT-LM file from a TFlite model."""
|
|
57
|
+
del kwargs
|
|
58
|
+
|
|
59
|
+
if not is_litertlm_builder_available():
|
|
60
|
+
raise ValueError('LiteRT-LM builder is not available.')
|
|
61
|
+
assert llm_metadata_pb2 is not None
|
|
62
|
+
assert litertlm_builder is not None
|
|
63
|
+
|
|
64
|
+
llm_metadata = llm_metadata_pb2.LlmMetadata()
|
|
65
|
+
|
|
66
|
+
if start_token_id is not None:
|
|
67
|
+
llm_metadata.start_token.token_ids.ids.append(start_token_id)
|
|
68
|
+
elif start_token is not None:
|
|
69
|
+
llm_metadata.start_token.token_str = start_token
|
|
70
|
+
|
|
71
|
+
stop_tokens_list = []
|
|
72
|
+
if stop_tokens is not None:
|
|
73
|
+
if isinstance(stop_tokens, str):
|
|
74
|
+
stop_tokens_list.append(stop_tokens)
|
|
75
|
+
else:
|
|
76
|
+
stop_tokens_list.extend(stop_tokens)
|
|
77
|
+
if stop_token_ids is not None:
|
|
78
|
+
stop_tokens_list.extend(stop_token_ids)
|
|
79
|
+
for stop_token in stop_tokens_list:
|
|
80
|
+
if isinstance(stop_token, str):
|
|
81
|
+
tu = llm_metadata.stop_tokens.add()
|
|
82
|
+
tu.token_str = stop_token
|
|
83
|
+
else:
|
|
84
|
+
assert isinstance(stop_token, int)
|
|
85
|
+
tu = llm_metadata.stop_tokens.add()
|
|
86
|
+
tu.token_ids.ids.append(stop_token)
|
|
87
|
+
|
|
88
|
+
if model_prompt_prefix is not None:
|
|
89
|
+
llm_metadata.prompt_templates.model.prefix = model_prompt_prefix
|
|
90
|
+
if model_prompt_suffix is not None:
|
|
91
|
+
llm_metadata.prompt_templates.model.suffix = model_prompt_suffix
|
|
92
|
+
if user_prompt_prefix is not None:
|
|
93
|
+
llm_metadata.prompt_templates.user.prefix = user_prompt_prefix
|
|
94
|
+
if user_prompt_suffix is not None:
|
|
95
|
+
llm_metadata.prompt_templates.user.suffix = user_prompt_suffix
|
|
96
|
+
|
|
97
|
+
llm_metadata.max_num_tokens = context_length
|
|
98
|
+
|
|
99
|
+
llm_metadata_path = os.path.join(workdir, 'llm_metadata.pb')
|
|
100
|
+
with open(llm_metadata_path, 'wb') as f:
|
|
101
|
+
f.write(llm_metadata.SerializeToString())
|
|
102
|
+
|
|
103
|
+
builder = litertlm_builder.LitertLmFileBuilder()
|
|
104
|
+
builder.add_system_metadata(
|
|
105
|
+
litertlm_builder.Metadata(
|
|
106
|
+
key='Authors',
|
|
107
|
+
value='',
|
|
108
|
+
dtype=litertlm_builder.DType.STRING,
|
|
109
|
+
)
|
|
110
|
+
)
|
|
111
|
+
builder.add_llm_metadata(llm_metadata_path)
|
|
112
|
+
if tokenizer_model_path:
|
|
113
|
+
builder.add_sentencepiece_tokenizer(tokenizer_model_path)
|
|
114
|
+
elif hf_tokenizer_model_path:
|
|
115
|
+
builder.add_hf_tokenizer(hf_tokenizer_model_path)
|
|
116
|
+
else:
|
|
117
|
+
raise ValueError(
|
|
118
|
+
'Either tokenizer_model_path or hf_tokenizer_model_path must be'
|
|
119
|
+
' provided.'
|
|
120
|
+
)
|
|
121
|
+
builder.add_tflite_model(
|
|
122
|
+
tflite_model_path, litertlm_builder.TfLiteModelType.PREFILL_DECODE
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
file_name = pathlib.Path(tflite_model_path).stem + '.litertlm'
|
|
126
|
+
if os.path.dirname(file_name):
|
|
127
|
+
os.makedirs(os.path.dirname(file_name), exist_ok=True)
|
|
128
|
+
with open(os.path.join(output_path, file_name), 'wb') as f:
|
|
129
|
+
builder.build(f)
|
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.7.0.
|
|
3
|
+
Version: 0.7.0.dev20250920
|
|
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=A7loFu8jE9CsXsfMmHYZ-KDFJiaD8Kkqwm_9d3IVzk0,5638
|
|
5
|
-
ai_edge_torch/version.py,sha256=
|
|
5
|
+
ai_edge_torch/version.py,sha256=AMujyxObvKlUOBjKtYZjM5FWraCbXiQmR9pEr_hpKI4,806
|
|
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
|
|
@@ -208,8 +208,9 @@ ai_edge_torch/generative/test/test_model_conversion_large.py,sha256=NkEwrjO8vIcd
|
|
|
208
208
|
ai_edge_torch/generative/test/test_quantize.py,sha256=kKJ01wscTC2t_Ylr7huO5gNKES01gm3dT1gx52z15PA,7356
|
|
209
209
|
ai_edge_torch/generative/test/utils.py,sha256=tF6aCfAGJnc9dmzCnZCEOuKNVimfWOqscv9og0DDLHU,2656
|
|
210
210
|
ai_edge_torch/generative/utilities/__init__.py,sha256=-_jxnnFnCgnTU4oTm4MnRsvL5lqhomBNdFBbqfmfHPo,720
|
|
211
|
-
ai_edge_torch/generative/utilities/converter.py,sha256=
|
|
211
|
+
ai_edge_torch/generative/utilities/converter.py,sha256=Q2C9DqYWqiyuu8KoL69SAxDL-RNJ7K8_Z45H2FpsAn0,17563
|
|
212
212
|
ai_edge_torch/generative/utilities/export_config.py,sha256=qjkEbjcvi2AgQikZS5qfgR95Z5z9pm07KX-RN5ibfNE,2280
|
|
213
|
+
ai_edge_torch/generative/utilities/litertlm_builder.py,sha256=0cNuaqhc7cQcAa4NRalUXyoPQUQC9O3-aHAJEDV1Mps,4265
|
|
213
214
|
ai_edge_torch/generative/utilities/loader.py,sha256=drgKBmNibuc3PCdc0kU0pVcp2Nt1_mjLYh67RyXOn7U,15952
|
|
214
215
|
ai_edge_torch/generative/utilities/model_builder.py,sha256=xBvcTxihB9TN88UtQiXA9sAITQgf-pA77R-VZlLgUeU,6950
|
|
215
216
|
ai_edge_torch/generative/utilities/moonshine_loader.py,sha256=_RpFabSqtGH5PHiP3_1f6QfO14qMADUxr_HGRlVDFB0,4891
|
|
@@ -269,8 +270,8 @@ ai_edge_torch/testing/__init__.py,sha256=_yGgvnBZWb7T3IN3mc4x1sS4vM96HZwM8pwIcPG
|
|
|
269
270
|
ai_edge_torch/testing/export.py,sha256=k5mGDGzwc23Z4zaIVDs8CNh-oOt64gsf9MS9NjhbPy4,3293
|
|
270
271
|
ai_edge_torch/testing/model_coverage/__init__.py,sha256=5P8J6Zk5YYtDvTBucFvB9NGSRI7Gw_24WnrbhXgycEE,765
|
|
271
272
|
ai_edge_torch/testing/model_coverage/model_coverage.py,sha256=UPB448aMDUyC0HNYVqio2rcJPnDN0tBQMP08J6vPYew,4718
|
|
272
|
-
ai_edge_torch_nightly-0.7.0.
|
|
273
|
-
ai_edge_torch_nightly-0.7.0.
|
|
274
|
-
ai_edge_torch_nightly-0.7.0.
|
|
275
|
-
ai_edge_torch_nightly-0.7.0.
|
|
276
|
-
ai_edge_torch_nightly-0.7.0.
|
|
273
|
+
ai_edge_torch_nightly-0.7.0.dev20250920.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
274
|
+
ai_edge_torch_nightly-0.7.0.dev20250920.dist-info/METADATA,sha256=IReTV7SfbskOGXNOj3XypKPULQxj4aIKg6QMaxGDMaY,2074
|
|
275
|
+
ai_edge_torch_nightly-0.7.0.dev20250920.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
|
276
|
+
ai_edge_torch_nightly-0.7.0.dev20250920.dist-info/top_level.txt,sha256=5KXRaF2hwkApYxf7Y8y_tVb9aulGTlbOoNdbx1aKRkE,14
|
|
277
|
+
ai_edge_torch_nightly-0.7.0.dev20250920.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|