Apply each kernel replacement individually to find which one causes failure.
(
model: nn.Module,
model_input: Union[torch.Tensor, Dict[str, torch.Tensor]],
ref_tensor: torch.Tensor,
replacements: List[KernelReplacement],
dtype: torch.dtype,
)
| 770 | # --------------------------------------------------------------------------- |
| 771 | |
| 772 | def diagnose_kernel_failures( |
| 773 | model: nn.Module, |
| 774 | model_input: Union[torch.Tensor, Dict[str, torch.Tensor]], |
| 775 | ref_tensor: torch.Tensor, |
| 776 | replacements: List[KernelReplacement], |
| 777 | dtype: torch.dtype, |
| 778 | ) -> List[Dict[str, Any]]: |
| 779 | """ |
| 780 | Apply each kernel replacement individually to find which one causes failure. |
| 781 | """ |
| 782 | results = [] |
| 783 | |
| 784 | for repl in replacements: |
| 785 | print(f"\n Testing kernel: {repl.kernel_type} (rank {repl.rank})...") |
| 786 | ctx = OptimizedModelContext(model, [repl]) |
| 787 | |
| 788 | try: |
| 789 | with ctx as patched_model: |
| 790 | with torch.no_grad(): |
| 791 | if isinstance(model_input, dict): |
| 792 | opt_output = patched_model(**model_input) |
| 793 | else: |
| 794 | opt_output = patched_model(model_input) |
| 795 | torch.cuda.synchronize() |
| 796 | |
| 797 | opt_tensor = extract_tensor(opt_output) |
| 798 | comp = compare_outputs(ref_tensor, opt_tensor, dtype) |
| 799 | |
| 800 | results.append({ |
| 801 | "kernel_type": repl.kernel_type, |
| 802 | "rank": repl.rank, |
| 803 | "path": repl.optimized_path, |
| 804 | "correctness": comp["correctness"], |
| 805 | "max_abs_error": comp.get("max_abs_error", 0.0), |
| 806 | "mean_abs_error": comp.get("mean_abs_error", 0.0), |
| 807 | "reason": comp.get("reason", ""), |
| 808 | }) |
| 809 | |
| 810 | status = comp["correctness"] |
| 811 | if status == "PASS": |
| 812 | print(f" -> PASS (max_err={comp.get('max_abs_error', 0):.6e})") |
| 813 | else: |
| 814 | print(f" -> FAIL: {comp.get('reason', 'unknown')}") |
| 815 | |
| 816 | except Exception as e: |
| 817 | results.append({ |
| 818 | "kernel_type": repl.kernel_type, |
| 819 | "rank": repl.rank, |
| 820 | "path": repl.optimized_path, |
| 821 | "correctness": "ERROR", |
| 822 | "max_abs_error": float("inf"), |
| 823 | "mean_abs_error": float("inf"), |
| 824 | "reason": str(e), |
| 825 | }) |
| 826 | print(f" -> ERROR: {e}") |
| 827 | |
| 828 | return results |
| 829 |
no test coverage detected