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
【Hackathon No.19】Implement ASGD optimizer #42431
Closed
tiancaishaonvjituizi
wants to merge
12
commits into
PaddlePaddle:develop
from
tiancaishaonvjituizi:asgd_optimizer
Closed
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ad8ceda
fix false positive warning in gcc>=9
tiancaishaonvjituizi 178dd27
use more aggressive way
tiancaishaonvjituizi 6cfd8ec
stash
tiancaishaonvjituizi c3b49d8
implement asgd optimizer
tiancaishaonvjituizi ef95457
refine
tiancaishaonvjituizi 2c6f29d
replace array_equal with allclose
tiancaishaonvjituizi 8a48bfb
add print to debug
tiancaishaonvjituizi 506d05b
add print to debug
tiancaishaonvjituizi 0403b08
add print to debug
tiancaishaonvjituizi f353873
update test
tiancaishaonvjituizi 3a21267
add print to debug
tiancaishaonvjituizi 46e5362
Merge branch 'develop' into asgd_optimizer
tiancaishaonvjituizi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* Copyright (c) 2016 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/infershape_utils.h" | ||
#include "paddle/fluid/framework/op_registry.h" | ||
#include "paddle/phi/core/infermeta_utils.h" | ||
#include "paddle/phi/infermeta/multiary.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
using Tensor = framework::Tensor; | ||
|
||
class AsgdOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
framework::OpKernelType GetExpectedKernelType( | ||
const framework::ExecutionContext &ctx) const override { | ||
return framework::OpKernelType( | ||
OperatorWithKernel::IndicateVarDataType(ctx, "Param"), ctx.GetPlace()); | ||
} | ||
}; | ||
|
||
class AsgdOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
void Make() override { | ||
AddInput("Param", "(Tensor) Input parameter"); | ||
AddInput("Grad", "(Tensor) Input gradient"); | ||
AddInput("LearningRate", "(Tensor) Learning rate of SGD"); | ||
AddInput("AvgParam", | ||
"(Tensor) Average of parameter"); | ||
AddInput("CurrentStep", | ||
"(Tensor) Current step"); | ||
AddOutput("ParamOut", | ||
"(Tensor, same with Param) " | ||
"Output parameter, should share the same memory with Param"); | ||
AddOutput("AvgParamOut", | ||
"(Tensor, same with AvgParam) Average of parameter"); | ||
AddOutput("CurrentStepOut", | ||
"(Tensor) Increased step"); | ||
|
||
AddAttr<float>("t0", | ||
"(float, default 1e6) point at which to start averaging") | ||
.SetDefault(0.95f); | ||
AddComment(R"DOC( | ||
)DOC"); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
DECLARE_INFER_SHAPE_FUNCTOR(asgd, AsgdInferMetaFunctor, | ||
PD_INFER_META(phi::AsgdInferMeta)); | ||
REGISTER_OPERATOR( | ||
asgd, ops::AsgdOp, ops::AsgdOpMaker, | ||
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>, | ||
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>, | ||
AsgdInferMetaFunctor); |
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,33 @@ | ||
// Copyright (c) 2022 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/phi/core/dense_tensor.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void AsgdKernel(const Context& dev_ctx, | ||
const DenseTensor& param, | ||
const DenseTensor& learning_rate, | ||
const DenseTensor& grad, | ||
const DenseTensor& avg_param, | ||
const DenseTensor& current_step, | ||
float t0, | ||
DenseTensor* param_out, | ||
DenseTensor* avg_param_out, | ||
DenseTensor* current_step_out); | ||
|
||
} // namespace phi |
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,65 @@ | ||
// Copyright (c) 2022 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/phi/kernels/asgd_kernel.h" | ||
|
||
#include "paddle/phi/backends/cpu/cpu_context.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
#include "paddle/phi/kernels/funcs/eigen/common.h" | ||
#include "paddle/phi/kernels/funcs/eigen/eigen_function.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void AsgdKernel(const Context& dev_ctx, | ||
const DenseTensor& param, | ||
const DenseTensor& grad, | ||
const DenseTensor& learning_rate, | ||
const DenseTensor& avg_param, | ||
const DenseTensor& current_step, | ||
float t0, | ||
DenseTensor* param_out, | ||
DenseTensor* avg_param_out, | ||
DenseTensor* current_step_out) { | ||
dev_ctx.template Alloc<T>(param_out); | ||
dev_ctx.template Alloc<T>(avg_param_out); | ||
dev_ctx.template Alloc<T>(current_step_out); | ||
|
||
auto eigen_param = EigenVector<T>::Flatten(param); | ||
auto eigen_grad = EigenVector<T>::Flatten(grad); | ||
auto eigen_avg_param = EigenVector<T>::Flatten(avg_param); | ||
auto eigen_param_out = EigenVector<T>::Flatten(*param_out); | ||
auto eigen_avg_param_out = EigenVector<T>::Flatten(*avg_param_out); | ||
auto& place = *dev_ctx.eigen_device(); | ||
|
||
auto lr = learning_rate.data<T>()[0]; | ||
eigen_param_out.device(place) = eigen_param - lr * eigen_grad; | ||
|
||
T current_step_data = current_step.data<T>()[0]; | ||
|
||
if (current_step_data <= t0) { | ||
eigen_avg_param_out.device(place) = eigen_param_out; | ||
} else { | ||
const auto mu1 = 1 / (current_step_data - t0); | ||
const auto mu2 = 1 - mu1; | ||
eigen_avg_param_out.device(place) = | ||
mu2 * eigen_avg_param + mu1 * eigen_param_out; | ||
} | ||
*current_step_out->mutable_data<T>(dev_ctx.GetPlace()) = | ||
current_step_data + 1; | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL(asgd, CPU, ALL_LAYOUT, phi::AsgdKernel, float, double) {} |
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,83 @@ | ||
// Copyright (c) 2022 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/phi/kernels/asgd_kernel.h" | ||
|
||
#include "paddle/phi/backends/gpu/gpu_context.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T> | ||
__global__ void ASGDKernel(const T* param, | ||
const T* grad, | ||
const T* learning_rate, | ||
const T* avg_param, | ||
const T* current_step, | ||
float t0, | ||
size_t num, | ||
T* param_out, | ||
T* avg_param_out) { | ||
T lr = learning_rate[0]; | ||
CUDA_KERNEL_LOOP(i, num) { param_out[i] = param[i] - lr * grad[i]; } | ||
T current_step_data = current_step[0]; | ||
if (current_step_data <= t0) { | ||
memcpy(avg_param_out, param, num * sizeof(T)); | ||
} else { | ||
const auto mu1 = 1 / (current_step_data - t0); | ||
const auto mu2 = 1 - mu1; | ||
CUDA_KERNEL_LOOP(i, num) { | ||
avg_param_out[i] = mu2 * avg_param[i] + mu1 * param_out[i]; | ||
} | ||
} | ||
} | ||
|
||
template <typename T> | ||
__global__ void IncreaseStep(const T* step, T* step_out) { | ||
*step_out = *step + 1; | ||
} | ||
|
||
template <typename T, typename Context> | ||
void AsgdKernel(const Context& dev_ctx, | ||
const DenseTensor& param, | ||
const DenseTensor& learning_rate, | ||
const DenseTensor& grad, | ||
const DenseTensor& avg_param, | ||
const DenseTensor& current_step, | ||
float t0, | ||
DenseTensor* param_out, | ||
DenseTensor* avg_param_out, | ||
DenseTensor* current_step_out) { | ||
int block = 512; | ||
int grid = (param.numel() + block - 1) / block; | ||
|
||
ASGDKernel<T><<<grid, block, 0, dev_ctx.stream()>>>( | ||
param.data<T>(), | ||
grad.data<T>(), | ||
learning_rate.data<T>(), | ||
avg_param.data<T>(), | ||
current_step.data<T>(), | ||
t0, | ||
param.numel(), | ||
param_out->mutable_data<T>(dev_ctx.GetPlace()), | ||
avg_param_out->mutable_data<T>(dev_ctx.GetPlace())); | ||
|
||
IncreaseStep<T><<<1, 1, 0, dev_ctx.stream()>>>( | ||
current_step.data<T>(), | ||
current_step_out->mutable_data<T>(dev_ctx.GetPlace())); | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL(asgd, GPU, ALL_LAYOUT, phi::AsgdKernel, float, double) {} |
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
Binary file not shown.
Binary file not shown.
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.
本文件的改动已经单独提交了 PR:#42265 ,在本 PR 的 review 中可以忽略这些改动