text: /* 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/compiler/paddle2piano/piano_op_kernel_context.h"
  
  #include <algorithm>
  
  #include "paddle/fluid/platform/enforce.h"
  
  namespace paddle {
  namespace piano {
  
  bool PianoOpKernelContext::HasAttr(const std::string& name) const {
    const auto& attrs = PianoOpRegistry::Attrs(Type());
    if (attrs.find(name) != attrs.end()) {
      return true;
    }
    return op_->HasAttr(name);
  }
  
  framework::Attribute PianoOpKernelContext::GetAttr(
      const std::string& name) const {
    PADDLE_ENFORCE_EQ(
        HasAttr(name), true,
        platform::errors::NotFound("Attribute %s is not found in op %s.",
                                   name.c_str(), op_->Type().c_str()));
  
    const auto& attrs = PianoOpRegistry::Attrs(Type());
    auto it = attrs.find(name);
    if (it != attrs.end()) {
      return it->second;
    }
    return op_->GetAttr(name);
  }
  
  symbolization::Operand PianoOpKernelContext::GetInput(
      const std::string& name) const {
    PADDLE_ENFORCE_EQ(
        HasInput(name), true,
        platform::errors::NotFound("Input %s is not found in op %s.",
                                   name.c_str(), op_->Type().c_str()));
    const std::vector<std::string>& input_args = op_->Input(name);
    PADDLE_ENFORCE_EQ(input_args.size(), 1,
                      platform::errors::InvalidArgument(
                          "Th input %s of op %s should has just 1 argument, "
                          "but here %zd",
                          name.c_str(), op_->Type().c_str(), input_args.size()));
    return scope_->GetOperand(input_args.at(0));
  }
  
  void PianoOpKernelContext::SetOutput(const std::string& name,
                                       const symbolization::Operand& op) const {
    PADDLE_ENFORCE_EQ(
        op_->HasOutput(name), true,
        platform::errors::NotFound("Output %s is not found in op %s.",
                                   name.c_str(), op_->Type().c_str()));
    const std::vector<std::string>& output_args = op_->Output(name);
    PADDLE_ENFORCE_EQ(output_args.size(), 1,
                      platform::errors::InvalidArgument(
                          "Th output %s of op %s should has just 1 argument, "
                          "but here %zd",
                          name.c_str(), op_->Type().c_str(), output_args.size()));
    scope_->SetOperand(output_args.at(0), op);
  }
  
  }  // namespace piano
  }  // namespace paddle
  