Buckets:

mishig's picture
|
download
raw
4.33 kB
# ExecuTorch
[`ExecuTorch`](https://github.com/pytorch/executorch) 是一种端到端解决方案,旨在为包括可穿戴设备、嵌入式设备和微控制器在内的移动及边缘设备提供端侧推理能力。它属于 PyTorch 生态系统的一部分,支持部署 PyTorch 模型,并专注于可移植性、生产力和性能。
ExecuTorch 提供了明确定义的入口点,用于执行针对特定模型、设备和/或使用场景的优化,例如后端委托、用户自定义的编译器转换、内存规划等。使用 ExecuTorch 在边缘设备上运行 PyTorch 模型的第一步是导出模型。这通过名为 [`torch.export`](https://pytorch.org/docs/stable/export.html) 的 PyTorch API 来实现。
## ExecuTorch 集成[[transformers.TorchExportableModuleWithStaticCache]]
正在开发一个集成点,以确保 🤗 Transformers 能够通过 `torch.export` 进行导出。该集成的目标不仅是实现导出,还要确保导出的产物能够进一步降低和优化,从而在 `ExecuTorch` 中高效运行,特别是针对移动和边缘用例。
#### transformers.TorchExportableModuleWithStaticCache[[transformers.TorchExportableModuleWithStaticCache]]
```python
transformers.TorchExportableModuleWithStaticCache(model: PreTrainedModel, batch_size: int | None = None, max_cache_len: int | None = None, device: typing.Optional[torch.device] = None)
```
[来源](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/executorch.py#L445)
一个配方模块,旨在使 `PreTrainedModel` 能够通过 `torch.export` 进行导出,特别是针对仅解码器语言模型到 `StaticCache` 的导出。该模块确保导出的模型兼容 `ExecuTorch` 中的进一步降低和运行。
注意:此类专门设计用于支持使用 `torch.export` 的导出流程,以确保模型能够进一步降低并高效地在 `ExecuTorch` 中运行。
#### forward[[transformers.TorchExportableModuleWithStaticCache.forward]]
```python
forward(input_ids: typing.Optional[torch.LongTensor] = None, inputs_embeds: typing.Optional[torch.Tensor] = None, cache_position: typing.Optional[torch.Tensor] = None)
```
[来源](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/executorch.py#L540)
**参数:**
input_ids (`torch.Tensor`) : 表示输入到该模块的当前输入 token id 的张量。
inputs_embeds (`torch.Tensor`) : 表示当前输入到该模块的嵌入的张量。
cache_position (`torch.Tensor`) : 表示缓存中当前输入位置的张量。
**返回:** `torch.Tensor`
模型输出的 Logits。
该模块的前向传播,与 ExecuTorch 运行时兼容。
此正向适配器有两个主要用途:
1. **使模型兼容 `torch.export`**:该适配器会将 `Cache` 等不受支持的对象从图的输入和输出中隐藏起来,从而确保模型能够使用 `torch.export` 顺利导出,而不会遇到问题。
2. **确保与 `ExecuTorch` 运行时兼容**:该适配器将模型的前向签名与 `executorch/extension/llm/runner` 中的签名进行匹配,从而确保导出的模型可以直接在 `ExecuTorch` 中运行。
#### transformers.convert_and_export_with_cache[[transformers.convert_and_export_with_cache]]
```python
transformers.convert_and_export_with_cache(model: PreTrainedModel, example_input_ids: typing.Optional[torch.Tensor] = None, example_cache_position: typing.Optional[torch.Tensor] = None, dynamic_shapes: dict | None = None, strict: bool | None = None)
```
[来源](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/executorch.py#L757)
**参数:**
model (`PreTrainedModel`):要导出的预训练模型。
example_input_ids (`Optional[torch.Tensor]`) : `torch.export` 使用的示例输入 token id。
example_cache_position (`Optional[torch.Tensor]`) : `torch.export` 使用的当前缓存位置示例。
dynamic_shapes(`Optional[dict]`):`torch.export` 使用的动态形状。
strict(`Optional[bool]`) : 指示 `torch.export` 使用 `dynamo` 的标志。
**返回值:** 导出的程序(`torch.export.ExportedProgram`)
通过 `torch.export` 生成的导出程序。
将 `PreTrainedModel` 转换为可导出的模块,并使用 `torch.export` 进行导出,确保导出的模型与 `ExecuTorch` 兼容。

Xet Storage Details

Size:
4.33 kB
·
Xet hash:
e9fc76538dce4f18948d24561741bf8695b9e034185c7311b7704509ac4437d1

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.