feature/DC asgd (#12722)
* wip * add ref_by_trainer_id op * ready to test * fix ref inputs * refine rpc_op_handle * fix merge bugfix_recordio_link
parent
c3cbf0b8ef
commit
306236c2c0
@ -0,0 +1,79 @@
|
||||
/* 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/ref_by_trainer_id_op.h"
|
||||
#include <string>
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class RefByTrainerIdOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
RefByTrainerIdOp(const std::string &type,
|
||||
const framework::VariableNameMap &inputs,
|
||||
const framework::VariableNameMap &outputs,
|
||||
const framework::AttributeMap &attrs)
|
||||
: OperatorWithKernel(type, inputs, outputs, attrs) {}
|
||||
|
||||
void InferShape(framework::InferShapeContext *ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInputs("X"),
|
||||
"Input(X) of RefByTrainerIdOp should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput("TrainerId"),
|
||||
"Input(TrainerId) of RefByTrainerIdOp should not be null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Out"),
|
||||
"Output(Out) of RefByTrainerIdOp should not be null.");
|
||||
PADDLE_ENFORCE_EQ(ctx->GetInputDim("TrainerId").size(), 1,
|
||||
"TrainerId should be a scalar.");
|
||||
// Out's shape is determined at runtime.
|
||||
}
|
||||
|
||||
protected:
|
||||
framework::OpKernelType GetExpectedKernelType(
|
||||
const framework::ExecutionContext &ctx) const override {
|
||||
return framework::OpKernelType(
|
||||
framework::ToDataType(
|
||||
ctx.MultiInput<framework::Tensor>("X")[0]->type()),
|
||||
ctx.GetPlace());
|
||||
}
|
||||
};
|
||||
|
||||
class RefByTrainerIdOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
void Make() override {
|
||||
AddInput("X", "(Tensor) Input tensor list.").AsDuplicable();
|
||||
AddInput("TrainerId", "(Tensor) Scalar int, the trainer id runtime value.");
|
||||
AddOutput("Out", "(Tensor) Return one tensor reference of X[trainer_id]");
|
||||
AddComment(R"DOC(
|
||||
**RefByTrainerId operator**
|
||||
|
||||
Return a reference of a tensor, using trainer_id as the index to find from the input.
|
||||
|
||||
$$Out = X[TrainerId]$$
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
REGISTER_OP_WITHOUT_GRADIENT(ref_by_trainer_id, ops::RefByTrainerIdOp,
|
||||
ops::RefByTrainerIdOpMaker);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
ref_by_trainer_id,
|
||||
ops::RefByTrainerIdKernel<paddle::platform::CPUDeviceContext, float>,
|
||||
ops::RefByTrainerIdKernel<paddle::platform::CPUDeviceContext, double>,
|
||||
ops::RefByTrainerIdKernel<paddle::platform::CPUDeviceContext, int>,
|
||||
ops::RefByTrainerIdKernel<paddle::platform::CPUDeviceContext, int64_t>);
|
@ -0,0 +1,26 @@
|
||||
/* 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/ref_by_trainer_id_op.h"
|
||||
|
||||
REGISTER_OP_CUDA_KERNEL(
|
||||
ref_by_trainer_id,
|
||||
paddle::operators::RefByTrainerIdKernel<paddle::platform::CUDADeviceContext,
|
||||
float>,
|
||||
paddle::operators::RefByTrainerIdKernel<paddle::platform::CUDADeviceContext,
|
||||
double>,
|
||||
paddle::operators::RefByTrainerIdKernel<paddle::platform::CUDADeviceContext,
|
||||
int>,
|
||||
paddle::operators::RefByTrainerIdKernel<paddle::platform::CUDADeviceContext,
|
||||
int64_t>);
|
@ -0,0 +1,49 @@
|
||||
/* 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 <stdio.h>
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
template <typename DeviceContext, typename T>
|
||||
class RefByTrainerIdKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
virtual void Compute(const framework::ExecutionContext& context) const {
|
||||
auto* out = context.Output<framework::Tensor>("Out");
|
||||
auto in_list = context.MultiInput<framework::Tensor>("X");
|
||||
auto* trainer_id_t = context.Input<framework::Tensor>("TrainerId");
|
||||
int64_t trainer_id;
|
||||
auto* trainer_id_data = trainer_id_t->data<int64_t>();
|
||||
if (platform::is_gpu_place(context.GetPlace())) {
|
||||
#ifdef PADDLE_WITH_CUDA
|
||||
auto stream = context.cuda_device_context().stream();
|
||||
memory::Copy<>(platform::CPUPlace(), &trainer_id,
|
||||
boost::get<platform::CUDAPlace>(context.GetPlace()),
|
||||
trainer_id_data, sizeof(int64_t), stream);
|
||||
#endif
|
||||
} else {
|
||||
trainer_id = *trainer_id_data;
|
||||
}
|
||||
printf("after get trainer_id %lu\n", trainer_id);
|
||||
PADDLE_ENFORCE_LT(trainer_id, in_list.size());
|
||||
out->mutable_data<T>(context.GetPlace());
|
||||
out->ShareDataWith(*(in_list[trainer_id]));
|
||||
}
|
||||
};
|
||||
|
||||
} // 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