ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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
31{
32namespace ir
33{
34
36{
37public:
38 explicit Operand(const Shape &shape, const TypeInfo &type)
39 : _info{shape, type, MemAllocType::STATIC}
40 {
41 // DO NOTHING
42 }
43 explicit Operand(const Operand &) = default;
44
45public:
46 const Shape &shape(void) const { return _info.shape(); }
47 const TypeInfo &typeInfo(void) const { return _info.typeInfo(); }
48 const OperandInfo &info(void) const { return _info; }
49 OperandInfo &info(void) { return _info; }
50 size_t operandSize(void) const;
51
52 const OperationIndexSet &getUses() const { return _uses; }
53 OperationIndex getDef() const { return _def; }
54 void insertUse(const OperationIndex &idx);
55 void removeUse(const OperationIndex &idx);
56 void setDef(const OperationIndex &idx);
57 void unsetDef();
58 void clearDefUse();
59
60public:
61 void type(const DataType type) { _info.type(type); };
62
63public:
64 void data(std::shared_ptr<Data> &&data)
65 {
66 _data = std::move(data);
67 _info.setAsConstant();
68 }
69 const Data *data(void) const { return _data.get(); }
70
71 void releaseData(void) { _data.reset(); }
72
73 std::shared_ptr<Data> shareData(void) const { return _data; }
74
79 bool isConstant(void) const { return _info.isConstant(); }
80
81public:
82 template <typename T, typename... Args> void data(Args &&...args)
83 {
84 data(std::make_unique<T>(std::forward<Args>(args)...));
85 }
86
87public:
88 template <typename T> T asScalar(void) const
89 {
90 assert((shape().rank() == 0) || ((shape().rank() == 1) && (shape().dim(0) == 1)));
91 assert(_data != nullptr);
92 assert((_data->base() != nullptr) && (_data->size() == sizeof(T)));
93
94 return *(reinterpret_cast<const T *>(_data->base()));
95 }
96
97 template <typename T> std::vector<T> asVector() const
98 {
99 assert(_data != nullptr);
100 assert(_data->size() % sizeof(T) == 0);
101
102 const auto *base = reinterpret_cast<const T *>(_data->base());
103 const std::size_t size = _data->size() / sizeof(T);
104 return std::vector<T>(base, base + size);
105 }
106
107 void setOriginIndex(OriginIndex origin) { _info.setOriginIndex(origin); }
108 OriginIndex originIndex() const { return _info.originIndex(); }
109
110private:
111 OperandInfo _info;
112 std::shared_ptr<Data> _data;
113
114 OperationIndexSet _uses;
115 OperationIndex _def;
116};
117
118} // namespace ir
119} // namespace onert
120
121#endif // __ONERT_IR_OPERAND_H__
This file contains OperandInfo class.
const OperationIndexSet & getUses() const
Definition Operand.h:52
const OperandInfo & info(void) const
Definition Operand.h:48
OriginIndex originIndex() const
Definition Operand.h:108
OperationIndex getDef() const
Definition Operand.h:53
const TypeInfo & typeInfo(void) const
Definition Operand.h:47
T asScalar(void) const
Definition Operand.h:88
void data(Args &&...args)
Definition Operand.h:82
Operand(const Operand &)=default
const Data * data(void) const
Definition Operand.h:69
const Shape & shape(void) const
Definition Operand.h:46
void data(std::shared_ptr< Data > &&data)
Definition Operand.h:64
void setDef(const OperationIndex &idx)
Definition Operand.cc:45
std::vector< T > asVector() const
Definition Operand.h:97
void releaseData(void)
Definition Operand.h:71
void insertUse(const OperationIndex &idx)
Definition Operand.cc:41
Operand(const Shape &shape, const TypeInfo &type)
Definition Operand.h:38
void setOriginIndex(OriginIndex origin)
Definition Operand.h:107
size_t operandSize(void) const
Definition Operand.cc:24
void removeUse(const OperationIndex &idx)
Definition Operand.cc:43
bool isConstant(void) const
Get true if Operand is const, otherwise false a.
Definition Operand.h:79
OperandInfo & info(void)
Definition Operand.h:49
void type(const DataType type)
Definition Operand.h:61
std::shared_ptr< Data > shareData(void) const
Definition Operand.h:73
Class to save tensor's shape and type.
Definition OperandInfo.h:56
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:95
OriginIndex originIndex() const
MemAllocType
enum class indicating when the memory for a tensor is allocated
Definition OperandInfo.h:38
@ 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