Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / backend / ITensor.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_OPERAND_I_TENSOR_H__
18 #define __ONERT_BACKEND_OPERAND_I_TENSOR_H__
19
20 #include <cstring>
21 #include <cstdint>
22 #include <functional>
23
24 #include "ir/DataType.h"
25 #include "ir/Layout.h"
26 #include "ir/Shape.h"
27 #include "ir/Coordinates.h"
28 #include "util/Utils.h"
29
30 namespace onert
31 {
32 namespace backend
33 {
34
35 struct IDynamicTensorManager;
36
37 class ITensor
38 {
39 public:
40   virtual ~ITensor() = default;
41
42 public:
43   virtual uint8_t *buffer() const = 0;
44   virtual size_t total_size() const = 0;
45   virtual size_t dimension(size_t index) const = 0;
46   virtual size_t num_dimensions() const = 0;
47   virtual size_t calcOffset(const ir::Coordinates &coords) const = 0;
48   virtual ir::Layout layout() const = 0;
49   virtual ir::DataType data_type() const = 0;
50   virtual float data_scale() const = 0;
51   virtual int32_t data_offset() const = 0;
52   virtual bool has_padding() const = 0;
53   virtual void access(const std::function<void(ITensor &tensor)> &fn) = 0;
54
55   /**
56    * @brief Return the dynamic tensor manager
57    *
58    * If dynamic tensors are not supported, it returns @c nullptr .
59    *
60    * @return IDynamicTensorManager* DynamicTensorManager
61    */
62   virtual IDynamicTensorManager *dynamic_tensor_manager() { return nullptr; }
63
64   /**
65    * @brief Return true if the tensor is constant
66    */
67   virtual bool is_constant() const
68   {
69     throw std::runtime_error("This backend does not support checking constant");
70   }
71
72   /**
73    * @brief Return true if the tensor needs dynamic allocation, meaning that during compile-time
74    *        the outpus shape cannot be known and the output shape is calculated during
75    *        kernel execution-time.
76    */
77   virtual bool is_dynamic() const = 0;
78
79   /// @brief set this tensor dynamic
80   virtual void set_dynamic()
81   {
82     throw std::runtime_error("This backend does not support dynamic tensor");
83   }
84
85   /**
86    * @brief Set the shape of tenser to new_shape
87    * @note  Higer dimension will be placed on front.
88    */
89   virtual void setShape(const ir::Shape &new_shape)
90   {
91     UNUSED_RELEASE(new_shape);
92     throw std::runtime_error("This backend does not support dynamic setShape");
93   }
94
95   /**
96    * @brief Get ir::Shape of tensor
97    * @note  Higer dimension will be placed on front.
98    */
99   virtual ir::Shape getShape() const;
100 };
101
102 } // namespace backend
103 } // namespace onert
104
105 #endif // __ONERT_BACKEND_OPERAND_I_TENSOR_H__