forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessedNodeInputs.h
239 lines (202 loc) · 6.19 KB
/
ProcessedNodeInputs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#pragma once
#include <cstddef>
#include <cstdint>
#include <memory>
#include <c10/macros/Macros.h>
#include <c10/util/Logging.h>
/**
* Packed representation of input indices for ProcessedNode.
*/
class ProcessedNodeInputs {
private:
// This keeps the size usage for inputs + outputs down to 16 bytes;
// we use 12 bytes, and then two 2-byte integers are used to store
// the outputs.
static constexpr size_t kMaxInlineInputs = 5;
public:
ProcessedNodeInputs() : ProcessedNodeInputs(0) {}
explicit ProcessedNodeInputs(size_t size) {
TORCH_DCHECK_LT(size, (1 << 16));
if (size <= kMaxInlineInputs) {
repr_.inline_repr_.size = size;
} else {
new (&repr_.outline_repr_) HeapArrayPtr(size);
}
}
uint16_t operator[](uint16_t idx) const {
return (*const_cast<ProcessedNodeInputs*>(this))[idx];
}
uint16_t& operator[](uint16_t idx) {
if (C10_LIKELY(repr_.is_inline())) {
TORCH_DCHECK_LT(idx, repr_.inline_repr_.size);
return repr_.inline_repr_.inputs[idx];
} else {
return repr_.outline_repr_[idx];
}
}
C10_NODISCARD uint16_t size() const {
if (C10_LIKELY(repr_.is_inline())) {
return repr_.inline_repr_.size;
} else {
return repr_.outline_repr_.size();
}
}
C10_NODISCARD bool empty() const {
return size() == 0;
}
private:
class HeapArrayPtr {
public:
HeapArrayPtr() = default;
~HeapArrayPtr() = default;
explicit HeapArrayPtr(uint16_t size) : array_(alloc(size)) {}
HeapArrayPtr(const HeapArrayPtr& rhs) : array_(alloc(rhs.size())) {
if (rhs.array_) {
std::memcpy(
array_.get(),
rhs.array_.get(),
(rhs.size() + 1) * sizeof(uint16_t));
}
}
HeapArrayPtr& operator=(const HeapArrayPtr& rhs) {
if (&rhs == this) {
return *this;
}
if (size() != rhs.size()) {
array_ = alloc(rhs.size());
}
if (rhs.array_) {
std::memcpy(
array_.get(),
rhs.array_.get(),
(rhs.size() + 1) * sizeof(uint16_t));
}
return *this;
}
HeapArrayPtr(HeapArrayPtr&&) noexcept = default;
HeapArrayPtr& operator=(HeapArrayPtr&&) noexcept = default;
C10_NODISCARD bool empty() const {
return size() != 0;
}
C10_NODISCARD uint16_t size() const {
return array_ ? array_[0] : 0;
}
uint16_t operator[](uint16_t idx) const {
TORCH_DCHECK_LT(idx, size());
return array_[idx + 1];
}
uint16_t& operator[](uint16_t idx) {
TORCH_DCHECK_LT(idx, size());
return array_[idx + 1];
}
private:
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
std::unique_ptr<uint16_t[]> array_;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
static std::unique_ptr<uint16_t[]> alloc(uint16_t num_elts) {
if (num_elts) {
auto result = std::make_unique<uint16_t[]>(num_elts + 1);
result[0] = num_elts;
return result;
} else {
return nullptr;
}
}
};
// We want ProcessedNode to be able to pack two more `uint16_t`
// fields after its ProcessedNodeInputs, and we'll end up being
// aligned to an 8-byte boundary anyway. We could avoid this pragma
// at the cost of having to move ProcessedNode::outputs_offset_ and
// ProcessedNode::num_outputs_ into this class, which would be
// awkward.
#pragma pack(push, 2)
union Repr {
C10_NODISCARD bool is_inline() const {
uint8_t tag;
// Use of reinterpret_cast to pointer to char or unsigned char
// is defined behavior; see
// https://en.cppreference.com/w/cpp/language/reinterpret_cast .
std::memcpy(&tag, reinterpret_cast<const uint8_t*>(this), 1);
// HeapArrayPtr will be represented as a plain old pointer,
// which will have alignment to at least a 2-byte boundary
// (because it's uint16_t*) and more likely an 8- or 16-byte
// boundary because malloc will tend to just align everything to
// one of those. So, we just set tag to 1 when inline_repr_ is
// active so as to be able to differentiate the two.
return (tag & 1) != 0;
}
// NOLINTNEXTLINE(modernize-use-equals-default)
Repr() {}
~Repr() {
destroyIfOutline();
}
Repr(const Repr& rhs) {
if (rhs.is_inline()) {
std::memcpy(&inline_repr_, &rhs.inline_repr_, sizeof(inline_repr_));
} else {
new (&outline_repr_) OutlineRepr(rhs.outline_repr_);
}
}
Repr& operator=(const Repr& rhs) {
if (&rhs == this) {
return *this;
}
if (rhs.is_inline()) {
destroyIfOutline();
new (&inline_repr_) InlineRepr();
std::memcpy(&inline_repr_, &rhs.inline_repr_, sizeof(inline_repr_));
} else {
if (is_inline()) {
new (&outline_repr_) OutlineRepr(rhs.outline_repr_);
} else {
outline_repr_ = rhs.outline_repr_;
}
}
return *this;
}
Repr(Repr&& rhs) noexcept {
if (rhs.is_inline()) {
std::memcpy(&inline_repr_, &rhs.inline_repr_, sizeof(inline_repr_));
} else {
new (&outline_repr_) OutlineRepr(std::move(rhs.outline_repr_));
}
}
Repr& operator=(Repr&& rhs) noexcept {
if (&rhs == this) {
return *this;
}
if (rhs.is_inline()) {
destroyIfOutline();
new (&inline_repr_) InlineRepr();
std::memcpy(&inline_repr_, &rhs.inline_repr_, sizeof(inline_repr_));
} else {
if (is_inline()) {
new (&outline_repr_) OutlineRepr(std::move(rhs.outline_repr_));
} else {
outline_repr_ = std::move(rhs.outline_repr_);
}
}
return *this;
}
struct InlineRepr {
uint8_t tag = 0x1;
uint8_t size;
uint16_t inputs[kMaxInlineInputs];
};
using OutlineRepr = HeapArrayPtr;
InlineRepr inline_repr_{};
OutlineRepr outline_repr_;
private:
void destroyIfOutline() {
if (!is_inline()) {
outline_repr_.~OutlineRepr();
}
}
} repr_;
#pragma pack(pop)
};
static_assert(
sizeof(ProcessedNodeInputs) == 12,
"ProcessedNodeInputs has the wrong size!");