CuPy is NumPy on The GPU

NeuralNine · Intermediate ·💻 AI-Assisted Coding ·1y ago

Key Takeaways

CuPy is utilized to run NumPy on the GPU, enhancing calculation speeds. The video explores CuPy's capabilities and applications.

Full Transcript

what is going on guys welcome back in this video today we're going to take a look at CPI which is a python package that allows us to use numpy on the GPU and thus allows us to speed up certain calculations so let us get right into it [Music] not all right so we're going to take a look at Cai in this video today now as I already mentioned coui is a python library that allows us to use numpy on the GPU and it can lead to certain performance increases to speed ups when it comes to certain calculations and operations it's based on Cuda and what I want to do in this video today is I want to show you how to use it how to use cpai on a basic level how to use it like numpy how to take numpy arrays and move them through the GPU do calculations move them back to the CPU and then I want to move on to the performance comparison I want to show you how much faster cpai is in certain situations but also that it's not necessarily always faster especially when you consider all parts that are involved in calculations so we're going to explore that you can see I have a bunch of images here and visualization scripts that I ran before recording this video I want to explore the results there but first of all I want to show you how to use cpy so the first thing is we want to open up a command line and we want to install cpai so pip or pip 3 install cpy you can either do it like this or you can say install ci- Cuda and a specific version so ci- Cuda 11x for example or cy- Cuda 12x now interestingly on my system I'm using Cuda 12 but I still have to use Cuda 11x I don't know why but the package doesn't work with Cuda 12x so I don't know why that's the case the best thing is you try out for yourself which one works either just cpai cpai Cuda 11 cpai Cuda 12 you can see what Cuda version you have by running the nvidia-smi command at least on um Linux you can see I have Cuda 12 but I still use C CI Cuda 11 because the other one doesn't work now once you have CPI installed you can import it by saying import CI as CP which is quite similar to numpy as NP and the interesting thing about CPI is you can basically use it like numpy so you can go ahead and say array equals cp. array and that's the same thing sorry that's the same thing as running num. array or np. array and if you have autoc completion you can type CP Dot and you can see that basically you have all the numpy functions available so that's one way to use uh Cy by just using it like numpy another way to use it is to take a numpy array like this one here and move it to the GPU turn it into a cpai array so to say by using the as array function so I can say C array or actually let's call it GPU array is equal to CP as array array that basically moves the array to the GPU and you can do then matrix multiplication or whatever you want to do and then you can take the result whatever that result is which is going to be a GPU array and we can turn it back to a CPU array by saying whatever the array is as numpy so or actually CP as numpy and we pass the GPU array in here that turns it back into an umpi array and uh moves it back to the CPU that is already what you need to know about this the rest is going to be a performance comparison so what I have here is three visualization scripts which are slightly different uh it's not too important to read through all of the code if you don't if you don't want to uh but basically what we're doing here is we're generating random numpy arrays um of a pretty high dimension so these are matrices n * n uh we start with a th we go up until 11,000 with a step size of a th000 so we do 1,000 2,000 3,000 and so on um and what we do then is we move these arrays to the GPU we perform a matrix multiplication using the dot method here or the dot function uh we do this one time with npy and one time with qy and then we compare the time it takes to do that now what you probably noticed here is this warm-up function now this warmup function I have my Mals open up great uh this warmup function is basically a very simple operation just to get CPI going because the first time you use cie in a script it's going to take some time to initialize if you don't do a warm-up um to initialize coupie you're going to skew the results so I'm going to show you the result of this this is what you get if you do the warmup you have the numpy time or actually the numpy time is the the one so how long it takes in seconds to do a matrix multiplication with these Dimensions here or with this size here uh you can see that it grows quite a bit and we have more than 8 seconds when we get to the 10,000 size um and for cpai it's basically 0 seconds almost so it's instantaneous uh doesn't matter what size we have here it's very very very fast um if I don't do the warmup you're going to see something like this so it's basically the same result but the first time we do it it takes some time to initialize like around 1 second and because of that it seems in the beginning that numpy is faster but this is only because of the initialization the very first time you run it and then it basically doesn't have this overhead anymore and you get the same result as before um another thing here is or one thing that you might want to consider is in the first visualization here uh what I do is I convert the numpy array to to a coupie array but I don't consider that as work so moving the array from the CPU to the GPU is not measured um In the comparison here which of course can be a problem because it's not faster if it takes super long to take a npy array and move it to the GPU to turn it into a coupy array so what I did then is I included that as the measurement here so I did the same multiplication uh or I did the same matrix multiplication but I then also included the time um that it takes to get the two arrays to the GPU and get them back from the GPU to the CPU and when you look at that you can see that the speed up is actually not there anymore because cpai is now slower than numpy because of this uh moving from the CPU to the GPU and back however this is only the case if you don't do a lot of work on the GPU so for example if of course if the time that it takes to move an array or multip AR raay from the CPU to the GPU is longer than the work itself then it doesn't make a lot of sense but if you have to do a lot of work if you do a lot of complicated things on the GPU and the transmission of the result is just one array being transmitted it can be faster so if you have a lot of work to do on a GPU and very little data to transmit it makes sense to use cie and this is what you can see now in the last image here after a certain size with a certain amount of calculations cpai is still way faster than numpy and the higher the problem size uh the less you have to transmit the less you have to convert from CPU to GPU um the more important this effect is going to be so the faster coupon is going to be if you do a lot of work on the GPU but that is basically what cpai does is it allows you to use the GPU for numpy operations specifically for Matrix multiplications this is very very useful because the gpus are optimized for that because of parallel Computing um and and yeah this is something that you can use intelligently to speed up your numpy calculations so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye

Original Description

In this video, we take a look at CuPy, which allows us to use NumPy on the GPU and to speed up certain calculations. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 💼 Services 💼 💻 Freelancing & Tutoring: https://www.neuralnine.com/services 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video introduces CuPy, a library that enables NumPy to run on the GPU, and demonstrates how to leverage it for faster calculations. By using CuPy, developers can significantly accelerate numerical computations. The video provides a practical overview of CuPy's applications and benefits.

Key Takeaways
  1. Install CuPy
  2. Import CuPy in Python
  3. Convert NumPy code to CuPy
  4. Run calculations on the GPU
  5. Compare performance with NumPy
💡 CuPy can substantially speed up numerical computations by running them on the GPU, making it a valuable tool for AI and scientific computing applications.

Related Reads

Up next
99% of People Still Don't Understand Vibe Coding
Vskills Certification
Watch →