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

Multihead matmul sparse #5

Closed
wants to merge 19 commits into from
Closed
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
3 changes: 2 additions & 1 deletion paddle/fluid/framework/ir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ pass_library(add_support_int8_pass inference)
pass_library(matmul_scale_fuse_pass inference)
pass_library(gpu_cpu_map_matmul_to_mul_pass inference)
pass_library(mixed_precision_configure_pass inference)
pass_library(desne_to_sparse_pass inference)
pass_library(desne_fc_to_sparse_pass inference)
pass_library(dense_multihead_matmul_to_sparse_pass inference)
pass_library(generate_pass DEPS pass_desc_proto)
target_link_libraries(generate_pass pass_desc_proto)

Expand Down
134 changes: 134 additions & 0 deletions paddle/fluid/framework/ir/dense_multihead_matmul_to_sparse_pass.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) 2022 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/framework/ir/dense_multihead_matmul_to_sparse_pass.h"
#include "paddle/fluid/framework/ir/graph_helper.h"
#include "paddle/fluid/framework/op_version_registry.h"

namespace paddle {
namespace framework {
namespace ir {

DenseMultiheadMatmulToSparsePass::DenseMultiheadMatmulToSparsePass() {
AddOpCompat(OpCompat("multihead_matmul"))
.AddInput("Input")
.IsTensor()
.End()
.AddInput("W")
.IsTensor()
.End()
.AddInput("Bias")
.IsTensor()
.End()
.AddInput("BiasQK")
.IsTensor()
.End()
.AddOutput("Out")
.IsTensor()
.End();
}

void DenseMultiheadMatmulToSparsePass::ApplyImpl(Graph *graph) const {
PADDLE_ENFORCE_NOT_NULL(
graph, platform::errors::InvalidArgument("Graph cannot be nullptr."));

std::string name_scope = "dense_multihead_matmul_to_sparse_pass";
FusePassBase::Init(name_scope, graph);
GraphPatternDetector gpd;

patterns::MultiheadMatmul multihead_matmul_pattern(
gpd.mutable_pattern(), "dense_multihead_matmul_replace_pass");
multihead_matmul_pattern();
int found_multihead_matmul_count = 0;
auto handler = [&](const GraphPatternDetector::subgraph_t &subgraph,
Graph *g) {
VLOG(4) << "Replace dense multihead matmul with sparse multihead matmul.";

/* if (!IsCompat(subgraph, g)) {
LOG(WARNING) << "Pass in op compat failed.";
return;
}*/

GET_IR_NODE_FROM_SUBGRAPH(multihead_matmul_out, multihead_matmul_out,
multihead_matmul_pattern);
GET_IR_NODE_FROM_SUBGRAPH(multihead_matmul, multihead_matmul,
multihead_matmul_pattern);
GET_IR_NODE_FROM_SUBGRAPH(multihead_matmul_input, multihead_matmul_input,
multihead_matmul_pattern);
GET_IR_NODE_FROM_SUBGRAPH(multihead_matmul_weights,
multihead_matmul_weights,
multihead_matmul_pattern);
GET_IR_NODE_FROM_SUBGRAPH(multihead_matmul_bias, multihead_matmul_bias,
multihead_matmul_pattern);
GET_IR_NODE_FROM_SUBGRAPH(multihead_matmul_biasqk, multihead_matmul_biasqk,
multihead_matmul_pattern);

auto *multihead_matmul_op = multihead_matmul->Op();
auto w_name = multihead_matmul_op->Input("W")[0];
// recognize sparse op by name
if (w_name.find("sparse_2_4") != w_name.npos) {
// fake op
OpDesc desc(multihead_matmul_op->Block());
desc.SetType("sparse_multihead_matmul");
desc.SetInput("Input", {multihead_matmul_input->Name()});
desc.SetInput("W", {multihead_matmul_weights->Name()});
desc.SetInput("Bias", {multihead_matmul_bias->Name()});
desc.SetInput("BiasQK", {multihead_matmul_biasqk->Name()});
desc.SetOutput("Out", {multihead_matmul_out->Name()});

// copy all attr
desc.SetAttr("alpha", multihead_matmul_op->GetAttr("alpha"));
desc.SetAttr("head_number", multihead_matmul_op->GetAttr("head_number"));
if (multihead_matmul_op->HasAttr("Input_scale")) {
desc.SetAttr("Input_scale",
multihead_matmul_op->GetAttr("Input_scale"));
}
if (multihead_matmul_op->HasAttr("fc_out_threshold")) {
desc.SetAttr("fc_out_threshold",
multihead_matmul_op->GetAttr("fc_out_threshold"));
}
if (multihead_matmul_op->HasAttr("qkv2context_plugin_int8")) {
desc.SetAttr("qkv2context_plugin_int8",
multihead_matmul_op->GetAttr("qkv2context_plugin_int8"));
}
if (multihead_matmul_op->HasAttr("dp_probs")) {
desc.SetAttr("dp_probs", multihead_matmul_op->GetAttr("dp_probs"));
}
if (multihead_matmul_op->HasAttr("out_threshold")) {
desc.SetAttr("out_threshold",
multihead_matmul_op->GetAttr("out_threshold"));
}
desc.Flush();
GraphSafeRemoveNodes(g, {multihead_matmul});
auto sparse_multihead_matmul_node = g->CreateOpNode(&desc);

IR_NODE_LINK_TO(multihead_matmul_input, sparse_multihead_matmul_node);
IR_NODE_LINK_TO(multihead_matmul_weights, sparse_multihead_matmul_node);
IR_NODE_LINK_TO(multihead_matmul_bias, sparse_multihead_matmul_node);
IR_NODE_LINK_TO(multihead_matmul_biasqk, sparse_multihead_matmul_node);
IR_NODE_LINK_TO(sparse_multihead_matmul_node, multihead_matmul_out);
found_multihead_matmul_count++;
}
};

gpd(graph, handler);
AddStatis(found_multihead_matmul_count);
}

} // namespace ir
} // namespace framework
} // namespace paddle

REGISTER_PASS(dense_multihead_matmul_to_sparse_pass,
paddle::framework::ir::DenseMultiheadMatmulToSparsePass);
45 changes: 45 additions & 0 deletions paddle/fluid/framework/ir/dense_multihead_matmul_to_sparse_pass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* Copyright (c) 2022 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 <string>

#include "paddle/fluid/framework/ir/fuse_pass_base.h"
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/ir/graph_pattern_detector.h"
#include "paddle/fluid/inference/api/paddle_analysis_config.h"

namespace paddle {
namespace framework {
namespace ir {

/**
* Replace dense multihead_matmul op with sparse multihead_matmul op
*/
class Graph;

class DenseMultiheadMatmulToSparsePass : public FusePassBase {
public:
DenseMultiheadMatmulToSparsePass();

protected:
void ApplyImpl(ir::Graph* graph) const override;

const std::string name_scope_{"dense_multihead_matmul_to_sparse_pass"};
};

} // namespace ir
} // namespace framework
} // namespace paddle
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/framework/ir/desne_to_sparse_pass.h"
#include "paddle/fluid/framework/ir/desne_fc_to_sparse_pass.h"
#include "paddle/fluid/framework/ir/graph_helper.h"
#include "paddle/fluid/framework/op_version_registry.h"

namespace paddle {
namespace framework {
namespace ir {

ReplaceDenseWithSparsePass::ReplaceDenseWithSparsePass() {
DenseFCToSparsePass::DenseFCToSparsePass() {
AddOpCompat(OpCompat("fc"))
.AddInput("Input")
.IsTensor()
Expand All @@ -36,16 +36,16 @@ ReplaceDenseWithSparsePass::ReplaceDenseWithSparsePass() {
.End();
}

void ReplaceDenseWithSparsePass::ApplyImpl(Graph *graph) const {
void DenseFCToSparsePass::ApplyImpl(Graph *graph) const {
PADDLE_ENFORCE_NOT_NULL(
graph, platform::errors::InvalidArgument("Graph cannot be nullptr."));

std::string name_scope = "desne_to_sparse_pass";
std::string name_scope = "desne_fc_to_sparse_pass";
FusePassBase::Init(name_scope, graph);
GraphPatternDetector gpd;

patterns::DenseFC dense_fc_pattern(gpd.mutable_pattern(),
"dense_replace_pass");
"dense_fc_replace_pass");
dense_fc_pattern();
int found_dense_fc_count = 0;
auto handler = [&](const GraphPatternDetector::subgraph_t &subgraph,
Expand Down Expand Up @@ -115,5 +115,5 @@ void ReplaceDenseWithSparsePass::ApplyImpl(Graph *graph) const {
} // namespace framework
} // namespace paddle

REGISTER_PASS(desne_to_sparse_pass,
paddle::framework::ir::ReplaceDenseWithSparsePass);
REGISTER_PASS(desne_fc_to_sparse_pass,
paddle::framework::ir::DenseFCToSparsePass);
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ namespace ir {
*/
class Graph;

class ReplaceDenseWithSparsePass : public FusePassBase {
class DenseFCToSparsePass : public FusePassBase {
public:
ReplaceDenseWithSparsePass();
DenseFCToSparsePass();

protected:
void ApplyImpl(ir::Graph* graph) const override;

const std::string name_scope_{"desne_to_sparse_pass"};
const std::string name_scope_{"desne_fc_to_sparse_pass"};
};

} // namespace ir
Expand Down
38 changes: 38 additions & 0 deletions paddle/fluid/framework/ir/graph_pattern_detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3423,6 +3423,44 @@ PDNode *patterns::DenseFC::operator()() {
return fc_out;
}

PDNode *patterns::MultiheadMatmul::operator()() {
auto *multihead_matmul = pattern->NewNode(multihead_matmul_repr())
->assert_is_op("multihead_matmul");
// Input
auto *multihead_matmul_input =
pattern->NewNode(multihead_matmul_input_repr())
->AsInput()
->assert_is_op_input("multihead_matmul", "Input");
// Filter
auto *multihead_matmul_weights =
pattern->NewNode(multihead_matmul_weights_repr())
->AsInput()
->assert_is_op_input("multihead_matmul", "W");
// Bias
auto *multihead_matmul_bias =
pattern->NewNode(multihead_matmul_bias_repr())
->AsInput()
->assert_is_op_input("multihead_matmul", "Bias");
// BiasQK
auto *multihead_matmul_biasqk =
pattern->NewNode(multihead_matmul_biasqk_repr())
->AsInput()
->assert_is_op_input("multihead_matmul", "BiasQK");
// Output
auto *multihead_matmul_out =
pattern->NewNode(multihead_matmul_out_repr())
->AsOutput()
->assert_is_op_output("multihead_matmul", "Out")
->assert_is_only_output_of_op("multihead_matmul");

multihead_matmul
->LinksFrom({multihead_matmul_input, multihead_matmul_weights,
multihead_matmul_bias, multihead_matmul_biasqk})
.LinksTo({multihead_matmul_out});

return multihead_matmul_out;
}

} // namespace ir
} // namespace framework
} // namespace paddle
18 changes: 18 additions & 0 deletions paddle/fluid/framework/ir/graph_pattern_detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,24 @@ struct DenseFC : public PatternBase {
PATTERN_DECL_NODE(fc_bias);
};

//
// \brief Pattern looking for multihead matmul fc.
//
struct MultiheadMatmul : public PatternBase {
MultiheadMatmul(PDPattern* pattern, const std::string& name_scope)
: PatternBase(pattern, name_scope, "multihead_matmul") {}

PDNode* operator()();

// declare operator node's name
PATTERN_DECL_NODE(multihead_matmul);
PATTERN_DECL_NODE(multihead_matmul_out);
PATTERN_DECL_NODE(multihead_matmul_input);
PATTERN_DECL_NODE(multihead_matmul_weights);
PATTERN_DECL_NODE(multihead_matmul_bias);
PATTERN_DECL_NODE(multihead_matmul_biasqk);
};

} // namespace patterns

// Link two ir::Nodes from each other.
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/inference/api/analysis_predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,7 @@ USE_TRT_CONVERTER(recover_padding)
USE_TRT_CONVERTER(remove_padding)
#if PADDLE_WITH_CUSPARSELT && IS_TRT_VERSION_GE(8000)
USE_TRT_CONVERTER(sparse_fc)
USE_TRT_CONVERTER(sparse_multihead_matmul)
#endif
#endif

Expand Down
7 changes: 4 additions & 3 deletions paddle/fluid/inference/api/paddle_pass_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ const std::vector<std::string> kTRTSubgraphPasses({
"remove_padding_recover_padding_pass", //
"delete_remove_padding_recover_padding_pass", //
// "yolo_box_fuse_pass", //
"desne_to_sparse_pass",
"tensorrt_subgraph_pass", //
"conv_bn_fuse_pass", //
"desne_fc_to_sparse_pass", //
"dense_multihead_matmul_to_sparse_pass" //
"tensorrt_subgraph_pass", //
"conv_bn_fuse_pass", //
#if CUDNN_VERSION >= 7100 // To run conv_fusion, the version of cudnn must be
// guaranteed at least v7
// cudnn8.0 has memory leak problem in conv + eltwise + act, so we
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/inference/tensorrt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ else()
endif()
nv_library(tensorrt_op_teller SRCS op_teller.cc DEPS framework_proto device_context boost)
nv_test(test_tensorrt SRCS test_tensorrt.cc DEPS dynload_cuda device_context dynamic_loader)
nv_test(test_tensorrt_engine SRCS test_engine.cc DEPS dynload_cuda tensorrt_engine)
nv_test(test_tensorrt_engine SRCS test_engine.cc test_dynamic_engine.cc DEPS dynload_cuda tensorrt_engine tensorrt_plugin)
add_subdirectory(plugin)
add_subdirectory(convert)
2 changes: 1 addition & 1 deletion paddle/fluid/inference/tensorrt/convert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ preln_skip_layernorm.cc strided_slice_op.cc roll_op.cc transformer_input_convert
recover_padding_op.cc)

if (CUSPARSELT_FOUND AND ${TENSORRT_MAJOR_VERSION} GREATER_EQUAL 8)
list(APPEND CONVERT_FILES sparse_fc_op.cc)
list(APPEND CONVERT_FILES sparse_fc_op.cc sparse_multihead_matmul_op.cc)
endif()

nv_library(tensorrt_converter
Expand Down
18 changes: 15 additions & 3 deletions paddle/fluid/inference/tensorrt/convert/sparse_fc_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,27 @@ class SparseFcOpConverter : public OpConverter {

float* bias_data = nullptr;
int bias_num = 0;
void* b_data = nullptr;
if (with_bias) {
auto* b_v = scope.GetVar(op_desc.Input("Bias").front());
auto* b_t = b_v->GetMutable<framework::LoDTensor>();
bias_data = engine_->GetWeightCPUData(op_desc.Input("Bias").front(), b_t);
bias_num = b_t->numel();

half* half_bias_data = nullptr;
if (with_fp16) {
half_bias_data = new half[bias_num];
for (int i = 0; i < bias_num; i++) {
half_bias_data[i] = static_cast<half>(bias_data[i]);
}
b_data = static_cast<void*>(half_bias_data);
} else {
b_data = static_cast<void*>(bias_data);
}
}
TensorRTEngine::Weight bias{nvinfer1::DataType::kFLOAT,
static_cast<void*>(bias_data),
static_cast<size_t>(bias_num)};
TensorRTEngine::Weight bias{
with_fp16 ? nvinfer1::DataType::kHALF : nvinfer1::DataType::kFLOAT,
b_data, static_cast<size_t>(bias_num)};

// Running the TRT Static Shape mode: x_num_col_dims-1
if (!engine_->with_dynamic_shape()) {
Expand Down
Loading