昇腾GE TensorDesc API文档
TensorDesc【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge产品支持情况产品是否支持Atlas A3 训练系列产品/Atlas A3 推理系列产品√Atlas A2 训练系列产品/Atlas A2 推理系列产品√模块导入from ge.graph import TensorDesc功能说明TensorDesc 类用于描述张量的元信息包括形状Shape、数据格式Format和数据类型DataType。支持原始形状/格式和当前形状/格式的区分适用于图构建过程中对张量属性的完整描述。所有set_*方法均返回self支持链式调用。不支持拷贝copy和深拷贝deepcopy。类定义class TensorDesc: def __init__( self, shape: Optional[List[int]] None, format: Optional[Format] Format.FORMAT_ND, data_type: Optional[DataType] DataType.DT_FLOAT, ) - None函数列表函数功能说明__init__(shapeNone, formatFormat.FORMAT_ND, data_typeDataType.DT_FLOAT)构造函数创建 TensorDesc 对象。shape 为维度列表None 表示标量format 为数据格式默认 FORMAT_NDdata_type 为数据类型默认 DT_FLOATshape (property)以属性方式获取当前形状返回 Shape 对象origin_shape (property)以属性方式获取原始形状返回 Shape 对象format (property)以属性方式获取当前数据格式返回 Format 枚举值origin_format (property)以属性方式获取原始数据格式返回 Format 枚举值data_type (property)以属性方式获取数据类型返回 DataType 枚举值get_shape()获取当前形状set_shape(shape)设置当前形状支持链式调用get_origin_shape()获取原始形状set_origin_shape(shape)设置原始形状支持链式调用get_format()获取当前数据格式set_format(format)设置当前数据格式支持链式调用get_origin_format()获取原始数据格式set_origin_format(format)设置原始数据格式支持链式调用get_data_type()获取数据类型set_data_type(data_type)设置数据类型支持链式调用参数说明__init__ 参数参数名类型是否必选说明shapeList[int]否张量的维度列表例如 [1, 3, 224, 224]。None 表示标量空列表。默认值为 NoneformatFormat否张量的数据格式取值为 Format 枚举值。默认值为 Format.FORMAT_NDdata_typeDataType否张量的元素数据类型取值为 DataType 枚举值。默认值为 DataType.DT_FLOATset_shape / set_origin_shape 参数参数名类型是否必选说明shapeList[int]是目标维度列表必须为整数列表set_format / set_origin_format 参数参数名类型是否必选说明formatFormat是目标数据格式必须为 Format 枚举值set_data_type 参数参数名类型是否必选说明data_typeDataType是目标数据类型必须为 DataType 枚举值返回值说明函数返回值类型说明shape (property)Shape当前形状对象origin_shape (property)Shape原始形状对象format (property)Format当前数据格式枚举值origin_format (property)Format原始数据格式枚举值data_type (property)DataType数据类型枚举值get_shape()Shape当前形状对象set_shape(shape)TensorDesc返回自身支持链式调用get_origin_shape()Shape原始形状对象set_origin_shape(shape)TensorDesc返回自身支持链式调用get_format()Format当前数据格式枚举值set_format(format)TensorDesc返回自身支持链式调用get_origin_format()Format原始数据格式枚举值set_origin_format(format)TensorDesc返回自身支持链式调用get_data_type()DataType数据类型枚举值set_data_type(data_type)TensorDesc返回自身支持链式调用约束说明不支持拷贝操作调用copy.copy()会抛出 RuntimeError。不支持深拷贝操作调用copy.deepcopy()会抛出 RuntimeError。构造函数中format 参数必须为 Format 枚举类型否则抛出 TypeError。构造函数中data_type 参数必须为 DataType 枚举类型否则抛出 TypeError。set_shape、set_origin_shape 的 shape 参数必须为整数列表list of int否则抛出 TypeError。set_format、set_origin_format 的 format 参数必须为 Format 枚举类型否则抛出 TypeError。set_data_type 的 data_type 参数必须为 DataType 枚举类型否则抛出 TypeError。若底层 C API 调用失败各方法将抛出 RuntimeError。使用示例from ge.graph import TensorDesc, Shape, Format, DataType # 创建 TensorDesc 对象 desc TensorDesc(shape[1, 3, 224, 224], formatFormat.FORMAT_NCHW, data_typeDataType.DT_FLOAT) # 通过属性获取张量信息 print(desc.shape) # [1, 3, 224, 224] print(desc.format) # Format.FORMAT_NCHW print(desc.data_type) # DataType.DT_FLOAT # 通过方法获取张量信息 shape desc.get_shape() fmt desc.get_format() dtype desc.get_data_type() # 链式调用设置属性 desc.set_shape([2, 3]).set_data_type(DataType.DT_INT32).set_format(Format.FORMAT_ND)【免费下载链接】geGEGraph Engine是面向昇腾的图编译器和执行器提供了计算图优化、多流并行、内存复用和模型下沉等技术手段加速模型执行效率减少模型内存占用。 GE 提供对 PyTorch、TensorFlow 前端的友好接入能力并同时支持 onnx、pb 等主流模型格式的解析与编译。项目地址: https://gitcode.com/cann/ge创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考