Skip to content

Commit

Permalink
Refactor ngraph builders 2 (openvinotoolkit#21043)
Browse files Browse the repository at this point in the history
* Deprecate makeBatchToSpace

* Deprecate makeSpaceToBatch

* Remove makeStridedSlice

* Remove and deprecate makeSlice

* Remove and deprecate makeMVN

* Fix
  • Loading branch information
olpipi authored Nov 21, 2023
1 parent aaa08d0 commit 165f574
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,19 @@ void MvnLayerCPUTest::SetUp() {
for (auto&& shape : inputDynamicShapes)
params.push_back(std::make_shared<ov::op::v0::Parameter>(netPrecision, shape));

auto mvn = ngraph::builder::makeMVN(params[0], acrossChanels, normalizeVariance, eps);
std::shared_ptr<ov::op::v0::MVN> mvn;
if (!axes.empty()) {
mvn = ngraph::builder::makeMVN(params[0], axes, normalizeVariance, eps);
mvn = std::make_shared<ov::op::v0::MVN>(params[0], axes, normalizeVariance, eps);
} else {
mvn = std::make_shared<ov::op::v0::MVN>(params[0], acrossChanels, normalizeVariance, eps);

// OpenVINO MVN implementation implicitly adds 0th dimension to reduction axes set which is not valid behavior
ov::AxisSet axes;
const size_t startAxis = acrossChanels ? 1 : 2;
const size_t numOfDims = params[0]->output(0).get_partial_shape().size();
for (size_t i = startAxis; i < numOfDims; i++)
axes.insert(i);
mvn->set_reduction_axes(axes);
}

rel_threshold = 0.015f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,13 @@ class LoopForDiffShapesLayerCPUTest : public LoopLayerCPUTest {
}

// Body
const auto axis = 1;
auto s = ngraph::builder::makeSlice(body_params[0], {0}, {1}, {1}, {axis}, inType);
ov::Shape constShape = {1};
auto beginNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, std::vector<int64_t>{0});
auto endNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, std::vector<int64_t>{1});
auto strideNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, std::vector<int64_t>{1});
auto axesNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, std::vector<int64_t>{1});
auto s = std::make_shared<ov::op::v8::Slice>(body_params[0], beginNode, endNode, strideNode, axesNode);

auto constant = ngraph::builder::makeConstant(inType, std::vector<size_t>{1}, std::vector<float>{0.5});
auto eltwise = std::make_shared<ov::op::v1::Add>(body_params[0], constant);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,25 @@ class Slice8LayerCPUTest : public testing::WithParamInterface<Slice8LayerTestCPU
// With axes parameter
auto axesNode = std::make_shared<ngraph::opset1::Parameter>(ov::element::i64, ov::Shape{sliceParams.axes.size()});
params.push_back(std::dynamic_pointer_cast<ngraph::opset3::Parameter>(axesNode));
sliceNode = ngraph::builder::makeSlice(params[0], startNode, stopdNode, stepNode, axesNode);
sliceNode = std::make_shared<ov::op::v8::Slice>(params[0], startNode, stopdNode, stepNode, axesNode);
} else {
//without axes parameter
sliceNode = ngraph::builder::makeSlice(params[0], startNode, stopdNode, stepNode);
sliceNode = std::make_shared<ov::op::v8::Slice>(params[0], startNode, stopdNode, stepNode);
}
} else if (secondaryInputType == ngraph::helpers::InputLayerType::CONSTANT) {
// Slice start, stop, step, axes are const.
sliceNode = ngraph::builder::makeSlice(params[0], sliceParams.start, sliceParams.stop, sliceParams.step, sliceParams.axes, netPrecision);;
ov::Shape constShape = {sliceParams.start.size()};
auto beginNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, sliceParams.start.data());
auto endNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, sliceParams.stop.data());
auto strideNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, sliceParams.step.data());
if (!sliceParams.axes.empty()) {
// With axes parameter
auto axesNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, sliceParams.axes.data());
sliceNode = std::make_shared<ov::op::v8::Slice>(params[0], beginNode, endNode, strideNode, axesNode);
} else {
//without axes parameter
sliceNode = std::make_shared<ov::op::v8::Slice>(params[0], beginNode, endNode, strideNode);
}
} else {
// Not supported others.
OPENVINO_THROW("Slice8LayerCPUTest: Unsupported ngraph::helpers::InputLayerType , value: ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,43 @@ class StridedSliceLayerCPUTest : public testing::WithParamInterface<StridedSlice
for (auto&& shape : inputDynamicShapes) {
params.push_back(std::make_shared<ov::op::v0::Parameter>(dataType, shape));
}
std::shared_ptr<ngraph::Node> ss;
ov::NodeVector ss_inputs;
if (secondaryInputType == ngraph::helpers::InputLayerType::PARAMETER) {
ov::Shape inShape = {ssParams.begin.size()};

auto beginNode = std::make_shared<ngraph::opset1::Parameter>(ov::element::i64, inShape);
auto endNode = std::make_shared<ngraph::opset1::Parameter>(ov::element::i64, inShape);
auto strideNode = std::make_shared<ngraph::opset1::Parameter>(ov::element::i64, inShape);
auto beginNode = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, inShape);
auto endNode = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, inShape);
auto strideNode = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, inShape);

params.push_back(std::dynamic_pointer_cast<ngraph::opset3::Parameter>(beginNode));
params.push_back(std::dynamic_pointer_cast<ngraph::opset3::Parameter>(endNode));
params.push_back(std::dynamic_pointer_cast<ngraph::opset3::Parameter>(strideNode));
params.push_back(beginNode);
params.push_back(endNode);
params.push_back(strideNode);

ss = ngraph::builder::makeStridedSlice(params[0], beginNode, endNode, strideNode, inType, ssParams.beginMask,
ssParams.endMask, ssParams.newAxisMask, ssParams.shrinkAxisMask, ssParams.ellipsisAxisMask);
ss_inputs.push_back(params[0]);
ss_inputs.push_back(beginNode);
ss_inputs.push_back(endNode);
ss_inputs.push_back(strideNode);
} else {
ss = ngraph::builder::makeStridedSlice(params[0], ssParams.begin, ssParams.end, ssParams.strides, inType, ssParams.beginMask,
ssParams.endMask, ssParams.newAxisMask, ssParams.shrinkAxisMask, ssParams.ellipsisAxisMask);
ov::Shape constShape = {ssParams.begin.size()};
auto beginNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, ssParams.begin.data());
auto endNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, ssParams.end.data());
auto strideNode = std::make_shared<ov::op::v0::Constant>(ov::element::i64, constShape, ssParams.strides.data());

ss_inputs.push_back(params[0]);
ss_inputs.push_back(beginNode);
ss_inputs.push_back(endNode);
ss_inputs.push_back(strideNode);
}
auto ss = std::make_shared<ov::op::v1::StridedSlice>(ss_inputs[0],
ss_inputs[1],
ss_inputs[2],
ss_inputs[3],
ssParams.beginMask,
ssParams.endMask,
ssParams.newAxisMask,
ssParams.shrinkAxisMask,
ssParams.ellipsisAxisMask);

function = makeNgraphFunction(inType, params, ss, "StridedSlice");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ class RemoveUselessBF16ConvertCPUTest : public testing::WithParamInterface<Remov
auto begin = builder::makeConstant(element::i64, ov::Shape{4}, std::vector<int64_t>{0, 0, 0, 0});
auto end = builder::makeConstant(element::i64, ov::Shape{4}, std::vector<int64_t>{0, 0, 16, 0});
auto stride = builder::makeConstant(element::i64, ov::Shape{4}, std::vector<int64_t>{1, 1, 1, 1});
auto slice = builder::makeStridedSlice(convert,
begin,
end,
stride,
element::f32,
{0, 0, 0, 0},
{1, 1, 0, 1},
{},
{},
{});
auto slice = std::make_shared<ov::op::v1::StridedSlice>(convert,
begin,
end,
stride,
std::vector<int64_t>{0, 0, 0, 0},
std::vector<int64_t>{1, 1, 0, 1},
std::vector<int64_t>{},
std::vector<int64_t>{},
std::vector<int64_t>{});
auto convert2 = std::make_shared<ov::op::v0::Convert>(slice, inType);
function = std::make_shared<ov::Model>(convert2, ov::ParameterVector{input_params}, "remove_convert");
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ class StridedSliceZeroDimsTest : public SubgraphBaseTest {
auto axes = builder::makeConstant(element::i64, {1}, std::vector<int64_t>{0});
auto shapeOf = std::make_shared<opset9::ShapeOf>(inputParams[1]);
auto gather = std::make_shared<opset9::Gather>(shapeOf, indices, axes);
auto strided_slice = builder::makeStridedSlice(inputParams.front(), gather, end, stride, element::f32, {0}, {0});
auto strided_slice = std::make_shared<ov::op::v1::StridedSlice>(inputParams.front(),
gather,
end,
stride,
std::vector<int64_t>{0},
std::vector<int64_t>{0},
std::vector<int64_t>{},
std::vector<int64_t>{},
std::vector<int64_t>{});
NodeVector results{strided_slice};
function = std::make_shared<Function>(results, inputParams, "StridedSliceStaticShape");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ class CropAfterConvolutionTest : public testing::WithParamInterface<cccmParams>,
const std::vector<int64_t> crop_end{20};
const std::vector<int64_t> crop_stride{1};
const std::vector<int64_t> axes{h_index_in_nchw};
OPENVINO_SUPPRESS_DEPRECATED_START
auto split_node = ngraph::builder::makeSlice(convolution_node, crop_begin, crop_end, crop_stride, axes, ngPrc);
OPENVINO_SUPPRESS_DEPRECATED_END

auto convolution_node2 = ngraph::builder::makeConvolution(split_node,
ngPrc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ class MvnLayerGPUTest : public testing::WithParamInterface<MvnLayerGPUTestParamS
params.push_back(std::make_shared<ov::op::v0::Parameter>(netPrecision, shape));

auto axesNode = ngraph::builder::makeConstant(axesType, ngraph::Shape{axes.size()}, axes);
auto mvn = ngraph::builder::makeMVN6(params[0], axesNode, normalizeVariance, eps, eps_mode);
ov::op::MVNEpsMode nEpsMode = ov::op::MVNEpsMode::INSIDE_SQRT;
if (eps_mode == "outside_sqrt")
nEpsMode = ov::op::MVNEpsMode::OUTSIDE_SQRT;
auto mvn = std::make_shared<ov::op::v6::MVN>(params[0], axesNode, normalizeVariance, eps, nEpsMode);

rel_threshold = 0.015f;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,36 @@ class PriorBoxLayerGPUTest : public testing::WithParamInterface<PriorBoxLayerGPU
inType = ov::element::Type(netPrecision);
outType = ElementType::f32;

auto beginInput = ngraph::opset1::Constant::create(ngraph::element::i32, ngraph::Shape{1}, {2});
auto endInput = ngraph::opset1::Constant::create(ngraph::element::i32, ngraph::Shape{1}, {4});
auto strideInput = ngraph::opset1::Constant::create(ngraph::element::i32, ngraph::Shape{1}, {1});
auto beginInput = ov::op::v0::Constant::create(ngraph::element::i32, ngraph::Shape{1}, {2});
auto endInput = ov::op::v0::Constant::create(ngraph::element::i32, ngraph::Shape{1}, {4});
auto strideInput = ov::op::v0::Constant::create(ngraph::element::i32, ngraph::Shape{1}, {1});

ov::ParameterVector functionParams;
for (auto&& shape : inputDynamicShapes)
functionParams.push_back(std::make_shared<ov::op::v0::Parameter>(inType, shape));

auto shapeOfOp1 = std::make_shared<opset3::ShapeOf>(functionParams[0], element::i32);
auto shapeOfOp2 = std::make_shared<opset3::ShapeOf>(functionParams[1], element::i32);


auto stridedSliceOp1 = ngraph::builder::makeStridedSlice(shapeOfOp1, beginInput, endInput, strideInput, element::i32,
{0}, {1}, {0}, {0}, {0});
auto stridedSliceOp2 = ngraph::builder::makeStridedSlice(shapeOfOp2, beginInput, endInput, strideInput, element::i32,
{0}, {1}, {0}, {0}, {0});
auto shapeOfOp1 = std::make_shared<ov::op::v3::ShapeOf>(functionParams[0], element::i32);
auto shapeOfOp2 = std::make_shared<ov::op::v3::ShapeOf>(functionParams[1], element::i32);

auto stridedSliceOp1 = std::make_shared<ov::op::v1::StridedSlice>(shapeOfOp1,
beginInput,
endInput,
strideInput,
std::vector<int64_t>{0},
std::vector<int64_t>{1},
std::vector<int64_t>{0},
std::vector<int64_t>{0},
std::vector<int64_t>{0});

auto stridedSliceOp2 = std::make_shared<ov::op::v1::StridedSlice>(shapeOfOp2,
beginInput,
endInput,
strideInput,
std::vector<int64_t>{0},
std::vector<int64_t>{1},
std::vector<int64_t>{0},
std::vector<int64_t>{0},
std::vector<int64_t>{0});

switch (priorboxType) {
case priorbox_type::Clustered: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ void BatchToSpaceLayerTest::SetUp() {
std::tie(blockShape, cropsBegin, cropsEnd, inputShape, netPrecision, inPrc, outPrc, inLayout, outLayout, targetDevice) = this->GetParam();
auto ngPrc = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(netPrecision);
ov::ParameterVector params{std::make_shared<ov::op::v0::Parameter>(ngPrc, ov::Shape(inputShape))};
OPENVINO_SUPPRESS_DEPRECATED_START
auto b2s = ngraph::builder::makeBatchToSpace(params[0], ngPrc, blockShape, cropsBegin, cropsEnd);
OPENVINO_SUPPRESS_DEPRECATED_END
ngraph::ResultVector results{std::make_shared<ngraph::opset1::Result>(b2s)};
function = std::make_shared<ngraph::Function>(results, params, "BatchToSpace");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ void Mvn1LayerTest::SetUp() {
std::tie(inputShapes, inputPrecision, axes, acrossChanels, normalizeVariance, eps, targetDevice) = this->GetParam();
auto inType = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(inputPrecision);
ov::ParameterVector param {std::make_shared<ov::op::v0::Parameter>(inType, ov::Shape(inputShapes))};
OPENVINO_SUPPRESS_DEPRECATED_START
auto mvn = std::dynamic_pointer_cast<ngraph::op::MVN>(ngraph::builder::makeMVN(param[0], acrossChanels, normalizeVariance, eps));
if (!axes.empty()) {
mvn = std::dynamic_pointer_cast<ngraph::op::MVN>(ngraph::builder::makeMVN(param[0], axes, normalizeVariance, eps));
}
OPENVINO_SUPPRESS_DEPRECATED_END
ngraph::ResultVector results{std::make_shared<ngraph::opset1::Result>(mvn)};
function = std::make_shared<ngraph::Function>(results, param, "MVN1");
}
Expand Down Expand Up @@ -82,7 +84,9 @@ void Mvn6LayerTest::SetUp() {

ov::ParameterVector param {std::make_shared<ov::op::v0::Parameter>(dataType, ov::Shape(inputShapes))};
auto axesNode = ngraph::builder::makeConstant(axesType, ngraph::Shape{axes.size()}, axes);
OPENVINO_SUPPRESS_DEPRECATED_START
auto mvn = ngraph::builder::makeMVN6(param[0], axesNode, normalizeVariance, eps, epsMode);
OPENVINO_SUPPRESS_DEPRECATED_END
ngraph::ResultVector results{std::make_shared<ngraph::opset1::Result>(mvn)};
function = std::make_shared<ngraph::Function>(results, param, "MVN6");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ void Slice8LayerTest::SetUp() {
for (auto&& shape : inputDynamicShapes) {
params.push_back(std::make_shared<ov::op::v0::Parameter>(netPrecision, shape));
}
OPENVINO_SUPPRESS_DEPRECATED_START
auto sliceOp = ngraph::builder::makeSlice(params[0], sliceParams.start, sliceParams.stop, sliceParams.step, sliceParams.axes, netPrecision);
OPENVINO_SUPPRESS_DEPRECATED_END

ov::ResultVector results;
for (int i = 0; i < sliceOp->get_output_size(); i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ void SpaceToBatchLayerTest::SetUp() {

auto ngPrc = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(netPrecision);
ov::ParameterVector params{std::make_shared<ov::op::v0::Parameter>(ngPrc, ov::Shape(inputShape))};
OPENVINO_SUPPRESS_DEPRECATED_START
auto s2b = ngraph::builder::makeSpaceToBatch(params[0], ngPrc, blockShape, padsBegin, padsEnd);
OPENVINO_SUPPRESS_DEPRECATED_END
ngraph::ResultVector results{std::make_shared<ngraph::opset1::Result>(s2b)};
function = std::make_shared<ngraph::Function>(results, params, "SpaceToBatch");
}
Expand Down
Loading

0 comments on commit 165f574

Please sign in to comment.