Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon 7th No.42】NO.42 为 Paddle 代码转换工具新增 API 转换规则(第 9 组)-part #6893

Merged
merged 13 commits into from
Oct 16, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## [ paddle 参数更多 ]torch.Tensor.bitwise_left_shift

### [torch.Tensor.bitwise_left_shift](https://pytorch.org/docs/stable/generated/torch.Tensor.bitwise_left_shift.html#torch-tensor-bitwise-left-shift)

```python
torch.Tensor.bitwise_left_shift(other)
```

### [paddle.Tensor.bitwise_left_shift]()

```python
paddle.Tensor.bitwise_left_shift(y, is_arithmetic=True)
```

其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------- | ------------------------------------------------------------------- |
| other | y | 表示输入的 Tensor ,仅参数名不一致。 |
| - | is_arithmetic | 用于表明是否执行算术位移, PyTorch 无此参数, Paddle 保持默认即可。 |

### 转写示例

```python
# PyTorch 写法
out = x.bitwise_left_shift(y)

# Paddle 写法
out = x.bitwise_left_shift(y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## [ paddle 参数更多 ]torch.Tensor.bitwise_left_shift_

### [torch.Tensor.bitwise_left_shift_](https://pytorch.org/docs/stable/generated/torch.Tensor.bitwise_left_shift_.html#torch-tensor-bitwise-left-shift)

```python
torch.Tensor.bitwise_left_shift_(other)
```

### [paddle.Tensor.bitwise_left_shift_]()

```python
paddle.Tensor.bitwise_left_shift_(y, is_arithmetic=True)
```

其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------- | ------------------------------------------------------------------- |
| other | y | 表示输入的 Tensor ,仅参数名不一致。 |
| - | is_arithmetic | 用于表明是否执行算术位移, PyTorch 无此参数, Paddle 保持默认即可。 |

### 转写示例

```python
# PyTorch 写法
out = x.bitwise_left_shift_(y)

# Paddle 写法
out = x.bitwise_left_shift_(y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## [ paddle 参数更多 ]torch.Tensor.bitwise_right_shift

### [torch.Tensor.bitwise_right_shift](https://pytorch.org/docs/stable/generated/torch.Tensor.bitwise_right_shift.html#torch-tensor-bitwise-right-shift)

```python
torch.Tensor.bitwise_right_shift(other)
```

### [paddle.Tensor.bitwise_right_shift]()

```python
paddle.Tensor.bitwise_right_shift(y, is_arithmetic=True)
```

其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------- | ------------------------------------------------------------------- |
| other | y | 表示输入的 Tensor ,仅参数名不一致。 |
| - | is_arithmetic | 用于表明是否执行算术位移, PyTorch 无此参数, Paddle 保持默认即可。 |

### 转写示例

```python
# PyTorch 写法
out = x.bitwise_right_shift(y)

# Paddle 写法
out = x.bitwise_right_shift(y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## [ paddle 参数更多 ]torch.Tensor.bitwise_right_shift_

### [torch.Tensor.bitwise_right_shift_](https://pytorch.org/docs/stable/generated/torch.Tensor.bitwise_right_shift_.html#torch-tensor-bitwise-right-shift)

```python
torch.Tensor.bitwise_right_shift_(other)
```

### [paddle.Tensor.bitwise_right_shift_]()

```python
paddle.Tensor.bitwise_right_shift_(y, is_arithmetic=True)
```

其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------- | ------------------------------------------------------------------- |
| other | y | 表示输入的 Tensor ,仅参数名不一致。 |
| - | is_arithmetic | 用于表明是否执行算术位移, PyTorch 无此参数, Paddle 保持默认即可。 |

### 转写示例

```python
# PyTorch 写法
out = x.bitwise_right_shift_(y)

# Paddle 写法
out = x.bitwise_right_shift_(y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [ 无参数 ] torch.Tensor.col_indices

### [torch.Tensor.col_indices](https://pytorch.org/docs/stable/generated/torch.Tensor.col_indices.html)

```python
torch.Tensor.col_indices()
```

### [paddle.Tensor.cols]()

```python
paddle.Tensor.cols()
```

两者功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [ 无参数 ] torch.Tensor.crow_indices

### [torch.Tensor.crow_indices](https://pytorch.org/docs/stable/generated/torch.Tensor.crow_indices.html)

```python
torch.Tensor.crow_indices()
```

### [paddle.Tensor.crows]()

```python
paddle.Tensor.crows()
```

两者功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [ 无参数 ] torch.Tensor.data_ptr

### [torch.Tensor.data_ptr](https://pytorch.org/docs/stable/generated/torch.Tensor.data_ptr.html)

```python
torch.Tensor.data_ptr()
```

### [paddle.Tensor.data_ptr](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html)

```python
paddle.Tensor.data_ptr()
```

两者功能一致。
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ] torch.Tensor.dim_order

### [torch.Tensor.dim_order](https://pytorch.org/docs/stable/generated/torch.Tensor.dim_order.html?highlight=dim_order#torch.Tensor.dim_order)

```python
torch.Tensor.dim_order()
```

Paddle 无此 API,需要组合实现。获取张量在内存中的物理布局,PaddlePaddle 的 Tensor 默认是 contiguous 的, 因此可直接返回一个从 0 到 Tensor 的维度长度的列表即可。

### 转写示例

```python
# PyTorch 写法
y = x.dim_order()

# Paddle 写法
y = tuple([i for i in range(len(x.shape))])
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## [ 无参数 ] torch.Tensor.is_sparse_csr

### [torch.Tensor.is_sparse_csr](https://pytorch.org/docs/stable/generated/torch.Tensor.is_sparse_csr.html)

```python
torch.Tensor.is_sparse_csr
```

### [paddle.Tensor.is_sparse_csr](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html)

```python
paddle.Tensor.is_sparse_csr()
```

两者功能一致,但使用方式不一致,前者可以直接访问属性,后者需要调用方法,具体如下:

### 转写示例

```python
# torch 版本可以直接访问属性
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

转写示例写简单点,核心一行就行,不用各种print、准备随机变量这些

x.is_sparse_csr

# Paddle 版本需要调用
x.is_sparse_csr()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [ 无参数 ]torch.Tensor.item_size

### [torch.Tensor.item_size](https://pytorch.org/docs/stable/generated/torch.Tensor.itemsize.html)

```python
torch.Tensor.item_size()
```

### [paddle.Tensor.element_size](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#element-size)

```python
paddle.Tensor.element_size()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这两个数值验证了能对上吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
验证倒是还没有

```

两者功能一致,无参数。
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## [ 组合替代实现 ]torch.Tensor.nbytes

### [torch.Tensor.nbytes](https://pytorch.org/docs/stable/generated/torch.Tensor.nbytes.html)

```python
torch.Tensor.nbytes
```

Paddle 无此 API,需要组合实现。

### 转写示例

```python
# PyTorch 写法
y = a.nbytes

# Paddle 写法
y = a.size * a.element_size()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## [ torch 参数更多 ] torch.Tensor.stride

### [torch.Tensor.stride](https://pytorch.org/docs/stable/generated/torch.Tensor.stride.html#torch-tensor-stride)

```python
torch.Tensor.stride(dim=None)
```

### [paddle.Tensor.get_strides](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/Tensor_cn.html#tensor)

```python
paddle.Tensor.get_strides()
```

PyTorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射


| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | --------------------------------------------------------------------------------------------- |
| dim | - | 返回指定维度的步长, Pytorch 为可选值,默认返回全部步长,此时无需转写,当有输入值时,需要转写。 |

### 转写示例

```python
# torch 版本, 默认返回全部
x.stride(dim=None)

# Paddle 版本
if dim is not None:
x.get_strides()[dim]
else:
x.get_strides()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## [ paddle 参数更多 ]torch.Tensor.to_sparse_coo

### [torch.Tensor.to_sparse_coo](https://pytorch.org/docs/stable/generated/torch.Tensor.to_sparse_coo.html)

```python
torch.Tensor.to_sparse_coo()
```

### [paddle.Tensor.to_sparse_coo]()

```python
paddle.Tensor.to_sparse_coo(sparse_dim)
```

其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射


| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | --------------------------------------------------------------------------------------------- |
| - | sparse_dim | 在新的稀疏张量中包含的稀疏维度的数量,pytorch 中无此参数,paddle 令其为 tensor 输入维度即可。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## [ paddle 参数更多 ]torch.bitwise_left_shift

### [torch.bitwise_left_shift](https://pytorch.org/docs/stable/generated/torch.bitwise_left_shift.html)

```python
torch.bitwise_left_shift(input, other, *, out=None)
```

### [paddle.bitwise_left_shift](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/bitwise_left_shift_cn.html#bitwise-left-shift)

```python
paddle.bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None)
```

其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------- | ------------------------------------------------------------------- |
| input | x | 表示输入的 Tensor ,仅参数名不一致。 |
| other | y | 表示输入的 Tensor ,仅参数名不一致。 |
| out | out | 表示输出的 Tensor。 |
| - | is_arithmetic | 用于表明是否执行算术位移, PyTorch 无此参数, Paddle 保持默认即可。 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## [ paddle 参数更多 ]torch.bitwise_right_shift

### [torch.bitwise_right_shift](https://pytorch.org/docs/stable/generated/torch.bitwise_right_shift.html)

```python
torch.bitwise_right_shift(input, other, *, out=None)
```

### [paddle.bitwise_right_shift](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/bitwise_right_shift_cn.html#bitwise-right-shift)

```python
paddle.bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None)
```

其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下:

### 参数映射

| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------- | ------------------------------------------------------------------- |
| input | x | 表示输入的 Tensor ,仅参数名不一致。 |
| other | y | 表示输入的 Tensor ,仅参数名不一致。 |
| out | out | 表示输出的 Tensor。 |
| - | is_arithmetic | 用于表明是否执行算术位移, PyTorch 无此参数, Paddle 保持默认即可。 |