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

[NNAdapter] Add fill_constant_calc_offline_pass for NNAdapter #8056

Merged
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions lite/api/paddle_use_passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,4 @@ USE_MIR_PASS(x86_int8_attribute_pass);
USE_MIR_PASS(fill_range_fuse_pass);
USE_MIR_PASS(range_calc_offline_pass);
USE_MIR_PASS(p_norm_fill_constant_max_div_fuse_pass);
USE_MIR_PASS(fill_constant_calc_offline_pass);
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright (c) 2019 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 "lite/core/optimizer/mir/elimination/fill_constant_calc_offline_pass.h"
#include <algorithm>
#include <cmath>
#include <list>
#include <memory>
#include <set>
#include <vector>
#include "lite/core/optimizer/mir/pass.h"
#include "lite/core/optimizer/mir/pass_registry.h"
#include "lite/core/optimizer/mir/pattern_matcher.h"
#include "lite/model_parser/cpp_desc.h"

namespace paddle {
namespace lite {
namespace mir {

template <typename T>
void FillConstData(lite::Tensor* out_t, T value) {
auto output_data = out_t->mutable_data<T>();
for (int i = 0; i < out_t->numel(); i++) {
output_data[i] = value;
}
}

void FillConstantCalcOfflinePass::Apply(
const std::unique_ptr<SSAGraph>& graph) {
RemoveFillConstantPattern(graph);
}

void FillConstantCalcOfflinePass::RemoveFillConstantPattern(
const std::unique_ptr<SSAGraph>& graph) {
for (auto& node : graph->StmtTopologicalOrder()) {
if (node->AsStmt().op_type() != "fill_constant") continue;

std::set<const Node*> nodes2rm_;
auto& fill_constant_instruct = node->AsStmt();
auto* scope = fill_constant_instruct.op()->scope();
auto op_desc = fill_constant_instruct.mutable_op_info();
if ((op_desc->HasInput("ValueTensor") &&
!op_desc->Input("ValueTensor").empty()) ||
(op_desc->HasInput("str_value") &&
!op_desc->GetAttr<std::string>("str_value").empty())) {
LOG(WARNING) << "Unsupported ValueTensor input or str_value input for "
"fill_contant op.";
continue;
} else if (!op_desc->HasAttr("value")) {
LOG(WARNING)
<< "One of ValueTensor, str_value(attr) or value(attr) must be set.";
continue;
}
if ((op_desc->HasInput("ShapeTensor") &&
!op_desc->Input("ShapeTensor").empty()) ||
(op_desc->HasInput("ShapeTensorList") &&
!op_desc->Input("ShapeTensorList").empty())) {
LOG(WARNING) << "Unsupported ShapeTensor or ShapeTensorList input for "
"fill_contant op.";
continue;
} else if (!op_desc->HasAttr("shape")) {
LOG(WARNING)
<< "One of ShapeTensor, ShapeTensorList or shape(attr) must be set.";
continue;
}
// Get fill_constant's attr
auto dtype = op_desc->GetAttr<int>("dtype");
auto value = op_desc->GetAttr<float>("value");
auto shape = op_desc->GetAttr<std::vector<int64_t>>("shape");
// Get fill_constant's output tensor
auto out_var = scope->FindVar(op_desc->Output("Out").front());
auto out_t = out_var->GetMutable<lite::Tensor>();
out_t->Resize(DDim({shape}));
switch (dtype) {
case static_cast<int>(lite::core::FluidType::BOOL):
FillConstData<bool>(out_t, static_cast<bool>(value));
break;
case static_cast<int>(lite::core::FluidType::INT32):
FillConstData<int32_t>(out_t, static_cast<int32_t>(value));
break;
case static_cast<int>(lite::core::FluidType::INT64):
FillConstData<int64_t>(out_t, static_cast<int64_t>(value));
break;
case static_cast<int>(lite::core::FluidType::FP32):
FillConstData<float>(out_t, static_cast<float>(value));
break;
default:
LOG(WARNING) << "Unsupported dtype for fill_constant op: " << dtype;
continue;
}
// Offline calc fill_constant, only retain output tensor as persistable
// tensor
out_t->set_persistable(true);
auto fill_constant_outlinks = node->outlinks;
for (auto& fill_constant_out_link : fill_constant_outlinks) {
fill_constant_out_link->arg()->is_weight = true;
}
nodes2rm_.insert(node);
GraphSafeRemoveNodes(graph.get(), nodes2rm_);
}
}

} // namespace mir
} // namespace lite
} // namespace paddle

REGISTER_MIR_PASS(fill_constant_calc_offline_pass,
paddle::lite::mir::FillConstantCalcOfflinePass)
.BindTargets({TARGET(kNNAdapter)});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2021 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 <limits>
#include <memory>
#include <string>
#include <vector>
#include "lite/core/optimizer/mir/pass.h"
#include "lite/core/optimizer/mir/pass_registry.h"
#include "lite/core/tensor.h"
#include "lite/core/types.h"

namespace paddle {
namespace lite {
namespace mir {

class FillConstantCalcOfflinePass : public mir::StmtPass {
public:
void Apply(const std::unique_ptr<SSAGraph>& graph) override;
void RemoveFillConstantPattern(const std::unique_ptr<SSAGraph>& graph);
};

} // namespace mir
} // namespace lite
} // namespace paddle
1 change: 1 addition & 0 deletions lite/core/optimizer/optimizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ std::unique_ptr<RuntimeProgram> RunDefaultOptimizer(
"lite_greater_than_cast_fuse_pass",
"fill_range_fuse_pass",
"range_calc_offline_pass",
"fill_constant_calc_offline_pass",
"identity_dropout_eliminate_pass",
"p_norm_fill_constant_max_div_fuse_pass",
"sparse_conv_detect_pass",
Expand Down