Skip to content

Commit 2573d57

Browse files
committed
breakgraph when using random.*
1 parent e7007d1 commit 2573d57

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

python/paddle/jit/sot/opcode_translator/executor/variables/callable.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
UnsupportedNumPyAPIBreak,
7171
UnsupportedOperationBreak,
7272
UnsupportedPaddleAPIBreak,
73+
UnsupportedRandomAPIBreak,
7374
)
7475
from ..dispatcher import Dispatcher
7576
from ..guard import (
@@ -250,6 +251,10 @@ def handle_psdb_function(self, /, *args, **kwargs):
250251
return None
251252

252253
def call_function(self, /, *args, **kwargs) -> VariableBase:
254+
if UserDefinedFunctionVariable.__is_random_function(self.value):
255+
raise BreakGraphError(
256+
UnsupportedRandomAPIBreak(fn_name=self.value.__name__)
257+
)
253258
from ..opcode_inline_executor import OpcodeInlineExecutor
254259

255260
result = self.handle_psdb_function(*args, **kwargs)
@@ -318,6 +323,15 @@ def main_info(self) -> dict[str, Any]:
318323
"name": self.value.__name__,
319324
}
320325

326+
@staticmethod
327+
def __is_random_function(value) -> bool:
328+
import random
329+
330+
return value.__qualname__ in [
331+
f"{random._inst.__class__.__name__}.{name}"
332+
for name in dir(random._inst)
333+
]
334+
321335

322336
class UserCodeVariable(FunctionVariable):
323337
"""

python/paddle/jit/sot/utils/exceptions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ def __init__(
125125
)
126126

127127

128+
class UnsupportedRandomAPIBreak(UnsupportedOperationBreak):
129+
def __init__(
130+
self,
131+
*,
132+
fn_name=None,
133+
reason_str=None,
134+
file_path="",
135+
line_number=-1,
136+
):
137+
if reason_str is None:
138+
reason_str = f"Random function {fn_name} is not supported."
139+
140+
super().__init__(
141+
reason_str=reason_str,
142+
file_path=file_path,
143+
line_number=line_number,
144+
)
145+
146+
128147
class BuiltinFunctionBreak(UnsupportedOperationBreak):
129148
"""Break reason for unsupported built-in function calls.
130149

test/sot/test_force_breakgraph_api.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
import random
18+
import unittest
19+
20+
from test_case_base import TestCaseBase
21+
22+
import paddle
23+
from paddle.jit.sot import symbolic_translate
24+
25+
26+
def fn_randint(x):
27+
x = x + 1
28+
x = random.randint(0, 100)
29+
x = x + 2
30+
return x
31+
32+
33+
def fn_random(x):
34+
x = x + 3
35+
x = random.random()
36+
x = x + 4
37+
return x
38+
39+
40+
class TestRandom(TestCaseBase):
41+
def test_random_randint(self):
42+
x = paddle.to_tensor(2024)
43+
44+
random.seed(2025)
45+
sym_output = symbolic_translate(fn_randint)(x)
46+
random.seed(2025)
47+
paddle_output = fn_randint(x)
48+
49+
self.assertEqual(sym_output, paddle_output)
50+
51+
def test_random_random(self):
52+
x = paddle.to_tensor(2025)
53+
54+
random.seed(2025)
55+
sym_output = symbolic_translate(fn_random)(x)
56+
random.seed(2025)
57+
paddle_output = fn_random(x)
58+
59+
self.assertEqual(sym_output, paddle_output)
60+
61+
62+
if __name__ == "__main__":
63+
unittest.main()

0 commit comments

Comments
 (0)