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] fix sot call locals #59710

Merged
merged 2 commits into from
Dec 7, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def start_translate(frame: types.FrameType, **kwargs) -> GuardedFunction:
"""
simulator = OpcodeExecutor(frame, **kwargs)
try:
simulator.check_code_simulatable()
new_custom_code, guard_fn = simulator.transform()
return new_custom_code, guard_fn
# TODO(zrr1999): InnerError maybe place before (FallbackError, BreakGraphError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,15 @@ def __init__(self, code: types.CodeType, graph: FunctionGraph):
str, ...
] | None = None # store kwnames for Python 3.11+
self._prepare_virtual_env()

self.stop_state = None

def check_code_simulatable(self):
for instr in self._instructions:
if instr.opname == "LOAD_GLOBAL" and instr.argval == "locals":
raise FallbackError(
"Can not support call builtin function `locals`"
)

def print_sir(self):
"""
Prints the Static Instruction Representation (SIR) in the executor.
Expand Down
37 changes: 37 additions & 0 deletions test/sot/test_unsupport_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.

import unittest

from test_case_base import TestCaseBase

import paddle
from paddle.jit.sot.utils import strict_mode_guard


def call_locals(x: int, y: paddle.Tensor):
tmp = locals()
return x + y + len(list(tmp.keys())), list(tmp.keys())


class TestUnsupportFunction(TestCaseBase):
@strict_mode_guard(False)
def test_locals(self):
x = paddle.to_tensor([2])
y = paddle.to_tensor([3])
self.assert_results(call_locals, x, y)


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