LLM: arc perf test for some popular models (#9188)
This commit is contained in:
		
							parent
							
								
									0d62bd4adb
								
							
						
					
					
						commit
						f87f67ee1c
					
				
					 7 changed files with 26 additions and 5272 deletions
				
			
		
							
								
								
									
										52
									
								
								.github/workflows/llm_performance_tests.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										52
									
								
								.github/workflows/llm_performance_tests.yml
									
									
									
									
										vendored
									
									
								
							| 
						 | 
					@ -88,25 +88,20 @@ jobs:
 | 
				
			||||||
      THREAD_NUM: 16
 | 
					      THREAD_NUM: 16
 | 
				
			||||||
      ANALYTICS_ZOO_ROOT: ${{ github.workspace }}
 | 
					      ANALYTICS_ZOO_ROOT: ${{ github.workspace }}
 | 
				
			||||||
    steps:
 | 
					    steps:
 | 
				
			||||||
      - name: Set environment variables
 | 
					 | 
				
			||||||
        shell: bash
 | 
					 | 
				
			||||||
        run: |
 | 
					 | 
				
			||||||
          echo "LLAMA2_7B_ORIGIN_PATH=${ORIGIN_DIR}/Llama-2-7b-chat-hf" >> "$GITHUB_ENV"
 | 
					 | 
				
			||||||
          echo "LLAMA2_13B_ORIGIN_PATH=${ORIGIN_DIR}/Llama-2-13b-chat-hf" >> "$GITHUB_ENV"
 | 
					 | 
				
			||||||
          echo "CHATGLM2_6B_ORIGIN_PATH=${ORIGIN_DIR}/chatglm2-6b" >> "$GITHUB_ENV"
 | 
					 | 
				
			||||||
          echo "WHISPER_MEDIUM_ORIGIN_PATH=${ORIGIN_DIR}/whisper-medium" >> "$GITHUB_ENV"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      - uses: actions/checkout@v3
 | 
					      - uses: actions/checkout@v3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Set up Python ${{ matrix.python-version }}
 | 
					      - name: Set up Python ${{ matrix.python-version }}
 | 
				
			||||||
        uses: actions/setup-python@v4
 | 
					        uses: actions/setup-python@v4
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          python-version: ${{ matrix.python-version }}
 | 
					          python-version: ${{ matrix.python-version }}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Install dependencies
 | 
					      - name: Install dependencies
 | 
				
			||||||
        shell: bash
 | 
					        shell: bash
 | 
				
			||||||
        run: |
 | 
					        run: |
 | 
				
			||||||
          python -m pip install --upgrade pip
 | 
					          python -m pip install --upgrade pip
 | 
				
			||||||
          python -m pip install --upgrade setuptools
 | 
					          python -m pip install --upgrade omegaconf
 | 
				
			||||||
          python -m pip install --upgrade wheel
 | 
					          python -m pip install --upgrade pandas
 | 
				
			||||||
 | 
					          python -m pip install --upgrade einops
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Download llm binary
 | 
					      - name: Download llm binary
 | 
				
			||||||
        uses: ./.github/actions/llm/download-llm-binary
 | 
					        uses: ./.github/actions/llm/download-llm-binary
 | 
				
			||||||
| 
						 | 
					@ -122,44 +117,15 @@ jobs:
 | 
				
			||||||
          source /opt/intel/oneapi/setvars.sh
 | 
					          source /opt/intel/oneapi/setvars.sh
 | 
				
			||||||
          bash python/llm/test/run-llm-install-tests.sh
 | 
					          bash python/llm/test/run-llm-install-tests.sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Download LLMs
 | 
					 | 
				
			||||||
        shell: bash
 | 
					 | 
				
			||||||
        run: |
 | 
					 | 
				
			||||||
          if [ ! -d $LLAMA2_7B_ORIGIN_PATH ]; then
 | 
					 | 
				
			||||||
            echo "Directory $LLAMA2_7B_ORIGIN_PATH not found. Downloading from FTP server..."
 | 
					 | 
				
			||||||
            wget -r -nH --no-verbose --cut-dirs=1 $LLM_FTP_URL/llm/Llama-2-7b-chat-hf -P $ORIGIN_DIR
 | 
					 | 
				
			||||||
          fi
 | 
					 | 
				
			||||||
          if [ ! -d $LLAMA2_13B_ORIGIN_PATH ]; then
 | 
					 | 
				
			||||||
            echo "Directory $LLAMA2_13B_ORIGIN_PATH not found. Downloading from FTP server..."
 | 
					 | 
				
			||||||
            wget -r -nH --no-verbose --cut-dirs=1 $LLM_FTP_URL/llm/Llama-2-13b-chat-hf -P $ORIGIN_DIR
 | 
					 | 
				
			||||||
          fi
 | 
					 | 
				
			||||||
          if [ ! -d $CHATGLM2_6B_ORIGIN_PATH ]; then
 | 
					 | 
				
			||||||
            echo "Directory $CHATGLM2_6B_ORIGIN_PATH not found. Downloading from FTP server..."
 | 
					 | 
				
			||||||
            wget -r -nH --no-verbose --cut-dirs=1 $LLM_FTP_URL/llm/chatglm2-6b -P $ORIGIN_DIR
 | 
					 | 
				
			||||||
          fi
 | 
					 | 
				
			||||||
          if [ ! -d $WHISPER_MEDIUM_ORIGIN_PATH ]; then
 | 
					 | 
				
			||||||
            echo "Directory $WHISPER_MEDIUM_ORIGIN_PATH not found. Downloading from FTP server..."
 | 
					 | 
				
			||||||
            wget -r -nH --no-verbose --cut-dirs=1 $LLM_FTP_URL/llm/whisper-medium -P $ORIGIN_DIR
 | 
					 | 
				
			||||||
          fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      - name: Test on xpu
 | 
					      - name: Test on xpu
 | 
				
			||||||
        shell: bash
 | 
					        shell: bash
 | 
				
			||||||
        run: |
 | 
					        run: |
 | 
				
			||||||
          source /opt/intel/oneapi/setvars.sh
 | 
					          source /opt/intel/oneapi/setvars.sh
 | 
				
			||||||
          export USE_XETLA=OFF
 | 
					          export USE_XETLA=OFF
 | 
				
			||||||
          export SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=1
 | 
					          export SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=1
 | 
				
			||||||
          cd python/llm/test/benchmark/gpu
 | 
					          mv python/llm/test/benchmark/arc-perf-test.yaml python/llm/dev/benchmark/all-in-one/config.yaml
 | 
				
			||||||
 | 
					          cd python/llm/dev/benchmark/all-in-one
 | 
				
			||||||
          export http_proxy=${HTTP_PROXY}
 | 
					          export http_proxy=${HTTP_PROXY}
 | 
				
			||||||
          export https_proxy=${HTTPS_PROXY}
 | 
					          export https_proxy=${HTTPS_PROXY}
 | 
				
			||||||
          rm -rf test-result || true
 | 
					          taskset -c 0-$((THREAD_NUM - 1)) python run.py
 | 
				
			||||||
          mkdir test-result
 | 
					          curl -T ./*.csv ${LLM_FTP_URL}/llm/ggml-actions/perf/
 | 
				
			||||||
          taskset -c 0-$((THREAD_NUM - 1)) python llama2.py --model-dir="${LLAMA2_7B_ORIGIN_PATH}" --input-tokens=32 --max-new-tokens=32 > test-result/llama2_7b-32-32.log
 | 
					 | 
				
			||||||
          taskset -c 0-$((THREAD_NUM - 1)) python llama2.py --model-dir="${LLAMA2_7B_ORIGIN_PATH}" --input-tokens=1024 --max-new-tokens=1024 > test-result/llama2_7b-1024-1024.log
 | 
					 | 
				
			||||||
          taskset -c 0-$((THREAD_NUM - 1)) python llama2.py --model-dir="${LLAMA2_13B_ORIGIN_PATH}" --input-tokens=32 --max-new-tokens=32 > test-result/llama2_13b-32-32.log
 | 
					 | 
				
			||||||
          taskset -c 0-$((THREAD_NUM - 1)) python llama2.py --model-dir="${LLAMA2_13B_ORIGIN_PATH}" --input-tokens=1024 --max-new-tokens=1024 > test-result/llama2_13b-1024-1024.log
 | 
					 | 
				
			||||||
          taskset -c 0-$((THREAD_NUM - 1)) python chatglm2.py --model-dir="${CHATGLM2_6B_ORIGIN_PATH}" --input-tokens=32 --max-new-tokens=32 > test-result/chatglm2_6b-32-32.log
 | 
					 | 
				
			||||||
          taskset -c 0-$((THREAD_NUM - 1)) python chatglm2.py --model-dir="${CHATGLM2_6B_ORIGIN_PATH}" --input-tokens=1024 --max-new-tokens=1024 > test-result/chatglm2_6b-1024-1024.log
 | 
					 | 
				
			||||||
          taskset -c 0-$((THREAD_NUM - 1)) python whisper.py --model-dir="${WHISPER_MEDIUM_ORIGIN_PATH}" > test-result/whisper_medium-default-default.log
 | 
					 | 
				
			||||||
          python ../analyze_log_dir.py --log-dir=./test-result --output-path=./xpu_latency.csv
 | 
					 | 
				
			||||||
          timestamp=`date '+%Y%m%d'`
 | 
					 | 
				
			||||||
          curl -T ./xpu_latency.csv ${LLM_FTP_URL}/llm/ggml-actions/perf/xpu_lantency_$timestamp.csv
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,64 +0,0 @@
 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# 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
 | 
					 | 
				
			||||||
import csv
 | 
					 | 
				
			||||||
import os
 | 
					 | 
				
			||||||
import re
 | 
					 | 
				
			||||||
import glob
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if __name__ == '__main__':
 | 
					 | 
				
			||||||
    parser = argparse.ArgumentParser(
 | 
					 | 
				
			||||||
        'Process the logs of benchmark utils and output a csv file of performance data',
 | 
					 | 
				
			||||||
        add_help=False)
 | 
					 | 
				
			||||||
    parser.add_argument('-m', '--log-dir',  default="./", type=str)
 | 
					 | 
				
			||||||
    parser.add_argument('--output-path',
 | 
					 | 
				
			||||||
                        default="./model_latency.csv", type=str)
 | 
					 | 
				
			||||||
    args = parser.parse_args()
 | 
					 | 
				
			||||||
    print(args)
 | 
					 | 
				
			||||||
    result_list = []
 | 
					 | 
				
			||||||
    for filename in glob.glob(os.path.join(args.log_dir, '*')):
 | 
					 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
            basename = os.path.basename(filename)
 | 
					 | 
				
			||||||
            name, _ = os.path.splitext(basename)
 | 
					 | 
				
			||||||
            model_name, prompt_length, output_length = name.strip().split('-')
 | 
					 | 
				
			||||||
            with open(filename, 'r', encoding='utf-8') as f:
 | 
					 | 
				
			||||||
                log = f.read()
 | 
					 | 
				
			||||||
            first_token_time_list = sorted(map(float,
 | 
					 | 
				
			||||||
                                               re.findall(r'First token cost (.*?)s', log)))
 | 
					 | 
				
			||||||
            rest_token_time_list = sorted(map(float,
 | 
					 | 
				
			||||||
                                              re.findall(r'Rest tokens cost average (.*?)s', log)))
 | 
					 | 
				
			||||||
            # For fairness, remove the fastest and slowest data
 | 
					 | 
				
			||||||
            first_token_latency = sum(first_token_time_list[1:-1]
 | 
					 | 
				
			||||||
                                      )/(len(first_token_time_list)-2)
 | 
					 | 
				
			||||||
            rest_token_latency = sum(rest_token_time_list[1:-1]
 | 
					 | 
				
			||||||
                                     )/(len(rest_token_time_list)-2)
 | 
					 | 
				
			||||||
            result_list.append({
 | 
					 | 
				
			||||||
                'model_name': model_name,
 | 
					 | 
				
			||||||
                'prompt_length': prompt_length,
 | 
					 | 
				
			||||||
                'output_length': output_length,
 | 
					 | 
				
			||||||
                'first_token_latency': first_token_latency,
 | 
					 | 
				
			||||||
                'rest_token_latency': rest_token_latency,
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
        except Exception as e:
 | 
					 | 
				
			||||||
            print(e.args)
 | 
					 | 
				
			||||||
            continue
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    with open(args.output_path, 'w', encoding='utf-8', newline='') as csvfile:
 | 
					 | 
				
			||||||
        writer = csv.DictWriter(csvfile, fieldnames=result_list[0].keys())
 | 
					 | 
				
			||||||
        writer.writeheader()
 | 
					 | 
				
			||||||
        writer.writerows(result_list)
 | 
					 | 
				
			||||||
    print('Log analysis finished!')
 | 
					 | 
				
			||||||
							
								
								
									
										17
									
								
								python/llm/test/benchmark/arc-perf-test.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								python/llm/test/benchmark/arc-perf-test.yaml
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,17 @@
 | 
				
			||||||
 | 
					repo_id:
 | 
				
			||||||
 | 
					  - 'THUDM/chatglm2-6b'
 | 
				
			||||||
 | 
					  - 'meta-llama/Llama-2-7b-chat-hf'
 | 
				
			||||||
 | 
					  - 'meta-llama/Llama-2-13b-chat-hf'
 | 
				
			||||||
 | 
					  - 'tiiuae/falcon-7b-instruct-with-patch'
 | 
				
			||||||
 | 
					  - 'mosaicml/mpt-7b-chat'
 | 
				
			||||||
 | 
					local_model_hub: '/mnt/disk1/models'
 | 
				
			||||||
 | 
					warm_up: 1
 | 
				
			||||||
 | 
					num_trials: 3
 | 
				
			||||||
 | 
					num_beams: 1 # default to greedy search
 | 
				
			||||||
 | 
					in_out_pairs:
 | 
				
			||||||
 | 
					  - '32-32'
 | 
				
			||||||
 | 
					  - '1024-128'
 | 
				
			||||||
 | 
					  - '2048-256'
 | 
				
			||||||
 | 
					test_api:
 | 
				
			||||||
 | 
					  - "transformer_int4_gpu"  # on Intel GPU
 | 
				
			||||||
 | 
					
 | 
				
			||||||
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							| 
						 | 
					@ -1,85 +0,0 @@
 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# 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.
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# This is to test latest ipex version, model is chatglm2
 | 
					 | 
				
			||||||
import torch
 | 
					 | 
				
			||||||
import os
 | 
					 | 
				
			||||||
import time
 | 
					 | 
				
			||||||
from bigdl.llm.transformers import AutoModel
 | 
					 | 
				
			||||||
from transformers import AutoTokenizer
 | 
					 | 
				
			||||||
import intel_extension_for_pytorch as ipex
 | 
					 | 
				
			||||||
import numpy as np
 | 
					 | 
				
			||||||
from itertools import chain
 | 
					 | 
				
			||||||
import pathlib
 | 
					 | 
				
			||||||
import argparse
 | 
					 | 
				
			||||||
import json
 | 
					 | 
				
			||||||
from benchmark_util import BenchmarkWrapper
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if __name__ == '__main__':
 | 
					 | 
				
			||||||
    parser = argparse.ArgumentParser('OPT generation script', add_help=False)
 | 
					 | 
				
			||||||
    parser.add_argument('-m', '--model-dir',
 | 
					 | 
				
			||||||
                        default="/mnt/disk1/models/chatglm2-6b", type=str)
 | 
					 | 
				
			||||||
    parser.add_argument('--input-tokens', default='32', type=str)
 | 
					 | 
				
			||||||
    parser.add_argument('--max-new-tokens', default=32,
 | 
					 | 
				
			||||||
                        type=int, help="output max new tokens")
 | 
					 | 
				
			||||||
    args = parser.parse_args()
 | 
					 | 
				
			||||||
    print(args)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    prompt_dict = {
 | 
					 | 
				
			||||||
        '32': "我总是在晚上失眠,这个症状已经持续很长时间,所以晚上睡不着到底应该怎么处理,请告诉我一些可行的建议与方法,越详细越好",
 | 
					 | 
				
			||||||
        '1024': "It is done, and submitted. You can play 'Survival of the Tastiest' on Android, and on the web. Playing on the web works, but you have to simulate multiple touch for table moving and that can be a bit confusing. There is a lot I'd like to talk about. I will go through every topic, insted of making the typical what went right/wrong list. Concept Working over the theme was probably one of the hardest tasks which I had to face. Originally, I had an idea of what kind of game I wanted to develop, gameplay wise - something with a lot of enemies/actors, simple graphics, maybe set in space, controlled from a top-down view. I was confident that I could fit any theme around it. In the end, the problem with a theme like 'Evolution' in a game is that evolution is unassisted. It happens through several seemingly random mutations over time, with the most apt permutation surviving. This genetic car simulator is, in my opinion, a great example of actual evolution of a species facing a challenge. But is it a game? In a game, you need to control something to reach an objective. That control goes against what evolution is supposed to be like. If you allow the user to pick how to evolve something, it's not evolution anymore - it's the equivalent of intelligent design, the fable invented by creationists to combat the idea of evolution. Being agnostic and a Pastafarian, that's not something that rubbed me the right way. Hence, my biggest dillema when deciding what to create was not with what I wanted to create, but with what I did not. I didn't want to create an 'intelligent design' simulator and wrongly call it evolution. This is a problem, of course, every other contestant also had to face. And judging by the entries submitted, not many managed to work around it. I'd say the only real solution was through the use of artificial selection, somehow. So far, I haven't seen any entry using this at its core gameplay. Alas, this is just a fun competition and after a while I decided not to be as strict with the game idea, and allowed myself to pick whatever I thought would work out. My initial idea was to create something where humanity tried to evolve to a next level, but had some kind of foe trying to stop them from doing so. I kind of had this image of human souls flying in space towards a monolith or a space baby (all based in 2001: A Space Odyssey of course) but I couldn't think of compelling (read: serious) mechanics for that. Borgs were my next inspiration, as their whole hypothesis fit pretty well into the evolution theme. But how to make it work? Are you the borg, or fighting the Borg? The third and final idea came to me through my girlfriend, who somehow gave me the idea of making something about the evolution of Pasta. The more I thought about it the more it sounded like it would work, so I decided to go with it. Conversations with my inspiring co-worker Roushey (who also created the 'Mechanical Underdogs' signature logo for my intros) further matured the concept, as it involved into the idea of having individual pieces of pasta flying around and trying to evolve until they became all-powerful. A secondary idea here was that the game would work to explain how the Flying Spaghetti Monster came to exist - by evolving from a normal dinner table. So the idea evolved more or less into this: you are sitting a table. You have your own plate, with is your 'base'. There are 5 other guests at the table, each with their own plate. Your plate can spawn little pieces of pasta. You do so by 'ordering' them through a menu. Some pastas are better than others; some are faster, some are stronger. They have varying 'costs', which are debited from your credits (you start with a number of credits). Once spawned, your pastas start flying around. Their instinct is to fly to other plates, in order to conquer them (the objective of the game is having your pasta conquer all the plates on the table). But they are really autonomous, so after being spawned, you have no control over your pasta (think DotA or LoL creeps). Your pasta doesn't like other people's pasta, so if they meet, they shoot sauce at each other until one dies. You get credits for other pastas your own pasta kill. Once a pasta is in vicinity of a plate. You get credits for other pastas your own pasta kill. Once a pasta is in vicinity of a plate.You get credits for other pastas your own pasta kill. Once a pasta is in vicinity of a plate.You get credits for"
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if args.input_tokens in prompt_dict:
 | 
					 | 
				
			||||||
        prompt = prompt_dict[args.input_tokens]
 | 
					 | 
				
			||||||
    else:
 | 
					 | 
				
			||||||
        prompt = args.input_tokens
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    print(f"Test {args.model_dir}...")
 | 
					 | 
				
			||||||
    # load_in_4bit=True in bigdl.llm.transformers will convert
 | 
					 | 
				
			||||||
    # the relevant layers in the model into int4 format
 | 
					 | 
				
			||||||
    model = AutoModel.from_pretrained(
 | 
					 | 
				
			||||||
        args.model_dir, load_in_4bit=True, optimize_model=False, trust_remote_code=True)
 | 
					 | 
				
			||||||
    model = model.half().to('xpu')
 | 
					 | 
				
			||||||
    model = BenchmarkWrapper(model)
 | 
					 | 
				
			||||||
    print(model.dtype)
 | 
					 | 
				
			||||||
    tokenizer = AutoTokenizer.from_pretrained(
 | 
					 | 
				
			||||||
        args.model_dir, trust_remote_code=True)
 | 
					 | 
				
			||||||
    inputs = tokenizer([prompt], return_tensors="pt").to('xpu')
 | 
					 | 
				
			||||||
    print(inputs["input_ids"].shape)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    total_list = []
 | 
					 | 
				
			||||||
    e2e_time = []
 | 
					 | 
				
			||||||
    with torch.inference_mode():
 | 
					 | 
				
			||||||
        for i in range(10):
 | 
					 | 
				
			||||||
            torch.xpu.synchronize()
 | 
					 | 
				
			||||||
            st = time.time()
 | 
					 | 
				
			||||||
            inputs = tokenizer([prompt], return_tensors="pt").to('xpu')
 | 
					 | 
				
			||||||
            # print(inputs["input_ids"].shape)
 | 
					 | 
				
			||||||
            # output = model.generate(**inputs, do_sample=False, temperature=0.9, max_new_tokens=32, token_latency=True)
 | 
					 | 
				
			||||||
            output = model.generate(
 | 
					 | 
				
			||||||
                **inputs, do_sample=False, temperature=0.9, max_new_tokens=args.max_new_tokens)
 | 
					 | 
				
			||||||
            gen_ids = output[0]
 | 
					 | 
				
			||||||
            gen_text = tokenizer.batch_decode(
 | 
					 | 
				
			||||||
                gen_ids, skip_special_tokens=True)
 | 
					 | 
				
			||||||
            torch.xpu.synchronize()
 | 
					 | 
				
			||||||
            end = time.time()
 | 
					 | 
				
			||||||
            e2e_time.append(end-st)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    print('Prompt:', prompt)
 | 
					 | 
				
			||||||
    print('Output:', gen_text)
 | 
					 | 
				
			||||||
    print(f'Inference time: {end-st} s')
 | 
					 | 
				
			||||||
    print(e2e_time)
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,88 +0,0 @@
 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# 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
 | 
					 | 
				
			||||||
import torch
 | 
					 | 
				
			||||||
import os
 | 
					 | 
				
			||||||
import time
 | 
					 | 
				
			||||||
from transformers import LlamaTokenizer
 | 
					 | 
				
			||||||
import intel_extension_for_pytorch as ipex
 | 
					 | 
				
			||||||
from benchmark_util import BenchmarkWrapper
 | 
					 | 
				
			||||||
from bigdl.llm.transformers import AutoModelForCausalLM
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if __name__ == '__main__':
 | 
					 | 
				
			||||||
    parser = argparse.ArgumentParser('OPT generation script', add_help=False)
 | 
					 | 
				
			||||||
    parser.add_argument('-m', '--model-dir',
 | 
					 | 
				
			||||||
                        default="/mnt/disk1/models/Llama-2-7b-chat-hf", type=str)
 | 
					 | 
				
			||||||
    parser.add_argument('--input-tokens', default='32', type=str)
 | 
					 | 
				
			||||||
    parser.add_argument('--max-new-tokens', default=32,
 | 
					 | 
				
			||||||
                        type=int, help="output max new tokens")
 | 
					 | 
				
			||||||
    args = parser.parse_args()
 | 
					 | 
				
			||||||
    print(args)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    model_path = args.model_dir
 | 
					 | 
				
			||||||
    print(f"Test {model_path}...")
 | 
					 | 
				
			||||||
    # load_in_4bit=True in bigdl.llm.transformers will convert
 | 
					 | 
				
			||||||
    # the relevant layers in the model into int4 format
 | 
					 | 
				
			||||||
    llama_model = AutoModelForCausalLM.from_pretrained(
 | 
					 | 
				
			||||||
        model_path, optimize_model=False, load_in_4bit=True)
 | 
					 | 
				
			||||||
    # llama_model = model.AutoModelForCausalLM.from_pretrained(model_path)
 | 
					 | 
				
			||||||
    llama_model = llama_model.half().to('xpu')
 | 
					 | 
				
			||||||
    # llama_model = llama_model.to(memory_format=torch.channels_last)
 | 
					 | 
				
			||||||
    # llama_model = ipex.optimize(llama_model, dtype=torch.float16)
 | 
					 | 
				
			||||||
    # llama_model = ipex.optimize_transformers(llama_model, dtype=torch.float16)
 | 
					 | 
				
			||||||
    print(llama_model.dtype)
 | 
					 | 
				
			||||||
    llama_model = BenchmarkWrapper(llama_model)
 | 
					 | 
				
			||||||
    tokenizer = LlamaTokenizer.from_pretrained(model_path)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    prompt_dict = {
 | 
					 | 
				
			||||||
        '32': "Once upon a time, there existed a little girl who liked to have adventures. She wanted to go to places and meet new people, and have fun",
 | 
					 | 
				
			||||||
        '1024': "It is done, and submitted. You can play 'Survival of the Tastiest' on Android, and on the web. Playing on the web works, but you have to simulate multiple touch for table moving and that can be a bit confusing. There is a lot I'd like to talk about. I will go through every topic, insted of making the typical what went right/wrong list. Concept Working over the theme was probably one of the hardest tasks which I had to face. Originally, I had an idea of what kind of game I wanted to develop, gameplay wise - something with a lot of enemies/actors, simple graphics, maybe set in space, controlled from a top-down view. I was confident that I could fit any theme around it. In the end, the problem with a theme like 'Evolution' in a game is that evolution is unassisted. It happens through several seemingly random mutations over time, with the most apt permutation surviving. This genetic car simulator is, in my opinion, a great example of actual evolution of a species facing a challenge. But is it a game? In a game, you need to control something to reach an objective. That control goes against what evolution is supposed to be like. If you allow the user to pick how to evolve something, it's not evolution anymore - it's the equivalent of intelligent design, the fable invented by creationists to combat the idea of evolution. Being agnostic and a Pastafarian, that's not something that rubbed me the right way. Hence, my biggest dillema when deciding what to create was not with what I wanted to create, but with what I did not. I didn't want to create an 'intelligent design' simulator and wrongly call it evolution. This is a problem, of course, every other contestant also had to face. And judging by the entries submitted, not many managed to work around it. I'd say the only real solution was through the use of artificial selection, somehow. So far, I haven't seen any entry using this at its core gameplay. Alas, this is just a fun competition and after a while I decided not to be as strict with the game idea, and allowed myself to pick whatever I thought would work out. My initial idea was to create something where humanity tried to evolve to a next level, but had some kind of foe trying to stop them from doing so. I kind of had this image of human souls flying in space towards a monolith or a space baby (all based in 2001: A Space Odyssey of course) but I couldn't think of compelling (read: serious) mechanics for that. Borgs were my next inspiration, as their whole hypothesis fit pretty well into the evolution theme. But how to make it work? Are you the borg, or fighting the Borg? The third and final idea came to me through my girlfriend, who somehow gave me the idea of making something about the evolution of Pasta. The more I thought about it the more it sounded like it would work, so I decided to go with it. Conversations with my inspiring co-worker Roushey (who also created the 'Mechanical Underdogs' signature logo for my intros) further matured the concept, as it involved into the idea of having individual pieces of pasta flying around and trying to evolve until they became all-powerful. A secondary idea here was that the game would work to explain how the Flying Spaghetti Monster came to exist - by evolving from a normal dinner table. So the idea evolved more or less into this: you are sitting a table. You have your own plate, with is your 'base'. There are 5 other guests at the table, each with their own plate. Your plate can spawn little pieces of pasta. You do so by 'ordering' them through a menu. Some pastas are better than others; some are faster, some are stronger. They have varying 'costs', which are debited from your credits (you start with a number of credits). Once spawned, your pastas start flying around. Their instinct is to fly to other plates, in order to conquer them (the objective of the game is having your pasta conquer all the plates on the table). But they are really autonomous, so after being spawned, you have no control over your pasta (think DotA or LoL creeps). Your pasta doesn't like other people's pasta, so if they meet, they shoot sauce at each other until one dies. You get credits for other pastas your own pasta kill. Once a pasta is in vicinity of a plate"
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if args.input_tokens in prompt_dict:
 | 
					 | 
				
			||||||
        prompt = prompt_dict[args.input_tokens]
 | 
					 | 
				
			||||||
    else:
 | 
					 | 
				
			||||||
        prompt = args.input_tokens
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # with torch.inference_mode():
 | 
					 | 
				
			||||||
    with torch.inference_mode(), torch.autocast(device_type='xpu', enabled=True, dtype=torch.float16):
 | 
					 | 
				
			||||||
        # warmup
 | 
					 | 
				
			||||||
        torch.xpu.synchronize()
 | 
					 | 
				
			||||||
        input_ids = tokenizer.encode(prompt, return_tensors="pt").to('xpu')
 | 
					 | 
				
			||||||
        print("input length is: ", len((input_ids[0])))
 | 
					 | 
				
			||||||
        output = llama_model.generate(
 | 
					 | 
				
			||||||
            input_ids, do_sample=False, max_new_tokens=args.max_new_tokens)
 | 
					 | 
				
			||||||
        output_str = tokenizer.decode(output[0], skip_special_tokens=True)
 | 
					 | 
				
			||||||
        torch.xpu.synchronize()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        e2e_time = []
 | 
					 | 
				
			||||||
        for i in range(10):
 | 
					 | 
				
			||||||
            st = time.time()
 | 
					 | 
				
			||||||
            torch.xpu.synchronize()
 | 
					 | 
				
			||||||
            input_ids = tokenizer.encode(
 | 
					 | 
				
			||||||
                prompt, return_tensors="pt").to('xpu')
 | 
					 | 
				
			||||||
            output = llama_model.generate(
 | 
					 | 
				
			||||||
                input_ids, do_sample=False, max_new_tokens=args.max_new_tokens)
 | 
					 | 
				
			||||||
            output_str = tokenizer.decode(output[0], skip_special_tokens=True)
 | 
					 | 
				
			||||||
            torch.xpu.synchronize()
 | 
					 | 
				
			||||||
            end = time.time()
 | 
					 | 
				
			||||||
            e2e_time.append(end-st)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    print('Prompt:', prompt)
 | 
					 | 
				
			||||||
    print('Output:', output_str)
 | 
					 | 
				
			||||||
    print(f'Inference time: {end-st} s')
 | 
					 | 
				
			||||||
    print(e2e_time)
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,75 +0,0 @@
 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# 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
 | 
					 | 
				
			||||||
import intel_extension_for_pytorch as ipex
 | 
					 | 
				
			||||||
from transformers import WhisperProcessor
 | 
					 | 
				
			||||||
import torch
 | 
					 | 
				
			||||||
import time
 | 
					 | 
				
			||||||
from benchmark_util import BenchmarkWrapper
 | 
					 | 
				
			||||||
from bigdl.llm.transformers import AutoModelForSpeechSeq2Seq
 | 
					 | 
				
			||||||
from datasets import load_dataset, load_from_disk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if __name__ == '__main__':
 | 
					 | 
				
			||||||
    parser = argparse.ArgumentParser('OPT generation script', add_help=False)
 | 
					 | 
				
			||||||
    parser.add_argument('-m', '--model-dir',
 | 
					 | 
				
			||||||
                        default="/mnt/disk1/models/whisper-medium", type=str)
 | 
					 | 
				
			||||||
    args = parser.parse_args()
 | 
					 | 
				
			||||||
    print(args)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    model_path = args.model_dir
 | 
					 | 
				
			||||||
    dataset_path = "hf-internal-testing/librispeech_asr_dummy"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # load model and processor
 | 
					 | 
				
			||||||
    ds = load_dataset(dataset_path, "clean", split="validation")
 | 
					 | 
				
			||||||
    print("pass")
 | 
					 | 
				
			||||||
    processor = WhisperProcessor.from_pretrained(model_path)
 | 
					 | 
				
			||||||
    print("model loaded")
 | 
					 | 
				
			||||||
    # load dummy dataset and read audio files
 | 
					 | 
				
			||||||
    sample = ds[0]["audio"]
 | 
					 | 
				
			||||||
    # for transformer == 4.30.2
 | 
					 | 
				
			||||||
    input_features = processor(
 | 
					 | 
				
			||||||
        sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt").input_features
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    input_features = input_features.half().contiguous()
 | 
					 | 
				
			||||||
    input_features = input_features.to('xpu')
 | 
					 | 
				
			||||||
    print(input_features.shape)
 | 
					 | 
				
			||||||
    print(input_features.is_contiguous())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # generate token ids
 | 
					 | 
				
			||||||
    whisper = AutoModelForSpeechSeq2Seq.from_pretrained(
 | 
					 | 
				
			||||||
        model_path, load_in_4bit=True, optimize_model=False)
 | 
					 | 
				
			||||||
    whisper.config.forced_decoder_ids = None
 | 
					 | 
				
			||||||
    whisper = whisper.half().to('xpu')
 | 
					 | 
				
			||||||
    whisper = BenchmarkWrapper(whisper)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    with torch.inference_mode():
 | 
					 | 
				
			||||||
        e2e_time = []
 | 
					 | 
				
			||||||
        for i in range(10):
 | 
					 | 
				
			||||||
            torch.xpu.synchronize()
 | 
					 | 
				
			||||||
            st = time.time()
 | 
					 | 
				
			||||||
            predicted_ids = whisper.generate(input_features)
 | 
					 | 
				
			||||||
            # print(len(predicted_ids[0]))
 | 
					 | 
				
			||||||
            torch.xpu.synchronize()
 | 
					 | 
				
			||||||
            output_str = processor.batch_decode(
 | 
					 | 
				
			||||||
                predicted_ids, skip_special_tokens=True)
 | 
					 | 
				
			||||||
            end = time.time()
 | 
					 | 
				
			||||||
            e2e_time.append(end-st)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        print(f'Inference time: {end-st} s')
 | 
					 | 
				
			||||||
        print('Output:', output_str)
 | 
					 | 
				
			||||||
        print(f'Inference time: {end-st} s')
 | 
					 | 
				
			||||||
        print(e2e_time)
 | 
					 | 
				
			||||||
		Loading…
	
		Reference in a new issue