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

Function tf_encoder_opennmt

examples/tensorflow/encoder/utils/encoder.py:47–133  ·  view source on GitHub ↗

Run the bert transformer layer by TensorFlow. Args: input_tensor: A tf.Tensor with shape [batch_size, seq_len, hidden_dimension]. The inputs tensor of encoder. The rank must be 3. encoder_args: The arguments for encoder. The details are in the c

(input_tensor,
                        encoder_args,
                        sequence_length,
                        initializer_range=0.02)

Source from the content-addressed store, hash-verified

45 return mask
46
47def tf_encoder_opennmt(input_tensor,
48 encoder_args,
49 sequence_length,
50 initializer_range=0.02):
51 '''
52 Run the bert transformer layer by TensorFlow.
53
54 Args:
55 input_tensor: A tf.Tensor with shape [batch_size, seq_len, hidden_dimension].
56 The inputs tensor of encoder. The rank must be 3.
57 encoder_args: The arguments for encoder. The details are in the class
58 "TransformerArgument" of common.py
59 sequence_length: A tf.Tensor with shape [batch_size], with tf.int type.
60 The sequence length of each sentence in input_tensor.
61 initializer_range: A float value.
62 The range of initializer for all weights.
63
64 Outputs:
65 output: A tf.Tensor with shape [batch_size, max(sequence_length), hidden_dimension].
66 The results of encoder.
67 '''
68
69 data_type = encoder_args.dtype
70 input_shape = get_shape_list(input_tensor, expected_rank=3)
71 batch_size = input_shape[0]
72 seq_length = input_shape[1]
73
74 input_tensor *= encoder_args.hidden_dim**0.5
75 position_encoder = SinusoidalPositionEncoder()
76 input_tensor = position_encoder(input_tensor, position=tf.range(seq_length))
77
78 mask = build_sequence_mask(
79 sequence_length,
80 encoder_args.head_num,
81 maximum_length=tf.shape(input_tensor)[1],
82 dtype=data_type)
83
84 intermediate_size = encoder_args.hidden_dim * 4
85 if encoder_args.hidden_dim % encoder_args.head_num != 0:
86 raise ValueError(
87 "The hidden size (%d) is not a multiple of the number of attention "
88 "heads (%d)" % (encoder_args.hidden_dim, encoder_args.head_num))
89
90 layer_input = input_tensor
91 for layer_idx in range(encoder_args.num_layer):
92 with tf.variable_scope("layer_%d" % layer_idx, reuse=tf.AUTO_REUSE):
93 with tf.variable_scope("multi_head"):
94 normed_input = tf.cast(layer_norm(tf.cast(layer_input, tf.float32)), data_type)
95
96 queries, keys, values = tf.split(tf.layers.conv1d(normed_input, encoder_args.hidden_dim * 3, 1), 3, axis=2)
97
98 # split head
99 queries = tf.reshape(queries, [batch_size, seq_length, encoder_args.head_num, encoder_args.size_per_head])
100 queries = tf.transpose(queries, [0, 2, 1, 3])
101
102 keys = tf.reshape(keys, [batch_size, seq_length, encoder_args.head_num, encoder_args.size_per_head])
103 keys = tf.transpose(keys, [0, 2, 1, 3])
104

Callers 3

encoder_exampleFunction · 0.90
_tf_bodyFunction · 0.90
translateFunction · 0.90

Calls 4

get_shape_listFunction · 0.70
build_sequence_maskFunction · 0.70
layer_normFunction · 0.70

Tested by

no test coverage detected