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.
Adds ReLU op matcher and lowering to MLIR named Linalg ops. Also, adds buffer deallocation passes to prevent memory leaks when temporary buffers are created in larger graphs.
- Loading branch information
Showing
6 changed files
with
152 additions
and
50 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
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
59 changes: 59 additions & 0 deletions
59
src/common/transformations/src/transformations/mlir/op/relu.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,59 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "mlir/Dialect/Tensor/IR/Tensor.h" | ||
#include "mlir/Dialect/Linalg/Passes.h" | ||
|
||
#include <openvino/op/relu.hpp> | ||
#include "openvino/pass/pattern/op/wrap_type.hpp" | ||
|
||
#include "relu.hpp" | ||
#include "../convert_common.hpp" | ||
|
||
namespace { | ||
|
||
using namespace ov::mlir; | ||
|
||
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); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace ov { | ||
namespace mlir { | ||
|
||
using namespace ov::pass::pattern; | ||
using namespace ov::op; | ||
|
||
ReluPattern::ReluPattern() | ||
: MarkPattern(wrap_type<v0::Relu>({any_input()}, elementwise_no_broadcast_predicate<ov::element::f32>), | ||
ConvertRelu()) {} | ||
|
||
} // namespace mlir | ||
} // namespace ov |
23 changes: 23 additions & 0 deletions
23
src/common/transformations/src/transformations/mlir/op/relu.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 ReluPattern : public MarkPattern { | ||
public: | ||
OPENVINO_RTTI("ReluPattern", "0"); | ||
ReluPattern(); | ||
}; | ||
|
||
} // namespace mlir | ||
} // namespace ov |