Paddle 0-d Tensor 使用指南

Paddle 0-d Tensor 使用指南

1. 0-d Tensor 的定义

在深度学习框架中,Tensor 是存储和操作数据的基本数据结构。一个 Tensor 可以有 0 到任意多的维度,每个维度对应一个 shape 值。而 0-d Tensor,顾名思义,就是一个无任何维度的 Tensor,也被称为标量(scalar) Tensor。

从数学的角度来看,0维 Tensor 可以看作是一个单个的数值,没有向量或矩阵等更高维度的结构。例如:

import paddle

# 创建0维Tensor
scalar = paddle.to_tensor(3.14)
print(scalar, scalar.shape)
# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
#        3.14000010) []

可以看到,这个 scalar 是一个单个的浮点数 3.14,它的 shape 是一个空列表 [],表示没有任何维度。0-d Tensor 其对应的是Numpy的 0-D array,可以表示为 np.array(10.),其shape为 [],维度为 0,size为 1。

对比之下,一维的 Tensor 则表示一个向量,其对应的是 Numpy的 1-D array,如果只有 1 个元素,可以表示为 np.array([10.]),其 shape 为 [1],维度为 1,size为 1。下面我们来看一个一维 Tensor 的例子:

vector = paddle.to_tensor([1, 2, 3])
print(vector, vector.shape)
# Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
#        [1, 2, 3]) [3]

这里vector是一个一维张量,有3个元素,对应shape为[3]。

以上从数学角度上区分了 0-d Tensor 和 1-d Tensor,在物理学中,标量和矢量是两个基本的物理量概念。标量只有一个数值,没有方向; 而矢量除了数值外,还附带一个确定的方向。

0-d Tensor对应着物理学中的标量概念。一个 0-d Tensor,如 3.14、2.78 等,仅仅表示一个单一的数值,没有任何其他维度的信息。它可以表示一些简单的物理量,如温度、质量、电荷等。

而 1-d Tensor 则对应着矢量的概念。即使只有1个元素,如 [5.0],它也不是一个纯标量,而是一个有确定方向的向量。这个方向在物理意义上可能表示力、速度、电场强度等有方向性的物理量。

尽管在代码实现上,0-d 和 1-d Tensor 可能没有太大的区别,但它们对应的数学和物理概念是不同的。作为开发者,明确这种区别将有助于写出更加符合数学规范、更加符合物理意义的代码,从而减少逻辑错误和调试成本。

2. 0-d Tensor 滥用为 1-d Tensor 的危害

滥用 0d Tensor 来代替1维单元素Tensor(shape为[1]) 给使用体验带来一些负面影响,主要体现在以下几个方面:

2.1 潜在的纬度错误

标量张量与仅含有一个元素的向量张量容易造成混淆,它们的元素个数相同,但在数学定义上完全不同。若将其形状表示为 shape=[1],则无法区分标量和向量,这与数学语义和行业通用的计算规则相悖,可能导致模型出现意料之外的错误,并增加开发调试成本。

由于 0-d 和 1-d Tensor 在数学上有着本质区别,很多 API 在处理这两种情况时的行为也不尽相同。如果不加区分地混用,就可能导致 API 的行为出现异常。

import torch

x = torch.tensor(3.14) 
out = torch.stack([x, x])
print(out.shape)  # 输出 torch.Size([2])  0D升为1D,符合预期

如果 Paddle 不支持 0-d Tensor,就需要额外判断 x 是否为 1D,然后补squeeze来矫正结果,这造成了代码的冗余与可读性降低。写法如下:

import paddle

x = paddle.to_tensor(3.14)

# Paddle 写法需4行:需要额外判断x是否为1D,是就需要补squeeze来矫正结果以对齐 pytorch
if len(x.shape) == 1:
    # 因为用shape=[1]的1维替代0维,导致x[0]还是1维,stack结果为2维,出现异常升维,需要补squeeze来校正维度
    out = paddle.stack([x[0], x[0
# Kunlunxin XPU ## Requirements - OS: Linux - Python: 3.10 - XPU Model: P800 - XPU Driver Version: ≥ 5.0.21.10 - XPU Firmware Version: ≥ 1.31 Verified platform: - CPU: INTEL(R) XEON(R) PLATINUM 8563C / Hygon C86-4G 7490 64-core Processor - Memory: 2T - Disk: 4T - OS: CentOS release 7.6 (Final) - Python: 3.10 - XPU Model: P800 (OAM Edition) - XPU Driver Version: 5.0.21.10 - XPU Firmware Version: 1.31 **Note:** Currently, only INTEL or Hygon CPU-based P800 (OAM Edition) servers have been verified. Other CPU types and P800 (PCIe Edition) servers have not been tested yet. ## 1. Set up using Docker (Recommended) ```bash mkdir Work cd Work docker pull ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlepaddle/fastdeploy-xpu:2.0.0 docker run --name fastdeploy-xpu --net=host -itd --privileged -v $PWD:/Work -w /Work \ ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlepaddle/fastdeploy-xpu:2.0.0 \ /bin/bash docker exec -it fastdeploy-xpu /bin/bash ``` ## 2. Set up using pre-built wheels ### Install PaddlePaddle ```bash python -m pip install paddlepaddle-xpu==3.1.0 -i https://www.paddlepaddle.org.cn/packages/stable/xpu-p800/ ``` Alternatively, you can install the latest version of PaddlePaddle (Not recommended) ```bash python -m pip install --pre paddlepaddle-xpu -i https://www.paddlepaddle.org.cn/packages/nightly/xpu-p800/ ``` ### Install FastDeploy (**Do NOT install via PyPI source**) ```bash python -m pip install fastdeploy-xpu==2.0.0 -i https://www.paddlepaddle.org.cn/packages/stable/fastdeploy-xpu-p800/ --extra-index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple ``` Alternatively, you can install the latest version of FastDeploy (Not recommended) ```bash python -m pip install --pre fastdeploy-xpu -i https://www.paddlepaddle.org.cn/packages/stable/fastdeploy-xpu-p800/ --extra-index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple ``` ## 3. Build wheel from source ### Install PaddlePaddle ```bash python -m pip install paddlepaddle-xpu==3.1.0 -i https://www.paddlepaddle.org.cn/packages/stable/xpu-p800/ ``` Alternatively, you can install the latest version of PaddlePaddle (Not recommended) ```bash python -m pip install --pre paddlepaddle-xpu -i https://www.paddlepaddle.org.cn/packages/nightly/xpu-p800/ ``` ### Download Kunlunxin Toolkit (XTDK) and XVLLM library, then set their paths. ```bash # XTDK wget https://klx-sdk-release-public.su.bcebos.com/xtdk_15fusion/dev/3.2.40.1/xtdk-llvm15-ubuntu2004_x86_64.tar.gz tar -xvf xtdk-llvm15-ubuntu2004_x86_64.tar.gz && mv xtdk-llvm15-ubuntu2004_x86_64 xtdk export CLANG_PATH=$(pwd)/xtdk # XVLLM wget https://klx-sdk-release-public.su.bcebos.com/xinfer/daily/eb/20250624/output.tar.gz tar -xvf output.tar.gz && mv output xvllm export XVLLM_PATH=$(pwd)/xvllm ``` Alternatively, you can download the latest versions of XTDK and XVLLM (Not recommended) ```bash XTDK: https://klx-sdk-release-public.su.bcebos.com/xtdk_15fusion/dev/latest/xtdk-llvm15-ubuntu2004_x86_64.tar.gz XVLLM: https://klx-sdk-release-public.su.bcebos.com/xinfer/daily/eb/latest/output.tar.gz ``` ### Download FastDeploy source code, checkout the stable branch/TAG, then compile and install. ```bash git clone https://github.com/PaddlePaddle/FastDeploy cd FastDeploy bash build.sh ``` The compiled outputs will be located in the ```FastDeploy/dist``` directory. ## Installation verification ```bash python -c "import paddle; paddle.version.show()" python -c "import paddle; paddle.utils.run_check()" python -c "from paddle.jit.marker import unified" python -c "from fastdeploy.model_executor.ops.xpu import block_attn" ``` If all the above steps execute successfully, FastDeploy is installed correctly. ## Quick start The P800 supports the deployment of the ```ERNIE-4.5-300B-A47B-Paddle``` model using the following configurations (Note: Different configurations may result in variations in performance). - 32K WINT4 with 8 XPUs (Recommended) - 128K WINT4 with 8 XPUs - 32K WINT4 with 4 XPUs ### Online serving (OpenAI API-Compatible server) Deploy an OpenAI API-compatible server using FastDeploy with the following commands: #### Start service **Deploy the ERNIE-4.5-300B-A47B-Paddle model with WINT4 precision and 32K context length on 8 XPUs(Recommended)** ```bash python -m fastdeploy.entrypoints.openai.api_server \ --model baidu/ERNIE-4.5-300B-A47B-Paddle \ --port 8188 \ --tensor-parallel-size 8 \ --max-model-len 32768 \ --max-num-seqs 64 \ --quantization "wint4" \ --gpu-memory-utilization 0.9 ``` **Deploy the ERNIE-4.5-300B-A47B-Paddle model with WINT4 precision and 128K context length on 8 XPUs** ```bash python -m fastdeploy.entrypoints.openai.api_server \ --model baidu/ERNIE-4.5-300B-A47B-Paddle \ --port 8188 \ --tensor-parallel-size 8 \ --max-model-len 131072 \ --max-num-seqs 64 \ --quantization "wint4" \ --gpu-memory-utilization 0.9 ``` **Deploy the ERNIE-4.5-300B-A47B-Paddle model with WINT4 precision and 32K context length on 4 XPUs** ```bash export XPU_VISIBLE_DEVICES="0,1,2,3" python -m fastdeploy.entrypoints.openai.api_server \ --model baidu/ERNIE-4.5-300B-A47B-Paddle \ --port 8188 \ --tensor-parallel-size 4 \ --max-model-len 32768 \ --max-num-seqs 64 \ --quantization "wint4" \ --gpu-memory-utilization 0.9 ``` Refer to [Parameters](../../parameters.md) for more options. #### Send requests Send requests using either curl or Python ```bash curl -X POST "http://0.0.0.0:8188/v1/chat/completions" \ -H "Content-Type: application/json" \ -d '{ "messages": [ {"role": "user", "content": "Where is the capital of China?"} ] }' ``` ```python import openai host = "0.0.0.0" port = "8188" client = openai.Client(base_url=f"http://{host}:{port}/v1", api_key="null") response = client.completions.create( model="null", prompt="Where is the capital of China?", stream=True, ) for chunk in response: print(chunk.choices[0].text, end='') print('\n') response = client.chat.completions.create( model="null", messages=[ {"role": "user", "content": "Where is the capital of China?"}, ], stream=True, ) for chunk in response: if chunk.choices[0].delta: print(chunk.choices[0].delta.content, end='') print('\n') ``` For detailed OpenAI protocol specifications, see [OpenAI Chat Compeltion API](https://platform.openai.com/docs/api-reference/chat/create). Differences from the standard OpenAI protocol are documented in [OpenAI Protocol-Compatible API Server](../../online_serving/README.md).
07-04
<think>我们正在讨论Tensor方法的使用,特别是参考了站内关于Tensor索引、0-D Tensor以及Tensor2Tensor的引用资料。用户希望了解Tensor方法的使用指南。结合引用内容和之前的对话历史(之前讨论过深度学习基础、核心技术等),我将重点整理Tensor操作的核心方法,特别是度操作(如None索引)、0-D Tensor的使用以及大型库中的Tensor处理实践。 根据引用[1],我们了解到None索引可以增加新度,方便后续计算(如广播机制)。引用[2][3]强调了0-D Tensor作为标量的使用方式及其在索引中的直接应用。引用[4]则提到Tensor2Tensor库简化了深度学习流程,其中Tensor操作是基础。 因此,我将从以下三个度组织回答: 1. Tensor度操作(重点:None索引与广播机制) 2. 0-D Tensor的特性与应用场景 3. 结合大型库(如T2T)的Tensor实践技巧 注意:所有数学表达式需严格按指令格式书写(行内用$...$,独立公式用$$...$$),并正确标注引用来源。 </think>### Tensor方法使用指南 结合引用资料,Tensor的核心操作方法可归纳为以下四类,重点关注**度操作**与**索引机制**: --- #### 一、度操作(核心技巧) 1. **度扩展(`None`索引)** - **作用**:通过插入新度实现广播计算 - **示例**: ```python import paddle f = paddle.to_tensor([1, 2, 3]) # shape=(3,) g = paddle.to_tensor([4, 5]) # shape=(2,) f_exp = f[:, None] # shape=(3,1) g_exp = g[None, :] # shape=(1,2) result = f_exp * g_exp # 广播乘法,输出shape=(3,2) ``` - **原理**:通过`None`(或`np.newaxis`)扩展度,使两Tensor满足广播规则 $ \mathbf{C}_{i,j} = \mathbf{A}_{i,1} \times \mathbf{B}_{1,j} $[^1] 2. **度压缩** - **`squeeze()`**:删除长度为1的度 ```python x = paddle.ones([2,1,4]) y = x.squeeze(axis=1) # shape=(2,4) ``` - **`reshape()`**:改变度结构 ```python z = paddle.reshape(y, [4,2]) # shape=(4,2) ``` --- #### 二、索引机制(关键差异) | 索引类型 | 特性 | 示例 | |------------|----------------------------------------------------------------------|-------------------------------| | **基础索引** | 返回原Tensor的视图(View),共享数据内存 | `a[1:3]`(修改会影响原Tensor) | | **高级索引** | 返回新Tensor的拷贝,独立内存 | `a[[0,2]]` | | **0-D索引** | 直接使用标量Tensor索引(飞桨2.5+特性): | | | ```python index = paddle.to_tensor(1, dtype='int32') a = paddle.to_tensor([[1,2],[3,4]]) print(a[index]) # 输出:[3,4] [^3] ``` | > ⚠️ **注意**:动态图模式下基础索引是视图(共享数据),静态图下生成新Tensor[^1]。 --- #### 三、广播计算规则 当操作不同形状的Tensor时,按以下规则自动对齐: 1. 从尾部度开始向前逐比较 2. 度兼容条件: - 长相等 - 其中一个长为1(可广播扩展) 3. **数学表达**: 对于$\mathbf{A} \in \mathbb{R}^{3 \times 1}$和$\mathbf{B} \in \mathbb{R}^{1 \times 2}$, $$ \mathbf{C}_{i,j} = \mathbf{A}_{i,0} \times \mathbf{B}_{0,j} \quad \text{(自动扩展)} $$ ![广播示意图](https://www.tensorflow.org/images/broadcasting.png)(来源:TensorFlow文档) --- #### 四、大型框架实践(Tensor2Tensor示例) 引用[4]中Tensor2Tensor库的典型流程: ```python import tensor2tensor as t2t # 创建Transformer模型的输入Tensor inputs = t2t.data_generator.problem_hparams.make_example_tensor( shape=[batch_size, seq_len], dtype=tf.int32 ) # 自动度广播处理 encoded = transformer_model(inputs) # 内部自动处理度对齐[^4] ``` --- ### 使用建议 1. **优先使用`None`扩展度**而非`reshape`,避免意外内存复制 2. **索引选择原则**: - 需修改数据 → 用高级索引(避免污染原数据) - 仅读取数据 → 用基础索引(节省内存) 3. **0-D Tensor优势**: - 直接参与标量计算:`loss = paddle.mean(output)` - 简化索引逻辑[^3] ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值