将模型作为委托进行降低¶
受众:对应用委托来加速运行时程序的 ML 工程师。
后端委托是后端处理和执行 PyTorch 程序的入口点,旨在利用专用后端和硬件的性能和效率优势,同时仍为 PyTorch 用户提供接近 PyTorch 运行时的体验。后端委托通常由 ExecuTorch 或供应商提供。在程序中利用委托的方法是通过标准入口点 to_backend
。
前端接口¶
将程序委托给后端有三种流程
将整个模块降低到后端。这适用于测试后端和预处理阶段。
将整个模块降低到后端,并将其与另一个模块组合。这适用于重用从其他流程导出的降低模块。
根据分区器降低模块的各个部分。这适用于降低既包含可降低节点又包含不可降低节点的模型,并且是最简化的流程。
流程 1:降低整个模块¶
此流程从具有 Edge Dialect 表示的跟踪图模块开始。为了降低它,我们调用以下函数,该函数返回 LoweredBackendModule
(有关此函数的更多文档可以在导出 API 参考中找到)
# defined in backend_api.py
def to_backend(
backend_id: str,
edge_program: ExportedProgram,
compile_spec: List[CompileSpec],
) -> LoweredBackendModule:
在此函数中,将调用后端的 preprocess()
函数,该函数生成编译后的 Blob,该 Blob 将被发送到 FlatBuffer 二进制文件。降低的模块可以直接捕获,也可以放回父模块中进行捕获。最终,捕获的模块将在 FlatBuffer 的模型中序列化,该模型可以由运行时加载。
以下是此流程的示例
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(), [])
我们可以通过直接运行以下命令将程序序列化为 FlatBuffer 格式
# Save the flatbuffer to a local file
save_path = "delegate.pte"
with open(save_path, "wb") as f:
f.write(lowered_module.buffer())
流程 2:降低整个模块并进行组合¶
或者,在流程 1 之后,我们可以将此降低的模块与另一个模块组合
# 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
函数。
def to_backend(
edge_program: ExportedProgram,
partitioner: Partitioner,
) -> ExportedProgram:
此函数接受一个 Partitioner
,它将标签添加到所有要降低的节点。它将返回一个 partition_tags
字典,该字典将标签映射到后端名称和模块编译规范。然后,将使用流程 1 的过程对标记的节点进行分区并降低到其映射的后端。可用的助手分区器记录在此处。这些降低的模块将插入到顶层模块中并进行序列化。
以下是流程的示例
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 以显式注册后端作为主函数的一部分。