Skip to content

hotfix / include ptrMatrices in matrix-axis slices #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 2 additions & 4 deletions include/tensor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -640,11 +640,13 @@ template<typename T>
DTensor<T>::DTensor(const DTensor<T> &other, size_t axis, size_t from, size_t to) {
if (from > to) throw std::invalid_argument("from > to");
size_t offset = 0, len = to - from + 1;
m_d_ptrMatrices = nullptr;
if (axis == 2) {
offset = other.m_numRows * other.m_numCols * from;
m_numRows = other.m_numRows;
m_numCols = other.m_numCols;
m_numMats = len;
m_d_ptrMatrices = other.m_d_ptrMatrices + from;
Copy link
Member

Choose a reason for hiding this comment

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

This line should be here (inside if (axis == 2))

} else if (axis == 1) {
offset = other.m_numRows * from;
m_numRows = other.m_numRows;
Expand All @@ -659,10 +661,6 @@ DTensor<T>::DTensor(const DTensor<T> &other, size_t axis, size_t from, size_t to
m_d_data = other.m_d_data + offset;
m_doDestroyData = false;
m_doDestroyPtrMatrices = false;
if (axis != 2) {
Copy link
Member

Choose a reason for hiding this comment

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

I removed this because I put m_d_ptrMatrices = nullptr; at the beginning.

// m_d_ptrMatrices is not needed for vectors and matrices
m_d_ptrMatrices = nullptr;
}
}

template<typename T>
Expand Down
23 changes: 22 additions & 1 deletion test/testTensor.cu
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ void tensorSlicingConstructorAxis2() {
EXPECT_EQ(3, tensSlice.numCols());
EXPECT_EQ(2, tensSlice.numMats());
EXPECT_EQ(tens.raw(), tensSlice.raw()); // it is indeed a slice
EXPECT_TRUE(tensSlice.ptrMatrices() != nullptr);
}

TEST_F(TensorTest, tensorSlicingConstructorAxis2) {
Expand All @@ -215,6 +216,7 @@ void tensorSlicingConstructorAxis1() {
EXPECT_EQ(2, tenzSlice.numRows());
EXPECT_EQ(2, tenzSlice.numCols());
EXPECT_EQ(1, tenzSlice.numMats());
EXPECT_TRUE(tenzSlice.ptrMatrices() == nullptr);
std::vector<T> expected = {3, 4, 5, 6};
std::vector<T> tenzSliceDown(4);
tenzSlice.download(tenzSliceDown);
Expand All @@ -229,7 +231,7 @@ TEST_F(TensorTest, tensorSlicingConstructorAxis1) {

/* ---------------------------------------
* Tensor: Slicing constructor
* axis = 0 (columns)
* axis = 0 (rows)
* --------------------------------------- */

TEMPLATE_WITH_TYPE_T
Expand All @@ -240,6 +242,7 @@ void tensorSlicingConstructorAxis0() {
EXPECT_EQ(2, tenzSlice.numRows());
EXPECT_EQ(1, tenzSlice.numCols());
EXPECT_EQ(1, tenzSlice.numMats());
EXPECT_TRUE(tenzSlice.ptrMatrices() == nullptr);
std::vector<T> expected = {3, 4};
std::vector<T> tenzSliceDown(2);
tenzSlice.download(tenzSliceDown);
Expand Down Expand Up @@ -738,6 +741,24 @@ TEST_F(TensorTest, tensorAddAB) {
tensorAddAB<float>();
}

/* ---------------------------------------
* Tensor: slice ptrMatrices
* axis = 2 (matrices)
* --------------------------------------- */

TEMPLATE_WITH_TYPE_T
void tensorSlicePtrMatrices() {
std::vector<T> dataA = TENSOR_DATA_234A;
DTensor<T> d_A(dataA, 2, 3, 4);
DTensor<T> d_ASlice(d_A, 2, 2, 3);
EXPECT_TRUE(d_ASlice.ptrMatrices() == d_A.ptrMatrices() + 2);
}

TEST_F(TensorTest, tensorSlicePtrMatrices) {
tensorSlicePtrMatrices<float>();
tensorSlicePtrMatrices<double>();
}

/* ---------------------------------------
* Tensor: getRows
* --------------------------------------- */
Expand Down