Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / controlflow / UserTensor.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_USER_TENSOR_H__
18 #define __ONERT_BACKEND_CONTROLFLOW_USER_TENSOR_H__
19
20 #include "ir/OperandInfo.h"
21 #include "backend/IPortableTensor.h"
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace controlflow
28 {
29
30 /**
31  * @brief Tensor object that is for Input and Output tensors from the user.
32  *
33  * This class is a wrapped buffer that is allocated by the user. So it does not have resposibility
34  * on allocation nor deallocation. All the model input/output tensors are wrapped with this class
35  * for execution.
36  *
37  */
38 class UserTensor : public IPortableTensor
39 {
40 public:
41   UserTensor(const ir::OperandInfo &info, ir::Layout layout, uint8_t *buffer, size_t size)
42       : _info{info}, _layout{layout}, _buffer{buffer}, _size{size}, _dynamic{false}
43   {
44   }
45
46   UserTensor(const ir::OperandInfo &info, ir::Layout layout) : UserTensor{info, layout, nullptr, 0}
47   {
48   }
49
50 public:
51   void setBuffer(uint8_t *buffer, size_t size)
52   {
53     _buffer = buffer;
54     _size = size;
55   }
56
57 public:
58   uint8_t *buffer() const override { return _buffer; }
59   size_t total_size() const override { return _size; }
60   size_t dimension(size_t index) const override { return _info.shape().dim(index); }
61   size_t num_dimensions() const override { return _info.shape().rank(); }
62   size_t calcOffset(const ir::Coordinates &coords) const override;
63   ir::Layout layout() const override { return _layout; }
64   ir::DataType data_type() const override { return _info.typeInfo().type(); }
65   float data_scale() const override { return _info.typeInfo().scale(); }
66   int32_t data_offset() const override { return _info.typeInfo().offset(); }
67   bool is_dynamic() const override { return _dynamic; }
68   void set_dynamic() override { _dynamic = true; }
69   ir::Shape getShape() const override { return _info.shape(); }
70   void setShape(const ir::Shape &new_shape) override { _info.shape(new_shape); }
71
72 private:
73   ir::OperandInfo _info;
74   ir::Layout _layout;
75   uint8_t *_buffer;
76   size_t _size;
77   bool _dynamic;
78 };
79
80 } // namespace controlflow
81 } // namespace backend
82 } // namespace onert
83
84 #endif // __ONERT_BACKEND_CONTROLFLOW_USER_TENSOR_H__