Line data Source code
1 : /*
2 : * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
3 : * Description: ccu representation implementation file
4 : * Author: sunzhepeng
5 : * Create: 2024-06-17
6 : */
7 :
8 : #include "ccu_rep_v1.h"
9 : #include <climits>
10 :
11 : #include "exception_util.h"
12 : #include "ccu_api_exception.h"
13 : #include "ccu_ins_generator_base.h"
14 : #include "ccu_ins_generator_v1.h"
15 : #include "ccu_ins_generator_v2.h"
16 :
17 : namespace hcomm {
18 : namespace CcuRep {
19 :
20 : using namespace Hccl;
21 :
22 10 : CcuRepLoop::CcuRepLoop(CcuInsGeneratorBase* insGeneratorPtr, const std::string &label, const Variable &loopParam) :
23 10 : insGeneratorPtr_(insGeneratorPtr), label(label), loopParam(loopParam)
24 : {
25 10 : type = CcuRepType::LOOP;
26 10 : instrCount = insGeneratorPtr_->GetInstrCount(type);
27 10 : supportCcuV1 = true;
28 10 : supportCcuV2 = false; // 缺少A6格式下必需的loop参数
29 10 : }
30 :
31 0 : CcuRepLoop::CcuRepLoop(CcuInsGeneratorBase* insGeneratorPtr, const std::string &label,
32 0 : const Variable &loopParam, const Variable &loopIterNum, const Variable &loopGsaOffset) :
33 0 : insGeneratorPtr_(insGeneratorPtr), label(label), loopParam(loopParam), loopIterNum(loopIterNum), loopGsaOffset(loopGsaOffset)
34 : {
35 0 : type = CcuRepType::LOOP;
36 0 : instrCount = insGeneratorPtr_->GetInstrCount(type);
37 0 : supportCcuV1 = false; // loopParam按照A6格式填写,不适用A5
38 0 : supportCcuV2 = true;
39 0 : }
40 :
41 5 : void CcuRepLoop::ValidateInsGeneratorForLoop()
42 : {
43 5 : CcuInsGeneratorV1* tmpPtrV1 = dynamic_cast<CcuInsGeneratorV1*>(insGeneratorPtr_);
44 5 : CcuInsGeneratorV2* tmpPtrV2 = dynamic_cast<CcuInsGeneratorV2*>(insGeneratorPtr_);
45 5 : if (tmpPtrV1 && !supportCcuV1) {
46 : // 在A5场景下没有使用A5的loop调用方式
47 0 : Hccl::THROW<Hccl::CcuApiException>("Cannot translate CcuRepLoop when supportCcuV1 is false!");
48 : }
49 5 : if (tmpPtrV2 && !supportCcuV2) {
50 : // 在A5场景下没有使用A6的loop调用方式
51 0 : Hccl::THROW<Hccl::CcuApiException>("Cannot translate CcuRepLoop when supportCcuV2 is false!");
52 : }
53 5 : }
54 :
55 1 : const std::string &CcuRepLoop::GetLabel() const
56 : {
57 1 : return label;
58 : }
59 :
60 5 : void CcuRepLoop::Reference(std::shared_ptr<CcuRepLoopBlock> refRep)
61 : {
62 5 : loopBlock = refRep;
63 5 : }
64 :
65 1 : std::shared_ptr<CcuRepBase> CcuRepLoop::SetLoopParam(Executor executor, Variable var)
66 : {
67 1 : return std::make_shared<CcuRepSetLoop>(insGeneratorPtr_, loopParam, executor, var);
68 : }
69 :
70 5 : bool CcuRepLoop::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
71 : {
72 : (void)dep;
73 5 : ValidateInsGeneratorForLoop();
74 :
75 5 : this->instrId = instrId;
76 5 : translated = true;
77 :
78 6 : Hccl::CHECK_NULLPTR(loopBlock, "[CcuRepLoop::Translate] LoopBlock is nullptr!");
79 :
80 4 : if (!loopBlock->Translated()) {
81 1 : Hccl::THROW<Hccl::CcuApiException>("Reference To Invalid LoopBlock");
82 : }
83 :
84 3 : uint16_t startInstrId = loopBlock->StartInstrId();
85 3 : uint16_t loopBlockInstrCount = loopBlock->InstrCount();
86 3 : if (loopBlockInstrCount == 0) {
87 1 : HCCL_ERROR("[CcuRepLoop][Translate] loopBlockInstrCount[%u] is 0, which causes underflow in endInstrId calculation.",
88 : loopBlockInstrCount);
89 1 : return false;
90 : }
91 2 : if (startInstrId > USHRT_MAX - loopBlockInstrCount) {
92 0 : HCCL_ERROR("[CcuRepLoop][Translate] startInstrId[%u] + loopBlockInstrCount[%u] exceeds the maximum value of unsigned short int.",
93 : startInstrId, loopBlockInstrCount);
94 0 : return false;
95 : }
96 :
97 2 : if (instrId > USHRT_MAX - instrCount) {
98 1 : HCCL_ERROR("[CcuRepLoop][Translate] instrId[%u] exceeds the maximum value of unsigned short int.", instrId);
99 1 : return false;
100 : }
101 :
102 1 : uint16_t endInstrId = startInstrId + loopBlockInstrCount - 1;
103 :
104 1 : LoopInstr(instr++, startInstrId, endInstrId, loopParam.Id());
105 :
106 :
107 1 : instrId += instrCount;
108 :
109 1 : return translated;
110 : }
111 :
112 1 : std::string CcuRepLoop::Describe()
113 : {
114 1 : return Hccl::StringFormat("Loop reference to [%s]", label.c_str());
115 : }
116 :
117 : }; // namespace CcuRep
118 : }; // namespace hcomm
|