This C++ API example demonstrates how to create and execute a Softmax primitive in forward training propagation mode.
Key optimizations included in this example:
- In-place primitive execution;
- Softmax along axis 1 (C) for 2D tensors.
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include "example_utils.hpp"
IC = 1000;
std::vector<float> src_data(product(src_dims));
std::generate(src_data.begin(), src_data.end(), []() {
static int i = 0;
return std::cos(i++ / 10.f);
});
write_to_dnnl_memory(src_data.data(), src_mem);
const int axis = 1;
std::unordered_map<int, memory> softmax_args;
softmax_prim.execute(engine_stream, softmax_args);
engine_stream.wait();
read_from_dnnl_memory(src_data.data(), src_mem);
}
int main(int argc, char **argv) {
return handle_example_errors(
softmax_example, parse_engine_kind(argc, argv));
}