Last active
June 13, 2020 16:42
-
-
Save entron/99f45c3bc22b536dbd1614f322af20bc to your computer and use it in GitHub Desktop.
simple gpu benchmark with pytorch
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import torch\n", | |
"import torchvision\n", | |
"import torchvision.transforms as transforms\n", | |
"from torchsummary import summary" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"cuda:0\n" | |
] | |
} | |
], | |
"source": [ | |
"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n", | |
"print(device)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Files already downloaded and verified\n" | |
] | |
} | |
], | |
"source": [ | |
"transform = transforms.Compose(\n", | |
" [transforms.ToTensor(),\n", | |
" transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\n", | |
"\n", | |
"trainset = torchvision.datasets.CIFAR10(root='./data', train=True,\n", | |
" download=True, transform=transform)\n", | |
"trainloader = torch.utils.data.DataLoader(trainset, batch_size=16,\n", | |
" shuffle=True, num_workers=2, pin_memory=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import torch.nn as nn\n", | |
"import torch.nn.functional as F\n", | |
"\n", | |
"\n", | |
"class Net(nn.Module):\n", | |
" def __init__(self):\n", | |
" super(Net, self).__init__()\n", | |
" self.conv1 = nn.Conv2d(3, 12, 5)\n", | |
" self.pool = nn.MaxPool2d(2, 2)\n", | |
" self.conv2 = nn.Conv2d(12, 16, 5)\n", | |
" self.fc1 = nn.Linear(16 * 5 * 5, 120)\n", | |
" self.fc2 = nn.Linear(120, 84)\n", | |
" self.fc3 = nn.Linear(84, 10)\n", | |
"\n", | |
" def forward(self, x):\n", | |
" x = self.pool(F.relu(self.conv1(x)))\n", | |
" x = self.pool(F.relu(self.conv2(x)))\n", | |
" x = x.view(-1, 16 * 5 * 5)\n", | |
" x = F.relu(self.fc1(x))\n", | |
" x = F.relu(self.fc2(x))\n", | |
" x = self.fc3(x)\n", | |
" return x\n", | |
"\n", | |
"\n", | |
"net = Net()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"----------------------------------------------------------------\n", | |
" Layer (type) Output Shape Param #\n", | |
"================================================================\n", | |
" Conv2d-1 [-1, 12, 28, 28] 912\n", | |
" MaxPool2d-2 [-1, 12, 14, 14] 0\n", | |
" Conv2d-3 [-1, 16, 10, 10] 4,816\n", | |
" MaxPool2d-4 [-1, 16, 5, 5] 0\n", | |
" Linear-5 [-1, 120] 48,120\n", | |
" Linear-6 [-1, 84] 10,164\n", | |
" Linear-7 [-1, 10] 850\n", | |
"================================================================\n", | |
"Total params: 64,862\n", | |
"Trainable params: 64,862\n", | |
"Non-trainable params: 0\n", | |
"----------------------------------------------------------------\n", | |
"Input size (MB): 0.01\n", | |
"Forward/backward pass size (MB): 0.11\n", | |
"Params size (MB): 0.25\n", | |
"Estimated Total Size (MB): 0.37\n", | |
"----------------------------------------------------------------\n" | |
] | |
} | |
], | |
"source": [ | |
"summary(net, (3, 32, 32), device='cpu')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Net(\n", | |
" (conv1): Conv2d(3, 12, kernel_size=(5, 5), stride=(1, 1))\n", | |
" (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n", | |
" (conv2): Conv2d(12, 16, kernel_size=(5, 5), stride=(1, 1))\n", | |
" (fc1): Linear(in_features=400, out_features=120, bias=True)\n", | |
" (fc2): Linear(in_features=120, out_features=84, bias=True)\n", | |
" (fc3): Linear(in_features=84, out_features=10, bias=True)\n", | |
")" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"net.to(device)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import torch.optim as optim\n", | |
"\n", | |
"criterion = nn.CrossEntropyLoss()\n", | |
"optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 2000] loss: 2.172\n", | |
"[2, 2000] loss: 1.580\n", | |
"[3, 2000] loss: 1.381\n", | |
"[4, 2000] loss: 1.257\n", | |
"[5, 2000] loss: 1.162\n", | |
"Finished Training\n", | |
"CPU times: user 33.5 s, sys: 6.55 s, total: 40.1 s\n", | |
"Wall time: 29.7 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"for epoch in range(5): # loop over the dataset multiple times\n", | |
"\n", | |
" running_loss = 0.0\n", | |
" for i, data in enumerate(trainloader, 0):\n", | |
" # get the inputs; data is a list of [inputs, labels]\n", | |
" # inputs, labels = data\n", | |
" inputs, labels = data[0].to(device), data[1].to(device)\n", | |
"\n", | |
" # zero the parameter gradients\n", | |
" optimizer.zero_grad()\n", | |
"\n", | |
" # forward + backward + optimize\n", | |
" outputs = net(inputs)\n", | |
" loss = criterion(outputs, labels)\n", | |
" loss.backward()\n", | |
" optimizer.step()\n", | |
"\n", | |
" # print statistics\n", | |
" running_loss += loss.item()\n", | |
" if i % 2000 == 1999: # print every 2000 mini-batches\n", | |
" print('[%d, %5d] loss: %.3f' %\n", | |
" (epoch + 1, i + 1, running_loss / 2000))\n", | |
" running_loss = 0.0\n", | |
"\n", | |
"print('Finished Training')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment