241 lines
3.8 KiB
Markdown
241 lines
3.8 KiB
Markdown
|
|
# Tensor 张量
|
|||
|
|
|
|||
|
|
Tensor 是模型推理的输入输出类型,包含了数据信息,实现内容管理
|
|||
|
|
|
|||
|
|
## \_\_init\_\_
|
|||
|
|
|
|||
|
|
初始化 Tensor,并为 Tensor 分配内存
|
|||
|
|
|
|||
|
|
**接口形式1**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def __init__(self, handle: Handle, data: np.arrat, own_sys_data=True)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**参数说明1**
|
|||
|
|
|
|||
|
|
- handle: sail.Handle
|
|||
|
|
|
|||
|
|
设备标识 Handle
|
|||
|
|
|
|||
|
|
- array_data: numpy.array
|
|||
|
|
|
|||
|
|
利用 numpy.array 类型初始化 Tensor,其数据类型可以是 np.float32,np.int8,np.uint8
|
|||
|
|
|
|||
|
|
- own_sys_data:bool
|
|||
|
|
|
|||
|
|
指示该 Tensor 是否拥有 system memory,如果为 False,则直接将数据复制到 device memory
|
|||
|
|
|
|||
|
|
**接口形式2**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def __init__(self, handle: Handle, shape: tuple, dtype: Dtype, own_sys_data: bool, own_dev_data: bool)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**参数说明2**
|
|||
|
|
|
|||
|
|
- handle: sail.Handle
|
|||
|
|
|
|||
|
|
设备标识 Handle
|
|||
|
|
|
|||
|
|
- shape: tuple
|
|||
|
|
|
|||
|
|
设置 Tensor 的 shape
|
|||
|
|
|
|||
|
|
- dtype: sail.Dtype
|
|||
|
|
|
|||
|
|
Tensor 的数据类型
|
|||
|
|
|
|||
|
|
- own_sys_data: bool
|
|||
|
|
|
|||
|
|
指示 Tensor 是否拥有 system memory
|
|||
|
|
|
|||
|
|
- own_dev_data: bool
|
|||
|
|
|
|||
|
|
指示 Tensor 是否拥有 device memory
|
|||
|
|
|
|||
|
|
## shape
|
|||
|
|
|
|||
|
|
获取 Tensor 的 shape
|
|||
|
|
|
|||
|
|
**接口形式:**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def shape(self) -> list :
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**返回值说明:**
|
|||
|
|
|
|||
|
|
- tensor_shape : list
|
|||
|
|
|
|||
|
|
返回Tensor的shape的列表。
|
|||
|
|
|
|||
|
|
## asnumpy
|
|||
|
|
|
|||
|
|
获取Tensor中系统内存的数据,返回numpy.array类型。
|
|||
|
|
|
|||
|
|
**接口形式:**
|
|||
|
|
```python
|
|||
|
|
def asnumpy(self) -> numpy.array
|
|||
|
|
|
|||
|
|
def asnumpy(self, shape: tuple) -> numpy.array
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**参数说明:**
|
|||
|
|
|
|||
|
|
- shape: tuple
|
|||
|
|
|
|||
|
|
可对Tensor中的数据reshape,返回形状为shape的numpy.array
|
|||
|
|
|
|||
|
|
**返回值说明**
|
|||
|
|
|
|||
|
|
返回Tensor中系统内存的数据,返回类型为numpy.array。
|
|||
|
|
|
|||
|
|
## update_data
|
|||
|
|
|
|||
|
|
更新Tensor中系统内存的数据
|
|||
|
|
|
|||
|
|
**接口形式:**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def update_data(self, data: numpy.array) -> None
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**参数说明:**
|
|||
|
|
|
|||
|
|
- data: numpy.array
|
|||
|
|
|
|||
|
|
更新的数据,数据size不能超过Tensor的size,Tensor的shape将保持不变。
|
|||
|
|
|
|||
|
|
## scale_from
|
|||
|
|
|
|||
|
|
先对data按比例缩放,再将数据更新到Tensor的系统内存。
|
|||
|
|
|
|||
|
|
**接口形式:**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def scale_from(self, data: numpy.array, scale: float32)->None
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**参数说明:**
|
|||
|
|
|
|||
|
|
- data: numpy.array
|
|||
|
|
|
|||
|
|
对data进行scale,再将数据更新到Tensor的系统内存。
|
|||
|
|
|
|||
|
|
- scale: float32
|
|||
|
|
|
|||
|
|
等比例缩放时的尺度。
|
|||
|
|
|
|||
|
|
## scale_to
|
|||
|
|
|
|||
|
|
先对Tensor进行等比例缩放,再将数据返回到系统内存。
|
|||
|
|
|
|||
|
|
**接口形式:**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def scale_to(self, scale: float32)->numpy.array
|
|||
|
|
|
|||
|
|
def scale_to(self, scale: float32, shape: tuple)->numpy.array
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**参数说明:**
|
|||
|
|
|
|||
|
|
- scale: float32
|
|||
|
|
|
|||
|
|
等比例缩放时的尺度。
|
|||
|
|
|
|||
|
|
- shape: tuple
|
|||
|
|
|
|||
|
|
数据返回前可进行reshape,返回shape形状的数据。
|
|||
|
|
|
|||
|
|
**返回值说明:**
|
|||
|
|
|
|||
|
|
- data: numpy.array
|
|||
|
|
|
|||
|
|
将处理后的数据返回至系统内存,返回numpy.array
|
|||
|
|
|
|||
|
|
## reshape
|
|||
|
|
|
|||
|
|
对Tensor进行reshape
|
|||
|
|
|
|||
|
|
**接口形式:**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def reshape(self, shape: list)->None
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**参数说明:**
|
|||
|
|
|
|||
|
|
- shape: list
|
|||
|
|
|
|||
|
|
设置期望得到的新shape。
|
|||
|
|
|
|||
|
|
# own_sys_data
|
|||
|
|
|
|||
|
|
查询该Tensor是否拥有系统内存的数据指针。
|
|||
|
|
|
|||
|
|
**接口形式:**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def own_sys_data(self)->bool
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**返回值说明:**
|
|||
|
|
|
|||
|
|
- judge_ret: bool
|
|||
|
|
|
|||
|
|
如果拥有系统内存的数据指针则返回True,否则False。
|
|||
|
|
|
|||
|
|
## own_dev_data
|
|||
|
|
|
|||
|
|
查询该Tensor是否拥有设备内存的数据
|
|||
|
|
|
|||
|
|
**接口形式:**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def own_dev_data(self)->bool
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**返回值说明:**
|
|||
|
|
|
|||
|
|
- judge_ret : bool
|
|||
|
|
|
|||
|
|
如果拥有设备内存中的数据则返回True,否则False。
|
|||
|
|
|
|||
|
|
## sync_s2d
|
|||
|
|
|
|||
|
|
将Tensor中的数据从系统内存拷贝到设备内存。
|
|||
|
|
|
|||
|
|
**接口形式:**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def sync_s2d(self)->None
|
|||
|
|
|
|||
|
|
def sync_s2d(self, size)->None
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**参数说明:**
|
|||
|
|
|
|||
|
|
- size: int
|
|||
|
|
|
|||
|
|
将特定size字节的数据从系统内存拷贝到设备内存。
|
|||
|
|
|
|||
|
|
## sync_d2s
|
|||
|
|
|
|||
|
|
将Tensor中的数据从设备内存拷贝到系统内存。
|
|||
|
|
|
|||
|
|
**接口形式:**
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def sync_d2s(self)->None
|
|||
|
|
|
|||
|
|
def sync_d2s(self, size: int)->None
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**参数说明:**
|
|||
|
|
|
|||
|
|
- size: int
|
|||
|
|
|
|||
|
|
将特定size字节的数据从设备内存拷贝到系统内存。
|