-
Notifications
You must be signed in to change notification settings - Fork 84
/
trace_interpreter.py
382 lines (311 loc) · 16 KB
/
trace_interpreter.py
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from functools import partial
from typing import Any
from thunder.core import prims
from thunder.core.pytree import tree_map, tree_flatten_with_dataclass
from thunder.core.trace import VariableInterface, from_trace, tracectx
from thunder.core.baseutils import ProxyInterface, TensorProxyInterface
from thunder.core.utils import safe_map_flat, sequencify
from thunder.core.proxies import variableify
from thunder.core.transform_common import VJPDual
# TODO: Currently we use trace.args and trace.kwargs to get the arguments
# Maybe we should use these instead
trace_interpreter_skip_list = (
prims.PrimIDs.UNPACK_EMPTY_DICT,
prims.PrimIDs.UNPACK_KEY,
prims.PrimIDs.UNPACK_SEQUENCE,
prims.PrimIDs.UNPACK_TRIVIAL,
prims.PrimIDs.RETURN,
)
def interpret_trace(trace, *args, symbol_mapper=None, with_env=False, **kwargs):
"""Interpret a trace.
Args:
trace: trace to interpret
*args: arguments to interpret the trace with
symbol_mapper: function that redirects the evaluation of a BoundSymbol to a different function
with_env: whether to return the environment after interpreting the trace. Environment is a dictionary
that maps VariableInterface objects to their values.
**kwargs: keyword arguments to interpret the trace with
Returns:
result of interpreting the trace, optionally with the environment that saves all intermediate values
"""
env = {}
def read(x: VariableInterface | Any) -> Any:
if isinstance(x, VariableInterface):
return env[x.name]
else:
return x
def write(v: VariableInterface | Any, val: Any, allow_duplicates=False) -> None:
if not isinstance(v, VariableInterface):
return
if v.name in env:
if allow_duplicates:
# Duplicates are allowed and not overwritten
return
raise ValueError(f"Variable {v.name} is being overwritten this is not allowed")
env[v.name] = val
safe_map_flat(write, list(trace.args), list(args))
safe_map_flat(write, list(trace.kwargs.values()), list(kwargs.values()))
for symbol in trace.bound_symbols:
if symbol.sym.id in trace_interpreter_skip_list:
continue
args = tree_map(read, symbol.args)
kwargs = tree_map(read, symbol.kwargs)
prim_func = symbol_mapper(symbol) if symbol_mapper is not None else symbol.sym
if prim_func is None:
continue
result = prim_func(*args, **kwargs)
try:
safe_map_flat(write, list(sequencify(symbol.output)), list(sequencify(result)))
except AssertionError as e:
raise RuntimeError(
f"Error while assigning the result of dispatched function {prim_func} to the output of the original symbol {symbol}."
" This is likely due to a mismatch in the number of outputs."
f" The original symbol has {len(symbol.output)} outputs and the dispatched function has {len(sequencify(result))} outputs."
) from e
if with_env:
return tree_map(read, trace.output), env
return tree_map(read, trace.output)
def interpret_trace_to_trace(trace, *args, symbol_mapper=None, with_env=False, **kwargs):
"""Interpret a trace.
Args:
trace: trace to interpret
*args: arguments to interpret the trace with
symbol_mapper: function that redirects the evaluation of a BoundSymbol to a different function
with_env: whether to return the environment after interpreting the trace. Environment is a dictionary
that maps VariableInterface objects to their values.
**kwargs: keyword arguments to interpret the trace with
Returns:
result of interpreting the trace, optionally with the environment that saves all intermediate values
"""
env = {}
def read(x: VariableInterface | Any) -> Any:
if isinstance(x, VariableInterface):
return env[x.name]
else:
return x
def write(v: VariableInterface | Any, val: Any, allow_duplicates=False) -> None:
if not isinstance(v, VariableInterface):
return
if v.name in env:
if allow_duplicates:
# Duplicates are allowed and not overwritten
return
raise ValueError(f"Variable {v.name} is being overwritten this is not allowed")
env[v.name] = val
def add_to_swap_map(old, new):
if isinstance(old, ProxyInterface):
if isinstance(new, ProxyInterface) and variableify(new) in env:
# the new isn't new, but something returned the input
# this means we need to map the old to the new
old, new = new, old
elif isinstance(old, TensorProxyInterface):
# should we have a fix shapes pass? the sharding
# (FSDP, tensor parallel) transforms do "break" shape metadata
new_trace.names.remove(old.name) # taken by the .replace proxy
if isinstance(new, VJPDual):
old = old.replace(shape=new.primal._shape)
else:
old = old.replace(shape=new._shape)
if isinstance(new, VJPDual):
swap_map[variableify(new.primal)] = old
new.primal = old
else:
assert isinstance(new, ProxyInterface), (old, new)
swap_map[variableify(new)] = old
def do_swap(v):
if isinstance(v, VJPDual):
v.primal = tree_map(do_swap, v.primal)
v.residuals = tree_map(do_swap, v.residuals)
return v
if not isinstance(v, ProxyInterface):
return v
return swap_map.get(variableify(v), v)
new_trace = from_trace(trace)
with tracectx(new_trace):
swap_map = {}
safe_map_flat(add_to_swap_map, list(trace.args), list(args))
safe_map_flat(add_to_swap_map, list(trace.kwargs.values()), list(kwargs.values()))
args, kwargs = tree_map(do_swap, (args, kwargs))
safe_map_flat(write, list(trace.args), list(args))
safe_map_flat(write, list(trace.kwargs.values()), list(kwargs.values()))
for bsym in trace.bound_symbols:
if bsym.sym.id in trace_interpreter_skip_list:
new_trace.bound_symbols.append(bsym.from_bsym())
continue
args = tree_map(read, bsym.args)
kwargs = tree_map(read, bsym.kwargs)
prim_func = symbol_mapper(bsym) if symbol_mapper is not None else bsym.sym
if prim_func is None:
continue
new_trace.push_scope([])
result = prim_func(*args, **kwargs)
new_bsyms = new_trace.pop_scope()
swap_map = {}
# TODO: if inputs are returned, the old outputs should be mapped on the new ones (= the inputs) instead of the other way round
if not new_bsyms:
# empty result means we want to swap references to the old
# result to the new result (which will be one of the args)
safe_map_flat(add_to_swap_map, list(sequencify(result)), list(sequencify(bsym.output)))
else:
safe_map_flat(add_to_swap_map, list(sequencify(bsym.output)), list(sequencify(result)))
### replace bsyms
for new_bsym in new_bsyms:
# TODO: what to do with bsym header? Maybe have a combined from_bsym_swap_proxies and from_bsym?
new_trace.bound_symbols.append(
new_bsym.from_bsym_swap_proxies(swap_map).from_bsym(
source_filename=bsym.source_filename, source_positions=bsym.source_positions
)
)
result = tree_map(do_swap, result)
try:
safe_map_flat(write, list(sequencify(bsym.output)), list(sequencify(result)))
except AssertionError as e:
raise RuntimeError(
f"Error while assigning the result of dispatched function {prim_func} to the output of the original symbol {bsym}."
" This is likely due to a mismatch in the number of outputs."
f" The original symbol has {len(bsym.output)} outputs and the dispatched function has {len(sequencify(result))} outputs."
) from e
if with_env:
return new_trace, tree_map(read, trace.output), env
return new_trace, tree_map(read, trace.output)
class TraceSubstitutionProcessor:
"""This processes a trace in an interpretation-style way by looping over the bound symbols.
This processing aims to preserve as much information on the proxies as possible.
Args:
trace: trace to process
*args: arguments to process the trace with
**kwargs: keyword arguments to process the trace with
The user is expected to subclass the trace and implement process_bsym with the help of add_unprocessed_bsyms (useful eg for using subsymbols to compute a symbol), add_processed_bsyms, and add_bsyms_from_function.
Calling the instantiated object initiates the processing and returns
the new trace and a mapping of the outputs.
See the OpExProcessor in thunder.executors.passes._transform_for_operator_executor_execution for an example of subclassing.
"""
NULL = object()
def __init__(self, trace, *args, **kwargs):
self.env = {}
self.trace = trace
self.new_trace = from_trace(self.trace)
self.have_processed_args = False
def read(self, x: VariableInterface | Any) -> Any:
if isinstance(x, VariableInterface):
return self.env[x.name]
else:
return x
def write(self, v: VariableInterface | Any, val: Any, allow_duplicates=True) -> None:
if not isinstance(v, VariableInterface):
return
if v.name in self.env:
if allow_duplicates:
# Duplicates are allowed and not overwritten
return
raise ValueError(f"Variable {v.name} is being overwritten this is not allowed")
self.env[v.name] = val
def add_to_swap_map(self, old, new):
if old is new:
return
if isinstance(old, ProxyInterface):
if isinstance(new, ProxyInterface) and variableify(new) in self.env:
# the new isn't new, but something returned the input
# this means we need to map the old to the new
old, new = new, old
elif isinstance(old, TensorProxyInterface):
# should we have a fix shapes pass? the sharding
# (FSDP, tensor parallel) transforms do "break" shape metadata
self.new_trace.names.remove(old.name) # taken by the .replace proxy
if isinstance(new, VJPDual):
old = old.replace(shape=new.primal._shape)
else:
old = old.replace(shape=new._shape)
if isinstance(new, VJPDual):
self.swap_map[variableify(new.primal)] = old
new.primal = old
else:
assert isinstance(new, ProxyInterface), (old, new)
self.swap_map[variableify(new)] = old
def do_swap(self, v):
if isinstance(v, VJPDual):
v.primal = tree_map(self.do_swap, v.primal)
v.residuals = tree_map(self.do_swap, v.residuals)
return v
if not isinstance(v, ProxyInterface):
return v
return self.swap_map.get(variableify(v), v)
def add_unprocessed_bsyms(self, bsyms):
self.unprocessed_bsyms[:0] = bsyms
def add_bsyms_from_function(self, fn, /, *args, **kwargs):
self.new_trace.push_scope([])
result = fn(*args, **kwargs)
self.new_bsyms += self.new_trace.pop_scope()
self.set_result(result)
return result
def add_processed_bsyms(self, bsyms):
self.new_bsyms += bsyms
def set_result(self, result):
self.replacement_result = result
def process_bsym(self, bsym):
raise NotImplementedError("This needs to be implemented in subclasses")
def process_args(self, *args, **kwargs):
self.have_processed_args = True
with tracectx(self.new_trace):
self.swap_map = {}
safe_map_flat(self.add_to_swap_map, list(self.trace.args), list(args))
safe_map_flat(self.add_to_swap_map, list(self.trace.kwargs.values()), list(kwargs.values()))
args, kwargs = tree_map(self.do_swap, (args, kwargs))
safe_map_flat(self.write, list(self.trace.args), list(args))
safe_map_flat(self.write, list(self.trace.kwargs.values()), list(kwargs.values()))
def __call__(self):
with tracectx(self.new_trace):
self.unprocessed_bsyms = self.trace.bound_symbols[:]
while self.unprocessed_bsyms:
bsym = self.unprocessed_bsyms.pop(0)
if self.have_processed_args and bsym.sym.id in trace_interpreter_skip_list:
self.new_trace.bound_symbols.append(bsym.from_bsym())
continue
args = tree_map(self.read, bsym.args)
kwargs = tree_map(self.read, bsym.kwargs)
# this should be prettier
self.replacement_result = self.NULL
self.new_bsyms = []
self.process_bsym(bsym)
if self.new_bsyms:
assert self.replacement_result is not self.NULL, "Need to call set_result if producing new bsyms"
if self.replacement_result is not self.NULL:
self.swap_map = {}
# TODO: if inputs are returned, the old outputs should be mapped on the new ones (= the inputs) instead of the other way round
if not self.new_bsyms:
# empty result means we want to swap references to the old
# result to the new result (which will be one of the args)
safe_map_flat(
self.add_to_swap_map,
list(sequencify(self.replacement_result)),
list(sequencify(bsym.output)),
)
else:
safe_map_flat(
self.add_to_swap_map,
list(sequencify(bsym.output)),
list(sequencify(self.replacement_result)),
)
### replace bsyms
for new_bsym in self.new_bsyms:
# TODO: what to do with bsym header? Maybe have a combined from_bsym_swap_proxies and from_bsym?
self.new_trace.bound_symbols.append(
new_bsym.from_bsym_swap_proxies(self.swap_map).from_bsym(
source_filename=bsym.source_filename, source_positions=bsym.source_positions
)
)
result = tree_map(self.do_swap, self.replacement_result)
# we need to allow duplicates here because the re-interpretation is not necessairly DCEed when subsymbols symbols are flattened into the trace after re-execution.
try:
safe_map_flat(
partial(self.write, allow_duplicates=True),
list(sequencify(bsym.output)),
list(sequencify(result)),
)
except AssertionError as e:
raise RuntimeError(
f"Error while assigning the result of dispatched function {prim_func} to the output of the original symbol {bsym}."
" This is likely due to a mismatch in the number of outputs."
f" The original symbol has {len(bsym.output)} outputs and the dispatched function has {len(sequencify(result))} outputs."
) from e
return self.new_trace, tree_map(self.read, self.trace.output)