Skip to content

Commit 075634c

Browse files
kaikaiyaochensuyue
authored andcommitted
neural_coder updates AutoQuant preliminary version (#1148)
(cherry picked from commit 5e4945e)
1 parent 4939127 commit 075634c

Some content is hidden

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

73 files changed

+4899
-2173
lines changed

neural_coder/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414

1515
from .interface import enable
1616
from .interface import bench
17-
from .interface import superbench
17+
from .interface import superbench
18+
# from .interface import superreport
19+
from .interface import auto_quant

neural_coder/coders/pytorch/neural_compressor/__init__.py renamed to neural_coder/backends/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +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-
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
transformation:
16+
location:
17+
- ["insert_below_model_definition_line", "insert_below_input_definition_line"]
18+
content:
19+
- |-
20+
[+] import torch_blade
21+
[+] with torch.no_grad():
22+
[+] MODEL_NAME = torch_blade.optimize(MODEL_NAME, allow_tracing=True, model_inputs=tuple(INPUT_NAME))
23+
order:
24+
- below:
25+
above:
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
transformation:
16+
location:
17+
- insert_above_inference_line
18+
- insert_below_inference_line
19+
- indent_inference_line
20+
content:
21+
- |-
22+
[+] if not ACCURACY_MODE:
23+
[+] import time
24+
[+] count_iter_ = 0
25+
[+] total_time_ = 0
26+
[+] num_iter_ = NUM_BENCHMARK_ITERATION
27+
[+] num_warmup_iter_ = 3
28+
[+] list_batch_time_ = []
29+
[+] for i_ in range(num_iter_):
30+
[+] count_iter_ = count_iter_ + 1
31+
[+] if count_iter_ > num_warmup_iter_:
32+
[+] t1_ = time.time()
33+
[+] with torch.no_grad():
34+
- |-
35+
[+] if count_iter_ > num_warmup_iter_:
36+
[+] t2_ = time.time()
37+
[+] batch_time_ = t2_ - t1_
38+
[+] list_batch_time_.append(batch_time_)
39+
[+] total_time_ = total_time_ + batch_time_
40+
[+] print("Neural_Coder_Bench_IPS: ", round((num_iter_ - num_warmup_iter_) / total_time_, 3))
41+
[+] print("Neural_Coder_Bench_MSPI: ", round(total_time_ / (num_iter_ - num_warmup_iter_) * 1000, 3))
42+
[+] list_batch_time_.sort()
43+
[+] p50_latency_ = list_batch_time_[int(len(list_batch_time_) * 0.50) - 1] * 1000
44+
[+] p90_latency_ = list_batch_time_[int(len(list_batch_time_) * 0.90) - 1] * 1000
45+
[+] p99_latency_ = list_batch_time_[int(len(list_batch_time_) * 0.99) - 1] * 1000
46+
[+] print("Neural_Coder_Bench_P50: ", round(p50_latency_, 3))
47+
[+] print("Neural_Coder_Bench_P90: ", round(p90_latency_, 3))
48+
[+] print("Neural_Coder_Bench_P99: ", round(p99_latency_, 3))
49+
[+] quit()
50+
[+] else:
51+
[+] INFERENCE_LINE
52+
- 3
53+
order:
54+
- below:
55+
above:
56+
- below:
57+
above:
58+
- below:
59+
above:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
transformation:
16+
location:
17+
- insert_below_model_definition_line
18+
content:
19+
- |-
20+
[+] import torch
21+
[+] with torch.no_grad():
22+
[+] MODEL_NAME.eval()
23+
[+] MODEL_NAME = MODEL_NAME.to(memory_format=torch.channels_last)
24+
order:
25+
- below:
26+
- pytorch_inc_static_quant_fx
27+
- pytorch_inc_static_quant_ipex
28+
- pytorch_inc_dynamic_quant
29+
above:
30+
- pytorch_ipex_fp32
31+
- pytorch_ipex_bf16
32+
- pytorch_ipex_int8_static_quant
33+
- pytorch_ipex_int8_dynamic_quant
34+
- pytorch_jit_script
35+
- pytorch_jit_script_ofi
36+
- pytorch_jit_trace
37+
- pytorch_jit_trace_ofi
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
transformation:
16+
location:
17+
- insert_below_model_definition_line
18+
content:
19+
- |-
20+
[+] from neural_compressor.experimental import MixedPrecision
21+
[+] converter = MixedPrecision()
22+
[+] converter.precisions = 'bf16'
23+
[+] converter.model = MODEL_NAME
24+
[+] MODEL_NAME = converter()
25+
order:
26+
- below:
27+
above:
28+
- pytorch_jit_script
29+
- pytorch_jit_script_ofi
30+
- pytorch_jit_trace
31+
- pytorch_jit_trace_ofi
32+
- pytorch_channels_last
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
transformation:
16+
location:
17+
- insert_below_model_definition_line
18+
content:
19+
- |-
20+
[+] if "GraphModule" not in str(type(MODEL_NAME)):
21+
[+] from neural_compressor.conf.config import QuantConf
22+
[+] from neural_compressor.experimental import Quantization, common
23+
[+] quant_config = QuantConf()
24+
[+] quant_config.usr_cfg.quantization.approach = "post_training_dynamic_quant"
25+
[+] quant_config.usr_cfg.model.framework = "pytorch"
26+
[+] quantizer = Quantization(quant_config)
27+
[+] quantizer.model = common.Model(MODEL_NAME)
28+
[+] MODEL_NAME = quantizer()
29+
[+] MODEL_NAME = MODEL_NAME.model
30+
[+] MODEL_NAME.eval()
31+
order:
32+
- below:
33+
above:
34+
- pytorch_jit_script
35+
- pytorch_jit_script_ofi
36+
- pytorch_jit_trace
37+
- pytorch_jit_trace_ofi
38+
- pytorch_channels_last
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
transformation:
16+
location:
17+
- ["insert_below_dataloader_definition_line", "insert_below_model_definition_line", "insert_below_input_definition_line"]
18+
content:
19+
- |-
20+
[+] if "GraphModule" not in str(type(MODEL_NAME)):
21+
[+] def eval_func(model):
22+
[+] EVAL_FUNC_LINES
23+
[+] from neural_compressor.conf.config import QuantConf
24+
[+] from neural_compressor.experimental import Quantization, common
25+
[+] quant_config = QuantConf()
26+
[+] quant_config.usr_cfg.model.framework = "pytorch_fx"
27+
[+] quantizer = Quantization(quant_config)
28+
[+] quantizer.model = common.Model(MODEL_NAME)
29+
[+] quantizer.calib_dataloader = DATALOADER_NAME
30+
[+] quantizer.eval_func = eval_func
31+
[+] MODEL_NAME = quantizer()
32+
[+] MODEL_NAME = MODEL_NAME.model
33+
[+] MODEL_NAME.eval()
34+
order:
35+
- below:
36+
above:
37+
- pytorch_jit_script
38+
- pytorch_jit_script_ofi
39+
- pytorch_jit_trace
40+
- pytorch_jit_trace_ofi
41+
- pytorch_channels_last
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
transformation:
16+
location:
17+
- ["insert_below_dataloader_definition_line", "insert_below_model_definition_line", "insert_below_input_definition_line"]
18+
content:
19+
- |-
20+
[+] if "GraphModule" not in str(type(MODEL_NAME)):
21+
[+] def eval_func(model):
22+
[+] EVAL_FUNC_LINES
23+
[+] from neural_compressor.conf.config import QuantConf
24+
[+] from neural_compressor.experimental import Quantization, common
25+
[+] quant_config = QuantConf()
26+
[+] quant_config.usr_cfg.model.framework = "pytorch_ipex"
27+
[+] quantizer = Quantization(quant_config)
28+
[+] quantizer.model = common.Model(MODEL_NAME)
29+
[+] quantizer.calib_dataloader = DATALOADER_NAME
30+
[+] quantizer.eval_func = eval_func
31+
[+] MODEL_NAME = quantizer()
32+
[+] MODEL_NAME = MODEL_NAME.model
33+
[+] MODEL_NAME.eval()
34+
order:
35+
- below:
36+
above:
37+
- pytorch_jit_script
38+
- pytorch_jit_script_ofi
39+
- pytorch_jit_trace
40+
- pytorch_jit_trace_ofi
41+
- pytorch_channels_last
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
transformation:
16+
location:
17+
- insert_below_model_definition_line
18+
- insert_above_inference_line
19+
- indent_inference_line
20+
content:
21+
- |-
22+
[+] import torch
23+
[+] import intel_extension_for_pytorch as ipex
24+
[+] with torch.no_grad():
25+
[+] MODEL_NAME.eval()
26+
[+] MODEL_NAME = ipex.optimize(MODEL_NAME, dtype=torch.bfloat16)
27+
- |-
28+
[+] import torch
29+
[+] with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloat16):
30+
- 1
31+
order:
32+
- below:
33+
- pytorch_channels_last
34+
above:
35+
- pytorch_jit_script
36+
- pytorch_jit_script_ofi
37+
- pytorch_jit_trace
38+
- pytorch_jit_trace_ofi
39+
- below:
40+
above:
41+
- below:
42+
above:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
transformation:
16+
location:
17+
- insert_below_model_definition_line
18+
content:
19+
- |-
20+
[+] import torch
21+
[+] import intel_extension_for_pytorch as ipex
22+
[+] with torch.no_grad():
23+
[+] MODEL_NAME.eval()
24+
[+] MODEL_NAME = ipex.optimize(MODEL_NAME, dtype=torch.float32)
25+
order:
26+
- below:
27+
- pytorch_channels_last
28+
above:
29+
- pytorch_jit_script
30+
- pytorch_jit_script_ofi
31+
- pytorch_jit_trace
32+
- pytorch_jit_trace_ofi

0 commit comments

Comments
 (0)