forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtracker.py
More file actions
472 lines (363 loc) · 14.9 KB
/
tracker.py
File metadata and controls
472 lines (363 loc) · 14.9 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import builtins
import dis
import sys
from itertools import chain
from typing import TYPE_CHECKING
from ...utils import InnerError, NameGenerator
from .guard import StringifyExpression, stringify_pyobject, union_free_vars
if TYPE_CHECKING:
from typing import Sequence
from .pycode_generator import PyCodeGen
from .variables import VariableBase
class Tracker:
"""
Tracker is a base class responsible for tracking variables or objects in Python code.
It is used to identify how a variable is derived from the initial state of the frame.
Args:
inputs: The list of variables to be tracked.
Note:
It serves as an abstract class and should not be instantiated directly.
"""
inputs: Sequence[VariableBase]
name_generator = NameGenerator("tracker_")
def __init__(self, inputs: Sequence[VariableBase], changed: bool = False):
self.inputs = inputs
self.changed = changed
self.id = Tracker.name_generator.next()
def gen_instructions(self, codegen: PyCodeGen) -> None:
"""
Generate instructions based on the tracked variables.
Args:
codegen (PyCodeGen): An instance of PyCodeGen to generate instructions.
"""
raise NotImplementedError()
# TODO(xiongkun): trace_value_from_frame is not a good name, it should be more related to guard but not tracable.
def trace_value_from_frame(self) -> StringifyExpression:
"""
Trace the value of the tracked variables from the frame. It used for generating the guard.
Returns:
The value of the tracked variables.
"""
raise NotImplementedError()
def is_traceable(self) -> bool:
"""
Determine if all the tracked variables can be traced from the frame.
Returns:
bool, True if all tracked variables are traceable, False otherwise.
"""
if self.changed:
return False
for input in self.inputs:
if not input.tracker.is_traceable():
return False
return True
def need_guard(self) -> bool:
return self.is_traceable()
class DummyTracker(Tracker):
"""
DummyTracker is a subclass of Tracker that specifically tracks variables cannot be reproduced from the frame.
It is mostly generated by complex operations (instructions).
Args:
inputs (list[VariableBase]): The input variables associated with the generated variables.
"""
def __init__(self, inputs: Sequence[VariableBase]):
super().__init__(inputs)
def gen_instructions(self, codegen: PyCodeGen):
raise InnerError("DummyTracker has no instructions")
def trace_value_from_frame(self):
raise InnerError("DummyTracker can't trace value from frame")
def is_traceable(self):
return False
def __repr__(self) -> str:
return f"DummyTracker(num_inputs={len(self.inputs)})"
def need_guard(self) -> bool:
return False
class DanglingTracker(Tracker):
"""
DanglingTracker is a subclass of Tracker that specifically tracks variables that are not in the frame.
Variables whose tracker is DanglingTracker should not be placed on the stack, except for NullVariable.
DanglingTracker is often used in conjunction with BuiltinVariable to reuse the dispatch mechanism.
Examples:
>>> import operator
>>> from sot.opcode_translator.executor.variables import BuiltinVariable, ConstantVariable
>>> a = ConstantVariable.wrap_literal(1, None)
>>> b = ConstantVariable.wrap_literal(2, None)
>>> c = BuiltinVariable(operator.add, None, DanglingTracker())(a, b)
>>> c.value
3
"""
def __init__(self):
super().__init__([])
def gen_instructions(self, codegen: PyCodeGen):
raise InnerError("DanglingTracker has no instructions")
def trace_value_from_frame(self):
raise InnerError("DanglingTracker can't trace value from frame")
def is_traceable(self):
return False
def __repr__(self) -> str:
return "DanglingTracker()"
class LocalTracker(Tracker):
"""
LocalTracker is a subclass of Tracker that specifically tracks variables from f_locals of frame.
Args:
name (str): The name of the variable in f_locals to be tracked.
"""
def __init__(self, name: str):
super().__init__([])
self.name = name
def gen_instructions(self, codegen: PyCodeGen) -> None:
codegen.gen_load_fast(self.name)
def trace_value_from_frame(self) -> StringifyExpression:
return StringifyExpression(f"frame.f_locals['{self.name}']", [], {})
def __repr__(self) -> str:
return f"LocalTracker(name={self.name})"
class CellTracker(LocalTracker):
def gen_instructions(self, codegen: PyCodeGen):
codegen.gen_load_deref(self.name)
def trace_value_from_frame(self):
return StringifyExpression(f"frame.f_locals['{self.name}']", [], {})
def __repr__(self) -> str:
return f"CellTracker(name={self.name})"
class GlobalTracker(Tracker):
"""
GlobalTracker is a subclass of Tracker that specifically tracks variables from f_globals of frame.
Args:
name (str): The name of the variable in f_globals to be tracked.
"""
def __init__(self, name: str):
super().__init__([])
self.name = name
def gen_instructions(self, codegen: PyCodeGen) -> None:
codegen.gen_load_global(self.name, push_null=False)
def trace_value_from_frame(self) -> StringifyExpression:
return StringifyExpression(f"frame.f_globals['{self.name}']", [], {})
def __repr__(self) -> str:
return f"GlobalTracker(name={self.name})"
class BuiltinTracker(Tracker):
"""
BuiltinTracker is a subclass of Tracker that specifically tracks variables from f_builtins of frame.
Args:
name (str): The name of the variable in f_builtins to be tracked.
"""
def __init__(self, name: str):
super().__init__([])
self.name = name
def gen_instructions(self, codegen: PyCodeGen) -> None:
codegen.gen_load_global(self.name, push_null=False)
def trace_value_from_frame(self) -> StringifyExpression:
return StringifyExpression(
f"builtins.__dict__['{self.name}']", [], {"builtins": builtins}
)
def __repr__(self) -> str:
return f"BuiltinTracker(name={self.name})"
class ConstTracker(Tracker):
"""
ConstTracker is a subclass of Tracker that specifically tracks a constant value.
Args:
value (Any): The value of the constant.
"""
def __init__(self, value):
super().__init__([])
self.value = value
def gen_instructions(self, codegen: PyCodeGen):
codegen.gen_load_const(self.value)
def trace_value_from_frame(self):
value_str, value_free_vars = stringify_pyobject(self.value)
return StringifyExpression(
f"{value_str}", [], union_free_vars(value_free_vars)
)
def __repr__(self) -> str:
return f"ConstTracker(value={self.value})"
def need_guard(self) -> bool:
return False
class BinaryOperatorTracker(Tracker):
def __init__(
self, operator: str, operands: list[VariableBase], addition=None
):
"""
addition is for the case that the operator is "COMPARE_OP", which represents the dis.cmp_op's index.
"""
super().__init__(operands, False)
assert len(operands) == 2, "Currently only support binary operator."
self.operands = operands
self.operator = operator
self.addition = addition
def gen_instructions(self, codegen: PyCodeGen):
for operand in self.operands:
operand.tracker.gen_instructions(codegen)
self.gen_operator_instr(codegen)
def gen_operator_instr(self, codegen: PyCodeGen):
if self.operator == "COMPARE_OP":
codegen.gen_compare(self.addition)
else:
codegen.gen_operator(self.operator)
def get_operator_symbol(self):
if self.operator == "COMPARE_OP":
return dis.cmp_op[self.addition]
return {
"BINARY_ADD": "+",
"BINARY_SUBTRACT": "-",
"BINARY_MUL": "*",
"BINARY_POWER": "**",
}[self.operator]
def trace_value_from_frame(self):
sub_exprs = [x.tracker.trace_value_from_frame() for x in self.operands]
sub_frees = [x.free_vars for x in sub_exprs]
expr = f"({{}} {self.get_operator_symbol()} {{}})"
return StringifyExpression(
expr,
list(sub_exprs),
union_free_vars(*list(sub_frees)),
)
def __repr__(self) -> str:
return f"BinaryOperatorTracker(operator={self.operator})"
class GetAttrTracker(Tracker):
"""
GetAttrTracker is a subclass of Tracker that specifically tracks the attribute access of an variable.
Args:
obj (VariableBase): The object whose attribute is to be tracked.
attr (str): The attribute to be tracked.
"""
def __init__(self, obj: VariableBase, attr: str, changed: bool = False):
super().__init__([obj], changed)
self.obj = obj
self.attr = attr
def gen_instructions(self, codegen: PyCodeGen):
self.obj.tracker.gen_instructions(codegen)
codegen.gen_load_attr(self.attr)
def trace_value_from_frame(self):
obj_tracer = self.obj.tracker.trace_value_from_frame()
if self.attr.isidentifier():
expr = f"{{}}.{self.attr}"
else:
expr = f"getattr({{}}, '{self.attr}')"
return StringifyExpression(
expr,
[obj_tracer],
union_free_vars(obj_tracer.free_vars),
)
def __repr__(self) -> str:
return f"GetAttrTracker(attr={self.attr})"
def need_guard(self) -> bool:
return self.is_traceable() and self.obj.tracker.need_guard()
class GetItemTracker(Tracker):
"""
GetItemTracker is a subclass of Tracker that specifically tracks item access of a container variable.
It generates instructions and traces the item value from the frame.
Args:
container_var (VariableBase): The container object whose item is to be tracked.
key: The key/index of the item to be tracked.
"""
def __init__(self, container_var: VariableBase, key: object, changed=False):
super().__init__([container_var], changed)
self.container = container_var
self.key = key
def gen_instructions(self, codegen: PyCodeGen):
self.container.tracker.gen_instructions(codegen)
if isinstance(self.key, slice):
codegen.gen_load_const(self.key.start)
codegen.gen_load_const(self.key.stop)
codegen.gen_load_const(self.key.step)
codegen.gen_build_slice(3)
else:
codegen.gen_load_const(self.key)
codegen.gen_subscribe()
def trace_value_from_frame(self):
container_tracer = self.container.tracker.trace_value_from_frame()
key_string, key_free_vars = stringify_pyobject(self.key)
return StringifyExpression(
f"{{}}[{key_string}]",
[container_tracer],
union_free_vars(container_tracer.free_vars, key_free_vars),
)
def __repr__(self) -> str:
return f"GetItemTracker(key={self.key!r})"
def need_guard(self) -> bool:
return self.is_traceable() and self.container.tracker.need_guard()
class GetIterTracker(Tracker):
"""
GetIterTracker is a subclass of Tracker that specifically tracks iteration of a variable.
It generates instructions and traces the iterator from the frame.
Args:
iter_source (VariableBase): The source variable to be iterated.
"""
def __init__(self, iter_source: VariableBase):
super().__init__([iter_source])
self.iter_source = iter_source
def gen_instructions(self, codegen: PyCodeGen):
self.iter_source.tracker.gen_instructions(codegen)
codegen.add_instr("GET_ITER")
def trace_value_from_frame(self):
iter_source_tracer = self.iter_source.tracker.trace_value_from_frame()
return StringifyExpression(
"iter({})",
[iter_source_tracer],
union_free_vars(iter_source_tracer.free_vars),
)
def __repr__(self) -> str:
return "GetIterTracker()"
class CreateLayerTracker(Tracker):
def __init__(self, layer_class, args, kwargs):
super().__init__([layer_class] + list(args) + list(kwargs.values()))
self.layer_class = layer_class
self.args = args
self.kwargs = kwargs
def gen_instructions(self, codegen: PyCodeGen):
if sys.version_info >= (3, 11):
codegen.gen_push_null()
self.layer_class.reconstruct(codegen)
for variable in self.args:
variable.reconstruct(codegen)
if len(self.kwargs) == 0:
codegen.gen_call_function(argc=len(self.args))
else:
codegen.gen_build_tuple(len(self.args))
for k, v in self.kwargs.items():
codegen.gen_load_const(k)
v.reconstruct(codegen)
codegen.gen_build_map(len(self.kwargs))
codegen.gen_call_function_ex(has_kwargs=True)
def trace_value_from_frame(self):
class_tracer = self.layer_class.tracker.trace_value_from_frame()
arg_tracers = [
arg.tracker.trace_value_from_frame() for arg in self.args
]
kwarg_tracers_dict = {
k: v.tracker.trace_value_from_frame()
for k, v in self.kwargs.items()
}
kwarg_tracers = list(kwarg_tracers_dict.values())
expr = "{}("
expr += ", ".join(["{}"] * len(arg_tracers))
if len(arg_tracers) and len(kwarg_tracers) > 0:
expr += ", "
expr += ", ".join(f"{k}={{}}" for k in kwarg_tracers_dict.keys())
expr += ")"
return StringifyExpression(
expr,
[class_tracer] + arg_tracers + kwarg_tracers,
union_free_vars(
*(
tracer.free_vars
for tracer in chain(
[class_tracer], arg_tracers, kwarg_tracers
)
)
),
)
def __repr__(self) -> str:
return f"CreateLayerTracker(Layer={self.layer_class}, args={self.args}, kwargs={self.kwargs})"