* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include "variable.h"
#include <unordered_set>
#include "graph/ge_error_codes.h"
#include "register/kernel_registry.h"
#include "kernel/tensor_attr.h"
#include "exe_graph/runtime/gert_tensor_data.h"
#include "exe_graph/runtime/gert_mem_allocator.h"
#include "common/checker.h"
#include "graph/manager/graph_var_manager.h"
#include "framework/runtime/rt_session.h"
#include "core/utils/tensor_utils.h"
#include "core/executor/multi_thread_topological/executor/schedule/producer/producers/kernel_tags/critical_section_config.h"
namespace gert {
namespace kernel {
namespace {
ge::graphStatus SplitVariable(KernelContext *context) {
auto rt_session = context->GetInputValue<RtSession *>(static_cast<size_t>(SplitVariableInputs::kRtSession));
GE_ASSERT_NOTNULL(rt_session);
auto var_id = context->GetInputValue<char *>(static_cast<size_t>(SplitVariableInputs::kVarId));
GE_ASSERT_NOTNULL(var_id);
auto gert_allocator = context->GetInputValue<GertAllocator *>(static_cast<size_t>(SplitVariableInputs::kAllocator));
GE_ASSERT_NOTNULL(gert_allocator);
auto shape = context->GetOutputPointer<StorageShape>(static_cast<size_t>(SplitVariableOutputs::kShape));
GE_ASSERT_NOTNULL(shape);
auto gtd = context->GetOutputPointer<GertTensorData>(static_cast<size_t>(SplitVariableOutputs::kTensorData));
GE_ASSERT_NOTNULL(gtd);
auto var_manager = rt_session->GetVarManager();
GE_ASSERT_NOTNULL(var_manager, "Var manager not found when split variable %s", var_id);
TensorData tmp_td;
GE_ASSERT_SUCCESS(var_manager->GetVarShapeAndMemory(var_id, *shape, tmp_td),
"Var manager %p get variable '%s' shape and memory failed", var_manager, var_id);
GE_ASSERT_SUCCESS(TensorUtils::ShareTdToGtd(tmp_td, *gert_allocator, *gtd));
return ge::GRAPH_SUCCESS;
}
ge::graphStatus CreateVariableOutputs(const ge::FastNode *node, KernelContext *context) {
(void)node;
auto shape_chain = context->GetOutput(static_cast<size_t>(SplitVariableOutputs::kShape));
GE_ASSERT_NOTNULL(shape_chain);
auto extend_context = reinterpret_cast<ExtendedKernelContext *>(context);
GE_ASSERT_NOTNULL(extend_context);
auto output_desc = extend_context->GetOutputDesc(0UL);
GE_ASSERT_NOTNULL(output_desc);
auto shape_tensor = new (std::nothrow) Tensor(StorageShape(),
output_desc->GetFormat(), output_desc->GetDataType());
shape_chain->SetWithDefaultDeleter(shape_tensor);
auto tensor_data_chain = context->GetOutput(static_cast<size_t>(SplitVariableOutputs::kTensorData));
GE_ASSERT_NOTNULL(tensor_data_chain);
auto td = new (std::nothrow) GertTensorData();
tensor_data_chain->SetWithDefaultDeleter(td);
return ge::GRAPH_SUCCESS;
}
}
REGISTER_KERNEL(SplitVariable)
.RunFunc(SplitVariable)
.OutputsCreator(CreateVariableOutputs)
.ConcurrentCriticalSectionKey(kKernelUseMemory);
}
}