| 1353 | namespace { |
| 1354 | |
| 1355 | Status TopKShapeFn(InferenceContext* c) { |
| 1356 | ShapeHandle input; |
| 1357 | TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(0), 1, &input)); |
| 1358 | |
| 1359 | // Get the k value, either from input tensor or attribute. |
| 1360 | DimensionHandle k_dim; |
| 1361 | if (c->num_inputs() >= 2) { |
| 1362 | TF_RETURN_IF_ERROR(c->MakeDimForScalarInput(1, &k_dim)); |
| 1363 | } else { |
| 1364 | int32 k; |
| 1365 | TF_RETURN_IF_ERROR(c->GetAttr("k", &k)); |
| 1366 | if (k < 0) { |
| 1367 | return errors::InvalidArgument("Need k >= 0, got ", k); |
| 1368 | } |
| 1369 | k_dim = c->MakeDim(k); |
| 1370 | } |
| 1371 | |
| 1372 | DimensionHandle last_dim = c->Dim(input, -1); |
| 1373 | if (c->ValueKnown(last_dim) && c->ValueKnown(k_dim) && |
| 1374 | c->Value(last_dim) < c->Value(k_dim)) { |
| 1375 | return errors::InvalidArgument( |
| 1376 | "input must have last dimension >= k = ", c->Value(k_dim), " but is ", |
| 1377 | c->Value(last_dim)); |
| 1378 | } |
| 1379 | |
| 1380 | // Replace last_dim with k_dim. |
| 1381 | ShapeHandle s; |
| 1382 | TF_RETURN_IF_ERROR(c->Subshape(input, 0, -1, &s)); |
| 1383 | TF_RETURN_IF_ERROR(c->Concatenate(s, c->Vector(k_dim), &s)); |
| 1384 | c->set_output(0, s); |
| 1385 | c->set_output(1, s); |
| 1386 | return Status::OK(); |
| 1387 | } |
| 1388 | |
| 1389 | } // namespace |
| 1390 |
nothing calls this directly
no test coverage detected