forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivalue_inl.h
690 lines (610 loc) · 22.4 KB
/
ivalue_inl.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#pragma once
#include <condition_variable>
#include <type_traits>
#include <ATen/core/functional.h>
#include <ATen/core/interned_strings.h>
#include <c10/core/Scalar.h>
#include <c10/core/TensorImpl.h>
#include <c10/core/UndefinedTensorImpl.h>
#include <ATen/core/Dict.h>
#include <ATen/core/List.h>
namespace torch {
namespace jit {
struct Function;
namespace script {
struct CompilationUnit;
}
} // namespace jit
} // namespace torch
namespace c10 {
struct IValue;
struct ClassType;
struct TupleType;
template<class T, class NullType>
c10::intrusive_ptr<T, NullType> IValue::moveToIntrusivePtr() {
auto t = c10::intrusive_ptr<T, NullType>::reclaim(static_cast<T*>(payload.as_intrusive_ptr));
clearToNone();
return t;
}
template<typename T, class NullType>
c10::intrusive_ptr<T, NullType> IValue::toIntrusivePtr() const {
auto r = c10::intrusive_ptr<T, NullType>::reclaim(static_cast<T*>(payload.as_intrusive_ptr));
auto p = r;
r.release();
return p;
}
inline c10::intrusive_ptr<ivalue::Future> IValue::toFuture() && {
AT_ASSERT(isFuture(), "Expected Future but got ", tagKind());
return moveToIntrusivePtr<ivalue::Future>();
}
inline c10::intrusive_ptr<ivalue::Future> IValue::toFuture() const & {
AT_ASSERT(isFuture(), "Expected Future but got ", tagKind());
return toIntrusivePtr<ivalue::Future>();
}
inline c10::intrusive_ptr<ivalue::ConstantString> IValue::toString() && {
AT_ASSERT(isString(), "Expected String but got ", tagKind());
return moveToIntrusivePtr<ivalue::ConstantString>();
}
inline c10::intrusive_ptr<ivalue::ConstantString> IValue::toString() const & {
AT_ASSERT(isString(), "Expected String but got ", tagKind());
return toIntrusivePtr<ivalue::ConstantString>();
}
inline c10::intrusive_ptr<ivalue::Object> IValue::toObject() && {
AT_ASSERT(isObject(), "Expected Object but got ", tagKind());
return toIntrusivePtr<ivalue::Object>();
}
inline c10::intrusive_ptr<ivalue::Object> IValue::toObject() const & {
AT_ASSERT(isObject(), "Expected Object but got ", tagKind());
return toIntrusivePtr<ivalue::Object>();
}
inline at::Tensor IValue::toTensor() && {
AT_ASSERT(isTensor(), "Expected Tensor but got ", tagKind());
return at::Tensor(moveToIntrusivePtr<at::TensorImpl, at::UndefinedTensorImpl>());
}
inline at::Tensor IValue::toTensor() const & {
AT_ASSERT(isTensor(), "Expected Tensor but got ", tagKind());
return at::Tensor(toIntrusivePtr<at::TensorImpl, at::UndefinedTensorImpl>());
}
inline c10::intrusive_ptr<caffe2::Blob> IValue::toBlob() && {
AT_ASSERT(isBlob(), "Expected Blob but got ", tagKind());
return moveToIntrusivePtr<caffe2::Blob>();
}
inline c10::intrusive_ptr<caffe2::Blob> IValue::toBlob() const & {
AT_ASSERT(isBlob(), "Expected Blob but got ", tagKind());
return toIntrusivePtr<caffe2::Blob>();;
}
namespace ivalue {
template <typename T>
using Shared = c10::intrusive_ptr<T>;
// string
struct CAFFE2_API ConstantString final : c10::intrusive_ptr_target {
private:
const std::string str_;
public:
ConstantString(std::string str)
: str_(std::move(str)) {}
static c10::intrusive_ptr<ConstantString> create(std::string str_);
const std::string & string() const {
return str_;
}
operator const std::string & () const {
return string();
}
CAFFE2_API friend std::ostream& operator<<(
std::ostream& out,
const ConstantString& v);
};
struct Future;
struct CAFFE2_API Tuple : c10::intrusive_ptr_target {
private:
std::vector<IValue> elements_;
public:
static c10::intrusive_ptr<Tuple> create(std::vector<IValue> elements_, std::shared_ptr<TupleType> type_) {
TORCH_INTERNAL_ASSERT(nullptr != type_.get(), "Type cannot be nullptr");
return c10::make_intrusive<Tuple>(std::move(elements_), type_);
}
C10_DEPRECATED_MESSAGE("Creating tuples without type information is deprecated. Please use Tuple::create(elements, type) instead.")
static c10::intrusive_ptr<Tuple> create(std::vector<IValue> elements_) {
return c10::make_intrusive<Tuple>(std::move(elements_), nullptr);
}
const std::vector<IValue>& elements() const & {
return elements_;
}
operator const std::vector<IValue>&() const {
return elements();
}
std::vector<IValue>& elements() & {
return elements_;
}
operator std::vector<IValue>&() {
return elements();
}
std::vector<IValue>&& elements() && {
return std::move(elements_);
}
std::shared_ptr<TupleType> type;
private:
Tuple(std::vector<IValue> elements, std::shared_ptr<TupleType> type)
: elements_(std::move(elements)), type(std::move(type)) {}
friend class c10::intrusive_ptr<Tuple>;
};
struct Object;
}
// Future
struct C10_EXPORT ivalue::Future final : c10::intrusive_ptr_target {
private:
c10::intrusive_ptr<Future> intrusive_from_this() {
c10::raw::intrusive_ptr::incref(this); // we are creating a new pointer
// from a raw `this` pointer
// so we need to bump the refcount
// to account for this ownership
return c10::intrusive_ptr<Future>::reclaim(this);
}
public:
struct CAFFE2_API FutureError final : public std::exception {
FutureError(std::string&& error_msg_)
: error_msg(std::move(error_msg_)) {}
FutureError() = default;
const char* what() const noexcept override {
return error_msg.c_str();
}
std::string error_msg;
};
/**
* Wait on the future until it completes.
*/
void wait() {
std::unique_lock<std::mutex> lock(mutex_);
while (!completed_) {
finished_cv_.wait(lock);
}
}
/**
* Explicitly mark the future as completed with the output value.
*/
void markCompleted(IValue value) {
std::unique_lock<std::mutex> lock(mutex_);
AT_ASSERT(!completed());
completed_ = true;
value_ = std::move(value);
fireCallbacks();
finished_cv_.notify_all();
}
void markCompleted() {
markCompleted(IValue {});
}
void markCompleted(FutureError&& error_) {
std::unique_lock<std::mutex> lock(mutex_);
AT_ASSERT(!completed());
completed_ = true;
has_error = true;
error = std::move(error_);
fireCallbacks();
finished_cv_.notify_all();
}
// Get the result of the current future.
IValue value() {
std::unique_lock<std::mutex> lock(mutex_);
AT_ASSERT(completed());
if (has_error) {
throw error;
}
return value_;
}
/**
* Add a callback to the future.
* The callbacks will be executed once the future completes.
* If the future has already completed,
* this function will execute the callback immediately.
*/
void addCallback(std::function<void(void)> callback) {
std::unique_lock<std::mutex> lock(mutex_);
if (completed()) {
lock.unlock();
callback();
return;
}
callbacks.push_back(callback);
}
// Check if the current future has completed
bool completed() {
return completed_;
}
CAFFE2_API friend std::ostream& operator<<(
std::ostream& out,
const Future& v);
private:
void fireCallbacks() {
AT_ASSERT(completed());
// There is no need to protect callbacks with the lock.
// Once completed_ is set to true, no one can add new callback to the list.
for (auto& callback : callbacks) {
callback();
}
callbacks.clear();
}
std::mutex mutex_;
std::atomic_bool completed_ = {false}; // is this future complete
std::condition_variable finished_cv_;
IValue value_; // when finished the value
std::vector<std::function<void(void)>> callbacks;
bool has_error = false;
FutureError error;
};
// User-defined object.
struct C10_EXPORT ivalue::Object final : c10::intrusive_ptr_target {
public:
Object(StrongTypePtr type, size_t numSlots) : type_(std::move(type)) {
slots_.resize(numSlots);
}
static c10::intrusive_ptr<Object> create(
StrongTypePtr type,
size_t numSlots) {
return c10::make_intrusive<Object>(std::move(type), numSlots);
}
/**
* Slot API.
*
* Attributes are stored as a simple vector so that lookups are fast at
* runtime. A "slot" is just an index into that vector, which can be computed
* statically if you have access to the class type. Use this API if you are
* writing compiler stuff.
*/
void setSlot(size_t slot, IValue v) {
if (slot >= slots_.size()) {
// for module types, it is possible that the members of the class have
// expanded after the object was created. In this case, we expand
// the slots to the right size
resizeObject(slot);
}
slots_[slot] = v;
}
const IValue& getSlot(size_t slot) const {
return slots_.at(slot);
}
/**
* Attribute API.
*
* Wrappers around the slot stuff so that users can access attributes
* directly. Use this API if you are a user.
*
* Note: Unlike in Python, TorchScript must make a distinction between
* attributes (which are IValues) and methods (which are Methods). If you
* want a method, use `obj.type()->getMethod()`
*/
IValue getAttr(const std::string& name) const;
void setAttr(const std::string& name, IValue v);
std::string name() const;
const std::vector<IValue>& slots() const {
return slots_;
}
std::shared_ptr<ClassType> type() const {
return type_.type_;
}
std::shared_ptr<torch::jit::script::CompilationUnit> compilation_unit() {
return type_.cu_;
}
private:
void resizeObject(size_t slot);
StrongTypePtr type_;
std::vector<IValue> slots_;
};
std::vector<std::pair<IValue, IValue>> iterationOrder(const c10::Dict<IValue, IValue>& dict);
#undef TORCH_FORALL_TAGS
namespace detail {
struct _guarded_unsigned_long_unique_dummy final {
_guarded_unsigned_long_unique_dummy(int64_t){};
};
using _guarded_unsigned_long = c10::guts::conditional_t<
std::is_same<unsigned long, uint32_t>::value ||
std::is_same<unsigned long, uint64_t>::value,
_guarded_unsigned_long_unique_dummy,
unsigned long>;
} // namespace detail
inline const ivalue::Object& IValue::toObjectRef() const {
AT_ASSERT(isObject(), "Expected Object but got ", tagKind());
return *static_cast<const c10::ivalue::Object*>(payload.as_intrusive_ptr);
}
// note: when adding a DEFINE_TO case here you should also add a
// toX method to IValue. These named methods are much more discoverable
// than the to templated function.
#define DEFINE_TO(type, method_name) \
template<> \
inline type IValue::to<type>() && { \
return std::move(*this).method_name(); \
} \
template<> \
inline type IValue::to<type>() const & { \
return this->method_name(); \
}
DEFINE_TO(at::Tensor, toTensor)
DEFINE_TO(float, toDouble)
DEFINE_TO(double, toDouble)
DEFINE_TO(unsigned char, toInt)
DEFINE_TO(signed char, toInt)
DEFINE_TO(unsigned short, toInt)
DEFINE_TO(short, toInt)
DEFINE_TO(int, toInt)
DEFINE_TO(uint32_t, toInt)
DEFINE_TO(uint64_t, toInt)
DEFINE_TO(detail::_guarded_unsigned_long, toInt)
DEFINE_TO(int64_t, toInt)
DEFINE_TO(bool, toBool)
DEFINE_TO(c10::intrusive_ptr<caffe2::Blob>, toBlob);
DEFINE_TO(c10::intrusive_ptr<ivalue::ConstantString>, toString)
DEFINE_TO(c10::intrusive_ptr<ivalue::Object>, toObject)
DEFINE_TO(at::Scalar, toScalar)
DEFINE_TO(c10::List<int64_t>, toIntList)
DEFINE_TO(c10::List<double>, toDoubleList)
DEFINE_TO(c10::List<bool>, toBoolList)
DEFINE_TO(c10::List<at::Tensor>, toTensorList)
DEFINE_TO(c10::impl::GenericList, toGenericList)
DEFINE_TO(c10::impl::GenericDict, toGenericDict)
DEFINE_TO(c10::intrusive_ptr<ivalue::Tuple>, toTuple)
DEFINE_TO(std::string, toStringRef)
DEFINE_TO(c10::intrusive_ptr<ivalue::Future>, toFuture)
DEFINE_TO(IValue, toIValue)
DEFINE_TO(c10::Device, toDevice)
DEFINE_TO(at::ScalarType, toScalarType)
DEFINE_TO(at::Layout, toLayout)
DEFINE_TO(at::MemoryFormat, toMemoryFormat)
DEFINE_TO(at::QScheme, toQScheme)
template <class T>
struct _fake_type {};
// generic_to<T> converts an IValue from a generic list or generic dict
// to a concrete list/dict type likelike List<T>, Dict<...> or optional<T>.
// Note that in the case of lists, this only works for IValue-based lists,
// i.e. not for int64_t, double, ...
// generic_to<T> is an implementation detail of IValue::to<T> and not
// supposed to be called directly.
// The _fake_type<T> parameter allows us to overload
// based on the return type.
template <class Elem>
C10_DEPRECATED_MESSAGE("IValues based on std::vector<T> are potentially slow and deprecated. Please use c10::List<T> instead.")
std::vector<Elem> generic_to(
IValue ivalue,
_fake_type<std::vector<Elem>>) {
// We need to do a deep copy of the vector because there might be other
// references to this same IValue that also use the list. We can't just
// move the elements out.
auto list = std::move(ivalue).to<List<Elem>>();
std::vector<Elem> result;
result.reserve(list.size());
for (Elem v : list) {
result.push_back(std::move(v));
}
return result;
}
template <typename Elem>
c10::List<Elem> generic_to(
IValue ivalue,
_fake_type<c10::List<Elem>>) {
return impl::toTypedList<Elem>(std::move(ivalue).toGenericList());
}
template <typename Key, typename Value>
c10::Dict<Key, Value> generic_to(
IValue ivalue,
_fake_type<c10::Dict<Key, Value>>) {
return impl::toTypedDict<Key, Value>(std::move(ivalue).toGenericDict());
}
template <typename K, typename V>
C10_DEPRECATED_MESSAGE("IValues based on std::unordered_map are slow and deprecated. Please use c10::Dict<K, V> instead.")
std::unordered_map<K, V> generic_to(
IValue ivalue,
_fake_type<std::unordered_map<K, V>>) {
std::unordered_map<K, V> specialized_dict;
for (const auto& item : std::move(ivalue).toGenericDict()) {
specialized_dict[item.key().to<K>()] = item.value().to<V>();
}
return specialized_dict;
}
template <typename T>
c10::optional<T> generic_to(
IValue ivalue,
_fake_type<c10::optional<T>>) {
if (ivalue.isNone()) {
return c10::nullopt;
}
return std::move(ivalue).to<T>();
}
template <typename T>
inline T IValue::to() && {
return generic_to(std::move(*this), _fake_type<T>{});
}
template <typename T>
inline T IValue::to() const& {
return generic_to(*this, _fake_type<T>{});
}
inline c10::List<int64_t> IValue::toIntList() && {
AT_ASSERT(isIntList(), "Expected IntList but got ", tagKind());
return c10::List<int64_t>(moveToIntrusivePtr<c10::detail::ListImpl<int64_t>>());
}
inline c10::List<int64_t> IValue::toIntList() const & {
AT_ASSERT(isIntList(), "Expected IntList but got ", tagKind());
return c10::List<int64_t>(toIntrusivePtr<c10::detail::ListImpl<int64_t>>());
}
inline c10::ArrayRef<int64_t> IValue::toIntListRef() const {
AT_ASSERT(isIntList(), "Expected IntList but got ", tagKind());
return static_cast<const c10::detail::ListImpl<int64_t>*>(payload.as_intrusive_ptr)->list;
}
inline c10::List<double> IValue::toDoubleList() && {
AT_ASSERT(isDoubleList(), "Expected DoubleList but got ", tagKind());
return c10::List<double>(moveToIntrusivePtr<c10::detail::ListImpl<double>>());
}
inline c10::List<double> IValue::toDoubleList() const & {
AT_ASSERT(isDoubleList(), "Expected DoubleList but got ", tagKind());
return c10::List<double>(toIntrusivePtr<c10::detail::ListImpl<double>>());
}
inline c10::ArrayRef<double> IValue::toDoubleListRef() const {
AT_ASSERT(isDoubleList(), "Expected DoubleList but got ", tagKind());
return static_cast<const c10::detail::ListImpl<double>*>(payload.as_intrusive_ptr)->list;
}
inline c10::List<bool> IValue::toBoolList() && {
AT_ASSERT(isBoolList(), "Expected BoolList but got ", tagKind());
return c10::List<bool>(moveToIntrusivePtr<c10::detail::ListImpl<bool>>());
}
inline c10::List<bool> IValue::toBoolList() const & {
AT_ASSERT(isBoolList(), "Expected BoolList but got ", tagKind());
return c10::List<bool>(toIntrusivePtr<c10::detail::ListImpl<bool>>());
}
inline c10::List<at::Tensor> IValue::toTensorList() && {
AT_ASSERT(isTensorList(), "Expected TensorList but got ", tagKind());
return c10::List<at::Tensor>(moveToIntrusivePtr<c10::detail::ListImpl<at::Tensor>>());
}
inline c10::List<at::Tensor> IValue::toTensorList() const & {
AT_ASSERT(isTensorList(), "Expected TensorList but got ", tagKind());
return c10::List<at::Tensor>(toIntrusivePtr<c10::detail::ListImpl<at::Tensor>>());
}
inline c10::ArrayRef<at::Tensor> IValue::toTensorListRef() const {
AT_ASSERT(isTensorList(), "Expected TensorList but got ", tagKind());
return static_cast<const c10::detail::ListImpl<at::Tensor>*>(payload.as_intrusive_ptr)->list;
}
inline c10::List<IValue> IValue::toGenericList() && {
AT_ASSERT(isGenericList(), "Expected GenericList but got ", tagKind());
return c10::List<IValue>(moveToIntrusivePtr<c10::detail::ListImpl<IValue>>());
}
inline c10::List<IValue> IValue::toGenericList() const & {
AT_ASSERT(isGenericList(), "Expected GenericList but got ", tagKind());
return c10::List<IValue>(toIntrusivePtr<c10::detail::ListImpl<IValue>>());
}
inline c10::ArrayRef<IValue> IValue::toGenericListRef() const {
AT_ASSERT(isGenericList(), "Expected GenericList but got ", tagKind());
return static_cast<const c10::detail::ListImpl<IValue>*>(payload.as_intrusive_ptr)->list;
}
inline c10::Dict<IValue, IValue> IValue::toGenericDict() && {
AT_ASSERT(isGenericDict(), "Expected GenericDict but got ", tagKind());
return c10::Dict<IValue, IValue>(moveToIntrusivePtr<c10::detail::DictImpl>());
}
inline c10::Dict<IValue, IValue> IValue::toGenericDict() const & {
AT_ASSERT(isGenericDict(), "Expected GenericDict but got ", tagKind());
return c10::Dict<IValue, IValue>(toIntrusivePtr<c10::detail::DictImpl>());
}
inline c10::intrusive_ptr<ivalue::Tuple> IValue::toTuple() && {
AT_ASSERT(isTuple(), "Expected Tuple but got ", tagKind());
return moveToIntrusivePtr<ivalue::Tuple>();
}
inline c10::intrusive_ptr<ivalue::Tuple> IValue::toTuple() const & {
AT_ASSERT(isTuple(), "Expected Tuple but got ", tagKind());
return toIntrusivePtr<ivalue::Tuple>();
}
inline IValue::IValue(c10::intrusive_ptr<ivalue::Tuple> v)
: tag(Tag::Tuple), is_intrusive_ptr(true) {
payload.as_intrusive_ptr = v.release();
}
inline IValue::IValue(c10::List<int64_t> v)
: tag(Tag::IntList), is_intrusive_ptr(true) {
payload.as_intrusive_ptr = v.impl_.release();
}
inline IValue::IValue(std::vector<int64_t> v)
: IValue(c10::impl::toList(v)) {}
inline IValue::IValue(c10::ArrayRef<int64_t> v)
: IValue(c10::List<int64_t>(v)) {}
inline IValue::IValue(c10::intrusive_ptr<ivalue::ConstantString> v)
: tag(Tag::String), is_intrusive_ptr(true) {
payload.as_intrusive_ptr = v.release();
}
inline IValue::IValue(std::string v)
: IValue(ivalue::ConstantString::create(std::move(v))) {}
inline IValue::IValue(c10::List<double> v)
: tag(Tag::DoubleList), is_intrusive_ptr(true) {
payload.as_intrusive_ptr = v.impl_.release();
}
inline IValue::IValue(std::vector<double> v)
: IValue(c10::impl::toList(std::move(v))) {}
inline IValue::IValue(c10::List<bool> v)
: tag(Tag::BoolList), is_intrusive_ptr(true) {
payload.as_intrusive_ptr = v.impl_.release();
}
inline IValue::IValue(std::vector<bool> v)
: IValue(c10::impl::toList(std::move(v))) {}
inline IValue::IValue(c10::List<at::Tensor> v)
: tag(Tag::TensorList), is_intrusive_ptr(true) {
payload.as_intrusive_ptr = v.impl_.release();
}
inline IValue::IValue(std::vector<at::Tensor> v)
: IValue(c10::impl::toList(std::move(v))) {}
inline IValue::IValue(c10::impl::GenericList v)
: tag(Tag::GenericList), is_intrusive_ptr(true) {
payload.as_intrusive_ptr = v.impl_.release();
}
template<class T> inline IValue::IValue(c10::List<T> v)
: IValue(impl::toGenericList<T>(std::move(v))) {
static_assert(std::is_same<IValue, typename c10::List<T>::StorageT>::value, "Can only use this constructor for generic list types");
}
template<class T> inline IValue::IValue(std::vector<T> v)
: IValue(c10::List<T>()) {
static_assert(std::is_same<IValue, typename c10::List<T>::StorageT>::value, "Can only use this constructor for generic list types");
auto list = to<c10::List<T>>();
list.reserve(v.size());
for (auto& e : v) {
list.push_back(std::move(e));
}
}
inline IValue::IValue(c10::impl::GenericDict v)
: tag(Tag::GenericDict), is_intrusive_ptr(true) {
payload.as_intrusive_ptr = v.impl_.release();
}
template<class Key, class Value>
inline IValue::IValue(c10::Dict<Key, Value> v)
: IValue(impl::toGenericDict(std::move(v))) {}
template<class Key, class Value> inline IValue::IValue(std::unordered_map<Key, Value> v)
: IValue(Dict<Key, Value>()) {
auto dict = to<c10::Dict<Key, Value>>();
dict.reserve(v.size());
for (auto& e : v) {
dict.insert(std::move(e.first), std::move(e.second));
}
}
template<class T> inline IValue::IValue(c10::optional<T> v): IValue() {
if (v.has_value()) {
*this = IValue(std::move(*v));
}
}
inline IValue::IValue(c10::nullopt_t): IValue() {}
inline IValue::IValue(c10::intrusive_ptr<ivalue::Object> v)
: tag(Tag::Object), is_intrusive_ptr(true) {
payload.as_intrusive_ptr = v.release();
}
inline IValue::IValue(c10::intrusive_ptr<ivalue::Future> v)
: tag(Tag::Future), is_intrusive_ptr(true) {
payload.as_intrusive_ptr = v.release();
}
inline const std::string& IValue::toStringRef() const {
return toString()->string();
}
template<typename T>
inline optional<T> IValue::toOptional() {
if (this->isNone()) {
return nullopt;
}
return this->to<T>();
}
inline bool IValue::isSameIdentity(const IValue& rhs) const {
// We choose to not use memcmp for payload check due to potential random padding characters on union type
// Semantics:
// 1. None is None, False is False, and True is True are all true
// 2. If it is a tensor type, we need to take undefined tensor into account
// 3. Undefined_tensor is None and vice versa should be true
// 4. If it is a reference type (i.e. is_intrusive_ptr), then is is True when the pointed-to object is the same.
// 5. False for all other comparisons.
if (this->isNone() && rhs.isNone()) {
return true;
} else if (this->isBool() && rhs.isBool()) {
// for bool type, do equality check
return this->toBool() == rhs.toBool();
} else if (this->isTensor() && rhs.isTensor()) {
// for tensor type, just check the as_intrusive_ptr since is_intrusive_ptr is false for undefined tensor
return this->payload.as_intrusive_ptr == rhs.payload.as_intrusive_ptr;
} else if (this->isTensor() && rhs.isNone()) {
// special case: undefined tensor and None are the same identity
return !this->is_intrusive_ptr;
} else if (this->isNone() && rhs.isTensor()) {
// special case: undefined tensor and None are the same identity
return !rhs.is_intrusive_ptr;
} else {
// for objects holding in IValue, do shallow compare on pointer address to testify the identity
return this->is_intrusive_ptr && rhs.is_intrusive_ptr
&& this->payload.as_intrusive_ptr == rhs.payload.as_intrusive_ptr;
}
}
} // namespace c10