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