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 binary.cpp
18/// > Annotated version: @ref binary_example_cpp
19///
20/// @page binary_example_cpp_short
21///
22/// This C++ API example demonstrates how to create and execute a
23/// [Binary](@ref dev_guide_binary) primitive.
24///
25/// Key optimizations included in this example:
26/// - In-place primitive execution;
27/// - Primitive attributes with fused post-ops.
28///
29/// @page binary_example_cpp Binary Primitive Example
30/// @copydetails binary_example_cpp_short
31///
32/// @include binary.cpp
33
34#include <algorithm>
35#include <cmath>
36#include <iostream>
37#include <string>
38#include <vector>
39
40#include "example_utils.hpp"
41#include "oneapi/dnnl/dnnl.hpp"
42
43using namespace dnnl;
44
45using tag = memory::format_tag;
46using dt = memory::data_type;
47
48void binary_example(dnnl::engine::kind engine_kind) {
49
50 // Create execution dnnl::engine.
51 dnnl::engine engine(engine_kind, 0);
52
53 // Create dnnl::stream.
54 dnnl::stream engine_stream(engine);
55
56 // Tensor dimensions.
57 const memory::dim N = 3, // batch size
58 IC = 3, // channels
59 IH = 150, // tensor height
60 IW = 150; // tensor width
61
62 // Source (src_0 and src_1) and destination (dst) tensors dimensions.
63 memory::dims src_0_dims = {N, IC, IH, IW};
64 memory::dims src_1_dims = {N, IC, IH, 1};
65
66 // Allocate buffers.
67 std::vector<float> src_0_data(product(src_0_dims));
68 std::vector<float> src_1_data(product(src_1_dims));
69
70 // Initialize src_0 and src_1 (src).
71 std::generate(src_0_data.begin(), src_0_data.end(), []() {
72 static int i = 0;
73 return std::cos(i++ / 10.f);
74 });
75 std::generate(src_1_data.begin(), src_1_data.end(), []() {
76 static int i = 0;
77 return std::sin(i++ * 2.f);
78 });
79
80 // Create src and dst memory descriptors.
81 auto src_0_md = memory::desc(src_0_dims, dt::f32, tag::nchw);
82 auto src_1_md = memory::desc(src_1_dims, dt::f32, tag::nchw);
83 auto dst_md = memory::desc(src_0_dims, dt::f32, tag::nchw);
84
85 // Create src memory objects.
86 auto src_0_mem = memory(src_0_md, engine);
87 auto src_1_mem = memory(src_1_md, engine);
88
89 // Write data to memory object's handle.
90 write_to_dnnl_memory(src_0_data.data(), src_0_mem);
91 write_to_dnnl_memory(src_1_data.data(), src_1_mem);
92
93 // Create primitive post-ops (ReLU).
94 const float alpha = 0.f;
95 const float beta = 0.f;
96 post_ops binary_ops;
97 binary_ops.append_eltwise(algorithm::eltwise_relu, alpha, beta);
98 primitive_attr binary_attr;
99 binary_attr.set_post_ops(binary_ops);
100
101 // Create primitive descriptor.
102 auto binary_pd = binary::primitive_desc(engine, algorithm::binary_mul,
103 src_0_md, src_1_md, dst_md, binary_attr);
104
105 // Create the primitive.
106 auto binary_prim = binary(binary_pd);
107
108 // Primitive arguments. Set up in-place execution by assigning src_0 as DST.
109 std::unordered_map<int, memory> binary_args;
110 binary_args.insert({DNNL_ARG_SRC_0, src_0_mem});
111 binary_args.insert({DNNL_ARG_SRC_1, src_1_mem});
112 binary_args.insert({DNNL_ARG_DST, src_0_mem});
113
114 // Primitive execution: binary with ReLU.
115 binary_prim.execute(engine_stream, binary_args);
116
117 // Wait for the computation to finalize.
118 engine_stream.wait();
119
120 // Read data from memory object's handle.
121 read_from_dnnl_memory(src_0_data.data(), src_0_mem);
122}
123
124int main(int argc, char **argv) {
125 return handle_example_errors(binary_example, parse_engine_kind(argc, argv));
126}
127