start_id=start_of_sentence_id,
                                     end_id=end_of_sentence_id,
                                     encoder_hidden_dim=memory_hidden_dim,
                                     dtype=tf_datatype)

    embedding_table = np.random.rand(vocab_size, hidden_dim).astype(
        np_datatype)  # a [vocab_size, hidden_dim] table
    embedding_table = tf.convert_to_tensor(embedding_table)
    memory, memory_sequence_length = generate_encoder_result(
        batch_size, max_seq_len, memory_hidden_dim, tf_datatype)

    finalized_tf_output_ids, finalized_tf_sequence_lengths, tf_output_ids, \
        tf_parent_ids, tf_sequence_lengths = tf_decoding(memory,
                                                         memory_sequence_length,
                                                         embedding_table,
                                                         decoding_args,
                                                         0,
                                                         kernel_initializer_range,
                                                         bias_initializer_range)

    all_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
    finalized_op_output_ids, finalized_op_sequence_lengths, op_output_ids, \
        op_parent_ids, op_sequence_lengths = op_decoding(memory,
                                                         memory_sequence_length,
                                                         embedding_table,
                                                         all_vars,
                                                         decoding_args)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    if use_XLA == 1:
示例#2
0
                                     start_id=start_of_sentence_id,
                                     end_id=end_of_sentence_id,
                                     encoder_hidden_dim=memory_hidden_dim,
                                     dtype=tf_datatype)

    embedding_table = np.random.randn(vocab_size, hidden_dim).astype(
        np_datatype) * 0.01  # a [vocab_size, hidden_dim] table
    embedding_table = tf.convert_to_tensor(embedding_table)
    memory, memory_sequence_length = generate_encoder_result(
        batch_size, max_seq_len, memory_hidden_dim, tf_datatype)

    finalized_tf_output_ids, finalized_tf_sequence_lengths, tf_output_ids, \
        tf_parent_ids, tf_sequence_lengths = tf_decoding(memory,
                                                         memory_sequence_length,
                                                         embedding_table,
                                                         decoding_args,
                                                         args.decoder_type,
                                                         kernel_initializer_range,
                                                         bias_initializer_range,
                                                         atol_threshold)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.tables_initializer())
        sess.run(finalized_tf_output_ids)

        if args.test_time == 1:
            time_cost = time_test(sess, finalized_tf_output_ids, iterations=50)
            types = ["TF", "OP", "TF+OP"]
            print("[INFO] time costs of {} decoder: {} ms.".format(
示例#3
0
            memory=memory,
            memory_sequence_length=memory_sequence_length)
        target_vocab_rev = target_inputter.vocabulary_lookup_reverse()
        target_tokens = target_vocab_rev.lookup(tf.cast(target_ids, tf.int64))
        opennmt_target_length = target_length
        opennmt_target_tokens = target_tokens
        opennmt_target_ids = target_ids

        opennmt_variables = tf.global_variables()

    ## TF Decoding ###
    finalized_tf_output_ids, finalized_tf_sequence_lengths, tf_output_ids, \
        tf_parent_ids, tf_sequence_lengths = tf_decoding(tf_encoder_result,
                                                            memory_sequence_length,
                                                            target_inputter.embedding,
                                                            decoding_args,
                                                            decoder_type=1,
                                                            kernel_initializer_range=kernel_initializer_range,
                                                            bias_initializer_range=bias_initializer_range)

    tf_target_ids = finalized_tf_output_ids
    tf_target_length = finalized_tf_sequence_lengths
    tf_target_tokens = target_vocab_rev.lookup(tf.cast(tf_target_ids,
                                                       tf.int64))
    ## end of tf decoding ##

    ## op decoding ##
    all_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
    decoder_var_start_id = 0

    while all_vars[decoder_var_start_id].name.find(
                                     max_seq_len=max_seq_len,
                                     vocab_size=vocab_size,
                                     start_id=start_of_sentence_id,
                                     end_id=end_of_sentence_id,
                                     encoder_hidden_dim=encoder_head_num *
                                     encoder_size_per_head,
                                     dtype=tf_datatype)

    tf_encoder_result = tf_encoder(input_tensor=from_tensor,
                                   encoder_args=encoder_args,
                                   attention_mask=attention_mask)
    tf_encoder_result = tf.reshape(
        tf_encoder_result, [batch_size, max_seq_len, encoder_hidden_dim])

    tf_decoding_result, _, _, _, _ = tf_decoding(
        tf_encoder_result, memory_sequence_length, embedding_table,
        decoding_args, args.decoder_type, kernel_initializer_range,
        bias_initializer_range, atol_threshold)

    encoder_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
    op_encoder_result = op_encoder(inputs=from_tensor,
                                   encoder_args=encoder_args,
                                   encoder_vars=encoder_variables,
                                   attention_mask=attention_mask)
    op_encoder_result = tf.reshape(
        op_encoder_result, [batch_size, max_seq_len, encoder_hidden_dim])

    op_decoding_result, _, _, _, _ = tf_decoding(
        op_encoder_result, memory_sequence_length, embedding_table,
        decoding_args, args.decoder_type, kernel_initializer_range,
        bias_initializer_range, atol_threshold)