.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 十进制数字。该头包含扁平缓冲区编码的核心程序数据的大小,以及可能跟随在程序数据之后的段的起始偏移量。请注意,此头是 ExecuTorch 特有的,但即使存在,也不会影响大多数扁平缓冲区解析代码(除了很少使用的 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.
Example:
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 架构。
扁平缓冲区编码的程序数据紧随头部之后。通过在扩展头中嵌入该区域的大小,客户端可以仅读取程序数据而无需读取段数据。这非常有用,因为程序数据通常在整个模型生命周期内保持不变,而较大的段数据往往在模型初始化后即可释放。
分段数据¶
第一个段起始于扩展头中嵌入的偏移量。
段通常对齐到 4096 或与目标系统内存页大小匹配的 2 的某个其他幂次方。这使得在需要时更容易使用 mmap()。
程序数据中的Program.segments数组包含关于可选跟随的分段的大小/偏移信息。此数组中的偏移量是相对于扩展头中的分段偏移量的。