Skip to content

A programming framework for agentic AI

fixing a few bugs in nlp (#503)

Xueqing Liu72301b856814
diff --git a/flaml/model.py b/flaml/model.py
index 7c4b8e861..168942379 100644
--- a/flaml/model.py
+++ b/flaml/model.py
@@ -407,14 +407,16 @@ class TransformersEstimator(BaseEstimator):
def _init_hf_args(self, automl_fit_kwargs: dict = None):
from .nlp.utils import HFArgs
- hf_args = HFArgs()
- for key, val in automl_fit_kwargs["hf_args"].items():
- assert (
- key in hf_args.__dict__
- ), "The specified key {} is not in the argument list of flaml.nlp.utils::HFArgs".format(
- key
- )
- setattr(hf_args, key, val)
+ hf_args = HFArgs(task=self._task)
+ fit_kwargs = automl_fit_kwargs.get("hf_args", None)
+ if fit_kwargs:
+ for key, val in fit_kwargs.items():
+ assert (
+ key in hf_args.__dict__
+ ), "The specified key {} is not in the argument list of flaml.nlp.utils::HFArgs".format(
+ key
+ )
+ setattr(hf_args, key, val)
self.hf_args = hf_args
def _update_hf_args(self, automl_pred_kwargs: dict = None):
diff --git a/flaml/nlp/utils.py b/flaml/nlp/utils.py
index 8604be28d..489d00b96 100644
--- a/flaml/nlp/utils.py
+++ b/flaml/nlp/utils.py
@@ -261,10 +261,6 @@ def tokenize_row(
hf_args=None,
return_column_name=False,
):
- assert (
- "max_seq_length" in hf_args.__dict__
- ), "max_seq_length must be provided for glue"
-
if prefix:
this_row = tuple(["".join(x) for x in zip(prefix, this_row)])
@@ -272,12 +268,13 @@ def tokenize_row(
tokenized_example = tokenizer(
*tuple(this_row),
padding="max_length",
- max_length=hf_args.max_seq_length,
+ max_length=hf_args.max_seq_length if hf_args else None,
truncation=True,
)
if task in NLG_TASKS:
tokenized_example["decoder_input_ids"] = tokenized_example["input_ids"]
tmp_column_names = sorted(tokenized_example.keys())
+
if return_column_name:
return [tokenized_example[x] for x in tmp_column_names], tmp_column_names
else:
@@ -324,7 +321,7 @@ def tokenize_swag(this_row, tokenizer, hf_args=None, return_column_name=False):
tokenized_example = tokenizer(
*tuple([first_sentences, second_sentences]),
truncation=True,
- max_length=hf_args.max_seq_length,
+ max_length=hf_args.max_seq_length if hf_args else None,
padding=False,
)
tmp_column_names = sorted(tokenized_example.keys())
@@ -442,7 +439,7 @@ class Counter:
return logdir
-def load_model(checkpoint_path, task, num_labels, per_model_config=None):
+def load_model(checkpoint_path, task, num_labels=None, per_model_config=None):
import transformers
transformers.logging.set_verbosity_error()
@@ -454,7 +451,7 @@ def load_model(checkpoint_path, task, num_labels, per_model_config=None):
)
from ..data import SEQCLASSIFICATION, SEQREGRESSION, TOKENCLASSIFICATION
- def get_this_model(task, model_config):
+ def get_this_model(checkpoint_path, task, model_config):
from transformers import AutoModelForSequenceClassification
from transformers import AutoModelForSeq2SeqLM
from transformers import AutoModelForMultipleChoice
@@ -519,7 +516,7 @@ def load_model(checkpoint_path, task, num_labels, per_model_config=None):
if is_pretrained_model_in_classification_head_list(this_model_type):
if num_labels != num_labels_old:
- this_model = get_this_model(task, new_config)
+ this_model = get_this_model(checkpoint_path, task, new_config)
new_config.num_labels = num_labels
this_model.num_labels = num_labels
this_model.classifier = (
@@ -528,9 +525,9 @@ def load_model(checkpoint_path, task, num_labels, per_model_config=None):
)
)
else:
- this_model = get_this_model(task, new_config)
+ this_model = get_this_model(checkpoint_path, task, new_config)
else:
- this_model = get_this_model(task, new_config)
+ this_model = get_this_model(checkpoint_path, task, new_config)
this_model.resize_token_embeddings(this_vocab_size)
return this_model
else:
@@ -539,7 +536,7 @@ def load_model(checkpoint_path, task, num_labels, per_model_config=None):
elif task == TOKENCLASSIFICATION:
model_config_num_labels = num_labels
model_config = _set_model_config(checkpoint_path)
- this_model = get_this_model(task, model_config)
+ this_model = get_this_model(checkpoint_path, task, model_config)
return this_model
@@ -574,6 +571,8 @@ class HFArgs:
ckpt_per_epoch (int, optional, defaults to 1): An integer, the number of checkpoints per epoch.
"""
+ task: str = field(default="seq-classification")
+
output_dir: str = field(
default="data/output/", metadata={"help": "data dir", "required": True}
)
@@ -608,8 +607,12 @@ class HFArgs:
metadata={"help": "per gpu evaluation batch size"},
)
+ def __post_init__(self):
+ if self.task in NLG_TASKS:
+ HFArgs.model_path = "t5-small"
+
@staticmethod
- def load_args():
+ def load_args_from_console():
from dataclasses import fields
arg_parser = argparse.ArgumentParser()
diff --git a/test/load_args.py b/test/load_args.py
index 3ad467c23..50b3a72b4 100644
--- a/test/load_args.py
+++ b/test/load_args.py
@@ -1,7 +1,7 @@
def test_load_args_sub():
from flaml.nlp.utils import HFArgs
- HFArgs.load_args()
+ HFArgs.load_args_from_console()
if __name__ == "__main__":
diff --git a/test/nlp/test_autohf_classificationhead.py b/test/nlp/test_autohf_classificationhead.py
index 8fc5fc474..826e6b553 100644
--- a/test/nlp/test_autohf_classificationhead.py
+++ b/test/nlp/test_autohf_classificationhead.py
@@ -69,3 +69,7 @@ def test_classification_head():
)
except requests.exceptions.HTTPError:
return
+
+
+if __name__ == "__main__":
+ test_classification_head()