#include <cmath>
#include <numeric>
#include <stdexcept>
#include <vector>
#include "example_utils.hpp"
const int N = 1, H = 13, W = 13, C = 3;
const int stride_N = H * W * C;
const int stride_H = W * C;
const int stride_W = C;
const int stride_C = 1;
auto offset = [=](int n, int h, int w, int c) {
return n * stride_N + h * stride_H + w * stride_W + c * stride_C;
};
const int image_size = N * H * W * C;
std::vector<float> image(image_size);
for (int n = 0; n < N; ++n)
for (int h = 0; h < H; ++h)
for (int w = 0; w < W; ++w)
for (int c = 0; c < C; ++c) {
int off = offset(
n, h, w, c);
image[off] = -std::cos(off / 10.f);
}
{N, C, H, W},
);
{N, C, H, W},
{stride_N, stride_C, stride_H, stride_W}
);
throw std::logic_error("Memory descriptor initialization mismatch.");
write_to_dnnl_memory(image.data(), src_mem);
0.f,
0.f
);
auto relu_pd
eng
);
relu.execute(engine_stream,
{
});
std::vector<float> relu_image(image_size);
read_from_dnnl_memory(relu_image.data(), dst_mem);
for (int n = 0; n < N; ++n)
for (int h = 0; h < H; ++h)
for (int w = 0; w < W; ++w)
for (int c = 0; c < C; ++c) {
int off = offset(
n, h, w, c);
float expected = image[off] < 0
? 0.f
: image[off];
if (relu_image[off] != expected) {
std::cout << "At index(" << n << ", " << c << ", " << h
<< ", " << w << ") expect " << expected
<< " but got " << relu_image[off]
<< std::endl;
throw std::logic_error("Accuracy check failed.");
}
}
}
int main(int argc, char **argv) {
int exit_code = 0;
try {
getting_started_tutorial(engine_kind);
std::cout << "oneDNN error caught: " << std::endl
<<
"\tStatus: " << dnnl_status2str(e.
status) << std::endl
<<
"\tMessage: " << e.
what() << std::endl;
exit_code = 1;
} catch (std::string &e) {
std::cout << "Error in the example: " << e << "." << std::endl;
exit_code = 2;
}
std::cout << "Example " << (exit_code ? "failed" : "passed") << " on "
<< engine_kind2str_upper(engine_kind) << "." << std::endl;
return exit_code;
}
@ eltwise_relu
Elementwise: rectified linear unit (ReLU)
Definition dnnl.hpp:490
@ forward_inference
Forward data propagation (inference mode).
Definition dnnl.hpp:449
#define DNNL_ARG_DST
A special mnemonic for destination argument for primitives that have a single destination.
Definition dnnl_types.h:1806
#define DNNL_ARG_SRC
A special mnemonic for source argument for primitives that have a single source.
Definition dnnl_types.h:1782
@ src_md
source memory desc
Definition dnnl.hpp:793
oneDNN namespace
Definition dnnl.hpp:81
Descriptor for an elementwise forward propagation primitive.
Definition dnnl.hpp:5488
Primitive descriptor for an elementwise forward propagation primitive.
Definition dnnl.hpp:5522
Elementwise unary operation forward propagation primitive.
Definition dnnl.hpp:5486
An execution engine.
Definition dnnl.hpp:844
kind
Kinds of engines.
Definition dnnl.hpp:849
oneDNN exception class.
Definition dnnl.hpp:91
const char * what() const noexcept override
Returns the explanatory string.
Definition dnnl.hpp:103
A memory descriptor.
Definition dnnl.hpp:1729
Memory object.
Definition dnnl.hpp:1188
@ nhwc
4D CNN activations tensor; an alias for dnnl::memory::format_tag::acdb
Definition dnnl.hpp:1361
@ f32
32-bit/single-precision floating point.
Definition dnnl.hpp:1216
An execution stream.
Definition dnnl.hpp:1047
stream & wait()
Waits for all primitives executing in the stream to finish.
Definition dnnl.hpp:1107