-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Relax][PyTroch] Add randn.default and randn_like.default support #18815
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -991,6 +991,27 @@ def _hamming_window(self, node: fx.Node) -> relax.Var: | |||||||||||||||||||||||||||||||||||||||||||||||||
| relax.op.hamming_window(window_size, periodic, alpha, beta, dtype) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def _randn(self, node: fx.Node) -> relax.Var: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| args = self.retrieve_args(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| size = args[0] if isinstance(args[0], (list, tuple)) else (args[0],) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| dtype = self._convert_data_type( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| node.kwargs.get("dtype", torch.get_default_dtype()), self.env | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| data = np.random.randn(*size).astype(dtype) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return self.block_builder.emit(relax.const(data, dtype)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def _randn_like(self, node: fx.Node) -> relax.Var: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| x = self.env[node.args[0]] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| x_sinfo = x.struct_info | ||||||||||||||||||||||||||||||||||||||||||||||||||
| shape = [int(s) for s in x_sinfo.shape] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| dtype = self._convert_data_type(node.kwargs.get("dtype", None) or x_sinfo.dtype, self.env) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| data = np.random.randn(*shape).astype(dtype) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return self.block_builder.emit(relax.const(data, dtype)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def _zeros(self, node: fx.Node) -> relax.Var: | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1005
to
1015
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||||||||||||||||||||||||||||||||||||||||||||||||||
| args = self.retrieve_args(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| size = relax.ShapeExpr(args[0] if isinstance(args[0], (list, tuple)) else (args[0],)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1484,6 +1505,8 @@ def create_convert_map( | |||||||||||||||||||||||||||||||||||||||||||||||||
| "new_zeros.default": self._new_zeros, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "one_hot.default": self._one_hot, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "ones.default": self._ones, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "randn.default": self._randn, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "randn_like.default": self._randn_like, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "ones_like.default": lambda node: self.block_builder.emit( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| relax.op.ones_like(self.env[node.args[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.
This implementation of
_randnwill fail ifsizecontains symbolic dimensions (i.e.,tir.Varfromtorch.export.Dim), asnp.random.randnrequires concrete integer shapes. To prevent a crash with dynamic shapes, it's better to add an explicit check and raise aNotImplementedError.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.
int(s) will already raise a TypeError with a clear message. Adding explicit checks here would be inconsistent with the rest of the codebase.