-
Couldn't load subscription status.
- Fork 5.9k
[API compatibility] add dtype conversion method #74416
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
Conversation
| 你的PR提交成功,感谢你对开源项目的贡献! |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
| 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 |
There was a problem hiding this comment.
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的类型吧
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
| if self.is_floating_point(): | ||
| tensor = astype(self, 'int8') | ||
| return astype(tensor, 'uint8') | ||
| elif self.is_complex(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
复数情况下在PaConvert里和torch对比下,看torch是不是也是这么处理的
There was a problem hiding this comment.
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"]) | /re-run all-failed |
| LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* 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
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