Coverage for hyper_parallel / platform / mindspore / custom_pass / build_plugin.py: 0%

56 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-01 07:33 +0800

1# Copyright 2026 Huawei Technologies Co., Ltd 

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 

16""" 

17Build plugin for mindspore custom pass. 

18""" 

19import os 

20import sys 

21import shutil 

22import subprocess 

23from pathlib import Path 

24 

25 

26def find_cmake(): 

27 """Locate CMake executable""" 

28 for candidate in ["cmake", "cmake3"]: 

29 if shutil.which(candidate): 

30 return candidate 

31 raise RuntimeError("CMake not found. Please install CMake.") 

32 

33 

34def build_plugin(): 

35 """ 

36 Build custom pass plugin with version safety and diagnostics. 

37 Exits with code 0 on intentional skip (version mismatch), non-zero on actual failure. 

38 """ 

39 # Proceed with compilation 

40 build_dir = Path(os.environ.get("MS_PLUGIN_BUILD_DIR", "build")).resolve() 

41 so_output = Path(os.environ.get( 

42 "MS_SO_OUTPUT", "libhyper_parallel_mindspore.so")).resolve() 

43 src_dir = Path(__file__).parent.resolve() 

44 

45 # Clean previous build 

46 if build_dir.exists(): 

47 shutil.rmtree(build_dir) 

48 build_dir.mkdir(parents=True, exist_ok=True) 

49 

50 # Configure CMake 

51 cmake = find_cmake() 

52 cmake_args = [ 

53 cmake, 

54 "-S", str(src_dir), 

55 "-B", str(build_dir), 

56 "-DCMAKE_BUILD_TYPE=Release", 

57 ] 

58 

59 if "MINDSPORE_ROOT" in os.environ: 

60 cmake_args.append(f"-DMINDSPORE_ROOT={os.environ['MINDSPORE_ROOT']}") 

61 print( 

62 f"Using user-specified MINDSPORE_ROOT: {os.environ['MINDSPORE_ROOT']}") 

63 

64 print(f"\nRunning CMake configure: {' '.join(cmake_args)}") 

65 result = subprocess.run( 

66 cmake_args, 

67 cwd=src_dir, 

68 capture_output=True, 

69 text=True, 

70 check=False 

71 ) 

72 

73 if result.returncode != 0: 

74 print("CMake configure FAILED:") 

75 print(result.stdout) 

76 print(result.stderr) 

77 if "MINDSPORE_ROOT" not in os.environ: 

78 print("\n💡 SUGGESTION: Set MINDSPORE_ROOT to enable custom pass compilation:") 

79 print(" Linux/macOS: export MINDSPORE_ROOT=/path/to/mindspore-2.8.1") 

80 print(" Windows: set MINDSPORE_ROOT=C:\\path\\to\\mindspore-2.8.1") 

81 raise RuntimeError("CMake configuration failed") 

82 

83 # Build plugin 

84 build_args = [cmake, "--build", 

85 str(build_dir), "--target", "hyper_parallel_mindspore", "-j4"] 

86 print(f"\nRunning CMake build: {' '.join(build_args)}") 

87 result = subprocess.run( 

88 build_args, 

89 cwd=src_dir, 

90 capture_output=True, 

91 text=True, 

92 check=False 

93 ) 

94 

95 if result.returncode != 0: 

96 print("Build FAILED:") 

97 print(result.stdout) 

98 print(result.stderr) 

99 raise RuntimeError("Build failed") 

100 

101 # Locate and copy SO file 

102 candidates = [ 

103 build_dir / "libhyper_parallel_mindspore.so", 

104 build_dir / "Release" / "libhyper_parallel_mindspore.so", 

105 build_dir / "Debug" / "libhyper_parallel_mindspore.so", 

106 ] 

107 

108 built_so = next((p for p in candidates if p.exists()), None) 

109 if not built_so: 

110 raise FileNotFoundError( 

111 f"Built SO not found. Searched: {[str(p) for p in candidates]}" 

112 ) 

113 

114 so_output.parent.mkdir(parents=True, exist_ok=True) 

115 shutil.copy2(built_so, so_output) 

116 print(f"\n✓ Plugin successfully built: {so_output}") 

117 print(f" Size: {so_output.stat().st_size / 1024:.2f} KB") 

118 

119 

120if __name__ == "__main__": 

121 try: 

122 build_plugin() 

123 except Exception as e: 

124 print(f"\n❌ Build failed: {e}", file=sys.stderr) 

125 print("\n💡 This is OPTIONAL - HyperParallel works without MindSpore jit(ast) mode.", file=sys.stderr) 

126 # Non-zero exit for actual failures (setup.py will show warning) 

127 sys.exit(1)