Skip to content

Commit 7de99d8

Browse files
authored
Added EagerUtils to Eager Dygraph (#37479)
* Added EagerUtils to Eager Dygraph * Purified include dependencies for global_utils * Fixed merge conflicts
1 parent 486b77f commit 7de99d8

File tree

9 files changed

+205
-30
lines changed

9 files changed

+205
-30
lines changed

paddle/fluid/eager/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
add_subdirectory(tests)
21
add_subdirectory(api)
2+
add_subdirectory(tests)
33
cc_library(grad_node_info SRCS grad_node_info.cc DEPS pten pten_api)
44
cc_library(autograd_meta SRCS autograd_meta.cc DEPS pten pten_api)
5-
cc_library(utils SRCS utils.cc DEPS pten pten_api autograd_meta eager_api)
5+
cc_library(utils SRCS utils.cc DEPS pten pten_api global_utils layer proto_desc operator op_registry variable_helper memcpy scale_op autograd_meta)
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
add_subdirectory(utils)
2-
3-
cc_library(eager_api SRCS all.cc DEPS global_utils)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cc_library(global_utils SRCS global_utils.cc DEPS enforce)
1+
cc_library(global_utils SRCS global_utils.cc DEPS place)

paddle/fluid/eager/api/utils/global_utils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515

1616
#pragma once
1717

18-
#include "paddle/fluid/eager/eager_tensor.h"
19-
#include "paddle/fluid/platform/enforce.h"
18+
#include <atomic>
19+
#include <memory>
20+
#include "paddle/fluid/platform/place.h"
2021

2122
namespace egr {
2223

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
set(eager_deps pten pten_api)
1+
set(eager_deps pten pten_api pten_tensor utils global_utils autograd_meta grad_node_info)
22
add_subdirectory(data_structure_tests)
33
add_subdirectory(task_tests)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cc_test(test_egr_task_eager_utils SRCS eager_utils_test.cc DEPS ${eager_deps} grad_node_info autograd_meta utils)
1+
cc_test(test_egr_task_eager_utils SRCS eager_utils_test.cc DEPS ${eager_deps})

paddle/fluid/eager/tests/task_tests/eager_utils_test.cc

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,96 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Eager Dygraph
15+
#include <sstream>
1616

1717
#include "gtest/gtest.h"
1818

19-
#include "paddle/fluid/eager/autograd_meta.h"
19+
#include "paddle/fluid/eager/eager_tensor.h"
2020
#include "paddle/fluid/eager/grad_node_info.h"
2121
#include "paddle/fluid/eager/tests/data_structure_tests/grad_node_test.h"
2222
#include "paddle/fluid/eager/tests/test_utils.h"
2323
#include "paddle/fluid/eager/utils.h"
2424

25+
#include "paddle/pten/api/lib/utils/allocator.h"
26+
27+
// TODO(jiabin): remove nolint here!!!
28+
using namespace egr; // NOLINT
29+
2530
namespace eager_test {
31+
32+
TEST(EagerUtils, AutoGradMeta) {
33+
// Construct Eager Tensor
34+
pten::DenseTensorMeta meta = pten::DenseTensorMeta(
35+
pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1}));
36+
std::shared_ptr<pten::DenseTensor> dt0 = std::make_shared<pten::DenseTensor>(
37+
std::make_shared<paddle::experimental::DefaultAllocator>(
38+
paddle::platform::CPUPlace()),
39+
meta);
40+
dt0->mutable_data<float>()[0] = 10.0;
41+
EagerTensor et0 = EagerTensor(dt0);
42+
43+
std::shared_ptr<pten::DenseTensor> dt1 = std::make_shared<pten::DenseTensor>(
44+
std::make_shared<paddle::experimental::DefaultAllocator>(
45+
paddle::platform::CPUPlace()),
46+
meta);
47+
dt1->mutable_data<float>()[0] = 20.0;
48+
EagerTensor et1 = EagerTensor(dt1);
49+
50+
std::vector<EagerTensor> ets = {et0, et1};
51+
auto test_node = std::make_shared<eager_test::GradTestNode>();
52+
53+
// unsafe_autograd_meta()
54+
// autograd_meta()
55+
// multi_autograd_meta()
56+
AutogradMeta* autograd_meta0 = EagerUtils::autograd_meta(&et0);
57+
AutogradMeta* autograd_meta1 = EagerUtils::autograd_meta(&et1);
58+
59+
AutogradMeta* unsafe_autograd_meta_after =
60+
EagerUtils::unsafe_autograd_meta(et0);
61+
CHECK_NOTNULL(unsafe_autograd_meta_after);
62+
63+
std::vector<AutogradMeta*> autograd_metas =
64+
EagerUtils::multi_autograd_meta(&ets);
65+
std::vector<AutogradMeta*> unsafe_autograd_metas =
66+
EagerUtils::unsafe_autograd_meta(&ets);
67+
CHECK_NOTNULL(unsafe_autograd_metas[0]);
68+
CHECK_NOTNULL(unsafe_autograd_metas[1]);
69+
70+
// Set Autograd Meta
71+
autograd_meta0->SetSingleOutRankWithSlot(0, 1);
72+
73+
autograd_meta0->SetGradNode(test_node);
74+
75+
// OutRankInfo()
76+
std::pair<size_t, size_t> out_rank_info0 = EagerUtils::OutRankInfo(et0);
77+
CHECK_EQ(static_cast<int>(out_rank_info0.first), 0);
78+
CHECK_EQ(static_cast<int>(out_rank_info0.second), 1);
79+
80+
// grad_node()
81+
std::shared_ptr<GradNodeBase> grad_node0 = EagerUtils::grad_node(et0);
82+
CHECK_NOTNULL(grad_node0.get());
83+
84+
EagerUtils::SetHistory(autograd_meta1, test_node);
85+
EagerUtils::SetHistory({autograd_meta1}, test_node);
86+
std::shared_ptr<GradNodeBase> grad_node1 = EagerUtils::grad_node(et1);
87+
CHECK_NOTNULL(grad_node1.get());
88+
89+
// SetOutRankWithSlot()
90+
EagerUtils::SetOutRankWithSlot(autograd_meta1, 0);
91+
std::pair<size_t, size_t> out_rank_info1 = EagerUtils::OutRankInfo(et1);
92+
CHECK_EQ(static_cast<int>(out_rank_info1.first), 0);
93+
CHECK_EQ(static_cast<int>(out_rank_info1.second), 0);
94+
95+
EagerUtils::SetOutRankWithSlot(&autograd_metas, 0);
96+
std::pair<size_t, size_t> out_rank_info2 = EagerUtils::OutRankInfo(et0);
97+
CHECK_EQ(static_cast<int>(out_rank_info2.first), 0);
98+
CHECK_EQ(static_cast<int>(out_rank_info2.second), 0);
99+
100+
std::pair<size_t, size_t> out_rank_info3 = EagerUtils::OutRankInfo(et1);
101+
CHECK_EQ(static_cast<int>(out_rank_info3.first), 0);
102+
CHECK_EQ(static_cast<int>(out_rank_info3.second), 1);
103+
}
104+
26105
template <typename T>
27106
egr::EagerTensor CreateTestCPUTensor(T val,
28107
const paddle::framework::DDim& ddim) {
@@ -40,7 +119,7 @@ egr::EagerTensor CreateTestCPUTensor(T val,
40119
tensor.set_impl(dt);
41120
return tensor;
42121
}
43-
} // namespace eager_test
122+
44123
TEST(EagerUtils, ComputeRequireGrad) {
45124
auto auto_grad0 = std::make_shared<egr::AutogradMeta>();
46125
auto auto_grad1 = std::make_shared<egr::AutogradMeta>();
@@ -200,3 +279,5 @@ TEST(EagerUtils, ConstructDuplicableOutput) {
200279
CHECK(outs[0]->defined() == false);
201280
CHECK(outs[0]->initialized() == false);
202281
}
282+
283+
} // namespace eager_test

paddle/fluid/eager/utils.cc

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,94 @@
1313
// limitations under the License.
1414

1515
#include "paddle/fluid/eager/utils.h"
16-
#include "paddle/fluid/eager/api/all.h"
16+
#include "paddle/fluid/eager/api/utils/global_utils.h"
17+
18+
#include "paddle/pten/api/all.h"
19+
#include "paddle/pten/common/layout.h"
20+
#include "paddle/pten/core/tensor_meta.h"
21+
22+
#include "paddle/fluid/framework/data_layout.h"
23+
#include "paddle/fluid/framework/pten_utils.h"
24+
#include "paddle/fluid/framework/variable.h"
1725

1826
namespace egr {
27+
/**
28+
* Implementation of Eager Utils.
29+
**/
30+
31+
AutogradMeta* EagerUtils::autograd_meta(egr::EagerTensor* target) {
32+
auto* p_autograd_meta = target->get_autograd_meta();
33+
if (!p_autograd_meta) {
34+
auto p_autograd_meta_ptr = std::make_shared<AutogradMeta>();
35+
p_autograd_meta = p_autograd_meta_ptr.get();
36+
target->set_autograd_meta(p_autograd_meta_ptr);
37+
}
38+
return static_cast<AutogradMeta*>(p_autograd_meta);
39+
}
40+
41+
AutogradMeta* EagerUtils::unsafe_autograd_meta(const egr::EagerTensor& target) {
42+
auto* p_autograd_meta = target.get_autograd_meta();
43+
PADDLE_ENFORCE(p_autograd_meta,
44+
paddle::platform::errors::Fatal(
45+
"Null autograd_meta gotten from unsafe_autograd_meta()"));
46+
return static_cast<AutogradMeta*>(p_autograd_meta);
47+
}
48+
49+
std::vector<AutogradMeta*> EagerUtils::unsafe_autograd_meta(
50+
std::vector<egr::EagerTensor>* targets) {
51+
std::vector<AutogradMeta*> metas;
52+
for (const egr::EagerTensor& t : *targets) {
53+
metas.push_back(unsafe_autograd_meta(t));
54+
}
55+
return metas;
56+
}
57+
58+
std::vector<AutogradMeta*> EagerUtils::multi_autograd_meta(
59+
std::vector<egr::EagerTensor>* targets) {
60+
std::vector<AutogradMeta*> ret;
61+
ret.reserve(targets->size());
62+
63+
// for multi_autograd_meta we can tolerent it has nullptr.
64+
for (auto& t : (*targets)) {
65+
auto* p_autograd_meta = autograd_meta(&t);
66+
ret.push_back(static_cast<AutogradMeta*>(p_autograd_meta));
67+
}
68+
return ret;
69+
}
70+
71+
std::pair<size_t, size_t> EagerUtils::OutRankInfo(
72+
const egr::EagerTensor& target) {
73+
return unsafe_autograd_meta(target)->OutRankInfo();
74+
}
75+
76+
std::shared_ptr<GradNodeBase> EagerUtils::grad_node(
77+
const egr::EagerTensor& target) {
78+
return unsafe_autograd_meta(target)->GetMutableGradNode();
79+
}
80+
81+
void EagerUtils::SetHistory(std::vector<AutogradMeta*>* autograd_metas,
82+
const std::shared_ptr<GradNodeBase>& grad_node) {
83+
for (const auto& autograd_meta : *autograd_metas) {
84+
autograd_meta->SetGradNode(grad_node);
85+
}
86+
}
87+
88+
void EagerUtils::SetHistory(AutogradMeta* autograd_meta,
89+
const std::shared_ptr<GradNodeBase>& grad_node) {
90+
autograd_meta->SetGradNode(grad_node);
91+
}
92+
93+
void EagerUtils::SetOutRankWithSlot(std::vector<AutogradMeta*>* targets,
94+
size_t slot_id) {
95+
// Set OutRankInfo from 0 to size of targets
96+
for (size_t i = 0; i < targets->size(); i++) {
97+
(*targets)[i]->SetSingleOutRankWithSlot(slot_id, i);
98+
}
99+
}
100+
void EagerUtils::SetOutRankWithSlot(AutogradMeta* target, size_t slot_id) {
101+
target->SetSingleOutRankWithSlot(slot_id, 0);
102+
}
103+
19104
/* ---- Tensor -> Var ---- */
20105
std::vector<std::shared_ptr<egr::EagerTensor>> EagerUtils::SyncToVars(
21106
const egr::EagerTensor& tensor) {
@@ -103,18 +188,4 @@ egr::EagerTensor EagerUtils::GetOutput(
103188
return EagerTensor((*(out.get())));
104189
}
105190

106-
AutogradMeta* EagerUtils::unsafe_autograd_meta(const egr::EagerTensor& target) {
107-
auto* p_autograd_meta = target.get_autograd_meta();
108-
PADDLE_ENFORCE(p_autograd_meta,
109-
paddle::platform::errors::Fatal(
110-
"Null autograd_meta gotten from unsafe_autograd_meta(), "
111-
"if you are using unsafe_autograd_meta, please make sure "
112-
"your tensor's autograd_meta is set"));
113-
return static_cast<AutogradMeta*>(p_autograd_meta);
114-
}
115-
116-
std::pair<size_t, size_t> EagerUtils::OutRankInfo(
117-
const egr::EagerTensor& target) {
118-
return unsafe_autograd_meta(target)->OutRankInfo();
119-
}
120191
} // namespace egr

paddle/fluid/eager/utils.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ class EagerUtils {
8787
* constructor (it's abstract class there)
8888
*
8989
* **/
90+
static AutogradMeta* autograd_meta(egr::EagerTensor* target);
91+
92+
static std::vector<AutogradMeta*> multi_autograd_meta(
93+
std::vector<egr::EagerTensor>* targets);
94+
95+
static std::pair<size_t, size_t> OutRankInfo(const egr::EagerTensor& target);
96+
97+
static std::shared_ptr<GradNodeBase> grad_node(
98+
const egr::EagerTensor& target);
99+
100+
// Set history is used to set backward info during forward process, it will
101+
// set forward var's autograd meta's grad node as current backward node.
102+
static void SetHistory(std::vector<AutogradMeta*>* autograd_metas,
103+
const std::shared_ptr<GradNodeBase>& grad_node);
104+
static void SetHistory(AutogradMeta* autograd_meta,
105+
const std::shared_ptr<GradNodeBase>& grad_node);
106+
107+
// This is used for Set vector of tensors' rank
108+
static void SetOutRankWithSlot(std::vector<AutogradMeta*>* targets,
109+
size_t slot_id);
110+
static void SetOutRankWithSlot(AutogradMeta* target, size_t slot_id);
111+
112+
// This method will return an AutogradMeta pointer unsafely.
113+
static AutogradMeta* unsafe_autograd_meta(const egr::EagerTensor& target);
114+
static std::vector<AutogradMeta*> unsafe_autograd_meta(
115+
std::vector<egr::EagerTensor>* targets);
116+
90117
template <typename T, typename... Args>
91118
static bool ComputeRequireGrad(T trace_backward, Args&&... args) {
92119
if (!trace_backward) return false;
@@ -103,9 +130,6 @@ class EagerUtils {
103130
iter.SetStopGradient(stop_gradient);
104131
iter.apply(std::forward<Args>(args)...);
105132
}
106-
static std::pair<size_t, size_t> OutRankInfo(const egr::EagerTensor& target);
107-
// This method will return an AutogradMeta pointer unsafely.
108-
static AutogradMeta* unsafe_autograd_meta(const egr::EagerTensor& target);
109133

110134
// Intermidate needed remove this once we don't need legacy
111135
static std::vector<std::shared_ptr<egr::EagerTensor>> SyncToVars(

0 commit comments

Comments
 (0)