Skip to content

Commit

Permalink
Update INC 3.0 New API for Keras Static Quant (#1400)
Browse files Browse the repository at this point in the history
Signed-off-by: zehao-intel <zehao.huang@intel.com>
Signed-off-by: chensuyue <suyue.chen@intel.com>
  • Loading branch information
zehao-intel authored Nov 28, 2023
1 parent 8160c71 commit 2627d33
Show file tree
Hide file tree
Showing 31 changed files with 2,518 additions and 16 deletions.
19 changes: 16 additions & 3 deletions .azure-pipelines/scripts/install_nc.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
#!/bin/bash

echo -e "\n Install Neural Compressor ... "
cd /neural-compressor
python -m pip install --no-cache-dir -r requirements.txt
python setup.py bdist_wheel
pip install dist/neural_compressor*.whl
if [[ $1 = *"3x_pt" ]]; then
python -m pip install --no-cache-dir -r requirements_pt.txt
python setup.py pt bdist_wheel
pip install dist/neural_compressor*.whl
elif [[ $1 = *"3x_tf" ]]; then
python -m pip install --no-cache-dir -r requirements_tf.txt
python setup.py tf bdist_wheel
pip install dist/neural_compressor*.whl
else
python -m pip install --no-cache-dir -r requirements.txt
python setup.py 2x bdist_wheel
pip install dist/neural_compressor*.whl
fi

echo -e "\n pip list after install Neural Compressor ... "
pip list
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source /neural-compressor/.azure-pipelines/scripts/change_color.sh

pip install coverage
export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/coverage.${1}
export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/3x/coverage.${1}
coverage_log="/neural-compressor/log_dir/coverage_log"
coverage_log_base="/neural-compressor/log_dir/coverage_log_base"
coverage_compare="/neural-compressor/log_dir/coverage_compare.html"
Expand All @@ -23,7 +23,7 @@ git config --global --add safe.directory /neural-compressor
git fetch
git checkout master
echo y | pip uninstall neural-compressor
cd /neural-compressor/.azure-pipelines/scripts && bash install_nc.sh
cd /neural-compressor/.azure-pipelines/scripts && bash install_nc.sh ${1}

coverage erase
cd /neural-compressor/log_dir
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions .azure-pipelines/scripts/ut/3x/coverage.3x_tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[run]
branch = True

[report]
include =
*/neural_compressor/common/*
*/neural_compressor/tensorflow/*
exclude_lines =
pragma: no cover
raise NotImplementedError
raise TypeError
if self.device == "gpu":
if device == "gpu":
except ImportError:
except Exception as e:
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#!/bin/bash
python -c "import neural_compressor as nc;print(nc.version.__version__)"
python -c "import neural_compressor as nc"
test_case="run 3x Torch"
echo "${test_case}"

# install requirements
echo "set up UT env..."
pip install -r /neural-compressor/requirements_pt.txt
pip install transformers
pip install coverage
pip install pytest
pip list

export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/coverage.pt
lpot_path=$(python -c 'import neural_compressor; import os; print(os.path.dirname(neural_compressor.__file__))')
export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/3x/coverage.3x_pt
inc_path=$(python -c 'import neural_compressor; print(neural_compressor.__path__[0])')
cd /neural-compressor/test || exit 1
find ./3x/torch/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${lpot_path}"' --append ,g' | sed 's/$/ --verbose/'> run.sh
find ./3x/torch/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'> run.sh

LOG_DIR=/neural-compressor/log_dir
mkdir -p ${LOG_DIR}
Expand Down
34 changes: 34 additions & 0 deletions .azure-pipelines/scripts/ut/3x/run_3x_tf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
python -c "import neural_compressor as nc"
test_case="run 3x TensorFlow"
echo "${test_case}"

# install requirements
echo "set up UT env..."
pip install coverage
pip install pytest
pip list

export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/3x/coverage.3x_tf
inc_path=$(python -c 'import neural_compressor; print(neural_compressor.__path__[0])')
cd /neural-compressor/test || exit 1
find ./3x/tensorflow/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'> run.sh

LOG_DIR=/neural-compressor/log_dir
mkdir -p ${LOG_DIR}
ut_log_name=${LOG_DIR}/ut_3x_tf.log

echo "cat run.sh..."
sort run.sh -o run.sh
cat run.sh | tee ${ut_log_name}
echo "------UT start-------"
bash -x run.sh 2>&1 | tee -a ${ut_log_name}
cp .coverage ${LOG_DIR}/.coverage

echo "------UT end -------"

if [ $(grep -c "FAILED" ${ut_log_name}) != 0 ] || [ $(grep -c "core dumped" ${ut_log_name}) != 0 ] || [ $(grep -c "ModuleNotFoundError:" ${ut_log_name}) != 0 ] || [ $(grep -c "OK" ${ut_log_name}) == 0 ];then
echo "Find errors in UT test, please check the output..."
exit 1
fi
echo "UT finished successfully! "
1 change: 1 addition & 0 deletions .azure-pipelines/scripts/ut/coverage.file
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ omit =
*/neural_compressor/template/*
*/neural_compressor/common/*
*/neural_compressor/torch/*
*/neural_compressor/tensorflow/*
exclude_lines =
pragma: no cover
raise NotImplementedError
Expand Down
2 changes: 1 addition & 1 deletion .azure-pipelines/template/ut-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ steps:

- script: |
docker exec ${{ parameters.utContainerName }} bash -c "cd /neural-compressor/.azure-pipelines/scripts \
&& bash install_nc.sh \
&& bash install_nc.sh ${{ parameters.utScriptFileName }} \
&& bash ut/${{ parameters.utScriptFileName }}.sh ${{ parameters.utTestMode }}"
displayName: "Run UT"
Expand Down
9 changes: 4 additions & 5 deletions .azure-pipelines/ut-3x-pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pr:
- neural_compressor/torch
- test/3x/torch
- setup.py
- requirements.txt
- requirements_pt.txt

pool: ICX-16C
Expand All @@ -36,7 +35,7 @@ stages:
- template: template/ut-template.yml
parameters:
dockerConfigName: "commonDockerConfig"
utScriptFileName: "run_3x_pt"
utScriptFileName: "3x/run_3x_pt"
uploadPath: $(UPLOAD_PATH)
utArtifact: "ut_coverage_3x"

Expand All @@ -51,7 +50,7 @@ stages:
- template: template/ut-template.yml
parameters:
dockerConfigName: "gitCloneDockerConfig"
utScriptFileName: "run_3x_pt"
utScriptFileName: "3x/run_3x_pt"
uploadPath: $(UPLOAD_PATH)
utArtifact: "ut_coverage_3x_baseline"
repo: $(REPO)
Expand Down Expand Up @@ -87,8 +86,8 @@ stages:
docker ps
echo "--- collect logs ---"
docker exec collectLogs /bin/bash +x -c "cd /neural-compressor/.azure-pipelines/scripts \
&& bash install_nc.sh \
&& bash ut/collect_log_3x.sh pt"
&& bash install_nc.sh 3x_pt \
&& bash ut/3x/collect_log_3x.sh 3x_pt"
displayName: "collect logs"
- task: PublishPipelineArtifact@1
Expand Down
106 changes: 106 additions & 0 deletions .azure-pipelines/ut-3x-tf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
trigger: none

pr:
autoCancel: true
drafts: false
branches:
include:
- master
paths:
include:
- neural_compressor/common
- neural_compressor/tensorflow
- test/3x/tensorflow
- setup.py
- requirements_tf.txt

pool: ICX-16C

variables:
IMAGE_NAME: "neural-compressor"
IMAGE_TAG: "py38"
UPLOAD_PATH: $(Build.SourcesDirectory)/log_dir
DOWNLOAD_PATH: $(Build.SourcesDirectory)/log_dir
ARTIFACT_NAME: "UT_coverage_report_3x_tf"
REPO: $(Build.Repository.Uri)

stages:
- stage: TensorFlow
displayName: Unit Test 3x TensorFlow
dependsOn: []
jobs:
- job:
displayName: Unit Test 3x TensorFlow
steps:
- template: template/ut-template.yml
parameters:
dockerConfigName: "commonDockerConfig"
utScriptFileName: "3x/run_3x_tf"
uploadPath: $(UPLOAD_PATH)
utArtifact: "ut_coverage_3x"


- stage: TensorFlow_baseline
displayName: Unit Test 3x TensorFlow baseline
dependsOn: []
jobs:
- job:
displayName: Unit Test 3x TensorFlow baseline
steps:
- template: template/ut-template.yml
parameters:
dockerConfigName: "gitCloneDockerConfig"
utScriptFileName: "3x/run_3x_tf"
uploadPath: $(UPLOAD_PATH)
utArtifact: "ut_coverage_3x_baseline"
repo: $(REPO)

- stage: Coverage
displayName: "Coverage Combine"
pool:
vmImage: "ubuntu-latest"
dependsOn: [TensorFlow, TensorFlow_baseline]
jobs:
- job: CollectDatafiles
steps:
- script: |
if [[ ! $(docker images | grep -i ${IMAGE_NAME}:${IMAGE_TAG}) ]]; then
docker build -f ${BUILD_SOURCESDIRECTORY}/.azure-pipelines/docker/Dockerfile.devel -t ${IMAGE_NAME}:${IMAGE_TAG} .
fi
docker images | grep -i ${IMAGE_NAME}
if [[ $? -ne 0 ]]; then
echo "NO Such Repo"
exit 1
fi
displayName: "Build develop docker image"
- task: DownloadPipelineArtifact@2
inputs:
artifact:
path: $(DOWNLOAD_PATH)

- script: |
echo "--- create container ---"
docker run -d -it --name="collectLogs" -v ${BUILD_SOURCESDIRECTORY}:/neural-compressor ${IMAGE_NAME}:${IMAGE_TAG} /bin/bash
echo "--- docker ps ---"
docker ps
echo "--- collect logs ---"
docker exec collectLogs /bin/bash +x -c "cd /neural-compressor/.azure-pipelines/scripts \
&& bash install_nc.sh 3x_tf \
&& bash ut/3x/collect_log_3x.sh 3x_tf"
displayName: "collect logs"
- task: PublishPipelineArtifact@1
condition: succeededOrFailed()
inputs:
targetPath: $(UPLOAD_PATH)
artifact: $(ARTIFACT_NAME)
publishLocation: "pipeline"

- task: Bash@3
condition: always()
inputs:
targetType: "inline"
script: |
docker exec collectLogs bash -c "rm -fr /neural-compressor/* && rm -fr /neural-compressor/.* || true"
displayName: "Docker clean up"
1 change: 1 addition & 0 deletions neural_compressor/common/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
BASE_CONFIG = "base_config"
COMPOSABLE_CONFIG = "composable_config"
RTN_WEIGHT_ONLY_QUANT = "rtn_weight_only_quant"
STATIC_QUANT = "static_quant"
GPTQ = "gptq"
DUMMY_CONFIG = "dummy_config"
17 changes: 17 additions & 0 deletions neural_compressor/tensorflow/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2023 Intel Corporation
#
# 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.

from neural_compressor.tensorflow.utils import register_algo
from neural_compressor.tensorflow.algorithms import static_quantize_entry
from neural_compressor.tensorflow.quantization import quantize_model, StaticQuantConfig, get_default_static_quant_config
16 changes: 16 additions & 0 deletions neural_compressor/tensorflow/algorithms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2023 Intel Corporation
#
# 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.


from neural_compressor.tensorflow.algorithms.static_quantize import static_quantize_entry
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) 2023 Intel Corporation
#
# 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.

from neural_compressor.tensorflow.algorithms.static_quantize.quantize_entry import static_quantize_entry
Loading

0 comments on commit 2627d33

Please sign in to comment.