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 "ccu_rep_reference_manager_v1.h"
10 : #include "ccu_rep_translator_v1.h"
11 :
12 : #include "string_util.h"
13 : #include "exception_util.h"
14 : #include "ccu_api_exception.h"
15 :
16 : #include "ccu_ins_generater_base.h"
17 : #include "ccu_kernel.h"
18 :
19 : namespace hcomm {
20 : namespace CcuRep {
21 :
22 : using namespace Hccl;
23 :
24 16 : CcuRepFuncBlock::CcuRepFuncBlock(CcuInsGeneraterBase* insGenPtr, const std::string &label) :
25 16 : CcuRepBlock(insGenPtr, label)
26 : {
27 16 : type = CcuRepType::FUNC_BLOCK;
28 16 : instrCount = 0;
29 16 : }
30 :
31 5 : std::string CcuRepFuncBlock::Describe()
32 : {
33 5 : return Hccl::StringFormat("FuncBlock[%s]", GetLabel().c_str());
34 : }
35 :
36 6 : void CcuRepFuncBlock::SetFuncManager(CcuRepReferenceManager *funcManager)
37 : {
38 6 : this->funcManager = funcManager;
39 6 : }
40 :
41 1 : void CcuRepFuncBlock::SetCallLayer(uint16_t callLayer)
42 : {
43 1 : if (callLayer != FUNC_CALL_LAYER_INVALID) {
44 1 : this->callLayer = callLayer;
45 1 : return;
46 : }
47 :
48 0 : uint16_t innerCallLayer = 0;
49 0 : for (const auto &rep : GetReps()) {
50 0 : if (rep->Type() == CcuRepType::FUNC_CALL) {
51 0 : innerCallLayer = std::static_pointer_cast<CcuRepFuncCall>(rep)->GetCallLayer() + 1;
52 0 : this->callLayer = this->callLayer > innerCallLayer ? this->callLayer : innerCallLayer;
53 : }
54 : }
55 0 : if (this->callLayer > FUNC_NEST_MAX - 1) {
56 0 : Hccl::THROW<Hccl::CcuApiException>("Max Func Call Nest Num is %u", FUNC_NEST_MAX);
57 : }
58 : }
59 :
60 0 : uint16_t CcuRepFuncBlock::GetCallLayer() const
61 : {
62 0 : return callLayer;
63 : }
64 :
65 9 : void CcuRepFuncBlock::DefineInArg(const Variable &var)
66 : {
67 9 : inArgCount++;
68 9 : inArgs.push_back(CcuRepArg(var));
69 9 : HCCL_INFO("Define Input Arg: Index[%u], Type[Variable] Id[%u]", inArgs.size(), var.Id());
70 9 : }
71 :
72 2 : void CcuRepFuncBlock::DefineOutArg(const Variable &var)
73 : {
74 2 : outArgCount++;
75 2 : if (outArgCount > FUNC_ARG_MAX) {
76 0 : Hccl::THROW<Hccl::CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_ARG_MAX);
77 : }
78 2 : outArgs.push_back(CcuRepArg(var));
79 2 : HCCL_INFO("Define Output Arg: Index[%u], Type[Variable] Id[%u]", outArgs.size(), var.Id());
80 2 : }
81 :
82 1 : void CcuRepFuncBlock::DefineInArg(const std::vector<Variable> &varList)
83 : {
84 1 : inArgCount += varList.size();
85 1 : inArgs.push_back(CcuRepArg(varList));
86 1 : HCCL_INFO("Define Input Arg: Index[%u], Type[Variable List]: ", inArgs.size());
87 2 : for (uint32_t index = 0; index < varList.size(); index++) {
88 1 : HCCL_INFO(" Index[%u].Id[%u]", index, varList[index].Id());
89 : }
90 1 : }
91 :
92 1 : void CcuRepFuncBlock::DefineOutArg(const std::vector<Variable> &varList)
93 : {
94 1 : outArgCount += varList.size();
95 1 : if (outArgCount > FUNC_ARG_MAX) {
96 0 : Hccl::THROW<Hccl::CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_ARG_MAX);
97 : }
98 1 : outArgs.push_back(CcuRepArg(varList));
99 1 : HCCL_INFO("Define Output Arg: Index[%u], Type[Variable List]: ", outArgs.size());
100 2 : for (uint32_t index = 0; index < varList.size(); index++) {
101 1 : HCCL_INFO(" Index[%u].Id[%u]", index, varList[index].Id());
102 : }
103 1 : }
104 :
105 5 : std::vector<Variable> CcuRepFuncBlock::GetInArgVars() const
106 : {
107 5 : std::vector<Variable> vars;
108 12 : for (const auto &arg : inArgs) {
109 7 : if (arg.type == CcuArgType::VARIABLE) {
110 7 : vars.push_back(arg.var);
111 0 : } else if (arg.type == CcuArgType::VARIABLE_LIST) {
112 0 : vars.insert(vars.end(), arg.varList.begin(), arg.varList.end());
113 : }
114 : }
115 5 : return vars;
116 0 : }
117 13 : uint16_t CcuRepFuncBlock::InstrCount()
118 : {
119 13 : instrCount = CcuRepBlock::InstrCount() + inArgCount + outArgCount + insGeneratorPtr_->GetInstrCount(type); // FuncBlock需要额外指令
120 13 : return instrCount;
121 : }
122 :
123 4 : bool CcuRepFuncBlock::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
124 : {
125 4 : if (funcManager == nullptr) {
126 0 : Hccl::THROW<Hccl::CcuApiException>("funcManager is nullptr");
127 : }
128 :
129 4 : this->instrId = instrId;
130 4 : translated = true;
131 :
132 4 : CHK_RET_THROW(Hccl::CcuApiException,
133 : Hccl::StringFormat("[CcuRepFuncBlock][%s] failed to translate inArgs processing for instrId[%u] ", __func__, instrId),
134 : insGeneratorPtr_->CcuRepFuncBlockTranslate(ccuKernel, instr, instrId, this, dep, 0));
135 : // 使用空实现的自定义删除器,避免智能指针析构时释放对象
136 : auto translator
137 4 : = CcuRepTranslator(std::shared_ptr<CcuRepReferenceManager>(funcManager, [](CcuRepReferenceManager *ptr) {}), dep);
138 4 : translator.Translate(ccuKernel, GetReps(), instr, instrId, [](std::shared_ptr<CcuRepBase> rep) -> bool {
139 5 : return true;
140 : });
141 :
142 4 : CHK_RET_THROW(Hccl::CcuApiException,
143 : Hccl::StringFormat("[CcuRepFuncBlock][%s] failed to translate outArgs processing for instrId[%u] ", __func__, instrId),
144 : insGeneratorPtr_->CcuRepFuncBlockTranslate(ccuKernel, instr, instrId, this, dep, 1));
145 :
146 4 : return translated;
147 4 : }
148 :
149 : }; // namespace CcuRep
150 : }; // namespace hcomm
|