Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/SparseTensorImpl.h +428 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/Storage.h +7 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/StorageUtils.h +54 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/Tensor.h +8 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorAccessor.h +7 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorGeometry.h +159 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorIndexing.h +772 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorIterator.h +1039 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorIteratorInternal.h +77 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorMeta.h +142 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorNames.h +80 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorOperators.h +56 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorOptions.h +7 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorSubclassLikeUtils.h +93 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorUtils.h +195 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/ThreadLocalPythonObjects.h +26 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/ThreadLocalState.h +131 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TracerMode.h +137 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TypeDefault.h +31 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/Utils.h +143 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/Version.h +23 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/VmapGeneratedPlumbing.h +0 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/WrapDimUtils.h +161 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/WrapDimUtilsMulti.h +49 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/autocast_mode.h +976 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/ceil_div.h +29 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/code_template.h +250 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/ATenGeneral.h +8 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/ATenOpList.h +18 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/ATen_fwd.h +51 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/ATen_pch.h +166 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Array.h +53 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Backtrace.h +7 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/CachingHostAllocator.h +800 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/CheckMemoryFormat.h +29 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/DeprecatedTypeProperties.h +144 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/DeprecatedTypePropertiesRegistry.h +38 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Dict.h +401 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Dict_inl.h +213 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/DimVector.h +18 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Dimname.h +53 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/DistributionsHelper.h +337 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Formatting.h +30 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Generator.h +194 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/GeneratorForPrivateuseone.h +44 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/IListRef.h +638 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/IListRef_inl.h +208 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/LegacyTypeDispatch.h +116 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/List.h +496 -0
- miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/List_inl.h +358 -0
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/SparseTensorImpl.h
ADDED
|
@@ -0,0 +1,428 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/Tensor.h>
|
| 5 |
+
#include <c10/core/TensorImpl.h>
|
| 6 |
+
#include <c10/core/impl/TorchDispatchModeTLS.h>
|
| 7 |
+
#include <c10/util/Exception.h>
|
| 8 |
+
#include <c10/util/irange.h>
|
| 9 |
+
|
| 10 |
+
#ifndef AT_PER_OPERATOR_HEADERS
|
| 11 |
+
#include <ATen/Functions.h>
|
| 12 |
+
#else
|
| 13 |
+
#include <ATen/ops/empty.h>
|
| 14 |
+
#include <ATen/ops/resize.h>
|
| 15 |
+
#endif
|
| 16 |
+
|
| 17 |
+
namespace at {
|
| 18 |
+
struct TORCH_API SparseTensorImpl : public TensorImpl {
|
| 19 |
+
// Stored in COO format, indices + values.
|
| 20 |
+
|
| 21 |
+
// INVARIANTS:
|
| 22 |
+
// sparse_dim: range [0, len(shape)]; sparse_dim + dense_dim = len(shape)
|
| 23 |
+
// dense_dim : range [0, len(shape)]; sparse_dim + dense_dim = len(shape)
|
| 24 |
+
// _indices.shape: dimensionality: 2, shape: (sparse_dim, nnz)
|
| 25 |
+
// _values.shape: dimensionality: 1 + dense_dim. shape: (nnz,
|
| 26 |
+
// shape[sparse_dim:])
|
| 27 |
+
|
| 28 |
+
int64_t sparse_dim_ = 0; // number of sparse dimensions
|
| 29 |
+
int64_t dense_dim_ = 0; // number of dense dimensions
|
| 30 |
+
|
| 31 |
+
Tensor indices_; // always a LongTensor
|
| 32 |
+
Tensor values_;
|
| 33 |
+
|
| 34 |
+
// A sparse tensor is 'coalesced' if every index occurs at most once in
|
| 35 |
+
// the indices tensor, and the indices are in sorted order. (This means
|
| 36 |
+
// that it is very easy to convert a coalesced tensor to CSR format: you
|
| 37 |
+
// need only compute CSR format indices.)
|
| 38 |
+
//
|
| 39 |
+
// Most math operations can only be performed on coalesced sparse tensors,
|
| 40 |
+
// because many algorithms proceed by merging two sorted lists (of indices).
|
| 41 |
+
bool coalesced_ = false;
|
| 42 |
+
|
| 43 |
+
// compute_numel with integer multiplication overflow check, see gh-57542
|
| 44 |
+
void refresh_numel() {
|
| 45 |
+
TensorImpl::safe_refresh_numel();
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
public:
|
| 49 |
+
// Public for now...
|
| 50 |
+
explicit SparseTensorImpl(
|
| 51 |
+
at::DispatchKeySet /*key_set*/,
|
| 52 |
+
const caffe2::TypeMeta /*data_type*/);
|
| 53 |
+
|
| 54 |
+
void release_resources() override;
|
| 55 |
+
|
| 56 |
+
int64_t nnz() const {
|
| 57 |
+
return values_.size(0);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
c10::SymInt sym_nnz() const {
|
| 61 |
+
return values_.sym_size(0);
|
| 62 |
+
}
|
| 63 |
+
int64_t sparse_dim() const {
|
| 64 |
+
return sparse_dim_;
|
| 65 |
+
}
|
| 66 |
+
int64_t dense_dim() const {
|
| 67 |
+
return dense_dim_;
|
| 68 |
+
}
|
| 69 |
+
bool coalesced() const {
|
| 70 |
+
return coalesced_;
|
| 71 |
+
}
|
| 72 |
+
Tensor indices() const {
|
| 73 |
+
return indices_;
|
| 74 |
+
}
|
| 75 |
+
Tensor values() const {
|
| 76 |
+
return values_;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
void set_size(int64_t dim, int64_t new_size) override;
|
| 80 |
+
void set_stride(int64_t dim, int64_t new_stride) override;
|
| 81 |
+
void set_storage_offset(int64_t storage_offset) override;
|
| 82 |
+
|
| 83 |
+
#ifdef DEBUG
|
| 84 |
+
bool has_storage() const override;
|
| 85 |
+
#endif
|
| 86 |
+
|
| 87 |
+
// WARNING: This function does NOT preserve invariants of sparse_dim/dense_dim
|
| 88 |
+
// with respect to indices and values
|
| 89 |
+
void raw_resize_(int64_t sparse_dim, int64_t dense_dim, IntArrayRef size) {
|
| 90 |
+
TORCH_CHECK(
|
| 91 |
+
allow_tensor_metadata_change(),
|
| 92 |
+
"raw_resize_ ",
|
| 93 |
+
err_msg_tensor_metadata_change_not_allowed);
|
| 94 |
+
TORCH_CHECK(
|
| 95 |
+
!has_symbolic_sizes_strides_,
|
| 96 |
+
"raw_resize_ called on tensor with symbolic shape")
|
| 97 |
+
set_sizes_and_strides(size, std::vector<int64_t>(size.size()));
|
| 98 |
+
sparse_dim_ = sparse_dim;
|
| 99 |
+
dense_dim_ = dense_dim;
|
| 100 |
+
refresh_numel();
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
// NOTE: This function preserves invariants of sparse_dim/dense_dim with
|
| 104 |
+
// respect to indices and values.
|
| 105 |
+
//
|
| 106 |
+
// NOTE: This function supports the following cases:
|
| 107 |
+
// 1. When we keep the number of dense dimensions unchanged, and NOT shrinking
|
| 108 |
+
// the size of any of the dense dimensions.
|
| 109 |
+
// 2. When we keep the number of sparse dimensions unchanged, and NOT
|
| 110 |
+
// shrinking the size of any of the sparse dimensions.
|
| 111 |
+
// 3. When the sparse tensor has zero nnz, in which case we are free to change
|
| 112 |
+
// the shapes of both its sparse and dense dimensions.
|
| 113 |
+
//
|
| 114 |
+
// This function DOESN'T support (and will throw an error) the following
|
| 115 |
+
// cases:
|
| 116 |
+
// 1. When we attempt to change the number of sparse dimensions on a non-empty
|
| 117 |
+
// sparse tensor (such an operation will invalidate the indices stored).
|
| 118 |
+
// 2. When we attempt to change the number of dense dimensions on a non-empty
|
| 119 |
+
// sparse tensor (such an operation will behave differently from an equivalent
|
| 120 |
+
// dense tensor's resize method, and for API consistency we don't support it).
|
| 121 |
+
// 3. When we attempt to shrink the size of any of the dense dimensions on a
|
| 122 |
+
// non-empty sparse tensor (such an operation will behave differently from an
|
| 123 |
+
// equivalent dense tensor's resize method, and for API consistency we don't
|
| 124 |
+
// support it).
|
| 125 |
+
// 4. When we attempt to shrink the size of any of the sparse dimensions on a
|
| 126 |
+
// non-empty sparse tensor (this could make some of the stored indices
|
| 127 |
+
// out-of-bound and thus unsafe).
|
| 128 |
+
template <typename T>
|
| 129 |
+
void _resize_(int64_t sparse_dim, int64_t dense_dim, ArrayRef<T> size) {
|
| 130 |
+
TORCH_CHECK(
|
| 131 |
+
allow_tensor_metadata_change(),
|
| 132 |
+
"resize_ ",
|
| 133 |
+
err_msg_tensor_metadata_change_not_allowed);
|
| 134 |
+
TORCH_CHECK(
|
| 135 |
+
!has_symbolic_sizes_strides_,
|
| 136 |
+
"resize_ called on tensor with symbolic shape")
|
| 137 |
+
TORCH_CHECK(
|
| 138 |
+
sparse_dim + dense_dim == static_cast<int64_t>(size.size()),
|
| 139 |
+
"'len(size) == sparse_dim + dense_dim' is not satisfied: len(size) = ",
|
| 140 |
+
size.size(),
|
| 141 |
+
", sparse_dim = ",
|
| 142 |
+
sparse_dim,
|
| 143 |
+
", dense_dim = ",
|
| 144 |
+
dense_dim);
|
| 145 |
+
if (nnz() > 0) {
|
| 146 |
+
[[maybe_unused]] auto constexpr alt_options_msg =
|
| 147 |
+
"You could try the following options:\n\
|
| 148 |
+
1. If you need an empty sparse tensor of this size, call `x = torch.sparse_coo_tensor(size)`.\n\
|
| 149 |
+
2. If you need to resize this tensor, you have the following options:\n\
|
| 150 |
+
1. For both sparse and dense dimensions, keep the number of them constant and the size of them non-shrinking, and then try the same call again.\n\
|
| 151 |
+
2. Or, create a new sparse tensor with the correct indices and values from this sparse tensor.";
|
| 152 |
+
|
| 153 |
+
TORCH_CHECK(
|
| 154 |
+
sparse_dim == sparse_dim_,
|
| 155 |
+
"changing the number of sparse dimensions (from ",
|
| 156 |
+
sparse_dim_,
|
| 157 |
+
" to ",
|
| 158 |
+
sparse_dim,
|
| 159 |
+
") on a non-empty sparse tensor is not supported.\n",
|
| 160 |
+
alt_options_msg);
|
| 161 |
+
|
| 162 |
+
TORCH_CHECK(
|
| 163 |
+
dense_dim == dense_dim_,
|
| 164 |
+
"changing the number of dense dimensions (from ",
|
| 165 |
+
dense_dim_,
|
| 166 |
+
" to ",
|
| 167 |
+
dense_dim,
|
| 168 |
+
") on a non-empty sparse tensor is not supported.\n",
|
| 169 |
+
alt_options_msg);
|
| 170 |
+
|
| 171 |
+
bool shrinking_sparse_dims = false;
|
| 172 |
+
bool shrinking_dense_dim = false;
|
| 173 |
+
auto sparse_size_original = generic_sizes<T>().slice(0, sparse_dim);
|
| 174 |
+
auto sparse_size_new = size.slice(0, sparse_dim);
|
| 175 |
+
for (const auto i : c10::irange(sparse_dim)) {
|
| 176 |
+
if (sparse_size_new[i] < sparse_size_original[i]) {
|
| 177 |
+
shrinking_sparse_dims = true;
|
| 178 |
+
break;
|
| 179 |
+
}
|
| 180 |
+
}
|
| 181 |
+
auto dense_size_original = generic_sizes<T>().slice(sparse_dim);
|
| 182 |
+
auto dense_size_new = size.slice(sparse_dim);
|
| 183 |
+
for (const auto i : c10::irange(dense_dim)) {
|
| 184 |
+
if (dense_size_new[i] < dense_size_original[i]) {
|
| 185 |
+
shrinking_dense_dim = true;
|
| 186 |
+
break;
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
TORCH_CHECK(
|
| 191 |
+
!shrinking_sparse_dims,
|
| 192 |
+
"shrinking the size of sparse dimensions (from ",
|
| 193 |
+
sparse_size_original,
|
| 194 |
+
" to ",
|
| 195 |
+
sparse_size_new,
|
| 196 |
+
") on a non-empty sparse tensor is not supported.\n",
|
| 197 |
+
alt_options_msg);
|
| 198 |
+
|
| 199 |
+
TORCH_CHECK(
|
| 200 |
+
!shrinking_dense_dim,
|
| 201 |
+
"shrinking the size of dense dimensions (from ",
|
| 202 |
+
dense_size_original,
|
| 203 |
+
" to ",
|
| 204 |
+
dense_size_new,
|
| 205 |
+
") on a non-empty sparse tensor is not supported.\n",
|
| 206 |
+
alt_options_msg);
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
auto sizes_and_strides = generic_sizes<T>();
|
| 210 |
+
const bool size_equals_sizes = std::equal(
|
| 211 |
+
size.begin(),
|
| 212 |
+
size.end(),
|
| 213 |
+
sizes_and_strides.begin(),
|
| 214 |
+
sizes_and_strides.end());
|
| 215 |
+
if ((!size_equals_sizes) || (sparse_dim != sparse_dim_) ||
|
| 216 |
+
(dense_dim != dense_dim_)) {
|
| 217 |
+
auto nnz = at::symint::sizes<T>(values())[0];
|
| 218 |
+
std::vector<T> values_size = {nnz};
|
| 219 |
+
auto dense_size = size.slice(sparse_dim);
|
| 220 |
+
values_size.insert(
|
| 221 |
+
values_size.end(), dense_size.begin(), dense_size.end());
|
| 222 |
+
at::symint::resize_<T>(values_, values_size);
|
| 223 |
+
at::symint::resize_<T>(indices_, {T(sparse_dim), nnz});
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
if (!size_equals_sizes) {
|
| 227 |
+
set_sizes_and_strides(size, std::vector<T>(size.size()));
|
| 228 |
+
}
|
| 229 |
+
sparse_dim_ = sparse_dim;
|
| 230 |
+
dense_dim_ = dense_dim;
|
| 231 |
+
refresh_numel();
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
void resize_(int64_t sparse_dim, int64_t dense_dim, ArrayRef<int64_t> size) {
|
| 235 |
+
_resize_(sparse_dim, dense_dim, size);
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
void resize_(
|
| 239 |
+
int64_t sparse_dim,
|
| 240 |
+
int64_t dense_dim,
|
| 241 |
+
ArrayRef<c10::SymInt> size) {
|
| 242 |
+
_resize_(sparse_dim, dense_dim, size);
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
// NOTE: this function will resize the sparse tensor and also set `indices`
|
| 246 |
+
// and `values` to empty.
|
| 247 |
+
void resize_and_clear_(
|
| 248 |
+
int64_t sparse_dim,
|
| 249 |
+
int64_t dense_dim,
|
| 250 |
+
IntArrayRef size) {
|
| 251 |
+
TORCH_CHECK(
|
| 252 |
+
allow_tensor_metadata_change(),
|
| 253 |
+
"resize_and_clear_ ",
|
| 254 |
+
err_msg_tensor_metadata_change_not_allowed);
|
| 255 |
+
TORCH_CHECK(
|
| 256 |
+
!has_symbolic_sizes_strides_,
|
| 257 |
+
"resize_and_clear_ called on tensor with symbolic shape")
|
| 258 |
+
TORCH_CHECK(
|
| 259 |
+
sparse_dim + dense_dim == static_cast<int64_t>(size.size()),
|
| 260 |
+
"'len(size) == sparse_dim + dense_dim' is not satisfied: len(size) = ",
|
| 261 |
+
size.size(),
|
| 262 |
+
", sparse_dim = ",
|
| 263 |
+
sparse_dim,
|
| 264 |
+
", dense_dim = ",
|
| 265 |
+
dense_dim);
|
| 266 |
+
|
| 267 |
+
set_sizes_and_strides(size, std::vector<int64_t>(size.size()));
|
| 268 |
+
sparse_dim_ = sparse_dim;
|
| 269 |
+
dense_dim_ = dense_dim;
|
| 270 |
+
|
| 271 |
+
auto empty_indices = at::empty({sparse_dim, 0}, indices().options());
|
| 272 |
+
std::vector<int64_t> values_size = {0};
|
| 273 |
+
auto dense_size = sizes().slice(sparse_dim);
|
| 274 |
+
values_size.insert(values_size.end(), dense_size.begin(), dense_size.end());
|
| 275 |
+
auto empty_values = at::empty(values_size, values().options());
|
| 276 |
+
set_indices_and_values_unsafe(empty_indices, empty_values);
|
| 277 |
+
refresh_numel();
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
void set_coalesced(bool coalesced) {
|
| 281 |
+
TORCH_CHECK(
|
| 282 |
+
allow_tensor_metadata_change(),
|
| 283 |
+
"set_coalesced ",
|
| 284 |
+
err_msg_tensor_metadata_change_not_allowed);
|
| 285 |
+
coalesced_ = coalesced;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
// NOTE: this function is only used internally and not exposed to Python
|
| 289 |
+
// frontend
|
| 290 |
+
void set_nnz_and_narrow(int64_t new_nnz) {
|
| 291 |
+
TORCH_CHECK(
|
| 292 |
+
allow_tensor_metadata_change(),
|
| 293 |
+
"set_nnz_and_narrow ",
|
| 294 |
+
err_msg_tensor_metadata_change_not_allowed);
|
| 295 |
+
AT_ASSERT(new_nnz <= nnz());
|
| 296 |
+
indices_ = indices_.narrow(1, 0, new_nnz);
|
| 297 |
+
values_ = values_.narrow(0, 0, new_nnz);
|
| 298 |
+
if (new_nnz < 2) {
|
| 299 |
+
coalesced_ = true;
|
| 300 |
+
}
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
// Takes indices and values and directly puts them into the sparse tensor, no
|
| 304 |
+
// copy. NOTE: this function is unsafe because it doesn't check whether any
|
| 305 |
+
// indices are out of boundaries of `sizes`, so it should ONLY be used where
|
| 306 |
+
// we know that the indices are guaranteed to be within bounds. This used to
|
| 307 |
+
// be called THSTensor_(_move) NB: This used to be able to avoid a refcount
|
| 308 |
+
// bump, but I was too lazy to make it happen
|
| 309 |
+
void set_indices_and_values_unsafe(
|
| 310 |
+
const Tensor& indices,
|
| 311 |
+
const Tensor& values);
|
| 312 |
+
|
| 313 |
+
template <typename VariableVersion>
|
| 314 |
+
c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach_core(
|
| 315 |
+
VariableVersion&& version_counter,
|
| 316 |
+
bool allow_tensor_metadata_change) const {
|
| 317 |
+
const auto mode_stack_len = c10::impl::TorchDispatchModeTLS::stack_len();
|
| 318 |
+
c10::impl::PyInterpreter&& interpreter = nullptr;
|
| 319 |
+
if (mode_stack_len > 0 &&
|
| 320 |
+
!c10::impl::tls_is_dispatch_key_excluded(DispatchKey::Python)) {
|
| 321 |
+
const auto& cur_torch_dispatch_mode_state =
|
| 322 |
+
c10::impl::TorchDispatchModeTLS::get_stack_at(mode_stack_len - 1);
|
| 323 |
+
interpreter = cur_torch_dispatch_mode_state->pyinterpreter();
|
| 324 |
+
} else if (
|
| 325 |
+
key_set_.has(DispatchKey::Python) &&
|
| 326 |
+
!c10::impl::tls_is_dispatch_key_excluded(DispatchKey::Python)) {
|
| 327 |
+
interpreter = pyobj_slot_.load_pyobj_interpreter();
|
| 328 |
+
} else {
|
| 329 |
+
// otherwise just copy the SparseTensorImpl and not the PyObject.
|
| 330 |
+
auto impl = c10::make_intrusive<SparseTensorImpl>(key_set(), dtype());
|
| 331 |
+
copy_tensor_metadata(
|
| 332 |
+
/*src_sparse_impl=*/this,
|
| 333 |
+
/*dest_sparse_impl=*/impl.get(),
|
| 334 |
+
/*version_counter=*/version_counter,
|
| 335 |
+
/*allow_tensor_metadata_change=*/allow_tensor_metadata_change);
|
| 336 |
+
impl->refresh_numel();
|
| 337 |
+
return impl;
|
| 338 |
+
}
|
| 339 |
+
auto r = interpreter->detach(this);
|
| 340 |
+
r->set_version_counter(std::forward<VariableVersion>(version_counter));
|
| 341 |
+
r->set_allow_tensor_metadata_change(allow_tensor_metadata_change);
|
| 342 |
+
return r;
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
/**
|
| 346 |
+
* Return a TensorImpl that is a shallow-copy of this TensorImpl.
|
| 347 |
+
*
|
| 348 |
+
* For usage of `version_counter` and `allow_tensor_metadata_change`,
|
| 349 |
+
* see NOTE [ TensorImpl Shallow-Copying ].
|
| 350 |
+
*/
|
| 351 |
+
c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach(
|
| 352 |
+
const c10::VariableVersion& version_counter,
|
| 353 |
+
bool allow_tensor_metadata_change) const override {
|
| 354 |
+
return shallow_copy_and_detach_core(
|
| 355 |
+
version_counter, allow_tensor_metadata_change);
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
/**
|
| 359 |
+
* Return a TensorImpl that is a shallow-copy of this TensorImpl.
|
| 360 |
+
*
|
| 361 |
+
* For usage of `version_counter` and `allow_tensor_metadata_change`,
|
| 362 |
+
* see NOTE [ TensorImpl Shallow-Copying ].
|
| 363 |
+
*/
|
| 364 |
+
c10::intrusive_ptr<TensorImpl> shallow_copy_and_detach(
|
| 365 |
+
c10::VariableVersion&& version_counter,
|
| 366 |
+
bool allow_tensor_metadata_change) const override {
|
| 367 |
+
return shallow_copy_and_detach_core(
|
| 368 |
+
std::move(version_counter), allow_tensor_metadata_change);
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
/**
|
| 372 |
+
* Shallow-copies data from another TensorImpl into this TensorImpl.
|
| 373 |
+
*
|
| 374 |
+
* For why this function doesn't check this TensorImpl's
|
| 375 |
+
* `allow_tensor_metadata_change_`, see NOTE [ TensorImpl Shallow-Copying ].
|
| 376 |
+
*/
|
| 377 |
+
void shallow_copy_from(const c10::intrusive_ptr<TensorImpl>& impl) override {
|
| 378 |
+
AT_ASSERT(has_compatible_shallow_copy_type(impl->key_set()));
|
| 379 |
+
auto sparse_impl = static_cast<const SparseTensorImpl*>(impl.get());
|
| 380 |
+
copy_tensor_metadata(
|
| 381 |
+
/*src_sparse_impl=*/sparse_impl,
|
| 382 |
+
/*dest_sparse_impl=*/this,
|
| 383 |
+
/*version_counter=*/version_counter(),
|
| 384 |
+
/*allow_tensor_metadata_change=*/allow_tensor_metadata_change());
|
| 385 |
+
refresh_numel();
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
private:
|
| 389 |
+
explicit SparseTensorImpl(
|
| 390 |
+
at::DispatchKeySet /*key_set*/,
|
| 391 |
+
const caffe2::TypeMeta /*data_type*/,
|
| 392 |
+
at::Tensor indices,
|
| 393 |
+
at::Tensor values);
|
| 394 |
+
|
| 395 |
+
/**
|
| 396 |
+
* Copy the tensor metadata fields (e.g. sizes / strides / storage pointer /
|
| 397 |
+
* storage_offset) from one TensorImpl to another TensorImpl.
|
| 398 |
+
*
|
| 399 |
+
* For usage of `version_counter` and `allow_tensor_metadata_change`, see NOTE
|
| 400 |
+
* [ TensorImpl Shallow-Copying ].
|
| 401 |
+
*/
|
| 402 |
+
static void copy_tensor_metadata(
|
| 403 |
+
const SparseTensorImpl* src_sparse_impl,
|
| 404 |
+
SparseTensorImpl* dest_sparse_impl,
|
| 405 |
+
c10::VariableVersion version_counter,
|
| 406 |
+
bool allow_tensor_metadata_change) {
|
| 407 |
+
TensorImpl::copy_tensor_metadata(
|
| 408 |
+
src_sparse_impl,
|
| 409 |
+
dest_sparse_impl,
|
| 410 |
+
std::move(version_counter),
|
| 411 |
+
allow_tensor_metadata_change);
|
| 412 |
+
|
| 413 |
+
// Sparse-specific fields
|
| 414 |
+
dest_sparse_impl->sparse_dim_ = src_sparse_impl->sparse_dim();
|
| 415 |
+
dest_sparse_impl->dense_dim_ = src_sparse_impl->dense_dim();
|
| 416 |
+
dest_sparse_impl->indices_ = src_sparse_impl->indices();
|
| 417 |
+
dest_sparse_impl->values_ = src_sparse_impl->values();
|
| 418 |
+
dest_sparse_impl->coalesced_ = src_sparse_impl->coalesced();
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
const char* tensorimpl_type_name() const override;
|
| 422 |
+
};
|
| 423 |
+
|
| 424 |
+
} // namespace at
|
| 425 |
+
|
| 426 |
+
#else
|
| 427 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 428 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/Storage.h
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <c10/core/Storage.h>
|
| 4 |
+
|
| 5 |
+
#else
|
| 6 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 7 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/StorageUtils.h
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/Storage.h>
|
| 5 |
+
#include <c10/core/StorageImpl.h>
|
| 6 |
+
#include <c10/util/intrusive_ptr.h>
|
| 7 |
+
|
| 8 |
+
namespace at {
|
| 9 |
+
|
| 10 |
+
class TensorBase;
|
| 11 |
+
|
| 12 |
+
// Here we define a series of utils to create/manipulate ATen backed
|
| 13 |
+
// c10 storage implementations.
|
| 14 |
+
|
| 15 |
+
/**
|
| 16 |
+
* Create a new shared memory storage impl managed by file descriptor
|
| 17 |
+
*
|
| 18 |
+
* @param size size in bytes
|
| 19 |
+
*/
|
| 20 |
+
C10_EXPORT c10::intrusive_ptr<c10::StorageImpl> new_shm_fd_storage(size_t size);
|
| 21 |
+
|
| 22 |
+
/**
|
| 23 |
+
* Copy src to dst
|
| 24 |
+
* Caller must guarantee the validness of the storage objects
|
| 25 |
+
* during the entire copy process, esp. when it's async.
|
| 26 |
+
*
|
| 27 |
+
* This can probably live in c10 namespace later if needed,
|
| 28 |
+
* but for now keep it in at to keep implementation simple.
|
| 29 |
+
*
|
| 30 |
+
* @param dst dst tensor
|
| 31 |
+
* @param src src tensor
|
| 32 |
+
* @param non_blocking (default false) whether this operation blocks caller
|
| 33 |
+
*/
|
| 34 |
+
C10_EXPORT void storage_copy(
|
| 35 |
+
c10::Storage& dst,
|
| 36 |
+
const c10::Storage& src,
|
| 37 |
+
bool non_blocking = false);
|
| 38 |
+
|
| 39 |
+
/**
|
| 40 |
+
* In place change the storage to shm based.
|
| 41 |
+
*
|
| 42 |
+
* This is only applicable to CPU tensors not already shared.
|
| 43 |
+
* Otherwise, it's a no op to mirror the THP tensor behavior:
|
| 44 |
+
* https://pytorch.org/docs/stable/generated/torch.Tensor.share_memory_.html
|
| 45 |
+
*
|
| 46 |
+
* @param t a tensor
|
| 47 |
+
*/
|
| 48 |
+
C10_EXPORT void share_memory_(TensorBase& t);
|
| 49 |
+
|
| 50 |
+
} // namespace at
|
| 51 |
+
|
| 52 |
+
#else
|
| 53 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 54 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/Tensor.h
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/Tensor.h>
|
| 5 |
+
|
| 6 |
+
#else
|
| 7 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 8 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorAccessor.h
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <ATen/core/TensorAccessor.h>
|
| 4 |
+
|
| 5 |
+
#else
|
| 6 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 7 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorGeometry.h
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/TensorBase.h>
|
| 5 |
+
#include <c10/core/WrapDimMinimal.h>
|
| 6 |
+
|
| 7 |
+
namespace at {
|
| 8 |
+
|
| 9 |
+
// Return if the tensor geometry represented by `sizes` and `strides` is
|
| 10 |
+
// contiguous Although we cache is_contiguous in tensor now, this is till useful
|
| 11 |
+
// because it allows checking if a particular geometry is contiguous without
|
| 12 |
+
// explicitly constructing a tensor, e.g., when you want to choose a kernel
|
| 13 |
+
// strategy based on whether a subgeometry is contiguous.
|
| 14 |
+
TORCH_API bool geometry_is_contiguous(IntArrayRef sizes, IntArrayRef strides);
|
| 15 |
+
|
| 16 |
+
struct TORCH_API TensorGeometry {
|
| 17 |
+
TensorGeometry() = default;
|
| 18 |
+
|
| 19 |
+
explicit TensorGeometry(c10::SymIntArrayRef sizes)
|
| 20 |
+
: sizes_(sizes.vec()),
|
| 21 |
+
strides_(sizes.size()),
|
| 22 |
+
has_symbolic_sizes_strides_(
|
| 23 |
+
!c10::asIntArrayRefSlowOpt(sizes).has_value()) {
|
| 24 |
+
int64_t dim = static_cast<int64_t>(sizes.size());
|
| 25 |
+
c10::SymInt expected_stride = 1;
|
| 26 |
+
for (int64_t i = dim - 1; i >= 0; i--) {
|
| 27 |
+
strides_[i] = expected_stride;
|
| 28 |
+
expected_stride *= sizes_[i];
|
| 29 |
+
}
|
| 30 |
+
numel_ = expected_stride;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
explicit TensorGeometry(const TensorBase& t)
|
| 34 |
+
: sizes_(t.sym_sizes().vec()),
|
| 35 |
+
strides_(t.sym_strides().vec()),
|
| 36 |
+
storage_offset_(t.sym_storage_offset()),
|
| 37 |
+
numel_(t.sym_numel()),
|
| 38 |
+
has_symbolic_sizes_strides_(
|
| 39 |
+
t.unsafeGetTensorImpl()->has_symbolic_sizes_strides()) {}
|
| 40 |
+
|
| 41 |
+
explicit TensorGeometry(
|
| 42 |
+
std::vector<at::SymInt> sizes,
|
| 43 |
+
std::vector<at::SymInt> strides,
|
| 44 |
+
at::SymInt storage_offset)
|
| 45 |
+
: sizes_(std::move(sizes)),
|
| 46 |
+
strides_(std::move(strides)),
|
| 47 |
+
storage_offset_(std::move(storage_offset)) {
|
| 48 |
+
recompute();
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// true if the tensor is contiguous
|
| 52 |
+
bool is_contiguous() const;
|
| 53 |
+
|
| 54 |
+
int64_t dim() const {
|
| 55 |
+
return static_cast<int64_t>(sizes_.size());
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
int64_t size(int64_t dim) const {
|
| 59 |
+
TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
|
| 60 |
+
dim = c10::maybe_wrap_dim(dim, this->dim());
|
| 61 |
+
return sizes_.at(static_cast<size_t>(dim)).as_int_unchecked();
|
| 62 |
+
}
|
| 63 |
+
c10::IntArrayRef sizes() const {
|
| 64 |
+
TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
|
| 65 |
+
return c10::asIntArrayRefUnchecked(sizes_);
|
| 66 |
+
}
|
| 67 |
+
int64_t stride(int64_t dim) const {
|
| 68 |
+
TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
|
| 69 |
+
dim = c10::maybe_wrap_dim(dim, this->dim());
|
| 70 |
+
return strides_.at(static_cast<size_t>(dim)).as_int_unchecked();
|
| 71 |
+
}
|
| 72 |
+
c10::IntArrayRef strides() const {
|
| 73 |
+
TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
|
| 74 |
+
return c10::asIntArrayRefUnchecked(strides_);
|
| 75 |
+
}
|
| 76 |
+
int64_t storage_offset() const {
|
| 77 |
+
TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
|
| 78 |
+
return storage_offset_.as_int_unchecked();
|
| 79 |
+
}
|
| 80 |
+
int64_t numel() const {
|
| 81 |
+
TORCH_INTERNAL_ASSERT(!has_symbolic_sizes_strides_);
|
| 82 |
+
return numel_.as_int_unchecked();
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
c10::SymInt sym_size(int64_t dim) const {
|
| 86 |
+
dim = c10::maybe_wrap_dim(dim, this->dim());
|
| 87 |
+
return sizes_.at(static_cast<size_t>(dim));
|
| 88 |
+
}
|
| 89 |
+
c10::SymIntArrayRef sym_sizes() const {
|
| 90 |
+
return sizes_;
|
| 91 |
+
}
|
| 92 |
+
c10::SymInt sym_stride(int64_t dim) const {
|
| 93 |
+
dim = c10::maybe_wrap_dim(dim, this->dim());
|
| 94 |
+
return strides_.at(static_cast<size_t>(dim));
|
| 95 |
+
}
|
| 96 |
+
c10::SymIntArrayRef sym_strides() const {
|
| 97 |
+
return strides_;
|
| 98 |
+
}
|
| 99 |
+
c10::SymInt sym_storage_offset() const {
|
| 100 |
+
return storage_offset_;
|
| 101 |
+
}
|
| 102 |
+
c10::SymInt sym_numel() const {
|
| 103 |
+
return numel_;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
TensorGeometry transpose(int64_t dim0, int64_t dim1) {
|
| 107 |
+
TensorGeometry r = *this; // copy
|
| 108 |
+
TORCH_CHECK(
|
| 109 |
+
dim0 < dim(),
|
| 110 |
+
"transpose: dim0=",
|
| 111 |
+
dim0,
|
| 112 |
+
" out of range (dim=",
|
| 113 |
+
dim(),
|
| 114 |
+
")")
|
| 115 |
+
TORCH_CHECK(
|
| 116 |
+
dim1 < dim(),
|
| 117 |
+
"transpose: dim1=",
|
| 118 |
+
dim1,
|
| 119 |
+
" out of range (dim=",
|
| 120 |
+
dim(),
|
| 121 |
+
")")
|
| 122 |
+
std::swap(r.sizes_[dim0], r.sizes_[dim1]);
|
| 123 |
+
std::swap(r.strides_[dim0], r.strides_[dim1]);
|
| 124 |
+
return r;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
std::vector<c10::SymInt>& mutable_sizes() {
|
| 128 |
+
return sizes_;
|
| 129 |
+
}
|
| 130 |
+
std::vector<c10::SymInt>& mutable_strides() {
|
| 131 |
+
return strides_;
|
| 132 |
+
}
|
| 133 |
+
c10::SymInt& mutable_storage_offset() {
|
| 134 |
+
return storage_offset_;
|
| 135 |
+
}
|
| 136 |
+
void recompute() {
|
| 137 |
+
// recalculate numel after a change
|
| 138 |
+
c10::SymInt numel = 1;
|
| 139 |
+
for (const auto& i : sizes_) {
|
| 140 |
+
numel = numel * i;
|
| 141 |
+
}
|
| 142 |
+
numel_ = std::move(numel);
|
| 143 |
+
has_symbolic_sizes_strides_ =
|
| 144 |
+
!c10::asIntArrayRefSlowOpt(sizes_).has_value();
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
private:
|
| 148 |
+
std::vector<c10::SymInt> sizes_;
|
| 149 |
+
std::vector<c10::SymInt> strides_;
|
| 150 |
+
c10::SymInt storage_offset_;
|
| 151 |
+
c10::SymInt numel_;
|
| 152 |
+
bool has_symbolic_sizes_strides_{false};
|
| 153 |
+
};
|
| 154 |
+
|
| 155 |
+
} // namespace at
|
| 156 |
+
|
| 157 |
+
#else
|
| 158 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 159 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorIndexing.h
ADDED
|
@@ -0,0 +1,772 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ExpandUtils.h>
|
| 5 |
+
#include <ATen/ScalarOps.h>
|
| 6 |
+
#include <ATen/core/Tensor.h>
|
| 7 |
+
#include <ATen/core/TensorBody.h>
|
| 8 |
+
#include <c10/core/SymInt.h>
|
| 9 |
+
#include <c10/util/irange.h>
|
| 10 |
+
#include <optional>
|
| 11 |
+
|
| 12 |
+
#ifndef AT_PER_OPERATOR_HEADERS
|
| 13 |
+
#include <ATen/Functions.h>
|
| 14 |
+
#include <ATen/NativeFunctions.h>
|
| 15 |
+
#else
|
| 16 |
+
#include <ATen/ops/alias.h>
|
| 17 |
+
#include <ATen/ops/empty.h>
|
| 18 |
+
#include <ATen/ops/scalar_tensor.h>
|
| 19 |
+
#include <ATen/ops/zeros.h>
|
| 20 |
+
#endif
|
| 21 |
+
|
| 22 |
+
#include <ATen/core/List.h>
|
| 23 |
+
|
| 24 |
+
#include <utility>
|
| 25 |
+
|
| 26 |
+
namespace at::indexing {
|
| 27 |
+
|
| 28 |
+
constexpr int64_t INDEX_MIN = c10::SymInt::min_representable_int();
|
| 29 |
+
constexpr int64_t INDEX_MAX = -(INDEX_MIN + 1);
|
| 30 |
+
|
| 31 |
+
enum class TensorIndexType { None, Ellipsis, SymInt, Boolean, Slice, Tensor };
|
| 32 |
+
|
| 33 |
+
constexpr std::nullopt_t None = std::nullopt;
|
| 34 |
+
|
| 35 |
+
struct TORCH_API EllipsisIndexType final {
|
| 36 |
+
EllipsisIndexType() = default;
|
| 37 |
+
};
|
| 38 |
+
TORCH_API extern const EllipsisIndexType Ellipsis;
|
| 39 |
+
|
| 40 |
+
struct TORCH_API Slice final {
|
| 41 |
+
public:
|
| 42 |
+
Slice(
|
| 43 |
+
std::optional<c10::SymInt> start_index = std::nullopt,
|
| 44 |
+
std::optional<c10::SymInt> stop_index = std::nullopt,
|
| 45 |
+
std::optional<c10::SymInt> step_index = std::nullopt) {
|
| 46 |
+
if (!step_index.has_value()) {
|
| 47 |
+
step_ = c10::SymInt(1);
|
| 48 |
+
} else {
|
| 49 |
+
step_ = std::move(step_index).value();
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
TORCH_CHECK_VALUE(
|
| 53 |
+
step_.sym_ne(0).expect_true(__FILE__, __LINE__),
|
| 54 |
+
"slice step cannot be zero");
|
| 55 |
+
|
| 56 |
+
if (!start_index.has_value()) {
|
| 57 |
+
start_ = c10::SymInt(step_ < 0 ? INDEX_MAX : 0);
|
| 58 |
+
} else {
|
| 59 |
+
start_ = std::move(start_index).value();
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
if (!stop_index.has_value()) {
|
| 63 |
+
stop_ = c10::SymInt(step_ < 0 ? INDEX_MIN : INDEX_MAX);
|
| 64 |
+
} else {
|
| 65 |
+
stop_ = std::move(stop_index).value();
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
inline c10::SymInt start() const {
|
| 70 |
+
return start_;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
inline c10::SymInt stop() const {
|
| 74 |
+
return stop_;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
inline c10::SymInt step() const {
|
| 78 |
+
return step_;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
private:
|
| 82 |
+
c10::SymInt start_;
|
| 83 |
+
c10::SymInt stop_;
|
| 84 |
+
c10::SymInt step_;
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
TORCH_API std::ostream& operator<<(std::ostream& stream, const Slice& slice);
|
| 88 |
+
|
| 89 |
+
// `at::indexing::TensorIndex` is used for converting C++ tensor indices such as
|
| 90 |
+
// `{None, "...", Ellipsis, 0, true, Slice(1, None, 2), torch::tensor({1, 2})}`
|
| 91 |
+
// into its equivalent `std::vector<TensorIndex>`, so that further tensor
|
| 92 |
+
// indexing operations can be performed using the supplied indices.
|
| 93 |
+
//
|
| 94 |
+
// There is one-to-one correspondence between Python and C++ tensor index types:
|
| 95 |
+
// Python | C++
|
| 96 |
+
// -----------------------------------------------------
|
| 97 |
+
// `None` | `at::indexing::None`
|
| 98 |
+
// `Ellipsis` | `at::indexing::Ellipsis`
|
| 99 |
+
// `...` | `"..."`
|
| 100 |
+
// `123` | `123`
|
| 101 |
+
// `True` / `False` | `true` / `false`
|
| 102 |
+
// `:` | `Slice()` / `Slice(None, None)`
|
| 103 |
+
// `::` | `Slice()` / `Slice(None, None, None)`
|
| 104 |
+
// `1:` | `Slice(1, None)`
|
| 105 |
+
// `1::` | `Slice(1, None, None)`
|
| 106 |
+
// `:3` | `Slice(None, 3)`
|
| 107 |
+
// `:3:` | `Slice(None, 3, None)`
|
| 108 |
+
// `::2` | `Slice(None, None, 2)`
|
| 109 |
+
// `1:3` | `Slice(1, 3)`
|
| 110 |
+
// `1::2` | `Slice(1, None, 2)`
|
| 111 |
+
// `:3:2` | `Slice(None, 3, 2)`
|
| 112 |
+
// `1:3:2` | `Slice(1, 3, 2)`
|
| 113 |
+
// `torch.tensor([1, 2])`) | `torch::tensor({1, 2})`
|
| 114 |
+
struct TORCH_API TensorIndex final {
|
| 115 |
+
// Case 1: `at::indexing::None`
|
| 116 |
+
TensorIndex(std::nullopt_t /*unused*/) : type_(TensorIndexType::None) {}
|
| 117 |
+
|
| 118 |
+
// Case 2: "..." / `at::indexing::Ellipsis`
|
| 119 |
+
TensorIndex(at::indexing::EllipsisIndexType /*unused*/)
|
| 120 |
+
: type_(TensorIndexType::Ellipsis) {}
|
| 121 |
+
TensorIndex(const char* str) : TensorIndex(at::indexing::Ellipsis) {
|
| 122 |
+
TORCH_CHECK_VALUE(
|
| 123 |
+
strcmp(str, "...") == 0,
|
| 124 |
+
"Expected \"...\" to represent an ellipsis index, but got \"",
|
| 125 |
+
str,
|
| 126 |
+
"\"");
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// Case 3: (Sym) Integer value
|
| 130 |
+
TensorIndex(SymInt integer)
|
| 131 |
+
: integer_(std::move(integer)), type_(TensorIndexType::SymInt) {}
|
| 132 |
+
TensorIndex(int64_t integer) : TensorIndex(SymInt(integer)) {}
|
| 133 |
+
TensorIndex(int integer) : TensorIndex(SymInt(integer)) {}
|
| 134 |
+
|
| 135 |
+
// Case 4: Boolean value
|
| 136 |
+
template <class T, class = std::enable_if_t<std::is_same_v<bool, T>>>
|
| 137 |
+
TensorIndex(T boolean) : boolean_(boolean), type_(TensorIndexType::Boolean) {}
|
| 138 |
+
|
| 139 |
+
// Case 5: Slice represented in `at::indexing::Slice` form
|
| 140 |
+
TensorIndex(Slice slice)
|
| 141 |
+
: slice_(std::move(slice)), type_(TensorIndexType::Slice) {}
|
| 142 |
+
|
| 143 |
+
// Case 6: Tensor value
|
| 144 |
+
TensorIndex(Tensor tensor)
|
| 145 |
+
: tensor_(std::move(tensor)), type_(TensorIndexType::Tensor) {}
|
| 146 |
+
|
| 147 |
+
inline bool is_none() const {
|
| 148 |
+
return type_ == TensorIndexType::None;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
inline bool is_ellipsis() const {
|
| 152 |
+
return type_ == TensorIndexType::Ellipsis;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
inline bool is_integer() const {
|
| 156 |
+
return type_ == TensorIndexType::SymInt;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
inline SymInt integer() const {
|
| 160 |
+
return integer_;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
inline bool is_boolean() const {
|
| 164 |
+
return type_ == TensorIndexType::Boolean;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
inline bool boolean() const {
|
| 168 |
+
return boolean_;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
inline bool is_slice() const {
|
| 172 |
+
return type_ == TensorIndexType::Slice;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
inline const Slice& slice() const {
|
| 176 |
+
return slice_;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
inline bool is_tensor() const {
|
| 180 |
+
return type_ == TensorIndexType::Tensor;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
inline const Tensor& tensor() const {
|
| 184 |
+
return tensor_;
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
private:
|
| 188 |
+
SymInt integer_ = 0;
|
| 189 |
+
bool boolean_ = false;
|
| 190 |
+
Slice slice_;
|
| 191 |
+
Tensor tensor_;
|
| 192 |
+
TensorIndexType type_;
|
| 193 |
+
};
|
| 194 |
+
|
| 195 |
+
TORCH_API std::ostream& operator<<(
|
| 196 |
+
std::ostream& stream,
|
| 197 |
+
const TensorIndex& tensor_index);
|
| 198 |
+
TORCH_API std::ostream& operator<<(
|
| 199 |
+
std::ostream& stream,
|
| 200 |
+
const std::vector<TensorIndex>& tensor_indices);
|
| 201 |
+
|
| 202 |
+
namespace impl {
|
| 203 |
+
inline Tensor applySlice(
|
| 204 |
+
const Tensor& self,
|
| 205 |
+
int64_t dim,
|
| 206 |
+
c10::SymInt start,
|
| 207 |
+
c10::SymInt stop,
|
| 208 |
+
c10::SymInt step,
|
| 209 |
+
bool disable_slice_optimization,
|
| 210 |
+
const at::Device& self_device,
|
| 211 |
+
const std::optional<SymIntArrayRef>& self_sizes) {
|
| 212 |
+
// TODO: implement negative step
|
| 213 |
+
TORCH_CHECK_VALUE(
|
| 214 |
+
step.sym_gt(0).expect_true(__FILE__, __LINE__),
|
| 215 |
+
"step must be greater than zero");
|
| 216 |
+
|
| 217 |
+
// See NOTE [nested tensor size for indexing]
|
| 218 |
+
if (self_sizes.has_value() && !self_sizes.value().empty()) {
|
| 219 |
+
// Skip this optimization if we are tracing, as the trace may be polymorphic
|
| 220 |
+
// over the shape of the `self` tensor, and we still want to record
|
| 221 |
+
// the slice.
|
| 222 |
+
SymInt length = (self_device == at::kCPU || self_device == at::kCUDA)
|
| 223 |
+
? (*self_sizes)[dim]
|
| 224 |
+
: self.sym_size(dim);
|
| 225 |
+
if (!disable_slice_optimization &&
|
| 226 |
+
TORCH_STATICALLY_KNOWN_TRUE(start.sym_eq(0)) &&
|
| 227 |
+
TORCH_STATICALLY_KNOWN_TRUE(length.sym_le(stop)) && step == 1) {
|
| 228 |
+
return self;
|
| 229 |
+
}
|
| 230 |
+
}
|
| 231 |
+
return self.slice_symint(
|
| 232 |
+
dim, std::move(start), std::move(stop), std::move(step));
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
inline Tensor applySelect(
|
| 236 |
+
const Tensor& self,
|
| 237 |
+
int64_t dim,
|
| 238 |
+
SymInt index,
|
| 239 |
+
int64_t real_dim,
|
| 240 |
+
const at::Device& /*self_device*/,
|
| 241 |
+
const std::optional<SymIntArrayRef>& self_sizes) {
|
| 242 |
+
// See NOTE [nested tensor size for indexing]
|
| 243 |
+
if (self_sizes.has_value()) {
|
| 244 |
+
auto maybe_index = index.maybe_as_int();
|
| 245 |
+
if (maybe_index.has_value()) {
|
| 246 |
+
TORCH_CHECK_INDEX(
|
| 247 |
+
!(maybe_index.value() == 0 && dim == 0 && self_sizes->empty()),
|
| 248 |
+
"invalid index of a 0-dim tensor. ",
|
| 249 |
+
"Use `tensor.item()` in Python or `tensor.item<T>()` in C++ to convert a 0-dim tensor to a number");
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
auto size = (*self_sizes)[dim];
|
| 253 |
+
// Note: `size >= -index` is not equivalent to `size > -1 - index` if index
|
| 254 |
+
// is INT64_MIN For std::numeric_limits<int64_t>::min() result of unary
|
| 255 |
+
// minus is undefined by the standard but in practice is equal to self. On
|
| 256 |
+
// the other hand, indexing wrapping is valid for all negative int64_t
|
| 257 |
+
// values, as x[INT64_MIN] is the same as x[INT64_MAX]
|
| 258 |
+
TORCH_CHECK_INDEX(
|
| 259 |
+
size.sym_gt(-1 - index)
|
| 260 |
+
.sym_and(size.sym_gt(index))
|
| 261 |
+
.expect_true(__FILE__, __LINE__),
|
| 262 |
+
"index ",
|
| 263 |
+
index,
|
| 264 |
+
" is out of bounds for dimension ",
|
| 265 |
+
real_dim,
|
| 266 |
+
" with size ",
|
| 267 |
+
size);
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
// if the index is negative, do not normalize it because that would fix the
|
| 271 |
+
// index on the current tensor size in the tracer. aten::select also works on
|
| 272 |
+
// negative indices
|
| 273 |
+
return self.select_symint(dim, std::move(index));
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
inline Tensor boolToIndexingTensorCPUOrCUDA(const Tensor& self, bool value) {
|
| 277 |
+
// booleans add a dimension of size 1. true indexes this dimension as if 0:,
|
| 278 |
+
// false as empty.
|
| 279 |
+
if (value) {
|
| 280 |
+
return at::empty({1}, self.options().dtype(kLong)).fill_(0.);
|
| 281 |
+
} else {
|
| 282 |
+
return at::empty({0}, self.options().dtype(kLong));
|
| 283 |
+
}
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
inline Tensor boolToIndexingTensorNonNativeDeviceType(
|
| 287 |
+
const Tensor& self,
|
| 288 |
+
bool value) {
|
| 289 |
+
// booleans add a dimension of size 1. true indexes this dimension as if 0:,
|
| 290 |
+
// false as empty.
|
| 291 |
+
if (value) {
|
| 292 |
+
return at::zeros({1}, self.options().dtype(kLong));
|
| 293 |
+
} else {
|
| 294 |
+
return at::empty({0}, self.options().dtype(kLong));
|
| 295 |
+
}
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
inline Tensor boolToIndexingTensor(
|
| 299 |
+
const Tensor& self,
|
| 300 |
+
bool value,
|
| 301 |
+
const at::Device& self_device) {
|
| 302 |
+
if (self_device == at::kCPU || self_device == at::kCUDA) {
|
| 303 |
+
return boolToIndexingTensorCPUOrCUDA(self, value);
|
| 304 |
+
} else {
|
| 305 |
+
return boolToIndexingTensorNonNativeDeviceType(self, value);
|
| 306 |
+
}
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
inline Tensor scalarToTensorNonNativeDeviceType(
|
| 310 |
+
const Scalar& v,
|
| 311 |
+
const TensorOptions& options) {
|
| 312 |
+
return at::scalar_tensor(v, options);
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
inline void recordTensorIndex(
|
| 316 |
+
const Tensor& tensor,
|
| 317 |
+
std::vector<Tensor>& outIndices,
|
| 318 |
+
int64_t* dim_ptr) {
|
| 319 |
+
if (outIndices.empty()) {
|
| 320 |
+
outIndices.resize(*dim_ptr + 1);
|
| 321 |
+
outIndices[*dim_ptr] = tensor;
|
| 322 |
+
} else {
|
| 323 |
+
outIndices.push_back(tensor);
|
| 324 |
+
}
|
| 325 |
+
if (tensor.scalar_type() == kByte || tensor.scalar_type() == kBool) {
|
| 326 |
+
*dim_ptr += tensor.dim();
|
| 327 |
+
} else {
|
| 328 |
+
*dim_ptr += 1;
|
| 329 |
+
}
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
inline c10::List<::std::optional<Tensor>> typeConvertIndices(
|
| 333 |
+
const Tensor& /*self*/,
|
| 334 |
+
std::vector<Tensor>&& indices) {
|
| 335 |
+
c10::List<::std::optional<Tensor>> converted_inds;
|
| 336 |
+
converted_inds.reserve(indices.size());
|
| 337 |
+
for (auto&& i : std::move(indices)) {
|
| 338 |
+
converted_inds.push_back(std::move(i));
|
| 339 |
+
}
|
| 340 |
+
return converted_inds;
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
// NOTE: Why do we mirror instead of replace the `count_specified_dimensions`
|
| 344 |
+
// function in torch/csrc/autograd/python_variable_indexing.cpp? It's because
|
| 345 |
+
// `count_specified_dimensions` is on the hot path of Python tensor multi-dim
|
| 346 |
+
// indexing (i.e. it's called by `applySlicing` which is called by
|
| 347 |
+
// `THPVariable_getitem` / `THPVariable_setitem` when handling indexing of more
|
| 348 |
+
// than one dimension). If we were to merge the Python/C++
|
| 349 |
+
// `count_specified_dimensions` function, on the Python side we would have to
|
| 350 |
+
// construct a `std::vector` container to be consumed by the C++
|
| 351 |
+
// `count_specified_dimensions` function, which adds 100s of nanoseconds
|
| 352 |
+
// overhead and is undesirable.
|
| 353 |
+
inline int64_t count_specified_dimensions(
|
| 354 |
+
const ArrayRef<TensorIndex>& indices) {
|
| 355 |
+
// Count the number of indexed dimensions (everything but ellipsis and None)
|
| 356 |
+
int64_t count = 0;
|
| 357 |
+
for (auto& obj : indices) {
|
| 358 |
+
if (obj.is_tensor()) {
|
| 359 |
+
auto& tensor = obj.tensor();
|
| 360 |
+
if (tensor.scalar_type() == kByte || tensor.scalar_type() == kBool) {
|
| 361 |
+
count += tensor.dim();
|
| 362 |
+
} else {
|
| 363 |
+
count++;
|
| 364 |
+
}
|
| 365 |
+
} else if (!obj.is_none() && !obj.is_ellipsis() && !obj.is_boolean()) {
|
| 366 |
+
count++;
|
| 367 |
+
}
|
| 368 |
+
}
|
| 369 |
+
return count;
|
| 370 |
+
}
|
| 371 |
+
} // namespace impl
|
| 372 |
+
|
| 373 |
+
// NOTE: Many functions below are only for consumption from Python indexing
|
| 374 |
+
// implementation, they include:
|
| 375 |
+
//
|
| 376 |
+
// - `Tensor scalarToTensor(...)`
|
| 377 |
+
// - `IntArrayRef slicePrefix1sSize(...)`
|
| 378 |
+
// - `void copy_to(...)`
|
| 379 |
+
// - `Tensor handleDimInMultiDimIndexing(...)`
|
| 380 |
+
// - `Tensor dispatch_index(...)`
|
| 381 |
+
// - `Tensor dispatch_index_put_(...)`
|
| 382 |
+
// - `Tensor get_item(...)`
|
| 383 |
+
// - `void set_item(...)`
|
| 384 |
+
//
|
| 385 |
+
// The rest of the functions are in `at::indexing::impl` namespace, signifying
|
| 386 |
+
// that they shouldn't be used from Python indexing implementation.
|
| 387 |
+
inline Tensor scalarToTensor(
|
| 388 |
+
const Scalar& v,
|
| 389 |
+
const TensorOptions& options,
|
| 390 |
+
const at::Device& self_device) {
|
| 391 |
+
if (self_device == at::kCPU && !v.isSymbolic()) {
|
| 392 |
+
return at::detail::scalar_tensor_static(
|
| 393 |
+
v,
|
| 394 |
+
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
|
| 395 |
+
options.dtype_opt()->toScalarType(),
|
| 396 |
+
self_device);
|
| 397 |
+
} else {
|
| 398 |
+
return impl::scalarToTensorNonNativeDeviceType(v, options);
|
| 399 |
+
}
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
// To match numpy semantics:
|
| 403 |
+
// As a special case for backwards compatibility,
|
| 404 |
+
// strip away unit dimensions from the left of 'src'
|
| 405 |
+
inline SymIntArrayRef slicePrefix1sSize(const SymIntArrayRef& sizes) {
|
| 406 |
+
size_t first_non1_src = sizes.size();
|
| 407 |
+
for (const auto i : c10::irange(sizes.size())) {
|
| 408 |
+
// Unbacked SymInt has different behavior, but this is sound because
|
| 409 |
+
// failing to slice will only ever cause an error, not divergent
|
| 410 |
+
// behavior
|
| 411 |
+
if (!sizes[i].has_hint() || sizes[i] != 1) {
|
| 412 |
+
first_non1_src = i;
|
| 413 |
+
break;
|
| 414 |
+
}
|
| 415 |
+
}
|
| 416 |
+
|
| 417 |
+
return sizes.slice(first_non1_src);
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
inline void copy_to(const Tensor& dst, const Tensor& src) {
|
| 421 |
+
if (dst.sym_sizes().equals(src.sym_sizes())) {
|
| 422 |
+
// A shortcut to avoid generating hard-coded constant sizes during tracing.
|
| 423 |
+
// This is not a perfect solution: when src & dst have different shapes,
|
| 424 |
+
// constants will still appear. Users can workaround that case by
|
| 425 |
+
// dst[index..] = src.reshape(..)
|
| 426 |
+
dst.copy_(src);
|
| 427 |
+
return;
|
| 428 |
+
} else if (src.dim() == 0 && src.device().type() == at::kCPU) {
|
| 429 |
+
dst.fill_(src);
|
| 430 |
+
return;
|
| 431 |
+
}
|
| 432 |
+
auto src_view = src.view_symint(slicePrefix1sSize(src.sym_sizes()));
|
| 433 |
+
c10::MaybeOwned<Tensor> b_src = expand_inplace(dst, src_view, "setitem");
|
| 434 |
+
dst.copy_(*b_src);
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
// See NOTE [ Setting `disable_slice_optimization` when calling C++ tensor
|
| 438 |
+
// indexing functions from Python ]
|
| 439 |
+
inline Tensor handleDimInMultiDimIndexing(
|
| 440 |
+
const Tensor& prev_dim_result,
|
| 441 |
+
const Tensor& original_tensor,
|
| 442 |
+
const TensorIndex& index,
|
| 443 |
+
int64_t* dim_ptr,
|
| 444 |
+
int64_t* specified_dims_ptr,
|
| 445 |
+
int64_t real_dim,
|
| 446 |
+
std::vector<Tensor>& outIndices,
|
| 447 |
+
bool disable_slice_optimization,
|
| 448 |
+
const at::Device& original_tensor_device,
|
| 449 |
+
const std::optional<SymIntArrayRef>& prev_dim_result_sizes) {
|
| 450 |
+
if (index.is_integer()) {
|
| 451 |
+
return impl::applySelect(
|
| 452 |
+
prev_dim_result,
|
| 453 |
+
*dim_ptr,
|
| 454 |
+
index.integer(),
|
| 455 |
+
real_dim,
|
| 456 |
+
original_tensor_device,
|
| 457 |
+
prev_dim_result_sizes);
|
| 458 |
+
} else if (index.is_slice()) {
|
| 459 |
+
Tensor result = impl::applySlice(
|
| 460 |
+
prev_dim_result,
|
| 461 |
+
*dim_ptr,
|
| 462 |
+
index.slice().start(),
|
| 463 |
+
index.slice().stop(),
|
| 464 |
+
index.slice().step(),
|
| 465 |
+
/*disable_slice_optimization=*/disable_slice_optimization,
|
| 466 |
+
original_tensor_device,
|
| 467 |
+
prev_dim_result_sizes);
|
| 468 |
+
(*dim_ptr)++;
|
| 469 |
+
if (!outIndices.empty()) {
|
| 470 |
+
outIndices.resize(outIndices.size() + 1);
|
| 471 |
+
}
|
| 472 |
+
return result;
|
| 473 |
+
} else if (index.is_ellipsis()) {
|
| 474 |
+
auto ellipsis_ndims = original_tensor.dim() - *specified_dims_ptr;
|
| 475 |
+
(*dim_ptr) += ellipsis_ndims;
|
| 476 |
+
if (!outIndices.empty()) {
|
| 477 |
+
outIndices.resize(outIndices.size() + ellipsis_ndims);
|
| 478 |
+
}
|
| 479 |
+
return prev_dim_result;
|
| 480 |
+
} else if (index.is_none()) {
|
| 481 |
+
Tensor result = prev_dim_result.unsqueeze(*dim_ptr);
|
| 482 |
+
(*dim_ptr)++;
|
| 483 |
+
if (!outIndices.empty()) {
|
| 484 |
+
outIndices.resize(outIndices.size() + 1);
|
| 485 |
+
}
|
| 486 |
+
return result;
|
| 487 |
+
} else if (index.is_boolean()) {
|
| 488 |
+
Tensor result = prev_dim_result.unsqueeze(*dim_ptr);
|
| 489 |
+
impl::recordTensorIndex(
|
| 490 |
+
impl::boolToIndexingTensor(
|
| 491 |
+
result, index.boolean(), original_tensor_device),
|
| 492 |
+
outIndices,
|
| 493 |
+
dim_ptr);
|
| 494 |
+
return result;
|
| 495 |
+
} else if (index.is_tensor()) {
|
| 496 |
+
Tensor result = prev_dim_result;
|
| 497 |
+
const Tensor& tensor = index.tensor();
|
| 498 |
+
auto scalar_type = tensor.scalar_type();
|
| 499 |
+
if (tensor.dim() == 0 &&
|
| 500 |
+
at::isIntegralType(scalar_type, /*includeBool=*/true)) {
|
| 501 |
+
if (scalar_type != at::kByte && scalar_type != at::kBool) {
|
| 502 |
+
result = impl::applySelect(
|
| 503 |
+
result,
|
| 504 |
+
*dim_ptr,
|
| 505 |
+
tensor.item<int64_t>(),
|
| 506 |
+
real_dim,
|
| 507 |
+
original_tensor_device,
|
| 508 |
+
prev_dim_result_sizes);
|
| 509 |
+
} else {
|
| 510 |
+
result = result.unsqueeze(*dim_ptr);
|
| 511 |
+
if (scalar_type == at::kBool) {
|
| 512 |
+
impl::recordTensorIndex(
|
| 513 |
+
impl::boolToIndexingTensor(
|
| 514 |
+
result, tensor.item<bool>() != 0, original_tensor_device),
|
| 515 |
+
outIndices,
|
| 516 |
+
dim_ptr);
|
| 517 |
+
} else {
|
| 518 |
+
impl::recordTensorIndex(
|
| 519 |
+
impl::boolToIndexingTensor(
|
| 520 |
+
result, tensor.item<uint8_t>() != 0, original_tensor_device),
|
| 521 |
+
outIndices,
|
| 522 |
+
dim_ptr);
|
| 523 |
+
}
|
| 524 |
+
}
|
| 525 |
+
} else {
|
| 526 |
+
impl::recordTensorIndex(tensor, outIndices, dim_ptr);
|
| 527 |
+
}
|
| 528 |
+
return result;
|
| 529 |
+
} else {
|
| 530 |
+
TORCH_INTERNAL_ASSERT(false, "Invalid TensorIndex type");
|
| 531 |
+
}
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
namespace impl {
|
| 535 |
+
// This mirrors `applySlicing` in
|
| 536 |
+
// torch/csrc/autograd/python_variable_indexing.cpp
|
| 537 |
+
inline Tensor applySlicing(
|
| 538 |
+
const Tensor& self,
|
| 539 |
+
const ArrayRef<TensorIndex>& indices,
|
| 540 |
+
std::vector<Tensor>& outIndices,
|
| 541 |
+
bool disable_slice_optimization,
|
| 542 |
+
const at::Device& self_device,
|
| 543 |
+
const std::optional<SymIntArrayRef>& self_sizes) {
|
| 544 |
+
int64_t dim = 0;
|
| 545 |
+
int64_t specified_dims = impl::count_specified_dimensions(indices);
|
| 546 |
+
|
| 547 |
+
// See NOTE [nested tensor size for indexing]
|
| 548 |
+
if (self_sizes.has_value()) {
|
| 549 |
+
TORCH_CHECK_INDEX(
|
| 550 |
+
specified_dims <= (int64_t)self_sizes->size(),
|
| 551 |
+
"too many indices for tensor of dimension ",
|
| 552 |
+
(int)self_sizes->size());
|
| 553 |
+
}
|
| 554 |
+
|
| 555 |
+
Tensor result = self;
|
| 556 |
+
for (const auto i : c10::irange(indices.size())) {
|
| 557 |
+
auto& obj = indices[i];
|
| 558 |
+
// See NOTE [nested tensor size for indexing]
|
| 559 |
+
std::optional<SymIntArrayRef> result_sizes = result.is_nested()
|
| 560 |
+
? std::optional<SymIntArrayRef>(std::nullopt)
|
| 561 |
+
: std::optional<SymIntArrayRef>(result.sym_sizes());
|
| 562 |
+
result = handleDimInMultiDimIndexing(
|
| 563 |
+
/*prev_dim_result=*/result,
|
| 564 |
+
/*original_tensor=*/self,
|
| 565 |
+
/*index=*/obj,
|
| 566 |
+
/*dim_ptr=*/&dim,
|
| 567 |
+
/*specified_dims_ptr=*/&specified_dims,
|
| 568 |
+
/*real_dim=*/static_cast<int64_t>(i),
|
| 569 |
+
/*outIndices=*/outIndices,
|
| 570 |
+
/*disable_slice_optimization=*/disable_slice_optimization,
|
| 571 |
+
/*original_tensor_device=*/self_device,
|
| 572 |
+
/*prev_dim_result_sizes=*/result_sizes);
|
| 573 |
+
}
|
| 574 |
+
return result;
|
| 575 |
+
}
|
| 576 |
+
} // namespace impl
|
| 577 |
+
|
| 578 |
+
inline Tensor dispatch_index(
|
| 579 |
+
const Tensor& self,
|
| 580 |
+
std::vector<Tensor>&& indices) {
|
| 581 |
+
// Remove trailing null elements from indices
|
| 582 |
+
while (!indices.empty() && !indices.back().defined()) {
|
| 583 |
+
indices.pop_back();
|
| 584 |
+
}
|
| 585 |
+
return self.index(impl::typeConvertIndices(self, std::move(indices)));
|
| 586 |
+
}
|
| 587 |
+
|
| 588 |
+
inline Tensor dispatch_index_put_(
|
| 589 |
+
Tensor& self,
|
| 590 |
+
std::vector<Tensor>&& indices,
|
| 591 |
+
const Tensor& value) {
|
| 592 |
+
// Remove trailing null elements from indices
|
| 593 |
+
while (!indices.empty() && !indices.back().defined()) {
|
| 594 |
+
indices.pop_back();
|
| 595 |
+
}
|
| 596 |
+
return self.index_put_(
|
| 597 |
+
impl::typeConvertIndices(self, std::move(indices)), value);
|
| 598 |
+
}
|
| 599 |
+
|
| 600 |
+
// NOTE [ Setting `disable_slice_optimization` when calling C++ tensor indexing
|
| 601 |
+
// functions from Python ]
|
| 602 |
+
//
|
| 603 |
+
// Question: When should we set `disable_slice_optimization` to `true` when
|
| 604 |
+
// calling C++ tensor indexing functions from Python indexing code?
|
| 605 |
+
//
|
| 606 |
+
// Answer: What "slice optimization" means: when we have a slicing expression
|
| 607 |
+
// like `x[0:5, 0]`, where the sliced tensor was of size 5 in dimension 0, we
|
| 608 |
+
// would skip dispatching the actual slice call as an optimization. However,
|
| 609 |
+
// here are the cases where we DON'T want this optimization:
|
| 610 |
+
//
|
| 611 |
+
// 1. When we are doing 1-D slicing (e.g. `tensor[:]`).
|
| 612 |
+
// Reason: we always return a shallow copy for expressions such as
|
| 613 |
+
// `tensor[:]` / `tensor[...]` / `tensor[:, :]`. (Note that for `tensor[:,
|
| 614 |
+
// :]`, we return an alias of `tensor` by doing the following:
|
| 615 |
+
// ```
|
| 616 |
+
// Tensor sliced = impl::applySlicing(self, indices, tensorIndices,
|
| 617 |
+
// disable_slice_optimization, self_device, self_sizes); if
|
| 618 |
+
// (tensorIndices.empty()) {
|
| 619 |
+
// if (sliced.is_same(self)) {
|
| 620 |
+
// // ensure we return a shallow copy for things like x[...]
|
| 621 |
+
// sliced = at::alias(sliced);
|
| 622 |
+
// }
|
| 623 |
+
// return sliced;
|
| 624 |
+
// }
|
| 625 |
+
// ```)
|
| 626 |
+
// 2. When we are doing JIT tracing.
|
| 627 |
+
// Reason: JIT tracing needs the `self.slice(...)` call to properly trace the
|
| 628 |
+
// slice operation.
|
| 629 |
+
|
| 630 |
+
// This mirrors `THPVariable_getitem` in
|
| 631 |
+
// torch/csrc/autograd/python_variable_indexing.cpp See NOTE [ Setting
|
| 632 |
+
// `disable_slice_optimization` when calling C++ tensor indexing functions from
|
| 633 |
+
// Python ]
|
| 634 |
+
inline Tensor get_item(
|
| 635 |
+
const Tensor& self,
|
| 636 |
+
const ArrayRef<TensorIndex>& indices,
|
| 637 |
+
bool disable_slice_optimization = false) {
|
| 638 |
+
at::Device self_device = self.device();
|
| 639 |
+
// NOTE [nested tensor size for indexing]
|
| 640 |
+
// nested tensor does not have a size (yet) so for now we represent its size
|
| 641 |
+
// as null may need to be changed after we reach a better solution for nested
|
| 642 |
+
// tensor size
|
| 643 |
+
std::optional<SymIntArrayRef> self_sizes = self.is_nested()
|
| 644 |
+
? std::optional<SymIntArrayRef>(std::nullopt)
|
| 645 |
+
: std::optional<SymIntArrayRef>(self.sym_sizes());
|
| 646 |
+
|
| 647 |
+
// handle simple types: integers, slices, none, ellipsis, bool
|
| 648 |
+
if (indices.size() == 1) {
|
| 649 |
+
const TensorIndex& index = indices[0];
|
| 650 |
+
if (index.is_integer()) {
|
| 651 |
+
return impl::applySelect(
|
| 652 |
+
self, 0, index.integer(), 0, self_device, self_sizes);
|
| 653 |
+
} else if (index.is_slice()) {
|
| 654 |
+
return impl::applySlice(
|
| 655 |
+
self,
|
| 656 |
+
0,
|
| 657 |
+
index.slice().start(),
|
| 658 |
+
index.slice().stop(),
|
| 659 |
+
index.slice().step(),
|
| 660 |
+
/*disable_slice_optimization=*/true,
|
| 661 |
+
self_device,
|
| 662 |
+
self_sizes);
|
| 663 |
+
} else if (index.is_none()) {
|
| 664 |
+
return self.unsqueeze(0);
|
| 665 |
+
} else if (index.is_ellipsis()) {
|
| 666 |
+
return at::alias(self);
|
| 667 |
+
} else if (index.is_boolean()) {
|
| 668 |
+
Tensor result = self.unsqueeze(0);
|
| 669 |
+
return dispatch_index(
|
| 670 |
+
result,
|
| 671 |
+
std::vector<Tensor>{impl::boolToIndexingTensor(
|
| 672 |
+
result, index.boolean(), self_device)});
|
| 673 |
+
}
|
| 674 |
+
}
|
| 675 |
+
|
| 676 |
+
std::vector<Tensor> tensorIndices;
|
| 677 |
+
Tensor sliced = impl::applySlicing(
|
| 678 |
+
self,
|
| 679 |
+
indices,
|
| 680 |
+
tensorIndices,
|
| 681 |
+
disable_slice_optimization,
|
| 682 |
+
self_device,
|
| 683 |
+
self_sizes);
|
| 684 |
+
if (tensorIndices.empty()) {
|
| 685 |
+
if (sliced.is_same(self)) {
|
| 686 |
+
// ensure we return a shallow copy for things like x[...]
|
| 687 |
+
sliced = at::alias(sliced);
|
| 688 |
+
}
|
| 689 |
+
return sliced;
|
| 690 |
+
}
|
| 691 |
+
|
| 692 |
+
// indexing by tensors ("advanced" indexing)
|
| 693 |
+
return dispatch_index(sliced, std::move(tensorIndices));
|
| 694 |
+
}
|
| 695 |
+
|
| 696 |
+
// This mirrors `THPVariable_setitem` in
|
| 697 |
+
// torch/csrc/autograd/python_variable_indexing.cpp for "the assigned value is a
|
| 698 |
+
// Tensor" case See NOTE [ Setting `disable_slice_optimization` when calling C++
|
| 699 |
+
// tensor indexing functions from Python ]
|
| 700 |
+
inline void set_item(
|
| 701 |
+
const Tensor& self,
|
| 702 |
+
const ArrayRef<TensorIndex>& indices,
|
| 703 |
+
const Tensor& value,
|
| 704 |
+
bool disable_slice_optimization = false) {
|
| 705 |
+
at::Device self_device = self.device();
|
| 706 |
+
SymIntArrayRef self_sizes = self.sym_sizes();
|
| 707 |
+
|
| 708 |
+
// handle simple types: integers, slices, ellipsis, bool
|
| 709 |
+
if (indices.size() == 1) {
|
| 710 |
+
const TensorIndex& index = indices[0];
|
| 711 |
+
if (index.is_boolean() && !index.boolean()) {
|
| 712 |
+
// do nothing for false (technically we should check the size, but we
|
| 713 |
+
// don't have real 0-sized shapes.
|
| 714 |
+
return;
|
| 715 |
+
} else if (index.is_ellipsis()) {
|
| 716 |
+
copy_to(self, value);
|
| 717 |
+
return;
|
| 718 |
+
} else if (index.is_none() || (index.is_boolean() && index.boolean())) {
|
| 719 |
+
copy_to(self.unsqueeze(0), value);
|
| 720 |
+
return;
|
| 721 |
+
} else if (index.is_integer()) {
|
| 722 |
+
copy_to(
|
| 723 |
+
impl::applySelect(
|
| 724 |
+
self, 0, index.integer(), 0, self_device, self_sizes),
|
| 725 |
+
value);
|
| 726 |
+
return;
|
| 727 |
+
} else if (index.is_slice()) {
|
| 728 |
+
copy_to(
|
| 729 |
+
impl::applySlice(
|
| 730 |
+
self,
|
| 731 |
+
0,
|
| 732 |
+
index.slice().start(),
|
| 733 |
+
index.slice().stop(),
|
| 734 |
+
index.slice().step(),
|
| 735 |
+
/*disable_slice_optimization=*/disable_slice_optimization,
|
| 736 |
+
self_device,
|
| 737 |
+
self_sizes),
|
| 738 |
+
value);
|
| 739 |
+
return;
|
| 740 |
+
}
|
| 741 |
+
}
|
| 742 |
+
|
| 743 |
+
std::vector<Tensor> tensorIndices;
|
| 744 |
+
Tensor sliced = impl::applySlicing(
|
| 745 |
+
self,
|
| 746 |
+
indices,
|
| 747 |
+
tensorIndices,
|
| 748 |
+
disable_slice_optimization,
|
| 749 |
+
self_device,
|
| 750 |
+
self_sizes);
|
| 751 |
+
if (tensorIndices.empty()) {
|
| 752 |
+
copy_to(sliced, value);
|
| 753 |
+
return;
|
| 754 |
+
}
|
| 755 |
+
|
| 756 |
+
SymIntArrayRef valueSizes = value.sym_sizes();
|
| 757 |
+
SymIntArrayRef slicedValueSizes = slicePrefix1sSize(valueSizes);
|
| 758 |
+
Tensor valuesSliced;
|
| 759 |
+
if (!valueSizes.equals(slicedValueSizes)) {
|
| 760 |
+
valuesSliced = value.view_symint(slicedValueSizes);
|
| 761 |
+
} else {
|
| 762 |
+
valuesSliced = value;
|
| 763 |
+
}
|
| 764 |
+
dispatch_index_put_(sliced, std::move(tensorIndices), valuesSliced);
|
| 765 |
+
return;
|
| 766 |
+
}
|
| 767 |
+
|
| 768 |
+
} // namespace at::indexing
|
| 769 |
+
|
| 770 |
+
#else
|
| 771 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 772 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorIterator.h
ADDED
|
@@ -0,0 +1,1039 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/TensorMeta.h>
|
| 5 |
+
#include <ATen/core/Dimname.h>
|
| 6 |
+
#include <ATen/core/Range.h>
|
| 7 |
+
#include <ATen/core/TensorBase.h>
|
| 8 |
+
#include <c10/core/DynamicCast.h>
|
| 9 |
+
#include <c10/util/FunctionRef.h>
|
| 10 |
+
#include <c10/util/MaybeOwned.h>
|
| 11 |
+
#include <c10/util/SmallVector.h>
|
| 12 |
+
#include <c10/util/TypeCast.h>
|
| 13 |
+
#include <c10/util/irange.h>
|
| 14 |
+
|
| 15 |
+
#include <array>
|
| 16 |
+
#include <bitset>
|
| 17 |
+
|
| 18 |
+
namespace at {
|
| 19 |
+
class Tensor;
|
| 20 |
+
class OptionalTensorRef;
|
| 21 |
+
using NameVector = SmallVector<Dimname, kDimVectorStaticSize>;
|
| 22 |
+
} // namespace at
|
| 23 |
+
|
| 24 |
+
// TensorIterator is a helper class for element-wise operations, such as
|
| 25 |
+
// arithmetic, comparisons, and trigonometric functions. It handles
|
| 26 |
+
// broadcasting and type conversions of operands.
|
| 27 |
+
//
|
| 28 |
+
// This is inspired by NumPy's Array Iterator API (NpyIter).
|
| 29 |
+
//
|
| 30 |
+
// The files Loops.h and Loops.cuh provide functions to build kernels that
|
| 31 |
+
// use TensorIterator.
|
| 32 |
+
//
|
| 33 |
+
// Example:
|
| 34 |
+
//
|
| 35 |
+
// auto iter = TensorIteratorConfig()
|
| 36 |
+
// .add_output(output)
|
| 37 |
+
// .add_input(input)
|
| 38 |
+
// .build()
|
| 39 |
+
//
|
| 40 |
+
// [MyKernel.cpp / MyKernel.cu]
|
| 41 |
+
// cpu_kernel(iter, [](float a, float b) {
|
| 42 |
+
// return a + b;
|
| 43 |
+
// });
|
| 44 |
+
//
|
| 45 |
+
// gpu_kernel(iter, []GPU_LAMBDA(float a, float b) -> float {
|
| 46 |
+
// return a + b;
|
| 47 |
+
// });
|
| 48 |
+
//
|
| 49 |
+
// Note [Order of Construction]
|
| 50 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 51 |
+
// When setting up the tensor iterator configuration, the output Tensors
|
| 52 |
+
// have to be added first via
|
| 53 |
+
// TensorIteratorConfig::add_owned_output(at::Tensor). After adding all outputs,
|
| 54 |
+
// the inputs can be added via
|
| 55 |
+
// TensorIteratorConfig::add_owned_input(at::Tensor).
|
| 56 |
+
// Adding another output after inputs have been added will rise an exception.
|
| 57 |
+
//
|
| 58 |
+
// Note [Common Dtype Computation]
|
| 59 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 60 |
+
// Some operations have a natural notion of a "common dtype" or
|
| 61 |
+
// "computation dtype" where all inputs are cast to one dtype, the
|
| 62 |
+
// operation is performed, and then the results are cast to all outputs.
|
| 63 |
+
//
|
| 64 |
+
// TensorIterator infers a common dtype if all inputs have the same dtype,
|
| 65 |
+
// and it computes one using type promotion rules on its inputs if
|
| 66 |
+
// promote_inputs_to_common_dtype_ is true. Attempting to query
|
| 67 |
+
// a common dtype otherwise will throw an exception.
|
| 68 |
+
//
|
| 69 |
+
// Note that the outputs are not considered when computing a common dtype.
|
| 70 |
+
|
| 71 |
+
namespace at {
|
| 72 |
+
|
| 73 |
+
namespace internal {
|
| 74 |
+
// This parameter is heuristically chosen to determine the minimum number of
|
| 75 |
+
// work that warrants parallelism. For example, when summing an array, it is
|
| 76 |
+
// deemed inefficient to parallelise over arrays shorter than 32768. Further,
|
| 77 |
+
// no parallel algorithm (such as parallel_reduce) should split work into
|
| 78 |
+
// smaller than GRAIN_SIZE chunks.
|
| 79 |
+
constexpr int64_t GRAIN_SIZE = 32768;
|
| 80 |
+
|
| 81 |
+
// Storage for a non-owning Tensor, without needing to include Tensor.h
|
| 82 |
+
class TORCH_API OpaqueOptionalTensorRef {
|
| 83 |
+
alignas(alignof(TensorBase)) std::array<char, sizeof(TensorBase)> data_{};
|
| 84 |
+
|
| 85 |
+
public:
|
| 86 |
+
OpaqueOptionalTensorRef();
|
| 87 |
+
OpaqueOptionalTensorRef(const OpaqueOptionalTensorRef&) = default;
|
| 88 |
+
OpaqueOptionalTensorRef& operator=(const OpaqueOptionalTensorRef&) = default;
|
| 89 |
+
OpaqueOptionalTensorRef(OpaqueOptionalTensorRef&&) noexcept = default;
|
| 90 |
+
OpaqueOptionalTensorRef& operator=(OpaqueOptionalTensorRef&&) noexcept =
|
| 91 |
+
default;
|
| 92 |
+
~OpaqueOptionalTensorRef();
|
| 93 |
+
|
| 94 |
+
OptionalTensorRef* get() {
|
| 95 |
+
return reinterpret_cast<OptionalTensorRef*>(data_.data());
|
| 96 |
+
}
|
| 97 |
+
const OptionalTensorRef* get() const {
|
| 98 |
+
return reinterpret_cast<const OptionalTensorRef*>(data_.data());
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
OptionalTensorRef& operator*() {
|
| 102 |
+
return *get();
|
| 103 |
+
}
|
| 104 |
+
const OptionalTensorRef& operator*() const {
|
| 105 |
+
return *get();
|
| 106 |
+
}
|
| 107 |
+
OptionalTensorRef* operator->() {
|
| 108 |
+
return get();
|
| 109 |
+
}
|
| 110 |
+
const OptionalTensorRef* operator->() const {
|
| 111 |
+
return get();
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
const Tensor& getTensor() const;
|
| 115 |
+
};
|
| 116 |
+
} // namespace internal
|
| 117 |
+
|
| 118 |
+
struct TORCH_API OperandInfo {
|
| 119 |
+
using StrideVector = SmallVector<int64_t, 6>;
|
| 120 |
+
OperandInfo() = default;
|
| 121 |
+
C10_ALWAYS_INLINE explicit OperandInfo(c10::MaybeOwned<TensorBase>&& t) {
|
| 122 |
+
if (t->defined()) {
|
| 123 |
+
device = t->device();
|
| 124 |
+
target_dtype = t->scalar_type();
|
| 125 |
+
current_dtype = target_dtype;
|
| 126 |
+
}
|
| 127 |
+
tensor(std::move(t));
|
| 128 |
+
validate();
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
C10_ALWAYS_INLINE OperandInfo(const OperandInfo&) = default;
|
| 132 |
+
C10_ALWAYS_INLINE OperandInfo& operator=(const OperandInfo&) = default;
|
| 133 |
+
C10_ALWAYS_INLINE OperandInfo(OperandInfo&&) noexcept = default;
|
| 134 |
+
C10_ALWAYS_INLINE OperandInfo& operator=(OperandInfo&&) noexcept = default;
|
| 135 |
+
C10_ALWAYS_INLINE ~OperandInfo() = default;
|
| 136 |
+
|
| 137 |
+
/// The data pointer. This may be different from tensor->data_ptr() if the
|
| 138 |
+
/// iterator is split.
|
| 139 |
+
void* data = nullptr;
|
| 140 |
+
|
| 141 |
+
/// Stride after broadcasting. The stride is in bytes, not number of elements.
|
| 142 |
+
StrideVector stride_bytes;
|
| 143 |
+
|
| 144 |
+
/// The desired device and type for the operand. For inputs, this specifies
|
| 145 |
+
/// that the input should be converted to this type if necessary. For outputs,
|
| 146 |
+
/// this specifies which type to allocate. target_dtype and device are
|
| 147 |
+
/// initialized with the dtype and device of the tensor but during type
|
| 148 |
+
/// promotion target_dtype value can become different from tensor's dtype
|
| 149 |
+
/// also, during type promotion target_dtype and device can be set for an
|
| 150 |
+
/// undefined tensor so that tensor can be properly constructed later.
|
| 151 |
+
std::optional<Device> device = std::nullopt;
|
| 152 |
+
ScalarType target_dtype = ScalarType::Undefined;
|
| 153 |
+
// Caches dtype of the tensor, because scalar_type is an expensive operation
|
| 154 |
+
// If dtype of the tensor is changed (e.g. as a result of type promotion or in
|
| 155 |
+
// allocate_outputs), this
|
| 156 |
+
// value should be changed too.
|
| 157 |
+
ScalarType current_dtype = ScalarType::Undefined;
|
| 158 |
+
|
| 159 |
+
bool is_device_defined() const {
|
| 160 |
+
return device.has_value();
|
| 161 |
+
}
|
| 162 |
+
bool is_type_defined() const {
|
| 163 |
+
return target_dtype != ScalarType::Undefined;
|
| 164 |
+
}
|
| 165 |
+
TensorOptions options() const {
|
| 166 |
+
return TensorOptions(target_dtype).device(device);
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
bool is_output = false;
|
| 170 |
+
|
| 171 |
+
// will_resize is only for output tensor.
|
| 172 |
+
// 1) Functional call(like torch.add(self, other)): output tensor is
|
| 173 |
+
// undefined, and pytorch creates a new tensor by using common shape
|
| 174 |
+
// and computed stride in TensorIterator;
|
| 175 |
+
// 2) Inplace call(like torch.add_(self, other)): output tensor is same
|
| 176 |
+
// with input tensor, and can't to modify tensor's size and stride;
|
| 177 |
+
// 3) Op call with output(like torch.add(self, other, out = output)):
|
| 178 |
+
// output tensor is defined, but tensor shape maybe different with common
|
| 179 |
+
// shape. If tensor shape is not same with common shape, this output
|
| 180 |
+
// tensor will be resized by using common shape and computed stride in
|
| 181 |
+
// TensorIterator. Otherwise can't modify tensor's size and stride.
|
| 182 |
+
bool will_resize = false;
|
| 183 |
+
|
| 184 |
+
bool is_read_write = false;
|
| 185 |
+
|
| 186 |
+
bool is_const = false;
|
| 187 |
+
|
| 188 |
+
void validate() {
|
| 189 |
+
TORCH_CHECK(
|
| 190 |
+
!tensor_base_->defined() || tensor_base_->layout() == kStrided,
|
| 191 |
+
"unsupported tensor layout: ",
|
| 192 |
+
tensor_base_->layout());
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
/// The tensor operand. Note that the strides, data pointer, and
|
| 196 |
+
/// other attributes may differ due to dimension reordering and
|
| 197 |
+
/// coalescing.
|
| 198 |
+
const Tensor& tensor() const {
|
| 199 |
+
return tensor_storage_.getTensor();
|
| 200 |
+
}
|
| 201 |
+
const TensorBase& tensor_base() const {
|
| 202 |
+
return *tensor_base_;
|
| 203 |
+
}
|
| 204 |
+
void tensor(c10::MaybeOwned<TensorBase>&& tensor);
|
| 205 |
+
|
| 206 |
+
// Save the original tensor operand in cases when an output is modified
|
| 207 |
+
// (e.g. if dtype is changed)
|
| 208 |
+
const Tensor& original_tensor() const {
|
| 209 |
+
return original_tensor_storage_.getTensor();
|
| 210 |
+
}
|
| 211 |
+
const TensorBase& original_tensor_base() const {
|
| 212 |
+
return *original_tensor_base_;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
// Set tensor to a new value, and store the old tensor value in
|
| 216 |
+
// original_tensor Should only ever be called once for the lifetime of an
|
| 217 |
+
// operand
|
| 218 |
+
void exchange_tensor(c10::MaybeOwned<TensorBase>&& new_tensor);
|
| 219 |
+
|
| 220 |
+
// Move original_tensor back into tensor, exchange_tensor must have been
|
| 221 |
+
// called before
|
| 222 |
+
void restore_original_tensor();
|
| 223 |
+
|
| 224 |
+
private:
|
| 225 |
+
c10::MaybeOwned<TensorBase> tensor_base_;
|
| 226 |
+
c10::MaybeOwned<TensorBase> original_tensor_base_ =
|
| 227 |
+
c10::MaybeOwned<TensorBase>::owned(std::in_place);
|
| 228 |
+
|
| 229 |
+
// We store TensorBase visibly in the header to allow inline access.
|
| 230 |
+
// However, we sometimes need a genuine `const Tensor &` for the
|
| 231 |
+
// TensorIterator API. So, we also store a non-owning `Tensor`
|
| 232 |
+
// object in these `_storage_` variables.
|
| 233 |
+
internal::OpaqueOptionalTensorRef tensor_storage_;
|
| 234 |
+
internal::OpaqueOptionalTensorRef original_tensor_storage_;
|
| 235 |
+
};
|
| 236 |
+
|
| 237 |
+
struct SplitUntil32Bit;
|
| 238 |
+
|
| 239 |
+
enum class FastSetupType : uint8_t {
|
| 240 |
+
NONE,
|
| 241 |
+
CONTIGUOUS,
|
| 242 |
+
CHANNELS_LAST,
|
| 243 |
+
NON_OVERLAPPING_DENSE
|
| 244 |
+
};
|
| 245 |
+
|
| 246 |
+
class TensorIteratorConfig;
|
| 247 |
+
struct TensorIterator;
|
| 248 |
+
|
| 249 |
+
struct TORCH_API TensorIteratorBase : public impl::MetaBase {
|
| 250 |
+
using DimMask = std::bitset<64>;
|
| 251 |
+
using PtrVector = SmallVector<char*, 4>;
|
| 252 |
+
using StrideVector = SmallVector<int64_t, 6>;
|
| 253 |
+
|
| 254 |
+
void build(TensorIteratorConfig& /*config*/);
|
| 255 |
+
|
| 256 |
+
// The inner-loop function operates on the fastest moving dimension. It
|
| 257 |
+
// implements element-wise operations in terms of 1-d strided tensors.
|
| 258 |
+
//
|
| 259 |
+
// Arguments:
|
| 260 |
+
// data: data pointers for each operand (length `ntensors`)
|
| 261 |
+
// strides: stride for each operand (length `ntensors`)
|
| 262 |
+
// size: size of inner loop
|
| 263 |
+
//
|
| 264 |
+
// The `size` often matches shape[0], but may be smaller due to
|
| 265 |
+
// parallelization of the inner loop.
|
| 266 |
+
using loop2d_t = c10::function_ref<
|
| 267 |
+
void(char** data, const int64_t* strides, int64_t size0, int64_t size1)>;
|
| 268 |
+
|
| 269 |
+
using loop_subiter_t = c10::function_ref<void(TensorIteratorBase& subiter)>;
|
| 270 |
+
|
| 271 |
+
void foreach_reduced_elt(loop_subiter_t loop, bool parallelize = true);
|
| 272 |
+
|
| 273 |
+
int ndim() const {
|
| 274 |
+
return static_cast<int>(shape_.size());
|
| 275 |
+
}
|
| 276 |
+
IntArrayRef shape() const {
|
| 277 |
+
return shape_;
|
| 278 |
+
}
|
| 279 |
+
int64_t numel() const;
|
| 280 |
+
int ntensors() const {
|
| 281 |
+
return static_cast<int>(operands_.size());
|
| 282 |
+
}
|
| 283 |
+
int noutputs() const {
|
| 284 |
+
return num_outputs_;
|
| 285 |
+
}
|
| 286 |
+
int ninputs() const {
|
| 287 |
+
return ntensors() - noutputs();
|
| 288 |
+
}
|
| 289 |
+
IntArrayRef view_offsets() const {
|
| 290 |
+
return view_offsets_;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
/// number of elements in the output operand. this is the same as numel() for
|
| 294 |
+
/// operations that are not reductions.
|
| 295 |
+
int64_t num_output_elements() const;
|
| 296 |
+
|
| 297 |
+
/// number of reduced dimensions in a reduction operation
|
| 298 |
+
int num_reduce_dims() const;
|
| 299 |
+
|
| 300 |
+
/// 1-dimensional iteration and no buffering or type conversion
|
| 301 |
+
bool is_trivial_1d() const;
|
| 302 |
+
/// Reducible to 1-dimensional and all operands are contiguous
|
| 303 |
+
bool is_contiguous() const;
|
| 304 |
+
bool is_dim_reduced(int dim) const;
|
| 305 |
+
|
| 306 |
+
/// Accessors for each operand
|
| 307 |
+
IntArrayRef strides(int64_t arg) const {
|
| 308 |
+
return operands_[arg].stride_bytes;
|
| 309 |
+
}
|
| 310 |
+
void* data_ptr(int64_t arg) const;
|
| 311 |
+
ScalarType dtype(int64_t arg = 0) const {
|
| 312 |
+
return operands_[arg].current_dtype;
|
| 313 |
+
}
|
| 314 |
+
ScalarType common_dtype() const {
|
| 315 |
+
TORCH_INTERNAL_ASSERT(
|
| 316 |
+
common_dtype_ != ScalarType::Undefined,
|
| 317 |
+
"Queried for invalid common dtype!");
|
| 318 |
+
return common_dtype_;
|
| 319 |
+
}
|
| 320 |
+
ScalarType input_dtype(int64_t arg = 0) const {
|
| 321 |
+
return operands_[num_outputs_ + arg].current_dtype;
|
| 322 |
+
}
|
| 323 |
+
Device device(int64_t arg = 0) const {
|
| 324 |
+
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
|
| 325 |
+
return operands_[arg].device.value();
|
| 326 |
+
}
|
| 327 |
+
c10::DeviceType device_type(int64_t arg = 0) const {
|
| 328 |
+
return device(arg).type();
|
| 329 |
+
}
|
| 330 |
+
int64_t element_size(int64_t arg) const {
|
| 331 |
+
return static_cast<int64_t>(elementSize(dtype(arg)));
|
| 332 |
+
}
|
| 333 |
+
bool is_scalar(int64_t arg) const;
|
| 334 |
+
bool is_cpu_scalar(int64_t arg) const;
|
| 335 |
+
|
| 336 |
+
const TensorBase& tensor_base(int64_t arg) const {
|
| 337 |
+
return operands_[arg].tensor_base();
|
| 338 |
+
}
|
| 339 |
+
const Tensor& tensor(int64_t arg) const {
|
| 340 |
+
return operands_[arg].tensor();
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
const TensorBase& output_base(int64_t arg = 0) const {
|
| 344 |
+
AT_ASSERT(arg < num_outputs_);
|
| 345 |
+
return tensor_base(arg);
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
const Tensor& output(int64_t arg = 0) const {
|
| 349 |
+
AT_ASSERT(arg < num_outputs_);
|
| 350 |
+
return tensor(arg);
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
const TensorBase& input_base(int64_t arg = 0) const {
|
| 354 |
+
AT_ASSERT(arg >= 0 && arg < ntensors() - num_outputs_);
|
| 355 |
+
return tensor_base(num_outputs_ + arg);
|
| 356 |
+
}
|
| 357 |
+
const Tensor& input(int64_t arg = 0) const {
|
| 358 |
+
AT_ASSERT(arg >= 0 && arg < ntensors() - num_outputs_);
|
| 359 |
+
return tensor(num_outputs_ + arg);
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
// Copies from temporary outputs back to the original outputs
|
| 363 |
+
// NOTE: only used on CPU
|
| 364 |
+
void cast_outputs();
|
| 365 |
+
|
| 366 |
+
/// Removes an operand from this iterator
|
| 367 |
+
void remove_operand(int64_t arg);
|
| 368 |
+
/// Shrinks an iterated dimension
|
| 369 |
+
void narrow(int dim, int64_t start, int64_t size);
|
| 370 |
+
/// Narrows every dim after and including `start_dim` to size one.
|
| 371 |
+
void select_all_keeping_dim(int start_dim, IntArrayRef starts);
|
| 372 |
+
/// Replaces the data pointer for the operand at index `arg`.
|
| 373 |
+
/// The new pointer should have the same sizes, strides and dtype as the
|
| 374 |
+
/// original
|
| 375 |
+
void unsafe_replace_operand(int64_t arg, void* data);
|
| 376 |
+
|
| 377 |
+
/// Splits this TensorIterator into two iterators. Together they iterate over
|
| 378 |
+
/// the entire operation. Used by `with_32bit_indexing()`.
|
| 379 |
+
std::unique_ptr<TensorIterator> split(int dim);
|
| 380 |
+
|
| 381 |
+
/// Returns the dimension with the largest extent: (size[dim]-1) * stride[dim]
|
| 382 |
+
int get_dim_to_split() const;
|
| 383 |
+
|
| 384 |
+
template <typename T>
|
| 385 |
+
T scalar_value(int64_t arg) {
|
| 386 |
+
auto& op = operands_[arg];
|
| 387 |
+
return c10::fetch_and_cast<T>(op.tensor_base().scalar_type(), op.data);
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
/// Return scalar value from original_tensor_base if it is defined. When
|
| 391 |
+
/// common_dtype is Half, casting scalar input to common_dtype might overflow.
|
| 392 |
+
/// If the scalar is already given in the type of Half, then return scalar
|
| 393 |
+
/// value from tensor_base.
|
| 394 |
+
template <typename T>
|
| 395 |
+
T original_scalar_value(int64_t arg) {
|
| 396 |
+
auto& original_tensor_base = operands_[arg].original_tensor_base();
|
| 397 |
+
if (original_tensor_base.defined()) {
|
| 398 |
+
TORCH_INTERNAL_ASSERT(
|
| 399 |
+
original_tensor_base.scalar_type() != common_dtype());
|
| 400 |
+
return c10::fetch_and_cast<T>(
|
| 401 |
+
original_tensor_base.scalar_type(),
|
| 402 |
+
original_tensor_base.const_data_ptr());
|
| 403 |
+
} else {
|
| 404 |
+
return scalar_value<T>(arg);
|
| 405 |
+
}
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
private:
|
| 409 |
+
template <typename loop1d_t>
|
| 410 |
+
auto loop_2d_from_1d(const loop1d_t& loop) {
|
| 411 |
+
return
|
| 412 |
+
[loop, ntensor = ntensors()](
|
| 413 |
+
char** base, const int64_t* strides, int64_t size0, int64_t size1) {
|
| 414 |
+
PtrVector data(base, base + ntensor);
|
| 415 |
+
const int64_t* outer_strides = &strides[ntensor];
|
| 416 |
+
for (const auto i : c10::irange(size1)) {
|
| 417 |
+
if (i > 0) {
|
| 418 |
+
for (const auto arg : c10::irange(ntensor)) {
|
| 419 |
+
data[arg] += outer_strides[arg];
|
| 420 |
+
}
|
| 421 |
+
}
|
| 422 |
+
loop(data.data(), strides, size0);
|
| 423 |
+
}
|
| 424 |
+
};
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
public:
|
| 428 |
+
template <
|
| 429 |
+
typename loop1d_t,
|
| 430 |
+
std::enable_if_t<
|
| 431 |
+
std::is_convertible_v<
|
| 432 |
+
loop1d_t,
|
| 433 |
+
c10::function_ref<
|
| 434 |
+
void(char**, const int64_t* strides, int64_t size)>>,
|
| 435 |
+
int> = 0>
|
| 436 |
+
void for_each(loop1d_t loop, int64_t grain_size = at::internal::GRAIN_SIZE) {
|
| 437 |
+
for_each(loop_2d_from_1d(loop), grain_size);
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
void for_each(loop2d_t loop, int64_t grain_size = at::internal::GRAIN_SIZE);
|
| 441 |
+
|
| 442 |
+
void parallel_reduce(loop2d_t loop);
|
| 443 |
+
|
| 444 |
+
template <
|
| 445 |
+
typename loop1d_t,
|
| 446 |
+
std::enable_if_t<
|
| 447 |
+
std::is_convertible_v<
|
| 448 |
+
loop1d_t,
|
| 449 |
+
c10::function_ref<
|
| 450 |
+
void(char**, const int64_t* strides, int64_t size)>>,
|
| 451 |
+
int> = 0>
|
| 452 |
+
void serial_for_each(loop1d_t loop, Range range) {
|
| 453 |
+
serial_for_each(loop_2d_from_1d(loop), range);
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
void serial_for_each(loop2d_t loop, Range range) const;
|
| 457 |
+
|
| 458 |
+
/// Create a strides array for a Tensor with shape of this iterator. The
|
| 459 |
+
/// parameter `element_size` specifies the size of Tensor's data type in
|
| 460 |
+
/// bytes (e.g. `4` for `float`)
|
| 461 |
+
StrideVector compatible_stride(int64_t element_size) const;
|
| 462 |
+
|
| 463 |
+
/// Inverts the re-ordering done by reorder_dimensions. This can only be
|
| 464 |
+
/// called *before* coalesce_dimensions() is called.
|
| 465 |
+
DimVector invert_perm(IntArrayRef input) const;
|
| 466 |
+
|
| 467 |
+
/// Reapply same re-ordering as it is done by reorder_dimensions. This can
|
| 468 |
+
/// only be called *before* coalesce_dimensions() is called.
|
| 469 |
+
DimVector apply_perm_and_mul(IntArrayRef input, int mul) const;
|
| 470 |
+
|
| 471 |
+
/// Helper functions for CPU iteration
|
| 472 |
+
StrideVector get_dim_strides(int dim) const;
|
| 473 |
+
StrideVector get_strides() const;
|
| 474 |
+
StrideVector get_inner_strides() const {
|
| 475 |
+
return get_dim_strides(0);
|
| 476 |
+
}
|
| 477 |
+
PtrVector get_base_ptrs() const;
|
| 478 |
+
|
| 479 |
+
// Helper functions for advanced stride manipulations (e.g. torch.flip)
|
| 480 |
+
void _unsafe_set_arg_strides(const int64_t arg, IntArrayRef strides) {
|
| 481 |
+
operands_[arg].stride_bytes = strides;
|
| 482 |
+
}
|
| 483 |
+
void _unsafe_set_arg_data(const int64_t arg, void* data) {
|
| 484 |
+
operands_[arg].data = data;
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
// Helper functions for custom device, custom device can get OperandInfo and
|
| 488 |
+
// NameVector in their side.
|
| 489 |
+
const OperandInfo& operand(int arg = 0) const {
|
| 490 |
+
return operands_[arg];
|
| 491 |
+
}
|
| 492 |
+
OperandInfo& operand(int arg = 0) {
|
| 493 |
+
return operands_[arg];
|
| 494 |
+
}
|
| 495 |
+
NameVector& get_dim_names() {
|
| 496 |
+
return names_;
|
| 497 |
+
}
|
| 498 |
+
const NameVector& get_dim_names() const {
|
| 499 |
+
return names_;
|
| 500 |
+
}
|
| 501 |
+
|
| 502 |
+
/// true if the stride computation can use 32-bit arithmetic. Used by GPU
|
| 503 |
+
/// kernels
|
| 504 |
+
bool can_use_32bit_indexing() const;
|
| 505 |
+
|
| 506 |
+
/// An "iterable" object that recursively splits this iterator into
|
| 507 |
+
/// sub-iterators that can use 32-bit indexing.
|
| 508 |
+
SplitUntil32Bit with_32bit_indexing() const;
|
| 509 |
+
|
| 510 |
+
/// If the kernel should accumulate into the output. Only relevant for CUDA
|
| 511 |
+
/// reductions.
|
| 512 |
+
bool should_accumulate() const {
|
| 513 |
+
return accumulate_;
|
| 514 |
+
}
|
| 515 |
+
|
| 516 |
+
/// Whether this iterator produces the actual output,
|
| 517 |
+
/// as opposed to something that will be accumulated further. Only relevant
|
| 518 |
+
/// for CUDA reductions.
|
| 519 |
+
bool is_final_output() const {
|
| 520 |
+
return final_output_;
|
| 521 |
+
}
|
| 522 |
+
|
| 523 |
+
bool has_contiguous_first_dim() const {
|
| 524 |
+
if (ndim() == 0) {
|
| 525 |
+
return true;
|
| 526 |
+
}
|
| 527 |
+
|
| 528 |
+
int num_tensors = ntensors();
|
| 529 |
+
for (const auto i : c10::irange(num_tensors)) {
|
| 530 |
+
if (strides(i)[0] != element_size(i)) {
|
| 531 |
+
return false;
|
| 532 |
+
}
|
| 533 |
+
}
|
| 534 |
+
return true;
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
void set_output_raw_strided(
|
| 538 |
+
int64_t output_idx,
|
| 539 |
+
IntArrayRef sizes,
|
| 540 |
+
IntArrayRef strides,
|
| 541 |
+
TensorOptions options,
|
| 542 |
+
DimnameList names) override;
|
| 543 |
+
|
| 544 |
+
#define TORCH_DISALLOW_TEMPORARIES_IMPL(methodname, maybestatic) \
|
| 545 |
+
maybestatic void methodname( \
|
| 546 |
+
TensorBase&& out, const TensorBase& a, const TensorBase& b) = delete; \
|
| 547 |
+
maybestatic void methodname( \
|
| 548 |
+
const TensorBase& out, TensorBase&& a, const TensorBase& b) = delete; \
|
| 549 |
+
maybestatic void methodname( \
|
| 550 |
+
const TensorBase& out, const TensorBase& a, TensorBase&& b) = delete; \
|
| 551 |
+
maybestatic void methodname( \
|
| 552 |
+
TensorBase&& out, TensorBase&& a, const TensorBase& b) = delete; \
|
| 553 |
+
maybestatic void methodname( \
|
| 554 |
+
TensorBase&& out, const TensorBase& a, TensorBase&& b) = delete; \
|
| 555 |
+
maybestatic void methodname( \
|
| 556 |
+
const TensorBase& out, TensorBase&& a, TensorBase&& b) = delete; \
|
| 557 |
+
maybestatic void methodname( \
|
| 558 |
+
TensorBase&& out, TensorBase&& a, TensorBase&& b) = delete;
|
| 559 |
+
|
| 560 |
+
#define TORCH_DISALLOW_TEMPORARIES(methodname) \
|
| 561 |
+
TORCH_DISALLOW_TEMPORARIES_IMPL(methodname, )
|
| 562 |
+
|
| 563 |
+
void build_binary_float_op(
|
| 564 |
+
const TensorBase& out,
|
| 565 |
+
const TensorBase& a,
|
| 566 |
+
const TensorBase& b);
|
| 567 |
+
void build_borrowing_binary_float_op(
|
| 568 |
+
const TensorBase& out,
|
| 569 |
+
const TensorBase& a,
|
| 570 |
+
const TensorBase& b);
|
| 571 |
+
TORCH_DISALLOW_TEMPORARIES(build_borrowing_binary_float_op)
|
| 572 |
+
void build_binary_op(
|
| 573 |
+
const TensorBase& out,
|
| 574 |
+
const TensorBase& a,
|
| 575 |
+
const TensorBase& b);
|
| 576 |
+
void build_borrowing_binary_op(
|
| 577 |
+
const TensorBase& out,
|
| 578 |
+
const TensorBase& a,
|
| 579 |
+
const TensorBase& b);
|
| 580 |
+
TORCH_DISALLOW_TEMPORARIES(build_borrowing_binary_op)
|
| 581 |
+
void build_unary_float_op(const TensorBase& out, const TensorBase& a);
|
| 582 |
+
void build_borrowing_unary_float_op(
|
| 583 |
+
const TensorBase& out,
|
| 584 |
+
const TensorBase& a);
|
| 585 |
+
TORCH_DISALLOW_TEMPORARIES(build_borrowing_unary_float_op)
|
| 586 |
+
void build_unary_op(const TensorBase& out, const TensorBase& a);
|
| 587 |
+
// Odd special case needed for pow. Has to borrow the output because
|
| 588 |
+
// it's a structured kernel, but the argument is potentially a copy.
|
| 589 |
+
void build_output_borrowing_argument_owning_unary_op(
|
| 590 |
+
const TensorBase& out,
|
| 591 |
+
const TensorBase& a);
|
| 592 |
+
void build_borrowing_unary_op(const TensorBase& out, const TensorBase& a);
|
| 593 |
+
TORCH_DISALLOW_TEMPORARIES(build_borrowing_unary_op)
|
| 594 |
+
void build_borrowing_unary_force_boolean_op(
|
| 595 |
+
const TensorBase& out,
|
| 596 |
+
const TensorBase& a);
|
| 597 |
+
TORCH_DISALLOW_TEMPORARIES(build_borrowing_unary_force_boolean_op)
|
| 598 |
+
void build_comparison_op(
|
| 599 |
+
const TensorBase& out,
|
| 600 |
+
const TensorBase& a,
|
| 601 |
+
const TensorBase& b);
|
| 602 |
+
void build_borrowing_comparison_op(
|
| 603 |
+
const TensorBase& out,
|
| 604 |
+
const TensorBase& a,
|
| 605 |
+
const TensorBase& b);
|
| 606 |
+
TORCH_DISALLOW_TEMPORARIES(build_borrowing_comparison_op)
|
| 607 |
+
// Another special case: we need to own the second argument for comparison
|
| 608 |
+
// ops.
|
| 609 |
+
void build_borrowing_except_last_argument_comparison_op(
|
| 610 |
+
const TensorBase& out,
|
| 611 |
+
const TensorBase& a,
|
| 612 |
+
const TensorBase& b);
|
| 613 |
+
void build_ternary_op(
|
| 614 |
+
const TensorBase& out,
|
| 615 |
+
const TensorBase& a,
|
| 616 |
+
const TensorBase& b,
|
| 617 |
+
const TensorBase& c);
|
| 618 |
+
|
| 619 |
+
#undef TORCH_DISALLOW_TEMPORARIES
|
| 620 |
+
protected:
|
| 621 |
+
// Mutable reference as it moves tensors out of TensorIteratorConfig
|
| 622 |
+
void populate_operands(TensorIteratorConfig& /*config*/);
|
| 623 |
+
void mark_outputs();
|
| 624 |
+
void mark_resize_outputs(const TensorIteratorConfig& /*config*/);
|
| 625 |
+
void compute_mem_overlaps(const TensorIteratorConfig& /*config*/);
|
| 626 |
+
void compute_shape(const TensorIteratorConfig& /*config*/);
|
| 627 |
+
void compute_strides(const TensorIteratorConfig& /*config*/);
|
| 628 |
+
void reorder_dimensions();
|
| 629 |
+
void permute_dimensions(IntArrayRef perm);
|
| 630 |
+
void compute_types(const TensorIteratorConfig& /*config*/);
|
| 631 |
+
ScalarType compute_common_dtype();
|
| 632 |
+
void allocate_or_resize_outputs();
|
| 633 |
+
bool fast_set_up(const TensorIteratorConfig& /*config*/);
|
| 634 |
+
FastSetupType compute_fast_setup_type(const TensorIteratorConfig& /*config*/);
|
| 635 |
+
void compute_names(const TensorIteratorConfig& /*config*/);
|
| 636 |
+
void propagate_names_to_outputs();
|
| 637 |
+
void coalesce_dimensions();
|
| 638 |
+
|
| 639 |
+
protected:
|
| 640 |
+
/// Records the "computation" shape of the output tensor. The computation
|
| 641 |
+
/// shape is different from the regular shape in a few ways:
|
| 642 |
+
///
|
| 643 |
+
/// - The shape may be permuted (via permute_dimensions) so that we
|
| 644 |
+
/// process the dimensions in the most computationally efficient order
|
| 645 |
+
/// (rather than the logical order given to us by the users.)
|
| 646 |
+
/// - The shape may have adjacent dimensions collapsed (via
|
| 647 |
+
/// coalesce_dimensions) so that we minimize the number of
|
| 648 |
+
/// dimensions we have to explicitly iterate over. For example,
|
| 649 |
+
/// a pointwise operation on a contiguous tensor "computationally"
|
| 650 |
+
/// consists of only a single dimension.
|
| 651 |
+
///
|
| 652 |
+
/// In other words, the computation shape is the output shape as it
|
| 653 |
+
/// actually matters for implementing the kernel, but not necessarily the
|
| 654 |
+
/// output shape that the user will see in the end.
|
| 655 |
+
///
|
| 656 |
+
/// The lifecycle of mutations to shape_ in TensorIterator:
|
| 657 |
+
/// - declare_static_shape() sets an initial shape explicitly
|
| 658 |
+
/// provided by user, otherwise
|
| 659 |
+
/// - compute_shape() computes the true (non-computational) shape
|
| 660 |
+
/// specified by the user.
|
| 661 |
+
/// - reorder_dimensions() reorders dimensions to improve coalescing.
|
| 662 |
+
/// - coalesce_dimensions() then coalesces adjacent dimensions when
|
| 663 |
+
/// possible.
|
| 664 |
+
///
|
| 665 |
+
/// The shape may also be further modified if we create sub-TensorIterators,
|
| 666 |
+
/// e.g., via narrow or select_all_keeping_dim.
|
| 667 |
+
DimVector shape_;
|
| 668 |
+
|
| 669 |
+
/// Temporarily records the permutation computed by reorder_dimensions.
|
| 670 |
+
/// This permutation maps the computation output dimension (dim) to
|
| 671 |
+
/// the original true output dimension (perm_[dim]). It is used by
|
| 672 |
+
/// invert_perm to undo the permutation. After coalesce_dimensions is
|
| 673 |
+
/// called, the permutation is no longer valid (as, in general, there
|
| 674 |
+
/// is no permutation that will make computation dimensions to
|
| 675 |
+
/// output dimensions); methods that manipulate perm_ are obligated
|
| 676 |
+
/// to test that !has_coalesced_dimensions
|
| 677 |
+
DimVector perm_;
|
| 678 |
+
|
| 679 |
+
/// Has coalesce_dimensions() (or any moral equivalent, e.g., fast_build())
|
| 680 |
+
/// been called? This is SOLELY used to check validity of perm_.
|
| 681 |
+
bool has_coalesced_dimensions_ = false;
|
| 682 |
+
|
| 683 |
+
/// Whether iteration must be fixed. This disables dimension permuting and
|
| 684 |
+
/// also changes how for_each divides work among threads.
|
| 685 |
+
bool enforce_linear_iteration_ = false;
|
| 686 |
+
|
| 687 |
+
/// The index offsets into the original tensors for each dimension.
|
| 688 |
+
/// This is only non-zero when you narrow() a TensorIterator (e.g.,
|
| 689 |
+
/// when you make sub-TensorIterators).
|
| 690 |
+
DimVector view_offsets_;
|
| 691 |
+
|
| 692 |
+
/// The computed names of the output tensor. Computed by compute_names()
|
| 693 |
+
NameVector names_;
|
| 694 |
+
|
| 695 |
+
/// The operands of the TensorIterator: both the inputs and outputs. The
|
| 696 |
+
/// outputs MUST come first in the operands_ list. There is always an
|
| 697 |
+
/// operand for each output of the TensorIterator, even if TensorIterator
|
| 698 |
+
/// will ultimately be responsible for allocating the output; in those
|
| 699 |
+
/// cases, tensor is simply undefined (and will be populated later
|
| 700 |
+
/// during build()).
|
| 701 |
+
///
|
| 702 |
+
/// This list is initially populated prior to build(), but build() mutates
|
| 703 |
+
/// OperandInfo to populate more information.
|
| 704 |
+
SmallVector<OperandInfo, 4> operands_;
|
| 705 |
+
|
| 706 |
+
/// Number of outputs in operands_ (the length of the outputs prefix
|
| 707 |
+
/// in operands_).
|
| 708 |
+
int num_outputs_ = 0;
|
| 709 |
+
|
| 710 |
+
/// Whether or not all operands have the same shape and are 1d+. Having all
|
| 711 |
+
/// the same shape affects whether or not the iterator is eligible for fast
|
| 712 |
+
/// setup.
|
| 713 |
+
bool all_ops_same_shape_ = false;
|
| 714 |
+
/// Whether or not all operands are 0d, this affects type promotion
|
| 715 |
+
bool all_ops_are_scalars_ = false;
|
| 716 |
+
|
| 717 |
+
/// The "computation" dtype of TensorIterator, specifying what the dtype
|
| 718 |
+
/// we will do the internal computation in TensorIterator. Typically,
|
| 719 |
+
/// this matches the dtype of the output tensors, but not always!
|
| 720 |
+
ScalarType common_dtype_ = ScalarType::Undefined;
|
| 721 |
+
|
| 722 |
+
/// This is currently defined as kCPU, or the device of the first non-CPU
|
| 723 |
+
/// tensor argument. See TensorIteratorBase::compute_types for details.
|
| 724 |
+
Device common_device_ = kCPU;
|
| 725 |
+
|
| 726 |
+
/// Set by split(), see should_accumulate() and is_final_output()
|
| 727 |
+
bool accumulate_ = false;
|
| 728 |
+
bool final_output_ = true;
|
| 729 |
+
|
| 730 |
+
// From TensorIteratorConfig
|
| 731 |
+
bool is_reduction_ = false;
|
| 732 |
+
|
| 733 |
+
/// Set by populate_operands(), says if we're handling meta tensors
|
| 734 |
+
bool is_meta_ = false;
|
| 735 |
+
};
|
| 736 |
+
|
| 737 |
+
struct TORCH_API TensorIterator final : public TensorIteratorBase {
|
| 738 |
+
TensorIterator() : TensorIteratorBase() {}
|
| 739 |
+
// Slicing is OK, TensorIterator guaranteed NOT to have any fields
|
| 740 |
+
TensorIterator(const TensorIteratorBase& iter) : TensorIteratorBase(iter) {}
|
| 741 |
+
|
| 742 |
+
#define TORCH_DISALLOW_TEMPORARIES(methodname) \
|
| 743 |
+
TORCH_DISALLOW_TEMPORARIES_IMPL(methodname, static)
|
| 744 |
+
|
| 745 |
+
static TensorIterator binary_float_op(
|
| 746 |
+
TensorBase& out,
|
| 747 |
+
const TensorBase& a,
|
| 748 |
+
const TensorBase& b);
|
| 749 |
+
static TensorIterator binary_op(
|
| 750 |
+
TensorBase& out,
|
| 751 |
+
const TensorBase& a,
|
| 752 |
+
const TensorBase& b);
|
| 753 |
+
static TensorIterator borrowing_binary_op(
|
| 754 |
+
const TensorBase& out,
|
| 755 |
+
const TensorBase& a,
|
| 756 |
+
const TensorBase& b);
|
| 757 |
+
TORCH_DISALLOW_TEMPORARIES(borrowing_binary_op)
|
| 758 |
+
static TensorIterator comparison_op(
|
| 759 |
+
TensorBase& out,
|
| 760 |
+
const TensorBase& a,
|
| 761 |
+
const TensorBase& b);
|
| 762 |
+
static TensorIterator unary_op(TensorBase& out, const TensorBase& a);
|
| 763 |
+
static TensorIterator unary_float_op(TensorBase& out, const TensorBase& a);
|
| 764 |
+
static TensorIterator nullary_op(TensorBase& out);
|
| 765 |
+
static TensorIterator borrowing_nullary_op(const TensorBase& out);
|
| 766 |
+
static TensorIterator borrowing_nullary_op(TensorBase&& out) = delete;
|
| 767 |
+
static TensorIterator reduce_op(TensorBase& out, const TensorBase& a);
|
| 768 |
+
static TensorIterator reduce_op(
|
| 769 |
+
TensorBase& out1,
|
| 770 |
+
TensorBase& out2,
|
| 771 |
+
const TensorBase& a);
|
| 772 |
+
#undef TORCH_DISALLOW_TEMPORARIES
|
| 773 |
+
#undef TORCH_DISALLOW_TEMPORARIES_IMPL
|
| 774 |
+
|
| 775 |
+
const Tensor& maybe_get_output(int64_t output_idx) override;
|
| 776 |
+
void set_output_raw_strided(
|
| 777 |
+
int64_t output_idx,
|
| 778 |
+
IntArrayRef sizes,
|
| 779 |
+
IntArrayRef strides,
|
| 780 |
+
TensorOptions options,
|
| 781 |
+
DimnameList names) override;
|
| 782 |
+
};
|
| 783 |
+
|
| 784 |
+
class TORCH_API TensorIteratorConfig final {
|
| 785 |
+
public:
|
| 786 |
+
friend struct TensorIteratorBase;
|
| 787 |
+
friend struct TensorIterator;
|
| 788 |
+
|
| 789 |
+
TensorIteratorConfig() = default;
|
| 790 |
+
|
| 791 |
+
C10_DISABLE_COPY_AND_ASSIGN(TensorIteratorConfig);
|
| 792 |
+
TensorIteratorConfig(TensorIteratorConfig&&) = default;
|
| 793 |
+
TensorIteratorConfig& operator=(TensorIteratorConfig&&) = default;
|
| 794 |
+
~TensorIteratorConfig() = default;
|
| 795 |
+
|
| 796 |
+
/// Construction
|
| 797 |
+
// Stores input/output Tensors without incrementing the reference count.
|
| 798 |
+
// Important: the outputs have to be added before the inputs.
|
| 799 |
+
TensorIteratorConfig& add_output(const TensorBase& output) {
|
| 800 |
+
return add_borrowed_output(output);
|
| 801 |
+
}
|
| 802 |
+
TensorIteratorConfig& add_input(const TensorBase& input) {
|
| 803 |
+
return add_borrowed_input(input);
|
| 804 |
+
}
|
| 805 |
+
TensorIteratorConfig& add_const_input(const TensorBase& input) {
|
| 806 |
+
return add_borrowed_const_input(input);
|
| 807 |
+
}
|
| 808 |
+
|
| 809 |
+
// Borrowing from temporaries is unlikely to go well.
|
| 810 |
+
TensorIteratorConfig& add_output(TensorBase&& output) = delete;
|
| 811 |
+
TensorIteratorConfig& add_input(TensorBase&& input) = delete;
|
| 812 |
+
TensorIteratorConfig& add_const_input(TensorBase&& input) = delete;
|
| 813 |
+
|
| 814 |
+
// Stores input/output Tensors while incrementing the reference count.
|
| 815 |
+
// Note that add_{in,out}put are nearly always what you
|
| 816 |
+
// want, and the exception (adding an unnamed temporary) won't
|
| 817 |
+
// compile.
|
| 818 |
+
TensorIteratorConfig& add_owned_output(const TensorBase& output);
|
| 819 |
+
TensorIteratorConfig& add_owned_input(const TensorBase& input);
|
| 820 |
+
TensorIteratorConfig& add_owned_const_input(const TensorBase& input);
|
| 821 |
+
|
| 822 |
+
// Advanced API: stores input/output Tensors without incrementing
|
| 823 |
+
// the reference count. The caller must ensure that these Tensors
|
| 824 |
+
// live at least as long as this TensorIteratorConfig and any
|
| 825 |
+
// TensorIteratorBase built from this TensorIteratorConfig.
|
| 826 |
+
// Important: the outputs have to be added before the inputs.
|
| 827 |
+
TensorIteratorConfig& add_borrowed_output(const TensorBase& output);
|
| 828 |
+
TensorIteratorConfig& add_borrowed_input(const TensorBase& input);
|
| 829 |
+
TensorIteratorConfig& add_borrowed_const_input(const TensorBase& input);
|
| 830 |
+
|
| 831 |
+
// Borrowing from temporaries is unlikely to go well.
|
| 832 |
+
TensorIteratorConfig& add_borrowed_output(TensorBase&& output) = delete;
|
| 833 |
+
TensorIteratorConfig& add_borrowed_input(TensorBase&& input) = delete;
|
| 834 |
+
TensorIteratorConfig& add_borrowed_const_input(TensorBase&& input) = delete;
|
| 835 |
+
|
| 836 |
+
// Sets the check_mem_overlap_ flag, which is true by default.
|
| 837 |
+
// If true, inputs are checked for partial overlap with the outputs and
|
| 838 |
+
// outputs are checked for internal overlap (e.g. broadcasted views). An error
|
| 839 |
+
// is raised if unacceptable overlap is detected.
|
| 840 |
+
// If you're migrating an existing operator to using TensorIterator, please
|
| 841 |
+
// consider if the previous implementation checked memory overlap. If it did
|
| 842 |
+
// not, and if the operator is idempotent (for example, Tensor.fill_(0)), then
|
| 843 |
+
// checking memory overlap is BC-breaking. Please don't check memory overlap
|
| 844 |
+
// in that case.
|
| 845 |
+
TensorIteratorConfig& set_check_mem_overlap(bool check_mem_overlap) {
|
| 846 |
+
check_mem_overlap_ = check_mem_overlap;
|
| 847 |
+
return *this;
|
| 848 |
+
}
|
| 849 |
+
|
| 850 |
+
// Sets the check_all_same_dtype_ flag, which is true by default
|
| 851 |
+
// If true, checks that all inputs and defined outputs have the same dtype
|
| 852 |
+
// Setting either of promote_inputs_to_common_dtype_
|
| 853 |
+
// or cast_common_dtype_to_outputs_ to true will set
|
| 854 |
+
// check_all_same_dtype_ to false.
|
| 855 |
+
TensorIteratorConfig& check_all_same_dtype(const bool _check_all_same_dtype) {
|
| 856 |
+
check_all_same_dtype_ = _check_all_same_dtype;
|
| 857 |
+
return *this;
|
| 858 |
+
}
|
| 859 |
+
|
| 860 |
+
// Sets the check_all_same_device_ flag, which is true by default
|
| 861 |
+
// If true, all operands must be on the same device, with the possible
|
| 862 |
+
// exception of CPU scalars, which can be passed to some CUDA kernels
|
| 863 |
+
// as kernel arguments.
|
| 864 |
+
TensorIteratorConfig& check_all_same_device(
|
| 865 |
+
const bool _check_all_same_device) {
|
| 866 |
+
check_all_same_device_ = _check_all_same_device;
|
| 867 |
+
return *this;
|
| 868 |
+
}
|
| 869 |
+
|
| 870 |
+
// Sets the enforce_safe_casting_to_output_ flag, which is false by default
|
| 871 |
+
// If true, the iterator's "common dtype" must be computable
|
| 872 |
+
// (see the [Common Dtype Computation] note) and
|
| 873 |
+
// canCast(common dtype, output dtype) must be true for all outputs.
|
| 874 |
+
TensorIteratorConfig& enforce_safe_casting_to_output(
|
| 875 |
+
const bool _enforce_safe_casting_to_output) {
|
| 876 |
+
enforce_safe_casting_to_output_ = _enforce_safe_casting_to_output;
|
| 877 |
+
return *this;
|
| 878 |
+
}
|
| 879 |
+
|
| 880 |
+
// Sets the enforce_linear_iteration_ flag, which is false by default.
|
| 881 |
+
// If true, iteration goes in the same order as a C-contiguous tensor
|
| 882 |
+
// is laid out in memory. i.e. last dimension iterates fastest.
|
| 883 |
+
//
|
| 884 |
+
// This iteration order can be less efficient and may even prevent
|
| 885 |
+
// vectorization. So only use if the correctness of your kernel depends on it.
|
| 886 |
+
TensorIteratorConfig& enforce_linear_iteration(
|
| 887 |
+
const bool _enforce_linear_iteration = true) {
|
| 888 |
+
enforce_linear_iteration_ = _enforce_linear_iteration;
|
| 889 |
+
return *this;
|
| 890 |
+
}
|
| 891 |
+
|
| 892 |
+
// Sets the promote_inputs_to_common_dtype_ flag, which is false by default
|
| 893 |
+
// If true, the iterator's "common dtype" is always computed (see the
|
| 894 |
+
// [Common Dtype Computation] note) and, on the CPU, temporary copies of
|
| 895 |
+
// the inputs in the common dtype are passed as the actual inputs to
|
| 896 |
+
// the operation.
|
| 897 |
+
// Setting this flag to true sets check_all_same_dtype_ to false.
|
| 898 |
+
TensorIteratorConfig& promote_inputs_to_common_dtype(
|
| 899 |
+
const bool _promote_inputs_to_common_dtype) {
|
| 900 |
+
promote_inputs_to_common_dtype_ = _promote_inputs_to_common_dtype;
|
| 901 |
+
if (_promote_inputs_to_common_dtype) {
|
| 902 |
+
check_all_same_dtype_ = false;
|
| 903 |
+
}
|
| 904 |
+
return *this;
|
| 905 |
+
}
|
| 906 |
+
|
| 907 |
+
// Sets the promote_integer_inputs_to_float_ flag, which is false by default
|
| 908 |
+
// NOTE: If set to true, the promote_inputs_to_common_dtype_ must also be
|
| 909 |
+
// true. If true, if the iterator's "common dtype" is an integral type
|
| 910 |
+
// (including bool)
|
| 911 |
+
// then it is changed to the default float scalar type.
|
| 912 |
+
TensorIteratorConfig& promote_integer_inputs_to_float(
|
| 913 |
+
const bool _promote_integer_inputs_to_float) {
|
| 914 |
+
promote_integer_inputs_to_float_ = _promote_integer_inputs_to_float;
|
| 915 |
+
TORCH_INTERNAL_ASSERT(
|
| 916 |
+
!promote_integer_inputs_to_float_ || promote_inputs_to_common_dtype_);
|
| 917 |
+
return *this;
|
| 918 |
+
}
|
| 919 |
+
|
| 920 |
+
TensorIteratorConfig& is_reduction(const bool _is_reduction) {
|
| 921 |
+
is_reduction_ = _is_reduction;
|
| 922 |
+
return *this;
|
| 923 |
+
}
|
| 924 |
+
|
| 925 |
+
TensorIteratorConfig& allow_cpu_scalars(const bool _allow_cpu_scalars) {
|
| 926 |
+
allow_cpu_scalars_ = _allow_cpu_scalars;
|
| 927 |
+
return *this;
|
| 928 |
+
}
|
| 929 |
+
|
| 930 |
+
// Sets the cast_common_dtype_to_outputs_ flag, which is false by default
|
| 931 |
+
// If true, the iterator's "common dtype" must be computatable
|
| 932 |
+
// (see the [Common Dtype Computation] note) and, on the CPU, temporary
|
| 933 |
+
// copies of the outputs are passed as the actual output to the operation.
|
| 934 |
+
// These temporaries are then copied to the original outputs after
|
| 935 |
+
// the operation is performed (see cast_outputs()).
|
| 936 |
+
// Setting this flag to true sets check_all_same_dtype_ to false.
|
| 937 |
+
TensorIteratorConfig& cast_common_dtype_to_outputs(
|
| 938 |
+
const bool _cast_common_dtype_to_outputs) {
|
| 939 |
+
cast_common_dtype_to_outputs_ = _cast_common_dtype_to_outputs;
|
| 940 |
+
if (_cast_common_dtype_to_outputs) {
|
| 941 |
+
check_all_same_dtype_ = false;
|
| 942 |
+
}
|
| 943 |
+
return *this;
|
| 944 |
+
}
|
| 945 |
+
|
| 946 |
+
TensorIteratorConfig& resize_outputs(bool resize_outputs) {
|
| 947 |
+
resize_outputs_ = resize_outputs;
|
| 948 |
+
return *this;
|
| 949 |
+
}
|
| 950 |
+
|
| 951 |
+
// Bypass output dtype/device computation and fix the dtype/device as
|
| 952 |
+
// specified here.
|
| 953 |
+
TensorIteratorConfig& declare_static_dtype_and_device(
|
| 954 |
+
ScalarType dtype,
|
| 955 |
+
Device device);
|
| 956 |
+
TensorIteratorConfig& declare_static_dtype(ScalarType dtype);
|
| 957 |
+
TensorIteratorConfig& declare_static_device(Device device);
|
| 958 |
+
TensorIteratorConfig& declare_static_shape(IntArrayRef shape);
|
| 959 |
+
TensorIteratorConfig& declare_static_shape(
|
| 960 |
+
IntArrayRef shape,
|
| 961 |
+
IntArrayRef squash_dims);
|
| 962 |
+
|
| 963 |
+
// It would be better if this was && qualified, but this would be at the cost
|
| 964 |
+
// of a lot of boilerplate above
|
| 965 |
+
TensorIterator build() {
|
| 966 |
+
TensorIterator iter;
|
| 967 |
+
iter.build(*this);
|
| 968 |
+
return iter;
|
| 969 |
+
}
|
| 970 |
+
|
| 971 |
+
private:
|
| 972 |
+
bool is_tensor_const(size_t idx);
|
| 973 |
+
|
| 974 |
+
SmallVector<c10::MaybeOwned<TensorBase>, 4> tensors_;
|
| 975 |
+
int num_outputs_ = 0;
|
| 976 |
+
int num_inputs_ = 0;
|
| 977 |
+
|
| 978 |
+
std::optional<DimVector> static_shape_ = std::nullopt;
|
| 979 |
+
std::optional<ScalarType> static_dtype_ = std::nullopt;
|
| 980 |
+
std::optional<Device> static_device_ = std::nullopt;
|
| 981 |
+
bool check_mem_overlap_ = true;
|
| 982 |
+
bool allow_cpu_scalars_ = false;
|
| 983 |
+
bool is_reduction_ = false;
|
| 984 |
+
bool resize_outputs_ = true;
|
| 985 |
+
bool check_all_same_dtype_ = true;
|
| 986 |
+
bool check_all_same_device_ = true;
|
| 987 |
+
bool enforce_safe_casting_to_output_ = false;
|
| 988 |
+
bool enforce_linear_iteration_ = false;
|
| 989 |
+
bool promote_inputs_to_common_dtype_ = false;
|
| 990 |
+
bool promote_integer_inputs_to_float_ = false;
|
| 991 |
+
bool cast_common_dtype_to_outputs_ = false;
|
| 992 |
+
|
| 993 |
+
SmallVector<size_t, 4> const_tensor_indices_;
|
| 994 |
+
};
|
| 995 |
+
|
| 996 |
+
/// A container-like struct that acts as if it contains splits of a
|
| 997 |
+
/// TensorIterator that can use 32-bit indexing. Taken together the splits cover
|
| 998 |
+
/// the original TensorIterator.
|
| 999 |
+
struct TORCH_API SplitUntil32Bit {
|
| 1000 |
+
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
|
| 1001 |
+
struct TORCH_API iterator {
|
| 1002 |
+
iterator() = default;
|
| 1003 |
+
iterator(const TensorIteratorBase& iter);
|
| 1004 |
+
iterator(iterator&&) = default;
|
| 1005 |
+
iterator& operator=(iterator&&) = default;
|
| 1006 |
+
~iterator() = default;
|
| 1007 |
+
|
| 1008 |
+
// Guaranteed to be a TensorIterator proper!
|
| 1009 |
+
TensorIterator& operator*() const;
|
| 1010 |
+
iterator& operator++();
|
| 1011 |
+
bool operator==(const iterator& other) const {
|
| 1012 |
+
// two iterators are equal if they are the same object or they're both
|
| 1013 |
+
// empty
|
| 1014 |
+
return this == &other || (vec.empty() && other.vec.empty());
|
| 1015 |
+
}
|
| 1016 |
+
// needed for C++11 range-based for loop
|
| 1017 |
+
bool operator!=(const iterator& other) const {
|
| 1018 |
+
return !(*this == other);
|
| 1019 |
+
}
|
| 1020 |
+
|
| 1021 |
+
/// stack of TensorIterators to be split
|
| 1022 |
+
std::vector<std::unique_ptr<TensorIterator>> vec;
|
| 1023 |
+
};
|
| 1024 |
+
|
| 1025 |
+
SplitUntil32Bit(const TensorIteratorBase& iter) : iter(iter) {}
|
| 1026 |
+
|
| 1027 |
+
iterator begin() const;
|
| 1028 |
+
iterator end() const;
|
| 1029 |
+
|
| 1030 |
+
private:
|
| 1031 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 1032 |
+
const TensorIteratorBase& iter;
|
| 1033 |
+
};
|
| 1034 |
+
|
| 1035 |
+
} // namespace at
|
| 1036 |
+
|
| 1037 |
+
#else
|
| 1038 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 1039 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorIteratorInternal.h
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <ATen/native/TensorIterator.h>
|
| 4 |
+
#include <c10/util/SmallBuffer.h>
|
| 5 |
+
#include <c10/util/irange.h>
|
| 6 |
+
|
| 7 |
+
namespace at {
|
| 8 |
+
|
| 9 |
+
struct DimCounter {
|
| 10 |
+
DimCounter(IntArrayRef shape, Range range);
|
| 11 |
+
|
| 12 |
+
void increment(const std::array<int64_t, 2>& step);
|
| 13 |
+
bool is_done() const;
|
| 14 |
+
std::array<int64_t, 2> max_2d_step() const;
|
| 15 |
+
|
| 16 |
+
IntArrayRef shape;
|
| 17 |
+
Range range;
|
| 18 |
+
c10::SmallBuffer<int64_t, 4> values;
|
| 19 |
+
int64_t offset;
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
namespace internal {
|
| 23 |
+
|
| 24 |
+
inline void get_data_ptrs(
|
| 25 |
+
char** ptrs,
|
| 26 |
+
ArrayRef<char*> base,
|
| 27 |
+
IntArrayRef strides,
|
| 28 |
+
IntArrayRef counter) {
|
| 29 |
+
const auto ntensors = base.size();
|
| 30 |
+
const auto ndim = counter.size();
|
| 31 |
+
std::copy(base.begin(), base.end(), ptrs);
|
| 32 |
+
for (const auto dim : c10::irange(ndim)) {
|
| 33 |
+
int64_t value = counter[dim];
|
| 34 |
+
for (const auto arg : c10::irange(ntensors)) {
|
| 35 |
+
ptrs[arg] += value * strides[dim * ntensors + arg];
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
inline void serial_for_each(
|
| 41 |
+
IntArrayRef shape,
|
| 42 |
+
IntArrayRef strides,
|
| 43 |
+
char** base_ptrs,
|
| 44 |
+
size_t ntensors,
|
| 45 |
+
TensorIteratorBase::loop2d_t loop,
|
| 46 |
+
Range range) {
|
| 47 |
+
const auto ndim = shape.size();
|
| 48 |
+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
|
| 49 |
+
strides.size() == ntensors * std::max(size_t{2}, ndim));
|
| 50 |
+
|
| 51 |
+
if (ndim <= 1) {
|
| 52 |
+
if (range.begin == 0) {
|
| 53 |
+
loop(base_ptrs, strides.data(), range.size(), 1);
|
| 54 |
+
} else {
|
| 55 |
+
c10::SmallBuffer<char*, 4> ptrs(ntensors);
|
| 56 |
+
get_data_ptrs(ptrs.data(), {base_ptrs, ntensors}, strides, {range.begin});
|
| 57 |
+
loop(ptrs.data(), strides.data(), range.size(), 1);
|
| 58 |
+
}
|
| 59 |
+
} else {
|
| 60 |
+
c10::SmallBuffer<char*, 4> ptrs(ntensors);
|
| 61 |
+
auto counter = DimCounter(shape, range);
|
| 62 |
+
while (!counter.is_done()) {
|
| 63 |
+
get_data_ptrs(
|
| 64 |
+
ptrs.data(), {base_ptrs, ntensors}, strides, counter.values);
|
| 65 |
+
auto step = counter.max_2d_step();
|
| 66 |
+
loop(ptrs.data(), strides.data(), step[0], step[1]);
|
| 67 |
+
counter.increment(step);
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
} // namespace internal
|
| 73 |
+
} // namespace at
|
| 74 |
+
|
| 75 |
+
#else
|
| 76 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 77 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorMeta.h
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/DimVector.h>
|
| 5 |
+
#include <ATen/core/Dimname.h>
|
| 6 |
+
#include <c10/core/TensorOptions.h>
|
| 7 |
+
#include <c10/util/strides.h>
|
| 8 |
+
|
| 9 |
+
namespace at {
|
| 10 |
+
|
| 11 |
+
class Tensor;
|
| 12 |
+
|
| 13 |
+
namespace impl {
|
| 14 |
+
|
| 15 |
+
// Use this to define the prototype for a meta function. There are two
|
| 16 |
+
// versions; one that takes one argument (just the operator name), or FUNC2
|
| 17 |
+
// variant that takes two arguments (operator name and overload name).
|
| 18 |
+
//
|
| 19 |
+
// Example usage:
|
| 20 |
+
//
|
| 21 |
+
// TORCH_META_FUNC2(add, Tensor) (
|
| 22 |
+
// const Tensor& self, const Tensor& other
|
| 23 |
+
// ) {
|
| 24 |
+
// ... compute sizes and options ...
|
| 25 |
+
// set_output(sizes, options);
|
| 26 |
+
// }
|
| 27 |
+
//
|
| 28 |
+
#define TORCH_META_FUNC(name) void structured_##name::meta
|
| 29 |
+
#define TORCH_META_FUNC2(name, overload) \
|
| 30 |
+
void structured_##name##_##overload::meta
|
| 31 |
+
|
| 32 |
+
// These are versions of TORCH_META_FUNC(2) that include a precompute_out struct
|
| 33 |
+
// as a return value. They should be used when the kernel in question has
|
| 34 |
+
// precomputed values declared in native_functions.yaml and the corresponding
|
| 35 |
+
// implementation should return an instance of the aforementioned struct.
|
| 36 |
+
#define TORCH_PRECOMPUTE_META_FUNC(name) \
|
| 37 |
+
structured_##name::meta_return_ty structured_##name::meta
|
| 38 |
+
#define TORCH_PRECOMPUTE_META_FUNC2(name, overload) \
|
| 39 |
+
structured_##name##_##overload::meta_return_ty \
|
| 40 |
+
structured_##name##_##overload::meta
|
| 41 |
+
|
| 42 |
+
// Use this to create a precompute struct in a meta function.
|
| 43 |
+
#define TORCH_PRECOMPUTE_STRUCT(name) structured_##name::precompute_out<>
|
| 44 |
+
#define TORCH_PRECOMPUTE_STRUCT2(name, overload) \
|
| 45 |
+
structured_##name##_##overload::precompute_out<>
|
| 46 |
+
|
| 47 |
+
// Use this to define the prototype for an implementation. This takes only
|
| 48 |
+
// one argument, which is the name of the dispatch key entry you're
|
| 49 |
+
// implementing.
|
| 50 |
+
//
|
| 51 |
+
// Example usage:
|
| 52 |
+
//
|
| 53 |
+
// TORCH_IMPL_FUNC(add_cpu) (
|
| 54 |
+
// Tensor& result, const Tensor& self, const Tensor& other
|
| 55 |
+
// ) {
|
| 56 |
+
// ... do the actual implementation ...
|
| 57 |
+
// }
|
| 58 |
+
//
|
| 59 |
+
#define TORCH_IMPL_FUNC(name) void structured_##name::impl
|
| 60 |
+
|
| 61 |
+
// Base class for all structured kernel classes. The set_output virtual
|
| 62 |
+
// method is varied depending whether or not the operator is
|
| 63 |
+
// functional/out/inplace, and could also be specialized for CPU/CUDA/etc
|
| 64 |
+
// (although presently it isn't).
|
| 65 |
+
//
|
| 66 |
+
// A notable subclass of this interface is TensorIteratorBase.
|
| 67 |
+
struct TORCH_API MetaBase {
|
| 68 |
+
MetaBase() = default;
|
| 69 |
+
MetaBase(const MetaBase&) = default;
|
| 70 |
+
MetaBase& operator=(const MetaBase&) = default;
|
| 71 |
+
MetaBase(MetaBase&&) noexcept = default;
|
| 72 |
+
MetaBase& operator=(MetaBase&&) noexcept = default;
|
| 73 |
+
virtual const Tensor& maybe_get_output(int64_t output_idx) = 0;
|
| 74 |
+
|
| 75 |
+
// Note: [set_output_*]
|
| 76 |
+
// See: https://github.com/pytorch/pytorch/issues/69813
|
| 77 |
+
// Whenever defining the output properties in the META function of a
|
| 78 |
+
// structured kernel (what was usually done with `set_output`), use one of
|
| 79 |
+
// these 3 variants, instead. In order to decide which variant to use, check
|
| 80 |
+
// the following decision tree:
|
| 81 |
+
//
|
| 82 |
+
// - Can the kernel you are going to implement support output tensors
|
| 83 |
+
// with arbitrary strides?
|
| 84 |
+
// |
|
| 85 |
+
// -- YES: `set_output_raw_strided`
|
| 86 |
+
// |
|
| 87 |
+
// -- NO: Should the output tensor strides be contiguous?
|
| 88 |
+
// |
|
| 89 |
+
// -- YES: `set_output_contiguous`
|
| 90 |
+
// |
|
| 91 |
+
// -- NO: `set_output_strided`
|
| 92 |
+
//
|
| 93 |
+
// Use this function whenever the kernel requires specific strides for the
|
| 94 |
+
// output. If `strides` does not match the given output strides, proxy outputs
|
| 95 |
+
// will be created and passed to the IMPL function.
|
| 96 |
+
virtual void set_output_strided(
|
| 97 |
+
int64_t output_idx [[maybe_unused]],
|
| 98 |
+
IntArrayRef sizes [[maybe_unused]],
|
| 99 |
+
IntArrayRef strides [[maybe_unused]],
|
| 100 |
+
TensorOptions options [[maybe_unused]],
|
| 101 |
+
DimnameList names [[maybe_unused]] = {}) {
|
| 102 |
+
TORCH_INTERNAL_ASSERT(false, "set_output_strided not implemented.");
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
// Use this function whenever the kernel knows how to handle arbitrary strided
|
| 106 |
+
// outputs. This function has the same behavior as the old `set_output`: it
|
| 107 |
+
// will only re-stride if the given output was resized.
|
| 108 |
+
virtual void set_output_raw_strided(
|
| 109 |
+
int64_t output_idx [[maybe_unused]],
|
| 110 |
+
IntArrayRef sizes [[maybe_unused]],
|
| 111 |
+
IntArrayRef strides_hint [[maybe_unused]],
|
| 112 |
+
TensorOptions options [[maybe_unused]],
|
| 113 |
+
DimnameList names [[maybe_unused]] = {}) {
|
| 114 |
+
TORCH_INTERNAL_ASSERT(false, "set_output_strided not implemented.");
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// Use this function if the kernel requires contiguous strides.
|
| 118 |
+
// Alias for `set_output_strided`, but with contiguous strides.
|
| 119 |
+
void set_output_contiguous(
|
| 120 |
+
int64_t output_idx,
|
| 121 |
+
IntArrayRef sizes,
|
| 122 |
+
TensorOptions options,
|
| 123 |
+
DimnameList names = {}) {
|
| 124 |
+
auto strides = c10::contiguous_strides(sizes);
|
| 125 |
+
set_output_strided(output_idx, sizes, strides, options, names);
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
// Returns a reference to an undefined tensor if there is no presupplied
|
| 129 |
+
// output
|
| 130 |
+
const Tensor& maybe_get_output() {
|
| 131 |
+
return maybe_get_output(0);
|
| 132 |
+
}
|
| 133 |
+
virtual ~MetaBase() = default;
|
| 134 |
+
};
|
| 135 |
+
|
| 136 |
+
} // namespace impl
|
| 137 |
+
|
| 138 |
+
} // namespace at
|
| 139 |
+
|
| 140 |
+
#else
|
| 141 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 142 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorNames.h
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/WrapDimUtils.h>
|
| 5 |
+
|
| 6 |
+
namespace at::namedinference {
|
| 7 |
+
|
| 8 |
+
// TensorName and TensorNames are wrappers around Dimname and DimnameList
|
| 9 |
+
// that contain helper functions to make writing name inference rules easier.
|
| 10 |
+
//
|
| 11 |
+
// A TensorName represents a Dimname associated with some DimnameList (from a
|
| 12 |
+
// Tensor). This encapsulates all the information that is needed to check if
|
| 13 |
+
// names *match* and to *unify* names.
|
| 14 |
+
//
|
| 15 |
+
// Definition: Two names in two tensors *match* if they are equal, or if at
|
| 16 |
+
// least one of them is a wildcard that can be *refined* to the other name.
|
| 17 |
+
//
|
| 18 |
+
// Definition: unify(name, other) fails if the names do not match. Otherwise,
|
| 19 |
+
// it returns the most refined of name and other.
|
| 20 |
+
//
|
| 21 |
+
// Here is an example of checking if two names match.
|
| 22 |
+
// tensor: Tensor[A, None]
|
| 23 |
+
// other: Tensor[A]
|
| 24 |
+
//
|
| 25 |
+
// Let's say we wish to check if tensor.names[-1] matches other.names[-1].
|
| 26 |
+
// None (in tensor) cannot match A (in other) because if the None were refined
|
| 27 |
+
// to A, `tensor` would have duplicate names [A, A]. Therefore we need to check
|
| 28 |
+
// tensor.names [A, None] for the existence of A.
|
| 29 |
+
struct TORCH_API TensorName {
|
| 30 |
+
explicit TensorName(ArrayRef<Dimname> origin, int origin_idx)
|
| 31 |
+
: origin_(origin),
|
| 32 |
+
name_(origin[maybe_wrap_dim(
|
| 33 |
+
origin_idx,
|
| 34 |
+
static_cast<int64_t>(origin.size()))]),
|
| 35 |
+
origin_idx_(origin_idx) {}
|
| 36 |
+
|
| 37 |
+
// op_name is only used for error reporting.
|
| 38 |
+
const TensorName& unify(const TensorName& other, const char* op_name) const;
|
| 39 |
+
Dimname toDimname() const;
|
| 40 |
+
|
| 41 |
+
private:
|
| 42 |
+
ArrayRef<Dimname> origin_;
|
| 43 |
+
Dimname name_;
|
| 44 |
+
int origin_idx_; // A named tensor can have at most 64 dims.
|
| 45 |
+
|
| 46 |
+
TORCH_API friend std::ostream& operator<<(
|
| 47 |
+
std::ostream& out,
|
| 48 |
+
const TensorName& tensorname);
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
using TensorNameVec = SmallVector<TensorName, 10>;
|
| 52 |
+
|
| 53 |
+
struct TORCH_API TensorNames {
|
| 54 |
+
explicit TensorNames(ArrayRef<Dimname> names);
|
| 55 |
+
|
| 56 |
+
// Create TensorNames from names[start:end]. Each individual TensorName stores
|
| 57 |
+
// `names`, NOT names[start:end], because the original tensor's names are
|
| 58 |
+
// `names`.
|
| 59 |
+
explicit TensorNames(ArrayRef<Dimname> names, int64_t start, int64_t end);
|
| 60 |
+
|
| 61 |
+
// op_name is only used for error reporting.
|
| 62 |
+
TensorNames& unifyFromRightInplace(
|
| 63 |
+
const TensorNames& other,
|
| 64 |
+
const char* op_name = "unify");
|
| 65 |
+
void checkUnique(const char* op_name) const;
|
| 66 |
+
|
| 67 |
+
void append(TensorName name);
|
| 68 |
+
std::vector<Dimname> toDimnameVec() const;
|
| 69 |
+
|
| 70 |
+
private:
|
| 71 |
+
explicit TensorNames(TensorNameVec&& names) : names_(std::move(names)) {}
|
| 72 |
+
|
| 73 |
+
TensorNameVec names_;
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
} // namespace at::namedinference
|
| 77 |
+
|
| 78 |
+
#else
|
| 79 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 80 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorOperators.h
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/Tensor.h>
|
| 5 |
+
#include <c10/core/Scalar.h>
|
| 6 |
+
|
| 7 |
+
#ifndef AT_PER_OPERATOR_HEADERS
|
| 8 |
+
#include <ATen/Functions.h>
|
| 9 |
+
#else
|
| 10 |
+
#include <ATen/ops/empty_like.h>
|
| 11 |
+
#endif
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
|
| 15 |
+
#define AT_FORALL_BINARY_OPS(_) \
|
| 16 |
+
_(+, x.add(y), y.add(x)) \
|
| 17 |
+
_(*, x.mul(y), y.mul(x)) \
|
| 18 |
+
_(-, \
|
| 19 |
+
x.sub(y), \
|
| 20 |
+
::at::empty_like(y, at::MemoryFormat::Preserve).fill_(x).sub_(y)) \
|
| 21 |
+
_(/, \
|
| 22 |
+
x.div(y), \
|
| 23 |
+
::at::empty_like(y, at::MemoryFormat::Preserve).fill_(x).div_(y)) \
|
| 24 |
+
_(%, \
|
| 25 |
+
x.remainder(y), \
|
| 26 |
+
::at::empty_like(y, at::MemoryFormat::Preserve).fill_(x).remainder_(y)) \
|
| 27 |
+
_(&, x.bitwise_and(y), y.bitwise_and(x)) \
|
| 28 |
+
_(|, x.bitwise_or(y), y.bitwise_or(x)) \
|
| 29 |
+
_(^, x.bitwise_xor(y), y.bitwise_xor(x)) \
|
| 30 |
+
_(<, x.lt(y), y.gt(x)) \
|
| 31 |
+
_(<=, x.le(y), y.ge(x)) \
|
| 32 |
+
_(>, x.gt(y), y.lt(x)) \
|
| 33 |
+
_(>=, x.ge(y), y.le(x)) \
|
| 34 |
+
_(==, x.eq(y), y.eq(x)) \
|
| 35 |
+
_(!=, x.ne(y), y.ne(x))
|
| 36 |
+
|
| 37 |
+
#define DEFINE_OPERATOR(op, body, reverse_scalar_body) \
|
| 38 |
+
inline Tensor operator op(const Tensor& x, const Tensor& y) { \
|
| 39 |
+
return body; \
|
| 40 |
+
} \
|
| 41 |
+
inline Tensor operator op(const Tensor& x, const Scalar& y) { \
|
| 42 |
+
return body; \
|
| 43 |
+
} \
|
| 44 |
+
inline Tensor operator op(const Scalar& x, const Tensor& y) { \
|
| 45 |
+
return reverse_scalar_body; \
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
AT_FORALL_BINARY_OPS(DEFINE_OPERATOR)
|
| 49 |
+
#undef DEFINE_OPERATOR
|
| 50 |
+
#undef AT_FORALL_BINARY_OPS
|
| 51 |
+
|
| 52 |
+
} // namespace at
|
| 53 |
+
|
| 54 |
+
#else
|
| 55 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 56 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorOptions.h
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <c10/core/TensorOptions.h>
|
| 4 |
+
|
| 5 |
+
#else
|
| 6 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 7 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorSubclassLikeUtils.h
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <ATen/core/List.h>
|
| 4 |
+
#include <ATen/core/Tensor.h>
|
| 5 |
+
#include <c10/core/impl/TorchDispatchModeTLS.h>
|
| 6 |
+
|
| 7 |
+
#ifndef AT_PER_OPERATOR_HEADERS
|
| 8 |
+
#include <ATen/Functions.h>
|
| 9 |
+
#else
|
| 10 |
+
#include <ATen/ops/equal.h>
|
| 11 |
+
#endif
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
|
| 15 |
+
// Note [Tensor-subclass-like Tensors]
|
| 16 |
+
// Tensor-subclass-like is defined as:
|
| 17 |
+
// - a Tensor subclass (via __torch_dispatch__ in Python or extending
|
| 18 |
+
// TensorImpl in C++)
|
| 19 |
+
// - anything else that shares the same perils as Tensor subclasses.
|
| 20 |
+
// For example, many Tensor subclasses do not have storage and meta Tensors
|
| 21 |
+
// do not have storage either, so meta Tensors belong here.
|
| 22 |
+
//
|
| 23 |
+
// We should ensure that PyTorch internals supports Tensor-subclass-like
|
| 24 |
+
// objects. In particular, Tensor-subclass-like objects struggle with two
|
| 25 |
+
// classes of operations that are problematic for Tensor subclasses:
|
| 26 |
+
// 1. Because some Tensor subclasses do not have storage, .item() or
|
| 27 |
+
// .data_ptr() calls are not good.
|
| 28 |
+
// 2. Certain in-place operations can eliminate the typing of the Tensor
|
| 29 |
+
// subclass. For example:
|
| 30 |
+
// >>> torch.zeros(input.sizes(), grad.options()).diag().copy_(input)
|
| 31 |
+
// If input is a Tensor subclass, then the above ends up either erroring out
|
| 32 |
+
// or returning a regular non-Tensor-subclass Tensor!
|
| 33 |
+
|
| 34 |
+
constexpr auto kFunctorchWrappedTensors = DispatchKeySet(
|
| 35 |
+
{DispatchKey::FuncTorchGradWrapper,
|
| 36 |
+
DispatchKey::FuncTorchBatched,
|
| 37 |
+
DispatchKey::Functionalize});
|
| 38 |
+
|
| 39 |
+
constexpr auto kTensorSubclassLike =
|
| 40 |
+
kFunctorchWrappedTensors |
|
| 41 |
+
DispatchKeySet(
|
| 42 |
+
{// WARNING: DO NOT put combined backend component + functionality keys
|
| 43 |
+
// here, you will incorrectly always match on the functionality key
|
| 44 |
+
// no matter the backend component
|
| 45 |
+
DispatchKey::Batched,
|
| 46 |
+
DispatchKey::Sparse,
|
| 47 |
+
DispatchKey::SparseCsr,
|
| 48 |
+
DispatchKey::Python}) |
|
| 49 |
+
DispatchKeySet(BackendComponent::MetaBit);
|
| 50 |
+
|
| 51 |
+
inline bool isTensorSubclassLike(const Tensor& tensor) {
|
| 52 |
+
if (c10::impl::dispatch_mode_enabled())
|
| 53 |
+
return true;
|
| 54 |
+
auto key_set = tensor.unsafeGetTensorImpl()->key_set();
|
| 55 |
+
return !(key_set & kTensorSubclassLike).empty();
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
inline bool areAnyTensorSubclassLike(TensorList tensors) {
|
| 59 |
+
if (c10::impl::dispatch_mode_enabled())
|
| 60 |
+
return true;
|
| 61 |
+
return std::any_of(tensors.begin(), tensors.end(), isTensorSubclassLike);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
inline bool areAnyOptionalTensorSubclassLike(
|
| 65 |
+
const c10::List<std::optional<Tensor>>& tensors) {
|
| 66 |
+
if (c10::impl::dispatch_mode_enabled())
|
| 67 |
+
return true;
|
| 68 |
+
return std::any_of(
|
| 69 |
+
tensors.begin(),
|
| 70 |
+
tensors.end(),
|
| 71 |
+
[](const std::optional<Tensor>& opt_tensor) {
|
| 72 |
+
return (
|
| 73 |
+
opt_tensor.has_value() && isTensorSubclassLike(opt_tensor.value()));
|
| 74 |
+
});
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// Helper function to deal testing truthfulness of a scalar tensor
|
| 78 |
+
// in a Composite Compliant manner.
|
| 79 |
+
// NOTE: This function expects a scalar tensor of boolean dtype.
|
| 80 |
+
// Eg.
|
| 81 |
+
// Non-Composite Compliant Pattern : (t == 0).all().item<bool>()
|
| 82 |
+
// Composite Compliant Pattern : is_salar_tensor_true((t == 0).all())
|
| 83 |
+
inline bool is_scalar_tensor_true(const Tensor& t) {
|
| 84 |
+
TORCH_INTERNAL_ASSERT(t.dim() == 0)
|
| 85 |
+
TORCH_INTERNAL_ASSERT(t.scalar_type() == kBool)
|
| 86 |
+
return at::equal(t, t.new_ones({}, t.options()));
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
} // namespace at
|
| 90 |
+
|
| 91 |
+
#else
|
| 92 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 93 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TensorUtils.h
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/DimVector.h>
|
| 5 |
+
#include <ATen/EmptyTensor.h>
|
| 6 |
+
#include <ATen/Tensor.h>
|
| 7 |
+
#include <ATen/TensorGeometry.h>
|
| 8 |
+
#include <ATen/Utils.h>
|
| 9 |
+
|
| 10 |
+
#include <utility>
|
| 11 |
+
|
| 12 |
+
// These functions are NOT in Utils.h, because this file has a dep on Tensor.h
|
| 13 |
+
|
| 14 |
+
#define TORCH_CHECK_TENSOR_ALL(cond, ...) \
|
| 15 |
+
TORCH_CHECK((cond)._is_all_true().item<bool>(), __VA_ARGS__);
|
| 16 |
+
|
| 17 |
+
namespace at {
|
| 18 |
+
|
| 19 |
+
// The following are utility functions for checking that arguments
|
| 20 |
+
// make sense. These are particularly useful for native functions,
|
| 21 |
+
// which do NO argument checking by default.
|
| 22 |
+
|
| 23 |
+
struct TORCH_API TensorArg {
|
| 24 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 25 |
+
const Tensor& tensor;
|
| 26 |
+
const char* name;
|
| 27 |
+
int pos; // 1-indexed
|
| 28 |
+
TensorArg(const Tensor& tensor, const char* name, int pos)
|
| 29 |
+
: tensor(tensor), name(name), pos(pos) {}
|
| 30 |
+
// Try to mitigate any possibility of dangling reference to temporaries.
|
| 31 |
+
// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
|
| 32 |
+
TensorArg(Tensor&& tensor, const char* name, int pos) = delete;
|
| 33 |
+
const Tensor* operator->() const {
|
| 34 |
+
return &tensor;
|
| 35 |
+
}
|
| 36 |
+
const Tensor& operator*() const {
|
| 37 |
+
return tensor;
|
| 38 |
+
}
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
struct TORCH_API TensorGeometryArg {
|
| 42 |
+
TensorGeometry tensor;
|
| 43 |
+
const char* name;
|
| 44 |
+
int pos; // 1-indexed
|
| 45 |
+
/* implicit */ TensorGeometryArg(TensorArg arg)
|
| 46 |
+
: tensor(TensorGeometry{arg.tensor}), name(arg.name), pos(arg.pos) {}
|
| 47 |
+
TensorGeometryArg(TensorGeometry tensor, const char* name, int pos)
|
| 48 |
+
: tensor(std::move(tensor)), name(name), pos(pos) {}
|
| 49 |
+
const TensorGeometry* operator->() const {
|
| 50 |
+
return &tensor;
|
| 51 |
+
}
|
| 52 |
+
const TensorGeometry& operator*() const {
|
| 53 |
+
return tensor;
|
| 54 |
+
}
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
// A string describing which function did checks on its input
|
| 58 |
+
// arguments.
|
| 59 |
+
// TODO: Consider generalizing this into a call stack.
|
| 60 |
+
using CheckedFrom = const char*;
|
| 61 |
+
|
| 62 |
+
// The undefined convention: singular operators assume their arguments
|
| 63 |
+
// are defined, but functions which take multiple tensors will
|
| 64 |
+
// implicitly filter out undefined tensors (to make it easier to perform
|
| 65 |
+
// tests which should apply if the tensor is defined, and should not
|
| 66 |
+
// otherwise.)
|
| 67 |
+
//
|
| 68 |
+
// NB: This means that the n-ary operators take lists of TensorArg,
|
| 69 |
+
// not TensorGeometryArg, because the Tensor to TensorGeometry
|
| 70 |
+
// conversion will blow up if you have undefined tensors.
|
| 71 |
+
|
| 72 |
+
TORCH_API std::ostream& operator<<(
|
| 73 |
+
std::ostream& out,
|
| 74 |
+
const TensorGeometryArg& t);
|
| 75 |
+
TORCH_API void checkDim(
|
| 76 |
+
CheckedFrom c,
|
| 77 |
+
const Tensor& tensor,
|
| 78 |
+
const char* name,
|
| 79 |
+
int pos, // 1-indexed
|
| 80 |
+
int64_t dim);
|
| 81 |
+
TORCH_API void checkDim(CheckedFrom c, const TensorGeometryArg& t, int64_t dim);
|
| 82 |
+
// NB: this is an inclusive-exclusive range
|
| 83 |
+
TORCH_API void checkDimRange(
|
| 84 |
+
CheckedFrom c,
|
| 85 |
+
const TensorGeometryArg& t,
|
| 86 |
+
int64_t dim_start,
|
| 87 |
+
int64_t dim_end);
|
| 88 |
+
TORCH_API void checkSameDim(
|
| 89 |
+
CheckedFrom c,
|
| 90 |
+
const TensorGeometryArg& t1,
|
| 91 |
+
const TensorGeometryArg& t2);
|
| 92 |
+
TORCH_API void checkContiguous(CheckedFrom c, const TensorGeometryArg& t);
|
| 93 |
+
TORCH_API void checkAllContiguous(CheckedFrom c, at::ArrayRef<TensorArg> ts);
|
| 94 |
+
TORCH_API void checkSize(
|
| 95 |
+
CheckedFrom c,
|
| 96 |
+
const TensorGeometryArg& t,
|
| 97 |
+
IntArrayRef sizes);
|
| 98 |
+
TORCH_API void checkSize_symint(
|
| 99 |
+
CheckedFrom c,
|
| 100 |
+
const TensorGeometryArg& t,
|
| 101 |
+
c10::SymIntArrayRef sizes);
|
| 102 |
+
TORCH_API void checkSize(
|
| 103 |
+
CheckedFrom c,
|
| 104 |
+
const TensorGeometryArg& t,
|
| 105 |
+
int64_t dim,
|
| 106 |
+
int64_t size);
|
| 107 |
+
TORCH_API void checkSize_symint(
|
| 108 |
+
CheckedFrom c,
|
| 109 |
+
const TensorGeometryArg& t,
|
| 110 |
+
int64_t dim,
|
| 111 |
+
const c10::SymInt& size);
|
| 112 |
+
TORCH_API void checkNumel(
|
| 113 |
+
CheckedFrom c,
|
| 114 |
+
const TensorGeometryArg& t,
|
| 115 |
+
int64_t numel);
|
| 116 |
+
TORCH_API void checkSameNumel(
|
| 117 |
+
CheckedFrom c,
|
| 118 |
+
const TensorArg& t1,
|
| 119 |
+
const TensorArg& t2);
|
| 120 |
+
TORCH_API void checkAllSameNumel(CheckedFrom c, ArrayRef<TensorArg> tensors);
|
| 121 |
+
TORCH_API void checkScalarType(CheckedFrom c, const TensorArg& t, ScalarType s);
|
| 122 |
+
TORCH_API void checkScalarTypes(
|
| 123 |
+
CheckedFrom c,
|
| 124 |
+
const TensorArg& t,
|
| 125 |
+
at::ArrayRef<ScalarType> l);
|
| 126 |
+
TORCH_API void checkSameGPU(
|
| 127 |
+
CheckedFrom c,
|
| 128 |
+
const TensorArg& t1,
|
| 129 |
+
const TensorArg& t2);
|
| 130 |
+
TORCH_API void checkAllSameGPU(CheckedFrom c, ArrayRef<TensorArg> tensors);
|
| 131 |
+
TORCH_API void checkSameType(
|
| 132 |
+
CheckedFrom c,
|
| 133 |
+
const TensorArg& t1,
|
| 134 |
+
const TensorArg& t2);
|
| 135 |
+
TORCH_API void checkAllSameType(CheckedFrom c, ArrayRef<TensorArg> tensors);
|
| 136 |
+
TORCH_API void checkSameSize(
|
| 137 |
+
CheckedFrom c,
|
| 138 |
+
const TensorArg& t1,
|
| 139 |
+
const TensorArg& t2);
|
| 140 |
+
TORCH_API void checkAllSameSize(CheckedFrom c, ArrayRef<TensorArg> tensors);
|
| 141 |
+
TORCH_API void checkDefined(CheckedFrom c, const TensorArg& t);
|
| 142 |
+
TORCH_API void checkAllDefined(CheckedFrom c, at::ArrayRef<TensorArg> t);
|
| 143 |
+
|
| 144 |
+
// FixMe: does TensorArg slow things down?
|
| 145 |
+
TORCH_API void checkBackend(
|
| 146 |
+
CheckedFrom c,
|
| 147 |
+
at::ArrayRef<Tensor> t,
|
| 148 |
+
at::Backend backend);
|
| 149 |
+
|
| 150 |
+
TORCH_API void checkDeviceType(
|
| 151 |
+
CheckedFrom c,
|
| 152 |
+
at::ArrayRef<Tensor> tensors,
|
| 153 |
+
at::DeviceType device_type);
|
| 154 |
+
|
| 155 |
+
TORCH_API void checkLayout(CheckedFrom c, const Tensor& t, Layout layout);
|
| 156 |
+
|
| 157 |
+
TORCH_API void checkLayout(
|
| 158 |
+
CheckedFrom c,
|
| 159 |
+
at::ArrayRef<Tensor> tensors,
|
| 160 |
+
at::Layout layout);
|
| 161 |
+
|
| 162 |
+
// Methods for getting data_ptr if tensor is defined
|
| 163 |
+
TORCH_API void* maybe_data_ptr(const Tensor& tensor);
|
| 164 |
+
TORCH_API void* maybe_data_ptr(const TensorArg& tensor);
|
| 165 |
+
|
| 166 |
+
TORCH_API void check_dim_size(
|
| 167 |
+
const Tensor& tensor,
|
| 168 |
+
int64_t dim,
|
| 169 |
+
int64_t dim_size,
|
| 170 |
+
int64_t size);
|
| 171 |
+
|
| 172 |
+
namespace detail {
|
| 173 |
+
TORCH_API std::vector<int64_t> defaultStrides(IntArrayRef sizes);
|
| 174 |
+
|
| 175 |
+
TORCH_API std::optional<std::vector<int64_t>> computeStride(
|
| 176 |
+
IntArrayRef oldshape,
|
| 177 |
+
IntArrayRef oldstride,
|
| 178 |
+
IntArrayRef newshape);
|
| 179 |
+
|
| 180 |
+
TORCH_API std::optional<SymDimVector> computeStride(
|
| 181 |
+
c10::SymIntArrayRef oldshape,
|
| 182 |
+
c10::SymIntArrayRef oldstride,
|
| 183 |
+
c10::SymIntArrayRef newshape);
|
| 184 |
+
|
| 185 |
+
TORCH_API std::optional<DimVector> computeStride(
|
| 186 |
+
IntArrayRef oldshape,
|
| 187 |
+
IntArrayRef oldstride,
|
| 188 |
+
const DimVector& newshape);
|
| 189 |
+
|
| 190 |
+
} // namespace detail
|
| 191 |
+
} // namespace at
|
| 192 |
+
|
| 193 |
+
#else
|
| 194 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 195 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/ThreadLocalPythonObjects.h
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/SafePyObject.h>
|
| 5 |
+
#include <c10/macros/Macros.h>
|
| 6 |
+
#include <unordered_map>
|
| 7 |
+
|
| 8 |
+
namespace at::impl {
|
| 9 |
+
|
| 10 |
+
struct TORCH_API ThreadLocalPythonObjects {
|
| 11 |
+
static void set(const std::string& key, std::shared_ptr<SafePyObject> value);
|
| 12 |
+
static const std::shared_ptr<SafePyObject>& get(const std::string& key);
|
| 13 |
+
static bool contains(const std::string& key);
|
| 14 |
+
|
| 15 |
+
static const ThreadLocalPythonObjects& get_state();
|
| 16 |
+
static void set_state(ThreadLocalPythonObjects state);
|
| 17 |
+
|
| 18 |
+
private:
|
| 19 |
+
std::unordered_map<std::string, std::shared_ptr<c10::SafePyObject>> obj_dict_;
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
} // namespace at::impl
|
| 23 |
+
|
| 24 |
+
#else
|
| 25 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 26 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/ThreadLocalState.h
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/InferenceMode.h>
|
| 5 |
+
#include <c10/core/impl/LocalDispatchKeySet.h>
|
| 6 |
+
#include <c10/util/Exception.h>
|
| 7 |
+
#include <c10/util/ThreadLocalDebugInfo.h>
|
| 8 |
+
|
| 9 |
+
#include <ATen/FuncTorchTLS.h>
|
| 10 |
+
#include <ATen/PythonTorchFunctionTLS.h>
|
| 11 |
+
#include <ATen/SavedTensorHooks.h>
|
| 12 |
+
#include <ATen/ThreadLocalPythonObjects.h>
|
| 13 |
+
#include <ATen/record_function.h>
|
| 14 |
+
#include <c10/core/impl/PythonDispatcherTLS.h>
|
| 15 |
+
#include <c10/core/impl/TorchDispatchModeTLS.h>
|
| 16 |
+
|
| 17 |
+
namespace at {
|
| 18 |
+
|
| 19 |
+
// Thread local state contains values that are preserved across
|
| 20 |
+
// thread boundaries (e.g. at::launch/JIT fork, autograd).
|
| 21 |
+
// Note at::parallel_for doesn't preserve TLS across thread boundaries.
|
| 22 |
+
class TORCH_API ThreadLocalState {
|
| 23 |
+
public:
|
| 24 |
+
// Saves the thread local variables' values and
|
| 25 |
+
// returns them as a ThreadLocalState
|
| 26 |
+
ThreadLocalState();
|
| 27 |
+
|
| 28 |
+
// set_grad_mode - force the value of the grad mode TLS in
|
| 29 |
+
// the current state object. This is used for example in the
|
| 30 |
+
// autograd engine.
|
| 31 |
+
void set_grad_mode(bool enabled);
|
| 32 |
+
|
| 33 |
+
// set_multithreading_enabled - force the value of the multithreadinmaximum
|
| 34 |
+
// threads TLS in
|
| 35 |
+
// the current state object. This is used for example in the
|
| 36 |
+
// autograd engine.
|
| 37 |
+
void set_multithreading_enabled(bool enabled);
|
| 38 |
+
|
| 39 |
+
// Sets thread local variables in the current thread,
|
| 40 |
+
// according to the thread boundary specified
|
| 41 |
+
static void setThreadLocalState(const ThreadLocalState& state);
|
| 42 |
+
|
| 43 |
+
private:
|
| 44 |
+
c10::impl::LocalDispatchKeySet dispatch_key_;
|
| 45 |
+
|
| 46 |
+
// ThreadLocalDebugInfo does not change after being created
|
| 47 |
+
// with DebugInfoGuard
|
| 48 |
+
std::shared_ptr<c10::ThreadLocalDebugInfo> debug_info_;
|
| 49 |
+
|
| 50 |
+
// RecordFunction TLS
|
| 51 |
+
RecordFunctionTLS rf_tls_;
|
| 52 |
+
|
| 53 |
+
// TLS for out-of-tree functorch
|
| 54 |
+
// See NOTE [functorch TLS in pytorch/pytorch] for why this needs to be a
|
| 55 |
+
// pointer (spoiler alert: it's due to the indirection)
|
| 56 |
+
// This needs to be a shared_ptr instead of a unique_ptr because
|
| 57 |
+
// ThreadLocalState is copy-able and does indeed get copied. Maybe we can
|
| 58 |
+
// consider adding an explicit copy constructor for ThreadLocalState in the
|
| 59 |
+
// future but I didn't want to add one just for this.
|
| 60 |
+
std::shared_ptr<const functorch::FuncTorchTLSBase> functorch_tls_;
|
| 61 |
+
|
| 62 |
+
// TLS for AutogradModes
|
| 63 |
+
AutogradState autograd_tls_;
|
| 64 |
+
|
| 65 |
+
// TLS for enable_torch_dispatch_mode
|
| 66 |
+
c10::impl::TorchDispatchModeTLS torch_dispatch_mode_state_;
|
| 67 |
+
|
| 68 |
+
// TLS for enable_python_dispatcher
|
| 69 |
+
c10::impl::PyInterpreter* python_dispatcher_state_;
|
| 70 |
+
|
| 71 |
+
// TLS for __torch_function__ (mode and disable_torch_function)
|
| 72 |
+
at::impl::PythonTorchFunctionTLS python_torch_function_state_;
|
| 73 |
+
|
| 74 |
+
// TLS for saved tensors default hooks
|
| 75 |
+
at::impl::SavedTensorDefaultHooksTLS saved_tensors_default_hooks_state_;
|
| 76 |
+
|
| 77 |
+
bool functionalization_reapply_views_state_;
|
| 78 |
+
|
| 79 |
+
bool dtensor_allow_implicit_replication_;
|
| 80 |
+
|
| 81 |
+
// TLS for arbitrary python objects that is registered via hooks
|
| 82 |
+
at::impl::ThreadLocalPythonObjects saved_objects_;
|
| 83 |
+
|
| 84 |
+
#if !defined(CAFFE2_IS_XPLAT_BUILD) && !defined(C10_MOBILE) && \
|
| 85 |
+
!defined(BUILD_LITE_INTERPRETER)
|
| 86 |
+
// TLS for autocast dtypes
|
| 87 |
+
std::array<at::ScalarType, at::COMPILE_TIME_MAX_DEVICE_TYPES>
|
| 88 |
+
autocast_dtypes_{};
|
| 89 |
+
#endif
|
| 90 |
+
|
| 91 |
+
friend class ThreadLocalStateGuard;
|
| 92 |
+
};
|
| 93 |
+
|
| 94 |
+
// Guard to set and reset the thread local state
|
| 95 |
+
class TORCH_API ThreadLocalStateGuard {
|
| 96 |
+
public:
|
| 97 |
+
explicit ThreadLocalStateGuard(const ThreadLocalState& state)
|
| 98 |
+
: prev_state_(ThreadLocalState()) {
|
| 99 |
+
// set the given state across the thread boundary
|
| 100 |
+
ThreadLocalState::setThreadLocalState(state);
|
| 101 |
+
}
|
| 102 |
+
ThreadLocalStateGuard(ThreadLocalStateGuard&& other) = delete;
|
| 103 |
+
ThreadLocalStateGuard(const ThreadLocalStateGuard&) = delete;
|
| 104 |
+
ThreadLocalStateGuard& operator=(const ThreadLocalStateGuard&) = delete;
|
| 105 |
+
ThreadLocalStateGuard& operator=(ThreadLocalStateGuard&&) = delete;
|
| 106 |
+
|
| 107 |
+
~ThreadLocalStateGuard() {
|
| 108 |
+
// restore previously set variables
|
| 109 |
+
ThreadLocalState::setThreadLocalState(prev_state_);
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
private:
|
| 113 |
+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
|
| 114 |
+
const ThreadLocalState prev_state_;
|
| 115 |
+
};
|
| 116 |
+
|
| 117 |
+
template <typename T>
|
| 118 |
+
auto wrapPropagateTLSState(T callback) {
|
| 119 |
+
return [tls_state = ThreadLocalState(),
|
| 120 |
+
callback = std::move(callback)](auto&&... args) {
|
| 121 |
+
ThreadLocalStateGuard g(tls_state);
|
| 122 |
+
// Propagate value returned by callback().
|
| 123 |
+
return callback(std::forward<decltype(args)>(args)...);
|
| 124 |
+
};
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
} // namespace at
|
| 128 |
+
|
| 129 |
+
#else
|
| 130 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 131 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TracerMode.h
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/impl/LocalDispatchKeySet.h>
|
| 5 |
+
#include <c10/macros/Export.h>
|
| 6 |
+
#include <c10/macros/Macros.h>
|
| 7 |
+
|
| 8 |
+
// NOTE [Tracing Mode Switches]
|
| 9 |
+
//
|
| 10 |
+
// Historically, tracing function was controlled by two switches:
|
| 11 |
+
//
|
| 12 |
+
// - `AutoDispatchBelowADInplaceOrView` guard
|
| 13 |
+
//
|
| 14 |
+
// Tracing function used to be script-generated inside `VariableType_*.cpp`
|
| 15 |
+
// kernels, sharing the same `Autograd` dispatch key with autograd function.
|
| 16 |
+
// Therefore, before tracing function was moved out of VariableType,
|
| 17 |
+
// `AutoDispatchBelowADInplaceOrView` guard can also disable tracing as a
|
| 18 |
+
// side effect of disabling `Autograd` dispatching.
|
| 19 |
+
//
|
| 20 |
+
// - `setTracingState()` API in `torch/csrc/jit/frontend/tracer.h`
|
| 21 |
+
//
|
| 22 |
+
// It stores tracing data in a `TracingState` object in TLS. If the
|
| 23 |
+
// `TracingState` object in TLS is `null`, then tracing is paused.
|
| 24 |
+
//
|
| 25 |
+
// The `TracingState` object is created in `tracer::trace()` - the main
|
| 26 |
+
// entrance of tracing function. It's temporarily set to `null` inside
|
| 27 |
+
// generated VariableType (now TraceType) to bypass tracing for intermediate
|
| 28 |
+
// ops (ops being called by other ops). After the intermediate op call
|
| 29 |
+
// finishes it's set back to the original `TracingState` object.
|
| 30 |
+
//
|
| 31 |
+
// The `TracingState` object in TLS can also be read/written via its Python
|
| 32 |
+
// binding in `python_tracer.cpp`, and `get/setTracingState()` C++ APIs,
|
| 33 |
+
// which are also exposed as `TORCH_API`.
|
| 34 |
+
//
|
| 35 |
+
// Two new switches were introduced since tracing function was moved out of
|
| 36 |
+
// VariableType:
|
| 37 |
+
//
|
| 38 |
+
// - `tracer::impl::set_dispatch_enabled()` API
|
| 39 |
+
//
|
| 40 |
+
// Unlike the special `Autograd` dispatch key which is included in dispatch
|
| 41 |
+
// key set by default, `Tracer` dispatch key is off by default. The
|
| 42 |
+
// dispatching switch can be toggled via this new API.
|
| 43 |
+
//
|
| 44 |
+
// - `tracer::impl::NoTracerDispatchMode` guard
|
| 45 |
+
//
|
| 46 |
+
// It's used to cover the old semantics of `AutoDispatchBelowADInplaceOrView`
|
| 47 |
+
// after tracing was moved out of VariableType.
|
| 48 |
+
//
|
| 49 |
+
// Before tracing function was moved out of VariableType, tracing was enabled
|
| 50 |
+
// when the following conditions are satisfied:
|
| 51 |
+
//
|
| 52 |
+
// 1) `TracingState` object in TLS != null;
|
| 53 |
+
// - Either inside the execution scope of `tracer::trace()`, or
|
| 54 |
+
// - Eagerly called `setTracingState()` with non-null object.
|
| 55 |
+
// 2) Not inside `AutoDispatchBelowADInplaceOrView` scope;
|
| 56 |
+
//
|
| 57 |
+
// After:
|
| 58 |
+
//
|
| 59 |
+
// 1) `TracingState` object in TLS != null;
|
| 60 |
+
// 2) Has called `tracer::impl::set_dispatch_enabled(true)`;
|
| 61 |
+
// 3) Not inside `tracer::impl::NonDispatchGuard` scope;
|
| 62 |
+
//
|
| 63 |
+
// [TODOs]
|
| 64 |
+
//
|
| 65 |
+
// - `setTracingState()` v.s. `tracer::impl::set_dispatch_enabled()`
|
| 66 |
+
//
|
| 67 |
+
// Currently `set_dispatch_enabled()` is set/unset inside `setTracingState()`
|
| 68 |
+
// to keep the semantics exactly the same as before - it's confusing to keep
|
| 69 |
+
// both switches, though. We should consider simplifying/limiting the exposed
|
| 70 |
+
// `setTracingState()` Python/C++ APIs (and other APIs calling it) so that
|
| 71 |
+
// these two can be unified.
|
| 72 |
+
//
|
| 73 |
+
// - `AutoDispatchBelowADInplaceOrView` v.s.
|
| 74 |
+
// `tracer::impl::NoTracerDispatchMode`
|
| 75 |
+
//
|
| 76 |
+
// We don't need to always set both guards together to keep semantics
|
| 77 |
+
// unchanged. For the follow use cases of `AutoDispatchBelowADInplaceOrView`
|
| 78 |
+
// we don't need set the new tracer guard:
|
| 79 |
+
//
|
| 80 |
+
// * Script-generated VariableType kernels. The guard is not necessary as
|
| 81 |
+
// tracing is already disabled explicitly by `setTracingState(null)` in
|
| 82 |
+
// generated TraceType kernels - we could keep it as is or use the new guard
|
| 83 |
+
// instead.
|
| 84 |
+
//
|
| 85 |
+
// * Custom ops. Will be handled by fallback kernel for `Tracer`.
|
| 86 |
+
//
|
| 87 |
+
// * Functions that are not likely to be called in tracing context (no python
|
| 88 |
+
// binding / not an operator), e.g.: all mobile forward() wrappers, test
|
| 89 |
+
// binaries, and etc.
|
| 90 |
+
//
|
| 91 |
+
// * Where new threads are spawned, e.g.: ATen/native/ConvolutionMM2d.cpp.
|
| 92 |
+
// It's not necessary as tracing is off by default.
|
| 93 |
+
//
|
| 94 |
+
// For the rest of cases we might need have both:
|
| 95 |
+
//
|
| 96 |
+
// * Functions that might be reachable from eager mode python (especially
|
| 97 |
+
// factory methods), e.g.:
|
| 98 |
+
// `internal_new_from_data()` in `torch/csrc/utils/tensor_new.cpp`.
|
| 99 |
+
// Without the new guard it will add `aten::empty` to the traced graph.
|
| 100 |
+
//
|
| 101 |
+
// * Some manually maintained functions, e.g.:
|
| 102 |
+
// `torch/csrc/autograd/VariableTypeManual.cpp`.
|
| 103 |
+
// Set the new guard if it's not obvious whether `setTracingState(null)`
|
| 104 |
+
// has been called before it reaches the `AutoDispatchBelowADInplaceOrView`
|
| 105 |
+
// guard.
|
| 106 |
+
//
|
| 107 |
+
// We might need tweak the usage of the new guard to optimize/fix things.
|
| 108 |
+
// It should only affect the correctness of tracing function, because the
|
| 109 |
+
// guard is essentially no-op when the master `setTracingState()` switch is
|
| 110 |
+
// off.
|
| 111 |
+
|
| 112 |
+
// TODO: move this from `at::` to `jit::torch::` after
|
| 113 |
+
// `aten/src/ATen/cpp_custom_type_hack.h` is removed.
|
| 114 |
+
|
| 115 |
+
namespace at::tracer::impl {
|
| 116 |
+
|
| 117 |
+
inline bool is_dispatch_enabled() {
|
| 118 |
+
return c10::impl::tls_is_dispatch_key_included(at::DispatchKey::Tracer) &&
|
| 119 |
+
!c10::impl::tls_is_dispatch_key_excluded(at::DispatchKey::Tracer);
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
inline void set_dispatch_enabled(bool enabled) {
|
| 123 |
+
TORCH_INTERNAL_ASSERT(
|
| 124 |
+
!c10::impl::tls_is_dispatch_key_excluded(at::DispatchKey::Tracer),
|
| 125 |
+
"Cannot enable tracing within the scope of NoTracerDispatchMode!");
|
| 126 |
+
c10::impl::tls_set_dispatch_key_included(at::DispatchKey::Tracer, enabled);
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
struct NoTracerDispatchMode {
|
| 130 |
+
c10::impl::ExcludeDispatchKeyGuard guard_{at::DispatchKey::Tracer};
|
| 131 |
+
};
|
| 132 |
+
|
| 133 |
+
} // namespace at::tracer::impl
|
| 134 |
+
|
| 135 |
+
#else
|
| 136 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 137 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/TypeDefault.h
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/Dimname.h>
|
| 5 |
+
#include <c10/core/MemoryFormat.h>
|
| 6 |
+
#include <c10/core/QScheme.h>
|
| 7 |
+
#include <c10/core/Scalar.h>
|
| 8 |
+
#include <c10/core/TensorOptions.h>
|
| 9 |
+
#include <c10/macros/Export.h>
|
| 10 |
+
#include <c10/util/ArrayRef.h>
|
| 11 |
+
#include <c10/util/intrusive_ptr.h>
|
| 12 |
+
|
| 13 |
+
namespace c10 {
|
| 14 |
+
struct Storage;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
namespace at {
|
| 18 |
+
|
| 19 |
+
class Tensor;
|
| 20 |
+
using TensorList = ArrayRef<Tensor>;
|
| 21 |
+
|
| 22 |
+
class Context;
|
| 23 |
+
struct Generator;
|
| 24 |
+
|
| 25 |
+
struct Quantizer;
|
| 26 |
+
|
| 27 |
+
} // namespace at
|
| 28 |
+
|
| 29 |
+
#else
|
| 30 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 31 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/Utils.h
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/EmptyTensor.h>
|
| 5 |
+
#include <ATen/Formatting.h>
|
| 6 |
+
#include <ATen/core/ATenGeneral.h>
|
| 7 |
+
#include <ATen/core/Generator.h>
|
| 8 |
+
#include <c10/core/ScalarType.h>
|
| 9 |
+
#include <c10/core/StorageImpl.h>
|
| 10 |
+
#include <c10/core/UndefinedTensorImpl.h>
|
| 11 |
+
#include <c10/util/ArrayRef.h>
|
| 12 |
+
#include <c10/util/Exception.h>
|
| 13 |
+
#include <c10/util/accumulate.h>
|
| 14 |
+
#include <c10/util/irange.h>
|
| 15 |
+
|
| 16 |
+
#include <algorithm>
|
| 17 |
+
|
| 18 |
+
#define AT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
| 19 |
+
TypeName(const TypeName&) = delete; \
|
| 20 |
+
void operator=(const TypeName&) = delete
|
| 21 |
+
|
| 22 |
+
namespace at {
|
| 23 |
+
|
| 24 |
+
TORCH_API int _crash_if_asan(int /*arg*/);
|
| 25 |
+
|
| 26 |
+
// Converts a TensorList (i.e. ArrayRef<Tensor> to vector of TensorImpl*)
|
| 27 |
+
// NB: This is ONLY used by legacy TH bindings, and ONLY used by cat.
|
| 28 |
+
// Once cat is ported entirely to ATen this can be deleted!
|
| 29 |
+
inline std::vector<TensorImpl*> checked_dense_tensor_list_unwrap(
|
| 30 |
+
ArrayRef<Tensor> tensors,
|
| 31 |
+
const char* name,
|
| 32 |
+
int pos,
|
| 33 |
+
c10::DeviceType device_type,
|
| 34 |
+
ScalarType scalar_type) {
|
| 35 |
+
std::vector<TensorImpl*> unwrapped;
|
| 36 |
+
unwrapped.reserve(tensors.size());
|
| 37 |
+
for (const auto i : c10::irange(tensors.size())) {
|
| 38 |
+
const auto& expr = tensors[i];
|
| 39 |
+
if (expr.layout() != Layout::Strided) {
|
| 40 |
+
TORCH_CHECK(
|
| 41 |
+
false,
|
| 42 |
+
"Expected dense tensor but got ",
|
| 43 |
+
expr.layout(),
|
| 44 |
+
" for sequence element ",
|
| 45 |
+
i,
|
| 46 |
+
" in sequence argument at position #",
|
| 47 |
+
pos,
|
| 48 |
+
" '",
|
| 49 |
+
name,
|
| 50 |
+
"'");
|
| 51 |
+
}
|
| 52 |
+
if (expr.device().type() != device_type) {
|
| 53 |
+
TORCH_CHECK(
|
| 54 |
+
false,
|
| 55 |
+
"Expected object of device type ",
|
| 56 |
+
device_type,
|
| 57 |
+
" but got device type ",
|
| 58 |
+
expr.device().type(),
|
| 59 |
+
" for sequence element ",
|
| 60 |
+
i,
|
| 61 |
+
" in sequence argument at position #",
|
| 62 |
+
pos,
|
| 63 |
+
" '",
|
| 64 |
+
name,
|
| 65 |
+
"'");
|
| 66 |
+
}
|
| 67 |
+
if (expr.scalar_type() != scalar_type) {
|
| 68 |
+
TORCH_CHECK(
|
| 69 |
+
false,
|
| 70 |
+
"Expected object of scalar type ",
|
| 71 |
+
scalar_type,
|
| 72 |
+
" but got scalar type ",
|
| 73 |
+
expr.scalar_type(),
|
| 74 |
+
" for sequence element ",
|
| 75 |
+
i,
|
| 76 |
+
" in sequence argument at position #",
|
| 77 |
+
pos,
|
| 78 |
+
" '",
|
| 79 |
+
name,
|
| 80 |
+
"'");
|
| 81 |
+
}
|
| 82 |
+
unwrapped.emplace_back(expr.unsafeGetTensorImpl());
|
| 83 |
+
}
|
| 84 |
+
return unwrapped;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
template <size_t N>
|
| 88 |
+
std::array<int64_t, N> check_intlist(
|
| 89 |
+
ArrayRef<int64_t> list,
|
| 90 |
+
const char* name,
|
| 91 |
+
int pos) {
|
| 92 |
+
if (list.empty()) {
|
| 93 |
+
// TODO: is this necessary? We used to treat nullptr-vs-not in IntList
|
| 94 |
+
// differently with strides as a way of faking optional.
|
| 95 |
+
list = {};
|
| 96 |
+
}
|
| 97 |
+
auto res = std::array<int64_t, N>();
|
| 98 |
+
if (list.size() == 1 && N > 1) {
|
| 99 |
+
res.fill(list[0]);
|
| 100 |
+
return res;
|
| 101 |
+
}
|
| 102 |
+
if (list.size() != N) {
|
| 103 |
+
TORCH_CHECK(
|
| 104 |
+
false,
|
| 105 |
+
"Expected a list of ",
|
| 106 |
+
N,
|
| 107 |
+
" ints but got ",
|
| 108 |
+
list.size(),
|
| 109 |
+
" for argument #",
|
| 110 |
+
pos,
|
| 111 |
+
" '",
|
| 112 |
+
name,
|
| 113 |
+
"'");
|
| 114 |
+
}
|
| 115 |
+
std::copy_n(list.begin(), N, res.begin());
|
| 116 |
+
return res;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
using at::detail::check_size_nonnegative;
|
| 120 |
+
|
| 121 |
+
namespace detail {
|
| 122 |
+
|
| 123 |
+
template <typename T>
|
| 124 |
+
TORCH_API Tensor tensor_cpu(ArrayRef<T> values, const TensorOptions& options);
|
| 125 |
+
|
| 126 |
+
template <typename T>
|
| 127 |
+
TORCH_API Tensor
|
| 128 |
+
tensor_backend(ArrayRef<T> values, const TensorOptions& options);
|
| 129 |
+
|
| 130 |
+
template <typename T>
|
| 131 |
+
TORCH_API Tensor
|
| 132 |
+
tensor_complex_cpu(ArrayRef<T> values, const TensorOptions& options);
|
| 133 |
+
|
| 134 |
+
template <typename T>
|
| 135 |
+
TORCH_API Tensor
|
| 136 |
+
tensor_complex_backend(ArrayRef<T> values, const TensorOptions& options);
|
| 137 |
+
} // namespace detail
|
| 138 |
+
|
| 139 |
+
} // namespace at
|
| 140 |
+
|
| 141 |
+
#else
|
| 142 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 143 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/Version.h
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#include <ATen/Context.h>
|
| 3 |
+
|
| 4 |
+
namespace at {
|
| 5 |
+
|
| 6 |
+
/// Returns a detailed string describing the configuration PyTorch.
|
| 7 |
+
TORCH_API std::string show_config();
|
| 8 |
+
|
| 9 |
+
TORCH_API std::string get_mkl_version();
|
| 10 |
+
|
| 11 |
+
TORCH_API std::string get_mkldnn_version();
|
| 12 |
+
|
| 13 |
+
TORCH_API std::string get_openmp_version();
|
| 14 |
+
|
| 15 |
+
TORCH_API std::string get_cxx_flags();
|
| 16 |
+
|
| 17 |
+
TORCH_API std::string get_cpu_capability();
|
| 18 |
+
|
| 19 |
+
} // namespace at
|
| 20 |
+
|
| 21 |
+
#else
|
| 22 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 23 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/VmapGeneratedPlumbing.h
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/WrapDimUtils.h
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/IListRef.h>
|
| 5 |
+
#include <ATen/core/Tensor.h>
|
| 6 |
+
#include <c10/core/TensorImpl.h>
|
| 7 |
+
#include <c10/core/WrapDimMinimal.h>
|
| 8 |
+
#include <c10/util/irange.h>
|
| 9 |
+
|
| 10 |
+
namespace at {
|
| 11 |
+
|
| 12 |
+
// if dim_post_expr is 0 and wrap_scalar is true, then dim must be in the
|
| 13 |
+
// range [-1, 0]. This is a special case for scalar tensors and manifests in
|
| 14 |
+
// e.g. torch.sum(scalar_tensor, 0) Otherwise, dim should be in the range
|
| 15 |
+
// [-dim_post_expr, dim_post_expr-1].
|
| 16 |
+
using c10::maybe_wrap_dim;
|
| 17 |
+
|
| 18 |
+
inline int64_t maybe_wrap_dim(int64_t dim, TensorImpl* tensor) {
|
| 19 |
+
return maybe_wrap_dim(dim, tensor->dim());
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
inline int64_t maybe_wrap_dim(int64_t dim, TensorList tensors) {
|
| 23 |
+
if (tensors.empty()) {
|
| 24 |
+
// can't wrap empty TensorList; rely on underlying implementation to throw
|
| 25 |
+
// error if necessary.
|
| 26 |
+
return dim;
|
| 27 |
+
}
|
| 28 |
+
return maybe_wrap_dim(dim, tensors[0].dim());
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
inline int64_t maybe_wrap_dim(
|
| 32 |
+
int64_t dim,
|
| 33 |
+
const std::vector<std::vector<int64_t>>& tensor_sizes) {
|
| 34 |
+
if (tensor_sizes.empty()) {
|
| 35 |
+
// can't wrap empty list; rely on underlying implementation to throw error
|
| 36 |
+
// if necessary
|
| 37 |
+
return dim;
|
| 38 |
+
}
|
| 39 |
+
return maybe_wrap_dim(dim, static_cast<int64_t>(tensor_sizes[0].size()));
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
// Given an array of dimensions `dims` of length `ndims`, this function "Wraps"
|
| 43 |
+
// each dim in-place for a tensor of rank `dim_post_expr`, allowing dims to be
|
| 44 |
+
// specified using negative indices.
|
| 45 |
+
//
|
| 46 |
+
// Additionally, if `wrap_scalar` is true then scalar tensors with rank 0, will
|
| 47 |
+
// allow dimensions in the range [-1, 0]. Otherwise, an IndexError is raised for
|
| 48 |
+
// dimensions not in the range [-dim_post_expr, dim_post_expr).
|
| 49 |
+
inline void maybe_wrap_dims_n(
|
| 50 |
+
int64_t* dims,
|
| 51 |
+
int64_t ndims,
|
| 52 |
+
int64_t dim_post_expr,
|
| 53 |
+
bool wrap_scalars = true) {
|
| 54 |
+
if (dim_post_expr <= 0) {
|
| 55 |
+
if (wrap_scalars) {
|
| 56 |
+
dim_post_expr = 1; // this will make range [-1, 0]
|
| 57 |
+
} else {
|
| 58 |
+
TORCH_CHECK_INDEX(
|
| 59 |
+
ndims == 0,
|
| 60 |
+
"Dimension specified as ",
|
| 61 |
+
dims[0],
|
| 62 |
+
" but tensor has no dimensions");
|
| 63 |
+
return;
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
int64_t min = -dim_post_expr;
|
| 67 |
+
int64_t max = dim_post_expr - 1;
|
| 68 |
+
for (const auto i : c10::irange(ndims)) {
|
| 69 |
+
auto& dim = dims[i];
|
| 70 |
+
if (dim < min || dim > max) {
|
| 71 |
+
TORCH_CHECK_INDEX(
|
| 72 |
+
false,
|
| 73 |
+
"Dimension out of range (expected to be in range of [",
|
| 74 |
+
min,
|
| 75 |
+
", ",
|
| 76 |
+
max,
|
| 77 |
+
"], but got ",
|
| 78 |
+
dim,
|
| 79 |
+
")");
|
| 80 |
+
}
|
| 81 |
+
if (dim < 0)
|
| 82 |
+
dim += dim_post_expr;
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// Given a contiguous container of dimensions `dims`, this function "Wraps"
|
| 87 |
+
// each dim in-place for a tensor of rank `dim_post_expr`, allowing dims to be
|
| 88 |
+
// specified using negative indices.
|
| 89 |
+
//
|
| 90 |
+
// Additionally, if `wrap_scalar` is true then scalar tensors with rank 0, will
|
| 91 |
+
// allow dimensions in the range [-1, 0]. Otherwise, an IndexError is raised for
|
| 92 |
+
// dimensions not in the range [-dim_post_expr, dim_post_expr).
|
| 93 |
+
template <typename Container>
|
| 94 |
+
inline void maybe_wrap_dims(
|
| 95 |
+
Container& dims,
|
| 96 |
+
int64_t dim_post_expr,
|
| 97 |
+
bool wrap_scalars = true) {
|
| 98 |
+
return maybe_wrap_dims_n(
|
| 99 |
+
dims.data(), dims.size(), dim_post_expr, wrap_scalars);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// previously, size [0] tensors were the only possible empty tensors; thus, it
|
| 103 |
+
// wasn't possible to cat empty tensors unless all the other tensors were
|
| 104 |
+
// 1-dimensional, so we allowed these tensors to be "skipped" (both for wrap
|
| 105 |
+
// dimension behavior and dimension size checking). We maintain this behavior
|
| 106 |
+
// for backwards compatibility, but only for this specific size (i.e. other
|
| 107 |
+
// empty sizes are not skipped).
|
| 108 |
+
inline int64_t legacy_cat_wrap_dim(
|
| 109 |
+
int64_t dim,
|
| 110 |
+
const std::vector<std::vector<int64_t>>& tensor_sizes) {
|
| 111 |
+
for (auto& sizes : tensor_sizes) {
|
| 112 |
+
if (sizes.size() == 1 && sizes[0] == 0) {
|
| 113 |
+
continue;
|
| 114 |
+
}
|
| 115 |
+
return maybe_wrap_dim(dim, static_cast<int64_t>(sizes.size()));
|
| 116 |
+
}
|
| 117 |
+
return dim;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
inline int64_t legacy_cat_wrap_dim_symint(
|
| 121 |
+
int64_t dim,
|
| 122 |
+
const std::vector<std::vector<c10::SymInt>>& tensor_sizes) {
|
| 123 |
+
for (auto& sizes : tensor_sizes) {
|
| 124 |
+
if (sizes.size() == 1) {
|
| 125 |
+
if (TORCH_GUARD_OR_FALSE(sizes[0].sym_eq(0))) {
|
| 126 |
+
continue;
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
return maybe_wrap_dim(dim, static_cast<int64_t>(sizes.size()));
|
| 130 |
+
}
|
| 131 |
+
return dim;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
inline int64_t legacy_cat_wrap_dim(
|
| 135 |
+
int64_t dim,
|
| 136 |
+
const MaterializedITensorListRef& tensors) {
|
| 137 |
+
for (const Tensor& tensor : tensors) {
|
| 138 |
+
if (tensor.dim() == 1) {
|
| 139 |
+
if (TORCH_GUARD_OR_FALSE(tensor.sym_sizes()[0].sym_eq(0))) {
|
| 140 |
+
continue;
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
return maybe_wrap_dim(dim, tensor.dim());
|
| 144 |
+
}
|
| 145 |
+
return dim;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
// wrap negative dims in a vector
|
| 149 |
+
inline void wrap_all_dims(
|
| 150 |
+
std::vector<int64_t>& dims_to_wrap,
|
| 151 |
+
int64_t tensor_total_dims) {
|
| 152 |
+
for (const auto i : c10::irange(dims_to_wrap.size())) {
|
| 153 |
+
dims_to_wrap[i] = maybe_wrap_dim(dims_to_wrap[i], tensor_total_dims);
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
} // namespace at
|
| 158 |
+
|
| 159 |
+
#else
|
| 160 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 161 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/WrapDimUtilsMulti.h
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/WrapDimUtils.h>
|
| 5 |
+
#include <c10/core/TensorImpl.h>
|
| 6 |
+
#include <c10/util/irange.h>
|
| 7 |
+
#include <bitset>
|
| 8 |
+
#include <sstream>
|
| 9 |
+
|
| 10 |
+
namespace at {
|
| 11 |
+
|
| 12 |
+
// This is in an extra file to work around strange interaction of
|
| 13 |
+
// bitset on Windows with operator overloading
|
| 14 |
+
|
| 15 |
+
constexpr size_t dim_bitset_size = 64;
|
| 16 |
+
|
| 17 |
+
inline std::bitset<dim_bitset_size> dim_list_to_bitset(
|
| 18 |
+
OptionalIntArrayRef opt_dims,
|
| 19 |
+
size_t ndims) {
|
| 20 |
+
TORCH_CHECK(
|
| 21 |
+
ndims <= dim_bitset_size,
|
| 22 |
+
"only tensors with up to ",
|
| 23 |
+
dim_bitset_size,
|
| 24 |
+
" dims are supported");
|
| 25 |
+
std::bitset<dim_bitset_size> seen;
|
| 26 |
+
if (opt_dims.has_value()) {
|
| 27 |
+
auto dims = opt_dims.value();
|
| 28 |
+
for (const auto i : c10::irange(dims.size())) {
|
| 29 |
+
size_t dim = maybe_wrap_dim(dims[i], static_cast<int64_t>(ndims));
|
| 30 |
+
TORCH_CHECK(
|
| 31 |
+
!seen[dim],
|
| 32 |
+
"dim ",
|
| 33 |
+
dim,
|
| 34 |
+
" appears multiple times in the list of dims");
|
| 35 |
+
seen[dim] = true;
|
| 36 |
+
}
|
| 37 |
+
} else {
|
| 38 |
+
for (size_t dim = 0; dim < ndims; dim++) {
|
| 39 |
+
seen[dim] = true;
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
return seen;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
} // namespace at
|
| 46 |
+
|
| 47 |
+
#else
|
| 48 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 49 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/autocast_mode.h
ADDED
|
@@ -0,0 +1,976 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/ATen.h>
|
| 5 |
+
#include <ATen/NativeFunctions.h>
|
| 6 |
+
#include <ATen/Operators.h>
|
| 7 |
+
#include <torch/library.h>
|
| 8 |
+
|
| 9 |
+
#include <c10/core/impl/LocalDispatchKeySet.h>
|
| 10 |
+
#include <c10/util/intrusive_ptr.h>
|
| 11 |
+
|
| 12 |
+
namespace at::autocast {
|
| 13 |
+
|
| 14 |
+
TORCH_API bool is_autocast_enabled(at::DeviceType device_type);
|
| 15 |
+
TORCH_API void set_autocast_enabled(at::DeviceType device_type, bool enabled);
|
| 16 |
+
TORCH_API at::ScalarType get_autocast_dtype(at::DeviceType device_type);
|
| 17 |
+
TORCH_API void set_autocast_dtype(
|
| 18 |
+
at::DeviceType device_type,
|
| 19 |
+
at::ScalarType dtype);
|
| 20 |
+
TORCH_API void clear_cache();
|
| 21 |
+
TORCH_API int increment_nesting();
|
| 22 |
+
TORCH_API int decrement_nesting();
|
| 23 |
+
TORCH_API bool is_autocast_cache_enabled();
|
| 24 |
+
TORCH_API void set_autocast_cache_enabled(bool enabled);
|
| 25 |
+
|
| 26 |
+
// deprecated CUDA-specific autocast APIs
|
| 27 |
+
C10_DEPRECATED_MESSAGE(
|
| 28 |
+
"at::autocast::is_enabled() is deprecated. Please use at::autocast::is_autocast_enabled(at::kCUDA) instead.")
|
| 29 |
+
inline bool is_enabled() {
|
| 30 |
+
TORCH_WARN_DEPRECATION(
|
| 31 |
+
"at::autocast::",
|
| 32 |
+
__func__,
|
| 33 |
+
"() is deprecated. Please use at::autocast::is_autocast_enabled(at::kCUDA) instead.")
|
| 34 |
+
return is_autocast_enabled(at::kCUDA);
|
| 35 |
+
}
|
| 36 |
+
C10_DEPRECATED_MESSAGE(
|
| 37 |
+
"at::autocast::set_enabled(enabled) is deprecated. Please use at::autocast::set_autocast_enabled(at::kCUDA, enabled) instead.")
|
| 38 |
+
inline void set_enabled(bool enabled) {
|
| 39 |
+
TORCH_WARN_DEPRECATION(
|
| 40 |
+
"at::autocast::",
|
| 41 |
+
__func__,
|
| 42 |
+
"(enabled) is deprecated. Please use at::autocast::set_autocast_enabled(at::kCUDA, enabled) instead.")
|
| 43 |
+
set_autocast_enabled(at::kCUDA, enabled);
|
| 44 |
+
}
|
| 45 |
+
C10_DEPRECATED_MESSAGE(
|
| 46 |
+
"at::autocast::get_autocast_gpu_dtype() is deprecated. Please use at::autocast::get_autocast_dtype(at::kCUDA) instead.")
|
| 47 |
+
inline at::ScalarType get_autocast_gpu_dtype() {
|
| 48 |
+
TORCH_WARN_DEPRECATION(
|
| 49 |
+
"at::autocast::",
|
| 50 |
+
__func__,
|
| 51 |
+
"() is deprecated. Please use at::autocast::get_autocast_dtype(at::kCUDA) instead.")
|
| 52 |
+
return get_autocast_dtype(at::kCUDA);
|
| 53 |
+
}
|
| 54 |
+
C10_DEPRECATED_MESSAGE(
|
| 55 |
+
"at::autocast::set_autocast_gpu_dtype(dtype) is deprecated. Please use at::autocast::set_autocast_dtype(at::kCUDA, dtype) instead.")
|
| 56 |
+
inline void set_autocast_gpu_dtype(at::ScalarType dtype) {
|
| 57 |
+
TORCH_WARN_DEPRECATION(
|
| 58 |
+
"at::autocast::",
|
| 59 |
+
__func__,
|
| 60 |
+
"(dtype) is deprecated. Please use at::autocast::set_autocast_dtype(at::kCUDA, dtype) instead.")
|
| 61 |
+
set_autocast_dtype(at::kCUDA, dtype);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
#define DECLARE_DEPRECATED_AUTOCAST_APIS(name, device_type) \
|
| 65 |
+
C10_DEPRECATED_MESSAGE( \
|
| 66 |
+
"at::autocast::is_" #name \
|
| 67 |
+
"_enabled() is deprecated. Please use at::autocast::is_autocast_enabled(" #device_type \
|
| 68 |
+
") instead.") \
|
| 69 |
+
inline bool is_##name##_enabled() { \
|
| 70 |
+
TORCH_WARN_DEPRECATION( \
|
| 71 |
+
"at::autocast::", \
|
| 72 |
+
__func__, \
|
| 73 |
+
"() is deprecated. Please use at::autocast::is_autocast_enabled(" #device_type \
|
| 74 |
+
") instead.") \
|
| 75 |
+
return is_autocast_enabled(device_type); \
|
| 76 |
+
} \
|
| 77 |
+
\
|
| 78 |
+
C10_DEPRECATED_MESSAGE( \
|
| 79 |
+
"at::autocast::set_" #name \
|
| 80 |
+
"_enabled(enabled) is deprecated. Please use at::autocast::set_autocast_enabled(" #device_type \
|
| 81 |
+
", enabled) instead.") \
|
| 82 |
+
inline void set_##name##_enabled(bool enabled) { \
|
| 83 |
+
TORCH_WARN_DEPRECATION( \
|
| 84 |
+
"at::autocast::", \
|
| 85 |
+
__func__, \
|
| 86 |
+
"(enabled) is deprecated. Please use at::autocast::set_autocast_enabled(" #device_type \
|
| 87 |
+
", enabled) instead.") \
|
| 88 |
+
set_autocast_enabled(device_type, enabled); \
|
| 89 |
+
} \
|
| 90 |
+
\
|
| 91 |
+
C10_DEPRECATED_MESSAGE( \
|
| 92 |
+
"at::autocast::get_autocast_" #name \
|
| 93 |
+
"_dtype() is deprecated. Please use at::autocast::get_autocast_dtype(" #device_type \
|
| 94 |
+
") instead.") \
|
| 95 |
+
inline at::ScalarType get_autocast_##name##_dtype() { \
|
| 96 |
+
TORCH_WARN_DEPRECATION( \
|
| 97 |
+
"at::autocast::", \
|
| 98 |
+
__func__, \
|
| 99 |
+
"() is deprecated. Please at::autocast::get_autocast_dtype(" #device_type \
|
| 100 |
+
") instead.") \
|
| 101 |
+
return get_autocast_dtype(device_type); \
|
| 102 |
+
} \
|
| 103 |
+
\
|
| 104 |
+
C10_DEPRECATED_MESSAGE( \
|
| 105 |
+
"at::autocast::set_autocast_" #name \
|
| 106 |
+
"_dtype(dtype) is deprecated. Please use at::autocast::set_autocast_dtype(" #device_type \
|
| 107 |
+
", dtype) instead.") \
|
| 108 |
+
inline void set_autocast_##name##_dtype(at::ScalarType dtype) { \
|
| 109 |
+
TORCH_WARN_DEPRECATION( \
|
| 110 |
+
"at::autocast::", \
|
| 111 |
+
__func__, \
|
| 112 |
+
"(dtype) is deprecated. Please use at::autocast::set_autocast_dtype(" #device_type \
|
| 113 |
+
", dtype) instead.") \
|
| 114 |
+
set_autocast_dtype(device_type, dtype); \
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
#define AT_FORALL_DEPRECATED_AUTOCAST_BACKENDS(_) \
|
| 118 |
+
_(cpu, at::kCPU) \
|
| 119 |
+
_(mtia, at::kMTIA) \
|
| 120 |
+
_(xpu, at::kXPU) \
|
| 121 |
+
_(xla, at::kXLA) \
|
| 122 |
+
_(hpu, at::kHPU) \
|
| 123 |
+
_(ipu, at::kIPU) \
|
| 124 |
+
_(privateuseone, at::kPrivateUse1)
|
| 125 |
+
|
| 126 |
+
// deprecated other backend specific autocast APIs
|
| 127 |
+
// NOLINTNEXTLINE(misc-use-internal-linkage)
|
| 128 |
+
AT_FORALL_DEPRECATED_AUTOCAST_BACKENDS(DECLARE_DEPRECATED_AUTOCAST_APIS)
|
| 129 |
+
|
| 130 |
+
const std::array<at::DeviceType, 10> _AUTOCAST_SUPPORTED_DEVICES{
|
| 131 |
+
at::kCPU,
|
| 132 |
+
at::kCUDA,
|
| 133 |
+
at::kMTIA,
|
| 134 |
+
at::kMAIA,
|
| 135 |
+
at::kXPU,
|
| 136 |
+
at::kIPU,
|
| 137 |
+
at::kHPU,
|
| 138 |
+
at::kXLA,
|
| 139 |
+
at::kPrivateUse1,
|
| 140 |
+
at::kMPS};
|
| 141 |
+
|
| 142 |
+
namespace {
|
| 143 |
+
inline bool is_autocast_eligible(
|
| 144 |
+
const Tensor& tensor,
|
| 145 |
+
c10::DeviceType device_type) {
|
| 146 |
+
switch (device_type) {
|
| 147 |
+
case c10::DeviceType::CUDA:
|
| 148 |
+
return (tensor.is_cuda() || tensor.is_xla()) &&
|
| 149 |
+
tensor.is_floating_point();
|
| 150 |
+
case c10::DeviceType::CPU:
|
| 151 |
+
return (tensor.is_cpu() || tensor.is_mkldnn()) &&
|
| 152 |
+
tensor.is_floating_point();
|
| 153 |
+
case c10::DeviceType::MTIA:
|
| 154 |
+
return tensor.is_mtia() && tensor.is_floating_point();
|
| 155 |
+
case c10::DeviceType::MAIA:
|
| 156 |
+
return tensor.is_maia() && tensor.is_floating_point();
|
| 157 |
+
case c10::DeviceType::XPU:
|
| 158 |
+
return tensor.is_xpu() && tensor.is_floating_point();
|
| 159 |
+
case c10::DeviceType::IPU:
|
| 160 |
+
return tensor.is_ipu() && tensor.is_floating_point();
|
| 161 |
+
case c10::DeviceType::HPU:
|
| 162 |
+
return tensor.is_hpu() && tensor.is_floating_point();
|
| 163 |
+
case c10::DeviceType::XLA:
|
| 164 |
+
return tensor.is_xla() && tensor.is_floating_point();
|
| 165 |
+
case c10::DeviceType::PrivateUse1:
|
| 166 |
+
return tensor.is_privateuseone() && tensor.is_floating_point();
|
| 167 |
+
case c10::DeviceType::MPS:
|
| 168 |
+
return tensor.is_mps() && tensor.is_floating_point();
|
| 169 |
+
default:
|
| 170 |
+
return false;
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
} // namespace
|
| 174 |
+
|
| 175 |
+
inline DispatchKey get_autocast_dispatch_key_from_device_type(
|
| 176 |
+
c10::DeviceType device_type) {
|
| 177 |
+
switch (device_type) {
|
| 178 |
+
case c10::DeviceType::CUDA:
|
| 179 |
+
return DispatchKey::Autocast;
|
| 180 |
+
case c10::DeviceType::CPU:
|
| 181 |
+
return DispatchKey::AutocastCPU;
|
| 182 |
+
case c10::DeviceType::MTIA:
|
| 183 |
+
return DispatchKey::AutocastMTIA;
|
| 184 |
+
case c10::DeviceType::MAIA:
|
| 185 |
+
return DispatchKey::AutocastMAIA;
|
| 186 |
+
case c10::DeviceType::XPU:
|
| 187 |
+
return DispatchKey::AutocastXPU;
|
| 188 |
+
case c10::DeviceType::IPU:
|
| 189 |
+
return DispatchKey::AutocastIPU;
|
| 190 |
+
case c10::DeviceType::HPU:
|
| 191 |
+
return DispatchKey::AutocastHPU;
|
| 192 |
+
case c10::DeviceType::XLA:
|
| 193 |
+
return DispatchKey::AutocastXLA;
|
| 194 |
+
case c10::DeviceType::PrivateUse1:
|
| 195 |
+
return DispatchKey::AutocastPrivateUse1;
|
| 196 |
+
case c10::DeviceType::MPS:
|
| 197 |
+
return DispatchKey::AutocastMPS;
|
| 198 |
+
default:
|
| 199 |
+
TORCH_CHECK(
|
| 200 |
+
false,
|
| 201 |
+
"unknown device type for autocast in get_autocast_dispatch_key_from_device_type");
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
inline bool is_autocast_available(c10::DeviceType device_type) {
|
| 206 |
+
if (std::find(
|
| 207 |
+
_AUTOCAST_SUPPORTED_DEVICES.begin(),
|
| 208 |
+
_AUTOCAST_SUPPORTED_DEVICES.end(),
|
| 209 |
+
device_type) != _AUTOCAST_SUPPORTED_DEVICES.end()) {
|
| 210 |
+
return true;
|
| 211 |
+
} else {
|
| 212 |
+
return false;
|
| 213 |
+
}
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
inline at::ScalarType get_lower_precision_fp_from_device_type(
|
| 217 |
+
c10::DeviceType device_type) {
|
| 218 |
+
if (is_autocast_available(device_type)) {
|
| 219 |
+
return get_autocast_dtype(device_type);
|
| 220 |
+
} else {
|
| 221 |
+
TORCH_CHECK(
|
| 222 |
+
false,
|
| 223 |
+
"unknown device type for autocast in get_lower_precision_fp_from_device_type");
|
| 224 |
+
}
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
/********************************************************************
|
| 228 |
+
Logic to extract the promote type from any Tensor or TensorList args.
|
| 229 |
+
********************************************************************/
|
| 230 |
+
|
| 231 |
+
// Overload to catch Tensor args.
|
| 232 |
+
// If nextArg is floating-point, compare its scalar_type with our
|
| 233 |
+
// current best guess for the promote type, and update if necessary.
|
| 234 |
+
inline at::ScalarType prioritize(
|
| 235 |
+
at::ScalarType current,
|
| 236 |
+
const Tensor& nextArg,
|
| 237 |
+
c10::DeviceType device_type = c10::DeviceType::CUDA) {
|
| 238 |
+
if (current == at::kDouble) {
|
| 239 |
+
TORCH_CHECK(false, "promote type is double in at::autocast::prioritize");
|
| 240 |
+
return current;
|
| 241 |
+
}
|
| 242 |
+
at::ScalarType lower_precision_fp =
|
| 243 |
+
get_lower_precision_fp_from_device_type(device_type);
|
| 244 |
+
if (is_autocast_eligible(nextArg, device_type)) {
|
| 245 |
+
auto next = nextArg.scalar_type();
|
| 246 |
+
if (next == at::kDouble) {
|
| 247 |
+
return current; // ignores double tensors
|
| 248 |
+
} else if (current == at::kFloat || next == at::kFloat) {
|
| 249 |
+
return at::kFloat; // prioritizes float over lower_precision_fp
|
| 250 |
+
} else if (current == lower_precision_fp && next == lower_precision_fp) {
|
| 251 |
+
return lower_precision_fp;
|
| 252 |
+
} else {
|
| 253 |
+
TORCH_CHECK(
|
| 254 |
+
false, "Unexpected floating ScalarType in at::autocast::prioritize");
|
| 255 |
+
return current;
|
| 256 |
+
}
|
| 257 |
+
} else {
|
| 258 |
+
return current;
|
| 259 |
+
}
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
// Overload to catch TensorList args (for e.g. cat, stack).
|
| 263 |
+
// Reuses the overload above to process each Tensor in the list.
|
| 264 |
+
inline at::ScalarType prioritize(
|
| 265 |
+
at::ScalarType current,
|
| 266 |
+
const TensorList& list,
|
| 267 |
+
c10::DeviceType device_type = c10::DeviceType::CUDA) {
|
| 268 |
+
for (const auto& tensor : list) {
|
| 269 |
+
current = prioritize(current, tensor, device_type);
|
| 270 |
+
}
|
| 271 |
+
return current;
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
inline at::ScalarType prioritize(
|
| 275 |
+
at::ScalarType current,
|
| 276 |
+
const ITensorListRef& list,
|
| 277 |
+
c10::DeviceType device_type = c10::DeviceType::CUDA) {
|
| 278 |
+
for (const auto& tensor : list) {
|
| 279 |
+
current = prioritize(current, tensor, device_type);
|
| 280 |
+
}
|
| 281 |
+
return current;
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
// Template to catch non-Tensor args (no-op that returns current best guess)
|
| 285 |
+
template <typename T>
|
| 286 |
+
inline at::ScalarType prioritize(
|
| 287 |
+
at::ScalarType current,
|
| 288 |
+
T nextArg,
|
| 289 |
+
c10::DeviceType device_type = c10::DeviceType::CUDA) {
|
| 290 |
+
return current;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
// Overload for the tail case.
|
| 294 |
+
inline at::ScalarType promote_type(
|
| 295 |
+
at::ScalarType current,
|
| 296 |
+
c10::DeviceType device_type) {
|
| 297 |
+
return current;
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
// Unpack args and determine if incoming lower_precision_fp tensors need to be
|
| 301 |
+
// promoted to float32. Non-Tensor arguments are ignored.
|
| 302 |
+
template <typename Arg0, typename... Args>
|
| 303 |
+
inline at::ScalarType promote_type(
|
| 304 |
+
at::ScalarType current,
|
| 305 |
+
c10::DeviceType device_type,
|
| 306 |
+
Arg0 arg0,
|
| 307 |
+
Args... args) {
|
| 308 |
+
auto new_current = prioritize(current, arg0, device_type);
|
| 309 |
+
return promote_type(new_current, device_type, args...);
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
/****************************************************
|
| 313 |
+
Logic to apply cached casting to any Tensor argument.
|
| 314 |
+
****************************************************/
|
| 315 |
+
inline bool is_eligible(
|
| 316 |
+
const Tensor& arg,
|
| 317 |
+
c10::DeviceType device_type = c10::DeviceType::CUDA) {
|
| 318 |
+
return (
|
| 319 |
+
arg.defined() && is_autocast_eligible(arg, device_type) &&
|
| 320 |
+
(arg.scalar_type() != at::kDouble));
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
// Overload to catch Tensor args
|
| 324 |
+
TORCH_API Tensor cached_cast(
|
| 325 |
+
at::ScalarType to_type,
|
| 326 |
+
const Tensor& arg,
|
| 327 |
+
c10::DeviceType device_type = c10::DeviceType::CUDA);
|
| 328 |
+
|
| 329 |
+
// Overload to process std::optional<Tensor>
|
| 330 |
+
inline std::optional<Tensor> cached_cast(
|
| 331 |
+
at::ScalarType to_type,
|
| 332 |
+
const std::optional<Tensor>& arg,
|
| 333 |
+
c10::DeviceType device_type = c10::DeviceType::CUDA) {
|
| 334 |
+
if (arg.has_value()) {
|
| 335 |
+
return cached_cast(to_type, *arg, device_type);
|
| 336 |
+
} else {
|
| 337 |
+
return std::nullopt;
|
| 338 |
+
}
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
// Overload to process TensorLists
|
| 342 |
+
inline std::vector<Tensor> cached_cast(
|
| 343 |
+
at::ScalarType to_type,
|
| 344 |
+
const TensorList& arg,
|
| 345 |
+
c10::DeviceType device_type = c10::DeviceType::CUDA) {
|
| 346 |
+
std::vector<Tensor> vec;
|
| 347 |
+
vec.reserve(arg.size());
|
| 348 |
+
for (const auto& t : arg) {
|
| 349 |
+
vec.emplace_back(cached_cast(to_type, t, device_type));
|
| 350 |
+
}
|
| 351 |
+
return vec;
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
inline std::vector<Tensor> cached_cast(
|
| 355 |
+
at::ScalarType to_type,
|
| 356 |
+
const ITensorListRef& arg,
|
| 357 |
+
c10::DeviceType device_type = c10::DeviceType::CUDA) {
|
| 358 |
+
std::vector<Tensor> vec;
|
| 359 |
+
vec.reserve(arg.size());
|
| 360 |
+
for (const auto& t : arg) {
|
| 361 |
+
vec.emplace_back(cached_cast(to_type, t, device_type));
|
| 362 |
+
}
|
| 363 |
+
return vec;
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
// Template to catch non-Tensor args.
|
| 367 |
+
template <typename T>
|
| 368 |
+
inline T cached_cast(
|
| 369 |
+
at::ScalarType to_type,
|
| 370 |
+
T arg,
|
| 371 |
+
c10::DeviceType device_type = c10::DeviceType::CUDA) {
|
| 372 |
+
return arg;
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
/*******************************************************
|
| 376 |
+
Logic to flip an output dtype flag.
|
| 377 |
+
Keep it simple for now by assuming only one such flag is
|
| 378 |
+
present in the argument list. If I ever need a function
|
| 379 |
+
with more than flag I'll figure out something else.
|
| 380 |
+
The policy is:
|
| 381 |
+
If the user has explicitly specified a dtype, respect it.
|
| 382 |
+
Otherwise, set it to the autocast type.
|
| 383 |
+
********************************************************/
|
| 384 |
+
|
| 385 |
+
// Overload to catch dtype flags
|
| 386 |
+
std::optional<ScalarType> inline set_opt_dtype(
|
| 387 |
+
at::ScalarType to_type,
|
| 388 |
+
const std::optional<ScalarType>& dtype) {
|
| 389 |
+
return dtype.has_value() ? dtype : to_type;
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
// Template to catch other args
|
| 393 |
+
template <typename T>
|
| 394 |
+
inline T set_opt_dtype(at::ScalarType to_type, T arg) {
|
| 395 |
+
return arg;
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
template <typename... Args>
|
| 399 |
+
inline bool firstarg_is_eligible(
|
| 400 |
+
c10::DeviceType device_type,
|
| 401 |
+
const Tensor& arg,
|
| 402 |
+
Args... args) {
|
| 403 |
+
return is_eligible(arg, device_type);
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
template <typename... Args>
|
| 407 |
+
inline at::ScalarType type_from_firstarg(
|
| 408 |
+
c10::DeviceType device_type,
|
| 409 |
+
at::ScalarType to_type,
|
| 410 |
+
const Tensor& arg,
|
| 411 |
+
Args... args) {
|
| 412 |
+
return (is_eligible(arg, device_type) ? to_type : arg.scalar_type());
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
// Policies correspond to op categories that need code-divergent handling.
|
| 416 |
+
// Wrapper templates below are specialized based on a policy template parameter.
|
| 417 |
+
enum class CastPolicy : uint8_t {
|
| 418 |
+
lower_precision_fp = 0, // Cast all inputs to lower_precision_fp before
|
| 419 |
+
// running the op. Currently, lower_precision_fp is
|
| 420 |
+
// fp16 for AutocastCUDA, and is defined by user
|
| 421 |
+
// (default bf16) for AutocastCPU or other device.
|
| 422 |
+
fp32, // Cast all inputs to at::kFloat before running the op.
|
| 423 |
+
fp32_set_opt_dtype, // Treats functions (like softmax) that
|
| 424 |
+
// 1. we'd like to run in fp32 and
|
| 425 |
+
// 2. have a std::optional<ScalarType> arg that controls
|
| 426 |
+
// the output type.
|
| 427 |
+
// fp32_set_opt_dtype wrappers' policy is: if the output
|
| 428 |
+
// type is already set, don't touch it, otherwise, set
|
| 429 |
+
// it to at::kFloat.
|
| 430 |
+
fp32_append_dtype, // Treats functions (like norm) that
|
| 431 |
+
// 1. we'd like to run in fp32 and
|
| 432 |
+
// 2. have some overloads that accept an output type and
|
| 433 |
+
// other overloads that don't.
|
| 434 |
+
// fp32_append_dtype wrappers wrap the overloads that don't
|
| 435 |
+
// have an output dtype.
|
| 436 |
+
// The wrapper policy is: append at::kFloat to the args,
|
| 437 |
+
// and redispatch to the type-aware overload.
|
| 438 |
+
promote, // Run in the widest dtype among several args.
|
| 439 |
+
};
|
| 440 |
+
|
| 441 |
+
/********************************************************************************************************
|
| 442 |
+
Templates to provide wrapper functions
|
| 443 |
+
|
| 444 |
+
I'm copying the pattern used in core/boxing/impl/WrapFunctionIntoFunctor.h to
|
| 445 |
+
extract args and return type. (see also
|
| 446 |
+
https://stackoverflow.com/questions/46533698/how-to-deduce-argument-list-from-function-pointer)
|
| 447 |
+
|
| 448 |
+
This strategy uses an exterior "WrapFunction" that extracts arguments on behalf
|
| 449 |
+
of (in my case several specializations of) an interior "WrapFunction_".
|
| 450 |
+
Interior WrapFunction_ specializations are defined for each CastPolicy.
|
| 451 |
+
********************************************************************************************************/
|
| 452 |
+
|
| 453 |
+
// Base template for WrapFunction_, which is specialized to contain a "call"
|
| 454 |
+
// method each CastPolicy
|
| 455 |
+
template <
|
| 456 |
+
CastPolicy policy,
|
| 457 |
+
c10::DeviceType device_type,
|
| 458 |
+
class Redispatch,
|
| 459 |
+
Redispatch* F,
|
| 460 |
+
class Ret,
|
| 461 |
+
class ArgList>
|
| 462 |
+
struct WrapFunction_ {};
|
| 463 |
+
|
| 464 |
+
// CastPolicy::lower_precision_fp General_DeviceType
|
| 465 |
+
template <
|
| 466 |
+
c10::DeviceType device_type,
|
| 467 |
+
class Redispatch,
|
| 468 |
+
Redispatch* F,
|
| 469 |
+
class Ret,
|
| 470 |
+
class... Args>
|
| 471 |
+
struct WrapFunction_<
|
| 472 |
+
CastPolicy::lower_precision_fp,
|
| 473 |
+
device_type,
|
| 474 |
+
Redispatch,
|
| 475 |
+
F,
|
| 476 |
+
Ret,
|
| 477 |
+
guts::typelist::typelist<Args...>> {
|
| 478 |
+
static Ret call(Args... args) {
|
| 479 |
+
c10::impl::ExcludeDispatchKeyGuard no_autocast(
|
| 480 |
+
get_autocast_dispatch_key_from_device_type(device_type));
|
| 481 |
+
return (*F)(cached_cast(
|
| 482 |
+
get_lower_precision_fp_from_device_type(device_type),
|
| 483 |
+
args,
|
| 484 |
+
device_type)...);
|
| 485 |
+
}
|
| 486 |
+
};
|
| 487 |
+
|
| 488 |
+
// CastPolicy::fp32 General_DeviceType
|
| 489 |
+
template <
|
| 490 |
+
c10::DeviceType device_type,
|
| 491 |
+
class Redispatch,
|
| 492 |
+
Redispatch* F,
|
| 493 |
+
class Ret,
|
| 494 |
+
class... Args>
|
| 495 |
+
struct WrapFunction_<
|
| 496 |
+
CastPolicy::fp32,
|
| 497 |
+
device_type,
|
| 498 |
+
Redispatch,
|
| 499 |
+
F,
|
| 500 |
+
Ret,
|
| 501 |
+
guts::typelist::typelist<Args...>> {
|
| 502 |
+
static Ret call(Args... args) {
|
| 503 |
+
c10::impl::ExcludeDispatchKeyGuard no_autocast(
|
| 504 |
+
get_autocast_dispatch_key_from_device_type(device_type));
|
| 505 |
+
return (*F)(cached_cast(at::kFloat, args, device_type)...);
|
| 506 |
+
}
|
| 507 |
+
};
|
| 508 |
+
|
| 509 |
+
// CastPolicy::fp32_set_opt_dtype General_DeviceType
|
| 510 |
+
template <
|
| 511 |
+
c10::DeviceType device_type,
|
| 512 |
+
class Redispatch,
|
| 513 |
+
Redispatch* F,
|
| 514 |
+
class Ret,
|
| 515 |
+
class... Args>
|
| 516 |
+
struct WrapFunction_<
|
| 517 |
+
CastPolicy::fp32_set_opt_dtype,
|
| 518 |
+
device_type,
|
| 519 |
+
Redispatch,
|
| 520 |
+
F,
|
| 521 |
+
Ret,
|
| 522 |
+
guts::typelist::typelist<Args...>> {
|
| 523 |
+
static Ret call(Args... args) {
|
| 524 |
+
c10::impl::ExcludeDispatchKeyGuard no_autocast(
|
| 525 |
+
get_autocast_dispatch_key_from_device_type(device_type));
|
| 526 |
+
if (firstarg_is_eligible(device_type, args...)) {
|
| 527 |
+
return (*F)(set_opt_dtype(at::kFloat, args)...);
|
| 528 |
+
} else {
|
| 529 |
+
// If ineligible, calls F with unaltered args. Does not set opt dtype,
|
| 530 |
+
// because setting opt dtype explicitly may interfere with internal
|
| 531 |
+
// implicit promotion decisions.
|
| 532 |
+
return (*F)(args...);
|
| 533 |
+
}
|
| 534 |
+
}
|
| 535 |
+
};
|
| 536 |
+
|
| 537 |
+
// CastPolicy::fp32_append_dtype General_DeviceType
|
| 538 |
+
template <
|
| 539 |
+
c10::DeviceType device_type,
|
| 540 |
+
class Redispatch,
|
| 541 |
+
Redispatch* F,
|
| 542 |
+
class Ret,
|
| 543 |
+
class... Args>
|
| 544 |
+
struct WrapFunction_<
|
| 545 |
+
CastPolicy::fp32_append_dtype,
|
| 546 |
+
device_type,
|
| 547 |
+
Redispatch,
|
| 548 |
+
F,
|
| 549 |
+
Ret,
|
| 550 |
+
guts::typelist::typelist<Args...>> {
|
| 551 |
+
static Ret call(Args... args) {
|
| 552 |
+
c10::impl::ExcludeDispatchKeyGuard no_autocast(
|
| 553 |
+
get_autocast_dispatch_key_from_device_type(device_type));
|
| 554 |
+
at::ScalarType out_type =
|
| 555 |
+
type_from_firstarg(device_type, at::kFloat, args...);
|
| 556 |
+
return (*F)(args..., out_type);
|
| 557 |
+
}
|
| 558 |
+
};
|
| 559 |
+
|
| 560 |
+
// CastPolicy::promote General_DeviceType
|
| 561 |
+
template <
|
| 562 |
+
c10::DeviceType device_type,
|
| 563 |
+
class Redispatch,
|
| 564 |
+
Redispatch* F,
|
| 565 |
+
class Ret,
|
| 566 |
+
class... Args>
|
| 567 |
+
struct WrapFunction_<
|
| 568 |
+
CastPolicy::promote,
|
| 569 |
+
device_type,
|
| 570 |
+
Redispatch,
|
| 571 |
+
F,
|
| 572 |
+
Ret,
|
| 573 |
+
guts::typelist::typelist<Args...>> {
|
| 574 |
+
static Ret call(Args... args) {
|
| 575 |
+
c10::impl::ExcludeDispatchKeyGuard no_autocast(
|
| 576 |
+
get_autocast_dispatch_key_from_device_type(device_type));
|
| 577 |
+
auto to_type = promote_type(
|
| 578 |
+
get_lower_precision_fp_from_device_type(device_type),
|
| 579 |
+
device_type,
|
| 580 |
+
args...);
|
| 581 |
+
return (*F)(cached_cast(to_type, args, device_type)...);
|
| 582 |
+
}
|
| 583 |
+
};
|
| 584 |
+
|
| 585 |
+
// Wrapper to infer return_type and parameter_types for WrapFunction_ (imitating
|
| 586 |
+
// core/boxing/impl/WrapFunctionIntoFunctor.h)
|
| 587 |
+
template <
|
| 588 |
+
CastPolicy policy,
|
| 589 |
+
c10::DeviceType device_type,
|
| 590 |
+
class Registered, // The signature for which we're registering. The
|
| 591 |
+
// dispatcher's calling code invokes our registered
|
| 592 |
+
// functions with arguments matching Registered, so we
|
| 593 |
+
// register WrapFunction_::call methods with a matching
|
| 594 |
+
// signature to properly field those arguments.
|
| 595 |
+
// guts::function_traits below extracts return_type and
|
| 596 |
+
// parameter_types from Registered, which WrapFunction_
|
| 597 |
+
// templates above use to declare their call methods.
|
| 598 |
+
class Redispatch, // The signature for the function we're redispatching to.
|
| 599 |
+
// In most cases this is the same as Registered, but for
|
| 600 |
+
// some ops (for example, ops where we append a dtype)
|
| 601 |
+
// it's useful to redispatch to a function with a
|
| 602 |
+
// different signature.
|
| 603 |
+
Redispatch* F> // The actual function we're redispatching to.
|
| 604 |
+
struct WrapFunction final {
|
| 605 |
+
using type = WrapFunction_<
|
| 606 |
+
policy,
|
| 607 |
+
device_type,
|
| 608 |
+
Redispatch,
|
| 609 |
+
F,
|
| 610 |
+
typename guts::function_traits<Registered>::return_type,
|
| 611 |
+
typename guts::function_traits<Registered>::parameter_types>;
|
| 612 |
+
};
|
| 613 |
+
|
| 614 |
+
/*****************************************************************************************************************
|
| 615 |
+
This section performs load-time registration for autocast wrappers.
|
| 616 |
+
|
| 617 |
+
It's debatable at what level operations should be patched. We'd like casts to
|
| 618 |
+
be autograd-exposed and precede autograd history recording, so that for
|
| 619 |
+
lower_precision_fp ops, input tensors are saved for backward in
|
| 620 |
+
lower_precision_fp rather than fp32. Saving inputs in lower_precision_fp
|
| 621 |
+
can significantly reduce a model's memory footprint.
|
| 622 |
+
|
| 623 |
+
Option 1 (strawman): Patch only at the level of explicit calls into
|
| 624 |
+
cudnn/cublas (cudnn_convolution, etc), because those are the code paths that are
|
| 625 |
+
guaranteed to use Tensor Cores, therefore they're the ones that will benefit
|
| 626 |
+
most from lower_precision_fp. Potential pitfall: convolutions (and other ops)
|
| 627 |
+
are wrapped in several layers of at::* calls. If one of those happens to record
|
| 628 |
+
autograd history, then we've lost the opportunity to save inputs in
|
| 629 |
+
lower_precision_fp.
|
| 630 |
+
|
| 631 |
+
Option 2: Patch the Python-exposed surface of calls, to make 100% sure autograd
|
| 632 |
+
history recording can't sneak in ahead of autocast. This mirrors Apex most
|
| 633 |
+
closely.
|
| 634 |
+
|
| 635 |
+
I think Option 2 is the right answer for all ops, not just convolutions. Option
|
| 636 |
+
2 is what I implement here.
|
| 637 |
+
*****************************************************************************************************************/
|
| 638 |
+
|
| 639 |
+
/********************************************************************************************************************
|
| 640 |
+
Explicit registration for out-of-place ops
|
| 641 |
+
|
| 642 |
+
The stuff below could be codegenned. Ed said
|
| 643 |
+
> you are going to have to write the function definition at some point, I
|
| 644 |
+
wouldn't try to get clever about it Therefore, for the moment, this is all
|
| 645 |
+
copy pasted in from VariableTypeEverything.cpp with appropriate substitutions.
|
| 646 |
+
********************************************************************************************************************/
|
| 647 |
+
|
| 648 |
+
} // namespace at::autocast
|
| 649 |
+
|
| 650 |
+
#define ADD_NS(RAW_OP) at::RAW_OP
|
| 651 |
+
|
| 652 |
+
#define _KERNEL_OVERLOAD_NARG_IMPL(_0, _1, _2, N, ...) N
|
| 653 |
+
#define _KERNEL_OVERLOAD_NARG(...) \
|
| 654 |
+
C10_EXPAND_MSVC_WORKAROUND(_KERNEL_OVERLOAD_NARG_IMPL(__VA_ARGS__, 2, 1))
|
| 655 |
+
|
| 656 |
+
// Common cases where registration signature matches redispatch signature
|
| 657 |
+
// (that's why SIGNATURE is repeated in the WrapFunction instantiation)
|
| 658 |
+
#define KERNEL1(DISPATCHKEY, OP, POLICY) \
|
| 659 |
+
m.impl( \
|
| 660 |
+
TORCH_SELECTIVE_NAME("aten::" #OP), \
|
| 661 |
+
&::at::autocast::WrapFunction< \
|
| 662 |
+
::at::autocast::CastPolicy::POLICY, \
|
| 663 |
+
DISPATCHKEY, \
|
| 664 |
+
decltype(ATEN_FN(OP)), \
|
| 665 |
+
decltype(ATEN_FN(OP)), \
|
| 666 |
+
&ATEN_FN(OP)>::type::call);
|
| 667 |
+
|
| 668 |
+
#define KERNEL2(DISPATCHKEY, OP, OVERLOAD, POLICY) \
|
| 669 |
+
m.impl( \
|
| 670 |
+
TORCH_SELECTIVE_NAME("aten::" #OP "." #OVERLOAD), \
|
| 671 |
+
&::at::autocast::WrapFunction< \
|
| 672 |
+
::at::autocast::CastPolicy::POLICY, \
|
| 673 |
+
DISPATCHKEY, \
|
| 674 |
+
decltype(ATEN_FN2(OP, OVERLOAD)), \
|
| 675 |
+
decltype(ATEN_FN2(OP, OVERLOAD)), \
|
| 676 |
+
&ATEN_FN2(OP, OVERLOAD)>::type::call);
|
| 677 |
+
|
| 678 |
+
#define _KERNEL_DISPATCH(DISPATCHKEY, NARG, ...) \
|
| 679 |
+
C10_CONCATENATE(KERNEL, NARG)(DISPATCHKEY, __VA_ARGS__)
|
| 680 |
+
|
| 681 |
+
#define _KERNEL_IMPL(DISPATCHKEY, ...) \
|
| 682 |
+
_KERNEL_DISPATCH(DISPATCHKEY, _KERNEL_OVERLOAD_NARG(__VA_ARGS__), __VA_ARGS__)
|
| 683 |
+
|
| 684 |
+
// It will dispatch to KERNEL1 or KERNEL2 based on its inputs.
|
| 685 |
+
#define KERNEL(DISPATCHKEY, ...) _KERNEL_IMPL(DISPATCHKEY, __VA_ARGS__)
|
| 686 |
+
|
| 687 |
+
// Less-common but still useful case: redispatching to a function
|
| 688 |
+
// with a new signature (e.g. appending a dtype)
|
| 689 |
+
#define KERNEL_DIFFERENT_REDISPATCH_SIGNATURE( \
|
| 690 |
+
DISPATCHKEY, \
|
| 691 |
+
REDISPATCH_FUNC, \
|
| 692 |
+
REGISTER_NAME, \
|
| 693 |
+
REGISTER_SIGNATURE, \
|
| 694 |
+
REDISPATCH_SIGNATURE, \
|
| 695 |
+
POLICY) \
|
| 696 |
+
m.impl( \
|
| 697 |
+
TORCH_SELECTIVE_NAME("aten::" REGISTER_NAME), \
|
| 698 |
+
&::at::autocast::WrapFunction< \
|
| 699 |
+
::at::autocast::CastPolicy::POLICY, \
|
| 700 |
+
DISPATCHKEY, \
|
| 701 |
+
REGISTER_SIGNATURE, \
|
| 702 |
+
REDISPATCH_SIGNATURE, \
|
| 703 |
+
&REDISPATCH_FUNC>::type::call);
|
| 704 |
+
|
| 705 |
+
// KERNEL_CPU/KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_CPU
|
| 706 |
+
// registration (OP, POLICY) or (OP, OVERLOAD, POLICY) for AutocastCPU
|
| 707 |
+
#define KERNEL_CPU(...) KERNEL(c10::DeviceType::CPU, __VA_ARGS__)
|
| 708 |
+
|
| 709 |
+
#define KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_CPU( \
|
| 710 |
+
REDISPATCH_FUNC, \
|
| 711 |
+
REGISTER_NAME, \
|
| 712 |
+
REGISTER_SIGNATURE, \
|
| 713 |
+
REDISPATCH_SIGNATURE, \
|
| 714 |
+
POLICY) \
|
| 715 |
+
KERNEL_DIFFERENT_REDISPATCH_SIGNATURE( \
|
| 716 |
+
c10::DeviceType::CPU, \
|
| 717 |
+
REDISPATCH_FUNC, \
|
| 718 |
+
REGISTER_NAME, \
|
| 719 |
+
REGISTER_SIGNATURE, \
|
| 720 |
+
REDISPATCH_SIGNATURE, \
|
| 721 |
+
POLICY)
|
| 722 |
+
|
| 723 |
+
// KERNEL_CUDA/KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_CUDA
|
| 724 |
+
// registration (OP, POLICY) or (OP, OVERLOAD, POLICY) for AutocastCUDA
|
| 725 |
+
#define KERNEL_CUDA(...) KERNEL(c10::DeviceType::CUDA, __VA_ARGS__)
|
| 726 |
+
|
| 727 |
+
#define KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_CUDA( \
|
| 728 |
+
REDISPATCH_FUNC, \
|
| 729 |
+
REGISTER_NAME, \
|
| 730 |
+
REGISTER_SIGNATURE, \
|
| 731 |
+
REDISPATCH_SIGNATURE, \
|
| 732 |
+
POLICY) \
|
| 733 |
+
KERNEL_DIFFERENT_REDISPATCH_SIGNATURE( \
|
| 734 |
+
c10::DeviceType::CUDA, \
|
| 735 |
+
REDISPATCH_FUNC, \
|
| 736 |
+
REGISTER_NAME, \
|
| 737 |
+
REGISTER_SIGNATURE, \
|
| 738 |
+
REDISPATCH_SIGNATURE, \
|
| 739 |
+
POLICY)
|
| 740 |
+
|
| 741 |
+
// KERNEL_MTIA/KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_MTIA
|
| 742 |
+
// registration (OP, POLICY) or (OP, OVERLOAD, POLICY) for AutocastMTIA
|
| 743 |
+
#define KERNEL_MTIA(...) KERNEL(c10::DeviceType::MTIA, __VA_ARGS__)
|
| 744 |
+
|
| 745 |
+
#define KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_MTIA( \
|
| 746 |
+
REDISPATCH_FUNC, \
|
| 747 |
+
REGISTER_NAME, \
|
| 748 |
+
REGISTER_SIGNATURE, \
|
| 749 |
+
REDISPATCH_SIGNATURE, \
|
| 750 |
+
POLICY) \
|
| 751 |
+
KERNEL_DIFFERENT_REDISPATCH_SIGNATURE( \
|
| 752 |
+
c10::DeviceType::MTIA, \
|
| 753 |
+
REDISPATCH_FUNC, \
|
| 754 |
+
REGISTER_NAME, \
|
| 755 |
+
REGISTER_SIGNATURE, \
|
| 756 |
+
REDISPATCH_SIGNATURE, \
|
| 757 |
+
POLICY)
|
| 758 |
+
|
| 759 |
+
// KERNEL_MAIA/KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_MAIA
|
| 760 |
+
// registration (OP, POLICY) or (OP, OVERLOAD, POLICY) for AutocastMAIA
|
| 761 |
+
#define KERNEL_MAIA(...) KERNEL(c10::DeviceType::MAIA, __VA_ARGS__)
|
| 762 |
+
|
| 763 |
+
#define KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_MAIA( \
|
| 764 |
+
REDISPATCH_FUNC, \
|
| 765 |
+
REGISTER_NAME, \
|
| 766 |
+
REGISTER_SIGNATURE, \
|
| 767 |
+
REDISPATCH_SIGNATURE, \
|
| 768 |
+
POLICY) \
|
| 769 |
+
KERNEL_DIFFERENT_REDISPATCH_SIGNATURE( \
|
| 770 |
+
c10::DeviceType::MAIA, \
|
| 771 |
+
REDISPATCH_FUNC, \
|
| 772 |
+
REGISTER_NAME, \
|
| 773 |
+
REGISTER_SIGNATURE, \
|
| 774 |
+
REDISPATCH_SIGNATURE, \
|
| 775 |
+
POLICY)
|
| 776 |
+
|
| 777 |
+
// KERNEL_XPU/KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_XPU
|
| 778 |
+
// registration (OP, POLICY) or (OP, OVERLOAD, POLICY) for AutocastXPU
|
| 779 |
+
#define KERNEL_XPU(...) KERNEL(c10::DeviceType::XPU, __VA_ARGS__)
|
| 780 |
+
|
| 781 |
+
#define KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_XPU( \
|
| 782 |
+
REDISPATCH_FUNC, \
|
| 783 |
+
REGISTER_NAME, \
|
| 784 |
+
REGISTER_SIGNATURE, \
|
| 785 |
+
REDISPATCH_SIGNATURE, \
|
| 786 |
+
POLICY) \
|
| 787 |
+
KERNEL_DIFFERENT_REDISPATCH_SIGNATURE( \
|
| 788 |
+
c10::DeviceType::XPU, \
|
| 789 |
+
REDISPATCH_FUNC, \
|
| 790 |
+
REGISTER_NAME, \
|
| 791 |
+
REGISTER_SIGNATURE, \
|
| 792 |
+
REDISPATCH_SIGNATURE, \
|
| 793 |
+
POLICY)
|
| 794 |
+
|
| 795 |
+
// KERNEL_PRIVATEUSEONE/KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_PRIVATEUSEONE
|
| 796 |
+
// registration (OP, POLICY) or (OP, OVERLOAD, POLICY) for AutocastPrivateUse1
|
| 797 |
+
#define KERNEL_PRIVATEUSEONE(...) \
|
| 798 |
+
KERNEL(c10::DeviceType::PrivateUse1, __VA_ARGS__)
|
| 799 |
+
|
| 800 |
+
#define KERNEL_DIFFERENT_REDISPATCH_SIGNATURE_PRIVATEUSEONE( \
|
| 801 |
+
REDISPATCH_FUNC, \
|
| 802 |
+
REGISTER_NAME, \
|
| 803 |
+
REGISTER_SIGNATURE, \
|
| 804 |
+
REDISPATCH_SIGNATURE, \
|
| 805 |
+
POLICY) \
|
| 806 |
+
KERNEL_DIFFERENT_REDISPATCH_SIGNATURE( \
|
| 807 |
+
c10::DeviceType::PrivateUse1, \
|
| 808 |
+
REDISPATCH_FUNC, \
|
| 809 |
+
REGISTER_NAME, \
|
| 810 |
+
REGISTER_SIGNATURE, \
|
| 811 |
+
REDISPATCH_SIGNATURE, \
|
| 812 |
+
POLICY)
|
| 813 |
+
|
| 814 |
+
// KERNEL_MPS
|
| 815 |
+
// registration (OP, POLICY) or (OP, OVERLOAD, POLICY) for AutocastMPS
|
| 816 |
+
#define KERNEL_MPS(...) KERNEL(c10::DeviceType::MPS, __VA_ARGS__)
|
| 817 |
+
|
| 818 |
+
// Op lists for different policies.
|
| 819 |
+
// To make sure other backends can reuse the policy op list.
|
| 820 |
+
#define AT_FORALL_LOWER_PRECISION_FP(_) \
|
| 821 |
+
_(_convolution, deprecated) \
|
| 822 |
+
_(_convolution) \
|
| 823 |
+
_(conv1d) \
|
| 824 |
+
_(conv2d) \
|
| 825 |
+
_(conv3d) \
|
| 826 |
+
_(conv_tbc) \
|
| 827 |
+
_(conv_transpose1d) \
|
| 828 |
+
_(conv_transpose2d, input) \
|
| 829 |
+
_(conv_transpose3d, input) \
|
| 830 |
+
_(convolution) \
|
| 831 |
+
_(prelu) \
|
| 832 |
+
_(addmm) \
|
| 833 |
+
_(addmv) \
|
| 834 |
+
_(addr) \
|
| 835 |
+
_(matmul) \
|
| 836 |
+
_(einsum) \
|
| 837 |
+
_(mm) \
|
| 838 |
+
_(mv) \
|
| 839 |
+
_(linalg_vecdot) \
|
| 840 |
+
_(linear) \
|
| 841 |
+
_(addbmm) \
|
| 842 |
+
_(baddbmm) \
|
| 843 |
+
_(bmm) \
|
| 844 |
+
_(chain_matmul) \
|
| 845 |
+
_(linalg_multi_dot) \
|
| 846 |
+
_(_thnn_fused_lstm_cell) \
|
| 847 |
+
_(_thnn_fused_gru_cell) \
|
| 848 |
+
_(lstm_cell) \
|
| 849 |
+
_(gru_cell) \
|
| 850 |
+
_(rnn_tanh_cell) \
|
| 851 |
+
_(rnn_relu_cell) \
|
| 852 |
+
_(_scaled_dot_product_flash_attention) \
|
| 853 |
+
_(scaled_dot_product_attention)
|
| 854 |
+
|
| 855 |
+
#define AT_FORALL_FP32(_) \
|
| 856 |
+
_(acos) \
|
| 857 |
+
_(asin) \
|
| 858 |
+
_(cosh) \
|
| 859 |
+
_(erfinv) \
|
| 860 |
+
_(exp) \
|
| 861 |
+
_(expm1) \
|
| 862 |
+
_(log) \
|
| 863 |
+
_(log10) \
|
| 864 |
+
_(log2) \
|
| 865 |
+
_(log1p) \
|
| 866 |
+
_(reciprocal) \
|
| 867 |
+
_(rsqrt) \
|
| 868 |
+
_(sinh) \
|
| 869 |
+
_(tan) \
|
| 870 |
+
_(pow, Tensor_Scalar) \
|
| 871 |
+
_(pow, Tensor_Tensor) \
|
| 872 |
+
_(pow, Scalar) \
|
| 873 |
+
_(softplus) \
|
| 874 |
+
_(layer_norm) \
|
| 875 |
+
_(native_layer_norm) \
|
| 876 |
+
_(group_norm) \
|
| 877 |
+
_(frobenius_norm, dim) \
|
| 878 |
+
_(nuclear_norm) \
|
| 879 |
+
_(nuclear_norm, dim) \
|
| 880 |
+
_(cosine_similarity) \
|
| 881 |
+
_(poisson_nll_loss) \
|
| 882 |
+
_(cosine_embedding_loss) \
|
| 883 |
+
_(nll_loss) \
|
| 884 |
+
_(nll_loss2d) \
|
| 885 |
+
_(hinge_embedding_loss) \
|
| 886 |
+
_(kl_div) \
|
| 887 |
+
_(l1_loss) \
|
| 888 |
+
_(smooth_l1_loss) \
|
| 889 |
+
_(huber_loss) \
|
| 890 |
+
_(mse_loss) \
|
| 891 |
+
_(margin_ranking_loss) \
|
| 892 |
+
_(multilabel_margin_loss) \
|
| 893 |
+
_(soft_margin_loss) \
|
| 894 |
+
_(triplet_margin_loss) \
|
| 895 |
+
_(multi_margin_loss) \
|
| 896 |
+
_(binary_cross_entropy_with_logits) \
|
| 897 |
+
_(dist) \
|
| 898 |
+
_(pdist) \
|
| 899 |
+
_(cdist) \
|
| 900 |
+
_(renorm) \
|
| 901 |
+
_(logsumexp) \
|
| 902 |
+
_(upsample_nearest1d) \
|
| 903 |
+
_(_upsample_nearest_exact1d) \
|
| 904 |
+
_(upsample_nearest2d) \
|
| 905 |
+
_(_upsample_nearest_exact2d) \
|
| 906 |
+
_(upsample_nearest3d) \
|
| 907 |
+
_(_upsample_nearest_exact3d) \
|
| 908 |
+
_(upsample_linear1d) \
|
| 909 |
+
_(upsample_bilinear2d) \
|
| 910 |
+
_(_upsample_bilinear2d_aa) \
|
| 911 |
+
_(upsample_trilinear3d) \
|
| 912 |
+
_(upsample_bicubic2d) \
|
| 913 |
+
_(_upsample_bicubic2d_aa)
|
| 914 |
+
|
| 915 |
+
#define AT_FORALL_FP32_SET_OPT_DTYPE(_) \
|
| 916 |
+
_(prod) \
|
| 917 |
+
_(prod, dim_int) \
|
| 918 |
+
_(prod, dim_Dimname) \
|
| 919 |
+
_(softmax, int) \
|
| 920 |
+
_(softmax, Dimname) \
|
| 921 |
+
_(log_softmax, int) \
|
| 922 |
+
_(log_softmax, Dimname) \
|
| 923 |
+
_(cumprod) \
|
| 924 |
+
_(cumprod, dimname) \
|
| 925 |
+
_(cumsum) \
|
| 926 |
+
_(cumsum, dimname) \
|
| 927 |
+
_(linalg_vector_norm) \
|
| 928 |
+
_(linalg_matrix_norm) \
|
| 929 |
+
_(linalg_matrix_norm, str_ord) \
|
| 930 |
+
_(sum) \
|
| 931 |
+
_(sum, dim_IntList) \
|
| 932 |
+
_(sum, dim_DimnameList)
|
| 933 |
+
|
| 934 |
+
#define AT_FORALL_DIFFERENT_REDISPATCH_SIGNATURE(_) \
|
| 935 |
+
_(ADD_NS(norm), \
|
| 936 |
+
"norm.Scalar", \
|
| 937 |
+
Tensor(const Tensor&, const Scalar&), \
|
| 938 |
+
Tensor(const Tensor&, const std::optional<Scalar>&, ScalarType), \
|
| 939 |
+
fp32_append_dtype) \
|
| 940 |
+
_(ADD_NS(norm), \
|
| 941 |
+
"norm.ScalarOpt_dim", \
|
| 942 |
+
Tensor(const Tensor&, const std::optional<Scalar>&, IntArrayRef, bool), \
|
| 943 |
+
Tensor( \
|
| 944 |
+
const Tensor&, \
|
| 945 |
+
const std::optional<Scalar>&, \
|
| 946 |
+
IntArrayRef, \
|
| 947 |
+
bool, \
|
| 948 |
+
ScalarType), \
|
| 949 |
+
fp32_append_dtype) \
|
| 950 |
+
_(ADD_NS(norm), \
|
| 951 |
+
"norm.names_ScalarOpt_dim", \
|
| 952 |
+
Tensor(const Tensor&, const std::optional<Scalar>&, DimnameList, bool), \
|
| 953 |
+
Tensor( \
|
| 954 |
+
const Tensor&, \
|
| 955 |
+
const std::optional<Scalar>&, \
|
| 956 |
+
DimnameList, \
|
| 957 |
+
bool, \
|
| 958 |
+
ScalarType), \
|
| 959 |
+
fp32_append_dtype)
|
| 960 |
+
|
| 961 |
+
#define AT_FORALL_PROMOTE(_) \
|
| 962 |
+
_(addcdiv) \
|
| 963 |
+
_(addcmul) \
|
| 964 |
+
_(atan2) \
|
| 965 |
+
_(bilinear) \
|
| 966 |
+
_(cross) \
|
| 967 |
+
_(dot) \
|
| 968 |
+
_(vdot) \
|
| 969 |
+
_(grid_sampler) \
|
| 970 |
+
_(index_put) \
|
| 971 |
+
_(tensordot) \
|
| 972 |
+
_(scatter_add)
|
| 973 |
+
|
| 974 |
+
#else
|
| 975 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 976 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/ceil_div.h
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <c10/macros/Macros.h>
|
| 4 |
+
#include <type_traits>
|
| 5 |
+
|
| 6 |
+
namespace at {
|
| 7 |
+
|
| 8 |
+
/**
|
| 9 |
+
Computes ceil(a / b)
|
| 10 |
+
*/
|
| 11 |
+
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
|
| 12 |
+
C10_ALWAYS_INLINE C10_HOST_DEVICE T ceil_div(T a, T b) {
|
| 13 |
+
return (a + b - 1) / b;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
/**
|
| 17 |
+
Computes ceil(a / b) * b; i.e., rounds up `a` to the next highest
|
| 18 |
+
multiple of b
|
| 19 |
+
*/
|
| 20 |
+
template <typename T>
|
| 21 |
+
C10_ALWAYS_INLINE C10_HOST_DEVICE T round_up(T a, T b) {
|
| 22 |
+
return ceil_div(a, b) * b;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
} // namespace at
|
| 26 |
+
|
| 27 |
+
#else
|
| 28 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 29 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/code_template.h
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/util/irange.h>
|
| 5 |
+
|
| 6 |
+
#include <sstream>
|
| 7 |
+
#include <string>
|
| 8 |
+
#include <unordered_map>
|
| 9 |
+
#include <vector>
|
| 10 |
+
|
| 11 |
+
namespace at::jit {
|
| 12 |
+
|
| 13 |
+
// A template environment is a mapping from template variable names, e.g.,
|
| 14 |
+
// identifier (corresponding to $identifier) to their expansions.
|
| 15 |
+
//
|
| 16 |
+
// This template environment supports storing strings, numbers and lists
|
| 17 |
+
// of strings, and can be chained together (so that lookup proceeds in
|
| 18 |
+
// in the top level environment, and then recurses into a parent
|
| 19 |
+
// environment if the key is not found.)
|
| 20 |
+
struct TemplateEnv {
|
| 21 |
+
TemplateEnv() = default;
|
| 22 |
+
TemplateEnv(TemplateEnv& parent) : parent(&parent) {}
|
| 23 |
+
TemplateEnv(TemplateEnv&&) = delete;
|
| 24 |
+
TemplateEnv& operator=(const TemplateEnv& parent) = delete;
|
| 25 |
+
TemplateEnv& operator=(TemplateEnv&& parent) = delete;
|
| 26 |
+
~TemplateEnv() = default;
|
| 27 |
+
|
| 28 |
+
using string_list = std::vector<std::string>;
|
| 29 |
+
|
| 30 |
+
// Add a string 'v' to the map at key 'k'.
|
| 31 |
+
void s(const std::string& k, const std::string& v) {
|
| 32 |
+
strings_[k] = v;
|
| 33 |
+
lists_.erase(k);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// Add a number 'v' to the map at key 'k'
|
| 37 |
+
template <typename T>
|
| 38 |
+
void d(const std::string& k, const T& v) {
|
| 39 |
+
strings_[k] = std::to_string(v);
|
| 40 |
+
lists_.erase(k);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// Retrieve the string representation of the value stored at 'k' from the map.
|
| 44 |
+
// Raises an exception if the key is not found.
|
| 45 |
+
const std::string& s(const std::string& k) const {
|
| 46 |
+
if (strings_.count(k) == 0) {
|
| 47 |
+
if (parent) {
|
| 48 |
+
return parent->s(k);
|
| 49 |
+
}
|
| 50 |
+
notFound(k);
|
| 51 |
+
}
|
| 52 |
+
return strings_.at(k);
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// Store a list of strings 'v' in the map at 'k'.
|
| 56 |
+
void v(const std::string& k, const string_list& v) {
|
| 57 |
+
lists_[k] = v;
|
| 58 |
+
strings_.erase(k);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
// Retrieve a list of strings stored at 'k' from the map.
|
| 62 |
+
// Raises an exception if the key is not found.
|
| 63 |
+
const string_list& v(const std::string& k) const {
|
| 64 |
+
if (lists_.count(k) == 0) {
|
| 65 |
+
if (parent) {
|
| 66 |
+
return parent->v(k);
|
| 67 |
+
}
|
| 68 |
+
notFound(k);
|
| 69 |
+
}
|
| 70 |
+
return lists_.at(k);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
// Test if a string 'k' is a string (as opposed to a list.)
|
| 74 |
+
bool keyIsString(const std::string& k) const {
|
| 75 |
+
if (strings_.count(k) > 0)
|
| 76 |
+
return true;
|
| 77 |
+
if (lists_.count(k) > 0)
|
| 78 |
+
return false;
|
| 79 |
+
if (parent)
|
| 80 |
+
return parent->keyIsString(k);
|
| 81 |
+
notFound(k);
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
private:
|
| 85 |
+
[[noreturn]] void notFound(const std::string& k) const {
|
| 86 |
+
std::stringstream ss;
|
| 87 |
+
ss << "key not found: " << k;
|
| 88 |
+
throw std::logic_error(ss.str());
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
std::unordered_map<std::string, std::string> strings_;
|
| 92 |
+
std::unordered_map<std::string, string_list> lists_;
|
| 93 |
+
TemplateEnv* parent{nullptr};
|
| 94 |
+
};
|
| 95 |
+
|
| 96 |
+
/*
|
| 97 |
+
# Match $identifier or ${identifier} and replace with the value in env.
|
| 98 |
+
# If this identifier is at the beginning of whitespace on a line
|
| 99 |
+
# and its value is a list then it is treated as
|
| 100 |
+
# block substitution by indenting all lines of all elements.
|
| 101 |
+
# If the identifier is on a line starting with non-whitespace and a list
|
| 102 |
+
# then it is comma separated. ${,foo} will insert a comma before the list
|
| 103 |
+
# if this list is not empty and ${foo,} will insert one after.
|
| 104 |
+
*/
|
| 105 |
+
struct CodeTemplate {
|
| 106 |
+
/* implicit */ CodeTemplate(std::string t) : template_text(std::move(t)) {}
|
| 107 |
+
|
| 108 |
+
std::string format(const TemplateEnv& env) const {
|
| 109 |
+
std::stringstream out;
|
| 110 |
+
size_t pos = 0;
|
| 111 |
+
size_t indent = 0;
|
| 112 |
+
bool all_whitespace = true;
|
| 113 |
+
while (pos < template_text.size()) {
|
| 114 |
+
char c = template_text[pos];
|
| 115 |
+
if (c == '$') {
|
| 116 |
+
std::stringstream kss;
|
| 117 |
+
bool comma_before = false;
|
| 118 |
+
bool comma_after = false;
|
| 119 |
+
size_t new_pos = parseKey(pos, kss, comma_before, comma_after);
|
| 120 |
+
std::string k = kss.str();
|
| 121 |
+
bool is_string = env.keyIsString(k);
|
| 122 |
+
if (all_whitespace) {
|
| 123 |
+
if (is_string)
|
| 124 |
+
emitStringWithIndents(out, indent, env.s(k));
|
| 125 |
+
else
|
| 126 |
+
emitLinesIndented(out, indent, env.v(k));
|
| 127 |
+
} else {
|
| 128 |
+
if (is_string)
|
| 129 |
+
out << env.s(k);
|
| 130 |
+
else
|
| 131 |
+
emitCommaSeparatedList(out, env.v(k), comma_before, comma_after);
|
| 132 |
+
}
|
| 133 |
+
all_whitespace = false;
|
| 134 |
+
pos = new_pos;
|
| 135 |
+
} else {
|
| 136 |
+
out << c;
|
| 137 |
+
if (!isspace(c))
|
| 138 |
+
all_whitespace = false;
|
| 139 |
+
indent++;
|
| 140 |
+
if (c == '\n') {
|
| 141 |
+
indent = 0;
|
| 142 |
+
all_whitespace = true;
|
| 143 |
+
}
|
| 144 |
+
pos++;
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
return out.str();
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
private:
|
| 151 |
+
using string_list = std::vector<std::string>;
|
| 152 |
+
char charAt(size_t p) const {
|
| 153 |
+
if (p >= template_text.size())
|
| 154 |
+
throw std::logic_error("EOS found in key");
|
| 155 |
+
return template_text[p];
|
| 156 |
+
}
|
| 157 |
+
size_t parseKey(
|
| 158 |
+
size_t pos,
|
| 159 |
+
std::ostream& k,
|
| 160 |
+
bool& comma_before,
|
| 161 |
+
bool& comma_after) const {
|
| 162 |
+
comma_before = false;
|
| 163 |
+
comma_after = false;
|
| 164 |
+
pos++;
|
| 165 |
+
if (charAt(pos) == '{') {
|
| 166 |
+
pos++;
|
| 167 |
+
if (charAt(pos) == ',') {
|
| 168 |
+
comma_before = true;
|
| 169 |
+
pos++;
|
| 170 |
+
}
|
| 171 |
+
pos = parseIdent(pos, k);
|
| 172 |
+
if (charAt(pos) == ',') {
|
| 173 |
+
comma_after = true;
|
| 174 |
+
pos++;
|
| 175 |
+
}
|
| 176 |
+
if (charAt(pos) != '}')
|
| 177 |
+
throw std::logic_error("missing terminating '}'");
|
| 178 |
+
pos++;
|
| 179 |
+
return pos;
|
| 180 |
+
} else {
|
| 181 |
+
return parseIdent(pos, k);
|
| 182 |
+
}
|
| 183 |
+
}
|
| 184 |
+
size_t parseIdent(size_t pos, std::ostream& k) const {
|
| 185 |
+
while (pos < template_text.size() &&
|
| 186 |
+
(isalnum(template_text[pos]) || template_text[pos] == '_')) {
|
| 187 |
+
k << template_text[pos];
|
| 188 |
+
pos++;
|
| 189 |
+
}
|
| 190 |
+
return pos;
|
| 191 |
+
}
|
| 192 |
+
void emitCommaSeparatedList(
|
| 193 |
+
std::ostream& out,
|
| 194 |
+
const string_list& strings,
|
| 195 |
+
bool comma_before,
|
| 196 |
+
bool comma_after) const {
|
| 197 |
+
if (comma_before && !strings.empty())
|
| 198 |
+
out << ", ";
|
| 199 |
+
for (const auto i : c10::irange(strings.size())) {
|
| 200 |
+
if (i > 0)
|
| 201 |
+
out << ", ";
|
| 202 |
+
out << strings[i];
|
| 203 |
+
}
|
| 204 |
+
if (comma_after && !strings.empty())
|
| 205 |
+
out << ", ";
|
| 206 |
+
}
|
| 207 |
+
// These indentation functions follow the convention that they never emit
|
| 208 |
+
// leading or trailing newlines when the input string does not have leading
|
| 209 |
+
// or trailing newlines. It's the responsibility of the calling function
|
| 210 |
+
// to indent correctly in the context.
|
| 211 |
+
void emitIndent(std::ostream& out, size_t indent) const {
|
| 212 |
+
for ([[maybe_unused]] const auto i : c10::irange(indent)) {
|
| 213 |
+
out << ' ';
|
| 214 |
+
}
|
| 215 |
+
}
|
| 216 |
+
void emitStringWithIndents(
|
| 217 |
+
std::ostream& out,
|
| 218 |
+
size_t indent,
|
| 219 |
+
const std::string& str) const {
|
| 220 |
+
for (auto c : str) {
|
| 221 |
+
out << c;
|
| 222 |
+
if (c == '\n') {
|
| 223 |
+
emitIndent(out, indent);
|
| 224 |
+
}
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
+
void emitLinesIndented(
|
| 228 |
+
std::stringstream& out,
|
| 229 |
+
size_t indent,
|
| 230 |
+
const string_list& strings) const {
|
| 231 |
+
for (const auto i : c10::irange(strings.size())) {
|
| 232 |
+
if (i > 0)
|
| 233 |
+
emitIndent(out, indent);
|
| 234 |
+
emitStringWithIndents(out, indent, strings[i]);
|
| 235 |
+
if (i + 1 != strings.size())
|
| 236 |
+
out << '\n';
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
std::string template_text;
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
static inline std::string format(const std::string& fmt, TemplateEnv& env) {
|
| 243 |
+
return CodeTemplate(fmt).format(env);
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
} // namespace at::jit
|
| 247 |
+
|
| 248 |
+
#else
|
| 249 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 250 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/ATenGeneral.h
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/macros/Macros.h>
|
| 5 |
+
|
| 6 |
+
#else
|
| 7 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 8 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/ATenOpList.h
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/macros/Export.h>
|
| 5 |
+
|
| 6 |
+
namespace c10 {
|
| 7 |
+
struct OperatorName;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
namespace at {
|
| 11 |
+
|
| 12 |
+
// check if an op is a custom op (i.e. did not come from native_functions.yaml)
|
| 13 |
+
TORCH_API bool is_custom_op(const c10::OperatorName& opName);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
#else
|
| 17 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 18 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/ATen_fwd.h
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <c10/core/QScheme.h>
|
| 4 |
+
|
| 5 |
+
// Forward declarations of core ATen types used in dispatch functions
|
| 6 |
+
namespace c10 {
|
| 7 |
+
|
| 8 |
+
template<typename T>
|
| 9 |
+
class List;
|
| 10 |
+
template<typename T>
|
| 11 |
+
class IListRef;
|
| 12 |
+
class Stream;
|
| 13 |
+
class Scalar;
|
| 14 |
+
class SymInt;
|
| 15 |
+
class SymIntList;
|
| 16 |
+
struct Storage;
|
| 17 |
+
struct TensorOptions;
|
| 18 |
+
template <typename T>
|
| 19 |
+
class ArrayRef;
|
| 20 |
+
template <typename T>
|
| 21 |
+
class OptionalArrayRef;
|
| 22 |
+
|
| 23 |
+
} // namespace c10
|
| 24 |
+
|
| 25 |
+
namespace at {
|
| 26 |
+
|
| 27 |
+
class Tensor;
|
| 28 |
+
class OptionalTensorRef;
|
| 29 |
+
struct Dimname;
|
| 30 |
+
struct Generator;
|
| 31 |
+
using TensorList = c10::ArrayRef<Tensor>;
|
| 32 |
+
using ITensorListRef = c10::IListRef<Tensor>;
|
| 33 |
+
using IOptTensorListRef = c10::IListRef<OptionalTensorRef>;
|
| 34 |
+
using DimnameList = c10::ArrayRef<Dimname>;
|
| 35 |
+
using IntArrayRef = c10::ArrayRef<int64_t>;
|
| 36 |
+
using OptionalIntArrayRef = c10::OptionalArrayRef<int64_t>;
|
| 37 |
+
using OptionalSymIntArrayRef = c10::OptionalArrayRef<c10::SymInt>;
|
| 38 |
+
|
| 39 |
+
using c10::Stream;
|
| 40 |
+
using c10::Storage;
|
| 41 |
+
using c10::QScheme;
|
| 42 |
+
using c10::Scalar;
|
| 43 |
+
using c10::SymInt;
|
| 44 |
+
using c10::SymIntList;
|
| 45 |
+
using c10::TensorOptions;
|
| 46 |
+
|
| 47 |
+
} // namespace at
|
| 48 |
+
|
| 49 |
+
#else
|
| 50 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 51 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/ATen_pch.h
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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ds will be next to useless
|
| 4 |
+
#pragma push_macro("TORCH_ASSERT_NO_OPERATORS")
|
| 5 |
+
#define TORCH_ASSERT_NO_OPERATORS
|
| 6 |
+
|
| 7 |
+
#include <cinttypes>
|
| 8 |
+
|
| 9 |
+
// This list of headers was generated using a script that finds
|
| 10 |
+
// high-impact headers and then manually tweaked to remove OS specific
|
| 11 |
+
// or duplicate headers (e.g. <cassert> and <assert.h>) and to remove
|
| 12 |
+
// "impl" headers (e.g BFloat16-inl.h or complex_math.h in c10).
|
| 13 |
+
|
| 14 |
+
// To generate the initial list:
|
| 15 |
+
// 1. Build pytorch from scratch with all build caching disabled
|
| 16 |
+
// 2. Generate a build trace with ninjatracing (https://github.com/nico/ninjatracing)
|
| 17 |
+
// $ ninjatracing /path/to/pytorch/build/.ninja_log > trace_all.json
|
| 18 |
+
// 3. Run pch_gen.py from https://github.com/peterbell10/build_analysis/
|
| 19 |
+
// $ python pch_gen.py --threshold .80 --target torch_cpu --build_dir /path/to/pytorch/build --trace trace_all.json
|
| 20 |
+
// Where the threshold can be tweaked until c10 and some of ATen
|
| 21 |
+
// core are included but TORCH_ASSERT_NO_OPERATORS still passes.
|
| 22 |
+
|
| 23 |
+
#include <cerrno>
|
| 24 |
+
#include <cmath>
|
| 25 |
+
#include <cstddef>
|
| 26 |
+
#include <cstdint>
|
| 27 |
+
#include <cstdlib>
|
| 28 |
+
#include <cstring>
|
| 29 |
+
|
| 30 |
+
#include <algorithm>
|
| 31 |
+
#include <array>
|
| 32 |
+
#include <atomic>
|
| 33 |
+
#include <chrono>
|
| 34 |
+
#include <complex>
|
| 35 |
+
#include <deque>
|
| 36 |
+
#include <exception>
|
| 37 |
+
#include <functional>
|
| 38 |
+
#include <initializer_list>
|
| 39 |
+
#include <iomanip>
|
| 40 |
+
#include <iosfwd>
|
| 41 |
+
#include <iterator>
|
| 42 |
+
#include <limits>
|
| 43 |
+
#include <list>
|
| 44 |
+
#include <map>
|
| 45 |
+
#include <memory>
|
| 46 |
+
#include <mutex>
|
| 47 |
+
#include <new>
|
| 48 |
+
#include <numeric>
|
| 49 |
+
#include <ostream>
|
| 50 |
+
#include <sstream>
|
| 51 |
+
#include <stdexcept>
|
| 52 |
+
#include <string>
|
| 53 |
+
#include <string_view>
|
| 54 |
+
#include <tuple>
|
| 55 |
+
#include <type_traits>
|
| 56 |
+
#include <typeindex>
|
| 57 |
+
#include <typeinfo>
|
| 58 |
+
#include <unordered_map>
|
| 59 |
+
#include <unordered_set>
|
| 60 |
+
#include <utility>
|
| 61 |
+
#include <vector>
|
| 62 |
+
|
| 63 |
+
#include <c10/core/Allocator.h>
|
| 64 |
+
#include <c10/core/AutogradState.h>
|
| 65 |
+
#include <c10/core/Backend.h>
|
| 66 |
+
#include <c10/core/DefaultDtype.h>
|
| 67 |
+
#include <c10/core/Device.h>
|
| 68 |
+
#include <c10/core/DeviceType.h>
|
| 69 |
+
#include <c10/core/DispatchKey.h>
|
| 70 |
+
#include <c10/core/DispatchKeySet.h>
|
| 71 |
+
#include <c10/core/GeneratorImpl.h>
|
| 72 |
+
#include <c10/core/InferenceMode.h>
|
| 73 |
+
#include <c10/core/Layout.h>
|
| 74 |
+
#include <c10/core/MemoryFormat.h>
|
| 75 |
+
#include <c10/core/OptionalRef.h>
|
| 76 |
+
#include <c10/core/QScheme.h>
|
| 77 |
+
#include <c10/core/Scalar.h>
|
| 78 |
+
#include <c10/core/ScalarType.h>
|
| 79 |
+
#include <c10/core/ScalarTypeToTypeMeta.h>
|
| 80 |
+
#include <c10/core/Storage.h>
|
| 81 |
+
#include <c10/core/StorageImpl.h>
|
| 82 |
+
#include <c10/core/SymBool.h>
|
| 83 |
+
#include <c10/core/SymFloat.h>
|
| 84 |
+
#include <c10/core/SymInt.h>
|
| 85 |
+
#include <c10/core/SymIntArrayRef.h>
|
| 86 |
+
#include <c10/core/SymNodeImpl.h>
|
| 87 |
+
#include <c10/core/TensorImpl.h>
|
| 88 |
+
#include <c10/core/TensorOptions.h>
|
| 89 |
+
#include <c10/core/UndefinedTensorImpl.h>
|
| 90 |
+
#include <c10/core/WrapDimMinimal.h>
|
| 91 |
+
#include <c10/core/impl/LocalDispatchKeySet.h>
|
| 92 |
+
#include <c10/core/impl/PyInterpreter.h>
|
| 93 |
+
#include <c10/core/impl/SizesAndStrides.h>
|
| 94 |
+
|
| 95 |
+
#include <c10/macros/Export.h>
|
| 96 |
+
#include <c10/macros/Macros.h>
|
| 97 |
+
|
| 98 |
+
#include <c10/util/AlignOf.h>
|
| 99 |
+
#include <c10/util/ArrayRef.h>
|
| 100 |
+
#include <c10/util/BFloat16.h>
|
| 101 |
+
#include <c10/util/C++17.h>
|
| 102 |
+
#include <c10/util/ConstexprCrc.h>
|
| 103 |
+
#include <c10/util/Deprecated.h>
|
| 104 |
+
#include <c10/util/DimVector.h>
|
| 105 |
+
#include <c10/util/Exception.h>
|
| 106 |
+
#include <c10/util/ExclusivelyOwned.h>
|
| 107 |
+
#include <c10/util/Flags.h>
|
| 108 |
+
#include <c10/util/Float8_e4m3fn.h>
|
| 109 |
+
#include <c10/util/Float8_e5m2.h>
|
| 110 |
+
#include <c10/util/Float8_e4m3fnuz.h>
|
| 111 |
+
#include <c10/util/Float8_e5m2fnuz.h>
|
| 112 |
+
#include <c10/util/FunctionRef.h>
|
| 113 |
+
#include <c10/util/Half.h>
|
| 114 |
+
#include <c10/util/IdWrapper.h>
|
| 115 |
+
#include <c10/util/Logging.h>
|
| 116 |
+
#include <c10/util/MaybeOwned.h>
|
| 117 |
+
#include <c10/util/Metaprogramming.h>
|
| 118 |
+
#include <c10/util/Optional.h>
|
| 119 |
+
#include <c10/util/Registry.h>
|
| 120 |
+
#include <c10/util/SmallVector.h>
|
| 121 |
+
#include <c10/util/StringUtil.h>
|
| 122 |
+
#include <c10/util/ThreadLocalDebugInfo.h>
|
| 123 |
+
#include <c10/util/Type.h>
|
| 124 |
+
#include <c10/util/TypeCast.h>
|
| 125 |
+
#include <c10/util/TypeIndex.h>
|
| 126 |
+
#include <c10/util/TypeList.h>
|
| 127 |
+
#include <c10/util/TypeSafeSignMath.h>
|
| 128 |
+
#include <c10/util/TypeTraits.h>
|
| 129 |
+
#include <c10/util/UniqueVoidPtr.h>
|
| 130 |
+
#include <c10/util/accumulate.h>
|
| 131 |
+
#include <c10/util/bit_cast.h>
|
| 132 |
+
#include <c10/util/bits.h>
|
| 133 |
+
#include <c10/util/complex.h>
|
| 134 |
+
#include <c10/util/floating_point_utils.h>
|
| 135 |
+
#include <c10/util/intrusive_ptr.h>
|
| 136 |
+
#include <c10/util/irange.h>
|
| 137 |
+
#include <c10/util/llvmMathExtras.h>
|
| 138 |
+
#include <c10/util/python_stub.h>
|
| 139 |
+
#include <c10/util/qint32.h>
|
| 140 |
+
#include <c10/util/qint8.h>
|
| 141 |
+
#include <c10/util/quint2x4.h>
|
| 142 |
+
#include <c10/util/quint4x2.h>
|
| 143 |
+
#include <c10/util/quint8.h>
|
| 144 |
+
#include <c10/util/safe_numerics.h>
|
| 145 |
+
#include <c10/util/string_utils.h>
|
| 146 |
+
#include <c10/util/string_view.h>
|
| 147 |
+
#include <c10/util/typeid.h>
|
| 148 |
+
|
| 149 |
+
#include <ATen/StorageUtils.h>
|
| 150 |
+
#include <ATen/core/ATen_fwd.h>
|
| 151 |
+
#include <ATen/core/DeprecatedTypeProperties.h>
|
| 152 |
+
#include <ATen/core/DeprecatedTypePropertiesRegistry.h>
|
| 153 |
+
#include <ATen/core/DimVector.h>
|
| 154 |
+
#include <ATen/core/Dimname.h>
|
| 155 |
+
#include <ATen/core/Generator.h>
|
| 156 |
+
#include <ATen/core/NamedTensor.h>
|
| 157 |
+
#include <ATen/core/QuantizerBase.h>
|
| 158 |
+
#include <ATen/core/TensorAccessor.h>
|
| 159 |
+
#include <ATen/core/TensorBase.h>
|
| 160 |
+
#include <ATen/core/symbol.h>
|
| 161 |
+
|
| 162 |
+
#pragma pop_macro("TORCH_ASSERT_NO_OPERATORS")
|
| 163 |
+
|
| 164 |
+
#else
|
| 165 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 166 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Array.h
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
// A fixed-size array type usable from both host and
|
| 5 |
+
// device code.
|
| 6 |
+
|
| 7 |
+
#include <c10/macros/Macros.h>
|
| 8 |
+
#include <c10/util/irange.h>
|
| 9 |
+
|
| 10 |
+
namespace at::detail {
|
| 11 |
+
|
| 12 |
+
template <typename T, int size_>
|
| 13 |
+
struct Array {
|
| 14 |
+
// NOLINTNEXTLINE(*c-array*)
|
| 15 |
+
T data[size_];
|
| 16 |
+
|
| 17 |
+
C10_HOST_DEVICE T operator[](int i) const {
|
| 18 |
+
return data[i];
|
| 19 |
+
}
|
| 20 |
+
C10_HOST_DEVICE T& operator[](int i) {
|
| 21 |
+
return data[i];
|
| 22 |
+
}
|
| 23 |
+
#if defined(USE_ROCM)
|
| 24 |
+
C10_HOST_DEVICE Array() = default;
|
| 25 |
+
C10_HOST_DEVICE Array(const Array&) = default;
|
| 26 |
+
C10_HOST_DEVICE Array& operator=(const Array&) = default;
|
| 27 |
+
C10_HOST_DEVICE Array(Array&&) = default;
|
| 28 |
+
C10_HOST_DEVICE Array& operator=(Array&&) = default;
|
| 29 |
+
C10_HOST_DEVICE ~Array() = default;
|
| 30 |
+
#else
|
| 31 |
+
Array() = default;
|
| 32 |
+
Array(const Array&) = default;
|
| 33 |
+
Array& operator=(const Array&) = default;
|
| 34 |
+
Array(Array&&) noexcept = default;
|
| 35 |
+
Array& operator=(Array&&) noexcept = default;
|
| 36 |
+
~Array() = default;
|
| 37 |
+
#endif
|
| 38 |
+
static constexpr int size() {
|
| 39 |
+
return size_;
|
| 40 |
+
}
|
| 41 |
+
// Fill the array with x.
|
| 42 |
+
C10_HOST_DEVICE Array(T x) {
|
| 43 |
+
for (int i = 0; i < size_; i++) {
|
| 44 |
+
data[i] = x;
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
};
|
| 48 |
+
|
| 49 |
+
} // namespace at::detail
|
| 50 |
+
|
| 51 |
+
#else
|
| 52 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 53 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Backtrace.h
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#include <c10/util/Backtrace.h>
|
| 3 |
+
#include <c10/util/Type.h>
|
| 4 |
+
|
| 5 |
+
#else
|
| 6 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 7 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/Allocator.h>
|
| 5 |
+
#include <c10/core/AllocatorConfig.h>
|
| 6 |
+
#include <c10/core/Stream.h>
|
| 7 |
+
#include <c10/core/thread_pool.h>
|
| 8 |
+
#include <c10/util/flat_hash_map.h>
|
| 9 |
+
#include <c10/util/llvmMathExtras.h>
|
| 10 |
+
#include <iostream>
|
| 11 |
+
#include <optional>
|
| 12 |
+
|
| 13 |
+
#include <deque>
|
| 14 |
+
#include <mutex>
|
| 15 |
+
|
| 16 |
+
C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED("-Wunused-parameter")
|
| 17 |
+
namespace at {
|
| 18 |
+
|
| 19 |
+
using c10::CachingAllocator::Stat;
|
| 20 |
+
using c10::CachingAllocator::DurationStat;
|
| 21 |
+
|
| 22 |
+
/**
|
| 23 |
+
* HostBlock is typically a fundamental memory block used in pinned memory. It
|
| 24 |
+
* is likely related to Event and Stream of device runtime. It is probably a
|
| 25 |
+
* base struct or interface that can be inherited and extended by each backend.
|
| 26 |
+
*/
|
| 27 |
+
template <typename S>
|
| 28 |
+
struct HostBlock {
|
| 29 |
+
// constructor for search key
|
| 30 |
+
HostBlock(size_t size) : size_(size) {}
|
| 31 |
+
|
| 32 |
+
HostBlock(size_t size, void* ptr) : size_(size), ptr_(ptr) {}
|
| 33 |
+
|
| 34 |
+
std::mutex mutex_;
|
| 35 |
+
size_t size_{0}; // block size in bytes
|
| 36 |
+
void* ptr_{nullptr}; // memory address
|
| 37 |
+
bool allocated_{false}; // in-use flag
|
| 38 |
+
size_t event_count_{0}; // number of related events
|
| 39 |
+
ska::flat_hash_set<S> streams_; // streams on which the block was used
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
template <typename B>
|
| 43 |
+
struct alignas(hardware_destructive_interference_size) FreeBlockList {
|
| 44 |
+
std::mutex mutex_;
|
| 45 |
+
std::deque<B*> list_;
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
namespace {
|
| 49 |
+
// Max cached block sizes: (1 << MAX_SIZE_INDEX) bytes
|
| 50 |
+
// NOLINTNEXTLINE(misc-definitions-in-headers)
|
| 51 |
+
constexpr size_t MAX_SIZE_INDEX = 64;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// A large reserved pinned memory segment that is created in advance which is used
|
| 55 |
+
// to allocate small pinned memory requests to avoid calling into expensive APIs.
|
| 56 |
+
// We never free this memory and move up the pointer as we allocate new blocks
|
| 57 |
+
// and when blocks are freed, they are cached in the free lists.
|
| 58 |
+
struct PinnedReserveSegment {
|
| 59 |
+
PinnedReserveSegment(void *start, size_t size) : start_(start), size_(size),
|
| 60 |
+
current_ptr_(start_), initialized_(true) {}
|
| 61 |
+
|
| 62 |
+
PinnedReserveSegment() : start_(nullptr), size_(0), current_ptr_(nullptr), initialized_(false) {}
|
| 63 |
+
|
| 64 |
+
bool initialized() {
|
| 65 |
+
return initialized_;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
void* allocate(size_t bytes) {
|
| 69 |
+
std::lock_guard<std::mutex> guard(mutex_);
|
| 70 |
+
|
| 71 |
+
// Round up the requested size to 4KB boundary for all including the small ones.
|
| 72 |
+
size_t rounded_bytes = (bytes + 4096 - 1) & ~(4096 - 1);
|
| 73 |
+
|
| 74 |
+
if (((uint8_t*)current_ptr_ + rounded_bytes) > ((uint8_t*)start_ + size_)) {
|
| 75 |
+
return nullptr;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
void* ptr = current_ptr_;
|
| 79 |
+
current_ptr_ = (uint8_t*)current_ptr_ + rounded_bytes;
|
| 80 |
+
return ptr;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
bool owns(void* ptr) {
|
| 84 |
+
return ptr >= start_ && ptr < (uint8_t*)start_ + size_;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
std::mutex mutex_;
|
| 88 |
+
void* start_;
|
| 89 |
+
size_t size_;
|
| 90 |
+
void* current_ptr_;
|
| 91 |
+
bool initialized_;
|
| 92 |
+
};
|
| 93 |
+
|
| 94 |
+
// Struct containing memory allocator summary statistics for host.
|
| 95 |
+
struct TORCH_API HostStats {
|
| 96 |
+
// COUNT: total allocations (active)
|
| 97 |
+
Stat active_requests;
|
| 98 |
+
// SUM: bytes allocated/reserved by this memory allocator. (active)
|
| 99 |
+
Stat active_bytes;
|
| 100 |
+
// COUNT: total allocations (active + free)
|
| 101 |
+
Stat allocations;
|
| 102 |
+
// SUM: bytes allocated/reserved by this memory allocator. This accounts
|
| 103 |
+
// for both free and in-use blocks.
|
| 104 |
+
Stat allocated_bytes;
|
| 105 |
+
|
| 106 |
+
// SUM: time spent in cudaHostAlloc/cudaHostRegister in microseconds
|
| 107 |
+
DurationStat host_alloc_time;
|
| 108 |
+
|
| 109 |
+
// SUM: time spent in cudaHostFree/cudaHostUnregister in microseconds
|
| 110 |
+
DurationStat host_free_time;
|
| 111 |
+
|
| 112 |
+
// COUNT: number of times cudaHostAlloc/cudaHostRegister was called because
|
| 113 |
+
// the request could not be satisfied from existing free blocks.
|
| 114 |
+
int64_t num_host_alloc = 0; // This is derived from segment or timing
|
| 115 |
+
|
| 116 |
+
// COUNT: number of times cudaHostFree/cudaHostUnregister was called.
|
| 117 |
+
int64_t num_host_free = 0; // This is derived from segment or timing
|
| 118 |
+
|
| 119 |
+
// Count of cudaHostAlloc/cudaHostRegister per bucket
|
| 120 |
+
std::vector<int64_t> bucket_allocation = std::vector<int64_t>(MAX_SIZE_INDEX);
|
| 121 |
+
};
|
| 122 |
+
|
| 123 |
+
// Struct containing memory allocator summary statistics for host, as they
|
| 124 |
+
// are staged for reporting. This is a temporary struct that is used to
|
| 125 |
+
// avoid locking the allocator while collecting stats.
|
| 126 |
+
struct alignas(hardware_destructive_interference_size) HostStatsStaged {
|
| 127 |
+
std::mutex timing_mutex_;
|
| 128 |
+
// COUNT: total allocations (active + free)
|
| 129 |
+
// LOCK: access to this stat is protected by the allocator's blocks_mutex_
|
| 130 |
+
Stat allocations;
|
| 131 |
+
// SUM: bytes allocated/reserved by this memory allocator. This accounts
|
| 132 |
+
// for both free and in-use blocks.
|
| 133 |
+
Stat allocated_bytes;
|
| 134 |
+
// COUNT: number of allocations per bucket (active)
|
| 135 |
+
// LOCK: access to this stat is protected by the per bucket free_list_[index].mutex_
|
| 136 |
+
std::vector<Stat> active_bucket_stats = std::vector<Stat>(MAX_SIZE_INDEX);
|
| 137 |
+
// SUM: bytes of allocation per bucket (active)
|
| 138 |
+
// LOCK: access to this stat is protected by the per bucket free_list_[index].mutex_
|
| 139 |
+
std::vector<Stat> active_bytes_bucket_stats = std::vector<Stat>(MAX_SIZE_INDEX);
|
| 140 |
+
// COUNT: number of allocations per bucket (active + free)
|
| 141 |
+
// LOCK: access to this stat is protected by the per bucket free_list_[index].mutex_
|
| 142 |
+
std::vector<Stat> allocation_bucket_stats = std::vector<Stat>(MAX_SIZE_INDEX);
|
| 143 |
+
// SUM: bytes of allocation per bucket (active + free)
|
| 144 |
+
// LOCK: access to this stat is protected by the per bucket free_list_[index].mutex_
|
| 145 |
+
std::vector<Stat> allocated_bytes_bucket_stats = std::vector<Stat>(MAX_SIZE_INDEX);
|
| 146 |
+
// SUM: time spent in cudaHostAlloc/cudaHostRegister
|
| 147 |
+
// LOCK: access to this stat is protected by the timing_mutex_
|
| 148 |
+
DurationStat host_alloc_time;
|
| 149 |
+
// SUM: time spent in cudaHostFree/cudaHostUnregister
|
| 150 |
+
// LOCK: access to this stat is protected by the timing_mutex_
|
| 151 |
+
DurationStat host_free_time;
|
| 152 |
+
};
|
| 153 |
+
|
| 154 |
+
/**
|
| 155 |
+
* Note [HostAllocator design]
|
| 156 |
+
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 157 |
+
* We have three key data structures - the free list which stores blocks that
|
| 158 |
+
* are not currently used, the block list which stores all blocks that have been
|
| 159 |
+
* allocated, and the event queue which stores runtime events and their
|
| 160 |
+
* corresponding blocks.
|
| 161 |
+
*
|
| 162 |
+
* Each of these are protected by a separate mutex. The key design principles
|
| 163 |
+
* are to 1) only hold each mutex for the minimal amount of time possible, 2)
|
| 164 |
+
* never do any possible expensive operations (such as CUDA runtime API calls)
|
| 165 |
+
* while holding the lock.
|
| 166 |
+
*
|
| 167 |
+
* There are four public methods: allocate, free, record_event and empty_cache.
|
| 168 |
+
* 1) In the allocate path, we first check to see if we can service our
|
| 169 |
+
* request from this free list, and otherwise we create a new block with
|
| 170 |
+
* allocate_host_memory.
|
| 171 |
+
* 2) In the free path, we insert events (if required) into the event queue,
|
| 172 |
+
* and if possible insert our block back into the free list. In allocate, we
|
| 173 |
+
* first eagerly query events until we find one that is not ready, and insert
|
| 174 |
+
* the corresponding block onto the free list if all the events recorded for a
|
| 175 |
+
* block are ready.
|
| 176 |
+
* 3) In the record_event path, we simply insert the given stream into the set
|
| 177 |
+
* of streams tracked by the specified block. This set of streams is then
|
| 178 |
+
* consumed in the free path.
|
| 179 |
+
* 4) In the empty_cache path, we flush any available blocks into the free
|
| 180 |
+
* list. Remove all element of free list, then remove them from block list and
|
| 181 |
+
* release the associated pinned memory allocation via free_block.
|
| 182 |
+
*
|
| 183 |
+
* We generalize the caching host allocator into two parts: interface and
|
| 184 |
+
* implementation. For any new backend looking to integrate with host allocator
|
| 185 |
+
* and reuse caching mechanism, these two parts are necessary to be specialized.
|
| 186 |
+
*
|
| 187 |
+
* For the implementation, we provide a CachingHostAllocatorImpl struct
|
| 188 |
+
* to abstract the caching mechanism. Any backend needs to provide a customized
|
| 189 |
+
* implementation by specializing its own public functions and the related
|
| 190 |
+
* runtime functions. Its template parameter S represents runtime Stream, E
|
| 191 |
+
* denotes runtime Event, B indicates the fundamental memory block.
|
| 192 |
+
*
|
| 193 |
+
* For the interface, we provide a CachingHostAllocatorInterface struct as an
|
| 194 |
+
* interface. Any backend needs to derive its own host allocator from this
|
| 195 |
+
* interface. Its template parameter T refers to an implementation that
|
| 196 |
+
* inherited from CachingHostAllocatorImpl.
|
| 197 |
+
*
|
| 198 |
+
* So this design can share the caching mechanism across each backend, and
|
| 199 |
+
* provide flexibility to each backend. A backend can choose to follow this
|
| 200 |
+
* implementation or reuse them by extending and overriding them as necessary.
|
| 201 |
+
* Taking CUDA as an example, it specializes runtime related functions to reuse
|
| 202 |
+
* the caching mechanism. Additionally, it extends the allocator's functionality
|
| 203 |
+
* by adding the allocWithCudaHostRegister function to support page-locking the
|
| 204 |
+
* memory range used by CUDA. Of course, you can also refer to
|
| 205 |
+
* XPUCachingHostAllocator, which is a host caching allocator supported on XPU
|
| 206 |
+
* backend, to implement a basic host caching allocator.
|
| 207 |
+
*
|
| 208 |
+
* Some of the invariants here are less strict than they could be - for example,
|
| 209 |
+
* we do not enforce that free(Block* block) => block->event_count == 0. This is
|
| 210 |
+
* for compatibility reasons, and we can explore enforcing these in subsequent
|
| 211 |
+
* versions.
|
| 212 |
+
*
|
| 213 |
+
* Note that this caching host allocator does not split larger allocations into
|
| 214 |
+
* smaller blocks, unlike the caching device allocator.
|
| 215 |
+
*
|
| 216 |
+
* In order to gather statistics about caching host allocator while minimally
|
| 217 |
+
* impacting performance, we use a HostStatsStaged struct to stage the stats
|
| 218 |
+
* before reporting them. This is done to avoid adding new locks to the allocator.
|
| 219 |
+
* Collecting stats is carefully done under existing locks, and then the staged
|
| 220 |
+
* stats are converted to the final stats when getStats is called. At that time
|
| 221 |
+
* we hold the same locks as empty_cache, to ensure the fidelity of the stats.
|
| 222 |
+
*/
|
| 223 |
+
|
| 224 |
+
template <
|
| 225 |
+
typename S,
|
| 226 |
+
typename E,
|
| 227 |
+
typename B = HostBlock<S>>
|
| 228 |
+
struct CachingHostAllocatorImpl {
|
| 229 |
+
virtual ~CachingHostAllocatorImpl() {
|
| 230 |
+
if (active_) {
|
| 231 |
+
active_ = false;
|
| 232 |
+
getBackgroundThreadPool()->waitWorkComplete();
|
| 233 |
+
}
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
public:
|
| 237 |
+
// return data_ptr and block pair.
|
| 238 |
+
virtual std::pair<void*, void*> allocate(size_t size) {
|
| 239 |
+
if (size == 0) {
|
| 240 |
+
return {nullptr, nullptr};
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
// If we are using background threads, we can process events in the
|
| 244 |
+
// background.
|
| 245 |
+
if (!pinned_use_background_threads()) {
|
| 246 |
+
process_events();
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
// Round up the allocation to the nearest power of two to improve reuse.
|
| 250 |
+
// These power of two sizes are also used to index into the free list.
|
| 251 |
+
size_t roundSize = c10::llvm::PowerOf2Ceil(size);
|
| 252 |
+
|
| 253 |
+
// First, try to allocate from the free list
|
| 254 |
+
auto* block = get_free_block(roundSize);
|
| 255 |
+
if (block) {
|
| 256 |
+
return {block->ptr_, reinterpret_cast<void*>(block)};
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
// Check in the recently freed blocks with pending events to see if we
|
| 260 |
+
// can reuse them. Call get_free_block again after processing events
|
| 261 |
+
if (pinned_use_background_threads()) {
|
| 262 |
+
// Launch the background thread and process events in a loop.
|
| 263 |
+
static bool background_thread_flag [[maybe_unused]] = [this] {
|
| 264 |
+
active_ = true;
|
| 265 |
+
getBackgroundThreadPool()->run([&]() {
|
| 266 |
+
while (active_) {
|
| 267 |
+
process_events();
|
| 268 |
+
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
| 269 |
+
}
|
| 270 |
+
});
|
| 271 |
+
return true;
|
| 272 |
+
}();
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
// Slow path: if we can't allocate from the cached free list, we need
|
| 276 |
+
// to create a new block.
|
| 277 |
+
void* ptr = nullptr;
|
| 278 |
+
allocate_host_memory(roundSize, &ptr);
|
| 279 |
+
|
| 280 |
+
// Then, create a new block.
|
| 281 |
+
block = new B(roundSize, ptr);
|
| 282 |
+
block->allocated_ = true;
|
| 283 |
+
|
| 284 |
+
add_allocated_block(block);
|
| 285 |
+
return {block->ptr_, reinterpret_cast<void*>(block)};
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
virtual void free(void* ctx) {
|
| 289 |
+
if (!ctx) {
|
| 290 |
+
return;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
// Note: we can assume that free is correctly paired with alloc, and thus we
|
| 294 |
+
// do not need to look up the ctx in blocks_.
|
| 295 |
+
auto* block = reinterpret_cast<B*>(ctx);
|
| 296 |
+
|
| 297 |
+
std::optional<std::vector<E>> events;
|
| 298 |
+
ska::flat_hash_set<S> streams;
|
| 299 |
+
{
|
| 300 |
+
std::lock_guard<std::mutex> g(block->mutex_);
|
| 301 |
+
block->allocated_ = false;
|
| 302 |
+
if (block->streams_.empty()) {
|
| 303 |
+
TORCH_INTERNAL_ASSERT(block->event_count_ == 0);
|
| 304 |
+
} else {
|
| 305 |
+
events = std::vector<E>();
|
| 306 |
+
events->reserve(block->streams_.size());
|
| 307 |
+
block->event_count_ += block->streams_.size();
|
| 308 |
+
// Move out streams to avoid holding the mutex during event recording
|
| 309 |
+
streams = std::move(block->streams_);
|
| 310 |
+
block->streams_.clear();
|
| 311 |
+
}
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
// Event recording must be done outside the mutex to avoid potential
|
| 315 |
+
// deadlocks (e.g., when Python GIL is involved)
|
| 316 |
+
for (auto stream : streams) {
|
| 317 |
+
record_stream(events, stream);
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
if (!events) {
|
| 321 |
+
auto index = size_index(block->size_);
|
| 322 |
+
std::lock_guard<std::mutex> g(free_list_[index].mutex_);
|
| 323 |
+
free_list_[index].list_.push_back(block);
|
| 324 |
+
} else {
|
| 325 |
+
// restore these events that record by used streams.
|
| 326 |
+
std::lock_guard<std::mutex> g(events_mutex_);
|
| 327 |
+
for (auto&& event : *events) {
|
| 328 |
+
events_.emplace_front(std::move(event), block);
|
| 329 |
+
}
|
| 330 |
+
}
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
virtual bool record_event(void* ptr, void* ctx, c10::Stream s) {
|
| 334 |
+
S stream = S(s);
|
| 335 |
+
auto* block = reinterpret_cast<B*>(ctx);
|
| 336 |
+
|
| 337 |
+
// Note: we need to check if the passed-in `ctx` is valid. This is because
|
| 338 |
+
// `record_event` (via `CachingHostAllocator_recordEvent`) can be invoked on
|
| 339 |
+
// an arbitrary tensor, and is not guaranteed to correspond to a pinned
|
| 340 |
+
// memory allocation. Therefore, we need to check that `ctx` is valid before
|
| 341 |
+
// proceeding.
|
| 342 |
+
{
|
| 343 |
+
std::lock_guard<std::mutex> g(blocks_mutex_);
|
| 344 |
+
if (blocks_.find(block) != blocks_.end()) {
|
| 345 |
+
// Now we know this object is safe to access.
|
| 346 |
+
std::lock_guard<std::mutex> gb(block->mutex_);
|
| 347 |
+
TORCH_INTERNAL_ASSERT(block->allocated_);
|
| 348 |
+
block->streams_.insert(stream);
|
| 349 |
+
return true;
|
| 350 |
+
}
|
| 351 |
+
auto it = ptr_to_block_.find(ptr);
|
| 352 |
+
if (it != ptr_to_block_.end()) {
|
| 353 |
+
block = it->second;
|
| 354 |
+
std::lock_guard<std::mutex> g(block->mutex_);
|
| 355 |
+
TORCH_INTERNAL_ASSERT(block->allocated_);
|
| 356 |
+
block->streams_.insert(stream);
|
| 357 |
+
return true;
|
| 358 |
+
}
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
return false;
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
virtual void empty_cache() {
|
| 365 |
+
// Flush any available blocks into the free_list.
|
| 366 |
+
process_events();
|
| 367 |
+
|
| 368 |
+
// Remove all elements from the free list, remove them from the blocks
|
| 369 |
+
// list, and free the associated pinned memory allocation. This requires
|
| 370 |
+
// concurrently holding both the free list mutexes and the blocks mutex, and
|
| 371 |
+
// is the only function that concurrently holds multiple mutexes.
|
| 372 |
+
for (size_t i = 0; i < free_list_.size(); ++i) {
|
| 373 |
+
std::lock(free_list_[i].mutex_, blocks_mutex_);
|
| 374 |
+
std::lock_guard<std::mutex> gf(free_list_[i].mutex_, std::adopt_lock);
|
| 375 |
+
std::lock_guard<std::mutex> gb(blocks_mutex_, std::adopt_lock);
|
| 376 |
+
|
| 377 |
+
std::vector<B*> blocks_to_remove(free_list_[i].list_.begin(), free_list_[i].list_.end());
|
| 378 |
+
free_list_[i].list_.clear();
|
| 379 |
+
|
| 380 |
+
for (auto* block : blocks_to_remove) {
|
| 381 |
+
blocks_.erase(block);
|
| 382 |
+
ptr_to_block_.erase(block->ptr_);
|
| 383 |
+
auto index = size_index(block->size_);
|
| 384 |
+
free_block(block);
|
| 385 |
+
stats_.allocations.decrease(1);
|
| 386 |
+
stats_.allocated_bytes.decrease(block->size_);
|
| 387 |
+
stats_.allocation_bucket_stats[index].decrease(1);
|
| 388 |
+
stats_.allocated_bytes_bucket_stats[index].decrease(block->size_);
|
| 389 |
+
delete block;
|
| 390 |
+
}
|
| 391 |
+
}
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
inline size_t size_index(size_t size) {
|
| 395 |
+
return c10::llvm::Log2_64_Ceil(size);
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
virtual bool pinned_use_background_threads() {
|
| 399 |
+
return c10::CachingAllocator::AcceleratorAllocatorConfig::
|
| 400 |
+
pinned_use_background_threads();
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
virtual void copy_data(void* dest [[maybe_unused]], const void* src [[maybe_unused]], std::size_t count [[maybe_unused]]) const {
|
| 404 |
+
TORCH_CHECK_NOT_IMPLEMENTED(false, "Not implemented for copy_data");
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
HostStats getStats() {
|
| 408 |
+
HostStats stats;
|
| 409 |
+
|
| 410 |
+
// To keep getStats lightweight we do *not* flush any available blocks
|
| 411 |
+
// into the free_list. This may skew the stats a bit.
|
| 412 |
+
|
| 413 |
+
auto add_bucket_stats = [](Stat& accumulator, const Stat& other) {
|
| 414 |
+
accumulator.allocated += other.allocated;
|
| 415 |
+
accumulator.current += other.current;
|
| 416 |
+
accumulator.freed += other.freed;
|
| 417 |
+
// Since peaks are measured per bucket independently, we add them up
|
| 418 |
+
// to estimate the total peak. This is not strictly correct, but it is
|
| 419 |
+
// the best approximation we can get after the fact.
|
| 420 |
+
accumulator.peak += other.peak;
|
| 421 |
+
};
|
| 422 |
+
|
| 423 |
+
// Accurate reading of memory stats requires concurrently holding both the
|
| 424 |
+
// free list mutexes and the blocks mutex. Previously, this was only done in
|
| 425 |
+
// empty_cache function.
|
| 426 |
+
for (size_t i = 0; i < free_list_.size(); ++i) {
|
| 427 |
+
std::lock(free_list_[i].mutex_, blocks_mutex_);
|
| 428 |
+
std::lock_guard<std::mutex> gf(free_list_[i].mutex_, std::adopt_lock);
|
| 429 |
+
std::lock_guard<std::mutex> gb(blocks_mutex_, std::adopt_lock);
|
| 430 |
+
|
| 431 |
+
// We collect the slow-path stats only once, since they are not collected
|
| 432 |
+
// per bucket (we pick index 0 arbitrarily). These are also all the host
|
| 433 |
+
// allocations, not taking into account caching and free lists.
|
| 434 |
+
if (i == 0) {
|
| 435 |
+
stats.allocations = stats_.allocations;
|
| 436 |
+
stats.allocated_bytes = stats_.allocated_bytes;
|
| 437 |
+
stats.num_host_alloc = stats.allocations.allocated;
|
| 438 |
+
stats.num_host_free = stats.allocations.freed;
|
| 439 |
+
}
|
| 440 |
+
|
| 441 |
+
// Bucket stats need to be merged with the slow-path stats. We do this in
|
| 442 |
+
// a best effort manner, since we can't really replay the cached events per bucket.
|
| 443 |
+
add_bucket_stats(stats.active_requests, stats_.active_bucket_stats[i]);
|
| 444 |
+
add_bucket_stats(stats.active_bytes, stats_.active_bytes_bucket_stats[i]);
|
| 445 |
+
stats.bucket_allocation[i] = stats_.allocation_bucket_stats[i].allocated;
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
// Get the timing stats
|
| 449 |
+
{
|
| 450 |
+
std::lock_guard<std::mutex> g(stats_.timing_mutex_);
|
| 451 |
+
|
| 452 |
+
stats.host_alloc_time = stats_.host_alloc_time;
|
| 453 |
+
stats.host_free_time = stats_.host_free_time;
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
return stats;
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
void resetAccumulatedStats() {
|
| 460 |
+
// Resetting accumulated memory stats requires concurrently holding both the
|
| 461 |
+
// free list mutexes and the blocks mutex. Previously, this was only done in
|
| 462 |
+
// empty_cache function.
|
| 463 |
+
for (size_t i = 0; i < free_list_.size(); ++i) {
|
| 464 |
+
std::lock(free_list_[i].mutex_, blocks_mutex_);
|
| 465 |
+
std::lock_guard<std::mutex> gf(free_list_[i].mutex_, std::adopt_lock);
|
| 466 |
+
std::lock_guard<std::mutex> gb(blocks_mutex_, std::adopt_lock);
|
| 467 |
+
|
| 468 |
+
if (i == 0) {
|
| 469 |
+
stats_.allocations.reset_accumulated();
|
| 470 |
+
stats_.allocated_bytes.reset_accumulated();
|
| 471 |
+
}
|
| 472 |
+
stats_.active_bucket_stats[i].reset_accumulated();
|
| 473 |
+
stats_.active_bytes_bucket_stats[i].reset_accumulated();
|
| 474 |
+
stats_.allocation_bucket_stats[i].reset_accumulated();
|
| 475 |
+
stats_.allocated_bytes_bucket_stats[i].reset_accumulated();
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
// Also reset timing stats
|
| 479 |
+
{
|
| 480 |
+
std::lock_guard<std::mutex> g(stats_.timing_mutex_);
|
| 481 |
+
stats_.host_alloc_time.reset_accumulated();
|
| 482 |
+
stats_.host_free_time.reset_accumulated();
|
| 483 |
+
}
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
void resetPeakStats() {
|
| 487 |
+
// Resetting peak memory stats requires concurrently holding both the
|
| 488 |
+
// free list mutexes and the blocks mutex. Previously, this was only done in
|
| 489 |
+
// empty_cache function.
|
| 490 |
+
for (size_t i = 0; i < free_list_.size(); ++i) {
|
| 491 |
+
std::lock(free_list_[i].mutex_, blocks_mutex_);
|
| 492 |
+
std::lock_guard<std::mutex> gf(free_list_[i].mutex_, std::adopt_lock);
|
| 493 |
+
std::lock_guard<std::mutex> gb(blocks_mutex_, std::adopt_lock);
|
| 494 |
+
|
| 495 |
+
if (i == 0) {
|
| 496 |
+
stats_.allocations.reset_peak();
|
| 497 |
+
stats_.allocated_bytes.reset_peak();
|
| 498 |
+
}
|
| 499 |
+
stats_.active_bucket_stats[i].reset_peak();
|
| 500 |
+
stats_.active_bytes_bucket_stats[i].reset_peak();
|
| 501 |
+
stats_.allocation_bucket_stats[i].reset_peak();
|
| 502 |
+
stats_.allocated_bytes_bucket_stats[i].reset_peak();
|
| 503 |
+
}
|
| 504 |
+
|
| 505 |
+
// Also reset timing stats
|
| 506 |
+
{
|
| 507 |
+
std::lock_guard<std::mutex> g(stats_.timing_mutex_);
|
| 508 |
+
stats_.host_alloc_time.reset_peak();
|
| 509 |
+
stats_.host_free_time.reset_peak();
|
| 510 |
+
}
|
| 511 |
+
}
|
| 512 |
+
|
| 513 |
+
private:
|
| 514 |
+
virtual void add_allocated_block(B* block) {
|
| 515 |
+
std::lock_guard<std::mutex> g(blocks_mutex_);
|
| 516 |
+
blocks_.insert(block);
|
| 517 |
+
stats_.allocations.increase(1);
|
| 518 |
+
stats_.allocated_bytes.increase(block->size_);
|
| 519 |
+
ptr_to_block_.insert({block->ptr_, block});
|
| 520 |
+
|
| 521 |
+
// Unfortunately, we have to, on the slow path, quickly
|
| 522 |
+
// lock the bucket to record the allocation. This should
|
| 523 |
+
// be a rare event once the cache is warmed up.
|
| 524 |
+
auto size = block->size_;
|
| 525 |
+
auto index = size_index(size);
|
| 526 |
+
{
|
| 527 |
+
std::lock_guard<std::mutex> g(free_list_[index].mutex_);
|
| 528 |
+
stats_.allocation_bucket_stats[index].increase(1);
|
| 529 |
+
stats_.allocated_bytes_bucket_stats[index].increase(size);
|
| 530 |
+
stats_.active_bucket_stats[index].increase(1);
|
| 531 |
+
stats_.active_bytes_bucket_stats[index].increase(size);
|
| 532 |
+
}
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
+
virtual B* get_free_block(size_t size) {
|
| 536 |
+
auto index = size_index(size);
|
| 537 |
+
std::lock_guard<std::mutex> g(free_list_[index].mutex_);
|
| 538 |
+
if (!free_list_[index].list_.empty()) {
|
| 539 |
+
B* block = free_list_[index].list_.back();
|
| 540 |
+
free_list_[index].list_.pop_back();
|
| 541 |
+
block->allocated_ = true;
|
| 542 |
+
stats_.active_bucket_stats[index].increase(1);
|
| 543 |
+
stats_.active_bytes_bucket_stats[index].increase(size);
|
| 544 |
+
return block;
|
| 545 |
+
}
|
| 546 |
+
return nullptr;
|
| 547 |
+
}
|
| 548 |
+
|
| 549 |
+
virtual void process_events() {
|
| 550 |
+
// process all events until the last unready event, not for specific size.
|
| 551 |
+
process_events_for_specific_size(-1);
|
| 552 |
+
}
|
| 553 |
+
|
| 554 |
+
// If size is -1, process all events from backwards until the last unready
|
| 555 |
+
// event. Otherwise, process events for a specific size and on first ready block
|
| 556 |
+
// is found, add it to the free list and return.
|
| 557 |
+
virtual void process_events_for_specific_size(int64_t size) {
|
| 558 |
+
size_t event_count = 0;
|
| 559 |
+
size_t max_events = 0;
|
| 560 |
+
{
|
| 561 |
+
std::lock_guard<std::mutex> g(events_mutex_);
|
| 562 |
+
max_events = events_.size();
|
| 563 |
+
}
|
| 564 |
+
|
| 565 |
+
while (true) {
|
| 566 |
+
// Avoid calling cudaEventDestroy while holding a mutex, so move
|
| 567 |
+
// intermediate events out of the lock into this object.
|
| 568 |
+
// process the last event
|
| 569 |
+
std::optional<std::pair<E, B*>> processed;
|
| 570 |
+
{
|
| 571 |
+
std::lock_guard<std::mutex> g(events_mutex_);
|
| 572 |
+
if (!events_.empty()) {
|
| 573 |
+
processed = std::move(events_.back());
|
| 574 |
+
events_.pop_back();
|
| 575 |
+
}
|
| 576 |
+
}
|
| 577 |
+
|
| 578 |
+
if (!processed) {
|
| 579 |
+
return;
|
| 580 |
+
}
|
| 581 |
+
|
| 582 |
+
if (size != -1) {
|
| 583 |
+
if (event_count++ > max_events) {
|
| 584 |
+
{
|
| 585 |
+
std::lock_guard<std::mutex> g(events_mutex_);
|
| 586 |
+
events_.push_front(std::move(*processed));
|
| 587 |
+
}
|
| 588 |
+
return;
|
| 589 |
+
}
|
| 590 |
+
if (size != (int64_t)processed->second->size_) {
|
| 591 |
+
// if we are processing a specific size, and the size of the block
|
| 592 |
+
// doesn't match, we can't use it.
|
| 593 |
+
{
|
| 594 |
+
std::lock_guard<std::mutex> g(events_mutex_);
|
| 595 |
+
events_.push_front(std::move(*processed));
|
| 596 |
+
}
|
| 597 |
+
continue;
|
| 598 |
+
}
|
| 599 |
+
}
|
| 600 |
+
|
| 601 |
+
// otherwise, query the event
|
| 602 |
+
{
|
| 603 |
+
// now, see if we can handle this element
|
| 604 |
+
auto& event = processed->first;
|
| 605 |
+
if (!query_event(event)) {
|
| 606 |
+
// push the event onto the back if it's not ready.
|
| 607 |
+
{
|
| 608 |
+
std::lock_guard<std::mutex> g(events_mutex_);
|
| 609 |
+
if (size == -1) {
|
| 610 |
+
events_.push_back(std::move(*processed));
|
| 611 |
+
return;
|
| 612 |
+
} else {
|
| 613 |
+
events_.push_front(std::move(*processed));
|
| 614 |
+
continue;
|
| 615 |
+
}
|
| 616 |
+
}
|
| 617 |
+
}
|
| 618 |
+
}
|
| 619 |
+
|
| 620 |
+
// Process the events.
|
| 621 |
+
TORCH_INTERNAL_ASSERT(processed);
|
| 622 |
+
auto* block = processed->second;
|
| 623 |
+
bool available = false;
|
| 624 |
+
{
|
| 625 |
+
std::lock_guard<std::mutex> g(block->mutex_);
|
| 626 |
+
TORCH_INTERNAL_ASSERT(!block->allocated_)
|
| 627 |
+
block->event_count_--;
|
| 628 |
+
if (block->event_count_ == 0) {
|
| 629 |
+
available = true;
|
| 630 |
+
}
|
| 631 |
+
}
|
| 632 |
+
|
| 633 |
+
if (available) {
|
| 634 |
+
auto index = size_index(block->size_);
|
| 635 |
+
std::lock_guard<std::mutex> g(free_list_[index].mutex_);
|
| 636 |
+
free_list_[index].list_.push_back(block);
|
| 637 |
+
stats_.active_bucket_stats[index].decrease(1);
|
| 638 |
+
stats_.active_bytes_bucket_stats[index].decrease(size);
|
| 639 |
+
if (size != -1) {
|
| 640 |
+
return;
|
| 641 |
+
}
|
| 642 |
+
}
|
| 643 |
+
}
|
| 644 |
+
}
|
| 645 |
+
|
| 646 |
+
TaskThreadPool* getBackgroundThreadPool() {
|
| 647 |
+
static TaskThreadPool* pool = new TaskThreadPool(1);
|
| 648 |
+
return pool;
|
| 649 |
+
}
|
| 650 |
+
|
| 651 |
+
/* These following functions are runtime-related. */
|
| 652 |
+
|
| 653 |
+
// Allocate page-locked memory on the host.
|
| 654 |
+
virtual void allocate_host_memory(size_t size, void** ptr) {
|
| 655 |
+
TORCH_CHECK_NOT_IMPLEMENTED(
|
| 656 |
+
false, "Not implemented for allocate_host_memory");
|
| 657 |
+
}
|
| 658 |
+
|
| 659 |
+
// Free block and release the pointer contained in block.
|
| 660 |
+
virtual void free_block(B* block) {
|
| 661 |
+
TORCH_CHECK_NOT_IMPLEMENTED(false, "Not implemented for free_block");
|
| 662 |
+
}
|
| 663 |
+
|
| 664 |
+
// Record an event on stream and store event into events.
|
| 665 |
+
virtual void record_stream(std::optional<std::vector<E>>& events, S stream) {
|
| 666 |
+
TORCH_CHECK_NOT_IMPLEMENTED(false, "Not implemented for record_stream");
|
| 667 |
+
}
|
| 668 |
+
|
| 669 |
+
// Query event if it is completed.
|
| 670 |
+
virtual bool query_event(E& event) {
|
| 671 |
+
TORCH_CHECK_NOT_IMPLEMENTED(false, "Not implemented for query_event");
|
| 672 |
+
}
|
| 673 |
+
|
| 674 |
+
alignas(hardware_destructive_interference_size) std::mutex blocks_mutex_;
|
| 675 |
+
ska::flat_hash_set<B*> blocks_; // block list
|
| 676 |
+
ska::flat_hash_map<void*, B*> ptr_to_block_;
|
| 677 |
+
|
| 678 |
+
// We keep free list as a vector of free lists, one for each power of two
|
| 679 |
+
// size. This allows us to quickly find a free block of the right size.
|
| 680 |
+
// We use deque to store per size free list and guard the list with its own
|
| 681 |
+
// mutex.
|
| 682 |
+
alignas(hardware_destructive_interference_size) std::vector<FreeBlockList<B>>
|
| 683 |
+
free_list_{MAX_SIZE_INDEX};
|
| 684 |
+
|
| 685 |
+
alignas(hardware_destructive_interference_size) std::mutex events_mutex_;
|
| 686 |
+
std::deque<std::pair<E, B*>> events_; // event queue paired with block
|
| 687 |
+
|
| 688 |
+
// Indicates whether the event-processing thread pool is active.
|
| 689 |
+
// Set to false in the destructor to signal background threads to stop.
|
| 690 |
+
std::atomic<bool> active_{false};
|
| 691 |
+
protected:
|
| 692 |
+
alignas(hardware_destructive_interference_size) HostStatsStaged stats_;
|
| 693 |
+
};
|
| 694 |
+
|
| 695 |
+
struct TORCH_API HostAllocator : public at::Allocator {
|
| 696 |
+
// Associates the pinned memory allocation with a stream to track
|
| 697 |
+
// dependencies. This ensures the memory won't be reused until the stream's
|
| 698 |
+
// operations complete
|
| 699 |
+
virtual bool record_event(void* ptr, void* ctx, c10::Stream stream) = 0;
|
| 700 |
+
|
| 701 |
+
// Frees all cached pinned memory and returns it to the system, clearing the
|
| 702 |
+
// allocator's internal cache
|
| 703 |
+
virtual void empty_cache() = 0;
|
| 704 |
+
|
| 705 |
+
// Returns comprehensive statistics about the allocator's memory usage,
|
| 706 |
+
// allocation patterns, and timing metrics
|
| 707 |
+
virtual HostStats get_stats() = 0;
|
| 708 |
+
|
| 709 |
+
// Resets the cumulative allocation statistics
|
| 710 |
+
virtual void reset_accumulated_stats() = 0;
|
| 711 |
+
|
| 712 |
+
// Resets the peak memory usage metrics
|
| 713 |
+
virtual void reset_peak_stats() = 0;
|
| 714 |
+
};
|
| 715 |
+
|
| 716 |
+
template <typename T, c10::DeleterFnPtr deleteFunc>
|
| 717 |
+
struct CachingHostAllocatorInterface : public HostAllocator {
|
| 718 |
+
CachingHostAllocatorInterface() : impl_(std::make_unique<T>()) {}
|
| 719 |
+
|
| 720 |
+
at::DataPtr allocate(size_t size) override {
|
| 721 |
+
auto ptr_and_ctx = impl_->allocate(size);
|
| 722 |
+
return {
|
| 723 |
+
ptr_and_ctx.first,
|
| 724 |
+
ptr_and_ctx.second,
|
| 725 |
+
deleteFunc, // Use the template parameter deleter function
|
| 726 |
+
at::DeviceType::CPU};
|
| 727 |
+
}
|
| 728 |
+
|
| 729 |
+
void free(void* ctx) {
|
| 730 |
+
impl_->free(ctx);
|
| 731 |
+
}
|
| 732 |
+
|
| 733 |
+
bool record_event(void* ptr, void* ctx, c10::Stream stream) override {
|
| 734 |
+
return impl_->record_event(ptr, ctx, stream);
|
| 735 |
+
}
|
| 736 |
+
|
| 737 |
+
void empty_cache() override {
|
| 738 |
+
impl_->empty_cache();
|
| 739 |
+
}
|
| 740 |
+
|
| 741 |
+
void copy_data(void* dest, const void* src, std::size_t count)
|
| 742 |
+
const override {
|
| 743 |
+
impl_->copy_data(dest, src, count);
|
| 744 |
+
}
|
| 745 |
+
|
| 746 |
+
HostStats get_stats() override {
|
| 747 |
+
return impl_->getStats();
|
| 748 |
+
}
|
| 749 |
+
|
| 750 |
+
void reset_accumulated_stats() override {
|
| 751 |
+
impl_->resetAccumulatedStats();
|
| 752 |
+
}
|
| 753 |
+
|
| 754 |
+
void reset_peak_stats() override {
|
| 755 |
+
impl_->resetPeakStats();
|
| 756 |
+
}
|
| 757 |
+
|
| 758 |
+
std::unique_ptr<T> impl_;
|
| 759 |
+
};
|
| 760 |
+
|
| 761 |
+
#define DECLARE_HOST_ALLOCATOR(name, impl, deleter, instance) \
|
| 762 |
+
void deleter(void* ptr); \
|
| 763 |
+
struct name final \
|
| 764 |
+
: public at::CachingHostAllocatorInterface<impl, deleter> {}; \
|
| 765 |
+
static name instance; \
|
| 766 |
+
void deleter(void* ptr) { \
|
| 767 |
+
instance.free(ptr); \
|
| 768 |
+
}
|
| 769 |
+
|
| 770 |
+
/**
|
| 771 |
+
* Set the host allocator for DeviceType `device_type`. This allocator manages
|
| 772 |
+
* pinned memory on the host that can be accessed efficiently by the specified
|
| 773 |
+
* device type. Note that this function is not thread-safe.
|
| 774 |
+
*/
|
| 775 |
+
TORCH_API void setHostAllocator(
|
| 776 |
+
at::DeviceType device_type,
|
| 777 |
+
at::HostAllocator* allocator,
|
| 778 |
+
uint8_t priority = 0);
|
| 779 |
+
|
| 780 |
+
TORCH_API at::HostAllocator* getHostAllocator(at::DeviceType device_type);
|
| 781 |
+
|
| 782 |
+
template <DeviceType device_type>
|
| 783 |
+
struct HostAllocatorRegistry {
|
| 784 |
+
explicit HostAllocatorRegistry(HostAllocator* allocator) {
|
| 785 |
+
at::setHostAllocator(device_type, allocator);
|
| 786 |
+
}
|
| 787 |
+
};
|
| 788 |
+
|
| 789 |
+
#define REGISTER_HOST_ALLOCATOR(device_type, allocator) \
|
| 790 |
+
namespace { \
|
| 791 |
+
static at::HostAllocatorRegistry<device_type> \
|
| 792 |
+
g_host_allocator_registry_instance(allocator); \
|
| 793 |
+
}
|
| 794 |
+
|
| 795 |
+
} // namespace at
|
| 796 |
+
C10_DIAGNOSTIC_POP()
|
| 797 |
+
|
| 798 |
+
#else
|
| 799 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 800 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/CheckMemoryFormat.h
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#include <c10/core/TensorOptions.h>
|
| 3 |
+
|
| 4 |
+
namespace c10::impl {
|
| 5 |
+
|
| 6 |
+
inline std::optional<MemoryFormat>
|
| 7 |
+
check_tensor_options_and_extract_memory_format(
|
| 8 |
+
const TensorOptions& options,
|
| 9 |
+
std::optional<MemoryFormat> memory_format) {
|
| 10 |
+
TORCH_CHECK(
|
| 11 |
+
options.requires_grad_opt() != true,
|
| 12 |
+
"Operators taking TensorOptions cannot take a TensorOptions with "
|
| 13 |
+
"options.requires_grad set as true. This isn't implemented yet.");
|
| 14 |
+
TORCH_CHECK(
|
| 15 |
+
!(options.has_memory_format() && memory_format.has_value()),
|
| 16 |
+
"Cannot set memory_format both in TensorOptions and explicit argument; please delete "
|
| 17 |
+
"the redundant setter.");
|
| 18 |
+
if (memory_format.has_value()) {
|
| 19 |
+
return memory_format;
|
| 20 |
+
} else {
|
| 21 |
+
return options.memory_format_opt();
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
} // namespace impl namespace c10
|
| 26 |
+
|
| 27 |
+
#else
|
| 28 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 29 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/DeprecatedTypeProperties.h
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/core/Backend.h>
|
| 5 |
+
#include <c10/core/ScalarType.h>
|
| 6 |
+
#include <c10/core/Layout.h>
|
| 7 |
+
#include <c10/core/TensorOptions.h>
|
| 8 |
+
#include <c10/core/Storage.h>
|
| 9 |
+
#include <ATen/core/DeprecatedTypePropertiesRegistry.h>
|
| 10 |
+
#include <ATen/core/Generator.h>
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
namespace at {
|
| 14 |
+
|
| 15 |
+
class Tensor;
|
| 16 |
+
|
| 17 |
+
// This class specifies a Backend and a ScalarType. Currently, it primarily
|
| 18 |
+
// serves as a replacement return value for Tensor::type(). Previously,
|
| 19 |
+
// Tensor::type() returned Type&, but we are changing Type to not be
|
| 20 |
+
// dtype-specific.
|
| 21 |
+
class TORCH_API DeprecatedTypeProperties {
|
| 22 |
+
public:
|
| 23 |
+
DeprecatedTypeProperties(Backend backend, ScalarType scalar_type)
|
| 24 |
+
: backend_(backend), scalar_type_(scalar_type) {}
|
| 25 |
+
|
| 26 |
+
Backend backend() const {
|
| 27 |
+
return backend_;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
Layout layout() const {
|
| 31 |
+
return layout_from_backend(backend_);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
bool is_sparse() const {
|
| 35 |
+
return layout_from_backend(backend()) == kSparse;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
bool is_sparse_csr() const {
|
| 39 |
+
return layout_from_backend(backend()) == kSparseCsr;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
c10::DeviceType device_type() const {
|
| 43 |
+
return backendToDeviceType(backend_);
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
bool is_cuda() const {
|
| 47 |
+
return backendToDeviceType(backend_) == kCUDA;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
ScalarType scalarType() const {
|
| 51 |
+
return scalar_type_;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
caffe2::TypeMeta typeMeta() const {
|
| 55 |
+
return scalarTypeToTypeMeta(scalar_type_);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
bool operator==(const DeprecatedTypeProperties& other) const {
|
| 59 |
+
return backend_ == other.backend() && scalar_type_ == other.scalarType();
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
bool operator!=(const DeprecatedTypeProperties& other) const {
|
| 63 |
+
return !(*this == other);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
std::string toString() const {
|
| 67 |
+
std::string base_str;
|
| 68 |
+
if (backend_ == Backend::Undefined || scalar_type_ == ScalarType::Undefined) {
|
| 69 |
+
base_str = "UndefinedType";
|
| 70 |
+
} else {
|
| 71 |
+
base_str = std::string(at::toString(backend_)) + at::toString(scalar_type_) + "Type";
|
| 72 |
+
}
|
| 73 |
+
return base_str;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
DeprecatedTypeProperties & toBackend(Backend b) const {
|
| 77 |
+
return globalDeprecatedTypePropertiesRegistry().getDeprecatedTypeProperties(
|
| 78 |
+
b, scalar_type_);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
DeprecatedTypeProperties & toScalarType(ScalarType s) const {
|
| 82 |
+
return globalDeprecatedTypePropertiesRegistry().getDeprecatedTypeProperties(
|
| 83 |
+
backend_, s);
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
DeprecatedTypeProperties & cpu() const {
|
| 87 |
+
return toBackend(Backend::CPU);
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
DeprecatedTypeProperties & cuda() const {
|
| 91 |
+
return toBackend(Backend::CUDA);
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
DeprecatedTypeProperties & hip() const {
|
| 95 |
+
return toBackend(Backend::HIP);
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
DeprecatedTypeProperties & privateUser1() const {
|
| 99 |
+
return toBackend(Backend::PrivateUse1);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
/// Constructs the `TensorOptions` from a type and a `device_index`.
|
| 103 |
+
TensorOptions options(int16_t device_index = -1) const {
|
| 104 |
+
return TensorOptions().dtype(typeMeta())
|
| 105 |
+
.device(device_type(), static_cast<c10::DeviceIndex>(device_index))
|
| 106 |
+
.layout(layout());
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
/// Constructs the `TensorOptions` from a type and a Device. Asserts that
|
| 110 |
+
/// the device type matches the device type of the type.
|
| 111 |
+
TensorOptions options(std::optional<Device> device_opt) const {
|
| 112 |
+
if (!device_opt.has_value()) {
|
| 113 |
+
return options(-1);
|
| 114 |
+
} else {
|
| 115 |
+
Device device = device_opt.value();
|
| 116 |
+
AT_ASSERT(device.type() == device_type());
|
| 117 |
+
return options(device.index());
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
operator TensorOptions() const {
|
| 122 |
+
return options();
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
int64_t id() const {
|
| 126 |
+
return static_cast<int64_t>(backend()) *
|
| 127 |
+
static_cast<int64_t>(ScalarType::NumOptions) +
|
| 128 |
+
static_cast<int64_t>(scalarType());
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
Tensor unsafeTensorFromTH(void * th_pointer, bool retain) const;
|
| 132 |
+
Storage unsafeStorageFromTH(void * th_pointer, bool retain) const;
|
| 133 |
+
Tensor copy(const Tensor & src, bool non_blocking=false, std::optional<Device> to_device={}) const;
|
| 134 |
+
|
| 135 |
+
private:
|
| 136 |
+
Backend backend_;
|
| 137 |
+
ScalarType scalar_type_;
|
| 138 |
+
};
|
| 139 |
+
|
| 140 |
+
} // namespace at
|
| 141 |
+
|
| 142 |
+
#else
|
| 143 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 144 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/DeprecatedTypePropertiesRegistry.h
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
// In order to preserve bc, we make DeprecatedTypeProperties instances unique
|
| 5 |
+
// just like they are for Type.
|
| 6 |
+
|
| 7 |
+
#include <c10/core/Backend.h>
|
| 8 |
+
#include <c10/core/ScalarType.h>
|
| 9 |
+
#include <memory>
|
| 10 |
+
|
| 11 |
+
namespace at {
|
| 12 |
+
|
| 13 |
+
class DeprecatedTypeProperties;
|
| 14 |
+
|
| 15 |
+
struct TORCH_API DeprecatedTypePropertiesDeleter {
|
| 16 |
+
void operator()(DeprecatedTypeProperties * ptr);
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
class TORCH_API DeprecatedTypePropertiesRegistry {
|
| 20 |
+
public:
|
| 21 |
+
DeprecatedTypePropertiesRegistry();
|
| 22 |
+
|
| 23 |
+
DeprecatedTypeProperties& getDeprecatedTypeProperties(Backend p, ScalarType s) const;
|
| 24 |
+
|
| 25 |
+
private:
|
| 26 |
+
// NOLINTNEXTLINE(*c-array*)
|
| 27 |
+
std::unique_ptr<DeprecatedTypeProperties> registry
|
| 28 |
+
[static_cast<int>(Backend::NumOptions)]
|
| 29 |
+
[static_cast<int>(ScalarType::NumOptions)];
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
TORCH_API DeprecatedTypePropertiesRegistry& globalDeprecatedTypePropertiesRegistry();
|
| 33 |
+
|
| 34 |
+
} // namespace at
|
| 35 |
+
|
| 36 |
+
#else
|
| 37 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 38 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Dict.h
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <c10/macros/Macros.h>
|
| 5 |
+
#include <c10/macros/Export.h>
|
| 6 |
+
#include <c10/util/TypeTraits.h>
|
| 7 |
+
#include <c10/util/TypeList.h>
|
| 8 |
+
#include <c10/util/intrusive_ptr.h>
|
| 9 |
+
#include <c10/util/order_preserving_flat_hash_map.h>
|
| 10 |
+
#include <optional>
|
| 11 |
+
#include <ATen/core/TensorBody.h>
|
| 12 |
+
#include <ATen/core/jit_type_base.h>
|
| 13 |
+
|
| 14 |
+
namespace c10 {
|
| 15 |
+
struct IValue;
|
| 16 |
+
template<class Key, class Value> class Dict;
|
| 17 |
+
struct Type;
|
| 18 |
+
|
| 19 |
+
namespace impl {
|
| 20 |
+
|
| 21 |
+
using valid_dict_key_types = guts::typelist::typelist<
|
| 22 |
+
int64_t,
|
| 23 |
+
std::string,
|
| 24 |
+
double,
|
| 25 |
+
c10::complex<double>,
|
| 26 |
+
bool,
|
| 27 |
+
at::Tensor
|
| 28 |
+
>;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
namespace detail {
|
| 32 |
+
|
| 33 |
+
struct DictKeyHash {
|
| 34 |
+
size_t operator()(const IValue& ivalue) const;
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
struct DictKeyEqualTo {
|
| 38 |
+
bool operator()(const IValue& lhs, const IValue& rhs) const;
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
struct DictImpl final : public c10::intrusive_ptr_target {
|
| 42 |
+
using dict_map_type = ska_ordered::order_preserving_flat_hash_map<IValue, IValue, DictKeyHash, DictKeyEqualTo>;
|
| 43 |
+
struct DictElementTypes final {
|
| 44 |
+
TypePtr keyType;
|
| 45 |
+
TypePtr valueType;
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
explicit DictImpl(dict_map_type dict_, DictElementTypes elementTypes_)
|
| 49 |
+
: dict(std::move(dict_))
|
| 50 |
+
, elementTypes(std::move(elementTypes_)) {}
|
| 51 |
+
dict_map_type dict;
|
| 52 |
+
|
| 53 |
+
DictElementTypes elementTypes;
|
| 54 |
+
|
| 55 |
+
intrusive_ptr<DictImpl> copy() const;
|
| 56 |
+
friend TORCH_API bool operator==(const DictImpl& lhs, const DictImpl& rhs);
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
namespace impl {
|
| 62 |
+
template<class Key, class Value, class Iterator> class DictIterator;
|
| 63 |
+
|
| 64 |
+
/**
|
| 65 |
+
* A reference to an entry in the Dict.
|
| 66 |
+
* Use the `key()` and `value()` methods to read the element.
|
| 67 |
+
*/
|
| 68 |
+
template<class Key, class Value, class Iterator>
|
| 69 |
+
class DictEntryRef final {
|
| 70 |
+
public:
|
| 71 |
+
explicit DictEntryRef(Iterator iterator)
|
| 72 |
+
: iterator_(std::move(iterator)) {}
|
| 73 |
+
|
| 74 |
+
decltype(auto) key() const {
|
| 75 |
+
return iterator_->first.template to<Key>();
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
decltype(auto) value() const {
|
| 79 |
+
return iterator_->second.template to<Value>();
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
template<class Value_>
|
| 83 |
+
void setValue(Value_&& value) const {
|
| 84 |
+
static_assert(std::is_constructible_v<Value, Value_>, "Wrong type for the value argument of setValue()");
|
| 85 |
+
iterator_->second = Value(std::forward<Value_>(value));
|
| 86 |
+
}
|
| 87 |
+
~DictEntryRef() = default;
|
| 88 |
+
|
| 89 |
+
private:
|
| 90 |
+
// allow copying and moving, but only our friends (i.e. the Dict class) can do
|
| 91 |
+
// it. Copying/moving this reference wrapper would be too ambiguous to allow it
|
| 92 |
+
// in the public API.
|
| 93 |
+
DictEntryRef(const DictEntryRef&) = default;
|
| 94 |
+
DictEntryRef& operator=(const DictEntryRef&) = default;
|
| 95 |
+
DictEntryRef(DictEntryRef&&) noexcept = default;
|
| 96 |
+
DictEntryRef& operator=(DictEntryRef&& rhs) & noexcept = default;
|
| 97 |
+
|
| 98 |
+
Iterator iterator_;
|
| 99 |
+
friend class DictIterator<Key, Value, Iterator>;
|
| 100 |
+
friend class Dict<Key, Value>;
|
| 101 |
+
};
|
| 102 |
+
|
| 103 |
+
// this wraps map_type::iterator to make sure user code can't rely
|
| 104 |
+
// on it being the type of the underlying map.
|
| 105 |
+
template<class Key, class Value, class Iterator>
|
| 106 |
+
class DictIterator final {
|
| 107 |
+
public:
|
| 108 |
+
// C++17 friendly std::iterator implementation
|
| 109 |
+
using iterator_category = std::forward_iterator_tag;
|
| 110 |
+
using value_type = DictEntryRef<Key, Value, Iterator>;
|
| 111 |
+
using difference_type = std::ptrdiff_t;
|
| 112 |
+
using pointer = value_type*;
|
| 113 |
+
using reference = value_type&;
|
| 114 |
+
|
| 115 |
+
explicit DictIterator() = default;
|
| 116 |
+
~DictIterator() = default;
|
| 117 |
+
|
| 118 |
+
DictIterator(const DictIterator& rhs): entryRef_(rhs.entryRef_) {}
|
| 119 |
+
DictIterator(DictIterator&& rhs) noexcept: entryRef_(std::move(rhs.entryRef_)) {}
|
| 120 |
+
DictIterator& operator=(const DictIterator& rhs) = default;
|
| 121 |
+
DictIterator& operator=(DictIterator&& rhs) noexcept {
|
| 122 |
+
entryRef_ = std::move(rhs.entryRef_);
|
| 123 |
+
return *this;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
DictIterator& operator++() {
|
| 127 |
+
++entryRef_.iterator_;
|
| 128 |
+
return *this;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
DictIterator operator++(int) {
|
| 132 |
+
DictIterator copy(*this);
|
| 133 |
+
++*this;
|
| 134 |
+
return copy;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
const DictEntryRef<Key, Value, Iterator>& operator*() const {
|
| 138 |
+
return entryRef_;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
const DictEntryRef<Key, Value, Iterator>* operator->() const {
|
| 142 |
+
return &entryRef_;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
friend difference_type operator-(const DictIterator& lhs, const DictIterator& rhs) {
|
| 146 |
+
return lhs.entryRef_.iterator_ - rhs.entryRef_.iterator_;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
private:
|
| 150 |
+
explicit DictIterator(Iterator iterator): entryRef_(std::move(iterator)) {}
|
| 151 |
+
|
| 152 |
+
const Iterator& get_iterator_() const {
|
| 153 |
+
return entryRef_.iterator_;
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
friend bool operator==(const DictIterator& lhs, const DictIterator& rhs) {
|
| 157 |
+
return lhs.get_iterator_() == rhs.get_iterator_();
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
friend bool operator!=(const DictIterator& lhs, const DictIterator& rhs) {
|
| 161 |
+
return lhs.get_iterator_() != rhs.get_iterator_();
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
friend bool operator<(const DictIterator& lhs, const DictIterator& rhs) {
|
| 165 |
+
return lhs.get_iterator_() < rhs.get_iterator_();
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
friend bool operator<=(const DictIterator& lhs, const DictIterator& rhs) {
|
| 169 |
+
return lhs.get_iterator_() <= rhs.get_iterator_();
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
friend bool operator>(const DictIterator& lhs, const DictIterator& rhs) {
|
| 173 |
+
return lhs.get_iterator_() > rhs.get_iterator_();
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
friend bool operator>=(const DictIterator& lhs, const DictIterator& rhs) {
|
| 177 |
+
return lhs.get_iterator_() >= rhs.get_iterator_();
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
DictEntryRef<Key, Value, Iterator> entryRef_;
|
| 181 |
+
|
| 182 |
+
friend class DictIterator<Key, Value, typename c10::detail::DictImpl::dict_map_type::iterator>;
|
| 183 |
+
friend class Dict<Key, Value>;
|
| 184 |
+
};
|
| 185 |
+
|
| 186 |
+
template<class Key, class Value> Dict<Key, Value> toTypedDict(Dict<IValue, IValue> dict);
|
| 187 |
+
template<class Key, class Value> Dict<IValue, IValue> toGenericDict(Dict<Key, Value> dict);
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
/**
|
| 191 |
+
* An object of this class stores a map from Key to Value.
|
| 192 |
+
*
|
| 193 |
+
* This is a pointer type. After a copy, both Dicts
|
| 194 |
+
* will share the same storage:
|
| 195 |
+
*
|
| 196 |
+
* > Dict<int, string> a;
|
| 197 |
+
* > Dict<int, string> b = a;
|
| 198 |
+
* > b.insert(3, "three");
|
| 199 |
+
* > ASSERT("three" == a.at(3));
|
| 200 |
+
*
|
| 201 |
+
* We use this class in the PyTorch kernel API because that
|
| 202 |
+
* allows us to do optimizations and switch out the underlying
|
| 203 |
+
* map implementation without breaking backwards compatibility
|
| 204 |
+
* for the kernel API.
|
| 205 |
+
*/
|
| 206 |
+
template<class Key, class Value>
|
| 207 |
+
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
|
| 208 |
+
class Dict final {
|
| 209 |
+
private:
|
| 210 |
+
static_assert((std::is_same_v<IValue, Key> && std::is_same_v<IValue, Value>) || guts::typelist::contains<impl::valid_dict_key_types, Key>::value, "Invalid Key type for Dict. We only support int64_t, double, bool, and string.");
|
| 211 |
+
|
| 212 |
+
// impl_ stores the underlying map as a ska_ordered::order_preserving_flat_hash_map.
|
| 213 |
+
// We intentionally don't offer conversion from/to
|
| 214 |
+
// order_preserving_flat_hash_map, return references to it or something like that,
|
| 215 |
+
// because such operations would get expensive if we switch out
|
| 216 |
+
// the actual map implementation.
|
| 217 |
+
// This is an intrusive_ptr because Dict is a pointer type.
|
| 218 |
+
// Invariant: This will never be a nullptr, there will always be a valid
|
| 219 |
+
// DictImpl.
|
| 220 |
+
c10::intrusive_ptr<detail::DictImpl> impl_;
|
| 221 |
+
|
| 222 |
+
explicit Dict(c10::intrusive_ptr<detail::DictImpl>&& impl);
|
| 223 |
+
friend struct IValue;
|
| 224 |
+
template<class K, class V> friend Dict<K, V> impl::toTypedDict(Dict<IValue, IValue>);
|
| 225 |
+
template<class K, class V> friend Dict<IValue, IValue> impl::toGenericDict(Dict<K, V>);
|
| 226 |
+
|
| 227 |
+
public:
|
| 228 |
+
using key_type = Key;
|
| 229 |
+
using mapped_type = Value;
|
| 230 |
+
using size_type = typename detail::DictImpl::dict_map_type::size_type;
|
| 231 |
+
using iterator = impl::DictIterator<Key, Value, typename detail::DictImpl::dict_map_type::iterator>;
|
| 232 |
+
|
| 233 |
+
/**
|
| 234 |
+
* Creates an empty dict.
|
| 235 |
+
*/
|
| 236 |
+
explicit Dict();
|
| 237 |
+
|
| 238 |
+
/**
|
| 239 |
+
* Create a generic dict with runtime type information.
|
| 240 |
+
* This only works for c10::impl::GenericDict and is not part of the public API
|
| 241 |
+
* but only supposed to be used internally by PyTorch.
|
| 242 |
+
*/
|
| 243 |
+
explicit Dict(TypePtr keyType, TypePtr valueType);
|
| 244 |
+
|
| 245 |
+
~Dict() = default;
|
| 246 |
+
|
| 247 |
+
Dict(const Dict&) = default;
|
| 248 |
+
Dict& operator=(const Dict&) = default;
|
| 249 |
+
|
| 250 |
+
/**
|
| 251 |
+
* Create a new Dict pointing to a deep copy of the same data.
|
| 252 |
+
* The Dict returned is a new dict with separate storage.
|
| 253 |
+
* Changes in it are not reflected in the original dict or vice versa.
|
| 254 |
+
*/
|
| 255 |
+
Dict copy() const;
|
| 256 |
+
|
| 257 |
+
/**
|
| 258 |
+
* Returns an iterator to the first element of the container.
|
| 259 |
+
* If the container is empty, the returned iterator will be equal to end().
|
| 260 |
+
*/
|
| 261 |
+
iterator begin() const;
|
| 262 |
+
|
| 263 |
+
/**
|
| 264 |
+
* Returns an iterator to the element following the last element of the container.
|
| 265 |
+
* This element acts as a placeholder; attempting to access it results in undefined behavior.
|
| 266 |
+
*/
|
| 267 |
+
iterator end() const;
|
| 268 |
+
|
| 269 |
+
/**
|
| 270 |
+
* Checks if the container has no elements.
|
| 271 |
+
*/
|
| 272 |
+
bool empty() const;
|
| 273 |
+
|
| 274 |
+
/**
|
| 275 |
+
* Returns the number of elements in the container.
|
| 276 |
+
*/
|
| 277 |
+
size_type size() const;
|
| 278 |
+
|
| 279 |
+
/**
|
| 280 |
+
* Erases all elements from the container. After this call, size() returns zero.
|
| 281 |
+
* Invalidates any references, pointers, or iterators referring to contained elements. May also invalidate past-the-end iterators.
|
| 282 |
+
*/
|
| 283 |
+
void clear() const;
|
| 284 |
+
|
| 285 |
+
/**
|
| 286 |
+
* Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key.
|
| 287 |
+
* May invalidate any references, pointers, or iterators referring to contained elements.
|
| 288 |
+
*
|
| 289 |
+
* @return A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool denoting whether the insertion took place.
|
| 290 |
+
*/
|
| 291 |
+
template<class Key_, class Value_>
|
| 292 |
+
std::pair<iterator, bool> insert(Key_&& key, Value_&& value) const;
|
| 293 |
+
|
| 294 |
+
/**
|
| 295 |
+
* If an element with the given key already exists, it is overwritten with the given value.
|
| 296 |
+
* Otherwise, a new element with the given key and value are inserted.
|
| 297 |
+
* May invalidate any references, pointers, or iterators referring to contained elements.
|
| 298 |
+
*
|
| 299 |
+
* @return The bool component is true if the insertion took place and false if the assignment took place. The iterator component is pointing at the element that was inserted or updated.
|
| 300 |
+
*/
|
| 301 |
+
template<class Key_, class Value_>
|
| 302 |
+
std::pair<iterator, bool> insert_or_assign(Key_&& key, Value_&& value) const;
|
| 303 |
+
|
| 304 |
+
/**
|
| 305 |
+
* Removes the element pointed to by iter.
|
| 306 |
+
* May invalidate any references, pointers, or iterators referring to contained elements.
|
| 307 |
+
* The iterator iter must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferenceable) cannot be used as a value for iter.
|
| 308 |
+
*/
|
| 309 |
+
void erase(iterator iter) const;
|
| 310 |
+
|
| 311 |
+
/**
|
| 312 |
+
* Removes the element with the given key, if it exists.
|
| 313 |
+
* May invalidate any references, pointers, or iterators referring to contained elements.
|
| 314 |
+
*
|
| 315 |
+
* @return The number of elements removed. This is either '1' if an element with the key existed, or '0' if it didn't.
|
| 316 |
+
*/
|
| 317 |
+
[[nodiscard]] size_t erase(const Key& key) const;
|
| 318 |
+
|
| 319 |
+
/**
|
| 320 |
+
* Returns the mapped value of the element with key equivalent to key.
|
| 321 |
+
* If no such element exists, an exception of type std::out_of_range is thrown.
|
| 322 |
+
*/
|
| 323 |
+
Value at(const Key& key) const;
|
| 324 |
+
|
| 325 |
+
/**
|
| 326 |
+
* Finds an element with key equivalent to key.
|
| 327 |
+
*
|
| 328 |
+
* @return Iterator to an element with key equivalent to key.
|
| 329 |
+
* If no such element is found, past-the-end (see end()) iterator is returned.
|
| 330 |
+
*/
|
| 331 |
+
iterator find(const Key& key) const;
|
| 332 |
+
|
| 333 |
+
/**
|
| 334 |
+
* Checks if there is an element with key equivalent to key in the container.
|
| 335 |
+
*
|
| 336 |
+
* @return true if there is such an element, otherwise false.
|
| 337 |
+
*/
|
| 338 |
+
bool contains(const Key& key) const;
|
| 339 |
+
|
| 340 |
+
/**
|
| 341 |
+
* Increase the capacity so that at least count elements can be stored without
|
| 342 |
+
* having to reallocate or rehash.
|
| 343 |
+
*/
|
| 344 |
+
void reserve(size_type count) const;
|
| 345 |
+
|
| 346 |
+
/**
|
| 347 |
+
* Value equality comparison. This function implements Python-like semantics for
|
| 348 |
+
* equality: two dicts with the same identity (e.g. same pointer) trivially
|
| 349 |
+
* compare equal, otherwise each element is compared for equality.
|
| 350 |
+
*/
|
| 351 |
+
template <class Key_, class Value_>
|
| 352 |
+
friend bool operator==(
|
| 353 |
+
const Dict<Key_, Value_>& lhs,
|
| 354 |
+
const Dict<Key_, Value_>& rhs);
|
| 355 |
+
template <class Key_, class Value_>
|
| 356 |
+
friend bool operator!=(
|
| 357 |
+
const Dict<Key_, Value_>& lhs,
|
| 358 |
+
const Dict<Key_, Value_>& rhs);
|
| 359 |
+
|
| 360 |
+
/**
|
| 361 |
+
* Identity comparison. Returns true if and only if `rhs` represents the same
|
| 362 |
+
* Dict object as `this`.
|
| 363 |
+
*/
|
| 364 |
+
bool is(const Dict& rhs) const;
|
| 365 |
+
|
| 366 |
+
// private API for now because the return type will change to TypePtr
|
| 367 |
+
// instead of std::optional<TypePtr> once types are mandatory.
|
| 368 |
+
TypePtr keyType() const;
|
| 369 |
+
TypePtr valueType() const;
|
| 370 |
+
|
| 371 |
+
// [unsafe set type]
|
| 372 |
+
// These functions mutate the tagged type of this dictionary in place.
|
| 373 |
+
// There is no checking that the members of the dictionary are instances
|
| 374 |
+
// of the new types, nor is there a check that other IValues which
|
| 375 |
+
// hold references to this dictionary have the right static type.
|
| 376 |
+
// This functionality is used only in the unpickler, where at
|
| 377 |
+
// creation type the real type of the dictionary is unknown, but
|
| 378 |
+
// then later recovered from the static type information of the
|
| 379 |
+
// unpickled object.
|
| 380 |
+
void unsafeSetKeyType(TypePtr t);
|
| 381 |
+
void unsafeSetValueType(TypePtr t);
|
| 382 |
+
};
|
| 383 |
+
|
| 384 |
+
namespace impl {
|
| 385 |
+
// GenericDict is how IValue stores dicts. It is, however, not part of the
|
| 386 |
+
// public API. Kernels should use Dicts with concrete Key, Value types instead
|
| 387 |
+
// (maybe except for some internal prim ops).
|
| 388 |
+
using GenericDict = Dict<IValue, IValue>;
|
| 389 |
+
|
| 390 |
+
}
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
namespace torch {
|
| 394 |
+
template<class Key, class Value> using Dict = c10::Dict<Key, Value>;
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
#include <ATen/core/Dict_inl.h> // IWYU pragma: keep
|
| 398 |
+
|
| 399 |
+
#else
|
| 400 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 401 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/ivalue.h>
|
| 5 |
+
#include <c10/util/hash.h>
|
| 6 |
+
|
| 7 |
+
namespace c10 {
|
| 8 |
+
namespace detail {
|
| 9 |
+
inline bool DictKeyEqualTo::operator()(const IValue& lhs, const IValue& rhs) const {
|
| 10 |
+
if (lhs.isTensor() && rhs.isTensor()) {
|
| 11 |
+
// for tensors, we compare only by identity (following how it's done in Python).
|
| 12 |
+
return lhs.is(rhs);
|
| 13 |
+
}
|
| 14 |
+
// Otherwise, we first compare by identity for efficiency, then by value (see:
|
| 15 |
+
// [container equality])
|
| 16 |
+
return _fastEqualsForContainer(lhs, rhs);
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
template<class T> decltype(auto) getTypePtr();
|
| 21 |
+
std::string toString(const Type& type);
|
| 22 |
+
|
| 23 |
+
namespace impl {
|
| 24 |
+
|
| 25 |
+
template<class Key, class Value>
|
| 26 |
+
Dict<Key, Value> toTypedDict(GenericDict dict) {
|
| 27 |
+
TORCH_INTERNAL_ASSERT(*getTypePtr<Key>() == *dict.impl_->elementTypes.keyType, "Tried to cast a Dict<", toString(*dict.impl_->elementTypes.keyType), ", ", toString(*dict.impl_->elementTypes.valueType) ,"> to a Dict<", toString(*getTypePtr<Key>()), ", ", toString(*getTypePtr<Value>()), ">. Key types mismatch.");
|
| 28 |
+
TORCH_INTERNAL_ASSERT(*getTypePtr<Value>() == *dict.impl_->elementTypes.valueType, "Tried to cast a Dict<", toString(*dict.impl_->elementTypes.keyType), ", ", toString(*dict.impl_->elementTypes.valueType) ,"> to a Dict<", toString(*getTypePtr<Key>()), ", ", toString(*getTypePtr<Value>()), ">. Value types mismatch.");
|
| 29 |
+
|
| 30 |
+
return Dict<Key, Value>(std::move(dict.impl_));
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
template<class Key, class Value>
|
| 34 |
+
GenericDict toGenericDict(Dict<Key, Value> dict) {
|
| 35 |
+
return GenericDict(std::move(dict.impl_));
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
namespace detail {
|
| 40 |
+
|
| 41 |
+
inline size_t DictKeyHash::operator()(const IValue& ivalue) const {
|
| 42 |
+
if (ivalue.isInt()) {
|
| 43 |
+
return std::hash<int64_t>()(ivalue.toInt());
|
| 44 |
+
} else if (ivalue.isString()) {
|
| 45 |
+
return std::hash<std::string_view>()(ivalue.toStringView());
|
| 46 |
+
} else if (ivalue.isDouble()) {
|
| 47 |
+
return std::hash<double>()(ivalue.toDouble());
|
| 48 |
+
} else if (ivalue.isComplexDouble()) {
|
| 49 |
+
return c10::hash<c10::complex<double>>()(ivalue.toComplexDouble());
|
| 50 |
+
} else if (ivalue.isBool()) {
|
| 51 |
+
return std::hash<bool>()(ivalue.toBool());
|
| 52 |
+
} else if (ivalue.isTensor()) {
|
| 53 |
+
return std::hash<TensorImpl*>()(ivalue.toTensor().unsafeGetTensorImpl());
|
| 54 |
+
} else if (ivalue.isDevice()) {
|
| 55 |
+
return std::hash<Device>()(ivalue.toDevice());
|
| 56 |
+
} else {
|
| 57 |
+
TORCH_CHECK(false, "Can't hash IValues with tag '", ivalue.tagKind(), "'");
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
inline intrusive_ptr<DictImpl> DictImpl::copy() const {
|
| 62 |
+
return make_intrusive<DictImpl>(dict, elementTypes);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
template<class Key, class Value>
|
| 68 |
+
Dict<Key, Value>::Dict()
|
| 69 |
+
:Dict(make_intrusive<detail::DictImpl>(
|
| 70 |
+
detail::DictImpl::dict_map_type(),
|
| 71 |
+
detail::DictImpl::DictElementTypes{getTypePtr<Key>(), getTypePtr<Value>()})) {
|
| 72 |
+
static_assert(!std::is_same_v<Key, IValue>, "This constructor is not valid for Dict<IValue, _>. Please use c10::impl::GenericDict(keyType, valueType) instead.");
|
| 73 |
+
static_assert(!std::is_same_v<Value, IValue>, "This constructor is not valid for Dict<_, IValue>. Please use c10::impl::GenericDict(keyType, valueType) instead.");
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
template<class Key, class Value>
|
| 77 |
+
Dict<Key, Value>::Dict(TypePtr keyType, TypePtr valueType)
|
| 78 |
+
: Dict(make_intrusive<detail::DictImpl>(
|
| 79 |
+
detail::DictImpl::dict_map_type(),
|
| 80 |
+
detail::DictImpl::DictElementTypes {std::move(keyType), std::move(valueType)})) {
|
| 81 |
+
static_assert(std::is_same_v<Key, IValue>, "This constructor is only valid for c10::impl::GenericDict.");
|
| 82 |
+
static_assert(std::is_same_v<Value, IValue>, "This constructor is only valid for c10::impl::GenericDict.");
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
template<class Key, class Value>
|
| 86 |
+
Dict<Key, Value>::Dict(c10::intrusive_ptr<detail::DictImpl>&& impl): impl_(std::move(impl)) {}
|
| 87 |
+
|
| 88 |
+
template<class Key, class Value>
|
| 89 |
+
Dict<Key, Value> Dict<Key, Value>::copy() const {
|
| 90 |
+
return Dict<Key, Value>(impl_->copy());
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
template<class Key, class Value>
|
| 94 |
+
typename Dict<Key, Value>::iterator Dict<Key, Value>::begin() const {
|
| 95 |
+
return iterator{impl_->dict.begin()};
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
template<class Key, class Value>
|
| 99 |
+
typename Dict<Key, Value>::iterator Dict<Key, Value>::end() const {
|
| 100 |
+
return iterator{impl_->dict.end()};
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
template<class Key, class Value>
|
| 104 |
+
bool Dict<Key, Value>::empty() const {
|
| 105 |
+
return impl_->dict.empty();
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
template<class Key, class Value>
|
| 109 |
+
typename Dict<Key, Value>::size_type Dict<Key, Value>::size() const {
|
| 110 |
+
return impl_->dict.size();
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
template<class Key, class Value>
|
| 114 |
+
void Dict<Key, Value>::clear() const {
|
| 115 |
+
impl_->dict.clear();
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
template<class Key, class Value>
|
| 119 |
+
template<class Key_, class Value_>
|
| 120 |
+
std::pair<typename Dict<Key, Value>::iterator, bool> Dict<Key, Value>::insert(Key_&& key, Value_&& value) const {
|
| 121 |
+
static_assert(std::is_constructible_v<Key, Key_>, "Wrong type for the key argument of Dict::insert");
|
| 122 |
+
static_assert(std::is_constructible_v<Value, Value_>, "Wrong type for the value argument of Dict::insert");
|
| 123 |
+
auto inserted = impl_->dict.emplace(
|
| 124 |
+
Key(std::forward<Key_>(key)),
|
| 125 |
+
Value(std::forward<Value_>(value)));
|
| 126 |
+
return {iterator{inserted.first}, inserted.second};
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
template<class Key, class Value>
|
| 130 |
+
template<class Key_, class Value_>
|
| 131 |
+
std::pair<typename Dict<Key, Value>::iterator, bool> Dict<Key, Value>::insert_or_assign(Key_&& key, Value_&& value) const {
|
| 132 |
+
static_assert(std::is_constructible_v<Key, Key_>, "Wrong type for the key argument of Dict::insert_or_assign");
|
| 133 |
+
static_assert(std::is_constructible_v<Value, Value_>, "Wrong type for the value argument of Dict::insert_or_assign");
|
| 134 |
+
auto inserted = impl_->dict.insert_or_assign(
|
| 135 |
+
Key(std::forward<Key_>(key)),
|
| 136 |
+
Value(std::forward<Value_>(value)));
|
| 137 |
+
return {iterator{inserted.first}, inserted.second};
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
template<class Key, class Value>
|
| 141 |
+
void Dict<Key, Value>::erase(iterator iter) const {
|
| 142 |
+
impl_->dict.erase(iter.entryRef_.iterator_);
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
template <class Key, class Value>
|
| 146 |
+
[[nodiscard]] size_t Dict<Key, Value>::erase(const Key& key) const {
|
| 147 |
+
return impl_->dict.erase(key);
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
template<class Key, class Value>
|
| 151 |
+
Value Dict<Key, Value>::at(const Key& key) const {
|
| 152 |
+
return impl_->dict.at(key).template to<Value>();
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
template<class Key, class Value>
|
| 156 |
+
typename Dict<Key, Value>::iterator Dict<Key, Value>::find(const Key& key) const {
|
| 157 |
+
return iterator{impl_->dict.find(key)};
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
template<class Key, class Value>
|
| 161 |
+
bool Dict<Key, Value>::contains(const Key& key) const {
|
| 162 |
+
return end() != find(key);
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
template<class Key, class Value>
|
| 166 |
+
void Dict<Key, Value>::reserve(size_type count) const {
|
| 167 |
+
impl_->dict.reserve(count);
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
template<class Key, class Value>
|
| 171 |
+
TypePtr Dict<Key, Value>::keyType() const {
|
| 172 |
+
return impl_->elementTypes.keyType;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
template<class Key, class Value>
|
| 176 |
+
TypePtr Dict<Key, Value>::valueType() const {
|
| 177 |
+
return impl_->elementTypes.valueType;
|
| 178 |
+
}
|
| 179 |
+
template <class Key, class Value>
|
| 180 |
+
void Dict<Key, Value>::unsafeSetKeyType(TypePtr t) {
|
| 181 |
+
impl_->elementTypes.keyType = std::move(t);
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
template <class Key, class Value>
|
| 185 |
+
void Dict<Key, Value>::unsafeSetValueType(TypePtr t) {
|
| 186 |
+
impl_->elementTypes.valueType = std::move(t);
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
template <class Key_, class Value_>
|
| 190 |
+
bool operator==(const Dict<Key_, Value_>& lhs, const Dict<Key_, Value_>& rhs) {
|
| 191 |
+
// Dicts with the same identity trivially compare equal.
|
| 192 |
+
if (lhs.impl_ == rhs.impl_) {
|
| 193 |
+
return true;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// Otherwise compare the values
|
| 197 |
+
return *lhs.impl_ == *rhs.impl_;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
template <class Key_, class Value_>
|
| 201 |
+
bool operator!=(const Dict<Key_, Value_>& lhs, const Dict<Key_, Value_>& rhs) {
|
| 202 |
+
return !(lhs == rhs);
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
template <class Key, class Value>
|
| 206 |
+
bool Dict<Key, Value>::is(const Dict& rhs) const {
|
| 207 |
+
return this->impl_ == rhs.impl_;
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
#else
|
| 212 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 213 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/DimVector.h
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
#include <c10/util/DimVector.h>
|
| 4 |
+
|
| 5 |
+
namespace at {
|
| 6 |
+
|
| 7 |
+
// Redeclaring 'DimVector' type and size inside 'at' namespace.
|
| 8 |
+
// This is done to avoid modifying every use into their 'c10'
|
| 9 |
+
// equivalent.
|
| 10 |
+
|
| 11 |
+
using c10::kDimVectorStaticSize;
|
| 12 |
+
using c10::DimVector;
|
| 13 |
+
|
| 14 |
+
} // namespace at
|
| 15 |
+
|
| 16 |
+
#else
|
| 17 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 18 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Dimname.h
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/symbol.h>
|
| 5 |
+
#include <c10/util/ArrayRef.h>
|
| 6 |
+
#include <optional>
|
| 7 |
+
#include <ostream>
|
| 8 |
+
|
| 9 |
+
namespace at {
|
| 10 |
+
|
| 11 |
+
enum class NameType: uint8_t { BASIC, WILDCARD };
|
| 12 |
+
|
| 13 |
+
struct TORCH_API Dimname {
|
| 14 |
+
static Dimname fromSymbol(Symbol name);
|
| 15 |
+
static Dimname wildcard();
|
| 16 |
+
static bool isValidName(const std::string& name);
|
| 17 |
+
|
| 18 |
+
NameType type() const { return type_; }
|
| 19 |
+
Symbol symbol() const { return name_; }
|
| 20 |
+
|
| 21 |
+
bool isBasic() const { return type_ == NameType::BASIC; }
|
| 22 |
+
bool isWildcard() const { return type_ == NameType::WILDCARD; }
|
| 23 |
+
|
| 24 |
+
bool matches(Dimname other) const;
|
| 25 |
+
std::optional<Dimname> unify(Dimname other) const;
|
| 26 |
+
|
| 27 |
+
private:
|
| 28 |
+
Dimname(Symbol name)
|
| 29 |
+
: name_(name), type_(NameType::BASIC) {}
|
| 30 |
+
Dimname(Symbol name, NameType type)
|
| 31 |
+
: name_(name), type_(type) {}
|
| 32 |
+
|
| 33 |
+
Symbol name_;
|
| 34 |
+
NameType type_;
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
using DimnameList = c10::ArrayRef<Dimname>;
|
| 38 |
+
|
| 39 |
+
TORCH_API std::ostream& operator<<(std::ostream& out, const Dimname& dimname);
|
| 40 |
+
|
| 41 |
+
inline bool operator==(const Dimname& lhs, const Dimname& rhs) {
|
| 42 |
+
return lhs.symbol() == rhs.symbol();
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
inline bool operator!=(const Dimname& lhs, const Dimname& rhs) {
|
| 46 |
+
return !(lhs == rhs);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
} // namespace at
|
| 50 |
+
|
| 51 |
+
#else
|
| 52 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 53 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/DistributionsHelper.h
ADDED
|
@@ -0,0 +1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ined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/TransformationHelper.h>
|
| 5 |
+
#include <c10/util/Half.h>
|
| 6 |
+
#include <c10/util/BFloat16.h>
|
| 7 |
+
#include <c10/util/MathConstants.h>
|
| 8 |
+
#include <c10/macros/Macros.h>
|
| 9 |
+
|
| 10 |
+
#include <cmath>
|
| 11 |
+
#include <limits>
|
| 12 |
+
#include <optional>
|
| 13 |
+
#include <type_traits>
|
| 14 |
+
|
| 15 |
+
/**
|
| 16 |
+
* Distributions kernel adapted from THRandom.cpp
|
| 17 |
+
* The kernels try to follow std::random distributions signature
|
| 18 |
+
* For instance: in ATen
|
| 19 |
+
* auto gen = at::detail::createCPUGenerator();
|
| 20 |
+
* at::uniform_real_distribution<double> uniform(0, 1);
|
| 21 |
+
* auto sample = uniform(gen.get());
|
| 22 |
+
*
|
| 23 |
+
* vs std::random
|
| 24 |
+
*
|
| 25 |
+
* std::mt19937 gen;
|
| 26 |
+
* std::uniform_real_distribution uniform(0, 1);
|
| 27 |
+
* auto sample = uniform(gen);
|
| 28 |
+
*/
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
namespace at {
|
| 32 |
+
namespace {
|
| 33 |
+
|
| 34 |
+
/**
|
| 35 |
+
* Samples a discrete uniform distribution in the range [base, base+range) of type T
|
| 36 |
+
*/
|
| 37 |
+
template <typename T>
|
| 38 |
+
struct uniform_int_from_to_distribution {
|
| 39 |
+
|
| 40 |
+
C10_HOST_DEVICE inline uniform_int_from_to_distribution(uint64_t range, int64_t base) : range_(range), base_(base) {}
|
| 41 |
+
|
| 42 |
+
template <typename RNG>
|
| 43 |
+
C10_HOST_DEVICE inline T operator()(RNG generator) {
|
| 44 |
+
#ifdef FBCODE_CAFFE2
|
| 45 |
+
if ((
|
| 46 |
+
std::is_same_v<T, int64_t> ||
|
| 47 |
+
std::is_same_v<T, double> ||
|
| 48 |
+
std::is_same_v<T, float> ||
|
| 49 |
+
std::is_same_v<T, at::BFloat16>) && range_ >= 1ULL << 32)
|
| 50 |
+
#else
|
| 51 |
+
if (range_ >= 1ULL << 28) // allow approx 5% skew in uniform int generation using %
|
| 52 |
+
#endif
|
| 53 |
+
{
|
| 54 |
+
return transformation::uniform_int_from_to<T>(generator->random64(), range_, base_);
|
| 55 |
+
} else {
|
| 56 |
+
return transformation::uniform_int_from_to<T>(generator->random(), range_, base_);
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
private:
|
| 61 |
+
uint64_t range_;
|
| 62 |
+
int64_t base_;
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
/**
|
| 66 |
+
* Samples a discrete uniform distribution in the range [min_value(int64_t), max_value(int64_t)]
|
| 67 |
+
*/
|
| 68 |
+
template <typename T>
|
| 69 |
+
struct uniform_int_full_range_distribution {
|
| 70 |
+
|
| 71 |
+
template <typename RNG>
|
| 72 |
+
C10_HOST_DEVICE inline T operator()(RNG generator) {
|
| 73 |
+
return transformation::uniform_int_full_range<T>(generator->random64());
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
/**
|
| 79 |
+
* Samples a discrete uniform distribution in the range [0, max_value(T)] for integral types
|
| 80 |
+
* and [0, 2^mantissa] for floating-point types.
|
| 81 |
+
*/
|
| 82 |
+
template <typename T>
|
| 83 |
+
struct uniform_int_distribution {
|
| 84 |
+
|
| 85 |
+
template <typename RNG>
|
| 86 |
+
C10_HOST_DEVICE inline T operator()(RNG generator) {
|
| 87 |
+
if constexpr (std::is_same_v<T, double> || std::is_same_v<T, int64_t>) {
|
| 88 |
+
return transformation::uniform_int<T>(generator->random64());
|
| 89 |
+
} else {
|
| 90 |
+
return transformation::uniform_int<T>(generator->random());
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
};
|
| 95 |
+
|
| 96 |
+
/**
|
| 97 |
+
* Samples a uniform distribution in the range [from, to) of type T
|
| 98 |
+
*/
|
| 99 |
+
template <typename T>
|
| 100 |
+
struct uniform_real_distribution {
|
| 101 |
+
|
| 102 |
+
C10_HOST_DEVICE inline uniform_real_distribution(T from, T to) : from_(from), to_(to) {
|
| 103 |
+
TORCH_CHECK_IF_NOT_ON_CUDA(from <= to);
|
| 104 |
+
TORCH_CHECK_IF_NOT_ON_CUDA(to - from <= std::numeric_limits<T>::max());
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
template <typename RNG>
|
| 108 |
+
C10_HOST_DEVICE inline dist_acctype<T> operator()(RNG generator){
|
| 109 |
+
if constexpr (std::is_same_v<T, double>) {
|
| 110 |
+
return transformation::uniform_real<T>(generator->random64(), from_, to_);
|
| 111 |
+
} else {
|
| 112 |
+
return transformation::uniform_real<T>(generator->random(), from_, to_);
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
private:
|
| 117 |
+
T from_;
|
| 118 |
+
T to_;
|
| 119 |
+
};
|
| 120 |
+
|
| 121 |
+
// The SFINAE checks introduced in #39816 looks overcomplicated and must revisited
|
| 122 |
+
// https://github.com/pytorch/pytorch/issues/40052
|
| 123 |
+
#define DISTRIBUTION_HELPER_GENERATE_HAS_MEMBER(member) \
|
| 124 |
+
template <typename T> \
|
| 125 |
+
struct has_member_##member \
|
| 126 |
+
{ \
|
| 127 |
+
typedef char yes; \
|
| 128 |
+
typedef long no; \
|
| 129 |
+
template <typename U> static yes test(decltype(&U::member)); \
|
| 130 |
+
template <typename U> static no test(...); \
|
| 131 |
+
static constexpr bool value = sizeof(test<T>(0)) == sizeof(yes); \
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
DISTRIBUTION_HELPER_GENERATE_HAS_MEMBER(next_double_normal_sample);
|
| 135 |
+
DISTRIBUTION_HELPER_GENERATE_HAS_MEMBER(set_next_double_normal_sample);
|
| 136 |
+
DISTRIBUTION_HELPER_GENERATE_HAS_MEMBER(next_float_normal_sample);
|
| 137 |
+
DISTRIBUTION_HELPER_GENERATE_HAS_MEMBER(set_next_float_normal_sample);
|
| 138 |
+
|
| 139 |
+
#define DISTRIBUTION_HELPER_GENERATE_NEXT_NORMAL_METHODS(TYPE) \
|
| 140 |
+
\
|
| 141 |
+
template <typename RNG, typename ret_type, \
|
| 142 |
+
typename std::enable_if_t<( \
|
| 143 |
+
has_member_next_##TYPE##_normal_sample<RNG>::value && \
|
| 144 |
+
has_member_set_next_##TYPE##_normal_sample<RNG>::value \
|
| 145 |
+
), int> = 0> \
|
| 146 |
+
C10_HOST_DEVICE inline bool maybe_get_next_##TYPE##_normal_sample(RNG* generator, ret_type* ret) { \
|
| 147 |
+
if (generator->next_##TYPE##_normal_sample()) { \
|
| 148 |
+
*ret = *(generator->next_##TYPE##_normal_sample()); \
|
| 149 |
+
generator->set_next_##TYPE##_normal_sample(std::optional<TYPE>()); \
|
| 150 |
+
return true; \
|
| 151 |
+
} \
|
| 152 |
+
return false; \
|
| 153 |
+
} \
|
| 154 |
+
\
|
| 155 |
+
template <typename RNG, typename ret_type, \
|
| 156 |
+
typename std::enable_if_t<( \
|
| 157 |
+
!has_member_next_##TYPE##_normal_sample<RNG>::value || \
|
| 158 |
+
!has_member_set_next_##TYPE##_normal_sample<RNG>::value \
|
| 159 |
+
), int> = 0> \
|
| 160 |
+
C10_HOST_DEVICE inline bool maybe_get_next_##TYPE##_normal_sample(RNG* /*generator*/, ret_type* /*ret*/) { \
|
| 161 |
+
return false; \
|
| 162 |
+
} \
|
| 163 |
+
\
|
| 164 |
+
template <typename RNG, typename ret_type, \
|
| 165 |
+
typename std::enable_if_t<( \
|
| 166 |
+
has_member_set_next_##TYPE##_normal_sample<RNG>::value \
|
| 167 |
+
), int> = 0> \
|
| 168 |
+
C10_HOST_DEVICE inline void maybe_set_next_##TYPE##_normal_sample(RNG* generator, ret_type cache) { \
|
| 169 |
+
generator->set_next_##TYPE##_normal_sample(cache); \
|
| 170 |
+
} \
|
| 171 |
+
\
|
| 172 |
+
template <typename RNG, typename ret_type, \
|
| 173 |
+
typename std::enable_if_t<( \
|
| 174 |
+
!has_member_set_next_##TYPE##_normal_sample<RNG>::value \
|
| 175 |
+
), int> = 0> \
|
| 176 |
+
C10_HOST_DEVICE inline void maybe_set_next_##TYPE##_normal_sample(RNG* /*generator*/, ret_type /*cache*/) { \
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
DISTRIBUTION_HELPER_GENERATE_NEXT_NORMAL_METHODS(double)
|
| 180 |
+
DISTRIBUTION_HELPER_GENERATE_NEXT_NORMAL_METHODS(float)
|
| 181 |
+
|
| 182 |
+
/**
|
| 183 |
+
* Samples a normal distribution using the Box-Muller method
|
| 184 |
+
* Takes mean and standard deviation as inputs
|
| 185 |
+
* Note that Box-muller method returns two samples at a time.
|
| 186 |
+
* Hence, we cache the "next" sample in the CPUGeneratorImpl class.
|
| 187 |
+
*/
|
| 188 |
+
template <typename T>
|
| 189 |
+
struct normal_distribution {
|
| 190 |
+
|
| 191 |
+
C10_HOST_DEVICE inline normal_distribution(T mean_in, T stdv_in) : mean(mean_in), stdv(stdv_in) {
|
| 192 |
+
TORCH_CHECK_IF_NOT_ON_CUDA(stdv_in >= 0, "stdv_in must be positive: ", stdv_in);
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
template <typename RNG>
|
| 196 |
+
C10_HOST_DEVICE inline dist_acctype<T> operator()(RNG generator){
|
| 197 |
+
dist_acctype<T> ret;
|
| 198 |
+
// return cached values if available
|
| 199 |
+
if constexpr (std::is_same_v<T, double>) {
|
| 200 |
+
if (maybe_get_next_double_normal_sample(generator, &ret)) {
|
| 201 |
+
return transformation::normal(ret, mean, stdv);
|
| 202 |
+
}
|
| 203 |
+
} else {
|
| 204 |
+
if (maybe_get_next_float_normal_sample(generator, &ret)) {
|
| 205 |
+
return transformation::normal(ret, mean, stdv);
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
// otherwise generate new normal values
|
| 209 |
+
uniform_real_distribution<T> uniform(0.0, 1.0);
|
| 210 |
+
const dist_acctype<T> u1 = uniform(generator);
|
| 211 |
+
const dist_acctype<T> u2 = uniform(generator);
|
| 212 |
+
const dist_acctype<T> r = ::sqrt(static_cast<T>(-2.0) * ::log1p(-u2));
|
| 213 |
+
const dist_acctype<T> theta = static_cast<T>(2.0) * c10::pi<T> * u1;
|
| 214 |
+
if constexpr (std::is_same_v<T, double>) {
|
| 215 |
+
maybe_set_next_double_normal_sample(generator, r * ::sin(theta));
|
| 216 |
+
} else {
|
| 217 |
+
maybe_set_next_float_normal_sample(generator, r * ::sin(theta));
|
| 218 |
+
}
|
| 219 |
+
ret = r * ::cos(theta);
|
| 220 |
+
return transformation::normal(ret, mean, stdv);
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
private:
|
| 224 |
+
T mean;
|
| 225 |
+
T stdv;
|
| 226 |
+
};
|
| 227 |
+
|
| 228 |
+
template <typename T>
|
| 229 |
+
struct DiscreteDistributionType { using type = float; };
|
| 230 |
+
|
| 231 |
+
template <> struct DiscreteDistributionType<double> { using type = double; };
|
| 232 |
+
|
| 233 |
+
/**
|
| 234 |
+
* Samples a bernoulli distribution given a probability input
|
| 235 |
+
*/
|
| 236 |
+
template <typename T>
|
| 237 |
+
struct bernoulli_distribution {
|
| 238 |
+
|
| 239 |
+
C10_HOST_DEVICE inline bernoulli_distribution(T p_in) : p(p_in) {
|
| 240 |
+
TORCH_CHECK_IF_NOT_ON_CUDA(p_in >= 0 && p_in <= 1);
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
template <typename RNG>
|
| 244 |
+
C10_HOST_DEVICE inline T operator()(RNG generator) {
|
| 245 |
+
uniform_real_distribution<T> uniform(0.0, 1.0);
|
| 246 |
+
return transformation::bernoulli<T>(uniform(generator), p);
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
private:
|
| 250 |
+
T p;
|
| 251 |
+
};
|
| 252 |
+
|
| 253 |
+
/**
|
| 254 |
+
* Samples a geometric distribution given a probability input
|
| 255 |
+
*/
|
| 256 |
+
template <typename T>
|
| 257 |
+
struct geometric_distribution {
|
| 258 |
+
|
| 259 |
+
C10_HOST_DEVICE inline geometric_distribution(T p_in) : p(p_in) {
|
| 260 |
+
TORCH_CHECK_IF_NOT_ON_CUDA(p_in > 0 && p_in < 1);
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
template <typename RNG>
|
| 264 |
+
C10_HOST_DEVICE inline T operator()(RNG generator) {
|
| 265 |
+
uniform_real_distribution<T> uniform(0.0, 1.0);
|
| 266 |
+
return transformation::geometric<T>(uniform(generator), p);
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
private:
|
| 270 |
+
T p;
|
| 271 |
+
};
|
| 272 |
+
|
| 273 |
+
/**
|
| 274 |
+
* Samples an exponential distribution given a lambda input
|
| 275 |
+
*/
|
| 276 |
+
template <typename T>
|
| 277 |
+
struct exponential_distribution {
|
| 278 |
+
|
| 279 |
+
C10_HOST_DEVICE inline exponential_distribution(T lambda_in) : lambda(lambda_in) {}
|
| 280 |
+
|
| 281 |
+
template <typename RNG>
|
| 282 |
+
C10_HOST_DEVICE inline T operator()(RNG generator) {
|
| 283 |
+
uniform_real_distribution<T> uniform(0.0, 1.0);
|
| 284 |
+
return transformation::exponential<T>(uniform(generator), lambda);
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
private:
|
| 288 |
+
T lambda;
|
| 289 |
+
};
|
| 290 |
+
|
| 291 |
+
/**
|
| 292 |
+
* Samples a cauchy distribution given median and sigma as inputs
|
| 293 |
+
*/
|
| 294 |
+
template <typename T>
|
| 295 |
+
struct cauchy_distribution {
|
| 296 |
+
|
| 297 |
+
C10_HOST_DEVICE inline cauchy_distribution(T median_in, T sigma_in) : median(median_in), sigma(sigma_in) {}
|
| 298 |
+
|
| 299 |
+
template <typename RNG>
|
| 300 |
+
C10_HOST_DEVICE inline T operator()(RNG generator) {
|
| 301 |
+
uniform_real_distribution<T> uniform(0.0, 1.0);
|
| 302 |
+
return transformation::cauchy<T>(uniform(generator), median, sigma);
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
private:
|
| 306 |
+
T median;
|
| 307 |
+
T sigma;
|
| 308 |
+
};
|
| 309 |
+
|
| 310 |
+
/**
|
| 311 |
+
* Samples a lognormal distribution
|
| 312 |
+
* Takes mean and standard deviation as inputs
|
| 313 |
+
* Outputs two samples at a time
|
| 314 |
+
*/
|
| 315 |
+
template <typename T>
|
| 316 |
+
struct lognormal_distribution {
|
| 317 |
+
|
| 318 |
+
C10_HOST_DEVICE inline lognormal_distribution(T mean_in, T stdv_in) : mean(mean_in), stdv(stdv_in) {
|
| 319 |
+
TORCH_CHECK_IF_NOT_ON_CUDA(stdv_in > 0);
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
template<typename RNG>
|
| 323 |
+
C10_HOST_DEVICE inline T operator()(RNG generator){
|
| 324 |
+
normal_distribution<T> normal(mean, stdv);
|
| 325 |
+
return transformation::log_normal<T>(normal(generator));
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
private:
|
| 329 |
+
T mean;
|
| 330 |
+
T stdv;
|
| 331 |
+
};
|
| 332 |
+
}
|
| 333 |
+
} // namespace at
|
| 334 |
+
|
| 335 |
+
#else
|
| 336 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 337 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Formatting.h
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ostream>
|
| 5 |
+
#include <string>
|
| 6 |
+
|
| 7 |
+
#include <c10/core/Scalar.h>
|
| 8 |
+
#include <ATen/core/Tensor.h>
|
| 9 |
+
|
| 10 |
+
namespace c10 {
|
| 11 |
+
TORCH_API std::ostream& operator<<(std::ostream& out, Backend b);
|
| 12 |
+
TORCH_API std::ostream& operator<<(std::ostream & out, const Scalar& s);
|
| 13 |
+
TORCH_API std::string toString(const Scalar& s);
|
| 14 |
+
}
|
| 15 |
+
namespace at {
|
| 16 |
+
|
| 17 |
+
TORCH_API std::ostream& operator<<(std::ostream& out, const DeprecatedTypeProperties& t);
|
| 18 |
+
TORCH_API std::ostream& print(
|
| 19 |
+
std::ostream& stream,
|
| 20 |
+
const Tensor& tensor,
|
| 21 |
+
int64_t linesize);
|
| 22 |
+
inline std::ostream& operator<<(std::ostream & out, const Tensor & t) {
|
| 23 |
+
return print(out,t,80);
|
| 24 |
+
}
|
| 25 |
+
TORCH_API void print(const Tensor & t, int64_t linesize=80);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
#else
|
| 29 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 30 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/Generator.h
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 |
+
#include <cstdint>
|
| 5 |
+
#include <deque>
|
| 6 |
+
#include <mutex>
|
| 7 |
+
#include <utility>
|
| 8 |
+
|
| 9 |
+
#include <c10/util/Exception.h>
|
| 10 |
+
#include <c10/util/intrusive_ptr.h>
|
| 11 |
+
#include <c10/core/Device.h>
|
| 12 |
+
#include <c10/core/DispatchKeySet.h>
|
| 13 |
+
|
| 14 |
+
// For the record I don't think this is a correct pimpl idiom.
|
| 15 |
+
// Including Impl header in interface header defeats the purpose
|
| 16 |
+
// because you can't change Impl private members without forcing
|
| 17 |
+
// everything that included the interface to rebuild.
|
| 18 |
+
// Impl should be forward-declared in the interface header instead.
|
| 19 |
+
#include <c10/core/GeneratorImpl.h>
|
| 20 |
+
|
| 21 |
+
/**
|
| 22 |
+
* Note [Generator]
|
| 23 |
+
* ~~~~~~~~~~~~~~~~
|
| 24 |
+
* A Pseudo Random Number Generator (PRNG) is an engine that uses an algorithm to
|
| 25 |
+
* generate a seemingly random sequence of numbers, that may be later be used in creating
|
| 26 |
+
* a random distribution. Such an engine almost always maintains a state and requires a
|
| 27 |
+
* seed to start off the creation of random numbers. Often times, users have
|
| 28 |
+
* found it beneficial to be able to explicitly create, retain, and destroy
|
| 29 |
+
* PRNG states and also be able to have control over the seed value.
|
| 30 |
+
*
|
| 31 |
+
* A Generator in ATen gives users the ability to read, write and modify a PRNG engine.
|
| 32 |
+
* For instance, it does so by letting users seed a PRNG engine, fork the state of the
|
| 33 |
+
* engine, etc.
|
| 34 |
+
*
|
| 35 |
+
* By default, there is one generator per device, and a device's generator is
|
| 36 |
+
* lazily created. A user can use the torch.Generator() api to create their own generator.
|
| 37 |
+
*/
|
| 38 |
+
|
| 39 |
+
/**
|
| 40 |
+
* Note [Acquire lock when using random generators]
|
| 41 |
+
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 42 |
+
* Generator and its derived classes are NOT thread-safe. Please note that most of the
|
| 43 |
+
* places where we have inserted locking for generators are historically based, and we
|
| 44 |
+
* haven't actually checked that everything is truly thread safe (and it probably isn't).
|
| 45 |
+
* Please use the public mutex_ when using any methods from these classes, except for the
|
| 46 |
+
* read-only methods. You can learn about the usage by looking into the unittests
|
| 47 |
+
* (aten/src/ATen/cpu_generator_test.cpp) and other places where we have used lock_guard.
|
| 48 |
+
*
|
| 49 |
+
* TODO: Look into changing the threading semantics of Generators in ATen (e.g., making
|
| 50 |
+
* them non-thread safe and instead making the generator state splittable, to accommodate
|
| 51 |
+
* forks into other threads).
|
| 52 |
+
*/
|
| 53 |
+
|
| 54 |
+
namespace at {
|
| 55 |
+
|
| 56 |
+
class Tensor;
|
| 57 |
+
|
| 58 |
+
struct TORCH_API Generator {
|
| 59 |
+
Generator() = default;
|
| 60 |
+
|
| 61 |
+
explicit Generator(c10::intrusive_ptr<c10::GeneratorImpl> gen_impl)
|
| 62 |
+
: impl_(std::move(gen_impl)) {
|
| 63 |
+
TORCH_CHECK(impl_.get(), "GeneratorImpl with nullptr is not supported");
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
bool operator==(const Generator& rhs) const {
|
| 67 |
+
return this->impl_ == rhs.impl_;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
bool operator!=(const Generator& rhs) const {
|
| 71 |
+
return !((*this) == rhs);
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
bool defined() const {
|
| 75 |
+
return static_cast<bool>(impl_);
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
c10::GeneratorImpl* unsafeGetGeneratorImpl() const {
|
| 79 |
+
return impl_.get();
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
c10::GeneratorImpl* unsafeReleaseGeneratorImpl() {
|
| 83 |
+
return impl_.release();
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
const c10::intrusive_ptr<c10::GeneratorImpl>& getIntrusivePtr() const {
|
| 87 |
+
return impl_;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
void set_current_seed(uint64_t seed) { impl_->set_current_seed(seed); }
|
| 91 |
+
// Sets the offset of Generator state to the desired offset. This is currently
|
| 92 |
+
// supported for only Philox based Generators, i.e., CUDA and MPS.
|
| 93 |
+
void set_offset(uint64_t offset) { impl_->set_offset(offset); }
|
| 94 |
+
|
| 95 |
+
// Returns the offset of Generator state. This is currently supported for only
|
| 96 |
+
// Philox based Generators, i.e., CUDA and MPS.
|
| 97 |
+
uint64_t get_offset() const { return impl_->get_offset(); }
|
| 98 |
+
|
| 99 |
+
uint64_t current_seed() const { return impl_->current_seed(); }
|
| 100 |
+
|
| 101 |
+
uint64_t seed() { return impl_->seed(); }
|
| 102 |
+
|
| 103 |
+
// Implementation not inlined to prevent cycle reference between
|
| 104 |
+
// `ATen/core/Generator.h` and `ATen/core/Tensor.h`
|
| 105 |
+
void set_state(const at::Tensor& new_state);
|
| 106 |
+
|
| 107 |
+
at::Tensor get_state() const;
|
| 108 |
+
|
| 109 |
+
void graphsafe_set_state(const Generator& new_state);
|
| 110 |
+
|
| 111 |
+
Generator graphsafe_get_state() const;
|
| 112 |
+
|
| 113 |
+
std::mutex& mutex() {
|
| 114 |
+
return impl_->mutex_;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
DispatchKeySet key_set() const {
|
| 118 |
+
return impl_->key_set();
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
Device device() const { return impl_->device(); }
|
| 122 |
+
|
| 123 |
+
inline void set_pyobj(PyObject* pyobj) const noexcept {
|
| 124 |
+
impl_->set_pyobj(pyobj);
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
inline PyObject* pyobj() const noexcept {
|
| 128 |
+
return impl_->pyobj();
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
template<typename T>
|
| 132 |
+
T* get() const { return static_cast<T*>(impl_.get()); }
|
| 133 |
+
|
| 134 |
+
Generator clone() const {
|
| 135 |
+
return Generator(impl_->clone());
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
private:
|
| 139 |
+
c10::intrusive_ptr<c10::GeneratorImpl> impl_;
|
| 140 |
+
};
|
| 141 |
+
|
| 142 |
+
template<class Impl, class... Args>
|
| 143 |
+
Generator make_generator(Args&&... args) {
|
| 144 |
+
return Generator(c10::make_intrusive<Impl>(std::forward<Args>(args)...));
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
/**
|
| 148 |
+
* Utility function to static cast input Generator* to
|
| 149 |
+
* the backend generator type (CPU/CUDAGeneratorImpl etc.)
|
| 150 |
+
*/
|
| 151 |
+
template <typename T>
|
| 152 |
+
inline T * check_generator(std::optional<Generator> gen) {
|
| 153 |
+
TORCH_CHECK(gen.has_value(), "Expected Generator but received nullopt");
|
| 154 |
+
TORCH_CHECK(gen->defined(), "Generator with undefined implementation is not allowed");
|
| 155 |
+
TORCH_CHECK(T::device_type() == gen->device().type(), "Expected a '", T::device_type(), "' device type for generator but found '", gen->device().type(), "'");
|
| 156 |
+
return gen->get<T>();
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
/**
|
| 160 |
+
* Utility function used in tensor implementations, which
|
| 161 |
+
* supplies the default generator to tensors, if an input generator
|
| 162 |
+
* is not supplied. The input Generator* is also static casted to
|
| 163 |
+
* the backend generator type (CPU/CUDAGeneratorImpl etc.)
|
| 164 |
+
*/
|
| 165 |
+
template <typename T>
|
| 166 |
+
inline T* get_generator_or_default(const std::optional<Generator>& gen, const Generator& default_gen) {
|
| 167 |
+
return gen.has_value() && gen->defined() ? check_generator<T>(gen) : check_generator<T>(default_gen);
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
namespace detail {
|
| 171 |
+
|
| 172 |
+
/**
|
| 173 |
+
* Helper function for checking the validity of new random generator
|
| 174 |
+
* state. Right now following conditions are checked:
|
| 175 |
+
*
|
| 176 |
+
* - The new state tensor must be a torch.ByteTensor
|
| 177 |
+
* - Data of the new state tensor must be contiguous
|
| 178 |
+
*/
|
| 179 |
+
inline void check_rng_state(const c10::TensorImpl& new_state) {
|
| 180 |
+
TORCH_CHECK_TYPE(
|
| 181 |
+
new_state.layout() == kStrided && new_state.device().type() == kCPU && new_state.dtype() == kByte,
|
| 182 |
+
"RNG state must be a torch.ByteTensor"
|
| 183 |
+
);
|
| 184 |
+
|
| 185 |
+
TORCH_CHECK(new_state.is_contiguous(), "RNG state must be contiguous");
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
} // namespace detail
|
| 189 |
+
|
| 190 |
+
} // namespace at
|
| 191 |
+
|
| 192 |
+
#else
|
| 193 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 194 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/GeneratorForPrivateuseone.h
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/Generator.h>
|
| 5 |
+
#include <c10/util/intrusive_ptr.h>
|
| 6 |
+
|
| 7 |
+
namespace at {
|
| 8 |
+
|
| 9 |
+
using GeneratorFuncType = std::function<at::Generator(c10::DeviceIndex)>;
|
| 10 |
+
|
| 11 |
+
TORCH_API std::optional<GeneratorFuncType>& GetGeneratorPrivate();
|
| 12 |
+
|
| 13 |
+
class TORCH_API _GeneratorRegister {
|
| 14 |
+
public:
|
| 15 |
+
explicit _GeneratorRegister(const GeneratorFuncType& func);
|
| 16 |
+
};
|
| 17 |
+
|
| 18 |
+
TORCH_API at::Generator GetGeneratorForPrivateuse1(
|
| 19 |
+
c10::DeviceIndex device_index);
|
| 20 |
+
|
| 21 |
+
/**
|
| 22 |
+
* This is used to register Generator to PyTorch for `privateuse1` key.
|
| 23 |
+
*
|
| 24 |
+
* Usage: REGISTER_GENERATOR_PRIVATEUSE1(MakeGeneratorForPrivateuse1)
|
| 25 |
+
*
|
| 26 |
+
* class CustomGeneratorImpl : public c10::GeneratorImpl {
|
| 27 |
+
* CustomGeneratorImpl(DeviceIndex device_index = -1);
|
| 28 |
+
* explicit ~CustomGeneratorImpl() override = default;
|
| 29 |
+
* ...
|
| 30 |
+
* };
|
| 31 |
+
*
|
| 32 |
+
* at::Generator MakeGeneratorForPrivateuse1(c10::DeviceIndex id) {
|
| 33 |
+
* return at::make_generator<CustomGeneratorImpl>(id);
|
| 34 |
+
* }
|
| 35 |
+
*/
|
| 36 |
+
|
| 37 |
+
#define REGISTER_GENERATOR_PRIVATEUSE1(GeneratorPrivate) \
|
| 38 |
+
static auto temp##GeneratorPrivate = at::_GeneratorRegister(GeneratorPrivate);
|
| 39 |
+
|
| 40 |
+
} // namespace at
|
| 41 |
+
|
| 42 |
+
#else
|
| 43 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 44 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/IListRef.h
ADDED
|
@@ -0,0 +1,638 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/ivalue_to.h>
|
| 5 |
+
#include <c10/util/ArrayRef.h>
|
| 6 |
+
#include <c10/util/Exception.h>
|
| 7 |
+
|
| 8 |
+
#include <functional>
|
| 9 |
+
#include <initializer_list>
|
| 10 |
+
#include <iterator>
|
| 11 |
+
#include <type_traits>
|
| 12 |
+
|
| 13 |
+
/*
|
| 14 |
+
* [Note: IListRef]
|
| 15 |
+
* Wrapper around different API containers (e.g. boxed and unboxed).
|
| 16 |
+
*
|
| 17 |
+
* What is it?
|
| 18 |
+
* ===========
|
| 19 |
+
* It is a tagged union of both boxed and unboxed API containers.
|
| 20 |
+
* Working implementations:
|
| 21 |
+
*
|
| 22 |
+
* - `IListRef<at::Tensor>`
|
| 23 |
+
* - `IListRef<at::OptionalTensorRef>`
|
| 24 |
+
*
|
| 25 |
+
* Note that `IListRef` is a view type. Meaning that it won't own the
|
| 26 |
+
* tensors it holds. It's intended to be used only as argument parameters.
|
| 27 |
+
* Specifically, where these 2 worlds overlap.
|
| 28 |
+
*
|
| 29 |
+
* What is this for?
|
| 30 |
+
* =================
|
| 31 |
+
* Historically, PyTorch has maintained 2 different APIs: the unboxed
|
| 32 |
+
* (called from C++ API and Python eager mode) and boxed APIs (called
|
| 33 |
+
* from the TorchScript JIT, mobile interpreter, and boxed fallbacks).
|
| 34 |
+
*
|
| 35 |
+
* Calling unboxed kernels from the boxed "world" and vice-versa may
|
| 36 |
+
* result in non-negligible overhead. Lists are one of those types:
|
| 37 |
+
*
|
| 38 |
+
* - Boxed world: `c10::List`
|
| 39 |
+
* - Unboxed world: `c10::ArrayRef`
|
| 40 |
+
*
|
| 41 |
+
* In this context, `c10::IListRef` solves this problem by wrapping those
|
| 42 |
+
* 2 container types, so that we don't need to convert from one to
|
| 43 |
+
* the other.
|
| 44 |
+
*
|
| 45 |
+
* (see https://github.com/pytorch/pytorch/issues/66328)
|
| 46 |
+
*
|
| 47 |
+
* What does it do?
|
| 48 |
+
* ================
|
| 49 |
+
* This container wraps around the different tagged containers
|
| 50 |
+
* (currently, only boxed and unboxed), without incurring in extra
|
| 51 |
+
* overhead for converting from one to another. It does so while
|
| 52 |
+
* exposing usual container methods, which dispatch to corresponding
|
| 53 |
+
* implementations.
|
| 54 |
+
*
|
| 55 |
+
* While it works with different container types, it introduces
|
| 56 |
+
* overhead for repeatedly calling member functions (since those will
|
| 57 |
+
* get dispatched, again). Therefore, you should only use it to iterate
|
| 58 |
+
* through the list up to one time. If you need to do more complex things,
|
| 59 |
+
* call `materialize()` first.
|
| 60 |
+
*
|
| 61 |
+
* Adding support for a new Tag
|
| 62 |
+
* ============================
|
| 63 |
+
* Suppose we want to add a new tag: `Chest`. Here are the steps
|
| 64 |
+
* we would have to go through:
|
| 65 |
+
*
|
| 66 |
+
* 1. Add a line for it in the macro `TORCH_ILISTREF_FORALL_TAGS`.
|
| 67 |
+
*
|
| 68 |
+
* #define TORCH_ILISTREF_FORALL_TAGS(_, ...) \
|
| 69 |
+
* ...
|
| 70 |
+
* _(Chest, ##__VA_ARGS__)
|
| 71 |
+
*
|
| 72 |
+
* 2. Add type aliases, union members, and constructors.
|
| 73 |
+
*
|
| 74 |
+
* template <typename T>
|
| 75 |
+
* class IListRef {
|
| 76 |
+
* ...
|
| 77 |
+
* using chest_type =
|
| 78 |
+
* typename detail::IListRefTagImpl<T, IListRefTag::Chest>::list_type;
|
| 79 |
+
* ...
|
| 80 |
+
* IListRef(...) : tag_(IListRefTag::Chest) {
|
| 81 |
+
* ...
|
| 82 |
+
* }
|
| 83 |
+
* ...
|
| 84 |
+
* union Payload {
|
| 85 |
+
* ...
|
| 86 |
+
* chest_type chest;
|
| 87 |
+
* ...
|
| 88 |
+
* };
|
| 89 |
+
* ...
|
| 90 |
+
* };
|
| 91 |
+
*
|
| 92 |
+
* 3. Add a default implementation for it (in 'IListRef_inl.h'). It's
|
| 93 |
+
* preferable to make the default implementation work for `T = Tensor`
|
| 94 |
+
* (both `Unboxed` and `Boxed` do it).
|
| 95 |
+
*
|
| 96 |
+
* template <typename T, typename ListElemT>
|
| 97 |
+
* class IListRefTagImplBase<IListRefTag::Chest, T, ListElemT> {
|
| 98 |
+
* public:
|
| 99 |
+
* using elem_type = ListElemT;
|
| 100 |
+
* using list_type = ChestContainer<elem_type>;
|
| 101 |
+
*
|
| 102 |
+
* static const list_type& unwrap(const IListRef<T>& ilist) { ... }
|
| 103 |
+
*
|
| 104 |
+
* static typename list_type::const_iterator& unwrap(
|
| 105 |
+
* IListRefIterator<T>& it) { ... }
|
| 106 |
+
*
|
| 107 |
+
* static const typename list_type::const_iterator& unwrap(
|
| 108 |
+
* const IListRefIterator<T>& it) { ... }
|
| 109 |
+
*
|
| 110 |
+
* static IListRefConstRef<T> iterator_get(
|
| 111 |
+
* const typename list_type::const_iterator& it) { ... }
|
| 112 |
+
* }
|
| 113 |
+
*
|
| 114 |
+
* 4. Add an specialization for each of the already supported types.
|
| 115 |
+
* Finally, for consistency, add them to the tracking list.
|
| 116 |
+
* (see [Note: IListRefTagImpl Specializations])
|
| 117 |
+
*
|
| 118 |
+
* template <>
|
| 119 |
+
* class IListRefTagImpl<IListRefTag::Chest, at::Tensor>
|
| 120 |
+
* : public IListRefTagImplBase<IListRefTag::Chest, at::Tensor> {};
|
| 121 |
+
*
|
| 122 |
+
* Adding support for a new Type
|
| 123 |
+
* =============================
|
| 124 |
+
* Suppose we want to add support for a new type: `Matrix`.
|
| 125 |
+
* Here are the steps we would have to go through:
|
| 126 |
+
*
|
| 127 |
+
* 1. Add an specialization for each of the existing tags.
|
| 128 |
+
* For consistency, add them to the tracking list.
|
| 129 |
+
* (see [Note: IListRefTagImpl Specializations])
|
| 130 |
+
*
|
| 131 |
+
* template <>
|
| 132 |
+
* class IListRefTagImpl<IListRefTag::Unboxed, Matrix>
|
| 133 |
+
* : public IListRefTagImplBase<IListRefTag::Unboxed, Matrix> {};
|
| 134 |
+
*
|
| 135 |
+
* template <>
|
| 136 |
+
* class IListRefTagImpl<Matrix, IListRefTag::Boxed>
|
| 137 |
+
* : public IListRefTagImplBase<IListRefTag::Boxed, Matrix> {};
|
| 138 |
+
*
|
| 139 |
+
* Common Problems
|
| 140 |
+
* ===============
|
| 141 |
+
* 1. One of `IListRef(Iterator)` methods are failing to compile.
|
| 142 |
+
*
|
| 143 |
+
* That may be happening because the container type you added
|
| 144 |
+
* is not compatible with the code written for that method. If
|
| 145 |
+
* that's true, then you might have to transform that code into
|
| 146 |
+
* a static method call (see `List::operator[]` method).
|
| 147 |
+
*
|
| 148 |
+
* 2. Can't make `IListRefIterator<T>::operator*` return a const-reference.
|
| 149 |
+
*
|
| 150 |
+
* First, keep in mind that we assume that boxed containers will
|
| 151 |
+
* have to deal with `IValue` (e.g. `c10::List`). In this context,
|
| 152 |
+
* what may be happening is that `IValue` doesn't store internally
|
| 153 |
+
* your type `T`. Instead, it constructs a type new `T` every time
|
| 154 |
+
* you try to get `T` for it (see `IListRef<at::OptinalTensorRef>`).
|
| 155 |
+
*/
|
| 156 |
+
|
| 157 |
+
namespace c10 {
|
| 158 |
+
template <typename T>
|
| 159 |
+
class IListRef;
|
| 160 |
+
|
| 161 |
+
/*
|
| 162 |
+
* Applies arbitrary macros to each `IListRefTag`.
|
| 163 |
+
*/
|
| 164 |
+
#define TORCH_ILISTREF_FORALL_TAGS(_, ...) \
|
| 165 |
+
_(Unboxed, ##__VA_ARGS__) \
|
| 166 |
+
_(Boxed, ##__VA_ARGS__) \
|
| 167 |
+
_(Materialized, ##__VA_ARGS__)
|
| 168 |
+
|
| 169 |
+
/*
|
| 170 |
+
* Defines a "switch-case" for `TAG`. Inside, it executes `BODY`,
|
| 171 |
+
* while bringing to scope:
|
| 172 |
+
*
|
| 173 |
+
* - `ImplT`: the implementation class for `TAG`
|
| 174 |
+
* - `this_`: the result of unwrapping `this`
|
| 175 |
+
*/
|
| 176 |
+
#define TORCH_ILISTREF_UNWRAP_CASE(TAG, BODY) \
|
| 177 |
+
case c10::IListRefTag::TAG: { \
|
| 178 |
+
using ImplT = c10::detail::IListRefTagImpl<IListRefTag::TAG, T>; \
|
| 179 |
+
auto& this_ = ImplT::unwrap(*this); \
|
| 180 |
+
BODY \
|
| 181 |
+
} break;
|
| 182 |
+
|
| 183 |
+
/*
|
| 184 |
+
* Dispatches the unwrap call, depending on `TAG`, followed by
|
| 185 |
+
* the execution of `BODY`. It aborts if `TAG` is not a `IListRefTag`.
|
| 186 |
+
*
|
| 187 |
+
* This macro is useful because it allows us to handle different
|
| 188 |
+
* types (that correspond to different tags) to be implemented
|
| 189 |
+
* only once. We can do it even when the implementation of the
|
| 190 |
+
* different tags aren't syntactically the same, by dispatching
|
| 191 |
+
* it to a function (e.g. `ImplT::<dispatch-function>(this_)`).
|
| 192 |
+
*/
|
| 193 |
+
#define TORCH_ILISTREF_UNWRAP(TAG, BODY) \
|
| 194 |
+
C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED("-Wswitch-enum") \
|
| 195 |
+
switch (TAG) { \
|
| 196 |
+
TORCH_ILISTREF_FORALL_TAGS(TORCH_ILISTREF_UNWRAP_CASE, BODY) \
|
| 197 |
+
break; \
|
| 198 |
+
default: \
|
| 199 |
+
TORCH_INTERNAL_ASSERT(false, "invalid IListRef tag."); \
|
| 200 |
+
} \
|
| 201 |
+
C10_DIAGNOSTIC_POP()
|
| 202 |
+
|
| 203 |
+
enum class IListRefTag {
|
| 204 |
+
#define DEFINE_TAG(tag, ...) tag,
|
| 205 |
+
TORCH_ILISTREF_FORALL_TAGS(DEFINE_TAG)
|
| 206 |
+
#undef DEFINE_TAG
|
| 207 |
+
None
|
| 208 |
+
};
|
| 209 |
+
|
| 210 |
+
namespace detail {
|
| 211 |
+
/*
|
| 212 |
+
* Type alias that specifies whether we return a reference or a copy of `T`.
|
| 213 |
+
*
|
| 214 |
+
* What is this for?
|
| 215 |
+
* =================
|
| 216 |
+
* Since values in the boxed world are represented by an `IValue`, we also
|
| 217 |
+
* depend on whether it can be converted to a const-reference (`Tensor`) or
|
| 218 |
+
* has to create a new copy of `T` (`OptionalTensorRef`).
|
| 219 |
+
*/
|
| 220 |
+
template <typename T>
|
| 221 |
+
using IListRefConstRef = typename ivalue_to_const_ref_overload_return<T>::type;
|
| 222 |
+
|
| 223 |
+
/*
|
| 224 |
+
* Interface that implements key functions for each `IListRefTag` type.
|
| 225 |
+
*
|
| 226 |
+
* What is this for?
|
| 227 |
+
* =================
|
| 228 |
+
* Given an `IListRef(Iterator)<T>`, some methods have to be implemented
|
| 229 |
+
* differently for each `TAG`. Therefore, the methods inside this class
|
| 230 |
+
* are used as dispatch targets for the different `IListRefTag` values.
|
| 231 |
+
*
|
| 232 |
+
* You should create an specialization of this class for each possible
|
| 233 |
+
* combination of `IListRefTag` type (except `None`) and element types
|
| 234 |
+
* (e.g. `Tensor`).
|
| 235 |
+
*
|
| 236 |
+
* What does it do?
|
| 237 |
+
* ================
|
| 238 |
+
* 1. defines static methods to be used as dispatch targets by both
|
| 239 |
+
* `IListRef<T>` and `IListRefIterator<T>` (see the implementation of
|
| 240 |
+
* `IListRefTagImplBase`).
|
| 241 |
+
*
|
| 242 |
+
* 2. defines the `elem_type` and `list_type` aliases that will be
|
| 243 |
+
* used in the definition of `IListRef<T>`. In general, we should do
|
| 244 |
+
* so by inheriting from `IListRefTagImplBase<TAG, T, ListElemT>`.
|
| 245 |
+
*
|
| 246 |
+
* [Note: IListRefTagImpl Specialization]
|
| 247 |
+
* ======================================
|
| 248 |
+
* For `IListRef(Iterator)<at::Tensor>`:
|
| 249 |
+
* - <IListRefTag::Unboxed, at::Tensor>
|
| 250 |
+
* - <IListRefTag::Boxed, at::Tensor>
|
| 251 |
+
* - <IListRefTag::Materialized, at::Tensor>
|
| 252 |
+
*
|
| 253 |
+
* For `IListRef(Iterator)<at::OptionalTensorRef>`:
|
| 254 |
+
* - <IListRefTag::Unboxed, at::OptionalTensorRef>
|
| 255 |
+
* - <IListRefTag::Boxed, at::OptionalTensorRef>
|
| 256 |
+
* - <IListRefTag::Materialized, at::OptionalTensorRef>
|
| 257 |
+
*/
|
| 258 |
+
template <IListRefTag TAG, typename T>
|
| 259 |
+
class IListRefTagImpl {};
|
| 260 |
+
|
| 261 |
+
/*
|
| 262 |
+
* Base implementation of `IListRefTagImpl<TAG, T>` methods.
|
| 263 |
+
*
|
| 264 |
+
* What is this for?
|
| 265 |
+
* =================
|
| 266 |
+
* This should make adding specializations for new types easier. For
|
| 267 |
+
* example, one should be able to add a new type just by making its
|
| 268 |
+
* `IListRefTagImpl` specialization inherit from `IListRefTagImplBase`.
|
| 269 |
+
*
|
| 270 |
+
* You should create a partial specialization for this class only if
|
| 271 |
+
* you introduce a new `IListRefTag`. The idea being that there is one
|
| 272 |
+
* default implementation for each possible value of `IListRefTag`.
|
| 273 |
+
*
|
| 274 |
+
* What does it do?
|
| 275 |
+
* ================
|
| 276 |
+
* 1. defines `elem_type` as an alias to `ListElemT`.
|
| 277 |
+
*
|
| 278 |
+
* 1. defines `list_type` as an alias to the default container type
|
| 279 |
+
* that will hold a collection of `elem_type`. The idea being that
|
| 280 |
+
* all types tagged as `TAG` will have `list_type` as its container,
|
| 281 |
+
* with different `elem_type`.
|
| 282 |
+
*
|
| 283 |
+
* 3. defines the default implementation for each of the methods that
|
| 284 |
+
* are supposed to be defined on `IListRefTagImpl` specializations.
|
| 285 |
+
*
|
| 286 |
+
* 4. inheriting from `IListRefTagImplBase<TAG, T, ListElemT>` also means
|
| 287 |
+
* that the payload of the type `IListRef<T>` will be of type `list_type`
|
| 288 |
+
* when it is tagged as `TAG`.
|
| 289 |
+
*/
|
| 290 |
+
template <IListRefTag TAG, typename T, typename ListElemT = T>
|
| 291 |
+
class IListRefTagImplBase {};
|
| 292 |
+
|
| 293 |
+
/*
|
| 294 |
+
* Materialized container for `IListRef<T>`.
|
| 295 |
+
*
|
| 296 |
+
* What is this for?
|
| 297 |
+
* =================
|
| 298 |
+
* Container that groups `T` references together. This exchanges the
|
| 299 |
+
* overhead of every method call from `IListRef<T>` for a dynamic allocation.
|
| 300 |
+
*
|
| 301 |
+
* You should use this container instead of `IListRef<T>` if:
|
| 302 |
+
*
|
| 303 |
+
* - You are going to iterate the list more than once
|
| 304 |
+
* - You need to repeatedly access arbitrary elements (using `operator[]`)
|
| 305 |
+
* What does it do?
|
| 306 |
+
|
| 307 |
+
* ================
|
| 308 |
+
* Removes the reference (&) from the type, and wraps it into a
|
| 309 |
+
* `std::reference_wrapper`. If `IListRefConstRef<T>` is not a
|
| 310 |
+
* reference type, then it's left unchanged.
|
| 311 |
+
*/
|
| 312 |
+
template <typename T>
|
| 313 |
+
using _MaterializedIListRefElem = std::conditional_t<
|
| 314 |
+
std::is_reference_v<T>,
|
| 315 |
+
typename std::reference_wrapper<std::remove_reference_t<T>>,
|
| 316 |
+
T>;
|
| 317 |
+
|
| 318 |
+
template <typename T>
|
| 319 |
+
using MaterializedIListRefElem = _MaterializedIListRefElem<IListRefConstRef<T>>;
|
| 320 |
+
|
| 321 |
+
template <typename T>
|
| 322 |
+
using MaterializedIListRef = std::vector<MaterializedIListRefElem<T>>;
|
| 323 |
+
|
| 324 |
+
} // namespace detail
|
| 325 |
+
|
| 326 |
+
/*
|
| 327 |
+
* Iterator for `IListRef<T>`.
|
| 328 |
+
*
|
| 329 |
+
* What is it?
|
| 330 |
+
* ===========
|
| 331 |
+
* Currently, a `std::bidirectional_iterator` that wraps the iterator
|
| 332 |
+
* types defined for each of the `IListRefTag`.
|
| 333 |
+
*
|
| 334 |
+
* One should be able to use it, as if it were the unwrapped
|
| 335 |
+
* iterators themselves.
|
| 336 |
+
|
| 337 |
+
* What does it do?
|
| 338 |
+
* ================
|
| 339 |
+
* Similarly to `IListRef<T>`, this is a wrapper class. Specifically, it
|
| 340 |
+
* wraps each container's `const_iterator` type alias. So, for example,
|
| 341 |
+
* given that the container for `IListRefTag::Boxed` is `c10::List`, this
|
| 342 |
+
* iterator will wrap a `c10::List::const_iterator`.
|
| 343 |
+
*
|
| 344 |
+
* [Note: MSVC Iterator Debug]
|
| 345 |
+
* ===========================
|
| 346 |
+
* MSVC `vector<T>::iterator` implementation (used in the boxed variant)
|
| 347 |
+
* makes it so this union's destructor, copy-constructor (assignment), and
|
| 348 |
+
* move-constructor (assignment) are implicitly deleted.
|
| 349 |
+
*
|
| 350 |
+
* Therefore, we need to explicitly define them as needed. Follows a list
|
| 351 |
+
* of places where these are needed and their reason:
|
| 352 |
+
*
|
| 353 |
+
* - `Payload` destructor:
|
| 354 |
+
* it is deleted only if the macro `_ITERATOR_DEBUG_LEVEL` is set to 2.
|
| 355 |
+
*
|
| 356 |
+
* - `IListRefIterator` destructor:
|
| 357 |
+
* same as above. However, we need to explicitly call the variant
|
| 358 |
+
* destructor explicitly.
|
| 359 |
+
*
|
| 360 |
+
* - `IListRefIterator` copy-constructor:
|
| 361 |
+
* it is deleted only if the macro `_ITERATOR_DEBUG_LEVEL` is different
|
| 362 |
+
* than 0.
|
| 363 |
+
*/
|
| 364 |
+
template <typename T>
|
| 365 |
+
class IListRefIterator {
|
| 366 |
+
private:
|
| 367 |
+
#define DEFINE_FRIEND_CLASS(TAG, ...) \
|
| 368 |
+
friend class detail::IListRefTagImpl<IListRefTag::TAG, T>; \
|
| 369 |
+
friend class detail::IListRefTagImplBase< \
|
| 370 |
+
IListRefTag::TAG, \
|
| 371 |
+
T, \
|
| 372 |
+
typename detail::IListRefTagImpl<IListRefTag::TAG, T>::elem_type>;
|
| 373 |
+
TORCH_ILISTREF_FORALL_TAGS(DEFINE_FRIEND_CLASS)
|
| 374 |
+
#undef DEFINE_FRIEND_CLASS
|
| 375 |
+
|
| 376 |
+
public:
|
| 377 |
+
// C++17 friendly std::iterator implementation
|
| 378 |
+
using iterator_category = std::bidirectional_iterator_tag;
|
| 379 |
+
using value_type = T;
|
| 380 |
+
using difference_type = std::ptrdiff_t;
|
| 381 |
+
using pointer = T*;
|
| 382 |
+
using reference = T&;
|
| 383 |
+
|
| 384 |
+
using unboxed_iterator_type = typename detail::
|
| 385 |
+
IListRefTagImpl<IListRefTag::Unboxed, T>::list_type::const_iterator;
|
| 386 |
+
using boxed_iterator_type = typename detail::
|
| 387 |
+
IListRefTagImpl<IListRefTag::Boxed, T>::list_type::const_iterator;
|
| 388 |
+
using materialized_iterator_type =
|
| 389 |
+
typename detail::MaterializedIListRef<T>::const_iterator;
|
| 390 |
+
|
| 391 |
+
IListRefIterator() : tag_(IListRefTag::None) {}
|
| 392 |
+
|
| 393 |
+
#if defined(_MSC_VER) && _ITERATOR_DEBUG_LEVEL != 0
|
| 394 |
+
// See [Note: MSVC Iterator Debug]
|
| 395 |
+
IListRefIterator(const IListRefIterator& iterator)
|
| 396 |
+
: tag_(iterator.tag_) {
|
| 397 |
+
switch (tag_) {
|
| 398 |
+
case IListRefTag::Boxed:
|
| 399 |
+
payload_.boxed_iterator = iterator.payload_.boxed_iterator;
|
| 400 |
+
break;
|
| 401 |
+
case IListRefTag::Unboxed:
|
| 402 |
+
payload_.unboxed_iterator = iterator.payload_.unboxed_iterator;
|
| 403 |
+
break;
|
| 404 |
+
case IListRefTag::Materialized:
|
| 405 |
+
payload_.materialized_iterator = iterator.payload_.materialized_iterator;
|
| 406 |
+
break;
|
| 407 |
+
default:
|
| 408 |
+
TORCH_INTERNAL_ASSERT(false, "invalid IListRef tag.");
|
| 409 |
+
}
|
| 410 |
+
}
|
| 411 |
+
#endif
|
| 412 |
+
|
| 413 |
+
#if defined(_MSC_VER) && _ITERATOR_DEBUG_LEVEL == 2
|
| 414 |
+
// See [Note: MSVC Iterator Debug]
|
| 415 |
+
~IListRefIterator() noexcept(false) {
|
| 416 |
+
switch (tag_) {
|
| 417 |
+
case IListRefTag::Boxed:
|
| 418 |
+
payload_.boxed_iterator.~boxed_iterator_type();
|
| 419 |
+
break;
|
| 420 |
+
case IListRefTag::Unboxed:
|
| 421 |
+
payload_.unboxed_iterator.~unboxed_iterator_type();
|
| 422 |
+
break;
|
| 423 |
+
case IListRefTag::Materialized:
|
| 424 |
+
payload_.materialized_iterator.~materialized_iterator_type();
|
| 425 |
+
break;
|
| 426 |
+
default:
|
| 427 |
+
TORCH_INTERNAL_ASSERT(false, "invalid IListRef tag.");
|
| 428 |
+
}
|
| 429 |
+
}
|
| 430 |
+
#endif
|
| 431 |
+
|
| 432 |
+
IListRefIterator(boxed_iterator_type boxed) : tag_(IListRefTag::Boxed) {
|
| 433 |
+
payload_.boxed_iterator = boxed;
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
IListRefIterator(unboxed_iterator_type unboxed) : tag_(IListRefTag::Unboxed) {
|
| 437 |
+
payload_.unboxed_iterator = unboxed;
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
IListRefIterator(materialized_iterator_type materialized) : tag_(IListRefTag::Materialized) {
|
| 441 |
+
payload_.materialized_iterator = materialized;
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
detail::IListRefConstRef<T> operator*() const {
|
| 445 |
+
TORCH_ILISTREF_UNWRAP(tag_, { return ImplT::iterator_get(this_); });
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
IListRefIterator& operator++() {
|
| 449 |
+
TORCH_ILISTREF_UNWRAP(tag_, { ++this_; });
|
| 450 |
+
return *this;
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
IListRefIterator operator++(int) {
|
| 454 |
+
auto old = *this;
|
| 455 |
+
TORCH_ILISTREF_UNWRAP(tag_, { ++this_; });
|
| 456 |
+
return old;
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
IListRefIterator& operator--() {
|
| 460 |
+
TORCH_ILISTREF_UNWRAP(tag_, { --this_; });
|
| 461 |
+
return *this;
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
IListRefIterator operator--(int) {
|
| 465 |
+
auto old = *this;
|
| 466 |
+
TORCH_ILISTREF_UNWRAP(tag_, { --this_; });
|
| 467 |
+
return old;
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
bool operator==(const IListRefIterator& rhs) const {
|
| 471 |
+
if (tag_ != rhs.tag_) {
|
| 472 |
+
return false;
|
| 473 |
+
}
|
| 474 |
+
TORCH_ILISTREF_UNWRAP(tag_, {
|
| 475 |
+
auto& rhs_it = ImplT::unwrap(rhs);
|
| 476 |
+
return this_ == rhs_it;
|
| 477 |
+
});
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
bool operator!=(const IListRefIterator& rhs) const {
|
| 481 |
+
return !(*this == rhs);
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
private:
|
| 485 |
+
union Payload {
|
| 486 |
+
boxed_iterator_type boxed_iterator;
|
| 487 |
+
unboxed_iterator_type unboxed_iterator;
|
| 488 |
+
materialized_iterator_type materialized_iterator;
|
| 489 |
+
void* _init_ptr;
|
| 490 |
+
Payload() : _init_ptr(nullptr) {}
|
| 491 |
+
#if defined(_MSC_VER)
|
| 492 |
+
// See [Note: MSVC Iterator Debug]
|
| 493 |
+
~Payload() {}
|
| 494 |
+
#endif
|
| 495 |
+
};
|
| 496 |
+
|
| 497 |
+
Payload payload_;
|
| 498 |
+
IListRefTag tag_;
|
| 499 |
+
};
|
| 500 |
+
|
| 501 |
+
/*
|
| 502 |
+
* See [Note: IListRef]
|
| 503 |
+
*/
|
| 504 |
+
template <typename T>
|
| 505 |
+
class IListRef {
|
| 506 |
+
private:
|
| 507 |
+
#define DEFINE_FRIEND_CLASS(TAG, ...) \
|
| 508 |
+
friend class detail::IListRefTagImpl<IListRefTag::TAG, T>; \
|
| 509 |
+
friend class detail::IListRefTagImplBase< \
|
| 510 |
+
IListRefTag::TAG, \
|
| 511 |
+
T, \
|
| 512 |
+
typename detail::IListRefTagImpl<IListRefTag::TAG, T>::elem_type>;
|
| 513 |
+
TORCH_ILISTREF_FORALL_TAGS(DEFINE_FRIEND_CLASS)
|
| 514 |
+
#undef DEFINE_FRIEND_CLASS
|
| 515 |
+
|
| 516 |
+
public:
|
| 517 |
+
using unboxed_type =
|
| 518 |
+
typename detail::IListRefTagImpl<IListRefTag::Unboxed, T>::list_type;
|
| 519 |
+
using boxed_type =
|
| 520 |
+
typename detail::IListRefTagImpl<IListRefTag::Boxed, T>::list_type;
|
| 521 |
+
using materialized_type =
|
| 522 |
+
typename detail::MaterializedIListRef<T>;
|
| 523 |
+
|
| 524 |
+
using iterator = IListRefIterator<T>;
|
| 525 |
+
using const_iterator = IListRefIterator<T>;
|
| 526 |
+
using reverse_iterator = std::reverse_iterator<iterator>;
|
| 527 |
+
using value_type = typename iterator::value_type;
|
| 528 |
+
|
| 529 |
+
IListRef() : tag_(IListRefTag::None) {}
|
| 530 |
+
|
| 531 |
+
IListRef(const boxed_type& boxed) : tag_(IListRefTag::Boxed) {
|
| 532 |
+
payload_.boxed = &boxed;
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
+
IListRef(const unboxed_type& unboxed) : tag_(IListRefTag::Unboxed) {
|
| 536 |
+
payload_.unboxed = unboxed;
|
| 537 |
+
}
|
| 538 |
+
|
| 539 |
+
IListRef(const std::initializer_list<T>& list) : tag_(IListRefTag::Unboxed) {
|
| 540 |
+
payload_.unboxed = at::ArrayRef<T>(list);
|
| 541 |
+
}
|
| 542 |
+
|
| 543 |
+
template <
|
| 544 |
+
typename... UnboxedConstructorArgs,
|
| 545 |
+
typename = std::enable_if_t<
|
| 546 |
+
std::is_constructible_v<unboxed_type, UnboxedConstructorArgs...>>>
|
| 547 |
+
IListRef(UnboxedConstructorArgs&&... args) : tag_(IListRefTag::Unboxed) {
|
| 548 |
+
payload_.unboxed = unboxed_type(std::forward<UnboxedConstructorArgs>(args)...);
|
| 549 |
+
}
|
| 550 |
+
|
| 551 |
+
IListRef(const materialized_type& materialized) : tag_(IListRefTag::Materialized) {
|
| 552 |
+
payload_.materialized = &materialized;
|
| 553 |
+
}
|
| 554 |
+
|
| 555 |
+
size_t size() const {
|
| 556 |
+
TORCH_ILISTREF_UNWRAP(tag_, { return this_.size(); });
|
| 557 |
+
}
|
| 558 |
+
|
| 559 |
+
bool empty() const {
|
| 560 |
+
return size() == 0;
|
| 561 |
+
}
|
| 562 |
+
|
| 563 |
+
iterator begin() const {
|
| 564 |
+
TORCH_ILISTREF_UNWRAP(tag_, { return this_.begin(); });
|
| 565 |
+
}
|
| 566 |
+
|
| 567 |
+
iterator end() const {
|
| 568 |
+
TORCH_ILISTREF_UNWRAP(tag_, { return this_.end(); });
|
| 569 |
+
}
|
| 570 |
+
|
| 571 |
+
detail::IListRefConstRef<T> front() const {
|
| 572 |
+
TORCH_ILISTREF_UNWRAP(tag_, { return ImplT::front(this_); });
|
| 573 |
+
}
|
| 574 |
+
|
| 575 |
+
/*
|
| 576 |
+
* Materializes the `IListRef` into a `std::vector`.
|
| 577 |
+
*
|
| 578 |
+
* This should be used when one wishes to either:
|
| 579 |
+
*
|
| 580 |
+
* - iterate over the list more than once: each `IListRefIterator`
|
| 581 |
+
* member function call has to go through a switch, introducing
|
| 582 |
+
* non-negligible overhead
|
| 583 |
+
*
|
| 584 |
+
* - randomly access an arbitrary element using `operator[]`:
|
| 585 |
+
* same reason as above
|
| 586 |
+
*/
|
| 587 |
+
detail::MaterializedIListRef<T> materialize() const {
|
| 588 |
+
if (isMaterialized()) {
|
| 589 |
+
return toMaterialized();
|
| 590 |
+
}
|
| 591 |
+
|
| 592 |
+
detail::MaterializedIListRef<T> materialized;
|
| 593 |
+
materialized.reserve(size());
|
| 594 |
+
for (const auto& t : *this) {
|
| 595 |
+
materialized.emplace_back(t);
|
| 596 |
+
}
|
| 597 |
+
return materialized;
|
| 598 |
+
}
|
| 599 |
+
|
| 600 |
+
#define DEFINE_CHECK(TAG, ...) \
|
| 601 |
+
bool is##TAG() const { \
|
| 602 |
+
return tag_ == IListRefTag::TAG; \
|
| 603 |
+
}
|
| 604 |
+
TORCH_ILISTREF_FORALL_TAGS(DEFINE_CHECK)
|
| 605 |
+
#undef DEFINE_CHECK
|
| 606 |
+
|
| 607 |
+
bool isNone() const {
|
| 608 |
+
return tag_ == IListRefTag::None;
|
| 609 |
+
}
|
| 610 |
+
|
| 611 |
+
#define DEFINE_CASTING(TAG, ...) \
|
| 612 |
+
const typename detail::IListRefTagImpl<IListRefTag::TAG, T>::list_type& \
|
| 613 |
+
to##TAG() const { \
|
| 614 |
+
TORCH_INTERNAL_ASSERT(is##TAG()); \
|
| 615 |
+
return detail::IListRefTagImpl<IListRefTag::TAG, T>::unwrap(*this); \
|
| 616 |
+
}
|
| 617 |
+
TORCH_ILISTREF_FORALL_TAGS(DEFINE_CASTING)
|
| 618 |
+
#undef DEFINE_CASTING
|
| 619 |
+
|
| 620 |
+
private:
|
| 621 |
+
union Payload {
|
| 622 |
+
const boxed_type* boxed;
|
| 623 |
+
unboxed_type unboxed;
|
| 624 |
+
const materialized_type* materialized;
|
| 625 |
+
Payload() : boxed(nullptr) {}
|
| 626 |
+
};
|
| 627 |
+
|
| 628 |
+
Payload payload_;
|
| 629 |
+
IListRefTag tag_;
|
| 630 |
+
};
|
| 631 |
+
|
| 632 |
+
} // namespace c10
|
| 633 |
+
|
| 634 |
+
#include <ATen/core/IListRef_inl.h>
|
| 635 |
+
|
| 636 |
+
#else
|
| 637 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 638 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/IListRef_inl.h
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/List.h>
|
| 5 |
+
#include <ATen/core/Tensor.h>
|
| 6 |
+
|
| 7 |
+
namespace at {
|
| 8 |
+
class Tensor;
|
| 9 |
+
class OptionalTensorRef;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
namespace c10::detail {
|
| 14 |
+
|
| 15 |
+
/*
|
| 16 |
+
* Specializations of `IListRefTagImplBase` that implement the default
|
| 17 |
+
* implementation for `IListRefTag::Unboxed`.
|
| 18 |
+
*/
|
| 19 |
+
template <typename T, typename ListElemT>
|
| 20 |
+
class IListRefTagImplBase<IListRefTag::Unboxed, T, ListElemT> {
|
| 21 |
+
public:
|
| 22 |
+
using elem_type = ListElemT;
|
| 23 |
+
using list_type = ArrayRef<elem_type>;
|
| 24 |
+
|
| 25 |
+
/*
|
| 26 |
+
* These `unwrap` static methods unwraps the inner containers out
|
| 27 |
+
* of `IListRef<T>` (and `IListRefIterator<T>`). They are required when
|
| 28 |
+
* the macro `TORCH_ILISTREF_UNWRAP` is called.
|
| 29 |
+
*/
|
| 30 |
+
static const list_type& unwrap(const IListRef<T>& ilist) {
|
| 31 |
+
return ilist.payload_.unboxed;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
static typename list_type::const_iterator& unwrap(IListRefIterator<T>& it) {
|
| 35 |
+
return it.payload_.unboxed_iterator;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
static const typename list_type::const_iterator& unwrap(
|
| 39 |
+
const IListRefIterator<T>& it) {
|
| 40 |
+
return it.payload_.unboxed_iterator;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
/*
|
| 44 |
+
* We have these function (besides the `unwrap`s above) because the
|
| 45 |
+
* implementation for both `IListRef::operator[]` and `IListRefIterator::operator*`
|
| 46 |
+
* weren't syntactically equal for the existing tags at the time
|
| 47 |
+
* (`Unboxed` and `Boxed`).
|
| 48 |
+
*/
|
| 49 |
+
static IListRefConstRef<T> front(const list_type& lst) {
|
| 50 |
+
return lst.front();
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
static IListRefConstRef<T> iterator_get(
|
| 54 |
+
const typename list_type::const_iterator& it) {
|
| 55 |
+
return *it;
|
| 56 |
+
}
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
/*
|
| 60 |
+
* Specializations of `IListRefTagImplBase` that implement the default
|
| 61 |
+
* implementation for `IListRefTag::Boxed`.
|
| 62 |
+
*/
|
| 63 |
+
template <typename T, typename ListElemT>
|
| 64 |
+
class IListRefTagImplBase<IListRefTag::Boxed, T, ListElemT> {
|
| 65 |
+
public:
|
| 66 |
+
using elem_type = ListElemT;
|
| 67 |
+
using list_type = List<elem_type>;
|
| 68 |
+
|
| 69 |
+
static const list_type& unwrap(const IListRef<T>& ilist) {
|
| 70 |
+
return *ilist.payload_.boxed;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
static typename list_type::const_iterator& unwrap(IListRefIterator<T>& it) {
|
| 74 |
+
return it.payload_.boxed_iterator;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
static const typename list_type::const_iterator& unwrap(
|
| 78 |
+
const IListRefIterator<T>& it) {
|
| 79 |
+
return it.payload_.boxed_iterator;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
static IListRefConstRef<T> front(const list_type& lst) {
|
| 83 |
+
return lst[0];
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
static IListRefConstRef<T> iterator_get(
|
| 87 |
+
const typename list_type::const_iterator& it) {
|
| 88 |
+
return (*it).get().toTensor();
|
| 89 |
+
}
|
| 90 |
+
};
|
| 91 |
+
|
| 92 |
+
/*
|
| 93 |
+
* Specializations of `IListRefTagImplBase` that implement the default
|
| 94 |
+
* implementation for `IListRefTag::Materialized`.
|
| 95 |
+
*/
|
| 96 |
+
template <typename T>
|
| 97 |
+
class IListRefTagImplBase<IListRefTag::Materialized, T, MaterializedIListRefElem<T>> {
|
| 98 |
+
public:
|
| 99 |
+
using elem_type = MaterializedIListRefElem<T>;
|
| 100 |
+
using list_type = MaterializedIListRef<T>;
|
| 101 |
+
|
| 102 |
+
static const list_type& unwrap(const IListRef<T>& ilist) {
|
| 103 |
+
return *ilist.payload_.materialized;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
static typename list_type::const_iterator& unwrap(IListRefIterator<T>& it) {
|
| 107 |
+
return it.payload_.materialized_iterator;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
static const typename list_type::const_iterator& unwrap(
|
| 111 |
+
const IListRefIterator<T>& it) {
|
| 112 |
+
return it.payload_.materialized_iterator;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
static IListRefConstRef<T> front(const list_type& lst) {
|
| 116 |
+
return lst[0];
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
static IListRefConstRef<T> iterator_get(
|
| 120 |
+
const typename list_type::const_iterator& it) {
|
| 121 |
+
return *it;
|
| 122 |
+
}
|
| 123 |
+
};
|
| 124 |
+
|
| 125 |
+
/*
|
| 126 |
+
* [Note: ITensorListRef]
|
| 127 |
+
* Specializations necessary for `IListRef<at::Tensor>` type.
|
| 128 |
+
*
|
| 129 |
+
* Since the default implementations are usually done with supporting
|
| 130 |
+
* `Tensor` in mind, we only have to inherit from the base implementations.
|
| 131 |
+
*/
|
| 132 |
+
template <>
|
| 133 |
+
class IListRefTagImpl<IListRefTag::Unboxed, at::Tensor>
|
| 134 |
+
: public IListRefTagImplBase<IListRefTag::Unboxed, at::Tensor> {};
|
| 135 |
+
|
| 136 |
+
template <>
|
| 137 |
+
class IListRefTagImpl<IListRefTag::Boxed, at::Tensor>
|
| 138 |
+
: public IListRefTagImplBase<IListRefTag::Boxed, at::Tensor> {};
|
| 139 |
+
|
| 140 |
+
template <>
|
| 141 |
+
class IListRefTagImpl<IListRefTag::Materialized, at::Tensor>
|
| 142 |
+
: public IListRefTagImplBase<
|
| 143 |
+
IListRefTag::Materialized,
|
| 144 |
+
at::Tensor,
|
| 145 |
+
MaterializedIListRefElem<at::Tensor>> {};
|
| 146 |
+
|
| 147 |
+
/*
|
| 148 |
+
* [Note: IOptTensorListRef]
|
| 149 |
+
* Specializations necessary for `IListRef<at::OptionalTensorRef>` type.
|
| 150 |
+
*
|
| 151 |
+
* We can't get an `at::OptionalTensorRef` directly from an instance of
|
| 152 |
+
* `List<optional<Tensor>>` (the type that corresponds to the boxed world).
|
| 153 |
+
*
|
| 154 |
+
* So, the default implementation won't help us. Thus, we have to implement
|
| 155 |
+
* this method ourselves.
|
| 156 |
+
*/
|
| 157 |
+
template <>
|
| 158 |
+
class IListRefTagImpl<IListRefTag::Unboxed, at::OptionalTensorRef>
|
| 159 |
+
: public IListRefTagImplBase<IListRefTag::Unboxed, at::OptionalTensorRef> {};
|
| 160 |
+
|
| 161 |
+
template <>
|
| 162 |
+
class IListRefTagImpl<IListRefTag::Boxed, at::OptionalTensorRef>
|
| 163 |
+
: public IListRefTagImplBase<IListRefTag::Boxed, at::OptionalTensorRef, std::optional<at::Tensor>> {
|
| 164 |
+
|
| 165 |
+
public:
|
| 166 |
+
/*
|
| 167 |
+
* Given an instance of the types corresponding to the `Boxed` tag, we override
|
| 168 |
+
* the default implementation, so that we can return a `at::OptionalTensorRef`.
|
| 169 |
+
*/
|
| 170 |
+
static IListRefConstRef<at::OptionalTensorRef> iterator_get(
|
| 171 |
+
const typename list_type::const_iterator& it) {
|
| 172 |
+
C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED("-Wdangling-reference")
|
| 173 |
+
const auto& ivalue = (*it).get();
|
| 174 |
+
C10_DIAGNOSTIC_POP()
|
| 175 |
+
if (!ivalue.isNone()) {
|
| 176 |
+
const auto& tensor = ivalue.toTensor();
|
| 177 |
+
return (tensor.defined()) ? tensor : at::OptionalTensorRef{};
|
| 178 |
+
}
|
| 179 |
+
return {};
|
| 180 |
+
}
|
| 181 |
+
};
|
| 182 |
+
|
| 183 |
+
template <>
|
| 184 |
+
class IListRefTagImpl<IListRefTag::Materialized, at::OptionalTensorRef>
|
| 185 |
+
: public IListRefTagImplBase<
|
| 186 |
+
IListRefTag::Materialized,
|
| 187 |
+
at::OptionalTensorRef,
|
| 188 |
+
MaterializedIListRefElem<at::OptionalTensorRef>> {};
|
| 189 |
+
|
| 190 |
+
} // namespace c10::detail
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
namespace at {
|
| 194 |
+
|
| 195 |
+
// [Note: ITensorListRef]
|
| 196 |
+
using ITensorListRef = c10::IListRef<at::Tensor>;
|
| 197 |
+
using ITensorListRefIterator = c10::IListRefIterator<at::Tensor>;
|
| 198 |
+
using MaterializedITensorListRef = c10::detail::MaterializedIListRef<at::Tensor>;
|
| 199 |
+
// [Note: IOptTensorListRef]
|
| 200 |
+
using IOptTensorListRef = c10::IListRef<at::OptionalTensorRef>;
|
| 201 |
+
using IOptTensorListRefIterator = c10::IListRefIterator<at::OptionalTensorRef>;
|
| 202 |
+
using MaterializedIOptTensorListRef = c10::detail::MaterializedIListRef<at::OptionalTensorRef>;
|
| 203 |
+
|
| 204 |
+
} // namespace at
|
| 205 |
+
|
| 206 |
+
#else
|
| 207 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 208 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/LegacyTypeDispatch.h
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
// The legacy mechanism for dispatching operators in ATen is a Type
|
| 5 |
+
// object, which is essentially a giant virtual dispatch table
|
| 6 |
+
// for every operation we support dynamically dispatching over.
|
| 7 |
+
//
|
| 8 |
+
// This has been deprecated in favor of ATenDispatch, and in the future,
|
| 9 |
+
// c10 dispatcher.
|
| 10 |
+
// TODO: Clean up what remains here
|
| 11 |
+
|
| 12 |
+
#include <c10/core/impl/LocalDispatchKeySet.h>
|
| 13 |
+
|
| 14 |
+
namespace at {
|
| 15 |
+
|
| 16 |
+
// A RAII, thread local (!) guard that will disable dispatch to variable
|
| 17 |
+
// handler.
|
| 18 |
+
//
|
| 19 |
+
// NOTE [ Treating Variables as non-Variables in type dispatch ]
|
| 20 |
+
//
|
| 21 |
+
// What exactly does AutoDispatchBelowAutograd do? The short answer is, it causes
|
| 22 |
+
// dispatches on ATen functions to go to the non-variable implementation,
|
| 23 |
+
// bypassing autograd handling (and also profiling and tracing).
|
| 24 |
+
//
|
| 25 |
+
// To understand why this guard exists, it's helpful to understand the history
|
| 26 |
+
// behind how Variable was implemented. Previously, Variables were implemented
|
| 27 |
+
// as a wrapper on Tensors; so the act of processing a Variable involved
|
| 28 |
+
// unwrapping the underlying Tensor, and then calling the underlying base
|
| 29 |
+
// operation on /that/ operation
|
| 30 |
+
//
|
| 31 |
+
// However, after the Variable/Tensor merge, there is no concept of unwrapping
|
| 32 |
+
// a tensor anymore. If you just call the operation on the same variable
|
| 33 |
+
// again inside your VariableType handler, you'll dispatch back to
|
| 34 |
+
// VariableType, which is not what we want.
|
| 35 |
+
//
|
| 36 |
+
// The solution to the above problem is to add `at::AutoDispatchBelowAutograd`, which
|
| 37 |
+
// when enabled will cause `legacyTensorType()` and `getType()` to always return
|
| 38 |
+
// non-Variable type, even if the tensor being called on is a variable.
|
| 39 |
+
|
| 40 |
+
/* Note [AutoDispatchBelowAutograd]
|
| 41 |
+
* AutoDispatchBelowAutograd is **INTERNAL ONLY** that it should be used
|
| 42 |
+
* for kernel implementations and customized C++ kernels.
|
| 43 |
+
* If you are looking for a guard to run workload in inference mode, please use
|
| 44 |
+
* c10::InferenceMode RAII which is user facing API.
|
| 45 |
+
* In the past AutoDispatchBelowAutograd(or its old version AutoNonVariableTypeMode)
|
| 46 |
+
* was used in the user code for inference-only workload, this was under risk of
|
| 47 |
+
* producing wrong results silently in some edge cases. For example:
|
| 48 |
+
* ```
|
| 49 |
+
* torch::Tensor s = torch::ones({1, 2, 3}).set_requires_grad(true);
|
| 50 |
+
* torch::Tensor out = s * s;
|
| 51 |
+
* {
|
| 52 |
+
* at::AutoDispatchBelowAutograd guard;
|
| 53 |
+
* s.add_(1); // Skips version bump on `s`.
|
| 54 |
+
* }
|
| 55 |
+
* // WRONG GRADIENT! s.grad() are now computed using `s` value after the
|
| 56 |
+
* // inplace update.
|
| 57 |
+
* out.backward(torch::ones_like(out));
|
| 58 |
+
* ```
|
| 59 |
+
* Users should use `c10::InferenceMode` here so that it'll properly throw an
|
| 60 |
+
* error saying "one of the variables needed for gradient computation has be modified."
|
| 61 |
+
*/
|
| 62 |
+
struct TORCH_API AutoDispatchBelowAutograd {
|
| 63 |
+
AutoDispatchBelowAutograd() :
|
| 64 |
+
autograd_guard_(c10::autograd_dispatch_keyset) {
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// disable all autograd dispatch keys
|
| 68 |
+
c10::impl::ExcludeDispatchKeyGuard autograd_guard_;
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
// TODO: AutoNonVariableTypeMode should be removed in release 1.10.
|
| 72 |
+
struct TORCH_API AutoNonVariableTypeMode {
|
| 73 |
+
AutoNonVariableTypeMode(bool enabled = true) :
|
| 74 |
+
autograd_guard_(c10::autograd_dispatch_keyset) {
|
| 75 |
+
TORCH_WARN_ONCE("AutoNonVariableTypeMode is deprecated and will be removed in 1.10 release. "
|
| 76 |
+
"For kernel implementations please use AutoDispatchBelowADInplaceOrView instead, "
|
| 77 |
+
"If you are looking for a user facing API to enable running your inference-only "
|
| 78 |
+
"workload, please use c10::InferenceMode. Using AutoDispatchBelowADInplaceOrView in user code "
|
| 79 |
+
"is under risk of producing silent wrong result in some edge cases. "
|
| 80 |
+
"See Note [AutoDispatchBelowAutograd] for more details.");
|
| 81 |
+
TORCH_INTERNAL_ASSERT(enabled);
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// disable all autograd dispatch keys
|
| 85 |
+
c10::impl::ExcludeDispatchKeyGuard autograd_guard_;
|
| 86 |
+
};
|
| 87 |
+
|
| 88 |
+
struct TORCH_API AutoDispatchSkipFunctionalize {
|
| 89 |
+
AutoDispatchSkipFunctionalize() :
|
| 90 |
+
dispatch_key_guard_(c10::DispatchKeySet(c10::DispatchKey::Functionalize)) {
|
| 91 |
+
}
|
| 92 |
+
c10::impl::ExcludeDispatchKeyGuard dispatch_key_guard_;
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
/* Note [AutoDispatchBelowADInplaceOrView]
|
| 96 |
+
* AutoDispatchBelowADInplaceOrView is equivalent to AutoNonVariableTypeMode
|
| 97 |
+
* before we split inplace & view ops out of VariableType kernel.
|
| 98 |
+
* Note this guard is used in VariableType kernels for functional ops
|
| 99 |
+
* as well as ADInplaceOrView kernels for inplace/view ops to enforce the
|
| 100 |
+
* Invariant:
|
| 101 |
+
* Once you are in VariableType/ADInplaceOrView kernel for an op,
|
| 102 |
+
* you never go back to a kernel on same dispatch key until
|
| 103 |
+
* you finish the current op.
|
| 104 |
+
*/
|
| 105 |
+
struct TORCH_API AutoDispatchBelowADInplaceOrView {
|
| 106 |
+
AutoDispatchBelowADInplaceOrView() :
|
| 107 |
+
dispatch_key_guard_(c10::autograd_dispatch_keyset_with_ADInplaceOrView) {
|
| 108 |
+
}
|
| 109 |
+
// disable Autograd & ADInplaceOrView dispatch keys
|
| 110 |
+
c10::impl::ExcludeDispatchKeyGuard dispatch_key_guard_;
|
| 111 |
+
};
|
| 112 |
+
} // namespace at
|
| 113 |
+
|
| 114 |
+
#else
|
| 115 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 116 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/List.h
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/ivalue_to.h>
|
| 5 |
+
#include <ATen/core/jit_type_base.h>
|
| 6 |
+
#include <c10/macros/Macros.h>
|
| 7 |
+
#include <c10/macros/Export.h>
|
| 8 |
+
#include <c10/util/TypeTraits.h>
|
| 9 |
+
#include <c10/util/TypeList.h>
|
| 10 |
+
#include <c10/util/intrusive_ptr.h>
|
| 11 |
+
#include <c10/util/ArrayRef.h>
|
| 12 |
+
#include <optional>
|
| 13 |
+
#include <vector>
|
| 14 |
+
|
| 15 |
+
namespace at {
|
| 16 |
+
class Tensor;
|
| 17 |
+
}
|
| 18 |
+
namespace c10 {
|
| 19 |
+
struct IValue;
|
| 20 |
+
template<class T> class List;
|
| 21 |
+
struct Type;
|
| 22 |
+
|
| 23 |
+
namespace detail {
|
| 24 |
+
|
| 25 |
+
struct ListImpl final : public c10::intrusive_ptr_target {
|
| 26 |
+
using list_type = std::vector<IValue>;
|
| 27 |
+
|
| 28 |
+
explicit TORCH_API ListImpl(list_type list_, TypePtr elementType_);
|
| 29 |
+
|
| 30 |
+
list_type list;
|
| 31 |
+
|
| 32 |
+
TypePtr elementType;
|
| 33 |
+
|
| 34 |
+
intrusive_ptr<ListImpl> copy() const {
|
| 35 |
+
return make_intrusive<ListImpl>(list, elementType);
|
| 36 |
+
}
|
| 37 |
+
friend TORCH_API bool operator==(const ListImpl& lhs, const ListImpl& rhs);
|
| 38 |
+
};
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
namespace impl {
|
| 42 |
+
|
| 43 |
+
template<class T, class Iterator> class ListIterator;
|
| 44 |
+
|
| 45 |
+
template<class T, class Iterator> class ListElementReference;
|
| 46 |
+
|
| 47 |
+
template<class T, class Iterator>
|
| 48 |
+
void swap(ListElementReference<T, Iterator>&& lhs, ListElementReference<T, Iterator>&& rhs) noexcept;
|
| 49 |
+
|
| 50 |
+
template<class T, class Iterator>
|
| 51 |
+
bool operator==(const ListElementReference<T, Iterator>& lhs, const T& rhs);
|
| 52 |
+
|
| 53 |
+
template<class T, class Iterator>
|
| 54 |
+
bool operator==(const T& lhs, const ListElementReference<T, Iterator>& rhs);
|
| 55 |
+
|
| 56 |
+
template<class T>
|
| 57 |
+
struct ListElementConstReferenceTraits {
|
| 58 |
+
// In the general case, we use IValue::to().
|
| 59 |
+
using const_reference = typename c10::detail::ivalue_to_const_ref_overload_return<T>::type;
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
// There is no to() overload for std::optional<std::string>.
|
| 63 |
+
template<>
|
| 64 |
+
struct ListElementConstReferenceTraits<std::optional<std::string>> {
|
| 65 |
+
using const_reference = std::optional<std::reference_wrapper<const std::string>>;
|
| 66 |
+
};
|
| 67 |
+
|
| 68 |
+
template<class T, class Iterator>
|
| 69 |
+
class ListElementReference final {
|
| 70 |
+
public:
|
| 71 |
+
operator std::conditional_t<
|
| 72 |
+
std::is_reference_v<typename c10::detail::
|
| 73 |
+
ivalue_to_const_ref_overload_return<T>::type>,
|
| 74 |
+
const T&,
|
| 75 |
+
T>() const;
|
| 76 |
+
|
| 77 |
+
ListElementReference& operator=(T&& new_value) &&;
|
| 78 |
+
|
| 79 |
+
ListElementReference& operator=(const T& new_value) &&;
|
| 80 |
+
|
| 81 |
+
// assigning another ref to this assigns the underlying value
|
| 82 |
+
ListElementReference& operator=(ListElementReference&& rhs) && noexcept;
|
| 83 |
+
|
| 84 |
+
const IValue& get() const& {
|
| 85 |
+
return *iterator_;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
friend void swap<T, Iterator>(ListElementReference&& lhs, ListElementReference&& rhs) noexcept;
|
| 89 |
+
|
| 90 |
+
ListElementReference(const ListElementReference&) = delete;
|
| 91 |
+
ListElementReference& operator=(const ListElementReference&) = delete;
|
| 92 |
+
~ListElementReference() = default;
|
| 93 |
+
|
| 94 |
+
private:
|
| 95 |
+
ListElementReference(Iterator iter)
|
| 96 |
+
: iterator_(iter) {}
|
| 97 |
+
|
| 98 |
+
// allow moving, but only our friends (i.e. the List class) can move us
|
| 99 |
+
ListElementReference(ListElementReference&&) noexcept = default;
|
| 100 |
+
ListElementReference& operator=(ListElementReference&& rhs) & noexcept {
|
| 101 |
+
iterator_ = std::move(rhs.iterator_);
|
| 102 |
+
return *this;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
friend class List<T>;
|
| 106 |
+
friend class ListIterator<T, Iterator>;
|
| 107 |
+
|
| 108 |
+
Iterator iterator_;
|
| 109 |
+
};
|
| 110 |
+
|
| 111 |
+
// this wraps vector::iterator to make sure user code can't rely
|
| 112 |
+
// on it being the type of the underlying vector.
|
| 113 |
+
template <class T, class Iterator>
|
| 114 |
+
class ListIterator final {
|
| 115 |
+
public:
|
| 116 |
+
// C++17 friendly std::iterator implementation
|
| 117 |
+
using iterator_category = std::random_access_iterator_tag;
|
| 118 |
+
using value_type = T;
|
| 119 |
+
using difference_type = std::ptrdiff_t;
|
| 120 |
+
using pointer = T*;
|
| 121 |
+
using reference = ListElementReference<T, Iterator>;
|
| 122 |
+
|
| 123 |
+
explicit ListIterator() = default;
|
| 124 |
+
~ListIterator() = default;
|
| 125 |
+
|
| 126 |
+
ListIterator(const ListIterator&) = default;
|
| 127 |
+
ListIterator(ListIterator&&) noexcept = default;
|
| 128 |
+
ListIterator& operator=(const ListIterator&) = default;
|
| 129 |
+
ListIterator& operator=(ListIterator&&) noexcept = default;
|
| 130 |
+
|
| 131 |
+
ListIterator& operator++() {
|
| 132 |
+
++iterator_;
|
| 133 |
+
return *this;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
ListIterator operator++(int) {
|
| 137 |
+
ListIterator copy(*this);
|
| 138 |
+
++*this;
|
| 139 |
+
return copy;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
ListIterator& operator--() {
|
| 143 |
+
--iterator_;
|
| 144 |
+
return *this;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
ListIterator operator--(int) {
|
| 148 |
+
ListIterator copy(*this);
|
| 149 |
+
--*this;
|
| 150 |
+
return copy;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
ListIterator& operator+=(typename List<T>::size_type offset) {
|
| 154 |
+
iterator_ += offset;
|
| 155 |
+
return *this;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
ListIterator& operator-=(typename List<T>::size_type offset) {
|
| 159 |
+
iterator_ -= offset;
|
| 160 |
+
return *this;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
ListIterator operator+(typename List<T>::size_type offset) const {
|
| 164 |
+
return ListIterator{iterator_ + offset};
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
ListIterator operator-(typename List<T>::size_type offset) const {
|
| 168 |
+
return ListIterator{iterator_ - offset};
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
friend difference_type operator-(const ListIterator& lhs, const ListIterator& rhs) {
|
| 172 |
+
return lhs.iterator_ - rhs.iterator_;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
ListElementReference<T, Iterator> operator*() const {
|
| 176 |
+
return {iterator_};
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
ListElementReference<T, Iterator> operator[](typename List<T>::size_type offset) const {
|
| 180 |
+
return {iterator_ + offset};
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
private:
|
| 184 |
+
explicit ListIterator(Iterator iterator): iterator_(std::move(iterator)) {}
|
| 185 |
+
|
| 186 |
+
Iterator iterator_;
|
| 187 |
+
|
| 188 |
+
friend bool operator==(const ListIterator& lhs, const ListIterator& rhs) {
|
| 189 |
+
return lhs.iterator_ == rhs.iterator_;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
friend bool operator!=(const ListIterator& lhs, const ListIterator& rhs) {
|
| 193 |
+
return !(lhs == rhs);
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
friend bool operator<(const ListIterator& lhs, const ListIterator& rhs) {
|
| 197 |
+
return lhs.iterator_ < rhs.iterator_;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
friend bool operator<=(const ListIterator& lhs, const ListIterator& rhs) {
|
| 201 |
+
return lhs.iterator_ <= rhs.iterator_;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
friend bool operator>(const ListIterator& lhs, const ListIterator& rhs) {
|
| 205 |
+
return lhs.iterator_ > rhs.iterator_;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
friend bool operator>=(const ListIterator& lhs, const ListIterator& rhs) {
|
| 209 |
+
return lhs.iterator_ >= rhs.iterator_;
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
friend class ListIterator<T, typename c10::detail::ListImpl::list_type::iterator>;
|
| 213 |
+
friend class List<T>;
|
| 214 |
+
};
|
| 215 |
+
|
| 216 |
+
template<class T> List<T> toTypedList(List<IValue> list);
|
| 217 |
+
template<class T> List<IValue> toList(List<T>&& list);
|
| 218 |
+
template<class T> List<IValue> toList(const List<T>& list);
|
| 219 |
+
const IValue* ptr_to_first_element(const List<IValue>& list);
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
/**
|
| 223 |
+
* An object of this class stores a list of values of type T.
|
| 224 |
+
*
|
| 225 |
+
* This is a pointer type. After a copy, both Lists
|
| 226 |
+
* will share the same storage:
|
| 227 |
+
*
|
| 228 |
+
* > List<int> a;
|
| 229 |
+
* > List<int> b = a;
|
| 230 |
+
* > b.push_back("three");
|
| 231 |
+
* > ASSERT("three" == a.get(0));
|
| 232 |
+
*
|
| 233 |
+
* We use this class in the PyTorch kernel API instead of
|
| 234 |
+
* std::vector<T>, because that allows us to do optimizations
|
| 235 |
+
* and switch out the underlying list implementation without
|
| 236 |
+
* breaking backwards compatibility for the kernel API.
|
| 237 |
+
*/
|
| 238 |
+
template<class T>
|
| 239 |
+
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
|
| 240 |
+
class List final {
|
| 241 |
+
private:
|
| 242 |
+
// This is an intrusive_ptr because List is a pointer type.
|
| 243 |
+
// Invariant: This will never be a nullptr, there will always be a valid
|
| 244 |
+
// ListImpl.
|
| 245 |
+
c10::intrusive_ptr<c10::detail::ListImpl> impl_;
|
| 246 |
+
|
| 247 |
+
using internal_reference_type = impl::ListElementReference<T, typename c10::detail::ListImpl::list_type::iterator>;
|
| 248 |
+
using internal_const_reference_type = typename impl::ListElementConstReferenceTraits<T>::const_reference;
|
| 249 |
+
|
| 250 |
+
public:
|
| 251 |
+
using value_type = T;
|
| 252 |
+
using size_type = typename c10::detail::ListImpl::list_type::size_type;
|
| 253 |
+
using iterator = impl::ListIterator<T, typename c10::detail::ListImpl::list_type::iterator>;
|
| 254 |
+
using const_iterator = impl::ListIterator<T, typename c10::detail::ListImpl::list_type::iterator>;
|
| 255 |
+
using reverse_iterator = impl::ListIterator<T, typename c10::detail::ListImpl::list_type::reverse_iterator>;
|
| 256 |
+
|
| 257 |
+
/**
|
| 258 |
+
* Constructs an empty list.
|
| 259 |
+
*/
|
| 260 |
+
explicit List();
|
| 261 |
+
|
| 262 |
+
/**
|
| 263 |
+
* Constructs a list with some initial values.
|
| 264 |
+
* Example:
|
| 265 |
+
* List<int> a({2, 3, 4});
|
| 266 |
+
*/
|
| 267 |
+
List(std::initializer_list<T> initial_values);
|
| 268 |
+
explicit List(ArrayRef<T> initial_values);
|
| 269 |
+
|
| 270 |
+
/**
|
| 271 |
+
* Create a generic list with runtime type information.
|
| 272 |
+
* This only works for c10::impl::GenericList and is not part of the public API
|
| 273 |
+
* but only supposed to be used internally by PyTorch.
|
| 274 |
+
*/
|
| 275 |
+
explicit List(TypePtr elementType);
|
| 276 |
+
|
| 277 |
+
List(const List&) = default;
|
| 278 |
+
List& operator=(const List&) = default;
|
| 279 |
+
~List() = default;
|
| 280 |
+
|
| 281 |
+
/**
|
| 282 |
+
* Create a new List pointing to a deep copy of the same data.
|
| 283 |
+
* The List returned is a new list with separate storage.
|
| 284 |
+
* Changes in it are not reflected in the original list or vice versa.
|
| 285 |
+
*/
|
| 286 |
+
List copy() const;
|
| 287 |
+
|
| 288 |
+
/**
|
| 289 |
+
* Returns the element at specified location pos, with bounds checking.
|
| 290 |
+
* If pos is not within the range of the container, an exception of type std::out_of_range is thrown.
|
| 291 |
+
*/
|
| 292 |
+
internal_const_reference_type get(size_type pos) const;
|
| 293 |
+
|
| 294 |
+
/**
|
| 295 |
+
* Moves out the element at the specified location pos and returns it, with bounds checking.
|
| 296 |
+
* If pos is not within the range of the container, an exception of type std::out_of_range is thrown.
|
| 297 |
+
* The list contains an invalid element at position pos afterwards. Any operations
|
| 298 |
+
* on it before re-setting it are invalid.
|
| 299 |
+
*/
|
| 300 |
+
value_type extract(size_type pos) const;
|
| 301 |
+
|
| 302 |
+
/**
|
| 303 |
+
* Returns a reference to the element at specified location pos, with bounds checking.
|
| 304 |
+
* If pos is not within the range of the container, an exception of type std::out_of_range is thrown.
|
| 305 |
+
*
|
| 306 |
+
* You cannot store the reference, but you can read it and assign new values to it:
|
| 307 |
+
*
|
| 308 |
+
* List<int64_t> list = ...;
|
| 309 |
+
* list[2] = 5;
|
| 310 |
+
* int64_t v = list[1];
|
| 311 |
+
*/
|
| 312 |
+
internal_const_reference_type operator[](size_type pos) const;
|
| 313 |
+
|
| 314 |
+
internal_reference_type operator[](size_type pos);
|
| 315 |
+
|
| 316 |
+
/**
|
| 317 |
+
* Assigns a new value to the element at location pos.
|
| 318 |
+
*/
|
| 319 |
+
void set(size_type pos, const value_type& value) const;
|
| 320 |
+
|
| 321 |
+
/**
|
| 322 |
+
* Assigns a new value to the element at location pos.
|
| 323 |
+
*/
|
| 324 |
+
void set(size_type pos, value_type&& value) const;
|
| 325 |
+
|
| 326 |
+
/**
|
| 327 |
+
* Returns an iterator to the first element of the container.
|
| 328 |
+
* If the container is empty, the returned iterator will be equal to end().
|
| 329 |
+
*/
|
| 330 |
+
iterator begin() const;
|
| 331 |
+
|
| 332 |
+
/**
|
| 333 |
+
* Returns an iterator to the element following the last element of the container.
|
| 334 |
+
* This element acts as a placeholder; attempting to access it results in undefined behavior.
|
| 335 |
+
*/
|
| 336 |
+
iterator end() const;
|
| 337 |
+
|
| 338 |
+
/**
|
| 339 |
+
* Checks if the container has no elements.
|
| 340 |
+
*/
|
| 341 |
+
bool empty() const;
|
| 342 |
+
|
| 343 |
+
/**
|
| 344 |
+
* Returns the number of elements in the container
|
| 345 |
+
*/
|
| 346 |
+
size_type size() const;
|
| 347 |
+
|
| 348 |
+
/**
|
| 349 |
+
* Increase the capacity of the vector to a value that's greater or equal to new_cap.
|
| 350 |
+
*/
|
| 351 |
+
void reserve(size_type new_cap) const;
|
| 352 |
+
|
| 353 |
+
/**
|
| 354 |
+
* Erases all elements from the container. After this call, size() returns zero.
|
| 355 |
+
* Invalidates any references, pointers, or iterators referring to contained elements. Any past-the-end iterators are also invalidated.
|
| 356 |
+
*/
|
| 357 |
+
void clear() const;
|
| 358 |
+
|
| 359 |
+
/**
|
| 360 |
+
* Inserts value before pos.
|
| 361 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 362 |
+
*/
|
| 363 |
+
iterator insert(iterator pos, const T& value) const;
|
| 364 |
+
|
| 365 |
+
/**
|
| 366 |
+
* Inserts value before pos.
|
| 367 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 368 |
+
*/
|
| 369 |
+
iterator insert(iterator pos, T&& value) const;
|
| 370 |
+
|
| 371 |
+
/**
|
| 372 |
+
* Inserts a new element into the container directly before pos.
|
| 373 |
+
* The new element is constructed with the given arguments.
|
| 374 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 375 |
+
*/
|
| 376 |
+
template<class... Args>
|
| 377 |
+
iterator emplace(iterator pos, Args&&... value) const;
|
| 378 |
+
|
| 379 |
+
/**
|
| 380 |
+
* Appends the given element value to the end of the container.
|
| 381 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 382 |
+
*/
|
| 383 |
+
void push_back(const T& value) const;
|
| 384 |
+
|
| 385 |
+
/**
|
| 386 |
+
* Appends the given element value to the end of the container.
|
| 387 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 388 |
+
*/
|
| 389 |
+
void push_back(T&& value) const;
|
| 390 |
+
|
| 391 |
+
/**
|
| 392 |
+
* Appends the given list to the end of the container. Uses at most one memory allocation.
|
| 393 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 394 |
+
*/
|
| 395 |
+
void append(List<T> lst) const;
|
| 396 |
+
|
| 397 |
+
/**
|
| 398 |
+
* Appends the given element value to the end of the container.
|
| 399 |
+
* The new element is constructed with the given arguments.
|
| 400 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 401 |
+
*/
|
| 402 |
+
template<class... Args>
|
| 403 |
+
void emplace_back(Args&&... args) const;
|
| 404 |
+
|
| 405 |
+
/**
|
| 406 |
+
* Removes the element at pos.
|
| 407 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 408 |
+
*/
|
| 409 |
+
iterator erase(iterator pos) const;
|
| 410 |
+
|
| 411 |
+
/**
|
| 412 |
+
* Removes the elements in the range [first, last).
|
| 413 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 414 |
+
*/
|
| 415 |
+
iterator erase(iterator first, iterator last) const;
|
| 416 |
+
|
| 417 |
+
/**
|
| 418 |
+
* Removes the last element of the container.
|
| 419 |
+
* Calling pop_back on an empty container is undefined.
|
| 420 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 421 |
+
*/
|
| 422 |
+
void pop_back() const;
|
| 423 |
+
|
| 424 |
+
/**
|
| 425 |
+
* Resizes the container to contain count elements.
|
| 426 |
+
* If the current size is less than count, additional default-inserted elements are appended.
|
| 427 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 428 |
+
*/
|
| 429 |
+
void resize(size_type count) const;
|
| 430 |
+
|
| 431 |
+
/**
|
| 432 |
+
* Resizes the container to contain count elements.
|
| 433 |
+
* If the current size is less than count, additional copies of value are appended.
|
| 434 |
+
* May invalidate any references, pointers, or iterators referring to contained elements. Any past-the-end iterators may also be invalidated.
|
| 435 |
+
*/
|
| 436 |
+
void resize(size_type count, const T& value) const;
|
| 437 |
+
|
| 438 |
+
/**
|
| 439 |
+
* Value equality comparison. This function implements Python-like semantics for
|
| 440 |
+
* equality: two lists with the same identity (e.g. same pointer) trivially
|
| 441 |
+
* compare equal, otherwise each element is compared for equality.
|
| 442 |
+
*/
|
| 443 |
+
template <class T_>
|
| 444 |
+
friend bool operator==(const List<T_>& lhs, const List<T_>& rhs);
|
| 445 |
+
|
| 446 |
+
template <class T_>
|
| 447 |
+
friend bool operator!=(const List<T_>& lhs, const List<T_>& rhs);
|
| 448 |
+
|
| 449 |
+
/**
|
| 450 |
+
* Identity comparison. Returns true if and only if `rhs` represents the same
|
| 451 |
+
* List object as `this`.
|
| 452 |
+
*/
|
| 453 |
+
bool is(const List<T>& rhs) const;
|
| 454 |
+
|
| 455 |
+
std::vector<T> vec() const;
|
| 456 |
+
|
| 457 |
+
/**
|
| 458 |
+
* Returns the number of Lists currently pointing to this same list.
|
| 459 |
+
* If this is the only instance pointing to this list, returns 1.
|
| 460 |
+
*/
|
| 461 |
+
// TODO Test use_count
|
| 462 |
+
size_t use_count() const;
|
| 463 |
+
|
| 464 |
+
TypePtr elementType() const;
|
| 465 |
+
|
| 466 |
+
// See [unsafe set type] for why this exists.
|
| 467 |
+
void unsafeSetElementType(TypePtr t);
|
| 468 |
+
|
| 469 |
+
private:
|
| 470 |
+
explicit List(c10::intrusive_ptr<c10::detail::ListImpl>&& elements);
|
| 471 |
+
explicit List(const c10::intrusive_ptr<c10::detail::ListImpl>& elements);
|
| 472 |
+
friend struct IValue;
|
| 473 |
+
template<class T_> friend List<T_> impl::toTypedList(List<IValue>);
|
| 474 |
+
template<class T_> friend List<IValue> impl::toList(List<T_>&&);
|
| 475 |
+
template<class T_> friend List<IValue> impl::toList(const List<T_>&);
|
| 476 |
+
friend const IValue* impl::ptr_to_first_element(const List<IValue>& list);
|
| 477 |
+
};
|
| 478 |
+
|
| 479 |
+
namespace impl {
|
| 480 |
+
// GenericList is how IValue stores lists. It is, however, not part of the
|
| 481 |
+
// public API. Kernels should use Lists with concrete types instead
|
| 482 |
+
// (maybe except for some internal prim ops).
|
| 483 |
+
using GenericList = List<IValue>;
|
| 484 |
+
|
| 485 |
+
}
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
namespace torch {
|
| 489 |
+
template<class T> using List = c10::List<T>;
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
#include <ATen/core/List_inl.h> // IWYU pragma: keep
|
| 493 |
+
|
| 494 |
+
#else
|
| 495 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 496 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
miniconda3/envs/ladir/lib/python3.10/site-packages/torch/include/ATen/core/List_inl.h
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|
| 2 |
+
#pragma once
|
| 3 |
+
|
| 4 |
+
#include <ATen/core/jit_type_base.h>
|
| 5 |
+
#include <ATen/core/ivalue.h>
|
| 6 |
+
|
| 7 |
+
namespace c10 {
|
| 8 |
+
|
| 9 |
+
template<class T> decltype(auto) getTypePtr();
|
| 10 |
+
std::string toString(const Type& type);
|
| 11 |
+
|
| 12 |
+
template<class T>
|
| 13 |
+
List<T>::List(c10::intrusive_ptr<c10::detail::ListImpl>&& elements)
|
| 14 |
+
: impl_(std::move(elements)) {}
|
| 15 |
+
|
| 16 |
+
template<class T>
|
| 17 |
+
List<T>::List(const c10::intrusive_ptr<c10::detail::ListImpl>& elements)
|
| 18 |
+
: impl_(elements) {}
|
| 19 |
+
|
| 20 |
+
template<class T>
|
| 21 |
+
List<T>::List()
|
| 22 |
+
: List(make_intrusive<c10::detail::ListImpl>(
|
| 23 |
+
typename c10::detail::ListImpl::list_type(),
|
| 24 |
+
getTypePtr<T>())) {
|
| 25 |
+
static_assert(!std::is_same_v<T, IValue>, "This constructor is not valid for List<IValue>. Please use c10::impl::GenericList(elementType) instead.");
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
template<class T>
|
| 29 |
+
List<T>::List(ArrayRef<T> values)
|
| 30 |
+
: List(make_intrusive<c10::detail::ListImpl>(
|
| 31 |
+
typename c10::detail::ListImpl::list_type(),
|
| 32 |
+
getTypePtr<T>())) {
|
| 33 |
+
static_assert(!std::is_same_v<T, IValue>, "This constructor is not valid for List<IValue>. Please use c10::impl::GenericList(elementType).");
|
| 34 |
+
impl_->list.reserve(values.size());
|
| 35 |
+
for (const T& element : values) {
|
| 36 |
+
impl_->list.push_back(element);
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
template<class T>
|
| 41 |
+
List<T>::List(std::initializer_list<T> initial_values)
|
| 42 |
+
: List(ArrayRef<T>(initial_values)) {
|
| 43 |
+
static_assert(!std::is_same_v<T, IValue>, "This constructor is not valid for List<IValue>. Please use c10::impl::GenericList(elementType).");
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
template<class T>
|
| 47 |
+
List<T>::List(TypePtr elementType)
|
| 48 |
+
: List(make_intrusive<c10::detail::ListImpl>(
|
| 49 |
+
typename c10::detail::ListImpl::list_type(),
|
| 50 |
+
std::move(elementType))) {
|
| 51 |
+
static_assert(std::is_same_v<T, IValue> || std::is_same_v<T, c10::intrusive_ptr<ivalue::Future>>,
|
| 52 |
+
"This constructor is only valid for c10::impl::GenericList or List<Future>.");
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
namespace impl {
|
| 56 |
+
template<class T>
|
| 57 |
+
List<T> toTypedList(impl::GenericList list) {
|
| 58 |
+
// If there's other instances of the list (i.e. list.use_count() > 1), then we have to be invariant
|
| 59 |
+
// because upcasting would allow people to add types into the new list that would break the old list.
|
| 60 |
+
// However, if there aren't any other instances of this list (i.e. list.use_count() == 1), then we can
|
| 61 |
+
// allow upcasting. This can be a perf improvement since we can cast List<T> to List<optional<T>>
|
| 62 |
+
// without having to copy it. This is also used to provide backwards compatibility with some old models
|
| 63 |
+
// that serialized the index arguments to aten::index, aten::index_put, aten::index_put_ and aten::index_put_impl_
|
| 64 |
+
// as List<Tensor> before we changed that argument to be List<optional<Tensor>>. When deserializing, we
|
| 65 |
+
// have list.use_count() == 1 and can deserialize the List<Tensor> directly as List<optional<Tensor>>.
|
| 66 |
+
TORCH_CHECK(*list.impl_->elementType == *getTypePtr<T>()
|
| 67 |
+
|| (list.use_count() == 1 && list.impl_->elementType->isSubtypeOf(*getTypePtr<T>()))
|
| 68 |
+
, "Tried to cast a List<", toString(*list.impl_->elementType), "> to a List<", toString(*getTypePtr<T>()), ">. Types mismatch.");
|
| 69 |
+
return List<T>(std::move(list.impl_));
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
template<class T>
|
| 73 |
+
impl::GenericList toList(List<T>&& list) {
|
| 74 |
+
return GenericList(std::move(list.impl_));
|
| 75 |
+
}
|
| 76 |
+
template<class T>
|
| 77 |
+
impl::GenericList toList(const List<T>& list) {
|
| 78 |
+
return GenericList(list.impl_);
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
template<class T>
|
| 83 |
+
List<T> List<T>::copy() const {
|
| 84 |
+
return List<T>(impl_->copy());
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
namespace detail {
|
| 88 |
+
template<class T>
|
| 89 |
+
T list_element_to(T element) {
|
| 90 |
+
return element;
|
| 91 |
+
}
|
| 92 |
+
template<class T>
|
| 93 |
+
T list_element_to(const IValue& element) {
|
| 94 |
+
return element.template to<T>();
|
| 95 |
+
}
|
| 96 |
+
template<class T>
|
| 97 |
+
T list_element_to(IValue&& element) {
|
| 98 |
+
return std::move(element).template to<T>();
|
| 99 |
+
}
|
| 100 |
+
template<class T>
|
| 101 |
+
struct ListElementFrom {
|
| 102 |
+
static IValue from(const T& element) {
|
| 103 |
+
return element;
|
| 104 |
+
}
|
| 105 |
+
static IValue from(T&& element) {
|
| 106 |
+
return std::move(element);
|
| 107 |
+
}
|
| 108 |
+
};
|
| 109 |
+
template<>
|
| 110 |
+
struct ListElementFrom<IValue> {
|
| 111 |
+
static const IValue& from(const IValue& element) {
|
| 112 |
+
return element;
|
| 113 |
+
}
|
| 114 |
+
static IValue&& from(IValue&& element) {
|
| 115 |
+
return std::move(element);
|
| 116 |
+
}
|
| 117 |
+
};
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
namespace impl {
|
| 121 |
+
|
| 122 |
+
template <class T, class Iterator>
|
| 123 |
+
ListElementReference<T, Iterator>::operator std::conditional_t<
|
| 124 |
+
std::is_reference_v<typename c10::detail::ivalue_to_const_ref_overload_return<
|
| 125 |
+
T>::type>,
|
| 126 |
+
const T&,
|
| 127 |
+
T>() const {
|
| 128 |
+
return iterator_->template to<T>();
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
template<class T, class Iterator>
|
| 132 |
+
ListElementReference<T, Iterator>& ListElementReference<T, Iterator>::operator=(T&& new_value) && {
|
| 133 |
+
*iterator_ = c10::detail::ListElementFrom<T>::from(std::move(new_value));
|
| 134 |
+
return *this;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
template<class T, class Iterator>
|
| 138 |
+
ListElementReference<T, Iterator>& ListElementReference<T, Iterator>::operator=(const T& new_value) && {
|
| 139 |
+
*iterator_ = c10::detail::ListElementFrom<T>::from(new_value);
|
| 140 |
+
return *this;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
template<class T, class Iterator>
|
| 144 |
+
ListElementReference<T, Iterator>& ListElementReference<T, Iterator>::operator=(ListElementReference<T, Iterator>&& rhs) && noexcept {
|
| 145 |
+
*iterator_ = *rhs.iterator_;
|
| 146 |
+
return *this;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
template<class T, class Iterator>
|
| 150 |
+
void swap(ListElementReference<T, Iterator>&& lhs, ListElementReference<T, Iterator>&& rhs) noexcept {
|
| 151 |
+
std::swap(*lhs.iterator_, *rhs.iterator_);
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
template<class T, class Iterator>
|
| 155 |
+
bool operator==(const ListElementReference<T, Iterator>& lhs, const T& rhs) {
|
| 156 |
+
const T& lhs_tmp = lhs;
|
| 157 |
+
return lhs_tmp == rhs;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
template<class T, class Iterator>
|
| 161 |
+
inline bool operator==(const T& lhs, const ListElementReference<T, Iterator>& rhs) {
|
| 162 |
+
return rhs == lhs;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
template<class T>
|
| 166 |
+
inline typename ListElementConstReferenceTraits<T>::const_reference
|
| 167 |
+
list_element_to_const_ref(const IValue& element) {
|
| 168 |
+
return element.template to<T>();
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
template<>
|
| 172 |
+
inline typename ListElementConstReferenceTraits<std::optional<std::string>>::const_reference
|
| 173 |
+
list_element_to_const_ref<std::optional<std::string>>(const IValue& element) {
|
| 174 |
+
return element.toOptionalStringRef();
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
} // namespace impl
|
| 178 |
+
|
| 179 |
+
template<class T>
|
| 180 |
+
void List<T>::set(size_type pos, const value_type& value) const {
|
| 181 |
+
impl_->list.at(pos) = c10::detail::ListElementFrom<T>::from(value);
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
template<class T>
|
| 185 |
+
void List<T>::set(size_type pos, value_type&& value) const {
|
| 186 |
+
impl_->list.at(pos) = c10::detail::ListElementFrom<T>::from(std::move(value));
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
template<class T>
|
| 190 |
+
typename List<T>::internal_const_reference_type List<T>::get(size_type pos) const {
|
| 191 |
+
return operator[](pos);
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
template<class T>
|
| 195 |
+
typename List<T>::internal_const_reference_type List<T>::operator[](size_type pos) const {
|
| 196 |
+
return c10::impl::list_element_to_const_ref<T>(impl_->list.at(pos));
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
template<class T>
|
| 200 |
+
typename List<T>::internal_reference_type List<T>::operator[](size_type pos) {
|
| 201 |
+
static_cast<void>(impl_->list.at(pos)); // Throw the exception if it is out of range.
|
| 202 |
+
return {impl_->list.begin() + static_cast<typename decltype(impl_->list)::difference_type>(pos)};
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
template<class T>
|
| 206 |
+
typename List<T>::value_type List<T>::extract(size_type pos) const {
|
| 207 |
+
auto& elem = impl_->list.at(pos);
|
| 208 |
+
auto result = c10::detail::list_element_to<T>(std::move(elem));
|
| 209 |
+
// Reset the list element to a T() instead of None to keep it correctly typed
|
| 210 |
+
elem = c10::detail::ListElementFrom<T>::from(T{});
|
| 211 |
+
return result;
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
template<class T>
|
| 215 |
+
typename List<T>::iterator List<T>::begin() const {
|
| 216 |
+
return iterator(impl_->list.begin());
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
template<class T>
|
| 220 |
+
typename List<T>::iterator List<T>::end() const {
|
| 221 |
+
return iterator(impl_->list.end());
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
template<class T>
|
| 225 |
+
bool List<T>::empty() const {
|
| 226 |
+
return impl_->list.empty();
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
template<class T>
|
| 230 |
+
typename List<T>::size_type List<T>::size() const {
|
| 231 |
+
return impl_->list.size();
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
template<class T>
|
| 235 |
+
void List<T>::reserve(size_type new_cap) const {
|
| 236 |
+
impl_->list.reserve(new_cap);
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
template<class T>
|
| 240 |
+
void List<T>::clear() const {
|
| 241 |
+
impl_->list.clear();
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
template<class T>
|
| 245 |
+
typename List<T>::iterator List<T>::insert(iterator pos, const T& value) const {
|
| 246 |
+
return iterator { impl_->list.insert(pos.iterator_, c10::detail::ListElementFrom<T>::from(value)) };
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
template<class T>
|
| 250 |
+
typename List<T>::iterator List<T>::insert(iterator pos, T&& value) const {
|
| 251 |
+
return iterator { impl_->list.insert(pos.iterator_, c10::detail::ListElementFrom<T>::from(std::move(value))) };
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
template<class T>
|
| 255 |
+
template<class... Args>
|
| 256 |
+
typename List<T>::iterator List<T>::emplace(iterator pos, Args&&... value) const {
|
| 257 |
+
// TODO Use list_element_from?
|
| 258 |
+
return iterator { impl_->list.emplace(pos.iterator_, std::forward<Args>(value)...) };
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
template<class T>
|
| 262 |
+
void List<T>::push_back(const T& value) const {
|
| 263 |
+
impl_->list.push_back(c10::detail::ListElementFrom<T>::from(value));
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
template<class T>
|
| 267 |
+
void List<T>::push_back(T&& value) const {
|
| 268 |
+
impl_->list.push_back(c10::detail::ListElementFrom<T>::from(std::move(value)));
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
template<class T>
|
| 272 |
+
void List<T>::append(List<T> b) const {
|
| 273 |
+
if (b.use_count() == 1) {
|
| 274 |
+
impl_->list.insert(impl_->list.end(), make_move_iterator(b.impl_->list.begin()), make_move_iterator(b.impl_->list.end()));
|
| 275 |
+
} else {
|
| 276 |
+
impl_->list.insert(impl_->list.end(), b.impl_->list.begin(), b.impl_->list.end());
|
| 277 |
+
}
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
template<class T>
|
| 281 |
+
template<class... Args>
|
| 282 |
+
void List<T>::emplace_back(Args&&... args) const {
|
| 283 |
+
// TODO Use list_element_from?
|
| 284 |
+
impl_->list.push_back(T(std::forward<Args>(args)...));
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
template<class T>
|
| 288 |
+
typename List<T>::iterator List<T>::erase(iterator pos) const {
|
| 289 |
+
return iterator { impl_->list.erase(pos.iterator_) };
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
template<class T>
|
| 293 |
+
typename List<T>::iterator List<T>::erase(iterator first, iterator last) const {
|
| 294 |
+
return iterator { impl_->list.erase(first.iterator_, last.iterator_) };
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
template<class T>
|
| 298 |
+
void List<T>::pop_back() const {
|
| 299 |
+
impl_->list.pop_back();
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
template<class T>
|
| 303 |
+
void List<T>::resize(size_type count) const {
|
| 304 |
+
impl_->list.resize(count, T{});
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
template<class T>
|
| 308 |
+
void List<T>::resize(size_type count, const T& value) const {
|
| 309 |
+
impl_->list.resize(count, value);
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
template<class T>
|
| 313 |
+
bool operator==(const List<T>& lhs, const List<T>& rhs) {
|
| 314 |
+
// Lists with the same identity trivially compare equal.
|
| 315 |
+
if (lhs.impl_ == rhs.impl_) {
|
| 316 |
+
return true;
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
// Otherwise, just compare values directly.
|
| 320 |
+
return *lhs.impl_ == *rhs.impl_;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
template<class T>
|
| 324 |
+
bool operator!=(const List<T>& lhs, const List<T>& rhs) {
|
| 325 |
+
return !(lhs == rhs);
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
template<class T>
|
| 329 |
+
bool List<T>::is(const List<T>& rhs) const {
|
| 330 |
+
return this->impl_ == rhs.impl_;
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
template<class T>
|
| 334 |
+
std::vector<T> List<T>::vec() const {
|
| 335 |
+
std::vector<T> result(begin(), end());
|
| 336 |
+
return result;
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
template<class T>
|
| 340 |
+
size_t List<T>::use_count() const {
|
| 341 |
+
return impl_.use_count();
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
template <class T>
|
| 345 |
+
TypePtr List<T>::elementType() const {
|
| 346 |
+
return impl_->elementType;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
template <class T>
|
| 350 |
+
void List<T>::unsafeSetElementType(TypePtr t) {
|
| 351 |
+
impl_->elementType = std::move(t);
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
#else
|
| 357 |
+
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
|
| 358 |
+
#endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
|