Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / condition.cpp
1 // Copyright (c) 2018 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 #include "condition_inst.h"
17
18 #include "error_handler.h"
19 #include "json_object.h"
20 #include "primitive_type_base.h"
21
22
23 namespace cldnn
24 {
25 primitive_type_id condition_type_id()
26 {
27     static primitive_type_base<condition> instance;
28     return &instance;
29 }
30 /*
31     Calc_output_layout method is called only when output layout is invalidated.
32     It means, that it is called when:
33     1) It has never been called.
34     2) Dependency has changed output layout.
35     In this both cases, we need to recalc branch_true and branch_false.
36     !* We can be sure, that this method was called AT LEAST once during graph compilation.*!
37 */
38 layout condition_inst::calc_output_layout(condition_node const& node)
39 {
40     assert((bool)node.get_primitive()->output_data_type == false
41            && "Output data type forcing is not supported for condition_node!");
42     node.set_branches();
43
44     auto branch_true_output = node.get_branch_true()->get_outputs();
45     auto branch_false_output = node.get_branch_false()->get_outputs();
46     CLDNN_ERROR_NOT_EQUAL(node.id(), "Count of branch true outputs", branch_true_output.size(), "expected outputs size", 1, "Branch true should have one output.");
47     CLDNN_ERROR_NOT_EQUAL(node.id(), "Count of branch false outputs", branch_false_output.size(), "expected outputs size", 1, "Branch false should have one output.");
48     
49     auto layout_true = branch_true_output.at(0)->get_output_layout();
50     auto layout_false = branch_false_output.at(0)->get_output_layout();
51     CLDNN_ERROR_LAYOUT_MISMATCH(node.id(), "Branch true output layout", layout_true, "branch false output layout", layout_false, "Layout of the branches should be the same.");
52
53     return layout_true;
54 }
55
56 std::string condition_inst::to_string(condition_node const& node)
57 {
58     auto desc = node.get_primitive();
59     auto node_info = node.desc_to_json();
60     json_composite condition_info;
61
62     node_info->add("condition info", condition_info);
63
64     std::stringstream primitive_description;
65     node_info->dump(primitive_description);
66     return primitive_description.str();
67 }
68
69 /*
70 Condition primitive is resuing memory with the input.
71 */
72 condition_inst::typed_primitive_inst(network_impl& network, condition_node const& node)
73     : parent(network, node)
74     , _net_true(node.get_program().get_engine().allocate_network(*node.get_branch_true(), true))
75     , _net_false(node.get_program().get_engine().allocate_network(*node.get_branch_false(), true))
76 {
77     auto compare_tensor = node.compare().get_output_layout().size;
78     auto input_tensor = node.input().get_output_layout().size;
79     CLDNN_ERROR_TENSOR_SIZES_GREATER_THAN(node.id(), "Compare tensor", compare_tensor, "input tensor", input_tensor, "Compare primitive is too big.");
80
81     auto compare_with_offster_tensor = compare_tensor + node.offset();
82     CLDNN_ERROR_TENSOR_SIZES_GREATER_THAN(node.id(), "Offset with compare tensor", compare_with_offster_tensor, "input tensor", input_tensor, "Offset is too big.");
83
84 }
85 }