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 shuffle.cpp |
18 | /// > Annotated version: @ref shuffle_example_cpp |
19 | /// |
20 | /// @page shuffle_example_cpp_short |
21 | /// |
22 | /// This C++ API example demonstrates how to create and execute a |
23 | /// [Shuffle](@ref dev_guide_shuffle) primitive. |
24 | /// |
25 | /// Key optimizations included in this example: |
26 | /// - Shuffle along axis 1 (channels). |
27 | /// |
28 | /// @page shuffle_example_cpp Shuffle Primitive Example |
29 | /// @copydetails shuffle_example_cpp_short |
30 | /// |
31 | /// @include shuffle.cpp |
32 | |
33 | #include <algorithm> |
34 | #include <cmath> |
35 | #include <iostream> |
36 | #include <string> |
37 | #include <vector> |
38 | |
39 | #include "example_utils.hpp" |
40 | #include "oneapi/dnnl/dnnl.hpp" |
41 | |
42 | using namespace dnnl; |
43 | |
44 | using tag = memory::format_tag; |
45 | using dt = memory::data_type; |
46 | |
47 | void shuffle_example(dnnl::engine::kind engine_kind) { |
48 | |
49 | // Create execution dnnl::engine. |
50 | dnnl::engine engine(engine_kind, 0); |
51 | |
52 | // Create dnnl::stream. |
53 | dnnl::stream engine_stream(engine); |
54 | |
55 | // Tensor dimensions. |
56 | const memory::dim N = 3, // batch size |
57 | IC = 72, // channels |
58 | IH = 227, // tensor height |
59 | IW = 227; // tensor width |
60 | |
61 | // Source (src) and destination (dst) tensors dimensions. |
62 | memory::dims src_dims = {N, IC, IH, IW}; |
63 | |
64 | // Allocate buffers. |
65 | std::vector<float> src_data(product(src_dims)); |
66 | std::vector<float> dst_data(product(src_dims)); |
67 | |
68 | // Initialize src. |
69 | std::generate(src_data.begin(), src_data.end(), []() { |
70 | static int i = 0; |
71 | return std::cos(i++ / 10.f); |
72 | }); |
73 | |
74 | // Shuffle axis and group size. |
75 | const int shuffle_axis = 1; |
76 | const int group_size = 4; |
77 | |
78 | // Create memory descriptor and memory objects for src and dst. |
79 | auto src_md = memory::desc(src_dims, dt::f32, tag::nchw); |
80 | auto dst_md = memory::desc(src_dims, dt::f32, tag::nchw); |
81 | auto src_mem = memory(src_md, engine); |
82 | |
83 | auto dst_mem = memory({src_dims, dt::f32, tag::abcd}, engine); |
84 | |
85 | // Write data to memory object's handle. |
86 | write_to_dnnl_memory(src_data.data(), src_mem); |
87 | |
88 | // Create primitive descriptor. |
89 | auto shuffle_pd = shuffle_forward::primitive_desc(engine, |
90 | prop_kind::forward_training, src_md, dst_md, shuffle_axis, |
91 | group_size); |
92 | |
93 | // Create the primitive. |
94 | auto shuffle_prim = shuffle_forward(shuffle_pd); |
95 | |
96 | // Primitive arguments. |
97 | std::unordered_map<int, memory> shuffle_args; |
98 | shuffle_args.insert({DNNL_ARG_SRC, src_mem}); |
99 | shuffle_args.insert({DNNL_ARG_DST, dst_mem}); |
100 | |
101 | // Primitive execution: shuffle. |
102 | shuffle_prim.execute(engine_stream, shuffle_args); |
103 | |
104 | // Wait for the computation to finalize. |
105 | engine_stream.wait(); |
106 | |
107 | // Read data from memory object. |
108 | read_from_dnnl_memory(dst_data.data(), dst_mem); |
109 | } |
110 | |
111 | int main(int argc, char **argv) { |
112 | return handle_example_errors( |
113 | shuffle_example, parse_engine_kind(argc, argv)); |
114 | } |
115 | |