Skip to content

Commit 7bf89eb

Browse files
authored
refactor folder of torch 3x API and refine RTN (#1560)
Signed-off-by: xin3he <[email protected]> Signed-off-by: chensuyue <[email protected]>
1 parent 02233fb commit 7bf89eb

File tree

34 files changed

+702
-770
lines changed

34 files changed

+702
-770
lines changed

.azure-pipelines/scripts/ut/3x/collect_log_3x.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
source /neural-compressor/.azure-pipelines/scripts/change_color.sh
22

3-
set -xe
3+
set -e
44
pip install coverage
55
export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/3x/coverage.${1}
66
coverage_log="/neural-compressor/log_dir/coverage_log"

.azure-pipelines/scripts/ut/3x/run_3x_ort.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ cp .coverage ${LOG_DIR}/.coverage
2828

2929
echo "------UT end -------"
3030

31-
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
31+
if [ $(grep -c "FAILED" ${ut_log_name}) != 0 ] || [ $(grep -c "core dumped" ${ut_log_name}) != 0 ] \
32+
|| [ $(grep -c "ModuleNotFoundError:" ${ut_log_name}) != 0 ] || [ $(grep -c "ImportError:" ${ut_log_name}) != 0 ] || [ $(grep -c "OK" ${ut_log_name}) == 0 ];then
3233
echo "Find errors in UT test, please check the output..."
3334
exit 1
3435
fi

.azure-pipelines/scripts/ut/3x/run_3x_pt.sh

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,55 @@ pip list
1111

1212
export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/3x/coverage.3x_pt
1313
inc_path=$(python -c 'import neural_compressor; print(neural_compressor.__path__[0])')
14-
cd /neural-compressor/test || exit 1
15-
find ./3x/torch/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'> run.sh
16-
find ./3x/common/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'>> run.sh
14+
cd /neural-compressor/test/3x || exit 1
15+
grep -lrv "import pytest" --include="test*.py" ./torch | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'> run_unittest.sh
16+
grep -lrv "import pytest" --include="test*.py" ./common | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'>> run_unittest.sh
17+
grep -lr "import pytest" --include="test*.py" ./torch | sed 's,\.\/,coverage run --source='"${inc_path}"' --append -m pytest --disable-warnings -v ,g' > run_pytest.sh
18+
grep -lr "import pytest" --include="test*.py" ./common | sed 's,\.\/,coverage run --source='"${inc_path}"' --append -m pytest --disable-warnings -v ,g'>> run_pytest.sh
1719

1820
LOG_DIR=/neural-compressor/log_dir
1921
mkdir -p ${LOG_DIR}
2022
ut_log_name=${LOG_DIR}/ut_3x_pt.log
2123

22-
echo "cat run.sh..."
23-
sort run.sh -o run.sh
24-
cat run.sh | tee ${ut_log_name}
25-
echo "------UT start-------"
26-
bash -x run.sh 2>&1 | tee -a ${ut_log_name}
24+
# unittest and pytest has some incompatible issue, so separate the test.
25+
echo "cat run_unittest.sh..."
26+
sort run_unittest.sh -o run_unittest.sh
27+
cat run_unittest.sh | tee ${ut_log_name}
28+
echo "------unittest start-------"
29+
bash -x run_unittest.sh 2>&1 | tee -a ${ut_log_name}
30+
echo "------unittest end -------"
31+
32+
if [ -s run_pytest.sh ]; then
33+
echo "cat run_pytest.sh..."
34+
sort run_pytest.sh -o run_pytest.sh
35+
cat run_pytest.sh | tee -a ${ut_log_name}
36+
echo "------pytest start-------"
37+
bash -x run_pytest.sh 2>&1 | tee -a ${ut_log_name}
38+
echo "------pytest end -------"
39+
fi
40+
2741
cp .coverage ${LOG_DIR}/.coverage
2842

29-
echo "------UT end -------"
43+
ut_status="passed"
44+
# check unittest issue
45+
if [ $(grep -c "FAILED" ${ut_log_name}) != 0 ] || [ $(grep -c "core dumped" ${ut_log_name}) != 0 ] \
46+
|| [ $(grep -c "ModuleNotFoundError:" ${ut_log_name}) != 0 ] || [ $(grep -c "ImportError:" ${ut_log_name}) != 0 ] || [ $(grep -c "OK" ${ut_log_name}) == 0 ];then
47+
echo "Find errors in unittest case, please check the output..."
48+
echo "Please search for 'FAILED' or 'core dumped' or 'ModuleNotFoundError:' or 'ImportError:'"
49+
ut_status="failed"
50+
fi
3051

31-
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
32-
echo "Find errors in UT test, please check the output..."
52+
# check pytest issue
53+
if [ -s run_pytest.sh ]; then
54+
if [ $(grep -c '== FAILURES ==' ${ut_log_name}) != 0 ] || [ $(grep -c '== ERRORS ==' ${ut_log_name}) != 0 ] || [ $(grep -c 'passed,' ${ut_log_name}) == 0 ]; then
55+
echo "Find errors in pytest case, please check the output..."
56+
echo "Please search for '== FAILURES ==' or '== ERRORS =='"
57+
ut_status="failed"
58+
fi
59+
fi
60+
61+
if [ "$ut_status" = "failed" ]; then
3362
exit 1
3463
fi
64+
3565
echo "UT finished successfully! "

.azure-pipelines/scripts/ut/3x/run_3x_tf.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ cp .coverage ${LOG_DIR}/.coverage
2828

2929
echo "------UT end -------"
3030

31-
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
31+
if [ $(grep -c "FAILED" ${ut_log_name}) != 0 ] || [ $(grep -c "core dumped" ${ut_log_name}) != 0 ] \
32+
|| [ $(grep -c "ModuleNotFoundError:" ${ut_log_name}) != 0 ] || [ $(grep -c "ImportError:" ${ut_log_name}) != 0 ] || [ $(grep -c "OK" ${ut_log_name}) == 0 ];then
3233
echo "Find errors in UT test, please check the output..."
3334
exit 1
3435
fi

examples/pytorch/nlp/huggingface_models/language-modeling/quantization/habana_fp8/models/modeling_llama.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from flash_attn import flash_attn_func, flash_attn_varlen_func
4949
from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
5050
### INC code ###
51-
from neural_compressor.torch.quantization.layers import Matmul, BatchMatmul, Autocast
51+
from neural_compressor.torch.quantization.modules import Matmul, BatchMatmul, Autocast
5252

5353
# This makes `_prepare_4d_causal_attention_mask` a leaf function in the FX graph.
5454
# It means that the function will not be traced through and simply appear as a node in the graph.

examples/pytorch/nlp/huggingface_models/language-modeling/quantization/habana_fp8/run_llm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ def itrex_bootstrap_stderr(f, xs, iters):
161161
if args.approach in ["dynamic", "static"]:
162162
print("device:", next(user_model.parameters()).device)
163163
from neural_compressor.torch.quantization.config import FP8QConfig, get_default_fp8_qconfig
164-
from neural_compressor.torch.quantization.fp8 import quantize_dynamic
165-
from neural_compressor.torch.quantization import quantize, quantize_dynamic
164+
from neural_compressor.torch.algorithms.habana_fp8 import quantize_dynamic
165+
from neural_compressor.torch.quantization import quantize
166166
if args.precision == "fp8_e4m3":
167167
dtype = torch.float8_e4m3fn
168168
else:

neural_compressor/torch/__init__.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,3 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
from neural_compressor.torch.utils.utility import register_algo
16-
from neural_compressor.torch.algorithms import rtn_quantize_entry, gptq_quantize_entry
17-
18-
from neural_compressor.torch.quantization import (
19-
quantize,
20-
RTNConfig,
21-
get_default_rtn_config,
22-
GPTQConfig,
23-
get_default_gptq_config,
24-
StaticQuantConfig,
25-
get_default_static_config,
26-
SmoothQuantConfig,
27-
get_default_sq_config,
28-
)
29-
30-
from neural_compressor.common.base_tuning import TuningConfig
31-
from neural_compressor.torch.quantization.autotune import autotune, get_all_config_set

neural_compressor/torch/algorithms/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,3 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
16-
from neural_compressor.torch.algorithms.weight_only_algos import rtn_quantize_entry
17-
from neural_compressor.torch.algorithms.weight_only_algos import gptq_quantize_entry

neural_compressor/torch/quantization/fp8/__init__.py renamed to neural_compressor/torch/algorithms/habana_fp8/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from .quantization_impl import quantize_dynamic, quantize
15+
from .fp8_quant import quantize_dynamic, quantize, white_list

neural_compressor/torch/quantization/fp8/quantization_impl.py renamed to neural_compressor/torch/algorithms/habana_fp8/fp8_quant.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
from deepspeed.module_inject.layers import LmHeadLinearAllreduce
2323

2424
from neural_compressor.common.utils import FP8_QUANT
25+
from neural_compressor.torch.quantization.modules import Autocast, BatchMatmul, Matmul
2526
from neural_compressor.torch.utils.utility import fetch_module, logger, register_algo, set_module
2627

27-
from ..layers import Autocast, BatchMatmul, Matmul
2828
from .modules import (
2929
FP8BatchMatmul,
3030
FP8Cast,
@@ -198,7 +198,6 @@ def convert(model, qconfig_mapping):
198198
return model
199199

200200

201-
@register_algo(name=FP8_QUANT)
202201
def quantize(model, qconfig_mapping, run_fn=None, run_args=None, inplace=True):
203202
q_model = model if inplace else copy.deepcopy(model)
204203
q_model = prepare(q_model, qconfig_mapping)
@@ -209,7 +208,3 @@ def quantize(model, qconfig_mapping, run_fn=None, run_args=None, inplace=True):
209208
run_fn(q_model)
210209
q_model = convert(q_model, qconfig_mapping)
211210
return q_model
212-
213-
214-
# def autotune(fp32_model, quant_config, tune_config, eval_func, ...):
215-
# pass

0 commit comments

Comments
 (0)