Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / Tensor.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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 #ifndef __ONERT_BACKEND_CPU_TENSOR_H__
18 #define __ONERT_BACKEND_CPU_TENSOR_H__
19
20 #include <backend/cpu_common/Tensor.h>
21 #include <ir/Data.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29
30 using Tensor = cpu_common::Tensor;
31
32 // Tensor which has data from external. To support this, assume below things
33 // no padding, always NHWC layout, constant tensor and not dynamic
34 class ExternalTensor : public Tensor
35 {
36 public:
37   ExternalTensor() = delete;
38
39 public:
40   ExternalTensor(const ir::OperandInfo &info, const ir::Layout layout) : Tensor(info, layout)
41   {
42     assert(_layout == ir::Layout::NHWC);
43     assert(_info.isConstant());
44     assert(_info.isDynamic() == false);
45   }
46
47 public:
48   void setData(const std::shared_ptr<ir::Data> data)
49   {
50     assert(data != nullptr);
51     _data = data;
52     // Note. Some op such as cker::Conv could take buffer as nullptr.
53     // That's why _buffer also would be used
54     _buffer = const_cast<uint8_t *>(_data->base());
55   }
56
57 public:
58   uint8_t *buffer() const override { return _buffer; }
59
60   bool is_constant() const override { return true; }
61   bool is_dynamic() const override { return false; }
62   void set_dynamic() override
63   {
64     throw std::runtime_error("This tensor does not support changing dynamic");
65   }
66
67   void setShape(const ir::Shape &) override
68   {
69     throw std::runtime_error("This tensor does not support changing shape");
70   }
71
72   void increase_ref() override { ++_num_references; }
73
74   void decrease_ref() override
75   {
76     assert(_data != nullptr);
77     assert(_num_references > 0);
78     --_num_references;
79     if (_num_references == 0)
80     {
81       _data.reset();
82       _buffer = nullptr;
83     }
84   }
85
86 private:
87   std::shared_ptr<const ir::Data> _data;
88 };
89
90 } // namespace cpu
91 } // namespace backend
92 } // namespace onert
93
94 #endif // __ONERT_BACKEND_CPU_TENSOR_H__