Add DGC(Deep Gradient Compression) interface. (#15841)
parent
b1d2605152
commit
eb83abeac3
@ -0,0 +1,42 @@
|
||||
# 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(ExternalProject)
|
||||
|
||||
SET(DGC_SOURCES_DIR "${THIRD_PARTY_PATH}/dgc")
|
||||
SET(DGC_INSTALL_DIR "${THIRD_PARTY_PATH}/install/dgc")
|
||||
SET(DGC_INCLUDE_DIR "${DGC_INSTALL_DIR}/include" CACHE PATH "dgc include directory." FORCE)
|
||||
SET(DGC_LIBRARIES "${DGC_INSTALL_DIR}/lib/libdgc.a" CACHE FILEPATH "dgc library." FORCE)
|
||||
INCLUDE_DIRECTORIES(${DGC_INCLUDE_DIR})
|
||||
|
||||
ExternalProject_Add(
|
||||
extern_dgc
|
||||
${EXTERNAL_PROJECT_LOG_ARGS}
|
||||
GIT_REPOSITORY "https://github.com/PaddlePaddle/Fleet"
|
||||
GIT_TAG "2d04dc3800cdd0601f1b65d547dabcc60b0cf9dc"
|
||||
SOURCE_DIR "${DGC_SOURCES_DIR}"
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND cd collective && make -j
|
||||
INSTALL_COMMAND mkdir -p ${DGC_INSTALL_DIR}/lib/ ${DGC_INCLUDE_DIR}/dgc
|
||||
&& cp ${DGC_SOURCES_DIR}/collective/build/lib/libdgc.a ${DGC_LIBRARIES}
|
||||
&& cp ${DGC_SOURCES_DIR}/collective/build/include/dgc.h ${DGC_INCLUDE_DIR}/dgc/
|
||||
BUILD_IN_SOURCE 1
|
||||
)
|
||||
|
||||
ADD_LIBRARY(dgc SHARED IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET dgc PROPERTY IMPORTED_LOCATION ${DGC_LIBRARIES})
|
||||
ADD_DEPENDENCIES(dgc extern_dgc)
|
||||
|
||||
LIST(APPEND external_project_dependencies dgc)
|
||||
|
@ -0,0 +1,67 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
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 <string>
|
||||
|
||||
#include "paddle/fluid/operators/dgc_clip_by_norm_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class DGCClipByNormOp : public ClipByNormOp {
|
||||
public:
|
||||
using ClipByNormOp::ClipByNormOp;
|
||||
|
||||
protected:
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("current_step"),
|
||||
"current_step should be set.");
|
||||
|
||||
return ClipByNormOp::InferShape(ctx);
|
||||
}
|
||||
|
||||
framework::OpKernelType GetKernelTypeForVar(
|
||||
const std::string& var_name, const framework::Tensor& tensor,
|
||||
const framework::OpKernelType& expected_kernel_type) const override {
|
||||
if (var_name == "current_step") {
|
||||
VLOG(10) << "var_name:" << var_name << " need not to transform";
|
||||
return expected_kernel_type;
|
||||
}
|
||||
|
||||
return framework::OperatorWithKernel::GetKernelTypeForVar(
|
||||
var_name, tensor, expected_kernel_type);
|
||||
}
|
||||
};
|
||||
|
||||
class DGCClipByNormOpMaker : public ClipByNormOpMaker {
|
||||
public:
|
||||
void Make() override {
|
||||
AddInput("current_step", "(Tensor) Current step.");
|
||||
AddAttr<float>("rampup_begin_step",
|
||||
"(float, -1.0)"
|
||||
"The period when begin k_select.")
|
||||
.SetDefault(-1.0);
|
||||
|
||||
return ClipByNormOpMaker::Make();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_WITHOUT_GRADIENT(dgc_clip_by_norm, ops::DGCClipByNormOp,
|
||||
ops::DGCClipByNormOpMaker);
|
||||
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
dgc_clip_by_norm,
|
||||
ops::DGCClipByNormKernel<paddle::platform::CPUDeviceContext, float>);
|
@ -0,0 +1,20 @@
|
||||
/* 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/operators/dgc_clip_by_norm_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
dgc_clip_by_norm,
|
||||
ops::DGCClipByNormKernel<paddle::platform::CUDADeviceContext, float>);
|
@ -0,0 +1,46 @@
|
||||
/* 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. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "paddle/fluid/operators/clip_by_norm_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
template <typename DeviceContext, typename T>
|
||||
class DGCClipByNormKernel : public ClipByNormKernel<DeviceContext, T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
auto rampup_begin_step = context.Attr<float>("rampup_begin_step");
|
||||
if (static_cast<int>(rampup_begin_step) >= 0) {
|
||||
auto current_step_tensor =
|
||||
context.Input<framework::Tensor>("current_step");
|
||||
auto* current_step = current_step_tensor->data<T>();
|
||||
|
||||
if (static_cast<int>(*current_step) <
|
||||
static_cast<int>(rampup_begin_step)) {
|
||||
VLOG(10) << "current_step:" << *current_step
|
||||
<< " < rampup_begin_step:" << rampup_begin_step
|
||||
<< " so does't use dgc_clip_by_norm";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return ClipByNormKernel<DeviceContext, T>::Compute(context);
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,138 @@
|
||||
/* 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/operators/dgc_op.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class DGCOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("U"), "Input(U) of DGCop should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput("V"), "Input(V) of DGCop should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput("Grad"),
|
||||
"Input(Grad) of DGCop should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput("current_step"),
|
||||
"Input(current_step) of DGCop should not be null.");
|
||||
|
||||
PADDLE_ENFORCE(ctx->HasOutput("U_out"),
|
||||
"Output(U_out) of DGCop should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("V_out"),
|
||||
"Output(V_out) of DGCop should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("k"),
|
||||
"Output(k) of DGCop should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("EncodeGrad"),
|
||||
"Output(EncodeGrad) of DGCop should not be null.");
|
||||
}
|
||||
|
||||
protected:
|
||||
framework::OpKernelType GetKernelTypeForVar(
|
||||
const std::string& var_name, const framework::Tensor& tensor,
|
||||
const framework::OpKernelType& expected_kernel_type) const override {
|
||||
if (var_name == "current_step" || var_name == "rampup_step" ||
|
||||
var_name == "k") {
|
||||
VLOG(10) << "var_name:" << var_name << " need not to transform";
|
||||
return expected_kernel_type;
|
||||
}
|
||||
|
||||
return framework::OperatorWithKernel::GetKernelTypeForVar(
|
||||
var_name, tensor, expected_kernel_type);
|
||||
}
|
||||
};
|
||||
|
||||
class DGCOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
void Make() override {
|
||||
AddInput("U", "(Tensor) Middle tensor of DGC");
|
||||
AddInput("V", "(Tensor) Middle tensor of DGC");
|
||||
AddInput("Grad", "(Tensor) Input gradient");
|
||||
AddInput("current_step", "(Tensor) Current step.");
|
||||
|
||||
AddOutput("U_out",
|
||||
"(Tensor) "
|
||||
"Output encoded gradient");
|
||||
AddOutput("V_out",
|
||||
"(Tensor) "
|
||||
"Output encoded gradient");
|
||||
AddOutput("EncodeGrad",
|
||||
"(Tensor) "
|
||||
"Output encoded gradient");
|
||||
AddOutput("Grad_out",
|
||||
"(Tensor) "
|
||||
"Output grad gradient");
|
||||
AddOutput("k",
|
||||
"(Tensor) "
|
||||
"Output top-k value");
|
||||
|
||||
AddAttr<float>("m",
|
||||
"(float, 0.9) "
|
||||
"The momentum of learning rate.")
|
||||
.SetDefault(0.9);
|
||||
|
||||
AddAttr<bool>("use_nesterov",
|
||||
"(bool, true)"
|
||||
"The momentum of learning rate.")
|
||||
.SetDefault(true);
|
||||
|
||||
AddAttr<std::vector<float>>("sparsity",
|
||||
"(vecotr, float)"
|
||||
"The period sparsity of k_select.");
|
||||
|
||||
AddAttr<float>("rampup_begin_step",
|
||||
"(float, 0.0)"
|
||||
"The period when begin k_select.")
|
||||
.SetDefault(0.0);
|
||||
|
||||
AddAttr<float>("rampup_step",
|
||||
"(float, 0.0)"
|
||||
"The period when begin k_select.");
|
||||
|
||||
AddComment(R"DOC(
|
||||
Original paper is https://arxiv.org/abs/1712.01887
|
||||
|
||||
DGC reduce the communication bandwidth by sending only the important gradients (sparse update):\
|
||||
only gradients larger than a threshold are transmitted.
|
||||
|
||||
To avoid losing information, DGC accumulate the rest of the gradients locally.
|
||||
|
||||
Eventually, these gradients become large enough to be transmitted.
|
||||
|
||||
Thus, DGC send the large gradients immediately but eventually send all of the gradients over time.
|
||||
|
||||
To ensure no loss of accuracy, DGC employs momentum correc-tionandlocal gradient clipping on top of the gradient sparsification to maintain model performance.
|
||||
|
||||
DGC also uses momentum factor masking and warmup training to overcome the staleness problem caused by reduced communication.
|
||||
|
||||
This optimizer will do two things:
|
||||
|
||||
1. Compress the gradient by get TopK import value from tensor \
|
||||
and use it for allreduce to reduce network bandwidth.
|
||||
|
||||
2. Call momentum to optimize on the cost.
|
||||
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_WITHOUT_GRADIENT(dgc, ops::DGCOp, ops::DGCOpMaker);
|
@ -0,0 +1,20 @@
|
||||
/* 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/operators/dgc_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
dgc, ops::DGCOpKernel<paddle::platform::CUDADeviceContext, float>);
|
@ -0,0 +1,132 @@
|
||||
/* 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. */
|
||||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "dgc/dgc.h"
|
||||
#include "paddle/fluid/framework/eigen.h"
|
||||
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
inline float get_period_sparcity(const std::vector<float>& sparsity,
|
||||
float cur_step, float rampup_steps) {
|
||||
PADDLE_ENFORCE(static_cast<int>(cur_step) >= 0);
|
||||
|
||||
size_t idx = static_cast<int>(cur_step * sparsity.size() / rampup_steps);
|
||||
if (idx >= sparsity.size()) {
|
||||
return 0.999;
|
||||
}
|
||||
|
||||
PADDLE_ENFORCE(idx < sparsity.size());
|
||||
return sparsity[idx];
|
||||
}
|
||||
|
||||
template <typename DeviceContext, typename T>
|
||||
class DGCOpKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override {
|
||||
auto u = ctx.Input<framework::Tensor>("U");
|
||||
auto v = ctx.Input<framework::Tensor>("V");
|
||||
auto g = ctx.Input<framework::Tensor>("Grad");
|
||||
|
||||
// attrs
|
||||
float m = ctx.Attr<float>("m");
|
||||
bool use_nesterov = ctx.Attr<bool>("use_nesterov");
|
||||
auto sparsity = ctx.Attr<std::vector<float>>("sparsity");
|
||||
auto rampup_begin_step = ctx.Attr<float>("rampup_begin_step");
|
||||
auto rampup_step = ctx.Attr<float>("rampup_step");
|
||||
|
||||
// current step
|
||||
auto current_step_tensor = ctx.Input<framework::Tensor>("current_step");
|
||||
const float* current_step = current_step_tensor->data<float>();
|
||||
|
||||
if (static_cast<int>(*current_step) < static_cast<int>(rampup_begin_step)) {
|
||||
VLOG(10) << "current_step:" << *current_step
|
||||
<< " < rampup_begin_step:" << rampup_begin_step
|
||||
<< " so does't use dgc";
|
||||
return;
|
||||
}
|
||||
|
||||
float ratio =
|
||||
1 - get_period_sparcity(sparsity, static_cast<float>(*current_step),
|
||||
rampup_step);
|
||||
PADDLE_ENFORCE(ratio > 0.0 && ratio < 1.0);
|
||||
int k = static_cast<int>(g->numel() * ratio);
|
||||
|
||||
VLOG(10) << "m:" << m << ", use_nesterov:" << use_nesterov
|
||||
<< ", rampup_begin_step:" << rampup_begin_step
|
||||
<< ", rampup_step:" << rampup_step
|
||||
<< ", current_step:" << *current_step << ", ratio:" << ratio
|
||||
<< ", k:" << k;
|
||||
|
||||
auto k_out = ctx.Output<framework::Tensor>("k");
|
||||
T* k_out_data = k_out->data<T>();
|
||||
*k_out_data = k;
|
||||
|
||||
auto u_out = ctx.Output<framework::Tensor>("U_out");
|
||||
auto v_out = ctx.Output<framework::Tensor>("V_out");
|
||||
auto encode_grad_out = ctx.Output<framework::Tensor>("EncodeGrad");
|
||||
|
||||
// FIXME(gongwb): use cublas.
|
||||
auto u_out_e = framework::EigenVector<T>::Flatten(*u_out);
|
||||
auto u_e = framework::EigenVector<T>::Flatten(*u);
|
||||
auto g_e = framework::EigenVector<T>::Flatten(*g);
|
||||
auto& dev_ctx = ctx.template device_context<DeviceContext>();
|
||||
auto& eigen_ctx = *dev_ctx.eigen_device();
|
||||
if (use_nesterov) {
|
||||
// u = m * (u + g)
|
||||
u_out_e.device(eigen_ctx) = m * (u_e + g_e);
|
||||
|
||||
// v = u + v + g
|
||||
ElementwiseComputeEx<AddFunctor<T>, DeviceContext, T>(
|
||||
ctx, u, v, 0, AddFunctor<T>(), v_out);
|
||||
|
||||
ElementwiseComputeEx<AddFunctor<T>, DeviceContext, T>(
|
||||
ctx, g, v, 0, AddFunctor<T>(), v_out);
|
||||
} else {
|
||||
// u = m * u + g
|
||||
u_out_e.device(eigen_ctx) = m * u_e + g_e;
|
||||
|
||||
// v = u + v
|
||||
ElementwiseComputeEx<AddFunctor<T>, DeviceContext, T>(
|
||||
ctx, u, v, 0, AddFunctor<T>(), v_out);
|
||||
}
|
||||
|
||||
T* v_out_data = v_out->mutable_data<T>(ctx.GetPlace());
|
||||
T* u_out_data = u_out->mutable_data<T>(ctx.GetPlace());
|
||||
T* encode_grad_out_data = encode_grad_out->mutable_data<T>(
|
||||
framework::DDim{2 * k}, ctx.GetPlace());
|
||||
|
||||
int buf_size = paddle::communication::dgc::get_buffer_size(k);
|
||||
auto& allocator = platform::DeviceTemporaryAllocator::Instance().Get(
|
||||
ctx.GetPlace(), dev_ctx.stream());
|
||||
auto tmp_ious_data = allocator.Allocate(buf_size);
|
||||
void* buf = reinterpret_cast<void*>(tmp_ious_data->ptr());
|
||||
|
||||
if (!paddle::communication::dgc::k_select(
|
||||
static_cast<void*>(encode_grad_out_data), k, v_out_data,
|
||||
static_cast<int>(v_out->numel()), buf, dev_ctx.stream(),
|
||||
u_out_data)) {
|
||||
LOG(FATAL) << "v_out numel:" << v_out->numel();
|
||||
}
|
||||
|
||||
auto grad_out = ctx.Output<framework::Tensor>("Grad_out");
|
||||
math::SetConstant<DeviceContext, T> tset;
|
||||
tset(dev_ctx, grad_out, static_cast<T>(0));
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue