8
from dataclasses import dataclass
9
from typing import Union
24
def __init__(self, name: str, size: Union[None, int] = None):
27
self._vmap_level = None
32
if self._vmap_level is not None:
33
_vmap_active_levels[self._vmap_stack].alive = False
35
not _vmap_levels[-1].alive
36
and current_level() == _vmap_levels[-1].level
38
_vmap_decrement_nesting()
47
def size(self, size: int):
48
from . import DimensionBindError
50
if self._size is None:
52
self._vmap_level = _vmap_increment_nesting(size, "same")
53
self._vmap_stack = len(_vmap_levels)
54
_vmap_levels.append(LevelInfo(self._vmap_level))
56
elif self._size != size:
57
raise DimensionBindError(
58
f"Dim '{self}' previously bound to a dimension of size {self._size} cannot bind to a dimension of size {size}"
63
return self._size is not None
69
def extract_name(inst):
70
assert inst.opname == "STORE_FAST" or inst.opname == "STORE_NAME"
78
frame = inspect.currentframe()
79
assert frame is not None
80
calling_frame = frame.f_back
81
assert calling_frame is not None
82
code, lasti = calling_frame.f_code, calling_frame.f_lasti
85
first = lasti // 2 + 1
86
instructions = list(dis.get_instructions(calling_frame.f_code))
87
unpack = instructions[first]
89
if unpack.opname == "STORE_FAST" or unpack.opname == "STORE_NAME":
92
ctor = Dim if lists == 0 else DimList
93
_cache[key] = lambda: ctor(name=name)
95
assert unpack.opname == "UNPACK_SEQUENCE"
98
extract_name(instructions[first + 1 + i]) for i in range(ndims)
100
first_list = len(names) - lists
101
_cache[key] = lambda: tuple(
102
Dim(n) if i < first_list else DimList(name=n)
103
for i, n in enumerate(names)
108
def _dim_set(positional, arg):
110
if isinstance(a, Dim):
113
assert isinstance(a, int)
118
elif not isinstance(arg, (Dim, int)):
119
return tuple(convert(a) for a in arg)
121
return (convert(arg),)