// Copyright (c) 2024 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. #pragma once #include "paddle/ap/include/drr/node.h" #include "paddle/ap/include/drr/value.h" #include "paddle/ap/include/graph/node.h" #include "paddle/ap/include/graph/node_descriptor.h" namespace ap::drr { struct DrrNodeDescriptor { using DrrNode = drr::Node; using DrrNativeIrValue = ap::drr::NativeIrValue; using DrrPackedIrValue = ap::drr::PackedIrValue; using DrrNativeIrOp = ap::drr::NativeIrOp; using DrrPackedIrOp = ap::drr::PackedIrOp; using DrrOptPackedIrOp = ap::drr::OptPackedIrOp; using DrrNativeIrOpOperand = ap::drr::NativeIrOpOperand; using DrrPackedIrOpOperand = ap::drr::PackedIrOpOperand; using DrrOptPackedIrOpOperand = ap::drr::OptPackedIrOpOperand; using DrrNativeIrOpResult = ap::drr::NativeIrOpResult; using DrrPackedIrOpResult = ap::drr::PackedIrOpResult; using DrrOptPackedIrOpResult = ap::drr::OptPackedIrOpResult; std::string DebugId(const graph::Node& node) { const auto& opt_drr_node = node.Get(); if (opt_drr_node.HasError()) { return std::to_string(node.node_id().value()); } const auto& drr_node = opt_drr_node.GetOkValue(); return drr_node.Match( [&](const DrrNativeIrValue& ir_value) -> std::string { return ir_value->name; }, [&](const DrrPackedIrValue& ir_value) -> std::string { return ir_value->name; }, [&](const DrrNativeIrOp& ir_op) -> std::string { return ir_op->op_declare->op_name + "[" + ir_op->name + "]"; }, [&](const DrrPackedIrOp& ir_op) -> std::string { return ir_op->op_declare->op_name + "[" + ir_op->name + "]"; }, [&](const DrrOptPackedIrOp& ir_op) -> std::string { return std::string("opt-") + ir_op->op_declare->op_name + "[" + ir_op->name + "]"; }, [&](const DrrNativeIrOpOperand& ir_op_operand) -> std::string { return EdgeDebugId(node); }, [&](const DrrPackedIrOpOperand& ir_op_operand) -> std::string { return EdgeDebugId(node); }, [&](const DrrOptPackedIrOpOperand& ir_op_operand) -> std::string { return EdgeDebugId(node); }, [&](const DrrNativeIrOpResult& ir_op_result) -> std::string { return EdgeDebugId(node); }, [&](const DrrPackedIrOpResult& ir_op_result) -> std::string { return EdgeDebugId(node); }, [&](const DrrOptPackedIrOpResult& ir_op_result) -> std::string { return EdgeDebugId(node); }); } std::string EdgeDebugId(const graph::Node& node) { const auto& opt_src_and_dst = GetSrcAndDst(node); if (!opt_src_and_dst.has_value()) { return std::string("invalid_edge_") + std::to_string(node.node_id().value()); } const auto& [src, dst] = opt_src_and_dst.value(); return DebugId(src) + "->" + DebugId(dst); } struct SrcAndDst { graph::Node src; graph::Node dst; }; std::optional GetSrcAndDst(const graph::Node& node) { const auto& opt_src_and_dst = TryGetSrcAndDst(node); if (opt_src_and_dst.HasError()) { return std::nullopt; } return opt_src_and_dst.GetOkValue(); } adt::Result TryGetSrcAndDst(const graph::Node& node) { ADT_LET_CONST_REF(upstreams, node.UpstreamNodes()); ADT_LET_CONST_REF(downstreams, node.DownstreamNodes()); ADT_LET_CONST_REF(src, upstreams.Sole()); ADT_LET_CONST_REF(dst, downstreams.Sole()); return SrcAndDst{src, dst}; } adt::Result AttrsSatisfyIfBothAreOpsOrValues( const drr::Node& node, const graph::Node& drr_node) { return adt::errors::NotImplementedError{ "NodeDescriptor>::AttrSatisfy() not " "implemented"}; } }; } // namespace ap::drr namespace ap::graph { template <> struct NodeDescriptor> : public drr::DrrNodeDescriptor { }; } // namespace ap::graph