IValue(Interpreter Value)
IValue(Interpreter Value)是一种boxing的数据类型,其中主要包含的成员变量为Payload,Tag和一个是否为指针的bool变量。其中Payload为Union类,真实的数据存储,Tag为一个枚举类。
代码位置:https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/core/ivalue.h
IValue中通过Tag完成对应数据类型判断,从而便于完成Payload数据的赋值拷贝或移动拷贝。
Payload payload;
Tag tag;
bool is_intrusive_ptr;
friend struct WeakIValue;
Payload类
主要包含一个union为TriviallyCopyablePayload和Tensor两个数据结构。借助于union存储的唯一性,使得Payload每次只存储单一的数据。
union Payload {
// We use a nested union here so that we can make the copy easy
// and efficient in the non-tensor (i.e., trivially copyable)
// case. Specifically, we do not have to do a switch-on-tag to
// figure out which union member to assign; we can just use
// TriviallyCopyablePayload::operator=.
union TriviallyCopyablePayload {
TriviallyCopyablePayload() : as_int(0) {}
int64_t as_int;
double as_double;
bool as_bool;
// Invariant: never nullptr; null state is represented as
// c10::UndefinedTensorImpl::singleton() for consistency of
// representation with Tensor.
c10::intrusive_ptr_target* as_intrusive_ptr;
struct {
DeviceType type;
DeviceIndex index;
} as_device;
} u;
at::Tensor as_tensor;
Payload() : u() {}
~Payload() {}
};
Tag枚举类
标识各数据类型
enum class Tag : uint32_t {
#define DEFINE_TAG(x) x,
TORCH_FORALL_TAGS(DEFINE_TAG)
#undef DEFINE_TAG
};
#define TORCH_FORALL_TAGS(_) \
_(None) \
_(Tensor) \
_(Storage) \
_(Double) \
_(ComplexDouble) \
_(Int) \
_(Bool) \
_(Tuple) \
_(String) \
_(Blob) \
_(GenericList) \
_(GenericDict) \
_(Future) \
_(Device) \
_(Stream) \
_(Object) \
_(PyObject) \
_(Uninitialized) \
_(Capsule) \
_(RRef) \
_(Quantizer) \
_(Generator) \
_(Enum)
(未完待续)