forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSizesAndStrides.h
315 lines (267 loc) · 8.18 KB
/
SizesAndStrides.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#pragma once
#include <algorithm>
#include <cstdint>
#include <c10/macros/Macros.h>
#include <c10/util/ArrayRef.h>
#include <c10/util/SmallVector.h>
#define C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE 5
namespace c10::impl {
// Packed container for TensorImpl sizes and strides.
// This design improves on the previous approach of using a pair of
// c10::SmallVector<int64_t, 5> by specializing for the operations we
// actually use and enforcing that the number of sizes is the same as
// the number of strides. The memory layout is as follows:
//
// 1 size_t for the size
// 5 eightbytes of inline sizes and 5 eightbytes of inline strides, OR pointer
// to out-of-line array
class C10_API SizesAndStrides {
public:
// TODO: different iterator types for sizes & strides to prevent
// mixing the two accidentally.
using sizes_iterator = int64_t*;
using sizes_const_iterator = const int64_t*;
using strides_iterator = int64_t*;
using strides_const_iterator = const int64_t*;
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
SizesAndStrides() {
size_at_unchecked(0) = 0;
stride_at_unchecked(0) = 1;
}
~SizesAndStrides() {
if (C10_UNLIKELY(!isInline())) {
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
free(outOfLineStorage_);
}
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
SizesAndStrides(const SizesAndStrides& rhs) : size_(rhs.size_) {
if (C10_LIKELY(rhs.isInline())) {
copyDataInline(rhs);
} else {
allocateOutOfLineStorage(size_);
copyDataOutline(rhs);
}
}
SizesAndStrides& operator=(const SizesAndStrides& rhs) {
if (this == &rhs) {
return *this;
}
if (C10_LIKELY(rhs.isInline())) {
if (C10_UNLIKELY(!isInline())) {
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
free(outOfLineStorage_);
}
copyDataInline(rhs);
} else {
if (isInline()) {
allocateOutOfLineStorage(rhs.size_);
} else {
resizeOutOfLineStorage(rhs.size_);
}
copyDataOutline(rhs);
}
size_ = rhs.size_;
return *this;
}
// Move from rhs. rhs.size() == 0 afterwards.
SizesAndStrides(SizesAndStrides&& rhs) noexcept : size_(rhs.size_) {
if (C10_LIKELY(isInline())) {
memcpy(inlineStorage_, rhs.inlineStorage_, sizeof(inlineStorage_));
} else {
outOfLineStorage_ = rhs.outOfLineStorage_;
rhs.outOfLineStorage_ = nullptr;
}
rhs.size_ = 0;
}
// Move from rhs. rhs.size() == 0 afterwards.
SizesAndStrides& operator=(SizesAndStrides&& rhs) noexcept {
if (this == &rhs) {
return *this;
}
if (C10_LIKELY(rhs.isInline())) {
if (C10_UNLIKELY(!isInline())) {
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
free(outOfLineStorage_);
}
copyDataInline(rhs);
} else {
// They're outline. We're going to steal their vector.
if (!isInline()) {
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
free(outOfLineStorage_);
}
outOfLineStorage_ = rhs.outOfLineStorage_;
rhs.outOfLineStorage_ = nullptr;
}
size_ = rhs.size_;
rhs.size_ = 0;
return *this;
}
size_t size() const noexcept {
return size_;
}
const int64_t* sizes_data() const noexcept {
if (C10_LIKELY(isInline())) {
return &inlineStorage_[0];
} else {
return &outOfLineStorage_[0];
}
}
int64_t* sizes_data() noexcept {
if (C10_LIKELY(isInline())) {
return &inlineStorage_[0];
} else {
return &outOfLineStorage_[0];
}
}
sizes_const_iterator sizes_begin() const noexcept {
return sizes_data();
}
sizes_iterator sizes_begin() noexcept {
return sizes_data();
}
sizes_const_iterator sizes_end() const noexcept {
return sizes_begin() + size();
}
sizes_iterator sizes_end() noexcept {
return sizes_begin() + size();
}
IntArrayRef sizes_arrayref() const noexcept {
return IntArrayRef{sizes_data(), size()};
}
void set_sizes(IntArrayRef newSizes) {
resize(newSizes.size());
std::copy(newSizes.begin(), newSizes.end(), sizes_begin());
}
void set_strides(IntArrayRef strides) {
TORCH_INTERNAL_ASSERT(strides.size() == size());
std::copy(strides.begin(), strides.end(), strides_begin());
}
const int64_t* strides_data() const noexcept {
if (C10_LIKELY(isInline())) {
return &inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE];
} else {
return &outOfLineStorage_[size()];
}
}
int64_t* strides_data() noexcept {
if (C10_LIKELY(isInline())) {
return &inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE];
} else {
return &outOfLineStorage_[size()];
}
}
strides_const_iterator strides_begin() const noexcept {
if (C10_LIKELY(isInline())) {
return &inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE];
} else {
return &outOfLineStorage_[size()];
}
}
strides_iterator strides_begin() noexcept {
if (C10_LIKELY(isInline())) {
return &inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE];
} else {
return &outOfLineStorage_[size()];
}
}
strides_const_iterator strides_end() const noexcept {
return strides_begin() + size();
}
strides_iterator strides_end() noexcept {
return strides_begin() + size();
}
IntArrayRef strides_arrayref() const noexcept {
return IntArrayRef{strides_data(), size()};
}
// Size accessors.
int64_t size_at(size_t idx) const noexcept {
assert(idx < size());
return sizes_data()[idx];
}
int64_t& size_at(size_t idx) noexcept {
assert(idx < size());
return sizes_data()[idx];
}
int64_t size_at_unchecked(size_t idx) const noexcept {
return sizes_data()[idx];
}
int64_t& size_at_unchecked(size_t idx) noexcept {
return sizes_data()[idx];
}
// Size accessors.
int64_t stride_at(size_t idx) const noexcept {
assert(idx < size());
return strides_data()[idx];
}
int64_t& stride_at(size_t idx) noexcept {
assert(idx < size());
return strides_data()[idx];
}
int64_t stride_at_unchecked(size_t idx) const noexcept {
return strides_data()[idx];
}
int64_t& stride_at_unchecked(size_t idx) noexcept {
return strides_data()[idx];
}
void resize(size_t newSize) {
const auto oldSize = size();
if (newSize == oldSize) {
return;
}
if (C10_LIKELY(
newSize <= C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE && isInline())) {
if (oldSize < newSize) {
const auto bytesToZero =
(newSize - oldSize) * sizeof(inlineStorage_[0]);
memset(&inlineStorage_[oldSize], 0, bytesToZero);
memset(
&inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE + oldSize],
0,
bytesToZero);
}
size_ = newSize;
} else {
resizeSlowPath(newSize, oldSize);
}
}
void resizeSlowPath(size_t newSize, size_t oldSize);
private:
bool isInline() const noexcept {
return size_ <= C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE;
}
void copyDataInline(const SizesAndStrides& rhs) {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(rhs.isInline());
memcpy(inlineStorage_, rhs.inlineStorage_, sizeof(inlineStorage_));
}
static size_t storageBytes(size_t size) noexcept {
return size * 2 * sizeof(int64_t);
}
void allocateOutOfLineStorage(size_t size) {
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
outOfLineStorage_ = static_cast<int64_t*>(malloc(storageBytes(size)));
TORCH_CHECK(
outOfLineStorage_,
"Could not allocate memory for Tensor SizesAndStrides!");
}
void resizeOutOfLineStorage(size_t newSize) {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(!isInline());
outOfLineStorage_ = static_cast<int64_t*>(
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
realloc(outOfLineStorage_, storageBytes(newSize)));
TORCH_CHECK(
outOfLineStorage_,
"Could not allocate memory for Tensor SizesAndStrides!");
}
void copyDataOutline(const SizesAndStrides& rhs) noexcept {
memcpy(outOfLineStorage_, rhs.outOfLineStorage_, storageBytes(rhs.size_));
}
size_t size_{1};
union {
int64_t* outOfLineStorage_;
// NOLINTNEXTLINE(*c-array*)
int64_t inlineStorage_[C10_SIZES_AND_STRIDES_MAX_INLINE_SIZE * 2]{};
};
};
} // namespace c10::impl