Skip to content

Commit 788b198

Browse files
committed
Add ov version parcer. Add ov version comparison to verify cache
1 parent 792ddf3 commit 788b198

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (C) 2018-2025 Intel Corporation
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
#pragma once
5+
6+
#include <string>
7+
#include <regex>
8+
#include <sstream>
9+
#include <stdexcept>
10+
11+
namespace ov::util {
12+
struct OVVersion {
13+
int major = 0;
14+
int minor = 0;
15+
int patch = 0;
16+
int tweak = 0;
17+
int build = 0;
18+
19+
OVVersion() = default;
20+
21+
OVVersion(const std::string& version_str) {
22+
// Pattern: MAJOR.MINOR.PATCH[.TWEAK]-BUILD-...
23+
std::regex full_pattern(R"(^([0-9]+)\.([0-9]+)\.([0-9]+)(\.([0-9]+))?\-([0-9]+)\-.*)");
24+
std::regex build_only_pattern(R"(^[0-9]+$)");
25+
std::smatch match;
26+
27+
if (std::regex_match(version_str, match, full_pattern)) {
28+
major = std::stoi(match[1].str());
29+
minor = std::stoi(match[2].str());
30+
patch = std::stoi(match[3].str());
31+
32+
// match[4] is the entire optional group (\.TWEAK), match[5] is TWEAK
33+
// the 4th version number is not used in OpenVINO, but used in OpenVINO extensions
34+
if (match[5].matched) {
35+
tweak = std::stoi(match[5].str());
36+
}
37+
38+
build = std::stoi(match[6].str());
39+
}
40+
else if (std::regex_match(version_str, build_only_pattern)) {
41+
// Parse as just a build number
42+
build = std::stoi(version_str);
43+
}
44+
else {
45+
throw std::invalid_argument("Failed to parse version: " + version_str);
46+
}
47+
}
48+
49+
OVVersion(const char* version_str) : OVVersion(std::string(version_str)) {};
50+
51+
// Comparison operators
52+
bool operator==(const OVVersion& other) const {
53+
return major == other.major &&
54+
minor == other.minor &&
55+
patch == other.patch &&
56+
tweak == other.tweak &&
57+
build == other.build;
58+
}
59+
60+
bool operator!=(const OVVersion& other) const {
61+
return !(*this == other);
62+
}
63+
64+
bool operator<(const OVVersion& other) const {
65+
if (major != other.major) return major < other.major;
66+
if (minor != other.minor) return minor < other.minor;
67+
if (patch != other.patch) return patch < other.patch;
68+
if (tweak != other.tweak) return tweak < other.tweak;
69+
return build < other.build;
70+
}
71+
72+
bool operator>(const OVVersion& other) const {
73+
return other < *this;
74+
}
75+
76+
bool operator<=(const OVVersion& other) const {
77+
return !(other < *this);
78+
}
79+
80+
bool operator>=(const OVVersion& other) const {
81+
return !(*this < other);
82+
}
83+
};
84+
85+
bool is_compiled_blob_compatible(const OVVersion& compiled_blob_version, const OVVersion& runtime_version) {
86+
if (compiled_blob_version > runtime_version) {
87+
return false;
88+
}
89+
90+
// Major version must be equal
91+
if (compiled_blob_version.major != runtime_version.major) {
92+
return false;
93+
}
94+
95+
// compiled blob minor version can be lower than runtime minor version up to 5
96+
if (runtime_version.minor - compiled_blob_version.minor >= 5) {
97+
return false;
98+
}
99+
100+
return true;
101+
}
102+
} // namespace ov::util

src/inference/src/dev/core_impl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "openvino/util/common_util.hpp"
3232
#include "openvino/util/file_util.hpp"
3333
#include "openvino/util/log.hpp"
34+
#include "openvino/util/ov_version.hpp"
3435
#include "openvino/util/shared_object.hpp"
3536
#include "openvino/util/variant_visitor.hpp"
3637
#include "openvino/util/xml_parse_utils.hpp"
@@ -1552,7 +1553,8 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
15521553
"Original model runtime properties have been changed, not supported anymore!");
15531554
}
15541555
} else {
1555-
if (header.get_openvino_version() != ov::get_openvino_version().buildNumber) {
1556+
if (!ov::util::is_compiled_blob_compatible(header.get_openvino_version(),
1557+
ov::get_openvino_version().buildNumber)) {
15561558
// Build number mismatch, don't use this cache
15571559
OPENVINO_THROW("Version does not match");
15581560
}

0 commit comments

Comments
 (0)