Last active
September 17, 2021 17:55
-
-
Save sandeepkumar-skb/8808f0e4617bc2ee0d3a90d69b9eb365 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch, time, gc, contextlib | |
# Timing utilities | |
start_time = None | |
@contextlib.contextmanager | |
def add_timer(local_msg): | |
try: | |
start_timer() | |
yield | |
finally: | |
end_timer(local_msg) | |
def start_timer(): | |
global start_time | |
gc.collect() | |
torch.cuda.empty_cache() | |
torch.cuda.reset_max_memory_allocated() | |
torch.cuda.synchronize() | |
start_time = time.perf_counter() | |
def end_timer(local_msg=""): | |
torch.cuda.synchronize() | |
end_time = time.perf_counter() | |
print("\n" + local_msg) | |
print("Total execution time = {:.3f} ms".format((end_time - start_time)*1000)) | |
print("Max memory used by tensors = {:.3f} GB".format(torch.cuda.max_memory_allocated()/(1024*1024*1024))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: