Args: exported_program: Exported Program to serialize
(
self, exported_program: ep.ExportedProgram
)
| 310 | |
| 311 | class ExportedProgramSerializer(export_serialize.ExportedProgramSerializer): |
| 312 | def serialize( |
| 313 | self, exported_program: ep.ExportedProgram |
| 314 | ) -> export_serialize._SerializedProgram: |
| 315 | """ |
| 316 | Args: |
| 317 | exported_program: Exported Program to serialize |
| 318 | """ |
| 319 | |
| 320 | assert isinstance(exported_program, ep.ExportedProgram) |
| 321 | |
| 322 | gm_serializer = GraphModuleSerializer( |
| 323 | exported_program.graph_signature, exported_program.module_call_graph |
| 324 | ) |
| 325 | serialized_graph_module = gm_serializer.serialize(exported_program.graph_module) |
| 326 | serialized_range_constraints = export_serialize.serialize_range_constraints( |
| 327 | exported_program.range_constraints |
| 328 | ) |
| 329 | |
| 330 | # TODO: Directly serialize exported_program.constants once |
| 331 | # CustomClassHolders get stored in the ExportedProgram rather than in |
| 332 | # the graph |
| 333 | constants = {} |
| 334 | for n, c in gm_serializer.custom_objs.items(): |
| 335 | constants[n] = c |
| 336 | for n, t in exported_program.constants.items(): |
| 337 | assert n not in constants |
| 338 | constants[n] = t |
| 339 | |
| 340 | additional_kwargs = {} |
| 341 | if hasattr(exported_program, "verifiers"): |
| 342 | additional_kwargs["verifiers"] = [ |
| 343 | v.dialect for v in exported_program.verifiers |
| 344 | ] |
| 345 | elif hasattr(exported_program, "dialect"): |
| 346 | additional_kwargs["dialect"] = exported_program.dialect |
| 347 | serialized_ep = schema.ExportedProgram( |
| 348 | graph_module=serialized_graph_module, |
| 349 | opset_version=self.opset_version, |
| 350 | range_constraints=serialized_range_constraints, |
| 351 | schema_version=SchemaVersion( |
| 352 | major=SCHEMA_VERSION[0], |
| 353 | minor=SCHEMA_VERSION[1], |
| 354 | ), |
| 355 | **additional_kwargs, |
| 356 | ) |
| 357 | |
| 358 | # Test canonical form is well defined. |
| 359 | # TODO : Doesn't pass currently on executorch graphs with alloc nodes. |
| 360 | # canonicalize(serialized_ep) |
| 361 | |
| 362 | if exported_program.example_inputs is not None: |
| 363 | example_inputs = export_serialize.serialize_torch_artifact( |
| 364 | exported_program.example_inputs |
| 365 | ) |
| 366 | else: |
| 367 | example_inputs = b"" |
| 368 | |
| 369 | return export_serialize._SerializedProgram( |
no test coverage detected