Title: Core Types — PyTorch main documentation
Description: Core types in PyTorch C++ — ArrayRef, optional, Dict, List, IListRef, Half, and IValue.
Keywords:
Domain: docs.pytorch.org
{
"@context": "https://schema.org",
"@type": "Article",
"name": "Core Types",
"headline": "Core Types",
"description": "Core types in PyTorch C++ \u2014 ArrayRef, optional, Dict, List, IListRef, Half, and IValue.",
"url": "/api/c10/types.html",
"articleBody": "Core Types# C10 provides fundamental types used throughout PyTorch. ArrayRef# template\u003ctypename T\u003eclass ArrayRef : public HeaderOnlyArrayRef\u003cT\u003e# ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory), i.e. a start pointer and a length. It allows various APIs to take consecutive elements easily and conveniently. This class does not own the underlying data, it is expected to be used in situations where the data resides in some other buffer, whose lifetime extends past that of the ArrayRef. For this reason, it is not in general safe to store an ArrayRef. This is intended to be trivially copyable, so it should be passed by value. NOTE: We have refactored out the headeronly parts of the ArrayRef struct into HeaderOnlyArrayRef. As adding virtual would change the performance of the underlying constexpr calls, we rely on apparent-type dispatch for inheritance. This should be fine because their memory format is the same, and it is never incorrect for ArrayRef to call HeaderOnlyArrayRef methods. However, you should prefer to use ArrayRef when possible, because its use of TORCH_CHECK will lead to better user-facing error messages. Constructors, all inherited from HeaderOnlyArrayRef except for SmallVector. As inherited constructors won\u2019t work with class template argument deduction (CTAD) until C++23, we add deduction guides after the class definition to enable CTAD. template\u003ctypename U\u003einline ArrayRef(const SmallVectorTemplateCommon\u003cT, U\u003e \u0026Vec)# Construct an ArrayRef from a SmallVector. This is templated in order to avoid instantiating SmallVectorTemplateCommon\u003cT\u003e whenever we copy-construct an ArrayRef. NOTE: this is the only constructor that is not inherited from HeaderOnlyArrayRef. Simple Operations, mostly inherited from HeaderOnlyArrayRef inline constexpr const T \u0026front() const# front - Get the first element. We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of STD_TORCH_CHECK inline constexpr const T \u0026back() const# back - Get the last element. We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of STD_TORCH_CHECK inline constexpr ArrayRef\u003cT\u003e slice(size_t N, size_t M) const# slice(n, m) - Take M elements of the array starting at element N We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of STD_TORCH_CHECK inline constexpr ArrayRef\u003cT\u003e slice(size_t N) const# slice(n) - Chop off the first N elements of the array. We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of STD_TORCH_CHECK Operator Overloads inline constexpr const T \u0026at(size_t Index) const# Vector compatibility We deviate from HeaderOnlyArrayRef by using TORCH_CHECK instead of STD_TORCH_CHECK. template\u003ctypename U\u003estd::enable_if_t\u003cstd::is_same_v\u003cU, T\u003e, ArrayRef\u003cT\u003e\u003e \u0026operator=(U \u0026\u0026Temporary) = delete# Disallow accidental assignment from a temporary. The declaration here is extra complicated so that \u201carrayRef = {}\u201d continues to select the move assignment operator. template\u003ctypename U\u003estd::enable_if_t\u003cstd::is_same_v\u003cU, T\u003e, ArrayRef\u003cT\u003e\u003e \u0026operator=(std::initializer_list\u003cU\u003e) = delete# Disallow accidental assignment from a temporary. The declaration here is extra complicated so that \u201carrayRef = {}\u201d continues to select the move assignment operator. Example: std::vector\u003cint64_t\u003e sizes = {3, 4, 5}; c10::ArrayRef\u003cint64_t\u003e sizes_ref(sizes); // Can also use initializer list auto tensor = at::zeros({3, 4, 5}); // implicitly converts OptionalArrayRef# template\u003ctypename T\u003eclass OptionalArrayRef Public Functions constexpr OptionalArrayRef() noexcept = default inline constexpr OptionalArrayRef(std::nullopt_t) noexcept OptionalArrayRef(const OptionalArrayRef \u0026other) = default OptionalArrayRef(OptionalArrayRef \u0026\u0026other) noexcept = default inline constexpr OptionalArrayRef(const std::optional\u003cArrayRef\u003cT\u003e\u003e \u0026other) noexcept inline constexpr OptionalArrayRef(std::optional\u003cArrayRef\u003cT\u003e\u003e \u0026\u0026other) noexcept inline constexpr OptionalArrayRef(const T \u0026value) noexcept template\u003ctypename U = ArrayRef\u003cT\u003e, std::enable_if_t\u003c!std::is_same_v\u003cstd::decay_t\u003cU\u003e, OptionalArrayRef\u003e \u0026\u0026 !std::is_same_v\u003cstd::decay_t\u003cU\u003e, std::in_place_t\u003e \u0026\u0026 std::is_constructible_v\u003cArrayRef\u003cT\u003e, U\u0026\u0026\u003e \u0026\u0026 std::is_convertible_v\u003cU\u0026\u0026, ArrayRef\u003cT\u003e\u003e \u0026\u0026 !std::is_convertible_v\u003cU\u0026\u0026, T\u003e, bool\u003e = false\u003einline constexpr OptionalArrayRef(U \u0026\u0026value) noexcept(std::is_nothrow_constructible_v\u003cArrayRef\u003cT\u003e, U\u0026\u0026\u003e) template\u003ctypename U = ArrayRef\u003cT\u003e, std::enable_if_t\u003c!std::is_same_v\u003cstd::decay_t\u003cU\u003e, OptionalArrayRef\u003e \u0026\u0026 !std::is_same_v\u003cstd::decay_t\u003cU\u003e, std::in_place_t\u003e \u0026\u0026 std::is_constructible_v\u003cArrayRef\u003cT\u003e, U\u0026\u0026\u003e \u0026\u0026 !std::is_convertible_v\u003cU\u0026\u0026, ArrayRef\u003cT\u003e\u003e, bool\u003e = false\u003einline explicit constexpr OptionalArrayRef(U \u0026\u0026value) noexcept(std::is_nothrow_constructible_v\u003cArrayRef\u003cT\u003e, U\u0026\u0026\u003e) template\u003ctypename ...Args\u003einline explicit constexpr OptionalArrayRef(std::in_place_t ip, Args\u0026\u0026... args) noexcept template\u003ctypename U, typename ...Args\u003einline explicit constexpr OptionalArrayRef(std::in_place_t ip, std::initializer_list\u003cU\u003e il, Args\u0026\u0026... args) inline constexpr OptionalArrayRef(const std::initializer_list\u003cT\u003e \u0026Vec) ~OptionalArrayRef() = default inline constexpr OptionalArrayRef \u0026operator=(std::nullopt_t) noexcept OptionalArrayRef \u0026operator=(const OptionalArrayRef \u0026other) = default OptionalArrayRef \u0026operator=(OptionalArrayRef \u0026\u0026other) noexcept = default inline constexpr OptionalArrayRef \u0026operator=(const std::optional\u003cArrayRef\u003cT\u003e\u003e \u0026other) noexcept inline constexpr OptionalArrayRef \u0026operator=(std::optional\u003cArrayRef\u003cT\u003e\u003e \u0026\u0026other) noexcept template\u003ctypename U = ArrayRef\u003cT\u003e, typename = std::enable_if_t\u003c!std::is_same_v\u003cstd::decay_t\u003cU\u003e, OptionalArrayRef\u003e \u0026\u0026 std::is_constructible_v\u003cArrayRef\u003cT\u003e, U\u0026\u0026\u003e \u0026\u0026 std::is_assignable_v\u003cArrayRef\u003cT\u003e\u0026, U\u0026\u0026\u003e\u003e\u003einline constexpr OptionalArrayRef \u0026operator=(U \u0026\u0026value) noexcept(std::is_nothrow_constructible_v\u003cArrayRef\u003cT\u003e, U\u0026\u0026\u003e \u0026\u0026 std::is_nothrow_assignable_v\u003cArrayRef\u003cT\u003e\u0026, U\u0026\u0026\u003e) inline constexpr ArrayRef\u003cT\u003e *operator-\u003e() noexcept inline constexpr const ArrayRef\u003cT\u003e *operator-\u003e() const noexcept inline constexpr ArrayRef\u003cT\u003e \u0026operator*() \u0026 noexcept inline constexpr const ArrayRef\u003cT\u003e \u0026operator*() const \u0026 noexcept inline constexpr ArrayRef\u003cT\u003e \u0026\u0026operator*() \u0026\u0026 noexcept inline constexpr const ArrayRef\u003cT\u003e \u0026\u0026operator*() const \u0026\u0026 noexcept inline explicit constexpr operator bool() const noexcept inline constexpr bool has_value() const noexcept inline constexpr ArrayRef\u003cT\u003e \u0026value() \u0026 inline constexpr const ArrayRef\u003cT\u003e \u0026value() const \u0026 inline constexpr ArrayRef\u003cT\u003e \u0026\u0026value() \u0026\u0026 inline constexpr const ArrayRef\u003cT\u003e \u0026\u0026value() const \u0026\u0026 template\u003ctypename U\u003einline constexpr std::enable_if_t\u003cstd::is_convertible_v\u003cU\u0026\u0026, ArrayRef\u003cT\u003e\u003e, ArrayRef\u003cT\u003e\u003e value_or(U \u0026\u0026default_value) const \u0026 template\u003ctypename U\u003einline constexpr std::enable_if_t\u003cstd::is_convertible_v\u003cU\u0026\u0026, ArrayRef\u003cT\u003e\u003e, ArrayRef\u003cT\u003e\u003e value_or(U \u0026\u0026default_value) \u0026\u0026 inline constexpr void swap(OptionalArrayRef \u0026other) noexcept inline constexpr void reset() noexcept template\u003ctypename ...Args\u003einline constexpr std::enable_if_t\u003cstd::is_constructible_v\u003cArrayRef\u003cT\u003e, Args\u0026\u0026...\u003e, ArrayRef\u003cT\u003e\u0026\u003e emplace(Args\u0026\u0026... args) noexcept(std::is_nothrow_constructible_v\u003cArrayRef\u003cT\u003e, Args\u0026\u0026...\u003e) template\u003ctypename U, typename ...Args\u003einline constexpr ArrayRef\u003cT\u003e \u0026emplace(std::initializer_list\u003cU\u003e il, Args\u0026\u0026... args) noexcept Friends inline friend bool operator==(OptionalArrayRef a1, ArrayRef\u003cT\u003e other) Example: void my_function(c10::OptionalArrayRef\u003cint64_t\u003e sizes = c10::nullopt) { if (sizes.has_value()) { for (auto s : sizes.value()) { // process sizes } } } Optional# class c10::optional# A wrapper type that may or may not contain a value. Similar to std::optional. bool has_value() const# Returns true if a value is present. T \u0026value()# Returns the contained value. Throws if empty. T value_or(T default_value) const# Returns the value if present, otherwise returns the default. Example: c10::optional\u003cint64_t\u003e maybe_dim = c10::nullopt; if (maybe_dim.has_value()) { std::cout \u003c\u003c \"Dim: \" \u003c\u003c maybe_dim.value() \u003c\u003c std::endl; } int64_t dim = maybe_dim.value_or(-1); // Returns -1 if empty Half# class c10::Half# 16-bit floating point type (IEEE 754 half-precision). Half(float value)# Construct from a float. operator float() const# Convert to float. Example: c10::Half h = 3.14f; float f = static_cast\u003cfloat\u003e(h); Containers# C10 provides container types that store IValue elements internally. These are pointer types: copies share the same underlying storage. Dict# An ordered hash map from Key to Value. Valid key types are int64_t, double, bool, std::string, and at::Tensor. template\u003cclass Key, class Value\u003eclass Dict# Example: #include \u003cATen/core/Dict.h\u003e c10::Dict\u003cstd::string, at::Tensor\u003e named_tensors; named_tensors.insert(\"weight\", torch::randn({3, 3})); named_tensors.insert(\"bias\", torch::zeros({3})); if (named_tensors.contains(\"weight\")) { at::Tensor w = named_tensors.at(\"weight\"); } for (const auto\u0026 entry : named_tensors) { std::cout \u003c\u003c entry.key() \u003c\u003c \": \" \u003c\u003c entry.value().sizes() \u003c\u003c std::endl; } List# A type-safe list container backed by IValue elements. template\u003cclass T\u003eclass List# Example: #include \u003cATen/core/List.h\u003e c10::List\u003cat::Tensor\u003e tensor_list; tensor_list.push_back(torch::randn({2, 3})); tensor_list.push_back(torch::zeros({2, 3})); at::Tensor first = tensor_list.get(0); std::cout \u003c\u003c \"List size: \" \u003c\u003c tensor_list.size() \u003c\u003c std::endl; c10::List\u003cint64_t\u003e int_list; int_list.push_back(1); int_list.push_back(2); int_list.push_back(3); IListRef# c10::IListRef\u003cT\u003e is a lightweight reference type that provides a unified interface over different list-like types (List\u003cT\u003e, ArrayRef\u003cT\u003e, std::vector\u003cT\u003e). It avoids copying when passing list arguments to operators. template\u003cclass T\u003eclass IListRef# Example: #include \u003cATen/core/IListRef.h\u003e // IListRef can wrap different underlying types std::vector\u003cat::Tensor\u003e vec = {torch::randn({2}), torch::randn({3})}; c10::IListRef\u003cat::Tensor\u003e ref(vec); for (const auto\u0026 t : ref) { std::cout \u003c\u003c t.sizes() \u003c\u003c std::endl; } IValue# c10::IValue (Interpreter Value) is a type-erased container used extensively for storing values of different types. It can hold tensors, scalars, lists, dictionaries, and other types. Note The full API documentation for IValue is complex due to its many type conversion methods. See the header file ATen/core/ivalue.h for complete details. Common methods: isTensor() / toTensor() - Check if tensor / convert to tensor isInt() / toInt() - Check if int / convert to int isDouble() / toDouble() - Check if double / convert to double isBool() / toBool() - Check if bool / convert to bool isString() / toString() - Check if string / convert to string isList() / toList() - Check if list / convert to list isGenericDict() / toGenericDict() - Check if dict / convert to dict isTuple() / toTuple() - Check if tuple / convert to tuple isNone() - Check if None/null Example: c10::IValue val = at::ones({2, 2}); if (val.isTensor()) { at::Tensor t = val.toTensor(); }",
"author": {
"@type": "Organization",
"name": "PyTorch Contributors",
"url": "https://pytorch.org"
},
"image": "https://pytorch.org/docs/stable/_static/img/pytorch_seo.png",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "/api/c10/types.html"
},
"datePublished": "2023-01-01T00:00:00Z",
"dateModified": "2023-01-01T00:00:00Z"
}
| docsearch:language | en |
| llm:site-type | documentation |
| llm:framework | PyTorch |
| llm:description | Core types in PyTorch C++ — ArrayRef, optional, Dict, List, IListRef, Half, and IValue. |
| llm:navigation-file | https://pytorch.org/docs/stable/llms.txt |
| llm:sitemap | https://pytorch.org/docs/stable/sitemap.xml |
| llm:version | main |
| llm:project | PyTorch |
| llm:page-type | documentation |
| og:image | https://docs.pytorch.org/docs/stable/_static/img/pytorch_seo.png |
| None | 3 |
Links:
Viewport: width=device-width, initial-scale=1