forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Closed
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
dc46040
fix precision-is-non error
minghaoBD 9ad5f64
develop finished
minghaoBD c8ac573
strange error
minghaoBD e574ad9
compilation failed
minghaoBD 5231918
style check
minghaoBD 0af0393
fix error
minghaoBD 3340d6b
shared_ptr compilation passed
minghaoBD 9001a73
shared_ptr is nullptr in enqueue
minghaoBD 291bc0b
shared_ptr is nullptr in enqueue
minghaoBD 865b673
UT passed
minghaoBD e9ffce2
nan fp16 output
minghaoBD 275ff0d
spmm
minghaoBD ba6877e
style check
minghaoBD 41463b5
style check
minghaoBD 6166c48
add UT for spmmDynamic
minghaoBD d4698e7
modify according to review
minghaoBD a86d66b
update comment
minghaoBD 88c4e52
update pass name
minghaoBD 100d200
merge https://github.com/PaddlePaddle/Paddle/pull/41770
minghaoBD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
paddle/fluid/framework/ir/replace_dense_multihead_matmul_with_sparse_pass.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// 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/replace_dense_multihead_matmul_with_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 { | ||
|
||
ReplaceDenseMultiheadMatmulWithSparsePass:: | ||
ReplaceDenseMultiheadMatmulWithSparsePass() { | ||
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 ReplaceDenseMultiheadMatmulWithSparsePass::ApplyImpl(Graph *graph) const { | ||
PADDLE_ENFORCE_NOT_NULL( | ||
graph, platform::errors::InvalidArgument("Graph cannot be nullptr.")); | ||
|
||
std::string name_scope = "replace_dense_multihead_matmul_with_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(replace_dense_multihead_matmul_with_sparse_pass, | ||
paddle::framework::ir::ReplaceDenseMultiheadMatmulWithSparsePass); |
46 changes: 46 additions & 0 deletions
46
paddle/fluid/framework/ir/replace_dense_multihead_matmul_with_sparse_pass.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* 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 ReplaceDenseMultiheadMatmulWithSparsePass : public FusePassBase { | ||
public: | ||
ReplaceDenseMultiheadMatmulWithSparsePass(); | ||
|
||
protected: | ||
void ApplyImpl(ir::Graph* graph) const override; | ||
|
||
const std::string name_scope_{ | ||
"replace_dense_multihead_matmul_with_sparse_pass"}; | ||
}; | ||
|
||
} // namespace ir | ||
} // namespace framework | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,6 @@ nv_library(tensorrt_engine SRCS engine.cc trt_int8_calibrator.cc DEPS ${GLOB_OPE | |
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 DEPS dynload_cuda tensorrt_engine tensorrt_plugin) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 少了.cc? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks |
||
add_subdirectory(plugin) | ||
add_subdirectory(convert) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
把replace_dense_with_sparse_pass改成replace_dense_fc_with_sparse_pass或者把replace_dense_multihead_matmul_with_sparse_pass合入到replace_dense_with_sparse_pass。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace_dense_with_sparse_pass -> replace_dense_fc_with_sparse_pass