generated from aicoe-aiops/project-template
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from MichaelClifford/gpu-test
add gpu test notebook
- Loading branch information
Showing
1 changed file
with
218 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "16e3552b-6245-4e8a-ab1c-2e17729901d4", | ||
"metadata": {}, | ||
"source": [ | ||
"# Confirm Notebook Can Access GPU \n", | ||
"\n", | ||
"This notebook provides a couple of small examples that confirm your notebook environment is able to connect to a GPU, either locally or on a cluster. The bottom section also provides a short example showcasing the increased performance of using the GPU over a CPU for a matrix multiplication task. \n", | ||
"\n", | ||
"\n", | ||
"The one caveat to ensuring this notebook works correctly and you can communicate with the gpu, is that you must install pytorch.\n", | ||
"\n", | ||
"```\n", | ||
"pip3 install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html\n", | ||
"```\n", | ||
"\n", | ||
"In the near future this requirement will be handled automatically. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8578d326-157b-4512-9379-367cf4629d93", | ||
"metadata": {}, | ||
"source": [ | ||
"## Am I connected to a GPU?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "a66360fb-ff15-477b-a815-62c229bb39b3", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"import timeit" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "ef63602d-c4e0-4056-898f-f43fba8eba35", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"tensor([[0.3546, 0.9113, 0.7730],\n", | ||
" [0.1329, 0.8297, 0.8176],\n", | ||
" [0.4446, 0.3666, 0.1261],\n", | ||
" [0.4993, 0.8514, 0.9197],\n", | ||
" [0.7537, 0.0293, 0.4049]])\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"x = torch.rand(5, 3)\n", | ||
"print(x)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "e22ed182-b282-4ebd-aba2-91cce53c321d", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"True" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"use_cuda = torch.cuda.is_available()\n", | ||
"use_cuda" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "840b1c7e-8832-4538-9173-90f07bab5be7", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"__CUDNN VERSION: 8005\n", | ||
"__Number CUDA Devices: 1\n", | ||
"__CUDA Device Name: Tesla K80\n", | ||
"__CUDA Device Total Memory [GB]: 11.99702016\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"if use_cuda:\n", | ||
" print('__CUDNN VERSION:', torch.backends.cudnn.version())\n", | ||
" print('__Number CUDA Devices:', torch.cuda.device_count())\n", | ||
" print('__CUDA Device Name:',torch.cuda.get_device_name(0))\n", | ||
" print('__CUDA Device Total Memory [GB]:',torch.cuda.get_device_properties(0).total_memory/1e9)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "18651412-75e9-4894-86a2-563fc89ed652", | ||
"metadata": {}, | ||
"source": [ | ||
"The above outputs indicate that pytorch is installed, it can see the gpu, and it shows us some details about the device we are connected to. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3dc87e6e-5c8a-4754-9d0b-76c44bb4cd04", | ||
"metadata": {}, | ||
"source": [ | ||
"## Does the GPU speed things up?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "5364fc66-f118-439e-891b-64c0e50cca01", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def batched_dot_mul_sum(a,b):\n", | ||
" return a.mul(b).sum(-1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "d2be1632-0854-4feb-8d51-c6ffeaa18797", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"cpu = torch.device(\"cpu\")\n", | ||
"gpu = torch.device(\"cuda\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "e58e65b6-4e04-44e5-a3a5-dd45d1af0dab", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"x_cpu = torch.randn(4000,4000,device=cpu)\n", | ||
"x_gpu = torch.randn(4000,4000, device=gpu)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "4a2cfde3-b101-46db-b9e4-0b5d72a10ca2", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"mul_sum(x, x) cpu: 2.2202524829190224 seconds\n", | ||
"mul_sum(x, x) gpu: 0.003196620149537921 seconds\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"t0 = timeit.Timer(stmt='batched_dot_mul_sum(x,x)', setup='from __main__ import batched_dot_mul_sum',\n", | ||
" globals={'x': x_cpu})\n", | ||
"\n", | ||
"t1 = timeit.Timer(stmt='batched_dot_mul_sum(x,x)', setup='from __main__ import batched_dot_mul_sum',\n", | ||
" globals={'x': x_gpu})\n", | ||
"\n", | ||
"print(f'mul_sum(x, x) cpu: {t0.timeit(100)} seconds')\n", | ||
"print(f'mul_sum(x, x) gpu: {t1.timeit(100)} seconds')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "833d220d-bc5a-4698-9076-b7725ea6d257", | ||
"metadata": {}, | ||
"source": [ | ||
"We can see from the output above that the speed up on this particular node is orders of magnitude faster using the GPU over the CPU for a 4000,4000 matrix multiplication. Yeah!\n", | ||
"\n", | ||
"If this notebook does not run for you, that means you are not connected to a GPU. " | ||
] | ||
} | ||
], | ||
"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": 5 | ||
} |