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

Update decoding.py #1713

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 20 additions & 20 deletions trax/supervised/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,26 @@ def autoregressive_sample(model, inputs=None,
a batch of output sequences. output_length is the maximum length of the
output sequences, where each sequence can be no longer than `max_length`.
"""
result = []
eos_seen = []
counter = 0
for sample in autoregressive_sample_stream(
model, inputs, batch_size=batch_size, temperature=temperature,
start_id=start_id, accelerate=accelerate, eval_mode=eval_mode,
eval_min_length=eval_min_length):
sample = sample[:, None]
result.append(sample)
counter += 1
if counter >= max_length:
return np.concatenate(result, axis=1)
# Check at which batch positions have we already encountered EOS.
for j in range(batch_size):
if int(sample[j, 0]) == eos_id:
eos_seen.append(j)
# If EOS has been seen on all positions, stop.
if all([j in eos_seen for j in range(batch_size)]):
return np.concatenate(result, axis=1)
return np.concatenate(result, axis=1)
saved_state = model.state
try:
for sample in autoregressive_sample_stream(
model, inputs, batch_size=batch_size, temperature=temperature,
start_id=start_id, accelerate=accelerate):
sample = sample[:, None]
result.append(sample)
counter += 1
if counter >= max_length:
return np.concatenate(result, axis=1)
# Check at which batch positions have we already encountered EOS.
for j in range(batch_size):
if int(sample[j, 0]) == eos_id:
eos_seen.append(j)
# If EOS has been seen on all positions, stop.
if all([j in eos_seen for j in range(batch_size)]):
return np.concatenate(result, axis=1)
return np.concatenate(result, axis=1)
finally:
model.state = saved_state


def beam_search(model, inputs=None, batch_size=1, n_beams=2, start_id=0,
Expand Down