random_routing_op.cc 3.9 KB
Newer Older
R
Roc 已提交
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
// Copyright (c) 2022 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/random_routing_op.h"

namespace paddle {
namespace operators {

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

  void InferShape(framework::InferShapeContext* ctx) const override {
    OP_INOUT_CHECK(ctx->HasInput("Prob"), "Input", "Porb", "RandomRouting");
26 27 28 29
    OP_INOUT_CHECK(
        ctx->HasInput("TopK_Value"), "Input", "TopKValue", "RandomRouting");
    OP_INOUT_CHECK(
        ctx->HasInput("TopK_Idx"), "Input", "TopKIdx", "RandomRouting");
R
Roc 已提交
30 31 32 33 34 35 36 37
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "RandomRouting");

    // check dims

    auto topk_val_dims = ctx->GetInputDim("TopK_Value");
    auto prob_dims = ctx->GetInputDim("Prob");
    auto topk_idx_dims = ctx->GetInputDim("TopK_Idx");

38 39
    PADDLE_ENFORCE_EQ(prob_dims[0],
                      topk_val_dims[0],
R
Roc 已提交
40 41 42
                      platform::errors::InvalidArgument(
                          "Output(Out) of ScatterNdAddOp should not be null."));

43 44
    PADDLE_ENFORCE_EQ(topk_idx_dims[1],
                      topk_val_dims[1],
R
Roc 已提交
45 46 47
                      platform::errors::InvalidArgument(
                          "Output(Out) of ScatterNdAddOp should not be null."));

48 49
    PADDLE_ENFORCE_EQ(topk_idx_dims[0],
                      topk_val_dims[0],
R
Roc 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
                      platform::errors::InvalidArgument(
                          "Output(Out) of ScatterNdAddOp should not be null."));

    ctx->SetOutputDim("Out", topk_idx_dims);
    ctx->ShareLoD("TopK_Idx", /*->*/ "Out");
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    // the dtype of the gate_idx should be same as int64
    const auto topk_idx_dtype =
        OperatorWithKernel::IndicateVarDataType(ctx, "TopK_Idx");
63 64
    PADDLE_ENFORCE_EQ(topk_idx_dtype,
                      framework::proto::VarType::INT64,
R
Roc 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
                      platform::errors::InvalidArgument(
                          "The dtype of the topk_idx_dtype should be int64"));

    const auto& topk_value_type =
        OperatorWithKernel::IndicateVarDataType(ctx, "TopK_Value");
    return framework::OpKernelType(topk_value_type, ctx.GetPlace());
  }
};

class RandomRoutingOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("Prob", "(Tensor) The input Prob index tensor.");
    AddInput("TopK_Value", "(Tensor) The input TopK_Value index tensor.");
    AddInput("TopK_Idx", "(Tensor) The input TopK_Idx index tensor.");
    AddOutput("Out", "(Tensor) The output random routing tensor.");
    AddComment(R"DOC(expert_count Operator random routing.)DOC");
  }
};

DECLARE_INPLACE_OP_INFERER(RandomRoutingInplaceInferer, {"TopK_Idx", "Out"});

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;

REGISTER_OPERATOR(
94 95 96
    random_routing,
    ops::RandomRoutingOp,
    ops::RandomRoutingOpMaker,
R
Roc 已提交
97 98 99 100
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    ops::RandomRoutingInplaceInferer)

101 102
REGISTER_OP_CPU_KERNEL(random_routing,
                       ops::RandomRoutingOpCPUKernel<float>,
R
Roc 已提交
103
                       ops::RandomRoutingOpCPUKernel<double>);