Skip to content

Commit 54545c2

Browse files
committed
Note [Undefined-dim versus 0-dim]
Signed-off-by: Edward Z. Yang <ezyang@fb.com>
1 parent ef3b09f commit 54545c2

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

templates/TensorDerived.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int64_t ${Tensor}::dim() {
2929
int64_t d = ${THTensor}_nDimension(${state,}tensor);
3030
if(d != 0)
3131
return d;
32+
// See Note [Undefined-dim versus 0-dim]
3233
return kUndefinedDimensions;
3334
}
3435

templates/Type.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ constexpr Backend kCUDA = Backend::CUDA;
4646
constexpr Backend kSparseCPU = Backend::SparseCPU;
4747
constexpr Backend kSparseCUDA = Backend::SparseCUDA;
4848

49+
// Note [Undefined-dim versus 0-dim]
50+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51+
// Unlike Torch, ATen treats zero-dimension tensors as having ONE
52+
// element (that is to say, a zero-dimensional tensor is a scalar!)
53+
// This is in contrast to Torch, where a zero-dimension tensor has
54+
// zero elements.
55+
//
56+
// Because we are backed by Torch tensors, we need to be able to
57+
// represent this state (of numel==0). kUndefinedDimensions represents this
58+
// situation.
4959
constexpr int64_t kUndefinedDimensions = std::numeric_limits<int64_t>::min();
5060

5161
static inline const char * toString(Backend b) {

0 commit comments

Comments
 (0)