-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[API Compatibilities] Add rand_like, multinomial and var #74920
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提交成功,感谢你对开源项目的贡献! |
python/paddle/tensor/random.py
Outdated
| seed: int = 0, | ||
| name: str | None = None, | ||
| *, | ||
| place: PlaceLike | 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.
这个应该不需要修改
python/paddle/tensor/random.py
Outdated
| if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)): | ||
| dtype = convert_np_dtype_to_dtype_(dtype) | ||
|
|
||
| tensor = uniform( |
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.
这个可以用paddle.rand吗,paddle.rand 也升级了,这些dtype、requires_grad都会被处理,这个API就只需要转发一下就行
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.
可以的,预计按照这种方式进行修改:
tensor = rand(input.shape, dtype=dtype, name=name)
if device is not None:
tensor = tensor.to(device)27ed42c to
24aaa86
Compare
| >>> print(out4.stop_gradient) | ||
| False | ||
| """ | ||
| if dtype is 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.
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.
我们需要调用的是均匀分布的rand而不是randn,而rand并不支持这几个参数
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #74920 +/- ##
==========================================
Coverage ? 92.30%
==========================================
Files ? 2
Lines ? 39
Branches ? 0
==========================================
Hits ? 36
Misses ? 3
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| False | ||
| """ | ||
| if dtype is None: | ||
| dtype = input.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.
这个无需要处理,后面的API会自行处理dtype=None的情况,取这个有点损耗
python/paddle/tensor/random.py
Outdated
| dtype = convert_np_dtype_to_dtype_(dtype) | ||
|
|
||
| tensor = paddle.rand(input.shape, dtype=dtype, name=name) | ||
| if device is not 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.
这里进行了一些额外的处理,会有一些损耗,底层kernel已经支持了place,需要利用起来。
参考paddle.randn的实现方式吧,这个实现比较标准:
https://github.com/PaddlePaddle/Paddle/pull/74849/files
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.
这个应该就是之前我利用uniform kernel实现的思路。我先去利用它修改一下rand,然后使用rand_like转发rand来实现。
24aaa86 to
479a28e
Compare
| False | ||
| """ | ||
| if dtype is None: | ||
| dtype = input.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.
只在最后一个API里处理dtype=None的情况,前面的API就直接将None往后面传,不用一直取None下的dtype。
如果其他的API不符合这个写法,做了冗余操作,帮忙一起处理下。
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.
rand_like/randn_like必须要在前面处理dtype=None的情况,因为目标是获取和输入Tensor一致的dtype。
| *, | ||
| out: Tensor | None = None, | ||
| device: PlaceLike | None = None, | ||
| requires_grad: bool = False, |
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.
这个是不是还有一个pin_memory,参考下最新的rand代码
test/legacy_test/test_creation.py
Outdated
| ) | ||
| self.assertEqual(x.data_ptr(), y.data_ptr()) | ||
|
|
||
| def test_rand(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.
7c5751c to
d42d7e5
Compare
zhwesky2010
left a comment
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
| paddle.disable_static() | ||
| x = paddle.to_tensor(self.x) | ||
| if self.use_correction: | ||
| out = paddle.var( |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
XiaoguangHu01
left a comment
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
rand_likeAPImultinomial支持out参数var支持correction和out参数