Skip to content

Commit 30142f1

Browse files
authored
Adjust notebooks examples (#1346)
1 parent ebc8afe commit 30142f1

File tree

7 files changed

+74
-37
lines changed

7 files changed

+74
-37
lines changed

examples/notebook/onnxruntime/Quick_Started_Notebook_of_INC_for_ONNXRuntime.ipynb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@
4747
"outputs": [],
4848
"source": [
4949
"# install neural-compressor from source\n",
50+
"import sys\n",
5051
"!git clone https://github.com/intel/neural-compressor.git\n",
5152
"%cd ./neural-compressor\n",
52-
"!pip install -r requirements.txt\n",
53-
"!python setup.py install\n",
53+
"!{sys.executable} -m pip install -r requirements.txt\n",
54+
"!{sys.executable} setup.py install\n",
5455
"%cd ..\n",
5556
"# or install stable basic version from pypi\n",
56-
"# pip install neural-compressor"
57+
"# pip install neural-compressor\n"
5758
]
5859
},
5960
{
@@ -65,10 +66,8 @@
6566
},
6667
"outputs": [],
6768
"source": [
68-
"# install onnx related packages\n",
69-
"!pip install onnx onnxruntime onnxruntime-extensions\n",
70-
"# install other packages used in this notebook.\n",
71-
"!pip install torch transformers accelerate coloredlogs sympy numpy sentencepiece protobuf optimum"
69+
"# install required packages\n",
70+
"!{sys.executable} install -r requirements.txt\n"
7271
]
7372
},
7473
{
@@ -168,7 +167,7 @@
168167
"source": [
169168
"!export GLUE_DIR=./glue_data\n",
170169
"!wget https://raw.githubusercontent.com/Shimao-Zhang/Download_GLUE_Data/master/download_glue_data.py\n",
171-
"!python download_glue_data.py --data_dir=GLUE_DIR --tasks=SST"
170+
"!{sys.executable} download_glue_data.py --data_dir=GLUE_DIR --tasks=SST\n"
172171
]
173172
},
174173
{
@@ -193,7 +192,7 @@
193192
"int8_model_path = \"onnx-model/int8-model.onnx\"\n",
194193
"data_path = \"./GLUE_DIR/SST-2\"\n",
195194
"task = \"sst-2\"\n",
196-
"batch_size = 8"
195+
"batch_size = 8\n"
197196
]
198197
},
199198
{
@@ -343,7 +342,7 @@
343342
" label=label\n",
344343
" )\n",
345344
" features.append(feats)\n",
346-
" return features"
345+
" return features\n"
347346
]
348347
},
349348
{
@@ -377,7 +376,7 @@
377376
" model_name_or_path=model_name_or_path,\n",
378377
" model_type=\"distilbert\",\n",
379378
" task=task)\n",
380-
"dataloader = DataLoader(framework=\"onnxruntime\", dataset=dataset, batch_size=batch_size)"
379+
"dataloader = DataLoader(framework=\"onnxruntime\", dataset=dataset, batch_size=batch_size)\n"
381380
]
382381
},
383382
{
@@ -448,7 +447,7 @@
448447
" elif output_mode == \"regression\":\n",
449448
" processed_preds = np.squeeze(self.pred_list)\n",
450449
" result = transformers.glue_compute_metrics(self.task, processed_preds, self.label_list)\n",
451-
" return result[self.return_key[self.task]]"
450+
" return result[self.return_key[self.task]]\n"
452451
]
453452
},
454453
{
@@ -486,7 +485,7 @@
486485
" ort_inputs.update({inputs_names[i]: inputs[i]})\n",
487486
" predictions = session.run(None, ort_inputs)\n",
488487
" metric.update(predictions[0], labels)\n",
489-
" return metric.result()"
488+
" return metric.result()\n"
490489
]
491490
},
492491
{
@@ -567,7 +566,7 @@
567566
" num_heads=num_heads,\n",
568567
" hidden_size=hidden_size,\n",
569568
" optimization_options=opt_options)\n",
570-
"model = model_optimizer.model"
569+
"model = model_optimizer.model\n"
571570
]
572571
},
573572
{
@@ -722,7 +721,7 @@
722721
" config,\n",
723722
" eval_func=eval_func,\n",
724723
" calib_dataloader=dataloader)\n",
725-
"q_model.save(int8_model_path)"
724+
"q_model.save(int8_model_path)\n"
726725
]
727726
},
728727
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
onnx
2+
onnxruntime
3+
onnxruntime-extensions
4+
torch
5+
transformers
6+
accelerate
7+
coloredlogs
8+
sympy
9+
numpy
10+
sentencepiece
11+
protobuf
12+
optimum

examples/notebook/pytorch/Quick_Started_Notebook_of_INC_for_Pytorch.ipynb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@
4545
"outputs": [],
4646
"source": [
4747
"# install neural-compressor from source\n",
48+
"import sys\n",
4849
"!git clone https://github.com/intel/neural-compressor.git\n",
4950
"%cd ./neural-compressor\n",
50-
"!pip install -r requirements.txt\n",
51-
"!python setup.py install\n",
51+
"!{sys.executable} -m pip install -r requirements.txt\n",
52+
"!{sys.executable} setup.py install\n",
5253
"%cd ..\n",
5354
"\n",
5455
"# or install stable basic version from pypi\n",
55-
"!pip install neural-compressor"
56+
"!{sys.executable} -m pip install neural-compressor\n"
5657
]
5758
},
5859
{
@@ -62,7 +63,7 @@
6263
"outputs": [],
6364
"source": [
6465
"# install other packages used in this notebook.\n",
65-
"!pip install torch>=1.9.0 transformers>=4.16.0 accelerate sympy numpy sentencepiece!=0.1.92 protobuf<=3.20.3 datasets>=1.1.3 scipy scikit-learn Keras-Preprocessing"
66+
"!{sys.executable} -m pip install -r requirements.txt\n"
6667
]
6768
},
6869
{
@@ -303,10 +304,10 @@
303304
"outputs": [],
304305
"source": [
305306
"# fp32 benchmark\n",
306-
"!python benchmark.py --input_model ./pytorch_model.bin 2>&1|tee fp32_benchmark.log\n",
307+
"!{sys.executable} benchmark.py --input_model ./pytorch_model.bin 2>&1|tee fp32_benchmark.log\n",
307308
"\n",
308309
"# int8 benchmark\n",
309-
"!python benchmark.py --input_model ./saved_results/best_model.pt 2>&1|tee int8_benchmark.log\n"
310+
"!{sys.executable} benchmark.py --input_model ./saved_results/best_model.pt 2>&1|tee int8_benchmark.log\n"
310311
]
311312
}
312313
],
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
torch>=1.9.0
2+
transformers>=4.16.0
3+
accelerate
4+
sympy
5+
numpy
6+
sentencepiece!=0.1.92
7+
protobuf<=3.20.3
8+
datasets>=1.1.3
9+
scipy
10+
scikit-learn
11+
Keras-Preprocessing
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
numpy
2+
neural-compressor
3+
tensorflow
4+
datasets
5+
requests
6+
urllib3
7+
pyOpenSSL
8+
git+https://github.com/huggingface/huggingface_hub

examples/notebook/tensorflow/resnet/resnet_quantization.ipynb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@
2929
"metadata": {},
3030
"outputs": [],
3131
"source": [
32+
"import sys\n",
3233
"!conda install python==3.10 -y\n",
33-
"!pip install neural-compressor\n",
34-
"!wget -nc https://storage.googleapis.com/intel-optimized-tensorflow/models/v1_6/resnet50_fp32_pretrained_model.pb\n",
35-
"!pip install tensorflow\n",
36-
"!pip install datasets\n",
37-
"!pip install git+https://github.com/huggingface/huggingface_hub"
34+
"!{sys.executable} -m pip install -r requirements.txt \n",
35+
"\n",
36+
"!wget -nc https://storage.googleapis.com/intel-optimized-tensorflow/models/v1_6/resnet50_fp32_pretrained_model.pb\n"
3837
]
3938
},
4039
{
@@ -43,9 +42,11 @@
4342
"metadata": {},
4443
"outputs": [],
4544
"source": [
45+
"print(sys.executable)\n",
46+
"!{sys.executable} -m pip list\n",
4647
"import tensorflow as tf\n",
4748
"import numpy as np\n",
48-
"import datasets"
49+
"import datasets\n"
4950
]
5051
},
5152
{
@@ -63,8 +64,8 @@
6364
"source": [
6465
"# login to huggingface to download the imagenet-1k dataset\n",
6566
"# you should replace this read-only token with your own by create one on (https://huggingface.co/settings/tokens)\n",
66-
"# !huggingface-cli login --token <YOUR HUGGINGFACE TOKEN>\n",
67-
"!huggingface-cli login --token hf_xxxxxxxxxxxxxxxxxxxxxx"
67+
"from huggingface_hub.hf_api import HfFolder\n",
68+
"HfFolder.save_token('hf_xxxxxxxxxxxxxxxxxxxxxx')\n"
6869
]
6970
},
7071
{
@@ -75,8 +76,8 @@
7576
"source": [
7677
"from datasets import load_dataset\n",
7778
"# load dataset in streaming way will get an IterableDatset\n",
78-
"calib_dataset = load_dataset('imagenet-1k', split='train', streaming=True, use_auth_token=True)\n",
79-
"eval_dataset = load_dataset('imagenet-1k', split='validation', streaming=True, use_auth_token=True)"
79+
"calib_dataset = load_dataset('imagenet-1k', split='train', streaming=True, token=True)\n",
80+
"eval_dataset = load_dataset('imagenet-1k', split='validation', streaming=True, token=True)\n"
8081
]
8182
},
8283
{
@@ -97,7 +98,7 @@
9798
" return datasets.Dataset.from_dict(data)\n",
9899
"\n",
99100
"sub_calib_dataset = sample_data(calib_dataset, MAX_SAMPLE_LENGTG)\n",
100-
"sub_eval_dataset = sample_data(eval_dataset, MAX_SAMPLE_LENGTG)"
101+
"sub_eval_dataset = sample_data(eval_dataset, MAX_SAMPLE_LENGTG)\n"
101102
]
102103
},
103104
{
@@ -136,7 +137,7 @@
136137
" batch_inputs = []\n",
137138
" labels = []\n",
138139
" def __len__(self):\n",
139-
" return self.length"
140+
" return self.length\n"
140141
]
141142
},
142143
{
@@ -146,7 +147,7 @@
146147
"outputs": [],
147148
"source": [
148149
"calib_dataloader = CustomDataloader(dataset=sub_calib_dataset, batch_size=32)\n",
149-
"eval_dataloader = CustomDataloader(dataset=sub_eval_dataset, batch_size=32)"
150+
"eval_dataloader = CustomDataloader(dataset=sub_eval_dataset, batch_size=32)\n"
150151
]
151152
},
152153
{
@@ -193,7 +194,7 @@
193194
" return acc\n",
194195
"\n",
195196
"q_model = quantization.fit(\"./resnet50_fp32_pretrained_model.pb\", conf=conf, calib_dataloader=calib_dataloader, eval_func=eval_func)\n",
196-
"q_model.save(\"resnet50_int8.pb\")"
197+
"q_model.save(\"resnet50_int8.pb\")\n"
197198
]
198199
},
199200
{
@@ -221,7 +222,7 @@
221222
"metadata": {},
222223
"outputs": [],
223224
"source": [
224-
"!python resnet_benchmark.py --input_model resnet50_fp32_pretrained_model.pb 2>&1|tee fp32_benchmark.log"
225+
"!{sys.executable} resnet_benchmark.py --input_model resnet50_fp32_pretrained_model.pb 2>&1|tee fp32_benchmark.log\n"
225226
]
226227
},
227228
{
@@ -237,7 +238,7 @@
237238
"metadata": {},
238239
"outputs": [],
239240
"source": [
240-
"!python resnet_benchmark.py --input_model resnet50_int8.pb 2>&1|tee int8_benchmark.log"
241+
"!{sys.executable} resnet_benchmark.py --input_model resnet50_int8.pb 2>&1|tee int8_benchmark.log\n"
241242
]
242243
},
243244
{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
numpy
2+
matplotlib
3+
tensorflow
4+
tensorflow-hub
5+
tensorflow-datasets

0 commit comments

Comments
 (0)