Skip to content

[JIT] Normalize op aliases#38735

Closed
eellison wants to merge 4 commits intogh/eellison/80/basefrom
gh/eellison/80/head
Closed

[JIT] Normalize op aliases#38735
eellison wants to merge 4 commits intogh/eellison/80/basefrom
gh/eellison/80/head

Conversation

@eellison
Copy link
Copy Markdown
Contributor

@eellison eellison commented May 19, 2020

Stack from ghstack:

Follow up to my comment #36597

This adds a pass to convert op aliases into a normalized form. Having two ops generated in our IR that do the same thing makes the IR harder for downstream consumers of the IR, such as TorchScript passes but also ONNX, glow, etc.

Another solution would have been to fix our code generation to only emit aten::abs from the start. This seems trickier, and doesn't really buy us much if we still have to expose aten::absolute in C++, as @glaringlee of the C++ API thinks we should.

Bike shedding: maybe this should be CanonicalizeOps instead

Differential Revision: D21673108

[ghstack-poisoned]
@eellison eellison requested a review from apaszke as a code owner May 19, 2020 19:44
eellison pushed a commit that referenced this pull request May 19, 2020
ghstack-source-id: c8df76f
Pull Request resolved: #38735
@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label May 19, 2020
@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented May 19, 2020

💊 CI failures summary and remediations

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


  • 1/1 failures possibly* introduced in this PR
    • 1/1 non-CircleCI failure(s)

ci.pytorch.org: 1 failed


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 on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 15 times.

@glaringlee
Copy link
Copy Markdown
Contributor

glaringlee commented May 19, 2020

I previously thought this numpy-compatible project is for logical compatible, so instead of adding 'absolute', we can reuse 'abs' and fix the non-numpy-compatible stuff inside if any. Now I see that we also need naming compatible, so adding normalization layer and support alias is reasonable to me now. 👍 Thanks.

Comment thread test/test_jit.py

graph = torch.jit.script(test_multiple).graph
FileCheck().check_count("prim::If", 3, exactly=True).run(graph)
print(torch.jit.script(test_multiple).code)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for cleaning this up!

Comment thread test/test_jit.py
"""
FileCheck().run(graph_str, parse_ir(graph_str))

def test_norm_aliases(self):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This test is cool but either in this PR or a follow-up we should make it extensible just like the method of registering aliases in the JIT is. That way when people add a new alias they can also easily extend this test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done here: #38746

@mruberry
Copy link
Copy Markdown
Collaborator

This is super cool, thanks @eellison! I left a few comments requesting clarification on how to add and test new aliases.

Follow up to my comment #36597

This adds a pass to convert op aliases into a normalized form. Having two ops generated in our IR that do the same thing makes the IR harder for downstream consumers of the IR, such as TorchScript passes but also ONNX, glow, etc. 

Another solution would have been to fix our code generation to only emit `aten::abs` from the start. This seems trickier, and doesn't really buy us much if we still have to expose `aten::absolute` in C++, as @glaringlee of the C++ API thinks we should.

Bike shedding: maybe this should be `CanonicalizeOps` instead

[ghstack-poisoned]
namespace torch {
namespace jit {

static const std::unordered_map<Symbol, Symbol> alias_map = {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry, my comment here didn't get saved, I guess. What about a comment here explaining how people should add (and test) new aliases?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added a comment. I would prefer not to put testing details in source code because it makes our code more verbose and the details would likely grow stale. If someone wants to look at how something is tested the typical thing is to look at git blame or ask around.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sounds good.

Follow up to my comment #36597

This adds a pass to convert op aliases into a normalized form. Having two ops generated in our IR that do the same thing makes the IR harder for downstream consumers of the IR, such as TorchScript passes but also ONNX, glow, etc. 

Another solution would have been to fix our code generation to only emit `aten::abs` from the start. This seems trickier, and doesn't really buy us much if we still have to expose `aten::absolute` in C++, as @glaringlee of the C++ API thinks we should.

Bike shedding: maybe this should be `CanonicalizeOps` instead

[ghstack-poisoned]
Copy link
Copy Markdown
Collaborator

@jamesr66a jamesr66a left a comment

Choose a reason for hiding this comment

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

Cool!

Do we have a plan for extending this to other use cases? Main one is e.g. aten::_convolution v.s. aten::conv*d

Comment thread torch/csrc/jit/passes/normalize_ops.h Outdated
namespace torch {
namespace jit {

// This passes converts aten ops to a normalized form. It is
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: This pass :p

Comment thread torch/csrc/jit/python/script_init.cpp Outdated

auto cu = get_python_cu();
auto name = c10::QualifiedName(qualname);
NormalizeOps(graph);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this also needed in _create_method_from_trace? Can we just embed this call in trace() (called by createGraphByTracing)? Maybe here:
https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/frontend/tracer.cpp#L454

{aten::absolute_, aten::abs_},
};

void replaceNodeWithNewSymbol(Node* node, Symbol new_symbol) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would this be a useful method to expose on Node or Graph? I can definitely think of places I've wanted a function like this elsewhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you have any examples of other call sites ?

// having multiple ops in our IR that do the same thing makes the IR more
// difficult to consumer for downstream user of the IR, such as our own
// optimization passes here, we convert op aliases into a standard form
bool normalizeOpAliases(graph_node_list_iterator& iter) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

static, or just wrap all the implementation code in an anonymous namespace

@eellison
Copy link
Copy Markdown
Contributor Author

@jamesr66a no concrete plan to move over _convolution but it is my intention that this pass will eventually encompass that & other use cases

Follow up to my comment #36597

This adds a pass to convert op aliases into a normalized form. Having two ops generated in our IR that do the same thing makes the IR harder for downstream consumers of the IR, such as TorchScript passes but also ONNX, glow, etc. 

Another solution would have been to fix our code generation to only emit `aten::abs` from the start. This seems trickier, and doesn't really buy us much if we still have to expose `aten::absolute` in C++, as @glaringlee of the C++ API thinks we should.

Bike shedding: maybe this should be `CanonicalizeOps` instead

[ghstack-poisoned]
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@eellison merged this pull request in f90dc74.

@facebook-github-bot facebook-github-bot deleted the gh/eellison/80/head branch May 25, 2020 14:16
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Pull Request resolved: pytorch#38735

Follow up to my comment pytorch#36597

This adds a pass to convert op aliases into a normalized form. Having two ops generated in our IR that do the same thing makes the IR harder for downstream consumers of the IR, such as TorchScript passes but also ONNX, glow, etc.

Another solution would have been to fix our code generation to only emit `aten::abs` from the start. This seems trickier, and doesn't really buy us much if we still have to expose `aten::absolute` in C++, as glaringlee of the C++ API thinks we should.

Bike shedding: maybe this should be `CanonicalizeOps` instead

Test Plan: Imported from OSS

Differential Revision: D21673108

Pulled By: eellison

fbshipit-source-id: c328618907de1af22e07f57fd27fa619978c2817
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants