How To CONVERT LLMs into GPTQ Models in 10 Mins - Tutorial with ๐Ÿค— Transformers

1littlecoder ยท Beginner ยท๐Ÿง  Large Language Models ยท2y ago

Key Takeaways

This video tutorial demonstrates how to convert large language models (LLMs) into GPTQ models using the ๐Ÿค— Transformers library, with a focus on post-training quantization and inference using Hugging Face Model Hub.

Full Transcript

to convert a large language model that is in a pytorch file format or safe tensors format to a gptq quantized format this video tutorial is going to directly help you to do that typically people who do not have large gpus want to run large language models prefer a quantized model because quantized model would fit in less compute resource machines if you know the block is one of the most prolific quantized model releaser I've ever seen so in this video we're going to see a new feature that came with hugging face Transformers where the GPT queue has been integrated with Transformers making it easier for you to use gptq models and also making it easier for you to export gptq models or quantize the regular llm into gptq models let's go ahead and start with this entire code that I'm going to use in this video is from this blog post making llms lighter with Auto gptq and Transformers all credited to the team that put together this Google collab notebook I'll link the Google collab notebook in the YouTube description to make it easier for you to get started once you reach the Google collab notebook the first thing that you need to do is you need to click connect make sure that you have got GPU already available in this case we are going to use an Nvidia GPU a T4 machine that has approximately 15 gigs of vram the GPU Graphics memory once you have connected this then you have to install certain libraries I'm going to install Transformers pift accelerate and Optimum so install all the libraries click run away because it says this is the author who created this do not share any details that's fine click and install all the libraries the next thing that you have to do is you have to install auto gbtq until the next release of Auto gbtq you can you can build the library from The Source but after the latest release of Auto gbtq comes in you don't have to do that you can directly do pip install or gptq so let's click install this as well once you do that then the next step is for us to know realize what to do so one we can either quantize the model like a language model llm from scratch or the second option is for us to load the model that has been already quantized this video is particularly focused on quantizing a large language model but in the future I have plans to make a fine tuning as well fine tuning of a GPT EQ model for you to quantize a large language model from scratch the first thing that you need to know is the gptq quantization method is a post Training quantization Method so if you go here you can click this and then see the paper it's called post training quantization that means you are going to train you are going to quantize the model after the training has been already finished so there are certain nuances about how do you do that and that is why for gptq quantization you need data set so the gptq quantization happens by doing inferencing over the data set so you need to pass a data set that the quantizer will use to quantize the model in and itself so you can use any of these default data sets or you can you know just hard code the data as well but the quality of the data set that you give also reflects in the quality of The quantization Happening that's something that you need to keep in mind so here what we are going to do is we're going to use the C4 data set as the data set that we are going to pass into the quantization configuration you can pause in any other data set that you have but just keep in mind if you hard code as well like you can have like a single line as well but then it has its own impact in the way the quantization has been happening so the better the data set is better the quantization or the lesser the accuracy degradation will be okay so how are we going to do the quantization the first step is from Transformers input Auto model for causal Element Auto tokenizer gptq config this is the new feature then you have to specify the model in itself in this case we are going to use the Facebook opt 125 million parameter model next we have to specify the quantization configuration what is the quantization that you want to do what is the Precision to which you want to bring down the quantization to it supports two bit quantization 4 bit quantization six bit quantization and 8-bit quantization and then you have to specify the data set in itself and then you can start the quantization let me run this the first thing is it's going to download the model after it downloads the model it is going to use the quantization configuration and it is going to create the quantized model as part of this tutorial we are also going to learn how to push this quantization quantized model into your hugging phase model Hub which means you can use it whenever you want to it to be used once the model has been downloaded once the quantization has been finished which would take approximately less than 5 minutes as you can see trained split has been done and the quantization has processed has been started at this point the quantization has been successfully done as you can see now let's run this and then see if the quantization has been proper so this is just to make sure whether the quantization as a process has been successful it does not validate whether the quantization inference process which means the model is performing good or not just to make sure whether the quantization has been done properly what we can do is we can look at the decoder layers and we can look at Q weight and Q zeros and then make sure they are of torch in 32 data type so if you see Q8 of torch in 32 data type and q0 of torch int 32 data type which is just here and you know that the quantization has been done properly once again just to click verify this does not make sure whether the quantization is of high quality this just ensures the process called quantization has been successfully done after this is done you can now do the inference on the quantities model we can use the same API so you can just call the tokenizer and you can give a text and then you can just expect it to return something in this case you're going to use the quantized model instead of the base model that we have downloaded the start so hello my name is this and then it gives you the response so that means the quantization has been successfully done you can go ahead they have also given you example of how you can hard code the quantization data set and then use it but if you happen to do that then the quality of the quantization may not be highly good so let's try one more thing question write a joke about Mac computers answer so this is if you know this is not a chat model that's why I'm giving this in this particular format and you can see that it did not give us anything in there anyways the point here is that the model works so the next thing that we are going to do is after the quantization is done we are going to upload the quantized model into our hugging phase modeler for us to use it later on whenever we want the first thing we need to do is we need to authenticate our notebook which means we need to go here and then use notebook login click this this will open up a box and here you have to add your hugging phase token to get your hugging phase token go to your hugging phase account and click the profile icon and click settings once you click settings you can go here and find the token access tokens and copy one of the tokens that are available here come back to the Google collab notebook add it here and then click login once you click login it says login is successful token is valid the permission is right now all you have to do is you have to push the model to the hub so the model name whatever name you want to give you can give and then just run it and this will push the quantized gptq 4-bit quantized model into hugging phase model of which you can use later on whenever we want to use it so model has been pushed here we can click this that will open the link here and you can see the model has been pushed so this is opt 125 million parameter gpdq 4-bit model under one little coder and that is where the model is available now to quickly use the model that we just created so you can go here and then select the model that you want like for example in this case if this is the model that we have used for example you can go here copy this link come back to the Google collab notebook select that model here or you can use the block if you want or in in our case if you want to use the model that we pushed then you can just simply load it nothing else you can simply use it with model tokenizer and just simply load it using the same way you load any large language model from hugging phase modeler once the model has been loaded now all you have to do is do some text generation just check the model in itself you can check the model and the next thing that you can do is you can just start load creating the text let me run this hello my name is something something and then run this and this is going to create okay hello my name is Kari I'm a student of University of California San Diego and it just repeats a bunch of things because we have specified to create a lot of tokens and again this is not the best model that we have got um if you happen to use one of the latest state of the art model then this might have a much better result they've got the code for quantizing or you train fine-tuning a contest model as well I'm going to make a separate tutorial but in case if you want to use it before I make the tutorial if you do not need a tutorial in itself you can go ahead and then start using it in the same Google collab notebook which I'll link it in the YouTube description so to quickly summarize in this video we learned how to quantize a large language model into gptq models and we also learned how to make it into different precisions like two bit four bit six bit eight bit and we also learned about how to push the model finally to the hugging phase model Hub and finally we also learned how to use the model for inference if we have already pushed the model to hogging phase model I hope this tutorial was helpful to you if you have any question let me know in the comment section otherwise Happy prompting

Original Description

In this tutorial, You'll learn everything from: 1. Converting a Pytorch LLM into GPTQ Models 2. Push the newly created GPTQ Models to HF Transformers 3. Load the GPTQ models into Hugging Face Transformers for Inference Blogpost https://huggingface.co/blog/gptq-integration Colab https://colab.research.google.com/drive/1_TIrmuKOFhuRRiTWN94iLKUFu6ZX4ceb?usp=sharing โค๏ธ If you want to support the channel โค๏ธ Support here: Patreon - https://www.patreon.com/1littlecoder/ Ko-Fi - https://ko-fi.com/1littlecoder
Watch on YouTube โ†— (saves to browser)
Sign in to unlock AI tutor explanation ยท โšก30

Playlist

Uploads from 1littlecoder ยท 1littlecoder ยท 0 of 60

โ† Previous Next โ†’
1 How to create your Free Data Science Blog on Github with Fastpages from Fastai
How to create your Free Data Science Blog on Github with Fastpages from Fastai
1littlecoder
2 Making Interactive Matplotlib Plots for Data Science Visualizations on Jupyter (Python)
Making Interactive Matplotlib Plots for Data Science Visualizations on Jupyter (Python)
1littlecoder
3 Create your first Data Science Web App using R Shiny
Create your first Data Science Web App using R Shiny
1littlecoder
4 How to create a Reproducible Example in R using reprex
How to create a Reproducible Example in R using reprex
1littlecoder
5 No Code Visualization using esquisse with Tableau-like Drag and Drop GUI in R
No Code Visualization using esquisse with Tableau-like Drag and Drop GUI in R
1littlecoder
6 Scrape HTML Table using rvest and Process them for insights using tidyverse in R
Scrape HTML Table using rvest and Process them for insights using tidyverse in R
1littlecoder
7 Google Teachable Machine Learning Build No Code AI solution
Google Teachable Machine Learning Build No Code AI solution
1littlecoder
8 Create meaningful fake tidy datasets in R using fakir [#rstats Package]
Create meaningful fake tidy datasets in R using fakir [#rstats Package]
1littlecoder
9 How to enable using R Programming with Visual Studio VS Code
How to enable using R Programming with Visual Studio VS Code
1littlecoder
10 Python, Community, Books - with Abhiram R - Bangpypers Co-organizers | 1littlecoder podcast
Python, Community, Books - with Abhiram R - Bangpypers Co-organizers | 1littlecoder podcast
1littlecoder
11 Growing a Tech Community across India - Anubha Maneshwar, Founder Girlscript | 1littlecoder Podcast
Growing a Tech Community across India - Anubha Maneshwar, Founder Girlscript | 1littlecoder Podcast
1littlecoder
12 Intro to Google Colab - How to use Colab
Intro to Google Colab - How to use Colab
1littlecoder
13 Intro to Plotly Express - Complex Interactive Charts with One-Line of Python Code
Intro to Plotly Express - Complex Interactive Charts with One-Line of Python Code
1littlecoder
14 Indic NLP Python Toolkit Open Source Development - iNLTK Creator Gaurav Arora | 1littlecoder Podcast
Indic NLP Python Toolkit Open Source Development - iNLTK Creator Gaurav Arora | 1littlecoder Podcast
1littlecoder
15 Do you want a career in Data Science - Tamil Webinar
Do you want a career in Data Science - Tamil Webinar
1littlecoder
16 Android Smartphone Analysis in R [Live Coding Screencast]
Android Smartphone Analysis in R [Live Coding Screencast]
1littlecoder
17 Programmatically create Images, Memes, Watermarks using Python with imgmaker
Programmatically create Images, Memes, Watermarks using Python with imgmaker
1littlecoder
18 Kaggle Walkthrough to get you started with Data Science - Webinar
Kaggle Walkthrough to get you started with Data Science - Webinar
1littlecoder
19 Community, Corporate Job, Coding - Gnana Lakshmi T C aka Gyan, WomenWhoCode Leadership Fellow
Community, Corporate Job, Coding - Gnana Lakshmi T C aka Gyan, WomenWhoCode Leadership Fellow
1littlecoder
20 Easy ggplot2 Theme Customization with {ggeasy} | Data Visualization in R
Easy ggplot2 Theme Customization with {ggeasy} | Data Visualization in R
1littlecoder
21 Excel to R - Pivot + Bar Chart in Excel  & R using tidyverse [Live Coding]
Excel to R - Pivot + Bar Chart in Excel & R using tidyverse [Live Coding]
1littlecoder
22 Excel to R #2 - VLOOKUP in Excel to LEFT_JOIN, MERGE in R
Excel to R #2 - VLOOKUP in Excel to LEFT_JOIN, MERGE in R
1littlecoder
23 5 websites to get Free Real-World Datasets for Data Science/ML Projects
5 websites to get Free Real-World Datasets for Data Science/ML Projects
1littlecoder
24 Excel to R #3 - APPROXIMATE VLOOKUP in Excel to FUZZY LEFT_JOIN in R
Excel to R #3 - APPROXIMATE VLOOKUP in Excel to FUZZY LEFT_JOIN in R
1littlecoder
25 Correlation-alternative PPS (Predictive Power Score) Python Package Demo
Correlation-alternative PPS (Predictive Power Score) Python Package Demo
1littlecoder
26 Automated Website Screenshots in R using {webshot}
Automated Website Screenshots in R using {webshot}
1littlecoder
27 Installing Custom RStudio Theme (Synthwave85)
Installing Custom RStudio Theme (Synthwave85)
1littlecoder
28 Analyse Google Trends Search Data in R using {gtrendsR}
Analyse Google Trends Search Data in R using {gtrendsR}
1littlecoder
29 3 Tips to ask question on Stack Overflow the right way to get answers
3 Tips to ask question on Stack Overflow the right way to get answers
1littlecoder
30 Learn Data Science with R - Mini Projects - Web Scraping Zomato
Learn Data Science with R - Mini Projects - Web Scraping Zomato
1littlecoder
31 Easily make Dumbbell Chart using {ggcharts} | Data Visualization in R
Easily make Dumbbell Chart using {ggcharts} | Data Visualization in R
1littlecoder
32 GET Hackernews Front Page Results using REST API in R
GET Hackernews Front Page Results using REST API in R
1littlecoder
33 Quickly deploy ML WebApps from Google Colab using ngrok
Quickly deploy ML WebApps from Google Colab using ngrok
1littlecoder
34 Use Jupyter Notebooks within VSCode (Visual Studio Code) in 2020
Use Jupyter Notebooks within VSCode (Visual Studio Code) in 2020
1littlecoder
35 Plotly Interactive Plots as Pandas Plotting Backend df.plot()
Plotly Interactive Plots as Pandas Plotting Backend df.plot()
1littlecoder
36 Stack Overflow Developer Survey 2020 Highlights for New Programmers
Stack Overflow Developer Survey 2020 Highlights for New Programmers
1littlecoder
37 Matplotlib Animation Charts in Python using Celluloid
Matplotlib Animation Charts in Python using Celluloid
1littlecoder
38 Coding, Postwoman, Passion Project Book - Liyas Thomas Open Source Developer - 1littlecoder podcast
Coding, Postwoman, Passion Project Book - Liyas Thomas Open Source Developer - 1littlecoder podcast
1littlecoder
39 Aspiring Data Scientist, Tips on How to learn Business Domain Knowledge
Aspiring Data Scientist, Tips on How to learn Business Domain Knowledge
1littlecoder
40 Bokeh Interactive Charts as Pandas Plotting Backend df.plot_bokeh()
Bokeh Interactive Charts as Pandas Plotting Backend df.plot_bokeh()
1littlecoder
41 Easy Fast Python Pandas Summary with Sidetable | Pandas Tips & Tricks
Easy Fast Python Pandas Summary with Sidetable | Pandas Tips & Tricks
1littlecoder
42 Inception, Content Ideas, Consistency - Srivatsan Srinivasan AIEngineering YouTube Content Creator
Inception, Content Ideas, Consistency - Srivatsan Srinivasan AIEngineering YouTube Content Creator
1littlecoder
43 ggplot2 Text Customization with ggtext | Data Visualization in R
ggplot2 Text Customization with ggtext | Data Visualization in R
1littlecoder
44 Penguins Dataset Overview - iris alternative | EDA Data Visualization in R
Penguins Dataset Overview - iris alternative | EDA Data Visualization in R
1littlecoder
45 YouTube Growth Tips, Content Creation - Bhavesh Bhatt, YouTuber (Data Science & Machine Learning) #7
YouTube Growth Tips, Content Creation - Bhavesh Bhatt, YouTuber (Data Science & Machine Learning) #7
1littlecoder
46 Matplotlib Animated Bar Chart Race in Python | Data Visualization
Matplotlib Animated Bar Chart Race in Python | Data Visualization
1littlecoder
47 Simple Python GUI Development using {guietta}
Simple Python GUI Development using {guietta}
1littlecoder
48 #8 Niche, Growth, Monetization - David Langer - YouTuber Dave on Data
#8 Niche, Growth, Monetization - David Langer - YouTuber Dave on Data
1littlecoder
49 Simple Fast 3-step Python OCR using Deep Learning 40+ Languages
Simple Fast 3-step Python OCR using Deep Learning 40+ Languages
1littlecoder
50 Github New Feature Profile Summary/Mini-Resume - Profile Views
Github New Feature Profile Summary/Mini-Resume - Profile Views
1littlecoder
51 Otto ML Assistant, GPT-3 on Philosophers, Nvidia-ARM - 3 ML Tech News
Otto ML Assistant, GPT-3 on Philosophers, Nvidia-ARM - 3 ML Tech News
1littlecoder
52 What is OpenAI GPT-3 - Hype, Examples, Worries
What is OpenAI GPT-3 - Hype, Examples, Worries
1littlecoder
53 Julia 1.5, Datamuse API, Live HDR+ Pixel 4a - Machine Learning Tech News
Julia 1.5, Datamuse API, Live HDR+ Pixel 4a - Machine Learning Tech News
1littlecoder
54 Self-driving Car Engineer sentenced, arXiv Dataset, AI/ML Startup Idea - Machine Learning Tech News
Self-driving Car Engineer sentenced, arXiv Dataset, AI/ML Startup Idea - Machine Learning Tech News
1littlecoder
55 GPT-3 Explorer, Ciphey (Automated Decryption), Py-Sudoku - ML Tech News
GPT-3 Explorer, Ciphey (Automated Decryption), Py-Sudoku - ML Tech News
1littlecoder
56 How to use Advanced Google Search to extract Email Ids from Linkedin
How to use Advanced Google Search to extract Email Ids from Linkedin
1littlecoder
57 Cartoonizer Toon-IT (AI Web App), GPT-3 Advice, Android Earthquake Detection - ML Tech News
Cartoonizer Toon-IT (AI Web App), GPT-3 Advice, Android Earthquake Detection - ML Tech News
1littlecoder
58 Flow - R Package to visualize code logic, functions as a Flow Diagram
Flow - R Package to visualize code logic, functions as a Flow Diagram
1littlecoder
59 Build GPT-3-like Language Model on Google Colab with minGPT [PyTorch]
Build GPT-3-like Language Model on Google Colab with minGPT [PyTorch]
1littlecoder
60 Create a Pencil Sketch Portrait with Python OpenCV
Create a Pencil Sketch Portrait with Python OpenCV
1littlecoder

This video tutorial teaches how to convert LLMs to GPTQ models using ๐Ÿค— Transformers and perform post-training quantization, with a focus on inference using Hugging Face Model Hub. The tutorial covers the entire process, from installing required libraries to loading the quantized model for text generation.

Key Takeaways
  1. Install ๐Ÿค— Transformers, Auto gptq, Optimum, and pifft accelerate libraries
  2. Connect to GPU and install libraries
  3. Quantize large language model from scratch using GPTQ
  4. Download the Facebook opt 125 million parameter model
  5. Use the gptq config feature for quantization
  6. Create a quantized model in under 5 minutes
  7. Verify the quantization by checking the decoder layers and Q weights
  8. Push the quantized GPTQ model to Hugging Face Model Hub
  9. Load the quantized model from Hugging Face Model Hub for inference
  10. Use the model for text generation with tokenizer and model
๐Ÿ’ก The ๐Ÿค— Transformers library provides a straightforward way to convert LLMs to GPTQ models using post-training quantization, enabling efficient inference and deployment of large language models.
๐Ÿ”’ Pro feature: Ask AI to explain this lesson โ†’

Related Reads

๐Ÿ“ฐ
Top AI Papers on Hugging Face - 2026-07-15
Explore the top AI papers on Hugging Face, focusing on agent longevity, robotics, and efficient model training methods
Dev.to AI
๐Ÿ“ฐ
Integrating Open-Weight LLMs as Drop-In API Replacements: A Practical Guide
Learn to integrate open-weight LLMs as drop-in API replacements for a vendor-locked-in free solution
Dev.to AI
๐Ÿ“ฐ
How I Built a Multi-Page AI Website Generator for Nigerian SMBs โ€” Architecture, LLM Prompting, and Lessons Learned
Learn how to build a multi-page AI website generator for small businesses using LLM prompting and key architectural decisions
Dev.to ยท Innocent Oyebode
๐Ÿ“ฐ
The Token Tax: Why You Are Paying for How AI โ€œThinks,โ€ Not What It Writes
Understand the token tax and its impact on AI budgeting to optimize LLM API integration and reduce costs
Medium ยท AI
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch โ†’