using calloc instead of malloc for deterministic hashing (#16326)

This commit is contained in:
Eddy Kim 2023-03-21 00:50:14 +09:00 committed by GitHub
parent d1a7b0e3c0
commit 8e464e992e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -82,9 +82,9 @@ size_t ngraph::hash_combine(const std::vector<size_t>& list) {
}
void* ngraph::ngraph_malloc(size_t size) {
auto ptr = malloc(size);
auto ptr = calloc(size, 1);
if (size != 0 && !ptr) {
NGRAPH_ERR << "malloc failed to allocate memory of size " << size;
NGRAPH_ERR << "calloc failed to allocate memory of size " << size;
throw std::bad_alloc();
}
return ptr;

View File

@ -10,6 +10,7 @@
#include "openvino/pass/serialize.hpp"
#include "openvino/util/file_util.hpp"
#include "read_ir.hpp"
#include "transformations/hash.hpp"
#include "util/test_common.hpp"
class SerializationDeterministicityTest : public ov::test::TestsCommon {
@ -83,6 +84,23 @@ TEST_F(SerializationDeterministicityTest, ModelWithMultipleLayers) {
ASSERT_TRUE(files_equal(bin_1, bin_2));
}
TEST_F(SerializationDeterministicityTest, Hash) {
const std::string model =
CommonTestUtils::getModelFromTestModelZoo(ov::util::path_join({SERIALIZED_ZOO, "ir/addmul_abc.onnx"}));
uint64_t seed1 = 0;
uint64_t seed2 = 0;
{
auto expected = ov::test::readModel(model, "");
ov::pass::Hash(seed1).run_on_model(expected);
}
{
auto expected = ov::test::readModel(model, "");
ov::pass::Hash(seed2).run_on_model(expected);
}
ASSERT_TRUE(seed1 == seed2);
}
#endif
TEST_F(SerializationDeterministicityTest, ModelWithMultipleOutputs) {