forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add floor, shape_of and squeeze patterns #175
Open
kurapov-peter
wants to merge
2
commits into
mlir
Choose a base branch
from
pakurapo/floor-squeeze
base: mlir
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
46 changes: 46 additions & 0 deletions
46
src/common/transformations/src/transformations/mlir/op/floor.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,46 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "mlir/Dialect/Linalg/Passes.h" | ||
#include "mlir/Dialect/Tensor/IR/Tensor.h" | ||
|
||
#include <openvino/op/floor.hpp> | ||
#include "openvino/pass/pattern/op/wrap_type.hpp" | ||
|
||
#include "floor.hpp" | ||
#include "../convert_common.hpp" | ||
|
||
|
||
namespace { | ||
|
||
using namespace ov::mlir; | ||
|
||
struct ConvertFloor { | ||
void operator()(ConversionContext& context, NodePtr node) { | ||
auto loc = createLocation(context.context, node); | ||
auto& builder = context.builder(); | ||
const auto input = context.getInputs(node)[0]; | ||
const auto ov_output_element_type = node->get_output_element_type(0); | ||
const auto ov_output_shape = node->get_output_partial_shape(0); | ||
auto outType = importTensor(context.context, ov_output_shape, ov_output_element_type); | ||
auto dynamic_dimensions = context.get_dynamic_dimension_values(ov_output_shape); | ||
auto empty = builder.create<tensor::EmptyOp>(loc, outType, dynamic_dimensions); | ||
auto floor = builder.create<linalg::FloorOp>(loc, mlir::ValueRange{input}, mlir::ValueRange{empty}); | ||
context.addOutputs(node, floor); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace ov { | ||
namespace mlir { | ||
|
||
using namespace ov::pass::pattern; | ||
using namespace ov::op; | ||
|
||
FloorPattern::FloorPattern() | ||
: MarkPattern(wrap_type<v0::Floor>({any_input()}), ConvertFloor()) {} | ||
|
||
} // namespace mlir | ||
} // namespace ov |
23 changes: 23 additions & 0 deletions
23
src/common/transformations/src/transformations/mlir/op/floor.hpp
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,23 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/MLIRContext.h" | ||
#include "mlir/IR/Value.h" | ||
|
||
#include "../conversion_context.hpp" | ||
|
||
namespace ov { | ||
namespace mlir { | ||
|
||
class FloorPattern : public MarkPattern { | ||
public: | ||
OPENVINO_RTTI("FloorPattern", "0"); | ||
FloorPattern(); | ||
}; | ||
|
||
} // namespace mlir | ||
} // namespace ov |
46 changes: 46 additions & 0 deletions
46
src/common/transformations/src/transformations/mlir/op/shape_of.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,46 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "mlir/Dialect/Shape/IR/Shape.h" | ||
#include "mlir/Dialect/Linalg/Passes.h" | ||
#include "mlir/Dialect/Tensor/IR/Tensor.h" | ||
#include "mlir/Dialect/Arith/IR/Arith.h" | ||
|
||
#include <openvino/op/shape_of.hpp> | ||
#include "openvino/pass/pattern/op/wrap_type.hpp" | ||
|
||
#include "shape_of.hpp" | ||
#include "../convert_common.hpp" | ||
|
||
|
||
namespace { | ||
|
||
using namespace ov::mlir; | ||
|
||
struct ConvertShapeOf { | ||
void operator()(ConversionContext& context, NodePtr node) { | ||
auto loc = createLocation(context.context, node); | ||
auto& builder = context.builder(); | ||
const auto ov_output_element_type = node->get_output_element_type(0); | ||
const auto ov_output_shape = node->get_output_partial_shape(0); | ||
const auto input = context.getInputs(node)[0]; | ||
auto shapeOf = builder.create<shape::ShapeOfOp>(loc, mlir::ValueRange{input}); | ||
auto casted_type = RankedTensorType::get(ArrayRef(importShape(ov_output_shape)), importPrecision(context.context, ov_output_element_type)); | ||
auto cast = builder.create<arith::IndexCastOp>(loc, casted_type, mlir::ValueRange{shapeOf}); | ||
context.addOutputs(node, cast); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace ov { | ||
namespace mlir { | ||
|
||
using namespace ov::pass::pattern; | ||
using namespace ov::op; | ||
|
||
ShapeOfPattern::ShapeOfPattern() : MarkPattern(wrap_type<v3::ShapeOf>({any_input()}), ConvertShapeOf()) {} | ||
|
||
} // namespace mlir | ||
} // namespace ov |
23 changes: 23 additions & 0 deletions
23
src/common/transformations/src/transformations/mlir/op/shape_of.hpp
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,23 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/MLIRContext.h" | ||
#include "mlir/IR/Value.h" | ||
|
||
#include "../conversion_context.hpp" | ||
|
||
namespace ov { | ||
namespace mlir { | ||
|
||
class ShapeOfPattern : public MarkPattern { | ||
public: | ||
OPENVINO_RTTI("ShapeOfPattern", "0"); | ||
ShapeOfPattern(); | ||
}; | ||
|
||
} // namespace mlir | ||
} // namespace ov |
56 changes: 56 additions & 0 deletions
56
src/common/transformations/src/transformations/mlir/op/squeeze.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,56 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "mlir/Dialect/Linalg/Passes.h" | ||
#include "mlir/Dialect/Tensor/IR/Tensor.h" | ||
|
||
#include <openvino/op/squeeze.hpp> | ||
#include "openvino/pass/pattern/op/wrap_type.hpp" | ||
|
||
#include "squeeze.hpp" | ||
#include "../convert_common.hpp" | ||
|
||
|
||
namespace { | ||
|
||
using namespace ov::mlir; | ||
|
||
struct ConvertSqueeze { | ||
void operator()(ConversionContext& context, NodePtr node) { | ||
auto loc = createLocation(context.context, node); | ||
auto& builder = context.builder(); | ||
const auto input = context.getInputs(node)[0]; | ||
|
||
auto src_partial_shape = node->get_input_partial_shape(0); | ||
auto src_rank = src_partial_shape.rank().get_length(); | ||
SmallVector<ReassociationIndices> collapse_groups; | ||
ReassociationIndices group = ReassociationIndices(); | ||
for (size_t src_i = 0; src_i < src_rank; src_i++) { | ||
auto src_d = src_partial_shape[src_i]; | ||
group.push_back(src_i); | ||
if (src_d.is_static() && src_d.get_length() == 1) { | ||
// continue collecting | ||
} else { | ||
collapse_groups.emplace_back(group); | ||
group = ReassociationIndices(); | ||
} | ||
} | ||
|
||
auto reshape = builder.create<tensor::CollapseShapeOp>(loc, input, collapse_groups); | ||
context.addOutputs(node, reshape); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace ov { | ||
namespace mlir { | ||
|
||
using namespace ov::pass::pattern; | ||
using namespace ov::op; | ||
|
||
SqueezePattern::SqueezePattern() : MarkPattern(wrap_type<v0::Squeeze>({any_input()}), ConvertSqueeze()) {} | ||
|
||
} // namespace mlir | ||
} // namespace ov |
23 changes: 23 additions & 0 deletions
23
src/common/transformations/src/transformations/mlir/op/squeeze.hpp
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,23 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/MLIRContext.h" | ||
#include "mlir/IR/Value.h" | ||
|
||
#include "../conversion_context.hpp" | ||
|
||
namespace ov { | ||
namespace mlir { | ||
|
||
class SqueezePattern : public MarkPattern { | ||
public: | ||
OPENVINO_RTTI("SqueezePattern", "0"); | ||
SqueezePattern(); | ||
}; | ||
|
||
} // namespace mlir | ||
} // namespace ov |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will we need the same in the GC pipeline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not if we only feed it with statically shaped batches. In any case, adding it should be a single-line change, so we can just add it.