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 reorder.cpp
18/// > Annotated version: @ref reorder_example_cpp
19///
20/// @page reorder_example_cpp_short
21///
22/// This C++ API demonstrates how to create and execute a
23/// [Reorder](@ref dev_guide_reorder) primitive.
24///
25/// Key optimizations included in this example:
26/// - Primitive attributes for output scaling.
27///
28/// @page reorder_example_cpp Reorder Primitive Example
29/// @copydetails reorder_example_cpp_short
30///
31/// @include reorder.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
42using namespace dnnl;
43
44using tag = memory::format_tag;
45using dt = memory::data_type;
46
47void reorder_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 = 3, // 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<int8_t> dst_data(product(src_dims));
67
68 // Initialize src tensor.
69 std::generate(src_data.begin(), src_data.end(), []() {
70 static int i = 0;
71 return std::cos(i++ / 10.f);
72 });
73
74 // Create memory descriptors and memory objects for src and dst.
75 auto src_md = memory::desc(src_dims, dt::f32, tag::nchw);
76 auto dst_md = memory::desc(src_dims, dt::s8, tag::nhwc);
77
78 auto src_mem = memory(src_md, engine);
79 auto dst_mem = memory(dst_md, engine);
80
81 // Write data to memory object's handle.
82 write_to_dnnl_memory(src_data.data(), src_mem);
83
84 // Per-channel scales.
85 std::vector<float> scales(IC);
86 std::generate(scales.begin(), scales.end(), []() {
87 static int i = 0;
88 return 64.f + 5.f * i++;
89 });
90
91 // Dimension of the dst tensor where the output scales will be applied
92 const int ic_dim = 1;
93
94 // Create primitive post-ops (per-channel output scales)
95 primitive_attr reorder_attr;
96 reorder_attr.set_scales_mask(DNNL_ARG_DST, 1 << ic_dim);
97 auto dst_scales_mem = memory({{IC}, dt::f32, tag::x}, engine);
98 write_to_dnnl_memory(scales.data(), dst_scales_mem);
99
100 // Create primitive descriptor.
101 auto reorder_pd = reorder::primitive_desc(
102 engine, src_md, engine, dst_md, reorder_attr);
103
104 // Create the primitive.
105 auto reorder_prim = reorder(reorder_pd);
106
107 // Primitive arguments.
108 std::unordered_map<int, memory> reorder_args;
109 reorder_args.insert({DNNL_ARG_SRC, src_mem});
110 reorder_args.insert({DNNL_ARG_DST, dst_mem});
111 reorder_args.insert({DNNL_ARG_ATTR_SCALES | DNNL_ARG_DST, dst_scales_mem});
112
113 // Primitive execution: reorder with scaled sum.
114 reorder_prim.execute(engine_stream, reorder_args);
115
116 // Wait for the computation to finalize.
117 engine_stream.wait();
118
119 // Read data from memory object's handle.
120 read_from_dnnl_memory(dst_data.data(), dst_mem);
121}
122
123int main(int argc, char **argv) {
124 return handle_example_errors(
125 reorder_example, parse_engine_kind(argc, argv));
126}
127