【Hackathon 9th No.127】feat: Implement torch._C._nn.linear to torch.nn.functional.linear conversion#353
Merged
lixinqi merged 3 commits intoPaddlePaddle:developfrom Nov 11, 2025
Merged
Conversation
|
Thanks for your contribution! |
b4c0103 to
2116c66
Compare
Contributor
Author
Contributor
Author
|
修改 log2json.py 是为了确保 JSON 报告包含完整且正确的状态和性能数据 |
lixinqi
reviewed
Nov 10, 2025
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" | ||
| ): |
Collaborator
There was a problem hiding this comment.
按如下方法改造:
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 |
Collaborator
There was a problem hiding this comment.
这么做是很不正式的做法。我们单独准备了 fx_graph_serialize 机制。具体你看#352 pr 里的https://github.com/PaddlePaddle/GraphNet/pull/352/files#diff-cdf8f6acc9f3a2d0573129027aaee53e37576cdcd576205d8e0b63c740003105 这里
lixinqi
reviewed
Nov 10, 2025
| gm.forward = types.MethodType(forward_func, gm) | ||
|
|
||
| # Update _code attribute so that gm.code returns the modified code | ||
| gm._code = new_code |
Collaborator
There was a problem hiding this comment.
这种直接改gm._code的做法是很不地道的,应当避免。
lixinqi
reviewed
Nov 11, 2025
| # 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) |
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 |
Collaborator
There was a problem hiding this comment.
check_unstable_to_stable 是裁判,裁判的逻辑不可以改。上面_impl_unstable_to_stable_linear_to_functional_linear是运动员。
lixinqi
reviewed
Nov 11, 2025
| # 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 |
lixinqi
reviewed
Nov 11, 2025
Collaborator
There was a problem hiding this comment.
把_impl_unstable_to_stable_linear_to_functional_linear函数的实现挪到这里,避开与其他 pr 的冲突
d49878a to
1a77a0e
Compare
- 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
1a77a0e to
3a3b149
Compare
Contributor
Author
lixinqi
approved these changes
Nov 11, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


PR Category
Description