Skip to content

Conversation

@Dayuxiaoshui
Copy link
Contributor

@Dayuxiaoshui Dayuxiaoshui commented Nov 10, 2025

  • Add _impl_unstable_to_stable_linear_to_functional_linear method using AST transformation
  • Adapt to new architecture with impl_unstable_to_stable prefix

PR Category

Description

@paddle-bot
Copy link

paddle-bot bot commented Nov 10, 2025

Thanks for your contribution!

@paddle-bot paddle-bot bot added the contributor External developers label Nov 10, 2025
@Dayuxiaoshui
Copy link
Contributor Author

image

@Dayuxiaoshui
Copy link
Contributor Author

修改 log2json.py 是为了确保 JSON 报告包含完整且正确的状态和性能数据

Comment on lines 224 to 243
if isinstance(node.func, ast.Attribute):
# node.func.attr should be "linear"
if node.func.attr == "linear":
# node.func.value should be torch._C._nn
if isinstance(node.func.value, ast.Attribute):
# node.func.value.attr should be "_nn"
if node.func.value.attr == "_nn":
# node.func.value.value should be torch._C
if isinstance(node.func.value.value, ast.Attribute):
# node.func.value.value.attr should be "_C"
if node.func.value.value.attr == "_C":
# node.func.value.value.value should be torch
if (
isinstance(
node.func.value.value.value,
ast.Name,
)
and node.func.value.value.value.id
== "torch"
):
Copy link
Collaborator

Choose a reason for hiding this comment

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

按如下方法改造:

filtered_nodes = [
    node
    for node in [node]
    if isinstance(node.func, ast.Attribute)
    if node.func.attr == "linear"
    if isinstance(node.func.value, ast.Attribute)
    if node.func.value.attr == "_nn"
    if isinstance(node.func.value.value, ast.Attribute)
    if node.func.value.value.attr == "_C"
    if isinstance(node.func.value.value.value, ast.Name)
    if node.func.value.value.value.id == "torch"
]
if len(filtered_nodes) > 0:
    ...

gm.forward = types.MethodType(forward_func, gm)

# Update _code attribute so that gm.code returns the modified code
gm._code = new_code
Copy link
Collaborator

Choose a reason for hiding this comment

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

这么做是很不正式的做法。我们单独准备了 fx_graph_serialize 机制。具体你看#352 pr 里的https://github.com/PaddlePaddle/GraphNet/pull/352/files#diff-cdf8f6acc9f3a2d0573129027aaee53e37576cdcd576205d8e0b63c740003105 这里

gm.forward = types.MethodType(forward_func, gm)

# Update _code attribute so that gm.code returns the modified code
gm._code = new_code
Copy link
Collaborator

Choose a reason for hiding this comment

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

这种直接改gm._code的做法是很不地道的,应当避免。

# replace this line with modification code for task 123 (torch._C._nn.pad)
# replace this line with modification code for task 125 (torch._C._nn.gelu)
# replace this line with modification code for task 126 (torch._C._nn.scaled_dot_product_attention)
# replace this line with modification code for task 127 (torch._C._nn.linear)
Copy link
Collaborator

Choose a reason for hiding this comment

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

把上面的改动放到这里,减少其他 PR 的合入冲突

Comment on lines 191 to 348
# Use serialized code to check for unstable APIs
graph_text = serialize_graph_module_to_str(gm)
# Use code to check for unstable APIs
graph_text = gm.code
Copy link
Collaborator

Choose a reason for hiding this comment

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

这部分的代码不能改

Copy link
Collaborator

Choose a reason for hiding this comment

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

check_unstable_to_stable 是裁判,裁判的逻辑不可以改。上面_impl_unstable_to_stable_linear_to_functional_linear是运动员。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

收到

# Use serialize_graph_module_to_str to get the serialized code
# This ensures the code is properly serialized with unstable API replacements
serialized_code = serialize_graph_module_to_str(gm)
gm._code = serialized_code
Copy link
Collaborator

Choose a reason for hiding this comment

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

这代码能去掉吗?


# replace this line with modification code for task 126 (torch._C._nn.scaled_dot_product_attention)

# replace this line with modification code for task 127 (torch._C._nn.linear)
Copy link
Collaborator

Choose a reason for hiding this comment

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

把_impl_unstable_to_stable_linear_to_functional_linear函数的实现挪到这里,避开与其他 pr 的冲突

- Implement direct node.target modification for API conversion
- Use serialize_graph_module_to_str for API check in check_unstable_api
- Add AST-based replacement function (commented) in fx_graph_serialize_util.py
- Fix log2json.py to properly initialize result field and map speedup data
- Simplify conversion logic by removing complex AST code
- Tested with 50 samples: 100% success rate, ES(-6) = 1.013
@Dayuxiaoshui
Copy link
Contributor Author

image

@lixinqi lixinqi merged commit 8fb4ad6 into PaddlePaddle:develop Nov 11, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants