CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
【Paddle Tensor No.8、9、14、15】为Tensor新增__rshift__
,__lshift__
,__rlshift__
,__rrshift__
#69348
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
【Paddle Tensor No.8、9、14、15】为Tensor新增__rshift__
,__lshift__
,__rlshift__
,__rrshift__
#69348
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
更新了链接 |
另外暂时也只需要考虑int->Tensor的情况即可,torch和numpy也都没有对float对额外的类型转换 >>> import torch
>>> data = torch.Tensor([2,4,8])
>>> data << 1.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: "lshift_cpu" not implemented for 'Float' >>> import numpy as np
>>> data = np.array([2,4,8])
>>> data << 1.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ufunc 'left_shift' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' float没有被转换成Tensor |
包括复用bitwise_left_shift,biwise_right_shift的__rshift__和__lshift__ |
好的 |
对的,移位操作一般只用于int,float是不需要的 |
python/paddle/tensor/__init__.py
Outdated
('__lshift__', 'left_shift'), | ||
('__rshift__', 'right_shift'), | ||
('__rlshift__', 'right_left_shift'), | ||
('_rrshift__', 'right_right_shift'), |
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.
_rrshift__
这里是不是少了个下划线?
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.
对。
python/paddle/tensor/math.py
Outdated
Apply ``left_shift`` on Tensor ``X`` and ``Y`` . | ||
.. math:: | ||
Out = X \gg Y | ||
.. note:: | ||
``Tensor.__rshift__`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . | ||
.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor | ||
Args: | ||
x (Tensor): Input Tensor of ``__rshift__`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. | ||
y (Tensor): Input Tensor of ``__rshift__`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. | ||
is_arithmetic (bool, optional): A boolean indicating whether to choose arithmetic shift, if False, means logic shift. Default True. | ||
out (Tensor|None, optional): Result of ``__rshift__`` . It is a N-D Tensor with the same data type of input Tensor. Default: None. | ||
name (str|None, optional): The default value is None. Normally there is no need for | ||
user to set this property. For more information, please refer to :ref:`api_guide_Name`. | ||
Returns: | ||
Tensor: Result of ``__rshift__`` . It is a N-D Tensor with the same data type of input Tensor. | ||
Examples: | ||
.. code-block:: python | ||
:name: rshift_example1 |
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.
这里的.. xxx
代码块之间是不是应该有一个空行?
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.
好
python/paddle/tensor/math.py
Outdated
[8 , 8 , 16, 32]]) | ||
""" | ||
# Directly call bitwise_left_shift | ||
return bitwise_left_shift(x, y, is_arithmetic, out, name) |
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.
left_shift
不是API标准里面的接口,不能露给用户- 这里是纯转发,是否能直接使用
bitwise_left_shift
,删掉left_shift
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.
这里我有想法。
我正在打算定义这样一个op:
- op: auto_tenosor
args: (int x)
output: Tensor(out)
infer_meta:
func: AutoTensorMeta
kernel:
func: auto_tensor
traits: paddle::dialect::ForwardOnlyTrait
我需要组合复用bitwise_left_shift和auto_tensor来实现对int的自动转换,所以我需要保留left_shift.
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.
然后left_shift可以重载为(Tensor,int),(int,Tenosr)和(Tensor,Tensor)
要下动车了,暂时可能回复不了
这里只是我当时复现问题时候的代码,docstring我最后都会修改和重写。 目前我正在尝试定义一个将int自动转换为Tensor的kernel:
命名可以再改 然后我使用它和 最后我希望实现的功能是像这样: >>> import paddle
>>> data = paddle.to_tensor([2,4,8],dtype=paddle.int32)
>>> shift = torch.Tensor([1],dtype=paddle.int32)
>>> data << shift
tensor([ 4, 8, 16], dtype=paddle.int32)
>>> data << 1
tensor([ 4, 8, 16], dtype=paddle.int32)
>>> 1 << data
tensor([4,16,256],dtype=paddle.int32)
>>> shift << data
tensor([4,16,256],dtype=paddle.int32) 目前的直接复用是不支持 等我觉得写好了 |
应该不需要这么麻烦,一个简单的左移例子(demo,没有添加必要的检查,且没有按照规范写的代码,只是尝试触发右运算数的 import paddle
def __rlshift__(self: paddle.Tensor, other: int | paddle.Tensor):
return paddle.bitwise_left_shift(
paddle.to_tensor(other),
self,
)
setattr(paddle.Tensor, '__rlshift__', __rlshift__)
two = paddle.to_tensor(2)
print(10 << two) |
好的,我尝试一下。 |
调用
|
>>> import paddle
>>> data = paddle.to_tensor([1,2,4],dtype=paddle.int32)
>>> shift = paddle.to_tensor(1,dtype=paddle.int32)
>>> data << shift
Tensor(shape=[3], dtype=int32, place=Place(cpu), stop_gradient=True,
[2, 4, 8])
>>> data << 1
Tensor(shape=[3], dtype=int32, place=Place(cpu), stop_gradient=True,
[2, 4, 8])
>>> shift << data
Tensor(shape=[3], dtype=int32, place=Place(cpu), stop_gradient=True,
[2 , 4 , 16])
>>> 1 << data
Tensor(shape=[3], dtype=int32, place=Place(cpu), stop_gradient=True,
[2 , 4 , 16])
>>> data << 1.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.9/dist-packages/paddle/tensor/math.py", line 7826, in __lshift__
raise TypeError("unsupported operand type(s) for <<:'float'")
TypeError: unsupported operand type(s) for <<:'float'
>>> 1.0 << data
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.9/dist-packages/paddle/tensor/math.py", line 7934, in __rlshift__
raise TypeError("unsupported operand type(s) for <<:'float'")
TypeError: unsupported operand type(s) for <<:'float'
>>> 现在基本上和torch以及numpy一致了。 我不是很清楚是不是可以直接在全局定义域里而不是类里定义魔术方法像 我现在正要添加testing,我是加到 |
__rshift__
,__lshift__
,__rlshift__
,__rrshift__
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.
再添加一些单测即可
好的 |
python/paddle/tensor/math.py
Outdated
out: Tensor | None = None, | ||
name: str | None = None, | ||
) -> Tensor: | ||
r""" |
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.
魔术方法不是公开的API,不用docstring,其他函数的docsting也删除
单测左操作数使用包含正负零的整数随机数,并且需要覆盖int64和int32的范围测试(如 |
…_`,`__rshift__`cannot pass ref_right_shift_logical `__rshift__` cannot pass TestBitwiseRightShiftAPI.test_static_api_logical and test_dygraph_api_arithmetic
python/paddle/tensor/math.py
Outdated
is_arithmetic: bool = True, | ||
out: Tensor | None = None, | ||
name: str | None = None, |
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.
__lshift__
签名有这么复杂么?后三个参数应该没有吧?
Array API 规范也没有
使用__rshift__而不是__rrshift__,使用__lshift__,而不是__rlshift__
python/paddle/tensor/math.py
Outdated
if isinstance(y, int): | ||
y = paddle.to_tensor(y, dtype=x.dtype) | ||
elif isinstance(y, float): | ||
raise TypeError("unsupported operand type(s) for <<:'float'") |
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.
python/paddle/tensor/math.py
Outdated
if isinstance(y, int): | ||
y = paddle.to_tensor(y, dtype=x.dtype) | ||
elif isinstance(y, float): | ||
raise TypeError("unsupported operand type(s) for >>:'float'") |
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.
同上
python/paddle/tensor/math.py
Outdated
if isinstance(y, int): | ||
y = paddle.to_tensor(y, dtype=x.dtype) | ||
elif isinstance(y, float): | ||
raise TypeError("unsupported operand type(s) for <<:'float'") |
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.
同上
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
PR Category
User Experience
PR Types
New features
Description
我是来提我发现的问题的:
第二次正经PR : 发现左右操作数类型的冲突Bug和可选解决方式 ,
__rrshift__
和__rshift__
,__rlshift__
和__lshift__
.@HydrogenSulfate
问题复现:
单纯复用会有这种情况.
1<<data
的时候应该调用data的__rlshift__
,但是一调用就报错。如果支持int的话就不会有这个问题,参见我上面的链接🔗。torch是支持的: