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 pass to eliminate assign_value op and move assgin op to converter #7207

Merged
merged 5 commits into from
Oct 20, 2021
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 @@ -90,6 +90,7 @@ USE_MIR_PASS(fix_mismatched_precision_pass);
USE_MIR_PASS(lite_flatten_fc_fuse_pass);
USE_MIR_PASS(lite_fc_prelu_fuse_pass);
USE_MIR_PASS(lite_greater_than_cast_fuse_pass);
USE_MIR_PASS(assign_value_calc_offline_pass);
USE_MIR_PASS(__xpu__graph_dedup_pass);
USE_MIR_PASS(__xpu__resnet_fuse_pass);
USE_MIR_PASS(__xpu__resnet_cbam_fuse_pass);
Expand Down
1 change: 1 addition & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ REGISTER_OPERATION(ABS, PrepareUnaryActivations)
REGISTER_OPERATION(ADAPTIVE_AVERAGE_POOL_2D, PrepareAdaptivePool2D)
REGISTER_OPERATION(ADAPTIVE_MAX_POOL_2D, PrepareAdaptivePool2D)
REGISTER_OPERATION(ADD, PrepareElementwise)
REGISTER_OPERATION(ASSIGN, PrepareAssign)
REGISTER_OPERATION(ARG_MAX, PrepareArgMinMax)
REGISTER_OPERATION(ARG_MIN, PrepareArgMinMax)
REGISTER_OPERATION(AVERAGE_POOL_2D, PreparePool2D)
Expand Down
35 changes: 35 additions & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/assign.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 "core/operation/assign.h"
#include "core/hal/types.h"
#include "utility/debug.h"
#include "utility/logging.h"
#include "utility/modeling.h"
#include "utility/utility.h"

namespace nnadapter {
namespace operation {

int PrepareAssign(hal::Operation* operation) {
ASSIGN_OPERATION_EXTRACT_INPUTS_OUTPUTS

// Infer the shape and type of output operands
CopyOperandTypeExceptQuantParams(&output_operand->type, input_operand->type);
NNADAPTER_VLOG(5) << "output: " << OperandToString(output_operand);
return NNADAPTER_NO_ERROR;
}

} // namespace operation
} // namespace nnadapter
2 changes: 1 addition & 1 deletion lite/backends/x86/math/conv_depthwise_3x3.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void conv_depthwise_3x3s2_p01_direct(
right = true;
}
int ri = (w_in - (1 - pad)) % 6;
//[pad == 0 && w_out == 3 && win == 8] ===>>> [ri == 1 && ro == 0]
// [pad == 0 && w_out == 3 && win == 8] ===>>> [ri == 1 && ro == 0]
// add condition ro > 0 for avoiding wrong rmaskr when pad == 0
if (ri > 0 && (ro > 0 || pad == 1)) {
for (int i = 0; i < 8; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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/assign_value_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 {

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

void AssignValueCalcOfflinePass::RemoveAssignValuePattern(
const std::unique_ptr<SSAGraph>& graph) {
for (auto& node : graph->StmtTopologicalOrder()) {
if (node->AsStmt().picked_kernel().op_type() != "assign_value") continue;

std::set<const Node*> nodes2rm_;
auto& assign_value_instruct = node->AsStmt();
auto* scope = assign_value_instruct.op()->scope();
auto op_desc = assign_value_instruct.mutable_op_info();

// Get assign_value's attr
CHECK(op_desc->HasAttr("shape"));
auto shape = op_desc->GetAttr<std::vector<int>>("shape");
CHECK(op_desc->HasAttr("dtype"));
auto dtype = op_desc->GetAttr<int>("dtype");

// Get assign_value's output tensor
auto out_var = scope->FindVar(op_desc->Output("Out").front());
auto out_t = out_var->GetMutable<lite::Tensor>();
std::vector<int64_t> shape_int64_t;
for (auto value : shape) {
shape_int64_t.push_back(static_cast<int64_t>(value));
}
out_t->Resize(DDim(shape_int64_t));
auto out_data = out_t->mutable_data<float>();

if (dtype == static_cast<int>(lite::core::FluidType::INT32)) {
auto int32_values = op_desc->GetAttr<std::vector<int>>("int32_values");
memcpy(out_data, int32_values.data(), sizeof(int) * int32_values.size());
} else if (dtype == static_cast<int>(lite::core::FluidType::FP32)) {
auto fp32_values = op_desc->GetAttr<std::vector<float>>("fp32_values");
memcpy(out_data, fp32_values.data(), sizeof(float) * fp32_values.size());
} else if (dtype == static_cast<int>(lite::core::FluidType::INT64)) {
auto int64_values =
op_desc->GetAttr<std::vector<int64_t>>("int64_values");
memcpy(
out_data, int64_values.data(), sizeof(int64_t) * int64_values.size());
} else if (dtype == static_cast<int>(lite::core::FluidType::BOOL)) {
auto bool_values = op_desc->GetAttr<std::vector<int>>("bool_values");
memcpy(out_data, bool_values.data(), sizeof(bool) * bool_values.size());
} else {
LOG(FATAL) << "Unsupported dtype for assign_value op: " << dtype;
}

// Offline calc assign_value, only retain output tensor as persistable
// tensor
out_t->set_persistable(true);
auto assign_value_outlinks = node->outlinks;
for (auto& assign_value_out_link : assign_value_outlinks) {
assign_value_out_link->arg()->is_weight = true;
}
nodes2rm_.insert(node);
GraphSafeRemoveNodes(graph.get(), nodes2rm_);
}
}

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

REGISTER_MIR_PASS(assign_value_calc_offline_pass,
paddle::lite::mir::AssignValueCalcOfflinePass)
.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 AssignValueCalcOfflinePass : public mir::StmtPass {
public:
void Apply(const std::unique_ptr<SSAGraph>& graph) override;
void RemoveAssignValuePattern(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 @@ -197,6 +197,7 @@ std::unique_ptr<RuntimeProgram> RunDefaultOptimizer(
"__xpu__dynamic_lstm_fuse_pass",
"__xpu__multi_softmax_fuse_pass",
"ssd_boxes_calc_offline_pass",
"assign_value_calc_offline_pass",
// Only for fully quantized model, infer the output scale and fix the
// attribute 'enable_int8' for all of the quantized ops.
"quantized_op_attributes_inference_pass",
Expand Down
4 changes: 0 additions & 4 deletions lite/kernels/nnadapter/bridges/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ lite_cc_library(subgraph_bridge_scale_op_nnadapter SRCS scale_op.cc DEPS ${nnada
lite_cc_library(subgraph_bridge_transpose_op_nnadapter SRCS transpose_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_cast_op_nnadapter SRCS cast_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_norm_op_nnadapter SRCS norm_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_assign_value_op_nnadapter SRCS assign_value_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_assign_op_nnadapter SRCS assign_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_pow_op_nnadapter SRCS pow_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_batch_normalization_op_nnadapter SRCS batch_normalization_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_clip_op_nnadapter SRCS clip_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
Expand All @@ -33,8 +31,6 @@ set(nnadapter_subgraph_bridges
subgraph_bridge_transpose_op_nnadapter
subgraph_bridge_cast_op_nnadapter
subgraph_bridge_norm_op_nnadapter
subgraph_bridge_assign_op_nnadapter
subgraph_bridge_assign_value_op_nnadapter
subgraph_bridge_pow_op_nnadapter
subgraph_bridge_batch_normalization_op_nnadapter
subgraph_bridge_clip_op_nnadapter
Expand Down
86 changes: 0 additions & 86 deletions lite/kernels/nnadapter/bridges/assign_op.cc

This file was deleted.

Loading