.pte
文件格式¶
ExecuTorch .pte
程序文件序列化为修改后的二进制 FlatBuffer 文件,并附加可选的数据段。
┌───────────────────────────────────┐
│Standard flatbuffer header │
├───────────────────────────────────┤
Optional ──> │ExecuTorch extended header │
├───────────────────────────────────┤
│Flatbuffer-serialized program data │
│ │
│ │
┌─ ├───────────────────────────────────┤
│ │Padding │
│ ├───────────────────────────────────┤
│ │Segment data │
│ │ │
│ │ │
│ ├───────────────────────────────────┤
│ │Padding │
Optional ─┤ ├───────────────────────────────────┤
│ │Segment data │
│ │ │
│ │ │
│ ├───────────────────────────────────┤
│ │Padding │
│ ├───────────────────────────────────┤
│ │... │
└─ └───────────────────────────────────┘
标头¶
程序文件可以通过字节偏移量 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)
程序数据¶
有关 Program FlatBuffer 模式,请参阅 //executorch/schema/program.fbs
。
FlatBuffer 编码的程序数据紧跟在标头之后。通过将此区域的大小嵌入到扩展标头中,客户端可以仅读取程序数据,而无需读取段数据。这很有用,因为程序数据通常会在模型的生命周期内保留,而大型段数据通常在模型初始化后可以释放。
段数据¶
第一个段从扩展标头中嵌入的偏移量开始。段通常与 4096 或其他与目标系统的内存页面大小匹配的 2 的幂对齐。这使得在需要时更容易使用 mmap()
。
程序数据中的 Program.segments
数组包含有关可选地跟随的段的大小/偏移量信息。此数组中的偏移量相对于扩展标头中的段偏移量。