Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

调整 paddle.jit.not_to_static 装饰范围 #120

Open
SigureMo opened this issue Jun 6, 2023 · 1 comment
Open

调整 paddle.jit.not_to_static 装饰范围 #120

SigureMo opened this issue Jun 6, 2023 · 1 comment
Assignees

Comments

@SigureMo
Copy link
Member

SigureMo commented Jun 6, 2023

我们目前会将整个 SIR 函数用 paddle.jit.to_static 包装

def value_fn(self, context, sir_name):
return FallbackWrapper(
paddle.jit.to_static(
compile_sir(context, sir_name), enable_fallback=False
)
)

将其转换为动转静的 StaticFunction,转换的函数中主体逻辑就是利用 Interpreter 解释 SIR,进行组网,由于这部分可能有些动转静会出问题的语法,因此使用了 paddle.jit.not_to_static 进行装饰

def compile_sir(context, name):
@paddle.jit.not_to_static
def wrapper(args):
"""
This function will be decorated by paddle.to_static.
so the args is variables, not eager tensors.
"""
interpreter = Interpreter(context)
SIR = interpreter.get_sir(name)
state = prepare_state(SIR, args)
return interpreter.run_sir(name, state)
return wrapper

然而实际情况我们有些 API 仍然需要 to_static 的代码转换,比如 paddle.to_tensor,因此我们希望不利用 paddle.jit.not_to_static 将整个函数装饰,而是只装饰部分,避免部分代码动转静转写出问题,并将组网代码进行转换

@Aurelius84
Copy link
Collaborator

一些思路

paddle.jit.not_to_static 的实现原理是:会给被装饰的函数动态添加一个CONVERSION_OPTIONS 的属性,然后在动转静触发代码AST转写的convert_call 函数里的convert_to_static 模块里优先判断一个 function/method 对象是否有CONVERSION_OPTIONS 属性,如果有,则直接返回,不做AST Transformer。

此Task的目的是通过额外的一些标记手段(比如扩展CONVERSION_OPTIONS),以保证在convert_call函数里,仅不对当前被装饰的函数做AST Transformer,但内层调用的其他函数依旧进行AST Transformer,即@not_to_static 仅生效在当前函数,忽略内层函数调用。

@zrr1999 zrr1999 self-assigned this Jun 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants