Sparse Transformers | A Demo
Last Updated on January 6, 2023 by Editorial Team
Last Updated on April 18, 2022 by Editorial Team
Author(s): Ricky Costa
Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.
How fast can BERT go with sparsity?
Hereβs a LittleΒ Secret:
If you want to analyze how fast 19 sparse BERT models perform inference, youβll only need a YAML file and 16GB of RAM to find out. And spoilerΒ alert:
β¦ they run onΒ CPUs.
β¦ and theyβre superΒ fast!
The latest feature from Neural Magicβs DeepSparse repo is the DeepSparse Server! And the objective of this article is to show not only how seamless it is to serve up to 19 sparse BERT models, but how much the impact of sparsity has on model performance. For a bit of background, sparsification is the process of taking a trained deep learning model and removing redundant information from the over-parameterized network resulting in a faster and smaller model. And for this demo, weβll be using various BERT models and loading them for inference to show the trade-off between accuracy and speed relative to the modelβs sparsification.
The DeepSparse Server is built on top of our DeepSparse Engine and the popular FastAPI web framework allowing anyone to deploy sparse models in production with GPU-class speed but on CPUs! With the DeepSparse Engine, we can integrate into popular deep learning libraries (e.g., Hugging Face, Ultralytics) allowing you to deploy sparse models withΒ ONNX.
As previously mentioned, all of the configuration required to run your models in production only requires a YAML file and a small bit of memory (thanks to sparsity). To get quickly started with serving four BERT models trained on the question answering task, this is what the config YAML file would lookΒ like:
If you want to go big and load all of the 19 Neural Magic sparse BERT models: this is what the config file would look likeΒ π:
For ease of use, weβve built a demo on top of Streamlit for anyone to demo the server and models for the question answering task in NLP. In order to test 19 models simultaneously, the app was tested on a virtual machine on the Google Cloud Platform.
To give some grounding on what I used for computing in my tests, here are theΒ deets:
Keep in mind that bare-metal machines will actually perform faster under the same computing constraints described in this article. However, since the models are already super fast, I feel comfortable showing their speed via virtualization.
We not only strongly encourage you to run the same tests on a VM for benchmarking performance but also so youβll have the RAM required to load all 19 BERTs into memory, otherwise youβll get thisΒ π:
If you prefer to get started quickly on a local machine without worrying about out-of-memory problems, you should try only loading a few models into memory. And the code below will show you how to do exactly this with 4 models (even though most sparse models are super light and you can possibly add more at your discretion).
Getting Started with the SparseServer.UI
We split our app into separate server and client directories. The server directory holds the YAML files for loading the models and the client has the logic for the Streamlit app:
~sparseserver-ui/
|__client/
|__app.py
|__pipelineclient.py
|__samples.py
|__settings.py
|__server/
|__big-config.yaml
|__config.yaml
|__requirements.txt
|__README.md
1. Clone the DeepSparse repo:
>>> git clone https://github.com/neuralmagic/deepsparse.git
2. Install the DeepSparse Server and Streamlit:
>>> cd deepsparse/examples/sparseserver-ui
>>> pip install -r requirements.txt
Before we run the server, you can configure the host and port parameters in our startup CLI command. If you choose to use the default settings, it will run the server on localhost and port 5543Β . For more info on the CLI arguments run:
>>> deepsparse.server --help
3. Run the DeepSparse Server:
Ok! Itβs time to serve all of the models defined in the config.yaml. This YAML file will download the four models from Neural Magicβs SparseZoo π¦Ύ.
>>> deepsparse.server --config_file server/config.yaml
After downloading the models and your server is up and running, open a second terminal to test out theΒ client.
β οΈ If you altered the host and port configuration when you first ran the server, please adjust these variables in the pipelineclient.py module asΒ well.
4. Run the Streamlit Client:
>>> streamlit run client/app.py --browser.serverAddress="localhost"
Thatβs it! Click on the URL in your terminal, and you are ready to start interacting with the demo. You can choose examples from a list, or you can add your own context and question.
In the future, weβll be expanding the number of NLP tasks outside of just question answering so you get a wider scope in performance with sparsity.
For the full code: check out the SparseServer.UIΒ β¦
β¦and donβt forget to give the DeepSparse repo aΒ GitHubβ!
- Ricky Costa| Neural MagicΒ π§
Sparse Transformers | A Demo was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.
Join thousands of data leaders on the AI newsletter. Itβs free, we donβt spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI