This function checks whether the desired type is "compatible" with the inferred type. At a high level, compatibility means that all integral types are compatible with each other, and all floating types are compatible with each other. Type compatibility doesn't consider overflows (i.e. int64 is *always compatible with int32). This is intended to match graph behavior.
| 194 | // Type compatibility doesn't consider overflows (i.e. int64 is *always* |
| 195 | // compatible with int32). This is intended to match graph behavior. |
| 196 | bool IsCompatible(DataType desired, DataType returned) { |
| 197 | if (desired == returned) return true; |
| 198 | |
| 199 | if (DataTypeIsInteger(desired) && DataTypeIsInteger(returned)) { |
| 200 | return true; |
| 201 | } else if (DataTypeIsFloating(desired) && |
| 202 | (DataTypeIsFloating(returned) || DataTypeIsInteger(returned))) { |
| 203 | return true; |
| 204 | } else if (DataTypeIsComplex(desired) && |
| 205 | (DataTypeIsComplex(returned) || DataTypeIsInteger(returned) || |
| 206 | DataTypeIsFloating(returned))) { |
| 207 | return true; |
| 208 | } else if (DataTypeIsQuantized(desired) && DataTypeIsInteger(returned)) { |
| 209 | return true; |
| 210 | } |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | // TODO(nareshmodi): Move EagerCast and ReadVariableOp (which use the C API to |
| 215 | // execute TFE Ops) to a separate common library. |
no test coverage detected