-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
fix the div 0 error of sequence_concat #49963
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
不知道为什么加入语句似乎没有触发执行,需要一些提示和帮助 |
根据报错可以发现 |
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
if input_x.dim() == 1 and 0 in input_x.shape: | ||
raise ValueError( | ||
'the shape of element in input list should not be 0.' | ||
) |
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.
got it, I will try to solve it
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.
Is this change intended to prevent division by zero at
SequenceConcatOp
'sInferShape
?If so, why only raise Exception when an input has shape
[0]
. In my opinion, when an input has a leading zero in its shape, like[0, 2, 3]
, an exception should be raised.
Hi, in the follow case, an exception have been writen.
If I simplely require that 'the input size must not be 0', some collisions may occur.
So, could you tell me where should I modify or what should I do?
import paddle
paddle.enable_static()
x = paddle.static.data(name='x', shape=[1,1], dtype='float32', lod_level=1)
y = paddle.static.data(name='y', shape=[1,0], dtype='float32', lod_level=1)
out = paddle.static.nn.sequence_concat(input=[x, y])
ValueError: Variable 'y' has been created before. The previous shape is (1, 0), the new shape is (0, 1). They are not matched.
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 I simplely require that 'the input size must not be 0', some collisions may occur.
emmmm,就这个问题而言,因为是 InferShape 里出现了除第一个维度的需求,'the input size must not be 0'
的检查是不是不太合适?when an input has a leading zero in its shape
对此的检查就刚刚好
Hi, in the follow case, an exception have been writen.
ValueError: Variable 'y' has been created before. The previous shape is (1, 0), the new shape is (0, 1). They are not matched.
这个错误与本问题无关啊,只要你重新使用相同 name 定义了不同 shape 的 Variable 就会出现此错误,这里上文应该有一个定义过的 shape=(1, 0)
的 y
,删掉或者将这个重命名即可(Jupyter 直接 Restart kernel
)
So, could you tell me where should I modify or what should I do?
直接在 Python 端修改:
- if input_x.dim() == 1 and 0 in input_x.shape: # 仅仅要求 shape != [0]
+ if input_x.shape[0] == 0: # 要求 shape != [0, ...]
或者直接在 C++ 端增加一样的检查,比如这里 71 行后就很合适,个人觉得直接 C++ 端更好一些?
Paddle/paddle/fluid/operators/sequence_ops/sequence_concat_op.cc
Lines 71 to 86 in 3e5a6df
batch_size += x_dim[0]; | |
if (feature_size == 0) { | |
feature_size = phi::product(x_dim) / x_dim[0]; | |
} else { | |
PADDLE_ENFORCE_EQ( | |
feature_size, | |
phi::product(x_dim) / x_dim[0], | |
platform::errors::InvalidArgument( | |
"Each input of SequenceConcatOp inputs must have same feature " | |
"size, But " | |
"the feature size we received is %d, the feature size of 1st " | |
"input is %d", | |
feature_size, | |
phi::product(x_dim) / x_dim[0])); | |
} | |
} |
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 I simplely require that 'the input size must not be 0', some collisions may occur.
emmmm,就这个问题而言,因为是 InferShape 里出现了除第一个维度的需求,
'the input size must not be 0'
的检查是不是不太合适?when an input has a leading zero in its shape
对此的检查就刚刚好Hi, in the follow case, an exception have been writen.
ValueError: Variable 'y' has been created before. The previous shape is (1, 0), the new shape is (0, 1). They are not matched.
这个错误与本问题无关啊,只要你重新使用相同 name 定义了不同 shape 的 Variable 就会出现此错误,这里上文应该有一个定义过的
shape=(1, 0)
的y
,删掉或者将这个重命名即可(Jupyter 直接Restart kernel
)So, could you tell me where should I modify or what should I do?
直接在 Python 端修改:
- if input_x.dim() == 1 and 0 in input_x.shape: # 仅仅要求 shape != [0] + if input_x.shape[0] == 0: # 要求 shape != [0, ...]或者直接在 C++ 端增加一样的检查,比如这里 71 行后就很合适,个人觉得直接 C++ 端更好一些?
Paddle/paddle/fluid/operators/sequence_ops/sequence_concat_op.cc
Lines 71 to 86 in 3e5a6df
batch_size += x_dim[0]; if (feature_size == 0) { feature_size = phi::product(x_dim) / x_dim[0]; } else { PADDLE_ENFORCE_EQ( feature_size, phi::product(x_dim) / x_dim[0], platform::errors::InvalidArgument( "Each input of SequenceConcatOp inputs must have same feature " "size, But " "the feature size we received is %d, the feature size of 1st " "input is %d", feature_size, phi::product(x_dim) / x_dim[0])); } }
更新一下报错信息~
ValueError: (InvalidArgument) Each input of SequenceConcatOp inputs must have same feature size, But the feature size we received is 1, the feature size of 1st input is 0
[Hint: Expected feature_size == phi::product(x_dim) / x_dim[0], but received feature_size:1 != phi::product(x_dim) / x_dim[0]:0.] (at /paddle/paddle/fluid/operators/sequence_ops/sequence_concat_op.cc:84)
[operator < sequence_concat > error]
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++ 端添加 x_dim[0] 不能为 0 的检查会更好一些。
P.S. 由于现在 paddle 对 zero-sized tensor 支持不够好,所以目前添加这样的检查(无论是在 c++ 端还是在 Python 端)我觉得都 ok.
不过其实为了计算 feature_size 大可以用 product(x_shape[1: ])
的方式来计算而不必用 x_numel / x_shape[0]
的方式计算. (P.S. paddle 的 lod tensor (多层不定长序列)的第 0 维表示递归折叠后的长度,而后面的可以称为 feature_shape,表示序列中的基础元素的形状)
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 types
Bug fixes
PR changes
APIs
Describe