lstsq_op.cc 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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/lstsq_op.h"
16

17 18
#include <string>
#include <vector>
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace operators {

class LstsqOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
  void InferShape(framework::InferShapeContext* ctx) const override {
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "LstsqOp");
    OP_INOUT_CHECK(ctx->HasInput("Y"), "Input", "Y", "LstsqOp");

    OP_INOUT_CHECK(ctx->HasOutput("Solution"), "Output", "Solution", "LstsqOp");
    OP_INOUT_CHECK(ctx->HasOutput("Rank"), "Output", "Rank", "LstsqOp");
34 35 36
    OP_INOUT_CHECK(ctx->HasOutput("SingularValues"),
                   "Output",
                   "SingularValues",
37 38 39 40 41 42 43
                   "LstsqOp");

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

44 45
    PADDLE_ENFORCE_GE(x_rank,
                      2,
46 47 48 49
                      platform::errors::InvalidArgument(
                          "Expects input tensor x to be not less than "
                          "2 dimentions, but got dimention %d",
                          x_rank));
50 51
    PADDLE_ENFORCE_GE(y_rank,
                      2,
52 53 54 55 56 57
                      platform::errors::InvalidArgument(
                          "Expects input tensor y to be not less than "
                          "2 dimentions, but got dimention %d",
                          y_rank));

    PADDLE_ENFORCE_EQ(
58 59
        x_rank,
        y_rank,
60 61 62
        platform::errors::InvalidArgument(
            "Expects input tensor x and y to have the same dimension "
            "but got x's dimention [%d] and y's dimention [%d]",
63 64
            x_rank,
            y_rank));
65 66 67 68

    std::vector<int> batch_dims_vec{};
    for (int i = 0; i < x_rank - 2; ++i) {
      PADDLE_ENFORCE_EQ(
69 70
          x_dims[i],
          y_dims[i],
71 72 73 74
          platform::errors::InvalidArgument(
              "Expects input tensor x and y to have the same batch "
              "dimension, but got x's batch dimention [%d] and "
              "y's batch dimention [%d] in %d-th dim",
75 76 77
              x_dims[i],
              y_dims[i],
              i));
78 79 80 81
      batch_dims_vec.emplace_back(x_dims[i]);
    }

    PADDLE_ENFORCE_EQ(
82 83
        x_dims[x_rank - 2],
        y_dims[y_rank - 2],
84 85 86 87
        platform::errors::InvalidArgument(
            "Expects input tensor x and y to have the same row dimension "
            "of the inner-most 2-dims matrix, "
            "but got x's row dimention [%d] and y's row dimention [%d]",
88 89
            x_dims[x_rank - 2],
            y_dims[y_rank - 2]));
90

91
    ctx->SetOutputDim("Rank", phi::make_ddim(batch_dims_vec));
92 93 94

    batch_dims_vec.emplace_back(
        std::min(x_dims[x_rank - 2], x_dims[x_rank - 1]));
95
    ctx->SetOutputDim("SingularValues", phi::make_ddim(batch_dims_vec));
96 97 98

    batch_dims_vec[x_rank - 2] = x_dims[x_rank - 1];
    batch_dims_vec.emplace_back(y_dims[x_rank - 1]);
99
    ctx->SetOutputDim("Solution", phi::make_ddim(batch_dims_vec));
100 101 102 103 104 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 141 142 143 144 145 146 147 148 149 150 151 152
  }

 protected:
  // The output of lstsq is always complex-valued even for real-valued inputs
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    auto dtype = OperatorWithKernel::IndicateVarDataType(ctx, "X");
    if (dtype != framework::proto::VarType::FP32 &&
        dtype != framework::proto::VarType::FP64) {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "unsupported data type: %s!", dtype));
    }
    return framework::OpKernelType(dtype, ctx.GetPlace());
  }
};

class LstsqOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X",
             "(Tensor), A real-valued tensor with shape (*, m, n). "
             "The accepted datatype is one of float32, float64");
    AddInput("Y",
             "(Tensor), A real-valued tensor with shape (*, m, k). "
             "The accepted datatype is one of float32, float64");
    AddAttr<float>(
        "rcond",
        "(float, default 0.0), A float value used to determine the effective "
        "rank of A.")
        .SetDefault(0.0f);
    AddAttr<std::string>("driver",
                         "(string, default \"gels\"). "
                         "name of the LAPACK method to be used.")
        .SetDefault("gels");
    AddOutput("Solution",
              "(Tensor), The output Solution tensor with shape (*, n, k).");
    AddOutput("Rank", "(Tensor), The output Rank tensor with shape (*).");
    AddOutput(
        "SingularValues",
        "(Tensor), The output SingularValues tensor with shape (*, min(m,n)).");
    AddComment(R"DOC(
        Lstsq Operator.
This API processes Lstsq functor for general matrices.
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(lstsq, ops::LstsqOp, ops::LstsqOpMaker)

L
Leo Chen 已提交
153 154 155
REGISTER_OP_CPU_KERNEL(lstsq,
                       ops::LstsqCPUKernel<phi::CPUContext, float>,
                       ops::LstsqCPUKernel<phi::CPUContext, double>);