Skip to content

Commit

Permalink
Ignore empty tensor for hash calculation (openvinotoolkit#15282)
Browse files Browse the repository at this point in the history
* Ignore empty tensor for hash calculation

* Added test
  • Loading branch information
ilyachur authored Jan 25, 2023
1 parent f12ffc0 commit a3fafda
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
24 changes: 13 additions & 11 deletions src/inference/src/compilation_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,19 @@ std::string NetworkCompilationContext::computeHash(const std::string& modelStr,
seed = hash_combine(seed, modelStr);

// tensor data
seed = hash_combine(seed, tensor.get_size());

auto ptr = static_cast<size_t*>(tensor.data());
size_t size = tensor.get_size() / sizeof(size_t);
for (size_t i = 0; i < size; i++)
seed = hash_combine(seed, ptr[i]);
auto size_done = size * sizeof(size_t);
auto ptr_left = static_cast<uint8_t*>(tensor.data()) + size_done;
size_t size_left = tensor.get_size() - size_done;
for (size_t i = 0; i < size_left; i++)
seed = hash_combine(seed, ptr_left[i]);
if (tensor) {
seed = hash_combine(seed, tensor.get_size());

auto ptr = static_cast<size_t*>(tensor.data());
size_t size = tensor.get_size() / sizeof(size_t);
for (size_t i = 0; i < size; i++)
seed = hash_combine(seed, ptr[i]);
auto size_done = size * sizeof(size_t);
auto ptr_left = static_cast<uint8_t*>(tensor.data()) + size_done;
size_t size_left = tensor.get_size() - size_done;
for (size_t i = 0; i < size_left; i++)
seed = hash_combine(seed, ptr_left[i]);
}

// compile options
for (const auto& kvp : compileOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ class CompileModelLoadFromMemoryTestBase : public testing::WithParamInterface<co
virtual public SubgraphBaseTest,
virtual public OVPluginTestBase {
std::string m_cacheFolderName;
std::vector<std::uint8_t> weights_vector;

protected:
std::string m_modelName;
std::string m_weightsName;
std::string m_model;
ov::Tensor m_weights;
std::vector<std::uint8_t> weights_vector;


public:
static std::string getTestCaseName(testing::TestParamInfo<compileModelLoadFromMemoryParams> obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "ngraph_functions/builders.hpp"
#include "ngraph_functions/subgraph_builders.hpp"
#include "cpp_interfaces/interface/ie_internal_plugin_config.hpp"
#include "openvino/core/node_vector.hpp"
#include "openvino/op/parameter.hpp"

#define GTEST_COUT std::cout << "[ ] [ INFO ] "

Expand Down Expand Up @@ -452,6 +454,33 @@ TEST_P(CompileModelLoadFromMemoryTestBase, CanLoadFromMemoryWithoutExecption) {
run();
}

TEST_P(CompileModelLoadFromMemoryTestBase, CanLoadFromMemoryWithoutWeightsANdExecption) {
ngraph::pass::Manager manager;
std::shared_ptr<ov::Model> model;
{
auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{3, 1, 2});

auto mul = std::make_shared<ov::op::v1::Multiply>(data, data);

auto res = std::make_shared<ov::op::v0::Result>(mul);
model = std::make_shared<ov::Model>(ov::ResultVector{res}, ov::ParameterVector{data});
}

manager.register_pass<ov::pass::Serialize>(m_modelName, m_weightsName);
manager.run_passes(model);

try {
std::ifstream model_file(m_modelName, std::ios::binary);
std::stringstream ss;
ss << model_file.rdbuf();
m_model = ss.str();
} catch (const Exception& ex) {
GTEST_FAIL() << "Can't read xml file from: " << m_modelName << "\nException [" << ex.what() << "]" << std::endl;
}
m_weights = ov::Tensor();
run();
}

std::string CompiledKernelsCacheTest::getTestCaseName(testing::TestParamInfo<compileKernelsCacheParams> obj) {
auto param = obj.param;
std::string deviceName;
Expand Down

0 comments on commit a3fafda

Please sign in to comment.