Skip to content

Conversation

@gchanan
Copy link
Contributor

@gchanan gchanan commented Jun 14, 2018

The goal here is to unify the tensor representations; since the "majority" of the representation is in TH, we push the empty size ({0}) and empty stride ({1}) logic into TH.

This PR does the following:

  1. Previously THTensor/THCTensor with dim_ == 0, size == nullptr, stride == nullptr are now dim_ == 1, size == {0}, stride == {1}.
  2. The logic that previously implemented this at the ATen level (e.g. THLongStorageView STRIDE_EMPTY_TENSOR) is removed.
  3. The above is pretty clean except for resize/resizeNd logic -- that is still called with nDimension == 0. So, we rename these to resizeLegacy, resizeNdLegacy, map nDimension == 1
    into the new regime, and will later write a empty-aware resize/resizeNd and move over the calls to resizeLegacy, resizeNdLegacy.
  4. Also introduces some ifdefs that are just used for testing:
    a) USE_TH_SCALAR: move scalar logic in TH
    b) USE_TH_ZERO_SIZE_DIM: support arbitrary 0-sized dimensions, i.e {...,0,...}.
    These are just used to write forward-looking correct code while call sites to _dim() (old TH nDimension) and resizeLegacy are updated.

The goal here is to unify the tensor representations; since the "majority" of the representation is in TH, we push the empty size ({0}) and empty stride ({1}) logic into TH.

This PR does the following:
1) Previously THTensor/THCTensor with dim_ == 0, size == nullptr, stride == nullptr are now dim_ == 1, size == {0}, stride == {1}.
2) The logic that previously implemented this at the ATen level (e.g. THLongStorageView STRIDE_EMPTY_TENSOR) is removed.
3) The above is pretty clean except for resize/resizeNd logic -- that is still called with nDimension == 0.  So, we rename these to resizeLegacy, resizeNdLegacy, map nDimension == 1
into the new regime, and will later write a empty-aware resize/resizeNd and move over the calls to resizeLegacy, resizeNdLegacy.
4) Also introduces some ifdefs that are just used for testing:
a) USE_TH_SCALAR: move scalar logic in TH
b) USE_TH_ZERO_SIZE_DIM: support arbitrary 0-sized dimensions, i.e {...,0,...}.
These are just used to write forward-looking correct code while call sites to _dim() (old TH nDimension) and resizeLegacy are updated.
@gchanan
Copy link
Contributor Author

gchanan commented Jun 14, 2018

@pytorchbot retest this please.

: zero_dim_to_null(false)
{
// zero_dim_to_one converts an empty ArrayRef into [1]
// zero_dim_to_null converts an empty ArrayRef into a null THLongStorage

This comment was marked as off-topic.

}

enum class THLongStorageViewKind {
SIZE,

This comment was marked as off-topic.

}
bool zero_dim_to_one = kind == THLongStorageViewKind::SIZE_STRIDE;

if(zero_dim_to_one && ref.size() == 0) {

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.


/* Resize */
void THTensor_(resize)(THTensor *self, THLongStorage *size, THLongStorage *stride)
void THTensor_(resizeLegacy)(THTensor *self, THLongStorage *size, THLongStorage *stride)

This comment was marked as off-topic.

src = self;

#ifndef USE_TH_SCALAR
THArgCheck(src->_dim() > 1, 1, "cannot select on a vector");

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

self->size = NULL;
self->stride = NULL;
self->dim_ = 0;
self->size = (int64_t *)THAlloc(sizeof(int64_t));

This comment was marked as off-topic.

TH_API void THTensor_(resize4d)(THTensor *tensor, int64_t size0_, int64_t size1_, int64_t size2_, int64_t size3_);
TH_API void THTensor_(resize5d)(THTensor *tensor, int64_t size0_, int64_t size1_, int64_t size2_, int64_t size3_, int64_t size4_);
// Note: these are legacy resize functions that treat sizes as size->size == 0 and size->data<int64_t>() as being 0-terminated.
// There will be new resize functions to call that fully support dimensions of size 0.

This comment was marked as off-topic.


inline int64_t dim() const {
// FixME: nDimensionI and nDimensionV should be set correctly by THS
return (nDimensionI + nDimensionV) == 0 ? 1 : (nDimensionI + nDimensionV);

This comment was marked as off-topic.

Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So beauuuuutiful

@gchanan
Copy link
Contributor Author

gchanan commented Jun 14, 2018

@pytorchbot retest this please.

@gchanan
Copy link
Contributor Author

gchanan commented Jun 14, 2018

caffe2 failure looks not related.

@gchanan gchanan merged commit edc3000 into pytorch:master Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants