OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Restore oneDNN activation zero-point memory on import (#37049)
Sungeun Kimc1dc69666188
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cppindex d71f7a68886..6637889170b 100644--- a/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp@@ -210,7 +210,7 @@ protected:cldnn::data_node& node, int& zero_point_mask) {int32_t zp_val = DNNL_RUNTIME_S32_VAL;bool is_per_tensor = onednn::is_per_tensor<T>(node, zp_val);- memory::ptr s32_mem = onednn::convert_zp_data_to_s32<T>(node.get_attached_memory_ptr());+ memory::ptr s32_mem = onednn::convert_zp_data_to_s32(node.get_attached_memory_ptr());node.attach_memory(s32_mem, false);zero_point_mask = is_per_tensor ? 0 : 2;attrs->set_zero_points_mask(DNNL_ARG_SRC, zero_point_mask);@@ -342,6 +342,12 @@ public:ib >> zero_bias;auto prim = impl_params->typed_desc<convolution>();+ if (prim->activations_zero_points.is_valid()) {+ auto& a_zp = impl_params->get_program().get_node_ptr(prim->id)->as<convolution>().activations_zero_points().as<data>();+ memory::ptr s32_mem = onednn::convert_zp_data_to_s32(a_zp.get_attached_memory_ptr());+ if (s32_mem != nullptr)+ a_zp.attach_memory(s32_mem, false);+ }bool has_wzp = prim->weights_zero_points.is_valid();if (has_wzp) {ib >> make_data(&_wzp_data_type, sizeof(dnnl::memory::data_type));diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cppindex 6d830c3c9e4..aa535bd0dc0 100644--- a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp@@ -47,28 +47,40 @@ std::string memory_desc_to_string(const dnnl::memory::desc& desc) {return ss.str();}+namespace {template <typename T>+void copy_zp_data_to_s32(const memory::ptr& zp_memory, const memory::ptr& zp_s32_memory, stream& stream) {+ mem_lock<T, mem_lock_type::read> zp_data(zp_memory, stream);+ mem_lock<int32_t, mem_lock_type::write> zp_s32_data(zp_s32_memory, stream);+ for (size_t i = 0; i < zp_data.size(); i++) {+ zp_s32_data.data()[i] = static_cast<int32_t>(zp_data.data()[i]);+ }+}+} // namespace+cldnn::memory::ptr convert_zp_data_to_s32(const memory::ptr zp_memory) {+ auto zp_s32_layout = zp_memory->get_layout();+ const auto src_dtype = zp_s32_layout.data_type;+ if (src_dtype == data_types::i32)+ return zp_memory;+ if (!data_type_traits::is_i8_u8(src_dtype))+ return nullptr;+auto engine = zp_memory->get_engine();auto& stream = engine->get_service_stream();- auto zp_s32_layout = zp_memory->get_layout();zp_s32_layout.data_type = data_types::i32;auto zp_s32_memory = engine->allocate_memory(zp_s32_layout, false);- mem_lock<T, mem_lock_type::read> zp_data(zp_memory, stream);- mem_lock<int32_t, mem_lock_type::write> zp_s32_data(zp_s32_memory, stream);- for (size_t i = 0; i < zp_data.size(); i++) {- zp_s32_data.data()[i] = static_cast<int32_t>(zp_data.data()[i]);+ if (src_dtype == data_types::i8) {+ copy_zp_data_to_s32<int8_t>(zp_memory, zp_s32_memory, stream);+ } else {+ copy_zp_data_to_s32<uint8_t>(zp_memory, zp_s32_memory, stream);}return zp_s32_memory;}-template cldnn::memory::ptr convert_zp_data_to_s32<int8_t>(const memory::ptr zp_memory);-template cldnn::memory::ptr convert_zp_data_to_s32<uint8_t>(const memory::ptr zp_memory);-template cldnn::memory::ptr convert_zp_data_to_s32<int32_t>(const memory::ptr zp_memory);-cldnn::format default_fmt_for_dims(size_t dims, bool is_grouped) {switch (dims) {case 6: return is_grouped ? cldnn::format::goizyx : cldnn::format::bfwzyx;diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hppindex a26f78be96c..5c34af2b1e6 100644--- a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp@@ -19,7 +19,6 @@ namespace cldnn {namespace onednn {// common utils-template <typename T>cldnn::memory::ptr convert_zp_data_to_s32(const memory::ptr zp_memory);cldnn::format default_fmt_for_dims(size_t dims, bool is_grouped = false);diff --git a/src/plugins/intel_gpu/tests/functional/behavior/model_cache.cpp b/src/plugins/intel_gpu/tests/functional/behavior/model_cache.cppindex bbe05bfd261..675eabeb2d3 100644--- a/src/plugins/intel_gpu/tests/functional/behavior/model_cache.cpp+++ b/src/plugins/intel_gpu/tests/functional/behavior/model_cache.cpp@@ -5,6 +5,7 @@#include <sys/stat.h>#include <sys/types.h>+#include <algorithm>#include <cstdio>#include "common_test_utils/common_utils.hpp"@@ -16,14 +17,17 @@#include "common_test_utils/subgraph_builders/ti_with_lstm_cell.hpp"#include "common_test_utils/test_common.hpp"#include "openvino/core/rt_info/weightless_caching_attributes.hpp"+#include "openvino/op/convolution.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/matmul.hpp"+#include "openvino/op/subtract.hpp"#include "openvino/op/util/op_types.hpp"#include "openvino/pass/constant_folding.hpp"#include "openvino/pass/manager.hpp"#include "openvino/pass/serialize.hpp"#include "openvino/runtime/weightless_properties_utils.hpp"#include "openvino/util/codec_xor.hpp"+#include "ov_ops/type_relaxed.hpp"#include "shared_test_classes/base/ov_behavior_test_utils.hpp"#include "shared_test_classes/subgraph/weights_decompression_params.hpp"#include "common_test_utils/subgraph_builders/weights_decompression_builders.hpp"@@ -274,6 +278,105 @@ TEST_P(CheckWeightlessCacheAccuracy, TiWithLstmCell) {class CheckWeightlessCacheAccuracyLowPrecision : public CheckWeightlessCacheAccuracy {};+class CheckModelCacheOnednnConvAZP : public ::testing::Test {+protected:+ std::string cache_dir;++ void SetUp() override {+ cache_dir = ov::test::utils::generateTestFilePrefix() + "_cache_dir";+ }++ void TearDown() override {+ ov::test::utils::removeFilesWithExt(cache_dir, "blob");+ ov::test::utils::removeFilesWithExt(cache_dir, "cl_cache");+ ov::test::utils::removeDir(cache_dir);+ }++ static std::shared_ptr<ov::Model> make_model() {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::u8, ov::Shape{1, 2, 5, 4});+ auto azp_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{1, 2, 1, 1}, {2, 5});+ auto activations = std::make_shared<ov::op::TypeRelaxed<ov::op::v1::Subtract>>(+ ov::element::TypeVector{ov::element::u8, ov::element::u8},+ ov::element::TypeVector{ov::element::f32},+ ov::op::TemporaryReplaceOutputType(input, ov::element::f32).get(),+ ov::op::TemporaryReplaceOutputType(azp_const, ov::element::f32).get());++ const std::vector<uint8_t> weights = {1, 2, 1, 2, 1, 2, 9, 7, 1,+ 9, 0, 4, 1, 3, 2, 0, 2, 5,+ 1, 2, 1, 2, 1, 2, 9, 7, 1,+ 9, 0, 4, 1, 3, 2, 0, 2, 5,+ 1, 2, 1, 2, 1, 2, 9, 7, 1,+ 9, 0, 4, 1, 3, 2, 0, 2, 5};+ auto weights_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{3, 2, 3, 3}, weights);+ auto wzp_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{1}, {2});+ auto quantized_weights = std::make_shared<ov::op::TypeRelaxed<ov::op::v1::Subtract>>(+ ov::element::TypeVector{ov::element::u8, ov::element::u8},+ ov::element::TypeVector{ov::element::f32},+ ov::op::TemporaryReplaceOutputType(weights_const, ov::element::f32).get(),+ ov::op::TemporaryReplaceOutputType(wzp_const, ov::element::f32).get());++ auto conv = std::make_shared<ov::op::TypeRelaxed<ov::op::v1::Convolution>>(+ ov::element::TypeVector{ov::element::u8, ov::element::u8},+ ov::element::TypeVector{ov::element::f32},+ ov::op::TemporaryReplaceOutputType(activations, ov::element::f32).get(),+ ov::op::TemporaryReplaceOutputType(quantized_weights, ov::element::f32).get(),+ ov::Strides{2, 2},+ ov::CoordinateDiff{0, 0},+ ov::CoordinateDiff{1, 2},+ ov::Strides{1, 1},+ ov::op::PadType::EXPLICIT);+ conv->set_friendly_name("conv");+ auto result = std::make_shared<ov::op::v0::Result>(conv);+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{input}, "OnednnConvAZP");+ }++ static ov::Tensor make_input_tensor() {+ const std::vector<uint8_t> input_data = {1, 2, 3, 4, 5,+ 2, 2, 3, 4, 6,+ 3, 3, 3, 5, 1,+ 1, 1, 1, 1, 1,+ 1, 2, 3, 4, 5,+ 2, 2, 3, 4, 6,+ 3, 3, 3, 5, 1,+ 1, 1, 1, 1, 1};+ ov::Tensor tensor{ov::element::u8, ov::Shape{1, 2, 5, 4}};+ std::copy(input_data.begin(), input_data.end(), tensor.data<uint8_t>());+ return tensor;+ }+};++TEST_F(CheckModelCacheOnednnConvAZP, ActivationZeroPointRestoredFromCache) {+ ov::AnyMap config = {ov::cache_dir(cache_dir)};++ auto core = ov::test::utils::PluginCache::get().core();+ auto compiled_model = core->compile_model(make_model(), ov::test::utils::DEVICE_GPU, config);++ auto blobs = ov::test::utils::listFilesWithExt(cache_dir, "blob");+ ASSERT_EQ(blobs.size(), 1);++ struct stat result;+ ASSERT_EQ(stat(blobs[0].c_str(), &result), 0);+ const auto first_mod_time = result.st_mtime;++ auto imported_model = core->compile_model(make_model(), ov::test::utils::DEVICE_GPU, config);++ blobs = ov::test::utils::listFilesWithExt(cache_dir, "blob");+ ASSERT_EQ(blobs.size(), 1);+ ASSERT_EQ(stat(blobs[0].c_str(), &result), 0);+ ASSERT_EQ(first_mod_time, result.st_mtime);++ auto input_tensor = make_input_tensor();+ auto orig_req = compiled_model.create_infer_request();+ auto new_req = imported_model.create_infer_request();+ orig_req.set_input_tensor(input_tensor);+ new_req.set_input_tensor(input_tensor);++ orig_req.infer();+ new_req.infer();++ ov::test::utils::compare(orig_req.get_output_tensor(), new_req.get_output_tensor(), compiled_model.output().get_element_type());+}+TEST_P(CheckWeightlessCacheAccuracyLowPrecision, MatmulWeightsDecompression) {ov::test::MatMulDecompressionShapeParams shape_params{{{}, {{1, 4, 16}}}, {1, 16, 32}};auto dynShape = shape_params.data_shape.first;