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 5th No.42】 为Paddle代码转换工具新增API转换规则 (第1组 编号1-20)-part #6212

Merged
merged 10 commits into from
Oct 19, 2023

Conversation

Li-fAngyU
Copy link
Contributor

@Li-fAngyU Li-fAngyU commented Sep 26, 2023

注: 对于torch.Tensor.resize_(*sizes, memory_format=torch.contiguous_format) 这个API,因为torch.Tensor.resize_支持输入size比原先Tensor更大或更小的维度,而目前Paddle没有该对应功能的实现。

# Torch code:
x = torch.tensor([[1, 2], [3, 4], [5, 6]])
x.resize_(2, 2)
# tensor([[ 1,  2],
#         [ 3,  4]])

@paddle-bot
Copy link

paddle-bot bot commented Sep 26, 2023

感谢你贡献飞桨文档,文档预览构建中,Docs-New 跑完后即可预览,预览链接:http://preview-pr-6212.paddle-docs-preview.paddlepaddle.org.cn/documentation/docs/zh/api/index_cn.html
预览工具的更多说明,请参考:飞桨文档预览工具

@paddle-bot
Copy link

paddle-bot bot commented Sep 26, 2023

✅ This PR's description meets the template requirements!
Please wait for other CI results.

@Li-fAngyU Li-fAngyU changed the title 【Hackthon 5】No.42 为Paddle代码转换工具新增API转换规则 (第1组 编号1-20) 【Hackthon 5 No.42】 为Paddle代码转换工具新增API转换规则 (第1组 编号1-20) Sep 26, 2023
@luotao1 luotao1 added the PaddlePaddle Hackathon 飞桨黑客松活动issue与PR label Sep 26, 2023
@@ -917,7 +917,9 @@
| 300 | [torch.ShortTensor](https://pytorch.org/docs/stable/tensors.html) |[paddle.to_tensor](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/to_tensor_cn.html#to-tensor)| 仅 paddle 参数更多 , [差异对比](https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/model_convert/convert_from_pytorch/api_difference/Tensor/torch.ShortTensor.md)|
| 301 | [torch.Tensor.copysign](https://pytorch.org/docs/1.13/generated/torch.Tensor.copysign.html?highlight=torch+tensor+copysign#torch.Tensor.copysign) | | 功能缺失 |
| 302 | [torch.Tensor.dequantize](https://pytorch.org/docs/1.13/generated/torch.Tensor.dequantize.html?highlight=torch+tensor+dequantize#torch.Tensor.dequantize) | | 功能缺失 |

| 303 | [torch.Tensor.is_pinned](https://pytorch.org/docs/stable/generated/torch.Tensor.is_pinned.html?highlight=is_pinned#torch.Tensor.is_pinned) | | 功能缺失 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个有办法组合替代实现吧,比如说判断x.place

Copy link
Contributor Author

@Li-fAngyU Li-fAngyU Oct 11, 2023

Choose a reason for hiding this comment

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

谢谢审查,经过查阅资料是可以组合替代实现判断张量是否在固定内存上,但是Paddle和Torch的pin_memory有一点不太一致。

Paddle 代码:

pin_memory_Tensor = paddle.to_tensor(1, place=paddle.CUDAPinnedPlace())
print(pin_memory_Tensor.place)
# Place(gpu_pinned)

Torch 代码:

x = torch.randn(4,4)
print(x.pin_memory().is_pinned())
# True
print(x.cuda().pin_memory().is_pinned())
# 会报错
# RuntimeError: cannot pin 'torch.cuda.FloatTensor' only dense CPU tensors can be pinned

即 Paddle 仅支持 CUDA Pin, 而 Torch 仅支持 dense CPU tensors to pin memory。

请问是否忽略这一点呢?

Copy link
Collaborator

Choose a reason for hiding this comment

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

这个点不用管,只需关注torch的代码转换为paddle如何呈现(前提是torch代码可正常运行),不关注其他的地方。上文中的 x.is_pinned() 如果转为 paddle的写法,'pinned' in x.place 这种写法有问题吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的,可以通过 'pinned' in str(x.place) 去判断。

@@ -917,7 +917,9 @@
| 300 | [torch.ShortTensor](https://pytorch.org/docs/stable/tensors.html) |[paddle.to_tensor](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/to_tensor_cn.html#to-tensor)| 仅 paddle 参数更多 , [差异对比](https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/model_convert/convert_from_pytorch/api_difference/Tensor/torch.ShortTensor.md)|
| 301 | [torch.Tensor.copysign](https://pytorch.org/docs/1.13/generated/torch.Tensor.copysign.html?highlight=torch+tensor+copysign#torch.Tensor.copysign) | | 功能缺失 |
| 302 | [torch.Tensor.dequantize](https://pytorch.org/docs/1.13/generated/torch.Tensor.dequantize.html?highlight=torch+tensor+dequantize#torch.Tensor.dequantize) | | 功能缺失 |

| 303 | [torch.Tensor.is_pinned](https://pytorch.org/docs/stable/generated/torch.Tensor.is_pinned.html?highlight=is_pinned#torch.Tensor.is_pinned) | | 功能缺失 |
| 304 | [torch.Tensor.sum_to_size](https://pytorch.org/docs/stable/generated/torch.Tensor.sum_to_size.html?highlight=sum_to_size#torch.Tensor.sum_to_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.

torch.sum_to_size 的实现是借助 sum_to 实现的其源码如下:

template <typename T>
inline Tensor _sum_to(
    Tensor tensor,
    const c10::ArrayRef<T> shape,
    bool always_return_non_view = false) {
  if (shape.size() == 0) {
    return tensor.sum();
  }

  auto sizes = at::symint::sizes<T>(tensor);
  c10::SmallVector<int64_t, 8> reduce_dims;
  const int64_t leading_dims = sizes.size() - shape.size();
  for (const auto i : c10::irange(leading_dims)) {
    reduce_dims.push_back(i);
  }
  for (int64_t i = leading_dims; i < static_cast<int64_t>(sizes.size()); ++i) {
    if (shape[i - leading_dims] == 1 && sizes[i] != 1) {
      reduce_dims.push_back(i);
    }
  }

  if (!reduce_dims.empty()) {
    tensor = tensor.sum(reduce_dims, /*keepdim=*/true);
  }

  if (always_return_non_view) {
    // This is only actually used by the functionalization pass.
    // We want to be able to guarantee that this function doesn't return a view
    // of the input.
    return leading_dims > 0 ? at::symint::view_copy<T>(tensor, shape)
                            : tensor.clone();
  } else {
    return leading_dims > 0 ? at::symint::view<T>(tensor, shape) : tensor;
  }
}

由于 Paddle 中没有 sum_to API 因此我不知道该如何去组合替代实现 sum_to_size.

Copy link
Collaborator

Choose a reason for hiding this comment

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

这个就先按功能缺失来吧

@Li-fAngyU Li-fAngyU requested a review from zhwesky2010 October 11, 2023 13:47
Copy link
Collaborator

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

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

上面有些评论看起来还没有改,但是mark_resolved了,有些描述不符合模板规范

@Li-fAngyU
Copy link
Contributor Author

抱歉疏忽了,已修改!


| PyTorch | PaddlePaddle | 备注 |
| ------- | ------------ | ------------------------------ |
| *shape | shape_or_dtype | 指定的维度。 Pytorch 参数 shape 既可以是可变参数,也可以是 list/tuple/torch.Size/dtype 的形式, Paddle 参数 shape_or_dtype 为 list/tuple/dtype 的形式。需要进行转写。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

对于可变参数的用法,需要进行转写

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| input | x | 表示输入的 Tensor 。 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

你这个写了 仅参数名不一致 吗?

torch.cummax(x,1, out=(values, indices))

# Paddle 写法
paddle.assign(paddle.cummax(x,1), (values, indices))
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

@Li-fAngyU Li-fAngyU Oct 18, 2023

Choose a reason for hiding this comment

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

这里是参考了其他文档, 比如 torch.histc.md

# Pytorch 写法
torch.histc(x, out=y)

# Paddle 写法
paddle.assign(paddle.histogram(x).astype('float32'), y)

| q | q | 待计算的分位数。 |
| dim | axis| 指定对 x 进行计算的轴,仅参数名不一致。 |
|keepdim|keepdim| 是否在输出 Tensor 中保留减小的维度。|
|interpolation| - | 当所需分位数位于两个数据点之间时使用的插值方法,Paddle 无此参数,需要转写,Paddle 暂无转写方式。|
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个 需要转写 不用写

#### out:指定输出
```python
# Pytorch 写法
torch.nanquantile(torch.tensor([float('nan'), 1., 2., 3.]), 0.6, interpolation='linear', out=y)
Copy link
Collaborator

Choose a reason for hiding this comment

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

interpolation 这个不能转写,写示例不用加上

@@ -917,7 +917,9 @@
| 300 | [torch.ShortTensor](https://pytorch.org/docs/stable/tensors.html) |[paddle.to_tensor](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/to_tensor_cn.html#to-tensor)| 仅 paddle 参数更多 , [差异对比](https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/model_convert/convert_from_pytorch/api_difference/Tensor/torch.ShortTensor.md)|
| 301 | [torch.Tensor.copysign](https://pytorch.org/docs/1.13/generated/torch.Tensor.copysign.html?highlight=torch+tensor+copysign#torch.Tensor.copysign) | | 功能缺失 |
| 302 | [torch.Tensor.dequantize](https://pytorch.org/docs/1.13/generated/torch.Tensor.dequantize.html?highlight=torch+tensor+dequantize#torch.Tensor.dequantize) | | 功能缺失 |

| 303 | [torch.Tensor.is_pinned](https://pytorch.org/docs/stable/generated/torch.Tensor.is_pinned.html?highlight=is_pinned#torch.Tensor.is_pinned) | | 功能缺失 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个已经给了你转写替代方案

@@ -917,7 +917,9 @@
| 300 | [torch.ShortTensor](https://pytorch.org/docs/stable/tensors.html) |[paddle.to_tensor](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/to_tensor_cn.html#to-tensor)| 仅 paddle 参数更多 , [差异对比](https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/model_convert/convert_from_pytorch/api_difference/Tensor/torch.ShortTensor.md)|
| 301 | [torch.Tensor.copysign](https://pytorch.org/docs/1.13/generated/torch.Tensor.copysign.html?highlight=torch+tensor+copysign#torch.Tensor.copysign) | | 功能缺失 |
| 302 | [torch.Tensor.dequantize](https://pytorch.org/docs/1.13/generated/torch.Tensor.dequantize.html?highlight=torch+tensor+dequantize#torch.Tensor.dequantize) | | 功能缺失 |

| 303 | [torch.Tensor.is_pinned](https://pytorch.org/docs/stable/generated/torch.Tensor.is_pinned.html?highlight=is_pinned#torch.Tensor.is_pinned) | | 功能缺失 |
| 304 | [torch.Tensor.sum_to_size](https://pytorch.org/docs/stable/generated/torch.Tensor.sum_to_size.html?highlight=sum_to_size#torch.Tensor.sum_to_size) | | 功能缺失 |
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个就先按功能缺失来吧

@Li-fAngyU
Copy link
Contributor Author

Done! 已对上述问题进行了修改,其中针对 cummax 的转写示例还未改动。

@Li-fAngyU Li-fAngyU requested a review from zhwesky2010 October 18, 2023 03:01
Copy link
Collaborator

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

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

LGTM

@zhwesky2010 zhwesky2010 merged commit 84a39dd into PaddlePaddle:develop Oct 19, 2023
1 check passed
@luotao1 luotao1 changed the title 【Hackthon 5 No.42】 为Paddle代码转换工具新增API转换规则 (第1组 编号1-20) 【Hackthon 5 No.42】 为Paddle代码转换工具新增API转换规则 (第1组 编号1-20)-part Oct 19, 2023
@luotao1 luotao1 changed the title 【Hackthon 5 No.42】 为Paddle代码转换工具新增API转换规则 (第1组 编号1-20)-part 【Hackathon 5th No.42】 为Paddle代码转换工具新增API转换规则 (第1组 编号1-20)-part Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor PaddlePaddle Hackathon 飞桨黑客松活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants