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

Small changes in greed search #2577

Closed
Closed
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
7 changes: 7 additions & 0 deletions onmt/opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,13 @@ def _add_decoding_opts(parser):
"(or the identified source token does not exist in "
"the table), then it will copy the source token.",
)
group.add(
"--stop_token",
"-stop_token",
type=str,
default="",
help="Stop token to be used instead of the EOS token.",
)


def translate_opts(parser):
Expand Down
1 change: 1 addition & 0 deletions onmt/translate/greedy_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def __init__(
self.topk_scores = None
self.beam_size = beam_size
self.n_best = n_best
self.parallel_paths = 1

def initialize(
self, enc_out, src_len, src_map=None, device=None, target_prefix=None
Expand Down
7 changes: 5 additions & 2 deletions onmt/translate/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def build_translator(opt, device_id=0, report_score=True, logger=None, out_file=
report_score=report_score,
logger=logger,
)
if opt.stop_token is DefaultTokens.SEP:
translator.eos = translator.vocabs["tgt"].lookup_token("<0x0A>")
else:
translator = Translator.from_opt(
model,
Expand Down Expand Up @@ -142,7 +144,7 @@ def __init__(
self._tgt_pad_idx = vocabs["tgt"].lookup_token(DefaultTokens.PAD)
self._tgt_bos_idx = vocabs["tgt"].lookup_token(DefaultTokens.BOS)
self._tgt_unk_idx = vocabs["tgt"].lookup_token(DefaultTokens.UNK)
self._tgt_sep_idx = vocabs["tgt"].lookup_token(DefaultTokens.SEP)
self._tgt_sep_idx = vocabs["tgt"].lookup_token("<0x0A>")
self._tgt_start_with = vocabs["tgt"].lookup_token(vocabs["decoder_start_token"])
self._tgt_vocab_len = len(self._tgt_vocab)

Expand Down Expand Up @@ -907,7 +909,6 @@ def _translate_batch_with_strategy(self, batch, decode_strategy):
# (0) Prep the components of the search.
use_src_map = self.copy_attn
parallel_paths = decode_strategy.parallel_paths # beam_size

batch_size = len(batch["srclen"])

# (1) Run the encoder on the src.
Expand Down Expand Up @@ -1022,6 +1023,7 @@ def translate_batch(self, batch, attn_debug, scoring=False):
max_length = 0 if scoring else self.max_length
with torch.no_grad():
if self.sample_from_topk != 0 or self.sample_from_topp != 0:
self.beam_size = 1
decode_strategy = GreedySearchLM(
pad=self._tgt_pad_idx,
bos=self._tgt_bos_idx,
Expand Down Expand Up @@ -1128,6 +1130,7 @@ def _translate_batch_with_strategy(self, batch, decode_strategy, left_pad=True):
# (4) Begin decoding step by step:
# beg_time = time()
for step in range(decode_strategy.max_length):
print("# step", step)
decoder_input = (
src if step == 0 else decode_strategy.current_predictions.view(-1, 1, 1)
)
Expand Down
Loading