-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Refactor dygraph to eager -- TensorWrapper, EagerUtils, GlobalUtils #37466
Merged
JiabinYang
merged 21 commits into
PaddlePaddle:develop
from
JiabinYang:refactor_dygraph_to_eager3
Nov 24, 2021
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7a027e1
Add EagerTensor and tests
JiabinYang 5e6a608
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
JiabinYang e9c1a3d
remove useless enforce
JiabinYang 6e36035
remove comment in cmake
JiabinYang b27a7a8
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
JiabinYang 311032d
support autograd meta
JiabinYang dcf2d5c
support grad node info test
JiabinYang 1fd5d7c
support grad_node_info
JiabinYang ab91f83
add more edge test
JiabinYang 18e3dfb
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
JiabinYang fb31c8b
remove Python.h
JiabinYang 74df9ae
Merge branch 'develop' into refactor_dygraph_to_eager2
JiabinYang 2bed84c
add tensor wrapper with tests
JiabinYang 60097bc
support compute require grad and stop gradient
JiabinYang 15abf3c
support sync methods and global utils
JiabinYang d62d590
merge develop code
JiabinYang 07e3077
support pure cpu test
JiabinYang 01dbf8d
refine error msg
JiabinYang 51590da
refine error msg
JiabinYang c729e98
refine error info
JiabinYang b073222
fix npu error
JiabinYang 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
add_subdirectory(tests) | ||
add_subdirectory(api) | ||
cc_library(grad_node_info SRCS grad_node_info.cc DEPS pten pten_api) | ||
cc_library(autograd_meta SRCS autograd_meta.cc DEPS pten pten_api) | ||
cc_library(utils SRCS utils.cc DEPS pten pten_api autograd_meta eager_api) |
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,3 @@ | ||
add_subdirectory(utils) | ||
|
||
cc_library(eager_api SRCS all.cc DEPS global_utils) |
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,18 @@ | ||
// 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/eager/api/all.h" | ||
|
||
namespace egr {} // namespace egr |
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,17 @@ | ||
// 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/eager/api/utils/global_utils.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 @@ | ||
cc_library(global_utils SRCS global_utils.cc DEPS enforce) |
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,22 @@ | ||
// 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/eager/api/utils/global_utils.h" | ||
|
||
namespace egr { | ||
|
||
Controller* Controller::controller_ = new Controller(); | ||
|
||
} // namespace egr |
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,62 @@ | ||
// 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/eager/eager_tensor.h" | ||
#include "paddle/fluid/platform/enforce.h" | ||
|
||
namespace egr { | ||
|
||
class UniqueNameGenerator { | ||
public: | ||
explicit UniqueNameGenerator(std::string prefix = "") : prefix_(prefix) {} | ||
std::string Generate(std::string key = "eager_tmp") { | ||
return prefix_ + key + "_" + std::to_string(id_++); | ||
} | ||
|
||
private: | ||
std::atomic<int> id_{0}; | ||
std::string prefix_; | ||
}; | ||
|
||
// Global | ||
class Controller { | ||
public: | ||
static Controller& Instance() { return *controller_; } | ||
const paddle::platform::Place& GetExpectedPlace() const { | ||
return *expected_place_.get(); | ||
} | ||
void SetExpectedPlace(const paddle::platform::Place& place) { | ||
expected_place_ = std::make_shared<paddle::platform::Place>(place); | ||
} | ||
void SetAMPLevel(int level) { amp_level_ = level; } | ||
int GetAMPLevel() const { return amp_level_; } | ||
bool HasGrad() const { return has_grad_; } | ||
std::string GenerateUniqueName(std::string key = "eager_tmp") { | ||
return generator_->Generate(key); | ||
} | ||
|
||
private: | ||
Controller() = default; | ||
static Controller* controller_; | ||
std::shared_ptr<paddle::platform::Place> expected_place_ = nullptr; | ||
int amp_level_ = 0; | ||
bool has_grad_ = true; | ||
std::unique_ptr<UniqueNameGenerator> generator_{new UniqueNameGenerator()}; | ||
DISABLE_COPY_AND_ASSIGN(Controller); | ||
}; | ||
|
||
} // namespace egr |
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,91 @@ | ||
// 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. | ||
|
||
/** | ||
* We now still need TensorWrapper and it is designed to Copy | ||
* tensor in autograd mode. | ||
* | ||
* Since in autograd usage, we need to pass autograd_meta to | ||
* backward computation however in tensor interface add to much | ||
* autograd_related method is not a good choice. | ||
* | ||
* In TensorWrapper we will keep autograd info to backward, only | ||
* for input var, but for output var it will only copy autograd | ||
* with no grad **/ | ||
|
||
#pragma once | ||
#include "paddle/fluid/eager/autograd_meta.h" | ||
#include "paddle/fluid/eager/grad_node_info.h" | ||
#include "paddle/fluid/eager/utils.h" | ||
|
||
namespace egr { | ||
class TensorWrapper { | ||
public: | ||
TensorWrapper() = default; | ||
explicit TensorWrapper(const egr::EagerTensor& tensor, | ||
bool full_reserved = false) { | ||
/** | ||
* Normally, we should fully reserved all non-output or non-leaf fwd tensor | ||
* here. And for fwd output tensor, we should not reserve its autogradmeta, | ||
* to avoid recursive depends on GradNodeBase | ||
* **/ | ||
full_reserved_ = full_reserved; | ||
if (full_reserved_) { | ||
VLOG(6) << "Fully reserved tensor: " << tensor.name(); | ||
intermidiate_tensor_ = tensor; | ||
return; | ||
} | ||
|
||
// shallow copy tensor_impl here | ||
intermidiate_tensor_.set_impl(tensor.impl()); | ||
intermidiate_tensor_.ResetVar(tensor.Var()); | ||
intermidiate_tensor_.set_name(tensor.name() + "@Saved"); | ||
PADDLE_ENFORCE_NOT_NULL( | ||
EagerUtils::unsafe_autograd_meta(tensor), | ||
paddle::platform::errors::Fatal( | ||
"Full reserved Tensor should not have null autograd meta, since " | ||
"tensor_wrapper is used to build backward info. There is no way " | ||
"for us to build it with null autograd_meta.")); | ||
// copy output_rank | ||
out_rank_info_ = EagerUtils::OutRankInfo(tensor); | ||
} | ||
|
||
egr::EagerTensor recover(const std::shared_ptr<GradNodeBase>& grad_node) { | ||
VLOG(6) << "Recover tensor for wrapper"; | ||
if ((!intermidiate_tensor_.defined()) && | ||
(!intermidiate_tensor_.Var().IsInitialized())) { | ||
VLOG(6) << "Return NULL tensor Here. "; | ||
return egr::EagerTensor(); | ||
} | ||
|
||
// if it's full_reserved just return the full copy of tensor | ||
if (full_reserved_) { | ||
return intermidiate_tensor_; | ||
} else { | ||
std::shared_ptr<GradNodeBase> new_grad_node = grad_node; | ||
auto p_ab_autograd_meta = | ||
std::make_shared<AutogradMeta>(Edge(new_grad_node, out_rank_info_)); | ||
intermidiate_tensor_.set_autograd_meta( | ||
std::static_pointer_cast<paddle::experimental::AbstractAutogradMeta>( | ||
p_ab_autograd_meta)); | ||
return intermidiate_tensor_; | ||
} | ||
} | ||
|
||
private: | ||
bool full_reserved_ = false; | ||
std::pair<size_t, size_t> out_rank_info_; | ||
egr::EagerTensor intermidiate_tensor_; | ||
}; | ||
} // namespace egr |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
set(eager_deps pten pten_api) | ||
add_subdirectory(data_structure_tests) | ||
add_subdirectory(task_tests) |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
cc_test(test_egr_ds_eager_tensor SRCS eager_tensor_test.cc DEPS ${eager_deps} ) | ||
cc_test(test_egr_ds_auotgrad_meta SRCS autograd_meta_test.cc DEPS ${eager_deps} grad_node_info) | ||
cc_test(test_egr_ds_grad_node_info SRCS grad_node_info_test.cc DEPS ${eager_deps} grad_node_info) | ||
cc_test(test_egr_ds_tensor_wrapper SRCS tensor_wrapper_test.cc DEPS ${eager_deps} grad_node_info utils) |
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
80 changes: 80 additions & 0 deletions
80
paddle/fluid/eager/tests/data_structure_tests/tensor_wrapper_test.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,80 @@ | ||
// 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 "glog/logging.h" | ||
#include "gtest/gtest.h" | ||
|
||
#include "paddle/fluid/eager/tensor_wrapper.h" | ||
#include "paddle/fluid/eager/tests/data_structure_tests/grad_node_test.h" | ||
#include "paddle/fluid/eager/utils.h" | ||
|
||
TEST(TensorWrapper, Basic) { | ||
VLOG(6) << "Test Full reserved"; | ||
egr::EagerTensor et1; | ||
pten::DenseTensorMeta meta = pten::DenseTensorMeta( | ||
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2})); | ||
std::shared_ptr<pten::DenseTensor> dt = std::make_shared<pten::DenseTensor>( | ||
std::make_shared<paddle::experimental::DefaultAllocator>( | ||
paddle::platform::CPUPlace()), | ||
meta); | ||
auto* dt_ptr = dt->mutable_data<float>(); | ||
dt_ptr[0] = 5.0f; | ||
dt_ptr[1] = 10.0f; | ||
et1.set_impl(dt); | ||
// Create grad node; | ||
auto grad_test_node0 = std::make_shared<eager_test::GradTestNode>( | ||
/* val */ 5.0, /* in_num */ 2, /* out_num */ 2); | ||
egr::Edge edge0(grad_test_node0, 1, 2); | ||
auto auto_grad0 = std::make_shared<egr::AutogradMeta>(edge0); | ||
et1.set_autograd_meta(auto_grad0); | ||
et1.set_name("et1"); | ||
auto tw0 = egr::TensorWrapper(et1, true); | ||
auto recover_et1 = tw0.recover(std::make_shared<eager_test::GradTestNode>()); | ||
CHECK_EQ(recover_et1.name(), std::string("et1")); | ||
CHECK_EQ(egr::EagerUtils::OutRankInfo(recover_et1).first, | ||
egr::EagerUtils::OutRankInfo(et1).first); | ||
CHECK_EQ(egr::EagerUtils::OutRankInfo(recover_et1).second, | ||
egr::EagerUtils::OutRankInfo(et1).second); | ||
VLOG(6) << "Test reconstruct"; | ||
egr::EagerTensor et2; | ||
pten::DenseTensorMeta meta2 = pten::DenseTensorMeta( | ||
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2})); | ||
std::shared_ptr<pten::DenseTensor> dt2 = std::make_shared<pten::DenseTensor>( | ||
std::make_shared<paddle::experimental::DefaultAllocator>( | ||
paddle::platform::CPUPlace()), | ||
meta2); | ||
auto* dt_ptr2 = dt->mutable_data<float>(); | ||
dt_ptr2[0] = 6.0f; | ||
dt_ptr2[1] = 11.0f; | ||
et2.set_impl(dt2); | ||
et2.set_name("et2"); | ||
auto grad_test_node1 = | ||
std::make_shared<eager_test::GradTestNode>(/* val */ 5.0, 2, 2); | ||
egr::Edge edge1(grad_test_node1, 1, 2); | ||
auto auto_grad1 = std::make_shared<egr::AutogradMeta>(edge1); | ||
et2.set_autograd_meta(auto_grad1); | ||
auto tw1 = egr::TensorWrapper(et2, false); | ||
auto recover_et2 = tw1.recover(grad_test_node1); | ||
CHECK_EQ(recover_et2.name(), std::string("et2@Saved")); | ||
CHECK_EQ(egr::EagerUtils::OutRankInfo(recover_et2).first, | ||
egr::EagerUtils::OutRankInfo(et2).first); | ||
CHECK_EQ(egr::EagerUtils::OutRankInfo(recover_et2).second, | ||
egr::EagerUtils::OutRankInfo(et2).second); | ||
// Test Raw recover | ||
egr::EagerTensor et3; | ||
auto tw2 = egr::TensorWrapper(et3, true); | ||
CHECK( | ||
tw2.recover(std::make_shared<eager_test::GradTestNode>()).initialized() == | ||
false); | ||
} |
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 @@ | ||
cc_test(test_egr_task_eager_utils SRCS eager_utils_test.cc DEPS ${eager_deps} grad_node_info autograd_meta utils) |
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.
不重要,这几个文件好像都多了一行空的注释,建议后续移除
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.
好的 感谢