This C++ API example demonstrates how to build GNMT model inference.
#include <assert.h>
#include <cstring>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string>
#include "example_utils.hpp"
const dim_t batch = 64;
const dim_t src_seq_length_max = 25;
const dim_t tgt_seq_length_max = 27;
const dim_t feature_size = 1024;
const dim_t enc_bidir_n_layers = 1;
const dim_t enc_unidir_n_layers = 7;
const dim_t dec_n_layers = 8;
const int lstm_n_gates = 4;
std::vector<int32_t> weighted_src_layer(batch *feature_size, 1);
std::vector<float> alignment_model(
src_seq_length_max *batch *feature_size, 1.0f);
std::vector<float> alignments(src_seq_length_max *batch, 1.0f);
std::vector<float> exp_sums(batch, 1.0f);
void compute_weighted_annotations(float *weighted_annotations,
dim_t src_seq_length_max, dim_t batch, dim_t feature_size,
float *weights_annot, float *annotations) {
dim_t num_weighted_annotations = src_seq_length_max * batch;
dnnl_sgemm(
'N',
'N', num_weighted_annotations, feature_size, feature_size,
1.f, annotations, feature_size, weights_annot, feature_size, 0.f,
weighted_annotations, feature_size);
}
void compute_sum_of_rows(
int8_t *a, dim_t rows, dim_t cols, int32_t *a_reduced) {
PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(1)
for (dim_t i = 0; i < cols; i++) {
a_reduced[i] = 0;
for (dim_t j = 0; j < rows; j++) {
a_reduced[i] += (int32_t)a[i * rows + j];
}
}
}
void compute_attention(float *context_vectors, dim_t src_seq_length_max,
dim_t batch, dim_t feature_size, int8_t *weights_src_layer,
float weights_src_layer_scale, int32_t *compensation,
uint8_t *dec_src_layer, float dec_src_layer_scale,
float dec_src_layer_shift, uint8_t *annotations,
float *weighted_annotations, float *weights_alignments) {
int32_t co = 0;
dec_src_layer, feature_size, 0, weights_src_layer, feature_size, 0,
0.f, weighted_src_layer.data(), feature_size, &co);
float *alignment_model_ptr = alignment_model.data();
PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(2)
for (dim_t i = 0; i < src_seq_length_max; i++) {
for (dim_t j = 0; j < batch; j++) {
for (dim_t k = 0; k < feature_size; k++) {
size_t tnc_offset
= i * batch * feature_size + j * feature_size + k;
alignment_model_ptr[tnc_offset]
= tanhf((float)(weighted_src_layer[j * feature_size + k]
- dec_src_layer_shift * compensation[k])
/ (dec_src_layer_scale
* weights_src_layer_scale)
+ weighted_annotations[tnc_offset]);
}
}
}
dim_t num_weighted_annotations = src_seq_length_max * batch;
dnnl_sgemm(
'N',
'N', num_weighted_annotations, 1, feature_size, 1.f,
alignment_model_ptr, feature_size, weights_alignments, 1, 0.f,
alignments.data(), 1);
PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(1)
for (dim_t i = 0; i < batch; i++)
exp_sums[i] = 0.0f;
PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(2)
for (dim_t i = 0; i < src_seq_length_max; i++) {
for (dim_t j = 0; j < batch; j++) {
alignments[i * batch + j] = expf(alignments[i * batch + j]);
exp_sums[j] += alignments[i * batch + j];
}
}
PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(2)
for (dim_t i = 0; i < src_seq_length_max; i++)
for (dim_t j = 0; j < batch; j++)
alignments[i * batch + j] /= exp_sums[j];
PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(2)
for (dim_t i = 0; i < batch; i++)
for (dim_t j = 0; j < feature_size; j++)
context_vectors[i * (feature_size + feature_size) + feature_size
+ j]
= 0.0f;
PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(3)
for (dim_t i = 0; i < batch; i++)
for (dim_t k = 0; k < src_seq_length_max; k++)
for (dim_t j = 0; j < feature_size; j++)
context_vectors[i * (feature_size + feature_size) + feature_size
+ j]
+= alignments[k * batch + i]
* (((float)annotations[j
+ feature_size * (i + batch * k)]
- dec_src_layer_shift)
/ dec_src_layer_scale);
}
void copy_context(
float *src_iter, dim_t n_layers, dim_t batch, dim_t feature_size) {
PRAGMA_OMP_PARALLEL_FOR_COLLAPSE(3)
for (dim_t k = 1; k < n_layers; k++)
for (dim_t j = 0; j < batch; j++)
for (dim_t i = 0; i < feature_size; i++)
src_iter[(k * batch + j) * (feature_size + feature_size)
+ feature_size + i]
= src_iter[j * (feature_size + feature_size)
+ feature_size + i];
}
void simple_net() {
std::vector<primitive> encoder_net, decoder_net;
std::vector<std::unordered_map<int, memory>> encoder_net_args,
decoder_net_args;
std::vector<float> net_src(batch * src_seq_length_max * feature_size, 0.1f);
std::vector<float> net_dst(batch * tgt_seq_length_max * feature_size, 0.1f);
const float data_shift = 64.;
const float data_scale = 63.;
const int weights_scale_mask = 0
+ (1 << 3)
+ (1 << 4);
std::vector<float> weights_scales(lstm_n_gates * feature_size);
const dim_t scales_half = lstm_n_gates * feature_size / 2;
std::fill(
weights_scales.begin(), weights_scales.begin() + scales_half, 30.f);
std::fill(
weights_scales.begin() + scales_half, weights_scales.end(), 65.5f);
= {src_seq_length_max, batch, feature_size};
= {enc_bidir_n_layers, 2, feature_size, lstm_n_gates, feature_size};
= {enc_bidir_n_layers, 2, feature_size, lstm_n_gates, feature_size};
= {enc_bidir_n_layers, 2, lstm_n_gates, feature_size};
= {src_seq_length_max, batch, 2 * feature_size};
std::vector<float> user_enc_bidir_wei_layer(
enc_bidir_n_layers * 2 * feature_size * lstm_n_gates * feature_size,
0.3f);
std::vector<float> user_enc_bidir_wei_iter(
enc_bidir_n_layers * 2 * feature_size * lstm_n_gates * feature_size,
0.2f);
std::vector<float> user_enc_bidir_bias(
enc_bidir_n_layers * 2 * lstm_n_gates * feature_size, 1.0f);
auto user_enc_bidir_src_layer_md =
memory::desc({enc_bidir_src_layer_tz},
auto user_enc_bidir_wei_layer_md
auto user_enc_bidir_wei_iter_md =
memory::desc({enc_bidir_weights_iter_tz},
auto user_enc_bidir_bias_md =
memory::desc({enc_bidir_bias_tz},
auto user_enc_bidir_src_layer_memory
=
memory(user_enc_bidir_src_layer_md, cpu_engine, net_src.data());
auto user_enc_bidir_wei_layer_memory =
memory(user_enc_bidir_wei_layer_md,
cpu_engine, user_enc_bidir_wei_layer.data());
auto user_enc_bidir_wei_iter_memory =
memory(user_enc_bidir_wei_iter_md,
cpu_engine, user_enc_bidir_wei_iter.data());
auto user_enc_bidir_bias_memory =
memory(
user_enc_bidir_bias_md, cpu_engine, user_enc_bidir_bias.data());
auto enc_bidir_src_layer_md =
memory::desc({enc_bidir_src_layer_tz},
auto enc_bidir_wei_layer_md =
memory::desc({enc_bidir_weights_layer_tz},
auto enc_bidir_wei_iter_md =
memory::desc({enc_bidir_weights_iter_tz},
auto enc_bidir_dst_layer_md =
memory::desc({enc_bidir_dst_layer_tz},
enc_bidir_wei_iter_md, user_enc_bidir_bias_md,
try {
enc_bidir_prim_desc
throw example_allows_unimplemented {
"No int8 LSTM implementation is available for this "
"platform.\n"
"Please refer to the developer guide for details."};
throw;
}
auto enc_bidir_src_layer_memory
user_enc_bidir_src_layer_memory, enc_bidir_src_layer_memory, attr);
encoder_net.push_back(
reorder(enc_bidir_src_layer_reorder_pd));
encoder_net_args.push_back(
auto enc_bidir_wei_layer_memory
user_enc_bidir_wei_layer_memory, enc_bidir_wei_layer_memory, attr);
reorder(enc_bidir_wei_layer_reorder_pd)
.
execute(s, user_enc_bidir_wei_layer_memory,
enc_bidir_wei_layer_memory);
auto enc_bidir_wei_iter_memory
user_enc_bidir_wei_iter_memory, enc_bidir_wei_iter_memory, attr);
reorder(enc_bidir_wei_iter_reorder_pd)
.
execute(s, user_enc_bidir_wei_iter_memory,
enc_bidir_wei_iter_memory);
auto enc_bidir_dst_layer_memory
encoder_net_args.push_back(
std::vector<float> user_enc_uni_first_wei_layer(
1 * 1 * 2 * feature_size * lstm_n_gates * feature_size, 0.3f);
std::vector<float> user_enc_uni_first_wei_iter(
1 * 1 * feature_size * lstm_n_gates * feature_size, 0.2f);
std::vector<float> user_enc_uni_first_bias(
1 * 1 * lstm_n_gates * feature_size, 1.0f);
= {1, 1, 2 * feature_size, lstm_n_gates, feature_size};
= {1, 1, feature_size, lstm_n_gates, feature_size};
= {1, 1, lstm_n_gates, feature_size};
= {src_seq_length_max, batch, feature_size};
auto user_enc_uni_first_wei_layer_md
auto user_enc_uni_first_wei_iter_md
auto user_enc_uni_first_bias_md
auto user_enc_uni_first_wei_layer_memory
=
memory(user_enc_uni_first_wei_layer_md, cpu_engine,
user_enc_uni_first_wei_layer.data());
auto user_enc_uni_first_wei_iter_memory
=
memory(user_enc_uni_first_wei_iter_md, cpu_engine,
user_enc_uni_first_wei_iter.data());
auto user_enc_uni_first_bias_memory =
memory(user_enc_uni_first_bias_md,
cpu_engine, user_enc_uni_first_bias.data());
auto enc_uni_first_wei_layer_md
auto enc_uni_first_wei_iter_md
auto enc_uni_first_dst_layer_md
enc_uni_first_wei_iter_md, user_enc_uni_first_bias_md,
enc_uni_first_layer_desc, attr, cpu_engine);
auto enc_uni_first_wei_layer_memory
=
memory(enc_uni_first_prim_desc.weights_layer_desc(), cpu_engine);
reorder(user_enc_uni_first_wei_layer_memory, enc_uni_first_wei_layer_memory)
.
execute(s, user_enc_uni_first_wei_layer_memory,
enc_uni_first_wei_layer_memory);
auto enc_uni_first_wei_iter_memory
=
memory(enc_uni_first_prim_desc.weights_iter_desc(), cpu_engine);
reorder(user_enc_uni_first_wei_iter_memory, enc_uni_first_wei_iter_memory)
.
execute(s, user_enc_uni_first_wei_iter_memory,
enc_uni_first_wei_iter_memory);
auto enc_uni_first_dst_layer_memory
=
memory(enc_uni_first_prim_desc.dst_layer_desc(), cpu_engine);
encoder_net.push_back(
lstm_forward(enc_uni_first_prim_desc));
encoder_net_args.push_back(
std::vector<float> user_enc_uni_wei_layer((enc_unidir_n_layers - 1) * 1
* feature_size * lstm_n_gates * feature_size,
0.3f);
std::vector<float> user_enc_uni_wei_iter((enc_unidir_n_layers - 1) * 1
* feature_size * lstm_n_gates * feature_size,
0.2f);
std::vector<float> user_enc_uni_bias(
(enc_unidir_n_layers - 1) * 1 * lstm_n_gates * feature_size, 1.0f);
memory::dims user_enc_uni_wei_layer_dims = {(enc_unidir_n_layers - 1), 1,
feature_size, lstm_n_gates, feature_size};
memory::dims user_enc_uni_wei_iter_dims = {(enc_unidir_n_layers - 1), 1,
feature_size, lstm_n_gates, feature_size};
= {(enc_unidir_n_layers - 1), 1, lstm_n_gates, feature_size};
memory::dims enc_dst_layer_dims = {src_seq_length_max, batch, feature_size};
auto user_enc_uni_wei_layer_md =
memory::desc({user_enc_uni_wei_layer_dims},
auto user_enc_uni_wei_iter_md =
memory::desc({user_enc_uni_wei_iter_dims},
auto user_enc_uni_bias_md =
memory::desc({user_enc_uni_bias_dims},
auto user_enc_uni_wei_layer_memory =
memory(user_enc_uni_wei_layer_md,
cpu_engine, user_enc_uni_wei_layer.data());
auto user_enc_uni_wei_iter_memory =
memory(
user_enc_uni_wei_iter_md, cpu_engine, user_enc_uni_wei_iter.data());
auto user_enc_uni_bias_memory =
memory(
user_enc_uni_bias_md, cpu_engine, user_enc_uni_bias.data());
auto enc_uni_wei_layer_md =
memory::desc({user_enc_uni_wei_layer_dims},
auto enc_uni_wei_iter_md =
memory::desc({user_enc_uni_wei_iter_dims},
enc_uni_wei_layer_md, enc_uni_wei_iter_md, user_enc_uni_bias_md,
enc_uni_layer_desc, attr, cpu_engine);
auto enc_uni_wei_layer_memory
=
memory(enc_uni_prim_desc.weights_layer_desc(), cpu_engine);
user_enc_uni_wei_layer_memory, enc_uni_wei_layer_memory, attr);
reorder(enc_uni_wei_layer_reorder_pd)
s, user_enc_uni_wei_layer_memory, enc_uni_wei_layer_memory);
auto enc_uni_wei_iter_memory
=
memory(enc_uni_prim_desc.weights_iter_desc(), cpu_engine);
user_enc_uni_wei_iter_memory, enc_uni_wei_iter_memory, attr);
reorder(enc_uni_wei_iter_reorder_pd)
.
execute(s, user_enc_uni_wei_iter_memory, enc_uni_wei_iter_memory);
auto enc_dst_layer_memory
=
memory(enc_uni_prim_desc.dst_layer_desc(), cpu_engine);
encoder_net_args.push_back(
std::vector<float> user_dec_wei_layer(
dec_n_layers * 1 * feature_size * lstm_n_gates * feature_size,
0.2f);
std::vector<float> user_dec_wei_iter(dec_n_layers * 1
* (feature_size + feature_size) * lstm_n_gates
* feature_size,
0.3f);
std::vector<float> user_dec_bias(
dec_n_layers * 1 * lstm_n_gates * feature_size, 1.0f);
std::vector<int8_t> user_weights_attention_src_layer(
feature_size * feature_size, 1);
float weights_attention_scale = 127.;
std::vector<float> user_weights_annotation(
feature_size * feature_size, 1.0f);
std::vector<float> user_weights_alignments(feature_size, 1.0f);
std::vector<uint8_t> dec_dst(tgt_seq_length_max * batch * feature_size, 0);
= {dec_n_layers, 1, feature_size, lstm_n_gates, feature_size};
feature_size + feature_size, lstm_n_gates, feature_size};
= {dec_n_layers, 1, lstm_n_gates, feature_size};
memory::dims dec_dst_iter_c_dims = {dec_n_layers, 1, batch, feature_size};
std::vector<float> dec_dst_iter(
dec_n_layers * batch * 2 * feature_size, 1.0f);
= {dec_n_layers, 1, batch, feature_size + feature_size};
= {dec_n_layers, 1, batch, feature_size};
auto user_dec_wei_layer_md =
memory::desc({user_dec_wei_layer_dims},
auto user_dec_wei_iter_md =
memory::desc({user_dec_wei_iter_dims},
auto dec_dst_iter_c_md =
memory::desc({dec_dst_iter_c_dims},
auto user_dec_wei_layer_memory =
memory(
user_dec_wei_layer_md, cpu_engine, user_dec_wei_layer.data());
auto user_dec_wei_iter_memory =
memory(
user_dec_wei_iter_md, cpu_engine, user_dec_wei_iter.data());
auto user_dec_bias_memory
=
memory(user_dec_bias_md, cpu_engine, user_dec_bias.data());
auto dec_src_layer_memory =
memory(dec_src_layer_md, cpu_engine);
auto dec_dst_layer_memory
=
memory(dec_dst_layer_md, cpu_engine, dec_dst.data());
auto dec_dst_iter_c_memory =
memory(dec_dst_iter_c_md, cpu_engine);
auto dec_wei_layer_md =
memory::desc({user_dec_wei_layer_dims},
auto dec_wei_iter_md =
memory::desc({user_dec_wei_iter_dims},
auto dec_dst_iter_memory
=
memory(dec_dst_iter_md, cpu_engine, dec_dst_iter.data());
dec_dst_iter_noctx_dims, {0, 0, 0, 0, 0});
dec_dst_iter_md, dec_dst_iter_c_md, dec_wei_layer_md,
dec_wei_iter_md, user_dec_bias_md, dec_dst_layer_md,
dec_dst_iter_noctx_md, dec_dst_iter_c_md);
auto dec_ctx_prim_desc
auto dec_wei_layer_memory
=
memory(dec_ctx_prim_desc.weights_layer_desc(), cpu_engine);
user_dec_wei_layer_memory, dec_wei_layer_memory, attr);
.
execute(s, user_dec_wei_layer_memory, dec_wei_layer_memory);
auto dec_wei_iter_memory
=
memory(dec_ctx_prim_desc.weights_iter_desc(), cpu_engine);
user_dec_wei_iter_memory, dec_wei_iter_memory, attr);
.
execute(s, user_dec_wei_iter_memory, dec_wei_iter_memory);
std::vector<float> weighted_annotations(
src_seq_length_max * batch * feature_size, 1.0f);
std::vector<int32_t> weights_attention_sum_rows(feature_size, 1);
assert(encoder_net.size() == encoder_net_args.size()
&& "something is missing");
for (size_t p = 0; p < encoder_net.size(); ++p)
encoder_net.at(p).execute(s, encoder_net_args.at(p));
compute_weighted_annotations(weighted_annotations.data(),
src_seq_length_max, batch, feature_size,
user_weights_annotation.data(),
(float *)enc_dst_layer_memory.get_data_handle());
compute_sum_of_rows(user_weights_attention_src_layer.data(),
feature_size, feature_size, weights_attention_sum_rows.data());
for (dim_t i = 0; i < tgt_seq_length_max; i++) {
uint8_t *src_att_layer_handle
float *src_att_iter_handle
= (float *)dec_dst_iter_memory.get_data_handle();
compute_attention(src_att_iter_handle, src_seq_length_max, batch,
feature_size, user_weights_attention_src_layer.data(),
weights_attention_scale, weights_attention_sum_rows.data(),
src_att_layer_handle, data_scale, data_shift,
(uint8_t *)enc_bidir_dst_layer_memory.get_data_handle(),
weighted_annotations.data(),
user_weights_alignments.data());
copy_context(
src_att_iter_handle, dec_n_layers, batch, feature_size);
assert(decoder_net.size() == decoder_net_args.size()
&& "something is missing");
for (size_t p = 0; p < decoder_net.size(); ++p)
decoder_net.at(p).execute(s, decoder_net_args.at(p));
auto dst_layer_handle
= (uint8_t *)dec_dst_layer_memory.get_data_handle();
dec_dst_layer_memory.set_data_handle(
dst_layer_handle + batch * feature_size);
}
};
std::cout << "Parameters:" << std::endl
<< " batch = " << batch << std::endl
<< " feature size = " << feature_size << std::endl
<< " maximum source sequence length = " << src_seq_length_max
<< std::endl
<< " maximum target sequence length = " << tgt_seq_length_max
<< std::endl
<< " number of layers of the bidirectional encoder = "
<< enc_bidir_n_layers << std::endl
<< " number of layers of the unidirectional encoder = "
<< enc_unidir_n_layers << std::endl
<< " number of layers of the decoder = " << dec_n_layers
<< std::endl;
s.wait();
}
int main(int argc, char **argv) {
}