Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / permute.cpp
1 /*
2 // Copyright (c) 2016 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 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include "permute_inst.h"
19 #include "primitive_type_base.h"
20 #include "error_handler.h"
21 #include "json_object.h"
22
23 #include <algorithm>
24
25 namespace cldnn
26 {
27
28 primitive_type_id permute_type_id()
29 {
30     static primitive_type_base<permute> instance;
31     return &instance;
32 }
33
34
35 layout permute_inst::calc_output_layout(permute_node const& node)
36 {
37     assert((bool)node.get_primitive()->output_data_type == false
38            && "Output data type forcing is not supported for permute_node!");
39     auto input_layout = node.input().get_output_layout();
40     auto permute_order = node.get_primitive()->permute_order;
41     std::vector<tensor::value_type> output_sizes;
42
43     for (size_t x = 0; x < permute_order.size(); x++)
44     {
45         output_sizes.push_back(input_layout.size.raw[permute_order[x]]);
46     }
47
48     auto input_size = tensor(output_sizes);
49     auto op = node.get_primitive()->output_padding;
50
51     return layout(input_layout.data_type, input_layout.format, input_size, op);
52 }
53
54 std::string permute_inst::to_string(permute_node const& node)
55 {
56     auto desc          = node.get_primitive();
57     auto node_info     = node.desc_to_json();
58     auto permute_order = desc->permute_order;
59     auto& input        = node.input();
60     
61     std::stringstream primitive_description;
62     std::stringstream ss_permute_order;
63
64     for (size_t i = 0; i < permute_order.size(); ++i)
65     {
66         ss_permute_order << permute_order.at(i);
67         i != (permute_order.size() - 1) ? ss_permute_order << ", " : ss_permute_order << "";
68     }
69
70     json_composite permute_info;
71     permute_info.add("input id", input.id());
72     permute_info.add("permute order", ss_permute_order.str());
73     
74     node_info->add("permute info", permute_info);
75     node_info->dump(primitive_description);
76
77     return primitive_description.str();
78 }
79
80 permute_inst::typed_primitive_inst(network_impl& network, permute_node const& node)
81     : parent(network, node)
82 {
83     auto permute_order = argument.permute_order;
84
85     CLDNN_ERROR_NOT_EQUAL(node.id(), "Permute order size", permute_order.size(), "expected order size", 4, "Permute order size needs to be 4.");
86
87     std::vector<uint16_t> required_order_values = { 0, 1, 2, 3 };
88     auto required_order_values_size = required_order_values.size();
89
90     for (decltype(required_order_values_size) i = 0; i < required_order_values_size; i++)
91     {
92         if (!(std::find(permute_order.begin(), permute_order.end(), required_order_values[i]) != permute_order.end()))
93             CLDNN_ERROR_MESSAGE(node.id(), "Permute order does not contain all of required values.");
94     }
95 }
96 }