1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
import einx
import inspect
from functools import partial
import threading
class Application:
def __init__(self, op, args, kwargs, output, signature, inplace_updates, comment, depend_on):
self.op = op
self.args = args
self.kwargs = kwargs
self.output = Tracer() if output is None else output
self.signature = signature
self.inplace_updates = inplace_updates
self.comment = comment
self.depend_on = depend_on
def update_origin(tracer, key):
tracer.origin = self
einx.tree_util.tree_map_with_key(update_origin, self.output)
# TODO: move this somewhere else?
if signature == "reshape":
params = inspect.getcallargs(lambda tensor, shape: None, *args, **kwargs)
self.shape = params["shape"]
self.tensor = params["tensor"]
elif signature == "broadcast_to":
params = inspect.getcallargs(lambda tensor, shape: None, *args, **kwargs)
self.shape = params["shape"]
self.tensor = params["tensor"]
elif signature == "transpose":
params = inspect.getcallargs(lambda tensor, permutation: None, *args, **kwargs)
self.permutation = params["permutation"]
self.tensor = params["tensor"]
@property
def dependencies(self):
return [self.op] + list(self.args) + list(self.kwargs.values()) + self.depend_on
def apply(
op,
args=[],
kwargs={},
output=None,
signature=None,
inplace_updates=[],
comment=None,
depend_on=[],
):
if isinstance(op, partial):
return apply(
op.func,
args=list(op.args) + list(args),
kwargs={**op.keywords, **kwargs},
output=output,
signature=signature,
inplace_updates=inplace_updates,
comment=comment,
depend_on=depend_on,
)
elif isinstance(op, TracableFunction):
assert len(inplace_updates) == 0
got_output = op(*args, **kwargs)
if not output is None:
def check(got_output, expected_output):
if type(got_output) != type(expected_output):
# TODO: also compare shape etc
raise ValueError(
f"Expected output type {type(expected_output)} when tracing "
f"TracableFunction, got {type(got_output)}"
)
einx.tree_util.tree_map(check, got_output, output)
return got_output
else:
return Application(
op,
args=args,
kwargs=kwargs,
output=output,
signature=signature,
inplace_updates=inplace_updates,
comment=comment,
depend_on=depend_on + _get_depend_on_stack(),
).output
_thread_local = threading.local()
def _get_depend_on_stack():
if not hasattr(_thread_local, "depend_on"):
_thread_local.depend_on = []
return _thread_local.depend_on
class depend_on:
def __init__(self, tracers):
self.tracer = list(einx.tree_util.tree_flatten(tracers))
def __enter__(self):
_get_depend_on_stack().append(self.tracer)
def __exit__(self, *args):
assert _get_depend_on_stack()[-1] is self.tracer
_get_depend_on_stack().pop()
class Tracer:
def __init__(self, origin=None):
self.origin = origin
def __getattr__(self, key):
if key.startswith("__") and key.endswith("__"):
return object.__getattribute__(self, key)
else:
return MemberAccess()(self, key)
def __getitem__(self, key):
return GetAt()(self, key)
def __call__(self, *args, **kwargs):
return apply(self, args=args, kwargs=kwargs)
def __copy__(self):
assert type(self) == Tracer
return Tracer()
class Import(Tracer):
def __init__(self, import_, as_, from_):
Tracer.__init__(self, origin="constant")
self.import_ = import_
self.as_ = as_
self.from_ = from_
def __call__(self): # Overwrite allowed arguments
return apply(self)
def import_(import_, as_=None, from_=None):
return Import(import_, as_, from_)()
class MemberAccess(Tracer):
def __init__(self):
Tracer.__init__(self, origin="constant")
def __call__(self, obj, key): # Overwrite allowed arguments
assert isinstance(key, str)
return apply(self, args=[obj, key])
class Operator(Tracer):
def __init__(self, op: str):
Tracer.__init__(self, origin="constant")
self.op = op
def __call__(self, *args): # Overwrite allowed arguments
return apply(self, args=args)
class AssignAt(Tracer):
def __init__(self, op: str):
Tracer.__init__(self, origin="constant")
self.op = op
def __call__(self, obj, key, update): # Overwrite allowed arguments
return apply(self, args=[obj, key, update])
class GetAt(Tracer):
def __init__(self):
Tracer.__init__(self, origin="constant")
def __call__(self, obj, key): # Overwrite allowed arguments
return apply(self, args=[obj, key])
class Function(Tracer):
def __init__(self, output):
self.output = output
def __copy__(self):
return Function(self.output)
def __call__(self, *args, **kwargs):
return apply(
self,
args=args,
kwargs=kwargs,
output=einx.tree_util.tree_map(lambda x: x.__copy__(), self.output),
)
class TracableFunction(Tracer):
def __init__(self, func=None, args=None, kwargs=None, virtual_args=[], output=None, name=None):
Tracer.__init__(self)
if isinstance(func, Tracer):
raise ValueError(f"func cannot be a tracer object")
if not output is None and args is None and kwargs is None:
raise ValueError(f"Cannot create a TracableFunction with an output but no input")
if args is None and not kwargs is None:
args = []
if not args is None and kwargs is None:
kwargs = {}
if not func is None and output is None and (not args is None or not kwargs is None):
output = func(*args, **kwargs)
self.func = func
self.args = args
self.kwargs = kwargs
self.virtual_args = virtual_args
self.output = output
self.name = name
def __call__(self, *args, **kwargs):
if self.func is None:
raise NotImplementedError(
f"Cannot call a TracableFunction that was created without a callable function"
)
return self.func(*args, **kwargs)
class Usages:
def __init__(self, tracers):
self.usages = {} # tracer-id: [using-applications]
def _capture_usages(x):
if not id(x) in self.usages:
self.usages[id(x)] = []
if isinstance(x, (list, tuple)):
for y in x:
_capture_usages(y)
elif isinstance(x, dict):
for y in x.values():
_capture_usages(y)
elif isinstance(x, Tracer) and isinstance(x.origin, Application):
for y in x.origin.dependencies:
if isinstance(y, Tracer):
# Add x.origin to y's usages
if not id(y) in self.usages:
self.usages[id(y)] = []
for usage in self.usages[id(y)]:
if id(usage) == id(x.origin):
break
else:
self.usages[id(y)].append(x.origin)
# Continue capturing usages with y
_capture_usages(y)
elif isinstance(x, TracableFunction):
_capture_usages(x.func)
_capture_usages(x.args)
_capture_usages(x.kwargs)
_capture_usages(x.output)
_capture_usages(tracers)
def get(self, tracers):
usages = []
def retrieve_usages(tracer):
if id(tracer) in self.usages:
usages.extend(self.usages[id(tracer)])
einx.tree_util.tree_map(retrieve_usages, tracers)
return usages
def trace(func=None, args=None, kwargs=None):
if func is None:
return partial(trace, args=args, kwargs=kwargs)
else:
return TracableFunction(func=func, args=args, kwargs=kwargs)
|