Skip to content
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

Match and lower ov::Relu #143

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions src/common/transformations/src/transformations/mlir/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include <algorithm>
#include <functional>
#include <openvino/op/add.hpp>
#include <openvino/op/subtract.hpp>
#include <openvino/op/multiply.hpp>
#include <openvino/op/divide.hpp>

#include <openvino/op/multiply.hpp>
#include <openvino/op/relu.hpp>
#include <openvino/op/subtract.hpp>
#include <openvino/pass/graph_rewrite.hpp>
#include <openvino/pass/manager.hpp>
#include <openvino/pass/pattern/op/wrap_type.hpp>
Expand Down Expand Up @@ -269,6 +269,70 @@ class Partitioner : public ov::pass::ModelPass {
}
};

struct ConvertRelu {
void operator()(ConversionContext& context, NodePtr node) {
auto loc = createLocation(context.context, node);
auto& builder = context.builder();
// TODO: Support broadcasts
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);
// Named unary ops directly overwrite data in `outs` buffer so, there is no need to provide non-empty
// destination at the tensor-level.
// Use `tensor.empty` to avoid temporary buffer allocation and memcpy after bufferization.
llvm::SmallVector<Value> dynamicSizes;
for (auto [idx, dim] : llvm::enumerate(outType.getShape())) {
if (!mlir::ShapedType::isDynamic(dim))
continue;
auto dimSize = builder.create<tensor::DimOp>(loc, input, idx);
dynamicSizes.push_back(dimSize);
}
auto empty = builder.create<tensor::EmptyOp>(loc, outType, dynamicSizes);
auto zero = getConstant(builder, ov_output_element_type, 0);
auto fill = builder.create<linalg::FillOp>(loc, mlir::ValueRange{zero}, mlir::ValueRange{empty});
auto relu =
builder.create<linalg::MaxOp>(loc, mlir::ValueRange{input, fill.getResult(0)}, mlir::ValueRange{empty});
context.addOutputs(node, relu);
}
};

bool elementwise_f32_unary_no_broadcast_predicate(const ov::Output<ov::Node>& output) {
if (output.get_element_type() != ov::element::f32) {
return false;
}
// Check if implicit broadcast is possible, reject in this case
// Relies on symbolic information -- register SymbolicPropagation before applying this pattern
auto input_shape = output.get_node_shared_ptr()->get_input_partial_shape(0);
auto output_shape = output.get_partial_shape();
if (output_shape.rank().is_dynamic() || input_shape.rank().is_dynamic()) {
return false;
}
if (output_shape.rank().get_length() != input_shape.rank().get_length()) {
return false;
}

for (size_t i = 0; i < output_shape.size(); ++i) {
if (output_shape[i] != input_shape[i]) {
return false;
}
// Continue if all shapes are static.
if (output_shape[i].is_static() && input_shape[i].is_static()) {
continue;
}
if (!ov::symbol::are_equal(output_shape[i].get_symbol(), input_shape[i].get_symbol())) {
return false;
}
}

return true;
}

template <typename Op>
NodePtr elementwise_f32_unary_no_broadcast() {
using namespace ov::pass::pattern;
return wrap_type<Op>({any_input()}, elementwise_f32_unary_no_broadcast_predicate);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adam-smnk, may I ask you to move the whole thing into a separate file under the op subdirectory as it was implemented for MatMul? If it is not very clear, you can refuse, no problem at all -- I'll do it by myself later and also will reorganizer the part that starts to be a boilerplate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, I'll move it there.


bool elementwise_f32_binary_no_broadcast_predicate(const ov::Output<ov::Node>& output) {
if(output.get_element_type() != ov::element::f32) {
Expand All @@ -291,9 +355,9 @@ bool elementwise_f32_binary_no_broadcast_predicate(const ov::Output<ov::Node>& o
return false;
}
// Continue if all shapes are static.
if (output_shape[i].is_static() && input_shape_a[i].is_static() &&
input_shape_b[i].is_static())
if (output_shape[i].is_static() && input_shape_a[i].is_static() && input_shape_b[i].is_static()) {
continue;
}
if(!ov::symbol::are_equal(output_shape[i].get_symbol(), input_shape_a[i].get_symbol()) || !ov::symbol::are_equal(output_shape[i].get_symbol(), input_shape_b[i].get_symbol())) {
return false;
}
Expand All @@ -319,6 +383,7 @@ void injectMLIR(std::shared_ptr<ov::Model> model, MLIRContext* context) {
manager.register_pass<MarkPattern>(elementwise_f32_binary_no_broadcast<v1::Subtract>(), ConvertBinary<linalg::SubOp>());
manager.register_pass<MarkPattern>(elementwise_f32_binary_no_broadcast<v1::Multiply>(), ConvertBinary<linalg::MulOp>());
manager.register_pass<MarkPattern>(elementwise_f32_binary_no_broadcast<v1::Divide>(), ConvertBinary<linalg::DivOp>());
manager.register_pass<MarkPattern>(elementwise_f32_unary_no_broadcast<v0::Relu>(), ConvertRelu());
manager.register_pass<MatMulPattern>();
manager.register_pass<Partitioner>(context);
manager.run_passes(model);
Expand Down
14 changes: 13 additions & 1 deletion src/common/transformations/src/transformations/mlir/mlir_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,24 @@ void prepareMLIRKernelWithoutWrapper(mlir::OwningOpRef<mlir::ModuleOp>& module)
pm.addPass(bufferization::createEmptyTensorEliminationPass());

pm.addPass(bufferization::createOneShotBufferizePass());
// TODO: Add deallocation pass/pipeline to avoid memory leaks.
pm.addNestedPass<func::FuncOp>(bufferization::createFinalizingBufferizePass());

// Cleanup after bufferization - possibly remove redundant copies.
pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
pm.addNestedPass<func::FuncOp>(createCSEPass());

// Deallocation pipeline to avoid memory leaks from created temporary buffers.
pm.addPass(memref::createExpandReallocPass(/*emitDeallocs=*/false));
pm.addPass(createCanonicalizerPass());
bufferization::DeallocationOptions deallocOpts;
deallocOpts.privateFuncDynamicOwnership = false;
pm.addPass(bufferization::createOwnershipBasedBufferDeallocationPass(deallocOpts));
pm.addPass(createCanonicalizerPass());
pm.addPass(bufferization::createBufferDeallocationSimplificationPass());
pm.addPass(bufferization::createLowerDeallocationsPass());
pm.addPass(createCSEPass());
pm.addPass(createCanonicalizerPass());

// Blanket-convert any remaining high-level vector ops to loops if any remain.
pm.addNestedPass<func::FuncOp>(createConvertVectorToSCFPass());
// pm.addNestedPass<func::FuncOp>(createLinalgGeneralizeNamedOpsPass());
Expand Down
Loading