Skip to content

Conversation

@eellison
Copy link
Contributor

@eellison eellison commented May 1, 2019

Support operator overloading for User Defined Types, which includes desugaring a + b and python builtin functions which call into a method if it is defined like len(x).

See https://rszalski.github.io/magicmethods/ for list of magic methods.

@pytorchbot pytorchbot added the oncall: jit Add this issue/PR to JIT oncall triage queue label May 1, 2019
@eellison eellison requested review from driazati and suo May 1, 2019 22:41
@eellison eellison force-pushed the class_op_overloads branch from fdd14a7 to aa2dcb6 Compare May 1, 2019 23:36
@eellison eellison force-pushed the class_op_overloads branch from aa2dcb6 to 721931c Compare May 2, 2019 17:35
output = m_loaded(input)
self.assertEqual(3 * input, output)

def test_overloaded_fn(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

It'll get long but I think it should have a test every magic method to make sure the switch in compiler.cpp is correct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure 🌵

at::ArrayRef<NamedValue> attributes,
size_t n_binders) override {
if (inputs.size() > 0 && attributes.size() == 0) {
auto class_ptr = inputs[0].value(*m.graph())->type()->cast<ClassType>();
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens in Python if you have two separate classes with different __add__ methods and you do something like Foo() + Bar()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is out of scope for this PR. Probably an error should be raised when compiling the Class. All of the api's for ClassTypes assume a single function per name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh i misread the question, but if it's a binary op this will call into the first value's function, as with python

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@eellison has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@eellison merged this pull request in 6606ac5.

@ezyang
Copy link
Contributor

ezyang commented May 8, 2019

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.

6 participants