From 12f2b8eb07b034a24e3a0e0538a757e389fb4c45 Mon Sep 17 00:00:00 2001 From: Liu Yiqun Date: Mon, 25 Sep 2017 04:12:04 +0000 Subject: [PATCH] Correct the forward of sequence_softmax_op. --- paddle/operators/reshape_op.cc | 3 +-- paddle/operators/sequence_softmax_op.cc | 10 ++++++---- paddle/operators/sequence_softmax_op.h | 6 +++++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/paddle/operators/reshape_op.cc b/paddle/operators/reshape_op.cc index 0d05e344148..d5d04dac275 100644 --- a/paddle/operators/reshape_op.cc +++ b/paddle/operators/reshape_op.cc @@ -42,8 +42,7 @@ class ReshapeOp : public framework::OperatorWithKernel { int64_t capacity = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies()); auto *in = ctx.Input("X"); - int64_t in_size = framework::product(in->dims()); - PADDLE_ENFORCE_EQ(capacity, in_size, + PADDLE_ENFORCE_EQ(capacity, in->numel(), "The size of Input(X) mismatches with Attr(shape)."); // resize output std::vector shape_int64(shape.size(), 0); diff --git a/paddle/operators/sequence_softmax_op.cc b/paddle/operators/sequence_softmax_op.cc index 0a99717440e..58ef77b1a3a 100644 --- a/paddle/operators/sequence_softmax_op.cc +++ b/paddle/operators/sequence_softmax_op.cc @@ -30,18 +30,20 @@ class SequenceSoftmaxOp : public framework::OperatorWithKernel { "Output(Out) of SequenceSoftmaxOp should not be null."); auto *x = ctx.Input("X"); - auto dims = x->dims(); auto lod = x->lod(); - PADDLE_ENFORCE_EQ(lod.size(), 1UL, "Only support one level sequence now."); + auto dims = x->dims(); PADDLE_ENFORCE_GE( dims[0], /* batch_size */ static_cast(lod[0].size() - 1), "The first dimension of Input(X) should be larger than batch size."); - PADDLE_ENFORCE_EQ(x->numel(), static_cast(lod[0].size() - 1), + + const size_t level = lod.size() - 1; + PADDLE_ENFORCE_EQ(x->numel(), static_cast(lod[level].back()), "The width of each timestep in Input(X) of " "SequenceSoftmaxOp should be 1."); - dims[0] = lod[0].size() - 1; + std::cout << DebugString() << std::endl; + ctx.Output("Out")->Resize({dims}); } }; diff --git a/paddle/operators/sequence_softmax_op.h b/paddle/operators/sequence_softmax_op.h index 54d82652711..f39c2ec6c31 100644 --- a/paddle/operators/sequence_softmax_op.h +++ b/paddle/operators/sequence_softmax_op.h @@ -38,7 +38,7 @@ class SequenceSoftmaxKernel : public framework::OpKernel { auto* out = ctx.Output("Out"); auto lod = x->lod(); - const size_t level = lod.size(); + const size_t level = lod.size() - 1; out->mutable_data(ctx.GetPlace()); for (int i = 0; i < static_cast(lod[level].size()) - 1; ++i) { @@ -47,6 +47,10 @@ class SequenceSoftmaxKernel : public framework::OpKernel { Tensor x_i = x->Slice(start_pos, end_pos); Tensor out_i = out->Slice(start_pos, end_pos); + // Reshape from (end_pos - start_pos) x 1UL to 1UL x (end_pos - start_pos) + framework::DDim dims = framework::make_ddim({1UL, end_pos - start_pos}); + x_i.Resize(dims); + out_i.Resize(dims); math::SoftmaxFunctor()(&x_i, &out_i, ctx); } } -- GitLab