Pandas Dataframes on your GPU w/ CuDF

sentdex · Beginner ·📐 ML Fundamentals ·2y ago

Key Takeaways

The video demonstrates the use of CuDF, a Pandas accelerator, to achieve GPU acceleration of Pandas dataframes, resulting in significant performance improvements compared to regular Pandas. The video showcases the ease of use of CuDF, with a mere module flag enabling GPU acceleration, and highlights its ability to handle string and float operations on dataframes.

Full Transcript

I of course like many packages in libraries in Python but probably my favorite package of all time is pandas I love it because it's just plain super easy to use and makes sense for a lot of my workflows in data science that said I also like machine learning and data sets can often get quite large for example we'll play with the data set today from kaggle of UK property prices with over 28 million rows of data using vanilla pandas for this despite how awesome it really is as a library and how efficient it is out of the box arguably uh it would and will be very slow in the past I've simply employed the skill of patience for this problem but today I'd like to show you the pandas accelerator from nvidia's Rapids Arsenal using KF which is a GPU accelerated data frame Library historically this library has been a bit of a challenge to deploy because if you wanted to use CDF you needed to completely adopt qdf and make sure every operation that you performed in your program was function within CF and given the scope of operations even in a simple project this made CF almost unusable in practice no matter how fast it ran for certain specific commands and then even if you did have full and perfect coverage you'd still have to refactor all of your code which no Dev really wants to do and then you'd probably have to maintain separate branches and all that nobody wants that instead what Nvidia has done is introduced a way to deploy C with a mere module flag when running the script or through an extension load in a notebook and that's it there's no other change necessary with a slight asterisk of maybe sometimes being explicit with data types uh otherwise you might run into some problems and this came up in my testing and I'll share with you all what I ran across U but at least in my testing so far this is basically fair to say a dropin functionality replacement that you should could absolutely be using right now today and from now on with your Panda data frames this also includes even if you just so happen to be using a library that also uses pandas itself because it will also accelerate that too in order to install it's just a pip install cf- q11 or q12 for the Cuda version that you're running so let's see just how much performance we can get with a mere flag I'm going to run the CF pandas accelerator on the left and then regular pandas on the right we could use the kind of time it magic and all of that but this does weird things with variables and is really only useful for getting a really solid concrete sort of average time um so my test here isn't going to be the most perfect of timing tests but it's a real comparison nonetheless and the kind of the difference between CDF and regular pandas is on the orders of like magnitudes right so so um we're not going to have um an issue with a ten of a second here and there there so anyways uh just know that this is basically it's just one pass through it's not the most perfect comparison but it's good enough because the difference in performance here is massive so the first sell in this notebook is just loading the accelerator for CF and then I just have like a comment for the other one which is our regular pandas between the two notebook files this is actually the only difference these are otherwise identical notebooks uh so next what we're going to do is we're going to load the data into a data frame note that I'm explicitly setting the data type for price as int64 and this is what I was talking about earlier the reason for this is in the data set itself in that CSV file it has these prices in in quotes like a string pandas with regular old python actually still magically handles for this and allows us to perform math operations on strings as if they were ins um or even floats and this is just I don't know some magic going on in the back end of of pandas to kind of handle for this when people do this um but but if you are having a problem with like in Infinity numbers and stuff like that you'll want to go ahead and look at your data set and see if it's doing this cuz in my opinion this isn't really I'm not sure this is something I would expect anybody to handle for even pandas but just keep in mind that this could be a thing in your data set that panas is just somehow magically handling for but anyways you're going to want to either make sure your data set doesn't do this or you can explicitly set data types to handle for it just in case it does as you can see even just loading the data frame into memory sees a mass M boost of a mirror only like 2.9 seconds to load with qdf compared to almost a minute with regular pandas the first time I ran through with CF I actually found that both took quite a bit of time to load after some debugging I realized that it was actually me loading from my Nas rather than from like a local storage on that machine so network storage versus local storage so just in case you're as dense as I am uh make sure your data is actually local so in this case it's on an nvme so it's much much faster to load instead of of loading over Network once it's in memory though it's kind of irrelevant where the data set started from so it's all kind of a wash from there okay so from this point we can do a quick operation just to make sure things are working as intended and we'll get the length of the data frame which looks good and is as expected so then what we can do is grab all of the unique towns and cities interestingly enough there is a slight Delta here in favor of regular CPU pandas the reason here is likely this is a CPU only oper ation so then you're paying for some data transfer and overhead going CPU back to GPU and I think that's the difference that said you're about to see why this difference is insignificant and irrelevant but you might also wonder what if you did a larger amount of these unique operations I don't think I've ever done a unique call in a loop but what if you did since I already had to redo this video once I went ahead and just did a quick test for this particular operation I looped through just over all of the columns and grabbed uniques from each of them and it's still just a CPU only operation but you can clearly see uh it's not like CPU to GPU transfer is actually causing a 2X slowdown if you were to need to scale out a unique operation for any reason um and actually it even looks like the kdf version was slightly faster for this run that's probably just by chance if like I I was saying before if we use the time it module for like little tiny differences like this we might find that either they basically identical or maybe sometimes the the CPU is faster or whatever but anyway I just wanted to further poke into this and make sure that the overhead was actually as irrelevant as I was pretty sure it was and in this case it definitely is so now what we're going to do is get the average home price for each unique town or city this operation takes qdf 10 and 1 12 seconds for regular pandas it takes 19 minutes uh 10 seconds versus 19 minutes is absolutely insane and again this is a zero effort switch if you already have an Nvidia GPU you've already got Cuda all that it's it you just install KF you call the flag and boom you're done the same code also can run on the GPU if it's available or it could be CPU only so you don't even need two separate code bases continuing on what we're going to do is we'll slice out just the year 2022 from this data frame and get just pricing information for that year surprisingly to me this operation was also orders of magnitude faster with KF than without this is surprising because these dates were not even read in as daytime objects or anything fancy at all they're actually simply strings and the actual operation that per we're performing here to do that slice is actually just a string dot starts withth so this means there's actually some fancy kouf GPU method for string operations which is particularly impressive and something that I did not at least personally expect to see here and that brings us to the profiler this lets us see exactly what's happening either closer to the qdf backend showing us the operations running on cdf's side of things and what device it's running on GPU or CPU and then how long that's taking alternatively we can also use the line profiler to show us sort of line by line what's running on the CPU or GPU and how long that's taking so here we can see both the line profile and just regular profile on that data frame slice line profile shows us that actually that line of code is indeed done entirely on the GPU we can then use the profiler to see what KF is actually doing and we can see that there's a get item method and then that we do indeed have string Methods which includes some fancy starts withth functionality allowing this to run entirely on the GPU despite the fact that it's just string operations which is particularly awesome next maybe we want to calculate the upper and lower 20% of home prices to discover areas with the most variance so here's a slightly longer bit of code we can again see that CDF is considerably faster here by about five times and we can also do a line profile here to see what's going on finally from here after pulling all this kind of data we might do something like some pure python code to grab ratios for high and low as well as maybe get an actual range and then kind of display maybe where are the biggest variants and stuff like that so then finally just a couple of quick common commands like finding the max for a column and a sort to just show off some more operations but I really encourage you to install CF and just use it on your own projects and stuff like that just to see how much faster it is and see what else might even just surprise you cuz like I said I I had seen some of the demos but then there were things that I just did not expect to be the case and that were definitely the case and and that was pretty cool to see so anyway uh you can try the pandas uh accelerator yourself yourself via CDF by installing with Pip install cf- q11 or Q2 also of note you can run scripts too so obviously not just notebooks and the way that you can do this is with just the kf. pandas module flag when you go to run that actual script and you'll get all the same performance speed UPS so that's all I have to show for now this pandas accelerator from KF is freaking awesome I love how darn simple the implementation of it really is and I look forward to seeing how this like rapid stuff just evolves over time this is been many years in the making and this is oh such a cool update to see so if you have an Nvidia GPU and you find yourself using pandas definitely try it out let me know what you think uh if you find anything in particularly interesting or if you're having a hard time with something leave a comment below um I really was just pretty surprised at just how fast everything actually is how well everything just simply works if there isn't something it just falls back to the CPU but like I said there was more coverage for GPU operations than I even expected like the string Methods and that was just super super cool to see I'll leave uh pip install commands and an example of running the script via the terminal in the description there's also an official collab notebook example from the Rapids team that I will put in there as well and then finally there was an AI data science Summit from Nvidia very recently that also showcased this uh Panda's accelerator which you can also watch I'll put a link to that in the description as well anyway I hope you all have enjoyed and learned a cool new tool I'm definitely going to be using this personally from now on and I will see you all in a another video

Original Description

An overview and some quick examples of using CuDF's Pandas accelerator and how much faster it can be than vanilla Pandas for data analysis. Colab demo of Rapids: https://nvda.ws/3LWggQj AI and Data Science Virtual Summit: https://nvda.ws/3ZR3wjL Notebook in this video: https://gist.github.com/Sentdex/469c30385d06719519af13125db85edc Install CuDF: pip install cudf-cu11 --extra-index-url=https://pypi.nvidia.com (or cu12) Neural Networks from Scratch book: https://nnfs.io Channel membership: https://www.youtube.com/channel/UCfzlCWGWYyIQ0aLC5w48gBQ/join Discord: https://discord.gg/sentdex Reddit: https://www.reddit.com/r/sentdex/ Support the content: https://pythonprogramming.net/support-donate/ Twitter: https://twitter.com/sentdex Instagram: https://instagram.com/sentdex Facebook: https://www.facebook.com/pythonprogramming.net/ Twitch: https://www.twitch.tv/sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video teaches how to use CuDF to accelerate Pandas dataframes on a GPU, resulting in significant performance improvements. The video provides a comprehensive overview of CuDF's capabilities and demonstrates its ease of use.

Key Takeaways
  1. Load the accelerator for CuDF
  2. Load the data into a dataframe with explicit data type setting
  3. Load data into memory with CuDF
  4. Get the length of the dataframe with CuDF
  5. Grab all unique towns and cities with CuDF
  6. Get the average home price for each unique town or city with CuDF
  7. Slice out a specific year from the dataframe with CuDF
  8. Use the profiler to see exactly what's happening on CDF's side of things
💡 CuDF can achieve significant performance improvements compared to regular Pandas, with a mere module flag enabling GPU acceleration, making it an attractive option for data scientists and analysts.

Related Reads

📰
Introduction Data Science and Machine Learning
Learn the basics of data science and machine learning to stay ahead in the emerging field
Medium · Data Science
📰
AgriScore: An Explainable AI Credit Scoring System for Smallholder Farmers
Learn how AgriScore uses explainable AI for credit scoring to help smallholder farmers access loans, and why this matters for financial inclusion
Medium · Machine Learning
📰
AgriScore: An Explainable AI Credit Scoring System for Smallholder Farmers
Learn how AgriScore uses explainable AI for credit scoring of smallholder farmers, and how to build a similar system using machine learning and Streamlit
Medium · Data Science
📰
The Sophistication Trap: Why the Smarter AI Technique Keeps Losing
Smarter AI techniques don't always guarantee better results, and understanding the sophistication trap can help you optimize your approach
Medium · Machine Learning
Up next
How to start learning AI | Complete AI Learning Path | Roadmap For Beginners (With No Background)
Career Talk
Watch →