|
| 1 | +from jsonargparse.typing import ( |
| 2 | + PositiveInt, |
| 3 | + NonNegativeInt, |
| 4 | + PositiveFloat, |
| 5 | + Path_fr, |
| 6 | + restricted_string_type, |
| 7 | + restricted_number_type, |
| 8 | +) |
| 9 | +from jsonargparse import ActionConfigFile, Namespace, ArgumentParser |
| 10 | +import numpy as np |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import uuid |
| 14 | + |
| 15 | +# Collection of restricted types |
| 16 | +IP_ADDRESS_TYPE = restricted_string_type( |
| 17 | + name="ip_address_type", |
| 18 | + regex=r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", |
| 19 | +) |
| 20 | +IP_PORT_TYPE = restricted_number_type( |
| 21 | + name="ip_port_type", |
| 22 | + base_type=int, |
| 23 | + restrictions=[(">", 0), ("<", 65536)], |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def process_args(args: Namespace) -> Namespace: |
| 28 | + """Process and validate command line arguments.""" |
| 29 | + |
| 30 | + # --- Utility Functions --- |
| 31 | + def _process_common_args(args: Namespace) -> None: |
| 32 | + """Common argument processing.""" |
| 33 | + # If the experiment name is the default, add a UUID to the end. |
| 34 | + if args.experiment == "test": |
| 35 | + id = str(uuid.uuid4())[:8] |
| 36 | + args.experiment = f"{args.mode}-{id}" |
| 37 | + else: |
| 38 | + args.experiment += f"-{args.mode}" |
| 39 | + |
| 40 | + if args.timeout is not None: |
| 41 | + args.timeout = round(args.timeout * 3600) |
| 42 | + |
| 43 | + def _validate_tune_args(args: Namespace) -> None: |
| 44 | + """Validate and process arguments specific to tuning.""" |
| 45 | + if args.tune.eval == "ppa-improv" and args.tune.reference is None: |
| 46 | + print( |
| 47 | + '[ERROR TUN-0006] The argument "--eval ppa-improv"' |
| 48 | + ' requires that "--reference <FILE>" is also given.' |
| 49 | + ) |
| 50 | + sys.exit(7) |
| 51 | + |
| 52 | + # Check for experiment name and resume flag. |
| 53 | + if args.tune.resume and args.experiment == "test": |
| 54 | + print( |
| 55 | + '[ERROR TUN-0031] The flag "--resume"' |
| 56 | + ' requires that "--experiment NAME" is also given.' |
| 57 | + ) |
| 58 | + sys.exit(1) |
| 59 | + |
| 60 | + def _validate_sweep_args(args: Namespace) -> None: |
| 61 | + """Validate and process arguments specific to sweeping.""" |
| 62 | + # Add any sweep-specific validations or post-processing here. |
| 63 | + pass |
| 64 | + |
| 65 | + # --- End of Utility Functions --- |
| 66 | + |
| 67 | + _process_common_args(args) |
| 68 | + if args.mode == "tune": |
| 69 | + _validate_tune_args(args) |
| 70 | + elif args.mode == "sweep": |
| 71 | + _validate_sweep_args(args) |
| 72 | + |
| 73 | + return args |
| 74 | + |
| 75 | + |
| 76 | +def main(): |
| 77 | + parser = ArgumentParser() |
| 78 | + tune_parser = ArgumentParser() |
| 79 | + sweep_parser = ArgumentParser() |
| 80 | + |
| 81 | + subcommands = parser.add_subcommands(dest="mode") |
| 82 | + subcommands.add_subcommand("tune", parser=tune_parser) |
| 83 | + subcommands.add_subcommand("sweep", parser=sweep_parser) |
| 84 | + |
| 85 | + # DUT |
| 86 | + parser.add_argument( |
| 87 | + "--design", |
| 88 | + type=str, |
| 89 | + metavar="<gcd,jpeg,ibex,aes,...>", |
| 90 | + required=True, |
| 91 | + help="Name of the design for Autotuning.", |
| 92 | + ) |
| 93 | + parser.add_argument( |
| 94 | + "--platform", |
| 95 | + type=str, |
| 96 | + metavar="<sky130hd,sky130hs,asap7,...>", |
| 97 | + required=True, |
| 98 | + help="Name of the platform for Autotuning.", |
| 99 | + ) |
| 100 | + |
| 101 | + # Experiment Setup |
| 102 | + parser.add_argument( |
| 103 | + "--experiment", |
| 104 | + type=str, |
| 105 | + metavar="<str>", |
| 106 | + default="test", |
| 107 | + help="Experiment name. This parameter is used to prefix the" |
| 108 | + " FLOW_VARIANT and to set the Ray log destination.", |
| 109 | + ) |
| 110 | + parser.add_argument( |
| 111 | + "--timeout", |
| 112 | + type=PositiveFloat, |
| 113 | + metavar="<float>", |
| 114 | + default=None, |
| 115 | + help="Time limit (in hours) for each trial run. Default is no limit.", |
| 116 | + ) |
| 117 | + parser.add_argument( |
| 118 | + "--yaml", |
| 119 | + action=ActionConfigFile, |
| 120 | + help="Path to Overall YAML config file.", |
| 121 | + ) |
| 122 | + |
| 123 | + # Sweep-specific |
| 124 | + sweep_parser.add_argument( |
| 125 | + "--config", |
| 126 | + type=Path_fr, |
| 127 | + metavar="<path>", |
| 128 | + required=True, |
| 129 | + help="Configuration file that sets which knobs to use for Autotuning.", |
| 130 | + ) |
| 131 | + |
| 132 | + # Tune-Specific |
| 133 | + tune_parser.add_argument( |
| 134 | + "--config", |
| 135 | + type=Path_fr, |
| 136 | + metavar="<path>", |
| 137 | + required=True, |
| 138 | + help="Configuration file that sets which knobs to use for Autotuning.", |
| 139 | + ) |
| 140 | + tune_parser.add_argument( |
| 141 | + "--resume", |
| 142 | + action="store_true", |
| 143 | + help="Resme previous run. Note that you must also set a unique experiment\ |
| 144 | + name identifier via `--experiment NAME` to be able to resume.", |
| 145 | + ) |
| 146 | + tune_parser.add_argument( |
| 147 | + "--algorithm", |
| 148 | + type=str.lower, |
| 149 | + choices=["hyperopt", "ax", "optuna", "pbt", "random"], |
| 150 | + default="hyperopt", |
| 151 | + help="Search algorithm to use for Autotuning.", |
| 152 | + ) |
| 153 | + tune_parser.add_argument( |
| 154 | + "--eval", |
| 155 | + type=str.lower, |
| 156 | + choices=["default", "ppa-improv"], |
| 157 | + default="default", |
| 158 | + help="Evaluate function to use with search algorithm.", |
| 159 | + ) |
| 160 | + tune_parser.add_argument( |
| 161 | + "--samples", |
| 162 | + type=PositiveInt, |
| 163 | + metavar="<int>", |
| 164 | + default=10, |
| 165 | + help="Number of samples for tuning.", |
| 166 | + ) |
| 167 | + tune_parser.add_argument( |
| 168 | + "--iterations", |
| 169 | + type=PositiveInt, |
| 170 | + metavar="<int>", |
| 171 | + default=1, |
| 172 | + help="Number of iterations for tuning.", |
| 173 | + ) |
| 174 | + tune_parser.add_argument( |
| 175 | + "--resources_per_trial", |
| 176 | + type=PositiveFloat, |
| 177 | + metavar="<float>", |
| 178 | + default=1, |
| 179 | + help="Number of CPUs to request for each tuning job.", |
| 180 | + ) |
| 181 | + tune_parser.add_argument( |
| 182 | + "--reference", |
| 183 | + type=Path_fr, |
| 184 | + metavar="<path>", |
| 185 | + default=None, |
| 186 | + help="Reference file for use with PPAImprov.", |
| 187 | + ) |
| 188 | + tune_parser.add_argument( |
| 189 | + "--perturbation", |
| 190 | + type=PositiveInt, |
| 191 | + metavar="<int>", |
| 192 | + default=25, |
| 193 | + help="Perturbation interval for PopulationBasedTraining.", |
| 194 | + ) |
| 195 | + tune_parser.add_argument( |
| 196 | + "--seed", |
| 197 | + type=NonNegativeInt, |
| 198 | + metavar="<int>", |
| 199 | + default=42, |
| 200 | + help="Random seed. (0 means no seed.)", |
| 201 | + ) |
| 202 | + |
| 203 | + # Workload |
| 204 | + parser.add_argument( |
| 205 | + "--jobs", |
| 206 | + type=PositiveInt, |
| 207 | + metavar="<int>", |
| 208 | + default=int(np.floor(os.cpu_count() / 2)), |
| 209 | + help="Max number of concurrent jobs.", |
| 210 | + ) |
| 211 | + parser.add_argument( |
| 212 | + "--openroad_threads", |
| 213 | + type=PositiveInt, |
| 214 | + metavar="<int>", |
| 215 | + default=16, |
| 216 | + help="Max number of threads openroad can use.", |
| 217 | + ) |
| 218 | + parser.add_argument( |
| 219 | + "--server", |
| 220 | + type=IP_ADDRESS_TYPE, |
| 221 | + metavar="<a.b.c.d>", |
| 222 | + default=None, |
| 223 | + help="The address of Ray server to connect.", |
| 224 | + ) |
| 225 | + parser.add_argument( |
| 226 | + "--port", |
| 227 | + type=IP_PORT_TYPE, |
| 228 | + metavar="<int>", |
| 229 | + default=10001, |
| 230 | + help="The port of Ray server to connect.", |
| 231 | + ) |
| 232 | + |
| 233 | + parser.add_argument( |
| 234 | + "-v", |
| 235 | + "--verbose", |
| 236 | + action="count", |
| 237 | + default=0, |
| 238 | + help="Verbosity level.\n\t0: only print Ray status\n\t1: also print" |
| 239 | + " training stderr\n\t2: also print training stdout.", |
| 240 | + ) |
| 241 | + |
| 242 | + args = parser.parse_args() |
| 243 | + process_args(args) |
| 244 | + return args |
| 245 | + |
| 246 | + |
| 247 | +if __name__ == "__main__": |
| 248 | + print(main()) |
0 commit comments