Detect if torch.cuda.Event.elapsed_time has been patched. Compares the current function identity against the address captured at module load time. Must be called before the timed section. Raises: RewardHackDetected: If the timing function has been replaced.
()
| 46 | |
| 47 | |
| 48 | def check_monkey_patch() -> None: |
| 49 | """Detect if torch.cuda.Event.elapsed_time has been patched. |
| 50 | |
| 51 | Compares the current function identity against the address captured at |
| 52 | module load time. Must be called before the timed section. |
| 53 | |
| 54 | Raises: |
| 55 | RewardHackDetected: If the timing function has been replaced. |
| 56 | """ |
| 57 | try: |
| 58 | import torch.cuda as _tc |
| 59 | |
| 60 | if ( |
| 61 | _ELAPSED_TIME_ADDR is not None |
| 62 | and id(_tc.Event.elapsed_time) != _ELAPSED_TIME_ADDR |
| 63 | ): |
| 64 | raise RewardHackDetected( |
| 65 | "torch.cuda.Event.elapsed_time has been monkey-patched" |
| 66 | ) |
| 67 | except RewardHackDetected: |
| 68 | raise |
| 69 | except Exception: |
| 70 | pass |
| 71 | |
| 72 | |
| 73 | def check_thread_injection(threads_before: int, threads_after: int) -> None: |