Skip to content
This repository was archived by the owner on Jul 7, 2023. It is now read-only.

Fix decoding in prepend mode #1726

Merged
merged 6 commits into from
Nov 21, 2019
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
2 changes: 0 additions & 2 deletions tensor2tensor/models/research/universal_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ def universal_transformer_base():
@registry.register_hparams
def universal_transformer_base_tpu():
hparams = universal_transformer_base()
hparams = update_hparams_for_universal_transformer(hparams)
transformer.update_hparams_for_tpu(hparams)
hparams.add_step_timing_signal = False
return hparams
Expand All @@ -467,7 +466,6 @@ def universal_transformer_base_tpu():
@registry.register_hparams
def universal_transformer_big():
hparams = universal_transformer_base()
hparams = update_hparams_for_universal_transformer(hparams)
hparams.hidden_size = 2048
hparams.filter_size = 8192
return hparams
Expand Down
13 changes: 8 additions & 5 deletions tensor2tensor/models/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,9 +863,15 @@ def symbols_to_logits_fn(ids, i, cache):
vocab_size = tf.shape(ret)[1]

def forced_logits():
# Workaround for: tf.one_hot(
# tf.repeat(partial_targets[:, i], [beam_size]), vocab_size, 0.0,
# -1e9)
# Can be replaced by the above in future versions (from tf 1.15).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @senarvi -- thanks for the PR, I'm going to be releasing on TF 1.15 really soon.

Can you "flip" the comments, i.e. make the 1.15 one main and put the other in a comment?

Also a brief explanation of why this workaround is needed would be great

return tf.one_hot(
tf.tile(partial_targets[:, i], [beam_size]), vocab_size, 0.0,
-1e9)
tf.reshape(tf.tile(
tf.reshape(partial_targets[:, i], [-1, 1]),
[1, beam_size]), [-1]),
vocab_size, 0.0, -1e9)

ret = tf.cond(
tf.less(i, partial_targets_length), forced_logits, lambda: ret)
Expand Down Expand Up @@ -1168,9 +1174,6 @@ def fast_decode(encoder_output,
"scores": decoding log probs from the beam search,
None if using greedy decoding (beam_size=1)
}

Raises:
NotImplementedError: If beam size > 1 with partial targets.
"""
if encoder_output is not None:
batch_size = common_layers.shape_list(encoder_output)[0]
Expand Down
14 changes: 14 additions & 0 deletions tensor2tensor/utils/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,13 @@ def _interactive_input_tensor_to_features_dict(feature_map, hparams):
features["decode_length"] = (
IMAGE_DECODE_LENGTH if input_is_image else inputs[1])
features["inputs"] = x
# Save inputs to "partial_targets" when prepending inputs to targets. Also
# keep "inputs" as some models crash if they don't exist.
if getattr(hparams, "prepend_mode", "none") != "none":
shape = tf.shape(x)
partial_targets = tf.reshape(x, [shape[0], shape[1]])
partial_targets = tf.pad(partial_targets, [[0, 0], [0, 1]])
features["partial_targets"] = partial_targets
return features


Expand Down Expand Up @@ -957,6 +964,13 @@ def _decode_input_tensor_to_features_dict(feature_map, hparams):
features["decode_length"] = (
IMAGE_DECODE_LENGTH if input_is_image else tf.shape(x)[1] + 50)
features["inputs"] = x
# Save inputs to "partial_targets" when prepending inputs to targets. Also
# keep "inputs" as some models crash if they don't exist.
if getattr(hparams, "prepend_mode", "none") != "none":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if hasattr(hparams, "prepend_mode"):
  ...

seems more idiomatic python, here and above

shape = tf.shape(x)
partial_targets = tf.reshape(x, [shape[0], shape[1]])
partial_targets = tf.pad(partial_targets, [[0, 0], [0, 1]])
features["partial_targets"] = partial_targets
return features


Expand Down