| 195 | } |
| 196 | |
| 197 | tensorflow::Status ImportShape( |
| 198 | const TFLITE_PROTO_NS::RepeatedPtrField<tensorflow::TensorShapeProto_Dim>& |
| 199 | input_dims, |
| 200 | int* input_flat_size, Shape* shape) { |
| 201 | std::vector<int> input_dims_only_sizes; |
| 202 | bool zero_sized_shape = false; |
| 203 | for (auto& d : input_dims) { |
| 204 | // TensorFlow's shapes use int64s, while TOCO uses ints. |
| 205 | if (d.size() > std::numeric_limits<int>::max()) { |
| 206 | return tensorflow::errors::InvalidArgument("Shape element overflows"); |
| 207 | } |
| 208 | if (d.size() == 0) { |
| 209 | zero_sized_shape = true; |
| 210 | } |
| 211 | input_dims_only_sizes.push_back(d.size()); |
| 212 | } |
| 213 | |
| 214 | // Note that up to this point we were OK with the input shape containing |
| 215 | // elements valued -1 or 0, which are perfectly legal in tensorflow. However |
| 216 | // our CheckValidShapeDimensions() insists on them being >= 1, with the |
| 217 | // exception of the "scalar" shape [0]. The main issue with zero-values shape |
| 218 | // elements is that the corresponding arrays don't contain any data and the |
| 219 | // allocation code gets a bit confused. It seems that the code expects an |
| 220 | // empty shape for zero-sized shapes, so we will do just that, except for the |
| 221 | // [0] case. |
| 222 | // TODO(b/119325030): In order to correctly import the "scalar" shapes the |
| 223 | // following test must include "&& input_dims_only_sizes.size() > 1", but |
| 224 | // that seems to slow everything down a lot. |
| 225 | if (zero_sized_shape) { |
| 226 | shape->mutable_dims()->clear(); |
| 227 | if (input_flat_size != nullptr) *input_flat_size = 0; |
| 228 | return tensorflow::Status::OK(); |
| 229 | } |
| 230 | |
| 231 | *shape->mutable_dims() = input_dims_only_sizes; |
| 232 | |
| 233 | if (input_flat_size == nullptr) return tensorflow::Status::OK(); |
| 234 | |
| 235 | return NumElements(input_dims_only_sizes, input_flat_size); |
| 236 | } |
| 237 | |
| 238 | // Define ways to retrieve data from tensors of different types. |
| 239 | // TODO(b/80208043): simply use tensorflow::Tensor::FromProto() instead. |
no test coverage detected