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 lrn.cpp |
18 | /// > Annotated version: @ref lrn_example_cpp |
19 | /// |
20 | /// @page lrn_example_cpp_short |
21 | /// |
22 | /// This C++ API demonstrates how to create and execute a |
23 | /// [Local response normalization](@ref dev_guide_lrn) primitive in forward |
24 | /// training propagation mode. |
25 | /// |
26 | /// @page lrn_example_cpp Local Response Normalization Primitive Example |
27 | /// @copydetails lrn_example_cpp_short |
28 | /// |
29 | /// @include lrn.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 lrn_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 | |
62 | // Allocate buffers. |
63 | std::vector<float> src_data(product(src_dims)); |
64 | std::vector<float> dst_data(product(src_dims)); |
65 | |
66 | std::generate(src_data.begin(), src_data.end(), []() { |
67 | static int i = 0; |
68 | return std::cos(i++ / 10.f); |
69 | }); |
70 | |
71 | // Create src and dst memory descriptors and memory objects. |
72 | auto src_md = memory::desc(src_dims, dt::f32, tag::nchw); |
73 | auto dst_md = memory::desc(src_dims, dt::f32, tag::nchw); |
74 | auto src_mem = memory(src_md, engine); |
75 | auto dst_mem = memory(src_md, engine); |
76 | |
77 | // Write data to memory object's handle. |
78 | write_to_dnnl_memory(src_data.data(), src_mem); |
79 | |
80 | // Create operation descriptor. |
81 | const memory::dim local_size = 5; |
82 | const float alpha = 1.e-4f; |
83 | const float beta = 0.75f; |
84 | const float k = 1.f; |
85 | // Create primitive descriptor. |
86 | auto lrn_pd = lrn_forward::primitive_desc(engine, |
87 | prop_kind::forward_training, algorithm::lrn_across_channels, src_md, |
88 | dst_md, local_size, alpha, beta, k); |
89 | |
90 | // Create workspace memory object using memory descriptors created by the |
91 | // primitive descriptor. |
92 | // NOTE: Here, workspace may or may not be required in forward training |
93 | // mode, and is used to speed-up the backward propagation. |
94 | auto workspace_mem = memory(lrn_pd.workspace_desc(), engine); |
95 | |
96 | // Create the primitive. |
97 | auto lrn_prim = lrn_forward(lrn_pd); |
98 | |
99 | // Primitive arguments. |
100 | std::unordered_map<int, memory> lrn_args; |
101 | lrn_args.insert({DNNL_ARG_SRC, src_mem}); |
102 | lrn_args.insert({DNNL_ARG_WORKSPACE, workspace_mem}); |
103 | lrn_args.insert({DNNL_ARG_DST, dst_mem}); |
104 | |
105 | // Primitive execution. |
106 | lrn_prim.execute(engine_stream, lrn_args); |
107 | |
108 | // Wait for the computation to finalize. |
109 | engine_stream.wait(); |
110 | |
111 | // Read data from memory object's handle. |
112 | read_from_dnnl_memory(dst_data.data(), dst_mem); |
113 | } |
114 | |
115 | int main(int argc, char **argv) { |
116 | return handle_example_errors(lrn_example, parse_engine_kind(argc, argv)); |
117 | } |
118 | |