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 concat.cpp |
18 | /// > Annotated version: @ref concat_example_cpp |
19 | /// |
20 | /// @page concat_example_cpp_short |
21 | /// |
22 | /// This C++ API example demonstrates how to create and execute a |
23 | /// [Concat](@ref dev_guide_concat) primitive. |
24 | /// |
25 | /// Key optimizations included in this example: |
26 | /// - Identical source (src) memory formats. |
27 | /// - Creation of optimized memory format for destination (dst) from the |
28 | /// primitive descriptor |
29 | /// |
30 | /// @page concat_example_cpp Concat Primitive Example |
31 | /// @copydetails concat_example_cpp_short |
32 | /// |
33 | /// @include concat.cpp |
34 | |
35 | #include <algorithm> |
36 | #include <cmath> |
37 | #include <iostream> |
38 | #include <string> |
39 | #include <vector> |
40 | |
41 | #include "example_utils.hpp" |
42 | #include "oneapi/dnnl/dnnl.hpp" |
43 | |
44 | using namespace dnnl; |
45 | |
46 | using tag = memory::format_tag; |
47 | using dt = memory::data_type; |
48 | |
49 | void concat_example(dnnl::engine::kind engine_kind) { |
50 | |
51 | // Create execution dnnl::engine. |
52 | dnnl::engine engine(engine_kind, 0); |
53 | |
54 | // Create dnnl::stream. |
55 | dnnl::stream engine_stream(engine); |
56 | |
57 | // Tensor dimensions. |
58 | const memory::dim N = 3, // batch size |
59 | IC = 3, // channels |
60 | IH = 120, // tensor height |
61 | IW = 120; // tensor width |
62 | |
63 | // Number of source (src) tensors. |
64 | const int num_src = 10; |
65 | |
66 | // Concatenation axis. |
67 | const int axis = 1; |
68 | |
69 | // src tensors dimensions |
70 | memory::dims src_dims = {N, IC, IH, IW}; |
71 | |
72 | // Allocate buffers. |
73 | std::vector<float> src_data(product(src_dims)); |
74 | |
75 | // Initialize src. |
76 | // NOTE: In this example, the same src memory buffer is used to demonstrate |
77 | // concatenation for simplicity |
78 | std::generate(src_data.begin(), src_data.end(), []() { |
79 | static int i = 0; |
80 | return std::cos(i++ / 10.f); |
81 | }); |
82 | |
83 | // Create a memory descriptor and memory object for each src tensor. |
84 | std::vector<memory::desc> src_mds; |
85 | std::vector<memory> src_mems; |
86 | |
87 | for (int n = 0; n < num_src; ++n) { |
88 | auto md = memory::desc(src_dims, dt::f32, tag::nchw); |
89 | auto mem = memory(md, engine); |
90 | |
91 | // Write data to memory object's handle. |
92 | write_to_dnnl_memory(src_data.data(), mem); |
93 | |
94 | src_mds.push_back(md); |
95 | src_mems.push_back(mem); |
96 | } |
97 | |
98 | // Create primitive descriptor. |
99 | auto concat_pd = concat::primitive_desc(engine, axis, src_mds); |
100 | |
101 | // Create destination (dst) memory object using the memory descriptor |
102 | // created by the primitive. |
103 | auto dst_mem = memory(concat_pd.dst_desc(), engine); |
104 | |
105 | // Create the primitive. |
106 | auto concat_prim = concat(concat_pd); |
107 | |
108 | // Primitive arguments. |
109 | std::unordered_map<int, memory> concat_args; |
110 | for (int n = 0; n < num_src; ++n) |
111 | concat_args.insert({DNNL_ARG_MULTIPLE_SRC + n, src_mems[n]}); |
112 | concat_args.insert({DNNL_ARG_DST, dst_mem}); |
113 | |
114 | // Primitive execution: concatenation. |
115 | concat_prim.execute(engine_stream, concat_args); |
116 | |
117 | // Wait for the computation to finalize. |
118 | engine_stream.wait(); |
119 | } |
120 | |
121 | int main(int argc, char **argv) { |
122 | return handle_example_errors(concat_example, parse_engine_kind(argc, argv)); |
123 | } |
124 | |