Returns true if the given pair of TensorFlow types can be cast to one another. In other words, a single run-time value is legal for both the types. For example, tensor<*xf32> and tensor<3xf32> are cast compatible.
| 113 | // another. In other words, a single run-time value is legal for both the types. |
| 114 | // For example, tensor<*xf32> and tensor<3xf32> are cast compatible. |
| 115 | static bool AreCastCompatible(Type a, Type b) { |
| 116 | if (TensorCastOp::areCastCompatible(a, b)) return true; |
| 117 | |
| 118 | // Resource types may optionally contain subtypes information that does not |
| 119 | // match. Check subtypes compatibility when possible, otherwise treat them as |
| 120 | // compatible. |
| 121 | auto a_or_element_type = getElementTypeOrSelf(a); |
| 122 | auto b_or_element_type = getElementTypeOrSelf(b); |
| 123 | |
| 124 | auto a_kind = a_or_element_type.getKind(); |
| 125 | auto b_kind = b_or_element_type.getKind(); |
| 126 | |
| 127 | if (a_kind == TensorFlowTypes::RESOURCE && |
| 128 | b_kind == TensorFlowTypes::RESOURCE) { |
| 129 | auto a_resource_type = a_or_element_type.dyn_cast<ResourceType>(); |
| 130 | auto b_resource_type = b_or_element_type.dyn_cast<ResourceType>(); |
| 131 | bool a_has_subtype = !a_resource_type.getSubtypes().empty(); |
| 132 | bool b_has_subtype = !b_resource_type.getSubtypes().empty(); |
| 133 | |
| 134 | if (!a_has_subtype || !b_has_subtype) return true; |
| 135 | |
| 136 | assert(a_resource_type.getSubtypes().size() <= 1 && |
| 137 | "Resource type must have at most one subtype"); |
| 138 | assert(b_resource_type.getSubtypes().size() <= 1 && |
| 139 | "Resource type must have at most one subtype"); |
| 140 | |
| 141 | return TensorCastOp::areCastCompatible( |
| 142 | a_resource_type.getSubtypes().front(), |
| 143 | b_resource_type.getSubtypes().front()); |
| 144 | } |
| 145 | |
| 146 | // Variant types may optionally contain subtypes information that need not |
| 147 | // match. It is also not possible to compare subtypes for compatibility as |
| 148 | // their interpretation depends on the ops operating on them. So, accept all |
| 149 | // pairs of variant types. |
| 150 | return a_kind == TensorFlowTypes::VARIANT && |
| 151 | b_kind == TensorFlowTypes::VARIANT; |
| 152 | } |
| 153 | |
| 154 | static bool IsUnknownDimOrRank(int64_t dim_or_rank) { |
| 155 | return dim_or_rank == -1; |
no test coverage detected