-
Notifications
You must be signed in to change notification settings - Fork 24
Conversation
zrr1999
commented
Sep 3, 2023
•
edited by SigureMo
Loading
edited by SigureMo
- 前移 _current_line 初始化为 -1 的语句,使得当 get_instructions 函数中发生错误时依然可以正确返回报错栈。
- 实现 gen_jump、gen_pop_jump,简化后期适配操作。
- fix _break_graph_in_for_loop in python3.11, 将以前固定的jump(大多数为绝对跳转),改为根据方向调整jump指令(python3.11新指令)。
- 修复 relocate_jump_target,使其可以正确计算JUMP_BACKWARD的参数。
- 启用如下单测
- test_12_for_loop ❌ -> ✅
- test_15_slice ❌ -> ✅
- test_21_global ❌ -> ✅
- test_enumerate ❌ -> ✅
- test_inplace_api ❌ -> ✅
- test_range ❌ -> ✅
- test_resnet50_backward ❌ -> ✅
- test_side_effects 🚧 -> ✅
Thanks for your contribution! |
34aa1b0
to
36922fe
Compare
if sys.version_info >= (3, 11): | ||
jump_if_break = self._graph.pycode_gen._add_instr( | ||
"POP_JUMP_FORWARD_IF_FALSE" | ||
) | ||
else: | ||
jump_if_break = self._graph.pycode_gen._add_instr( | ||
"POP_JUMP_IF_FALSE" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是否可以直接用 gen_pop_jump
呢?
elif instr.opname == "JUMP_BACKWARD": | ||
new_arg = instr.offset - jump_target + 2 | ||
if new_arg < 0: | ||
# NOTE(zrr1999): in Python 3.11, JUMP_ABSOLUTE is removed, so we need to use JUMP_FORWARD instead, | ||
# but in for loop breakgraph, we reuse JUMP_BACKWARD to jump forward, so we need to change it to JUMP_FORWARD. | ||
instr.opname = "JUMP_FORWARD" | ||
instr.opcode = dis.opmap["JUMP_FORWARD"] | ||
new_arg = jump_target - instr.offset - 2 | ||
else: | ||
# other REL_JUMP, such as JUMP_FORWARD, FOR_ITER | ||
new_arg = jump_target - instr.offset - 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是否可以写个函数来自动修正 JUMP 呢(Callable[[Instruction], Instruction]
)?包括 BACKWARD -> FORWARD 和 FORWARD -> BACKWARD
这段逻辑就可以封装起来了
|
||
# 7. add JUMP_ABSOLUTE to FOR_ITER | ||
self._graph.pycode_gen._add_instr("JUMP_ABSOLUTE", jump_to=for_iter) | ||
self._graph.pycode_gen.gen_jump(cur_instr, for_iter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cur_instr 只是栈分析时候使用的临时变量,这样传可能不太对
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里一定是往回跳(跳回FOR_ITER)
break_jump = pycode_gen._add_instr( | ||
"JUMP_ABSOLUTE", jump_to=out_loop_instr | ||
) | ||
cur_instr = self._instructions[self._lasti] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不就是for_iter这个输入吗
"JUMP_ABSOLUTE", jump_to=out_loop_instr | ||
) | ||
cur_instr = self._instructions[self._lasti] | ||
pycode_gen.gen_jump(cur_instr, out_loop_instr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里jump的起点也不是self._instructions[self._lasti]吧,应该是我们生成的最新代码的起点
如果是这样,那么起点和终点应该都到 pycode gen 里面拿啊,怎么去 self._instruction 里面拿,那个是原始的code啊
nop_for_continue = pycode_gen._add_instr("NOP") | ||
jump = pycode_gen.gen_jump(cur_instr, for_iter_instr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
@@ -871,6 +871,38 @@ def gen_swap(self, n): | |||
else: | |||
raise NotImplementedError("swap is not supported before python3.11") | |||
|
|||
def gen_jump(self, instr: Instruction, jump_to: Instruction) -> Instruction: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要传instr吧?没有用而且是错的,因为跳转起点不应该是待会要插入的jump语句吗?那么应该用下一个待插入的位置的offset来比吧?
|
||
def gen_pop_jump( | ||
self, instr: Instruction, jump_to: Instruction, *, suffix: str = "" | ||
): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
instr.opname = "JUMP_BACKWARD" | ||
instr.opcode = dis.opmap["JUMP_BACKWARD"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这貌似不太对?这里是不是应该将 FORWARD 改成 BACKWARD,BACKWARD 改成 FORWARD?
另外 else 分支是不是不用动?
@@ -871,6 +871,35 @@ def gen_swap(self, n): | |||
else: | |||
raise NotImplementedError("swap is not supported before python3.11") | |||
|
|||
def gen_forward_jump(self, jump_to: Instruction) -> Instruction: | |||
return self._add_instr("JUMP_BACKWARD", jump_to=jump_to) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emmm?FORWARD?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTMeow 🐾