diff --git a/include/Weights_Reader/reader_weights.hpp b/include/Weights_Reader/reader_weights.hpp index 8249f1b7..34eecfd3 100644 --- a/include/Weights_Reader/reader_weights.hpp +++ b/include/Weights_Reader/reader_weights.hpp @@ -13,3 +13,4 @@ void extract_values_from_json(const json& j, std::vector& values); void parse_json_shape(const json& j, std::vector& shape, size_t dim = 0); Tensor create_tensor_from_json(const json& j, Type type); +void extract_bias_from_json(const json& j, std::vector& bias); diff --git a/src/Weights_Reader/reader_weights.cpp b/src/Weights_Reader/reader_weights.cpp index 3a299b1a..2138ce6c 100644 --- a/src/Weights_Reader/reader_weights.cpp +++ b/src/Weights_Reader/reader_weights.cpp @@ -16,9 +16,6 @@ json read_json(const std::string& filename) { ifs.seekg(0, std::ios::end); size_t size = ifs.tellg(); - if (size == 0) { - ifs.close(); - } ifs.seekg(0, std::ios::beg); std::vector buffer(size); @@ -56,8 +53,6 @@ void extract_values_without_bias(const json& j, std::vector& values) { std::cout << "Bias size: " << bias_size << std::endl; if (temp_values.size() >= bias_size) { values.assign(temp_values.begin(), temp_values.end() - bias_size); - } else { - throw std::runtime_error("Extracted values are smaller than bias size."); } std::cout << "Values size after extraction: " << values.size() << std::endl; } @@ -94,8 +89,6 @@ void extract_bias_from_json(const json& j, std::vector& bias) { } } } - } else { - throw std::runtime_error("Input JSON structure should be an array."); } } diff --git a/test/model_read/model_read.cpp b/test/model_read/model_read.cpp index 464775f7..80ceb080 100644 --- a/test/model_read/model_read.cpp +++ b/test/model_read/model_read.cpp @@ -135,3 +135,40 @@ TEST(ExtractValuesWithoutBiasTest, HandlesComplexNestedCase) { std::vector expected = {1.0, 2.0, 3.0, 4.0}; EXPECT_EQ(values, expected); } + +TEST(ExtractBiasFromJsonTests, extract_bias) { + json j = json::array({{1.0, 2.0}, {3.0, 4.0}}); + std::vector values; + extract_bias_from_json(j, values); + std::vector expected = {3.0, 4.0}; + EXPECT_EQ(values, expected); +} + +TEST(ExtractBiasFromJsonTests, extract_bias_error_array) { + json j = json::array({{1.0, 2.0}, {3.0, 4.0}}); + std::vector values; + extract_bias_from_json(j, values); + std::vector expected = {3.0, 4.0}; + EXPECT_EQ(values, expected); +} + +TEST(CreateTensorFromJsonTest, SimpleTensor) { + json j = json::array({{1.0, 2.0}, {3.0, 4.0}}); + EXPECT_NO_THROW(Tensor tensor = create_tensor_from_json(j, Type::kFloat);); +} + +TEST(CreateTensorFromJsonTest, SimpleTensorCheckBias) { + json j = json::array({{1.0, 2.0}, {3.0, 4.0}}); + Tensor tensor = create_tensor_from_json(j, Type::kFloat); + + EXPECT_EQ(tensor.get_bias().size(), 2); + EXPECT_EQ(tensor.get_bias()[0], 3.0); + EXPECT_EQ(tensor.get_bias()[1], 4.0); +} + +TEST(CreateTensorFromJsonTest, SimpleTensorCheckWeights) { + json j = json::array({{1.0, 2.0}, {3.0, 4.0}}); + Tensor tensor = create_tensor_from_json(j, Type::kFloat); + + EXPECT_EQ(tensor.get({1}), 2.0); +}