From 54d2f580066866d450c0dafc99a34da8bc8680e8 Mon Sep 17 00:00:00 2001 From: vmadhu <33803027+vishnumadhu365@users.noreply.github.com> Date: Thu, 30 Mar 2023 10:51:53 +0530 Subject: [PATCH] notebook example update to INC2.0 API (#772) Signed-off-by: Vishnu Madhu Co-authored-by: u110737 (cherry picked from commit a02819251a43c9cc981f33dc702808060ed6341c) --- .../scripts/codeScan/pyspelling/inc_dict.txt | 3 +- examples/README.md | 2 +- .../pytorch/alexnet_fashion_mnist/README.md | 95 ++++++++++--------- .../alexnet_fashion_mnist/alexnet.yaml | 17 ---- .../alexnet_fashion_mnist/conda_set_env.sh | 10 -- .../devcloud_setup_env.sh | 11 --- .../inc_quantize_model.py | 62 ------------ .../inc_sample_for_pytorch.ipynb | 63 ++++++------ .../alexnet_fashion_mnist/output/readme.txt | 1 + .../alexnet_fashion_mnist/pip_set_env.sh | 11 --- .../alexnet_fashion_mnist/requirements.txt | 6 -- .../run_in_intel_devcloud.sh | 6 -- .../alexnet_fashion_mnist/run_sample.sh | 49 ---------- .../scripts/conda_set_env.sh | 8 ++ .../scripts/devcloud_setup_env.sh | 7 ++ .../scripts/pip_set_env.sh | 11 +++ .../{ => scripts/python_src}/alexnet.py | 0 .../{ => scripts/python_src}/compare_perf.py | 12 +-- .../{ => scripts/python_src}/fashion_mnist.py | 0 .../scripts/python_src/inc_quantize_model.py | 71 ++++++++++++++ .../{ => scripts/python_src}/profiling_inc.py | 4 +- .../train_alexnet_fashion_mnist.py | 2 +- .../scripts/requirements.txt | 7 ++ .../scripts/run_in_intel_devcloud.sh | 8 ++ .../{ => scripts}/run_jupyter.sh | 0 .../scripts/run_sample.sh | 48 ++++++++++ 26 files changed, 255 insertions(+), 259 deletions(-) delete mode 100644 examples/notebook/pytorch/alexnet_fashion_mnist/alexnet.yaml delete mode 100644 examples/notebook/pytorch/alexnet_fashion_mnist/conda_set_env.sh delete mode 100644 examples/notebook/pytorch/alexnet_fashion_mnist/devcloud_setup_env.sh delete mode 100644 examples/notebook/pytorch/alexnet_fashion_mnist/inc_quantize_model.py create mode 100644 examples/notebook/pytorch/alexnet_fashion_mnist/output/readme.txt delete mode 100644 examples/notebook/pytorch/alexnet_fashion_mnist/pip_set_env.sh delete mode 100644 examples/notebook/pytorch/alexnet_fashion_mnist/requirements.txt delete mode 100644 examples/notebook/pytorch/alexnet_fashion_mnist/run_in_intel_devcloud.sh delete mode 100644 examples/notebook/pytorch/alexnet_fashion_mnist/run_sample.sh create mode 100755 examples/notebook/pytorch/alexnet_fashion_mnist/scripts/conda_set_env.sh create mode 100755 examples/notebook/pytorch/alexnet_fashion_mnist/scripts/devcloud_setup_env.sh create mode 100755 examples/notebook/pytorch/alexnet_fashion_mnist/scripts/pip_set_env.sh rename examples/notebook/pytorch/alexnet_fashion_mnist/{ => scripts/python_src}/alexnet.py (100%) mode change 100644 => 100755 rename examples/notebook/pytorch/alexnet_fashion_mnist/{ => scripts/python_src}/compare_perf.py (89%) mode change 100644 => 100755 rename examples/notebook/pytorch/alexnet_fashion_mnist/{ => scripts/python_src}/fashion_mnist.py (100%) mode change 100644 => 100755 create mode 100755 examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/inc_quantize_model.py rename examples/notebook/pytorch/alexnet_fashion_mnist/{ => scripts/python_src}/profiling_inc.py (97%) mode change 100644 => 100755 rename examples/notebook/pytorch/alexnet_fashion_mnist/{ => scripts/python_src}/train_alexnet_fashion_mnist.py (85%) mode change 100644 => 100755 create mode 100644 examples/notebook/pytorch/alexnet_fashion_mnist/scripts/requirements.txt create mode 100755 examples/notebook/pytorch/alexnet_fashion_mnist/scripts/run_in_intel_devcloud.sh rename examples/notebook/pytorch/alexnet_fashion_mnist/{ => scripts}/run_jupyter.sh (100%) mode change 100644 => 100755 create mode 100755 examples/notebook/pytorch/alexnet_fashion_mnist/scripts/run_sample.sh diff --git a/.azure-pipelines/scripts/codeScan/pyspelling/inc_dict.txt b/.azure-pipelines/scripts/codeScan/pyspelling/inc_dict.txt index 43de4a3c408..b7a079f0b27 100644 --- a/.azure-pipelines/scripts/codeScan/pyspelling/inc_dict.txt +++ b/.azure-pipelines/scripts/codeScan/pyspelling/inc_dict.txt @@ -2593,4 +2593,5 @@ instancenorm leakyrelu llamanorm nbias -pc \ No newline at end of file +pc +cdrdv diff --git a/examples/README.md b/examples/README.md index 71789b9751a..ddd95557464 100644 --- a/examples/README.md +++ b/examples/README.md @@ -17,7 +17,7 @@ Intel® Neural Compressor validated examples with multiple compression technique * *[BERT Mini SST2 performance boost with INC](/examples/notebook/bert_mini_distillation): train a BERT-Mini model on SST-2 dataset through distillation, and leverage quantization to accelerate the inference while maintaining the accuracy using Intel® Neural Compressor. * [Performance of FP32 Vs. INT8 ResNet50 Model](/examples/notebook/perf_fp32_int8_tf): compare existed FP32 & INT8 ResNet50 model directly. -* *[Intel® Neural Compressor Sample for PyTorch*](/examples/notebook/pytorch/alexnet_fashion_mnist): an End-To-End pipeline to build up a CNN model by PyTorch to recognize fashion image and speed up AI model by Intel® Neural Compressor. +* [Intel® Neural Compressor Sample for PyTorch*](/examples/notebook/pytorch/alexnet_fashion_mnist): an End-To-End pipeline to build up a CNN model by PyTorch to recognize fashion image and speed up AI model by Intel® Neural Compressor. * [Intel® Neural Compressor Sample for TensorFlow*](/examples/notebook/tensorflow/alexnet_mnist): an End-To-End pipeline to build up a CNN model by TensorFlow to recognize handwriting number and speed up AI model by Intel® Neural Compressor. * [Accelerate VGG19 Inference on Intel® Gen4 Xeon® Sapphire Rapids](/examples/notebook/tensorflow/vgg19_ibean): an End-To-End pipeline to train VGG19 model by transfer learning based on pre-trained model from [TensorFlow Hub](https://tfhub.dev); quantize it by Intel® Neural Compressor on Intel® Gen4 Xeon® Sapphire Rapids. diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/README.md b/examples/notebook/pytorch/alexnet_fashion_mnist/README.md index f62d7b62e0a..eaead4041b5 100644 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/README.md +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/README.md @@ -3,28 +3,28 @@ ## Background -Low-precision inference can speed up inference obviously, by converting the fp32 model to int8 or bf16 model. Intel provides Intel® Deep Learning Boost technology in the Second Generation Intel® Xeon® Scalable Processors and newer Xeon®, which supports to speed up int8 and bf16 model by hardware. +Low-precision inference can significantly speed up inference pipelines. This is achieved by converting an FP32 model to quantized INT8 or BF16 model. Second Generation Intel® Xeon® Scalable Processors (and newer) have Intel® Deep Learning Boost technology, which provides dedicated silicon for speeding up INT8 and BF16 operations. -Intel® Neural Compressor helps the user to simplify the processing to convert the fp32 model to int8/bf16. +Intel® Neural Compressor (INC in short) helps developers in quantizing models, thereby converting an FP32 model into lower precisions like INT8 and BF16. At the same time, Intel® Neural Compressor will tune the quantization method to reduce the accuracy loss, which is a big blocker for low-precision inference. -Intel® Neural Compressor is released in Intel® AI Analytics Toolkit and works with Intel® Optimization of PyTorch*. +Intel® Neural Compressor is packaged into Intel® AI Analytics Toolkit and works with Intel® Optimization for PyTorch*. Please refer to the official website for detailed info and news: [https://github.com/intel/neural-compressor](https://github.com/intel/neural-compressor) ## Introduction -This is a demo to show an End-To-End pipeline to build up a CNN model by Pytorch to recognize fashion image and speed up AI model by Intel® Neural Compressor. +This sample is an End-To-End pipeline which demonstrates the usage specifics of the Intel® Neural Compressor. The pipeline does the following: -1. Train a CNN AlexNet model by PyTorch based on dataset Fashion-MNIST. +1. Using Pytorch, **Train** an ResNet50 model(CNN) on the Fashion-MNIST dataset. -2. Quantize the frozen PB model file by Intel® Neural Compressor to INT8 model. +2. Using the Intel® Neural Compressor, **quantize** the FP32 Pytorch model file(.pth) to an INT8 model. -3. Compare the performance of FP32 and INT8 model by same script. +3. **Compare** the inference performance of the FP32 and INT8 model. -We will learn the acceleration of AI inference by Intel AI technology: +The sample showcases AI inference performance optimizations delivered by, 1. Intel® Deep Learning Boost @@ -39,21 +39,24 @@ We will learn the acceleration of AI inference by Intel AI technology: |Test performance|profiling_inc.py|alexnet_mnist_fp32_mod.pth
alexnet_mnist_int8_mod|32.json
8.json| |Compare the performance|compare_perf.py|32.json
8.json|stdout/stderr
log file
fp32_int8_absolute.png
fp32_int8_times.png| -**run_sample.sh** will call above python scripts to finish the demo. +**run_sample.sh** will call above python scripts to finish the demo.
+Bash scripts are placed in 'scripts' directory
+Python files are placed in 'scripts/python_src' directory
+ ## Hardware Environment This demo could be executed on any Intel CPU. But it's recommended to use 2nd Generation Intel® Xeon® Scalable Processors or newer, which include: -1. AVX512 instruction to speed up training & inference AI model. - -2. Intel® Deep Learning Boost: Vector Neural Network Instruction (VNNI) to accelerate AI/DL Inference with INT8/BF16 Model. +1. AVX512 instruction to speed up training & inference of AI models. -With Intel® Deep Learning Boost, the performance will be increased obviously. +2. Intel® Deep Learning Boost: Vector Neural Network Instruction (VNNI) & [Intel® AMX](https://www.intel.in/content/www/in/en/products/docs/accelerator-engines/advanced-matrix-extensions/overview.html) (Advanced Matrix Extensions) to accelerate AI/DL Inference of INT8/BF16 Model. 3. Intel® DevCloud -If you have no such CPU support Intel® Deep Learning Boost, you could register to Intel® DevCloud and try this example on new Xeon with Intel® Deep Learning Boost freely. To learn more about working with Intel® DevCloud, please refer to [Intel® DevCloud](https://www.intel.com/content/www/us/en/developer/tools/devcloud/overview.html) +In case you don't have access to the latest Intel® Xeon® CPU's, you could use the Intel® DevCloud for running this sample.
+Intel® DevCloud offers free access to the newer Intel® hardware.
+To learn more about working with Intel® DevCloud, please refer to [Intel® DevCloud](https://devcloud.intel.com/oneapi/home/) ## Running Environment @@ -68,8 +71,8 @@ This article assumes you are familiar with Intel® DevCloud environment. To lear Specifically, this article assumes: 1. You have an Intel® DevCloud account. -2. You are familiar with usage of Intel® DevCloud, like login by SSH client.. -3. Developers are familiar with Python, AI model training and inference based on PyTorch*. +2. You are familiar with usage of Intel® DevCloud, like login by SSH client or using the Jupyter* lab interface. +3. You are familiar with Python, AI model training and inference based on PyTorch*. #### Setup based on Intel® oneAPI AI Analytics Toolkit @@ -78,26 +81,29 @@ Specifically, this article assumes: 2. Create virtual environment **env_inc**: ``` -./devcloud_setup_env.sh +cd neural-compressor/examples/notebook/pytorch/alexnet_fashion_mnist +chmod +x -R scripts/* +bash scripts/devcloud_setup_env.sh ``` +Note : If you are running this for the first time, it could take a while to download all the required packages. -#### Run in Jupyter Notebook in Intel® DevCloud for oneAPI +#### Run the Jupyter Notebook in Intel® DevCloud for oneAPI -Please open **inc_sample_for_pytorch.ipynb** in Jupyter Notebook. +Open **inc_sample_for_pytorch.ipynb** in Jupyter Notebook. Follow the steps in the notebook to complete the sample -Following the guide to run this demo. #### Run in SSH Login Intel® DevCloud for oneAPI -This demo will show the obviously acceleration by VNNI. In Intel® DevCloud, please choose compute node with the property 'clx' or 'icx' or 'spr' which support VNNI. +This demo is intended to show the performance acceleration provided by, +1. [Intel® VNNI](https://cdrdv2-public.intel.com/727804/dl-boost-product-overview.pdf) (Vector Neural Network Instructions). On Intel® DevCloud, choose compute node with the property 'clx' or 'icx' or 'spr'. These node types offer support for Intel® VNNI +2. [Intel® AMX](https://www.intel.in/content/www/in/en/products/docs/accelerator-engines/advanced-matrix-extensions/overview.html) (Advanced Matrix Extensions). On Intel® DevCloud, choose compute node with the property 'spr'. This node type offer support for Intel® AMX ##### Job Submit ``` -!qsub run_in_intel_devcloud.sh -d `pwd` -l nodes=1:icx:ppn=2 -28029.v-qsvr-nda.aidevcloud +qsub scripts/run_in_intel_devcloud.sh -d `pwd` -l nodes=1:icx:ppn=2 -o output/ -e output/ ``` -Note, please run above command in login node. There will be error as below if run it on compute node: +Note: You have to run the above command in the "login node". If you run it on the "compute node" by mistake, the system will throw an error message as below . ``` qsub: submit error (Bad UID for job execution MSG=ruserok failed validating uXXXXX/uXXXXX from s001-n054.aidevcloud) ``` @@ -105,10 +111,10 @@ qsub: submit error (Bad UID for job execution MSG=ruserok failed validating uXXX ##### Check job status ``` -qstat +qstat -a ``` -After the job is over (successfully or fault), there will be log files, like: +Once the job execution completes (either successfully or error-out), look out for log files in the 'output' directory. Below are two log file names for reference: 1. **run_in_intel_devcloud.sh.o28029** 2. **run_in_intel_devcloud.sh.e28029** @@ -119,6 +125,7 @@ After the job is over (successfully or fault), there will be log files, like: ``` tail -23 `ls -lAtr run_in_intel_devcloud.sh.o* | tail -1 | awk '{print $9}'` + ``` Or Check the result in a log file, like : **run_in_intel_devcloud.sh.o28029**: @@ -128,16 +135,16 @@ Check the result in a log file, like : **run_in_intel_devcloud.sh.o28029**: Model FP32 INT8 -throughput(fps) 572.4982883964987 3030.70552731285 -latency(ms) 2.8339174329018104 2.128233714979522 -accuracy(%) 0.9799 0.9796 +throughput(fps) xxx.4982883964987 xxx.70552731285 +latency(ms) x.8339174329018104 x.128233714979522 +accuracy(%) 0.x799 0.x796 Save to fp32_int8_absolute.png Model FP32 INT8 -throughput_times 1 5.293824608282245 -latency_times 1 0.7509864932092611 -accuracy_times 1 0.9996938463108482 +throughput_times 1 x.293824608282245 +latency_times 1 x.7509864932092611 +accuracy_times 1 0.x996938463108482 Save to fp32_int8_times.png Please check the PNG files to see the performance! @@ -153,11 +160,11 @@ Thank you! ``` -We will see the performance and accuracy of FP32 and INT8 model. The performance could be obviously increased if running on Xeon with VNNI. +The output shows the performance and accuracy of FP32 and INT8 model. ##### Check Result in PNG file -The demo creates figure files: fp32_int8_absolute.png, fp32_int8_times.png to show performance bar. They could be used in report. +The demo saves performance comparison as PNG files: fp32_int8_absolute.png, fp32_int8_times.png Copy files from DevCloud in host: @@ -172,7 +179,7 @@ Set up own running environment in local server, cloud (including Intel® DevClou #### Install by PyPi -Create virtual environment **env_inc**: +Create virtual environment **pip_env_inc**: ``` pip_set_env.sh @@ -180,7 +187,7 @@ pip_set_env.sh Activate it by: ``` -source env_inc/bin/activate +source pip_env_inc/bin/activate ``` #### Install by Conda @@ -200,7 +207,7 @@ conda activate env_inc #### Run by SSH ``` -./run_sample.sh +bash scripts/run_sample.sh ``` 1. Check the result in screen print out: @@ -208,16 +215,16 @@ conda activate env_inc ... Model FP32 INT8 -throughput(fps) 572.4982883964987 3030.70552731285 -latency(ms) 2.8339174329018104 2.128233714979522 -accuracy(%) 0.9799 0.9796 +throughput(fps) xxx.4982883964987 xxx.70552731285 +latency(ms) x.8339174329018104 x.128233714979522 +accuracy(%) 0.x799 0.x796 Save to fp32_int8_absolute.png Model FP32 INT8 -throughput_times 1 5.293824608282245 -latency_times 1 0.7509864932092611 -accuracy_times 1 0.9996938463108482 +throughput_times 1 x.293824608282245 +latency_times 1 x.7509864932092611 +accuracy_times 1 x.9996938463108482 Save to fp32_int8_times.png Please check the PNG files to see the performance! @@ -238,8 +245,6 @@ Please open **inc_sample_for_pytorch.ipynb** in Jupyter Notebook. Following the guide of chapter **Run in Customer Server or Cloud** to run this demo. - - ## License Code samples are licensed under the MIT license. See diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/alexnet.yaml b/examples/notebook/pytorch/alexnet_fashion_mnist/alexnet.yaml deleted file mode 100644 index f5376f4ef33..00000000000 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/alexnet.yaml +++ /dev/null @@ -1,17 +0,0 @@ -version: 1.0 - -model: - name: hello_world - framework: pytorch_fx # possible values are tensorflow, mxnet and pytorch - -evaluation: - accuracy: - metric: - topk: 1 - -tuning: - accuracy_criterion: - relative: 0.01 # the tuning target of accuracy loss percentage: 1% - exit_policy: - timeout: 0 # tuning timeout (seconds) - random_seed: 100 # random seed diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/conda_set_env.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/conda_set_env.sh deleted file mode 100644 index 4fed50e0055..00000000000 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/conda_set_env.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -ENV_NAME=env_inc -conda deactivate -conda env remove -n $ENV_NAME -conda create -n $ENV_NAME python=3.9 -c intel -y -conda activate $ENV_NAME -conda install -n $ENV_NAME pytorch -c intel -y -conda install -n $ENV_NAME neural-compressor runipy notebook ipykernel -c conda-forge -c intel -y -python -m ipykernel install --user --name $ENV_NAME diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/devcloud_setup_env.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/devcloud_setup_env.sh deleted file mode 100644 index 7342e40c763..00000000000 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/devcloud_setup_env.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -ENV_NAME=env_inc -conda deactivate -conda env remove -n $ENV_NAME -conda create -n $ENV_NAME -c ${ONEAPI_ROOT}/conda_channel python=`python -V| awk '{print $2}'` -y -conda activate $ENV_NAME -conda install -n $ENV_NAME -c ${ONEAPI_ROOT}/conda_channel pytorch torchvision torchaudio cpuonly -c intel -c pytorch -y -conda install -n $ENV_NAME -c ${ONEAPI_ROOT}/conda_channel neural-compressor -y --offline -conda install -c anaconda scipy -conda install -n $ENV_NAME runipy notebook -y diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/inc_quantize_model.py b/examples/notebook/pytorch/alexnet_fashion_mnist/inc_quantize_model.py deleted file mode 100644 index a58f1582415..00000000000 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/inc_quantize_model.py +++ /dev/null @@ -1,62 +0,0 @@ -""" -Environment Setting - -Enable Intel Optimized TensorFlow 2.6.0 and newer by setting environment variable TF_ENABLE_ONEDNN_OPTS=1 -That will accelerate training and inference, and it's mandatory requirement of running Intel® Neural Compressor quantize Fp32 model or deploying the quantized model. -""" - -import neural_compressor as inc -print("neural_compressor version {}".format(inc.__version__)) - -import torch -print("torch {}".format(torch.__version__)) - -from neural_compressor.experimental import Quantization, common -import fashion_mnist -import alexnet - - -def ver2int(ver): - s_vers = ver.split(".") - res = 0 - for i, s in enumerate(s_vers): - res += int(s)*(100**(2-i)) - - return res - -def compare_ver(src, dst): - src_ver = ver2int(src) - dst_ver = ver2int(dst) - if src_ver>dst_ver: - return 1 - if src_ver=0: - q_model = quantizer.fit() - else: - q_model = quantizer() - - return q_model - -def main(): - yaml_file = "alexnet.yaml" - batch_size = 200 - fp32_model_file = "alexnet_mnist_fp32_mod.pth" - int8_model = "alexnet_mnist_int8_mod" - - q_model = auto_tune(fp32_model_file, yaml_file, batch_size) - q_model.save(int8_model) - print("Save int8 model to {}".format(int8_model)) - -if __name__ == "__main__": - main() - \ No newline at end of file diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/inc_sample_for_pytorch.ipynb b/examples/notebook/pytorch/alexnet_fashion_mnist/inc_sample_for_pytorch.ipynb index 9205cfeb2cd..8783955ab03 100644 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/inc_sample_for_pytorch.ipynb +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/inc_sample_for_pytorch.ipynb @@ -9,29 +9,29 @@ ] }, { - "cell_type": "code", - "execution_count": null, - "id": "9ec9d220", + "cell_type": "markdown", + "id": "5fda805e-a405-4fa4-9a11-d890206a27d7", "metadata": { - "collapsed": true + "tags": [] }, - "outputs": [], "source": [ "## Introduction\n", "\n", - "This is a demo to show an End-To-End pipeline to speed up AI model by Intel® Neural Compressor.\n", + "This sample is an End-To-End pipeline which demonstrates the usage specifics of the Intel® Neural Compressor. The pipeline does the following:\n", "\n", - "1. Train a CNN AlexNet model by PyTorch based on dataset Fashion-MNIST.\n", + "1. Using Pytorch, **Train** an AlexNet model(CNN) on the Fashion-MNIST dataset.\n", "\n", - "2. Quantize the frozen PB model file by Intel® Neural Compressor to INT8 model.\n", + "2. Using the Intel® Neural Compressor, **quantize** the FP32 Pytorch model file(.pth) to an INT8 model.\n", "\n", - "3. Test and compare the performance of FP32 and INT8 model by same script.\n" + "3. **Compare** the inference performance of the FP32 and INT8 model." ] }, { "cell_type": "markdown", "id": "e538cd95-f291-41aa-9b48-00956855aec1", - "metadata": {}, + "metadata": { + "tags": [] + }, "source": [ "## Code\n", "Please refer to [README.md](README.md)." @@ -62,7 +62,8 @@ "metadata": {}, "outputs": [], "source": [ - "!rm -rf run_in_intel_devcloud.sh.*" + "# !!! CAREFUL !!!, this will delete output data from your previous runs\n", + "!rm -rf output/*" ] }, { @@ -82,7 +83,7 @@ "metadata": {}, "outputs": [], "source": [ - "!qsub run_in_intel_devcloud.sh -d `pwd` -l nodes=1:icx:ppn=2" + "!qsub scripts/run_in_intel_devcloud.sh -d `pwd` -l nodes=1:icx:ppn=2 -o output/ -e output/" ] }, { @@ -100,7 +101,7 @@ "metadata": {}, "outputs": [], "source": [ - "!qstat" + "!qstat -a" ] }, { @@ -121,25 +122,25 @@ "metadata": {}, "outputs": [], "source": [ - "!tail -23 `ls -lAtr run_in_intel_devcloud.sh.o* | tail -1 | awk '{print $9}'`" + "!tail -23 `ls -lAtr output/run_in_intel_devcloud.sh.o* | tail -1 | awk '{print $9}'`" ] }, { "cell_type": "markdown", - "id": "f9822cad-22e8-415d-b33b-fd12065c5163", + "id": "9a6a6651-cb8f-4667-9ddf-3b215ad35a00", "metadata": {}, "source": [ - "Check any existed log file, for example **run_in_intel_devcloud.sh.o1842343**" + "Check the error logs with prefix: **run_in_intel_devcloud.sh.e**" ] }, { "cell_type": "code", "execution_count": null, - "id": "7d49e69a-ccfe-42f0-982d-38395ed4a808", + "id": "7b69a349-0d84-4a25-af24-7fc6fd8a64c8", "metadata": {}, "outputs": [], "source": [ - "!tail -23 run_in_intel_devcloud.sh.o1842343" + "!tail -40 `ls -lAtr output/run_in_intel_devcloud.sh.e* | tail -1 | awk '{print $9}'`" ] }, { @@ -159,8 +160,8 @@ "source": [ "from IPython.display import Image, display\n", "\n", - "listOfImageNames = ['fp32_int8_aboslute.png',\n", - " 'fp32_int8_times.png']\n", + "listOfImageNames = ['output/fp32_int8_aboslute.png',\n", + " 'output/fp32_int8_times.png']\n", "\n", "for imageName in listOfImageNames:\n", " display(Image(filename=imageName))" @@ -214,20 +215,20 @@ "\n", "Compare the Performance of FP32 and INT8 Models\n", "Model FP32 INT8 \n", - "throughput(fps) 572.4982883964987 3218.52236638019 \n", - "latency(ms) 2.8339174329018104 1.9863116497896156 \n", - "accuracy(%) 0.9799 0.9796 \n", + "throughput(fps) xxx.4982883964987 xxx.52236638019 \n", + "latency(ms) x.8339174329018104 x.9863116497896156 \n", + "accuracy(%) 0.x799 0.x796 \n", "\n", "Save to fp32_int8_aboslute.png\n", "\n", "Model FP32 INT8 \n", - "throughput_times 1 5.621889936815179 \n", - "latency_times 1 0.7009066766478504 \n", - "accuracy_diff(%) 0 -0.029999999999986926 \n", + "throughput_times 1 x.621889936815179 \n", + "latency_times 1 0.x009066766478504 \n", + "accuracy_diff(%) 0 -0.x29999999999986926 \n", "\n", "Save to fp32_int8_times.png\n", - "Please check the PNG files to see the performance!\n", - "This demo is finished successfully!\n", + "Check the output PNG files for performance comparison!\n", + "Demo execution completed successfully! Check output directory for results.\n", "Thank you!\n", "...\n", "\n", @@ -264,9 +265,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3 (Intel® oneAPI 2023.0)", "language": "python", - "name": "python3" + "name": "c009-intel_distribution_of_python_3_oneapi-beta05-python" }, "language_info": { "codemirror_mode": { @@ -278,7 +279,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.10" + "version": "3.9.15" }, "nbTranslate": { "displayLangs": [ diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/output/readme.txt b/examples/notebook/pytorch/alexnet_fashion_mnist/output/readme.txt new file mode 100644 index 00000000000..902478c2a45 --- /dev/null +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/output/readme.txt @@ -0,0 +1 @@ +This directory will hold all the output files generated while running this Intel Neural Compressor sample. diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/pip_set_env.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/pip_set_env.sh deleted file mode 100644 index 09444e199b9..00000000000 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/pip_set_env.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -ENV_NAME=env_inc -deactivate -rm -rf $ENV_NAME -python -m venv $ENV_NAME -source $ENV_NAME/bin/activate -pip install --upgrade pip -pip install -r requirements.txt - - diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/requirements.txt b/examples/notebook/pytorch/alexnet_fashion_mnist/requirements.txt deleted file mode 100644 index 4e447feaaa2..00000000000 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ ---find-links https://download.pytorch.org/whl/torch_stable.html -torch==1.8.0+cpu -torchvision==0.9.0+cpu -neural-compressor -runipy -notebook \ No newline at end of file diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/run_in_intel_devcloud.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/run_in_intel_devcloud.sh deleted file mode 100644 index adf2209717a..00000000000 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/run_in_intel_devcloud.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -echo "Enable Conda Env." -source /glob/development-tools/versions/oneapi/2022.1.1/oneapi/intelpython/python3.9/etc/profile.d/conda.sh -conda activate env_inc -bash run_sample.sh diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/run_sample.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/run_sample.sh deleted file mode 100644 index 0aefa9a70b0..00000000000 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/run_sample.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/bash - -echo "Note, please enable running environment before running this script." - - -echo "Train Model by Pytorch with Fashion MNIST" -python train_alexnet_fashion_mnist.py - -FP32_FILE="alexnet_mnist_fp32_mod.pth" -if [ ! -f $FP32_FILE ]; then - echo "$FP32_FILE not exists." - echo "Train AlexNet model is fault, exit!" - exit 1 -else - echo "Training is finished" -fi - -echo "Quantize Model by Intel Neural Compressor" -python inc_quantize_model.py - -INT8_FOLDER="alexnet_mnist_int8_mod" -if [ ! -d $INT8_FOLDER ]; then - echo "$INT8_FOLDER not exists." - echo "Quantize FP32 model is fault, exit!" - exit 1 -else - echo "Quantization is finished" -fi - -echo "Execute the profiling_inc.py with FP32 model file" -python profiling_inc.py --input-graph=./alexnet_mnist_fp32_mod.pth --index=32 -echo "FP32 performance test is finished" - -echo "Execute the profiling_inc.py with INT8 model file" -python profiling_inc.py --input-graph=./alexnet_mnist_int8_mod --index=8 -echo "INT8 performance test is finished" - -echo "Compare the Performance of FP32 and INT8 Models" -python compare_perf.py -echo "Please check the PNG files to see the performance!" - -if [[ $? -eq 0 ]]; then - echo "This demo is finished successfully!" -else - echo "This demo is fault!" -fi - -echo "Thank you!" -exit 0 diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/conda_set_env.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/conda_set_env.sh new file mode 100755 index 00000000000..c82a5b54b97 --- /dev/null +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/conda_set_env.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +ENV_NAME=env_inc +conda deactivate +conda env remove -n $ENV_NAME +conda create -n $ENV_NAME python=3.9 pip pytorch=1.13.1 torchvision=0.14.1 torchaudio=0.13.1 cpuonly neural-compressor=2.0 matplotlib jupyter -y -c conda-forge -c pytorch -c intel +conda activate $ENV_NAME +python -m ipykernel install --user --name $ENV_NAME \ No newline at end of file diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/devcloud_setup_env.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/devcloud_setup_env.sh new file mode 100755 index 00000000000..ded2bccac4c --- /dev/null +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/devcloud_setup_env.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +source /opt/intel/oneapi/setvars.sh +ENV_NAME=env_inc +conda deactivate +conda env remove -n $ENV_NAME +conda create -n $ENV_NAME python=3.9 pip pytorch=1.13.1 torchvision=0.14.1 torchaudio=0.13.1 cpuonly neural-compressor=2.0 matplotlib -y -c conda-forge -c pytorch -c intel diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/pip_set_env.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/pip_set_env.sh new file mode 100755 index 00000000000..735f07788e9 --- /dev/null +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/pip_set_env.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +ENV_NAME=pip_env_inc +deactivate +rm -rf $ENV_NAME +python3 -m venv $ENV_NAME +source $ENV_NAME/bin/activate +pip3 install --upgrade pip +pip3 install -r requirements.txt + + diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/alexnet.py b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/alexnet.py old mode 100644 new mode 100755 similarity index 100% rename from examples/notebook/pytorch/alexnet_fashion_mnist/alexnet.py rename to examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/alexnet.py diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/compare_perf.py b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/compare_perf.py old mode 100644 new mode 100755 similarity index 89% rename from examples/notebook/pytorch/alexnet_fashion_mnist/compare_perf.py rename to examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/compare_perf.py index a1426b832f9..d5f83e1aa35 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/compare_perf.py +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/compare_perf.py @@ -41,8 +41,8 @@ def load_res(json_file): data = json.load(f) return data -res_32 = load_res('32.json') -res_8 = load_res('8.json') +res_32 = load_res('../output/32.json') +res_8 = load_res('../output/8.json') accuracys = [res_32['accuracy'], res_8['accuracy']] throughputs = [res_32['throughput'], res_8['throughput']] @@ -61,8 +61,8 @@ def load_res(json_file): draw_bar(x, t, throughputs, 131, 'tab:green', 'Throughput(fps)', '', width=0.2) draw_bar(x, t, latencys, 132, 'tab:blue', 'Latency(ms)', '', width=0.2) draw_bar(x, t, accuracys_perc, 133, '#28a99d', 'Accuracys(%)', '', width=0.2) -plt.savefig("fp32_int8_aboslute.png") -print("\nSave to fp32_int8_aboslute.png\n") +plt.savefig("../output/fp32_int8_aboslute.png") +print("\n Comparison plot saved to output/fp32_int8_aboslute.png\n") throughputs_times = [1, throughputs[1]/throughputs[0]] latencys_times = [1, latencys[1]/latencys[0]] @@ -78,5 +78,5 @@ def load_res(json_file): draw_bar(x, t, latencys_times, 132, 'tab:blue', 'Latency Normalized (small is better)', '', width=0.2) draw_bar(x, t, accuracys_times, 133, '#28a99d', 'Accuracys Normalized (small is better)', '', width=0.2) -plt.savefig("fp32_int8_times.png") -print("\nSave to fp32_int8_times.png") +plt.savefig("../output/fp32_int8_times.png") +print("\n Comparison plot saved to output/fp32_int8_times.png") diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/fashion_mnist.py b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/fashion_mnist.py old mode 100644 new mode 100755 similarity index 100% rename from examples/notebook/pytorch/alexnet_fashion_mnist/fashion_mnist.py rename to examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/fashion_mnist.py diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/inc_quantize_model.py b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/inc_quantize_model.py new file mode 100755 index 00000000000..833bf3c7783 --- /dev/null +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/inc_quantize_model.py @@ -0,0 +1,71 @@ +import neural_compressor as inc +print("neural_compressor version {}".format(inc.__version__)) + +import torch +print("torch {}".format(torch.__version__)) + +from neural_compressor.quantization import fit +from neural_compressor.config import PostTrainingQuantConfig, AccuracyCriterion, TuningCriterion, Options +from neural_compressor.data import DataLoader + +import fashion_mnist +import alexnet + + +def ver2int(ver): + s_vers = ver.split(".") + res = 0 + for i, s in enumerate(s_vers): + res += int(s)*(100**(2-i)) + + return res + +def compare_ver(src, dst): + src_ver = ver2int(src) + dst_ver = ver2int(dst) + if src_ver>dst_ver: + return 1 + if src_ver=0: + print(f"Compatible Intel Neural Compressor version detected : v{inc.__version__} ") + else: + raise Exception(f"Installed Intel Neural Compressor version[v{inc.__version__}] is NOT compatible. Please upgrade to version 2.0 or higher.") + + q_model = auto_tune(fp32_model_file, batch_size) + q_model.save(int8_model) + print("Save int8 model to {}".format(int8_model)) + +if __name__ == "__main__": + main() + \ No newline at end of file diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/profiling_inc.py b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/profiling_inc.py old mode 100644 new mode 100755 similarity index 97% rename from examples/notebook/pytorch/alexnet_fashion_mnist/profiling_inc.py rename to examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/profiling_inc.py index 7d44893a89a..93f5ac9d489 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/profiling_inc.py +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/profiling_inc.py @@ -9,7 +9,7 @@ def infer_perf(index, model_file): - train_loader, test_loader = alexnet.data_loader(batch_size=10000) + train_loader, test_loader = alexnet.data_loader(batch_size=100) if index=='8': model = alexnet.load_int8_mod(model_file) @@ -66,7 +66,7 @@ def save_res(index, result): res['throughput'] = throughput res['latency'] = latency - outfile = index + ".json" + outfile = "../output/" + str(index) + ".json" with open(outfile, 'w') as f: json.dump(res, f) print("Save result to {}".format(outfile)) diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/train_alexnet_fashion_mnist.py b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/train_alexnet_fashion_mnist.py old mode 100644 new mode 100755 similarity index 85% rename from examples/notebook/pytorch/alexnet_fashion_mnist/train_alexnet_fashion_mnist.py rename to examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/train_alexnet_fashion_mnist.py index 25ca4ca8db7..d96c8404d80 --- a/examples/notebook/pytorch/alexnet_fashion_mnist/train_alexnet_fashion_mnist.py +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/python_src/train_alexnet_fashion_mnist.py @@ -15,7 +15,7 @@ def main(): alexnet.train_mod(model, train_loader, test_loader, optimizer, epochs) alexnet.test_mod(model, test_loader) - alexnet.save_mod(model, "alexnet_mnist_fp32_mod.pth") + alexnet.save_mod(model, "../output/alexnet_mnist_fp32_mod.pth") if __name__ == "__main__": main() diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/requirements.txt b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/requirements.txt new file mode 100644 index 00000000000..ef6bf8858e4 --- /dev/null +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/requirements.txt @@ -0,0 +1,7 @@ +--find-links https://download.pytorch.org/whl/torch_stable.html +torch==1.13.1+cpu +torchvision==0.14.1+cpu +neural-compressor==2.0 +runipy +notebook +matplotlib \ No newline at end of file diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/run_in_intel_devcloud.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/run_in_intel_devcloud.sh new file mode 100755 index 00000000000..6195ba75e86 --- /dev/null +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/run_in_intel_devcloud.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +echo "Enable Conda Env." +source /opt/intel/oneapi/setvars.sh +conda activate env_inc +export PYTHONPATH=$(find $CONDA_PREFIX -type d -name "site-packages" | head -n 1) +cd scripts +bash run_sample.sh diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/run_jupyter.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/run_jupyter.sh old mode 100644 new mode 100755 similarity index 100% rename from examples/notebook/pytorch/alexnet_fashion_mnist/run_jupyter.sh rename to examples/notebook/pytorch/alexnet_fashion_mnist/scripts/run_jupyter.sh diff --git a/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/run_sample.sh b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/run_sample.sh new file mode 100755 index 00000000000..07ee8b215ec --- /dev/null +++ b/examples/notebook/pytorch/alexnet_fashion_mnist/scripts/run_sample.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +echo "Note: This script relies on relevant Python environment for its execution. Refer README.md for more details." + +echo "Train Pytorch AlexNet model on Fashion MNIST dataset" +python python_src/train_alexnet_fashion_mnist.py + +FP32_FILE="../output/alexnet_mnist_fp32_mod.pth" +if [ ! -f $FP32_FILE ]; then + echo "$FP32_FILE - model file does not exist" + echo "Model training failed, exiting!. Check error logs for details" + exit 1 +else + echo "Model training has completed successfully" +fi + +echo "Quantize Model using Intel Neural Compressor" +python python_src/inc_quantize_model.py + +INT8_FOLDER="../output/alexnet_mnist_int8_mod" +if [ ! -d $INT8_FOLDER ]; then + echo "$INT8_FOLDER not exists." + echo "Model quantization has failed, exiting!. Check error logs for details" + exit 1 +else + echo "Model quantization has completed successfully" +fi + +echo "Execute the profiling_inc.py with FP32 model file" +python python_src/profiling_inc.py --input-graph=../output/alexnet_mnist_fp32_mod.pth --index=32 +echo "FP32 model performance test has completed successfully" + +echo "Execute the profiling_inc.py with INT8 model file" +python python_src/profiling_inc.py --input-graph=../output/alexnet_mnist_int8_mod --index=8 +echo "INT8 model performance test has completed successfully" + +echo "Comparing the Performance of FP32 and INT8 Models" +python python_src/compare_perf.py +echo "Check the output PNG files for performance comparison!" + +if [[ $? -eq 0 ]]; then + echo "Demo execution completed successfully! Check output directory for results." +else + echo "Demo execution has failed! Check error logs for more details." +fi + +echo "Thank you!" +exit 0