自定义大语言模型导出至ExecuTorch的手动流程
面向未被原生export_llm覆盖的PyTorch大语言模型,介绍手动导出并下沉到ExecuTorch的流程,涵盖静态图导出、KV缓存结构、后端委托与量化等关键环节,并以nanoGPT为例说明。
## 导出自定义 LLMS[#](<https://docs.pytorch.org/executorch/stable/llm/export-custom-llm.html#exporting-custom-llms>) 如果您有自己的 PyTorch 模型, 即 LLM , 此指南将显示如何手动导出并将其降为 ExecuTorch 。 如果您的模型没有被本地化覆盖, 请使用此流量 。 [`export_llm`](<https://docs.pytorch.org/executorch/stable/llm/export-llm.html>) API, 由不直接处理 [优化 ExecuTorch](<https://docs.pytorch.org/executorch/stable/llm/export-llm-optimum.html>),或需要模型特定变化才能使用标准的ExecuTorch LLM运行时间. 这个例子使用Karopathy的 [nanoGPT](<https://github.com/karpathy/nanoGPT>),这是GPT-2 124M的最小执行。同样的手动导出模式大致适用于PyTorch型号。但是,导出 a `.pte` 文件并运行该文件的股票LLM运行器是单独的步骤。LLM运行器,输出型号也必须遵循以下描述的运行时间合同. ## 导出到 ExecuTorch( 基本)[#](<https://docs.pytorch.org/executorch/stable/llm/export-custom-llm.html#exporting-to-executorch-basic>) 导出时采用PyTorch型号,将其转换为能够在消费设备上高效运行的格式. 对于这个例子,您需要nanoGPT模型和相应的代号词汇. 卷曲 ``` curl https://raw.githubusercontent.com/karpathy/nanoGPT/master/model.py -O curl https://huggingface.co/openai-community/gpt2/resolve/main/vocab.json -O ``` 微软 ``` wget https://raw.githubusercontent.com/karpathy/nanoGPT/master/model.py wget https://huggingface.co/openai-community/gpt2/resolve/main/vocab.json ``` 要将模型转换成一个为独立执行而优化的格式,有两个步骤。 首先,使用 PyTorch `export` 函数可以将 PyTorch 模型转换为中间,平台独立的中间代表。然后使用 ExecuTorch `to_edge` 财务报告和财务报告 `to_executorch` 用于为设备上执行准备模型的方法。这创造了一个.pte文件,可以在运行时由桌面或移动应用程序加载。 创建一个名为export_nanogpt.py的文件,其内容如下: ``` # export_nanogpt.py import torch from executorch.exir import EdgeCompileConfig, to_edge from torch.nn.attention import sdpa_kernel, SDPBackend from torch.export import export from model import GPT # Load the model. model = GPT.from_pretrained('gpt2') # Create example inputs. This is used in the export process to provide # hints on the expected shape of the model input. example_inputs = (torch.randint(0, 100, (1, model.config.block_size), dtype=torch.long), ) # Set up dynamic shape configuration. This allows the sizes of the input tensors # to differ from the sizes of the tensors in `example_inputs` during runtime, as # long as they adhere to the rules specified in the dynamic shape configuration. # Here we set the range of 0th model input's 1st dimension as # [0, model.config.block_size]. # See https://pytorch.org/executorch/main/concepts#dynamic-shapes # for details about creating dynamic shapes. dynamic_shape = ( {1: torch.export.Dim("token_dim", max=model.config.block_size)}, ) # Trace the model, converting it to a portable intermediate representation. # The torch.no_grad() call tells PyTorch to exclude training-specific logic. with torch.nn.attention.sdpa_kernel([SDPBackend.MATH]), torch.no_grad(): m = export(model, example_inputs, dynamic_shapes=dynamic_shape).module() traced_model = export(m, example_inputs, dynamic_shapes=dynamic_shape) # Convert the model into a runnable ExecuTorch program. edge_config = EdgeCompileConfig(_check_ir_validity=False) edge_manager = to_edge(traced_model, compile_config=edge_config) et_program = edge_manager.to_executorch() # Save the ExecuTorch program to a file. with open("nanogpt.pte", "wb") as file: file.write(et_program.buffer) ``` 要导出, 请用 `python export_nanogpt.py` (或蟒蛇3, 适合你的环境) 它会生成一个 `nanogpt.pte` 当前目录中的文件。 更多信息见: [导出到 ExecuTorch](<https://docs.pytorch.org/executorch/stable/tutorials/export-to-executorch-tutorial.html>) 财务报告和财务报告 [火炬. 导出](<https://pytorch.org/docs/stable/export.html>). ## 使用带有自定义模型的 LLM 跑车[#](<https://docs.pytorch.org/executorch/stable/llm/export-custom-llm.html#using-the-llm-runners-with-a-custom-model>) 输出 `.pte` 文件可直接通过 ExecuTorch 运行时间加载,但许多文本生成应用程序使用在其中描述的更高层次的 LLM 运行程序 [C++ 运行 LLMS](<https://docs.pytorch.org/executorch/stable/llm/run-with-c-plus-plus.html>)。这些导引器处理符号化、预填、解码、采样和流输出。要使用自定义模型,请围绕自动递归文本生成来塑造模型边界: KV缓存存储了先前的注意键和值载数,因此解码器可以附加新的令牌,而无需在全上下文中重新计算注意. - 该模型应接受代号ID作为主要输入. - 如果该模型使用KV缓存,它也应该接受一个位置输入,经常命名 `input_pos` 或 时 间 `start_pos`. - 该模型应返回一个能够由跑者取样的单一对数。 - 标致器文件和BOS / EOS 标致器ID应该与模型匹配. - 缓存计数器通常应当是模型自有的缓冲器,而不是通过跑车传递的额外投入和输出. 一个典型的与跑步相匹配的前向签名外观如下: ``` def forward( self, tokens: torch.Tensor, input_pos: torch.Tensor, ) -> torch.Tensor: ... return logits ``` 没有KV缓存的模型可能只暴露符号输入,但生成过程通常会慢得多,因为模型在每个解码步骤的完整上下文上重新计算注意力. 运行器还读取了来自 `.pte` 文件。至少包括描述序列限制的值、 KV 缓存行为、 以及指示器终止 : - `get_max_seq_len`: 由一个模型引用处理的代号的最大数量. - `get_max_context_len`:模型所记住的最大上下文长度. - `use_kv_cache`:该模型是否有内部的KV缓存. - `enable_dynamic_shape`: 预填是否可以使用动态序列长度. - `get_bos_id` 财务报告和财务报告 `get_eos_ids`: 跑者使用的代号. 例如: ``` metadata = { "get_bos_id": bos_id, "get_eos_ids": [eos_id], "get_max_seq_len": max_seq_len, "get_max_context_len": max_context_len, "use_kv_cache": True, "enable_dynamic_shape": True, } ``` 手动导出时, 将元数据序列化为恒定方法。 恒定方法在 `.pte` 文件, 运行者可在加载时间查询 : ``` edge_manager = to_edge( traced_model, constant_methods=metadata, compile_config=edge_config, ) ``` 如果您的模型需要额外的运行时间输入, 如明确的缓存显示器、 注意罩、 编码器输出, 或交叉注意状态, 默认文本 LLM 运行器很可能不是正确的边界 。 在这种情况下, 要么将模型包起来, 使这些值存储在模块中, 要么构建自定义的运行器 。 `IOManager` 用于特定模型输入和输出协议。 `IOManager` 是准备模型输入和处理模型输出以进行预填和解码的运行组件。 编码器-解码器模型,如Fairseq的翻译模型,是这种区分很重要的常见案例. ExecuTorch可以运行导出程序,但股票的文本生成导引方向是围绕解码器唯一的生成。如果该模型得到Optimum ExecuTorch的支持,则倾向于该路径。否则,决定是将模型包成与导出兼容的形状,还是曝光自定义的运行时间界面。 ## 调整注意力和 KV 缓存[#](<https://docs.pytorch.org/executorch/stable/llm/export-custom-llm.html#adapting-attention-and-kv-cache>) 优化的LLM输出在ExecuTorch中效果良好,当注意和解码状态的结构是有利于输出的时,重要的设计选择是在将可变解码状态移动到模块时保持运行时间接口的简单. ExecuTorch中优化的变压器执行保留了几个属性,这些属性对自定义模型中保存有用: - 导出的图表足够静态 `torch.export`: 百度操作是可追踪的,生成状态不依赖于Python-side突变. - 跑步边界仍然很小: 指示符和可选位置进入, 登录符出来, 元数据描述如何驱动生成 。 - KV缓存状态被存储在模型缓冲器中,并被加载器位置更新. - 注意被考虑进去,因此标准缩放点产品注意度(SDPA)可以在拉诺布局匹配时被优化或后端专用的SDPA执行器所取代. - 大型计算图案,如线性图层和注意力,仍可识别为后端分区器. 对于 KV 缓存支持 : - 将密钥和值缓存注册为模块缓冲器,从而成为导出程序状态的一部分. - 更新缓存条目时使用传递给模型的百度位置,而不是Python-side计数器或依赖数据的控制流量. - 保持缓存形状可以预测. 后端和自定义操作员经常依赖固定缓存布局假设. - 只返回对数。 默认的运行程序不会将缓存的对数作为模型输出 。 - 通过运行/运行时间生命周期重设或重新初始化缓存状态,而不是通过生成时改变 Python 属性. 请注意: - 优先标准 `torch.nn.functional.scaled_dot_product_attention` 或等效的模块边界,以后可以交换后端特定注意. - 保持查询,密钥,值,掩码,以及缓存形状清晰稳定. - 首先使模型可导出并正确,然后应用SDPA,缓存,量化,以及后端的转换,用于你所关心的目标. ExecuTorch 包括优化的 SDPA 和 Llama 导出流所用的缓存更新自定义操作器。 当您的模型的注意力布局与预期的查询/ 键/ 值/ 缓存常规相匹配时, 您可以使用这些路径 。 如果您的注意力布局不同, 通常最好先调整模块边界, 而不是强迫自定义操作器形成不兼容的形状 。 ## 重用 LLM 组件[#](<https://docs.pytorch.org/executorch/stable/llm/export-custom-llm.html#reusing-llm-components>) 您不需要复制 Llama 执行来构建一个自定义模式 。 `extension/llm` 树上包含在修改用于导出的模式时有用的可重复使用的棋子 : - [`extension/llm/modules`](<https://github.com/pytorch/executorch/tree/main/extension/llm/modules>) 包含便于出口的模块。 - [`KVCache`](<https://github.com/pytorch/executorch/blob/main/extension/llm/modules/kv_cache.py>) 提供从火炬调制改编的对出口友好的缓存执行。 - [`MultiHeadAttention`](<https://github.com/pytorch/executorch/blob/main/extension/llm/modules/attention.py>) SDPA出自关注模块,从而可以被优化的实现所取代. - [`examples/models/llama/source_transformation`](<https://github.com/pytorch/executorch/tree/main/examples/models/llama/source_transformation>) 显示自定义的SDPA,自定义的KV缓存中的Llama流互换,定量的KV缓存,以及后端特有的关注变体. 这些组件作为构件和引用执行最有用. 保持您的模型架构可读性,并首先接近最初的PyTorch版本,然后只有当单个部件改善了导出兼容性,跑步兼容性,或后端性能时才替换. 如果您正在从头开始编写或微调一个变压器模型,请也查看 [火炬调](<https://github.com/pytorch/torchtune>). 几个ExecuTorch LLM模块从火炬调制模块中改编,并有变化用于导出和推论. ## 后端代表团[#](<https://docs.pytorch.org/executorch/stable/llm/export-custom-llm.html#backend-delegation>) 虽然ExecuTorch为所有操作者提供便携式跨平台执行,但它也为一些不同的目标提供专门的后端,包括但不限于x86和ARM CPU通过XNNPACK后端加速,苹果加速通过核心ML后端,以及GPU通过Vulkan后端加速. 由于优化是特定后端的特异性,每个pte文件都是针对针对导出对象的后端的特异性. 为了支持多个设备,如Android的XNNPACK加速和iOS的Core ML,每个后端输出一个单独的PTE文件. 要在导出时将一个模型委托给特定的后端, ExecuTorch 使用 `to_edge_transform_and_lower()` 函数。此函数从 `torch.export` 和后端指定分区器对象。该分区器可识别可被目标后端优化的计算图部分内容。 `to_edge_transform_and_lower()`,导出的程序将被转换为边缘方言程序。然后,分配器将图表部分与后端兼容,以便加速和优化。任何未授权的图表部分都由 ExecuTorch 默认操作员执行。 要将输出的模型委托给特定的后端,我们需要导入其分区器以及边缘编译配置从 ExecuTorch 代码库中先调用 `to_edge_transform_and_lower`. 如果您还早些添加了运行元数据, 请通过该程序通过相同的元数据 。 `constant_methods` 在电话中论证,所以授权 `.pte` 保持相同的可移动值 。 对于自定义LLMS,后端性能取决于后端的模型图能识别和委托的多少. 在修改模型时记住以下内容: - 检查授权操作人员和非授权操作人员,在降低后 `get_delegation_info()`. - 偏好线性模式和注意力模式,它们将模型的权重作为常数显示给分配器. - 注意下放子图内部的动态形状,动态预填可以有用,但并不是每个动态模式都是后端友好的. - 导出分离 `.pte` 针对不同目标的文件,如XNNPACK用于CPU,核心ML用于苹果设备. 瞄准 XNNPACK 时,请使用 XNNPACK 分区器和定量流。详情请参见 [XNNPACK 后端概览](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-overview.html>), [XNNPACK 量化](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-quantization.html>),以及 [XNNPACK 排除故障](<https://docs.pytorch.org/executorch/stable/backends/xnnpack/xnnpack-troubleshooting.html>). 在瞄准核心ML时,遵循核心ML后端配置,并在目标苹果OS和硬件上验证. 状态化的KV缓存和引信化的SDPA支持可以依赖后端和OS版本. [核心 ML 后端概览](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-overview.html>), [核心 ML 分区器](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-partitioner.html>),以及 [核心 ML 故障排除](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-troubleshooting.html>). 以下是如何将nanoGPT授权给XNNPACK(如果你正在部署到一个Android电话上的话)的例子: ``` # export_nanogpt.py # Load partitioner for Xnnpack backend from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner # Model to be delegated to specific backend should use specific edge compile config from executorch.backends.xnnpack.utils.configs import get_xnnpack_edge_compile_config from executorch.exir import EdgeCompileConfig, to_edge_transform_and_lower import torch from torch.export import export from torch.nn.attention import sdpa_kernel, SDPBackend from torch.export import export from model import GPT # Load the nanoGPT model. model = GPT.from_pretrained('gpt2') # Create example inputs. This is used in the export process to provide # hints on the expected shape of the model input. example_inputs = ( torch.randint(0, 100, (1, model.config.block_size - 1), dtype=torch.long), ) # Set up dynamic shape configuration. This allows the sizes of the input tensors # to differ from the sizes of the tensors in `example_inputs` during runtime, as # long as they adhere to the rules specified in the dynamic shape configuration. # Here we set the range of 0th model input's 1st dimension as # [0, model.config.block_size]. # See ../concepts.html#dynamic-shapes # for details about creating dynamic shapes. dynamic_shape = ( {1: torch.export.Dim("token_dim", max=model.config.block_size - 1)}, ) # Trace the model, converting it to a portable intermediate representation. # The torch.no_grad() call tells PyTorch to exclude training-specific logic. with torch.nn.attention.sdpa_kernel([SDPBackend.MATH]), torch.no_grad(): m = export(model, example_inputs, dynamic_shapes=dynamic_shape).module() traced_model = export(m, example_inputs, dynamic_shapes=dynamic_shape) # Convert the model into a runnable ExecuTorch program. # To be further lowered to Xnnpack backend, `traced_model` needs xnnpack-specific edge compile config edge_config = get_xnnpack_edge_compile_config() # Converted to edge program and then delegate exported model to Xnnpack backend # by invoking `to` function with Xnnpack partitioner. edge_manager = to_edge_transform_and_lower(traced_model, partitioner = [XnnpackPartitioner()], compile_config = edge_config) et_program = edge_manager.to_executorch() # Save the Xnnpack-delegated ExecuTorch program to a file. with open("nanogpt.pte", "wb") as file: file.write(et_program.buffer) ``` ## 量化[#](<https://docs.pytorch.org/executorch/stable/llm/export-custom-llm.html#quantization>) 量子化是指一套使用较低精度类型运行计算和存储百分位数的技术. 相比于32 - 位浮点,使用8 - 位整数既能提供巨大的速度,又能减少内存的使用. 有很多方法可以对一个模型进行量子化,在所需的预处理量,使用的数据类型,以及对模型精度和性能的影响方面有所不同. 由于计算和内存在移动设备上受到很大限制,某种形式的量化对于将大型模型运至消费电子产品上是必要的,特别是,大型语言模型,如Llama2,可能需要将模型重量量化到4比特或更少. 杠杆化量化在导出前需要转换模型. PyTorch为此提供了pt2e(PyTorch 2 Export) API. 本例针对的是使用CPU加速,使用XNNPACK代表器,因此它需要使用XNNPACK专用的量化器. 瞄准不同的后端需要使用相应的定量器. 要使用 8 - bit 整数动态整数化 , 请使用 XNNPACK 代表, 调用 `prepare_pt2e`,通过使用具有代表性的输入来校准模型,然后调用 `convert_pt2e`。此选项将更新计算图,以便利用现有的量化运算符。 ``` # export_nanogpt.py from executorch.backends.transforms.duplicate_dynamic_quant_chain import ( DuplicateDynamicQuantChainPass, ) from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import ( get_symmetric_quantization_config, XNNPACKQuantizer, ) from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e ``` ``` # Use dynamic, per-channel quantization. xnnpack_quant_config = get_symmetric_quantization_config( is_per_channel=True, is_dynamic=True ) xnnpack_quantizer = XNNPACKQuantizer() xnnpack_quantizer.set_global(xnnpack_quant_config) m = export(model, example_inputs).module() # Annotate the model for quantization. This prepares the model for calibration. m = prepare_pt2e(m, xnnpack_quantizer) # Calibrate the model using representative inputs. This allows the quantization # logic to determine the expected range of values in each tensor. m(*example_inputs) # Perform the actual quantization. m = convert_pt2e(m, fold_quantize=False) DuplicateDynamicQuantChainPass()(m) traced_model = export(m, example_inputs) ``` 此外,增加或更新 `to_edge_transform_and_lower()` 调用 `XnnpackPartitioner`,这指示了ExecuTorch通过XNNPACK后端优化用于CPU执行的模型. ``` from executorch.backends.xnnpack.partition.xnnpack_partitioner import ( XnnpackPartitioner, ) ``` ``` edge_config = get_xnnpack_edge_compile_config() # Convert to edge dialect and lower to XNNPack. edge_manager = to_edge_transform_and_lower(traced_model, partitioner = [XnnpackPartitioner()], compile_config = edge_config) et_program = edge_manager.to_executorch() with open("nanogpt.pte", "wb") as file: file.write(et_program.buffer) ``` 更多信息见: [在 ExecuTorch 中的量化](<https://docs.pytorch.org/executorch/stable/quantization-overview.html>). ## 剖析和调试[#](<https://docs.pytorch.org/executorch/stable/llm/export-custom-llm.html#profiling-and-debugging>) 在调用一个模型后 `to_edge_transform_and_lower()`,您也许想知道什么是授权的,什么是没有。ExecuTorch提供了对授权进行洞察的实用方法。您可以利用这些信息在基本的计算和诊断潜在性能问题上获得可见度。模型作者可以使用这些信息来构建与目标后端兼容的模型。 ### 代表团形象化[#](<https://docs.pytorch.org/executorch/stable/llm/export-custom-llm.html#visualizing-the-delegation>) 那个... `get_delegation_info()` 方法提供了模型在以后发生的情况概要。 `to_edge_transform_and_lower()` 电话 : ``` from executorch.devtools.backend_debug import get_delegation_info from tabulate import tabulate # ... After call to to_edge_transform_and_lower(), but before to_executorch() graph_module = edge_manager.exported_program().graph_module delegation_info = get_delegation_info(graph_module) print(delegation_info.get_summary()) df = delegation_info.get_operator_delegation_dataframe() print(tabulate(df, headers="keys", tablefmt="fancy_grid")) ``` 对于针对XNNPACK后端的nanoGPT,你可能会看到以下内容(注意以下数字仅用于插图目的,实际值可能有所不同): 数字: ``` Total delegated subgraphs: 145 Number of delegated nodes: 350 Number of non-delegated nodes: 760 ``` | | op_type | # in_delegated_graphs | # in_non_delegated_graphs | | --- | --- | --- | --- | | 0 | aten__softmax_default | 12 | 0 | | 1 | aten_add_tensor | 37 | 0 | | 2 | aten_addmm_default | 48 | 0 | | 3 | aten_any_dim | 0 | 12 | | | … | | | | 25 | aten_view_copy_default | 96 | 122 | | | … | | | | 30 | 共计 | 350 | 760 | 从桌子上,操作员 `aten_view_copy_default` 在代表图表中显示96倍,在非授权图表中显示122倍。要查看更详细的视图,请使用 `format_delegated_graph()` 获取整个图表或使用格式化的打印字符串的方法 `print_delegated_graph()` 要直接打印 : ``` from executorch.exir.backend.utils import format_delegated_graph graph_module = edge_manager.exported_program().graph_module print(format_delegated_graph(graph_module)) ``` 这可能会为大型模型产生大量的输出。 考虑使用“ Control+F ” 或“Command+F ” 来定位您感兴趣的操作员( 如“ aten_view_copy_default ” ) 。 观察那些不低于下图的情况 。 在下文nanoGPT的输出片段中,观察到一个变压器模块被下放到XNNPACK,而操作员则不在. ``` %aten_where_self_22 : [num_users=1] = call_function[target=executorch.exir.dialects.edge._ops.aten.where.self](args = (%aten_logical_not_default_33, %scalar_tensor_23, %scalar_tensor_22), kwargs = {}) %lowered_module_144 : [num_users=1] = get_attr[target=lowered_module_144] backend_id: XnnpackBackend lowered graph(): %p_transformer_h_0_attn_c_attn_weight : [num_users=1] = placeholder[target=p_transformer_h_0_attn_c_attn_weight] %p_transformer_h_0_attn_c_attn_bias : [num_users=1] = placeholder[target=p_transformer_h_0_attn_c_attn_bias] %getitem : [num_users=1] = placeholder[target=getitem] %sym_size : [num_users=2] = placeholder[target=sym_size] %aten_view_copy_default : [num_users=1] = call_function[target=executorch.exir.dialects.edge._ops.aten.view_copy.default](args = (%getitem, [%sym_size, 768]), kwargs = {}) %aten_permute_copy_default : [num_users=1] = call_function[target=executorch.exir.dialects.edge._ops.aten.permute_copy.default](args = (%p_transformer_h_0_attn_c_attn_weight, [1, 0]), kwargs = {}) %aten_addmm_default : [num_users=1] = call_function[target=executorch.exir.dialects.edge._ops.aten.addmm.default](args = (%p_transformer_h_0_attn_c_attn_bias, %aten_view_copy_default, %aten_permute_copy_default), kwargs = {}) %aten_view_copy_default_1 : [num_users=1] = call_function[target=executorch.exir.dialects.edge._ops.aten.view_copy.default](args = (%aten_addmm_default, [1, %sym_size, 2304]), kwargs = {}) return [aten_view_copy_default_1] ``` ### 进一步的模型分析和调试[#](<https://docs.pytorch.org/executorch/stable/llm/export-custom-llm.html#further-model-analysis-and-debugging>) 通过 [ExecuTorch 的开发工具](<https://docs.pytorch.org/executorch/stable/llm/getting-started.html#performance-analysis>),用户可以配置模型执行,为模型中的每个操作员提供计时信息,进行模型数字调试等. ETRecord是导出时生成的包含将ExecuTorch程序与原PyTorch模型连接的模型图表和源级元数据,你可以在没有ETRecord的情况下查看所有剖面事件,尽管有ETRecord,但也可以将每个事件与被执行的运算符类型,模块层次,以及原PyTorch源代码的堆积痕迹联系起来.更多信息见: [ETRecord 文档](<https://docs.pytorch.org/executorch/stable/etrecord.html>). 在您的导出脚本中, 拨打后 `to_edge()` 财务报告和财务报告 `to_executorch()`电话 `generate_etrecord()` 与 `EdgeProgramManager` 从 `to_edge()` 页:1 `ExecuTorchProgramManager` 从 `to_executorch()`。确保复制 `EdgeProgramManager`,作为呼吁 `to_edge_transform_and_lower()` 变形图在位. ``` # export_nanogpt.py import copy from executorch.devtools import generate_etrecord # Make the deep copy immediately after to to_edge() edge_manager_copy = copy.deepcopy(edge_manager) # ... # Generate ETRecord right after to_executorch() etrecord_path = "etrecord.bin" generate_etrecord(etrecord_path, edge_manager_copy, et_program) ``` 运行导出脚本和 ETRecord 将生成为 `etrecord.bin`. 欲进一步了解 ExecuTorch 的开发者工具,请参见: [ExecuTorch 开发工具介绍](<https://docs.pytorch.org/executorch/stable/devtools-overview.html>).
查看原始来源