/** * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved. * This file is a part of the CANN Open Software. * Licensed under 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. */ /*! * \file cou_context_util.h * \brief */ #ifndef CPU_CONTEXT_UTIL_H #define CPU_CONTEXT_UTIL_H #include "cpu_context.h" #include "cpu_kernel.h" #include "cpu_tensor.h" #include "log.h" #include #include #define KERNEL_STATUS_OK 0 #define KERNEL_STATUS_PARAM_INVALID 1 namespace aicpu { template inline typename std::enable_if, bool>::type GetAttrValue(CpuKernelContext &ctx, const std::string &name, T &value) { auto attr = ctx.GetAttr(name); if (!attr) { KERNEL_LOG_ERROR("attr is null: %s", name.c_str()); return false; } value = static_cast(attr->GetInt()); return true; } inline bool GetAttrValue(CpuKernelContext &ctx, const std::string &name, std::string &value) { auto attr = ctx.GetAttr(name); if (!attr) { KERNEL_LOG_ERROR("attr is null: %s", name.c_str()); return false; } value = attr->GetString(); return true; } inline bool GetAttrValue(CpuKernelContext &ctx, const std::string &name, bool &value) { auto attr = ctx.GetAttr(name); if (!attr) { KERNEL_LOG_ERROR("attr is null: %s", name.c_str()); return false; } value = attr->GetBool(); return true; } template inline typename std::enable_if, void>::type GetAttrValueOpt(CpuKernelContext &ctx, const std::string &name, T &value) { auto attr = ctx.GetAttr(name); if (attr != nullptr) { value = static_cast(attr->GetInt()); } } inline void GetAttrValueOpt(CpuKernelContext &ctx, const std::string &name, std::string &value) { auto attr = ctx.GetAttr(name); if (attr != nullptr) { value = attr->GetString(); } } inline void GetAttrValueOpt(CpuKernelContext &ctx, const std::string &name, bool &value) { auto attr = ctx.GetAttr(name); if (attr != nullptr) { value = attr->GetBool(); } } } // namespace aicpu #endif