Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adaptertrainer hyperparameter tuning #482

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/transformers/adapters/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ def __init__(
or isinstance(self.model.active_adapters, AdapterCompositionBlock)
and any([isinstance(child, Fuse) for child in self.model.active_adapters.children])
)
if model.active_adapters is None:
if self.model.active_adapters is None:
raise ValueError(
"Expected a model with an active adapter setup."
"If you want to fully finetune the model use the Trainer class."
)
if (self.label_names is None or len(self.label_names) < 1) and model.active_head is not None:
if (self.label_names is None or len(self.label_names) < 1) and self.model.active_head is not None:
all_label_names = set()
for head in model._active_heads:
all_label_names |= set(model.heads[head].get_label_names())
for head in self.model._active_heads:
all_label_names |= set(self.model.heads[head].get_label_names())
self.label_names = list(all_label_names)

def create_optimizer(self):
Expand Down
51 changes: 50 additions & 1 deletion tests_adapters/test_adapter_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
from transformers.adapters.composition import Fuse, Stack
from transformers.adapters.trainer import AdapterTrainer, logger
from transformers.testing_utils import slow
from transformers.testing_utils import slow, require_ray


class TestAdapterTrainer(unittest.TestCase):
Expand Down Expand Up @@ -364,6 +364,55 @@ def test_general(self):
self.assertEqual("task", model.active_head)
self.assertEqual(Stack("task"), model.active_adapters)

@require_ray
def test_hyperparameter_search_works_with_AdapterTrainer(self):
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
data_args = GlueDataTrainingArguments(
task_name="mrpc", data_dir="./tests/fixtures/tests_samples/MRPC", overwrite_cache=True
)
train_dataset = GlueDataset(data_args, tokenizer=tokenizer, mode="train")
eval_dataset = train_dataset

def hp_space(params):
from ray import tune
return {
"learning_rate": tune.choice([0.1, 0.2]),
}

def model_init(trail=None):
model = AutoAdapterModel.from_pretrained("bert-base-uncased")

model.add_classification_head("task", num_labels=3)

# add the adapters to be fused
model.add_adapter("task")
model.add_adapter("additional_adapter")

model.train_adapter("task")
return model

with TemporaryDirectory() as tempdir:
training_args = TrainingArguments(
output_dir=tempdir,
do_train=True,
learning_rate=0.1,
logging_steps=1,
max_steps=1,
save_steps=1,
remove_unused_columns=False,
)
trainer = AdapterTrainer(
model=None,
model_init=model_init,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset
)

trainer.hyperparameter_search(
direction="minimize", hp_space=hp_space, backend="ray", n_trials=2
)


if __name__ == "__main__":
unittest.main()