Skip to content

Commit

Permalink
[IR&PASS] add conv2d + bn fuse pattern, pass support ir verify
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanlehome committed Jun 29, 2023
1 parent 12a296c commit 182fd35
Show file tree
Hide file tree
Showing 20 changed files with 340 additions and 40 deletions.
2 changes: 1 addition & 1 deletion paddle/fluid/framework/ir/conv_bn_fuse_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ void ConvBNFusePass::ApplyImpl(ir::Graph* graph) const {
return;
}

// conv_weight fp32 --> fp16
// conv_weight fp16 --> fp32
auto* conv_weight_tensor =
scope->FindVar(conv_weight->Name())->GetMutable<phi::DenseTensor>();
auto tensor_type = conv_weight_tensor->dtype();
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/new_executor/standalone_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "paddle/fluid/framework/new_executor/interpreter/interpreter_util.h"
#include "paddle/fluid/platform/profiler/event_tracing.h"

#include "paddle/fluid/ir/pass/pd_op_to_kernel_pass.h"
#include "paddle/fluid/ir/transforms/pd_op_to_kernel_pass.h"

#include "paddle/fluid/ir_adaptor/translator/translate.h"

Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/ir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_subdirectory(interface)
add_subdirectory(dialect)
add_subdirectory(pass)
add_subdirectory(transforms)
add_subdirectory(phi_kernel_adaptor)
7 changes: 0 additions & 7 deletions paddle/fluid/ir/pass/CMakeLists.txt

This file was deleted.

9 changes: 9 additions & 0 deletions paddle/fluid/ir/transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cc_library(
transform_general_functions
SRCS transform_general_functions.cc
DEPS ir phi pd_dialect)

cc_library(
pd_op_to_kernel_pass
SRCS pd_op_to_kernel_pass.cc
DEPS ir phi_utils pd_interface)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include <iostream>

#include "paddle/fluid/ir/pass/pd_op_to_kernel_pass.h"
#include "paddle/fluid/ir/transforms/pd_op_to_kernel_pass.h"

#include "paddle/fluid/ir/dialect/kernel_attribute.h"
#include "paddle/fluid/ir/dialect/kernel_dialect.h"
Expand Down
File renamed without changes.
46 changes: 46 additions & 0 deletions paddle/fluid/ir/transforms/transform_general_functions.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/ir/transforms/transform_general_functions.h"

#include "paddle/fluid/ir/dialect/pd_dialect.h"
#include "paddle/fluid/ir/dialect/pd_type.h"
#include "paddle/ir/core/builtin_op.h"
#include "paddle/ir/core/program.h"

namespace ir {

ir::Parameter* GetParameterFromValue(ir::Value value) {
ir::GetParameterOp op = value.GetDefiningOp()->dyn_cast<ir::GetParameterOp>();
PADDLE_ENFORCE_NOT_NULL(
op,
phi::errors::InvalidArgument(
"Value must be a weight from a GetParameter op."));
ir::Program* program = op->GetParentProgram();
std::string name = op->attributes()
.at(op.attributes_name[0])
.dyn_cast<ir::StrAttribute>()
.data();
return program->GetParameter(name);
}

const phi::DDim& GetShapeFromValue(ir::Value value) {
PADDLE_ENFORCE_EQ(
value.type().isa<paddle::dialect::DenseTensorType>(),
true,
phi::errors::InvalidArgument("Value's type must be a DenseTensorType."));
return value.type().dyn_cast<paddle::dialect::DenseTensorType>().dims();
}

} // namespace ir
84 changes: 84 additions & 0 deletions paddle/fluid/ir/transforms/transform_general_functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "paddle/ir/core/operation.h"
#include "paddle/ir/core/parameter.h"
#include "paddle/ir/core/value.h"
#include "paddle/phi/core/ddim.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/errors.h"

namespace ir {

/**
* @brief
*
* @note
*
* @param ir::Value
*
* @return ir::Parameter*
*/
ir::Parameter* GetParameterFromValue(ir::Value value);

/**
* @brief
*
* @note
*
* @param ir::Value
*
* @return const phi::DDim&
*/
const phi::DDim& GetShapeFromValue(ir::Value value);

/**
* @brief
*
* @note
*
* @param Operation*
*
* @return Operation*
*/
template <uint32_t Index = 0>
Operation* GetPreviousOperation(Operation* op) {
PADDLE_ENFORCE_EQ(
Index < op->num_operands(),
true,
phi::errors::InvalidArgument("Intput operand's index must be valid."));
return op->operand(Index).GetDefiningOp();
}

/**
* @brief
*
* @note
*
* @param Operation*
*
* @return Operation*
*/
template <uint32_t Index = 0>
Operation* GetNextFirstUseOperation(Operation* op) {
PADDLE_ENFORCE_EQ(
Index < op->num_results(),
true,
phi::errors::InvalidArgument("Output op result's index must be valid."));
return op->result(Index).first_use().owner();
}

} // namespace ir
8 changes: 5 additions & 3 deletions paddle/ir/core/operation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Operation *Operation::Create(const std::vector<ir::OpResult> &inputs,
// Call destructors for Region , OpResults, Operation, and OpOperands in
// sequence, and finally free memory.
void Operation::Destroy() {
VLOG(6) << "Destroy Operation [" << name() << "] ...";
// 1. Deconstruct Regions.
if (num_regions_ > 0) {
for (size_t idx = 0; idx < num_regions_; idx++) {
Expand All @@ -117,7 +118,8 @@ void Operation::Destroy() {
// 2. Deconstruct Result.
for (size_t idx = 0; idx < num_results_; ++idx) {
detail::OpResultImpl *impl = result(idx).impl();
IR_ENFORCE(impl->use_empty(), "operation destroyed but still has uses.");
IR_ENFORCE(impl->use_empty(),
name() + " operation destroyed but still has uses.");
if (detail::OpOutlineResultImpl::classof(*impl)) {
static_cast<detail::OpOutlineResultImpl *>(impl)->~OpOutlineResultImpl();
} else {
Expand All @@ -143,8 +145,8 @@ void Operation::Destroy() {
: sizeof(detail::OpInlineResultImpl) * num_results_;
void *aligned_ptr = reinterpret_cast<char *>(this) - result_mem_size;

VLOG(4) << "Destroy an Operation: {ptr = " << aligned_ptr
<< ", size = " << result_mem_size << "}";
VLOG(6) << "Destroy Operation [" << name() << "]: {ptr = " << aligned_ptr
<< ", size = " << result_mem_size << "} done.";
aligned_free(aligned_ptr);
}

Expand Down
4 changes: 2 additions & 2 deletions paddle/ir/core/program.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ Program::~Program() {
}
}

Parameter* Program::GetParameter(std::string name) const {
Parameter* Program::GetParameter(const std::string& name) const {
if (parameters_.count(name) != 0) {
return parameters_.at(name).get();
}
return nullptr;
}

void Program::SetParameter(std::string name,
void Program::SetParameter(const std::string& name,
std::unique_ptr<Parameter>&& parameter) {
parameters_[name].reset(parameter.release());
}
Expand Down
5 changes: 3 additions & 2 deletions paddle/ir/core/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class IR_API Program {

Block* block() { return module_.block(); }

Parameter* GetParameter(std::string name) const;
void SetParameter(std::string name, std::unique_ptr<Parameter>&& parameter);
Parameter* GetParameter(const std::string& name) const;
void SetParameter(const std::string& name,
std::unique_ptr<Parameter>&& parameter);

ParameterMap& parameters() { return parameters_; }
void set_parameters(ParameterMap&& parameters) {
Expand Down
6 changes: 3 additions & 3 deletions paddle/ir/pass/pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "paddle/ir/core/operation.h"
#include "paddle/ir/core/program.h"
#include "paddle/ir/core/region.h"
#include "paddle/ir/core/verify.h"
#include "paddle/ir/pass/pass_adaptor.h"
#include "paddle/ir/pass/pass_instrumentation.h"
#include "paddle/ir/pass/pass_manager.h"
Expand Down Expand Up @@ -109,10 +110,9 @@ bool detail::PassAdaptor::RunPass(Pass* pass,

bool pass_failed = pass->pass_state().pass_failed;

// TODO(liuyuanle): Support verification of operation
if (!pass_failed && verify) {
// bool verify_recursively = !dynamic_cast<PassAdaptor*>(pass);
// pass_failed = ir::Verify(op, verify_recursively);
bool verify_recursively = !dynamic_cast<PassAdaptor*>(pass);
ir::Verify(op, verify_recursively);
}

return !pass_failed;
Expand Down
2 changes: 2 additions & 0 deletions paddle/ir/transforms/dce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class DCEPass : public ir::Pass {
for (uint32_t i = 0; i < (*it)->num_results(); ++i) {
use_empty &= (*it)->result(i).use_empty();
}

// TODO(wilber): Support end trait.
if (use_empty && (*it)->name() != "pd.fetch") {
erased_op.push_back(**it);
}
Expand Down
2 changes: 1 addition & 1 deletion test/cpp/ir/core/ir_exe_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

#include "paddle/fluid/ir/dialect/pd_attribute.h"

#include "paddle/fluid/ir/pass/pd_op_to_kernel_pass.h"
#include "paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_adaptor.h"
#include "paddle/fluid/ir/transforms/pd_op_to_kernel_pass.h"
#include "paddle/phi/core/kernel_registry.h"

PD_DECLARE_KERNEL(full, CPU, ALL_LAYOUT);
Expand Down
2 changes: 1 addition & 1 deletion test/cpp/ir/kernel_dialect/ir_kernel_dialect_pass_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#include "paddle/fluid/ir/dialect/pd_type.h"
#include "paddle/fluid/ir/dialect/utils.h"
#include "paddle/fluid/ir/interface/op_yaml_info.h"
#include "paddle/fluid/ir/pass/pd_op_to_kernel_pass.h"
#include "paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_adaptor.h"
#include "paddle/fluid/ir/transforms/pd_op_to_kernel_pass.h"
#include "paddle/fluid/platform/init.h"
#include "paddle/ir/core/builtin_attribute.h"
#include "paddle/ir/core/builtin_dialect.h"
Expand Down
3 changes: 2 additions & 1 deletion test/cpp/ir/pass/pass_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ IR_DEFINE_EXPLICIT_TYPE_ID(AddOp)

struct CountOpAnalysis {
explicit CountOpAnalysis(ir::Operation *container_op) {
IR_ENFORCE(container_op->num_regions() > 0, true);
IR_ENFORCE(container_op->num_regions() > 0,
"op must be a container with zero or multiple regions.");

LOG(INFO) << "In CountOpAnalysis, op is " << container_op->name() << "\n";
for (size_t i = 0; i < container_op->num_regions(); ++i) {
Expand Down
1 change: 1 addition & 0 deletions test/cpp/ir/pattern_rewrite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ cc_test_old(
DEPS
ir
pd_dialect
transform_general_functions
gtest)
Loading

0 comments on commit 182fd35

Please sign in to comment.