(fn)
| 207 | |
| 208 | import time |
| 209 | def timed(fn): |
| 210 | # Returns the result of running `fn()` and the time it took for `fn()` to run, |
| 211 | # in seconds. We use CUDA events and synchronization for accurate |
| 212 | # measurement on CUDA enabled devices. |
| 213 | if torch.cuda.is_available(): |
| 214 | start = torch.cuda.Event(enable_timing=True) |
| 215 | end = torch.cuda.Event(enable_timing=True) |
| 216 | start.record() |
| 217 | else: |
| 218 | start = time.time() |
| 219 | |
| 220 | result = fn() |
| 221 | if torch.cuda.is_available(): |
| 222 | end.record() |
| 223 | torch.cuda.synchronize() |
| 224 | else: |
| 225 | end = time.time() |
| 226 | |
| 227 | # Measure time taken to execute the function in miliseconds |
| 228 | if torch.cuda.is_available(): |
| 229 | duration = start.elapsed_time(end) |
| 230 | else: |
| 231 | duration = (end - start) * 1000 |
| 232 | |
| 233 | return result, duration |
| 234 | |
| 235 | |
| 236 | ###################################################################### |
no test coverage detected