快捷方式

.pte 文件格式

ExecuTorch .pte 程序文件被序列化为修改过的二进制 flatbuffer 文件,并可选择附加数据段。

             ┌───────────────────────────────────┐
             │Standard flatbuffer header         │
             ├───────────────────────────────────┤
Optional ──> │ExecuTorch extended header         │
             ├───────────────────────────────────┤
             │Flatbuffer-serialized program data │
             │                                   │
             │                                   │
          ┌─ ├───────────────────────────────────┤
          │  │Padding                            │
          │  ├───────────────────────────────────┤
          │  │Segment data                       │
          │  │                                   │
          │  │                                   │
          │  ├───────────────────────────────────┤
          │  │Padding                            │
Optional ─┤  ├───────────────────────────────────┤
          │  │Segment data                       │
          │  │                                   │
          │  │                                   │
          │  ├───────────────────────────────────┤
          │  │Padding                            │
          │  ├───────────────────────────────────┤
          │  │...                                │
          └─ └───────────────────────────────────┘

兼容性

请参阅 Runtime Compatibility Policy,了解有关 .pte 格式与 ExecuTorch 运行时之间兼容性保证的详细信息。

头部

程序文件可以通过字节偏移量 4 处的魔术字符串识别,该字符串以 ET 开头,后跟两个 ASCII 十进制数字。

程序文件可以在字节偏移量 8 处有一个可选的扩展头部,该头部由以 eh 开头并后跟两个 ASCII 十进制数字的魔术字符串识别。此头部包含 flatbuffer 编码的核心程序数据的大小,以及可能紧随程序数据之后的段的起始偏移量。请注意,此头部是 ExecuTorch 特有的,但即使存在,它也不会扰乱大多数 flatbuffer 解析代码(除了极少使用的 GetBufferStartFromRootPointer())。

所有数字均为小端序,无论主机系统如何。

头部布局

[0..3] uint32_t byte offset to the beginning of the flatbuffer root table.
[4..7] File magic bytes: "ET" followed by two ASCII decimal digits. The digits
       will change if the binary format of this file is changed in a
       non-backwards-compatible way.
Optional extended header:
|  [8..11] Extended header magic bytes: "eh" followed by two ASCII decimal
|          digits. The digits will change if the binary format of this header is
|          changed in a non-backwards-compatible way.
| [12..15] uint32_t size of this extended header in bytes, including the magic
|          header and this size field. Fields can be added to this header in
|          the future by increasing this size. This size does not include any
|          padding that may follow the header.
| [16..23] uint64_t size of the flatbuffer-encoded program data, starting from
|          byte offset zero above. I.e., it includes these headers.
| [24..31] uint64_t offset (from byte offset zero above) to the start of the
|          first segment, or zero if there are no segments.
|  [31..?] Any zero-padding necessary to preserve the alignment of the data
|          that follows.
End of optional extended header.

示例

        Offset to flatbuffer root (0x38)
        |            File magic ("ET??")
        |            |            Extended header magic ("eh??")
        |            |            |            Extended header size (0x18)
        vvvvvvvvvvv  vvvvvvvvvvv  vvvvvvvvvvv  vvvvvvvvvvv
0x0000  38 00 00 00  45 54 3F 3F  65 68 3F 3F  18 00 00 00
0x0010  F0 02 00 00  00 00 00 00  00 10 00 00  00 00 00 00
        ^^^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^
        |                         Offset to segments (0x1000)
        Size of program flatbuffer data (0x2f0)

程序数据

请参阅 //executorch/schema/program.fbs 了解 Program flatbuffer 模式。

flatbuffer 编码的程序数据紧随头部。通过将此区域的大小嵌入扩展头部,客户端可以只读取程序数据而无需读取段数据。这很有用,因为程序数据通常会在模型的生命周期内保留,而较大的段数据通常在模型初始化后即可释放。

段数据

第一个段从扩展头部中嵌入的偏移量处开始。段通常对齐到 4096 或其他与目标系统内存页面大小匹配的 2 的幂次方。如果需要,这使得使用 mmap() 更加容易。

程序数据中的 Program.segments 数组包含可选的后续段的大小/偏移信息。此数组中的偏移量是相对于扩展头部中的段偏移量而言的。

文档

查阅 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获取问题解答

查看资源