LCOV - code coverage report
Current view: top level - ut/activation/swish_grad/op_host/arch35 - swish_grad_tiling_arch35.cpp Coverage Total Hit
Test: CHG Lines: 0.0 % 1 0
Test Date: 2026-08-26 16:41:30
Legend: Lines: hit not hit

            Line data    Source code
       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              :  * \file swish_grad_regbase_optiling.cc
      13              :  * \brief
      14              :  */
      15              : #include "swish_grad_tiling_arch35.h"
      16              : #include <graph/utils/type_utils.h>
      17              : #include "tiling/tiling_api.h"
      18              : #include "register/op_def_registry.h"
      19              : #include "register/op_impl_registry.h"
      20              : #include "log/log.h"
      21              : #include "register/tilingdata_base.h"
      22              : #include "atvoss/elewise/elewise_tiling.h"
      23              : #include "atvoss/broadcast/broadcast_tiling.h"
      24              : #include "activation/swish_grad/op_kernel/arch35/swish_grad_dag.h"
      25              : #include "activation/swish_grad/op_kernel/arch35/swish_grad_struct.h"
      26              : 
      27              : #include <iostream>
      28              : 
      29              : namespace optiling {
      30              : const size_t ASCEND_WORKSPACE = 16777216; // 16M
      31              : 
      32              : ge::graphStatus SwishGradTiling::SetTilingData() const
      33              : {
      34              :     size_t* currentWorkspace = tilingContext->GetWorkspaceSizes(1);
      35              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, currentWorkspace);
      36              :     currentWorkspace[0] = ASCEND_WORKSPACE;
      37              : 
      38              :     const uint64_t tilingKey = GET_TPL_TILING_KEY(tiling->baseTiling.scheMode, dType);
      39              :     OP_LOGD(tilingContext->GetNodeName(), "[TilingData] : tilingKey=%lu", tilingKey);
      40              :     tilingContext->SetTilingKey(tilingKey);
      41              :     tilingContext->SetBlockDim(tiling->baseTiling.blockNum);
      42              :     return ge::GRAPH_SUCCESS;
      43              : }
      44              : 
      45              : ge::graphStatus SwishGradTiling::CalcInputDtype()
      46              : {
      47              :     auto inputDesc = tilingContext->GetInputDesc(0);
      48              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputDesc);
      49              :     this->inputDtype = inputDesc->GetDataType();
      50              :     OP_CHECK_IF(
      51              :         this->inputDtype != ge::DT_FLOAT16 && this->inputDtype != ge::DT_BF16 && this->inputDtype != ge::DT_FLOAT,
      52              :         OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "grad",
      53              :                                               ge::TypeUtils::DataTypeToSerialString(this->inputDtype),
      54              :                                               "The dtype of grad must be DT_FLOAT16, DT_BF16 or DT_FLOAT"),
      55              :         return ge::GRAPH_FAILED);
      56              :     auto inputDesc1 = tilingContext->GetInputDesc(1);
      57              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputDesc1);
      58              :     this->inputDtype1 = inputDesc1->GetDataType();
      59              :     OP_CHECK_IF(this->inputDtype1 != this->inputDtype,
      60              :                 OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(tilingContext->GetNodeName(), "x, grad",
      61              :                                                        ge::TypeUtils::DataTypeToSerialString(this->inputDtype) + ", " +
      62              :                                                            ge::TypeUtils::DataTypeToSerialString(this->inputDtype1),
      63              :                                                        "The dtypes of x and grad must be the same"),
      64              :                 return ge::GRAPH_FAILED);
      65              :     auto inputDesc2 = tilingContext->GetInputDesc(2);
      66              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputDesc2);
      67              :     this->inputDtype2 = inputDesc2->GetDataType();
      68              :     OP_CHECK_IF(this->inputDtype2 != this->inputDtype,
      69              :                 OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(tilingContext->GetNodeName(), "y, grad",
      70              :                                                        ge::TypeUtils::DataTypeToSerialString(this->inputDtype) + ", " +
      71              :                                                            ge::TypeUtils::DataTypeToSerialString(this->inputDtype2),
      72              :                                                        "The dtypes of y and grad must be the same"),
      73              :                 return ge::GRAPH_FAILED);
      74              :     return ge::GRAPH_SUCCESS;
      75              : }
      76              : 
      77              : ge::graphStatus SwishGradTiling::CheckShape()
      78              : {
      79              :     auto gradStorageShape = tilingContext->GetInputShape(0);
      80              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, gradStorageShape);
      81              :     const gert::Shape& inputGradShape = Ops::Base::EnsureNotScalar(gradStorageShape->GetStorageShape());
      82              :     auto xStorageShape = tilingContext->GetInputShape(1);
      83              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, xStorageShape);
      84              :     const gert::Shape& inputXShape = Ops::Base::EnsureNotScalar(xStorageShape->GetStorageShape());
      85              :     auto yStorageShape = tilingContext->GetInputShape(2);
      86              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, yStorageShape);
      87              :     const gert::Shape& inputYShape = Ops::Base::EnsureNotScalar(yStorageShape->GetStorageShape());
      88              : 
      89              :     auto gradXStorageShape = tilingContext->GetOutputShape(0);
      90              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, gradXStorageShape);
      91              :     const gert::Shape& outputGradXShape = Ops::Base::EnsureNotScalar(gradXStorageShape->GetStorageShape());
      92              : 
      93              :     OP_CHECK_IF(inputGradShape != inputXShape,
      94              :                 OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
      95              :                     tilingContext->GetNodeName(), "grad, x",
      96              :                     Ops::Base::ToString(inputGradShape) + ", " + Ops::Base::ToString(inputXShape),
      97              :                     "The shapes of grad and x must be the same"),
      98              :                 return ge::GRAPH_FAILED);
      99              :     OP_CHECK_IF(inputGradShape != inputYShape,
     100              :                 OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
     101              :                     tilingContext->GetNodeName(), "grad, y",
     102              :                     Ops::Base::ToString(inputGradShape) + ", " + Ops::Base::ToString(inputYShape),
     103              :                     "The shapes of grad and y must be the same"),
     104              :                 return ge::GRAPH_FAILED);
     105              :     OP_CHECK_IF(inputGradShape != outputGradXShape,
     106              :                 OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
     107              :                     tilingContext->GetNodeName(), "grad, grad_x",
     108              :                     Ops::Base::ToString(inputGradShape) + ", " + Ops::Base::ToString(outputGradXShape),
     109              :                     "The shapes of grad and grad_x must be the same"),
     110              :                 return ge::GRAPH_FAILED);
     111              :     return ge::GRAPH_SUCCESS;
     112              : }
     113              : 
     114              : ge::graphStatus SwishGradTiling::CalcOutputDtype()
     115              : {
     116              :     auto outputDesc = tilingContext->GetOutputDesc(0);
     117              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outputDesc);
     118              :     this->outputDtype = outputDesc->GetDataType();
     119              :     OP_CHECK_IF(this->outputDtype != this->inputDtype,
     120              :                 OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(tilingContext->GetNodeName(), "grad_x, grad",
     121              :                                                        ge::TypeUtils::DataTypeToSerialString(this->outputDtype) + ", " +
     122              :                                                            ge::TypeUtils::DataTypeToSerialString(this->inputDtype),
     123              :                                                        "The dtypes of grad_x and grad must be the same"),
     124              :                 return ge::GRAPH_FAILED);
     125              :     return ge::GRAPH_SUCCESS;
     126              : }
     127              : 
     128              : ge::graphStatus SwishGradTiling::SetAttr()
     129              : {
     130              :     auto attrs = tilingContext->GetAttrs();
     131              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, attrs);
     132              :     const float* scaleValueAttr = attrs->GetAttrPointer<float>(0);
     133              :     float scaleValue = scaleValueAttr == nullptr ? 1.0f : *scaleValueAttr;
     134              :     tiling->scale = scaleValue;
     135              : 
     136              :     return ge::GRAPH_SUCCESS;
     137              : }
     138              : 
     139              : ge::graphStatus SwishGradTiling::RunTiling()
     140              : {
     141              :     ElewiseBaseTiling elewiseBaseTiling(tilingContext);
     142              :     OP_CHECK_IF(CalcInputDtype() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "get input dtype failed"),
     143              :                 return ge::GRAPH_FAILED);
     144              :     OP_CHECK_IF(CalcOutputDtype() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "get output dtype failed"),
     145              :                 return ge::GRAPH_FAILED);
     146              :     OP_CHECK_IF(CheckShape() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "check shape failed"),
     147              :                 return ge::GRAPH_FAILED);
     148              : 
     149              :     // get tilingdata address in context
     150              :     tiling = tilingContext->GetTilingData<SwishGradTilingData>();
     151              :     OP_CHECK_NULL_WITH_CONTEXT(tilingContext, tiling);
     152              : 
     153              :     OP_CHECK_IF(SetAttr() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "set Attr failed"), return ge::GRAPH_FAILED);
     154              : 
     155              :     ge::graphStatus baseTilingResult = ge::GRAPH_FAILED;
     156              :     if (this->outputDtype == ge::DT_FLOAT16) {
     157              :         dType = TPL_FP16;
     158              :         baseTilingResult = elewiseBaseTiling.DoTiling<SwishGradOp::SwishGradDAG<half>::OpDag>(tiling->baseTiling);
     159              :     } else if (this->outputDtype == ge::DT_BF16) {
     160              :         dType = TPL_BF16;
     161              :         baseTilingResult = elewiseBaseTiling.DoTiling<SwishGradOp::SwishGradDAG<bfloat16_t>::OpDag>(tiling->baseTiling);
     162              :     } else if (this->outputDtype == ge::DT_FLOAT) {
     163              :         dType = TPL_FP32;
     164              :         baseTilingResult = elewiseBaseTiling.DoTiling<SwishGradOp::SwishGradDAG<float>::OpDag>(tiling->baseTiling);
     165              :     } else {
     166              :         OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "grad_x",
     167              :                                               ge::TypeUtils::DataTypeToSerialString(this->outputDtype),
     168              :                                               "The dtype of grad_x must be DT_FLOAT16, DT_BF16 or DT_FLOAT");
     169              :         return ge::GRAPH_FAILED;
     170              :     }
     171            0 :     OP_CHECK_IF(baseTilingResult == ge::GRAPH_FAILED,
     172              :                 OP_LOGE(tilingContext->GetNodeName(), "elewiseBaseTiling failed, output dtype: %s.",
     173              :                         ge::TypeUtils::DataTypeToSerialString(this->outputDtype).c_str()),
     174              :                 return ge::GRAPH_FAILED);
     175              : 
     176              :     return SetTilingData();
     177              : }
     178              : 
     179              : static ge::graphStatus Tiling4SwishGrad(gert::TilingContext* tilingContextGen)
     180              : {
     181              :     OP_LOGD(tilingContextGen->GetNodeName(), "Tiling4SwishGrad rt2.0 is running.");
     182              : 
     183              :     SwishGradTiling baseOpTiling(tilingContextGen);
     184              :     return baseOpTiling.RunTiling();
     185              : }
     186              : 
     187              : static ge::graphStatus TilingPrepareForSwishGrad([[maybe_unused]] gert::TilingParseContext* context)
     188              : {
     189              :     return ge::GRAPH_SUCCESS;
     190              : }
     191              : 
     192              : IMPL_OP_OPTILING(SwishGrad).Tiling(Tiling4SwishGrad).TilingParse<ElewiseCompileInfo>(TilingPrepareForSwishGrad);
     193              : } // namespace optiling
        

Generated by: LCOV version 2.0-1