def tune_tasks(tasks, measure_option, tuner='xgb', n_trial=1000, early_stopping=None, log_filename='tuning.log'): # create tmp log file tmp_log_file = log_filename + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, tsk in enumerate(reversed(tasks)): prefix = "\t[Task %2d/%2d] " % (i + 1, len(tasks)) tuner_obj = XGBTuner(tsk, loss_type='rank') # do tuning tuner_obj.tune(n_trial=min(n_trial, len(tsk.config_space)), early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file) ]) # pick best records to a cache file autotvm.record.pick_best(tmp_log_file, log_filename) os.remove(tmp_log_file)
def tune_tasks(tasks, measure_option, tuner='xgb', n_trial=1000, early_stopping=None, log_filename='tuning.log', use_transfer_learning=True, try_winograd=True): if try_winograd: for i in range(len(tasks)): try: # try winograd template tsk = autotvm.task.create(tasks[i].name, tasks[i].args, tasks[i].target, tasks[i].target_host, 'winograd') input_channel = tsk.workload[1][1] if input_channel >= 64: tasks[i] = tsk except Exception: pass # create tmp log file tmp_log_file = log_filename + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(tsk, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(tsk, pop_size=100) elif tuner == 'random': tuner_obj = RandomTuner(tsk) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history( autotvm.record.load_from_file(tmp_log_file)) # do tuning tuner_obj.tune(n_trial=min(n_trial, len(tsk.config_space)), early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file) ]) if not os.path.exists(tmp_log_file): open(tmp_log_file, 'a') # pick best records to a cache file autotvm.record.pick_best(tmp_log_file, log_filename) os.remove(tmp_log_file)
def xgb_tune_module( target, params, mod, number=10, repeat=1, min_repeat_ms=0, timeout=10, tuner="xgb", trials=10, early_stopping=100, tuning_records="/root/temp/tvm-autojson/resnet-50-v2-autotuning.json"): runner = tvm.autotvm.LocalRunner( number=number, repeat=repeat, timeout=timeout, min_repeat_ms=min_repeat_ms, ) tuning_option = { "tuner": tuner, "trials": trials, "early_stopping": 100, "measure_option": tvm.autotvm.measure_option( builder=tvm.autotvm.LocalBuilder(build_func="default"), runner=runner), "tuning_records": tuning_records, } # begin by extracting the taks from the onnx model tasks = tvm.autotvm.task.extract_from_program(mod["main"], target=target, params=params) # Tune the extracted tasks sequentially. for i, task in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) tuner_obj = XGBTuner(task, loss_type="rank") tuner_obj.tune( n_trial=min(tuning_option["trials"], len(task.config_space)), early_stopping=tuning_option["early_stopping"], measure_option=tuning_option["measure_option"], callbacks=[ tvm.autotvm.callback.progress_bar(tuning_option["trials"], prefix=prefix), tvm.autotvm.callback.log_to_file( tuning_option["tuning_records"]), ], ) return tuning_option
def tune_tasks( tasks, measure_option, tuner="xgb", n_trial=1000, early_stopping=None, log_filename="tuning.log", use_transfer_learning=True, ): """Tune tasks and generate tuning log to file""" # create tmp log file tmp_log_file = log_filename + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, tsk in enumerate(reversed(tasks)): prefix = f"[Task {i + 1:2d}/{len(tasks):2d}] " # create tuner if tuner in ("xgb", "sgb-rank"): tuner_obj = XGBTuner(tsk, loss_type="rank") elif tuner == "ga": tuner_obj = GATuner(tsk, pop_size=100) elif tuner == "random": tuner_obj = RandomTuner(tsk) elif tuner == "gridsearch": tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history( autotvm.record.load_from_file(tmp_log_file)) # do tuning tsk_trial = min(n_trial, len(tsk.config_space)) tuner_obj.tune( n_trial=tsk_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(tsk_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file), ], ) # pick best records to a cache file if not os.path.exists(log_filename): with open(log_filename, "w", encoding="utf-8"): pass if os.path.exists(tmp_log_file): autotvm.record.pick_best(tmp_log_file, log_filename) os.remove(tmp_log_file)
def tune_tasks( tasks, measure_option, tuner="xgb", n_trial=1000, early_stopping=None, log_filename="tuning.log", use_transfer_learning=True, try_winograd=True, ): # create tmp log file tmp_log_file = log_filename + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # create tuner if tuner == "xgb" or tuner == "xgb-rank": tuner_obj = XGBTuner(tsk, loss_type="rank") elif tuner == "ga": tuner_obj = GATuner(tsk, pop_size=50) elif tuner == "random": tuner_obj = RandomTuner(tsk) elif tuner == "gridsearch": tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history( autotvm.record.load_from_file(tmp_log_file)) # do tuning n_trial_ = min(n_trial, len(tsk.config_space)) tuner_obj.tune( n_trial_, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial_, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file), ], ) # pick best records to a cache file autotvm.record.pick_best(tmp_log_file, log_filename) os.remove(tmp_log_file)
def tune_kernels(tasks, measure_option, tuner='gridsearch', early_stopping=None, log_filename='tuning.log'): for i, task in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i+1, len(tasks)) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=50) elif tuner == 'random': tuner_obj = RandomTuner(task) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial=len(task.config_space) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename)])
def tune_kernels(tasks, measure_option, tuner="xgb", n_trial=1000, early_stopping=None, log_filename="tuning.log"): for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # create tuner if tuner == "xgb" or tuner == "xgb-rank": tuner_obj = XGBTuner(tsk, loss_type="rank") elif tuner == "ga": tuner_obj = GATuner(tsk, pop_size=100) elif tuner == "random": tuner_obj = RandomTuner(tsk) elif tuner == "gridsearch": tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) # do tuning tsk_trial = min(n_trial, len(tsk.config_space)) tuner_obj.tune( n_trial=tsk_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(tsk_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename), ], )
def tune_kernels(tasks, measure_option, tuner="gridsearch", early_stopping=None, log_filename="tuning.log", mode=0): for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) name = "topi_nn_conv2d" if mode == 0 else "topi_x86_conv2d_NCHWc" print(name) task = autotvm.task.create(name, args=tsk.args, target=target, template_key="direct") task.workload = tsk.workload # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=50) elif tuner == 'random': tuner_obj = RandomTuner(task) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial = 1000 # min(len(task.config_space), 1000) print("trials=", n_trial) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename)])
def tune_kernels( tasks, measure_option, tuner="gridsearch", early_stopping=None, log_filename="logs/tuning.log" ): for i, task in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # create tuner if tunes == 0: tuner_obj = XGBTuner(task, loss_type="rank") # print("Using XGBTuner") elif tunes == 1: tuner_obj = GATuner(task, pop_size=50) # print("Using GATuner") elif tunes == 2: tuner_obj = RandomTuner(task) # print("Using Random") elif tunes == 3: tuner_obj = GridSearchTuner(task) # print("Using GridSearch") else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial = len(task.config_space) tuner_obj.tune( n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename), ], )
def tune_tasks(tasks, measure_option, tuner='xgb', n_trial=1000, early_stopping=None, log_filename='tuning.log', use_transfer_learning=True, try_winograd=True): if try_winograd: for i in range(len(tasks)): try: # try winograd template tsk = autotvm.task.create(tasks[i].name, tasks[i].args, tasks[i].target, tasks[i].target_host, 'winograd') input_channel = tsk.workload[1][1] if input_channel >= 64: tasks[i] = tsk except Exception: pass # create tmp log file tmp_log_file = log_filename + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " %(i+1, len(tasks)) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(tsk, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(tsk, pop_size=100) elif tuner == 'random': tuner_obj = RandomTuner(tsk) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) # do tuning tuner_obj.tune(n_trial=min(n_trial, len(tsk.config_space)), early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file)]) # pick best records to a cache file autotvm.record.pick_best(tmp_log_file, log_filename) os.remove(tmp_log_file)
def tune_tasks(tasks, measure_option, tuner='xgb', n_trial=1000, early_stopping=None, log_filename='cuda_v0.08.log', use_transfer_learning=True): # create tmp log file tmp_log_file = log_filename + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) print(use_transfer_learning) for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) print(tsk) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(tsk, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(tsk, pop_size=100) elif tuner == 'random': tuner_obj = RandomTuner(tsk) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history( autotvm.record.load_from_file(tmp_log_file)) # do tuning tsk_trial = min(n_trial, len(tsk.config_space)) print(tsk.config_space) tuner_obj.tune(n_trial=tsk_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(tsk_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file) ]) # pick best records to a cache file autotvm.record.pick_best(tmp_log_file, log_filename) os.remove(tmp_log_file)
def tune_tasks(target): target = tvm.target.create(target) tasks = config_tasks(target) tuner = tuning_option['tuner'] use_transfer_learning = tuning_option['use_transfer_learning'] n_trial = tuning_option['n_trial'] early_stopping = tuning_option['early_stopping'] measure_option = tuning_option['measure_option'] tmp_log_file = log_file + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(tsk, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(tsk, pop_size=50) elif tuner == 'random': tuner_obj = RandomTuner(tsk) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history( autotvm.record.load_from_file(tmp_log_file)) # Do tuning tuner_obj.tune(n_trial=min(n_trial, len(tsk.config_space)), early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file) ]) # Pick best records to a cache file autotvm.record.pick_best(tmp_log_file, log_file) os.remove(tmp_log_file)
def tune_kernels(tasks, builder=autotvm.LocalBuilder(), runner=autotvm.LocalRunner(number=10, repeat=1, min_repeat_ms=1000), tuner='ga', early_stopping=None, log_filename=log_file): measure_option = autotvm.measure_option(builder, runner) for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # converting conv2d tasks to conv2d_NCHWc tasks if tsk.workload: op_name = tsk.workload[0] if op_name == 'conv2d': func_create = 'topi_x86_conv2d_NCHWc' elif op_name == 'depthwise_conv2d_nchw': func_create = 'topi_x86_depthwise_conv2d_NCHWc_from_nchw' else: raise ValueError( "Tuning {} is not supported on x86".format(op_name)) task = autotvm.task.create(func_create, args=tsk.args, target=target, template_key='direct') task.workload = tsk.workload else: task = tsk # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=1000) elif tuner == 'random': tuner_obj = RandomTuner(task) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial = len(task.config_space) print("n_trial", n_trial) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename) ])
def tune_task(name, tasks, measure, resume_log_file="tune.log", n_trial=10): from tvm.autotvm.tuner import XGBTuner import os dir_name = os.path.dirname(resume_log_file) try: os.mkdir(dir_name) except: pass for idx, task in enumerate(tasks): prefix = "[%s][Task %2d/%2d] " % (name, idx + 1, len(tasks)) tuner = XGBTuner(task, loss_type='rank') if os.path.isfile(resume_log_file): print("load log file:" + resume_log_file) tuner.load_history(autotvm.record.load_from_file(resume_log_file)) n_try = min(n_trial, len(task.config_space)) callbacks = [ autotvm.callback.progress_bar(n_try, prefix=prefix), autotvm.callback.log_to_file(resume_log_file) ] try: import tvm.tvmt log_db = resume_log_file + ".db" callbacks.append(tvm.tvmt.log_to_sqlite(log_db)) except: pass tuner.tune(n_trial=n_try, early_stopping=80, measure_option=measure, callbacks=callbacks) return
def tune(self): # create tuner if self.tuner == "xgb" or self.tuner == "xgb-rank": tuner_obj = XGBTuner(self.task, loss_type="rank") elif self.tuner == "xgb_knob": tuner_obj = XGBTuner(selftask, loss_type="rank", feature_type="knob") elif self.tuner == "ga": tuner_obj = GATuner(self.task, pop_size=50) elif self.tuner == "random": tuner_obj = RandomTuner(self.task) elif self.tuner == "gridsearch": tuner_obj = GridSearchTuner(self.task) else: raise ValueError("Invalid tuner: " + tuner) def _search_best_config(): def _callback(_, inputs, results): for inp, result in zip(inputs, results): new_latency = result.costs[0] if result.error_no ==0 else 1e9 if self.record == None or self.best_latency > new_latency: self.record = autotvm.record.encode(inp, result) self.best_config = inp.config.to_json_dict()['entity'] self.best_latency = new_latency return _callback # do tuning task_trial = min(self.n_trial, len(self.task.config_space)) tuner_obj.tune( n_trial=task_trial, early_stopping=self.early_stopping, measure_option=self.measure_option, callbacks=[ autotvm.callback.progress_bar(task_trial), _search_best_config(), ], ) print(self.record) kernel_db = KernelDB() kernel_db.write_task(self) del kernel_db
def tune_kernels(tasks, measure_option, tuner='gridsearch', early_stopping=None, log_filename='tuning.log'): for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # converting conv2d tasks to conv2d_NCHWc tasks op_name = tsk.workload[0] if op_name == 'conv2d': func_create = 'topi_x86_conv2d_NCHWc' elif op_name == 'depthwise_conv2d_nchw': func_create = 'topi_x86_depthwise_conv2d_NCHWc_from_nchw' else: raise ValueError( "Tuning {} is not supported on x86".format(op_name)) task = autotvm.task.create(func_create, args=tsk.args, target=target, template_key='direct') task.workload = tsk.workload # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=50) elif tuner == 'random': tuner_obj = RandomTuner(task) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial = len(task.config_space) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename) ]) with open('data/task%i.pickle' % i, 'wb') as output: pickle.dump([task, tuner_obj.cost_model.saved_features], output, pickle.HIGHEST_PROTOCOL) print('Saved data/task%i.pickle' % i)
def tune_kernels(tasks, measure_option, tuner='gridsearch', early_stopping=None, log_filename='tuning.log'): for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # converting conv2d tasks to conv2d_NCHWc tasks # data, kernel are tuples of ("TENSOR", shape, dtype) data, kernel, strides, padding, layout, dtype = tsk.args kernel_size = (kernel[1][2], kernel[1][3]) data_plc = tvm.placeholder(data[1], name="data") kernel_plc = tvm.placeholder(kernel[1], name="kernel") args = [ data_plc, kernel_plc, kernel[1][0], kernel_size, strides, padding, layout, layout, dtype ] args = autotvm.task.nnvm_integration.serialize_args(args) task = autotvm.task.create("topi_x86_conv2d_NCHWc", args=args, target=target) task.workload = conv_NCHWc_arg_to_workload(data_plc, kernel_plc, kernel_size, strides, padding, layout, layout, dtype) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=50) elif tuner == 'random': tuner_obj = RandomTuner(task) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial = len(task.config_space) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename) ])
def tune_kernels(tasks, measure_option, tuner='gridsearch', early_stopping=None, log_filename='tuning.log'): print('tasks: {}'.format(tasks)) for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # converting conv2d tasks to conv2d_NCHWc tasks op_name = tsk.workload[0] if op_name == 'conv2d': func_create = 'topi_x86_conv2d_NCHWc' elif op_name == 'depthwise_conv2d_nchw': func_create = 'topi_x86_depthwise_conv2d_NCHWc_from_nchw' else: raise ValueError( "Tuning {} is not supported on x86".format(op_name)) task = autotvm.task.create(func_create, args=tsk.args, target=target, template_key='direct') task.workload = tsk.workload # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=50) elif tuner == 'random': tuner_obj = RandomTuner(task) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial = len(task.config_space) n_trial = 1 # set small num to quick test print('n trial {}'.format(n_trial)) # print(task.config_space) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename) ])
def tune_kernels(tasks, measure_option, tuner="gridsearch", early_stopping=None, log_filename="tuning.log"): for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # converting conv2d tasks to conv2d_NCHWc tasks op_name = tsk.workload[0] if op_name == "conv2d": func_create = "topi_x86_conv2d_NCHWc" elif op_name == "depthwise_conv2d_nchw": func_create = "topi_x86_depthwise_conv2d_NCHWc_from_nchw" else: raise ValueError( "Tuning {} is not supported on x86".format(op_name)) task = autotvm.task.create(func_create, args=tsk.args, target=target, template_key="direct") task.workload = tsk.workload # create tuner if tuner == "xgb" or tuner == "xgb-rank": tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == "ga": tuner_obj = GATuner(task, pop_size=50) elif tuner == "random": tuner_obj = RandomTuner(task) elif tuner == "gridsearch": tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial = len(task.config_space) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename) ])
def tune_kernels(tasks, measure_option, tuner=tuner, n_trial=trials, early_stopping=None, log_filename=log_file): for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # Converting conv2d tasks to conv2d_NCHWc tasks. Do we actually want this? op_name = tsk.workload[0] #input_shape = tsk.workload[1][0:-1] #kernel_shape = tsk.workload[2][0:-1] #input_channels = input_shape[1] # Only can convert to NCHWc if input channels is divisible by 8. #convertible = input_channels % 8 == 0 func_create = tsk.name #if op_name == 'conv2d': # func_create = 'topi_x86_conv2d_NCHWc' #elif op_name == 'depthwise_conv2d_nchw': # func_create = 'topi_x86_depthwise_conv2d_NCHWc_from_nchw' print(func_create) task = autotvm.task.create(func_create, args=tsk.args, target=target, template_key='direct') task.workload = tsk.workload # Create tuner. if tuner == 'xgb' or tuner == 'xbg-rank': tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=50) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tunder: " + tuner) # Do tuning. n_trial = min(n_trial, len(task.config_space)) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename) ])
def tune_kernels( tasks, measure_option, tuner, n_trial, early_stopping, log_filename, use_transfer_learning, ): for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # create tuner if tuner == "random" or n_trial >= len(tsk.config_space): tuner_obj = RandomTuner(tsk) elif tuner == "xgb" or tuner == "xgb-rank": tuner_obj = XGBTuner(tsk, loss_type="rank") # use history data to pre-train the cost model if use_transfer_learning: if os.path.isfile(log_filename): tuner_obj.load_history( autotvm.record.load_from_file(log_filename)) elif tuner == "ga": tuner_obj = GATuner(tsk, pop_size=100) elif tuner == "gridsearch": tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) # do tuning tsk_trial = min(n_trial, len(tsk.config_space)) tuner_obj.tune( n_trial=tsk_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(tsk_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename), ], )
def tune_kernels(tasks, measure_option, tuner='gridsearch', early_stopping=None, log_filename='tuning.log'): for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # converting conv2d tasks to conv2d_NCHWc tasks task = autotvm.task.create("topi_x86_conv2d_NCHWc", args=tsk.args, target=target, template_key='direct') task.workload = tsk.workload # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=50) elif tuner == 'random': tuner_obj = RandomTuner(task) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial = len(task.config_space) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename) ])
def tune_kernels(tasks, measure_option, tuner='gridsearch', early_stopping=None, log_filename='tuning.log'): for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i+1, len(tasks)) # converting conv2d tasks to conv2d_NCHWc tasks op_name = tsk.workload[0] if op_name == 'conv2d': func_create = 'topi_x86_conv2d_NCHWc' elif op_name == 'depthwise_conv2d_nchw': func_create = 'topi_x86_depthwise_conv2d_NCHWc_from_nchw' else: raise ValueError("Tuning {} is not supported on x86".format(op_name)) task = autotvm.task.create(func_create, args=tsk.args, target=target, template_key='direct') task.workload = tsk.workload # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=50) elif tuner == 'random': tuner_obj = RandomTuner(task) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial=len(task.config_space) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename)])
def tune_tasks( tasks: List[autotvm.task.Task], log_file: str, measure_option: autotvm.measure_option, tuner: str, trials: int, early_stopping: Optional[int] = None, tuning_records: Optional[str] = None, ): """Tune a list of tasks and output the history to a log file. Parameters ---------- tasks : list A list of autotvm.Tasks to tune. log_file : str A file to output the tuning history, in JSON. measure_option : autotvm.measure_option Options to build and run a tuning task. tuner : str Which tuner to use. trials : int The maximum number of tuning trials to perform. early_stopping : int, optional The minimum number of tuning trials to perform. This will be equal to 'trials' if not specified. tuning_records: str, optional Path to the file produced by the tuning, to be used during tuning. """ if not tasks: logger.warning("there were no tasks found to be tuned") return if not early_stopping: early_stopping = trials for i, tsk in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # Create a tuner if tuner in ("xgb", "xgb-rank"): tuner_obj = XGBTuner(tsk, loss_type="rank") elif tuner == "xgb_knob": tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="knob") elif tuner == "ga": tuner_obj = GATuner(tsk, pop_size=50) elif tuner == "random": tuner_obj = RandomTuner(tsk) elif tuner == "gridsearch": tuner_obj = GridSearchTuner(tsk) else: raise TVMCException("invalid tuner: %s " % tuner) # If transfer learning is being used, load the existing results if tuning_records and os.path.exists(tuning_records): logger.info("loading tuning records from %s", tuning_records) start_time = time.time() tuner_obj.load_history( autotvm.record.load_from_file(tuning_records)) logging.info("loaded history in %.2f sec(s)", time.time() - start_time) tuner_obj.tune( n_trial=min(trials, len(tsk.config_space)), early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(trials, prefix=prefix), autotvm.callback.log_to_file(log_file), ], )
def tune_kernels(tasks, gen_graph_tuner_candidates, measure_top_n, measure_option, tuner='random', early_stopping=None, n_trial=5000, log_filename='tuning.log'): """Tune kernels with the ranking model.""" remeasure_option = None if tuner == 'round': # Setup another measure option for final remeasurment. remeasure_option = autotvm.measure_option( builder=LocalBuilder(), runner=measure_option['runner'].local_runner, ) assert isinstance(measure_option['runner'], RankModelRunner) best_results = [] for i, task in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) callbacks = [] if task.name in [ 'dense_small_batch.cuda', 'conv2d_cudnn.cuda', 'dense_cublas.cuda', 'dense_large_batch.cuda', 'conv2d_transpose_nchw.cuda', 'dense_tensorcore.cuda' ]: # Ignore these four tasks continue if task.name not in measure_option['runner'].models: print('not covered by cost models') continue # create tuner if tuner == 'round': tuner_obj = RoundTuner(task, n_cfg=measure_top_n) callbacks = [rank_progress(n_trial, prefix=prefix) ] # Use different callbacks. else: if tuner in ('xgb', 'xgb-rank'): tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=50) elif tuner == 'random': tuner_obj = RandomTuner(task) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) callbacks = [ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename) ] tic = time.time() # do tuning tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=callbacks) # Round tuner needs an extra measurement step to get the real throughputs. if tuner == 'round': max_n_layout = 20 if gen_graph_tuner_candidates else 1 top_cfgs = tuner_obj.get_top_rank_cfgs(max_n_layout) measure_batch = create_measure_batch(task, remeasure_option) inputs = [ MeasureInput(task.target, task, config) for config in top_cfgs ] sys.stderr.write('{} Measure Top {} Configs'.format( prefix, len(inputs))) results = measure_batch(inputs) best_idx, best_flops = max( [(idx, i.task.flop / np.mean(r.costs) / 1e9 if r.error_no == 0 else 0) for idx, (i, r) in enumerate(zip(inputs, results))], key=lambda x: x[1]) best_results.append((task.workload, best_idx, best_flops)) sys.stderr.write(' | Best %.2f GFLOPS at Top %d | %.2fs\n' % (best_flops, best_idx, time.time() - tic)) autotvm.callback.log_to_file(log_filename)(None, inputs, results) return best_results
# # In this example, in the interest of time, we set the number of trials and # early stopping to 10. You will likely see more performance improvements if # you set these values to be higher but this comes at the expense of time # spent tuning. The number of trials required for convergence will vary # depending on the specifics of the model and the target platform. # begin by extracting the taks from the onnx model tasks = autotvm.task.extract_from_program(mod["main"], target=target, params=params) # Tune the extracted tasks sequentially. for i, task in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) tuner_obj = XGBTuner(task, loss_type="rank") tuner_obj.tune( n_trial=min(tuning_option["trials"], len(task.config_space)), early_stopping=tuning_option["early_stopping"], measure_option=tuning_option["measure_option"], callbacks=[ autotvm.callback.progress_bar(tuning_option["trials"], prefix=prefix), autotvm.callback.log_to_file(tuning_option["tuning_records"]), ], ) ################################################################################ # The output from this tuning process will look something like this: # # .. code-block:: bash
def tune_tasks(tasks, measure_option, tuner='xgb', n_trial=500, early_stopping=None, log_filename='tuning.log', use_transfer_learning=True, try_winograd=True): for i in range(len(tasks)): print(tasks[i].args) data, kernel, padding, stride, temp, layout, dtype = tasks[i].args new_args = (data, kernel, padding, stride, temp, layout, dtype) block_factor = 4 CO, CI, KH, KW = kernel[1] if CO % block_factor == 0 and CI % block_factor == 0: new_task = autotvm.task.create(tasks[i].name, new_args, tasks[i].target, tasks[i].target_host, 'fp16') tasks[i] = new_task if args.pretuned is not None: pretuned_ctx = autotvm.apply_history_best(args.pretuned) _tasks = [] for task in tasks: if pretuned_ctx._query_inside(target, task.workload) is None: _tasks.append(task) else: print('Ignoring {}'.format(task)) tasks = _tasks print(tasks) # create tmp log file tmp_log_file = log_filename + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(tsk, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(tsk, pop_size=100) elif tuner == 'random': tuner_obj = RandomTuner(tsk) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history( autotvm.record.load_from_file(tmp_log_file)) # do tuning tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file) ]) # pick best records to a cache file autotvm.record.pick_best(tmp_log_file, log_filename) os.remove(tmp_log_file)
def tune_tasks(mod, params, option): print('Creating tasks ...') tasks = autotvm.task.extract_from_program(mod["main"], target=option['target'], params=params, ops=(relay.op.nn.conv2d, relay.op.nn.dense)) if option['try_winograd']: for i in range(len(tasks)): try: # try winograd template tsk = autotvm.task.create(tasks[i].name, tasks[i].args, tasks[i].target, tasks[i].target_host, 'winograd') input_channel = tsk.workload[1][1] if input_channel >= 64: tasks[i] = tsk except Exception as err: print(err) # create tmp log file tmp_log_file = option['log_file'] if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, tsk in enumerate(reversed(tasks)): # converting conv2d tasks to conv2d_NCHWc tasks # op_name = tsk.workload[0] # if op_name == 'conv2d': # func_create = 'topi_x86_conv2d_NCHWc' # elif op_name == 'depthwise_conv2d_nchw': # func_create = 'topi_x86_depthwise_conv2d_NCHWc_from_nchw' # else: # func_create = tasks[i].name # task = autotvm.task.create(func_create, args=tsk.args, # target=target, template_key='direct') # task.workload = tsk.workload # tsk = task prefix = "[Task %2d/%2d] (%s)" %(i+1, len(tasks), tsk.name) # create tuner tuner = option['tuner'] if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(tsk, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(tsk, pop_size=100) elif tuner == 'random': tuner_obj = RandomTuner(tsk) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if option['use_transfer_learning']: if os.path.isfile(tmp_log_file): tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) # do tuning n_trial = min(option['n_trial'], len(tsk.config_space)) tuner_obj.tune(n_trial=n_trial, early_stopping=option['early_stopping'], measure_option=option['measure_option'], callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file)]) if os.path.exists(option['log_best_file']): os.remove(option['log_best_file']) autotvm.record.pick_best(option['log_file'], option['log_best_file'])
def tune_tasks( tasks, measure_option, tuner="xgb", n_trial=2048, early_stopping=None, log_filename="tuning.log", use_transfer_learning=True, ): """Tune tasks with different tuners""" tmp_log_file = log_filename + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) if tuner in ("xgb", "xgb-rank"): tuner_obj = XGBTuner(tsk, loss_type="rank") elif tuner == "xgb_knob": tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="knob") elif tuner == "ga": tuner_obj = GATuner(tsk, pop_size=50) elif tuner == "random": tuner_obj = RandomTuner(tsk) elif tuner == "gridsearch": tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) tsk_trial = min(n_trial, len(tsk.config_space)) tuner_obj.tune( n_trial=tsk_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(tsk_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file), ], ) autotvm.record.pick_best(tmp_log_file, log_filename) os.remove(tmp_log_file)
def autotvm_tune(func, params, target): """ Parameters: ---------- func : relay.expr.Function params : dict of str to numpy array target : tvm.target.Target ops : List of relay.op """ # Array of autotvm.task.Task tasks = autotvm.task.extract_from_program(func, target=target, params=params, ops=(relay.op.nn.conv2d, )) # Check tasks. for i in range(len(tasks)): op_name = tasks[i].workload[0] if op_name == 'conv2d': func_create = 'topi_x86_conv2d_NCHWc' elif op_name == 'depthwise_conv2d_nchw': func_create = 'topi_x86_depthwise_conv2d_NCHWc_from_nchw' else: raise ValueError( "Tuning {} is not supported on x86".format(op_name)) print("[Create Task %2d/%2d (%s, %s) ] " % (i + 1, len(tasks), tasks[i].name, tasks[i].workload[0])) tsk = autotvm.task.create(func_create, args=tasks[i].args, target=tasks[i].target, template_key='direct') tsk.workload = tasks[i].workload tasks[i] = tsk # turning option. tuner = 'xgb' n_trial = 100 early_stopping = None log_filename = 'tuning.log' use_transfer_learning = True measure_option = autotvm.measure_option( builder=autotvm.LocalBuilder(timeout=10), runner=autotvm.LocalRunner(number=10, repeat=1, min_repeat_ms=1000)) # create tmp log file tmp_log_file = log_filename + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(tsk, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(tsk, pop_size=100) elif tuner == 'random': tuner_obj = RandomTuner(tsk) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history( autotvm.record.load_from_file(tmp_log_file)) # do tuning tuner_obj.tune(n_trial=min(n_trial, len(tsk.config_space)), early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file) ]) # pick best records to a cache file autotvm.record.pick_best(tmp_log_file, log_filename) os.remove(tmp_log_file)
def tune_tasks(tasks, measure_option, tuner='xgb', n_trial=1000, early_stopping=None, log_filename='tuning.log', use_transfer_learning=True): # create tmp log file tmp_log_file = log_filename + ".tmp" #if os.path.exists(tmp_log_file): # os.remove(tmp_log_file) check_tmp_his = True ind = 0 '''if check_tmp_his: length = 0 if os.path.isfile(tmp_log_file): lines = list(open(tmp_log_file)) length = len(lines) else: check_tmp_his = False''' tasks = prune_old_tasks(tasks,log_filename) for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " %(i+1, len(tasks)) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(tsk, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(tsk, pop_size=100) elif tuner == 'random': tuner_obj = RandomTuner(tsk) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) # do tuning tsk_trial = min(n_trial, len(tsk.config_space)) '''print(ind) if check_tmp_his: if ind < length: ret = autotvm.record.decode(lines[ind]) inp, _ = ret if inp.task.workload == tsk.workload: if (ind + tsk_trial - 1) < length: ind = (ind + tsk_trial - 1) ret_end = autotvm.record.decode(lines[ind]) inp_end , _= ret_end if inp.task.workload == inp_end.task.workload: ind = ind + 1 continue else: check_tmp_his = False tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) elif (ind + tsk_trial - 1) == length: ind = (ind + tsk_trial - 1) ret_end = autotvm.record.decode(lines[ind]) inp_end , _= ret_end if inp.task.workload == inp_end.task.workload: ind = ind + 1 check_tmp_his = False tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) continue else: check_tmp_his = False tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) else: cmtd = length - ind ind = length - 1 ret_end = autotvm.record.decode(lines[ind]) inp_end , _= ret_end if inp.task.workload == inp_end.task.workload: ind = ind + 1 check_tmp_his = False tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) tsk_trial = tsk_trial - cmtd else: check_tmp_his = False tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file))''' with tempfile.NamedTemporaryFile() as tmp_task_log_file: tuner_obj.tune(n_trial=tsk_trial,early_stopping=early_stopping,measure_option=measure_option, callbacks=[autotvm.callback.progress_bar(tsk_trial, prefix=prefix),autotvm.callback.log_to_file(tmp_log_file)]) with open(tmp_log_file, 'a') as tmp_log: tmp_log.write(tmp_task_log_file.read().decode('utf8')) # pick best records to a cache file autotvm.record.pick_best(tmp_log_file, log_filename) os.remove(tmp_log_file)
def tune_tasks(tasks, measure_option, tuner='xgb', n_trial=1000, early_stopping=500, log_filename='tuning.log', use_transfer_learning=True, try_winograd=True): if try_winograd: for i in range(len(tasks)): try: # try winograd template tsk = autotvm.task.create(tasks[i].name, tasks[i].args, tasks[i].target, tasks[i].target_host, 'winograd') input_channel = tsk.workload[1][1] if input_channel >= 64: tasks[i] = tsk except Exception: pass # create tmp log file tmp_log_file = log_filename + ".tmp" # if os.path.exists(tmp_log_file): # os.remove(tmp_log_file) for i, tsk in enumerate(reversed(tasks)): prefix = "[Task %2d/%2d] " % (i + 1, len(tasks)) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(tsk, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(tsk, pop_size=100) elif tuner == 'random': tuner_obj = RandomTuner(tsk) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(tsk) else: raise ValueError("Invalid tuner: " + tuner) if use_transfer_learning: if os.path.isfile(tmp_log_file): tuner_obj.load_history( autotvm.record.load_from_file(tmp_log_file)) # do tuning n_trial = 1000 n_trial = min(n_trial, len(tsk.config_space)) res = [] for i in range(len(tsk.config_space)): res.append(tuner_obj.space.get(i + 1)) with open("res.txt", "w", encoding='utf-8') as f: for line in res: f.write(str(line) + '\n') f.close() #任务单独保存 # import shutil # shutil.copyfile('res.txt', 'res'+prefix+'.txt') early_stopping = len(tsk.config_space) // 2 print("n_trial=", n_trial) print("early_stopping=", early_stopping) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(tmp_log_file) ]) np.savetxt("feas" + prefix + ".txt", tuner_obj.cost_model.feas, fmt='%s', delimiter=' ') np.savetxt("x_train" + prefix + ".txt", tuner_obj.cost_model.x_train, fmt='%s', delimiter=' ') np.savetxt("y_train" + prefix + ".txt", tuner_obj.cost_model.y_train, fmt='%s', delimiter=' ') #他做到了同算子--》空间维度相同的迁移学习,那么实际上resnet18--->resnet50? # pick best records to a cache file autotvm.record.pick_best(tmp_log_file, log_filename)