ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Operand.h
Go to the documentation of this file.
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_IR_OPERAND_H__
18#define __ONERT_IR_OPERAND_H__
19
20#include <cassert>
21#include <cstdint>
22#include <memory>
23#include <algorithm>
24
25#include "ir/Data.h"
26#include "ir/DataType.h"
27#include "ir/OperandInfo.h"
29
30namespace onert::ir
31{
32
34{
35public:
36 explicit Operand(const Shape &shape, const TypeInfo &type)
37 : _info{shape, type, MemAllocType::STATIC}
38 {
39 // DO NOTHING
40 }
41 explicit Operand(const Operand &) = default;
42
43public:
44 const Shape &shape(void) const { return _info.shape(); }
45 const TypeInfo &typeInfo(void) const { return _info.typeInfo(); }
46 const OperandInfo &info(void) const { return _info; }
47 OperandInfo &info(void) { return _info; }
48 size_t operandSize(void) const;
49
50 const OperationIndexSet &getUses() const { return _uses; }
51 OperationIndex getDef() const { return _def; }
52 void insertUse(const OperationIndex &idx);
53 void removeUse(const OperationIndex &idx);
54 void setDef(const OperationIndex &idx);
55 void unsetDef();
56 void clearDefUse();
57
58public:
59 void type(const DataType type) { _info.type(type); };
60
61public:
62 void data(std::shared_ptr<Data> &&data)
63 {
64 _data = std::move(data);
65 _info.setAsConstant();
66 }
67 const Data *data(void) const { return _data.get(); }
68
69 void releaseData(void) { _data.reset(); }
70
71 std::shared_ptr<Data> shareData(void) const { return _data; }
72
77 bool isConstant(void) const { return _info.isConstant(); }
78
79public:
80 template <typename T, typename... Args> void data(Args &&...args)
81 {
82 data(std::make_unique<T>(std::forward<Args>(args)...));
83 }
84
85public:
86 template <typename T> T asScalar(void) const
87 {
88 assert((shape().rank() == 0) || ((shape().rank() == 1) && (shape().dim(0) == 1)));
89 assert(_data != nullptr);
90 assert((_data->base() != nullptr) && (_data->size() == sizeof(T)));
91
92 return *(reinterpret_cast<const T *>(_data->base()));
93 }
94
95 template <typename T> std::vector<T> asVector() const
96 {
97 assert(_data != nullptr);
98 assert(_data->size() % sizeof(T) == 0);
99
100 const auto *base = reinterpret_cast<const T *>(_data->base());
101 const std::size_t size = _data->size() / sizeof(T);
102 return std::vector<T>(base, base + size);
103 }
104
105 void setOriginIndex(OriginIndex origin) { _info.setOriginIndex(origin); }
106 OriginIndex originIndex() const { return _info.originIndex(); }
107
108private:
109 OperandInfo _info;
110 std::shared_ptr<Data> _data;
111
112 OperationIndexSet _uses;
113 OperationIndex _def;
114};
115
116} // namespace onert::ir
117
118#endif // __ONERT_IR_OPERAND_H__
This file contains OperandInfo class.
const OperationIndexSet & getUses() const
Definition Operand.h:50
const OperandInfo & info(void) const
Definition Operand.h:46
OriginIndex originIndex() const
Definition Operand.h:106
OperationIndex getDef() const
Definition Operand.h:51
const TypeInfo & typeInfo(void) const
Definition Operand.h:45
T asScalar(void) const
Definition Operand.h:86
void data(Args &&...args)
Definition Operand.h:80
Operand(const Operand &)=default
const Data * data(void) const
Definition Operand.h:67
const Shape & shape(void) const
Definition Operand.h:44
void data(std::shared_ptr< Data > &&data)
Definition Operand.h:62
void setDef(const OperationIndex &idx)
Definition Operand.cc:43
std::vector< T > asVector() const
Definition Operand.h:95
void releaseData(void)
Definition Operand.h:69
void insertUse(const OperationIndex &idx)
Definition Operand.cc:39
Operand(const Shape &shape, const TypeInfo &type)
Definition Operand.h:36
void setOriginIndex(OriginIndex origin)
Definition Operand.h:105
size_t operandSize(void) const
Definition Operand.cc:22
void removeUse(const OperationIndex &idx)
Definition Operand.cc:41
bool isConstant(void) const
Get true if Operand is const, otherwise false a.
Definition Operand.h:77
OperandInfo & info(void)
Definition Operand.h:47
void type(const DataType type)
Definition Operand.h:59
std::shared_ptr< Data > shareData(void) const
Definition Operand.h:71
Class to save tensor's shape and type.
Definition OperandInfo.h:54
void setOriginIndex(OriginIndex origin)
const TypeInfo & typeInfo() const
Return tensor data type info.
void type(const DataType type)
Set tensor data type.
bool isConstant() const
const Shape & shape() const
Return tensor shape.
Definition OperandInfo.h:93
OriginIndex originIndex() const
MemAllocType
enum class indicating when the memory for a tensor is allocated
Definition OperandInfo.h:36
@ STATIC
At compile time, shape for a tensor is known, thus requried memory capacity can be calculated.
int32_t size[5]
Definition Slice.cpp:35