frame_op.cc 7.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// Copyright (c) 2021 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/frame_op.h"

namespace paddle {
namespace operators {

class FrameOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "frame");
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "frame");

    const int frame_length = ctx->Attrs().Get<int>("frame_length");
    const int hop_length = ctx->Attrs().Get<int>("hop_length");
    const int axis = ctx->Attrs().Get<int>("axis");

    const auto x_dims = ctx->GetInputDim("X");
    const int x_rank = x_dims.size();

    PADDLE_ENFORCE_GE(
36 37
        x_rank,
        1,
38 39 40 41
        platform::errors::InvalidArgument(
            "Input(X) of FrameOp should be a tensor which contains "
            "at least 1 dimension, but got rank %s.",
            x_rank));
42 43
    PADDLE_ENFORCE_GT(hop_length,
                      0,
44 45 46 47 48
                      platform::errors::InvalidArgument(
                          "Attribute(hop_length) of FrameOp should be greater "
                          "than 0, but got %s.",
                          hop_length));
    PADDLE_ENFORCE_EQ(
49 50
        (axis == 0 || axis == -1),
        true,
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        platform::errors::InvalidArgument(
            "Attribute(axis) of FrameOp should 0 or -1, but got %s.", axis));

    std::vector<int64_t> output_shape;
    int seq_length;
    int n_frames;

    int start_axis;
    int end_axis;

    if (axis == 0) {
      seq_length = x_dims[0];
      start_axis = 1;
      end_axis = x_rank - 1;
    } else {
      seq_length = x_dims[x_rank - 1];
      start_axis = 0;
      end_axis = x_rank - 2;
    }

71 72 73
    bool contain_unknown_dim = phi::contain_unknown_dim(x_dims);
    bool check = ctx->IsRuntime() || !contain_unknown_dim;
    if (check) {
74 75
      PADDLE_ENFORCE_LE(frame_length,
                        seq_length,
76 77 78
                        platform::errors::InvalidArgument(
                            "Attribute(frame_length) of FrameOp should be less "
                            "equal than sequence length, but got (%s) > (%s).",
79 80
                            frame_length,
                            seq_length));
81
    }
82 83 84 85 86 87

    // It won't go into for loop when x_rank == 1U.
    for (int i = start_axis; i <= end_axis; i++) {
      output_shape.push_back(x_dims[i]);
    }

88 89 90 91 92
    if (seq_length == -1) {
      n_frames = -1;
    } else {
      n_frames = 1 + (seq_length - frame_length) / hop_length;
    }
93 94 95 96 97 98 99 100 101 102 103

    if (axis == 0) {
      // (n_frames, frame_length, ...)
      output_shape.insert(output_shape.begin(), frame_length);
      output_shape.insert(output_shape.begin(), n_frames);
    } else {
      // (..., frame_length, n_frames)
      output_shape.push_back(frame_length);
      output_shape.push_back(n_frames);
    }

104
    ctx->SetOutputDim("Out", phi::make_ddim(output_shape));
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    const auto in_dtype = OperatorWithKernel::IndicateVarDataType(ctx, "X");
    return framework::OpKernelType(in_dtype, ctx.GetPlace());
  }
};

class FrameOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "(Tensor), The input tensor of frame op.");
    AddOutput("Out", "(Tensor), The output tensor of frame op.");
    AddAttr<int>(
        "frame_length",
        "Length of the frame and `0 < frame_length <= x.shape[axis]`.");
    AddAttr<int>("hop_length",
                 "Number of steps to advance between adjacent frames and "
                 "`0 < hop_length`.");
    AddAttr<int>("axis",
                 "Specify the axis to operate on the input Tensors. Its value "
                 "should be 0(the first dimension) or -1(the last dimension).")
        .SetDefault(-1);
    AddComment(R"DOC(
      Slice the N-dimensional (where N >= 1) input into (overlapping) frames.
    )DOC");
  }
};

class FrameOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
  void InferShape(framework::InferShapeContext* ctx) const override {
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "frame_grad");
141 142 143 144
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")),
                   "Input",
                   "Out@GRAD",
                   "frame_grad");
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    const auto x_dims = ctx->GetInputDim("X");
    if (ctx->HasOutput(framework::GradVarName("X"))) {
      ctx->SetOutputDim(framework::GradVarName("X"), x_dims);
    }
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    const auto in_dtype = OperatorWithKernel::IndicateVarDataType(ctx, "X");
    return framework::OpKernelType(in_dtype, ctx.GetPlace());
  }
};

template <typename T>
class FrameOpGradMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

  void Apply(GradOpPtr<T> retv) const override {
    retv->SetType("frame_grad");
    retv->SetInput("X", this->Input("X"));
    retv->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    retv->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    retv->SetAttrMap(this->Attrs());
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

178 179 180
REGISTER_OPERATOR(frame,
                  ops::FrameOp,
                  ops::FrameOpMaker,
181 182 183 184 185 186
                  ops::FrameOpGradMaker<paddle::framework::OpDesc>,
                  ops::FrameOpGradMaker<paddle::imperative::OpBase>);

REGISTER_OPERATOR(frame_grad, ops::FrameOpGrad);

REGISTER_OP_CPU_KERNEL(
187
    frame,
L
Leo Chen 已提交
188 189 190 191 192 193
    ops::FrameKernel<phi::CPUContext, int>,
    ops::FrameKernel<phi::CPUContext, int64_t>,
    ops::FrameKernel<phi::CPUContext, float>,
    ops::FrameKernel<phi::CPUContext, double>,
    ops::FrameKernel<phi::CPUContext, paddle::platform::complex<float>>,
    ops::FrameKernel<phi::CPUContext, paddle::platform::complex<double>>);
194 195

REGISTER_OP_CPU_KERNEL(
196
    frame_grad,
L
Leo Chen 已提交
197 198 199 200 201 202
    ops::FrameGradKernel<phi::CPUContext, int>,
    ops::FrameGradKernel<phi::CPUContext, int64_t>,
    ops::FrameGradKernel<phi::CPUContext, float>,
    ops::FrameGradKernel<phi::CPUContext, double>,
    ops::FrameGradKernel<phi::CPUContext, paddle::platform::complex<float>>,
    ops::FrameGradKernel<phi::CPUContext, paddle::platform::complex<double>>);