* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#ifndef OPTIMIZE_PLATFORM_COMMON_GRAPH_PASS_POW_EQUIV_SUBSTITUTION_PASS_H
#define OPTIMIZE_PLATFORM_COMMON_GRAPH_PASS_POW_EQUIV_SUBSTITUTION_PASS_H
#include "optimize/graph_pass/base_graph_pass.h"
namespace optimize {
using SubstitutionFunc = std::function<af::Status(af::AscGraph &, const af::AscNodePtr &)>;
enum class PatternType : uint8_t {
kZero = 0U,
kHalf = 1U,
kNegHalf = 2U,
kOne = 3U,
kNegOne = 4U,
kTwo = 5U,
kNegTwo = 6U,
kThree = 7U,
kFour = 8U,
kNone = 100U
};
class PowEquivSubstitutionPass final : public BaseGraphPass {
public:
PowEquivSubstitutionPass() = default;
~PowEquivSubstitutionPass() override = default;
af::Status RunPass(af::AscGraph &graph) override;
private:
static std::vector<af::AscNodePtr> FilterPowNodes(af::AscGraph &graph);
static const std::unordered_map<PatternType, SubstitutionFunc> &GetGlobalSubstitutionMap();
static bool GetScalarInput(const af::AscNodePtr &pow_node, std::string &scalar_val);
static af::Status EnsureInputWithBrcIfNeeded(af::AscGraph &graph, const af::AscNodePtr &pow_node);
static af::Status RelinkPowInputToNode(const af::AscNodePtr &pow_node, const af::AscNodePtr &target_node,
const int32_t in_idx = 0);
static af::Status RelinkPowOutputToNode(const af::AscNodePtr &pow_node, const af::AscNodePtr &target_node);
static af::Status ReplaceWithScalarBrc(af::AscGraph &graph, const af::AscNodePtr &pow_node);
static af::Status ReplaceWithSqrt(af::AscGraph &graph, const af::AscNodePtr &pow_node);
static af::Status ReplaceWithInverseSqrt(af::AscGraph &graph, const af::AscNodePtr &pow_node);
static af::Status RemoveUseLessPow(af::AscGraph &graph, const af::AscNodePtr &pow_node);
static af::Status ReplaceWithInverseInput(af::AscGraph &graph, const af::AscNodePtr &pow_node);
static af::Status ReplaceWithMul(af::AscGraph &graph, const af::AscNodePtr &pow_node);
static af::Status ReplaceWithInverseMul(af::AscGraph &graph, const af::AscNodePtr &pow_node);
static af::Status ReplaceWithCube(af::AscGraph &graph, const af::AscNodePtr &pow_node);
static af::Status ReplaceWithFourthPower(af::AscGraph &graph, const af::AscNodePtr &pow_node);
};
}
#endif