Skip to content

Commit 888cc14

Browse files
authored
[CORE][CACHE][HASH] DYNAMIC_CODE_POLICY check on Windows (#29006)
### Details: - *Some customers can set policy ProcessDynamicCodePolicy to prohibit dynamic code. So need to disable JIT in such cases.* ### Tickets: - *162440*
1 parent c3ae4b0 commit 888cc14

File tree

4 files changed

+105
-77
lines changed

4 files changed

+105
-77
lines changed

src/common/util/include/openvino/util/common_util.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,13 @@ constexpr std::array<std::conditional_t<std::is_void_v<T>, std::common_type_t<Ar
180180
return {std::forward<Args>(args)...};
181181
}
182182

183+
#if defined(_WIN32)
184+
bool may_i_use_dynamic_code();
185+
#else
186+
constexpr bool may_i_use_dynamic_code() {
187+
return true;
188+
}
189+
#endif
190+
183191
} // namespace util
184192
} // namespace ov

src/common/util/src/common_util.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
#include <algorithm>
88

9+
#if defined(_WIN32)
10+
# include <windows.h>
11+
#endif
12+
913
std::string ov::util::to_lower(const std::string& s) {
1014
std::string rc = s;
1115
std::transform(rc.begin(), rc.end(), rc.begin(), ::tolower);
@@ -60,3 +64,12 @@ std::string ov::util::filter_lines_by_prefix(const std::string& str, const std::
6064
}
6165
return res.str();
6266
}
67+
68+
#if defined(_WIN32)
69+
bool ov::util::may_i_use_dynamic_code() {
70+
HANDLE handle = GetCurrentProcess();
71+
PROCESS_MITIGATION_DYNAMIC_CODE_POLICY dynamic_code_policy = {0};
72+
GetProcessMitigationPolicy(handle, ProcessDynamicCodePolicy, &dynamic_code_policy, sizeof(dynamic_code_policy));
73+
return dynamic_code_policy.ProhibitDynamicCode != TRUE;
74+
}
75+
#endif

src/core/reference/src/op/convert.cpp

+16-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#ifdef OV_CORE_USE_XBYAK_JIT
1010
# include "openvino/reference/utils/jit_generator.hpp"
11+
# include "openvino/util/common_util.hpp"
1112
#endif
1213

1314
#ifdef OV_CORE_USE_INTRINSICS
@@ -480,14 +481,15 @@ class jit_count_out_of_range : public jit::Generator {
480481
template <class Clamp, typename TI, typename TO>
481482
void convert_impl(const TI* arg, TO* out, size_t count) {
482483
#ifdef OV_CORE_USE_XBYAK_JIT
483-
if (auto converter = jit_convert_array::get<TI, TO, Clamp::enabled>()) {
484-
jit_convert_array::args_t args = {arg, out, count};
485-
converter(&args);
486-
} else
487-
#endif
488-
{
489-
Converter<TI, TO>::template apply<Clamp>(arg, out, count);
484+
if (util::may_i_use_dynamic_code()) {
485+
if (auto converter = jit_convert_array::get<TI, TO, Clamp::enabled>()) {
486+
jit_convert_array::args_t args = {arg, out, count};
487+
converter(&args);
488+
return;
489+
}
490490
}
491+
#endif // OV_CORE_USE_XBYAK_JIT
492+
Converter<TI, TO>::template apply<Clamp>(arg, out, count);
491493
}
492494
} // namespace
493495

@@ -544,11 +546,13 @@ void convert_from_bf16_to_f16_with_clamp(const bfloat16* arg, float16* out, size
544546

545547
size_t count_out_of_f16_range(const float* arg, size_t count) {
546548
#ifdef OV_CORE_USE_XBYAK_JIT
547-
if (auto converter = jit_count_out_of_range::get<float, float16>()) {
548-
size_t num_out_of_range = 0;
549-
jit_count_out_of_range::args_t args = {arg, &num_out_of_range, count};
550-
converter(&args);
551-
return num_out_of_range;
549+
if (util::may_i_use_dynamic_code()) {
550+
if (auto converter = jit_count_out_of_range::get<float, float16>()) {
551+
size_t num_out_of_range = 0;
552+
jit_count_out_of_range::args_t args = {arg, &num_out_of_range, count};
553+
converter(&args);
554+
return num_out_of_range;
555+
}
552556
}
553557
#endif // OV_CORE_USE_XBYAK_JIT
554558
const auto is_out_of_f16_range = [](const float v) {

src/core/src/runtime/compute_hash.cpp

+68-65
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifdef OV_CORE_USE_XBYAK_JIT
2222
# include "openvino/core/parallel.hpp"
2323
# include "openvino/reference/utils/registers_pool.hpp"
24+
# include "openvino/util/common_util.hpp"
2425
#endif // OV_CORE_USE_XBYAK_JIT
2526

2627
namespace ov {
@@ -822,77 +823,79 @@ void ComputeHash<isa>::fold_to_64(const Vmm& v_dst) {
822823

823824
size_t compute_hash(const void* src, size_t size) {
824825
#ifdef OV_CORE_USE_XBYAK_JIT
825-
if (Generator::mayiuse(avx2)) {
826-
uint64_t result = 0lu;
827-
828-
// Parallel section
829-
constexpr uint64_t min_wa_per_thread = 131072lu; // 2^17
830-
const uint64_t size_u64 = static_cast<uint64_t>(size);
831-
if (size_u64 >= min_wa_per_thread * 2lu) {
832-
static auto first_thr_kernel = Generator::mayiuse(avx512_core)
833-
? jit::ComputeHash<avx512_core>::create({jit::FIRST_THREAD})
834-
: jit::ComputeHash<avx2>::create({jit::FIRST_THREAD});
835-
static auto n_thr_kernel = Generator::mayiuse(avx512_core)
836-
? jit::ComputeHash<avx512_core>::create({jit::N_THREAD})
837-
: jit::ComputeHash<avx2>::create({jit::N_THREAD});
838-
static auto final_fold_kernel = Generator::mayiuse(avx512_core)
839-
? jit::ComputeHash<avx512_core>::create({jit::FINAL_FOLD})
840-
: jit::ComputeHash<avx2>::create({jit::FINAL_FOLD});
841-
842-
static const uint64_t max_thr_num = 2lu;
843-
uint64_t thr_num = std::min(size_u64 / min_wa_per_thread, max_thr_num);
844-
const uint64_t el_per_thread =
845-
first_thr_kernel->get_vlen() * ((size_u64 / thr_num) / first_thr_kernel->get_vlen());
846-
std::vector<uint8_t> intermediate(thr_num * first_thr_kernel->get_vlen());
847-
848-
parallel_nt_static(static_cast<int>(thr_num), [&](const int ithr, const int nthr) {
849-
uint64_t start = el_per_thread * ithr;
850-
if (start >= size_u64) {
851-
return;
852-
}
853-
uint64_t work_amount = (el_per_thread + start > size_u64) ? size_u64 - start : el_per_thread;
826+
if (util::may_i_use_dynamic_code()) {
827+
if (Generator::mayiuse(avx2)) {
828+
uint64_t result = 0lu;
829+
830+
// Parallel section
831+
constexpr uint64_t min_wa_per_thread = 131072lu; // 2^17
832+
const uint64_t size_u64 = static_cast<uint64_t>(size);
833+
if (size_u64 >= min_wa_per_thread * 2lu) {
834+
static auto first_thr_kernel = Generator::mayiuse(avx512_core)
835+
? jit::ComputeHash<avx512_core>::create({jit::FIRST_THREAD})
836+
: jit::ComputeHash<avx2>::create({jit::FIRST_THREAD});
837+
static auto n_thr_kernel = Generator::mayiuse(avx512_core)
838+
? jit::ComputeHash<avx512_core>::create({jit::N_THREAD})
839+
: jit::ComputeHash<avx2>::create({jit::N_THREAD});
840+
static auto final_fold_kernel = Generator::mayiuse(avx512_core)
841+
? jit::ComputeHash<avx512_core>::create({jit::FINAL_FOLD})
842+
: jit::ComputeHash<avx2>::create({jit::FINAL_FOLD});
843+
844+
static const uint64_t max_thr_num = 2lu;
845+
uint64_t thr_num = std::min(size_u64 / min_wa_per_thread, max_thr_num);
846+
const uint64_t el_per_thread =
847+
first_thr_kernel->get_vlen() * ((size_u64 / thr_num) / first_thr_kernel->get_vlen());
848+
std::vector<uint8_t> intermediate(thr_num * first_thr_kernel->get_vlen());
849+
850+
parallel_nt_static(static_cast<int>(thr_num), [&](const int ithr, const int nthr) {
851+
uint64_t start = el_per_thread * ithr;
852+
if (start >= size_u64) {
853+
return;
854+
}
855+
uint64_t work_amount = (el_per_thread + start > size_u64) ? size_u64 - start : el_per_thread;
856+
857+
jit::ComputeHashCallArgs args;
858+
859+
args.src_ptr = reinterpret_cast<const uint8_t*>(src) + first_thr_kernel->get_vlen() * ithr;
860+
args.dst_ptr = &(intermediate[first_thr_kernel->get_vlen() * ithr]);
861+
args.k_ptr = jit::K_PULL;
862+
args.work_amount = work_amount;
863+
args.size = size_u64;
864+
args.threads_num = thr_num;
865+
866+
if (ithr == 0) {
867+
(*first_thr_kernel)(&args);
868+
} else {
869+
(*n_thr_kernel)(&args);
870+
}
871+
});
854872

855873
jit::ComputeHashCallArgs args;
874+
args.work_amount = size_u64 - el_per_thread * thr_num;
875+
args.src_ptr = reinterpret_cast<const uint8_t*>(src) + size_u64 - args.work_amount;
876+
args.dst_ptr = &result;
877+
args.k_ptr = jit::K_PULL;
878+
args.size = size_u64;
879+
args.intermediate_ptr = intermediate.data();
856880

857-
args.src_ptr = reinterpret_cast<const uint8_t*>(src) + first_thr_kernel->get_vlen() * ithr;
858-
args.dst_ptr = &(intermediate[first_thr_kernel->get_vlen() * ithr]);
881+
(*final_fold_kernel)(&args);
882+
} else {
883+
static auto single_thr_kernel = Generator::mayiuse(avx512_core)
884+
? jit::ComputeHash<avx512_core>::create({jit::SINGLE_THREAD})
885+
: jit::ComputeHash<avx2>::create({jit::SINGLE_THREAD});
886+
887+
jit::ComputeHashCallArgs args;
888+
args.src_ptr = src;
889+
args.dst_ptr = &result;
859890
args.k_ptr = jit::K_PULL;
860-
args.work_amount = work_amount;
891+
args.work_amount = size_u64;
861892
args.size = size_u64;
862-
args.threads_num = thr_num;
863-
864-
if (ithr == 0) {
865-
(*first_thr_kernel)(&args);
866-
} else {
867-
(*n_thr_kernel)(&args);
868-
}
869-
});
870-
871-
jit::ComputeHashCallArgs args;
872-
args.work_amount = size_u64 - el_per_thread * thr_num;
873-
args.src_ptr = reinterpret_cast<const uint8_t*>(src) + size_u64 - args.work_amount;
874-
args.dst_ptr = &result;
875-
args.k_ptr = jit::K_PULL;
876-
args.size = size_u64;
877-
args.intermediate_ptr = intermediate.data();
878-
879-
(*final_fold_kernel)(&args);
880-
} else {
881-
static auto single_thr_kernel = Generator::mayiuse(avx512_core)
882-
? jit::ComputeHash<avx512_core>::create({jit::SINGLE_THREAD})
883-
: jit::ComputeHash<avx2>::create({jit::SINGLE_THREAD});
884-
885-
jit::ComputeHashCallArgs args;
886-
args.src_ptr = src;
887-
args.dst_ptr = &result;
888-
args.k_ptr = jit::K_PULL;
889-
args.work_amount = size_u64;
890-
args.size = size_u64;
891-
892-
(*single_thr_kernel)(&args);
893-
}
894893

895-
return result;
894+
(*single_thr_kernel)(&args);
895+
}
896+
897+
return result;
898+
}
896899
}
897900

898901
#endif // OV_CORE_USE_XBYAK_JIT

0 commit comments

Comments
 (0)