I write a minimal reproducible example
import torch
import torch.nn as nn
import time
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.shared_layers = nn.Sequential(
nn.Conv2d(4, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.AdaptiveAvgPool2d(1)
)
self.policy_head = nn.Linear(64, 400)
self.value_head = nn.Linear(64, 1)
self.action_value_head = nn.Linear(64, 400)
def forward(self, x):
x = self.shared_layers(x)
x = x.view(x.size(0), -1)
policy = self.policy_head(x)
value = self.value_head(x)
action_values = self.action_value_head(x)
return policy, value, action_values
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("using device: ", device)
model = Net().to(device)
model.eval()
print("\nnow no sleep:")
for i in range(10):
states = torch.rand(128, 4, 20, 20, device = device)
#no time.sleep
start_time = time.time()
start_event = torch.cuda.Event(enable_timing=True)
end_event = torch.cuda.Event(enable_timing=True)
start_event.record()
# forward
policy, value, action_value = model(states)
end_event.record()
torch.cuda.synchronize()
end_time = time.time()
total_time = start_event.elapsed_time(end_event) / 1000 # 转换为秒
print(f"time cost counted by gpu: {total_time:.4f} second")
total_time = end_time - start_time
print(f"time cost counted by cpu:{total_time:.4f} second")
print("\nnow sleep 1:")
for i in range(10):
states = torch.rand(128, 4, 20, 20, device = device)
time.sleep(1)
start_time = time.time()
start_event = torch.cuda.Event(enable_timing=True)
end_event = torch.cuda.Event(enable_timing=True)
start_event.record()
# forward
policy, value, action_value = model(states)
end_event.record()
torch.cuda.synchronize()
end_time = time.time()
total_time = start_event.elapsed_time(end_event) / 1000 # 转换为秒
print(f"time cost counted by gpu: {total_time:.4f} second")
total_time = end_time - start_time
print(f"time cost counted by cpu:{total_time:.4f} second")
print("\nnow sleep 10:")
for i in range(10):
states = torch.rand(128, 4, 20, 20, device = device)
time.sleep(10)
start_time = time.time()
start_event = torch.cuda.Event(enable_timing=True)
end_event = torch.cuda.Event(enable_timing=True)
start_event.record()
# forward
policy, value, action_value = model(states)
end_event.record()
torch.cuda.synchronize()
end_time = time.time()
total_time = start_event.elapsed_time(end_event) / 1000 # 转换为秒
print(f"time cost counted by gpu: {total_time:.4f} second")
total_time = end_time - start_time
print(f"time cost counted by cpu:{total_time:.4f} second")
print("\nnow sleep 60:")
for i in range(10):
states = torch.rand(128, 4, 20, 20, device = device)
time.sleep(60)
start_time = time.time()
start_event = torch.cuda.Event(enable_timing=True)
end_event = torch.cuda.Event(enable_timing=True)
start_event.record()
# forward
policy, value, action_value = model(states)
end_event.record()
torch.cuda.synchronize()
end_time = time.time()
total_time = start_event.elapsed_time(end_event) / 1000 # 转换为秒
print(f"time cost counted by gpu: {total_time:.4f} second")
total_time = end_time - start_time
print(f"time cost counted by cpu:{total_time:.4f} second")
The result of running this minimal.py on my computer is:
using device: cuda
now no sleep:
time cost counted by gpu: 0.2766 second
time cost counted by cpu:0.2768 second
time cost counted by gpu: 0.0012 second
time cost counted by cpu:0.0013 second
time cost counted by gpu: 0.0004 second
time cost counted by cpu:0.0004 second
time cost counted by gpu: 0.0004 second
time cost counted by cpu:0.0004 second
time cost counted by gpu: 0.0003 second
time cost counted by cpu:0.0004 second
time cost counted by gpu: 0.0003 second
time cost counted by cpu:0.0004 second
time cost counted by gpu: 0.0004 second
time cost counted by cpu:0.0004 second
time cost counted by gpu: 0.0003 second
time cost counted by cpu:0.0004 second
time cost counted by gpu: 0.0004 second
time cost counted by cpu:0.0004 second
time cost counted by gpu: 0.0003 second
time cost counted by cpu:0.0004 second
now sleep 1:
time cost counted by gpu: 0.0005 second
time cost counted by cpu:0.0007 second
time cost counted by gpu: 0.0006 second
time cost counted by cpu:0.0007 second
time cost counted by gpu: 0.0006 second
time cost counted by cpu:0.0007 second
time cost counted by gpu: 0.0006 second
time cost counted by cpu:0.0007 second
time cost counted by gpu: 0.0008 second
time cost counted by cpu:0.0009 second
time cost counted by gpu: 0.0007 second
time cost counted by cpu:0.0008 second
time cost counted by gpu: 0.0007 second
time cost counted by cpu:0.0007 second
time cost counted by gpu: 0.0006 second
time cost counted by cpu:0.0008 second
time cost counted by gpu: 0.0008 second
time cost counted by cpu:0.0008 second
time cost counted by gpu: 0.0007 second
time cost counted by cpu:0.0008 second
now sleep 10:
time cost counted by gpu: 0.0007 second
time cost counted by cpu:0.0009 second
time cost counted by gpu: 0.0009 second
time cost counted by cpu:0.0011 second
time cost counted by gpu: 0.0009 second
time cost counted by cpu:0.0011 second
time cost counted by gpu: 0.0010 second
time cost counted by cpu:0.0014 second
time cost counted by gpu: 0.0009 second
time cost counted by cpu:0.0011 second
time cost counted by gpu: 0.0008 second
time cost counted by cpu:0.0010 second
time cost counted by gpu: 0.0016 second
time cost counted by cpu:0.0019 second
time cost counted by gpu: 0.0009 second
time cost counted by cpu:0.0011 second
time cost counted by gpu: 0.0007 second
time cost counted by cpu:0.0010 second
time cost counted by gpu: 0.0008 second
time cost counted by cpu:0.0010 second
now sleep 60:
time cost counted by gpu: 0.0014 second
time cost counted by cpu:0.0101 second
time cost counted by gpu: 0.0013 second
time cost counted by cpu:0.0142 second
time cost counted by gpu: 0.0015 second
time cost counted by cpu:0.0120 second
time cost counted by gpu: 0.0010 second
time cost counted by cpu:0.0114 second
time cost counted by gpu: 0.0013 second
time cost counted by cpu:0.0152 second
time cost counted by gpu: 0.0014 second
time cost counted by cpu:0.0117 second
time cost counted by gpu: 0.0015 second
time cost counted by cpu:0.0119 second
time cost counted by gpu: 0.0011 second
time cost counted by cpu:0.0154 second
time cost counted by gpu: 0.0035 second
time cost counted by cpu:0.0264 second
time cost counted by gpu: 0.0011 second
time cost counted by cpu:0.0115 second
From the results, as the time of time.sleep
increases, the time for forward also increases