Skip to content

Commit b353de3

Browse files
committed
Revert "Remove netsec as no longer needed"
This reverts commit 8f3276d.
1 parent 4fd65d6 commit b353de3

File tree

4 files changed

+239
-0
lines changed

4 files changed

+239
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# OpenVINO™ Bert Model Benchmarking
2+
Benchmark OpenVINO™ bert model
3+
4+
## Steps
5+
### 1. Setup OpenVINO™
6+
Setup OpenVINO™
7+
```bash
8+
./setup_ov.sh
9+
```
10+
Installation is completed when you see this message:
11+
> ✓ OpenVINO™ configured
12+
13+
### 2. Benchmark
14+
15+
Benchmark bert-base-cased model (FP32)
16+
```bash
17+
#Python virtual environment to testing bert openvino model
18+
source bert_ov_venv/bin/activate
19+
20+
#CPU
21+
numactl -C 0 benchmark_app -m models/bert-base-cased.xml -d CPU -hint latency -shape "[1, 512]"
22+
#GPU
23+
numactl -C 0 benchmark_app -m models/bert-base-cased.xml -d GPU -hint latency -shape "[1, 512]"
24+
25+
#Deactivate virtual environment
26+
deactivate
27+
28+
```
29+
30+
Benchmark quantized bert-base-cased model (INT8)
31+
```bash
32+
33+
#Python virtual environment to testing bert openvino model
34+
source bert_ov_venv/bin/activate
35+
36+
#CPU
37+
numactl -C 0 benchmark_app -m models/quantized_bert_base_cased.xml -d CPU -hint latency -shape "[1, 512]"
38+
#GPU
39+
numactl -C 0 benchmark_app -m models/quantized_bert_base_cased.xml -d GPU -hint latency -shape "[1, 512]"
40+
41+
#Deactivate virtual environment
42+
deactivate
43+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--extra-index-url https://download.pytorch.org/whl/cpu
2+
nncf==2.7.0
3+
transformers==4.38.0
4+
openvino==2023.2.0
5+
evaluate==0.4.1
6+
datasets==2.15.0
7+
torch==2.2.0
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
3+
# Copyright (C) 2024 Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
set -e
7+
8+
# symbol
9+
S_VALID=""
10+
#S_INVALID="✗"
11+
12+
# verify current user
13+
if [ "$EUID" -eq 0 ]; then
14+
echo "Must not run with sudo or root user"
15+
exit 1
16+
fi
17+
18+
CURRENT_DIR=$(pwd)
19+
20+
install_packages(){
21+
local PACKAGES=("$@")
22+
local INSTALL_REQUIRED=0
23+
for PACKAGE in "${PACKAGES[@]}"; do
24+
INSTALLED_VERSION=$(dpkg-query -W -f='${Version}' "$PACKAGE" 2>/dev/null || true)
25+
LATEST_VERSION=$(apt-cache policy "$PACKAGE" | grep Candidate | awk '{print $2}')
26+
27+
if [ -z "$INSTALLED_VERSION" ] || [ "$INSTALLED_VERSION" != "$LATEST_VERSION" ]; then
28+
echo "$PACKAGE is not installed or not the latest version."
29+
INSTALL_REQUIRED=1
30+
fi
31+
done
32+
if [ $INSTALL_REQUIRED -eq 1 ]; then
33+
sudo -E apt update
34+
sudo -E apt install -y "${PACKAGES[@]}"
35+
fi
36+
}
37+
38+
verify_dependencies_ov(){
39+
echo -e "# Verifying OV dependencies"
40+
DEPENDENCIES_PACKAGES=(
41+
zip
42+
wget
43+
numactl
44+
python3-pip
45+
python3-venv
46+
python3-opencv
47+
)
48+
install_packages "${DEPENDENCIES_PACKAGES[@]}"
49+
echo "$S_VALID Dependencies installed";
50+
}
51+
52+
#verify gpu
53+
verify_gpu() {
54+
mkdir "$HOME"/neo
55+
cd "$HOME"/neo
56+
wget https://github.com/intel/intel-graphics-compiler/releases/download/igc-1.0.15985.7/intel-igc-core_1.0.15985.7_amd64.deb
57+
wget https://github.com/intel/intel-graphics-compiler/releases/download/igc-1.0.15985.7/intel-igc-opencl_1.0.15985.7_amd64.deb
58+
wget https://github.com/intel/compute-runtime/releases/download/24.05.28454.6/intel-level-zero-gpu-dbgsym_1.3.28454.6_amd64.ddeb
59+
wget https://github.com/intel/compute-runtime/releases/download/24.05.28454.6/intel-level-zero-gpu_1.3.28454.6_amd64.deb
60+
wget https://github.com/intel/compute-runtime/releases/download/24.05.28454.6/intel-opencl-icd-dbgsym_24.05.28454.6_amd64.ddeb
61+
wget https://github.com/intel/compute-runtime/releases/download/24.05.28454.6/intel-opencl-icd_24.05.28454.6_amd64.deb
62+
wget https://github.com/intel/compute-runtime/releases/download/24.05.28454.6/libigdgmm12_22.3.11_amd64.deb
63+
sudo dpkg -i ./*.deb
64+
}
65+
66+
verify_ov() {
67+
cd "$CURRENT_DIR"
68+
python3 -m venv bert_ov_venv
69+
# shellcheck source=/dev/null
70+
source bert_ov_venv/bin/activate
71+
pip install -U pip
72+
pip install -r requirements.txt
73+
python torch-to-ov.py
74+
}
75+
76+
77+
setup() {
78+
verify_dependencies_ov
79+
verify_gpu
80+
verify_ov
81+
82+
echo -e "\n# Status"
83+
echo "$S_VALID OpenVINO™ configured"
84+
}
85+
86+
setup
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import os
5+
import sys
6+
import time
7+
from pathlib import Path
8+
from zipfile import ZipFile
9+
from typing import Iterable
10+
from typing import Any
11+
12+
import datasets
13+
import numpy as np
14+
import nncf
15+
from nncf.parameters import ModelType
16+
import openvino as ov
17+
import torch
18+
from transformers import BertForSequenceClassification, BertTokenizer
19+
20+
21+
MODEL_DIR = "models"
22+
os.makedirs(MODEL_DIR, exist_ok=True)
23+
24+
MAX_SEQ_LENGTH = 512
25+
26+
27+
def load_model(inputs, input_info):
28+
try:
29+
ir_model_xml = Path(MODEL_DIR) / "bert-base-cased.xml"
30+
core = ov.Core()
31+
32+
torch_model = BertForSequenceClassification.from_pretrained('bert-base-cased')
33+
torch_model.eval
34+
35+
# Convert the PyTorch model to OpenVINO IR FP32.
36+
if not ir_model_xml.exists():
37+
model = ov.convert_model(torch_model, example_input=inputs, input=input_info)
38+
ov.save_model(model, str(ir_model_xml))
39+
else:
40+
model = core.read_model(ir_model_xml)
41+
42+
return model
43+
except Exception as e:
44+
print(f"Error in load_model: {e}")
45+
sys.exit(1)
46+
47+
def create_data_source():
48+
try:
49+
raw_dataset = datasets.load_dataset('glue', 'mrpc', split='validation')
50+
tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
51+
52+
def _preprocess_fn(examples):
53+
texts = (examples['sentence1'], examples['sentence2'])
54+
result = tokenizer(*texts, padding='max_length', max_length=MAX_SEQ_LENGTH, truncation=True)
55+
result['labels'] = examples['label']
56+
return result
57+
processed_dataset = raw_dataset.map(_preprocess_fn, batched=True, batch_size=1)
58+
59+
return processed_dataset
60+
except Exception as e:
61+
print(f"Error in create_data_source: {e}")
62+
sys.exit(1)
63+
64+
def nncf_quantize(model, inputs):
65+
try:
66+
INPUT_NAMES = [key for key in inputs.keys()]
67+
data_source = create_data_source()
68+
69+
def transform_fn(data_item):
70+
"""
71+
Extract the model's input from the data item.
72+
The data item here is the data item that is returned from the data source per iteration.
73+
This function should be passed when the data item cannot be used as model's input.
74+
"""
75+
inputs = {
76+
name: np.asarray([data_item[name]], dtype=np.int64) for name in INPUT_NAMES
77+
}
78+
return inputs
79+
80+
calibration_dataset = nncf.Dataset(data_source, transform_fn)
81+
# Quantize the model. By specifying model_type, we specify additional transformer patterns in the model.
82+
quantized_model = nncf.quantize(model, calibration_dataset,
83+
model_type=ModelType.TRANSFORMER)
84+
85+
86+
compressed_model_xml = Path(MODEL_DIR) / "quantized_bert_base_cased.xml"
87+
ov.save_model(quantized_model, compressed_model_xml)
88+
except Exception as e:
89+
print(f"Error in nncf_quantize: {e}")
90+
sys.exit(1)
91+
92+
if __name__ == '__main__':
93+
input_shape = ov.PartialShape([1, 512])
94+
input_info = [("input_ids", input_shape, np.int64),("attention_mask", input_shape, np.int64),("token_type_ids", input_shape, np.int64)]
95+
default_input = torch.ones(1, MAX_SEQ_LENGTH, dtype=torch.int64)
96+
inputs = {
97+
"input_ids": default_input,
98+
"attention_mask": default_input,
99+
"token_type_ids": default_input,
100+
}
101+
102+
model = load_model(inputs, input_info)
103+
quantized_model = nncf_quantize(model, inputs)

0 commit comments

Comments
 (0)