Skip to content

Commit 922b247

Browse files
authored
Add TF 3x Examples (#1839)
Signed-off-by: zehao-intel <zehao.huang@intel.com>
1 parent 70a1d50 commit 922b247

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+5428
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"tensorflow": {
3+
"bert_large_squad_model_zoo": {
4+
"model_src_dir": "nlp/bert_large_squad_model_zoo/quantization/ptq",
5+
"dataset_location": "/tf_dataset/tensorflow/bert/data",
6+
"input_model": "/tf_dataset/tensorflow/bert/fp32_bert_squad.pb",
7+
"main_script": "main.py",
8+
"batch_size": 64,
9+
"fp32_model_url": "https://storage.googleapis.com/intel-optimized-tensorflow/models/v2_7_0/fp32_bert_squad.pb"
10+
},
11+
"opt_125m_sq": {
12+
"model_src_dir": "nlp/large_language_models/quantization/ptq/smoothquant",
13+
"dataset_location": "",
14+
"input_model": "facebook/opt-125m",
15+
"main_script": "main.py",
16+
"batch_size": 16
17+
},
18+
"gpt2_medium_sq": {
19+
"model_src_dir": "nlp/large_language_models/quantization/ptq/smoothquant",
20+
"dataset_location": "",
21+
"input_model": "gpt2-medium",
22+
"main_script": "main.py",
23+
"batch_size": 16
24+
},
25+
"gpt-j-6B": {
26+
"model_src_dir": "nlp/large_language_models/quantization/ptq/gpt-j",
27+
"dataset_location": "",
28+
"input_model": "/tf_dataset2/models/tensorflow/gpt-j-6B",
29+
"main_script": "main.py",
30+
"batch_size": 1
31+
},
32+
"ViT": {
33+
"model_src_dir": "image_recognition/vision_transformer/quantization/ptq",
34+
"dataset_location": "/tf_dataset/dataset/imagenet",
35+
"input_model": "/tf_dataset/tensorflow/vit/HF-ViT-Base16-Img224-frozen.pb",
36+
"main_script": "main.py",
37+
"batch_size": 32
38+
},
39+
"GraphSage": {
40+
"model_src_dir": "graph_networks/graphsage/quantization/ptq",
41+
"dataset_location": "/tf_dataset/dataset/ppi",
42+
"input_model": "/tf_dataset/tensorflow/graphsage/graphsage_frozen_model.pb",
43+
"main_script": "main.py",
44+
"batch_size": 1000
45+
}
46+
}
47+
}
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
Step-by-Step
2+
============
3+
4+
This document is used to list steps of reproducing TensorFlow Object Detection models tuning results. This example can run on Intel CPUs and GPUs.
5+
6+
# Prerequisite
7+
8+
9+
## 1. Environment
10+
Recommend python 3.6 or higher version.
11+
12+
### Install Intel® Neural Compressor
13+
```shell
14+
pip install neural-compressor
15+
```
16+
17+
### Install Intel Tensorflow
18+
```shell
19+
pip install intel-tensorflow
20+
```
21+
> Note: Validated TensorFlow [Version](/docs/source/installation_guide.md#validated-software-environment).
22+
23+
### Installation Dependency packages
24+
```shell
25+
cd examples\tensorflow\graph_networks\graphsage\quantization\ptq
26+
pip install -r requirements.txt
27+
```
28+
29+
### Install Intel Extension for Tensorflow
30+
31+
#### Quantizing the model on Intel GPU(Mandatory to install ITEX)
32+
Intel Extension for Tensorflow is mandatory to be installed for quantizing the model on Intel GPUs.
33+
34+
```shell
35+
pip install --upgrade intel-extension-for-tensorflow[xpu]
36+
```
37+
For any more details, please follow the procedure in [install-gpu-drivers](https://github.com/intel/intel-extension-for-tensorflow/blob/main/docs/install/install_for_xpu.md#install-gpu-drivers)
38+
39+
#### Quantizing the model on Intel CPU(Optional to install ITEX)
40+
Intel Extension for Tensorflow for Intel CPUs is experimental currently. It's not mandatory for quantizing the model on Intel CPUs.
41+
42+
```shell
43+
pip install --upgrade intel-extension-for-tensorflow[cpu]
44+
```
45+
46+
> **Note**:
47+
> The version compatibility of stock Tensorflow and ITEX can be checked [here](https://github.com/intel/intel-extension-for-tensorflow#compatibility-table). Please make sure you have installed compatible Tensorflow and ITEX.
48+
49+
## 2. Prepare Model
50+
Download Frozen graph:
51+
```shell
52+
wget https://storage.googleapis.com/intel-optimized-tensorflow/models/2_12_0/graphsage_frozen_model.pb
53+
```
54+
55+
## 3. Prepare Dataset
56+
57+
```shell
58+
wget https://snap.stanford.edu/graphsage/ppi.zip
59+
unzip ppi.zip
60+
```
61+
62+
# Run
63+
64+
## 1. Quantization
65+
66+
```shell
67+
# The cmd of running faster_rcnn_resnet50
68+
bash run_quant.sh --input_model=./graphsage_frozen_model.pb --output_model=./nc_graphsage_int8_model.pb --dataset_location=./ppi
69+
```
70+
71+
## 2. Benchmark
72+
```shell
73+
bash run_benchmark.sh --input_model=./nc_graphsage_int8_model.pb --dataset_location=./ppi --mode=performance
74+
```
75+
76+
Details of enabling Intel® Neural Compressor on graphsage for Tensorflow.
77+
=========================
78+
79+
This is a tutorial of how to enable graphsage model with Intel® Neural Compressor.
80+
## User Code Analysis
81+
User specifies fp32 *model*, calibration dataset *calib_dataloader* and a custom *eval_func* which encapsulates the evaluation dataset and metric by itself.
82+
83+
For graphsage, we applied the latter one because our philosophy is to enable the model with minimal changes. Hence we need to make two changes on the original code. The first one is to implement the q_dataloader and make necessary changes to *eval_func*.
84+
85+
### Code update
86+
87+
After prepare step is done, we just need update main.py like below.
88+
```python
89+
if args.tune:
90+
from neural_compressor.tensorflow import StaticQuantConfig, quantize_model
91+
from neural_compressor.tensorflow.utils import BaseDataLoader
92+
93+
dataset = CustomDataset()
94+
calib_dataloader = BaseDataLoader(dataset=dataset, batch_size=1, collate_fn=collate_function)
95+
quant_config = StaticQuantConfig()
96+
q_model = quantize_model(args.input_graph, quant_config, calib_dataloader)
97+
q_model.save(args.output_graph)
98+
99+
if args.benchmark:
100+
if args.mode == 'performance':
101+
evaluate(args.input_graph)
102+
elif args.mode == 'accuracy':
103+
acc_result = evaluate(args.input_graph)
104+
print("Batch size = %d" % args.batch_size)
105+
print("Accuracy: %.5f" % acc_result)
106+
107+
```
108+
109+
The quantization.fit() function will return a best quantized model during timeout constrain.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2024 Intel Corporation
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
import numpy as np
20+
import random
21+
import json
22+
import sys
23+
import os
24+
25+
import networkx as nx
26+
from networkx.readwrite import json_graph
27+
28+
29+
def load_data(prefix, normalize=True, load_walks=False):
30+
G_data = json.load(open(prefix + "-G.json"))
31+
G = json_graph.node_link_graph(G_data)
32+
if isinstance(list(G.nodes())[0], int):
33+
conversion = lambda n : int(n)
34+
else:
35+
conversion = lambda n : n
36+
37+
if os.path.exists(prefix + "-feats.npy"):
38+
feats = np.load(prefix + "-feats.npy")
39+
else:
40+
print("No features present.. Only identity features will be used.")
41+
feats = None
42+
id_map = json.load(open(prefix + "-id_map.json"))
43+
id_map = {conversion(k):int(v) for k,v in id_map.items()}
44+
walks = []
45+
class_map = json.load(open(prefix + "-class_map.json"))
46+
if isinstance(list(class_map.values())[0], list):
47+
lab_conversion = lambda n : n
48+
else:
49+
lab_conversion = lambda n : int(n)
50+
51+
class_map = {conversion(k):lab_conversion(v) for k,v in class_map.items()}
52+
53+
## Remove all nodes that do not have val/test annotations
54+
## (necessary because of networkx weirdness with the Reddit data)
55+
broken_count = 0
56+
for node in G.nodes():
57+
if not 'val' in G.nodes[node] or not 'test' in G.nodes[node]:
58+
G.remove_node(node)
59+
broken_count += 1
60+
print("Removed {:d} nodes that lacked proper annotations due to networkx versioning issues".format(broken_count))
61+
62+
## Make sure the graph has edge train_removed annotations
63+
## (some datasets might already have this..)
64+
print("Loaded data.. now preprocessing..")
65+
for edge in G.edges():
66+
if (G.nodes[edge[0]]['val'] or G.nodes[edge[1]]['val'] or
67+
G.nodes[edge[0]]['test'] or G.nodes[edge[1]]['test']):
68+
G[edge[0]][edge[1]]['train_removed'] = True
69+
else:
70+
G[edge[0]][edge[1]]['train_removed'] = False
71+
72+
if normalize and not feats is None:
73+
from sklearn.preprocessing import StandardScaler
74+
train_ids = np.array([id_map[n] for n in G.nodes() if not G.nodes[n]['val'] and not G.nodes[n]['test']])
75+
train_feats = feats[train_ids]
76+
scaler = StandardScaler()
77+
scaler.fit(train_feats)
78+
feats = scaler.transform(feats)
79+
80+
return G, feats, id_map, walks, class_map

0 commit comments

Comments
 (0)