[RFC][Relay] Implicit type casting vs explicit dtype conversion
See original GitHub issueI 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 isfloat32
,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:
- Created 5 years ago
- Reactions:2
- Comments:9 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
Pros of Explicit Casting
Yes, I agree that we should just keep the explicit casting.