Skip to content

Instantly share code, notes, and snippets.

@entron
Last active June 13, 2020 16:43
Show Gist options
  • Save entron/7183fb433958b2906cb60fb6fc7e7a2f to your computer and use it in GitHub Desktop.
Save entron/7183fb433958b2906cb60fb6fc7e7a2f to your computer and use it in GitHub Desktop.
simple gpu benchmark code with pytorch for common CNN netoworks
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"SqueezeNet(\n",
" (features): Sequential(\n",
" (0): Conv2d(3, 96, kernel_size=(7, 7), stride=(2, 2))\n",
" (1): ReLU(inplace=True)\n",
" (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=True)\n",
" (3): Fire(\n",
" (squeeze): Conv2d(96, 16, kernel_size=(1, 1), stride=(1, 1))\n",
" (squeeze_activation): ReLU(inplace=True)\n",
" (expand1x1): Conv2d(16, 64, kernel_size=(1, 1), stride=(1, 1))\n",
" (expand1x1_activation): ReLU(inplace=True)\n",
" (expand3x3): Conv2d(16, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (expand3x3_activation): ReLU(inplace=True)\n",
" )\n",
" (4): Fire(\n",
" (squeeze): Conv2d(128, 16, kernel_size=(1, 1), stride=(1, 1))\n",
" (squeeze_activation): ReLU(inplace=True)\n",
" (expand1x1): Conv2d(16, 64, kernel_size=(1, 1), stride=(1, 1))\n",
" (expand1x1_activation): ReLU(inplace=True)\n",
" (expand3x3): Conv2d(16, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (expand3x3_activation): ReLU(inplace=True)\n",
" )\n",
" (5): Fire(\n",
" (squeeze): Conv2d(128, 32, kernel_size=(1, 1), stride=(1, 1))\n",
" (squeeze_activation): ReLU(inplace=True)\n",
" (expand1x1): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))\n",
" (expand1x1_activation): ReLU(inplace=True)\n",
" (expand3x3): Conv2d(32, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (expand3x3_activation): ReLU(inplace=True)\n",
" )\n",
" (6): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=True)\n",
" (7): Fire(\n",
" (squeeze): Conv2d(256, 32, kernel_size=(1, 1), stride=(1, 1))\n",
" (squeeze_activation): ReLU(inplace=True)\n",
" (expand1x1): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))\n",
" (expand1x1_activation): ReLU(inplace=True)\n",
" (expand3x3): Conv2d(32, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (expand3x3_activation): ReLU(inplace=True)\n",
" )\n",
" (8): Fire(\n",
" (squeeze): Conv2d(256, 48, kernel_size=(1, 1), stride=(1, 1))\n",
" (squeeze_activation): ReLU(inplace=True)\n",
" (expand1x1): Conv2d(48, 192, kernel_size=(1, 1), stride=(1, 1))\n",
" (expand1x1_activation): ReLU(inplace=True)\n",
" (expand3x3): Conv2d(48, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (expand3x3_activation): ReLU(inplace=True)\n",
" )\n",
" (9): Fire(\n",
" (squeeze): Conv2d(384, 48, kernel_size=(1, 1), stride=(1, 1))\n",
" (squeeze_activation): ReLU(inplace=True)\n",
" (expand1x1): Conv2d(48, 192, kernel_size=(1, 1), stride=(1, 1))\n",
" (expand1x1_activation): ReLU(inplace=True)\n",
" (expand3x3): Conv2d(48, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (expand3x3_activation): ReLU(inplace=True)\n",
" )\n",
" (10): Fire(\n",
" (squeeze): Conv2d(384, 64, kernel_size=(1, 1), stride=(1, 1))\n",
" (squeeze_activation): ReLU(inplace=True)\n",
" (expand1x1): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1))\n",
" (expand1x1_activation): ReLU(inplace=True)\n",
" (expand3x3): Conv2d(64, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (expand3x3_activation): ReLU(inplace=True)\n",
" )\n",
" (11): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=True)\n",
" (12): Fire(\n",
" (squeeze): Conv2d(512, 64, kernel_size=(1, 1), stride=(1, 1))\n",
" (squeeze_activation): ReLU(inplace=True)\n",
" (expand1x1): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1))\n",
" (expand1x1_activation): ReLU(inplace=True)\n",
" (expand3x3): Conv2d(64, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (expand3x3_activation): ReLU(inplace=True)\n",
" )\n",
" )\n",
" (classifier): Sequential(\n",
" (0): Dropout(p=0.5, inplace=False)\n",
" (1): Conv2d(512, 1000, kernel_size=(1, 1), stride=(1, 1))\n",
" (2): ReLU(inplace=True)\n",
" (3): AdaptiveAvgPool2d(output_size=(1, 1))\n",
" )\n",
")"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"from torch.utils.data import DataLoader\n",
"\n",
"import torchvision.models as models\n",
"import torchvision.datasets as datasets\n",
"import torchvision.transforms as transforms\n",
"\n",
"\n",
"model = models.squeezenet1_0()\n",
"criterion = nn.CrossEntropyLoss()\n",
"optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)\n",
"\n",
"dataset = datasets.FakeData(\n",
" size=1000,\n",
" transform=transforms.ToTensor())\n",
"loader = DataLoader(\n",
" dataset,\n",
" num_workers=1,\n",
" pin_memory=True\n",
")\n",
"\n",
"device = 'cuda:1'\n",
"\n",
"model.to(device)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done\n",
"CPU times: user 9.62 s, sys: 947 ms, total: 10.6 s\n",
"Wall time: 9.84 s\n"
]
}
],
"source": [
"%%time\n",
"for data, target in loader:\n",
" data = data.to(device, non_blocking=True)\n",
" target = target.to(device, non_blocking=True)\n",
" optimizer.zero_grad()\n",
" output = model(data)\n",
" loss = criterion(output, target)\n",
" loss.backward()\n",
" optimizer.step()\n",
" \n",
"print('Done')"
]
},
{
"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": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment