sophon_ai_project/docs/sail_api/6.Tensor张量.md
2025-10-17 16:30:24 +08:00

241 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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.float32np.int8np.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的sizeTensor的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字节的数据从设备内存拷贝到系统内存。