TensorBaseMalloc
TensorBase
函数功能
TensorBase的构造函数,用来创建TensorBase对象,根据传入的不同参数可选用不同的构造函数。
note
须知
如出现因内存耗尽,导致构造函数执行失败抛出的异常时,请勿继续调用后续的成员函数。
函数原型
TensorBase();
TensorBase(const MemoryData &memoryData, const bool &isBorrowed, const std::vector<uint32_t> &shape, const TensorDataType &type);
TensorBase(const std::vector<uint32_t> &shape, const TensorDataType &type, const MemoryData::MemoryType &bufferType, const int32_t &deviceId);
TensorBase(const std::vector<uint32_t> &shape, const TensorDataType &type, const int32_t &deviceId);
TensorBase(const std::vector<uint32_t> &shape, const TensorDataType &type);
TensorBase(const std::vector<uint32_t> &shape);
TensorBase(const TensorBase& tensor) = default;
参数说明
:::note 说明
- 函数原型2、3、4、5只是预先设置了shape,但不会申请内存空间,需要调用TensorBaseMalloc才能申请对应内存空间。
- 函数原型1使用场景:用户已在外部申请内存空间,构造Tensor对象的时候直接引用已申请的内存,需确保内存空间大小与Tensor对象的shape一致,同时可以选择外部申请的空间是Tensor对象内部自行释放还是用户外部释放。 :::
| 参数名 | 输入/输出 | 说明 |
|---|---|---|
| memoryData | 输入 | 用于构造TensorBase对象的参数,内存管理结构体,具体请参见MemoryData。 |
| isBorrowed | 输入 | 表示传入的MemoryData数据是否要Tensor主动释放。 - 若为“true”,表示不需要主动释放,用户自行释放。 - 若为“false”,用户不需要手动释放,Tensor析构时自动释放。 |
| shape | 输入 | 用于构造TensorBase对象的参数,张量的形状。 |
| type | 输入 | 用于构造TensorBase对象的参数,TensorDataType类型数据,具体请参见TensorDataType枚举说明。 |
| bufferType | 输入 | 用于构造TensorBase对象的参数,张量数据的内存类型。 |
| deviceId | 输入 | 用于构造TensorBase对象的参数,int类型数据,设备编号。 |
| tensor | 输入 | TensorBase对象,用于构造TensorBase对象的参数。 |
父主题: TensorBase
~TensorBase
函数功能
TensorBase类的默认析构函数。
函数原型
virtual ~TensorBase() = default;
父主题: TensorBase
TensorBaseMalloc
函数功能
用于获取TensorBase对象的内存。
Tensor对象在析构时会自动释放内存,因此无需通过函数释放内存。
函数原型
static APP_ERROR TensorBaseMalloc(TensorBase &tensor);
参数说明
| 参数名 | 输入/输出 | 说明 |
|---|---|---|
| tensor | 输入 | 用来获取对应内存的Tensor数据。 |
返回参数说明
| 数据结构 | 说明 |
|---|---|
| APP_ERROR | 程序执行返回的错误码,请参考APP_ERROR说明。 |
父主题: TensorBase
TensorBaseCopy
函数功能
内存复制函数,根据MemoryData中指定的内存位置在Host侧和Device侧之间进行复制。
函数原型
static APP_ERROR TensorBaseCopy(TensorBase &dst, const TensorBase &src);
参数说明
| 参数名 | 输入/输出 | 说明 |
|---|---|---|
| dst | 输出 | 复制后的目标内存。 |
| src | 输入 | 待复制的源内存。 |
返回参数说明
| 数据结构 | 说明 |
|---|---|
| APP_ERROR | 程序执行返回的错误码,请参考APP_ERROR说明。 |
父主题: TensorBase
GetTensorType
函数功能
用于获取Tensor的内存类型。
函数原型
MemoryData::MemoryType GetTensorType() const;
返回参数说明
| 数据结构 | 说明 |
|---|---|
| MemoryType | 申请的内存类型: ● MEMORY_HOST:对应Host侧。 ● MEMORY_DEVICE:对应Device侧。 ● MEMORY_DVPP:对应DVPP侧。 ● MEMORY_HOST_MALLOC:对应malloc申请内存。 ● MEMORY_HOST_NEW:对应new申请内存。 |
父主题: TensorBase
GetSize
函数功能
用于获取张量数据对应内存大小。
size大小应与实际内存大小一致,否则可能会导致程序出现coredump情况。
函数原型
size_t GetSize() const;
返回参数说明
| 数据结构 | 说明 |
|---|---|
| size_t | 返回具体数值。 |
父主题: TensorBase
在线提单