2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __ONERT_BACKEND_CONTROLFLOW_IO_TENSOR_H__
18 #define __ONERT_BACKEND_CONTROLFLOW_IO_TENSOR_H__
20 #include "backend/IPortableTensor.h"
21 #include "UserTensor.h"
31 * @brief Tensor object that indirects to the tensor it is pointing to.
33 * A model I/O tensor could be two types.
35 * 1. @c UserTensor, if it is the primary graph
36 * 2. Any other derivative of @c IPortableTensor from another backend, otherwise
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.
41 class IOTensor : public IPortableTensor
44 IOTensor(const ir::OperandInfo &info, ir::Layout layout);
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; }
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
59 return _tensor->calcOffset(coords);
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
70 // Workaround for IPortableTensor holds _info as its member
72 _tensor->setShape(shape);
74 bool is_constant() const override { return _tensor->is_constant(); }
75 bool applyShape(const ir::Shape &shape) override
77 // Workaround for IPortableTensor holds _info as its member
79 return _tensor->applyShape(shape);
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
90 } // namespace controlflow
91 } // namespace backend
94 #endif // __ONERT_BACKEND_CONTROLFLOW_IO_TENSOR_H__