Serialize the output from Emitter into ExecuTorch artifacts; PTE and PTD files.
(
emitter_output: EmitterOutput,
config: ExecutorchBackendConfig,
data_serializer: DataSerializer,
named_data_store: Optional[NamedDataStoreOutput] = None,
)
| 42 | |
| 43 | |
| 44 | def serialize_for_executorch( |
| 45 | emitter_output: EmitterOutput, |
| 46 | config: ExecutorchBackendConfig, |
| 47 | data_serializer: DataSerializer, |
| 48 | named_data_store: Optional[NamedDataStoreOutput] = None, |
| 49 | ) -> Tuple[Cord, Dict[str, Cord]]: |
| 50 | """Serialize the output from Emitter into ExecuTorch artifacts; PTE and PTD files.""" |
| 51 | |
| 52 | # Serialize PTE file. |
| 53 | pte_named_data = None |
| 54 | if ( |
| 55 | named_data_store is not None |
| 56 | and len(named_data_store.buffers) > 0 |
| 57 | and len(named_data_store.pte_data) > 0 |
| 58 | ): |
| 59 | # Create a separate NamedDataStoreOutput with only pte_data; exclude |
| 60 | # external_data, which shouldn't be serialized with the PTE file. |
| 61 | if len(named_data_store.external_data) == 0: |
| 62 | pte_named_data = named_data_store |
| 63 | else: |
| 64 | pte_named_data = NamedDataStoreOutput( |
| 65 | buffers=named_data_store.buffers, |
| 66 | pte_data=named_data_store.pte_data, |
| 67 | external_data={}, |
| 68 | ) |
| 69 | pte: Cord = serialize_pte_binary( |
| 70 | pte_file=PTEFile( |
| 71 | program=emitter_output.program, |
| 72 | mutable_data=emitter_output.mutable_data, |
| 73 | named_data=pte_named_data, |
| 74 | ), |
| 75 | extract_delegate_segments=config.extract_delegate_segments, |
| 76 | segment_alignment=config.segment_alignment, |
| 77 | constant_tensor_alignment=config.constant_tensor_alignment, |
| 78 | delegate_alignment=config.delegate_alignment, |
| 79 | ) |
| 80 | |
| 81 | # Early exit if no external weights. |
| 82 | if len(emitter_output.external_constant_map) == 0 and ( |
| 83 | named_data_store is None or len(named_data_store.external_data) == 0 |
| 84 | ): |
| 85 | return pte, {} |
| 86 | |
| 87 | ptd_files: Dict[str, Cord] = {} |
| 88 | |
| 89 | # If there are no emitter constants, use named_data_store directly. |
| 90 | if len(emitter_output.external_constant_map) == 0: |
| 91 | for tag in named_data_store.external_data.keys(): |
| 92 | ptd_files[tag] = data_serializer.serialize( |
| 93 | DataPayload( |
| 94 | buffers=named_data_store.buffers, |
| 95 | named_data=named_data_store.external_data[tag], |
| 96 | ) |
| 97 | ) |
| 98 | return pte, ptd_files |
| 99 | |
| 100 | # Collect external weights from emitter output and merge them. |
| 101 | fqn_to_tensor_layout = _extract_external_tensor_layouts(emitter_output.program) |