Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

[DOC]Add modelscope example #1578

Merged
merged 8 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/modelscope/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ModelScope with ITREX

Intel® Extension for Transformers(ITREX) support almost all the LLMs in Pytorch format from ModelScope such as phi, Qwen, ChatGLM, Baichuan, gemma, etc.

## Usage Example

ITREX provides a script that demonstrates the use of modelscope. Use numactl to improve performance and run it with the following command:
```bash
OMP_NUM_THREADS=num_cores numactl -l -C 0-num_cores-1 python run_modelscope_example.py --model=qwen/Qwen-7B --prompt=你好
```

## Supported and Validated Models
We have validated the majority of existing models using modelscope==1.13.1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the requirment.txt

* [qwen/Qwen-7B](https://www.modelscope.cn/models/qwen/Qwen-7B/summary)
* [ZhipuAI/ChatGLM-6B](https://www.modelscope.cn/models/ZhipuAI/ChatGLM-6B/summary)(transformers=4.33.1)
* [ZhipuAI/chatglm2-6b](https://www.modelscope.cn/models/ZhipuAI/chatglm2-6b/summary)(transformers=4.33.1)
* [ZhipuAI/chatglm3-6b](https://www.modelscope.cn/models/ZhipuAI/chatglm3-6b/summary)(transformers=4.33.1)
* [baichuan-inc/Baichuan2-7B-Chat](https://www.modelscope.cn/models/baichuan-inc/Baichuan2-7B-Chat/summary)(transformers=4.33.1)
* [baichuan-inc/Baichuan2-13B-Chat](https://www.modelscope.cn/models/baichuan-inc/Baichuan2-13B-Chat/summary)(transformers=4.33.1)
* [LLM-Research/Phi-3-mini-4k-instruct](https://www.modelscope.cn/models/LLM-Research/Phi-3-mini-4k-instruct/summary)
* [LLM-Research/Phi-3-mini-128k-instruct](https://www.modelscope.cn/models/LLM-Research/Phi-3-mini-128k-instruct/summary)
* [AI-ModelScope/gemma-2b](https://www.modelscope.cn/models/AI-ModelScope/gemma-2b/summary)

If you encounter any problems, please let us know.
13 changes: 13 additions & 0 deletions examples/modelscope/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
intel_extension_for_transformers
neural-speed
lm-eval
sentencepiece
gguf
--extra-index-url https://download.pytorch.org/whl/cpu
torch==2.3.0+cpu
transformers
intel_extension_for_pytorch==2.3.0
tiktoken
transformers_stream_generator
zipfile38
modelscope
30 changes: 30 additions & 0 deletions examples/modelscope/run_modelscope_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from transformers import TextStreamer
from modelscope import AutoTokenizer
from intel_extension_for_transformers.transformers import AutoModelForCausalLM
from typing import List, Optional
import argparse

def main(args_in: Optional[List[str]] = None) -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--model", type=str, help="Model name: String", required=True, default="qwen/Qwen-7B")
parser.add_argument(
"-p",
"--prompt",
type=str,
help="Prompt to start generation with: String (default: empty)",
default="你好,你可以做点什么?",
)
parser.add_argument("--benchmark", action="store_true")
parser.add_argument("--use_neural_speed", action="store_true")
args = parser.parse_args(args_in)
print(args)
model_name = args.model # Modelscope model_id or local model
prompt = args.prompt
model = AutoModelForCausalLM.from_pretrained(model_name, load_in_4bit=True, model_hub="modelscope")
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
inputs = tokenizer(prompt, return_tensors="pt").input_ids
streamer = TextStreamer(tokenizer)
outputs = model.generate(inputs, streamer=streamer, max_new_tokens=300)

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class _BaseQBitsAutoModelClass:
"whisper",
"qwen2",
"gemma",
"phi3",
"tinyllama",
]

Expand Down
Loading