builtin_op.cc 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2023 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.

15
#include "paddle/ir/builtin_op.h"
16

17
#include "paddle/ir/builtin_attribute.h"
18 19
#include "paddle/ir/builtin_type.h"
#include "paddle/phi/core/enforce.h"
20 21

namespace ir {
22 23
const char *GetParameterOp::attributes_name[attributes_num] = {
    "parameter_name"};
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
void GetParameterOp::verify(const std::vector<ir::OpResult> &inputs,
                            const std::vector<ir::Type> &outputs,
                            const ir::AttributeMap &attributes) {
  VLOG(4) << "Verifying inputs, outputs and attributes for: GetParameterOp.";
  // Verify inputs type:
  if (inputs.size() != 0) {
    throw("The size of inputs must be equal to 0.");
  }
  // Verify outputs type:
  if (outputs.size() != 1) {
    throw("The size of outputs must be equal to 1.");
  }
  // Verify if attributes contain attribute name in attributes_name:
  if (!attributes.at("parameter_name").isa<StrAttribute>()) {
    throw("Type of attribute: parameter_name is not right.");
  }
}

43 44
const char *SetParameterOp::attributes_name[attributes_num] = {
    "parameter_name"};
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
void SetParameterOp::verify(const std::vector<ir::OpResult> &inputs,
                            const std::vector<ir::Type> &outputs,
                            const ir::AttributeMap &attributes) {
  VLOG(4) << "Verifying inputs, outputs and attributes for: SetParameterOp.";
  // Verify inputs type:
  if (inputs.size() != 1) {
    throw("The size of inputs must be equal to 1.");
  }
  // Verify outputs type:
  if (outputs.size() != 0) {
    throw("The size of outputs must be equal to 0.");
  }
  // Verify if attributes contain attribute name in attributes_name:
  if (!attributes.at("parameter_name").isa<StrAttribute>()) {
    throw("Type of attribute: parameter_name is not right.");
  }
}

64 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 94 95 96 97 98 99 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 153 154 155 156 157 158 159 160 161 162
void CombineOp::verify(const std::vector<ir::OpResult> &inputs,
                       const std::vector<ir::Type> &outputs,
                       const ir::AttributeMap &attributes) {
  // outputs.size() == 1
  PADDLE_ENFORCE_EQ(
      outputs.size(),
      1,
      phi::errors::PreconditionNotMet(
          "The size %d of outputs must be equal to 1.", outputs.size()));
  // outputs[0].type == Vector<Type>
  PADDLE_ENFORCE(outputs[0].isa<ir::VectorType>(),
                 phi::errors::PreconditionNotMet(
                     "The type %s of outputs[0] must be equal to VectorType.",
                     outputs[0]));
  ir::VectorType output_type = outputs[0].dyn_cast<ir::VectorType>();
  // inputs.size() == outputs[0].size()
  PADDLE_ENFORCE_EQ(
      output_type.size(),
      inputs.size(),
      phi::errors::PreconditionNotMet(
          "The size %d of outputs[0] must be equal to size %d of inputs.",
          output_type.size(),
          inputs.size()));

  // forall i in inputs.size(): inputs[i].type == outputs[0][i].type
  for (size_t i = 0; i < inputs.size(); i++) {
    PADDLE_ENFORCE_EQ(
        output_type[i],
        inputs[i].type(),
        phi::errors::PreconditionNotMet("The type %s of outputs[0][%d] must be "
                                        "equal to type %s of inputs[%d].",
                                        output_type[i],
                                        i,
                                        inputs[i].type(),
                                        i));
  }
}

const char *SliceOp::attributes_name[attributes_num] = {"index"};
void SliceOp::verify(const std::vector<ir::OpResult> &inputs,
                     const std::vector<ir::Type> &outputs,
                     const ir::AttributeMap &attributes) {
  // inputs.size() == 1
  PADDLE_ENFORCE_EQ(
      inputs.size(),
      1,
      phi::errors::PreconditionNotMet(
          "The size %d of inputs must be equal to 1.", inputs.size()));

  // inputs[0].type == Vector<Type>
  PADDLE_ENFORCE(inputs[0].type().isa<ir::VectorType>(),
                 phi::errors::PreconditionNotMet(
                     "The type %s of inputs[0] must be equal to VectorType.",
                     inputs[0].type()));
  ir::VectorType input_type = inputs[0].type().dyn_cast<ir::VectorType>();

  // outputs.size() == 1
  PADDLE_ENFORCE_EQ(
      outputs.size(),
      1,
      phi::errors::PreconditionNotMet(
          "The size %d of outputs must be equal to 1.", outputs.size()));

  // attributes contains index: Int32
  PADDLE_ENFORCE_NE(
      attributes.count("index"),
      0,
      phi::errors::PreconditionNotMet("The attributes must contains index."));
  const ir::Attribute &attr = attributes.at("index");
  PADDLE_ENFORCE(
      attr.isa<ir::Int32_tAttribute>(),
      phi::errors::PreconditionNotMet("The attribute index must be INT32."));
  auto index = attr.dyn_cast<ir::Int32_tAttribute>().data();

  // index >= 0 and < inputs[0].size()
  PADDLE_ENFORCE_GE(
      index,
      0,
      phi::errors::PreconditionNotMet(
          "The index %d must be greater or equal than 0.", index));
  PADDLE_ENFORCE_LT(
      index,
      input_type.size(),
      phi::errors::PreconditionNotMet(
          "The index %d must be less or equal than size %d of inputs[0].",
          index,
          input_type.size()));

  // inputs[index].type == outputs[0].type
  PADDLE_ENFORCE_EQ(
      input_type[index],
      outputs[0],
      phi::errors::PreconditionNotMet(
          "The type %s of inputs[%d] must be equal to type %s of outputs[0].",
          input_type[index],
          index,
          outputs[0]));
}

163
}  // namespace ir