diff --git a/paddle/operators/reshape_op.cc b/paddle/operators/reshape_op.cc index 0d05e344148c68f5625dd819ec59c5991892e4ce..d5d04dac275157e9538e8fdbcfb64b61e7ea657a 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 0a99717440e4321cd7b01a6899187cccfd519632..58ef77b1a3a55c1dfe450463f52b4ab0a9177666 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 54d826527111cb77e58fe268eeef6d433a690b9f..f39c2ec6c31d57727899fe99124ae34ee78d522c 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); } }