| 27 | using errors::Unknown; |
| 28 | |
| 29 | Status ShapeInferenceTestutil::InferShapes(ShapeInferenceTestOp op, |
| 30 | const string& ins, |
| 31 | const string& expected_outs) { |
| 32 | const OpRegistrationData* op_reg_data; |
| 33 | TF_RETURN_IF_ERROR(OpRegistry::Global()->LookUp(op.name, &op_reg_data)); |
| 34 | |
| 35 | std::vector<string> ins_v = str_util::Split(ins, ';'); |
| 36 | |
| 37 | InferenceContext::ShapeManager manager; |
| 38 | std::vector<ShapeHandle> in_shapes; |
| 39 | for (const string& spec : ins_v) { |
| 40 | ShapeHandle shape; |
| 41 | TF_RETURN_IF_ERROR(MakeShapeFromString(&manager, spec, &shape)); |
| 42 | in_shapes.push_back(shape); |
| 43 | } |
| 44 | |
| 45 | std::vector<std::unique_ptr<std::vector<shape_inference::ShapeAndType>>> |
| 46 | input_resource_handle_shapes_and_types; |
| 47 | for (const auto p : op.input_resource_handle_shapes_and_types) { |
| 48 | if (p == nullptr) { |
| 49 | input_resource_handle_shapes_and_types.push_back(nullptr); |
| 50 | } else { |
| 51 | std::unique_ptr<std::vector<ShapeAndType>> v( |
| 52 | new std::vector<ShapeAndType>()); |
| 53 | for (const auto& shape_and_type : *p) { |
| 54 | ShapeHandle shape; |
| 55 | TF_RETURN_IF_ERROR( |
| 56 | MakeShapeFromString(&manager, shape_and_type.first, &shape)); |
| 57 | v->emplace_back(shape, shape_and_type.second); |
| 58 | } |
| 59 | input_resource_handle_shapes_and_types.emplace_back(v.release()); |
| 60 | } |
| 61 | } |
| 62 | shape_inference::InferenceContext c( |
| 63 | op.graph_def_version, &op.node_def, op_reg_data->op_def, in_shapes, |
| 64 | op.input_tensors, {}, std::move(input_resource_handle_shapes_and_types)); |
| 65 | TF_RETURN_IF_ERROR(c.construction_status()); |
| 66 | if (op_reg_data->shape_inference_fn == nullptr) { |
| 67 | return errors::InvalidArgument( |
| 68 | "No shape inference function exists for op '", op.name, |
| 69 | "', did you forget to define it?"); |
| 70 | } |
| 71 | |
| 72 | TF_RETURN_IF_ERROR(c.Run(op_reg_data->shape_inference_fn)); |
| 73 | |
| 74 | const int num_outputs = c.num_outputs(); |
| 75 | |
| 76 | if (expected_outs == "e") { |
| 77 | return Unknown("Shape inference should have returned error"); |
| 78 | } |
| 79 | |
| 80 | // Verify the output shape. |
| 81 | std::vector<string> expected_outs_v = str_util::Split(expected_outs, ';'); |
| 82 | if (num_outputs != expected_outs_v.size()) { |
| 83 | return Unknown("The expected output string lists the wrong number of ", |
| 84 | "outputs. It lists ", expected_outs_v.size(), |
| 85 | " but should list ", num_outputs); |
| 86 | } |
nothing calls this directly
no test coverage detected