-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
add empty_like op (python, and unit test), use c++ implementation of … #27287
Conversation
Thanks for your contribution! |
✅ This PR's description meets the template requirements! |
python/paddle/tensor/creation.py
Outdated
type='empty', | ||
inputs={}, | ||
outputs={'Out': [out]}, | ||
attrs={'shape': x.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.
x.shape在如果输入shape存在负数的时候是否在empty的代码考虑了?
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.
thanks. 对于参数 shape,目前 empty op 的 C++ 实现有问题,没有校验里面的值是否为负数,不过 review 意见已经指出来了,会在这个 op 里面修复。
只是这里我有个疑问,假设 x 校验通过后,x.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.
需要用过输入ShapeTensor
来设置输出的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.
thanks。已经修改。通过 paddle.shape(x) 可以返回正确的 shape,这个 shape 是一个 tensor,然后作为 empty op 的 ShapeTensor 传入。
这里同时修改了一下 shape op,之前不支持 bool 类型,现在支持。同时添加了 shape op 的文档预览。
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.
thanks。已添加对应单侧。
a55ab34
to
ce492cf
Compare
paddle/fluid/operators/empty_op.cc
Outdated
"is [%s]", | ||
dims); | ||
platform::errors::InvalidArgument( | ||
"ShapeError: The shape of Tensor in list must be [1]. " |
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.
去掉ShapeError:
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.
thanks。已修改。
ab04e59
to
bbb1bf9
Compare
bbb1bf9
to
307857b
Compare
REGISTER_OP_CPU_KERNEL(shape, ops::ShapeKernel<int>, ops::ShapeKernel<int32_t>, | ||
ops::ShapeKernel<int64_t>, ops::ShapeKernel<float>, | ||
ops::ShapeKernel<double>); | ||
REGISTER_OP_CPU_KERNEL(shape, ops::ShapeKernel<bool>, ops::ShapeKernel<int>, |
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.
这shape_op咋同时注册了int
和int32_t
类型的kernel啊。不过这个与你这个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.
thanks。我把 int32_t 去除吧。
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.
已去除 int32_t
|
||
class TestEmptyLikeAPI_Static(unittest.TestCase): | ||
def __check_out__(self, out): | ||
data_type = convert_dtype(out.dtype) |
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.
这个函数的实现,看起来和上一个类里面的__check__out__
实现有很多重复的代码
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.
thanks。我优化一下。
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.
已优化
|
||
self.dst_dtype = dtype | ||
self.dst_shape = x.shape | ||
self.__check_out__(res[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.
thanks。我优化一下。
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.
已优化
307857b
to
8e6482b
Compare
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
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
@@ -11229,7 +11229,7 @@ def shape(input): | |||
input.shape = [3, 2] | |||
|
|||
Args: | |||
input (Variable): The input can be N-D Tensor or SelectedRows with data type float16, float32, float64, int32, int64. | |||
input (Variable): The input can be N-D Tensor or SelectedRows with data type bool, float16, float32, float64, int32, int64. |
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.
跟这个PR无关。
这个API现在直接以paddle.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.
thanks. 这个我要怎么改呢,因为我里面需要用到 paddle.shape(x) 这个 op 来返回 x 的 shape。这里,x 支持 bool 类型,而 paddle.shape(x) 并不支持 bool 类型,所以想在这个 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.
要不这个 PR 中先保留这些修改,涉及文档和示例的地方后面再提一个 PR?你看这样可以吗。
python/paddle/tensor/creation.py
Outdated
If the ``dtype`` is None, the data type of Tensor is same with ``x``. | ||
|
||
Args: | ||
x(Tensor): The input tensor which specifies shape and data type. The data type can be bool, float32, float64, int32, int64. |
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.
说明x的时候不需要说明data type吧?
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.
thanks。我修改一下。
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.
我看了一下 full_like,也是这样描述 x 的,要不这里维持原样,不修改?
python/paddle/tensor/creation.py
Outdated
|
||
Args: | ||
x(Tensor): The input tensor which specifies shape and data type. The data type can be bool, float32, float64, int32, int64. | ||
dtype(np.dtype|str, optional): Data type of the output Tensor |
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.
说明一下default value
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.
thanks. 我修改一下。
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.
已修改,参考 full_like 改成如下:
dtype(np.dtype|str, optional): The data type of output. The data type can be one
of bool, float16, float32, float64, int32, int64. The default value is None, which means the output
data type is the same as input.
python/paddle/tensor/creation.py
Outdated
paddle.set_device("cpu") # and use cpu device | ||
|
||
x_data = np.random.random((2, 3)).astype("float32") | ||
x = paddle.to_tensor(x_data) |
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.
x可以直接用paddle.randn
创建。
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.
thanks. 我修改一下。
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.
已修改。
…empty op, and optimize the c++ implmentation of empty op as PR#26659 reviews, and add bool for shape op.
7ad4ffb
8e6482b
to
7ad4ffb
Compare
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
New features
PR changes
APIs
Describe
add empty_like op.
Returns an uninitialized tensor with the same size as input.