diff --git a/python/llm/example/CPU/Applications/hf-agent/README.md b/python/llm/example/CPU/Applications/hf-agent/README.md new file mode 100644 index 00000000..9c87e93e --- /dev/null +++ b/python/llm/example/CPU/Applications/hf-agent/README.md @@ -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 +``` diff --git a/python/llm/example/CPU/Applications/hf-agent/demo.jpg b/python/llm/example/CPU/Applications/hf-agent/demo.jpg new file mode 100644 index 00000000..a0fed25c Binary files /dev/null and b/python/llm/example/CPU/Applications/hf-agent/demo.jpg differ diff --git a/python/llm/example/CPU/Applications/hf-agent/run_agent.py b/python/llm/example/CPU/Applications/hf-agent/run_agent.py new file mode 100644 index 00000000..98fca0c5 --- /dev/null +++ b/python/llm/example/CPU/Applications/hf-agent/run_agent.py @@ -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)) diff --git a/python/llm/example/CPU/applications/streaming-llm/README.md b/python/llm/example/CPU/Applications/streaming-llm/README.md similarity index 100% rename from python/llm/example/CPU/applications/streaming-llm/README.md rename to python/llm/example/CPU/Applications/streaming-llm/README.md diff --git a/python/llm/example/CPU/applications/streaming-llm/run_streaming_llama.py b/python/llm/example/CPU/Applications/streaming-llm/run_streaming_llama.py similarity index 100% rename from python/llm/example/CPU/applications/streaming-llm/run_streaming_llama.py rename to python/llm/example/CPU/Applications/streaming-llm/run_streaming_llama.py diff --git a/python/llm/example/CPU/applications/streaming-llm/streaming_llm/__init__.py b/python/llm/example/CPU/Applications/streaming-llm/streaming_llm/__init__.py similarity index 100% rename from python/llm/example/CPU/applications/streaming-llm/streaming_llm/__init__.py rename to python/llm/example/CPU/Applications/streaming-llm/streaming_llm/__init__.py diff --git a/python/llm/example/CPU/applications/streaming-llm/streaming_llm/enable_streaming_llm.py b/python/llm/example/CPU/Applications/streaming-llm/streaming_llm/enable_streaming_llm.py similarity index 100% rename from python/llm/example/CPU/applications/streaming-llm/streaming_llm/enable_streaming_llm.py rename to python/llm/example/CPU/Applications/streaming-llm/streaming_llm/enable_streaming_llm.py diff --git a/python/llm/example/CPU/applications/streaming-llm/streaming_llm/kv_cache.py b/python/llm/example/CPU/Applications/streaming-llm/streaming_llm/kv_cache.py similarity index 100% rename from python/llm/example/CPU/applications/streaming-llm/streaming_llm/kv_cache.py rename to python/llm/example/CPU/Applications/streaming-llm/streaming_llm/kv_cache.py diff --git a/python/llm/example/CPU/applications/streaming-llm/streaming_llm/modify_falcon.py b/python/llm/example/CPU/Applications/streaming-llm/streaming_llm/modify_falcon.py similarity index 100% rename from python/llm/example/CPU/applications/streaming-llm/streaming_llm/modify_falcon.py rename to python/llm/example/CPU/Applications/streaming-llm/streaming_llm/modify_falcon.py diff --git a/python/llm/example/CPU/applications/streaming-llm/streaming_llm/modify_gpt_neox.py b/python/llm/example/CPU/Applications/streaming-llm/streaming_llm/modify_gpt_neox.py similarity index 100% rename from python/llm/example/CPU/applications/streaming-llm/streaming_llm/modify_gpt_neox.py rename to python/llm/example/CPU/Applications/streaming-llm/streaming_llm/modify_gpt_neox.py diff --git a/python/llm/example/CPU/applications/streaming-llm/streaming_llm/modify_llama.py b/python/llm/example/CPU/Applications/streaming-llm/streaming_llm/modify_llama.py similarity index 100% rename from python/llm/example/CPU/applications/streaming-llm/streaming_llm/modify_llama.py rename to python/llm/example/CPU/Applications/streaming-llm/streaming_llm/modify_llama.py diff --git a/python/llm/example/CPU/applications/streaming-llm/streaming_llm/utils.py b/python/llm/example/CPU/Applications/streaming-llm/streaming_llm/utils.py similarity index 100% rename from python/llm/example/CPU/applications/streaming-llm/streaming_llm/utils.py rename to python/llm/example/CPU/Applications/streaming-llm/streaming_llm/utils.py diff --git a/python/llm/example/CPU/README.md b/python/llm/example/CPU/README.md index 1344cbb6..b6be45cd 100644 --- a/python/llm/example/CPU/README.md +++ b/python/llm/example/CPU/README.md @@ -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") - [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 +- [Applications](Applications): running Transformers applications on BigDl-LLM ## System Support **Hardware**: