Skip to content

Conversation

Qin-sx
Copy link
Contributor

@Qin-sx Qin-sx commented Jun 4, 2025

PR Category

User Experience

PR Types

Bug fixes

Description

当前问题
当输入为tensor的list时,输出为tensor

解决方案
当输入为tensor的list时,进行判断,现在输出也为tensor的list

	modified:   python/paddle/tensor/manipulation.py
	modified:   test/legacy_test/test_atleast_xd.py
Copy link

paddle-bot bot commented Jun 4, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Jun 4, 2025
@codecov-commenter
Copy link

codecov-commenter commented Jun 4, 2025

Codecov Report

Attention: Patch coverage is 0% with 9 lines in your changes missing coverage. Please review.

Please upload report for BASE (develop@917ecbd). Learn more about missing BASE report.

Files with missing lines Patch % Lines
python/paddle/tensor/manipulation.py 0.00% 9 Missing ⚠️

❌ Your patch status has failed because the patch coverage (0.00%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #73102   +/-   ##
==========================================
  Coverage           ?    0.00%           
==========================================
  Files              ?        1           
  Lines              ?        9           
  Branches           ?        0           
==========================================
  Hits               ?        0           
  Misses             ?        9           
  Partials           ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Qin-sx
Copy link
Contributor Author

Qin-sx commented Jun 5, 2025

CI: https://xly.bce.baidu.com/paddlepaddle/paddle/newipipe/detail/12905852/job/29455975
可否麻烦帮忙看一下,我在代码中加入print,测试中的代码应该是覆盖了新增代码的

    if len(inputs) == 1 and isinstance(inputs[0], list):
        if all(
            isinstance(
                item,
                (
                    paddle.Tensor,
                    paddle.base.framework.Variable,
                    paddle.base.libpaddle.pir.Value,
                ),
            )
            for item in inputs[0]
        ):
            inputs = inputs[0]
            print("in atleast_1d inputs:", inputs)
in atleast_1d inputs: [Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True,
       123), Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
       [1., 2., 3.]), Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
       [[1, 2],
        [3, 4]])]
in atleast_2d inputs: [Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True,
       123), Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
       [1., 2., 3.]), Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
       [[1, 2],
        [3, 4]])]
in atleast_3d inputs: [Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True,
       123), Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
       [1., 2., 3.]), Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
       [[1, 2],
        [3, 4]])]

@Qin-sx
Copy link
Contributor Author

Qin-sx commented Jun 5, 2025

PR-CI-Coverage
这个CI好像无法重新执行,“该流水线禁止重新执行!”

@@ -5277,6 +5277,20 @@ def atleast_1d(*inputs, name=None):
[123]), Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.23000002]])]
"""
if len(inputs) == 1 and isinstance(inputs[0], list):
Copy link
Contributor

Choose a reason for hiding this comment

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

if len(inputs) == 1 and isinstance(inputs[0], (list, tuple)):
    inputs = inputs[0]

不用判断list里是什么

Copy link
Contributor Author

Choose a reason for hiding this comment

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

您好,请问是指不用判断list里面是不是paddle.tensor吗?因为好像原本的测试中有list包含list的情况,所以不判断的话测试无法通过。

Copy link
Contributor

@zhwesky2010 zhwesky2010 Jun 9, 2025

Choose a reason for hiding this comment

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

具体是哪个case呢?

list包含list情况按这么写,应该也没问题吧?

if len(inputs) == 1 and isinstance(inputs[0], (list, tuple)): 
    inputs = inputs[0]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

import numpy as np

import paddle
import torch


# inputs = ([[[137]]],)
inputs = [[[[137]]]]

print("Inputs:", inputs)
print("type Inputs:", type(inputs))
print("type Inputs[0]:", type(inputs[0]))

result = paddle.atleast_1d(*inputs)
print("Result:", type(result))
print("Result:", result)

# result_pt = torch.atleast_1d(*inputs)
# print("Result_pt:", type(result_pt))
# print("Result_pt:", result_pt)
Inputs: [[[[137]]]]
type Inputs: <class 'list'>
type Inputs[0]: <class 'list'>
len(out) == 1
Result: <class 'paddle.Tensor'>
Result: Tensor(shape=[1, 1, 1], dtype=int64, place=Place(gpu:0), stop_gradient=True,
       [[[137]]])

对于上面这个例子,paddle会将[[[[137]]]]的list改为[[[137]]]的tensor。pytorch应该不支持这个输入。
如果按照下面的方式写的话,paddle会将[[[[137]]]]的list改为[[137]]的tensor。会比之前少一个维度。

if len(inputs) == 1 and isinstance(inputs[0], (list, tuple)): 
    inputs = inputs[0]
Inputs: [[[[137]]]]
type Inputs: <class 'list'>
type Inputs[0]: <class 'list'>
len(out) == 1
Result: <class 'paddle.Tensor'>
Result: Tensor(shape=[1, 1], dtype=int64, place=Place(gpu:0), stop_gradient=True,
       [[137]])

@@ -5277,6 +5277,20 @@ def atleast_1d(*inputs, name=None):
[123]), Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.23000002]])]
"""
if len(inputs) == 1 and isinstance(inputs[0], list):
if all(
Copy link
Contributor

Choose a reason for hiding this comment

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

这里改成any,规则是:如果有任意一个Tensor,就可以按list来分解,如果全部为子list,就不用分解。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

嗯,收到,已修改

@@ -5277,6 +5277,20 @@ def atleast_1d(*inputs, name=None):
[123]), Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.23000002]])]
"""
if len(inputs) == 1 and isinstance(inputs[0], list):
Copy link
Contributor

Choose a reason for hiding this comment

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

这里改成 isinstance(inputs[0], (list, tuple)):

覆盖 输入一个tuple(Tensor)的情况

Copy link
Contributor Author

Choose a reason for hiding this comment

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

嗯,收到,已修改

	modified:   python/paddle/tensor/manipulation.py
@Qin-sx
Copy link
Contributor Author

Qin-sx commented Jun 11, 2025

CI: https://xly.bce.baidu.com/paddlepaddle/paddle/newipipe/detail/12905852/job/29455975 可否麻烦帮忙看一下,我在代码中加入print,测试中的代码应该是覆盖了新增代码的

    if len(inputs) == 1 and isinstance(inputs[0], list):
        if all(
            isinstance(
                item,
                (
                    paddle.Tensor,
                    paddle.base.framework.Variable,
                    paddle.base.libpaddle.pir.Value,
                ),
            )
            for item in inputs[0]
        ):
            inputs = inputs[0]
            print("in atleast_1d inputs:", inputs)
in atleast_1d inputs: [Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True,
       123), Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
       [1., 2., 3.]), Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
       [[1, 2],
        [3, 4]])]
in atleast_2d inputs: [Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True,
       123), Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
       [1., 2., 3.]), Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
       [[1, 2],
        [3, 4]])]
in atleast_3d inputs: [Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True,
       123), Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
       [1., 2., 3.]), Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
       [[1, 2],
        [3, 4]])]

CI还是有同样的问题,测试的代码应该可以覆盖

class TestAtleastWithTensorList(unittest.TestCase):
"""Test when input is a list of paddle tensors"""

def test_tensor_list_input(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

再加几个case,分别是 list包含list、Tensor list、tuple包含tuple、Tensor tuple,目前只有第二种Tensor list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

嗯,收到,已添加

Qin-sx added 2 commits June 12, 2025 14:33
	modified:   test/legacy_test/test_atleast_xd.py
	modified:   test/legacy_test/test_atleast_xd.py
Copy link
Contributor

@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
Copy link
Contributor

@Qin-sx 看下这个CE-Framework为何失败,是否有不兼容问题

@Qin-sx
Copy link
Contributor Author

Qin-sx commented Jun 17, 2025

@Qin-sx 看下这个CE-Framework为何失败,是否有不兼容问题

嗯,我重新执行通过了,应该是偶发的。我记得之前提交的版本应该都是通过的。

@zhwesky2010 zhwesky2010 merged commit b6b5d60 into PaddlePaddle:develop Jun 23, 2025
50 of 51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants