(
exec_prog,
original_model: torch.nn.Module,
output_name: str,
example_inputs: Tuple[torch.Tensor, ...],
args,
)
| 712 | |
| 713 | |
| 714 | def _save_bpte_program( |
| 715 | exec_prog, |
| 716 | original_model: torch.nn.Module, |
| 717 | output_name: str, |
| 718 | example_inputs: Tuple[torch.Tensor, ...], |
| 719 | args, |
| 720 | ): |
| 721 | # Construct MethodTestSuite for Each Method |
| 722 | |
| 723 | # Generate Test Suites |
| 724 | method_names = [ |
| 725 | method.name for method in exec_prog.executorch_program.execution_plan |
| 726 | ] |
| 727 | |
| 728 | program_inputs = {m_name: [example_inputs] for m_name in method_names} |
| 729 | |
| 730 | method_test_suites: List[MethodTestSuite] = [] |
| 731 | for m_name in method_names: |
| 732 | method_inputs = program_inputs[m_name] |
| 733 | |
| 734 | # To create a bundled program, we first create every test cases from input. We leverage eager model |
| 735 | # to generate expected output for each test input, and use MethodTestCase to hold the information of |
| 736 | # each test case. We gather all MethodTestCase for same method into one MethodTestSuite, and generate |
| 737 | # bundled program by all MethodTestSuites. |
| 738 | method_test_cases: List[MethodTestCase] = [] |
| 739 | |
| 740 | if args.intermediates: |
| 741 | # Save model.pth |
| 742 | intermediates_path = Path(args.intermediates) |
| 743 | model_path = os.path.join(intermediates_path, "model.pth") |
| 744 | try: |
| 745 | torch.save(original_model, model_path) |
| 746 | except: |
| 747 | logging.warning(f"Could not torch.save(model, {model_path})") |
| 748 | |
| 749 | method_index = 0 |
| 750 | for method_input in method_inputs: |
| 751 | output_ref = original_model(*method_input) |
| 752 | |
| 753 | logging.debug(f"input_{method_index}: {method_input}") |
| 754 | logging.debug(f"output_ref_{method_index}: {output_ref}") |
| 755 | |
| 756 | if args.intermediates: |
| 757 | # Save model input and referece output |
| 758 | input_path = os.path.join( |
| 759 | intermediates_path, f"input_{method_index}.pt" # type: ignore[possibly-undefined] |
| 760 | ) |
| 761 | try: |
| 762 | torch.save(method_input, input_path) |
| 763 | except: |
| 764 | logging.warning( |
| 765 | f"Could not torch.save(input_{method_index}, {input_path})" |
| 766 | ) |
| 767 | refoutput_path = os.path.join( |
| 768 | intermediates_path, f"output_ref_{method_index}.pt" |
| 769 | ) |
| 770 | try: |
| 771 | torch.save(output_ref, refoutput_path) |
no test coverage detected