提交 9edfd200 编写于 作者: X xutianbing

use Daoyuan's SequenceArg.

上级 23ac0b78
......@@ -190,7 +190,7 @@ private:
size_t numSeqs_;
};
// sequence data
// sequence data {seqId(vec), buf(matrix)}
class SequenceArg : public BufferArg {
public:
SequenceArg(void* buf,
......@@ -210,6 +210,7 @@ public:
void* getIdBuf() const { return startPositions_.data(); }
size_t numSeqs() const { return startPositions_.numSeqs(); }
const SequenceIdArg& getSequenceIds() const { return startPositions_; }
private:
SequenceIdArg startPositions_;
......
......@@ -17,7 +17,6 @@ limitations under the License. */
#include "paddle/math/Vector.h"
namespace paddle {
/**
* Context Projection Forward with CPU Matrix Device.
*
......@@ -208,10 +207,10 @@ void ContextProjectionBackward<DEVICE_TYPE_CPU>(const CpuMatrix& out_grad_mat,
* Context Projection Backward Function.
* Update the weight gradient and input layer gradient with backprop
*
* \param inputs[0] input sequence.
* \param inputs[1] output layer grad.
* \param outputs[0] input layer grad.
* \param outputs[1] weight grad.
* \param inputs[0].seq input sequence.
* \param inputs[0].matrix output layer grad.
* \param outputs[0] input layer grad.
* \param outputs[1] weight grad.
*/
template <DeviceType Device>
class ContextProjectionBackwardFunc : public FunctionBase {
......@@ -225,27 +224,28 @@ public:
}
void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
CHECK_EQ((size_t)2, inputs.size());
CHECK_EQ((size_t)1, inputs.size());
CHECK_EQ((size_t)2, outputs.size());
CHECK(inputs[0].data() && inputs[1].data());
CHECK_EQ(inputs[0].shape().ndims(), (size_t)1);
CHECK_EQ(inputs[1].shape().ndims(), (size_t)2);
const auto seqArg = dynamic_cast<const SequenceArg&>(inputs[0]);
CHECK(seqArg.data() && inputs[0].data());
CHECK_EQ(seqArg.shape().ndims(), (size_t)2);
CHECK_EQ(seqArg.getSequenceIds().shape().ndims(), (size_t)1);
CHECK_EQ(outputs[0].shape().ndims(), (size_t)2);
CHECK_EQ(outputs[1].shape().ndims(), (size_t)2);
/// dim of input grad == dim of weight
CHECK_EQ(outputs[0].shape()[1], outputs[1].shape()[1]);
/// input and output grad has the same batch_size
CHECK_EQ(outputs[0].shape()[0], inputs[1].shape()[0]);
CHECK_EQ(outputs[0].shape()[0], seqArg.shape()[0]);
/// dim of output val = dim of input grad * context_length
CHECK_EQ(inputs[1].shape()[1], outputs[0].shape()[1] * context_length_);
CHECK_EQ(seqArg.shape()[1], outputs[0].shape()[1] * context_length_);
CHECK_EQ(outputs[0].getArgType(), ADD_TO);
CHECK_EQ(outputs[1].getArgType(), ADD_TO);
const auto seq_vec = inputs[0].vector<int, Device>();
const auto out_grad_mat = inputs[1].matrix<Device>();
const auto seq_vec = seqArg.getSequenceIds().vector<int, Device>();
const auto out_grad_mat = seqArg.matrix<Device>();
auto in_grad_mat =
!outputs[0].data()
? typename Tensor<real, Device>::Matrix(nullptr, 0, 0)
......
......@@ -122,8 +122,7 @@ void testMatrixProjectionBackward(int context_start,
BufferArgs cpu_inputs;
BufferArgs cpu_outputs;
cpu_inputs.addArg(*cpu_seq);
cpu_inputs.addArg(cpu_out_grad);
cpu_inputs.addArg(cpu_out_grad, *cpu_seq);
cpu_outputs.addArg(cpu_in_grad, ADD_TO);
cpu_outputs.addArg(
cpu_w_grad ? *cpu_w_grad : CpuMatrix(nullptr, 0, input_dim), ADD_TO);
......@@ -132,8 +131,7 @@ void testMatrixProjectionBackward(int context_start,
BufferArgs gpu_inputs;
BufferArgs gpu_outputs;
gpu_inputs.addArg(*gpu_seq);
gpu_inputs.addArg(gpu_out_grad);
gpu_inputs.addArg(gpu_out_grad, *gpu_seq);
gpu_outputs.addArg(gpu_in_grad, ADD_TO);
gpu_outputs.addArg(
gpu_w_grad ? *gpu_w_grad : GpuMatrix(nullptr, 0, input_dim), ADD_TO);
......
......@@ -90,6 +90,12 @@ void BufferArgs::addArg(const GpuSparseMatrix& arg, ArgType argType) {
args_.push_back(std::make_shared<SparseMatrixArg>(arg, argType));
}
void BufferArgs::addArg(const Matrix& matrix,
const IVector& vector,
ArgType argType) {
args_.push_back(std::make_shared<SequenceArg>(matrix, vector, argType));
}
ClassRegistrar<FunctionBase> FunctionBase::funcRegistrar_;
} // namespace paddle
......@@ -77,6 +77,10 @@ public:
void addArg(const CpuSparseMatrix& arg, ArgType argType = UNSPECIFIED);
void addArg(const GpuSparseMatrix& arg, ArgType argType = UNSPECIFIED);
void addArg(const Matrix& matrix,
const IVector& vector,
ArgType argType = UNSPECIFIED);
// get argument
const BufferArg& operator[](size_t num) const {
CHECK_LT(num, args_.size());
......
......@@ -166,8 +166,7 @@ void ContextProjection::backward(const UpdateCallback& callback) {
BufferArgs inputs;
BufferArgs outputs;
inputs.addArg(*in_->sequenceStartPositions->getVector(useGpu_));
inputs.addArg(*out_->grad);
inputs.addArg(*out_->grad, *in_->sequenceStartPositions->getVector(useGpu_));
outputs.addArg(
CpuMatrix(
in_->grad ? in_->grad->getData() : nullptr, batch_size, input_dim),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册