CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[SOT] Support builtin function super
#71865
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
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py
Outdated
Show resolved
Hide resolved
Dispatcher.register( | ||
super, | ||
(), | ||
lambda _cls, _obj: SuperVariable( | ||
cls=_cls, | ||
obj=_obj.value, | ||
graph=Dispatcher.graph, | ||
tracker=DummyTracker([]), | ||
), | ||
) |
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.
删掉,不存在这种情况
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.
是因为,由于 handle_super_init_without_args
这个函数填充了super的参数,所以不存在 super().get(...)
的情况吗?
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.
嗯,另外看看外部传入的 super()
是否能够支持,比如
def fn(sup):
sup.fn()
static_fn = to_static(fn)
static_fn(super()) # 当然这里要写在 class 里
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.
这样吗? 还是说有啥特殊场景
import paddle
from paddle.jit import to_static
from paddle.jit.sot.psdb import check_no_breakgraph
class A:
def fn(this, x):
return x + 1
def __call__(self, x):
return self.fn(x)
class B(A):
@check_no_breakgraph
def get(self, x):
return to_static(super())(x)
b = B()
x = paddle.to_tensor(1)
print(b(x))
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.
class A:
def fn(self, x):
return x + 1
def super_as_input(spr):
return spr.fn(2)
class TestSuperAsInput(TestCaseBase, A):
def test_super_as_input(self):
self.assert_results(super_as_input, super())
类似这种,主要是为了支持打断的场景,需要为 SuperVariable
实现 from_value
python/paddle/jit/sot/opcode_translator/executor/variable_dispatch.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/variables/basic.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/variables/basic.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/variables/basic.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/variables/basic.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/variables/basic.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/variables/basic.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/variables/basic.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py
Outdated
Show resolved
Hide resolved
python/paddle/jit/sot/opcode_translator/executor/variable_dispatch.py
Outdated
Show resolved
Hide resolved
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.
# super | ||
Dispatcher.register( | ||
super, | ||
("ClassVariable", "VariableBase"), | ||
lambda cls, obj: SuperVariable( | ||
cls=cls, | ||
obj=obj, | ||
graph=Dispatcher.graph, | ||
tracker=DummyTracker([cls, obj]), | ||
), | ||
) |
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.
干嘛把 dict 给隔开了,上下都是 dict 相关的,后续 PR 移动到其他位置
PR Category
Execute Infrastructure
PR Types
Improvements
Description
Under @SigureMo ’s guidance, this PR enables the use of the super function within SOT. Once merged, encountering super will no longer trigger a breakgraph.
在这里记录一下需要注意的地方:
super()
参数为空时,在函数handle_super_init_without_args
中从vframe
中取出super()
的参数CALL_FUNCTION
这个字节码,所以要在PRECAL
与CALL
中添加super()
的参数,见commit fec5355LOAD_SUPER_ATTR
这个字节码,这个优化指令将多个操作合并为一步,使 super() 调用更高效,见commit 35bbd41super
的参数自动填入,所以并没有调用handle_super_init_without_args
SuperVariable
的getattr
中,取__mro__
部分用GetAttrTracker
跟踪;多重继承中,从__mro__[1:]
开始查找属性/方法;bind
过程中,是 function 持有 instance 而不是 instance 持有 functionself.function(*args, **kwargs)
实际上等价于self.__class__.function.__get__(self, self.__class__)(*args, **kwargs)
, 即 Python 描述符协议PCard-66972