* Copyright (c) 2025-2026 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.
*/
#ifndef NN_TESTS_UT_COMMON_KERNEL_RUN_CONTEXT_HOLDER_H_
#define NN_TESTS_UT_COMMON_KERNEL_RUN_CONTEXT_HOLDER_H_
#include <numeric>
#include "op_tiling_parse_context_builder.h"
#include "op_tiling_context_builder.h"
#include "op_infer_shape_context_builder.h"
#include "op_infer_shape_range_context_builder.h"
#include "op_infer_datatype_context_builder.h"
namespace gert {
class KernelRunContextHolder {
public:
KernelRunContextHolder() = default;
~KernelRunContextHolder() = default;
KernelRunContextHolder& operator=(KernelRunContextHolder&& holder)
{
context_ = holder.context_;
opType_ = std::move(holder.opType_);
outputShapes_ = std::move(holder.outputShapes_);
inputTensors_ = std::move(holder.inputTensors_);
outputTensors_ = std::move(holder.outputTensors_);
inputMinTensors_ = std::move(holder.inputMinTensors_);
inputMaxTensors_ = std::move(holder.inputMaxTensors_);
inputTensorRanges_ = std::move(holder.inputTensorRanges_);
inputInstanceNumPacked_ = std::move(holder.inputInstanceNumPacked_);
inputInstanceNum_ = std::move(holder.inputInstanceNum_);
outputInstanceNum_ = std::move(holder.outputInstanceNum_);
tilingContextHolder_ = std::move(holder.tilingContextHolder_);
tilingParseContextHolder_ = std::move(holder.tilingParseContextHolder_);
inferShapeContextHolder_ = std::move(holder.inferShapeContextHolder_);
inferShapeRangeContextHolder_ = std::move(holder.inferShapeRangeContextHolder_);
inferDataTypeContextHolder_ = std::move(holder.inferDataTypeContextHolder_);
return *this;
}
KernelRunContextHolder(KernelRunContextHolder&& holder)
{
KernelRunContextHolder::operator=(std::move(holder));
}
void SetContext(void* context)
{
context_ = context;
}
template <typename T>
T* GetContext()
{
return static_cast<T*>(context_);
}
void CalcInputTensorIndex(int32_t inputIdx, int32_t& lowTensorIdx, int32_t& highTensorIdx)
{
if (inputIdx == 0) {
lowTensorIdx = 0;
highTensorIdx = inputInstanceNum_[0] - 1;
return;
}
lowTensorIdx = std::accumulate(inputInstanceNum_.begin(), inputInstanceNum_.begin() + inputIdx, 0);
highTensorIdx = lowTensorIdx + inputInstanceNum_[inputIdx] - 1;
}
protected:
void* context_ = nullptr;
std::string opType_;
std::vector<StorageShape> outputShapes_;
std::vector<Tensor> inputTensors_;
std::vector<Tensor> outputTensors_;
std::vector<Tensor> inputMinTensors_;
std::vector<Tensor> inputMaxTensors_;
std::vector<Range<Tensor>> inputTensorRanges_;
std::vector<uint32_t> inputInstanceNumPacked_;
std::vector<uint32_t> inputInstanceNum_;
std::vector<uint32_t> outputInstanceNum_;
ContextHolder<TilingContext> tilingContextHolder_;
ContextHolder<TilingParseContext> tilingParseContextHolder_;
ContextHolder<InferShapeContext> inferShapeContextHolder_;
ContextHolder<InferShapeRangeContext> inferShapeRangeContextHolder_;
ContextHolder<InferDataTypeContext> inferDataTypeContextHolder_;
};
}
#endif