* 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.
*/
#ifndef OPTIMIZE_BUFFER_ALLOCATE_TENSOR_MEM_DEFS_H_
#define OPTIMIZE_BUFFER_ALLOCATE_TENSOR_MEM_DEFS_H_
#include <vector>
#include <string>
#include "ascendc_ir_core/ascendc_ir.h"
namespace optimize {
constexpr auto kAttrNameNoReuseOutputIndices = "_no_reuse_output_indices";
constexpr auto kAttrNameNoReuseInputs = "_no_reuse_inputs";
constexpr int64_t kDbBufNum = 2;
enum class MemorySizeLevel : int32_t { kScalar = 0, kMedium, kLargest };
struct TensorInfo {
int64_t group_id{-1};
af::AscTensorAttr *output_tensor_attr{nullptr};
int64_t life_start{-1};
int64_t life_end{-1};
af::Position mem_position{af::Position::kPositionInvalid};
MemorySizeLevel size_level{MemorySizeLevel::kLargest};
bool is_reusable{true};
bool is_can_reuse_others{true};
std::set<int64_t> loop_axes;
int64_t buf_num{kDbBufNum};
std::string ToString() const {
std::stringstream ss;
ss << "TensorInfo{";
ss << "group_id=" << group_id << ", ";
ss << "life_start=" << life_start << ", ";
ss << "life_end=" << life_end << ", ";
ss << "buf_num=" << buf_num << ", ";
ss << "is_reusable=" << std::boolalpha << is_reusable << ", ";
ss << "is_can_reuse_others=" << std::boolalpha << is_can_reuse_others << ", ";
ss << "mem_position=" << static_cast<int32_t>(mem_position) << ", ";
ss << "size_level=" << static_cast<int32_t>(size_level) << ", ";
ss << "loop_axes={";
for (auto it = loop_axes.begin(); it != loop_axes.end(); ++it) {
if (it != loop_axes.begin()) {
ss << ", ";
}
ss << *it;
}
ss << "}";
ss << "}";
return ss.str();
}
};
struct TensorGroup {
int64_t group_id{-1};
std::vector<TensorInfo *> grouped_tensors;
std::set<int64_t> merged_loop_axes;
int64_t merged_life_start;
int64_t merged_life_end;
MemorySizeLevel max_size_level;
af::Position mem_position;
bool group_is_reusable;
bool group_is_can_reuse_others;
};
enum class MemoryType { kCopyIn, kCopyOut, kCalc, kTmpBuff, kLoopTmpBuff};
struct MemoryBlock {
int64_t id;
MemoryType mem_type;
MemorySizeLevel max_size_level;
std::vector<const TensorGroup *> tensor_groups;
};
using TensorInfoMap = std::map<af::AscTensorAttr *, TensorInfo>;
using TmpBuffInfoMap = std::map<af::TmpBuffer *, TensorInfo>;
}
#endif