Returns the runtime binary representation of the given Program. Args: pte_file: PTEFile class containing the program and segments. extract_delegate_segments: Whether to move delegate data blobs from the Program into separate segments, rather than encoding those blobs
(
pte_file: PTEFile,
*,
extract_delegate_segments: bool = False,
segment_alignment: int = 128,
constant_tensor_alignment: Optional[int] = None,
delegate_alignment: Optional[int] = None,
)
| 420 | |
| 421 | |
| 422 | def serialize_pte_binary( |
| 423 | pte_file: PTEFile, |
| 424 | *, |
| 425 | extract_delegate_segments: bool = False, |
| 426 | segment_alignment: int = 128, |
| 427 | constant_tensor_alignment: Optional[int] = None, |
| 428 | delegate_alignment: Optional[int] = None, |
| 429 | ) -> Cord: |
| 430 | """Returns the runtime binary representation of the given Program. |
| 431 | |
| 432 | Args: |
| 433 | pte_file: PTEFile class containing the program and segments. |
| 434 | extract_delegate_segments: Whether to move delegate data blobs from the |
| 435 | Program into separate segments, rather than encoding those blobs |
| 436 | in the flatbuffer data. When true, will also: |
| 437 | - Add an extended header to the output, containing the program size |
| 438 | and the starting segment offset. |
| 439 | - Update the Program.segments field with the offsets and lengths |
| 440 | of each segment. |
| 441 | segment_alignment: Alignment in bytes. The starting offset of each |
| 442 | segment will be aligned to this value in the output data. |
| 443 | constant_tensor_alignment: The minimum alignment of tensor |
| 444 | buffers in the program. Must be a power of 2. Defaults to ALIGNMENT. |
| 445 | delegate_alignment: If provided, the minimum alignment of delegate data |
| 446 | in the program. Must be a power of 2. If not provided, uses the |
| 447 | value in the schema file. |
| 448 | Returns: |
| 449 | The serialized form of the Program, ready for execution by the runtime. |
| 450 | """ |
| 451 | # Default tensor alignment. |
| 452 | if constant_tensor_alignment is None: |
| 453 | constant_tensor_alignment = ALIGNMENT |
| 454 | |
| 455 | # Don't modify the original program. |
| 456 | # TODO(T144120904): Could avoid yet more huge copies with a more shallow |
| 457 | # copy, reusing the actual data blobs. |
| 458 | program = copy.deepcopy(pte_file.program) |
| 459 | |
| 460 | # Store extracted segment data, with any buffer-specific alignment. |
| 461 | # This may be constant data, delegate data or named data. |
| 462 | segments: List[AlignedData] = [] |
| 463 | |
| 464 | constant_segment_data, constant_segment_offsets = _extract_constant_segment( |
| 465 | program.constant_buffer, tensor_alignment=constant_tensor_alignment |
| 466 | ) |
| 467 | |
| 468 | # If there are no constants, len(constant_segment_data) = 0. However, there may |
| 469 | # be non-constants, in which case len(constant_segment_offsets) = 1, containing |
| 470 | # the placeholder value 0. Ensure the placeholder value is put into |
| 471 | # program.constant_segment.offsets. |
| 472 | if len(constant_segment_offsets) > 0: |
| 473 | # Update program.constant_segment with constant subsegment offset information. |
| 474 | program.constant_segment = SubsegmentOffsets( |
| 475 | segment_index=len(segments), offsets=constant_segment_offsets |
| 476 | ) |
| 477 | # Clear the constant buffer, as constant data will be stored in segments. |
| 478 | program.constant_buffer = [] |
| 479 | # Add to the aggregate segments cord. |