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_utils.h
13 : * \brief
14 : */
15 : #ifndef ASCENDC_MODULE_UTILS_H
16 : #define ASCENDC_MODULE_UTILS_H
17 : #include "utils/kernel_utils_ceil_oom_que.h"
18 : #include "utils/kernel_utils_constants.h"
19 : #include "utils/kernel_utils_struct_norm_sort.h"
20 : #include "utils/kernel_utils_mode_cpu.h"
21 : #include "utils/kernel_utils_ceil_oom_que.h"
22 : #include "utils/kernel_utils_constants.h"
23 : #include "utils/kernel_utils_struct_norm_sort.h"
24 :
25 : namespace AscendC {
26 : #if defined(__NPU_ARCH__) && \
27 : ((__NPU_ARCH__ == 2201) || (__NPU_ARCH__ == 3002) || (__NPU_ARCH__ == 3510) || (__NPU_ARCH__ == 5102))
28 :
29 : #if defined(__NPU_ARCH__) && ((__NPU_ARCH__ == 3510) || (__NPU_ARCH__ == 5102))
30 :
31 : namespace FPTranslation {
32 : // HiFloat8 -> Fp32
33 : #define HIF8_SIGN_INDEX (7)
34 : #define HIF8_BIT6_INDEX (6)
35 : #define HIF8_BIT5_INDEX (5)
36 : #define HIF8_BIT4_INDEX (4)
37 : #define HIF8_BIT3_INDEX (3)
38 : #define HIF8_EXTRACT_SIGN(x) (((x) >> HIF8_SIGN_INDEX) & 0x1)
39 : #define HIF8_EXTRACT_BIT6(x) (((x) >> HIF8_BIT6_INDEX) & 0x1)
40 : #define HIF8_EXTRACT_BIT5(x) (((x) >> HIF8_BIT5_INDEX) & 0x1)
41 : #define HIF8_EXTRACT_BIT4(x) (((x) >> HIF8_BIT4_INDEX) & 0x1)
42 : #define HIF8_EXTRACT_BIT3(x) (((x) >> HIF8_BIT3_INDEX) & 0x1)
43 :
44 : constexpr int8_t HIF8_NAN = 0x80;
45 : constexpr int8_t HIF8_POS_INF = 0x6F;
46 : constexpr int8_t HIF8_NEG_INF = 0xEF;
47 : constexpr int8_t HIF8_BIT_LEN = 8;
48 : constexpr uint32_t FP32_NAN = 0x7FFFFFFF;
49 : constexpr uint32_t FP32_EXP_BIAS = 127;
50 :
51 : #define FP32_MAX_MAN (0x7FFFFF)
52 : #define FP32_POS_INF (0x7F800000)
53 : #define FP32_NEG_INF (0xff800000)
54 : constexpr uint32_t FP32_SIGN_INDEX = 31;
55 : constexpr uint32_t FP32_MAN_LEN = 23;
56 :
57 : // FP8 (E5M2) -> Fp32
58 : #define FP8_SIGN_INDEX (7)
59 : #define FP8_T_NAN (0x7F)
60 : #define FP8_MAX_MAN (0x7)
61 :
62 : constexpr int16_t FP8E5M2_EXP_MASK = 0x7C;
63 : constexpr int16_t FP8E5M2_MAN_MASK = 0x3;
64 : constexpr uint32_t FP8E5M2_MAN_LEN = 2;
65 : constexpr uint32_t FP8E5M2_EXP_BIAS = 15;
66 :
67 : #define FP8E5M2_MAN_HIDE_BIT (0x4)
68 : #define FP8E5M2_MAX_EXP (0x1F)
69 : #define FP8E5M2_MAX_MAN (0x3)
70 : #define FP8E5M2_INF (0X7C)
71 :
72 : // FP8 (E4M3) -> Fp32
73 : #define FP8_SIGN_INDEX (7)
74 : #define FP8_T_NAN (0x7F)
75 : constexpr uint32_t FP8E4M3_EXP_BIAS = 7;
76 : constexpr uint32_t FP8E4M3_MAN_LEN = 3;
77 : #define FP8_MAX_EXP (0xF)
78 : #define FP8_MAX_MAN (0x7)
79 : #define FP8_MAN_HIDE_BIT (0x8)
80 :
81 : } // namespace FPTranslation
82 : #endif
83 : #endif
84 : } // namespace AscendC
85 :
86 : namespace AscendC {
87 : struct SliceInfo {
88 832 : __aicore__ SliceInfo() {}
89 :
90 208 : __aicore__ SliceInfo(
91 : const uint32_t startIndexIn, const uint32_t endIndexIn, const uint32_t strideIn, const uint32_t burstLenIn,
92 : const uint32_t shapeValueIn = 0)
93 208 : : startIndex(startIndexIn),
94 208 : endIndex(endIndexIn),
95 208 : stride(strideIn),
96 208 : burstLen(burstLenIn),
97 208 : shapeValue(shapeValueIn)
98 208 : {}
99 :
100 : uint32_t startIndex = 0;
101 : uint32_t endIndex = ONE_BLK_SIZE - 1;
102 : uint32_t stride = 0;
103 : uint32_t burstLen = ONE_BLK_SIZE;
104 : uint32_t shapeValue = 0;
105 : };
106 :
107 : class AscendCUtils {
108 : public:
109 : __aicore__ static inline int32_t GetC0Size() { return DEFAULT_C0_SIZE; }
110 :
111 : __aicore__ static inline int32_t GetC0Count(const int32_t dtypeSize)
112 : {
113 : ASCENDC_ASSERT((dtypeSize != 0), { KERNEL_LOG(KERNEL_ERROR, "dtypeSize can not be 0"); });
114 : return GetC0Size() / dtypeSize;
115 : }
116 : };
117 :
118 : #ifdef ASCENDC_CPU_DEBUG
119 : enum AtomicType { SUM, MAX, MIN };
120 : extern bool g_isAtomic;
121 : extern AtomicType g_atomicType;
122 :
123 : #endif // ASCENDC_CPU_DEBUG
124 :
125 : } // namespace AscendC
126 : #endif // ASCENDC_MODULE_UTILS_H
|