11import argparse
22from .devices import InfiniDeviceEnum
33
4+ # hardware_info.py
5+ """
6+ Shared hardware platform information for the InfiniCore testing framework
7+ """
8+
9+
10+ def get_supported_hardware_platforms ():
11+ """
12+ Get list of supported hardware platforms with descriptions.
13+
14+ Returns:
15+ List of tuples (flag, description)
16+ """
17+ return [
18+ ("--cpu" , "Standard CPU execution" ),
19+ ("--nvidia" , "NVIDIA GPUs with CUDA support" ),
20+ ("--cambricon" , "Cambricon MLU accelerators (requires torch_mlu)" ),
21+ ("--ascend" , "Huawei Ascend NPUs (requires torch_npu)" ),
22+ ("--iluvatar" , "Iluvatar GPUs" ),
23+ ("--metax" , "Metax GPUs" ),
24+ ("--moore" , "Moore Threads GPUs (requires torch_musa)" ),
25+ ("--kunlun" , "Kunlun XPUs (requires torch_xmlir)" ),
26+ ("--hygon" , "Hygon DCUs" ),
27+ ]
28+
29+
30+ def get_hardware_help_text ():
31+ """
32+ Get formatted help text for hardware platforms.
33+
34+ Returns:
35+ str: Formatted help text for argument parsers
36+ """
37+ platforms = get_supported_hardware_platforms ()
38+ help_lines = ["Supported Hardware Platforms:" ]
39+
40+ for flag , description in platforms :
41+ # Remove leading dashes for cleaner display
42+ name = flag .lstrip ("-" )
43+ help_lines .append (f" - { name .upper ():<10} { description } " )
44+
45+ return "\n " .join (help_lines )
46+
47+
48+ def get_hardware_args_group (parser ):
49+ """
50+ Add hardware platform arguments to an argument parser.
51+
52+ Args:
53+ parser: argparse.ArgumentParser instance
54+
55+ Returns:
56+ The argument group for hardware platforms
57+ """
58+ hardware_group = parser .add_argument_group ("Hardware Platform Options" )
59+
60+ for flag , description in get_supported_hardware_platforms ():
61+ hardware_group .add_argument (flag , action = "store_true" , help = description )
62+
63+ return hardware_group
64+
465
566def get_args ():
6- """Parse command line arguments"""
7- parser = argparse .ArgumentParser (description = "Test Operator" )
67+ """Parse command line arguments for operator testing"""
68+ parser = argparse .ArgumentParser (
69+ description = "Test InfiniCore operators across multiple hardware platforms" ,
70+ formatter_class = argparse .RawDescriptionHelpFormatter ,
71+ epilog = f"""
72+ Examples:
73+ # Run all tests on CPU only
74+ python test_operator.py --cpu
75+
76+ # Run with benchmarking on NVIDIA GPU
77+ python test_operator.py --nvidia --bench
78+
79+ # Run with debug mode on multiple devices
80+ python test_operator.py --cpu --nvidia --debug
81+
82+ # Run performance profiling with custom iterations
83+ python test_operator.py --nvidia --bench --num_prerun 50 --num_iterations 5000
84+
85+ { get_hardware_help_text ()}
86+ """ ,
87+ )
88+
89+ # Core testing options
890 parser .add_argument (
991 "--bench" ,
1092 action = "store_true" ,
11- help = "Whether to benchmark performance " ,
93+ help = "Enable performance benchmarking mode " ,
1294 )
1395 parser .add_argument (
1496 "--num_prerun" ,
1597 type = lambda x : max (0 , int (x )),
1698 default = 10 ,
17- help = "Set the number of pre- runs before benchmarking. Default is 10. " ,
99+ help = "Number of warm-up runs before benchmarking (default: 10) " ,
18100 )
19101 parser .add_argument (
20102 "--num_iterations" ,
21103 type = lambda x : max (0 , int (x )),
22104 default = 1000 ,
23- help = "Set the number of iterations for benchmarking. Default is 1000. " ,
105+ help = "Number of iterations for benchmarking (default: 1000) " ,
24106 )
25107 parser .add_argument (
26108 "--debug" ,
27109 action = "store_true" ,
28- help = "Whether to turn on debug mode. " ,
110+ help = "Enable debug mode for detailed tensor comparison " ,
29111 )
30112
31- # Device options
32- device_group = parser .add_argument_group ("Device options" )
33- device_group .add_argument ("--cpu" , action = "store_true" , help = "Run CPU test" )
34- device_group .add_argument (
35- "--nvidia" , action = "store_true" , help = "Run NVIDIA GPU test"
36- )
37- device_group .add_argument (
38- "--cambricon" , action = "store_true" , help = "Run Cambricon MLU test"
39- )
40- device_group .add_argument (
41- "--ascend" , action = "store_true" , help = "Run ASCEND NPU test"
42- )
43- device_group .add_argument (
44- "--iluvatar" , action = "store_true" , help = "Run Iluvatar GPU test"
45- )
46- device_group .add_argument ("--metax" , action = "store_true" , help = "Run METAX GPU test" )
47- device_group .add_argument (
48- "--moore" , action = "store_true" , help = "Run MTHREADS GPU test"
49- )
50- device_group .add_argument (
51- "--kunlun" , action = "store_true" , help = "Run KUNLUN XPU test"
52- )
113+ # Device options using shared hardware info
114+ hardware_group = get_hardware_args_group (parser )
53115
54116 return parser .parse_args ()
55117
56118
57119def get_test_devices (args ):
58120 """
59121 Determine which devices to test based on command line arguments
122+
123+ Returns:
124+ List[InfiniDeviceEnum]: List of devices to test
60125 """
61126 devices_to_test = []
62127
128+ # Check each hardware platform with proper dependency validation
63129 if args .cpu :
64130 devices_to_test .append (InfiniDeviceEnum .CPU )
131+
65132 if args .nvidia :
66- devices_to_test .append (InfiniDeviceEnum .NVIDIA )
67- if args .iluvatar :
68- devices_to_test .append (InfiniDeviceEnum .ILUVATAR )
133+ try :
134+ import torch .cuda
135+
136+ devices_to_test .append (InfiniDeviceEnum .NVIDIA )
137+ except ImportError :
138+ print ("Warning: CUDA not available, skipping NVIDIA tests" )
139+
69140 if args .cambricon :
70141 try :
71142 import torch_mlu
72143
73144 devices_to_test .append (InfiniDeviceEnum .CAMBRICON )
74145 except ImportError :
75146 print ("Warning: torch_mlu not available, skipping Cambricon tests" )
147+
76148 if args .ascend :
77149 try :
78150 import torch
@@ -82,10 +154,15 @@ def get_test_devices(args):
82154 devices_to_test .append (InfiniDeviceEnum .ASCEND )
83155 except ImportError :
84156 print ("Warning: torch_npu not available, skipping Ascend tests" )
157+
85158 if args .metax :
86- import torch
159+ try :
160+ import torch
161+
162+ devices_to_test .append (InfiniDeviceEnum .METAX )
163+ except ImportError :
164+ print ("Warning: Metax GPU support not available" )
87165
88- devices_to_test .append (InfiniDeviceEnum .METAX )
89166 if args .moore :
90167 try :
91168 import torch
@@ -94,6 +171,16 @@ def get_test_devices(args):
94171 devices_to_test .append (InfiniDeviceEnum .MOORE )
95172 except ImportError :
96173 print ("Warning: torch_musa not available, skipping Moore tests" )
174+
175+ if args .iluvatar :
176+ try :
177+ # Iluvatar GPU detection
178+ import torch
179+
180+ devices_to_test .append (InfiniDeviceEnum .ILUVATAR )
181+ except ImportError :
182+ print ("Warning: Iluvatar GPU support not available" )
183+
97184 if args .kunlun :
98185 try :
99186 import torch_xmlir
@@ -102,8 +189,17 @@ def get_test_devices(args):
102189 except ImportError :
103190 print ("Warning: torch_xmlir not available, skipping Kunlun tests" )
104191
192+ if args .hygon :
193+ try :
194+ import torch
195+
196+ devices_to_test .append (InfiniDeviceEnum .HYGON )
197+ except ImportError :
198+ print ("Warning: Hygon DCU support not available" )
199+
105200 # Default to CPU if no devices specified
106201 if not devices_to_test :
107202 devices_to_test = [InfiniDeviceEnum .CPU ]
203+ print ("No devices specified, defaulting to CPU" )
108204
109205 return devices_to_test
0 commit comments