Skip to content

Conversation

@zhiminzhang0830
Copy link
Contributor

@zhiminzhang0830 zhiminzhang0830 commented Aug 5, 2025

PR Category

User Experience

PR Types

New features

Description

add Tensor.bool, Tensor.float16, Tensor.half, Tensor.bfloat16, Tensor.float32, Tensor.float, Tensor.float64, Tensor.double, Tensor.int8, Tensor.char, Tensor.uint8, Tensor.byte, Tensor.int16, Tensor.short, Tensor.int32, Tensor.int, Tensor.int64, Tensor.long, Tensor.complex64, Tensor.complex128, Tensor.cfloat, Tensor.cdouble API

Pcard-75624

@zhiminzhang0830 zhiminzhang0830 changed the title Tensor bool add Tensor.bool API Aug 5, 2025
@zhiminzhang0830 zhiminzhang0830 changed the title add Tensor.bool API [API] add dtype conversion method Aug 5, 2025
@paddle-bot
Copy link

paddle-bot bot commented Aug 5, 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.

@codecov-commenter
Copy link

codecov-commenter commented Aug 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@befa2b5). Learn more about missing BASE report.

Additional details and impacted files
@@ Coverage Diff @@ ## develop #74416 +/- ## =========================================== Coverage ? 100.00% =========================================== Files ? 2 Lines ? 46 Branches ? 0 =========================================== Hits ? 46 Misses ? 0 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.
return _C_ops.cast(self, dtype)

def byte(self: Tensor) -> Tensor:
# since paddle don't support float to uint8, so we need to convert it to int8 first
Copy link
Contributor

Choose a reason for hiding this comment

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

这个在c++ cast kernel里能新增注册类型吗,c++里没注册上uint8的类型吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

看到cast支持转换到uint8,默认会将小于0的float数据转换为0,直接使用cast转换float 到 uint8的结果与torch.Tensor.byte不一致,所以用了两次转换暂时保证paddle.Tensor.byte与Torch一致。

# 直接转换 >>> paddle.cast(paddle.to_tensor([-1, -2, -3, 12, 2, 3], dtype='float32'), 'uint8') Tensor(shape=[6], dtype=uint8, place=Place(gpu:0), stop_gradient=True, [0 , 0 , 0 , 12, 2 , 3 ]) # 先转成int, 再转成uint8 >>> paddle.cast(paddle.to_tensor([-1., -2., -3., 12., 2., 3.], dtype='int32'), 'uint8') Tensor(shape=[6], dtype=uint8, place=Place(gpu:0), stop_gradient=True, [255, 254, 253, 12 , 2 , 3 ]) # Torch >>> torch.Tensor([-1., -2., -3., 12., 2., 3.]).byte() tensor([255, 254, 253, 12, 2, 3], dtype=torch.uint8) 
tensor = astype(self, 'int8')
return astype(tensor, 'uint8')
elif self.is_complex():
real = astype(self.real(), 'int8')
Copy link
Contributor

Choose a reason for hiding this comment

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

同上


return _C_ops.cast(self, dtype)

def byte(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

@zhwesky2010 zhwesky2010 changed the title [API] add dtype conversion method [API compatibility] add dtype conversion method Aug 6, 2025
if self.is_floating_point():
tensor = astype(self, 'int8')
return astype(tensor, 'uint8')
elif self.is_complex():
Copy link
Contributor

Choose a reason for hiding this comment

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

复数情况下在PaConvert里和torch对比下,看torch是不是也是这么处理的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已在PaConvert新增test: PaddlePaddle/PaConvert#617 ,并在本地测试通过

def test_complex64_to_byte(): # for complex --> uint8, we used the new test data pytorch_code = textwrap.dedent( """ import torch src = torch.tensor([0.+3.5j, -1+4.2j, 2.34-5.2j, -3.45+7.9j, -0.34-8.2j, 0.23+9.2j, 1.+1.j, 2.+0.5j, 3.-1.j,], dtype=torch.complex64) result = src.byte() """ ) obj.run(pytorch_code, ["result"]) def test_complex128_to_byte(): # for complex --> uint8, we used the new test data pytorch_code = textwrap.dedent( """ import torch src = torch.tensor([0.+3.5j, -1+4.2j, 2.34-5.2j, -3.45+7.9j, -0.34-8.2j, 0.23+9.2j, 1.+1.j, 2.+0.5j, 3.-1.j,], dtype=torch.complex128) result = src.byte() """ ) obj.run(pytorch_code, ["result"]) 
@zhiminzhang0830
Copy link
Contributor Author

/re-run all-failed

@zhwesky2010
Copy link
Contributor

LGTM

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 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 0b7e62c into PaddlePaddle:develop Aug 8, 2025
144 of 153 checks passed
Enigmatisms pushed a commit to Enigmatisms/Paddle that referenced this pull request Aug 9, 2025
* add tensor.bool * add tensor.bool test * update tensor.bool test * update tensor.bool * delete useless test code * add dtype conversions method * skip the complex128 test on XPU * fix: Implement the byte function separately * fix: update unit testing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

5 participants