Skip to content

Commit d28cdf8

Browse files
authored
Merge pull request #55 from GPUEngineering/hf/max_digits
Set precision in DTensor::saveToFile properly version 1.5.1 (quick fixes)
2 parents 4d88cd8 + 57fdd30 commit d28cdf8

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
<!-- ---------------------
9+
v1.5.1
10+
--------------------- -->
11+
## v1.5.1 - 29-11-2024
12+
13+
### Fixed
14+
15+
- Set precision in `DTensor::saveToFile` properly
16+
- `DTensor<T>::parseFromTextFile` throws `std::invalid_argument` if `T` is unsupported
17+
818
<!-- ---------------------
919
v1.5.0
1020
--------------------- -->

include/tensor.cuh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,9 @@ data_t<T> vectorFromFile(std::string path_to_file) {
629629
vecDataFromFile[i] = std::stoull(line.c_str());
630630
} else if constexpr (std::is_same_v<T, size_t>) {
631631
sscanf(line.c_str(), "%zu", &vecDataFromFile[i]);
632+
} else {
633+
throw std::invalid_argument("data type not supported");
632634
}
633-
// todo
634635

635636
if (++i == numElements) break;
636637
}
@@ -653,8 +654,7 @@ void DTensor<T>::saveToFile(std::string pathToFile) {
653654
file << numRows() << std::endl << numCols() << std::endl << numMats() << std::endl;
654655
std::vector<T> myData(numEl()); download(myData);
655656
if constexpr (std::is_floating_point<T>::value) {
656-
int prec = std::numeric_limits<T>::max_digits10 - 1;
657-
file << std::setprecision(prec);
657+
file << std::setprecision(std::numeric_limits<T>::max_digits10);
658658
}
659659
for(const T& el : myData) file << el << std::endl;
660660
}

test/testTensor.cu

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,35 @@ TEST_F(TensorTest, randomTensorCreation) {
122122

123123
TEMPLATE_WITH_TYPE_T
124124
void parseTensorFromFile() {
125-
size_t nR = 20, nC = 40, nM = 60;
126-
auto r = DTensor<T>::createRandomTensor(nR, nC, nM, -1, 1);
127-
std::string fName = "myTest.dtensor";
128-
r.saveToFile(fName);
129-
auto a = DTensor<T>::parseFromTextFile(fName);
130-
EXPECT_EQ(nR, a.numRows());
131-
EXPECT_EQ(nC, a.numCols());
132-
EXPECT_EQ(nM, a.numMats());
133-
auto diff = a - r;
134-
T err = diff.maxAbs();
135-
EXPECT_LT(err, 2*std::numeric_limits<T>::epsilon());
125+
size_t n_runs = 10;
126+
for (size_t i = 0; i < n_runs; i++) {
127+
size_t nR = 20, nC = 40, nM = 6;
128+
auto r = DTensor<T>::createRandomTensor(nR, nC, nM, -1, 1);
129+
std::string fName = "myTest.dtensor";
130+
r.saveToFile(fName);
131+
auto a = DTensor<T>::parseFromTextFile(fName);
132+
EXPECT_EQ(nR, a.numRows());
133+
EXPECT_EQ(nC, a.numCols());
134+
EXPECT_EQ(nM, a.numMats());
135+
auto diff = a - r;
136+
T err = diff.maxAbs();
137+
EXPECT_LT(err, 2 * std::numeric_limits<T>::epsilon());
138+
}
136139
}
137140

138141
TEST_F(TensorTest, parseTensorFromFile) {
139142
parseTensorFromFile<float>();
140143
parseTensorFromFile<double>();
141144
}
142145

146+
TEST_F(TensorTest, parseTensorUnsupportedDataType) {
147+
size_t nR = 20, nC = 40, nM = 60;
148+
auto r = DTensor<double>::createRandomTensor(nR, nC, nM, -1, 1);
149+
std::string fName = "myTest.dtensor";
150+
r.saveToFile(fName);
151+
EXPECT_THROW(DTensor<char>::parseFromTextFile(fName), std::invalid_argument);
152+
}
153+
143154
/* ---------------------------------------
144155
* Move constructor
145156
* --------------------------------------- */

0 commit comments

Comments
 (0)