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 kernel_vec_binary_check.cpp
13 : * \brief
14 : */
15 :
16 : #include "kernel_check_params.h"
17 : #include "kernel_vec_binary_check.h"
18 :
19 : namespace AscendC {
20 : namespace check {
21 :
22 0 : bool TikcppVecBinaryCheck::CheckCmpTensorOverflowHigh(
23 : const uint32_t dtypeSize, const uint64_t bufferSize, const uint32_t calCount, const std::string& tensorName)
24 : {
25 0 : uint64_t needSize = static_cast<uint64_t>(dtypeSize * calCount / 8); // 1 uint8 equal to 8 bits
26 0 : ASCENDC_CHECK(CheckTensorSizeOverflow(needSize, bufferSize, tensorName, apiName));
27 0 : return true;
28 : }
29 :
30 0 : static uint64_t CalculateNeededCmpTensorSize(const uint64_t repeatTimes, const uint64_t srcDtypeBytes)
31 : {
32 0 : if (repeatTimes == 0) {
33 0 : return 0;
34 : }
35 0 : ASCENDC_CHECK(srcDtypeBytes != 0);
36 0 : uint64_t maxOffset = repeatTimes * static_cast<uint64_t>(PlatFormParams::ONE_REP_BYTE_SIZE) / srcDtypeBytes /
37 : 8; // 1 uint8 equal to 8 bits
38 0 : return maxOffset;
39 : }
40 :
41 0 : bool TikcppVecBinaryCheck::CheckCmpTensorOverflowLowNorm(
42 : const TensorOverflowParams& params, const std::string& tensorName)
43 : {
44 0 : uint64_t maxOffset = CalculateNeededCmpTensorSize(params.repeatTimes, param_.src0DtypeBytes);
45 0 : ASCENDC_CHECK(CheckTensorSizeOverflow(maxOffset, params.bufferSize, tensorName, apiName, ModeType::NORM_MODE));
46 0 : return true;
47 : }
48 :
49 40 : bool TikcppVecBinaryCheck::CommonCheck()
50 : {
51 40 : const std::string supportPos = "VECIN/VECOUT/VECCALC";
52 120 : ASCENDC_CHECK(CheckTensorScope(param_.dstLogicPos, static_cast<uint8_t>(HardWareIndex::UB), "dst", supportPos));
53 96 : ASCENDC_CHECK(CheckTensorScope(param_.src0LogicPos, static_cast<uint8_t>(HardWareIndex::UB), "src0", supportPos));
54 96 : ASCENDC_CHECK(CheckTensorScope(param_.src1LogicPos, static_cast<uint8_t>(HardWareIndex::UB), "src1", supportPos));
55 :
56 96 : ASCENDC_CHECK(CheckBufferSizeOverFlow(
57 : param_.dstSize, GlobalParams::Instance().bufferSizeMap.at(param_.dstPos),
58 : "check dst tensor buffersize failed"));
59 96 : ASCENDC_CHECK(CheckBufferSizeOverFlow(
60 : param_.src0Size, GlobalParams::Instance().bufferSizeMap.at(param_.src0Pos),
61 : "check src0 tensor buffersize failed"));
62 96 : ASCENDC_CHECK(CheckBufferSizeOverFlow(
63 : param_.src1Size, GlobalParams::Instance().bufferSizeMap.at(param_.src1Pos),
64 : "check src1 tensor buffersize failed"));
65 32 : ASCENDC_CHECK(CheckAddrAlign());
66 32 : return true;
67 40 : }
68 :
69 20 : bool TikcppVecBinaryCheck::CheckAllLowLevel(std::vector<uint64_t> maskArray)
70 : {
71 20 : uint32_t maxByteLen = std::max(std::max(param_.dstDtypeBytes, param_.src0DtypeBytes), param_.src1DtypeBytes);
72 20 : ASCENDC_CHECK(UpdateMaskArrayAndCheck(maskArray, maxByteLen));
73 20 : ASCENDC_CHECK(CommonCheck());
74 :
75 : TensorOverflowParams params = {
76 16 : param_.dstSize,
77 16 : param_.dstDtypeBytes,
78 16 : static_cast<uint64_t>(param_.repeatTimes),
79 16 : static_cast<uint64_t>(param_.dstBlockStride),
80 16 : static_cast<uint64_t>(param_.dstRepeatStride),
81 16 : false};
82 : // check dst src0 and src1 tensor overflow
83 16 : if (apiName == "Compare" || apiName == "Compare operator") {
84 0 : ASCENDC_CHECK(CheckCmpTensorOverflowLowNorm(params, "dstLocal"));
85 : } else {
86 48 : ASCENDC_CHECK(CheckTensorOverflowLow(maskArray, params, "dstLocal"));
87 : }
88 12 : params = {
89 12 : param_.src0Size,
90 12 : param_.src0DtypeBytes,
91 12 : static_cast<uint64_t>(param_.repeatTimes),
92 12 : static_cast<uint64_t>(param_.src0BlockStride),
93 12 : static_cast<uint64_t>(param_.src0RepeatStride),
94 12 : false};
95 36 : ASCENDC_CHECK(CheckTensorOverflowLow(maskArray, params, "src0Local"));
96 12 : params = {
97 12 : param_.src1Size,
98 12 : param_.src1DtypeBytes,
99 12 : static_cast<uint64_t>(param_.repeatTimes),
100 12 : static_cast<uint64_t>(param_.src1BlockStride),
101 12 : static_cast<uint64_t>(param_.src1RepeatStride),
102 12 : false};
103 36 : ASCENDC_CHECK(CheckTensorOverflowLow(maskArray, params, "src1Local"));
104 :
105 12 : return true;
106 : }
107 :
108 20 : bool TikcppVecBinaryCheck::CheckAllHighLevel()
109 : {
110 20 : ASCENDC_CHECK(CommonCheck());
111 :
112 16 : if (apiName == "Compare" || apiName == "Compare operator") {
113 0 : ASCENDC_CHECK(CheckCmpTensorOverflowHigh(param_.dstDtypeBytes, param_.dstSize, param_.calCount, "dstLocal"));
114 : } else {
115 48 : ASCENDC_CHECK(CheckTensorOverflowHigh(param_.dstDtypeBytes, param_.dstSize, param_.calCount, "dstLocal"));
116 : }
117 36 : ASCENDC_CHECK(CheckTensorOverflowHigh(param_.src0DtypeBytes, param_.src0Size, param_.calCount, "src0Local"));
118 36 : ASCENDC_CHECK(CheckTensorOverflowHigh(param_.src1DtypeBytes, param_.src1Size, param_.calCount, "src1Local"));
119 12 : return true;
120 : }
121 :
122 32 : bool TikcppVecBinaryCheck::CheckAddrAlign()
123 : {
124 64 : bool dstRes = CheckTensorAddrAlign(param_.dstAddr, param_.dstPos, ONE_BLK_SIZE, "dst");
125 64 : bool src0Res = CheckTensorAddrAlign(param_.src0Addr, param_.src0Pos, ONE_BLK_SIZE, "src0");
126 64 : bool src1Res = CheckTensorAddrAlign(param_.src1Addr, param_.src1Pos, ONE_BLK_SIZE, "src1");
127 32 : return dstRes && src0Res && src1Res;
128 : }
129 : } // namespace check
130 : } // namespace AscendC
|