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 "operator_kernel_mark_step.h"
12 :
13 : #include "aicpusd_status.h"
14 : #include "aicpusd_util.h"
15 : #include "aicpusd_model_execute.h"
16 :
17 :
18 : namespace AicpuSchedule {
19 : namespace {
20 : const std::string KERNEL_MARK_STEP = "markStep";
21 : } // namespace
22 :
23 6 : int32_t OperatorKernelMarkStep::Compute(const AicpuTaskInfo &kernelTaskInfo, const RunContext &taskContext)
24 : {
25 6 : aicpusd_info("Begin to execute MarkStep modelId[%u].", taskContext.modelId);
26 : MarkStepInfo * const bufInfo =
27 6 : PtrToPtr<void, MarkStepInfo>(ValueToPtr(static_cast<uintptr_t>(kernelTaskInfo.paraBase)));
28 6 : if (CheckMarkStepPara(bufInfo) != AICPU_SCHEDULE_OK) {
29 1 : aicpusd_err("MarkStep para check failed, modelId[%u], streamId[%u], taskId[%u]",
30 : taskContext.modelId, taskContext.streamId, kernelTaskInfo.taskID);
31 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
32 : }
33 :
34 : // bufInfo parse
35 5 : AicpuModel * const model = AicpuModelManager::GetInstance().GetModel(taskContext.modelId);
36 5 : if (model == nullptr) {
37 1 : aicpusd_err("cannot get model by modelId:[%u]!", taskContext.modelId);
38 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
39 : }
40 :
41 4 : uint64_t * const stepIdAddr = PtrToPtr<void, uint64_t>(ValueToPtr(static_cast<uintptr_t>(bufInfo->stepIdAddr)));
42 4 : if (static_cast<uint32_t>(bufInfo->headFlag) == 1U) {
43 1 : aicpusd_debug("Not is head node, modelId=%u", taskContext.modelId);
44 1 : model->SetHeadNodeFlag(false);
45 1 : model->SetStepIdInfo(std::move(StepIdInfo(stepIdAddr, 0U)));
46 1 : return AICPU_SCHEDULE_OK;
47 : }
48 :
49 3 : const uint64_t iteratorCount = model->GetIteratorId();
50 3 : if (AicpuUtil::IsUint64MulOverflow(iteratorCount, bufInfo->groupTotalCount)) {
51 1 : aicpusd_err("modelId[%u], iteratorCount:[%lu], groupIndex:%u, totalCnt:%u!",
52 : taskContext.modelId, iteratorCount, bufInfo->groupIndex, bufInfo->groupTotalCount);
53 1 : return AICPU_SCHEDULE_ERROR_OVERFLOW;
54 : }
55 2 : uint64_t tempCnt = iteratorCount * bufInfo->groupTotalCount;
56 2 : if ((std::numeric_limits<uint64_t>::max() - tempCnt) <= bufInfo->groupIndex) {
57 1 : aicpusd_err("modelId[%u], iteratorCount:[%lu], groupIndex:%u, totalCnt:%u!",
58 : taskContext.modelId, iteratorCount, bufInfo->groupIndex, bufInfo->groupTotalCount);
59 1 : return AICPU_SCHEDULE_ERROR_OVERFLOW;
60 : }
61 1 : tempCnt += bufInfo->groupIndex;
62 1 : if ((std::numeric_limits<uint64_t>::max() - tempCnt) <= static_cast<uint64_t>(bufInfo->groupTotalCount - 1U)) {
63 0 : aicpusd_err("modelId[%u], iteratorCount:[%lu], groupIndex:%u, totalCnt:%u!",
64 : taskContext.modelId, iteratorCount, bufInfo->groupIndex, bufInfo->groupTotalCount);
65 0 : return AICPU_SCHEDULE_ERROR_OVERFLOW;
66 : }
67 :
68 : // because transId starts from 1, the following operations is needed
69 1 : const uint64_t stepId = (bufInfo->groupIndex == 0U) ?
70 1 : tempCnt + static_cast<uint64_t>(bufInfo->groupTotalCount - 1U) : tempCnt - 1UL;
71 1 : aicpusd_info("[MarkStep] headFlag[true], iteratorCount[%lu], totalCnt[%u], groupIndex[%u], stepId:[%lu].",
72 : iteratorCount, bufInfo->groupTotalCount, bufInfo->groupIndex, stepId);
73 1 : *stepIdAddr = stepId;
74 :
75 1 : model->SetHeadNodeFlag(true);
76 1 : model->SetStepIdInfo(std::move(StepIdInfo(stepIdAddr, stepId)));
77 1 : return AICPU_SCHEDULE_OK;
78 : }
79 :
80 6 : int32_t OperatorKernelMarkStep::CheckMarkStepPara(const MarkStepInfo * const markStepInfo) const
81 : {
82 6 : if ((markStepInfo == nullptr) || (markStepInfo->stepIdAddr == 0UL) || (markStepInfo->groupTotalCount == 0U)) {
83 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
84 : }
85 :
86 5 : return AICPU_SCHEDULE_OK;
87 : }
88 :
89 6 : REGISTER_OPERATOR_KERNEL(KERNEL_MARK_STEP, OperatorKernelMarkStep);
90 : } // namespace AicpuSchedule
|