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 inner_product.cpp |
18 | /// > Annotated version: @ref inner_product_example_cpp |
19 | /// |
20 | /// @page inner_product_example_cpp_short |
21 | /// |
22 | /// This C++ API example demonstrates how to create and execute an |
23 | /// [Inner Product](@ref dev_guide_inner_product) primitive. |
24 | /// |
25 | /// Key optimizations included in this example: |
26 | /// - Primitive attributes with fused post-ops; |
27 | /// - Creation of optimized memory format from the primitive descriptor. |
28 | /// |
29 | /// @page inner_product_example_cpp Inner Product Primitive Example |
30 | /// @copydetails inner_product_example_cpp_short |
31 | /// |
32 | /// @include inner_product.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 | |
43 | using namespace dnnl; |
44 | |
45 | using tag = memory::format_tag; |
46 | using dt = memory::data_type; |
47 | |
48 | void inner_product_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, // input channels |
59 | IH = 227, // tensor height |
60 | IW = 227, // tensor width |
61 | OC = 96; // output channels |
62 | |
63 | // Source (src), weights, bias, and destination (dst) tensors |
64 | // dimensions. |
65 | memory::dims src_dims = {N, IC, IH, IW}; |
66 | memory::dims weights_dims = {OC, IC, IH, IW}; |
67 | memory::dims bias_dims = {OC}; |
68 | memory::dims dst_dims = {N, OC}; |
69 | |
70 | // Allocate buffers. |
71 | std::vector<float> src_data(product(src_dims)); |
72 | std::vector<float> weights_data(product(weights_dims)); |
73 | std::vector<float> bias_data(OC); |
74 | std::vector<float> dst_data(product(dst_dims)); |
75 | |
76 | // Initialize src, weights, and bias tensors. |
77 | std::generate(src_data.begin(), src_data.end(), []() { |
78 | static int i = 0; |
79 | return std::cos(i++ / 10.f); |
80 | }); |
81 | std::generate(weights_data.begin(), weights_data.end(), []() { |
82 | static int i = 0; |
83 | return std::sin(i++ * 2.f); |
84 | }); |
85 | std::generate(bias_data.begin(), bias_data.end(), []() { |
86 | static int i = 0; |
87 | return std::tanh(float(i++)); |
88 | }); |
89 | |
90 | // Create memory descriptors and memory objects for src and dst. In this |
91 | // example, NCHW layout is assumed. |
92 | auto src_md = memory::desc(src_dims, dt::f32, tag::nchw); |
93 | auto bias_md = memory::desc(bias_dims, dt::f32, tag::a); |
94 | auto dst_md = memory::desc(dst_dims, dt::f32, tag::nc); |
95 | |
96 | auto src_mem = memory(src_md, engine); |
97 | auto bias_mem = memory(bias_md, engine); |
98 | auto dst_mem = memory(dst_md, engine); |
99 | |
100 | // Create memory object for user's layout for weights. In this example, OIHW |
101 | // is assumed. |
102 | auto user_weights_mem = memory({weights_dims, dt::f32, tag::oihw}, engine); |
103 | |
104 | // Write data to memory object's handles. |
105 | write_to_dnnl_memory(src_data.data(), src_mem); |
106 | write_to_dnnl_memory(bias_data.data(), bias_mem); |
107 | write_to_dnnl_memory(weights_data.data(), user_weights_mem); |
108 | |
109 | // Create memory descriptor for weights with format_tag::any. This enables |
110 | // the inner product primitive to choose the memory layout for an optimized |
111 | // primitive implementation, and this format may differ from the one |
112 | // provided by the user. |
113 | auto inner_product_weights_md |
114 | = memory::desc(weights_dims, dt::f32, tag::any); |
115 | |
116 | // Create primitive post-ops (ReLU). |
117 | const float alpha = 0.f; |
118 | const float beta = 0.f; |
119 | post_ops inner_product_ops; |
120 | inner_product_ops.append_eltwise(algorithm::eltwise_relu, alpha, beta); |
121 | primitive_attr inner_product_attr; |
122 | inner_product_attr.set_post_ops(inner_product_ops); |
123 | |
124 | // Create inner product primitive descriptor. |
125 | auto inner_product_pd = inner_product_forward::primitive_desc(engine, |
126 | prop_kind::forward_training, src_md, inner_product_weights_md, |
127 | bias_md, dst_md, inner_product_attr); |
128 | |
129 | // For now, assume that the weights memory layout generated by the primitive |
130 | // and the one provided by the user are identical. |
131 | auto inner_product_weights_mem = user_weights_mem; |
132 | |
133 | // Reorder the data in case the weights memory layout generated by the |
134 | // primitive and the one provided by the user are different. In this case, |
135 | // we create additional memory objects with internal buffers that will |
136 | // contain the reordered data. |
137 | if (inner_product_pd.weights_desc() != user_weights_mem.get_desc()) { |
138 | inner_product_weights_mem |
139 | = memory(inner_product_pd.weights_desc(), engine); |
140 | reorder(user_weights_mem, inner_product_weights_mem) |
141 | .execute(engine_stream, user_weights_mem, |
142 | inner_product_weights_mem); |
143 | } |
144 | |
145 | // Create the primitive. |
146 | auto inner_product_prim = inner_product_forward(inner_product_pd); |
147 | |
148 | // Primitive arguments. |
149 | std::unordered_map<int, memory> inner_product_args; |
150 | inner_product_args.insert({DNNL_ARG_SRC, src_mem}); |
151 | inner_product_args.insert({DNNL_ARG_WEIGHTS, inner_product_weights_mem}); |
152 | inner_product_args.insert({DNNL_ARG_BIAS, bias_mem}); |
153 | inner_product_args.insert({DNNL_ARG_DST, dst_mem}); |
154 | |
155 | // Primitive execution: inner-product with ReLU. |
156 | inner_product_prim.execute(engine_stream, inner_product_args); |
157 | |
158 | // Wait for the computation to finalize. |
159 | engine_stream.wait(); |
160 | |
161 | // Read data from memory object's handle. |
162 | read_from_dnnl_memory(dst_data.data(), dst_mem); |
163 | } |
164 | |
165 | int main(int argc, char **argv) { |
166 | return handle_example_errors( |
167 | inner_product_example, parse_engine_kind(argc, argv)); |
168 | } |
169 | |