Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / controlflow / IOTensor.h
1 /*
2  * Copyright (c) 2020 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_CONTROLFLOW_IO_TENSOR_H__
18 #define __ONERT_BACKEND_CONTROLFLOW_IO_TENSOR_H__
19
20 #include "backend/IPortableTensor.h"
21 #include "UserTensor.h"
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace controlflow
28 {
29
30 /**
31  * @brief Tensor object that indirects to the tensor it is pointing to.
32  *
33  * A model I/O tensor could be two types.
34  *
35  * 1. @c UserTensor, if it is the primary graph
36  * 2. Any other derivative of @c IPortableTensor from another backend, otherwise
37  *
38  * To support these, this object indirects everything to the actual tensor pointer.
39  * Exceptionally if it is UserTensor, this class creates and manages it.
40  */
41 class IOTensor : public IPortableTensor
42 {
43 public:
44   IOTensor(const ir::OperandInfo &info, ir::Layout layout);
45
46 public:
47   void setTensor(IPortableTensor *tensor);
48   void setUserTensor(uint8_t *buffer, size_t size);
49   ir::OperandInfo orig_info() const { return _orig_info; }
50   ir::Layout orig_layout() const { return _orig_layout; }
51
52 public:
53   uint8_t *buffer() const override { return _tensor->buffer(); }
54   size_t total_size() const override { return _tensor->total_size(); }
55   size_t dimension(size_t index) const override { return _tensor->dimension(index); }
56   size_t num_dimensions() const override { return _tensor->num_dimensions(); }
57   size_t calcOffset(const ir::Coordinates &coords) const override
58   {
59     return _tensor->calcOffset(coords);
60   }
61   ir::Layout layout() const override { return _tensor->layout(); }
62   ir::DataType data_type() const override { return _tensor->data_type(); }
63   float data_scale() const override { return _tensor->data_scale(); }
64   int32_t data_offset() const override { return _tensor->data_offset(); }
65   bool is_dynamic() const override { return _is_dynamic || (_tensor && _tensor->is_dynamic()); }
66   void set_dynamic() override { _is_dynamic = true; }
67   ir::Shape getShape() const override { return _tensor->getShape(); }
68   void setShape(const ir::Shape &shape) override
69   {
70     // Workaround for IPortableTensor holds _info as its member
71     _info.shape(shape);
72     _tensor->setShape(shape);
73   }
74   bool is_constant() const override { return _tensor->is_constant(); }
75   bool applyShape(const ir::Shape &shape) override
76   {
77     // Workaround for IPortableTensor holds _info as its member
78     _info.shape(shape);
79     return _tensor->applyShape(shape);
80   }
81
82 private:
83   const ir::OperandInfo _orig_info;
84   const ir::Layout _orig_layout;
85   bool _is_dynamic{false};
86   IPortableTensor *_tensor{nullptr};        //< The actual tensor that is indirected
87   std::unique_ptr<UserTensor> _user_tensor; //< If it is a user tensor, it is managed by this object
88 };
89
90 } // namespace controlflow
91 } // namespace backend
92 } // namespace onert
93
94 #endif // __ONERT_BACKEND_CONTROLFLOW_IO_TENSOR_H__