-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added accumulation codes to Eager Dygraph
- Loading branch information
1 parent
15984f4
commit 061be26
Showing
15 changed files
with
576 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
add_subdirectory(accumulation) | ||
add_subdirectory(tests) | ||
|
||
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(grad_tensor_holder SRCS grad_tensor_holder.cc DEPS grad_node_info) | ||
cc_library(grad_tensor_holder SRCS grad_tensor_holder.cc DEPS grad_node_info gradient_accumulation) |
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,2 @@ | ||
cc_library(gradient_accumulation SRCS gradient_accumulation.cc DEPS blas pten pten_api var_type_traits layer math_function) | ||
cc_library(accumulation_node SRCS accumulation_node.cc DEPS gradient_accumulation pten pten_api grad_node_info) |
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,81 @@ | ||
// 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/accumulation/accumulation_node.h" | ||
#include "paddle/fluid/eager/accumulation/gradient_accumulation.h" | ||
#include "paddle/fluid/eager/eager_tensor.h" | ||
|
||
#include "paddle/pten/api/all.h" | ||
#include "paddle/pten/core/dense_tensor.h" | ||
#include "paddle/pten/include/core.h" | ||
|
||
#include "paddle/fluid/platform/device_context.h" | ||
#include "paddle/fluid/platform/enforce.h" | ||
#include "paddle/fluid/platform/errors.h" | ||
|
||
#include "glog/logging.h" | ||
|
||
static void CopyOrAddTensor(egr::EagerTensor* tensor, | ||
const egr::EagerTensor& t) { | ||
if (!tensor->defined() || !tensor->initialized()) { | ||
// Simply copy tensor->impl | ||
*tensor = t; | ||
} else { | ||
// Accumulation | ||
egr::TensorAdd(t, tensor); | ||
} | ||
} | ||
|
||
namespace egr { | ||
|
||
void GradNodeAccumulation::RetainGrad( | ||
const std::function<egr::EagerTensor(const egr::EagerTensor&)>& hook) { | ||
retain_grad_hook_ = hook; | ||
} | ||
|
||
std::vector<std::vector<egr::EagerTensor>> GradNodeAccumulation::operator()( | ||
const std::vector<std::vector<egr::EagerTensor>>& grads) { | ||
PADDLE_ENFORCE(grads.size() == 1, | ||
paddle::platform::errors::Fatal( | ||
"GradNodeAccumulation should take exactly 1 grad tensor" | ||
"However received: %d slot.", | ||
grads.size())); | ||
PADDLE_ENFORCE(grads[0].size() == 1, | ||
paddle::platform::errors::Fatal( | ||
"GradNodeAccumulation should take exactly 1 grad tensor" | ||
"However received: %d in slot %d .", | ||
grads[0].size(), 0)); | ||
// Apply Gradient Hooks | ||
if (GradientHooksRegistered()) { | ||
std::vector<std::vector<egr::EagerTensor>> hooked_grads = | ||
ApplyGradientHooks(grads); | ||
// TODO(jiabin): It's little weird | ||
CopyOrAddTensor(&accumulated_grad, hooked_grads[0][0]); | ||
} else { | ||
CopyOrAddTensor(&accumulated_grad, grads[0][0]); | ||
} | ||
|
||
if (retain_grad_hook_ != nullptr) { | ||
retain_grad_hook_(accumulated_grad); | ||
} | ||
|
||
// Apply Reduce Hooks | ||
if (ReduceHooksRegistered()) { | ||
ApplyReduceHooks(); | ||
} | ||
|
||
return {{accumulated_grad}}; | ||
} | ||
|
||
} // 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,41 @@ | ||
// 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/grad_node_info.h" | ||
|
||
namespace egr { | ||
|
||
class GradNodeAccumulation : public GradNodeBase { | ||
public: | ||
// Constructor: configure fwd input tensors to grad node | ||
GradNodeAccumulation() : GradNodeBase(1, 1) { SetDefaultGradInOutMeta(); } | ||
|
||
~GradNodeAccumulation() override = default; | ||
|
||
// Functor: perform backward computations | ||
virtual std::vector<std::vector<egr::EagerTensor>> operator()( | ||
const std::vector<std::vector<egr::EagerTensor>>& grads) override; | ||
|
||
void RetainGrad( | ||
const std::function<egr::EagerTensor(const egr::EagerTensor&)>& hook); | ||
|
||
private: | ||
egr::EagerTensor accumulated_grad; | ||
|
||
std::function<egr::EagerTensor(const egr::EagerTensor&)> retain_grad_hook_; | ||
}; | ||
|
||
} // namespace egr |
Oops, something went wrong.