42 class IndexerIterator;
45 static constexpr int64_t MAX_DIMS = 10;
49 static constexpr int64_t MAX_INPUTS = 10;
53 static constexpr int64_t MAX_OUTPUTS = 2;
56 template <
typename T,
int size>
68 template <
int NARGS,
typename index_t = u
int32_t>
72 const int64_t*
const* strides)
74 if (dims_ > MAX_DIMS) {
78 for (
int i = 0; i < MAX_DIMS; ++i) {
84 for (
int arg = 0; arg < NARGS; arg++) {
85 strides_[i][arg] = i < dims_ ? strides[arg][i] : 0;
91 index_t linear_idx)
const {
93 #if defined(__CUDA_ARCH__) 96 for (
int arg = 0; arg < NARGS; arg++) {
100 #if defined(__CUDA_ARCH__) 103 for (
int dim = 0; dim < MAX_DIMS; ++dim) {
107 index_t mod = linear_idx % sizes_[dim];
108 linear_idx = linear_idx / sizes_[dim];
110 #if defined(__CUDA_ARCH__) 113 for (
int arg = 0; arg < NARGS; arg++) {
114 offsets[arg] += mod * strides_[dim][arg];
121 index_t sizes_[MAX_DIMS];
122 index_t strides_[MAX_DIMS][NARGS];
130 TensorRef() : data_ptr_(nullptr), ndims_(0), dtype_byte_size_(0) {}
137 data_ptr_ =
const_cast<void*
>(t.
GetDataPtr());
140 for (int64_t i = 0; i < ndims_; ++i) {
142 byte_strides_[i] = t.
GetStride(i) * dtype_byte_size_;
148 if (static_cast<int64_t>(dims.size()) != ndims_) {
150 dims.size(), ndims_);
152 std::vector<bool> seen_dims(ndims_,
false);
153 for (
const int64_t& dim : dims) {
154 seen_dims[dim] =
true;
156 if (!std::all_of(seen_dims.begin(), seen_dims.end(),
157 [](
bool seen) {
return seen; })) {
159 "Permute dims must be a permuntation from 0 to {}.",
166 for (int64_t i = 0; i < ndims_; ++i) {
168 new_shape[i] = shape_[old_dim];
169 new_byte_strides[i] = byte_strides_[old_dim];
171 for (int64_t i = 0; i < ndims_; ++i) {
172 shape_[i] = new_shape[i];
173 byte_strides_[i] = new_byte_strides[i];
179 rc = rc && (data_ptr_ == other.
data_ptr_);
180 rc = rc && (ndims_ == other.
ndims_);
182 for (int64_t i = 0; i < ndims_; ++i) {
183 rc = rc && (shape_[i] == other.
shape_[i]);
193 int64_t dtype_byte_size_ = 0;
194 int64_t shape_[MAX_DIMS];
195 int64_t byte_strides_[MAX_DIMS];
223 : input_(
TensorRef(tensor)), ndims_(tensor.NumDims()) {}
226 int64_t num_workloads = 1;
227 for (int64_t i = 0; i < ndims_; ++i) {
228 num_workloads *= input_.shape_[i];
230 return num_workloads;
234 if (workload_idx < 0 || workload_idx >= NumWorkloads()) {
238 workload_idx = workload_idx * input_.dtype_byte_size_;
239 for (int64_t i = 0; i < ndims_; ++i) {
240 offset += workload_idx / input_.byte_strides_[i] *
241 input_.byte_strides_[i];
242 workload_idx = workload_idx % input_.byte_strides_[i];
244 return static_cast<void*
>(
static_cast<char*
>(input_.data_ptr_) +
269 Indexer(
const std::vector<Tensor>& input_tensors,
270 const Tensor& output_tensor,
274 Indexer(
const std::vector<Tensor>& input_tensors,
275 const std::vector<Tensor>& output_tensors,
280 bool CanUse32BitIndexing()
const;
289 std::unique_ptr<Indexer> SplitLargestDim();
293 Indexer GetPerOutputIndexer(int64_t output_idx)
const;
304 void ShrinkDim(int64_t dim, int64_t start, int64_t
size);
307 int64_t NumReductionDims()
const;
331 int64_t NumWorkloads()
const;
334 int64_t NumOutputElements()
const;
341 if (i >= num_inputs_ || i < 0) {
348 if (i >= num_inputs_ || i < 0) {
357 if (i >= num_outputs_ || i < 0) {
364 if (i >= num_outputs_ || i < 0) {
374 if (num_outputs_ > 1) {
381 if (num_outputs_ > 1) {
393 return outputs_[0].byte_strides_[dim] == 0 && master_shape_[dim] > 1;
402 int64_t workload_idx)
const {
403 if (input_idx < 0 || input_idx >= num_inputs_) {
406 return GetWorkloadDataPtr(inputs_[input_idx], workload_idx);
414 return GetWorkloadDataPtr(outputs_[0], workload_idx);
417 int64_t workload_idx)
const {
418 return GetWorkloadDataPtr(outputs_[output_idx], workload_idx);
424 void CoalesceDimensions();
429 void ReorderDimensions(
const SizeVector& reduction_dims);
432 void UpdateMasterStrides();
460 static void BroadcastRestride(
TensorRef& src,
462 const int64_t* dst_shape);
466 static void ReductionRestride(
TensorRef& dst,
468 const int64_t* src_shape,
475 int64_t workload_idx)
const {
478 if (workload_idx < 0) {
482 for (int64_t i = 0; i < ndims_; ++i) {
483 offset += workload_idx / master_strides_[i] * tr.
byte_strides_[i];
484 workload_idx = workload_idx % master_strides_[i];
486 return static_cast<char*
>(tr.
data_ptr_) + offset;
490 int64_t num_inputs_ = 0;
491 int64_t num_outputs_ = 0;
510 int64_t master_shape_[MAX_DIMS];
514 int64_t master_strides_[MAX_DIMS];
522 bool final_output_ =
true;
526 bool accumulate_ =
false;
538 bool operator==(
const Iterator& other)
const;
539 bool operator!=(
const Iterator& other)
const;
541 std::vector<std::unique_ptr<Indexer>>
vec_;
int64_t ndims_
Definition: Indexer.h:192
Dtype GetDtype() const
Definition: Tensor.h:742
const TensorRef & GetOutput(int64_t i) const
Definition: Indexer.h:363
bool IsFinalOutput() const
Definition: Indexer.h:297
T data_[size]
Definition: Indexer.h:58
void * GetDataPtr()
Definition: Tensor.h:738
TensorRef(const Tensor &t)
Definition: Indexer.h:132
TensorRef input_
Definition: Indexer.h:249
Definition: Indexer.h:531
int64_t byte_strides_[MAX_DIMS]
Definition: Indexer.h:195
TensorRef & GetInput(int64_t i)
Returns input TensorRef.
Definition: Indexer.h:340
int64_t NumDims() const
Definition: Tensor.h:750
bool IsReductionDim(int64_t dim) const
Returns true if the dim -th dimension is reduced.
Definition: Indexer.h:389
int offset
Definition: FilePCD.cpp:62
Indexer()
Definition: Indexer.h:262
int64_t ndims_
Definition: Indexer.h:250
void LogError(const char *format, const Args &... args)
Definition: Console.h:174
TensorRef & GetOutput()
Definition: Indexer.h:373
OPEN3D_HOST_DEVICE char * GetOutputPtr(int64_t output_idx, int64_t workload_idx) const
Definition: Indexer.h:416
bool operator!=(const TensorRef &other) const
Definition: Indexer.h:189
bool operator==(const TensorRef &other) const
Definition: Indexer.h:177
int64_t * GetMasterShape()
Definition: Indexer.h:315
TensorRef & GetOutput(int64_t i)
Returns output TensorRef.
Definition: Indexer.h:356
OPEN3D_HOST_DEVICE char * GetWorkloadDataPtr(const TensorRef &tr, int64_t workload_idx) const
Definition: Indexer.h:474
int dims_
Definition: Indexer.h:120
const TensorRef & GetInput(int64_t i) const
Definition: Indexer.h:347
OPEN3D_HOST_DEVICE char * GetOutputPtr(int64_t workload_idx) const
Definition: Indexer.h:413
int size
Definition: FilePCD.cpp:57
#define OPEN3D_HOST_DEVICE
Definition: CUDAUtils.h:54
Definition: SizeVector.h:40
A minimalistic class that reference a Tensor.
Definition: Indexer.h:126
Tensor operator*(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:886
Definition: Indexer.h:529
static int64_t ByteSize(const Dtype &dtype)
Definition: Dtype.h:61
const int64_t * GetMasterShape() const
Definition: Indexer.h:314
OPEN3D_HOST_DEVICE T operator[](int i) const
Definition: Indexer.h:60
OPEN3D_HOST_DEVICE int64_t NumWorkloads() const
Definition: Indexer.h:225
OPEN3D_HOST_DEVICE T & operator[](int i)
Definition: Indexer.h:61
void Permute(const SizeVector &dims)
Definition: Indexer.h:146
Definition: Indexer.h:220
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:734
Iterator()
Definition: Indexer.h:532
OPEN3D_HOST_DEVICE void * GetPtr(int64_t workload_idx) const
Definition: Indexer.h:233
const int64_t * GetMasterStrides() const
Definition: Indexer.h:319
int64_t shape_[MAX_DIMS]
Definition: Indexer.h:194
SmallArray & operator=(const SmallArray &)=default
int64_t NumDims() const
Returns number of dimensions of the Indexer.
Definition: Indexer.h:310
TensorRef()
Definition: Indexer.h:130
Definition: Open3DViewer.h:29
void * data_ptr_
Definition: Indexer.h:191
Definition: Indexer.h:260
int64_t NumInputs() const
Number of input Tensors.
Definition: Indexer.h:337
OffsetCalculator(int dims, const int64_t *sizes, const int64_t *const *strides)
Definition: Indexer.h:70
int64_t WrapDim(int64_t dim, int64_t max_dim)
Wrap around negative dim.
Definition: ShapeUtil.cpp:147
std::vector< std::unique_ptr< Indexer > > vec_
Definition: Indexer.h:541
bool ShouldAccumulate() const
Definition: Indexer.h:295
OPEN3D_HOST_DEVICE char * GetInputPtr(int64_t input_idx, int64_t workload_idx) const
Definition: Indexer.h:401
DtypePolicy
Definition: Indexer.h:198
TensorIterator(const Tensor &tensor)
Definition: Indexer.h:222
const TensorRef & GetOutput() const
Definition: Indexer.h:380
int64_t dtype_byte_size_
Definition: Indexer.h:193
SizeVector GetShape() const
Definition: Tensor.h:722