pyrefly-shape-extensions 1.3.0.dev4__tar.gz → 1.3.1__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.
- {pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/PKG-INFO +2 -2
- {pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/README.md +1 -1
- {pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/pyproject.toml +1 -1
- {pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/shape_extensions/__init__.py +48 -43
- {pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/LICENSE +0 -0
- {pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/shape_extensions/dsl.py +0 -0
- {pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/shape_extensions/py.typed +0 -0
- {pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/shape_extensions/torchscript.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: pyrefly-shape-extensions
|
|
3
|
-
Version: 1.3.
|
|
3
|
+
Version: 1.3.1
|
|
4
4
|
Summary: Shape typing primitives and DSL for Pyrefly tensor shape tracking
|
|
5
5
|
Project-URL: homepage, https://pyrefly.org
|
|
6
6
|
Project-URL: documentation, https://pyrefly.org/en/docs/
|
|
@@ -41,7 +41,7 @@ Runtime helpers for Pyrefly tensor shape annotations.
|
|
|
41
41
|
This package provides the lightweight `shape_extensions` module used by
|
|
42
42
|
Pyrefly's tensor shape stubs. It defines runtime no-op versions of the shape
|
|
43
43
|
typing primitives so annotations such as `Tensor[B, T]`, `IntVar("B")`, and
|
|
44
|
-
`assert_shape(x, (2, 3))` can be evaluated by Python while Pyrefly uses the
|
|
44
|
+
`assert_shape(x.shape, (2, 3))` can be evaluated by Python while Pyrefly uses the
|
|
45
45
|
corresponding stubs for static shape checking.
|
|
46
46
|
|
|
47
47
|
The package is versioned in lockstep with Pyrefly.
|
|
@@ -5,7 +5,7 @@ Runtime helpers for Pyrefly tensor shape annotations.
|
|
|
5
5
|
This package provides the lightweight `shape_extensions` module used by
|
|
6
6
|
Pyrefly's tensor shape stubs. It defines runtime no-op versions of the shape
|
|
7
7
|
typing primitives so annotations such as `Tensor[B, T]`, `IntVar("B")`, and
|
|
8
|
-
`assert_shape(x, (2, 3))` can be evaluated by Python while Pyrefly uses the
|
|
8
|
+
`assert_shape(x.shape, (2, 3))` can be evaluated by Python while Pyrefly uses the
|
|
9
9
|
corresponding stubs for static shape checking.
|
|
10
10
|
|
|
11
11
|
The package is versioned in lockstep with Pyrefly.
|
|
@@ -8,7 +8,7 @@ description = "Shape typing primitives and DSL for Pyrefly tensor shape tracking
|
|
|
8
8
|
readme = "README.md"
|
|
9
9
|
requires-python = ">=3.12"
|
|
10
10
|
license = { file = "LICENSE" }
|
|
11
|
-
version = "1.3.
|
|
11
|
+
version = "1.3.1"
|
|
12
12
|
keywords = ["typechecker", "typechecking", "shapes", "tensor", "pyrefly"]
|
|
13
13
|
classifiers = [
|
|
14
14
|
"Development Status :: 4 - Beta",
|
{pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/shape_extensions/__init__.py
RENAMED
|
@@ -31,7 +31,6 @@ __all__ = [
|
|
|
31
31
|
"broadcast",
|
|
32
32
|
"defines_assert_shape",
|
|
33
33
|
"gufunc_broadcast",
|
|
34
|
-
"shaped_array",
|
|
35
34
|
"index_shape",
|
|
36
35
|
"type_shape_dsl_function",
|
|
37
36
|
]
|
|
@@ -89,12 +88,25 @@ def _patch_torch_if_available() -> None:
|
|
|
89
88
|
_patch_torch_if_available()
|
|
90
89
|
|
|
91
90
|
|
|
91
|
+
def _patch_jax_if_available() -> None:
|
|
92
|
+
try:
|
|
93
|
+
import jax # @manual
|
|
94
|
+
except ImportError:
|
|
95
|
+
return
|
|
96
|
+
|
|
97
|
+
if hasattr(jax, "Array") and not hasattr(jax.Array, "__class_getitem__"):
|
|
98
|
+
jax.Array.__class_getitem__ = classmethod(_return_class)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
_patch_jax_if_available()
|
|
102
|
+
|
|
103
|
+
|
|
92
104
|
class IntTuple:
|
|
93
105
|
"""Tuple-valued shape annotation surface.
|
|
94
106
|
|
|
95
|
-
In type positions, Pyrefly treats `IntTuple` as
|
|
96
|
-
`tuple[int, ...]`. At runtime, calling it coerces any
|
|
97
|
-
tuple.
|
|
107
|
+
In type positions, Pyrefly treats `IntTuple` as a whole shape whose runtime
|
|
108
|
+
representation is `tuple[int, ...]`. At runtime, calling it coerces any
|
|
109
|
+
iterable to a plain tuple.
|
|
98
110
|
"""
|
|
99
111
|
|
|
100
112
|
def __new__(cls, iterable=()):
|
|
@@ -112,30 +124,30 @@ class IntTuples:
|
|
|
112
124
|
|
|
113
125
|
|
|
114
126
|
class Elements:
|
|
115
|
-
"""Inverse of ``tuple[Unpack[S]]``: extracts the
|
|
127
|
+
"""Inverse of ``tuple[Unpack[S]]``: extracts the dimensions from an ``IntTuple``.
|
|
116
128
|
|
|
117
129
|
In the Python typing spec, ``tuple[Unpack[Ts]]`` wraps a ``TypeVarTuple`` into a
|
|
118
|
-
concrete tuple type. ``Elements[S]`` is the conceptual inverse: given
|
|
119
|
-
|
|
130
|
+
concrete tuple type. ``Elements[S]`` is the conceptual inverse: given an ``IntTuple``
|
|
131
|
+
shape ``S``, ``*Elements[S]`` splices its dimensions into a shape position,
|
|
120
132
|
e.g. ``Array[[*Elements[S], OUT], DType]``.
|
|
121
133
|
|
|
122
134
|
This fills a gap in the current typing spec — there is no standard mechanism to
|
|
123
|
-
decompose a variadic
|
|
135
|
+
decompose a variadic shape without a ``TypeVarTuple``. Pyrefly uses the ``.pyi``
|
|
124
136
|
stub for type inference; this class exists so annotations evaluate without crashing
|
|
125
137
|
at runtime.
|
|
126
138
|
"""
|
|
127
139
|
|
|
128
|
-
def __init__(self,
|
|
129
|
-
self.
|
|
140
|
+
def __init__(self, shape):
|
|
141
|
+
self.shape = shape
|
|
130
142
|
|
|
131
|
-
def __class_getitem__(cls,
|
|
132
|
-
return cls(
|
|
143
|
+
def __class_getitem__(cls, shape):
|
|
144
|
+
return cls(shape)
|
|
133
145
|
|
|
134
146
|
def __iter__(self):
|
|
135
147
|
yield self
|
|
136
148
|
|
|
137
149
|
def __repr__(self):
|
|
138
|
-
return f"Elements[{self.
|
|
150
|
+
return f"Elements[{self.shape!r}]"
|
|
139
151
|
|
|
140
152
|
|
|
141
153
|
class Int[T]:
|
|
@@ -243,53 +255,40 @@ def defines_assert_shape(fn: typing.Callable) -> typing.Callable:
|
|
|
243
255
|
"""
|
|
244
256
|
Decorator that marks a function as an assert_shape helper.
|
|
245
257
|
|
|
246
|
-
Used in order to allow custom assert_shape functions if necessary
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
the `assert_shape` function of this library.
|
|
258
|
+
Used in order to allow custom assert_shape functions if necessary. A default
|
|
259
|
+
version that works for tuple-like shapes is defined in the `assert_shape`
|
|
260
|
+
function of this library.
|
|
250
261
|
"""
|
|
251
262
|
return fn
|
|
252
263
|
|
|
253
264
|
|
|
254
265
|
@defines_assert_shape
|
|
255
|
-
def assert_shape(
|
|
266
|
+
def assert_shape(actual, shape):
|
|
256
267
|
"""
|
|
257
|
-
At runtime, assert that
|
|
258
|
-
some type like `torch.Tensor` or `np.ndarray` that has a tuple-valued
|
|
259
|
-
field `shape` indicating the shape.
|
|
268
|
+
At runtime, assert that a tuple-like shape has the expected value.
|
|
260
269
|
|
|
261
|
-
Pyrefly will validate that the
|
|
262
|
-
|
|
270
|
+
Pyrefly will validate that the statically modeled shape matches, similar to
|
|
271
|
+
`assert_type`.
|
|
263
272
|
|
|
264
273
|
TODO(stroxler): for now, symbolic dimensions are skipped at runtime,
|
|
265
274
|
so in the case of a symbolic `shape` the runtime validation is only checking
|
|
266
275
|
the rank for those axes. But the static analysis will fully validate.
|
|
267
276
|
"""
|
|
268
277
|
|
|
269
|
-
|
|
278
|
+
# Preserve legacy calls that pass an array object rather than its shape.
|
|
279
|
+
if not isinstance(actual, tuple) and hasattr(actual, "shape"):
|
|
280
|
+
actual_tuple = tuple(actual.shape)
|
|
281
|
+
else:
|
|
282
|
+
actual_tuple = tuple(actual)
|
|
270
283
|
expected = tuple(shape)
|
|
271
284
|
if any(isinstance(dim, SymbolicArithExpr) for dim in expected):
|
|
272
|
-
if len(
|
|
285
|
+
if len(actual_tuple) != len(expected):
|
|
273
286
|
raise AssertionError(
|
|
274
|
-
f"expected rank {len(expected)} for shape {expected}, got shape {
|
|
287
|
+
f"expected rank {len(expected)} for shape {expected}, got shape {actual_tuple}"
|
|
275
288
|
)
|
|
276
|
-
elif
|
|
277
|
-
raise AssertionError(f"expected shape {expected}, got {
|
|
278
|
-
return
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
def shaped_array(
|
|
282
|
-
*, shape: str, builtin_indexing: bool = True
|
|
283
|
-
) -> typing.Callable[[type], type]:
|
|
284
|
-
"""Mark a class as carrying a shape parameter.
|
|
285
|
-
|
|
286
|
-
``builtin_indexing=False`` lets its annotated ``__getitem__`` determine the result.
|
|
287
|
-
"""
|
|
288
|
-
|
|
289
|
-
def decorator(cls: type) -> type:
|
|
290
|
-
return cls
|
|
291
|
-
|
|
292
|
-
return decorator
|
|
289
|
+
elif actual_tuple != expected:
|
|
290
|
+
raise AssertionError(f"expected shape {expected}, got {actual_tuple}")
|
|
291
|
+
return actual
|
|
293
292
|
|
|
294
293
|
|
|
295
294
|
def index_shape(_shape: IntTuple, _index: typing.Any) -> IntTuple:
|
|
@@ -394,6 +393,9 @@ class IntVar:
|
|
|
394
393
|
def __typing_subst__(self, arg):
|
|
395
394
|
return arg
|
|
396
395
|
|
|
396
|
+
def has_default(self):
|
|
397
|
+
return False
|
|
398
|
+
|
|
397
399
|
|
|
398
400
|
class TypeVarTuple:
|
|
399
401
|
"""TypeVarTuple with support for integer shape dimensions.
|
|
@@ -431,3 +433,6 @@ class TypeVarTuple:
|
|
|
431
433
|
@property
|
|
432
434
|
def __typing_is_unpacked_typevartuple__(self):
|
|
433
435
|
return True
|
|
436
|
+
|
|
437
|
+
def has_default(self):
|
|
438
|
+
return False
|
|
File without changes
|
{pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/shape_extensions/dsl.py
RENAMED
|
File without changes
|
{pyrefly_shape_extensions-1.3.0.dev4 → pyrefly_shape_extensions-1.3.1}/shape_extensions/py.typed
RENAMED
|
File without changes
|
|
File without changes
|