op_yaml_info_parser.cc 5.5 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
// 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.

#include "paddle/fluid/ir/interface/op_yaml_info_parser.h"

namespace paddle {
namespace dialect {

OpYamlInfoParser::OpYamlInfoParser(const OpInfoTuple& op_info_tuple)
    : op_info_tuple_(op_info_tuple) {
  parse();
}

H
hong 已提交
25
bool OpYamlInfoParser::IsTensorAttribute(size_t index) const {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  PADDLE_ENFORCE_LT(
      index,
      InputInfo().size(),
      phi::errors::OutOfRange("Input index [%d] large than op input size [d]",
                              index,
                              InputInfo().size()));

  return InputInfo()[index].is_mutable_attribute;
}

size_t OpYamlInfoParser::InputTensorNumber() const {
  return input_tensor_number_;
}

const std::string& OpYamlInfoParser::AttrTypeName(
    const std::string& name) const {
H
hong 已提交
42
  auto it = attr_info_.find(name);
43 44 45

  PADDLE_ENFORCE_NE(
      it,
H
hong 已提交
46
      attr_info_.end(),
47 48 49 50
      phi::errors::NotFound("Not found [%s] in attribute map", name));
  return it->second.type_name;
}

H
hong 已提交
51 52
const std::string& OpYamlInfoParser::TensorAttrTypeName(
    const std::string& name) const {
H
hong 已提交
53
  auto it = input_info_.find(name);
H
hong 已提交
54 55

  PADDLE_ENFORCE_NE(it,
H
hong 已提交
56
                    input_info_.end(),
H
hong 已提交
57 58 59 60 61 62 63 64 65
                    phi::errors::NotFound("Not found [%s] in input map", name));

  PADDLE_ENFORCE_EQ(
      it->second.is_mutable_attribute,
      true,
      phi::errors::PreconditionNotMet("[%s] MUST be a tensor attribute", name));
  return it->second.type_name;
}

H
hong 已提交
66 67 68 69 70 71 72
const std::vector<std::string>& OpYamlInfoParser::TensorParams(
    bool is_kernel) const {
  if (is_kernel) {
    return kernel_fn_tensor_params_;
  } else {
    return infer_meta_tensor_params_;
  }
73
}
H
hong 已提交
74 75 76 77 78 79 80
const std::vector<std::string>& OpYamlInfoParser::AttrParams(
    bool is_kernel) const {
  if (is_kernel) {
    return kernel_fn_attr_params_;
  } else {
    return infer_meta_attr_params_;
  }
81 82
}

H
hong 已提交
83 84 85 86
const OpRunTimeInfo& OpYamlInfoParser::OpRuntimeInfo() const {
  return std::get<3>(op_info_tuple_);
}

87 88 89 90
const std::map<std::string, int>& OpYamlInfoParser::InputName2Id() const {
  return input_name2id_;
}

91
const std::map<std::string, int>& OpYamlInfoParser::OutputName2Id() const {
92
  return output_name2id_;
93 94
}

95 96 97 98
const std::vector<int>& OpYamlInfoParser::NoNeedBufferIds() const {
  return no_need_buffer_ids_;
}

99
bool OpYamlInfoParser::HasInplace(const std::string& out_name) const {
Z
zhangbo9674 已提交
100
  auto& inplace_info = std::get<3>(op_info_tuple_).inplace;
101 102
  for (const auto& info : inplace_info) {
    if (out_name == info.first) {
103 104 105 106 107 108 109 110
      return true;
    }
  }
  return false;
}

const std::string& OpYamlInfoParser::InplaceName(
    const std::string& out_name) const {
Z
zhangbo9674 已提交
111
  auto& inplace_info = std::get<3>(op_info_tuple_).inplace;
112 113 114
  for (const auto& info : inplace_info) {
    if (out_name == info.first) {
      return info.second;
115 116 117 118
    }
  }
  PADDLE_THROW(phi::errors::PreconditionNotMet(
      "Can not find inplace input of [%s].", out_name));
H
hong 已提交
119 120
}

Z
zhangbo9674 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
bool OpYamlInfoParser::HasView(const std::string& out_name) const {
  auto& view_info = std::get<3>(op_info_tuple_).view;
  for (size_t i = 0; i < view_info.size(); i++) {
    if (out_name == view_info[i].first) {
      return true;
    }
  }
  return false;
}

const std::string& OpYamlInfoParser::ViewName(
    const std::string& out_name) const {
  auto& view_info = std::get<3>(op_info_tuple_).view;
  for (size_t i = 0; i < view_info.size(); i++) {
    if (out_name == view_info[i].first) {
      return view_info[i].second;
    }
  }
  PADDLE_THROW(phi::errors::PreconditionNotMet(
      "Can not find inplace input of [%s].", out_name));
}

143 144 145 146
void OpYamlInfoParser::parse() {
  auto input_info = std::get<0>(op_info_tuple_);

  for (size_t i = 0; i < input_info.size(); ++i) {
147
    input_name2id_[input_info[i].name] = i;
148 149
    input_name_list_.push_back(input_info[i].name);
    input_info_[input_info[i].name] = input_info[i];
150 151 152
    if (!input_info[i].is_mutable_attribute) {
      input_tensor_number_++;
    }
153 154 155
    if (input_info[i].no_need_buffer) {
      no_need_buffer_ids_.push_back(i);
    }
156 157 158
  }

  auto attribute_info = std::get<1>(op_info_tuple_);
159 160 161
  for (auto& info : attribute_info) {
    attribute_name_list_.push_back(info.name);
    attr_info_[info.name] = info;
162 163 164 165
  }

  auto output_info = std::get<2>(op_info_tuple_);
  for (size_t i = 0; i < output_info.size(); ++i) {
166
    output_name2id_[output_info[i].name] = i;
167
    output_name_list_.push_back(output_info[i].name);
H
hong 已提交
168
    output_info_[output_info[i].name] = output_info[i];
169 170 171 172 173
  }

  auto runtime_info = std::get<3>(op_info_tuple_);

  for (auto& name : runtime_info.infer_meta_param) {
174
    if (input_name2id_.count(name) && !input_info_[name].is_mutable_attribute) {
H
hong 已提交
175
      infer_meta_tensor_params_.push_back(name);
176
    } else {
H
hong 已提交
177
      infer_meta_attr_params_.push_back(name);
178 179 180 181
    }
  }

  for (auto& name : runtime_info.kernel_param) {
182
    if (input_name2id_.count(name) && !input_info_[name].is_mutable_attribute) {
H
hong 已提交
183
      kernel_fn_tensor_params_.push_back(name);
184
    } else {
H
hong 已提交
185
      kernel_fn_attr_params_.push_back(name);
186 187 188 189 190 191
    }
  }
}

}  // namespace dialect
}  // namespace paddle