Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SOT][3.11] Do not restore KW_NAMES + PRECALL in 3.11 #59017

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions python/paddle/jit/sot/opcode_translator/executor/function_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,17 @@ def load(self, var, allow_push_null=True):
# only restored vars in stack, so used gen_load to process global var
self._pycode_gen.gen_load(self._store_var_info[var])

origin_instr = get_instructions(self.pycode_gen._origin_code)
origin_instrs = get_instructions(self.pycode_gen._origin_code)

for instr in origin_instr[0:instr_idx]:
restore_instrs = origin_instrs[:instr_idx]
restore_instr_names = [
instr.opname for instr in restore_instrs[:instr_idx]
]
# NOTE(SigureMo): Trailing KW_NAMES + PRECALL is no need to restore in Python 3.11+
if restore_instr_names[-2:] == ["KW_NAMES", "PRECALL"]:
restore_instrs = restore_instrs[:-2]

for instr in restore_instrs:
if (
instr.opname == 'LOAD_FAST'
and instr.argval in self.pycode_gen._frame.f_locals.keys()
Expand All @@ -288,13 +296,13 @@ def load(self, var, allow_push_null=True):

nop = self.pycode_gen._add_instr("NOP")

for instr in origin_instr:
if instr.jump_to == origin_instr[instr_idx]:
for instr in origin_instrs:
if instr.jump_to == origin_instrs[instr_idx]:
instr.jump_to = nop

self.pycode_gen.hooks.append(
lambda: self.pycode_gen.extend_instrs(
iter(origin_instr[instr_idx + 1 :])
iter(origin_instrs[instr_idx + 1 :])
)
)

Expand Down
15 changes: 15 additions & 0 deletions test/sot/test_min_graph_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def case_call(x):
return x


def call_with_kwargs_inner(x):
return paddle.to_tensor(x.numpy())


def call_with_kwargs(x):
y = call_with_kwargs_inner(x=x)
x += y
return x


def case_all(x, vars):
x = x + 1
for y in vars:
Expand Down Expand Up @@ -82,6 +92,11 @@ def test_layer(self):
layer = CustomLayer()
self.assert_results(layer.forward, x)

@min_graph_size_guard(10)
def test_call_with_kwargs(self):
x = paddle.to_tensor(1)
self.assert_results(call_with_kwargs, x)


if __name__ == "__main__":
unittest.main()