#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include "example_utils.hpp"
using tag = memory::format_tag;
using dt = memory::data_type;
const memory::dim MB = 3,
M = 128, K = 256, N = 512;
memory::dims src_dims = {MB, M, K};
memory::dims weights_dims = {MB, K, N};
memory::dims bias_dims = {1, 1, N};
memory::dims dst_dims = {MB, M, N};
std::vector<float> src_data(product(src_dims));
std::vector<float> weights_data(product(weights_dims));
std::vector<float> bias_data(product(bias_dims));
std::vector<float> dst_data(product(dst_dims));
std::generate(src_data.begin(), src_data.end(), []() {
static int i = 0;
return std::cos(i++ / 10.f);
});
std::generate(weights_data.begin(), weights_data.end(), []() {
static int i = 0;
return std::sin(i++ * 2.f);
});
std::generate(bias_data.begin(), bias_data.end(), []() {
static int i = 0;
return std::tanh(i++);
});
auto src_md = memory::desc(src_dims, dt::f32, tag::abc);
auto weights_md = memory::desc(weights_dims, dt::f32, tag::abc);
auto bias_md = memory::desc(bias_dims, dt::f32, tag::abc);
auto dst_md = memory::desc(dst_dims, dt::f32, tag::abc);
auto bias_mem = memory(bias_md,
engine);
write_to_dnnl_memory(src_data.data(), src_mem);
write_to_dnnl_memory(weights_data.data(), weights_mem);
write_to_dnnl_memory(bias_data.data(), bias_mem);
const float scale = 1.0f;
const float alpha = 0.f;
const float beta = 0.f;
post_ops matmul_ops;
matmul_ops.append_eltwise(scale, algorithm::eltwise_relu, alpha, beta);
primitive_attr matmul_attr;
matmul_attr.set_post_ops(matmul_ops);
auto matmul_pd = matmul::primitive_desc(
matmul_d, matmul_attr,
engine);
auto matmul_prim = matmul(matmul_pd);
std::unordered_map<int, memory> matmul_args;
matmul_prim.execute(engine_stream, matmul_args);
read_from_dnnl_memory(dst_data.data(), dst_mem);
}
int main(int argc, char **argv) {
return handle_example_errors(matmul_example, parse_engine_kind(argc, argv));
}
#define DNNL_ARG_DST
A special mnemonic for destination argument for primitives that have a single destination.
Definition: dnnl_types.h:2307
#define DNNL_ARG_SRC
A special mnemonic for source argument for primitives that have a single source.
Definition: dnnl_types.h:2283
#define DNNL_ARG_BIAS
Bias tensor argument.
Definition: dnnl_types.h:2357
#define DNNL_ARG_WEIGHTS
A special mnemonic for primitives that have a single weights argument.
Definition: dnnl_types.h:2330
@ weights_md
weights memory descriptor desc
@ matmul_d
matmul descriptor
@ dst_md
destination memory desc
@ src_md
source memory desc
oneDNN namespace
Definition: dnnl.hpp:74
An execution engine.
Definition: dnnl.hpp:869
kind
Kinds of engines.
Definition: dnnl.hpp:874
An execution stream.
Definition: dnnl.hpp:985
stream & wait()
Waits for all primitives executing in the stream to finish.
Definition: dnnl.hpp:1025