Struct IValue
Defined in File ivalue.h
Page Contents
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
-
inline IValue(const IValue &rhs)
-
inline IValue(IValue &&rhs) noexcept
-
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.
-
at::Tensor toTensor() &&
-
at::Tensor &toTensor() &
-
const at::Tensor &toTensor() const &
-
inline const IValue &toIValue() const
-
inline IValue &toIValue()
-
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)
-
template<typename T>
c10::intrusive_ptr<T> toCustomClass() &&
-
template<typename T>
c10::intrusive_ptr<T> toCustomClass() const &
-
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)
-
template<typename T>
IValue(c10::complex<T> c)
-
c10::List<int64_t> toIntList() &&
-
c10::List<int64_t> toIntList() const &
-
c10::List<c10::SymInt> toSymIntList() &&
-
c10::List<c10::SymInt> toSymIntList() const &
-
c10::List<double> toDoubleList() &&
-
c10::List<double> toDoubleList() const &
-
c10::List<c10::complex<double>> toComplexDoubleList() &&
-
c10::List<c10::complex<double>> toComplexDoubleList() const &
-
c10::List<bool> toBoolList() &&
-
c10::List<bool> toBoolList() const &
-
std::vector<at::Tensor> toTensorVector() const
-
std::vector<std::optional<at::Tensor>> toOptionalTensorVector() 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)
-
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)
-
torch::jit::Module toModule() const
-
inline IValue(c10::Device d)
-
inline c10::Device toDevice() const
-
template<typename T>
T to() &&
-
template<typename T>
c10::detail::ivalue_to_const_ref_overload_return<T>::type to() const &
-
template<typename T>
std::optional<T> toOptional()
-
template<typename T>
std::optional<T> toOptional() const
-
std::ostream &repr(std::ostream &stream, std::function<bool(std::ostream&, const IValue &v)> customFormatter) const
-
bool overlaps(const IValue &rhs) const
-
void getSubValues(HashAliasedIValues &subValues) const
-
void visit(const std::function<bool(const IValue&)> &visitor) const
-
IValue deepcopy(std::optional<at::Device> device = std::nullopt) const
-
IValue deepcopy(HashIdentityIValueMap &memo, std::optional<at::Device> device = std::nullopt) const
-
inline IValue(const Payload &p, Tag t)
Public Static Functions
-
static size_t hash(const IValue &iv)
-
static inline IValue make_capsule(intrusive_ptr<torch::CustomClassHolder> blob)
-
static inline IValue uninitialized()
Friends
- friend struct WeakIValue
-
friend bool operator==(const IValue &lhs, const IValue &rhs)
This implements the same semantics as
bool(lhs == rhs)
in Python.which is the same as
equals()
except for Tensor types.
-
friend std::ostream &operator<<(std::ostream &out, const IValue &v)
-
struct HashIdentityIValue
Public Functions
-
inline size_t operator()(const IValue &val) const
-
inline size_t operator()(const IValue &val) const
-
union Payload
-
-
union TriviallyCopyablePayload
-
Public Members
-
c10::DeviceType type
-
DeviceIndex index
-
struct c10::IValue::Payload::TriviallyCopyablePayload::[anonymous] as_device
-
c10::DeviceType type
-
union TriviallyCopyablePayload
-
template<class T>