| 186 | } |
| 187 | |
| 188 | Status SetOutputShapeForReshape(InferenceContext* c) { |
| 189 | ShapeHandle in = c->input(0); |
| 190 | ShapeHandle out; |
| 191 | TF_RETURN_IF_ERROR(c->MakeShapeFromShapeTensor(1, &out)); |
| 192 | |
| 193 | if (!c->RankKnown(out)) { |
| 194 | // We have no information about the shape of the output. |
| 195 | c->set_output(0, out); |
| 196 | return Status::OK(); |
| 197 | } |
| 198 | |
| 199 | if (c->RankKnown(out) && c->RankKnown(in)) { |
| 200 | // We don't know the number of output elements, but we can try to infer |
| 201 | // the missing dimension. |
| 202 | bool too_many_unknown = false; |
| 203 | int32 out_unknown_idx = -1; |
| 204 | |
| 205 | DimensionHandle known_out_elems = c->NumElements(out); |
| 206 | if (!c->ValueKnown(known_out_elems)) { |
| 207 | known_out_elems = c->MakeDim(1); |
| 208 | for (int32 i = 0; i < c->Rank(out); ++i) { |
| 209 | DimensionHandle dim = c->Dim(out, i); |
| 210 | if (!c->ValueKnown(dim)) { |
| 211 | if (out_unknown_idx >= 0) { |
| 212 | too_many_unknown = true; |
| 213 | break; |
| 214 | } |
| 215 | out_unknown_idx = i; |
| 216 | } else { |
| 217 | TF_RETURN_IF_ERROR( |
| 218 | c->Multiply(known_out_elems, dim, &known_out_elems)); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | int32 in_unknown_idx = -1; |
| 223 | DimensionHandle known_in_elems = c->NumElements(in); |
| 224 | if (!c->ValueKnown(known_in_elems)) { |
| 225 | known_in_elems = c->MakeDim(1); |
| 226 | for (int32 i = 0; i < c->Rank(in); ++i) { |
| 227 | DimensionHandle dim = c->Dim(in, i); |
| 228 | if (!c->ValueKnown(dim)) { |
| 229 | if (in_unknown_idx >= 0) { |
| 230 | too_many_unknown = true; |
| 231 | break; |
| 232 | } |
| 233 | in_unknown_idx = i; |
| 234 | } else { |
| 235 | TF_RETURN_IF_ERROR(c->Multiply(known_in_elems, dim, &known_in_elems)); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (!too_many_unknown) { |
| 241 | if (in_unknown_idx < 0 && out_unknown_idx < 0) { |
| 242 | // Just check that the dimensions match. |
| 243 | if (c->Value(known_in_elems) != c->Value(known_out_elems)) { |
| 244 | return errors::InvalidArgument( |
| 245 | "Cannot reshape a tensor with ", c->DebugString(known_in_elems), |
no test coverage detected