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_mmad_check.cpp
13 : * \brief
14 : */
15 :
16 : #include "kernel_check_params.h"
17 : #include "kernel_mmad_check.h"
18 :
19 : namespace AscendC {
20 : namespace check {
21 36 : bool TikcppMmadCheck::CheckMmadParamsRange(const uint32_t num, const std::string& paramName) const
22 : {
23 36 : const uint32_t mmadParamRange = 4095; // m, n, k only has 12 bits, thus range [0, 4095]
24 36 : ASCENDC_CHECK_AND_LOG((num <= mmadParamRange), {
25 : CHECK_LOG_ERROR(
26 : "Failed to check %s value in %s, its valid range "
27 : "is 0 ~ 4095, current value is %u.",
28 : paramName.c_str(), apiName.c_str(), num);
29 : });
30 36 : return true;
31 : }
32 :
33 12 : bool TikcppMmadCheck::CheckMmadOverflow(const std::string& errMsg)
34 : {
35 12 : uint32_t needElementL0c = static_cast<uint32_t>(param_.m * param_.n * param_.dstDtypeBytes);
36 12 : uint32_t needElementL0a = static_cast<uint32_t>(param_.m * param_.k * param_.src0DtypeBytes);
37 12 : uint32_t needElementL0b = static_cast<uint32_t>(param_.n * param_.k * param_.src1DtypeBytes);
38 12 : uint32_t totalL0CSize = static_cast<uint32_t>(PlatFormParams::L0C_SIZE);
39 12 : uint32_t totalL1Size = static_cast<uint32_t>(PlatFormParams::L1_SIZE);
40 12 : if (needElementL0c > totalL0CSize) {
41 0 : CHECK_LOG_ERROR(
42 : "%s: "
43 : "needElementL0c(%u) is bigger than totalL0CSize(%u)",
44 : errMsg.c_str(), needElementL0c, totalL0CSize);
45 0 : return false;
46 : }
47 :
48 12 : if ((needElementL0b + needElementL0a) > totalL1Size) {
49 0 : CHECK_LOG_ERROR(
50 : "%s: "
51 : "needElementL0b(%u) + needElementL0a(%u) is bigger than totalL1Size(%u)",
52 : errMsg.c_str(), needElementL0b, needElementL0a, totalL1Size);
53 0 : return false;
54 : }
55 12 : return true;
56 : }
57 :
58 12 : bool TikcppMmadCheck::CheckAllHighLevel()
59 : {
60 60 : ASCENDC_CHECK(CheckTensorScope(param_.dstLogicPos, static_cast<uint8_t>(HardWareIndex::L0C), "dstLocal", "CO1"));
61 60 : ASCENDC_CHECK(CheckTensorScope(param_.src0LogicPos, static_cast<uint8_t>(HardWareIndex::L0A), "fmLocal", "A2"));
62 60 : ASCENDC_CHECK(CheckTensorScope(param_.src1LogicPos, static_cast<uint8_t>(HardWareIndex::L0B), "filterLocal", "B2"));
63 :
64 24 : ASCENDC_CHECK(CheckMmadParamsRange(param_.m, "m"));
65 24 : ASCENDC_CHECK(CheckMmadParamsRange(param_.n, "n"));
66 24 : ASCENDC_CHECK(CheckMmadParamsRange(param_.k, "k"));
67 :
68 : // check Mmad overflow
69 24 : ASCENDC_CHECK(CheckMmadOverflow("check mmad overflow failed"));
70 12 : return true;
71 : }
72 : } // namespace check
73 : } // namespace AscendC
|