.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
数组包含有关可选跟随段的大小/偏移量信息。此数组中的偏移量是相对于扩展头部中段的偏移量。