Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] Refactor onednn::layout_to_memory_desc function (#33100)

Jade Cho5edcf25a07ce
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.cpp
index 04f992473ed..0fb7933fc29 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
@@ -107,7 +107,7 @@ static std::shared_ptr<dnnl::convolution_forward::primitive_desc> get_convolutio
}
if (prim->bias.is_valid()) {
- auto bias_md = onednn::layout_to_memory_desc(impl_params.get_input_layout(2), dnnl::memory::format_tag::any, onednn::mem_flags::flatten);
+ auto bias_md = onednn::layout_to_memory_desc_flatten(impl_params.get_input_layout(2), dnnl::memory::format_tag::any);
return std::make_shared<dnnl::convolution_forward::primitive_desc>(
engine.get_onednn_engine(),
dnnl::prop_kind::forward_inference,
@@ -178,7 +178,7 @@ protected:
a_zp = a_zp_node.get_attached_memory_ptr();
}
- dnnl::memory::desc desc = onednn::layout_to_memory_desc(a_zp->get_layout(), dnnl::memory::format_tag::a, onednn::mem_flags::flatten);
+ dnnl::memory::desc desc = onednn::layout_to_memory_desc_flatten(a_zp->get_layout(), dnnl::memory::format_tag::a);
args.insert({DNNL_ARG_ATTR_ZERO_POINTS | DNNL_ARG_SRC, a_zp->get_onednn_memory(desc)});
GPU_DEBUG_TRACE_DETAIL << instance.id() << " activations_zero_points: "
@@ -187,7 +187,7 @@ protected:
if (instance.weights_zero_points_term()) {
auto w_zp = instance.weights_zero_points_memory();
- dnnl::memory::desc desc = onednn::layout_to_memory_desc(w_zp->get_layout(), dnnl::memory::format_tag::a, onednn::mem_flags::flatten);
+ dnnl::memory::desc desc = onednn::layout_to_memory_desc_flatten(w_zp->get_layout(), dnnl::memory::format_tag::a);
args.insert({DNNL_ARG_ATTR_ZERO_POINTS | DNNL_ARG_WEIGHTS, w_zp->get_onednn_memory(desc)});
GPU_DEBUG_TRACE_DETAIL << instance.id() << " weights_zero_points: "
@@ -263,7 +263,7 @@ protected:
auto shape_consistent = onednn::keep_weights_reorder_shape_consistent(source_weights_layout, target_weights_desc);
OPENVINO_ASSERT(shape_consistent, "[GPU] Input shape and output shape of weight reorder should be same.");
- auto source_weights_desc = onednn::layout_to_memory_desc(source_weights_layout);
+ auto source_weights_desc = onednn::layout_to_memory_desc(source_weights_layout, dnnl::memory::format_tag::undef);
const bool weights_format = true;
auto traits = convert_memory_desc_to_traits(target_weights_desc, weights_format, grouped_weights);
@@ -352,7 +352,7 @@ public:
*_attrs.get());
_pd = *prim_desc;
} else {
- auto bias_md = onednn::layout_to_memory_desc(impl_params->get_input_layout(2), dnnl::memory::format_tag::any, onednn::mem_flags::flatten);
+ auto bias_md = onednn::layout_to_memory_desc_flatten(impl_params->get_input_layout(2), dnnl::memory::format_tag::any);
auto prim_desc = std::make_shared<dnnl::convolution_forward::primitive_desc>(
ib.get_engine().get_onednn_engine(),
dnnl::prop_kind::forward_inference, dnnl::algorithm::convolution_direct,
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/deconvolution_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/deconvolution_onednn.cpp
index c52a1fde319..57dfcbbc411 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/deconvolution_onednn.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/deconvolution_onednn.cpp
@@ -53,7 +53,7 @@ static std::shared_ptr<dnnl::deconvolution_forward::primitive_desc> get_deconvol
}
if (prim->bias.is_valid()) {
- auto bias_md = onednn::layout_to_memory_desc(impl_params.get_input_layout(2), dnnl::memory::format_tag::any, onednn::mem_flags::flatten);
+ auto bias_md = onednn::layout_to_memory_desc_flatten(impl_params.get_input_layout(2), dnnl::memory::format_tag::any);
return std::make_shared<dnnl::deconvolution_forward::primitive_desc>(
engine.get_onednn_engine(),
dnnl::prop_kind::forward_inference,
@@ -192,7 +192,7 @@ public:
*_attrs.get());
_pd = *prim_desc;
} else {
- auto bias_md = onednn::layout_to_memory_desc(impl_params->get_input_layout(2), dnnl::memory::format_tag::any, onednn::mem_flags::flatten);
+ auto bias_md = onednn::layout_to_memory_desc_flatten(impl_params->get_input_layout(2), dnnl::memory::format_tag::any);
auto prim_desc = std::make_shared<dnnl::deconvolution_forward::primitive_desc>(
ib.get_engine().get_onednn_engine(),
dnnl::prop_kind::forward_inference, dnnl::algorithm::deconvolution_direct,
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp
index 47576c0beeb..df0314114b7 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp
@@ -69,14 +69,14 @@ protected:
if (prim->decompression_scale.is_valid()) {
auto decompression_scale_idx = idx++;
auto scale_mem = instance.dep_memory_ptr(decompression_scale_idx);
- dnnl::memory::desc desc = onednn::layout_to_memory_desc(scale_mem->get_layout(), dnnl::memory::format_tag::a, onednn::mem_flags::flatten);
+ dnnl::memory::desc desc = onednn::layout_to_memory_desc_flatten(scale_mem->get_layout(), dnnl::memory::format_tag::a);
args.insert({DNNL_ARG_ATTR_SCALES | DNNL_ARG_WEIGHTS, scale_mem->get_onednn_memory(desc)});
}
if (prim->decompression_zero_point.is_valid()) {
auto decompression_zp_idx = idx++;
auto zp_mem = instance.dep_memory_ptr(decompression_zp_idx);
- dnnl::memory::desc desc = onednn::layout_to_memory_desc(zp_mem->get_layout(), dnnl::memory::format_tag::a, onednn::mem_flags::flatten);
+ dnnl::memory::desc desc = onednn::layout_to_memory_desc_flatten(zp_mem->get_layout(), dnnl::memory::format_tag::a);
args.insert({DNNL_ARG_ATTR_ZERO_POINTS | DNNL_ARG_WEIGHTS, zp_mem->get_onednn_memory(desc)});
}
bool is_dyn_quan_input = instance.get_input_layout(0).data_type == data_types::i8 || instance.get_input_layout(0).data_type == data_types::u8;
@@ -84,22 +84,21 @@ protected:
if (is_dyn_quan_input && prim->activation_scale.is_valid()) {
auto activation_scale_idx = idx++;
auto act_scale_mem = instance.dep_memory_ptr(activation_scale_idx);
- dnnl::memory::desc desc = onednn::layout_to_memory_desc(act_scale_mem->get_layout(), dnnl::memory::format_tag::ab, onednn::mem_flags::flatten);
+ dnnl::memory::desc desc = onednn::layout_to_memory_desc_flatten(act_scale_mem->get_layout(), dnnl::memory::format_tag::ab);
args.insert({DNNL_ARG_ATTR_SCALES | DNNL_ARG_SRC_0, act_scale_mem->get_onednn_memory(desc)});
}
if (is_dyn_quan_input && prim->activation_zero_point.is_valid()) {
auto activation_zp_idx = idx++;
auto act_zp_mem = instance.dep_memory_ptr(activation_zp_idx);
- dnnl::memory::desc desc = onednn::layout_to_memory_desc(act_zp_mem->get_layout(), dnnl::memory::format_tag::ab, onednn::mem_flags::flatten);
+ dnnl::memory::desc desc = onednn::layout_to_memory_desc_flatten(act_zp_mem->get_layout(), dnnl::memory::format_tag::ab);
args.insert({DNNL_ARG_ATTR_ZERO_POINTS | DNNL_ARG_SRC_0, act_zp_mem->get_onednn_memory(desc)});
}
if (is_dyn_quan_input && prim->activation_precomputed_reduction.is_valid()) {
auto activation_precomputed_reduction_idx = idx++;
auto act_precomputed_reduction_mem = instance.dep_memory_ptr(activation_precomputed_reduction_idx);
- dnnl::memory::desc desc = onednn::layout_to_memory_desc(act_precomputed_reduction_mem->get_layout(),
- dnnl::memory::format_tag::ab, onednn::mem_flags::flatten);
+ dnnl::memory::desc desc = onednn::layout_to_memory_desc_flatten(act_precomputed_reduction_mem->get_layout(), dnnl::memory::format_tag::ab);
args.insert({DNNL_ARG_ATTR_PRECOMPUTED_REDUCTIONS | DNNL_ARG_SRC_0, act_precomputed_reduction_mem->get_onednn_memory(desc)});
}
}
@@ -197,13 +196,16 @@ protected:
weights_layout.format = input_layout.format;
}
- auto use_strides_for_weight_md = (weights_layout.data_padding
- && format::is_default_format(weights_layout.format)
- && (weights_layout.data_type == data_types::i4 || weights_layout.data_type == data_types::u4)) ?
- onednn::mem_flags::use_strides : onednn::mem_flags::None;
+ dnnl::memory::desc weights_md;
+ if (weights_layout.data_padding
+ && format::is_default_format(weights_layout.format)
+ && (weights_layout.data_type == data_types::i4 || weights_layout.data_type == data_types::u4)) {
+ weights_md = onednn::layout_to_memory_desc_strides(weights_layout, weights_fmt);
+ } else {
+ weights_md = onednn::layout_to_memory_desc(weights_layout, weights_fmt);
+ }
dnnl::memory::desc input_md = onednn::layout_to_memory_desc(input_layout, target_fmt);
- dnnl::memory::desc weights_md = onednn::layout_to_memory_desc(weights_layout, weights_fmt, use_strides_for_weight_md);
dnnl::memory::desc output_md = onednn::layout_to_memory_desc(output_layout, target_fmt);
if (has_bias) {
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/primitive_onednn_base.h b/src/plugins/intel_gpu/src/graph/impls/onednn/primitive_onednn_base.h
index 38034280659..5ac39d3c139 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/primitive_onednn_base.h
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/primitive_onednn_base.h
@@ -268,10 +268,9 @@ struct typed_primitive_onednn_impl : public typed_primitive_impl<PType> {
_post_ops.append_binary(aalgorithm,
dnnl::memory::desc(fused_desc.at(idx).dims, fused_desc.at(idx).dt, fused_desc.at(idx).tag));
} else {
- dnnl::memory::desc md = onednn::layout_to_memory_desc(
- impl_params->get_input_layout(fused_desc.at(idx).mem_dep),
- fused_desc.at(idx).tag,
- (fused_desc.at(idx).flatten ? onednn::mem_flags::flatten : onednn::mem_flags::None));
+ dnnl::memory::desc md = fused_desc.at(idx).flatten
+ ? onednn::layout_to_memory_desc_flatten(impl_params->get_input_layout(fused_desc.at(idx).mem_dep), fused_desc.at(idx).tag)
+ : onednn::layout_to_memory_desc(impl_params->get_input_layout(fused_desc.at(idx).mem_dep), fused_desc.at(idx).tag);
_post_ops.append_binary(aalgorithm, md);
}
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/reduce_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/reduce_onednn.cpp
index 48c0cf9016a..6b7c98ff7f8 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/reduce_onednn.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/reduce_onednn.cpp
@@ -57,8 +57,8 @@ protected:
// oneDNN reduction does not allow this. So this function reverts it.
reorder_unreduced_axis_no_fusion(input_layout, output_layout, prim->axes);
- auto input_md = onednn::layout_to_memory_desc(input_layout, dnnl::memory::format_tag::undef, mem_flags::need_blocked);
- auto output_md = onednn::layout_to_memory_desc(output_layout, dnnl::memory::format_tag::undef, mem_flags::need_blocked);
+ auto input_md = onednn::layout_to_memory_desc_blocked(input_layout, dnnl::memory::format_tag::undef);
+ auto output_md = onednn::layout_to_memory_desc_blocked(output_layout, dnnl::memory::format_tag::undef);
float p = 0.f;
float eps = 0.f;
@@ -122,8 +122,8 @@ public:
dnnl::algorithm alg;
ib >> make_data(&alg, sizeof(dnnl::algorithm));
- auto input_md = onednn::layout_to_memory_desc(impl_params->get_input_layout(0), dnnl::memory::format_tag::undef, mem_flags::need_blocked);
- auto output_md = onednn::layout_to_memory_desc(impl_params->get_output_layout(), dnnl::memory::format_tag::undef, mem_flags::need_blocked);
+ auto input_md = onednn::layout_to_memory_desc_blocked(impl_params->get_input_layout(0), dnnl::memory::format_tag::undef);
+ auto output_md = onednn::layout_to_memory_desc_blocked(impl_params->get_output_layout(), dnnl::memory::format_tag::undef);
float p, eps;
ib >> p >> eps;
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/reorder_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/reorder_onednn.cpp
index d3085567197..20a30ed3120 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/reorder_onednn.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/reorder_onednn.cpp
@@ -54,8 +54,8 @@ protected:
auto input_layout = impl_params.get_input_layout(0);
auto output_layout = impl_params.get_output_layout();
- auto input_md = onednn::layout_to_memory_desc(input_layout, dnnl::memory::format_tag::undef, onednn::mem_flags::need_blocked);
- auto output_md = onednn::layout_to_memory_desc(output_layout, dnnl::memory::format_tag::undef, onednn::mem_flags::need_blocked);
+ auto input_md = onednn::layout_to_memory_desc_blocked(input_layout, dnnl::memory::format_tag::undef);
+ auto output_md = onednn::layout_to_memory_desc_blocked(output_layout, dnnl::memory::format_tag::undef);
OPENVINO_ASSERT(input_md.get_format_kind() != dnnl::memory::format_kind::any,
"[GPU] The format kind of the input memory descriptor of onednn reorder cannot be 'any'.");
@@ -87,8 +87,8 @@ public:
const kernel_impl_params* impl_params = reinterpret_cast<kernel_impl_params*>(ib.getKernelImplParams());
- auto input_md = onednn::layout_to_memory_desc(impl_params->get_input_layout(0), dnnl::memory::format_tag::undef, onednn::mem_flags::need_blocked);
- auto output_md = onednn::layout_to_memory_desc(impl_params->get_output_layout(), dnnl::memory::format_tag::undef, onednn::mem_flags::need_blocked);
+ auto input_md = onednn::layout_to_memory_desc_blocked(impl_params->get_input_layout(0), dnnl::memory::format_tag::undef);
+ auto output_md = onednn::layout_to_memory_desc_blocked(impl_params->get_output_layout(), dnnl::memory::format_tag::undef);
auto prim_desc = std::make_shared<dnnl::reorder::primitive_desc>(
ib.get_engine().get_onednn_engine(),
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp
index 2af7f0e7ce6..79a270326a9 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.cpp
@@ -10,6 +10,39 @@
namespace cldnn {
namespace onednn {
+std::string memory_desc_to_string(const dnnl::memory::desc& desc) {
+ auto get_format_tag_from_desc = [](const dnnl::memory::desc& d) {
+ if (d.get_format_kind() != dnnl::memory::format_kind::blocked) {
+ return dnnl::memory::format_tag::undef;
+ }
+ for (int tag = 0; tag < static_cast<int>(dnnl::memory::format_tag::format_tag_last); ++tag) {
+ auto format_tag = static_cast<dnnl::memory::format_tag>(tag);
+ try {
+ dnnl::memory::desc test_desc(d.get_dims(), d.get_data_type(), format_tag);
+ if (test_desc == d) {
+ return format_tag;
+ }
+ } catch (...) {
+ continue;
+ }
+ }
+ return dnnl::memory::format_tag::undef;
+ };
+
+ std::stringstream ss;
+ ss << dnnl_dt2str(static_cast<dnnl_data_type_t>(desc.get_data_type())) << ":";
+ ss << dnnl_fmt_tag2str(static_cast<dnnl_format_tag_t>(get_format_tag_from_desc(desc))) << ":";
+ for (int i = 0; i < desc.get_ndims(); i++) {
+ ss << (i ? "x" : "") << desc.get_dims()[i];
+ }
+ ss << ":strides=";
+ for (int i = 0; i < desc.get_ndims(); i++) {
+ ss << (i ? "x" : "") << desc.get_strides()[i];
+ }
+
+ return ss.str();
+}
+
template <typename T>
cldnn::memory::ptr convert_zp_data_to_s32(const memory::ptr zp_memory) {
auto engine = zp_memory->get_engine();
@@ -297,150 +330,248 @@ static const std::map<cldnn::format::type, dnnl::memory::format_tag> format_map_
{ cldnn::format::oxiy, dnnl::memory::format_tag::acb },
};
-std::tuple<dnnl::memory::desc, dnnl::memory::desc, dnnl::memory::desc>
-get_conv_memory_descs(cldnn::layout input_layout, cldnn::layout weights_layout, cldnn::layout output_layout, dnnl::memory::format_tag target_fmt) {
- mem_flags flag = (input_layout.format.is_blocked() || output_layout.format.is_blocked()) ? mem_flags::need_blocked : mem_flags::None;
- flag = format::is_grouped(weights_layout.format) ? mem_flags::grouped : flag;
- dnnl::memory::desc input_desc = layout_to_memory_desc(input_layout, target_fmt, flag);
- dnnl::memory::desc weights_desc = layout_to_memory_desc(weights_layout, dnnl::memory::format_tag::any, flag);
- dnnl::memory::desc output_desc = layout_to_memory_desc(output_layout, target_fmt, flag);
- return {input_desc, weights_desc, output_desc};
-}
+// Internal builder class for memory descriptor construction
+class MemoryDescriptorBuilder {
+public:
+ explicit MemoryDescriptorBuilder(const cldnn::layout& l, dnnl::memory::format_tag target_fmt)
+ : _layout(l)
+ , _target_fmt(target_fmt)
+ , _flatten(false)
+ , _use_strides(false)
+ , _keep_rank(false) {}
-inline mem_flags operator|(mem_flags lhs, mem_flags rhs) {
- using T = std::underlying_type_t<mem_flags>;
- return static_cast<mem_flags>(static_cast<T>(lhs) | static_cast<T>(rhs));
-}
-
-inline mem_flags& operator|=(mem_flags& lhs, mem_flags rhs) {
- lhs = lhs | rhs;
- return lhs;
-}
-
-inline mem_flags operator&(mem_flags lhs, mem_flags rhs) {
- using T = std::underlying_type_t<mem_flags>;
- return static_cast<mem_flags>(static_cast<T>(lhs) & static_cast<T>(rhs));
-}
-
-inline bool has_mem_flag(mem_flags flags, mem_flags flag_to_check) {
- return (flags & flag_to_check) != mem_flags::None;
-}
-
-dnnl::memory::desc layout_to_memory_desc(cldnn::layout l, dnnl::memory::format_tag target_fmt, mem_flags flags) {
- dnnl::memory::dims dims;
- bool flatten = has_mem_flag(flags, mem_flags::flatten);
- bool use_strides = has_mem_flag(flags, mem_flags::use_strides);
- bool need_blocked = has_mem_flag(flags, mem_flags::need_blocked);
- bool is_grouped = has_mem_flag(flags, mem_flags::grouped);
- if (target_fmt == dnnl::memory::format_tag::ab && flatten) {
- dims = flatten_tensor(l.get_tensor());
- dims.insert(dims.begin(), 1);
- } else if (target_fmt == dnnl::memory::format_tag::ab) {
- dims.push_back(l.batch());
- dims.push_back(l.get_tensor().count() / l.batch());
- } else if (target_fmt == dnnl::memory::format_tag::abc) {
- dims.push_back(l.batch());
- dims.push_back(l.feature());
- dims.push_back(l.spatial(1));
- } else if (target_fmt == dnnl::memory::format_tag::acb) {
- dims.push_back(l.batch());
- dims.push_back(l.spatial(1));
- dims.push_back(l.feature());
- } else if (target_fmt == dnnl::memory::format_tag::abdc) {
- dims.push_back(l.batch());
- dims.push_back(l.feature());
- dims.push_back(l.spatial(0));
- dims.push_back(l.spatial(1));
- } else if (target_fmt == dnnl::memory::format_tag::abced) {
- dims.push_back(l.batch());
- dims.push_back(l.feature());
- dims.push_back(l.spatial(2));
- dims.push_back(l.spatial(0));
- dims.push_back(l.spatial(1));
- } else if (target_fmt == dnnl::memory::format_tag::abcdfe) {
- dims.push_back(l.batch());
- dims.push_back(l.feature());
- dims.push_back(l.spatial(3));
- dims.push_back(l.spatial(2));
- dims.push_back(l.spatial(0));
- dims.push_back(l.spatial(1));
- } else if (target_fmt == dnnl::memory::format_tag::ba) {
- dims.push_back(l.feature());
- dims.push_back(l.get_tensor().count() / l.feature());
- } else if (flatten) {
- dims = flatten_tensor(l.get_tensor());
- } else {
- // clDNN expresses 3d tensor with 4d format. This code is to use 3d format on oneDNN for such case.
- // However, if the memory::desc to be converted is related to another blocked format, it should be expanded to a 4d tensor.
- auto shape_rank = l.is_dynamic() ?
- static_cast<size_t>(l.get_partial_shape().rank().get_length()) : l.get_shape().size();
- if (shape_rank == 3 && !need_blocked && !is_grouped) {
- dims.push_back(l.batch());
- dims.push_back(l.feature());
- // In cldnn::layer, when it is a 3D shape, the values ​​of the XY axes can sometimes be flipped,
- // so the larger value of the two is used.
- dims.push_back(std::max(l.spatial(0), l.spatial(1)));
- auto it = format_map_cldnn_4d_to_onednn_3d.find(l.format);
- if (it != format_map_cldnn_4d_to_onednn_3d.end()) {
- target_fmt = it->second;
+ // Enables flattening mode to create a 1D memory descriptor
+ MemoryDescriptorBuilder& as_flattened() {
+ _flatten = true;
+ return *this;
+ }
+
+ // Enables stride-based memory descriptor creation
+ MemoryDescriptorBuilder& with_strides() {
+ _use_strides = true;
+ return *this;
+ }
+
+ // Preserves the original tensor rank during conversion. cldnn::layout only supports 4D to 6D tensors.
+ // If it's a blocked format or grouped format, a 4D tensor cannot be converted to 3D, so it must be set by the caller.
+ MemoryDescriptorBuilder& keep_rank() {
+ _keep_rank = true;
+ return *this;
+ }
+
+ dnnl::memory::desc build() const {
+ auto [dims, updated_fmt] = calculate_dims();
+ auto dt = convert_data_type(_layout.data_type);
+
+ if (_use_strides) {
+ OPENVINO_ASSERT(!_flatten, "The padded layout cannot be flattened.");
+ auto strides = calculate_strides(updated_fmt);
+ return dnnl::memory::desc(dims, dt, strides);
+ } else {
+ dnnl::memory::format_tag fmt = updated_fmt == dnnl::memory::format_tag::undef
+ ? convert_data_format(_layout.format) : updated_fmt;
+ dnnl::memory::desc res(dims, dt, fmt);
+ return res;
+ }
+ }
+
+private:
+ const cldnn::layout& _layout;
+ dnnl::memory::format_tag _target_fmt;
+ bool _flatten;
+ bool _use_strides;
+ bool _keep_rank;
+
+ // Calculates dimensions and determines the appropriate format tag
+ std::pair<dnnl::memory::dims, dnnl::memory::format_tag> calculate_dims() const {
+ dnnl::memory::dims dims;
+ auto fmt_tag = _target_fmt;
+
+ if (fmt_tag == dnnl::memory::format_tag::ab && _flatten) {
+ dims = flatten_tensor(_layout.get_tensor());
+ dims.insert(dims.begin(), 1);
+ } else if (fmt_tag == dnnl::memory::format_tag::ab) {
+ dims.push_back(_layout.batch());
+ dims.push_back(_layout.get_tensor().count() / _layout.batch());
+ } else if (fmt_tag == dnnl::memory::format_tag::abc) {
+ dims.push_back(_layout.batch());
+ dims.push_back(_layout.feature());
+ dims.push_back(_layout.spatial(1));
+ } else if (fmt_tag == dnnl::memory::format_tag::acb) {
+ dims.push_back(_layout.batch());
+ dims.push_back(_layout.spatial(1));
+ dims.push_back(_layout.feature());
+ } else if (fmt_tag == dnnl::memory::format_tag::abdc) {
+ dims.push_back(_layout.batch());
+ dims.push_back(_layout.feature());
+ dims.push_back(_layout.spatial(0));
+ dims.push_back(_layout.spatial(1));
+ } else if (fmt_tag == dnnl::memory::format_tag::abced) {
+ dims.push_back(_layout.batch());
+ dims.push_back(_layout.feature());
+ dims.push_back(_layout.spatial(2));
+ dims.push_back(_layout.spatial(0));
+ dims.push_back(_layout.spatial(1));
+ } else if (fmt_tag == dnnl::memory::format_tag::abcdfe) {
+ dims.push_back(_layout.batch());
+ dims.push_back(_layout.feature());
+ dims.push_back(_layout.spatial(3));
+ dims.push_back(_layout.spatial(2));
+ dims.push_back(_layout.spatial(0));
+ dims.push_back(_layout.spatial(1));
+ } else if (fmt_tag == dnnl::memory::format_tag::ba) {
+ dims.push_back(_layout.feature());
+ dims.push_back(_layout.get_tensor().count() / _layout.feature());
+ } else if (_flatten) {
+ dims = flatten_tensor(_layout.get_tensor());
+ } else {
+ auto shape_rank = _layout.is_dynamic() ?
+ static_cast<size_t>(_layout.get_partial_shape().rank().get_length()) :
+ _layout.get_shape().size();
+ // TODO: Consider refactoring
+ // Converting 4D to 3D and updating format_tag within dims calculation breaks function atomicity.
+ // This logic should be separated for better maintainability.
+ if (shape_rank == 3 && !_keep_rank) {
+ dims.push_back(_layout.batch());
+ dims.push_back(_layout.feature());
+ dims.push_back(std::max(_layout.spatial(0), _layout.spatial(1)));
+ // Since cldnn::format does not support 3D format, format_tag must be changed to be suitable for onednn.
+ auto it = format_map_cldnn_4d_to_onednn_3d.find(_layout.format);
+ if (it != format_map_cldnn_4d_to_onednn_3d.end()) {
+ fmt_tag = it->second;
+ } else {
+ OPENVINO_THROW("[GPU] Unexpected layout format " + _layout.to_short_string());
+ }
} else {
- OPENVINO_THROW("[GPU] Unexpected layout format " + l.to_short_string());
+ auto rank = cldnn::format::dimension(_layout.format);
+ dims = convert_tensor(_layout.get_tensor(), rank, cldnn::format::is_grouped(_layout.format));
}
- } else {
- auto rank = cldnn::format::dimension(l.format);
- dims = convert_tensor(l.get_tensor(), rank, cldnn::format::is_grouped(l.format));
}
+ return {dims, fmt_tag};
}
- dnnl::memory::data_type dt = convert_data_type(l.data_type);
- if (use_strides) {
+ // Calculates strides for onednn memory descriptor based on padded dimensions of cldnn::layout.
+ // onednn internally uses strides instead of format_tag.
+ // format_tags that don't have a 1:1 correspondence with cldnn need to be converted to strides to create the memory descriptor.
+ dnnl::memory::dims calculate_strides(dnnl::memory::format_tag fmt) const {
+ auto padded_dims = _layout.get_padded_dims();
dnnl::memory::dims strides;
- OPENVINO_ASSERT(flatten == false, "The padded layout cannot be flattened.");
- auto padded_dims = l.get_padded_dims();
- if (target_fmt == dnnl::memory::format_tag::ab) {
- strides.push_back(1);
- strides.push_back(padded_dims[0]);
- } else if (target_fmt == dnnl::memory::format_tag::abc) {
- strides.push_back(1);
- strides.push_back(padded_dims[0]);
- strides.push_back(padded_dims[0] * padded_dims[1]);
- } else if (target_fmt == dnnl::memory::format_tag::acb) {
- strides.push_back(1);
- strides.push_back(padded_dims[0]);
- strides.push_back(padded_dims[0] * padded_dims[2]);
- } else if (target_fmt == dnnl::memory::format_tag::abdc) {
- strides.push_back(1);
- strides.push_back(padded_dims[0]);
- strides.push_back(padded_dims[0] * padded_dims[1]);
- strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[3]);
- } else if (target_fmt == dnnl::memory::format_tag::abced) {
- strides.push_back(1);
- strides.push_back(padded_dims[0]);
- strides.push_back(padded_dims[0] * padded_dims[1]);
- strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[2]);
- strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[2] * padded_dims[4]);
- } else if (target_fmt == dnnl::memory::format_tag::abcdfe) {
- strides.push_back(1);
- strides.push_back(padded_dims[0]);
- strides.push_back(padded_dims[0] * padded_dims[1]);
- strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[2]);
- strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[2] * padded_dims[3]);
- strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[2] * padded_dims[3] * padded_dims[5]);
- } else if (target_fmt == dnnl::memory::format_tag::ba) {
- strides.push_back(1);
- strides.push_back(padded_dims[1]);
- } else {
- auto pitches = l.get_pitches();
- strides.assign(pitches.begin(), pitches.end());
+
+ switch (fmt) {
+ case dnnl::memory::format_tag::ab:
+ strides.push_back(1);
+ strides.push_back(padded_dims[0]);
+ break;
+
+ case dnnl::memory::format_tag::abc:
+ strides.push_back(1);
+ strides.push_back(padded_dims[0]);
+ strides.push_back(padded_dims[0] * padded_dims[1]);
+ break;
+
+ case dnnl::memory::format_tag::acb:
+ strides.push_back(1);
+ strides.push_back(padded_dims[0]);
+ strides.push_back(padded_dims[0] * padded_dims[2]);
+ break;
+
+ case dnnl::memory::format_tag::abdc:
+ strides.push_back(1);
+ strides.push_back(padded_dims[0]);
+ strides.push_back(padded_dims[0] * padded_dims[1]);
+ strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[3]);
+ break;
+
+ case dnnl::memory::format_tag::abced:
+ strides.push_back(1);
+ strides.push_back(padded_dims[0]);
+ strides.push_back(padded_dims[0] * padded_dims[1]);
+ strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[2]);
+ strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[2] * padded_dims[4]);
+ break;
+
+ case dnnl::memory::format_tag::abcdfe:
+ strides.push_back(1);
+ strides.push_back(padded_dims[0]);
+ strides.push_back(padded_dims[0] * padded_dims[1]);
+ strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[2]);
+ strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[2] * padded_dims[3]);
+ strides.push_back(padded_dims[0] * padded_dims[1] * padded_dims[2] * padded_dims[3] * padded_dims[5]);
+ break;
+
+ case dnnl::memory::format_tag::ba:
+ strides.push_back(1);
+ strides.push_back(padded_dims[1]);
+ break;
+
+ default: {
+ auto pitches = _layout.get_pitches();
+ strides.assign(pitches.begin(), pitches.end());
+ break;
+ }
}
- dnnl::memory::desc res(dims, dt, strides);
- return res;
- } else {
- dnnl::memory::format_tag fmt = target_fmt == dnnl::memory::format_tag::undef ? convert_data_format(l.format) : target_fmt;
- dnnl::memory::desc res(dims, dt, fmt);
- return res;
+ return strides;
+ }
+};
+
+// Public API functions using the builder
+dnnl::memory::desc layout_to_memory_desc(const cldnn::layout& l, dnnl::memory::format_tag target_fmt) {
+ return MemoryDescriptorBuilder(l, target_fmt).build();
+}
+
+dnnl::memory::desc layout_to_memory_desc_flatten(const cldnn::layout& l, dnnl::memory::format_tag target_fmt) {
+ return MemoryDescriptorBuilder(l, target_fmt).as_flattened().build();
+}
+
+dnnl::memory::desc layout_to_memory_desc_blocked(const cldnn::layout& l, dnnl::memory::format_tag target_fmt) {
+ return MemoryDescriptorBuilder(l, target_fmt).keep_rank().build();
+}
+
+dnnl::memory::desc layout_to_memory_desc_strides(const cldnn::layout& l, dnnl::memory::format_tag target_fmt) {
+ return MemoryDescriptorBuilder(l, target_fmt).with_strides().build();
+}
+
+dnnl::memory::desc layout_to_memory_desc(cldnn::layout l, bool use_default_format, bool is_output_blocked) {
+ OPENVINO_ASSERT(!(use_default_format && is_output_blocked), "[GPU] use_default_format and is_output_blocked are mutually exclusive.");
+
+ if (use_default_format) {
+ return MemoryDescriptorBuilder(l, get_default_data_format(l)).build();
+ }
+
+ auto md = MemoryDescriptorBuilder(l, dnnl::memory::format_tag::undef);
+ if (is_output_blocked) {
+ md.keep_rank();
}
+
+ return md.build();
}
+
+std::tuple<dnnl::memory::desc, dnnl::memory::desc, dnnl::memory::desc>
+get_conv_memory_descs(cldnn::layout input_layout, cldnn::layout weights_layout, cldnn::layout output_layout, dnnl::memory::format_tag target_fmt) {
+ bool need_blocked = input_layout.format.is_blocked() || output_layout.format.is_blocked();
+ bool is_grouped = format::is_grouped(weights_layout.format);
+
+ auto input_mdb = MemoryDescriptorBuilder(input_layout, target_fmt);
+ auto weights_mdb = MemoryDescriptorBuilder(weights_layout, dnnl::memory::format_tag::any);
+ auto output_mdb = MemoryDescriptorBuilder(output_layout, target_fmt);
+
+ if (need_blocked || is_grouped) {
+ input_mdb.keep_rank();
+ weights_mdb.keep_rank();
+ output_mdb.keep_rank();
+ }
+
+ auto input_desc = input_mdb.build();
+ auto weights_desc = weights_mdb.build();
+ auto output_desc = output_mdb.build();
+
+ GPU_DEBUG_TRACE_DETAIL << "input: " << input_layout.to_short_string() << " -> " << memory_desc_to_string(input_desc) << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "weights: " << weights_layout.to_short_string() << " -> " << memory_desc_to_string(weights_desc) << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "output: " << output_layout.to_short_string() << " -> " << memory_desc_to_string(output_desc) << std::endl;
+
+ return {input_desc, weights_desc, output_desc};
+}
+
static void get_identical_order(std::vector<std::vector<size_t>>& orders, std::vector<size_t> order,
size_t first, size_t depth) {
if (depth == 0)
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp
index 6237a9d5cf5..97de21f251f 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/utils.hpp
@@ -37,16 +37,27 @@ cldnn::format convert_data_format(dnnl::memory::format_tag fmt);
dnnl::memory::format_tag get_default_data_format(const cldnn::layout& l);
dnnl::memory::format_tag convert_gemm_data_format(dnnl::memory::dims dims, format target);
-enum class mem_flags : uint32_t {
- None = 0,
- flatten = 1 << 0,
- use_strides = 1 << 1,
- need_blocked = 1 << 2,
- grouped = 1 << 3,
-};
-
-dnnl::memory::desc layout_to_memory_desc(cldnn::layout l,
- dnnl::memory::format_tag target_fmt = dnnl::memory::format_tag::undef, mem_flags flags = mem_flags::None);
+dnnl::memory::desc layout_to_memory_desc(
+ const cldnn::layout& l,
+ dnnl::memory::format_tag target_fmt = dnnl::memory::format_tag::undef);
+
+dnnl::memory::desc layout_to_memory_desc_flatten(
+ const cldnn::layout& l,
+ dnnl::memory::format_tag target_fmt);
+
+dnnl::memory::desc layout_to_memory_desc_strides(
+ const cldnn::layout& l,
+ dnnl::memory::format_tag target_fmt);
+
+dnnl::memory::desc layout_to_memory_desc_blocked(
+ const cldnn::layout& l,
+ dnnl::memory::format_tag target_fmt);
+
+/// This function is specifically designed for quantize post-op inputs where:
+/// - For gemm/fully_connected: always use default format
+/// - For other primitives: use blocked format if output is blocked, otherwise use undef
+dnnl::memory::desc layout_to_memory_desc(cldnn::layout l, bool use_default_format, bool is_output_blocked = false);
+
std::tuple<dnnl::memory::desc, dnnl::memory::desc, dnnl::memory::desc> get_conv_memory_descs(cldnn::layout input_layout,
cldnn::layout weights_layout,
cldnn::layout output_layout,
@@ -70,5 +81,8 @@ bool is_per_tensor(cldnn::data_node& node, int32_t& zp_val);
int get_prelu_mask_from_layouts(const std::function<layout()>& get_output_layout,
const std::function<layout(int32_t)>& get_input_layout,
int32_t slope_input_idx);
+
+std::string memory_desc_to_string(const dnnl::memory::desc& desc);
+
} // namespace onednn
} // namespace cldnn
diff --git a/src/plugins/intel_gpu/src/graph/program_node.cpp b/src/plugins/intel_gpu/src/graph/program_node.cpp
index 35ca510a77a..697c493247f 100644
--- a/src/plugins/intel_gpu/src/graph/program_node.cpp
+++ b/src/plugins/intel_gpu/src/graph/program_node.cpp
@@ -1653,8 +1653,9 @@ void program_node::create_onednn_primitive_attributes(
post_ops.append_binary(alg, dnnl::memory::desc(dims, dt, fmt));
update_onednn_post_op_list(op_type, dep_idx, fmt, false, dims, dt);
} else {
- auto mem_flag = cldnn::format::is_blocked(get_output_layout().format) ? onednn::mem_flags::need_blocked : onednn::mem_flags::None;
- auto mem_desc = onednn::layout_to_memory_desc(in, dnnl::memory::format_tag::undef, mem_flag);
+ auto mem_desc = cldnn::format::is_blocked(get_output_layout().format)
+ ? onednn::layout_to_memory_desc_blocked(in, dnnl::memory::format_tag::undef)
+ : onednn::layout_to_memory_desc(in, dnnl::memory::format_tag::undef);
post_ops.append_binary(alg, mem_desc);
update_onednn_post_op_list(op_type, dep_idx, onednn::convert_data_format(in.format), false,
mem_desc.get_dims(), mem_desc.get_data_type());
@@ -1692,17 +1693,8 @@ void program_node::create_onednn_primitive_attributes(
// ********************************* Common case with output range usage ********************************* //
const auto& q_param = desc.get_typed_fuse_params<QuantizeFuseParams>();
if (q_param->_per_tensor_output_range && q_param->_out_lo < q_param->_out_hi) {
- auto generate_onednn_memory_desc = [this](const cldnn::layout& lay) -> dnnl::memory::desc {
- if (this->is_type<gemm>() || this->is_type<fully_connected>()) {
- return onednn::layout_to_memory_desc(lay, onednn::get_default_data_format(lay));
- } else {
- auto mem_flag = cldnn::format::is_blocked(this->get_output_layout().format) ?
- onednn::mem_flags::need_blocked : onednn::mem_flags::None;
- return onednn::layout_to_memory_desc(lay, dnnl::memory::format_tag::undef, mem_flag);
- }
- };
-
-
+ bool is_fc_or_gemm = this->is_type<gemm>() || this->is_type<fully_connected>();
+ bool is_output_blocked = cldnn::format::is_blocked(this->get_output_layout().format);
// 1. pre-scale & pre-shift
{
if (q_param->_per_tensor_input_scale && q_param->_per_tensor_input_shift) {
@@ -1716,7 +1708,7 @@ void program_node::create_onednn_primitive_attributes(
auto in_scale = get_input_layout(dep_idx++);
resize_layout_for_fc(this, in_scale);
- dnnl::memory::desc in_scale_desc = generate_onednn_memory_desc(in_scale);
+ dnnl::memory::desc in_scale_desc = onednn::layout_to_memory_desc(in_scale, is_fc_or_gemm, is_output_blocked);
post_ops.append_binary(dnnl::algorithm::binary_mul, in_scale_desc);
update_onednn_post_op_list(onednn_post_op_type::binary_mul, dep_idx - 1, onednn::get_default_data_format(in_scale), false,
in_scale_desc.get_dims(), in_scale_desc.get_data_type());
@@ -1730,7 +1722,7 @@ void program_node::create_onednn_primitive_attributes(
auto in_shift = get_input_layout(dep_idx++);
resize_layout_for_fc(this, in_shift);
- dnnl::memory::desc in_shift_desc = generate_onednn_memory_desc(in_shift);
+ dnnl::memory::desc in_shift_desc = onednn::layout_to_memory_desc(in_shift, is_fc_or_gemm, is_output_blocked);
post_ops.append_binary(dnnl::algorithm::binary_add, in_shift_desc);
update_onednn_post_op_list(onednn_post_op_type::binary_add, dep_idx - 1, onednn::get_default_data_format(in_shift), false,
in_shift_desc.get_dims(), in_shift_desc.get_data_type());
@@ -1764,7 +1756,7 @@ void program_node::create_onednn_primitive_attributes(
auto out_scale = get_input_layout(dep_idx++);
resize_layout_for_fc(this, out_scale);
- dnnl::memory::desc out_scale_desc = generate_onednn_memory_desc(out_scale);
+ dnnl::memory::desc out_scale_desc = onednn::layout_to_memory_desc(out_scale, is_fc_or_gemm, is_output_blocked);
post_ops.append_binary(dnnl::algorithm::binary_mul, out_scale_desc);
update_onednn_post_op_list(onednn_post_op_type::binary_mul, dep_idx - 1, onednn::get_default_data_format(out_scale), false,
out_scale_desc.get_dims(), out_scale_desc.get_data_type());
diff --git a/src/plugins/intel_gpu/tests/unit/onednn/utils_test.cpp b/src/plugins/intel_gpu/tests/unit/onednn/utils_test.cpp
index c9e247f5d64..9758ec13ce6 100644
--- a/src/plugins/intel_gpu/tests/unit/onednn/utils_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/onednn/utils_test.cpp
@@ -13,6 +13,7 @@
#include "graph/impls/onednn/utils.hpp"
using namespace cldnn;
+using namespace cldnn::onednn;
struct dnnl_desc_params {
// Descriptor info to test
@@ -296,3 +297,239 @@ TEST(keep_weights_reorder_shape_consistent_test, simple_data_formats) {
EXPECT_EQ(layout.get_partial_shape(), ov::Shape({16, 1, 1, 16, 16}));
}
}
+
+class test_layout_to_memory_desc : public ::testing::Test {
+protected:
+ void SetUp() override {
+ // Common test setup
+ }
+
+ dnnl::memory::format_tag get_format_tag_from_desc(const dnnl::memory::desc& desc) {
+ if (desc.get_format_kind() != dnnl::memory::format_kind::blocked) {
+ return dnnl::memory::format_tag::undef;
+ }
+ for (int tag = 0; tag < static_cast<int>(dnnl::memory::format_tag::format_tag_last); ++tag) {
+ auto format_tag = static_cast<dnnl::memory::format_tag>(tag);
+ try {
+ dnnl::memory::desc test_desc(desc.get_dims(), desc.get_data_type(), format_tag);
+ if (test_desc == desc) {
+ return format_tag;
+ }
+ } catch (...) {
+ continue;
+ }
+ }
+ return dnnl::memory::format_tag::undef;
+ }
+};
+
+TEST_F(test_layout_to_memory_desc, basic_bfyx) {
+ layout l = layout{ov::PartialShape{1, 3, 224, 224}, data_types::f32, format::bfyx};
+
+ auto desc = layout_to_memory_desc(l, dnnl::memory::format_tag::undef);
+
+ EXPECT_EQ(desc.get_ndims(), 4);
+ EXPECT_EQ(desc.get_dims()[0], 1);
+ EXPECT_EQ(desc.get_dims()[1], 3);
+ EXPECT_EQ(desc.get_dims()[2], 224);
+ EXPECT_EQ(desc.get_dims()[3], 224);
+ EXPECT_EQ(desc.get_data_type(), dnnl::memory::data_type::f32);
+ EXPECT_EQ(get_format_tag_from_desc(desc), dnnl::memory::format_tag::abcd);
+}
+
+TEST_F(test_layout_to_memory_desc, 3d_shape) {
+ // 3D shape should be handled specially
+ layout l = layout{ov::PartialShape{1, 256, 64}, data_types::f16, format::bfyx};
+
+ auto desc = layout_to_memory_desc(l, dnnl::memory::format_tag::undef);
+
+ EXPECT_EQ(desc.get_ndims(), 3);
+ EXPECT_EQ(desc.get_data_type(), dnnl::memory::data_type::f16);
+ EXPECT_EQ(get_format_tag_from_desc(desc), dnnl::memory::format_tag::abc);
+}
+
+TEST_F(test_layout_to_memory_desc, 3d_shape_with_ioyx) {
+ // 3D shape should be handled specially
+ layout l = layout{ov::PartialShape{1, 256, 64}, data_types::f16, format::ioyx};
+
+ auto desc = layout_to_memory_desc(l, dnnl::memory::format_tag::any);
+
+ // dnnl::memory::desc represents dnnl::memory::format_tag::bac using strides.
+ // refer to src/plugins/intel_gpu/thirdparty/onednn_gpu/src/common/memory_desc_wrapper.cpp
+ EXPECT_EQ(desc.get_ndims(), 3);
+ EXPECT_EQ(desc.get_dims()[0], 1);
+ EXPECT_EQ(desc.get_dims()[1], 256);
+ EXPECT_EQ(desc.get_dims()[2], 64);
+ EXPECT_EQ(desc.get_data_type(), dnnl::memory::data_type::f16);
+ EXPECT_EQ(get_format_tag_from_desc(desc), dnnl::memory::format_tag::abc);
+ EXPECT_EQ(desc.get_strides()[0], 64);
+ EXPECT_EQ(desc.get_strides()[1], 64);
+ EXPECT_EQ(desc.get_strides()[2], 1);
+}
+
+TEST_F(test_layout_to_memory_desc, with_target_format) {
+ layout l = layout{ov::PartialShape{1, 256}, data_types::i32, format::bfyx};
+ auto desc = layout_to_memory_desc(l, dnnl::memory::format_tag::ab);
+ EXPECT_EQ(desc.get_ndims(), 2);
+ EXPECT_EQ(desc.get_dims()[0], 1);
+ EXPECT_EQ(desc.get_dims()[1], 256);
+ EXPECT_EQ(desc.get_data_type(), dnnl::memory::data_type::s32);
+ EXPECT_EQ(get_format_tag_from_desc(desc), dnnl::memory::format_tag::ab);
+}
+
+
+TEST_F(test_layout_to_memory_desc, flatten_basic) {
+ layout l = layout{ov::PartialShape{2, 3, 4, 5}, data_types::f16, format::bfyx};
+ auto desc = layout_to_memory_desc_flatten(l, dnnl::memory::format_tag::any);
+ EXPECT_EQ(desc.get_ndims(), 1);
+ EXPECT_EQ(desc.get_dims()[0], 2 * 3 * 4 * 5);
+}
+
+TEST_F(test_layout_to_memory_desc, flatten_with_ab_format) {
+ layout l = layout{ov::PartialShape{2, 128}, data_types::f16, format::bfyx};
+ auto desc = layout_to_memory_desc_flatten(l, dnnl::memory::format_tag::ab);
+
+ EXPECT_EQ(desc.get_ndims(), 2);
+ EXPECT_EQ(desc.get_dims()[0], 1); // Prepended dimension
+ EXPECT_EQ(desc.get_dims()[1], 2 * 128);
+}
+
+TEST_F(test_layout_to_memory_desc, flatten_bias) {
+ // Common use case: bias flattening for convolution
+ layout l = layout{ov::PartialShape{64}, data_types::f16, format::bfyx};
+ auto desc = layout_to_memory_desc_flatten(l, dnnl::memory::format_tag::a);
+ EXPECT_EQ(desc.get_ndims(), 1);
+ EXPECT_EQ(desc.get_dims()[0], 64);
+ EXPECT_EQ(get_format_tag_from_desc(desc), dnnl::memory::format_tag::a);
+}
+
+TEST_F(test_layout_to_memory_desc, blocked_format) {
+ layout l = layout{ov::PartialShape{1, 64, 56, 56}, data_types::f16, format::b_fs_yx_fsv16};
+ auto desc = layout_to_memory_desc_blocked(l, dnnl::memory::format_tag::undef);
+ EXPECT_EQ(desc.get_ndims(), 4);
+ EXPECT_GT(desc.get_inner_nblks(), 0); // Should have blocking
+}
+
+TEST_F(test_layout_to_memory_desc, strides_with_padding) {
+ layout l = layout{ov::PartialShape{1, 64, 56, 56}, data_types::f16, format::bfyx};
+ l.data_padding = padding{{0, 0, 2, 2}, {0, 0, 2, 2}};
+ auto desc = layout_to_memory_desc_strides(l, dnnl::memory::format_tag::abcd);
+ EXPECT_EQ(desc.get_ndims(), 4);
+ auto strides = desc.get_strides();
+ EXPECT_GT(strides[2], 56); // Stride should account for padding
+}
+
+TEST_F(test_layout_to_memory_desc, strides_cannot_flatten) {
+ layout l = layout{ov::PartialShape{1, 64, 56, 56}, data_types::f32, format::bfyx};
+ l.data_padding = padding{{0, 0, 1, 1}, {0, 0, 1, 1}};
+ EXPECT_NO_THROW({
+ auto desc = layout_to_memory_desc_strides(l, dnnl::memory::format_tag::abcd);
+ });
+}
+
+TEST_F(test_layout_to_memory_desc, quantize_use_default) {
+ layout l = layout{ov::PartialShape{1, 256}, data_types::f32, format::bfyx};
+ auto desc = layout_to_memory_desc(l, true, false);
+ EXPECT_EQ(desc.get_ndims(), 2);
+ EXPECT_EQ(get_format_tag_from_desc(desc), dnnl::memory::format_tag::ab);
+}
+
+TEST_F(test_layout_to_memory_desc, quantize_output_blocked) {
+ layout l = layout{ov::PartialShape{1, 64, 56, 56}, data_types::f16, format::bfyx};
+ auto desc = layout_to_memory_desc(l, false, true);
+ EXPECT_EQ(desc.get_ndims(), 4);
+ EXPECT_EQ(get_format_tag_from_desc(desc), dnnl::memory::format_tag::abcd);
+}
+
+TEST_F(test_layout_to_memory_desc, quantize_gemm_case) {
+ // Simulating gemm/fc case where use_default_format=true
+ layout scale_layout = layout{ov::PartialShape{1, 256}, data_types::f16, format::bfyx};
+ auto desc = layout_to_memory_desc(scale_layout, true, false);
+ EXPECT_EQ(desc.get_ndims(), 2);
+ EXPECT_EQ(desc.get_dims()[0], 1);
+ EXPECT_EQ(desc.get_dims()[1], 256);
+}
+
+TEST_F(test_layout_to_memory_desc, quantize_conv_case) {
+ // Simulating conv case where use_default_format=false, is_output_blocked=true
+ layout scale_layout = layout{ov::PartialShape{1, 64}, data_types::f16, format::bfyx};
+ auto desc = layout_to_memory_desc(scale_layout, false, true);
+ EXPECT_EQ(desc.get_ndims(), 4);
+ EXPECT_EQ(desc.get_dims()[0], 1);
+ EXPECT_EQ(desc.get_dims()[1], 64);
+ EXPECT_EQ(desc.get_dims()[2], 1);
+ EXPECT_EQ(desc.get_dims()[3], 1);
+}
+
+TEST_F(test_layout_to_memory_desc, builder_method_chaining_flatten_and_format) {
+ layout l = layout{ov::PartialShape{2, 3, 4, 5}, data_types::f32, format::bfyx};
+ auto desc = layout_to_memory_desc_flatten(l, dnnl::memory::format_tag::ab);
+ EXPECT_EQ(desc.get_ndims(), 2);
+ EXPECT_EQ(desc.get_dims()[0], 1);
+ EXPECT_EQ(desc.get_dims()[1], 2 * 3 * 4 * 5);
+}
+
+TEST_F(test_layout_to_memory_desc, builder_blocked_with_undef_format) {
+ layout l = layout{ov::PartialShape{1, 64, 56, 56}, data_types::f32, format::bfyx};
+ auto desc = layout_to_memory_desc_blocked(l, dnnl::memory::format_tag::undef);
+ EXPECT_EQ(desc.get_ndims(), 4);
+ EXPECT_EQ(get_format_tag_from_desc(desc), dnnl::memory::format_tag::abcd);
+}
+
+TEST_F(test_layout_to_memory_desc, get_conv_memory_descs_query_formats) {
+ layout input_layout = layout{ov::PartialShape{1, 3, 224, 224}, data_types::f32, format::bfyx};
+ layout weights_layout = layout{ov::PartialShape{64, 3, 7, 7}, data_types::f32, format::oiyx};
+ layout output_layout = layout{ov::PartialShape{1, 64, 112, 112}, data_types::f32, format::bfyx};
+
+ // format_tag::any is used in ConvolutionImplementationManager::query_formats().
+ auto [input_desc, weights_desc, output_desc] = get_conv_memory_descs(
+ input_layout, weights_layout, output_layout, dnnl::memory::format_tag::any);
+
+ EXPECT_EQ(input_desc.get_ndims(), 4);
+ EXPECT_EQ(weights_desc.get_ndims(), 4);
+ EXPECT_EQ(output_desc.get_ndims(), 4);
+ EXPECT_EQ(get_format_tag_from_desc(input_desc), dnnl::memory::format_tag::undef);
+ EXPECT_EQ(get_format_tag_from_desc(weights_desc), dnnl::memory::format_tag::undef);
+ EXPECT_EQ(get_format_tag_from_desc(output_desc), dnnl::memory::format_tag::undef);
+}
+
+TEST_F(test_layout_to_memory_desc, get_conv_memory_descs_blocked_format) {
+ layout input_layout = layout{ov::PartialShape{1, 64, 56, 56}, data_types::f16, format::b_fs_yx_fsv16};
+ layout weights_layout = layout{ov::PartialShape{128, 64, 3, 3}, data_types::f16, format::os_is_yx_osv16_isv16};
+ layout output_layout = layout{ov::PartialShape{1, 128, 56, 56}, data_types::f16, format::b_fs_yx_fsv16};
+
+ auto [input_desc, weights_desc, output_desc] = get_conv_memory_descs(
+ input_layout, weights_layout, output_layout, dnnl::memory::format_tag::undef);
+
+ EXPECT_EQ(input_desc.get_ndims(), 4);
+ EXPECT_EQ(output_desc.get_ndims(), 4);
+ // Should be blocked
+ EXPECT_EQ(input_desc.get_inner_nblks(), 1);
+ EXPECT_EQ(output_desc.get_inner_nblks(), 1);
+}
+
+TEST_F(test_layout_to_memory_desc, get_conv_memory_descs_grouped_weights) {
+ layout input_layout = layout{ov::PartialShape{1, 256, 28, 28}, data_types::f32, format::bfyx};
+ layout weights_layout = layout{ov::PartialShape{32, 128, 8, 3, 3}, data_types::f32, format::goiyx};
+ layout output_layout = layout{ov::PartialShape{1, 256, 28, 28}, data_types::f32, format::bfyx};
+
+ auto [input_desc, weights_desc, output_desc] = get_conv_memory_descs(
+ input_layout, weights_layout, output_layout, dnnl::memory::format_tag::undef);
+
+ EXPECT_EQ(weights_desc.get_ndims(), 5); // Grouped weights have 5 dims
+}
+
+TEST_F(test_layout_to_memory_desc, regression_3d_shape_format_selection) {
+ // Regression test: 3D shapes should use abc/acb format correctly
+ layout l_bfyx = layout{ov::PartialShape{1, 256, 64}, data_types::f16, format::bfyx};
+ layout l_byxf = layout{ov::PartialShape{1, 256, 64}, data_types::f16, format::byxf};
+
+ auto desc_bfyx = layout_to_memory_desc(l_bfyx, dnnl::memory::format_tag::undef);
+ auto desc_byxf = layout_to_memory_desc(l_byxf, dnnl::memory::format_tag::undef);
+
+ EXPECT_EQ(desc_bfyx.get_ndims(), 3);
+ EXPECT_EQ(desc_byxf.get_ndims(), 3);
+ EXPECT_EQ(get_format_tag_from_desc(desc_bfyx), dnnl::memory::format_tag::abc);
+ EXPECT_EQ(get_format_tag_from_desc(desc_byxf), dnnl::memory::format_tag::acb);
+ // Format tags should be different (abc vs acb)
+}