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_transpose_check.cpp
13 : * \brief
14 : */
15 :
16 : #include "kernel_check_params.h"
17 : #include "kernel_vec_transpose_check.h"
18 :
19 : namespace AscendC {
20 : namespace check {
21 : constexpr const uint32_t BLOCK_COUNT_16 = 16;
22 : constexpr const uint32_t BLOCK_COUNT_32 = 8;
23 :
24 16 : bool TikcppVecTransposeCheck::CheckAddrAlign()
25 : {
26 32 : bool dstRes = CheckTensorAddrAlign(param_.dstAddr, param_.dstPos, ONE_BLK_SIZE, "dst");
27 32 : bool srcRes = CheckTensorAddrAlign(param_.srcAddr, param_.srcPos, ONE_BLK_SIZE, "src");
28 16 : return dstRes && srcRes;
29 : }
30 :
31 8 : bool TikcppVecTransposeCheck::CheckTempTensorSizeOverflow() const
32 : {
33 8 : uint64_t needSize = 0;
34 8 : uint32_t baseSize = 0;
35 8 : uint8_t coef = 2;
36 :
37 8 : if (param_.transposeType == TransposeType::TRANSPOSE_NCHW2NHWC) {
38 0 : baseSize = param_.cSize + coef;
39 8 : } else if (param_.transposeType == TransposeType::TRANSPOSE_NHWC2NCHW) {
40 0 : baseSize = param_.cSize * coef + 1;
41 : }
42 8 : if (param_.srcDtypeBytes == sizeof(uint8_t)) { // int8_t / uint8_t
43 0 : needSize = baseSize * ONE_BLK_SIZE * ONE_BLK_SIZE;
44 8 : } else if (param_.srcDtypeBytes == sizeof(uint16_t) || param_.srcDtypeBytes == sizeof(uint32_t)) {
45 8 : needSize = baseSize * BLOCK_COUNT_16 * ONE_BLK_SIZE;
46 : }
47 32 : ASCENDC_CHECK(CheckTensorSizeOverflow(needSize, param_.tmpBufferSize, "sharedTmpBuffer", "Transpose"));
48 8 : return true;
49 : }
50 :
51 0 : bool TikcppVecTransposeCheck::TransdataCheckTensorSize()
52 : {
53 0 : if (param_.repeatTimes == 0) {
54 0 : return true;
55 : }
56 0 : uint64_t srcExpectedSize = param_.repeatTimes * param_.srcRepeatStride * ONE_BLK_SIZE + ONE_BLK_SIZE;
57 0 : uint64_t dstExpectedSize = param_.repeatTimes * param_.dstRepeatStride * ONE_BLK_SIZE + ONE_BLK_SIZE;
58 0 : std::string tensorName = (param_.index >= 0) ? std::to_string(param_.index) + "Local" : "Local";
59 0 : ASCENDC_CHECK(CheckTensorSizeOverflow(srcExpectedSize, param_.srcSize, "src" + tensorName, "TransDataTo5HD"));
60 0 : ASCENDC_CHECK(CheckTensorSizeOverflow(dstExpectedSize, param_.dstSize, "dst" + tensorName, "TransDataTo5HD"));
61 0 : return true;
62 0 : }
63 :
64 20 : bool TikcppVecTransposeCheck::CheckAllLowLevel()
65 : {
66 20 : const std::string supportPos = "VECIN/VECOUT/VECCALC";
67 60 : ASCENDC_CHECK(CheckTensorScope(param_.dstLogicPos, static_cast<uint8_t>(HardWareIndex::UB), "dst", supportPos));
68 48 : ASCENDC_CHECK(CheckTensorScope(param_.srcLogicPos, static_cast<uint8_t>(HardWareIndex::UB), "src", supportPos));
69 16 : ASCENDC_CHECK(CheckAddrAlign());
70 48 : ASCENDC_CHECK(CheckBufferSizeOverFlow(
71 : param_.dstSize, GlobalParams::Instance().bufferSizeMap.at(param_.dstPos),
72 : "check dst tensor buffersize failed"));
73 48 : ASCENDC_CHECK(CheckBufferSizeOverFlow(
74 : param_.srcSize, GlobalParams::Instance().bufferSizeMap.at(param_.srcPos),
75 : "check src tensor buffersize failed"));
76 : #if defined(__NPU_ARCH__) && ((__NPU_ARCH__ != 3003) && (__NPU_ARCH__ != 3103) && (__NPU_ARCH__ != 3113))
77 16 : uint64_t expectedSize = 0;
78 16 : if (apiName == "Transpose") {
79 : #if defined(__NPU_ARCH__) && ((__NPU_ARCH__ == 2201) || (__NPU_ARCH__ == 3002) || (__NPU_ARCH__ == 3102) || \
80 : (__NPU_ARCH__ == 3510) || (__NPU_ARCH__ == 5102))
81 8 : ASCENDC_CHECK(CheckTempTensorSizeOverflow());
82 : #endif
83 16 : TransposeType transType = param_.transposeType;
84 16 : ASCENDC_CHECK_AND_LOG(
85 : (transType == TransposeType::TRANSPOSE_TYPE_NONE || transType == TransposeType::TRANSPOSE_ND2ND_B16 ||
86 : transType == TransposeType::TRANSPOSE_NCHW2NHWC || transType == TransposeType::TRANSPOSE_NHWC2NCHW),
87 : {
88 : CHECK_LOG_ERROR(
89 : "Failed to check transposeType when it only supports TRANSPOSE_TYPE_NONE, TRANSPOSE_ND2ND_B16, "
90 : "TRANSPOSE_NCHW2NHWC, TRANSPOSE_NHWC2NCHW in Transpose, while the current value is %u.",
91 : static_cast<uint32_t>(transType));
92 : });
93 16 : if (transType == TransposeType::TRANSPOSE_ND2ND_B16) {
94 4 : ASCENDC_CHECK_AND_LOG((param_.hSize == NCHW_CONV_ADDR_LIST_SIZE), {
95 : CHECK_LOG_ERROR(
96 : "Failed to check hSize "
97 : "value when transposeType is TRANSPOSE_ND2ND_B16 in Transpose, its valid value is 16, current "
98 : "value "
99 : "is %u.",
100 : param_.hSize);
101 : });
102 0 : ASCENDC_CHECK_AND_LOG((param_.wSize == NCHW_CONV_ADDR_LIST_SIZE), {
103 : CHECK_LOG_ERROR(
104 : "Failed to check wSize "
105 : "value when transposeType is TRANSPOSE_ND2ND_B16 in Transpose, its valid value is 16, current "
106 : "value "
107 : "is %u.",
108 : param_.wSize);
109 : });
110 : }
111 :
112 12 : if (transType == TransposeType::TRANSPOSE_ND2ND_B16) {
113 0 : expectedSize = VALUE_512; // 16 * 16 B16 matrix do transpose -> src and local both 512B
114 12 : } else if (transType == TransposeType::TRANSPOSE_NCHW2NHWC || transType == TransposeType::TRANSPOSE_NHWC2NCHW) {
115 0 : expectedSize = param_.nSize * param_.cSize * param_.hSize * param_.wSize * param_.srcDtypeBytes; // NCHW
116 : }
117 48 : ASCENDC_CHECK(CheckTensorSizeOverflow(expectedSize, param_.srcSize, "srcLocal", "Transpose"));
118 48 : ASCENDC_CHECK(CheckTensorSizeOverflow(expectedSize, param_.dstSize, "dstLocal", "Transpose"));
119 0 : } else if (apiName == "TransDataTo5HD") {
120 0 : if (param_.index != -1) { // api that directly pass tensors
121 0 : ASCENDC_CHECK(TransdataCheckTensorSize());
122 : } else { // api that passes uint64_t tensor that stores address
123 0 : expectedSize = 16 * param_.srcDtypeBytes; // tensor only needs to store 16 tensor address
124 0 : ASCENDC_CHECK(CheckTensorSizeOverflow(expectedSize, param_.srcSize, "srcLocal", "TransDataTo5HD"));
125 0 : ASCENDC_CHECK(CheckTensorSizeOverflow(expectedSize, param_.dstSize, "dstLocal", "TransDataTo5HD"));
126 : }
127 : }
128 : #endif
129 12 : return true;
130 20 : }
131 : } // namespace check
132 : } // namespace AscendC
|