Struct IValue¶
Defined in File ivalue.h
Page Contents
Nested Relationships¶
Struct Documentation¶
-
struct IValue¶
IValue (Interpreter Value) is a tagged union over the types supported by the TorchScript interpreter.
IValues contain their values as an
IValue::Payload
, which holds primitive types (int64_t
,bool
,double
,Device
) andTensor
as values, and all other types as ac10::intrusive_ptr
. In order to optimize performance of the destructor and related operations by making theTensor
andc10::intrusive_ptr
paths generate the same code, we represent a nullc10::intrusive_ptr
asUndefinedTensorImpl::singleton()
, notnullptr
.IValues are used as inputs to and outputs from the TorchScript interpreter. To retrieve the value contained within an IValue, use the
.toX()
methods, whereX
is the type you are trying to get. Note that neither the.toX()
methods nor the templated.to<T>
functions do any kind of casting, they only unwrap the contained value. For example:// Make the IValue torch::IValue my_ivalue(26); std::cout << my_ivalue << "\n"; // Unwrap the IValue int64_t my_int = my_ivalue.toInt(); std::cout << my_int << "\n"; // This will throw an error! // `my_ivalue` is tagged as an int and cannot be used as another type torch::Tensor my_tensor = my_ivalue.toTensor();
Public Types
-
template<class T>
using enable_if_ivalue_constructible = std::enable_if_t<std::is_constructible_v<IValue, T>, std::nullptr_t>¶
-
template<class T>
using enable_if_list_is_ivalue_constructible = std::enable_if_t<std::is_constructible_v<IValue, T> && !std::is_same_v<T, c10::SymInt>, std::nullptr_t>¶
-
template<class T>
using enable_if_symint = std::enable_if_t<std::is_same_v<T, c10::SymInt>, std::nullptr_t>¶
-
template<class T>
using enable_if_ilist_is_ivalue_constructible = std::enable_if_t<std::is_constructible_v<IValue, T> && std::is_constructible_v<IValue, typename IListRef<T>::boxed_type> && !std::is_same_v<T, c10::SymInt>, std::nullptr_t>¶
-
using HashAliasedIValues = std::unordered_set<IValue, HashAliasedIValue, CompAliasedIValues>¶
-
using HashAliasedIValueMap = std::unordered_map<IValue, IValue, HashAliasedIValue, CompAliasedIValues>¶
-
using HashIdentityIValues = std::unordered_set<IValue, HashIdentityIValue, CompIdentityIValues>¶
-
using HashIdentityIValueMap = std::unordered_map<IValue, IValue, HashIdentityIValue, CompIdentityIValues>¶
Public Functions
-
void dump() const¶
-
IValue equals(const IValue &rhs) const¶
Equality comparison.
The semantics are the same as Python’s
==
:Numerical types are compared by value.
Tensors compute element-wise equality, returning a BoolTensor (see:
torch.eq()
)Strings are compared by value.
Sequence types (list, tuple) are compared lexicographically by comparing their elements. Different sequence types never compare equal.
Mappings (dict) must have equal (key, value) pairs.
If not listed above, the default behavior for is to test identity equality (e.g. pointer equality).
Why does this return an IValue instead of a bool? Because in PyTorch,
tensor1 == tensor2
returns aBoolTensor
, not a bool.NOTE: we (like Python) assume that identity equality implies value equality for efficiency. TODO: need to support customizing equality
-
bool is(const IValue &rhs) const¶
Identity comparison.
Checks if
this
is the same object asrhs
. The semantics are the same as Python’sis
operator.NOTE: Like in Python, this operation is poorly defined for primitive types like numbers and strings. Prefer to use
==
unless you really want to check identity equality.
-
inline IValue hash() const¶
Hashing for IValues.
Returns an IValue-boxed int.
Some notes:
Like eager, Tensors are hashed by looking at the pointer. This is not strictly correct because two value-equal tensors with different tensor pointers will hash differently, but we choose to reproduce the eager semantics.
Hashing is not defined on all built-in IValue types (e.g. list and dict), following Python. Calling
hash()
on these types will throw.
-
inline IValue(at::TensorBase t)¶
-
inline bool isTensor() const¶
-
inline at::TensorImpl *unsafeToTensorImpl() const¶
-
inline IValue(at::Storage s)¶
-
inline bool isStorage() const¶
-
c10::Storage toStorage() &&¶
-
c10::Storage toStorage() const &¶
-
inline bool isCapsule() const¶
-
c10::intrusive_ptr<torch::CustomClassHolder> toCapsule() &&¶
-
c10::intrusive_ptr<torch::CustomClassHolder> toCapsule() const &¶
-
template<typename T, std::enable_if_t<std::is_base_of_v<torch::CustomClassHolder, T>, int> = 0>
IValue(intrusive_ptr<T> custom_class)¶
-
bool isCustomClass() const¶
-
IValue(c10::intrusive_ptr<ivalue::Tuple> v)¶
-
template<typename ...Args, std::enable_if_t<!std::disjunction_v<std::is_lvalue_reference<Args>..., std::negation<std::is_constructible<IValue, Args>>...>, std::nullptr_t> = nullptr>
IValue(const std::tuple<Args...> &t)¶
-
template<typename ...Args, std::enable_if_t<!std::disjunction_v<std::is_lvalue_reference<Args>..., std::negation<std::is_constructible<IValue, Args>>...>, std::nullptr_t> = nullptr>
IValue(std::tuple<Args...> &&t)¶
-
inline bool isTuple() const¶
-
c10::intrusive_ptr<ivalue::Tuple> toTuple() &&¶
-
c10::intrusive_ptr<ivalue::Tuple> toTuple() const &¶
-
ivalue::Tuple &toTupleRef() const¶
-
inline IValue(double d)¶
-
inline bool isDouble() const¶
-
inline double toDouble() const¶
-
inline bool isComplexDouble() const¶
-
c10::complex<double> toComplexDouble() const¶
-
IValue(c10::intrusive_ptr<ivalue::Future> v)¶
-
inline bool isFuture() const¶
-
c10::intrusive_ptr<ivalue::Future> toFuture() &&¶
-
c10::intrusive_ptr<ivalue::Future> toFuture() const &¶
-
IValue(c10::intrusive_ptr<ivalue::Await> v)¶
-
inline bool isAwait() const¶
-
c10::intrusive_ptr<ivalue::Await> toAwait() &&¶
-
c10::intrusive_ptr<ivalue::Await> toAwait() const &¶
-
IValue(c10::intrusive_ptr<c10::RRefInterface> v)¶
-
inline bool isRRef() const¶
-
c10::intrusive_ptr<c10::RRefInterface> toRRef() &&¶
-
c10::intrusive_ptr<c10::RRefInterface> toRRef() const &¶
-
IValue(c10::intrusive_ptr<at::Quantizer> v)¶
-
inline bool isQuantizer() const¶
-
c10::intrusive_ptr<at::Quantizer> toQuantizer() &&¶
-
c10::intrusive_ptr<at::Quantizer> toQuantizer() const &¶
-
inline IValue(int64_t i)¶
-
inline IValue(const c10::SymInt &i)¶
-
inline bool isSymInt() const¶
-
c10::SymInt toSymInt() &&¶
-
c10::SymInt toSymInt() const &¶
-
inline IValue(const c10::SymFloat &i)¶
-
inline bool isSymFloat() const¶
-
c10::SymFloat toSymFloat() &&¶
-
c10::SymFloat toSymFloat() const &¶
-
inline IValue(const c10::SymBool &i)¶
-
inline bool isSymBool() const¶
-
c10::SymBool toSymBool() &&¶
-
c10::SymBool toSymBool() const &¶
-
inline IValue(int32_t i)¶
-
inline bool isInt() const¶
-
inline int64_t toInt() const¶
-
inline IValue(bool b)¶
-
inline bool isBool() const¶
-
inline bool toBool() const¶
-
bool isIntList() const¶
-
bool isSymIntList() const¶
-
std::vector<int64_t> toIntVector() const¶
-
std::vector<c10::SymInt> toSymIntVector() const¶
-
at::DimVector toDimVector() const¶
-
IValue(c10::intrusive_ptr<ivalue::ConstantString> v)¶
-
IValue(std::string v)¶
-
inline IValue(const char *v)¶
-
inline IValue(c10::string_view v)¶
-
inline IValue(std::string_view v)¶
-
inline bool isString() const¶
-
c10::intrusive_ptr<ivalue::ConstantString> toString() &&¶
-
c10::intrusive_ptr<ivalue::ConstantString> toString() const &¶
-
const std::string &toStringRef() const¶
-
std::optional<std::reference_wrapper<const std::string>> toOptionalStringRef() const¶
-
c10::string_view toStringView() const¶
-
bool isDoubleList() const¶
-
std::vector<double> toDoubleVector() const¶
-
bool isComplexDoubleList() const¶
-
std::vector<c10::complex<double>> toComplexDoubleVector() const¶
-
bool isBoolList() const¶
-
bool isTensorList() const¶
-
bool isOptionalTensorList() const¶
-
inline bool isList() const¶
-
template<class T, enable_if_list_is_ivalue_constructible<T> = nullptr>
IValue(c10::List<T> &&v)¶
-
template<class T, enable_if_list_is_ivalue_constructible<T> = nullptr>
IValue(const c10::List<T> &v)¶
-
template<class T, enable_if_list_is_ivalue_constructible<T> = nullptr>
IValue(at::ArrayRef<T> v)¶
-
template<class T, enable_if_list_is_ivalue_constructible<T> = nullptr>
IValue(const std::vector<T> &v)¶
-
template<class T, enable_if_list_is_ivalue_constructible<T> = nullptr>
IValue(std::vector<T> &&v)¶
-
template<class T, enable_if_symint<T> = nullptr>
IValue(at::ArrayRef<T> v)¶
-
template<class T, enable_if_symint<T> = nullptr>
IValue(at::OptionalArrayRef<T> v)¶
-
template<class T, enable_if_symint<T> = nullptr>
IValue(const std::vector<T> &v)¶
-
template<class T, enable_if_symint<T> = nullptr>
IValue(std::vector<T> &&v)¶
-
template<class T, enable_if_ilist_is_ivalue_constructible<T> = nullptr>
IValue(c10::IListRef<T> v)¶
-
inline bool isGenericDict() const¶
-
template<class T, enable_if_ivalue_constructible<T> = nullptr>
IValue(std::optional<T> v)¶
-
template<class T, enable_if_list_is_ivalue_constructible<T> = nullptr>
IValue(c10::OptionalArrayRef<T> v)¶
-
IValue(std::nullopt_t)¶
-
IValue(c10::intrusive_ptr<ivalue::Object> v)¶
-
inline bool isObject() const¶
-
c10::intrusive_ptr<ivalue::Object> toObject() &&¶
-
c10::intrusive_ptr<ivalue::Object> toObject() const &¶
-
ivalue::Object &toObjectRef() const¶
-
bool isModule() const¶
-
IValue(c10::intrusive_ptr<ivalue::PyObjectHolder> v)¶
-
inline bool isPyObject() const¶
-
c10::intrusive_ptr<ivalue::PyObjectHolder> toPyObjectHolder() &&¶
-
c10::intrusive_ptr<ivalue::PyObjectHolder> toPyObjectHolder() const &¶
-
PyObject *toPyObject() const¶
-
explicit IValue(c10::intrusive_ptr<ivalue::EnumHolder> v)¶
-
inline bool isEnum() const¶
-
c10::intrusive_ptr<ivalue::EnumHolder> toEnumHolder() &&¶
-
c10::intrusive_ptr<ivalue::EnumHolder> toEnumHolder() const &¶
-
IValue() = default¶
-
inline bool isNone() const¶
-
inline std::string toNone() const¶
-
inline IValue(const at::Scalar &s)¶
-
inline bool isScalar() const¶
-
inline at::Scalar toScalar() const¶
-
inline bool isDevice() const¶
-
inline IValue(c10::Stream s)¶
-
c10::Stream toStream() &&¶
-
c10::Stream toStream() const &¶
-
inline bool isStream() const¶
-
inline IValue(ScalarType t)¶
-
inline at::ScalarType toScalarType() const¶
-
inline IValue(Layout l)¶
-
inline at::Layout toLayout() const¶
-
inline IValue(MemoryFormat m)¶
-
inline at::MemoryFormat toMemoryFormat() const¶
-
inline IValue(at::QScheme qscheme)¶
-
inline at::QScheme toQScheme() const¶
-
inline IValue(at::Dimname dimname)¶
-
inline at::Dimname toDimname() const¶
-
inline IValue(at::Generator g)¶
-
inline bool isGenerator() const¶
-
at::Generator toGenerator() &&¶
-
at::Generator toGenerator() const &¶
-
inline std::string tagKind() const¶
-
std::ostream &repr(std::ostream &stream, std::function<bool(std::ostream&, const IValue &v)> customFormatter) const¶
-
inline bool isPtrType() const¶
-
template<typename T = c10::PlatformType>
TypePtr type() const¶
-
void getSubValues(HashAliasedIValues &subValues) const¶
-
IValue deepcopy(HashIdentityIValueMap &memo, std::optional<at::Device> device = std::nullopt) const¶
-
inline bool isIntrusivePtr() const¶
-
inline bool isIntrusivePtrLegacyBehavior() const¶
Public Static Functions
-
static inline IValue make_capsule(intrusive_ptr<torch::CustomClassHolder> blob)¶
Friends
- friend struct WeakIValue
-
struct CompAliasedIValues¶
-
struct CompIdentityIValues¶
-
struct HashAliasedIValue¶
-
struct HashIdentityIValue¶
-
union Payload¶
-
-
union TriviallyCopyablePayload¶
Public Functions
-
inline TriviallyCopyablePayload()¶
Public Members
-
int64_t as_int¶
-
double as_double¶
-
bool as_bool¶
-
c10::intrusive_ptr_target *as_intrusive_ptr¶
-
c10::DeviceType type¶
-
DeviceIndex index¶
-
struct c10::IValue::Payload::TriviallyCopyablePayload::[anonymous] as_device¶
-
inline TriviallyCopyablePayload()¶
-
union TriviallyCopyablePayload¶
-
template<typename T>
struct TagType¶
-
template<class T>