Created
October 26, 2018 15:55
-
-
Save soumith/4d562474ff63e1fcf59857c6d4ffcfca 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 time | |
import torch | |
import torchvision | |
batch_size = 128 | |
num_iterations = 10 | |
resnet50 = torchvision.models.resnet50().to(device="cuda") | |
inp = torch.randn(batch_size, 3, 224, 224, device="cuda") | |
target = torch.arange(batch_size, device="cuda") | |
optimizer = torch.optim.SGD(resnet50.parameters(), lr=0.01, momentum=0.9) | |
def forwardbackward(): | |
optimizer.zero_grad() | |
out = resnet50(inp) | |
loss = torch.nn.functional.cross_entropy(out, target) | |
loss.backward() | |
optimizer.step() | |
# warmup | |
forwardbackward() | |
forwardbackward() | |
# bencmark | |
torch.cuda.synchronize() | |
tm = time.time() | |
for i in range(num_iterations): | |
forwardbackward() | |
torch.cuda.synchronize() | |
print((time.time() - tm) / num_iterations) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment