Skip to content

Commit

Permalink
Properly handle no validation split
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaspit committed Oct 14, 2023
1 parent 05ce590 commit e5de92c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions silnlp/nmt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ def stats_max_size(self) -> int:
def has_parent(self) -> bool:
return "parent" in self.data

@property
def has_val_split(self) -> bool:
return any(
pair.is_val and (pair.size if pair.val_size is None else pair.val_size) > 0 for pair in self.corpus_pairs
)

def set_seed(self) -> None:
seed = self.data["seed"]
set_seed(seed)
Expand Down
6 changes: 6 additions & 0 deletions silnlp/nmt/hugging_face_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ def __init__(self, exp_dir: Path, config: dict) -> None:

super().__init__(exp_dir, config)

# disable evaluation if there is no validation split
if not self.has_val_split:
config["eval"]["evaluation_strategy"] = "no"
config["eval"]["load_best_model_at_end"] = False
config["eval"]["early_stopping"] = None

@property
def model_dir(self) -> Path:
return Path(self.train["output_dir"])
Expand Down
5 changes: 3 additions & 2 deletions silnlp/nmt/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ def test(

tokenizer = config.create_tokenizer()
model = config.create_model()
_, best_step = model.get_checkpoint_path(CheckpointType.BEST)
results: Dict[int, List[PairScore]] = {}
step: int
if checkpoint is not None:
Expand Down Expand Up @@ -523,7 +522,9 @@ def test(
except ValueError:
LOGGER.warn("No average checkpoint available.")

if best:
best_step = 0
if best and config.has_val_split:
_, best_step = model.get_checkpoint_path(CheckpointType.BEST)
step = best_step
if step not in results:
results[step] = test_checkpoint(
Expand Down

0 comments on commit e5de92c

Please sign in to comment.