Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / ie_data.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include "ie_layers.h"
6 #include "ie_data.h"
7 #include "blob_factory.hpp"
8 #include <memory>
9 #include <string>
10 #include <map>
11
12 using namespace InferenceEngine;
13
14 Blob::Ptr Blob::CreateFromData(const DataPtr &data) {
15     return CreateBlobFromData(data);
16 }
17
18 Data::Data(const std::string &name, Precision _precision, Layout layout): precision(_precision), layout(layout),
19                                                                           name(name), userObject({0}),
20                                                                           tensorDesc(_precision, layout) {}
21
22 Data::Data(const std::string &name, const SizeVector &a_dims, Precision _precision, Layout layout)
23         : precision(_precision), layout(layout), dims(a_dims), name(name), userObject({0}),
24           tensorDesc(_precision, a_dims, layout) {
25     SizeVector tensorDims = a_dims;
26     std::reverse(tensorDims.begin(), tensorDims.end());
27     tensorDesc = TensorDesc(_precision, tensorDims, layout);
28 }
29
30 Data::Data(const std::string &name, const TensorDesc &desc): tensorDesc(desc), precision(desc.getPrecision()),
31                                                              layout(desc.getLayout()), dims(desc.getDims()),
32                                                              name(name), userObject({0}) {
33     std::reverse(dims.begin(), dims.end());
34 }
35
36 const SizeVector& Data::getDims() const {
37     return tensorDesc.getDims();
38 }
39
40 const Precision& Data::getPrecision() const {
41     if (precision)
42         return precision;
43
44     return tensorDesc.getPrecision();
45 }
46
47 const TensorDesc& Data::getTensorDesc() const {
48     if ((tensorDesc.getDims().size() == 0 && tensorDesc.getDims() != dims) ||
49             (tensorDesc.getLayout() == Layout::ANY && layout != Layout::ANY) ||
50             (!tensorDesc.getPrecision() && precision)) {
51         THROW_IE_EXCEPTION << "Tensor descriptor is empty!";
52     }
53     if (precision && tensorDesc.getPrecision() != precision) {
54         tensorDesc.setPrecision(precision);
55     }
56     return tensorDesc;
57 }
58
59 bool Data::isInitialized() const {
60     return !dims.empty() || !tensorDesc.getDims().empty() || layout == SCALAR;
61 }
62
63 void Data::setDims(const SizeVector &a_dims) {
64     dims = a_dims;
65     std::reverse(dims.begin(), dims.end());
66     tensorDesc.setDims(a_dims);
67 }
68
69 void Data::setBatchSize(size_t batch_size) {
70     if (dims.empty()) {
71         dims = tensorDesc.getDims();
72         std::reverse(dims.begin(), dims.end());
73     }
74     if (dims.empty())
75         return;
76     dims.at(dims.size() - 1) = batch_size;
77     SizeVector normalDims = dims;
78     std::reverse(normalDims.begin(), normalDims.end());
79     tensorDesc.setDims(normalDims);
80 }
81
82 void Data::setLayout(Layout layout) {
83     tensorDesc.setLayout(layout);
84     this->layout = layout;
85 }
86
87 void Data::reshape(const SizeVector &a_dims, Layout a_layout) {
88     dims = a_dims;
89     layout = a_layout;
90     std::reverse(dims.begin(), dims.end());
91
92     tensorDesc.reshape(a_dims, layout);
93 }
94
95 CNNLayerWeakPtr &Data::getCreatorLayer() {
96     return creatorLayer;
97 }
98
99 const std::string &Data::getName() const {
100     return name;
101 }
102
103 std::map<std::string, CNNLayerPtr> &Data::getInputTo() {
104     return inputTo;
105 }
106
107 const UserValue& Data::getUserObject() const {
108     return userObject;
109 }
110
111 Layout Data::getLayout() const {
112     if (tensorDesc.getLayout() == Layout::ANY && layout != Layout::ANY)
113         return layout;
114     return tensorDesc.getLayout();
115 }
116
117 void Data::setPrecision(const Precision & precision) {
118     this->precision = precision;
119     tensorDesc.setPrecision(precision);
120 }