From 04f24853343b1e6efb0da3b0c8859d097c436455 Mon Sep 17 00:00:00 2001 From: Tomasz Adamowicz Date: Fri, 1 Dec 2023 15:39:26 +0100 Subject: [PATCH] [GNA] Use OV thread_local implementation (#21284) * [GNA] Use OV thread_local implementation --- .../intel_gna/src/backend/gna_limitations.cpp | 10 ++++++++-- .../intel_gna/src/backend/gna_limitations.hpp | 18 +++++++++++++----- src/plugins/intel_gna/src/gna_plugin.cpp | 2 ++ .../unit/engines/gna/I8_quantisation_test.cpp | 4 ++++ .../unit/engines/gna/i16_quantisation_test.cpp | 4 ++++ .../unit/backend/gna_limitations_test.cpp | 4 ++++ .../tests/unit/gna_get_aligned_split_sizes.cpp | 2 ++ .../tests/unit/gna_memory_alignment.cpp | 2 ++ .../gna_decompose_2d_convolution.cpp | 10 ++++++++++ .../transformations/gna_insert_copy_layer.cpp | 5 +++++ ...plit_convolution_with_large_buffer_size.cpp | 5 +++++ .../unit/transformations/gna_split_eltwise.cpp | 5 +++++ 12 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/plugins/intel_gna/src/backend/gna_limitations.cpp b/src/plugins/intel_gna/src/backend/gna_limitations.cpp index 0d459ecabaebb7..276ea7c5f2f9f3 100644 --- a/src/plugins/intel_gna/src/backend/gna_limitations.cpp +++ b/src/plugins/intel_gna/src/backend/gna_limitations.cpp @@ -677,7 +677,7 @@ constexpr uint32_t Limitations::kBytesPerCropElement; constexpr uint32_t Limitations::kBytesPerConcatElement; constexpr uint32_t Limitations::kMemoryPageSize; -thread_local std::shared_ptr Limitations::k_instance{nullptr}; +ov::threading::ThreadLocal> Limitations::kInstance{nullptr}; Limitations::Limitations(const DeviceVersion& target) { m_use_only_16bit_conv_weights = @@ -689,7 +689,13 @@ Limitations::Limitations(const DeviceVersion& target) { } void Limitations::init(const DeviceVersion& compile_target) { - k_instance = std::shared_ptr(new Limitations(compile_target)); + auto& localInstance = kInstance.local(); + localInstance.reset(new Limitations(compile_target)); +} + +void Limitations::deinit() { + auto& localInstance = kInstance.local(); + localInstance.reset(); } size_t Limitations::get_min_batch_to_fit_in_buffer(InferenceEngine::DataPtr input) { diff --git a/src/plugins/intel_gna/src/backend/gna_limitations.hpp b/src/plugins/intel_gna/src/backend/gna_limitations.hpp index b30a32a18561e1..bfb68b3f35f5c3 100644 --- a/src/plugins/intel_gna/src/backend/gna_limitations.hpp +++ b/src/plugins/intel_gna/src/backend/gna_limitations.hpp @@ -20,6 +20,7 @@ #include "legacy/ngraph_ops/fully_connected.hpp" #include "ngraph/opsets/opset7.hpp" #include "ngraph/opsets/opset9.hpp" +#include "openvino/runtime/threading/thread_local.hpp" #include "ops/gna_convolution.hpp" #include "ops/gna_max_pool.hpp" @@ -164,12 +165,17 @@ class AbstractValidator { class Limitations { public: /** - * @brief Create instance of the Limitations class. Due to Limitations being a singleton, multiple instances of the - * plugin with different compilation targets cannot exist at the same time + * @brief Create an instance of the Limitations class. Since Limitations is designed as a singleton, multiple + * instances of the plugin with different compilation targets cannot coexist simultaneously for the same thread. * @param compile_target GNA compile target */ static void init(const target::DeviceVersion& compile_target); + /** + * @brief Delete the instance of the Limitations class for the currently running thread. + */ + static void deinit(); + /** * @brief Returns the instance of Limitations object. Requires an Init call before the first usage */ @@ -309,14 +315,16 @@ class Limitations { bool m_use_only_16bit_conv_weights = false; size_t m_mem_alignment = 0; std::shared_ptr m_cnn_validator; - static thread_local std::shared_ptr k_instance; + + static ov::threading::ThreadLocal> kInstance; }; inline std::shared_ptr Limitations::get_instance() { - if (!k_instance) { + auto& instance = kInstance.local(); + if (!instance) { THROW_GNA_EXCEPTION << "Limitations instance is not initialized.\n"; } - return k_instance; + return instance; } inline bool Limitations::is_crop_affined_offset(size_t numberOfElements) const { diff --git a/src/plugins/intel_gna/src/gna_plugin.cpp b/src/plugins/intel_gna/src/gna_plugin.cpp index e9adb59c70b613..ef658a339a1798 100644 --- a/src/plugins/intel_gna/src/gna_plugin.cpp +++ b/src/plugins/intel_gna/src/gna_plugin.cpp @@ -1429,4 +1429,6 @@ InferenceEngine::QueryNetworkResult GNAPlugin::QueryNetwork( GNAPlugin::~GNAPlugin() { if (gnadevice) gnadevice->close(); + + Limitations::deinit(); } diff --git a/src/plugins/intel_gna/tests/deprecated/unit/engines/gna/I8_quantisation_test.cpp b/src/plugins/intel_gna/tests/deprecated/unit/engines/gna/I8_quantisation_test.cpp index cdd882c107aa4c..e7da37e1467bab 100644 --- a/src/plugins/intel_gna/tests/deprecated/unit/engines/gna/I8_quantisation_test.cpp +++ b/src/plugins/intel_gna/tests/deprecated/unit/engines/gna/I8_quantisation_test.cpp @@ -52,6 +52,10 @@ class I8QuantisationTest : public GNATest<> { void SetUp() override { Limitations::init(target::DeviceVersion::Default); } + + void TearDown() override { + Limitations::deinit(); + } }; // TODO: add test for FC weights after quantization diff --git a/src/plugins/intel_gna/tests/deprecated/unit/engines/gna/i16_quantisation_test.cpp b/src/plugins/intel_gna/tests/deprecated/unit/engines/gna/i16_quantisation_test.cpp index 64645325513d93..5a64a2c3bff795 100644 --- a/src/plugins/intel_gna/tests/deprecated/unit/engines/gna/i16_quantisation_test.cpp +++ b/src/plugins/intel_gna/tests/deprecated/unit/engines/gna/i16_quantisation_test.cpp @@ -54,6 +54,10 @@ class I16QuantisationTest : public GNATest<> { void SetUp() override { Limitations::init(target::DeviceVersion::Default); } + + void TearDown() override { + Limitations::deinit(); + } }; template diff --git a/src/plugins/intel_gna/tests/unit/backend/gna_limitations_test.cpp b/src/plugins/intel_gna/tests/unit/backend/gna_limitations_test.cpp index 4ce23c8de29bd2..fd1014e716c46e 100644 --- a/src/plugins/intel_gna/tests/unit/backend/gna_limitations_test.cpp +++ b/src/plugins/intel_gna/tests/unit/backend/gna_limitations_test.cpp @@ -287,6 +287,10 @@ class GNAcnn2dValidatorTest : public ::testing::TestWithParam validator; }; diff --git a/src/plugins/intel_gna/tests/unit/gna_get_aligned_split_sizes.cpp b/src/plugins/intel_gna/tests/unit/gna_get_aligned_split_sizes.cpp index 672d8666b5dbcf..0cfc4caed8ae59 100644 --- a/src/plugins/intel_gna/tests/unit/gna_get_aligned_split_sizes.cpp +++ b/src/plugins/intel_gna/tests/unit/gna_get_aligned_split_sizes.cpp @@ -65,6 +65,7 @@ void RunVariadicSplitSupportedTest(DeviceVersion device_version, std::vectorget_memory_alignment(), 64); + Limitations::deinit(); } TEST(MemoryAlignmentTest, getMemoryAlignmentBytes_Expect16ByteAlignmentWhenTargetIsGNA3_6) { Limitations::init(DeviceVersion::GNA3_6); EXPECT_EQ(Limitations::get_instance()->get_memory_alignment(), 16); + Limitations::deinit(); } } // namespace testing diff --git a/src/plugins/intel_gna/tests/unit/transformations/gna_decompose_2d_convolution.cpp b/src/plugins/intel_gna/tests/unit/transformations/gna_decompose_2d_convolution.cpp index ea03b9971e1166..2eed9b367d034e 100644 --- a/src/plugins/intel_gna/tests/unit/transformations/gna_decompose_2d_convolution.cpp +++ b/src/plugins/intel_gna/tests/unit/transformations/gna_decompose_2d_convolution.cpp @@ -295,6 +295,7 @@ class Decompose2DConvTestInvalidFixture : public ov::test::TestsCommon, public ::testing::WithParamInterface { public: void SetUp() override; + void TearDown() override; public: std::shared_ptr function, reference_function; @@ -339,12 +340,17 @@ void Decompose2DConvTestInvalidFixture::SetUp() { conv_params); } +void Decompose2DConvTestInvalidFixture::TearDown() { + Limitations::deinit(); +} + // --------------------------------------------------------------------------------------------------------------------- class Decompose2DConvTestFixture : public ov::test::TestsCommon, public ::testing::WithParamInterface { public: void SetUp() override; + void TearDown() override; std::shared_ptr get_reference(const bool& fq, const modelType& model, @@ -385,6 +391,10 @@ void Decompose2DConvTestFixture::SetUp() { reference_function = get_reference(fq, model, input_shape, graph_data, conv_params); } +void Decompose2DConvTestFixture::TearDown() { + Limitations::deinit(); +} + std::shared_ptr ReshapeBiasConst(std::shared_ptr conv_bias, const ConvParams& conv_params) { auto add_const = diff --git a/src/plugins/intel_gna/tests/unit/transformations/gna_insert_copy_layer.cpp b/src/plugins/intel_gna/tests/unit/transformations/gna_insert_copy_layer.cpp index c3d778b903e22d..87d53ff68f9c1f 100644 --- a/src/plugins/intel_gna/tests/unit/transformations/gna_insert_copy_layer.cpp +++ b/src/plugins/intel_gna/tests/unit/transformations/gna_insert_copy_layer.cpp @@ -44,6 +44,7 @@ class InsertCopyLayerTest : public ov::test::TestsCommon, public ::testing::With return result.str(); } void SetUp() override; + void TearDown() override; virtual void Validate(); virtual void Run(); @@ -64,6 +65,9 @@ void InsertCopyLayerTest::SetUp() { std::tie(m_device_ver, m_axis, m_inputs_num) = this->GetParam(); Limitations::init(m_device_ver); } +void InsertCopyLayerTest::TearDown() { + Limitations::deinit(); +} void InsertCopyLayerTest::Run() { Validate(); @@ -212,6 +216,7 @@ class TransformationTestsBase : public ov::test::TestsCommon, void TearDown() override { m_func.reset(); + Limitations::deinit(); } void RunPasses(ngraph::pass::Manager& m) { diff --git a/src/plugins/intel_gna/tests/unit/transformations/gna_split_convolution_with_large_buffer_size.cpp b/src/plugins/intel_gna/tests/unit/transformations/gna_split_convolution_with_large_buffer_size.cpp index e51b7832d44ee5..863e42d21a45bd 100644 --- a/src/plugins/intel_gna/tests/unit/transformations/gna_split_convolution_with_large_buffer_size.cpp +++ b/src/plugins/intel_gna/tests/unit/transformations/gna_split_convolution_with_large_buffer_size.cpp @@ -270,6 +270,7 @@ class SplitConvolutionFixture : public ov::test::TestsCommon, public ::testing::WithParamInterface> { public: void SetUp() override; + void TearDown() override; public: std::shared_ptr function, reference_function; @@ -290,6 +291,10 @@ void SplitConvolutionFixture::SetUp() { reference_function = reference_graph.createFunction(); } +void SplitConvolutionFixture::TearDown() { + Limitations::deinit(); +} + void execute_test(std::shared_ptr function, std::shared_ptr reference_function, ngraph::pass::Manager& pass_manager) { diff --git a/src/plugins/intel_gna/tests/unit/transformations/gna_split_eltwise.cpp b/src/plugins/intel_gna/tests/unit/transformations/gna_split_eltwise.cpp index 05f928a596d07a..fd92a12cf89eb2 100644 --- a/src/plugins/intel_gna/tests/unit/transformations/gna_split_eltwise.cpp +++ b/src/plugins/intel_gna/tests/unit/transformations/gna_split_eltwise.cpp @@ -134,6 +134,7 @@ class SplitEltwiseTestSuiteFixture : public ov::test::TestsCommon, public ::testing::WithParamInterface { public: void SetUp() override; + void TearDown() override; public: std::shared_ptr function, reference_function; @@ -151,6 +152,10 @@ void SplitEltwiseTestSuiteFixture::SetUp() { reference_function = createFunction(shape, with_const, with_fq, type, true); } +void SplitEltwiseTestSuiteFixture::TearDown() { + Limitations::deinit(); +} + void execute_test(std::shared_ptr function, std::shared_ptr reference_function) { ngraph::pass::Manager manager; manager.register_pass();