Run the decoder transformer layer by FasterTransformer. Args: inputs: A tf.Tensor with shape [batch_size * beam_width, 1, hidden_dimension]. The inputs tensor of encoder. The rank must be 3. memory_tensor: A tf.tensor with shape [batch_size * beam_width,
(inputs,
memory_tensor,
memory_sequence_length,
op_self_cache,
op_mem_cache,
psuedo_input,
var_dict,
decoder_args,
step,
sequence_lengths)
| 327 | return self_cache, mem_cache |
| 328 | |
| 329 | def op_decoder(inputs, |
| 330 | memory_tensor, |
| 331 | memory_sequence_length, |
| 332 | op_self_cache, |
| 333 | op_mem_cache, |
| 334 | psuedo_input, |
| 335 | var_dict, |
| 336 | decoder_args, |
| 337 | step, |
| 338 | sequence_lengths): |
| 339 | ''' |
| 340 | Run the decoder transformer layer by FasterTransformer. |
| 341 | |
| 342 | Args: |
| 343 | inputs: A tf.Tensor with shape [batch_size * beam_width, 1, hidden_dimension]. |
| 344 | The inputs tensor of encoder. The rank must be 3. |
| 345 | memory_tensor: A tf.tensor with shape [batch_size * beam_width, max(memory_sequence_length), encoder_hidden_dimension]. |
| 346 | The results of encoder transformer layer. The rank must be 3. |
| 347 | Note that it must be extended by beam_width times |
| 348 | memory_sequence_length: A tf.Tensor with shape [batch_size * beam_width], type tf.int. |
| 349 | The length of each sentence of results of encoder. |
| 350 | Note that it must be extended by beam_width times |
| 351 | op_self_cache: A tf.Tensor with shape [num_layer, 2, None, batch_size * beam_width, hidden_dimension]. |
| 352 | The cache space to store the keys and values of first attention layer in each step. |
| 353 | op_mem_cache: A tf.Tensor with shape [num_layer, 2, batch_size * beam_width, max(memory_sequence_length) hidden_dimension]. |
| 354 | The cache space to store the keys and values of second attention layer. |
| 355 | Since they are same in each step, it is only need to compute them in first time. |
| 356 | psuedo_input: A tf.Tensor or null list. |
| 357 | Put the decoder results of TensorFlow when running the TensorFlow decoder and FasterTransformer |
| 358 | decoder in one model. This prevents the race condition. |
| 359 | It is useless when only run the FasterTransformer decoder. |
| 360 | decoder_args: The arguments for decoder. The details are in the class "TransformerArgument" of common.py |
| 361 | var_dict: A dict of tf.Tensor or numpy array. The variables for decoder. |
| 362 | They can be either some tensor or some numpy array. |
| 363 | |
| 364 | Outputs: |
| 365 | outputs: A tf.Tensor with shape [batch_size * beam_width, 1, hidden_dimension]. |
| 366 | The results of decoder. |
| 367 | ''' |
| 368 | |
| 369 | ''' |
| 370 | If fuse_qkv == True, this means that the computation of q, k, v in decoder are fused in one convolution. |
| 371 | |
| 372 | Therefore, we need to split them and then passing into the decoder op. The split will bring additional overhead, |
| 373 | especially when the batch size is small because the computation time is short. |
| 374 | |
| 375 | However, because most of the pretrained model on network fuse the qkv, so we fuse them as default. |
| 376 | ''' |
| 377 | |
| 378 | decoder_op_module = tf.load_op_library(os.path.join('./lib/libtf_decoder.so')) |
| 379 | |
| 380 | use_batch_major_op_cache, _ = get_op_cache_config(decoder_args.size_per_head, decoder_args.dtype) |
| 381 | if use_batch_major_op_cache == False: |
| 382 | op_self_cache = tf.contrib.framework.nest.map_structure( |
| 383 | lambda s: tf.concat([s, tf.zeros([decoder_args.num_layer, 1, |
| 384 | tf.shape(memory_tensor)[0], |
| 385 | decoder_args.hidden_dim], dtype=decoder_args.dtype)], axis=1), |
| 386 | op_self_cache ) |
no test coverage detected