forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
130 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/common/transformations/tests/common_optimizations/fuse_u4_weights_zero_point.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "transformations/common_optimizations/fuse_u4_weights_zero_point.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "common_test_utils/ov_test_utils.hpp" | ||
#include "openvino/op/convert.hpp" | ||
#include "openvino/op/subtract.hpp" | ||
#include "openvino/op/multiply.hpp" | ||
#include "openvino/core/model.hpp" | ||
#include "openvino/pass/manager.hpp" | ||
|
||
using namespace testing; | ||
using namespace ov; | ||
|
||
TEST_F(TransformationTestsF, FuseU4WeightsAndZeroPoint) { | ||
auto decompression_precision = ov::element::f32; | ||
ov::Shape weights_shape{32, 128, 64}; | ||
ov::Shape decompression_shape{32, 1, 64}; | ||
{ | ||
std::vector<int> weights_values(ov::shape_size(weights_shape)); | ||
for (size_t i = 0; i < weights_values.size(); ++i) { | ||
weights_values[i] = i % 16; | ||
} | ||
auto weights = ov::op::v0::Constant::create(ov::element::u4, weights_shape, weights_values); | ||
auto convert = std::make_shared<ov::op::v0::Convert>(weights, decompression_precision); | ||
auto zero_point = ov::op::v0::Constant::create(decompression_precision, decompression_shape, {8}); | ||
auto subtract = std::make_shared<ov::op::v1::Subtract>(convert, zero_point); | ||
auto scale = ov::op::v0::Constant::create(decompression_precision, decompression_shape, {3.f}); | ||
auto multiply = std::make_shared<ov::op::v1::Multiply>(subtract, scale); | ||
model = std::make_shared<Model>(NodeVector{multiply}, ParameterVector{}); | ||
manager.register_pass<ov::pass::FuseU4WeightsAndZeroPoint>(); | ||
} | ||
{ | ||
std::vector<int> weights_values(ov::shape_size(weights_shape)); | ||
for (size_t i = 0; i < weights_values.size(); ++i) { | ||
weights_values[i] = i % 16 - 8; | ||
} | ||
auto weights = ov::op::v0::Constant::create(ov::element::i4, weights_shape, weights_values); | ||
auto convert = std::make_shared<ov::op::v0::Convert>(weights, decompression_precision); | ||
auto scale = ov::op::v0::Constant::create(decompression_precision, decompression_shape, {3.f}); | ||
auto multiply = std::make_shared<ov::op::v1::Multiply>(convert, scale); | ||
model_ref = std::make_shared<Model>(NodeVector{multiply}, ParameterVector{}); | ||
} | ||
} | ||
|
||
TEST_F(TransformationTestsF, FuseU4WeightsAndZeroPointUnaccaptableZeroPoint) { | ||
auto decompression_precision = ov::element::f32; | ||
ov::Shape weights_shape{32, 128, 64}; | ||
ov::Shape decompression_shape{32, 1, 64}; | ||
|
||
std::vector<int> weights_values(ov::shape_size(weights_shape)); | ||
for (size_t i = 0; i < weights_values.size(); ++i) { | ||
weights_values[i] = i % 8; | ||
} | ||
auto weights = ov::op::v0::Constant::create(ov::element::u4, weights_shape, weights_values); | ||
auto convert = std::make_shared<ov::op::v0::Convert>(weights, decompression_precision); | ||
auto zero_point = ov::op::v0::Constant::create(decompression_precision, decompression_shape, {4}); | ||
auto subtract = std::make_shared<ov::op::v1::Subtract>(convert, zero_point); | ||
auto scale = ov::op::v0::Constant::create(decompression_precision, decompression_shape, {3.f}); | ||
auto multiply = std::make_shared<ov::op::v1::Multiply>(subtract, scale); | ||
model = std::make_shared<Model>(NodeVector{multiply}, ParameterVector{}); | ||
manager.register_pass<ov::pass::FuseU4WeightsAndZeroPoint>(); | ||
} | ||
|
||
TEST_F(TransformationTestsF, FuseU4WeightsAndZeroPointOutOfI4Range) { | ||
auto decompression_precision = ov::element::f32; | ||
ov::Shape weights_shape{32, 128, 64}; | ||
ov::Shape decompression_shape{32, 1, 64}; | ||
|
||
std::vector<int> weights_values(ov::shape_size(weights_shape)); | ||
for (size_t i = 0; i < weights_values.size(); ++i) { | ||
weights_values[i] = i % 16; | ||
} | ||
auto weights = ov::op::v0::Constant::create(ov::element::u4, weights_shape, weights_values); | ||
auto convert = std::make_shared<ov::op::v0::Convert>(weights, decompression_precision); | ||
std::vector<float> zero_point_values(ov::shape_size(decompression_shape)); | ||
zero_point_values.back() = 16; | ||
auto zero_point = ov::op::v0::Constant::create(decompression_precision, decompression_shape, zero_point_values); | ||
auto subtract = std::make_shared<ov::op::v1::Subtract>(convert, zero_point); | ||
auto scale = ov::op::v0::Constant::create(decompression_precision, decompression_shape, {3.f}); | ||
auto multiply = std::make_shared<ov::op::v1::Multiply>(subtract, scale); | ||
model = std::make_shared<Model>(NodeVector{multiply}, ParameterVector{}); | ||
manager.register_pass<ov::pass::FuseU4WeightsAndZeroPoint>(); | ||
} | ||
|
||
TEST_F(TransformationTestsF, FuseU4WeightsAndZeroPointWrongWeightsPrecision) { | ||
auto decompression_precision = ov::element::f32; | ||
ov::Shape weights_shape{32, 128, 64}; | ||
|
||
std::vector<int> weights_values(ov::shape_size(weights_shape)); | ||
for (size_t i = 0; i < weights_values.size(); ++i) { | ||
weights_values[i] = i % 256; | ||
} | ||
auto weights = ov::op::v0::Constant::create(ov::element::u8, weights_shape, weights_values); | ||
auto convert = std::make_shared<ov::op::v0::Convert>(weights, decompression_precision); | ||
auto zero_point = ov::op::v0::Constant::create(decompression_precision, {32, 1, 64}, {8}); | ||
auto subtract = std::make_shared<ov::op::v1::Subtract>(convert, zero_point); | ||
auto scale = ov::op::v0::Constant::create(decompression_precision, {32, 1, 64}, {3.f}); | ||
auto multiply = std::make_shared<ov::op::v1::Multiply>(subtract, scale); | ||
model = std::make_shared<Model>(NodeVector{multiply}, ParameterVector{}); | ||
manager.register_pass<ov::pass::FuseU4WeightsAndZeroPoint>(); | ||
} | ||
|
||
TEST_F(TransformationTestsF, FuseU4WeightsAndZeroPointSeveralWeightsConsumers) { | ||
auto decompression_precision = ov::element::f32; | ||
ov::Shape weights_shape{32, 128, 64}; | ||
ov::Shape decompression_shape{32, 1, 64}; | ||
|
||
auto weights = ov::op::v0::Constant::create(ov::element::u4, weights_shape, {4}); | ||
auto convert = std::make_shared<ov::op::v0::Convert>(weights, decompression_precision); | ||
auto additional_consumer = std::make_shared<ov::op::v0::Convert>(weights, ov::element::i32); | ||
auto additional_op = std::make_shared<ov::op::v1::Multiply>(additional_consumer, ov::op::v0::Constant::create(ov::element::i32, {}, {-1})); | ||
auto zero_point = ov::op::v0::Constant::create(decompression_precision, decompression_shape, {8}); | ||
auto subtract = std::make_shared<ov::op::v1::Subtract>(convert, zero_point); | ||
auto scale = ov::op::v0::Constant::create(decompression_precision, decompression_shape, {3.f}); | ||
auto multiply = std::make_shared<ov::op::v1::Multiply>(subtract, scale); | ||
model = std::make_shared<Model>(NodeVector{multiply, additional_op}, ParameterVector{}); | ||
manager.register_pass<ov::pass::FuseU4WeightsAndZeroPoint>(); | ||
} |