把模型委托给后端执行
面向机器学习工程师介绍将PyTorch程序委托给后端执行的流程,涵盖整体模块下沉与运行时注册两个环节,并说明注册方式取决于委托实现。
# 将模型降为代表 观众:ML工程师,他们有兴趣在运行时间里应用代表来加速他们的节目. 后端授权是后端处理和执行PyTorch的切入点 利用专门技术的绩效和效率效益的方案 后端和硬件,同时仍然为 PyTorch 用户提供体验 接近 PyTorch 运行时间 。 后端代表通常由 ExecuTorch 或 卖家 。 如何在您的节目中利用授权, 是通过标准切入点 。 `to_backend`. ## 前端接口 将程序下放到后端有三种流程: 1. 将整个模块降为后端。 这对测试后端和 预处理阶段。 1. 将整个模块降为后端, 并用另一个模块进行编译。 this 用于重用从其他流量导出的下调模块。 1. 模块的下部按照分区器显示。这对 降低包括可降低和不可降低节点的模型,并且 最精细的螺旋桨。 ### Flow 1: 降低整个模块 此流起自带有Edge Dialect 表示的可追溯图表模块。 下,我们称以下函数为返回 a `LoweredBackendModule` (关于此功能的更多文档可见于 [导出 API 引用](<https://github.com/pytorch/executorch/blob/HEAD/docs/source/export-to-executorch-api-reference.rst>)) ```python # defined in backend_api.py def to_backend( backend_id: str, edge_program: ExportedProgram, compile_spec: List[CompileSpec], ) -> LoweredBackendModule: ``` 在此函数范围内,后端的 `preprocess()` 函数称为 生成一个编译的blob,该blob将释放到平缓缓冲器二进制中。 降低的模块可直接捕获,或放回母模块中 最终被俘的模块在平缓器的模型中被序列化 可以在运行时间中加载。 以下是这种流动的例子: ```python from executorch.exir.backend.backend_api import to_backend import executorch.exir as exir import torch from torch.export import export from executorch.exir import to_edge # The submodule runs in a specific backend. In this example, `BackendWithCompilerDemo` backend class LowerableSubModel(torch.nn.Module): def __init__(self): super().__init__() def forward(self, x): return torch.sin(x) # Convert the lowerable module to Edge IR Representation to_be_lowered = LowerableSubModel() example_input = (torch.ones(1), ) to_be_lowered_exir_submodule = to_edge(export(to_be_lowered, example_input)) # Import the backend implementation from executorch.exir.backend.test.backend_with_compiler_demo import ( BackendWithCompilerDemo, ) lowered_module = to_backend('BackendWithCompilerDemo', to_be_lowered_exir_submodule.exported_program(), []) ``` 我们可以通过直接运行来将程序序列化为平缓冲器格式: ```python # Save the flatbuffer to a local file save_path = "delegate.pte" with open(save_path, "wb") as f: f.write(lowered_module.buffer()) ``` ### Flow 2: 降低整个模块和复合材料 或者,在流过1之后,我们可以与另一个低调的模块组成 模块 : ```python # This submodule runs in executor runtime class NonLowerableSubModel(torch.nn.Module): def __init__(self, bias): super().__init__() self.bias = bias def forward(self, a, b): return torch.add(torch.add(a, b), self.bias) # The composite module, including lower part and non-lowerpart class CompositeModel(torch.nn.Module): def __init__(self): super().__init__() self.non_lowerable = NonLowerableSubModel(torch.ones(1) * 0.3) self.lowerable = lowered_module def forward(self, x): a = self.lowerable(x) b = self.lowerable(a) ret = self.non_lowerable(a, b) return a, b, ret composite_model = CompositeModel() model_inputs = (torch.ones(1), ) exec_prog = to_edge(export(composite_model, model_inputs)).to_executorch() # Save the flatbuffer to a local file save_path = "delegate.pte" with open(save_path, "wb") as f: f.write(exec_prog.buffer) ``` ### 流程 3 : 分区 第三个流也从带有Edge Dialect的跟踪图模块开始 表示式。要降低此图表模块中的某些节点,我们可以使用 超载 [`to_backend` 函数](https://github.com/pytorch/executorch/blob/d9eef24bb720804aa7b400b05241487510ae0dc2/exir/backend/backend_api.py#L39). ```python def to_backend( edge_program: ExportedProgram, partitioner: Partitioner, ) -> ExportedProgram: ``` 此函数在 `Partitioner` 它会在所有节点上添加标签 将返回 a `partition_tags` 字典映射标记为 后端名称和模块编译规格。标记的节点将会是 使用 Flow 1 进程分割并降低到其映射的后端 。 可用辅助器分区器被记录 [这儿](<https://github.com/pytorch/executorch/blob/HEAD/docs/source/compiler-custom-compiler-passes.md>)。这些降低的模块 将插入顶级模块并进行序列化。 以下是流经的例子: ```python import executorch.exir as exir from executorch.exir.backend.backend_api import to_backend from executorch.exir.backend.test.op_partitioner_demo import AddMulPartitionerDemo from executorch.exir.program import ( EdgeProgramManager, to_edge, ) from torch.export import export import torch class Model(torch.nn.Module): def __init__(self): super().__init__() def forward(self, x, y): x = x + y x = x * y x = x - y x = x / y x = x * y x = x + y return x model = Model() model_inputs = (torch.randn(1, 3), torch.randn(1, 3)) core_aten_ep = export(model, model_inputs) edge: EdgeProgramManager = to_edge(core_aten_ep) edge = edge.to_backend(AddMulPartitionerDemo()) exec_prog = edge.to_executorch() # Save the flatbuffer to a local file save_path = "delegate.pte" with open(save_path, "wb") as f: f.write(exec_prog.buffer) ``` ## 运行时间 与代表们有程序后,用后端运行模型,需要注册后端. 根据委托执行情况,后端可以作为全球变量的一部分注册,或 明确在主函数内注册。 - 如果在全局变量初始化时注册,则后端只要静态链接即可注册,用户只需将库作为依赖的一部分. - 如果供应商提供API来注册后端,用户需要将库作为依赖的一部分,并呼叫供应商提供的API明确注册后端作为主要功能的一部分.
查看原始来源