MCPcopy Create free account
hub / github.com/NVIDIA/FasterTransformer / tf_decoder

Function tf_decoder

examples/tensorflow/decoder/utils/decoder.py:76–278  ·  view source on GitHub ↗

Run the decoder transformer layer by TensorFlow. Args: decoder_args: The arguments for decoder. The details are in the class "TransformerArgument" of common.py inputs: A tf.Tensor with shape [batch_size * beam_width, 1, hidden_dimension].

(decoder_args,
               inputs,
               memory,
               memory_sequence_length,
               step,
               cache=None)

Source from the content-addressed store, hash-verified

74
75
76def tf_decoder(decoder_args,
77 inputs,
78 memory,
79 memory_sequence_length,
80 step,
81 cache=None):
82 '''
83 Run the decoder transformer layer by TensorFlow.
84
85 Args:
86 decoder_args: The arguments for decoder. The details are in the class "TransformerArgument" of common.py
87 inputs: A tf.Tensor with shape [batch_size * beam_width, 1, hidden_dimension].
88 The inputs tensor of encoder. The rank must be 3.
89 memory: A tf.tensor with shape [batch_size * beam_width, max(memory_sequence_length), encoder_hidden_dimension].
90 The results of encoder transformer layer. The rank must be 3.
91 Note that it must be extended by beam_width times
92 memory_sequence_length: A tf.Tensor with shape [batch_size * beam_width], type tf.int.
93 The length of each sentence of results of encoder.
94 Note that it must be extended by beam_width times
95 step: A tf.Tensor with tf.int type. The current step in the translation process.
96 cache: A dict. The cache space to store the keys and values of attention layers.
97
98 Outputs:
99 outputs: A tf.Tensor with shape [batch_size * beam_width, 1, hidden_dimension].
100 The results of decoder.
101 '''
102
103 k_init_range = decoder_args.kernel_init_range
104 b_init_range = decoder_args.bias_init_range
105 data_type = decoder_args.dtype
106 fuse_qkv = decoder_args.fuse_qkv
107 hidden_dim = decoder_args.hidden_dim
108
109 memory_mask = None # has something
110
111 if memory is not None and not tf.contrib.framework.nest.is_sequence(memory):
112 memory = (memory,)
113 if memory_sequence_length is not None:
114 if not tf.contrib.framework.nest.is_sequence(memory_sequence_length):
115 memory_sequence_length = (memory_sequence_length,)
116 memory_mask = [
117 build_sequence_mask(
118 length, num_heads=decoder_args.head_num, maximum_length=tf.shape(m)[1], data_type=data_type)
119 for m, length in zip(memory, memory_sequence_length)]
120
121 for l in range(decoder_args.num_layer):
122 layer_name = "layer_{}".format(l)
123 layer_cache = cache[layer_name] if cache is not None else None
124
125 with tf.variable_scope(layer_name):
126 with tf.variable_scope("masked_multi_head"):
127 norm_inputs = norm(inputs)
128 if fuse_qkv == True:
129 queries, keys, values = tf.split( tf.layers.conv1d(norm_inputs, decoder_args.hidden_dim * 3, 1,
130 bias_initializer=create_initializer(b_init_range, data_type),
131 kernel_initializer=create_initializer(k_init_range, data_type)), 3, axis=2)
132 else:
133 '''

Callers 1

decoding_bodyFunction · 0.90

Calls 3

create_initializerFunction · 0.90
normFunction · 0.85
build_sequence_maskFunction · 0.70

Tested by

no test coverage detected