optimum-executorch 0.0.0.dev0__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.
Files changed (35) hide show
  1. optimum/commands/export/executorch.py +236 -0
  2. optimum/commands/register/register_export.py +19 -0
  3. optimum/executorch/__init__.py +43 -0
  4. optimum/executorch/attentions/custom_kv_cache.py +415 -0
  5. optimum/executorch/attentions/custom_sdpa.py +143 -0
  6. optimum/executorch/modeling.py +1388 -0
  7. optimum/executorch/passes/remove_padding_idx_embedding_pass.py +22 -0
  8. optimum/executorch/stats.py +209 -0
  9. optimum/executorch/version.py +15 -0
  10. optimum/exporters/executorch/__init__.py +59 -0
  11. optimum/exporters/executorch/__main__.py +174 -0
  12. optimum/exporters/executorch/convert.py +91 -0
  13. optimum/exporters/executorch/integrations.py +884 -0
  14. optimum/exporters/executorch/quantization.py +108 -0
  15. optimum/exporters/executorch/recipe_registry.py +70 -0
  16. optimum/exporters/executorch/recipes/__init__.py +15 -0
  17. optimum/exporters/executorch/recipes/coreml.py +151 -0
  18. optimum/exporters/executorch/recipes/cuda.py +129 -0
  19. optimum/exporters/executorch/recipes/metal.py +129 -0
  20. optimum/exporters/executorch/recipes/portable.py +91 -0
  21. optimum/exporters/executorch/recipes/xnnpack.py +123 -0
  22. optimum/exporters/executorch/task_registry.py +70 -0
  23. optimum/exporters/executorch/tasks/__init__.py +15 -0
  24. optimum/exporters/executorch/tasks/asr.py +105 -0
  25. optimum/exporters/executorch/tasks/causal_lm.py +149 -0
  26. optimum/exporters/executorch/tasks/image_classification.py +42 -0
  27. optimum/exporters/executorch/tasks/masked_lm.py +54 -0
  28. optimum/exporters/executorch/tasks/multimodal_text_to_text.py +249 -0
  29. optimum/exporters/executorch/tasks/seq2seq_lm.py +58 -0
  30. optimum/exporters/executorch/utils.py +205 -0
  31. optimum_executorch-0.0.0.dev0.dist-info/METADATA +289 -0
  32. optimum_executorch-0.0.0.dev0.dist-info/RECORD +35 -0
  33. optimum_executorch-0.0.0.dev0.dist-info/WHEEL +5 -0
  34. optimum_executorch-0.0.0.dev0.dist-info/licenses/LICENSE +201 -0
  35. optimum_executorch-0.0.0.dev0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,236 @@
1
+ # Copyright 2025 The HuggingFace Team. All rights reserved.
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
+ """Defines the command line for the export with ExecuTorch."""
16
+
17
+ from pathlib import Path
18
+ from typing import TYPE_CHECKING
19
+
20
+ from ...exporters import TasksManager
21
+ from ..base import BaseOptimumCLICommand, CommandInfo
22
+
23
+
24
+ if TYPE_CHECKING:
25
+ from argparse import ArgumentParser
26
+
27
+
28
+ def parse_args_executorch(parser):
29
+ required_group = parser.add_argument_group("Required arguments")
30
+ required_group.add_argument(
31
+ "-m",
32
+ "--model",
33
+ type=str,
34
+ required=True,
35
+ help="Model ID on huggingface.co or path on disk to load model from.",
36
+ )
37
+ required_group.add_argument(
38
+ "-o",
39
+ "--output_dir",
40
+ type=Path,
41
+ help="Path indicating the directory where to store the generated ExecuTorch model.",
42
+ )
43
+ required_group.add_argument(
44
+ "--task",
45
+ type=str,
46
+ default="text-generation",
47
+ help=(
48
+ "The task to export the model for. Available tasks depend on the model, but are among:"
49
+ f" {str(TasksManager.get_all_tasks())}."
50
+ ),
51
+ )
52
+ required_group.add_argument(
53
+ "--recipe",
54
+ type=str,
55
+ default="xnnpack",
56
+ help='Pre-defined recipes for export to ExecuTorch. Defaults to "xnnpack".',
57
+ )
58
+ required_group.add_argument(
59
+ "--use_custom_sdpa",
60
+ required=False,
61
+ action="store_true",
62
+ help="For decoder-only models to use custom sdpa with static kv cache to boost performance. Defaults to False.",
63
+ )
64
+ required_group.add_argument(
65
+ "--use_custom_kv_cache",
66
+ required=False,
67
+ action="store_true",
68
+ help="For decoder-only models to use custom kv cache for static cache that updates cache using custom op. Defaults to False.",
69
+ )
70
+ required_group.add_argument(
71
+ "--disable_dynamic_shapes",
72
+ required=False,
73
+ action="store_true",
74
+ help="When this flag is set on decoder-only models, dynamic shapes are disabled during export.",
75
+ )
76
+ required_group.add_argument(
77
+ "--qlinear",
78
+ type=str,
79
+ choices=["8da4w", "4w", "8w"],
80
+ required=False,
81
+ help=(
82
+ "Quantization config for decoder linear layers.\n\n"
83
+ "Options:\n"
84
+ " 8da4w - 8-bit dynamic activation, 4-bit weight\n"
85
+ " 4w - 4-bit weight only\n"
86
+ " 8w - 8-bit weight only"
87
+ ),
88
+ )
89
+ required_group.add_argument(
90
+ "--qlinear_group_size", type=int, required=False, help="Group size for decoder linear quantization."
91
+ )
92
+ required_group.add_argument(
93
+ "--qlinear_packing_format",
94
+ type=str,
95
+ choices=["tile_packed_to_4d"],
96
+ required=False,
97
+ help=(
98
+ "Packing format for decoder linear layers.\n"
99
+ "Only applicable to certain backends such as CUDA and Metal\n\n"
100
+ "Options:\n"
101
+ " tile_packed_to_4d - int4 4d packing format"
102
+ ),
103
+ )
104
+ required_group.add_argument(
105
+ "--qlinear_encoder",
106
+ type=str,
107
+ choices=["8da4w", "4w", "8w"],
108
+ required=False,
109
+ help=(
110
+ "Quantization config for encoder linear layers.\n\n"
111
+ "Options:\n"
112
+ " 8da4w - 8-bit dynamic activation, 4-bit weight\n"
113
+ " 4w - 4-bit weight only\n"
114
+ " 8w - 8-bit weight only"
115
+ ),
116
+ )
117
+ required_group.add_argument(
118
+ "--qlinear_encoder_group_size", type=int, required=False, help="Group size for encoder linear quantization."
119
+ )
120
+ required_group.add_argument(
121
+ "--qlinear_encoder_packing_format",
122
+ type=str,
123
+ choices=["tile_packed_to_4d"],
124
+ required=False,
125
+ help=(
126
+ "Packing format for encoder linear layers.\n"
127
+ "Only applicable to certain backends such as CUDA and Metal\n\n"
128
+ "Options:\n"
129
+ " tile_packed_to_4d - int4 4d packing format"
130
+ ),
131
+ )
132
+ required_group.add_argument(
133
+ "--qembedding",
134
+ type=str,
135
+ choices=["4w", "8w"],
136
+ required=False,
137
+ help=(
138
+ "Quantization config for embedding layer.\n\n"
139
+ "Options:\n"
140
+ " 4w - 4-bit weight only\n"
141
+ " 8w - 8-bit weight only"
142
+ ),
143
+ )
144
+ required_group.add_argument(
145
+ "--qembedding_group_size", type=int, required=False, help="Group size for embedding quantization."
146
+ )
147
+ required_group.add_argument(
148
+ "--max_seq_len",
149
+ type=int,
150
+ required=False,
151
+ help="Maximum sequence length for the model. If not specified, uses the model's default max_position_embeddings.",
152
+ )
153
+ required_group.add_argument(
154
+ "--dtype",
155
+ type=str,
156
+ choices=["float32", "float16", "bfloat16"],
157
+ required=False,
158
+ help="Data type for model weights. Options: float32, float16, bfloat16. Default: float32. For quantization (int8/int4), use the --qlinear arguments.",
159
+ )
160
+ required_group.add_argument(
161
+ "--device",
162
+ type=str,
163
+ choices=["cpu", "cuda", "cuda:0", "cuda:1", "cuda:2", "cuda:3"],
164
+ required=False,
165
+ help="Device to run the model on. Options: cpu, cuda. Default: cpu.",
166
+ )
167
+
168
+
169
+ class ExecuTorchExportCommand(BaseOptimumCLICommand):
170
+ COMMAND = CommandInfo(name="executorch", help="Export models to ExecuTorch.")
171
+
172
+ @staticmethod
173
+ def parse_args(parser: "ArgumentParser"):
174
+ return parse_args_executorch(parser)
175
+
176
+ def run(self):
177
+ from ...exporters.executorch import main_export
178
+
179
+ # Validate int4 packing format can only be used with CUDA devices and 4w quantization
180
+ device = getattr(self.args, "device", None)
181
+ qlinear_packing_format = getattr(self.args, "qlinear_packing_format", None)
182
+ if qlinear_packing_format:
183
+ if not device or not device.startswith("cuda"):
184
+ raise ValueError(
185
+ "--qlinear_packing_format can only be used when --device is set to CUDA (e.g., 'cuda', 'cuda:0', etc.)"
186
+ )
187
+ if not self.args.qlinear or self.args.qlinear != "4w":
188
+ raise ValueError("--qlinear_packing_format can only be used when --qlinear is set to '4w'")
189
+ qlinear_encoder_packing_format = getattr(self.args, "qlinear_encoder_packing_format", None)
190
+ if qlinear_encoder_packing_format:
191
+ if not device or not device.startswith("cuda"):
192
+ raise ValueError(
193
+ "--qlinear_encoder_packing_format can only be used when --device is set to CUDA (e.g., 'cuda', 'cuda:0', etc.)"
194
+ )
195
+ if not self.args.qlinear_encoder or self.args.qlinear_encoder != "4w":
196
+ raise ValueError(
197
+ "--qlinear_encoder_packing_format can only be used when --qlinear_encoder is set to '4w'"
198
+ )
199
+
200
+ kwargs = {}
201
+ if self.args.use_custom_sdpa:
202
+ kwargs["use_custom_sdpa"] = self.args.use_custom_sdpa
203
+ if self.args.use_custom_kv_cache:
204
+ kwargs["use_custom_kv_cache"] = self.args.use_custom_kv_cache
205
+ if self.args.disable_dynamic_shapes:
206
+ kwargs["disable_dynamic_shapes"] = self.args.disable_dynamic_shapes
207
+ if self.args.qlinear:
208
+ kwargs["qlinear"] = self.args.qlinear
209
+ if self.args.qlinear_group_size:
210
+ kwargs["qlinear_group_size"] = self.args.qlinear_group_size
211
+ if qlinear_packing_format:
212
+ kwargs["qlinear_packing_format"] = qlinear_packing_format
213
+ if self.args.qlinear_encoder:
214
+ kwargs["qlinear_encoder"] = self.args.qlinear_encoder
215
+ if self.args.qlinear_encoder_group_size:
216
+ kwargs["qlinear_encoder_group_size"] = self.args.qlinear_encoder_group_size
217
+ if qlinear_encoder_packing_format:
218
+ kwargs["qlinear_encoder_packing_format"] = qlinear_encoder_packing_format
219
+ if self.args.qembedding:
220
+ kwargs["qembedding"] = self.args.qembedding
221
+ if self.args.qembedding_group_size:
222
+ kwargs["qembedding_group_size"] = self.args.qembedding_group_size
223
+ if self.args.max_seq_len:
224
+ kwargs["max_seq_len"] = self.args.max_seq_len
225
+ if hasattr(self.args, "dtype") and self.args.dtype:
226
+ kwargs["dtype"] = self.args.dtype
227
+ if hasattr(self.args, "device") and self.args.device:
228
+ kwargs["device"] = self.args.device
229
+
230
+ main_export(
231
+ model_name_or_path=self.args.model,
232
+ task=self.args.task,
233
+ recipe=self.args.recipe,
234
+ output_dir=self.args.output_dir,
235
+ **kwargs,
236
+ )
@@ -0,0 +1,19 @@
1
+ # Copyright 2025 The HuggingFace Team. All rights reserved.
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
+ from ..export import ExportCommand
16
+ from ..export.executorch import ExecuTorchExportCommand
17
+
18
+
19
+ REGISTER_COMMANDS = [(ExecuTorchExportCommand, ExportCommand)]
@@ -0,0 +1,43 @@
1
+ # Copyright 2025 The HuggingFace Team. All rights reserved.
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
+ from typing import TYPE_CHECKING
16
+
17
+ from transformers.utils import _LazyModule
18
+
19
+
20
+ _import_structure = {
21
+ "modeling": [
22
+ "ExecuTorchModelForCausalLM",
23
+ "ExecuTorchModelForImageClassification",
24
+ "ExecuTorchModelForMaskedLM",
25
+ "ExecuTorchModelForSeq2SeqLM",
26
+ "ExecuTorchModelForSpeechSeq2Seq",
27
+ "ExecuTorchModelForMultiModalToText",
28
+ ],
29
+ }
30
+
31
+ if TYPE_CHECKING:
32
+ from .modeling import (
33
+ ExecuTorchModelForCausalLM,
34
+ ExecuTorchModelForImageClassification,
35
+ ExecuTorchModelForMaskedLM,
36
+ ExecuTorchModelForMultiModalToText,
37
+ ExecuTorchModelForSeq2SeqLM,
38
+ ExecuTorchModelForSpeechSeq2Seq,
39
+ )
40
+ else:
41
+ import sys
42
+
43
+ sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)