__version__ = '26.0.0'
import os
import platform
import shutil
import subprocess
import sys
import setuptools
from wheel.bdist_wheel import bdist_wheel
whl_version = os.getenv('WHL_VERSION')
if whl_version is not None:
__version__ = whl_version
if platform.system() != "Linux":
raise SystemError("This package only supports Linux platform. {}".format(platform.system()))
def build_frontend(plugin_name):
"""构建前端资源"""
fe_path = os.path.join("plugins", "tb_graph_ascend", plugin_name, "front")
failed_message = f"Failed to build fronted of {plugin_name},"
if not os.path.exists(fe_path):
raise RuntimeError(f"{failed_message} the fronted path '{fe_path}' is not exist")
original_cwd = os.getcwd()
try:
os.chdir(fe_path)
if not os.path.exists("package.json"):
raise RuntimeError(f"{failed_message} file 'package.json' is not exist!")
install_result = subprocess.run(
["npm", "ci"],
capture_output=True,
text=True,
check=False,
)
if install_result.returncode != 0:
raise RuntimeError(f"{failed_message} run 'npm ci' failed!")
build_result = subprocess.run(
["npm", "run", "build"], capture_output=True, text=True, check=False
)
if build_result.returncode != 0:
raise RuntimeError(f"{failed_message} run 'npm run build' failed!")
except Exception as e:
raise RuntimeError(f"{failed_message} {e}")
finally:
os.chdir(original_cwd)
def clean_frontend_build(plugin_name_list):
"""清除前端构建产物"""
fe_path = os.path.join("build", "lib")
if not os.path.exists(fe_path):
return True
clean_targets = [os.path.join(fe_path, plugin_name) for plugin_name in plugin_name_list]
cleaned = False
for target in clean_targets:
if os.path.exists(target):
try:
if os.path.isdir(target):
shutil.rmtree(target)
else:
os.remove(target)
except Exception as e:
print(f"Warning: Failed to clean {target}: {e}")
continue
cleaned = True
else:
cleaned = True
return cleaned
INSTALL_REQUIRED = [
"wheel",
"einops",
"numpy >= 1.23.0",
"pandas >= 1.3.5",
"pyyaml",
"tqdm",
"openpyxl >= 3.0.6",
"matplotlib",
"tensorboard >= 2.11.2",
"protobuf <= 3.20.2",
"rich",
"onnx >= 1.14.0",
"onnxruntime >= 1.14.1, != 1.16.0",
"skl2onnx >= 1.14.1",
"setuptools <= 81.0.0",
"pytz",
"psutil",
]
if "--plat-name" in sys.argv or "--python-tag" in sys.argv:
raise SystemError("Specifying platforms or python version is not supported.")
if platform.system() != "Linux":
raise SystemError("MindStudio-Probe is only supported on Linux platforms.")
mod_list_range = {
"adump",
"tb_graph_ascend",
"trend_analyzer",
"atb_probe",
"aclgraph_dump",
"nan_check",
"xor_checksum",
}
mod_list = []
for i, arg in enumerate(sys.argv):
if arg.startswith("--include-mod"):
if "--no-check" in sys.argv:
os.environ["INSTALL_WITHOUT_CHECK"] = "1"
sys.argv.remove("--no-check")
if arg.startswith("--include-mod="):
mod_list = arg[len("--include-mod=") :].split(',')
sys.argv.remove(arg)
elif i + 1 < len(sys.argv) and not sys.argv[i + 1].startswith("--"):
mod_list = sys.argv[i + 1].split(',')
sys.argv.remove(sys.argv[i + 1])
sys.argv.remove(arg)
mod_list = list(set(mod_list) & mod_list_range)
break
with_tb_graph_ascend = False
with_trend_analyzer = False
if mod_list:
if "tb_graph_ascend" in mod_list:
print("Building tb_graph_ascend frontend...")
build_frontend('hierarchy_plugin')
with_tb_graph_ascend = True
else:
clean_success = clean_frontend_build(['hierarchy_plugin'])
if not clean_success:
raise RuntimeError("警告: 前端构建产物清理不完整")
with_tb_graph_ascend = False
if "trend_analyzer" in mod_list:
print("Building trend_analyzer frontend...")
build_frontend('monvis_plugin')
with_trend_analyzer = True
else:
clean_success = clean_frontend_build(['trend_analyzer'])
if not clean_success:
raise RuntimeError("警告: 前端构建产物清理不完整")
with_trend_analyzer = False
if (
"adump" in mod_list
or "atb_probe" in mod_list
or "aclgraph_dump" in mod_list
or "nan_check" in mod_list
or "xor_checksum" in mod_list
):
os.environ["PYTHON_BIN"] = sys.executable
arch = platform.machine()
sys.argv.append("--plat-name")
sys.argv.append(f"linux_{arch}")
sys.argv.append("--python-tag")
sys.argv.append(f"cp{sys.version_info.major}{sys.version_info.minor}")
build_cmd = (
f"bash ./build.sh -j16 -a {arch} -v {sys.version_info.major}.{sys.version_info.minor}"
f" -m {str(mod_list).replace(' ', '')}"
)
p = subprocess.run(build_cmd.split(), shell=False, check=False)
if p.returncode != 0:
raise RuntimeError(f"Failed to build source({p.returncode})")
else:
clean_success = clean_frontend_build(['hierarchy_plugin', 'monvis_plugin'])
if not clean_success:
raise RuntimeError("警告: 前端构建产物清理不完整")
with_tb_graph_ascend = False
python_nan_check_vendor_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "python", "msprobe", "vendors")
if "nan_check" not in mod_list and os.path.isdir(python_nan_check_vendor_dir):
shutil.rmtree(python_nan_check_vendor_dir)
current_dir = os.path.dirname(os.path.realpath(__file__))
src_path = os.path.join(current_dir, 'scripts')
dst_path = os.path.join(current_dir, 'python', 'msprobe', 'scripts')
if not os.path.isdir(dst_path):
shutil.copytree(src_path, dst_path)
else:
for root, dirs, files in os.walk(src_path):
target_root = os.path.join(dst_path, root[len(src_path) + 1 :])
for dir_name in dirs:
os.makedirs(os.path.join(target_root, dir_name), mode=0o750, exist_ok=True)
for file in files:
shutil.copy(os.path.join(root, file), os.path.join(target_root, file))
packages = setuptools.find_packages(where="python")
if with_tb_graph_ascend:
packages.append('hierarchy_plugin')
if with_trend_analyzer:
packages.append('trend_analyzer')
entry_points_dict = {
'console_scripts': ['msprobe=msprobe.msprobe:main'],
}
tensorboard_plugins = []
if with_tb_graph_ascend:
tensorboard_plugins.append('graph_ascend = hierarchy_plugin.server.plugin:GraphsPlugin')
if with_trend_analyzer:
tensorboard_plugins.append('TrendVis = trend_analyzer.server.app:TrendVis')
if tensorboard_plugins:
entry_points_dict['tensorboard_plugins'] = tensorboard_plugins
package_dir_config = {"": "python"}
package_data_config = {}
if "nan_check" in mod_list:
package_data_config["msprobe"] = ["vendors/**/*"]
if with_tb_graph_ascend:
package_dir_config.update({'hierarchy_plugin': 'plugins/tb_graph_ascend/hierarchy_plugin'})
package_data_config['hierarchy_plugin'] = ['server/**/*.py', 'server/**/*.js', 'server/**/*.html']
if with_trend_analyzer:
package_dir_config.update(
{
'trend_analyzer': 'plugins/tb_graph_ascend/monvis_plugin',
}
)
package_data_config['trend_analyzer'] = ['server/**/*.py', 'server/**/*.js', 'server/**/*.html']
setuptools.setup(
name="mindstudio-probe",
version=__version__,
description="Ascend MindStudio Probe Utils",
long_description="MindStudio-Probe is a set of tools for diagnosing and improving model accuracy on Ascend NPU.",
url="https://gitcode.com/Ascend/MindStudio-Probe",
author="Ascend Team",
author_email="pmail_mindstudio@huawei.com",
packages=packages,
package_dir=package_dir_config,
package_data=package_data_config,
platforms=["Linux"],
include_package_data=True,
python_requires=">=3.7",
install_requires=INSTALL_REQUIRED,
classifiers=[
'Intended Audience :: Developers',
'Intended Audience :: Education',
'Intended Audience :: Science/Research',
'Programming Language :: Python :: 3',
'Programming Language :: C++',
'Topic :: Scientific/Engineering',
'Topic :: Scientific/Engineering :: Mathematics',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
'Topic :: Software Development',
'Topic :: Software Development :: Libraries',
'Topic :: Software Development :: Libraries :: Python Modules',
],
license='Mulan PSL v2',
keywords='pytorch msprobe ascend',
ext_modules=[],
zip_safe=False,
entry_points=entry_points_dict,
)