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 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
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<!-- ---------------------
v1.3.2
--------------------- -->
## v1.3.2 - 8-11-2024

### Fixed

- When slicing a `DTensor` along `axis=2`, update the pointer to matrices
- We got rid of warning `DTensor<T>::createRandomTensor`


<!-- ---------------------
v1.3.1
--------------------- -->
## v1.3.1 - 8-11-2024
## v1.3.1 - 7-11-2024

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ tests() {
if [ -z "${hwInfoOrin}" ]; then

# -- run compute sanitizer
cd ./build/test
pushd ./build/test
mem=$(/usr/local/cuda/bin/compute-sanitizer --tool memcheck --leak-check=full ./device_test)
grep "0 errors" <<< "$mem"
cd ../..
popd

# ------------------------------------
# Run example executable
Expand Down
9 changes: 3 additions & 6 deletions include/tensor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,8 @@ DTensor<T> DTensor<T>::createRandomTensor(size_t numRows, size_t numCols, size_t
auto randVec = generateIntRandomVector(numRows * numCols * numMats, low, hi);
DTensor<T> a(randVec, numRows, numCols, numMats);
return a;
} else {
throw std::invalid_argument("[createRandomTensor] unsupported type T");
}
throw std::invalid_argument("[createRandomTensor] unsupported type T");
}

template<typename T>
Expand Down Expand Up @@ -640,11 +639,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 +660,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
45 changes: 44 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,46 @@ TEST_F(TensorTest, tensorAddAB) {
tensorAddAB<float>();
}

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

TEMPLATE_WITH_TYPE_T
void tensorSliceAxis2PtrMatrices() {
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, tensorSliceAxis2PtrMatrices) {
tensorSliceAxis2PtrMatrices<float>();
tensorSliceAxis2PtrMatrices<double>();
tensorSliceAxis2PtrMatrices<int>();
}

/* ---------------------------------------
* Tensor: slice ptrMatrices
* axis = 0 and 1
* --------------------------------------- */

TEMPLATE_WITH_TYPE_T
void tensorSliceAxis01PtrMatrices() {
std::vector<T> dataA = TENSOR_DATA_234A;
DTensor<T> d_A(dataA, 2, 3, 4);
DTensor<T> d_ASlice0(d_A, 0, 0, 1);
EXPECT_TRUE(!d_ASlice0.ptrMatrices());
DTensor<T> d_ASlice1(d_A, 1, 0, 2);
EXPECT_TRUE(!d_ASlice0.ptrMatrices());
}

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

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