Added support for RowMajor layout to the tensor patch extraction cofde.

This commit is contained in:
Benoit Steiner 2015-02-25 13:29:12 -08:00
parent eb21a8173e
commit 1cfd51908c
2 changed files with 202 additions and 85 deletions

View File

@ -99,12 +99,10 @@ struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device) EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
: m_impl(op.expression(), device) : m_impl(op.expression(), device)
{ {
// Only column major tensors are supported for now.
EIGEN_STATIC_ASSERT((static_cast<int>(Layout) == static_cast<int>(ColMajor)), YOU_MADE_A_PROGRAMMING_MISTAKE);
Index num_patches = 1; Index num_patches = 1;
const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions(); const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
const PatchDim& patch_dims = op.patch_dims(); const PatchDim& patch_dims = op.patch_dims();
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
for (int i = 0; i < NumDims-1; ++i) { for (int i = 0; i < NumDims-1; ++i) {
m_dimensions[i] = patch_dims[i]; m_dimensions[i] = patch_dims[i];
num_patches *= (input_dims[i] - patch_dims[i] + 1); num_patches *= (input_dims[i] - patch_dims[i] + 1);
@ -121,6 +119,24 @@ struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device>
for (int i = 1; i < NumDims; ++i) { for (int i = 1; i < NumDims; ++i) {
m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1]; m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
} }
} else {
for (int i = 0; i < NumDims-1; ++i) {
m_dimensions[i+1] = patch_dims[i];
num_patches *= (input_dims[i] - patch_dims[i] + 1);
}
m_dimensions[0] = num_patches;
m_inputStrides[NumDims-2] = 1;
m_patchStrides[NumDims-2] = 1;
for (int i = NumDims-3; i >= 0; --i) {
m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
m_patchStrides[i] = m_patchStrides[i+1] * (input_dims[i+1] - patch_dims[i+1] + 1);
}
m_outputStrides[NumDims-1] = 1;
for (int i = NumDims-2; i >= 0; --i) {
m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
}
}
} }
typedef typename XprType::CoeffReturnType CoeffReturnType; typedef typename XprType::CoeffReturnType CoeffReturnType;
@ -139,12 +155,13 @@ struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
{ {
Index output_stride_index = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? NumDims - 1 : 0;
// Find the location of the first element of the patch. // Find the location of the first element of the patch.
Index patchIndex = index / m_outputStrides[NumDims - 1]; Index patchIndex = index / m_outputStrides[output_stride_index];
// Find the offset of the element wrt the location of the first element. // Find the offset of the element wrt the location of the first element.
Index patchOffset = index - patchIndex * m_outputStrides[NumDims - 1]; Index patchOffset = index - patchIndex * m_outputStrides[output_stride_index];
Index inputIndex = 0; Index inputIndex = 0;
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
for (int i = NumDims - 2; i > 0; --i) { for (int i = NumDims - 2; i > 0; --i) {
const Index patchIdx = patchIndex / m_patchStrides[i]; const Index patchIdx = patchIndex / m_patchStrides[i];
patchIndex -= patchIdx * m_patchStrides[i]; patchIndex -= patchIdx * m_patchStrides[i];
@ -152,6 +169,15 @@ struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device>
patchOffset -= offsetIdx * m_outputStrides[i]; patchOffset -= offsetIdx * m_outputStrides[i];
inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i]; inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
} }
} else {
for (int i = 0; i < NumDims - 2; ++i) {
const Index patchIdx = patchIndex / m_patchStrides[i];
patchIndex -= patchIdx * m_patchStrides[i];
const Index offsetIdx = patchOffset / m_outputStrides[i+1];
patchOffset -= offsetIdx * m_outputStrides[i+1];
inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
}
}
inputIndex += (patchIndex + patchOffset); inputIndex += (patchIndex + patchOffset);
return m_impl.coeff(inputIndex); return m_impl.coeff(inputIndex);
} }
@ -163,13 +189,15 @@ struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device>
EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE) EIGEN_STATIC_ASSERT(packetSize > 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
eigen_assert(index+packetSize-1 < dimensions().TotalSize()); eigen_assert(index+packetSize-1 < dimensions().TotalSize());
Index output_stride_index = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? NumDims - 1 : 0;
Index indices[2] = {index, index + packetSize - 1}; Index indices[2] = {index, index + packetSize - 1};
Index patchIndices[2] = {indices[0] / m_outputStrides[NumDims - 1], Index patchIndices[2] = {indices[0] / m_outputStrides[output_stride_index],
indices[1] / m_outputStrides[NumDims - 1]}; indices[1] / m_outputStrides[output_stride_index]};
Index patchOffsets[2] = {indices[0] - patchIndices[0] * m_outputStrides[NumDims - 1], Index patchOffsets[2] = {indices[0] - patchIndices[0] * m_outputStrides[output_stride_index],
indices[1] - patchIndices[1] * m_outputStrides[NumDims - 1]}; indices[1] - patchIndices[1] * m_outputStrides[output_stride_index]};
Index inputIndices[2] = {0, 0}; Index inputIndices[2] = {0, 0};
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
for (int i = NumDims - 2; i > 0; --i) { for (int i = NumDims - 2; i > 0; --i) {
const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i], const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i],
patchIndices[1] / m_patchStrides[i]}; patchIndices[1] / m_patchStrides[i]};
@ -184,6 +212,22 @@ struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device>
inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i]; inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i]; inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
} }
} else {
for (int i = 0; i < NumDims - 2; ++i) {
const Index patchIdx[2] = {patchIndices[0] / m_patchStrides[i],
patchIndices[1] / m_patchStrides[i]};
patchIndices[0] -= patchIdx[0] * m_patchStrides[i];
patchIndices[1] -= patchIdx[1] * m_patchStrides[i];
const Index offsetIdx[2] = {patchOffsets[0] / m_outputStrides[i+1],
patchOffsets[1] / m_outputStrides[i+1]};
patchOffsets[0] -= offsetIdx[0] * m_outputStrides[i+1];
patchOffsets[1] -= offsetIdx[1] * m_outputStrides[i+1];
inputIndices[0] += (patchIdx[0] + offsetIdx[0]) * m_inputStrides[i];
inputIndices[1] += (patchIdx[1] + offsetIdx[1]) * m_inputStrides[i];
}
}
inputIndices[0] += (patchIndices[0] + patchOffsets[0]); inputIndices[0] += (patchIndices[0] + patchOffsets[0]);
inputIndices[1] += (patchIndices[1] + patchOffsets[1]); inputIndices[1] += (patchIndices[1] + patchOffsets[1]);
@ -205,29 +249,50 @@ struct TensorEvaluator<const TensorPatchOp<PatchDim, ArgType>, Device>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(const array<Index, NumDims>& coords) const EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(const array<Index, NumDims>& coords) const
{ {
Index patch_coord_idx = Layout == ColMajor ? NumDims - 1 : 0;
// Location of the first element of the patch. // Location of the first element of the patch.
const Index patchIndex = coords[NumDims - 1]; const Index patchIndex = coords[patch_coord_idx];
if (TensorEvaluator<ArgType, Device>::CoordAccess) { if (TensorEvaluator<ArgType, Device>::CoordAccess) {
array<Index, NumDims-1> inputCoords; array<Index, NumDims-1> inputCoords;
if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
for (int i = NumDims - 2; i > 0; --i) { for (int i = NumDims - 2; i > 0; --i) {
const Index patchIdx = patchIndex / m_patchStrides[i]; const Index patchIdx = patchIndex / m_patchStrides[i];
patchIndex -= patchIdx * m_patchStrides[i]; patchIndex -= patchIdx * m_patchStrides[i];
const Index offsetIdx = coords[i]; const Index offsetIdx = coords[i];
inputCoords[i] = coords[i] + patchIdx; inputCoords[i] = coords[i] + patchIdx;
} }
inputCoords[0] = (patchIndex + coords[0]); } else {
for (int i = 0; i < NumDims - 2; ++i) {
const Index patchIdx = patchIndex / m_patchStrides[i];
patchIndex -= patchIdx * m_patchStrides[i];
const Index offsetIdx = coords[i+1];
inputCoords[i] = coords[i+1] + patchIdx;
}
}
Index coords_idx = Layout == ColMajor ? 0 : NumDims - 1;
inputCoords[0] = (patchIndex + coords[coords_idx]);
return m_impl.coeff(inputCoords); return m_impl.coeff(inputCoords);
} }
else { else {
Index inputIndex = 0; Index inputIndex = 0;
if (Layout == ColMajor) {
for (int i = NumDims - 2; i > 0; --i) { for (int i = NumDims - 2; i > 0; --i) {
const Index patchIdx = patchIndex / m_patchStrides[i]; const Index patchIdx = patchIndex / m_patchStrides[i];
patchIndex -= patchIdx * m_patchStrides[i]; patchIndex -= patchIdx * m_patchStrides[i];
const Index offsetIdx = coords[i]; const Index offsetIdx = coords[i];
inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i]; inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
} }
inputIndex += (patchIndex + coords[0]); } else {
for (int i = 0; i < NumDims - 2; ++i) {
const Index patchIdx = patchIndex / m_patchStrides[i];
patchIndex -= patchIdx * m_patchStrides[i];
const Index offsetIdx = coords[i+1];
inputIndex += (patchIdx + offsetIdx) * m_inputStrides[i];
}
}
Index coords_idx = Layout == ColMajor ? 0 : NumDims - 1;
inputIndex += (patchIndex + coords[coords_idx]);
return m_impl.coeff(inputIndex); return m_impl.coeff(inputIndex);
} }
} }

View File

@ -13,24 +13,34 @@
using Eigen::Tensor; using Eigen::Tensor;
template<int DataLayout>
static void test_simple_patch() static void test_simple_patch()
{ {
Tensor<float, 4> tensor(2,3,5,7); Tensor<float, 4, DataLayout> tensor(2,3,5,7);
tensor.setRandom(); tensor.setRandom();
array<ptrdiff_t, 4> patch_dims; array<ptrdiff_t, 4> patch_dims;
patch_dims[0] = 1; patch_dims[0] = 1;
patch_dims[1] = 1; patch_dims[1] = 1;
patch_dims[2] = 1; patch_dims[2] = 1;
patch_dims[3] = 1; patch_dims[3] = 1;
Tensor<float, 5> no_patch; Tensor<float, 5, DataLayout> no_patch;
no_patch = tensor.extract_patches(patch_dims); no_patch = tensor.extract_patches(patch_dims);
if (DataLayout == ColMajor) {
VERIFY_IS_EQUAL(no_patch.dimension(0), 1); VERIFY_IS_EQUAL(no_patch.dimension(0), 1);
VERIFY_IS_EQUAL(no_patch.dimension(1), 1); VERIFY_IS_EQUAL(no_patch.dimension(1), 1);
VERIFY_IS_EQUAL(no_patch.dimension(2), 1); VERIFY_IS_EQUAL(no_patch.dimension(2), 1);
VERIFY_IS_EQUAL(no_patch.dimension(3), 1); VERIFY_IS_EQUAL(no_patch.dimension(3), 1);
VERIFY_IS_EQUAL(no_patch.dimension(4), tensor.size()); VERIFY_IS_EQUAL(no_patch.dimension(4), tensor.size());
} else {
VERIFY_IS_EQUAL(no_patch.dimension(0), tensor.size());
VERIFY_IS_EQUAL(no_patch.dimension(1), 1);
VERIFY_IS_EQUAL(no_patch.dimension(2), 1);
VERIFY_IS_EQUAL(no_patch.dimension(3), 1);
VERIFY_IS_EQUAL(no_patch.dimension(4), 1);
}
for (int i = 0; i < tensor.size(); ++i) { for (int i = 0; i < tensor.size(); ++i) {
VERIFY_IS_EQUAL(tensor.data()[i], no_patch.data()[i]); VERIFY_IS_EQUAL(tensor.data()[i], no_patch.data()[i]);
@ -40,14 +50,22 @@ static void test_simple_patch()
patch_dims[1] = 3; patch_dims[1] = 3;
patch_dims[2] = 5; patch_dims[2] = 5;
patch_dims[3] = 7; patch_dims[3] = 7;
Tensor<float, 5> single_patch; Tensor<float, 5, DataLayout> single_patch;
single_patch = tensor.extract_patches(patch_dims); single_patch = tensor.extract_patches(patch_dims);
if (DataLayout == ColMajor) {
VERIFY_IS_EQUAL(single_patch.dimension(0), 2); VERIFY_IS_EQUAL(single_patch.dimension(0), 2);
VERIFY_IS_EQUAL(single_patch.dimension(1), 3); VERIFY_IS_EQUAL(single_patch.dimension(1), 3);
VERIFY_IS_EQUAL(single_patch.dimension(2), 5); VERIFY_IS_EQUAL(single_patch.dimension(2), 5);
VERIFY_IS_EQUAL(single_patch.dimension(3), 7); VERIFY_IS_EQUAL(single_patch.dimension(3), 7);
VERIFY_IS_EQUAL(single_patch.dimension(4), 1); VERIFY_IS_EQUAL(single_patch.dimension(4), 1);
} else {
VERIFY_IS_EQUAL(single_patch.dimension(0), 1);
VERIFY_IS_EQUAL(single_patch.dimension(1), 2);
VERIFY_IS_EQUAL(single_patch.dimension(2), 3);
VERIFY_IS_EQUAL(single_patch.dimension(3), 5);
VERIFY_IS_EQUAL(single_patch.dimension(4), 7);
}
for (int i = 0; i < tensor.size(); ++i) { for (int i = 0; i < tensor.size(); ++i) {
VERIFY_IS_EQUAL(tensor.data()[i], single_patch.data()[i]); VERIFY_IS_EQUAL(tensor.data()[i], single_patch.data()[i]);
@ -57,23 +75,40 @@ static void test_simple_patch()
patch_dims[1] = 2; patch_dims[1] = 2;
patch_dims[2] = 2; patch_dims[2] = 2;
patch_dims[3] = 1; patch_dims[3] = 1;
Tensor<float, 5> twod_patch; Tensor<float, 5, DataLayout> twod_patch;
twod_patch = tensor.extract_patches(patch_dims); twod_patch = tensor.extract_patches(patch_dims);
if (DataLayout == ColMajor) {
VERIFY_IS_EQUAL(twod_patch.dimension(0), 1); VERIFY_IS_EQUAL(twod_patch.dimension(0), 1);
VERIFY_IS_EQUAL(twod_patch.dimension(1), 2); VERIFY_IS_EQUAL(twod_patch.dimension(1), 2);
VERIFY_IS_EQUAL(twod_patch.dimension(2), 2); VERIFY_IS_EQUAL(twod_patch.dimension(2), 2);
VERIFY_IS_EQUAL(twod_patch.dimension(3), 1); VERIFY_IS_EQUAL(twod_patch.dimension(3), 1);
VERIFY_IS_EQUAL(twod_patch.dimension(4), 2*2*4*7); VERIFY_IS_EQUAL(twod_patch.dimension(4), 2*2*4*7);
} else {
VERIFY_IS_EQUAL(twod_patch.dimension(0), 2*2*4*7);
VERIFY_IS_EQUAL(twod_patch.dimension(1), 1);
VERIFY_IS_EQUAL(twod_patch.dimension(2), 2);
VERIFY_IS_EQUAL(twod_patch.dimension(3), 2);
VERIFY_IS_EQUAL(twod_patch.dimension(4), 1);
}
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) { for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 4; ++k) { for (int k = 0; k < 4; ++k) {
for (int l = 0; l < 7; ++l) { for (int l = 0; l < 7; ++l) {
int patch_loc = i + 2 * (j + 2 * (k + 4 * l)); int patch_loc;
if (DataLayout == ColMajor) {
patch_loc = i + 2 * (j + 2 * (k + 4 * l));
} else {
patch_loc = l + 7 * (k + 4 * (j + 2 * i));
}
for (int x = 0; x < 2; ++x) { for (int x = 0; x < 2; ++x) {
for (int y = 0; y < 2; ++y) { for (int y = 0; y < 2; ++y) {
if (DataLayout == ColMajor) {
VERIFY_IS_EQUAL(tensor(i,j+x,k+y,l), twod_patch(0,x,y,0,patch_loc)); VERIFY_IS_EQUAL(tensor(i,j+x,k+y,l), twod_patch(0,x,y,0,patch_loc));
} else {
VERIFY_IS_EQUAL(tensor(i,j+x,k+y,l), twod_patch(patch_loc,0,x,y,0));
}
} }
} }
} }
@ -85,24 +120,41 @@ static void test_simple_patch()
patch_dims[1] = 2; patch_dims[1] = 2;
patch_dims[2] = 3; patch_dims[2] = 3;
patch_dims[3] = 5; patch_dims[3] = 5;
Tensor<float, 5> threed_patch; Tensor<float, 5, DataLayout> threed_patch;
threed_patch = tensor.extract_patches(patch_dims); threed_patch = tensor.extract_patches(patch_dims);
if (DataLayout == ColMajor) {
VERIFY_IS_EQUAL(threed_patch.dimension(0), 1); VERIFY_IS_EQUAL(threed_patch.dimension(0), 1);
VERIFY_IS_EQUAL(threed_patch.dimension(1), 2); VERIFY_IS_EQUAL(threed_patch.dimension(1), 2);
VERIFY_IS_EQUAL(threed_patch.dimension(2), 3); VERIFY_IS_EQUAL(threed_patch.dimension(2), 3);
VERIFY_IS_EQUAL(threed_patch.dimension(3), 5); VERIFY_IS_EQUAL(threed_patch.dimension(3), 5);
VERIFY_IS_EQUAL(threed_patch.dimension(4), 2*2*3*3); VERIFY_IS_EQUAL(threed_patch.dimension(4), 2*2*3*3);
} else {
VERIFY_IS_EQUAL(threed_patch.dimension(0), 2*2*3*3);
VERIFY_IS_EQUAL(threed_patch.dimension(1), 1);
VERIFY_IS_EQUAL(threed_patch.dimension(2), 2);
VERIFY_IS_EQUAL(threed_patch.dimension(3), 3);
VERIFY_IS_EQUAL(threed_patch.dimension(4), 5);
}
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) { for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 3; ++k) { for (int k = 0; k < 3; ++k) {
for (int l = 0; l < 3; ++l) { for (int l = 0; l < 3; ++l) {
int patch_loc = i + 2 * (j + 2 * (k + 3 * l)); int patch_loc;
if (DataLayout == ColMajor) {
patch_loc = i + 2 * (j + 2 * (k + 3 * l));
} else {
patch_loc = l + 3 * (k + 3 * (j + 2 * i));
}
for (int x = 0; x < 2; ++x) { for (int x = 0; x < 2; ++x) {
for (int y = 0; y < 3; ++y) { for (int y = 0; y < 3; ++y) {
for (int z = 0; z < 5; ++z) { for (int z = 0; z < 5; ++z) {
if (DataLayout == ColMajor) {
VERIFY_IS_EQUAL(tensor(i,j+x,k+y,l+z), threed_patch(0,x,y,z,patch_loc)); VERIFY_IS_EQUAL(tensor(i,j+x,k+y,l+z), threed_patch(0,x,y,z,patch_loc));
} else {
VERIFY_IS_EQUAL(tensor(i,j+x,k+y,l+z), threed_patch(patch_loc,0,x,y,z));
}
} }
} }
} }
@ -111,10 +163,10 @@ static void test_simple_patch()
} }
} }
} }
void test_cxx11_tensor_patch() void test_cxx11_tensor_patch()
{ {
CALL_SUBTEST(test_simple_patch()); CALL_SUBTEST(test_simple_patch<ColMajor>());
CALL_SUBTEST(test_simple_patch<RowMajor>());
// CALL_SUBTEST(test_expr_shuffling()); // CALL_SUBTEST(test_expr_shuffling());
} }