def test_tuning_gpu(target, ctx): # init task task, target = get_sample_task(target, None) logging.info("task config space: %s", task.config_space) measure_option = autotvm.measure_option(autotvm.LocalBuilder(), autotvm.LocalRunner()) results = [] tuner = RandomTuner(task) tuner.tune( n_trial=20, measure_option=measure_option, callbacks=(lambda _tuner, _inputs, rs: results.extend(rs), ), ) assert len(results) == 20 successful_results = [ r for r in results if r.error_no == autotvm.MeasureErrorNo.NO_ERROR ] assert len( successful_results) > 0, f"No successful tuning runs: {results!r}"
def test_tuning_cpu(): ir_mod = tvm.parser.fromtext( textwrap.dedent(""" #[version = "0.0.5"] def @main(%a : Tensor[(1, 3, 32, 32), float32], %b : Tensor[(3, 3, 5, 5), float32]) { nn.conv2d(%a, %b, data_layout="NCHW", kernel_layout="OIHW") } """)) tasks = autotvm.task.relay_integration.extract_from_program( ir_mod, {}, tvm.target.create("llvm")) assert len(tasks) == 1, f"Extracted != 1 task from program: {tasks!r}" task = tasks[0] measure_option = autotvm.measure_option(autotvm.LocalBuilder(), autotvm.LocalRunner()) results = [] tuner = RandomTuner(task) tuner.tune( n_trial=20, measure_option=measure_option, callbacks=(lambda _tuner, _inputs, rs: results.extend(rs), ), ) assert len(results) == 20 successful_results = [ r for r in results if r.error_no == autotvm.MeasureErrorNo.NO_ERROR ] assert len( successful_results) > 0, f"No successful tuning runs: {results!r}"
def runner(target): # init task task, target = get_sample_task(target, None) logging.info("task config space: %s", task.config_space) measure_option = autotvm.measure_option(autotvm.LocalBuilder(), autotvm.LocalRunner()) results = [] tuner = RandomTuner(task) tuner.tune( n_trial=20, measure_option=measure_option, callbacks=(lambda _tuner, _inputs, rs: results.extend(rs), ), ) assert len(results) == 20 successful_results = [ r for r in results if r.error_no == autotvm.MeasureErrorNo.NO_ERROR # We filter records before building if we know they won't work ahead of time. # We can't guarantee we get one good record so we count these as success too or r.error_no == autotvm.MeasureErrorNo.INSTANTIATION_ERROR ] assert len( successful_results) > 0, f"No successful tuning runs: {results!r}"
def test_tuning(target, ctx): # init task task, target = get_sample_task(target, None) logging.info("%s", task.config_space) measure_option = autotvm.measure_option(autotvm.LocalBuilder(), autotvm.LocalRunner()) tuner = RandomTuner(task) tuner.tune(n_trial=20, measure_option=measure_option)
def check(target, target_host): ctx = tvm.context(target, 0) if not ctx.exist: logging.info("Skip test because %s is not available" % target) return # init task task, target = get_sample_task(target, target_host) logging.info("%s", task.config_space) measure_option = autotvm.measure_option(autotvm.LocalBuilder(), autotvm.LocalRunner()) tuner = RandomTuner(task) tuner.tune(n_trial=20, measure_option=measure_option)
def check(target, target_host): ctx = tvm.context(target, 0) if not ctx.exist: logging.info("Skip test because %s is not available" % target) return # init task task, target = get_sample_task(target, target_host) logging.info("%s", task.config_space) measure_option = autotvm.measure_option( autotvm.LocalBuilder(), autotvm.LocalRunner()) tuner = RandomTuner(task) tuner.tune(n_trial=20, measure_option=measure_option)
def check(target, target_host): ctx = tvm.context(target, 0) if not ctx.exist: logging.info("Skip test because %s is not available" % target) return # init task task, target = get_sample_task(target, target_host) logging.info("%s", task.config_space) measure_option = autotvm.measure_option(mode='local', timeout=4, number=2) tuner = RandomTuner(task) tuner.tune(n_trial=10, measure_option=measure_option)
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 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='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="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 runner(target, dev): task, target = get_sample_task(target, None) logging.info("task config space: %s", task.config_space) # Note: we use the MockedLocalBuilder here instead of autotvm.LocalBuilder() measure_option = autotvm.measure_option(MockedLocalBuilder(), autotvm.LocalRunner()) results = [] tuner = RandomTuner(task) tuner.tune( n_trial=1, measure_option=measure_option, callbacks=(lambda _tuner, _inputs, rs: results.extend(rs), ), ) assert len(results) == 1
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) 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 == "xgb_knob": tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="knob") elif tuner == "xgb_itervar": tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="itervar") elif tuner == "xgb_curve": tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="curve") 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)) # process 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 autotvm.record.pick_best(tmp_log_file, log_filename) 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_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): # 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', feature_type='curve') elif tuner == 'xgb_knob': tuner_obj = XGBTuner(tsk, loss_type='rank', feature_type='curve') 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: print("use_transfer_learning...") if os.path.isfile(tmp_log_file): print("from file " + tmp_log_file) tuner_obj.load_history( autotvm.record.load_from_file(tmp_log_file)) # do tuning #print("INFO: len(tsk.config_space) = " + str(len(tsk.config_space))) n_trial = min(n_trial, len(tsk.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(tmp_log_file) ]) # pick best records to a cache file autotvm.record.pick_best(tmp_log_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) ]) 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, 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 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(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_tasks(tasks, measure_option, tuner='gridsearch', early_stopping=None, log_filename='tuning.log'): tmp_log_file = log_filename + ".tmp" if os.path.exists(tmp_log_file): os.remove(tmp_log_file) for i, task in enumerate(tasks): print(task) 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 == 'xgb_knob': tuner_obj = XGBTuner(tsk, loss_type='rank', feature_type='knob') 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 = 2 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(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 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_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: 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_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)
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
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)