question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[RFC][Relay] Implicit type casting vs explicit dtype conversion

See original GitHub issue

I found two issues in inferring the output dtype of Relay’s type inference.

  • For some unary operations such as sqrt, log, exp, the type inference directly uses the input dtype as output dtype. However, if the input type is int type, this inference fails.
  • All binary operations now enforeces both operands have the same dtype. However, it is possible that a float tensor adds a int tensor. But it is not allowed in Relay. Though one can argue this is a good language feature for strict type constraint, imported models from existing frameworks could generate such operations.

I suggest two changes to the current type inference in Relay.

  • For unary ops like sqrt, log, exp, don’t specify the output dtype. Instead only provide a hint that the output could be a float type. Let the subsequent ops specify whether the output dtype is float32, float64, float16, etc.
  • To solve the second issue, there are two options. (I prefer the first option as it is more systemical.)
    • Disable the enforcement that both operands have the same dtype. Implicit cast the dtype for the output dtype. Notice that, implicit cast also exists in the HalideIR.
    • Check the dtype in Relay’s frontend converter, and cast the operand’s dtype whenever they’re different.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tqchencommented, Feb 23, 2019

I agree there are tradeoffs that need to be made. This is like the same trade-off we have to make when thinking about broadcasting in binary operators. Let us list the pros and cons

Pros of Implicit Casting

  • it makes frontend building relatively easy when dealing with mixed-dtype code

Pros of Explicit Casting

  • Mixed dtype is relatively rare in deep learning, so one can argue explicit casting is fine.
  • It makes support user-defined new floating type easy(e.g. bfloat), which is crucial for accelerators.
  • It simplifies the codegen support in the IR
0reactions
icemeloncommented, Mar 20, 2019

Yes, I agree that we should just keep the explicit casting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[RFC][Relay] Implicit type casting vs explicit dtype conversion
I found two issues in inferring the output dtype of Relay's type inference. For some unary operations such as sqrt, log, exp, the...
Read more >
C# Type Casting: Explicit & Implicit Data Conversion With ...
It is implicit conversion when a smaller data type is converted into a larger data type or derived class into a base class....
Read more >
Explicit and Implicit C# Type Casting - Pluralsight
C# provides four ways of converting one type to another: Implicit conversion. Explicit conversion. User-defined conversion.
Read more >
What is the difference between explicit and implicit type casts?
In this type casting programmer tells compiler to type cast one data type to another data type using type casting operator.
Read more >
Type Casting in C: Type Conversion, Implicit, Explicit ... - Guru99
Typecasting is converting one data type into another one. It is also called as data conversion or type conversion in C language.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found