Add a cpu example of HuggingFace Transformers Agent (use vicuna-7b-v1.5) (#9284)

* Add examples of HF Agent

* Modify folder structure and add link of demo.jpg

* Fixes of readme

* Merge applications and Applications
This commit is contained in:
Zheng, Yi 2023-10-27 17:14:12 +08:00 committed by GitHub
parent aa319de5e8
commit a4a1dec064
13 changed files with 120 additions and 0 deletions

View file

@ -0,0 +1,68 @@
# BigDL-LLM Transformers INT4 Optimization for HuggingFace Transformers Agent
In this example, we apply low-bit optimizations to [HuggingFace Transformers Agents](https://huggingface.co/docs/transformers/transformers_agents) using BigDL-LLM, which allows LLMs to use tools such as image generation, image captioning, text summarization, etc.
For illustration purposes, we utilize the [lmsys/vicuna-7b-v1.5](https://huggingface.co/lmsys/vicuna-7b-v1.5) as the reference model. We use [lmsys/vicuna-7b-v1.5](https://huggingface.co/lmsys/vicuna-7b-v1.5) to create an agent, and then ask the agent to generate the caption for an image from coco dataset, i.e. [demo.jpg](https://cocodataset.org/#explore?id=264959)
## 0. Requirements
To run this example with BigDL-LLM, we have some recommended requirements for your machine, please refer to [here](https://github.com/intel-analytics/BigDL/tree/main/python/llm/example/CPU/HF-Transformers-AutoModels/Model#recommended-requirements) for more information.
### 1. Install
We suggest using conda to manage environment:
```bash
conda create -n llm python=3.9
conda activate llm
pip install bigdl-llm[all] # install bigdl-llm with 'all' option
pip install pillow # additional package required for opening images
```
### 2. Run
```
python ./run_agent.py --repo-id-or-model-path REPO_ID_OR_MODEL_PATH --image-path IMAGE_PATH
```
Arguments info:
- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the huggingface repo id for the Vicuna model (e.g. `lmsys/vicuna-7b-v1.5`) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be `'lmsys/vicuna-7b-v1.5'`.
- `--image-path IMAGE_PATH`: argument defining the image to be infered. It is default to be `demo.jpg`.
> **Note**: When loading the model in 4-bit, BigDL-LLM converts linear layers in the model into INT4 format. In theory, a *X*B model saved in 16-bit will requires approximately 2*X* GB of memory for loading, and ~0.5*X* GB memory for further inference.
>
> Please select the appropriate size of the Vicuna model based on the capabilities of your machine.
#### 2.1 Client
On client Windows machine, it is recommended to run directly with full utilization of all cores:
```powershell
python ./run_agent.py
```
#### 2.2 Server
For optimal performance on server, it is recommended to set several environment variables (refer to [here](../README.md#best-known-configuration-on-linux) for more information), and run the example with all the physical cores of a single socket.
E.g. on Linux,
```bash
# set BigDL-Nano env variables
source bigdl-nano-init
# e.g. for a server with 48 cores per socket
export OMP_NUM_THREADS=48
numactl -C 0-47 -m 0 python ./run_agent.py
```
#### 2.3 Sample Output
#### [lmsys/vicuna-7b-v1.5](https://huggingface.co/lmsys/vicuna-7b-v1.5)
```log
Image path: demo.jpg
== Prompt ==
Generate a caption for the 'image'
==Explanation from the agent==
I will use the following tool: `image_captioner` to generate a caption for the image.
==Code generated by the agent==
caption = image_captioner(image)
==Result==
a little girl holding a stuffed teddy bear
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

View file

@ -0,0 +1,51 @@
#
# Copyright 2016 The BigDL Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
from PIL import Image
from transformers import AutoTokenizer, LocalAgent
from bigdl.llm.transformers import AutoModelForCausalLM
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run agent using vicuna model")
parser.add_argument("--repo-id-or-model-path", type=str, default="lmsys/vicuna-7b-v1.5",
help="The huggingface repo id for the Vicuna model to be downloaded"
", or the path to the huggingface checkpoint folder")
parser.add_argument("--image-path", type=str, default="demo.jpg",
help="Image to generate caption")
args = parser.parse_args()
model_path = args.repo_id_or_model_path
# Load model in 4 bit,
# which convert the relevant layers in the model into INT4 format
model = AutoModelForCausalLM.from_pretrained(model_path,
load_in_4bit=True)
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_path)
# Load image
image = Image.open(args.image_path)
# Create an agent
agent = LocalAgent(model, tokenizer)
# Generate results
prompt = "Generate a caption for the 'image'"
print(f"Image path: {args.image_path}")
print('==', 'Prompt', '==')
print(prompt)
print(agent.run(prompt, image=image))

View file

@ -6,6 +6,7 @@ This folder contains examples of running BigDL-LLM on Intel CPU:
- [PyTorch-Models](PyTorch-Models): running any PyTorch model on BigDL-LLM (with "one-line code change") - [PyTorch-Models](PyTorch-Models): running any PyTorch model on BigDL-LLM (with "one-line code change")
- [Native-Models](Native-Models): converting & running LLM in `llama`/`chatglm`/`bloom`/`gptneox`/`starcoder` model family using native (cpp) implementation - [Native-Models](Native-Models): converting & running LLM in `llama`/`chatglm`/`bloom`/`gptneox`/`starcoder` model family using native (cpp) implementation
- [LangChain](LangChain): running LangChain applications on BigDL-LLM - [LangChain](LangChain): running LangChain applications on BigDL-LLM
- [Applications](Applications): running Transformers applications on BigDl-LLM
## System Support ## System Support
**Hardware**: **Hardware**: