Created
March 21, 2020 15:11
-
-
Save koukyo1994/ae1465300deae6da74e61cbf235f8461 to your computer and use it in GitHub Desktop.
PyTorch Performance Analysis
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 1. Libraries" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'1.4.0'" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import cProfile\n", | |
"import time\n", | |
"\n", | |
"import numpy as np\n", | |
"import torch\n", | |
"import torch.utils.data as torchdata\n", | |
"\n", | |
"from multiprocessing import cpu_count\n", | |
"\n", | |
"\n", | |
"torch.__version__" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 2. DataLoaderでnum_workers > 0を設定して早くなるのはどんな時か?" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Dataset定義\n", | |
"\n", | |
"オプションをつけることでわざと遅くする機能を持たせます。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class DatasetWrapper(torchdata.Dataset):\n", | |
" def __init__(self, slow=False, wait=0.1):\n", | |
" self.slow = slow\n", | |
" self.wait = wait\n", | |
" \n", | |
" def __len__(self):\n", | |
" return 128\n", | |
" \n", | |
" def __getitem__(self, idx):\n", | |
" if self.slow:\n", | |
" time.sleep(self.wait)\n", | |
" return np.random.random(10)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### num_workes > 0に指定して効果検証" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# parameters\n", | |
"batch_size = 32\n", | |
"shuffle = False\n", | |
"pin_memory = False" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"cpuの数: 8\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"cpuの数:\", cpu_count())\n", | |
"fastloader = torchdata.DataLoader(\n", | |
" DatasetWrapper(), \n", | |
" num_workers=0, \n", | |
" batch_size=batch_size,\n", | |
" shuffle=shuffle,\n", | |
" pin_memory=pin_memory)\n", | |
"\n", | |
"fastloader_multi = torchdata.DataLoader(\n", | |
" DatasetWrapper(), \n", | |
" num_workers=cpu_count() // 2, \n", | |
" batch_size=batch_size,\n", | |
" shuffle=shuffle,\n", | |
" pin_memory=pin_memory)\n", | |
"\n", | |
"slowloader = torchdata.DataLoader(\n", | |
" DatasetWrapper(slow=True, wait=0.01), \n", | |
" num_workers=0, \n", | |
" batch_size=batch_size,\n", | |
" shuffle=shuffle,\n", | |
" pin_memory=pin_memory)\n", | |
"\n", | |
"slowloader_multi = torchdata.DataLoader(\n", | |
" DatasetWrapper(slow=True, wait=0.01), \n", | |
" num_workers=cpu_count() // 2, \n", | |
" batch_size=batch_size,\n", | |
" shuffle=shuffle,\n", | |
" pin_memory=pin_memory)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### `DataLoader`のロード時間 < `for`文内の場合" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 4.31 ms, sys: 1.77 ms, total: 6.09 ms\n", | |
"Wall time: 4.01 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"for batch in fastloader:\n", | |
" time.sleep(1.0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 14.3 ms, sys: 18.9 ms, total: 33.1 ms\n", | |
"Wall time: 4.16 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"for batch in fastloader_multi:\n", | |
" time.sleep(1.0)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### `DataLoader`のロード時間 > `for`文内の場合" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 13.3 ms, sys: 4.77 ms, total: 18.1 ms\n", | |
"Wall time: 5.52 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"for batch in slowloader:\n", | |
" time.sleep(1.0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 13.2 ms, sys: 20.3 ms, total: 33.5 ms\n", | |
"Wall time: 4.4 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"for batch in slowloader_multi:\n", | |
" time.sleep(1.0)" | |
] | |
}, | |
{ | |
"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.6.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment