1 | /******************************************************************************* |
2 | * Copyright 2020-2022 Intel Corporation |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | *******************************************************************************/ |
16 | |
17 | /// @example eltwise.cpp |
18 | /// > Annotated version: @ref eltwise_example_cpp |
19 | /// |
20 | /// @page eltwise_example_cpp_short |
21 | /// |
22 | /// This C++ API example demonstrates how to create and execute an |
23 | /// [Element-wise](@ref dev_guide_eltwise) primitive in forward training |
24 | /// propagation mode. |
25 | /// |
26 | /// @page eltwise_example_cpp Element-Wise Primitive Example |
27 | /// @copydetails eltwise_example_cpp_short |
28 | /// |
29 | /// @include eltwise.cpp |
30 | |
31 | #include <algorithm> |
32 | #include <cmath> |
33 | #include <iostream> |
34 | #include <string> |
35 | #include <vector> |
36 | |
37 | #include "example_utils.hpp" |
38 | #include "oneapi/dnnl/dnnl.hpp" |
39 | |
40 | using namespace dnnl; |
41 | |
42 | using tag = memory::format_tag; |
43 | using dt = memory::data_type; |
44 | |
45 | void eltwise_example(dnnl::engine::kind engine_kind) { |
46 | |
47 | // Create execution dnnl::engine. |
48 | dnnl::engine engine(engine_kind, 0); |
49 | |
50 | // Create dnnl::stream. |
51 | dnnl::stream engine_stream(engine); |
52 | |
53 | // Tensor dimensions. |
54 | const memory::dim N = 3, // batch size |
55 | IC = 3, // channels |
56 | IH = 227, // tensor height |
57 | IW = 227; // tensor width |
58 | |
59 | // Source (src) and destination (dst) tensors dimensions. |
60 | memory::dims src_dims = {N, IC, IH, IW}; |
61 | memory::dims dst_dims = {N, IC, IH, IW}; |
62 | |
63 | // Allocate buffers. In this example, out-of-place primitive execution is |
64 | // demonstrated since both src and dst are required for later backward |
65 | // propagation. |
66 | std::vector<float> src_data(product(src_dims)); |
67 | std::vector<float> dst_data(product(dst_dims)); |
68 | |
69 | // Initialize src tensor. |
70 | std::generate(src_data.begin(), src_data.end(), []() { |
71 | static int i = 0; |
72 | return std::cos(i++ / 10.f); |
73 | }); |
74 | |
75 | // Create src and dst memory descriptors and memory objects. |
76 | auto src_md = memory::desc(src_dims, dt::f32, tag::nchw); |
77 | auto dst_md = memory::desc(dst_dims, dt::f32, tag::nchw); |
78 | |
79 | auto src_mem = memory(src_md, engine); |
80 | auto dst_mem = memory(dst_md, engine); |
81 | |
82 | // Write data to memory object's handle. |
83 | write_to_dnnl_memory(src_data.data(), src_mem); |
84 | |
85 | // Create primitive descriptor. |
86 | auto eltwise_pd = eltwise_forward::primitive_desc(engine, |
87 | prop_kind::forward_training, algorithm::eltwise_relu, src_md, |
88 | dst_md, 0.f, 0.f); |
89 | |
90 | // Create the primitive. |
91 | auto eltwise_prim = eltwise_forward(eltwise_pd); |
92 | |
93 | // Primitive arguments. |
94 | std::unordered_map<int, memory> eltwise_args; |
95 | eltwise_args.insert({DNNL_ARG_SRC, src_mem}); |
96 | eltwise_args.insert({DNNL_ARG_DST, dst_mem}); |
97 | |
98 | // Primitive execution: element-wise (ReLU). |
99 | eltwise_prim.execute(engine_stream, eltwise_args); |
100 | |
101 | // Wait for the computation to finalize. |
102 | engine_stream.wait(); |
103 | |
104 | // Read data from memory object's handle. |
105 | read_from_dnnl_memory(dst_data.data(), dst_mem); |
106 | } |
107 | |
108 | int main(int argc, char **argv) { |
109 | return handle_example_errors( |
110 | eltwise_example, parse_engine_kind(argc, argv)); |
111 | } |
112 | |