Skip to content

Conversation

@DrRyanHuang
Copy link
Contributor

@DrRyanHuang DrRyanHuang commented May 30, 2025

PR Category

Execute Infrastructure

PR Types

Improvements

Description

之前在import random之后,由于存在 try ... except ...,所以导入 random 会 fallback
现在SOT异常支持后,在模拟 random 中的函数导入,由于random的部分函数,都是实例 _inst = Random() 的方法

# Py3.10 Random 库源码中的一部分 _inst = Random() seed = _inst.seed random = _inst.random uniform = _inst.uniform triangular = _inst.triangular randint = _inst.randint choice = _inst.choice randrange = _inst.randrange sample = _inst.sample shuffle = _inst.shuffle choices = _inst.choices 

所以此时使用 random 中的函数会报:

 File "/workspace/paddle/build/python/paddle/jit/sot/opcode_translator/executor/variables/base.py", line 518, in getattr raise HasNoAttributeError( paddle.jit.sot.utils.exceptions.HasNoAttributeError: ModuleVariable ModuleVariable(<module 'random' from '/usr/lib/python3.10/random.py'>, object_176, random) has no attribute _randbelow 

在执行:

>>> import random >>> random._inst.seed <bound method Random.seed of <random.Random object at 0x12f00e810>>

inst.seed 理应是 Random.seed.__get__(Random())
但现在是 Random.seed.__get__(random)
注意 randomModuleTypeRandom()Random

所以目前现将 random.* 函数全部打断

cc @SigureMo
PCard-66972

@paddle-bot
Copy link

paddle-bot bot commented May 30, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label May 30, 2025
def __is_random_function(value) -> bool:
import random

return value.__qualname__ in [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确认下,所有 value 都有 __qualname__ 么?

>>> object().__qualname__ Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'object' object has no attribute '__qualname__'
Copy link
Contributor Author

@DrRyanHuang DrRyanHuang May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在这里搜了下看看:https://docs.python.org/3/reference/datamodel.html
所有的 function 和 type 都有 __qualname__

另外PEP3155,也说说明了在函数中添加 __qualname__

This PEP proposes the addition of a qualname attribute to functions and classes. For top-level functions and classes, the qualname attribute is equal to the name attribute. For nested classes, methods, and nested functions, the qualname attribute contains a dotted path leading to the object from the module top-level. A function’s local namespace is represented in that dotted path by a component named .

咱们的 from_value 部分有限制value是函数:

 def from_value(value: Any, graph: FunctionGraph, tracker: Tracker): if isinstance(value, (types.FunctionType)): return UserDefinedFunctionVariable(value, graph, tracker) if isinstance( value, paddle.jit.dy2static.program_translator.StaticFunction ):
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾

@SigureMo
Copy link
Member

SigureMo commented Jun 3, 2025

记得加一下 PR 描述

return None

def call_function(self, /, *args, **kwargs) -> VariableBase:
if UserDefinedFunctionVariable.__is_random_function(self.value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

后续考虑统一到 #73059 的 force break 形式

@SigureMo SigureMo merged commit 94558ca into PaddlePaddle:develop Jun 3, 2025
68 of 71 checks passed
@DrRyanHuang DrRyanHuang deleted the random branch June 3, 2025 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

2 participants