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

add purify graph pass #271

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions src/nnfusion/engine/device/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "nnfusion/engine/pass/graph/multi_reshape_folding_pass.hpp"
#include "nnfusion/engine/pass/graph/op_inplace_pass.hpp"
#include "nnfusion/engine/pass/graph/pattern_substitution.hpp"
#include "nnfusion/engine/pass/graph/purify_graph_pass.hpp"
#include "nnfusion/engine/pass/graph/runtime_const_folding_pass.hpp"
#include "nnfusion/engine/pass/graph/vector_dot_transpose_pass.hpp"

Expand All @@ -46,6 +47,7 @@ CpuEngine::CpuEngine()
g_passes->push_back(make_shared<AutodiffPass>());
g_passes->push_back(make_shared<GradientWeightMappingPass>());
g_passes->push_back(make_shared<RuntimeConstantFoldingPass>());
g_passes->push_back(make_shared<PurifyGraphPass>());
g_passes->push_back(make_shared<MultiReshapeFoldingPass>());
g_passes->push_back(make_shared<VectorDotTransposePass>());
g_passes->push_back(make_shared<GemmFusionPass>());
Expand Down
2 changes: 2 additions & 0 deletions src/nnfusion/engine/device/cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "nnfusion/engine/pass/graph/multi_reshape_folding_pass.hpp"
#include "nnfusion/engine/pass/graph/op_inplace_pass.hpp"
#include "nnfusion/engine/pass/graph/pattern_substitution.hpp"
#include "nnfusion/engine/pass/graph/purify_graph_pass.hpp"
#include "nnfusion/engine/pass/graph/reduce_fusion_pass.hpp"
#include "nnfusion/engine/pass/graph/runtime_const_folding_pass.hpp"
#include "nnfusion/engine/pass/graph/superscaler_dataparallelism_pass.hpp"
Expand All @@ -49,6 +50,7 @@ CudaEngine::CudaEngine()
g_passes->push_back(make_shared<AutodiffPass>());
g_passes->push_back(make_shared<GradientWeightMappingPass>());
g_passes->push_back(make_shared<RuntimeConstantFoldingPass>());
g_passes->push_back(make_shared<PurifyGraphPass>());
g_passes->push_back(make_shared<MultiReshapeFoldingPass>());
g_passes->push_back(make_shared<VectorDotTransposePass>());
g_passes->push_back(make_shared<GemmFusionPass>());
Expand Down
4 changes: 4 additions & 0 deletions src/nnfusion/engine/device/hlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "nnfusion/engine/pass/graph/multi_reshape_folding_pass.hpp"
#include "nnfusion/engine/pass/graph/op_inplace_pass.hpp"
#include "nnfusion/engine/pass/graph/pattern_substitution.hpp"
#include "nnfusion/engine/pass/graph/purify_graph_pass.hpp"
#include "nnfusion/engine/pass/graph/reduce_fusion_pass.hpp"
#include "nnfusion/engine/pass/graph/runtime_const_folding_pass.hpp"
#include "nnfusion/engine/pass/graph/vector_dot_transpose_pass.hpp"
Expand Down Expand Up @@ -52,6 +53,7 @@ HLSLEngine::HLSLEngine()
g_passes->push_back(make_shared<AutodiffPass>());
g_passes->push_back(make_shared<GradientWeightMappingPass>());
g_passes->push_back(make_shared<RuntimeConstantFoldingPass>());
g_passes->push_back(make_shared<PurifyGraphPass>());
g_passes->push_back(make_shared<MultiReshapeFoldingPass>());
g_passes->push_back(make_shared<VectorDotTransposePass>());
g_passes->push_back(make_shared<GemmFusionPass>());
Expand Down Expand Up @@ -91,6 +93,7 @@ HLSLEngine::HLSLEngine()
g_passes->push_back(make_shared<AutodiffPass>());
g_passes->push_back(make_shared<GradientWeightMappingPass>());
g_passes->push_back(make_shared<RuntimeConstantFoldingPass>());
g_passes->push_back(make_shared<PurifyGraphPass>());
g_passes->push_back(make_shared<MultiReshapeFoldingPass>());
g_passes->push_back(make_shared<VectorDotTransposePass>());
g_passes->push_back(make_shared<GemmFusionPass>());
Expand Down Expand Up @@ -128,6 +131,7 @@ HLSLEngine::HLSLEngine()
{
g_passes->push_back(make_shared<GradientWeightMappingPass>());
g_passes->push_back(make_shared<RuntimeConstantFoldingPass>());
g_passes->push_back(make_shared<PurifyGraphPass>());
g_passes->push_back(make_shared<ReduceFusionPass>());

// Kernel selection
Expand Down
2 changes: 2 additions & 0 deletions src/nnfusion/engine/device/rocm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "nnfusion/engine/pass/graph/multi_reshape_folding_pass.hpp"
#include "nnfusion/engine/pass/graph/op_inplace_pass.hpp"
#include "nnfusion/engine/pass/graph/pattern_substitution.hpp"
#include "nnfusion/engine/pass/graph/purify_graph_pass.hpp"
#include "nnfusion/engine/pass/graph/reduce_fusion_pass.hpp"
#include "nnfusion/engine/pass/graph/runtime_const_folding_pass.hpp"
#include "nnfusion/engine/pass/graph/vector_dot_transpose_pass.hpp"
Expand All @@ -47,6 +48,7 @@ ROCmEngine::ROCmEngine()
g_passes->push_back(make_shared<AutodiffPass>());
g_passes->push_back(make_shared<GradientWeightMappingPass>());
g_passes->push_back(make_shared<RuntimeConstantFoldingPass>());
g_passes->push_back(make_shared<PurifyGraphPass>());
g_passes->push_back(make_shared<MultiReshapeFoldingPass>());
g_passes->push_back(make_shared<VectorDotTransposePass>());
g_passes->push_back(make_shared<GemmFusionPass>());
Expand Down
1 change: 1 addition & 0 deletions src/nnfusion/engine/pass/graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(SRC
dot_transpose_pass.cpp
reduce_fusion_pass.cpp
superscaler_dataparallelism_pass.cpp
purify_graph_pass.cpp
)

add_library(nnfusion_engine_pass_graph STATIC ${SRC})
Expand Down
35 changes: 35 additions & 0 deletions src/nnfusion/engine/pass/graph/purify_graph_pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <set>
#include <string>

#include "nnfusion/core/operators/generic_op/generic_op.hpp"
#include "nnfusion/core/operators/op_define/fused.hpp"
#include "nnfusion/core/operators/op_define/noop.hpp"
#include "purify_graph_pass.hpp"

using namespace nnfusion::graph;
using namespace nnfusion::pass::graph;
using namespace nnfusion::kernels;

bool PurifyGraphPass::run_on_graph(std::shared_ptr<Graph>& graph)
{
NNFUSION_LOG(INFO) << "Purify Graph Pass started:";
std::unordered_set<std::shared_ptr<GNode>> valided_nodes;
for (auto node : graph->get_ordered_ops())
{
valided_nodes.insert(node);
}
auto all_nodes = graph->get_nodes();
NNFUSION_LOG(INFO) << "Before: " + to_string(all_nodes.size());
for (auto node : all_nodes)
{
if (valided_nodes.find(node) != valided_nodes.end())
continue;
graph->remove_node(node);
}
NNFUSION_LOG(INFO) << "After: " + to_string(graph->get_nodes().size());
NNFUSION_LOG(INFO) << "Purify Graph Pass finished";
return true;
}
25 changes: 25 additions & 0 deletions src/nnfusion/engine/pass/graph/purify_graph_pass.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include "graph_pass_base.hpp"
#include "nnfusion/common/common.hpp"
#include "nnfusion/engine/cache/manager.hpp"
#include "nnfusion/engine/op.hpp"
#include "nnfusion/engine/profiler/profiler.hpp"

namespace nnfusion
{
namespace pass
{
namespace graph
{
class PurifyGraphPass : public GraphPassBase
{
public:
bool run_on_graph(std::shared_ptr<nnfusion::graph::Graph>& graph) override;
};
} // namespace graph
} // namespace pass
} // namespace nnfusion