Skip to content

Commit

Permalink
Delete dead TH size inference code. (pytorch#8866)
Browse files Browse the repository at this point in the history
  • Loading branch information
gchanan authored and ezyang committed Jun 25, 2018
1 parent cca2476 commit 89afb93
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 124 deletions.
114 changes: 0 additions & 114 deletions aten/src/TH/THStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,117 +65,3 @@ THLongStorage *THLongStorage_newInferSize(THLongStorage *size, ptrdiff_t nElemen
}
return copy;
}

int THLongStorage_inferSize2(THLongStorage *output, int64_t *sizesA, int64_t dimsA, int64_t *sizesB, int64_t dimsB,
char *error_buffer, int buffer_len) {
THArgCheck(sizesA != NULL, 1, "sizesA must not be null");
THArgCheck(sizesB != NULL, 2, "sizesB must not be null");
THArgCheck(dimsA, 1, "Can't expand empty tensor a");
THArgCheck(dimsB, 1, "Can't expand empty tensor b");
ptrdiff_t ndim = dimsA > dimsB ? dimsA : dimsB;

int64_t *expandedSizes = static_cast<int64_t*>(THAlloc(sizeof(int64_t)*ndim));

for (int64_t i = ndim - 1; i >= 0; --i) {
int64_t offset = ndim - 1 - i;
int64_t dimA = dimsA - 1 - offset;
int64_t dimB = dimsB - 1 - offset;
int64_t sizeA = (dimA >= 0) ? sizesA[dimA] : 1;
int64_t sizeB = (dimB >= 0) ? sizesB[dimB] : 1;
if (sizeA == sizeB || sizeA == 1 || sizeB == 1) {
expandedSizes[i] = THMax(sizeA, sizeB);
} else {
THFree(expandedSizes);
snprintf(error_buffer, buffer_len, "The size of tensor a (%" PRId64 ") must match the size of tensor b (%" PRId64 ") at "
"non-singleton dimension %" PRId64 ".", sizeA, sizeB, i);
return -1;
}
}
THLongStorage_resize(output, ndim);
memcpy(THLongStorage_data(output), expandedSizes, sizeof(int64_t)*ndim);
THFree(expandedSizes);
return 0;
}

int THLongStorage_inferSizeN(THLongStorage *output, int n, int64_t **sizes, int64_t *dims,
char *error_buffer, int buffer_len) {
THArgCheck(n > 0, 2, "n must be greater than 0");
THArgCheck(sizes != NULL, 1, "sizes must not be null");
THArgCheck(dims != NULL, 1, "dims must not be null");

ptrdiff_t ndim = 0;
for (int j = 0; j < n; ++j) {
THArgCheck(sizes[ j ] != NULL, 1, "size %d must not be null", j);
THArgCheck(dims[ j ], 1, "Can't expand empty tensor %d", j);
ndim = dims[ j ] > ndim ? dims[ j ] : ndim;
}

int64_t *expandedSizes = static_cast<int64_t*>(THAlloc(sizeof(int64_t)*ndim));

for (int64_t i = ndim - 1; i >= 0; --i) {
expandedSizes[ i ] = 1;
int64_t offset = ndim - 1 - i;
for (int j = 0; j < n; ++j) {
int64_t dim = dims[ j ] - 1 - offset;
int64_t size = (dim >= 0) ? sizes[ j ][ dim ] : 1;
if (size == expandedSizes[ i ] || size == 1 || expandedSizes[ i ] == 1) {
expandedSizes[ i ] = THMax(expandedSizes[ i ], size);
} else {
snprintf(error_buffer, buffer_len, "The size of tensor %i (%" PRId64 ") must match the expanded size"
"of tensor (%" PRId64 ") at non-singleton dimension %" PRId64 ".", j, size, expandedSizes[ i ], i);
THFree(expandedSizes);
return -1;
}
}
}
THLongStorage_resize(output, ndim);
memcpy(THLongStorage_data(output), expandedSizes, sizeof(int64_t)*ndim);
THFree(expandedSizes);
return 0;
}

int THLongStorage_inferExpandGeometry(int64_t *tensorSizes, int64_t *tensorStrides, int64_t tensorDim,
THLongStorage *sizes, int64_t **expandedSizes, int64_t **expandedStrides,
char *error_buffer, int buffer_len) {
ptrdiff_t ndim = THLongStorage_size(sizes);

int64_t *expandedSizesCalc = static_cast<int64_t*>(THAlloc(sizeof(int64_t)*ndim));
int64_t *expandedStridesCalc = static_cast<int64_t*>(THAlloc(sizeof(int64_t)*ndim));

// create a new geometry for the tensors
for (int64_t i = ndim - 1; i >= 0; --i) {
int64_t offset = ndim - 1 - i;
int64_t dim = tensorDim - 1 - offset;
int64_t size = (dim >= 0) ? tensorSizes[dim] : 1;
int64_t stride = (dim >= 0) ?
tensorStrides[dim] : expandedSizesCalc[i + 1] * expandedStridesCalc[i+1];
int64_t targetSize = THLongStorage_data(sizes)[i];
if (targetSize == -1) {
if (dim < 0) {
THFree(expandedSizesCalc);
THFree(expandedStridesCalc);
snprintf(error_buffer, buffer_len, "The expanded size of the tensor (%" PRId64 ") isn't allowed in a leading, non-existing dimension %" PRId64 ".", targetSize, i);
return -1;
} else {
targetSize = size;
}
}
if (size != targetSize) {
if (size == 1) {
size = targetSize;
stride = 0;
} else {
THFree(expandedSizesCalc);
THFree(expandedStridesCalc);
snprintf(error_buffer, buffer_len, "The expanded size of the tensor (%" PRId64 ") must match the existing size (%" PRId64 ") at "
"non-singleton dimension %" PRId64 ".", targetSize, size, i);
return -1;
}
}
expandedSizesCalc[i] = size;
expandedStridesCalc[i] = stride;
}
*expandedSizes = expandedSizesCalc;
*expandedStrides = expandedStridesCalc;
return 0;
}
10 changes: 0 additions & 10 deletions aten/src/TH/THStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,4 @@ TH_API void THStorage_free(THStorage *storage);
TH_API THDescBuff THLongStorage_sizeDesc(const THLongStorage *size);
TH_API THLongStorage *THLongStorage_newInferSize(THLongStorage *size, ptrdiff_t nElement);

// Given the sizes of {2,N} tensors, write out the size when the tensors are expanded together.
TH_API int THLongStorage_inferSize2(THLongStorage *output, int64_t *sizesA, int64_t dimsA,
int64_t *sizesB, int64_t dimsB, char *error_buffer, int buffer_len);
TH_API int THLongStorage_inferSizeN(THLongStorage *output, int n, int64_t **sizes, int64_t *dims,
char *error_buffer, int buffer_len);

TH_API int THLongStorage_inferExpandGeometry(int64_t *tensorSizes, int64_t *tensorStrides, int64_t tensorDim,
THLongStorage *sizes, int64_t **expandedSizes, int64_t **expandedStrides,
char *error_buffer, int buffer_len);

#endif

0 comments on commit 89afb93

Please sign in to comment.