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

conv_eltwiseadd_bn_fuse support fp16 #45379

Merged
merged 1 commit into from
Aug 24, 2022
Merged
Changes from all commits
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
86 changes: 39 additions & 47 deletions paddle/fluid/framework/ir/conv_bn_fuse_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
#include <string>

#include "paddle/fluid/framework/convert_utils.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/phi/common/data_type.h"

namespace phi {
class DenseTensor;
Expand All @@ -30,6 +34,23 @@ class Scope;
} // namespace framework
} // namespace paddle

namespace {
template <typename T1, typename T2>
void ConvertTensorType(paddle::framework::LoDTensor* tensor) {
paddle::framework::Tensor tmp_tensor;
tmp_tensor.set_type(paddle::experimental::CppTypeToDataType<T2>::Type());
tmp_tensor.Resize(tensor->dims());
auto* tmp_data = tmp_tensor.mutable_data<T2>(paddle::platform::CPUPlace());
auto* data = tensor->mutable_data<T1>(paddle::platform::CPUPlace());
for (int i = 0; i < tensor->numel(); i++) {
tmp_data[i] = static_cast<T2>(data[i]);
}
tensor->clear();
paddle::framework::TensorCopySync(
tmp_tensor, paddle::platform::CPUPlace(), tensor);
}
} // namespace

namespace paddle {
namespace framework {
namespace ir {
Expand Down Expand Up @@ -290,19 +311,7 @@ void ConvBNFusePass::ApplyImpl(ir::Graph* graph) const {
auto tensor_type = conv_weight_tensor->dtype();

if (tensor_type == paddle::experimental::DataType::FLOAT16) {
framework::Tensor weight_float_tensor;
weight_float_tensor.set_type(paddle::experimental::DataType::FLOAT32);
weight_float_tensor.Resize(conv_weight_tensor->dims());
auto* weight_float_data =
weight_float_tensor.mutable_data<float>(platform::CPUPlace());
auto* data =
conv_weight_tensor->mutable_data<float16>(platform::CPUPlace());
for (int i = 0; i < conv_weight_tensor->numel(); i++) {
weight_float_data[i] = static_cast<float>(data[i]);
}
conv_weight_tensor->clear();
paddle::framework::TensorCopySync(
weight_float_tensor, platform::CPUPlace(), conv_weight_tensor);
ConvertTensorType<float16, float>(conv_weight_tensor);
}

// Get batch norm bias
Expand Down Expand Up @@ -341,40 +350,8 @@ void ConvBNFusePass::ApplyImpl(ir::Graph* graph) const {
conv_type());

if (tensor_type == paddle::experimental::DataType::FLOAT16) {
{
framework::Tensor weight_float16_tensor;
weight_float16_tensor.set_type(paddle::experimental::DataType::FLOAT16);
weight_float16_tensor.Resize(conv_weight_tensor->dims());
auto* weight_float16_data =
weight_float16_tensor.mutable_data<float16>(platform::CPUPlace());
auto* data =
conv_weight_tensor->mutable_data<float>(platform::CPUPlace());
for (int i = 0; i < conv_weight_tensor->numel(); i++) {
weight_float16_data[i] = static_cast<float16>(data[i]);
}
conv_weight_tensor->clear();
paddle::framework::TensorCopySync(
weight_float16_tensor, platform::CPUPlace(), conv_weight_tensor);
}

{
framework::Tensor eltwise_y_in_float16_tensor;
eltwise_y_in_float16_tensor.set_type(
paddle::experimental::DataType::FLOAT16);
eltwise_y_in_float16_tensor.Resize(eltwise_y_in_tensor->dims());
auto* eltwise_y_in_float16_data =
eltwise_y_in_float16_tensor.mutable_data<float16>(
platform::CPUPlace());
auto* data =
eltwise_y_in_tensor->mutable_data<float>(platform::CPUPlace());
for (int i = 0; i < eltwise_y_in_tensor->numel(); i++) {
eltwise_y_in_float16_data[i] = static_cast<float16>(data[i]);
}
eltwise_y_in_tensor->clear();
paddle::framework::TensorCopySync(eltwise_y_in_float16_tensor,
platform::CPUPlace(),
eltwise_y_in_tensor);
}
ConvertTensorType<float, float16>(conv_weight_tensor);
ConvertTensorType<float, float16>(eltwise_y_in_tensor);
}

// with MKL-DNN fuse conv+bn into conv with bias
Expand Down Expand Up @@ -612,6 +589,16 @@ void ConvEltwiseAddBNFusePass::ApplyImpl(ir::Graph* graph) const {
float epsilon =
PADDLE_GET_CONST(float, batch_norm->Op()->GetAttr("epsilon"));

// conv_weight fp16 --> fp32
auto* conv_weight_tensor =
scope->FindVar(conv_weight->Name())->GetMutable<LoDTensor>();
auto tensor_type = conv_weight_tensor->dtype();

if (tensor_type == paddle::experimental::DataType::FLOAT16) {
ConvertTensorType<float16, float>(conv_weight_tensor);
ConvertTensorType<float16, float>(eltwise_y_in_tensor);
}

// if bias is an input to other ops as well then we cannot overwrite it
// so we create separate elementwise Y in nodes
if (eltwise_y_in->outputs.size() > 1) {
Expand Down Expand Up @@ -666,6 +653,11 @@ void ConvEltwiseAddBNFusePass::ApplyImpl(ir::Graph* graph) const {
conv_type());
}

if (tensor_type == paddle::experimental::DataType::FLOAT16) {
ConvertTensorType<float, float16>(conv_weight_tensor);
ConvertTensorType<float, float16>(eltwise_y_in_tensor);
}

// Update the elementwise_add node
eltwise->Op()->SetAttr("axis", 1);
eltwise->Op()->SetOutput("Out", std::vector<std::string>({bn_out->Name()}));
Expand Down