已关闭
SoftplusV2Grad算子Ascend C实现 #415
cxj01创建于 2025年12月12日关闭于 5月12日
SoftplusV2Grad算子Ascend C实现 #415
已关闭
共 27 个文件变更+2270-0
| @@ -0,0 +1,19 @@ | |||
| 1 | +# ---------------------------------------------------------------------------- | ||
| 2 | +# This program is free software, you can redistribute it and/or modify. | ||
| 3 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 4 | +# This file is a part of the CANN Open Software. | ||
| 5 | +# Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 6 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 7 | +# 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. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +# ---------------------------------------------------------------------------- | ||
| 10 | + | ||
| 11 | +file(GLOB CURRENT_DIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*) | ||
| 12 | +if(NOT ENABLE_TEST) | ||
| 13 | + list(REMOVE_ITEM CURRENT_DIRS tests) | ||
| 14 | +endif() | ||
| 15 | +foreach(SUB_DIR ${CURRENT_DIRS}) | ||
| 16 | + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUB_DIR}/CMakeLists.txt") | ||
| 17 | + add_subdirectory(${SUB_DIR}) | ||
| 18 | + endif() | ||
| 19 | +endforeach() | ||
Aexperimental/activation/softplus_v2_grad/examples/test_aclnn_softplus_v2_grad.cpp+153-0文件内容审核中,请稍后刷新重试
Aexperimental/activation/softplus_v2_grad/op_host/op_api/aclnn_softplus_backward.cpp+169-0文件内容审核中,请稍后刷新重试
| @@ -0,0 +1,39 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +*/ | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +extern "C" { | ||
| 18 | + | ||
| 19 | + | ||
| 20 | +/** | ||
| 21 | + * @brief aclnnSoftplusBackward的第一段接口,根据具体的计算流程,计算workspace大小。 | ||
| 22 | + * @domain aclnn_ops_train | ||
| 23 | + */ | ||
| 24 | +ACLNN_API aclnnStatus aclnnSoftplusBackwardGetWorkspaceSize(const aclTensor* gradOutput, const aclTensor* self, | ||
| 25 | + const aclScalar* beta, const aclScalar* threshold, | ||
| 26 | + aclTensor* gradInput, uint64_t* workspaceSize, | ||
| 27 | + aclOpExecutor** executor); | ||
| 28 | + | ||
| 29 | +/** | ||
| 30 | + * @brief aclnnSoftplusBackward的第二段接口,用于执行计算。 | ||
| 31 | + */ | ||
| 32 | +ACLNN_API aclnnStatus aclnnSoftplusBackward(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, | ||
| 33 | + aclrtStream stream); | ||
| 34 | + | ||
| 35 | + | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + | ||
| @@ -0,0 +1,39 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +*/ | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +using namespace op; | ||
| 18 | + | ||
| 19 | +namespace l0op { | ||
| 20 | +OP_TYPE_REGISTER(SoftplusV2Grad); | ||
| 21 | + | ||
| 22 | +const aclTensor *SoftplusV2Grad(const aclTensor *grad_output, const aclTensor *self, | ||
| 23 | + float beta, float threshold, aclOpExecutor *executor) { | ||
| 24 | + // AICORE算子kernel | ||
| 25 | + L0_DFX(SoftplusV2Grad, grad_output, self, beta, threshold); | ||
| 26 | + | ||
| 27 | + auto grad_input = executor->AllocTensor(grad_output->GetViewShape(), grad_output->GetDataType()); | ||
| 28 | + CHECK_RET(grad_input != nullptr, nullptr); | ||
| 29 | + | ||
| 30 | + // 使用框架宏ADD_TO_LAUNCHER_LIST_AICORE,将AiCore SoftplusV2Grad算子加入任务队列 | ||
| 31 | + auto ret = ADD_TO_LAUNCHER_LIST_AICORE(SoftplusV2Grad, | ||
| 32 | + OP_INPUT(grad_output, self), | ||
| 33 | + OP_OUTPUT(grad_input), | ||
| 34 | + OP_ATTR(beta, threshold)); | ||
| 35 | + OP_CHECK(ret == ACLNN_SUCCESS, OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "SoftplusV2GradAiCore ADD_TO_LAUNCHER_LIST_AICORE failed."), | ||
| 36 | + return nullptr); | ||
| 37 | + return grad_input; | ||
| 38 | +} | ||
| 39 | +} // namespace l0op | ||
| @@ -0,0 +1,20 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +*/ | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
| 15 | +namespace l0op { | ||
| 16 | +const aclTensor *SoftplusV2Grad(const aclTensor *grad_output, const aclTensor *self, | ||
| 17 | + float beta, float threshold, aclOpExecutor *executor); | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | + | ||
Aexperimental/activation/softplus_v2_grad/op_host/softplus_v2_grad_infershape.cpp+38-0文件内容审核中,请稍后刷新重试
Aexperimental/activation/softplus_v2_grad/op_kernel/softplus_v2_grad_tiling_data.h+41-0文件内容审核中,请稍后刷新重试
Aexperimental/activation/softplus_v2_grad/op_kernel/softplus_v2_grad_tiling_key.h+32-0文件内容审核中,请稍后刷新重试
Aexperimental/activation/softplus_v2_grad/tests/ut/op_host/softplus_v2_grad_tiling.h+30-0文件内容审核中,请稍后刷新重试
Aexperimental/activation/softplus_v2_grad/tests/ut/op_kernel/softplus_v2_grad_data/compare_data.py+57-0
| @@ -0,0 +1,57 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# This program is free software, you can redistribute it and/or modify it. | ||
| 5 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 6 | +# This file is a part of the CANN Open Software. | ||
| 7 | +# Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 8 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 9 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING | ||
| 10 | +# BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. See LICENSE in the root of | ||
| 11 | +# the software repository for the full text of the License. | ||
| 12 | +# ---------------------------------------------------------------------------- | ||
| 13 | + | ||
| 14 | +import sys | ||
| 15 | +import numpy as np | ||
| 16 | +import glob | ||
| 17 | +import os | ||
| 18 | + | ||
| 19 | +curr_dir = os.path.dirname(os.path.realpath(__file__)) | ||
| 20 | + | ||
| 21 | +def compare_data(golden_file_lists, output_file_lists, d_type): | ||
| 22 | + if d_type == "float16": | ||
| 23 | + np_dtype = np.float16 | ||
| 24 | + elif d_type == "float32": | ||
| 25 | + np_dtype = np.float32 | ||
| 26 | + else: | ||
| 27 | + raise ValueError("d_type must be float16 or float32") | ||
| 28 | + | ||
| 29 | + data_same = True | ||
| 30 | + for gold, out in zip(golden_file_lists, output_file_lists): | ||
| 31 | + tmp_out = np.fromfile(out, np_dtype) | ||
| 32 | + tmp_gold = np.fromfile(gold, np_dtype) | ||
| 33 | + diff_res = np.isclose(tmp_out, tmp_gold, 0, 0, True) | ||
| 34 | + diff_idx = np.where(diff_res != True)[0] | ||
| 35 | + if len(diff_idx) == 0: | ||
| 36 | + print("PASSED!") | ||
| 37 | + else: | ||
| 38 | + print("FAILED!") | ||
| 39 | + for idx in diff_idx[:5]: | ||
| 40 | + print(f"index: {idx}, output: {tmp_out[idx]}, golden: {tmp_gold[idx]}") | ||
| 41 | + data_same = False | ||
| 42 | + return data_same | ||
| 43 | + | ||
| 44 | +def get_file_lists(dtype): | ||
| 45 | + golden_file_lists = sorted(glob.glob(curr_dir + "/*golden*.bin")) | ||
| 46 | + output_file_lists = sorted(glob.glob(curr_dir + "/*output*.bin")) | ||
| 47 | + return golden_file_lists, output_file_lists | ||
| 48 | + | ||
| 49 | +def process(d_type): | ||
| 50 | + golden_file_lists, output_file_lists = get_file_lists(d_type) | ||
| 51 | + result = compare_data(golden_file_lists, output_file_lists, d_type) | ||
| 52 | + print("compare result:", result) | ||
| 53 | + return result | ||
| 54 | + | ||
| 55 | +if __name__ == '__main__': | ||
| 56 | + ret = process(sys.argv[1]) | ||
| 57 | + exit(0 if ret else 1) | ||
Aexperimental/activation/softplus_v2_grad/tests/ut/op_kernel/test_softplus_v2_grad.cpp+202-0文件内容审核中,请稍后刷新重试