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 : #include "ccu_rep.h"
12 :
13 : #include "string_util.h"
14 :
15 : namespace Hccl {
16 : namespace CcuRep {
17 :
18 12 : CcuRepBufReduce::CcuRepBufReduce(const std::vector<CcuBuffer> &mem, uint16_t count, uint16_t dataType,
19 : uint16_t outputDataType, uint16_t opType, MaskSignal sem, const CcuRep::Variable &len,
20 12 : uint16_t mask)
21 12 : : mem(mem), count(count), dataType(dataType), outputDataType(outputDataType), opType(opType), sem(sem),
22 24 : xnIdLength_(len), mask(mask)
23 : {
24 12 : type = CcuRepType::BUF_REDUCE;
25 12 : instrCount = 1;
26 12 : }
27 :
28 5 : bool CcuRepBufReduce::Translate(CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
29 : {
30 5 : this->instrId = instrId;
31 5 : translated = true;
32 :
33 5 : if (count > CCU_REDUCE_MAX_MS || mem.size() > CCU_REDUCE_MAX_MS) {
34 0 : THROW<CcuApiException>("count and mem size must less than %u", CCU_REDUCE_MAX_MS);
35 : }
36 5 : if (count < CCU_REDUCE_MIN_MS) {
37 0 : THROW<Hccl::CcuApiException>("count must be at least %u", CCU_REDUCE_MIN_MS);
38 : }
39 :
40 : // 这里需要注意,在数据格式膨胀的情况下,需要传入用来存放输出的MSId
41 : // 特别是2P场景,输入MS的数目为2,但是在8bit进,32bit出的场景,输出MS的数目为4
42 : // 传入的MS中已经包含了需要使用的输入输出的最大量,因此,这里应该直接去MS的size
43 5 : uint16_t msId[CCU_REDUCE_MAX_MS] = {0};
44 25 : for (uint16_t i = 0; i < mem.size(); i++) {
45 20 : msId[i] = mem[i].Id();
46 : }
47 :
48 5 : if (opType == CCU_REDUCE_SUM) {
49 3 : if (outputDataType == 1) { // 1是fp16
50 1 : AddInstr(instr++, msId, count, outputDataType, dataType, sem.Id(), mask, 0, 0, 1, xnIdLength_.Id());
51 2 : } else if (outputDataType == 2) { // 2是bf16
52 1 : AddInstr(instr++, msId, count, outputDataType, dataType, sem.Id(), mask, 0, 0, 1, xnIdLength_.Id());
53 : } else {
54 1 : AddInstr(instr++, msId, count, 0, dataType, sem.Id(), mask, 0, 0, 1, xnIdLength_.Id());
55 : }
56 2 : } else if (opType == CCU_REDUCE_MAX) {
57 1 : MaxInstr(instr++, msId, count, dataType, sem.Id(), mask, 0, 0, 1, xnIdLength_.Id());
58 1 : } else if (opType == CCU_REDUCE_MIN) {
59 1 : MinInstr(instr++, msId, count, dataType, sem.Id(), mask, 0, 0, 1, xnIdLength_.Id());
60 : }
61 5 : instrId += instrCount;
62 :
63 5 : return translated;
64 : }
65 :
66 5 : std::string CcuRepBufReduce::Describe()
67 : {
68 5 : return StringFormat("Reduce");
69 : }
70 :
71 : }; // namespace CcuRep
72 : }; // namespace Hccl
|