-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
sparse_fc supported #41770
sparse_fc supported #41770
Changes from 10 commits
f42a773
f667231
0b1519d
78d585e
1ac29fd
b72bcbd
2a3fff1
093e4a4
17338e9
1808537
256c6ca
4c44db5
ec004da
7f8c63f
c4e1db8
35d16f1
a16489e
2e85d1b
9a42620
3f1ddb2
1e6d8e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# 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. | ||
|
||
if (NOT (WITH_CUSPARSELT AND WITH_TENSORRT)) | ||
return() | ||
endif () | ||
|
||
if (WITH_ARM OR WIN32) | ||
message(SEND_ERROR "The current sparselt support linux only") | ||
return() | ||
endif () | ||
|
||
INCLUDE(ExternalProject) | ||
|
||
SET(CUSPARSELT_PROJECT "extern_cusparselt") | ||
SET(CUSPARSELT_URL "https://developer.download.nvidia.com/compute/libcusparse-lt/0.2.0/local_installers/libcusparse_lt-linux-x86_64-0.2.0.1.tar.gz" CACHE STRING "" FORCE) | ||
SET(CUSPARSELT_PREFIX_DIR ${THIRD_PARTY_PATH}/cusparselt) | ||
SET(CUSPARSELT_INSTALL_DIR ${THIRD_PARTY_PATH}/install/cusparselt) | ||
SET(CUSPARSELT_INC_DIR "${CUSPARSELT_INSTALL_DIR}/include" CACHE PATH "sparselt include directory." FORCE) | ||
SET(CUSPARSELT_LIB_DIR "${CUSPARSELT_INSTALL_DIR}/lib64" CACHE PATH "sparselt lib directory." FORCE) | ||
set_directory_properties(PROPERTIES CLEAN_NO_CUSTOM 1) | ||
include_directories(${CUSPARSELT_INC_DIR}) | ||
|
||
ExternalProject_Add( | ||
${CUSPARSELT_PROJECT} | ||
${EXTERNAL_PROJECT_LOG_ARGS} | ||
URL ${CUSPARSELT_URL} | ||
PREFIX ${CUSPARSELT_PREFIX_DIR} | ||
DOWNLOAD_NO_PROGRESS 1 | ||
CONFIGURE_COMMAND "" | ||
BUILD_COMMAND "" | ||
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory ${CUSPARSELT_PREFIX_DIR}/src/extern_cusparselt/lib64 ${CUSPARSELT_LIB_DIR} && | ||
${CMAKE_COMMAND} -E copy_directory ${CUSPARSELT_PREFIX_DIR}/src/extern_cusparselt/include ${CUSPARSELT_INC_DIR} | ||
UPDATE_COMMAND "" | ||
) | ||
|
||
add_library(cusparselt INTERFACE) | ||
add_dependencies(cusparselt ${CUSPARSELT_PROJECT}) | ||
set(CUSPARSELT_FOUND ON) | ||
add_definitions(-DPADDLE_WITH_CUSPARSELT) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// 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_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 { | ||
|
||
ReplaceDenseWithSparsePass::ReplaceDenseWithSparsePass() { | ||
AddOpCompat(OpCompat("fc")) | ||
.AddInput("Input") | ||
.IsTensor() | ||
.End() | ||
.AddInput("W") | ||
.IsTensor() | ||
.End() | ||
.AddInput("Bias") | ||
.IsTensor() | ||
.End() | ||
.AddOutput("Out") | ||
.IsTensor() | ||
.End(); | ||
} | ||
|
||
void ReplaceDenseWithSparsePass::ApplyImpl(Graph *graph) const { | ||
PADDLE_ENFORCE_NOT_NULL( | ||
graph, platform::errors::InvalidArgument("Graph cannot be nullptr.")); | ||
|
||
std::string name_scope = "replace_dense_with_sparse_pass"; | ||
b3602sss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FusePassBase::Init(name_scope, graph); | ||
GraphPatternDetector gpd; | ||
|
||
patterns::DenseFC dense_fc_pattern(gpd.mutable_pattern(), | ||
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. 在op_convert中,会将某些mul op转为fc op, 这种情况是不是也要转为稀疏fc? 例如: 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. 这种情况应该是分布式要解决的,应该不是我们层面考虑的。 |
||
"dense_replace_pass"); | ||
dense_fc_pattern(); | ||
int found_dense_fc_count = 0; | ||
auto handler = [&](const GraphPatternDetector::subgraph_t &subgraph, | ||
Graph *g) { | ||
VLOG(4) << "Replace dense fc with sparse_fc."; | ||
|
||
/* if (!IsCompat(subgraph, g)) { | ||
LOG(WARNING) << "Pass in op compat failed."; | ||
return; | ||
}*/ | ||
|
||
GET_IR_NODE_FROM_SUBGRAPH(fc_out, fc_out, dense_fc_pattern); | ||
GET_IR_NODE_FROM_SUBGRAPH(fc, fc, dense_fc_pattern); | ||
GET_IR_NODE_FROM_SUBGRAPH(fc_input, fc_input, dense_fc_pattern); | ||
GET_IR_NODE_FROM_SUBGRAPH(fc_weights, fc_weights, dense_fc_pattern); | ||
GET_IR_NODE_FROM_SUBGRAPH(fc_bias, fc_bias, dense_fc_pattern); | ||
|
||
auto *fc_op = fc->Op(); | ||
auto w_name = fc_op->Input("W")[0]; | ||
// recognize sparse op by name | ||
if (w_name.find("sparse_2_4") != w_name.npos) { | ||
// fake op | ||
OpDesc desc(fc_op->Block()); | ||
desc.SetType("sparse_fc"); | ||
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. 这个命名需要和其它稀疏方式区分么?比如后续可能会添加的block sparsity 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. 这个可以后续定好的后改,用户没有感知。 |
||
desc.SetInput("Input", {fc_input->Name()}); | ||
desc.SetInput("W", {fc_weights->Name()}); | ||
desc.SetInput("Bias", {fc_bias->Name()}); | ||
desc.SetOutput("Out", {fc_out->Name()}); | ||
|
||
// copy all attr | ||
if (fc_op->HasAttr("x_num_col_dims")) { | ||
desc.SetAttr("x_num_col_dims", fc_op->GetAttr("x_num_col_dims")); | ||
} | ||
if (fc_op->HasAttr("in_num_col_dims")) { | ||
desc.SetAttr("in_num_col_dims", fc_op->GetAttr("in_num_col_dims")); | ||
} | ||
desc.SetAttr("activation_type", fc_op->GetAttr("activation_type")); | ||
if (fc_op->HasAttr("enable_int8")) { | ||
desc.SetAttr("enable_int8", fc_op->GetAttr("enable_int8")); | ||
} | ||
if (fc_op->HasAttr("Input_scale")) { | ||
desc.SetAttr("Input_scale", fc_op->GetAttr("Input_scale")); | ||
} | ||
if (fc_op->HasAttr("support_int8")) { | ||
desc.SetAttr("support_int8", fc_op->GetAttr("support_int8")); | ||
} | ||
if (fc_op->HasAttr("out_threshold")) { | ||
desc.SetAttr("out_threshold", fc_op->GetAttr("out_threshold")); | ||
} | ||
desc.Flush(); | ||
GraphSafeRemoveNodes(g, {fc}); | ||
auto sparse_fc_node = g->CreateOpNode(&desc); | ||
|
||
IR_NODE_LINK_TO(fc_input, sparse_fc_node); | ||
IR_NODE_LINK_TO(fc_weights, sparse_fc_node); | ||
IR_NODE_LINK_TO(fc_bias, sparse_fc_node); | ||
IR_NODE_LINK_TO(sparse_fc_node, fc_out); | ||
found_dense_fc_count++; | ||
} | ||
}; | ||
|
||
gpd(graph, handler); | ||
AddStatis(found_dense_fc_count); | ||
} | ||
|
||
} // namespace ir | ||
} // namespace framework | ||
} // namespace paddle | ||
|
||
REGISTER_PASS(replace_dense_with_sparse_pass, | ||
paddle::framework::ir::ReplaceDenseWithSparsePass); |
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 op with sparse op | ||
*/ | ||
class Graph; | ||
|
||
class ReplaceDenseWithSparsePass : public FusePassBase { | ||
public: | ||
ReplaceDenseWithSparsePass(); | ||
|
||
protected: | ||
void ApplyImpl(ir::Graph* graph) const override; | ||
|
||
const std::string name_scope_{"replace_dense_with_sparse_pass"}; | ||
}; | ||
|
||
} // namespace ir | ||
} // namespace framework | ||
} // namespace paddle |
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.
这个新增的Pattern和当前文件中已定义的patterns::FC有什么区别么?为什么不直接用patterns::FC?
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.
patterns::FC是找mul+element的 不一样