Skip to content

Commit

Permalink
[GPU] Fix a bug of onednn asymmetric convolution (#26536)
Browse files Browse the repository at this point in the history
### Details:
- *Fixed incorrect value ​​being set when weights zero-points are
scalar.*

### Tickets:
 - *151898*
  • Loading branch information
jade-cho authored Sep 12, 2024
1 parent 97030ad commit 4785767
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,39 @@ bool all_same_value(const T* qp_ptr, size_t size) {
});
}

std::shared_ptr<ov::Node> scalar_parameter(std::shared_ptr<ov::op::v0::Constant> qp) {
template <typename T>
std::shared_ptr<ov::op::v0::Constant>
create_scalar_constant(const std::shared_ptr<ov::op::v0::Constant>& qp) {
auto type = qp->get_element_type();
size_t size = ov::shape_size(qp->get_shape());
bool has_same_value = false;
switch (type) {
case ov::element::u8:
has_same_value = all_same_value(static_cast<const uint8_t*>(qp->get_data_ptr()), size);
break;
case ov::element::i8:
has_same_value = all_same_value(static_cast<const int8_t*>(qp->get_data_ptr()), size);
break;
case ov::element::f16:
has_same_value = all_same_value(static_cast<const ov::float16*>(qp->get_data_ptr()), size);
break;
case ov::element::f32:
has_same_value = all_same_value(static_cast<const float*>(qp->get_data_ptr()), size);
break;
default: OPENVINO_THROW("[GPU] Can't pad quantization parameter for ", type, " element type");
auto shape = qp->get_shape();
if (all_same_value(static_cast<const T*>(qp->get_data_ptr()), ov::shape_size(shape))) {
ov::Shape new_shape(shape.size(), 1);
ov::Tensor new_tensor(type, new_shape);
auto new_qp = std::make_shared<ov::op::v0::Constant>(new_tensor);
auto val = qp->get_vector<T>()[0];
new_qp->fill_data(type, val);
return new_qp;
}
return nullptr;
}

if (has_same_value) {
auto new_shape = qp->get_shape();
std::fill(new_shape.begin(), new_shape.end(), 1);
ov::Tensor new_qp(type, new_shape);
return std::make_shared<ov::op::v0::Constant>(new_qp);
std::shared_ptr<ov::Node> scalar_parameter(std::shared_ptr<ov::op::v0::Constant> qp) {
auto type = qp->get_element_type();
std::shared_ptr<ov::op::v0::Constant> new_qp = nullptr;

if (type == ov::element::u8) {
new_qp = create_scalar_constant<uint8_t>(qp);
} else if (type == ov::element::i8) {
new_qp = create_scalar_constant<int8_t>(qp);
} else if (type == ov::element::f16) {
new_qp = create_scalar_constant<ov::float16>(qp);
} else if (type == ov::element::f32) {
new_qp = create_scalar_constant<float>(qp);
} else {
OPENVINO_THROW("[GPU] Can't pad quantization parameter for ", type, " element type");
}

return nullptr;
return new_qp;
}

} // namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,61 @@ TEST_F(TransformationTestsF, BroadcastAndPadZeroPointBuffers_3) {
}
}

TEST_F(TransformationTestsF, BroadcastAndPadZeroPointBuffers_scalar_wzp) {
ov::Strides strides{1, 1};
ov::Strides dilations{1, 1};
ov::CoordinateDiff pads_begin{0, 0};
ov::CoordinateDiff pads_end{0, 0};
{
auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::u8, ov::PartialShape{ 1, 8, 11, 12 });
auto weights_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{ 8, 8, 3, 3 }, { 1 });
auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
auto azp_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{ 1, 1, 1, 1 }, { 1 });
auto wzp_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{ 1, 8, 1, 1 }, { 12 });
auto compensation = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{ 1, 8, 1, 1 }, { 1 });
auto conv = std::make_shared<ov::intel_gpu::op::Convolution>(input,
weights_const,
no_bias,
azp_const,
wzp_const,
compensation,
strides,
pads_begin,
pads_end,
dilations,
-1,
ov::op::PadType::EXPLICIT,
ov::element::f32);

model = std::make_shared<ov::Model>(ov::NodeVector{ conv }, ov::ParameterVector{ input });
manager.register_pass<BroadcastAndPadZeroPointBuffers>(8, true);
}
{
auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::u8, ov::PartialShape{ 1, 8, 11, 12 });
auto weights_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{ 8, 8, 3, 3 }, { 1 });
auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
auto azp_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{ 1, 8, 1, 1 }, { 1 });
auto wzp_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{ 1, 1, 1, 1 }, { 12 });
auto compensation = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{ 1, 8, 1, 1 }, { 1 });
auto conv = std::make_shared<ov::intel_gpu::op::Convolution>(input,
weights_const,
no_bias,
azp_const,
wzp_const,
compensation,
strides,
pads_begin,
pads_end,
dilations,
-1,
ov::op::PadType::EXPLICIT,
ov::element::f32);

model_ref = std::make_shared<ov::Model>(ov::NodeVector{ conv }, ov::ParameterVector{ input });
}
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
}

} // namespace intel_gpu
} // namespace test
} // namespace ov

0 comments on commit 4785767

Please sign in to comment.