forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
infer shape pass and avg shard pass (PaddlePaddle#165)
* infer shape pass and avg shard pass * rm useless test
- Loading branch information
Showing
27 changed files
with
297 additions
and
76 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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. | ||
|
||
#include "paddle/fluid/framework/ir/ipu/avg_shard_pass.h" | ||
|
||
#include "paddle/fluid/framework/ipu/ipu_backend.h" | ||
|
||
#include "paddle/fluid/framework/ir/graph_helper.h" | ||
#include "paddle/fluid/framework/ir/pass_tester_helper.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
namespace ir { | ||
|
||
void AvgShardPass::ApplyImpl(ir::Graph* graph) const { | ||
VLOG(10) << "enter AvgShardPass::ApplyImpl"; | ||
|
||
std::shared_ptr<ipu::IpuBackend> ipu_backend = ipu::IpuBackend::GetInstance(); | ||
|
||
if (ipu_backend->GetIpuStrategy()->need_avg_shard) { | ||
VLOG(10) << "start AvgShardPass"; | ||
auto nodes = ir::TopologySortOperations(*graph); | ||
auto num_ipus = ipu_backend->GetIpuStrategy()->num_ipus; | ||
|
||
int shard_position = nodes.size() / num_ipus; | ||
int index_and_stage = -1; | ||
for (int i = 0; i < nodes.size(); i++) { | ||
if ((i % shard_position) == 0 && index_and_stage < num_ipus - 1) { | ||
index_and_stage++; | ||
} | ||
nodes[i]->Op()->SetAttr("ipu_index", index_and_stage); | ||
nodes[i]->Op()->SetAttr("ipu_stage", index_and_stage); | ||
} | ||
VLOG(10) << "end AvgShardPass"; | ||
} | ||
|
||
VLOG(10) << "leave AvgShardPass::ApplyImpl"; | ||
} | ||
|
||
} // namespace ir | ||
} // namespace framework | ||
} // namespace paddle | ||
|
||
REGISTER_PASS(avg_shard_pass, paddle::framework::ir::AvgShardPass); |
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,30 @@ | ||
// 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 "paddle/fluid/framework/ir/ipu/ipu_pass_base.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
namespace ir { | ||
|
||
class AvgShardPass : public IPUPassBase { | ||
protected: | ||
void ApplyImpl(ir::Graph* graph) const override; | ||
}; | ||
|
||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// 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. | ||
|
||
#include "paddle/fluid/framework/ir/ipu/infer_shape_pass.h" | ||
|
||
#include "paddle/fluid/framework/ddim.h" | ||
#include "paddle/fluid/framework/ipu/ipu_backend.h" | ||
#include "paddle/fluid/framework/ir/graph_helper.h" | ||
#include "paddle/fluid/framework/ir/pass_tester_helper.h" | ||
#include "paddle/fluid/framework/op_registry.h" | ||
#include "paddle/fluid/framework/variable_helper.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
namespace ir { | ||
|
||
void InferShapePass::ApplyImpl(ir::Graph* graph) const { | ||
VLOG(10) << "enter InferShapePass::ApplyImpl"; | ||
|
||
// graph_viz_pass | ||
// auto graph_viz_pass = PassRegistry::Instance().Get("graph_viz_pass"); | ||
// graph_viz_pass->Set("graph_viz_path", | ||
// new std::string("before_pass.dot")); | ||
// graph_viz_pass->Apply(graph); | ||
|
||
std::shared_ptr<ipu::IpuBackend> ipu_backend = ipu::IpuBackend::GetInstance(); | ||
|
||
if (ipu_backend->GetIpuStrategy()->need_infer_shape) { | ||
VLOG(10) << "start InferShapePass"; | ||
// temp scope for shape inference | ||
std::shared_ptr<paddle::framework::Scope> scope( | ||
new paddle::framework::Scope()); | ||
for (auto node : graph->Nodes()) { | ||
if (!node->IsVar()) { | ||
continue; | ||
} | ||
auto var_desc = node->Var(); | ||
auto* ptr = scope->Var(var_desc->Name()); | ||
paddle::framework::InitializeVariable(ptr, var_desc->GetType()); | ||
|
||
auto tensor = ptr->GetMutable<paddle::framework::LoDTensor>(); | ||
tensor->Resize(paddle::framework::make_ddim(var_desc->GetShape())); | ||
|
||
// skip lowerWeights in IpuBackend if persistable node has inputs | ||
if (var_desc->Persistable() && node->inputs.size()) { | ||
var_desc->SetPersistable(false); | ||
} | ||
} | ||
|
||
// infer shape | ||
auto nodes = ir::TopologySortOperations(*graph); | ||
for (auto node : nodes) { | ||
auto op_desc = node->Op(); | ||
auto op = paddle::framework::OpRegistry::CreateOp(*op_desc); | ||
paddle::framework::RuntimeContext ctx(op->Inputs(), op->Outputs(), | ||
*scope); | ||
op->RuntimeInferShape(*scope, paddle::platform::CPUPlace(), ctx); | ||
|
||
for (auto it = ctx.outputs.begin(); it != ctx.outputs.end(); it++) { | ||
for (int i = 0; i < it->second.size(); i++) { | ||
auto output_name = op_desc->Output(it->first)[i]; | ||
auto dim = | ||
it->second[i]->GetMutable<paddle::framework::LoDTensor>()->dims(); | ||
auto new_shape = paddle::framework::vectorize(dim); | ||
for (auto output_node : node->outputs) { | ||
if (output_node->Name() == output_name) { | ||
output_node->Var()->SetShape(new_shape); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// release the temp scope | ||
scope.reset(); | ||
VLOG(10) << "end InferShapePass"; | ||
} | ||
|
||
// graph_viz_pass | ||
// graph_viz_pass->Erase("graph_viz_path"); | ||
// graph_viz_pass->Set("graph_viz_path", | ||
// new std::string("after_pass.dot")); | ||
// graph_viz_pass->Apply(graph); | ||
|
||
VLOG(10) << "leave InferShapePass::ApplyImpl"; | ||
} | ||
|
||
} // namespace ir | ||
} // namespace framework | ||
} // namespace paddle | ||
|
||
REGISTER_PASS(infer_shape_pass, paddle::framework::ir::InferShapePass); |
Oops, something went wrong.