Add SigOpt HPO to transformers trainer api (#13572)
* add sigopt hpo to transformers. Signed-off-by: Ding, Ke <ke.ding@intel.com> * extend sigopt changes to test code and others.. Signed-off-by: Ding, Ke <ke.ding@intel.com> * Style. * fix style for sigopt integration. Signed-off-by: Ding, Ke <ke.ding@intel.com> * Add necessary information to run unittests on SigOpt. Co-authored-by: Morgan Funtowicz <funtowiczmo@gmail.com>
This commit is contained in:
@@ -50,6 +50,7 @@ from transformers.testing_utils import (
|
||||
require_optuna,
|
||||
require_ray,
|
||||
require_sentencepiece,
|
||||
require_sigopt,
|
||||
require_tokenizers,
|
||||
require_torch,
|
||||
require_torch_gpu,
|
||||
@@ -1522,3 +1523,54 @@ class TrainerHyperParameterRayIntegrationTest(unittest.TestCase):
|
||||
with ray_start_client_server():
|
||||
assert ray.util.client.ray.is_connected()
|
||||
self.ray_hyperparameter_search()
|
||||
|
||||
|
||||
@require_torch
|
||||
@require_sigopt
|
||||
class TrainerHyperParameterSigOptIntegrationTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
args = TrainingArguments(".")
|
||||
self.n_epochs = args.num_train_epochs
|
||||
self.batch_size = args.train_batch_size
|
||||
|
||||
def test_hyperparameter_search(self):
|
||||
class MyTrialShortNamer(TrialShortNamer):
|
||||
DEFAULTS = {"a": 0, "b": 0}
|
||||
|
||||
def hp_space(trial):
|
||||
return [
|
||||
{"bounds": {"min": -4, "max": 4}, "name": "a", "type": "int"},
|
||||
{"bounds": {"min": -4, "max": 4}, "name": "b", "type": "int"},
|
||||
]
|
||||
|
||||
def model_init(trial):
|
||||
if trial is not None:
|
||||
a = trial.assignments["a"]
|
||||
b = trial.assignments["b"]
|
||||
else:
|
||||
a = 0
|
||||
b = 0
|
||||
config = RegressionModelConfig(a=a, b=b, double_output=False)
|
||||
|
||||
return RegressionPreTrainedModel(config)
|
||||
|
||||
def hp_name(trial):
|
||||
return MyTrialShortNamer.shortname(trial.assignments)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
trainer = get_regression_trainer(
|
||||
output_dir=tmp_dir,
|
||||
learning_rate=0.1,
|
||||
logging_steps=1,
|
||||
evaluation_strategy=IntervalStrategy.EPOCH,
|
||||
save_strategy=IntervalStrategy.EPOCH,
|
||||
num_train_epochs=4,
|
||||
disable_tqdm=True,
|
||||
load_best_model_at_end=True,
|
||||
logging_dir="runs",
|
||||
run_name="test",
|
||||
model_init=model_init,
|
||||
)
|
||||
trainer.hyperparameter_search(
|
||||
direction="minimize", hp_space=hp_space, hp_name=hp_name, backend="sigopt", n_trials=4
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user