-
Notifications
You must be signed in to change notification settings - Fork 550
Infer wp.array DType from argument in constructor #1539
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -66,7 +66,7 @@ def ParamSpec(name): | |
| from warp._src.codegen import WarpCodegenTypeError, synchronized | ||
| from warp._src.logger import LOG_DEBUG, LOG_WARNING, get_logger, log_debug, log_error, log_info, log_warning | ||
| from warp._src.texture import Texture1D, Texture2D, Texture3D, texture1d_t, texture2d_t, texture3d_t | ||
| from warp._src.types import LAUNCH_MAX_DIMS, Array, LaunchBounds, launch_bounds_t, type_repr | ||
| from warp._src.types import LAUNCH_MAX_DIMS, Array, DType, LaunchBounds, launch_bounds_t, type_repr | ||
|
|
||
| _wp_module_name_ = "warp.context" | ||
|
|
||
|
|
@@ -7647,13 +7647,13 @@ def unmap(self): | |
|
|
||
| def zeros( | ||
| shape: int | tuple[int, ...] | list[int] | None = None, | ||
| dtype: type = float, | ||
| dtype: type[DType] | None = None, | ||
|
Contributor
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. The default was changed to This is to avoid a type error, because the concrete default float would pin the free DType TypeVar. There is precedent to this ( This changes the behavior of the code.
Member
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. It's better to have |
||
| device: DeviceLike = None, | ||
| requires_grad: bool = False, | ||
| pinned: bool = False, | ||
| retain_grad: bool = False, | ||
| **kwargs, | ||
| ) -> warp.array: | ||
| ) -> warp.array[DType]: | ||
|
Comment on lines
7648
to
+7656
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.
Changing the parameter default from Adding Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| """Return a zero-initialized array. | ||
|
|
||
| Args: | ||
|
|
@@ -7667,6 +7667,8 @@ def zeros( | |
| Returns: | ||
| A warp.array object representing the allocation | ||
| """ | ||
| if dtype is None: | ||
| dtype = float | ||
|
|
||
| arr = empty( | ||
| shape=shape, | ||
|
|
@@ -7712,13 +7714,13 @@ def zeros_like( | |
|
|
||
| def ones( | ||
| shape: int | tuple[int, ...] | list[int] | None = None, | ||
| dtype: type = float, | ||
| dtype: type[DType] | None = None, | ||
| device: DeviceLike = None, | ||
| requires_grad: bool = False, | ||
| pinned: bool = False, | ||
| retain_grad: bool = False, | ||
| **kwargs, | ||
| ) -> warp.array: | ||
| ) -> warp.array[DType]: | ||
| """Return a one-initialized array. | ||
|
|
||
| Args: | ||
|
|
@@ -7732,6 +7734,8 @@ def ones( | |
| Returns: | ||
| A warp.array object representing the allocation | ||
| """ | ||
| if dtype is None: | ||
| dtype = float | ||
|
|
||
| return full( | ||
| shape=shape, | ||
|
|
@@ -7771,13 +7775,13 @@ def ones_like( | |
| def full( | ||
| shape: int | tuple[int, ...] | list[int] | None = None, | ||
| value: Any = 0, | ||
| dtype: type | None = None, | ||
| dtype: type[DType] | None = None, | ||
| device: DeviceLike = None, | ||
| requires_grad: bool = False, | ||
| pinned: bool = False, | ||
| retain_grad: bool = False, | ||
| **kwargs, | ||
| ) -> warp.array: | ||
| ) -> warp.array[DType]: | ||
| """Return an array with all elements initialized to the given value. | ||
|
|
||
| Args: | ||
|
|
@@ -7902,13 +7906,13 @@ def clone( | |
|
|
||
| def empty( | ||
| shape: int | tuple[int, ...] | list[int] | None = None, | ||
| dtype=float, | ||
| dtype: type[DType] | None = None, | ||
| device: DeviceLike = None, | ||
| requires_grad: bool = False, | ||
| pinned: bool = False, | ||
| retain_grad: bool = False, | ||
| **kwargs, | ||
| ) -> warp.array: | ||
| ) -> warp.array[DType]: | ||
| """Return an uninitialized array. | ||
|
|
||
| Args: | ||
|
|
@@ -7923,6 +7927,9 @@ def empty( | |
| A warp.array object representing the allocation | ||
| """ | ||
|
|
||
| if dtype is None: | ||
| dtype = float | ||
|
|
||
| # backwards compatibility for case where users called wp.empty(n=length, ...) | ||
| if "n" in kwargs: | ||
| shape = (kwargs["n"],) | ||
|
|
@@ -7998,12 +8005,12 @@ def empty_like( | |
|
|
||
| def from_numpy( | ||
| arr: np.ndarray, | ||
| dtype: type | None = None, | ||
| dtype: type[DType] | None = None, | ||
| shape: Sequence[int] | None = None, | ||
| device: DeviceLike | None = None, | ||
| requires_grad: bool = False, | ||
| retain_grad: bool = False, | ||
| ) -> warp.array: | ||
| ) -> warp.array[DType]: | ||
|
Comment on lines
8006
to
+8013
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.
When Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| """Return a Warp array created from a NumPy array. | ||
|
|
||
| Args: | ||
|
|
||
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.
🧩 Analysis chain
🏁 Script executed:
Repository: NVIDIA/warp
Length of output: 37
🏁 Script executed:
Repository: NVIDIA/warp
Length of output: 6199
🏁 Script executed:
Repository: NVIDIA/warp
Length of output: 50675
🏁 Script executed:
Repository: NVIDIA/warp
Length of output: 7298
🏁 Script executed:
Repository: NVIDIA/warp
Length of output: 10632
🏁 Script executed:
Repository: NVIDIA/warp
Length of output: 86
🏁 Script executed:
Repository: NVIDIA/warp
Length of output: 8692
🏁 Script executed:
Repository: NVIDIA/warp
Length of output: 11202
Fix
zeros()stub to bind return dtype whendtypeis omittedwarp/_src/context.py::zeros()defaultsdtypeto Pythonfloatwhen omitted, andwarp/_src/types.py::type_to_warp()maps that towarp.float32; however the current stub keepsdtypeastype[DType] | None = None, sowp.zeros(10)can’t bindDTypeand the return often falls back to an imprecise type (e.g.,array[Any]/unknown). Split overloads so omitteddtypereturnsarray[float32]and explicitdtypebindsDType.Suggested stub shape
`@over` def zeros( shape: int | tuple[int, ...] | list[int] | None = None, - dtype: type[DType] | None = None, + dtype: None = None, device: DeviceLike = None, requires_grad: _builtins.bool = False, pinned: _builtins.bool = False, retain_grad: _builtins.bool = False, **kwargs, -) -> array[DType]: +) -> array[float32]: ... + +@over +def zeros( + shape: int | tuple[int, ...] | list[int] | None = None, + dtype: type[DType] = ..., + device: DeviceLike = None, + requires_grad: _builtins.bool = False, + pinned: _builtins.bool = False, + retain_grad: _builtins.bool = False, + **kwargs, +) -> array[DType]:📝 Committable suggestion
🤖 Prompt for AI Agents