Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions aten/src/THC/generic/THCTensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,23 @@ THCTensor *THCTensor_(newView)(THCState *state, THCTensor *tensor, THLongStorage
return self;
}

// Collapses the first two dimensions of a tensor.
// Assumes the input tensor is contiguous.
THCTensor *THCTensor_(newFoldBatchDim)(THCState *state, THCTensor *input) {
int in_dims = THCTensor_(nDimension)(state, input);
THArgCheck(in_dims >= 2, 1, "Tensor needs to have at least two dimensions");
THArgCheck(THCTensor_(isContiguous)(state, input), 1,
"Tensor must be contiguous");
THLongStorage *newSize = THLongStorage_newWithSize(in_dims - 1);
newSize->data[0] = THCTensor_(size)(state, input, 0) * THCTensor_(size)(state, input, 1);
for (int i = 2; i < in_dims; i++) {
newSize->data[i - 1] = THCTensor_(size)(state, input, i);
}
THCTensor *output = THCTensor_(newView)(state, input, newSize);
THLongStorage_free(newSize);
return output;
}

/* Resize */
void THCTensor_(resize)(THCState *state, THCTensor *self, THLongStorage *size, THLongStorage *stride)
{
Expand Down
1 change: 1 addition & 0 deletions aten/src/THC/generic/THCTensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ THC_API THCTensor *THCTensor_(newNarrow)(THCState *state, THCTensor *tensor, int
THC_API THCTensor *THCTensor_(newTranspose)(THCState *state, THCTensor *tensor, int dimension1_, int dimension2_);
THC_API THCTensor *THCTensor_(newUnfold)(THCState *state, THCTensor *tensor, int dimension_, int64_t size_, int64_t step_);
THC_API THCTensor *THCTensor_(newView)(THCState *state, THCTensor *tensor, THLongStorage *size);
THC_API THCTensor *THCTensor_(newFoldBatchDim)(THCState *state, THCTensor *input);
THC_API THCTensor *THCTensor_(newExpand)(THCState *state, THCTensor *tensor, THLongStorage *size);

THC_API void THCTensor_(expand)(THCState *state, THCTensor *r, THCTensor *tensor, THLongStorage *sizes);
Expand Down
61 changes: 33 additions & 28 deletions aten/src/THCUNN/generic/VolumetricAveragePooling.cu
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ void THNN_(VolumetricAveragePooling_updateOutput)(
int dimh = 2;
int dimw = 3;

if (input->nDimension == 5)
int fiveDimensionalInput = THCTensor_(nDimension)(state, input) == 5;
if (fiveDimensionalInput)
{
dimt++;
dimh++;
Expand All @@ -139,7 +140,7 @@ void THNN_(VolumetricAveragePooling_updateOutput)(
(state, input, NULL, kT, kW, kH, dT, dW, dH,
padT, padW, padH, ceil_mode);

if (THCTensor_(nDimension)(state, input) == 4)
if (!fiveDimensionalInput) /* 4D */
{
/* sizes */
batchSize = 1;
Expand Down Expand Up @@ -186,7 +187,7 @@ void THNN_(VolumetricAveragePooling_updateOutput)(
--outputWidth;
}

if (input->nDimension == 4) /* 4D */
if (!fiveDimensionalInput) /* 4D */
{
/* resize output */
THCTensor_(resize4d)(state, output, inputSlices,
Expand All @@ -199,20 +200,21 @@ void THNN_(VolumetricAveragePooling_updateOutput)(
}

input = THCTensor_(newContiguous)(state, input);
if (fiveDimensionalInput) {
// Collapse batch and feature dimensions
output = THCTensor_(newFoldBatchDim)(state, output);

THCTensor *old_input = input;
input = THCTensor_(newFoldBatchDim)(state, input);
THCTensor_(free)(state, old_input);
} else {
THCTensor_(retain)(state, output);
}

// Collapse batch and feature dimensions
THCDeviceTensor<real, 4> cudaInput;
THCDeviceTensor<real, 4> cudaOutput;
if (THCTensor_(nDimension)(state, input) == 4)
{
cudaInput = toDeviceTensor<real, 4>(state, input);
cudaOutput = toDeviceTensor<real, 4>(state, output);
}
else
{
cudaInput = toDeviceTensor<real, 5>(state, input).downcastOuter<4>();
cudaOutput = toDeviceTensor<real, 5>(state, output).downcastOuter<4>();
}
cudaInput = toDeviceTensor<real, 4>(state, input);
cudaOutput = toDeviceTensor<real, 4>(state, output);

int totalZ = outputTime * inputSlices * batchSize;
int offsetZ = 0;
Expand Down Expand Up @@ -247,7 +249,9 @@ void THNN_(VolumetricAveragePooling_updateOutput)(
offsetZ += 65535;
THCudaCheck(cudaGetLastError());
}

THCTensor_(free)(state, input);
THCTensor_(free)(state, output);
}

void THNN_(VolumetricAveragePooling_updateGradInput)(
Expand Down Expand Up @@ -280,7 +284,8 @@ void THNN_(VolumetricAveragePooling_updateGradInput)(
int outputHeight;
int outputWidth;

if (THCTensor_(nDimension)(state, input) == 4) /* 4D */
int fiveDimensionalInput = THCTensor_(nDimension)(state, input) == 5;
if (!fiveDimensionalInput) /* 4D */
{
batchSize = 1;
inputSlices = THCTensor_(size)(state, input, 0);
Expand All @@ -306,22 +311,21 @@ void THNN_(VolumetricAveragePooling_updateGradInput)(
}

gradOutput = THCTensor_(newContiguous)(state, gradOutput);
if (fiveDimensionalInput) {
// Collapse batch and feature dimensions
gradInput = THCTensor_(newFoldBatchDim)(state, gradInput);

THCTensor *old_gradOutput = gradOutput;
gradOutput = THCTensor_(newFoldBatchDim)(state, gradOutput);
THCTensor_(free)(state, old_gradOutput);
} else {
THCTensor_(retain)(state, gradInput);
}

// Collapse batch and feature dimensions
THCDeviceTensor<real, 4> cudaGradInput;
THCDeviceTensor<real, 4> cudaGradOutput;
if (THCTensor_(nDimension)(state, input) == 4)
{
cudaGradInput = toDeviceTensor<real, 4>(state, gradInput);
cudaGradOutput = toDeviceTensor<real, 4>(state, gradOutput);
}
else
{
cudaGradInput =
toDeviceTensor<real, 5>(state, gradInput).downcastOuter<4>();
cudaGradOutput =
toDeviceTensor<real, 5>(state, gradOutput).downcastOuter<4>();
}
cudaGradInput = toDeviceTensor<real, 4>(state, gradInput);
cudaGradOutput = toDeviceTensor<real, 4>(state, gradOutput);

dim3 block(32, 8);

Expand Down Expand Up @@ -372,6 +376,7 @@ void THNN_(VolumetricAveragePooling_updateGradInput)(
}
}

THCTensor_(free)(state, gradInput);
THCTensor_(free)(state, gradOutput);
}

Expand Down
63 changes: 35 additions & 28 deletions aten/src/THCUNN/generic/VolumetricDilatedMaxPooling.cu
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ void THNN_(VolumetricDilatedMaxPooling_updateOutput)(
int dimh = 2;
int dimw = 3;

if (input->nDimension == 5)
int fiveDimensionalInput = THCTensor_(nDimension)(state, input) == 5;

if (fiveDimensionalInput)
{
dimt++;
dimh++;
Expand All @@ -163,7 +165,7 @@ void THNN_(VolumetricDilatedMaxPooling_updateOutput)(
inputHeight = THCTensor_(size)(state, input, 2);
inputWidth = THCTensor_(size)(state, input, 3);
}
else if (THCTensor_(nDimension)(state, input) == 5)
else if (fiveDimensionalInput)
{
/* sizes */
batchSize = THCTensor_(size)(state, input, 0);
Expand Down Expand Up @@ -200,7 +202,7 @@ void THNN_(VolumetricDilatedMaxPooling_updateOutput)(
--outputWidth;
}

if (input->nDimension == 4) /* 4D */
if (!fiveDimensionalInput) /* 4D */
{
/* resize output */
THCTensor_(resize4d)(state, output, inputSlices,
Expand All @@ -217,23 +219,25 @@ void THNN_(VolumetricDilatedMaxPooling_updateOutput)(
// Index tensor packs index offsets as uchars into floats
THCIndexTensor_(resize5d)(state, indices, batchSize, inputSlices,
outputTime, outputHeight, outputWidth);
fiveDimensionalInput = 1;
}

input = THCTensor_(newContiguous)(state, input);
if (fiveDimensionalInput) {
// Collapse batch and feature dimensions
output = THCTensor_(newFoldBatchDim)(state, output);

THCTensor *old_input = input;
input = THCTensor_(newFoldBatchDim)(state, input);
THCTensor_(free)(state, old_input);
} else {
THCTensor_(retain)(state, output);
}

// Collapse batch and feature dimensions
THCDeviceTensor<real, 4> cudaInput;
THCDeviceTensor<real, 4> cudaOutput;
if (THCTensor_(nDimension)(state, input) == 4)
{
cudaInput = toDeviceTensor<real, 4>(state, input);
cudaOutput = toDeviceTensor<real, 4>(state, output);
}
else
{
cudaInput = toDeviceTensor<real, 5>(state, input).downcastOuter<4>();
cudaOutput = toDeviceTensor<real, 5>(state, output).downcastOuter<4>();
}
cudaInput = toDeviceTensor<real, 4>(state, input);
cudaOutput = toDeviceTensor<real, 4>(state, output);

THLongStorage *indicesSize = THLongStorage_newWithSize(4);
int64_t indicesSizeRaw[4] = { batchSize * inputSlices,
Expand Down Expand Up @@ -281,6 +285,7 @@ void THNN_(VolumetricDilatedMaxPooling_updateOutput)(
}

THCTensor_(free)(state, input);
THCTensor_(free)(state, output);
THCIndexTensor_(free)(state, indices1);
}

Expand Down Expand Up @@ -310,13 +315,15 @@ void THNN_(VolumetricDilatedMaxPooling_updateGradInput)(
int outputHeight;
int outputWidth;

int fiveDimensionalInput = THCTensor_(nDimension)(state, input) == 5;

THCUNN_assertSameGPU(state, 4, input, indices, gradOutput, gradInput);
THNN_(VolumetricDilatedMaxPooling_shapeCheck)(
state, input, gradOutput, indices, kT, kW, kH,
dT, dW, dH, padT, padW, padH,
dilationT, dilationW, dilationH, ceilMode);

if (THCTensor_(nDimension)(state, input) == 4) /* 4D */
if (!fiveDimensionalInput) /* 4D */
{
batchSize = 1;
inputSlices = THCTensor_(size)(state, input, 0);
Expand All @@ -336,22 +343,21 @@ void THNN_(VolumetricDilatedMaxPooling_updateGradInput)(
}

gradOutput = THCTensor_(newContiguous)(state, gradOutput);
if (fiveDimensionalInput) {
// Collapse batch and feature dimensions
gradInput = THCTensor_(newFoldBatchDim)(state, gradInput);

THCTensor *old_gradOutput = gradOutput;
gradOutput = THCTensor_(newFoldBatchDim)(state, gradOutput);
THCTensor_(free)(state, old_gradOutput);
} else {
THCTensor_(retain)(state, gradInput);
}

// Collapse batch and feature dimensions
THCDeviceTensor<real, 4> cudaGradInput;
THCDeviceTensor<real, 4> cudaGradOutput;
if (THCTensor_(nDimension)(state, input) == 4)
{
cudaGradInput = toDeviceTensor<real, 4>(state, gradInput);
cudaGradOutput = toDeviceTensor<real, 4>(state, gradOutput);
}
else
{
cudaGradInput =
toDeviceTensor<real, 5>(state, gradInput).downcastOuter<4>();
cudaGradOutput =
toDeviceTensor<real, 5>(state, gradOutput).downcastOuter<4>();
}
cudaGradInput = toDeviceTensor<real, 4>(state, gradInput);
cudaGradOutput = toDeviceTensor<real, 4>(state, gradOutput);

THLongStorage *indicesSize = THLongStorage_newWithSize(4);
int64_t indicesSizeRaw[4] = { batchSize * inputSlices,
Expand Down Expand Up @@ -388,6 +394,7 @@ void THNN_(VolumetricDilatedMaxPooling_updateGradInput)(
}

// cleanup
THCTensor_(free)(state, gradInput);
THCTensor_(free)(state, gradOutput);
THCIndexTensor_(free)(state, indices1);
}
Expand Down
Loading