|
24 | 24 | ETRecordReservedFileNames, |
25 | 25 | ) |
26 | 26 | from executorch.exir import EdgeCompileConfig, EdgeProgramManager, to_edge |
| 27 | +from executorch.exir.program._program import to_edge_transform_and_lower |
27 | 28 | from torch.export import export |
28 | 29 |
|
29 | 30 |
|
@@ -275,6 +276,167 @@ def test_etrecord_generation_with_exported_program(self): |
275 | 276 | # Validate that export_graph_id matches the expected value |
276 | 277 | self.assertEqual(etrecord.export_graph_id, expected_graph_id) |
277 | 278 |
|
| 279 | + def test_to_edge_transform_and_lower_with_etrecord_generation(self): |
| 280 | + """Test that to_edge_transform_and_lower generates ETRecord correctly.""" |
| 281 | + f = models.BasicSinMax() |
| 282 | + aten_program = export(f, f.get_random_inputs(), strict=True) |
| 283 | + |
| 284 | + # Test with generate_etrecord=True |
| 285 | + edge_manager = to_edge_transform_and_lower( |
| 286 | + aten_program, |
| 287 | + generate_etrecord=True, |
| 288 | + ) |
| 289 | + |
| 290 | + # Verify that ETRecord was generated and attached |
| 291 | + self.assertIsNotNone(edge_manager._etrecord) |
| 292 | + etrecord = edge_manager._etrecord |
| 293 | + |
| 294 | + # Verify that ETRecord has the expected data |
| 295 | + self.assertIsNotNone(etrecord.exported_program) |
| 296 | + self.assertIsNotNone(etrecord.export_graph_id) |
| 297 | + self.assertIsNotNone(etrecord.edge_dialect_program) |
| 298 | + |
| 299 | + # Verify the exported program matches the input |
| 300 | + self.check_graph_closeness( |
| 301 | + etrecord.exported_program, |
| 302 | + aten_program.graph_module, |
| 303 | + ) |
| 304 | + self.assertEqual( |
| 305 | + etrecord.export_graph_id, |
| 306 | + id(aten_program.graph), |
| 307 | + ) |
| 308 | + |
| 309 | + # Verify the edge dialect program matches the edge manager |
| 310 | + self.check_graph_closeness( |
| 311 | + etrecord.edge_dialect_program, |
| 312 | + edge_manager.exported_program().graph_module, |
| 313 | + ) |
| 314 | + |
| 315 | + def test_to_edge_transform_and_lower_without_etrecord_generation(self): |
| 316 | + """Test that to_edge_transform_and_lower works correctly without ETRecord generation.""" |
| 317 | + f = models.BasicSinMax() |
| 318 | + aten_program = export(f, f.get_random_inputs(), strict=True) |
| 319 | + |
| 320 | + # Test with generate_etrecord=False (default) |
| 321 | + edge_manager = to_edge_transform_and_lower(aten_program) |
| 322 | + |
| 323 | + # Verify that no ETRecord was generated |
| 324 | + self.assertIsNone(edge_manager._etrecord) |
| 325 | + |
| 326 | + # Verify that the edge manager still works correctly |
| 327 | + self.assertIsNotNone(edge_manager.exported_program()) |
| 328 | + |
| 329 | + def test_get_etrecord_from_executorch_program_manager(self): |
| 330 | + """Test getting ETRecord from ExecutorchProgramManager using get_etrecord() method.""" |
| 331 | + f = models.BasicSinMax() |
| 332 | + aten_program = export(f, f.get_random_inputs(), strict=True) |
| 333 | + |
| 334 | + # Generate edge manager with ETRecord |
| 335 | + edge_manager = to_edge_transform_and_lower( |
| 336 | + aten_program, |
| 337 | + generate_etrecord=True, |
| 338 | + ) |
| 339 | + |
| 340 | + # Convert to executorch |
| 341 | + et_manager = edge_manager.to_executorch() |
| 342 | + |
| 343 | + # Test get_etrecord method |
| 344 | + etrecord = et_manager.get_etrecord() |
| 345 | + self.assertIsNotNone(etrecord) |
| 346 | + |
| 347 | + # Verify that the returned ETRecord has all expected data |
| 348 | + self.assertIsNotNone(etrecord.exported_program) |
| 349 | + self.assertIsNotNone(etrecord.export_graph_id) |
| 350 | + self.assertIsNotNone(etrecord.edge_dialect_program) |
| 351 | + self.assertIsNotNone(etrecord._debug_handle_map) |
| 352 | + self.assertIsNotNone(etrecord._delegate_map) |
| 353 | + |
| 354 | + # Verify the data matches the original input |
| 355 | + self.check_graph_closeness( |
| 356 | + etrecord.exported_program, |
| 357 | + aten_program.graph_module, |
| 358 | + ) |
| 359 | + self.assertEqual( |
| 360 | + etrecord.export_graph_id, |
| 361 | + id(aten_program.graph), |
| 362 | + ) |
| 363 | + |
| 364 | + # Verify the executorch program data matches |
| 365 | + # ETRecord stores data directly (not JSON serialized), so compare with original data |
| 366 | + self.assertEqual(etrecord._debug_handle_map, et_manager.debug_handle_map) |
| 367 | + self.assertEqual(etrecord._delegate_map, et_manager.delegate_map) |
| 368 | + |
| 369 | + def test_get_etrecord_from_executorch_program_manager_without_generation(self): |
| 370 | + """Test getting ETRecord from ExecutorchProgramManager when ETRecord was not generated.""" |
| 371 | + f = models.BasicSinMax() |
| 372 | + aten_program = export(f, f.get_random_inputs(), strict=True) |
| 373 | + |
| 374 | + # Generate edge manager without ETRecord |
| 375 | + edge_manager = to_edge_transform_and_lower(aten_program) |
| 376 | + |
| 377 | + # Verify no ETRecord on edge manager |
| 378 | + self.assertIsNone(edge_manager._etrecord) |
| 379 | + |
| 380 | + # Convert to executorch |
| 381 | + et_manager = edge_manager.to_executorch() |
| 382 | + |
| 383 | + # Verify no ETRecord on executorch manager |
| 384 | + self.assertIsNone(et_manager._etrecord) |
| 385 | + |
| 386 | + # Test get_etrecord method should raise RuntimeError |
| 387 | + with self.assertRaises(RuntimeError) as context: |
| 388 | + et_manager.get_etrecord() |
| 389 | + |
| 390 | + self.assertIn("ETRecord was not generated", str(context.exception)) |
| 391 | + |
| 392 | + def test_to_edge_transform_and_lower_etrecord_save_and_parse(self): |
| 393 | + """Test that ETRecord generated by to_edge_transform_and_lower can be saved and parsed.""" |
| 394 | + f = models.BasicSinMax() |
| 395 | + aten_program = export(f, f.get_random_inputs(), strict=True) |
| 396 | + |
| 397 | + # Generate edge manager with ETRecord |
| 398 | + edge_manager = to_edge_transform_and_lower( |
| 399 | + aten_program, |
| 400 | + generate_etrecord=True, |
| 401 | + ) |
| 402 | + |
| 403 | + # Convert to executorch to get complete ETRecord |
| 404 | + et_manager = edge_manager.to_executorch() |
| 405 | + etrecord = et_manager.get_etrecord() |
| 406 | + |
| 407 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 408 | + etrecord_path = tmpdirname + "/etrecord_flow2.bin" |
| 409 | + |
| 410 | + etrecord.save(etrecord_path) |
| 411 | + |
| 412 | + # Parse ETRecord back and verify |
| 413 | + parsed_etrecord = parse_etrecord(etrecord_path) |
| 414 | + |
| 415 | + # Validate that all components are preserved |
| 416 | + # Note: Skip graph structure comparison due to transformation differences |
| 417 | + self.check_graph_closeness( |
| 418 | + etrecord.exported_program, parsed_etrecord.exported_program |
| 419 | + ) |
| 420 | + self.check_graph_closeness( |
| 421 | + etrecord.edge_dialect_program, parsed_etrecord.edge_dialect_program |
| 422 | + ) |
| 423 | + |
| 424 | + # Validate executorch program data |
| 425 | + self.assertEqual( |
| 426 | + parsed_etrecord._debug_handle_map, |
| 427 | + json.loads(json.dumps(et_manager.debug_handle_map)), |
| 428 | + ) |
| 429 | + self.assertEqual( |
| 430 | + parsed_etrecord._delegate_map, |
| 431 | + json.loads(json.dumps(et_manager.delegate_map)), |
| 432 | + ) |
| 433 | + |
| 434 | + # Validate export graph id |
| 435 | + self.assertEqual( |
| 436 | + parsed_etrecord.export_graph_id, |
| 437 | + id(aten_program.graph), |
| 438 | + ) |
| 439 | + |
278 | 440 | def test_add_extra_export_modules(self): |
279 | 441 | """Test add_extra_export_modules when ETRecord already has a graph_map.""" |
280 | 442 | captured_output, edge_output, et_output = self.get_test_model() |
|
0 commit comments