Why use GPU with Neural Networks?
Key Takeaways
This video explains why GPUs are used with Neural Networks, using an analogy and demonstrating CUDA with PyTorch code, highlighting the benefits of using GPUs over CPUs for deep learning computations.
Full Transcript
why use GPUs in neural networks there's a hardware and a software aspect to this answer and so this is going to be a two-part series in this video we're going to take a look at what the GPU Hardware provides that makes neural net training much faster and in the next video what software and algorithm changes do GPUs make to speed-up GPU processing what is five times six times seven 210 210 what is eight times three times five 120 120 what is seven times 12 times three to 50 to 250 to the CPU is blazing fast the GPU is almost as fast but slightly trails behind what is matrix a times matrix B this CPU your answer this looks like the CPU took its own sweet time GPUs are used in neural nets because of this ability to perform matrix multiplications so fast but why does this happen why or CPUs great with scalar multiplications and GPUs better with matrix multiplications three main reasons GPUs have a larger memory bandwidth they use parallelization and they have faster and more memory access than CPUs we're gonna delve into these three points in detail and then show some PI torch code in action that really demonstrates the difference in speed let's get started so here's a block diagram we have a system memory this is the main memory which contains the matrices that we need to multiply we have a CPU and a GPU and each of these also have their own memory that I'll just label as memory without the details for now let's make an analogy here the CPU is a Ferrari and the GPU is a truck case one performing scalar multiplication the CPU can get this data with a fetch operation to memory the amount of data that we can fetch though is pretty tiny but it's large enough to fetch floating-point numbers within a couple of trips and these fetches happen blazing fast like a Ferrari but GPUs fetch operation on the other hand can transmit a lot more data but they are not optimized for speed so they're like a truck they can take more time to fetch and process small chunks of data and that's why for scalar multiplications GPUs can lag behind CPUs case two when we multiply two large matrices CPU it has a super fast fetch operation but each fetch can only transmit a tiny amount of data to and from memory so it can take thousands of trips to fetch all the required data well one trip is being fetched the CPU memory is processed and freed up for the next inputs this is where GPUs work much better at a time we can fetch much larger amounts of data from the ram into the GPUs memory so the GPU doesn't need to make that many trips in more technical terms GPUs have a higher memory bandwidth than CPUs memory bandwidth is the amount of data that can be transmitted in a single trip to and from the memory and this is one of the main reasons GPUs have an edge over the CPUs for large matrix multiplication but even if we get a lot of packages or a lot of data the GPU processors remain idle for a while after all they're too fast and the truck is just too slow so instead of having a single truck we can have a fleet of trucks this way GPU processors do not need to wait around and it always has data to work with this notion of using a fleet of trucks is called parallelization coupling large memory bandwidth with parallelization reduces any time a GPU would be waiting so basically we fetch a lot of data and we do it fast but GPUs offer something more faster memory I use the term memory very vaguely before but what I'm really talking about are the caches and the registers GPUs have a similar structure but GPUs l1 and l2 caches are smaller in size than a CPUs l1 and l2 caches smaller size means that it can be accessed much faster and also these streamlined processors of a GPU have a bunch of registers which are super fast and GPUs have upwards of a thousand times more registers to play around with then CPUs so all of these memory enhancements together makes computations just blazing fast and it's a combination of these three points that GPUs are faster than CPUs from matrix multiplication especially for large matrices faster multiplication means that we can perform operations in deep learning also faster cool but how do we actually make use of GPUs as a programmer well this is where CUDA comes in from a programmers view CUDA provides an API that allows us to access the components of a GPU like the stream line processors the caches and the registers CUDA already provides a nice high level abstraction but deep learning frameworks like pi torch make life even easier we don't even need to know about the inner components of a GPU we just treat it as one big abstract unit called a GPU and we're all good to go let's actually see how matrix multiplication with GPUs perform I'm in Google Kolob and it's an environment that allows you to run your code in chunks and see the outputs directly I have some code cells here that we can walk through the first cell is a scalar multiplication with the CPU we're just taking the square of a number PI torch uses tensors as the fundamental building blocks and you can think of tensors as wrappers around your scalars and matrices for any dimension I'm just squaring a 1 cross 1 tensor here we're using a command called timeit to calculate the execution time of this block of code these are known as magic functions if you want to look them up for more reference it looks like the CPU took 5.26 microseconds to do this that's super fast in the second cell we have similar code but this time we multiply a 10,000 by 10,000 matrix with itself and this took eleven point eight seconds it probably seems like it it's taking longer when you actually run it but that's only because it's being executed three times for the rest of the code you need to set up a GPU so for this you got to go to the menu bar and click on run time then on the drop down you go to change run time type and from the hardware accelerator drop-down select GPU this initializes the GPU we now have to tell PI torch to use this GPU and we use torch device to get a reference to this GPU and this dot two device tells PI torch to store and process this variable Z using the GPU let's run the cell you okay so that took thirty five point two microseconds which was slower than the five point two six microseconds that we did using without the GPU so it was slower let's now run the large matrix multiplication with the GPU okay wow that took less than a second way faster than the 12 seconds it took before pretty slick right what I explained right here is only half the reason GPUs are blazing fast with matrix multiplication in the next video we're going to take a look at how we can change the original matrix multiplication algorithm for faster processing but that's all I've got for you now hope y'all enjoy what you saw click one of these cards to see some of my amazing work and I will see you very soon bye you
Original Description
Start with an analogy. Then delve into CUDA with some pytorch code to demonstrate why we use GPUs instead of just CPUs.
REFERENCES
[1] Why GPU’s work well in deep learning: https://www.quora.com/Why-are-GPUs-well-suited-to-deep-learning
[2] Matrix math on a GPU: http://www.ijcee.org/vol9/949-E1621.pdf
[3] GPUs are not necessarily faster than CPUs: https://www.quora.com/Why-are-GPUs-more-powerful-than-CPUs
[4] Code for difference: https://medium.com/analytics-vidhya/using-pytorch-and-cuda-for-large-computation-in-google-colabs-f1c026c17673
[5] GPU memory architecture: https://medium.com/@ashanpriyadarshana/cuda-gpu-memory-architecture-8c3ac644bd64
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CodeEmporium · CodeEmporium · 45 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
▶
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Linear Regression and Multiple Regression
CodeEmporium
Logistic Regression - THE MATH YOU SHOULD KNOW!
CodeEmporium
Generative Adversarial Networks - FUTURISTIC & FUN AI !
CodeEmporium
Deep Learning on the Cloud - GPU TO LEARN FASTER
CodeEmporium
Deep Mind's AlphaGo Zero - EXPLAINED
CodeEmporium
Mask Region based Convolution Neural Networks - EXPLAINED!
CodeEmporium
Attention in Neural Networks
CodeEmporium
Depthwise Separable Convolution - A FASTER CONVOLUTION!
CodeEmporium
One Neural network learns EVERYTHING ?!
CodeEmporium
Neural Voice Cloning
CodeEmporium
AI creates Image Classifiers…by DRAWING?
CodeEmporium
Unpaired Image-Image Translation using CycleGANs
CodeEmporium
K-Means Clustering - EXPLAINED!
CodeEmporium
Random Forest Classification
CodeEmporium
Data Science in Finance
CodeEmporium
Hypothesis testing with Applications in Data Science
CodeEmporium
A/B Testing - Simply Explained
CodeEmporium
The Kernel Trick - THE MATH YOU SHOULD KNOW!
CodeEmporium
Support Vector Machines - THE MATH YOU SHOULD KNOW
CodeEmporium
Principal Component Analysis (PCA) - THE MATH YOU SHOULD KNOW!
CodeEmporium
History of Calculus - Animated
CodeEmporium
Curiosity in AI
CodeEmporium
DropBlock - A BETTER DROPOUT for Neural Networks
CodeEmporium
Autoencoders - EXPLAINED
CodeEmporium
Recurrent Neural Networks - EXPLAINED!
CodeEmporium
LSTM Networks - EXPLAINED!
CodeEmporium
Building an Image Captioner with Neural Networks
CodeEmporium
10 Machine Learning Questions - ANSWERED!
CodeEmporium
How do neural networks work?
CodeEmporium
Evolution of Face Generation | Evolution of GANs
CodeEmporium
How does Google Translate's AI work?
CodeEmporium
How to keep up with AI research?
CodeEmporium
How does YouTube recommend videos? - AI EXPLAINED!
CodeEmporium
Variational Autoencoders - EXPLAINED!
CodeEmporium
Logistic Regression - VISUALIZED!
CodeEmporium
Gradient Descent - THE MATH YOU SHOULD KNOW
CodeEmporium
Boosting - EXPLAINED!
CodeEmporium
Transformer Neural Networks - EXPLAINED! (Attention is all you need)
CodeEmporium
Loss Functions - EXPLAINED!
CodeEmporium
Optimizers - EXPLAINED!
CodeEmporium
NLP with Neural Networks & Transformers
CodeEmporium
Batch Normalization - EXPLAINED!
CodeEmporium
Activation Functions - EXPLAINED!
CodeEmporium
Data Scientist Answers Interview Questions
CodeEmporium
Why use GPU with Neural Networks?
CodeEmporium
How do GPUs speed up Neural Network training?
CodeEmporium
BERT Neural Network - EXPLAINED!
CodeEmporium
ConvNets Scaled Efficiently
CodeEmporium
Transformer Neural Net makes music! (JukeboxAI)
CodeEmporium
What do filters of Convolution Neural Network learn?
CodeEmporium
We're hosting a Machine Learning Conference!
CodeEmporium
MLconfEU 2020: Machine Learning Conference for Software Engineers
CodeEmporium
Are Neural Networks Intelligent?
CodeEmporium
Time Series Forecasting with Machine Learning
CodeEmporium
Few Shot Learning - EXPLAINED!
CodeEmporium
How does a Data Scientist Fight FRAUD?
CodeEmporium
How would a Data Scientist analyze Customer Churn?
CodeEmporium
Expectations with Machine Learning
CodeEmporium
Why Logistic Regression DOESN'T return probabilities?!
CodeEmporium
How you SHOULD code Machine Learning
CodeEmporium
More on: Neural Network Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Want to get started with deep learning
Reddit r/deeplearning
Building a Deepfake Detector From Scratch — What Nobody Tells You
Medium · Deep Learning
Unfolding the Meandering Path: High-Dimensional Invariance and the Flat 2D Plane of Neural…
Medium · Deep Learning
Implementing Neural Style Transfer from Scratch: The Project That Started It All
Medium · Deep Learning
🎓
Tutor Explanation
DeepCamp AI