3131import sys
3232import glob
3333import subprocess
34+ import random
3435from datetime import datetime
3536from multiprocessing import cpu_count
3637from subprocess import run
3738from itertools import product
39+ from collections import namedtuple
3840from uuid import uuid4 as uuid
3941
4042import numpy as np
4749from ray .tune .search .ax import AxSearch
4850from ray .tune .search .basic_variant import BasicVariantGenerator
4951from ray .tune .search .hyperopt import HyperOptSearch
50-
51- # from ray.tune.search.nevergrad import NevergradSearch
5252from ray .tune .search .optuna import OptunaSearch
5353from ray .util .queue import Queue
5454
55- # import nevergrad as ng
5655from ax .service .ax_client import AxClient
5756
5857DATE = datetime .now ().strftime ("%Y-%m-%d-%H-%M-%S" )
@@ -188,6 +187,9 @@ def percent(x_1, x_2):
188187 def evaluate (self , metrics ):
189188 error = "ERR" in metrics .values () or "ERR" in reference .values ()
190189 not_found = "N/A" in metrics .values () or "N/A" in reference .values ()
190+ print ("Metrics" , metrics .values ())
191+ print ("Reference" , reference .values ())
192+ print (error , not_found )
191193 if error or not_found :
192194 return ERROR_METRIC
193195 ppa = self .get_ppa (metrics )
@@ -221,24 +223,19 @@ def apply_condition(config, data):
221223 if args .algorithm != "random" :
222224 return config
223225 dp_pad_min = data ["CELL_PAD_IN_SITES_DETAIL_PLACEMENT" ]["minmax" ][0 ]
224- # dp_pad_max = data['CELL_PAD_IN_SITES_DETAIL_PLACEMENT']['minmax'][1]
225226 dp_pad_step = data ["CELL_PAD_IN_SITES_DETAIL_PLACEMENT" ]["step" ]
226227 if dp_pad_step == 1 :
227228 config ["CELL_PAD_IN_SITES_DETAIL_PLACEMENT" ] = tune .sample_from (
228- lambda spec : tune .randint (
229+ lambda spec : np . random .randint (
229230 dp_pad_min , spec .config .CELL_PAD_IN_SITES_GLOBAL_PLACEMENT + 1
230231 )
231232 )
232233 if dp_pad_step > 1 :
233234 config ["CELL_PAD_IN_SITES_DETAIL_PLACEMENT" ] = tune .sample_from (
234- lambda spec : tune .choice (
235- np .ndarray .tolist (
236- np .arange (
237- dp_pad_min ,
238- spec .config .CELL_PAD_IN_SITES_GLOBAL_PLACEMENT + 1 ,
239- dp_pad_step ,
240- )
241- )
235+ lambda spec : random .randrange (
236+ dp_pad_min ,
237+ spec .config .CELL_PAD_IN_SITES_GLOBAL_PLACEMENT + 1 ,
238+ dp_pad_step ,
242239 )
243240 )
244241 return config
@@ -248,13 +245,8 @@ def read_tune(this):
248245 if min_ == max_ :
249246 # Returning a choice of a single element allow pbt algorithm to
250247 # work. pbt does not accept single values as tunable.
251- return tune .choice ([min_ ])
248+ return tune .choice ([min_ , max_ ])
252249 if this ["type" ] == "int" :
253- if min_ == 0 and args .algorithm == "nevergrad" :
254- print (
255- "[WARNING TUN-0011] NevergradSearch may not work "
256- "with lower bound value 0."
257- )
258250 if this ["step" ] == 1 :
259251 return tune .randint (min_ , max_ )
260252 return tune .choice (np .ndarray .tolist (np .arange (min_ , max_ , this ["step" ])))
@@ -265,7 +257,12 @@ def read_tune(this):
265257 return None
266258
267259 def read_tune_ax (name , this ):
260+ """
261+ Ax format: https://ax.dev/versions/0.3.7/api/service.html
262+ """
268263 dict_ = dict (name = name )
264+ if "minmax" not in this :
265+ return None
269266 min_ , max_ = this ["minmax" ]
270267 if min_ == max_ :
271268 dict_ ["type" ] = "fixed"
@@ -292,6 +289,21 @@ def read_tune_ax(name, this):
292289 dict_ ["value_type" ] = "float"
293290 return dict_
294291
292+ def read_tune_pbt (name , this ):
293+ """
294+ PBT format: https://docs.ray.io/en/releases-2.9.3/tune/examples/pbt_guide.html
295+ Note that PBT does not support step values.
296+ """
297+ if "minmax" not in this :
298+ return None
299+ min_ , max_ = this ["minmax" ]
300+ if min_ == max_ :
301+ return ray .tune .choice ([min_ , max_ ])
302+ if this ["type" ] == "int" :
303+ return ray .tune .randint (min_ , max_ )
304+ if this ["type" ] == "float" :
305+ return ray .tune .uniform (min_ , max_ )
306+
295307 # Check file exists and whether it is a valid JSON file.
296308 assert os .path .isfile (file_name ), f"File { file_name } not found."
297309 try :
@@ -319,13 +331,25 @@ def read_tune_ax(name, this):
319331 fr_file = read (f"{ os .path .dirname (file_name )} /{ value } " )
320332 continue
321333 if not isinstance (value , dict ):
322- config [key ] = value
334+ # To take care of empty values like _FR_FILE_PATH
335+ if args .mode == "tune" and args .algorithm == "ax" :
336+ param_dict = read_tune_ax (key , value )
337+ if param_dict :
338+ config .append (param_dict )
339+ elif args .mode == "tune" and args .algorithm == "pbt" :
340+ param_dict = read_tune_pbt (key , value )
341+ if param_dict :
342+ config [key ] = param_dict
343+ else :
344+ config [key ] = value
323345 elif args .mode == "sweep" :
324346 config [key ] = read_sweep (value )
325- elif args .mode == "tune" and args .algorithm != "ax" :
326- config [key ] = read_tune (value )
327347 elif args .mode == "tune" and args .algorithm == "ax" :
328348 config .append (read_tune_ax (key , value ))
349+ elif args .mode == "tune" and args .algorithm == "pbt" :
350+ config [key ] = read_tune_pbt (key , value )
351+ elif args .mode == "tune" :
352+ config [key ] = read_tune (value )
329353 if args .mode == "tune" :
330354 config = apply_condition (config , data )
331355 return config , sdc_file , fr_file
@@ -724,7 +748,7 @@ def parse_arguments():
724748 tune_parser .add_argument (
725749 "--algorithm" ,
726750 type = str ,
727- choices = ["hyperopt" , "ax" , "nevergrad" , " optuna" , "pbt" , "random" ],
751+ choices = ["hyperopt" , "ax" , "optuna" , "pbt" , "random" ],
728752 default = "hyperopt" ,
729753 help = "Search algorithm to use for Autotuning." ,
730754 )
@@ -840,18 +864,13 @@ def set_algorithm(experiment_name, config):
840864 algorithm = HyperOptSearch (points_to_evaluate = best_params )
841865 elif args .algorithm == "ax" :
842866 ax_client = AxClient (enforce_sequential_optimization = False )
867+ AxClientMetric = namedtuple ("AxClientMetric" , "minimize" )
843868 ax_client .create_experiment (
844869 name = experiment_name ,
845870 parameters = config ,
846- objective_name = METRIC ,
847- minimize = True ,
871+ objectives = {METRIC : AxClientMetric (minimize = True )},
848872 )
849873 algorithm = AxSearch (ax_client = ax_client , points_to_evaluate = best_params )
850- elif args .algorithm == "nevergrad" :
851- algorithm = NevergradSearch (
852- points_to_evaluate = best_params ,
853- optimizer = ng .optimizers .registry ["PortfolioDiscreteOnePlusOne" ],
854- )
855874 elif args .algorithm == "optuna" :
856875 algorithm = OptunaSearch (points_to_evaluate = best_params , seed = args .seed )
857876 elif args .algorithm == "pbt" :
0 commit comments