Skip to content

Commit cfb38fa

Browse files
xin3hexinhe3pre-commit-ci[bot]XuehaoSun
authored
[SW-219134] dynamically remove 2x content (#2173)
* [SW-219134] dynamically remove 2x content Signed-off-by: Xin He <[email protected]> Signed-off-by: Sun, Xuehao <[email protected]> --------- Signed-off-by: Xin He <[email protected]> Signed-off-by: Sun, Xuehao <[email protected]> Co-authored-by: Xin He <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sun, Xuehao <[email protected]>
1 parent c4a8c77 commit cfb38fa

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ $BOLD_YELLOW && echo "collect coverage for baseline" && $RESET
2222
cd /neural-compressor
2323
cp -r /neural-compressor/.azure-pipelines .azure-pipelines-pr
2424
git config --global --add safe.directory /neural-compressor
25+
git reset --hard
2526
git fetch
2627
git checkout master
2728
rm -rf build dist *egg-info

neural_compressor/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"""Intel® Neural Compressor: An open-source Python library supporting popular model compression techniques."""
1818
from .version import __version__
1919

20-
# we need to set a global 'NA' backend, or Model can't be used
2120
from .config import (
2221
DistillationConfig,
2322
PostTrainingQuantConfig,

setup.py

+46-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,47 @@
66

77
from setuptools import find_packages, setup
88

9+
# Remove 2x content in "__init__.py" when only 3x is installed and recover it when 2x is installed
10+
content_position = ("neural_compressor/__init__.py", 20) # file path and line number
11+
backup_content = """from .config import (
12+
DistillationConfig,
13+
PostTrainingQuantConfig,
14+
WeightPruningConfig,
15+
QuantizationAwareTrainingConfig,
16+
MixedPrecisionConfig,
17+
)
18+
from .contrib import *
19+
from .model import *
20+
from .metric import *
21+
from .utils import options
22+
from .utils.utility import set_random_seed, set_tensorboard, set_workspace, set_resume_from
23+
"""
24+
25+
26+
def delete_lines_from_file(file_path, start_line):
27+
"""Deletes all lines from the specified start_line to the end of the file."""
28+
with open(file_path, "r") as file:
29+
lines = file.readlines()
30+
31+
# Keep only lines before the start_line
32+
lines = lines[: start_line - 1]
33+
34+
with open(file_path, "w") as file:
35+
file.writelines(lines)
36+
37+
38+
def replace_lines_from_file(file_path, start_line, replacement_content):
39+
"""Replaces all lines from the specified start_line to the end of the file with replacement_content."""
40+
with open(file_path, "r") as file:
41+
lines = file.readlines()
42+
43+
# Keep lines before the start_line and append replacement_content
44+
lines = lines[: start_line - 1]
45+
lines.append(replacement_content)
46+
47+
with open(file_path, "w") as file:
48+
file.writelines(lines)
49+
950

1051
def fetch_requirements(path):
1152
with open(path, "r") as fd:
@@ -106,10 +147,13 @@ def get_build_version():
106147
if "pt" in sys.argv:
107148
sys.argv.remove("pt")
108149
cfg_key = "neural_compressor_pt"
109-
110-
if "tf" in sys.argv:
150+
delete_lines_from_file(*content_position)
151+
elif "tf" in sys.argv:
111152
sys.argv.remove("tf")
112153
cfg_key = "neural_compressor_tf"
154+
delete_lines_from_file(*content_position)
155+
else:
156+
replace_lines_from_file(*content_position, backup_content)
113157

114158
project_name = PKG_INSTALL_CFG[cfg_key].get("project_name")
115159
include_packages = PKG_INSTALL_CFG[cfg_key].get("include_packages") or {}

0 commit comments

Comments
 (0)