pyrefly-torch-stubs 1.4.0.dev3__tar.gz → 1.5.0.dev1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/PKG-INFO +6 -3
  2. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/README.md +3 -1
  3. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/pyproject.toml +3 -2
  4. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/__init__.pyi +210 -71
  5. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/_shapes.pyi +294 -37
  6. pyrefly_torch_stubs-1.5.0.dev1/torch-stubs/distributions/constraints.pyi +234 -0
  7. pyrefly_torch_stubs-1.5.0.dev1/torch-stubs/distributions/transforms.pyi +149 -0
  8. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/fft.pyi +94 -9
  9. pyrefly_torch_stubs-1.5.0.dev1/torch-stubs/linalg.pyi +490 -0
  10. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/nn/__init__.pyi +8 -5
  11. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/nn/attention/__init__.pyi +6 -4
  12. pyrefly_torch_stubs-1.5.0.dev1/torch-stubs/nn/attention/flex_attention.pyi +143 -0
  13. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/nn/functional.pyi +1079 -39
  14. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/nn/init.pyi +24 -5
  15. pyrefly_torch_stubs-1.5.0.dev1/torch-stubs/return_types.pyi +190 -0
  16. pyrefly_torch_stubs-1.4.0.dev3/torch-stubs/distributions/constraints.pyi +0 -48
  17. pyrefly_torch_stubs-1.4.0.dev3/torch-stubs/distributions/transforms.pyi +0 -46
  18. pyrefly_torch_stubs-1.4.0.dev3/torch-stubs/linalg.pyi +0 -138
  19. pyrefly_torch_stubs-1.4.0.dev3/torch-stubs/nn/attention/flex_attention.pyi +0 -78
  20. pyrefly_torch_stubs-1.4.0.dev3/torch-stubs/return_types.pyi +0 -89
  21. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/LICENSE +0 -0
  22. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/test/README.md +0 -0
  23. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/_tensor.pyi +0 -0
  24. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/distributions/__init__.pyi +0 -0
  25. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/distributions/beta.pyi +0 -0
  26. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/distributions/categorical.pyi +0 -0
  27. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/distributions/transformed_distribution.pyi +0 -0
  28. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/nn/modules/__init__.pyi +0 -0
  29. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/nn/modules/module.pyi +0 -0
  30. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/nn/parameter.pyi +0 -0
  31. {pyrefly_torch_stubs-1.4.0.dev3 → pyrefly_torch_stubs-1.5.0.dev1}/torch-stubs/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: pyrefly-torch-stubs
3
- Version: 1.4.0.dev3
3
+ Version: 1.5.0.dev1
4
4
  Summary: PyTorch type stubs with tensor-shape tracking, maintained by Pyrefly
5
5
  Project-URL: homepage, https://pyrefly.org
6
6
  Project-URL: documentation, https://pyrefly.org/en/docs/
@@ -33,7 +33,8 @@ Classifier: Programming Language :: Python
33
33
  Classifier: Programming Language :: Python :: 3
34
34
  Classifier: Typing :: Stubs Only
35
35
  Requires-Python: >=3.10
36
- Requires-Dist: pyrefly-shape-extensions==1.4.0.dev3
36
+ Requires-Dist: pyrefly-shape-extensions==1.5.0.dev1
37
+ Requires-Dist: torch
37
38
  Description-Content-Type: text/markdown
38
39
 
39
40
  # pyrefly-torch-stubs
@@ -45,7 +46,9 @@ This package is a PEP 561 stub-only distribution. It installs the
45
46
  runtime `torch` package without replacing or shadowing PyTorch itself.
46
47
 
47
48
  The package is versioned in lockstep with Pyrefly and depends on the matching
48
- `pyrefly-shape-extensions` package.
49
+ `pyrefly-shape-extensions` package. Installing it also installs `torch`, which
50
+ provides upstream modules used by these partial stubs. The `torch` dependency is
51
+ unpinned, so projects can choose their own PyTorch version.
49
52
 
50
53
  Static checks use the installed Torch package for modules that this package
51
54
  intentionally leaves unshaped:
@@ -7,7 +7,9 @@ This package is a PEP 561 stub-only distribution. It installs the
7
7
  runtime `torch` package without replacing or shadowing PyTorch itself.
8
8
 
9
9
  The package is versioned in lockstep with Pyrefly and depends on the matching
10
- `pyrefly-shape-extensions` package.
10
+ `pyrefly-shape-extensions` package. Installing it also installs `torch`, which
11
+ provides upstream modules used by these partial stubs. The `torch` dependency is
12
+ unpinned, so projects can choose their own PyTorch version.
11
13
 
12
14
  Static checks use the installed Torch package for modules that this package
13
15
  intentionally leaves unshaped:
@@ -8,7 +8,7 @@ description = "PyTorch type stubs with tensor-shape tracking, maintained by Pyre
8
8
  readme = "README.md"
9
9
  requires-python = ">=3.10"
10
10
  license = { file = "LICENSE" }
11
- version = "1.4.0.dev3"
11
+ version = "1.5.0.dev1"
12
12
  keywords = ["typechecker", "typechecking", "stubs", "torch", "pytorch"]
13
13
  classifiers = [
14
14
  "Development Status :: 4 - Beta",
@@ -18,7 +18,8 @@ classifiers = [
18
18
  "Typing :: Stubs Only",
19
19
  ]
20
20
  dependencies = [
21
- "pyrefly-shape-extensions==1.4.0.dev3",
21
+ "pyrefly-shape-extensions==1.5.0.dev1",
22
+ "torch",
22
23
  ]
23
24
 
24
25
  [project.urls]
@@ -16,6 +16,9 @@ from math import e as e, nan as nan
16
16
  from types import EllipsisType
17
17
  from typing import Any, Callable, Literal, overload, Self, TYPE_CHECKING, Unpack
18
18
 
19
+ # Torch does not require NumPy, so this import is intentionally not a package
20
+ # dependency.
21
+ import numpy as np
19
22
  from shape_extensions import (
20
23
  broadcast,
21
24
  Flag,
@@ -621,6 +624,7 @@ from torch._shapes import (
621
624
  repeat_shape,
622
625
  replace_axis_extent,
623
626
  reshape_shape,
627
+ reverse_shape,
624
628
  scatter_shape,
625
629
  select_shape,
626
630
  size_dim_shape,
@@ -642,7 +646,10 @@ from torch._shapes import (
642
646
  )
643
647
  from torch._tensor_str import set_printoptions as set_printoptions
644
648
  from torch.amp import autocast as autocast, GradScaler as GradScaler
645
- from torch.autograd import enable_grad as enable_grad
649
+ from torch.autograd import (
650
+ enable_grad as enable_grad,
651
+ set_grad_enabled as set_grad_enabled,
652
+ )
646
653
  from torch.func import vmap as vmap
647
654
  from torch.functional import (
648
655
  align_tensors as align_tensors,
@@ -665,6 +672,7 @@ from torch.random import (
665
672
  thread_safe_generator as thread_safe_generator,
666
673
  )
667
674
  from torch.storage import TypedStorage as TypedStorage, UntypedStorage as UntypedStorage
675
+ from typing_extensions import TypeIs
668
676
 
669
677
  if TYPE_CHECKING:
670
678
  from shape_extensions import Int as _Int
@@ -682,18 +690,22 @@ type _BasicIndex = builtins.int | slice | list[builtins.int] | None | EllipsisTy
682
690
  type _TensorScalar = builtins.bool | builtins.int | builtins.float | builtins.complex
683
691
  type _LegacyTensorScalar = builtins.bool | builtins.int | builtins.float
684
692
 
685
- # Dtype constants
693
+ # Dtype and memory-format constants
686
694
  qint8: Any
687
695
  quint8: Any
688
696
  float16: Any
697
+ half: Any
689
698
  float32: Any
690
699
  float64: Any
691
700
  int8: Any
692
701
  int16: Any
693
702
  int32: Any
694
703
  int64: Any
704
+ uint8: Any
695
705
  int: Any
696
706
  bool: Any
707
+ channels_last: memory_format
708
+ strided: layout
697
709
  ops: Any
698
710
 
699
711
  # ============================================================================
@@ -754,7 +766,8 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
754
766
  device: Any # Device where tensor is stored (cpu, cuda, etc.)
755
767
  dtype: Any # Data type of tensor elements (float32, int64, etc.)
756
768
  ndim: builtins.int # Number of dimensions
757
- T: Self # Transpose property (for 2D tensors). Use .t() method for shape inference.
769
+ @property
770
+ def T(self: Tensor[Shape]) -> Tensor[reverse_shape(Shape)]: ...
758
771
  real: Self # Real part of complex tensor (shape-preserving)
759
772
  imag: Self # Imaginary part of complex tensor (shape-preserving)
760
773
 
@@ -815,7 +828,7 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
815
828
  | Tensor,
816
829
  ...,
817
830
  ],
818
- value: Tensor | builtins.int | builtins.float,
831
+ value: Tensor | _Scalar,
819
832
  ) -> None:
820
833
  """Set values in tensor via indexing. Mutates tensor in-place."""
821
834
  ...
@@ -872,11 +885,11 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
872
885
  ) -> Tensor[inplace_broadcast_shape(Shape, OtherShape)]: ...
873
886
 
874
887
  # Reverse operations for scalars
875
- def __radd__(self, other: builtins.float | builtins.int) -> Self: ...
876
- def __rsub__(self, other: builtins.float | builtins.int) -> Self: ...
877
- def __rmul__(self, other: builtins.float | builtins.int) -> Self: ...
878
- def __rtruediv__(self, other: builtins.float | builtins.int) -> Self: ...
879
- def __rpow__(self, other: builtins.float | builtins.int) -> Self: ...
888
+ def __radd__(self, other: _Scalar) -> Self: ...
889
+ def __rsub__(self, other: _Scalar) -> Self: ...
890
+ def __rmul__(self, other: _Scalar) -> Self: ...
891
+ def __rtruediv__(self, other: _Scalar) -> Self: ...
892
+ def __rpow__(self, other: _Scalar) -> Self: ...
880
893
 
881
894
  # Power operations
882
895
  def __pow__[OtherShape: _Shape = []](
@@ -892,18 +905,12 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
892
905
 
893
906
  # ==== Comparison Operations ====
894
907
 
895
- @overload
896
- def __eq__[OtherShape: _Shape](
897
- self, other: Tensor[OtherShape]
908
+ def __eq__[OtherShape: _Shape = []](
909
+ self, other: _TensorLike[OtherShape]
898
910
  ) -> Tensor[broadcast(Shape, OtherShape)]: ... # type: ignore[override]
899
- @overload
900
- def __eq__(self, other: builtins.float | builtins.int) -> Self: ... # type: ignore[override]
901
- @overload
902
- def __ne__[OtherShape: _Shape](
903
- self, other: Tensor[OtherShape]
911
+ def __ne__[OtherShape: _Shape = []](
912
+ self, other: _TensorLike[OtherShape]
904
913
  ) -> Tensor[broadcast(Shape, OtherShape)]: ... # type: ignore[override]
905
- @overload
906
- def __ne__(self, other: builtins.float | builtins.int) -> Self: ... # type: ignore[override]
907
914
  # Ordering results are gradual because they are tensors elementwise, but
908
915
  # scalar tensors are also valid in truth-valued comparison protocols.
909
916
  def __lt__(self, other: Tensor | builtins.float | builtins.int) -> Any: ...
@@ -916,24 +923,15 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
916
923
  # comparison operators do. Combining masks with `&` and `|` is the common
917
924
  # case, and those masks come from the comparisons above.
918
925
 
919
- @overload
920
- def __and__[OtherShape: _Shape](
921
- self, other: Tensor[OtherShape]
926
+ def __and__[OtherShape: _Shape = []](
927
+ self, other: _IntegerTensorLike[OtherShape]
922
928
  ) -> Tensor[broadcast(Shape, OtherShape)]: ...
923
- @overload
924
- def __and__(self, other: bool | int) -> Self: ...
925
- @overload
926
- def __or__[OtherShape: _Shape](
927
- self, other: Tensor[OtherShape]
929
+ def __or__[OtherShape: _Shape = []](
930
+ self, other: _IntegerTensorLike[OtherShape]
928
931
  ) -> Tensor[broadcast(Shape, OtherShape)]: ...
929
- @overload
930
- def __or__(self, other: bool | int) -> Self: ...
931
- @overload
932
- def __xor__[OtherShape: _Shape](
933
- self, other: Tensor[OtherShape]
932
+ def __xor__[OtherShape: _Shape = []](
933
+ self, other: _IntegerTensorLike[OtherShape]
934
934
  ) -> Tensor[broadcast(Shape, OtherShape)]: ...
935
- @overload
936
- def __xor__(self, other: bool | int) -> Self: ...
937
935
  def __rand__(self, other: bool | int) -> Self: ...
938
936
  def __ror__(self, other: bool | int) -> Self: ...
939
937
  def __rxor__(self, other: bool | int) -> Self: ...
@@ -1200,7 +1198,7 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
1200
1198
  def new_full(
1201
1199
  self,
1202
1200
  size: tuple[builtins.int, ...],
1203
- fill_value: builtins.float | builtins.int,
1201
+ fill_value: _Scalar,
1204
1202
  dtype: Any = None,
1205
1203
  device: Any = None,
1206
1204
  requires_grad: builtins.bool = False,
@@ -1293,8 +1291,14 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
1293
1291
  """Returns tensor as a nested Python list."""
1294
1292
  ...
1295
1293
 
1296
- def numpy(self: Tensor) -> Any:
1297
- """Returns tensor as a NumPy array."""
1294
+ def numpy[Shape: IntTuple](self: Tensor[Shape]) -> np.ndarray[Shape]:
1295
+ """Return a NumPy array with the same shape.
1296
+
1297
+ NumPy is an optional dependency of this stub package. Installing
1298
+ `pyrefly-numpy-stubs` adds shape inference for values created through
1299
+ NumPy APIs; otherwise only shape information already present in NumPy's
1300
+ own annotations is preserved.
1301
+ """
1298
1302
  ...
1299
1303
 
1300
1304
  # TODO: Restrict this to statically single-element tensors once the type
@@ -1675,22 +1679,26 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
1675
1679
  """Compute distance to another tensor. Returns scalar tensor."""
1676
1680
  ...
1677
1681
 
1678
- def cumsum[Shape: IntTuple](self: Tensor[Shape], dim: int) -> Tensor[Shape]:
1682
+ def cumsum[Shape: IntTuple](
1683
+ self: Tensor[Shape], dim: builtins.int
1684
+ ) -> Tensor[Shape]:
1679
1685
  """Cumulative sum along dimension. Shape-preserving operation."""
1680
1686
  ...
1681
1687
 
1682
- def cumprod[Shape: IntTuple](self: Tensor[Shape], dim: int) -> Tensor[Shape]:
1688
+ def cumprod[Shape: IntTuple](
1689
+ self: Tensor[Shape], dim: builtins.int
1690
+ ) -> Tensor[Shape]:
1683
1691
  """Cumulative product along dimension. Shape-preserving operation."""
1684
1692
  ...
1685
1693
 
1686
1694
  def cummax[Shape: IntTuple](
1687
- self: Tensor[Shape], dim: int
1695
+ self: Tensor[Shape], dim: builtins.int
1688
1696
  ) -> return_types.cummax[Shape]:
1689
1697
  """Cumulative maximum along dimension. Returns (values, indices). Shape-preserving operation."""
1690
1698
  ...
1691
1699
 
1692
1700
  def cummin[Shape: IntTuple](
1693
- self: Tensor[Shape], dim: int
1701
+ self: Tensor[Shape], dim: builtins.int
1694
1702
  ) -> return_types.cummin[Shape]:
1695
1703
  """Cumulative minimum along dimension. Returns (values, indices). Shape-preserving operation."""
1696
1704
  ...
@@ -1707,17 +1715,17 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
1707
1715
  self: Tensor[Shape],
1708
1716
  k: K,
1709
1717
  dim: Dim = -1,
1710
- largest: bool = True,
1711
- sorted: bool = True,
1718
+ largest: builtins.bool = True,
1719
+ sorted: builtins.bool = True,
1712
1720
  ) -> return_types.topk[topk_shape(Shape, Dim, K)]:
1713
1721
  """Top k elements. Returns (values, indices). Shape inference via meta-shape: torch.Tensor.topk"""
1714
1722
  ...
1715
1723
 
1716
1724
  def sort[Shape: IntTuple](
1717
1725
  self: Tensor[Shape],
1718
- dim: int = -1,
1719
- descending: bool = False,
1720
- stable: bool = False,
1726
+ dim: builtins.int = -1,
1727
+ descending: builtins.bool = False,
1728
+ stable: builtins.bool = False,
1721
1729
  ) -> return_types.sort[Shape]:
1722
1730
  """Sort tensor. Returns (values, indices). Shape-preserving operation."""
1723
1731
  ...
@@ -1727,7 +1735,10 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
1727
1735
  Dim: Flag[builtins.int],
1728
1736
  Keepdim: Flag[builtins.bool],
1729
1737
  ](
1730
- self: Tensor[Shape], k: int, dim: Dim = -1, keepdim: Keepdim = False
1738
+ self: Tensor[Shape],
1739
+ k: builtins.int,
1740
+ dim: Dim = -1,
1741
+ keepdim: Keepdim = False,
1731
1742
  ) -> return_types.kthvalue[reduce_shape(Shape, Dim, Keepdim)]:
1732
1743
  """Kth smallest value. Returns (values, indices). Shape inference via meta-shape: torch.Tensor.kthvalue"""
1733
1744
  ...
@@ -1745,11 +1756,11 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
1745
1756
  """Create diagonal tensor. Shape inference via meta-shape: torch.Tensor.diag_embed"""
1746
1757
  ...
1747
1758
 
1748
- def tril(self, diagonal: int = 0) -> Self:
1759
+ def tril(self, diagonal: builtins.int = 0) -> Self:
1749
1760
  """Lower triangular part. Shape inference via generic fixture signature."""
1750
1761
  ...
1751
1762
 
1752
- def triu(self, diagonal: int = 0) -> Self:
1763
+ def triu(self, diagonal: builtins.int = 0) -> Self:
1753
1764
  """Upper triangular part. Shape inference via generic fixture signature."""
1754
1765
  ...
1755
1766
 
@@ -2039,6 +2050,14 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
2039
2050
  """Element-wise hypotenuse. Shape inference via generic fixture signature."""
2040
2051
  ...
2041
2052
 
2053
+ @overload
2054
+ def lerp[OtherShape: _Shape, WeightShape: _Shape](
2055
+ self, end: Tensor[OtherShape], weight: Tensor[WeightShape]
2056
+ ) -> Tensor[broadcast(Shape, broadcast(OtherShape, WeightShape))]:
2057
+ """Linear interpolation. Shape inference via generic fixture signature."""
2058
+ ...
2059
+
2060
+ @overload
2042
2061
  def lerp[OtherShape: _Shape](
2043
2062
  self, end: Tensor[OtherShape], weight: builtins.float
2044
2063
  ) -> Tensor[broadcast(Shape, OtherShape)]:
@@ -2089,7 +2108,7 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
2089
2108
  """Digamma function. Shape inference via generic fixture signature."""
2090
2109
  ...
2091
2110
 
2092
- def polygamma(self, n: int) -> Self:
2111
+ def polygamma(self, n: builtins.int) -> Self:
2093
2112
  """Polygamma function. Shape inference via generic fixture signature."""
2094
2113
  ...
2095
2114
 
@@ -2191,32 +2210,32 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
2191
2210
  ...
2192
2211
 
2193
2212
  def maximum[OtherShape: _Shape = []](
2194
- self, other: _TensorLike[OtherShape]
2213
+ self, other: Tensor[OtherShape]
2195
2214
  ) -> Tensor[broadcast(Shape, OtherShape)]:
2196
2215
  """Element-wise maximum. Shape inference via generic fixture signature."""
2197
2216
  ...
2198
2217
 
2199
2218
  def minimum[OtherShape: _Shape = []](
2200
- self, other: _TensorLike[OtherShape]
2219
+ self, other: Tensor[OtherShape]
2201
2220
  ) -> Tensor[broadcast(Shape, OtherShape)]:
2202
2221
  """Element-wise minimum. Shape inference via generic fixture signature."""
2203
2222
  ...
2204
2223
 
2205
2224
  def fmax[OtherShape: _Shape = []](
2206
- self, other: _TensorLike[OtherShape]
2225
+ self, other: Tensor[OtherShape]
2207
2226
  ) -> Tensor[broadcast(Shape, OtherShape)]:
2208
2227
  """Element-wise maximum (NaN handling). Shape inference via generic fixture signature."""
2209
2228
  ...
2210
2229
 
2211
2230
  def fmin[OtherShape: _Shape = []](
2212
- self, other: _TensorLike[OtherShape]
2231
+ self, other: Tensor[OtherShape]
2213
2232
  ) -> Tensor[broadcast(Shape, OtherShape)]:
2214
2233
  """Element-wise minimum (NaN handling). Shape inference via generic fixture signature."""
2215
2234
  ...
2216
2235
 
2217
2236
  # ==== Phase 4: Advanced Linear Algebra Methods ====
2218
2237
 
2219
- def cholesky(self, upper: bool = False) -> Self:
2238
+ def cholesky(self, upper: builtins.bool = False) -> Self:
2220
2239
  """Cholesky decomposition. Shape inference via generic fixture signature."""
2221
2240
  ...
2222
2241
 
@@ -2244,7 +2263,7 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
2244
2263
  def slogdet[Shape: IntTuple](
2245
2264
  self: Tensor[Shape],
2246
2265
  ) -> return_types.slogdet[slogdet_shape(Shape)]: ...
2247
- def matrix_power(self, n: int) -> Self:
2266
+ def matrix_power(self, n: builtins.int) -> Self:
2248
2267
  """Matrix power. Shape inference via generic fixture signature."""
2249
2268
  ...
2250
2269
 
@@ -2336,7 +2355,7 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
2336
2355
  self,
2337
2356
  indices: tuple[Tensor, ...],
2338
2357
  values: Tensor,
2339
- accumulate: bool = False,
2358
+ accumulate: builtins.bool = False,
2340
2359
  ) -> Self:
2341
2360
  """Put values at indices. Shape inference via generic fixture signature."""
2342
2361
  ...
@@ -2345,7 +2364,7 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
2345
2364
  self,
2346
2365
  indices: tuple[Tensor, ...],
2347
2366
  values: Tensor,
2348
- accumulate: bool = False,
2367
+ accumulate: builtins.bool = False,
2349
2368
  ) -> Self:
2350
2369
  """Put values at indices in-place. Shape inference via generic fixture signature."""
2351
2370
  ...
@@ -2379,7 +2398,7 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
2379
2398
  self: Tensor[Shape],
2380
2399
  index: Tensor[IndexShape],
2381
2400
  source: Tensor[SourceShape],
2382
- accumulate: bool = False,
2401
+ accumulate: builtins.bool = False,
2383
2402
  ) -> Tensor[put_shape(Shape, IndexShape, SourceShape)]:
2384
2403
  """Put values at indices. Shape inference via generic fixture signature."""
2385
2404
  ...
@@ -2388,7 +2407,7 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
2388
2407
  self: Tensor[Shape],
2389
2408
  index: Tensor[IndexShape],
2390
2409
  source: Tensor[SourceShape],
2391
- accumulate: bool = False,
2410
+ accumulate: builtins.bool = False,
2392
2411
  ) -> Tensor[put_shape(Shape, IndexShape, SourceShape)]:
2393
2412
  """Put values at indices in-place. Shape inference via generic fixture signature."""
2394
2413
  ...
@@ -2438,8 +2457,8 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
2438
2457
 
2439
2458
  def random_(
2440
2459
  self,
2441
- low: int = 0,
2442
- high: int | None = None,
2460
+ low: builtins.int = 0,
2461
+ high: builtins.int | None = None,
2443
2462
  *,
2444
2463
  generator: Generator | None = None,
2445
2464
  ) -> Self:
@@ -2468,6 +2487,8 @@ class Tensor[Shape: _Shape = _Shape](_TensorBase):
2468
2487
  """Return the number of elements."""
2469
2488
  ...
2470
2489
 
2490
+ FloatTensor = Tensor
2491
+
2471
2492
  # ============================================================================
2472
2493
  # Module-level Functions
2473
2494
  # ============================================================================
@@ -2478,6 +2499,78 @@ def matmul[Left: IntTuple, Right: IntTuple](
2478
2499
  """Matrix multiplication function. Shape inference via meta-shape: torch.matmul"""
2479
2500
  ...
2480
2501
 
2502
+ @overload
2503
+ def unique(
2504
+ input: Tensor,
2505
+ sorted: builtins.bool = True,
2506
+ return_inverse: Literal[False] = False,
2507
+ return_counts: Literal[False] = False,
2508
+ dim: None = None,
2509
+ ) -> Tensor[[Any]]: ...
2510
+ @overload
2511
+ def unique(
2512
+ input: Tensor,
2513
+ sorted: builtins.bool = True,
2514
+ return_inverse: Literal[False] = False,
2515
+ return_counts: Literal[False] = False,
2516
+ dim: builtins.int = 0,
2517
+ ) -> Tensor: ...
2518
+ @overload
2519
+ def unique[Shape: IntTuple](
2520
+ input: Tensor[Shape],
2521
+ sorted: builtins.bool = True,
2522
+ return_inverse: Literal[True] = True,
2523
+ return_counts: Literal[False] = False,
2524
+ dim: None = None,
2525
+ ) -> tuple[Tensor[[Any]], Tensor[Shape]]: ...
2526
+ @overload
2527
+ def unique(
2528
+ input: Tensor,
2529
+ sorted: builtins.bool = True,
2530
+ return_inverse: Literal[True] = True,
2531
+ return_counts: Literal[False] = False,
2532
+ dim: builtins.int = 0,
2533
+ ) -> tuple[Tensor, Tensor[[Any]]]: ...
2534
+ @overload
2535
+ def unique(
2536
+ input: Tensor,
2537
+ sorted: builtins.bool = True,
2538
+ return_inverse: Literal[False] = False,
2539
+ return_counts: Literal[True] = True,
2540
+ dim: None = None,
2541
+ ) -> tuple[Tensor[[Any]], Tensor[[Any]]]: ...
2542
+ @overload
2543
+ def unique(
2544
+ input: Tensor,
2545
+ sorted: builtins.bool = True,
2546
+ return_inverse: Literal[False] = False,
2547
+ return_counts: Literal[True] = True,
2548
+ dim: builtins.int = 0,
2549
+ ) -> tuple[Tensor, Tensor[[Any]]]: ...
2550
+ @overload
2551
+ def unique[Shape: IntTuple](
2552
+ input: Tensor[Shape],
2553
+ sorted: builtins.bool = True,
2554
+ return_inverse: Literal[True] = True,
2555
+ return_counts: Literal[True] = True,
2556
+ dim: None = None,
2557
+ ) -> tuple[Tensor[[Any]], Tensor[Shape], Tensor[[Any]]]: ...
2558
+ @overload
2559
+ def unique(
2560
+ input: Tensor,
2561
+ sorted: builtins.bool = True,
2562
+ return_inverse: Literal[True] = True,
2563
+ return_counts: Literal[True] = True,
2564
+ dim: builtins.int = 0,
2565
+ ) -> tuple[Tensor, Tensor[[Any]], Tensor[[Any]]]: ...
2566
+ @overload
2567
+ def unique(
2568
+ input: Tensor,
2569
+ sorted: builtins.bool = True,
2570
+ return_inverse: builtins.bool = False,
2571
+ return_counts: builtins.bool = False,
2572
+ dim: builtins.int | None = None,
2573
+ ) -> Tensor | tuple[Tensor, Tensor] | tuple[Tensor, Tensor, Tensor]: ...
2481
2574
  def cat[Shapes: IntTuples, Dim: Flag[builtins.int]](
2482
2575
  tensors: MapIntTuples[lambda S: Tensor[S], Shapes], dim: Dim = 0
2483
2576
  ) -> Tensor[cat_shape(Shapes, Dim)]:
@@ -2883,14 +2976,21 @@ def ones(
2883
2976
  ) -> Tensor: ...
2884
2977
  @overload
2885
2978
  def empty[Shape: IntTuple](
2886
- *size: *Shape, dtype: Any = None, device: Any = None
2979
+ *size: *Shape,
2980
+ dtype: Any = None,
2981
+ device: Any = None,
2982
+ requires_grad: builtins.bool = False,
2887
2983
  ) -> Tensor[Shape]:
2888
2984
  """Create uninitialized tensor. Shape is inferred from `size`."""
2889
2985
  ...
2890
2986
 
2891
2987
  @overload
2892
2988
  def empty[Shape: IntTuple](
2893
- size: Shape, dtype: Any = None, device: Any = None
2989
+ size: Shape,
2990
+ *,
2991
+ dtype: Any = None,
2992
+ device: Any = None,
2993
+ requires_grad: builtins.bool = False,
2894
2994
  ) -> Tensor[Shape]:
2895
2995
  """Create uninitialized tensor. Shape is inferred from `size`."""
2896
2996
  ...
@@ -3601,6 +3701,14 @@ def atan[Shape: IntTuple](input: Tensor[Shape]) -> Tensor[Shape]:
3601
3701
  """Element-wise arctangent. Shape inference via generic fixture signature."""
3602
3702
  ...
3603
3703
 
3704
+ @overload
3705
+ def lerp[Shape: IntTuple, OtherShape: IntTuple, WeightShape: IntTuple](
3706
+ input: Tensor[Shape], end: Tensor[OtherShape], weight: Tensor[WeightShape]
3707
+ ) -> Tensor[broadcast(Shape, broadcast(OtherShape, WeightShape))]:
3708
+ """Linear interpolation. Shape inference via generic fixture signature."""
3709
+ ...
3710
+
3711
+ @overload
3604
3712
  def lerp[Shape: IntTuple, OtherShape: IntTuple](
3605
3713
  input: Tensor[Shape], end: Tensor[OtherShape], weight: builtins.float
3606
3714
  ) -> Tensor[broadcast(Shape, OtherShape)]:
@@ -3775,13 +3883,13 @@ def allclose(
3775
3883
  equal_nan: builtins.bool = False,
3776
3884
  ) -> builtins.bool: ...
3777
3885
  def maximum[Shape: IntTuple, OtherShape: IntTuple = []](
3778
- input: Tensor[Shape], other: _TensorLike[OtherShape]
3886
+ input: Tensor[Shape], other: Tensor[OtherShape]
3779
3887
  ) -> Tensor[broadcast(Shape, OtherShape)]:
3780
3888
  """Element-wise maximum. Shape inference via generic fixture signature."""
3781
3889
  ...
3782
3890
 
3783
3891
  def minimum[Shape: IntTuple, OtherShape: IntTuple = []](
3784
- input: Tensor[Shape], other: _TensorLike[OtherShape]
3892
+ input: Tensor[Shape], other: Tensor[OtherShape]
3785
3893
  ) -> Tensor[broadcast(Shape, OtherShape)]:
3786
3894
  """Element-wise minimum. Shape inference via generic fixture signature."""
3787
3895
  ...
@@ -3790,13 +3898,13 @@ def expm1[Shape: IntTuple](input: Tensor[Shape]) -> Tensor[Shape]: ...
3790
3898
  def log10[Shape: IntTuple](input: Tensor[Shape]) -> Tensor[Shape]: ...
3791
3899
  def sign[Shape: IntTuple](input: Tensor[Shape]) -> Tensor[Shape]: ...
3792
3900
  def fmax[Shape: IntTuple, OtherShape: IntTuple = []](
3793
- input: Tensor[Shape], other: _TensorLike[OtherShape]
3901
+ input: Tensor[Shape], other: Tensor[OtherShape]
3794
3902
  ) -> Tensor[broadcast(Shape, OtherShape)]:
3795
3903
  """Element-wise maximum (NaN handling). Shape inference via generic fixture signature."""
3796
3904
  ...
3797
3905
 
3798
3906
  def fmin[Shape: IntTuple, OtherShape: IntTuple = []](
3799
- input: Tensor[Shape], other: _TensorLike[OtherShape]
3907
+ input: Tensor[Shape], other: Tensor[OtherShape]
3800
3908
  ) -> Tensor[broadcast(Shape, OtherShape)]:
3801
3909
  """Element-wise minimum (NaN handling). Shape inference via generic fixture signature."""
3802
3910
  ...
@@ -4107,7 +4215,7 @@ def poisson[Shape: IntTuple](input: Tensor[Shape]) -> Tensor[Shape]:
4107
4215
  ...
4108
4216
 
4109
4217
  # Tensor property functions
4110
- def is_tensor(obj: object, /) -> builtins.bool:
4218
+ def is_tensor(obj: object, /) -> TypeIs[Tensor]:
4111
4219
  """Return whether an object is a PyTorch tensor."""
4112
4220
  ...
4113
4221
 
@@ -4138,6 +4246,8 @@ class dtype:
4138
4246
  ...
4139
4247
 
4140
4248
  float: dtype = ...
4249
+ complex64: dtype = ...
4250
+ complex128: dtype = ...
4141
4251
 
4142
4252
  # ==============================================================================
4143
4253
  # Tensor Creation with dtype support
@@ -4181,10 +4291,27 @@ def as_tensor(
4181
4291
  """Create tensor from data, sharing memory with the input when possible."""
4182
4292
  ...
4183
4293
 
4184
- def from_numpy(ndarray: Any) -> Tensor:
4185
- """Create a CPU tensor that shares memory with a numpy array."""
4294
+ def from_numpy[Shape: IntTuple](ndarray: np.ndarray[Shape]) -> Tensor[Shape]:
4295
+ """Create a shape-preserving CPU tensor that shares memory with a NumPy array.
4296
+
4297
+ NumPy is an optional dependency of this stub package. Installing
4298
+ `pyrefly-numpy-stubs` adds shape inference for values created through NumPy
4299
+ APIs; otherwise only shape information already present in NumPy's own
4300
+ annotations is preserved.
4301
+ """
4186
4302
  ...
4187
4303
 
4304
+ @overload
4305
+ def randint[Shape: IntTuple](
4306
+ high: int,
4307
+ size: Shape,
4308
+ *,
4309
+ generator: Any = None,
4310
+ dtype: Any = None,
4311
+ device: Any = None,
4312
+ requires_grad: bool = False,
4313
+ ) -> Tensor[Shape]: ...
4314
+ @overload
4188
4315
  def randint[Shape: IntTuple](
4189
4316
  low: int,
4190
4317
  high: int,
@@ -4291,6 +4418,14 @@ def cross[B: IntTuple](
4291
4418
  ...
4292
4419
 
4293
4420
  # Context managers
4421
+ # Define the top-level API directly because older Torch versions lack torch.compiler.
4422
+ @overload
4423
+ def compile[F: Callable[..., Any]](model: F, **kwargs: Any) -> F: ...
4424
+ @overload
4425
+ def compile[**P, R](
4426
+ model: None = None, **kwargs: Any
4427
+ ) -> Callable[[Callable[P, R]], Callable[P, R]]: ...
4428
+
4294
4429
  class no_grad:
4295
4430
  """Context manager and decorator that disables gradient tracking.
4296
4431
 
@@ -4345,6 +4480,10 @@ def meshgrid(
4345
4480
  # The functions below carry no shape information. They are declared because this
4346
4481
  # module shadows torch's own `__init__`, as described at the `torch._C` import
4347
4482
  # above, so omitting them removes them from `torch` for every dependent target.
4483
+ def set_default_dtype(d: dtype, /) -> None: ...
4484
+ def get_num_threads() -> builtins.int: ...
4485
+ def get_float32_matmul_precision() -> str: ...
4486
+ def are_deterministic_algorithms_enabled() -> builtins.bool: ...
4348
4487
  def manual_seed(seed: int) -> Generator:
4349
4488
  """Set the seed for generating random numbers on all devices."""
4350
4489
  ...