Skip to content
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

[FX] Fix python code having spurious newlines from placeholders #49720

Closed
wants to merge 1 commit into from

Conversation

jamesr66a
Copy link
Collaborator

@jamesr66a jamesr66a commented Dec 22, 2020

Stack from ghstack:

The logic in Graph.python_code didn't account for placeholder nodes with name == target not emitting a line, so it was spuriously emitting one newline per placeholder in the Graph. This patch fixes that.

Test script:

import torch
import torch.fx

graph = torch.fx.Graph()
for i in range(30):
    ph_value = graph.placeholder(f'v{i}')
graph.output(ph_value)

print(graph.python_code(''))

Before

def forward(self, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29):
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    return v29

After

def forward(self, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29):
    return v29

Differential Revision: D25675825

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented Dec 22, 2020

💊 CI failures summary and remediations

As of commit cca698a (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

This comment has been revised 3 times.

jamesr66a pushed a commit that referenced this pull request Dec 22, 2020
ghstack-source-id: e398fc8d16129d85d928f4fd2c7259d34c3818a1
Pull Request resolved: #49720
Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

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

Can this be refactored? The newline handling can just be:

emit_node(node)
delete_unused_values(node)
body.append('\n')

@jamesr66a
Copy link
Collaborator Author

@zdevito that would still incorrectly append the newline after a placeholder with name == target, right?

@facebook-github-bot
Copy link
Contributor

@jamesr66a merged this pull request in 11598da.

@facebook-github-bot
Copy link
Contributor

@jamesr66a merged this pull request in 11598da.

@zdevito
Copy link
Contributor

zdevito commented Dec 22, 2020

The newline handling here is still pretty convoluted now -- there shouldn't need to be comment saying that some unrelated function is handling a newline for another function. Is it possible to refactor this so that newline handling for a statement occurs in one place?

@facebook-github-bot facebook-github-bot deleted the gh/jamesr66a/338/head branch December 25, 2020 15:16
hwangdeyu pushed a commit to hwangdeyu/pytorch that referenced this pull request Jan 6, 2021
…rch#49720)

Summary: Pull Request resolved: pytorch#49720

Test Plan: Imported from OSS

Reviewed By: zdevito

Differential Revision: D25675825

Pulled By: jamesr66a

fbshipit-source-id: a9028acad9c8feb877fff5cd09aedabed52a3f4b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants