* Copyright (c) 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.
*/
* \file tensor_descriptor.hpp
* \brief 张量描述符头文件
*/
#ifndef ACLTENSOR_LIB_CORE_TENSOR_DESCRIPTOR_HPP
#define ACLTENSOR_LIB_CORE_TENSOR_DESCRIPTOR_HPP
#include <vector>
#include <cstdint>
#include "cann_ops_tensor_types.h"
#include "utils/type_utils.hpp"
* @brief 张量描述符结构体
*/
struct acltensorTensorDescriptor {
acltensorDataType_t dataType;
uint32_t numModes;
std::vector<int64_t> lens;
std::vector<int64_t> strides;
uint32_t alignmentRequirement;
size_t elementSize;
size_t totalElements;
size_t totalBytes;
bool isContiguous;
* @brief 计算派生属性
*/
void computeDerivedAttributes()
{
elementSize = acltensor::GetDataTypeSize(dataType);
totalElements = 1;
for (uint32_t i = 0; i < numModes; ++i) {
totalElements *= lens[i];
}
if (strides.empty() || strides.size() != numModes) {
strides.resize(numModes);
if (numModes > 0) {
strides[numModes - 1] = 1;
for (int32_t i = numModes - 2; i >= 0; --i) {
strides[i] = strides[i + 1] * lens[i + 1];
}
}
isContiguous = true;
} else {
isContiguous = checkContiguous();
}
totalBytes = totalElements * elementSize;
}
* @brief 检查是否为连续内存布局
*/
bool checkContiguous() const
{
if (numModes == 0) {
return true;
}
int64_t expectedStride = 1;
for (int32_t i = numModes - 1; i >= 0; --i) {
if (strides[i] != expectedStride) {
return false;
}
expectedStride *= lens[i];
}
return true;
}
};
#endif