已合并
修复GRU、GRUGard算子cleancode问题 #8851
chenyifeng创建于 8月18日
修复GRU、GRUGard算子cleancode问题 #8851
已合并
共 5 个文件变更+30-31
| @@ -241,7 +241,7 @@ static bool CheckDims(const aclTensor* input, const aclTensorList* params, const | |||
| 241 | uint64_t bScale = hasBias ? 2 : 1; | 241 | uint64_t bScale = hasBias ? 2 : 1; |
| 242 | uint64_t dScale = bidirection ? 2 : 1; | 242 | uint64_t dScale = bidirection ? 2 : 1; |
| 243 | uint64_t oneLayerParams = 2 * bScale * dScale; | 243 | uint64_t oneLayerParams = 2 * bScale * dScale; |
| 244 | - for (uint64_t i = 0; i < (uint64_t)numLayers; i++) { | 244 | + for (uint64_t i = 0; i < static_cast<uint64_t>(numLayers); i++) { |
| 245 | for (uint64_t j = 0; j < dScale; j++) { | 245 | for (uint64_t j = 0; j < dScale; j++) { |
| 246 | uint64_t offsets = i * oneLayerParams + j * oneLayerParams / 2; | 246 | uint64_t offsets = i * oneLayerParams + j * oneLayerParams / 2; |
| 247 | OP_CHECK_WRONG_DIMENSION((*params)[offsets], WEIGHT_DIMS, return false); | 247 | OP_CHECK_WRONG_DIMENSION((*params)[offsets], WEIGHT_DIMS, return false); |
| @@ -297,7 +297,7 @@ static bool CheckShape(const aclTensor* input, const aclTensorList* params, cons | |||
| 297 | uint64_t oneLayerParams = 2 * bScale * dScale; | 297 | uint64_t oneLayerParams = 2 * bScale * dScale; |
| 298 | 298 | ||
| 299 | // 逐层校验 | 299 | // 逐层校验 |
| 300 | - for (uint64_t i = 0; i < (uint64_t)numLayers; i++) { | 300 | + for (uint64_t i = 0; i < static_cast<uint64_t>(numLayers); i++) { |
| 301 | op::Shape expWiShape = {GRU_GATE_NUM * hiddenSize, curLayerInputSize}; | 301 | op::Shape expWiShape = {GRU_GATE_NUM * hiddenSize, curLayerInputSize}; |
| 302 | op::Shape expWhShape = {GRU_GATE_NUM * hiddenSize, hiddenSize}; | 302 | op::Shape expWhShape = {GRU_GATE_NUM * hiddenSize, hiddenSize}; |
| 303 | op::Shape expBShape = {GRU_GATE_NUM * hiddenSize}; | 303 | op::Shape expBShape = {GRU_GATE_NUM * hiddenSize}; |
| @@ -632,8 +632,7 @@ static aclnnStatus CheckDataDtypes(const GruDataParamsIn& inputs, const GruDataP | |||
| 632 | static aclnnStatus CheckDataParamsValid(const GruDataParamsIn& inputs, const GruDataParamsOut& outputs, | 632 | static aclnnStatus CheckDataParamsValid(const GruDataParamsIn& inputs, const GruDataParamsOut& outputs, |
| 633 | GruDataInfo& info) | 633 | GruDataInfo& info) |
| 634 | { | 634 | { |
| 635 | - aclnnStatus ret; | 635 | + aclnnStatus ret = CheckDataParamsNullptr(inputs, outputs); |
| 636 | - ret = CheckDataParamsNullptr(inputs, outputs); | ||
| 637 | OP_CHECK(ret == ACLNN_SUCCESS, | 636 | OP_CHECK(ret == ACLNN_SUCCESS, |
| 638 | OP_LOGE(ret, "CheckDataParamsNullptr failed, certain incoming nullptrs may be invalid."), return ret); | 637 | OP_LOGE(ret, "CheckDataParamsNullptr failed, certain incoming nullptrs may be invalid."), return ret); |
| 639 | 638 | ||
| @@ -64,7 +64,6 @@ ge::graphStatus GruTilingOp::GetOpInfo() | |||
| 64 | 64 | ||
| 65 | auto outputHShape = context_->GetOutputShape(1)->GetStorageShape(); // [B,H] | 65 | auto outputHShape = context_->GetOutputShape(1)->GetStorageShape(); // [B,H] |
| 66 | gruParams_.batch = outputHShape.GetDim(0); | 66 | gruParams_.batch = outputHShape.GetDim(0); |
| 67 | - | ||
| 68 | } else { | 67 | } else { |
| 69 | OP_LOGE(context_->GetNodeName(), "input_x only support 2D or 3D."); | 68 | OP_LOGE(context_->GetNodeName(), "input_x only support 2D or 3D."); |
| 70 | return ge::GRAPH_FAILED; | 69 | return ge::GRAPH_FAILED; |
| @@ -46,6 +46,7 @@ constexpr int64_t DIM_ONE = 1; | |||
| 46 | constexpr int64_t DIM_TWO = 2; | 46 | constexpr int64_t DIM_TWO = 2; |
| 47 | constexpr int64_t DIM_THREE = 3; | 47 | constexpr int64_t DIM_THREE = 3; |
| 48 | constexpr int64_t GATE_COUNT = 3; // GRU: reset, update, new | 48 | constexpr int64_t GATE_COUNT = 3; // GRU: reset, update, new |
| 49 | +constexpr int64_t GATE_LIST_COUNT = 5; | ||
| 49 | 50 | ||
| 50 | // 参数列表中每层的参数个数 | 51 | // 参数列表中每层的参数个数 |
| 51 | constexpr int64_t PARAMS_PER_LAYER_NO_BIAS_NO_BIDI = 2; // w_ih, w_hh | 52 | constexpr int64_t PARAMS_PER_LAYER_NO_BIAS_NO_BIDI = 2; // w_ih, w_hh |
| @@ -112,12 +113,12 @@ static bool CheckTensorListDtype(const aclTensorList* list, const char* name, ge | |||
| 112 | OP_CHECK_DTYPE_NOT_SUPPORT(tensor, DTYPE_SUPPORT_LIST, return false); | 113 | OP_CHECK_DTYPE_NOT_SUPPORT(tensor, DTYPE_SUPPORT_LIST, return false); |
| 113 | if (tensor->GetDataType() != baseDtype) { | 114 | if (tensor->GetDataType() != baseDtype) { |
| 114 | OP_LOGE(ACLNN_ERR_PARAM_INVALID, "%s[%llu] dtype inconsistent, expected %s, actual %s.", name, | 115 | OP_LOGE(ACLNN_ERR_PARAM_INVALID, "%s[%llu] dtype inconsistent, expected %s, actual %s.", name, |
| 115 | - (unsigned long long)i, op::ToString(baseDtype).GetString(), | 116 | + static_cast<unsigned long long>(i), op::ToString(baseDtype).GetString(), |
| 116 | op::ToString(tensor->GetDataType()).GetString()); | 117 | op::ToString(tensor->GetDataType()).GetString()); |
| 117 | return false; | 118 | return false; |
| 118 | } | 119 | } |
| 119 | if (!IsFormatSupported(tensor, name)) { | 120 | if (!IsFormatSupported(tensor, name)) { |
| 120 | - OP_LOGE(ACLNN_ERR_PARAM_INVALID, "%s[%llu] format invalid", name, (unsigned long long)i); | 121 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "%s[%llu] format invalid", name, static_cast<unsigned long long>(i)); |
| 121 | return false; | 122 | return false; |
| 122 | } | 123 | } |
| 123 | } | 124 | } |
| @@ -131,7 +132,6 @@ static bool CheckDtypeValid(const aclTensor* input, const aclTensor* hx, const a | |||
| 131 | aclTensorList* dparamsOut) | 132 | aclTensorList* dparamsOut) |
| 132 | { | 133 | { |
| 133 | ge::DataType baseDtype = input->GetDataType(); | 134 | ge::DataType baseDtype = input->GetDataType(); |
| 134 | - | ||
| 135 | if (!CheckDtypeConsistent(input, "input", baseDtype)) | 135 | if (!CheckDtypeConsistent(input, "input", baseDtype)) |
| 136 | return false; | 136 | return false; |
| 137 | if (!CheckDtypeConsistent(hx, "hx", baseDtype)) | 137 | if (!CheckDtypeConsistent(hx, "hx", baseDtype)) |
| @@ -187,9 +187,9 @@ static bool ValidateShape(const aclTensor* tensor, const std::vector<int64_t>& e | |||
| 187 | 187 | ||
| 188 | static bool CheckShapeValid(const aclTensor* input, const aclTensor* hx, const aclTensorList* params, | 188 | static bool CheckShapeValid(const aclTensor* input, const aclTensor* hx, const aclTensorList* params, |
| 189 | const aclTensor* dy, const aclTensor* dh, const aclTensorList* r, const aclTensorList* z, | 189 | const aclTensor* dy, const aclTensor* dh, const aclTensorList* r, const aclTensorList* z, |
| 190 | - const aclTensorList* n, const aclTensorList* hn, const aclTensorList* h, | 190 | + const aclTensorList* n, const aclTensorList* hn, const aclTensorList* h, bool hasBias, |
| 191 | - const aclTensor* batchSizes, bool hasBias, int64_t numLayers, bool bidirection, | 191 | + int64_t numLayers, bool bidirection, bool batchFirst, const aclTensor* dxOut, |
| 192 | - bool batchFirst, aclTensor* dxOut, aclTensor* dhPrevOut, aclTensorList* dparamsOut) | 192 | + const aclTensor* dhPrevOut, const aclTensorList* dparamsOut) |
| 193 | { | 193 | { |
| 194 | // 从 input 推断 T, B, I | 194 | // 从 input 推断 T, B, I |
| 195 | auto xShape = input->GetViewShape(); | 195 | auto xShape = input->GetViewShape(); |
| @@ -235,10 +235,11 @@ static bool CheckShapeValid(const aclTensor* input, const aclTensor* hx, const a | |||
| 235 | uint64_t expectedGateLen = static_cast<uint64_t>(D * numLayers); | 235 | uint64_t expectedGateLen = static_cast<uint64_t>(D * numLayers); |
| 236 | const char* gateNames[] = {"r", "z", "n", "hn", "h"}; | 236 | const char* gateNames[] = {"r", "z", "n", "hn", "h"}; |
| 237 | const aclTensorList* gateLists[] = {r, z, n, hn, h}; | 237 | const aclTensorList* gateLists[] = {r, z, n, hn, h}; |
| 238 | - for (int g = 0; g < 5; ++g) { | 238 | + for (int g = 0; g < GATE_LIST_COUNT; ++g) { |
| 239 | if (gateLists[g]->Size() != expectedGateLen) { | 239 | if (gateLists[g]->Size() != expectedGateLen) { |
| 240 | OP_LOGE(ACLNN_ERR_PARAM_INVALID, "%s list size mismatch, expected %llu, actual %llu.", gateNames[g], | 240 | OP_LOGE(ACLNN_ERR_PARAM_INVALID, "%s list size mismatch, expected %llu, actual %llu.", gateNames[g], |
| 241 | - (unsigned long long)expectedGateLen, (unsigned long long)gateLists[g]->Size()); | 241 | + static_cast<unsigned long long>(expectedGateLen), |
| 242 | + static_cast<unsigned long long>(gateLists[g]->Size())); | ||
| 242 | return false; | 243 | return false; |
| 243 | } | 244 | } |
| 244 | std::vector<int64_t> gateExpected = {T, B, H}; | 245 | std::vector<int64_t> gateExpected = {T, B, H}; |
| @@ -253,7 +254,7 @@ static bool CheckShapeValid(const aclTensor* input, const aclTensor* hx, const a | |||
| 253 | uint64_t expectedParamsLen = static_cast<uint64_t>(paramsPerLayer * numLayers); | 254 | uint64_t expectedParamsLen = static_cast<uint64_t>(paramsPerLayer * numLayers); |
| 254 | if (params->Size() != expectedParamsLen) { | 255 | if (params->Size() != expectedParamsLen) { |
| 255 | OP_LOGE(ACLNN_ERR_PARAM_INVALID, "params list size mismatch, expected %llu, actual %llu.", | 256 | OP_LOGE(ACLNN_ERR_PARAM_INVALID, "params list size mismatch, expected %llu, actual %llu.", |
| 256 | - (unsigned long long)expectedParamsLen, (unsigned long long)params->Size()); | 257 | + static_cast<unsigned long long>(expectedParamsLen), static_cast<unsigned long long>(params->Size())); |
| 257 | return false; | 258 | return false; |
| 258 | } | 259 | } |
| 259 | 260 | ||
| @@ -270,7 +271,8 @@ static bool CheckShapeValid(const aclTensor* input, const aclTensor* hx, const a | |||
| 270 | 271 | ||
| 271 | if (dparamsOut->Size() != expectedParamsLen) { | 272 | if (dparamsOut->Size() != expectedParamsLen) { |
| 272 | OP_LOGE(ACLNN_ERR_PARAM_INVALID, "dparamsOut list size mismatch, expected %llu, actual %llu.", | 273 | OP_LOGE(ACLNN_ERR_PARAM_INVALID, "dparamsOut list size mismatch, expected %llu, actual %llu.", |
| 273 | - (unsigned long long)expectedParamsLen, (unsigned long long)dparamsOut->Size()); | 274 | + static_cast<unsigned long long>(expectedParamsLen), |
| 275 | + static_cast<unsigned long long>(dparamsOut->Size())); | ||
| 274 | return false; | 276 | return false; |
| 275 | } | 277 | } |
| 276 | 278 | ||
| @@ -289,8 +291,8 @@ static aclnnStatus CheckParams(const aclTensor* input, const aclTensor* hx, cons | |||
| 289 | CHECK_RET(CheckDtypeValid(input, hx, params, dy, dh, r, z, n, hn, h, batchSizes, dxOut, dhPrevOut, dparamsOut), | 291 | CHECK_RET(CheckDtypeValid(input, hx, params, dy, dh, r, z, n, hn, h, batchSizes, dxOut, dhPrevOut, dparamsOut), |
| 290 | ACLNN_ERR_PARAM_INVALID); | 292 | ACLNN_ERR_PARAM_INVALID); |
| 291 | 293 | ||
| 292 | - CHECK_RET(CheckShapeValid(input, hx, params, dy, dh, r, z, n, hn, h, batchSizes, hasBias, numLayers, bidirection, | 294 | + CHECK_RET(CheckShapeValid(input, hx, params, dy, dh, r, z, n, hn, h, hasBias, numLayers, bidirection, batchFirst, |
| 293 | - batchFirst, dxOut, dhPrevOut, dparamsOut), | 295 | + dxOut, dhPrevOut, dparamsOut), |
| 294 | ACLNN_ERR_PARAM_INVALID); | 296 | ACLNN_ERR_PARAM_INVALID); |
| 295 | 297 | ||
| 296 | return ACLNN_SUCCESS; | 298 | return ACLNN_SUCCESS; |
| @@ -179,30 +179,30 @@ ge::graphStatus GruGradTiling::GetWorkspaceSize() | |||
| 179 | return ge::GRAPH_SUCCESS; | 179 | return ge::GRAPH_SUCCESS; |
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | -bool GruGradTiling::ValidateInputShape(int idx, const std::vector<int64_t>& e) | 182 | +bool GruGradTiling::ValidateInputShape(int index, const std::vector<int64_t>& expected_dims) |
| 183 | { | 183 | { |
| 184 | - auto in = context_->GetInputShape(idx); | 184 | + auto in = context_->GetInputShape(index); |
| 185 | if (!in) | 185 | if (!in) |
| 186 | return false; | 186 | return false; |
| 187 | auto s = in->GetStorageShape(); | 187 | auto s = in->GetStorageShape(); |
| 188 | - if (s.GetDimNum() != (int64_t)e.size()) | 188 | + if (s.GetDimNum() != static_cast<int64_t>(expected_dims.size())) |
| 189 | return false; | 189 | return false; |
| 190 | - for (size_t i = 0; i < e.size(); i++) | 190 | + for (size_t i = 0; i < expected_dims.size(); i++) |
| 191 | - if (e[i] != s.GetDim(i)) | 191 | + if (expected_dims[i] != s.GetDim(i)) |
| 192 | return false; | 192 | return false; |
| 193 | return true; | 193 | return true; |
| 194 | } | 194 | } |
| 195 | 195 | ||
| 196 | -bool GruGradTiling::ValidateOutputShape(int idx, const std::vector<int64_t>& e) | 196 | +bool GruGradTiling::ValidateOutputShape(int index, const std::vector<int64_t>& expected_dims) |
| 197 | { | 197 | { |
| 198 | - auto out = context_->GetOutputShape(idx); | 198 | + auto out = context_->GetOutputShape(index); |
| 199 | if (!out) | 199 | if (!out) |
| 200 | return false; | 200 | return false; |
| 201 | auto s = out->GetStorageShape(); | 201 | auto s = out->GetStorageShape(); |
| 202 | - if (s.GetDimNum() != (int64_t)e.size()) | 202 | + if (s.GetDimNum() != static_cast<int64_t>(expected_dims.size())) |
| 203 | return false; | 203 | return false; |
| 204 | - for (size_t i = 0; i < e.size(); i++) | 204 | + for (size_t i = 0; i < expected_dims.size(); i++) |
| 205 | - if (e[i] != s.GetDim(i)) | 205 | + if (expected_dims[i] != s.GetDim(i)) |
| 206 | return false; | 206 | return false; |
| 207 | return true; | 207 | return true; |
| 208 | } | 208 | } |
| @@ -221,7 +221,7 @@ bool GruGradTiling::CheckParamsShape() | |||
| 221 | tilingData_.inputSize, tilingData_.hiddenSize); | 221 | tilingData_.inputSize, tilingData_.hiddenSize); |
| 222 | 222 | ||
| 223 | bool ret = true; | 223 | bool ret = true; |
| 224 | - auto ci = [&](int i, auto& e, const char* n) { | 224 | + auto ci = [this](int i, auto& e, const char* n) { |
| 225 | bool ok = ValidateInputShape(i, e); | 225 | bool ok = ValidateInputShape(i, e); |
| 226 | auto in = context_->GetInputShape(i); | 226 | auto in = context_->GetInputShape(i); |
| 227 | if (in) { | 227 | if (in) { |
| @@ -239,7 +239,7 @@ bool GruGradTiling::CheckParamsShape() | |||
| 239 | } | 239 | } |
| 240 | return ok; | 240 | return ok; |
| 241 | }; | 241 | }; |
| 242 | - auto co = [&](int i, auto& e, const char* n) { | 242 | + auto co = [this](int i, auto& e, const char* n) { |
| 243 | bool ok = ValidateOutputShape(i, e); | 243 | bool ok = ValidateOutputShape(i, e); |
| 244 | auto out = context_->GetOutputShape(i); | 244 | auto out = context_->GetOutputShape(i); |
| 245 | if (out) { | 245 | if (out) { |
| @@ -58,8 +58,7 @@ private: | |||
| 58 | void ConcatXhBlockCalculate(); | 58 | void ConcatXhBlockCalculate(); |
| 59 | void SetTilingData(); | 59 | void SetTilingData(); |
| 60 | 60 | ||
| 61 | - CutBatchTiling CalculateCutBatchTiling(int64_t ubParaNum, int64_t alignedSize, int64_t actualSize, | 61 | + CutBatchTiling CalculateCutBatchTiling(int64_t ub, int64_t al, int64_t act, int64_t ml, int64_t b); |
| 62 | - int64_t copyMLinesMax, int64_t batch); | ||
| 63 | 62 | ||
| 64 | bool ValidateInputShape(int index, const std::vector<int64_t>& expected_dims); | 63 | bool ValidateInputShape(int index, const std::vector<int64_t>& expected_dims); |
| 65 | bool ValidateOutputShape(int index, const std::vector<int64_t>& expected_dims); | 64 | bool ValidateOutputShape(int index, const std::vector<int64_t>& expected_dims); |