Skip to content

Conversation

@eellison
Copy link
Contributor

Previous implementation of magic methods extended from BuiltinOperators, but it should be able to work with other sugared values, such as casts.

I was also considering making CastValue's and BuiltinOperators's extend from a MagicMethod super class, and having them try to call into the super's before their own call. However, not all Builtin Operators have corresponding magic methods so i did it this way instead (although there are workarounds for that).

@eellison eellison requested review from driazati and suo May 17, 2019 19:08
@pytorchbot pytorchbot added the oncall: jit Add this issue/PR to JIT oncall triage queue label May 17, 2019
v = cast_v;
}
Value* out;
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this try-catch necessary? Shouldn't bool_cast be a CastValue and throw the right error?

Copy link
Contributor Author

@eellison eellison May 20, 2019

Choose a reason for hiding this comment

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

It's to report a better error message. Otherwise it would just show prim::Bool schema matching errors. I guess i could move this whole logic to CastValue. EDIT: that wouldn't work with UDT

def __bool__(self):
return (1, 2)

with self.assertRaisesRegex(RuntimeError, "expected a bool expression for condition"):
Copy link
Contributor

Choose a reason for hiding this comment

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

The error here is up a level too high, it should be thrown at the cast level to match Python:

class X(object):
    def __bool__(self):
        return (1, 2)

x = X()
if x:
    print("hi")

prints

Traceback (most recent call last):
  File "../test.py", line 142, in <module>
    if x:
TypeError: __bool__ should return bool, returned tuple

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't currently have the infrastructure for that. This could be possible with Interfaces, but is not currently possible. I don't think that should be a blocker for landing this.

Copy link
Contributor

Choose a reason for hiding this comment

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

In CastValue you know the type it should be and what the type actually is, can't you compare there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That doesn't fit in with MagicMethod sugared val, which abstracts over the input sugared val. Where would that check go?

Copy link
Contributor

Choose a reason for hiding this comment

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

MagicMethod calls CastValue, so it'd be fine there, then you could also get rid of the special erroring for emitCond

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MagicMethod also calls BuiltinFunctions - it doesn't just call CastValue. it's an abstraction

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 is landing 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 5acc664.

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