| 28 | namespace { |
| 29 | |
| 30 | class OperatorTest : public ::testing::Test { |
| 31 | protected: |
| 32 | // Return the operator for the given name and type. |
| 33 | const BaseOperator& GetOperator(const string& name, OperatorType type) { |
| 34 | using OpsByName = std::map<string, std::unique_ptr<BaseOperator>>; |
| 35 | using OpsByType = std::map<OperatorType, std::unique_ptr<BaseOperator>>; |
| 36 | |
| 37 | static auto* by_name = new OpsByName(BuildOperatorByNameMap()); |
| 38 | static auto* by_type = new OpsByType(BuildOperatorByTypeMap()); |
| 39 | |
| 40 | // Make sure the two maps were consitently built. |
| 41 | CHECK(by_name->count(name)) << "No operator for '" << name << "'."; |
| 42 | BaseOperator* op1 = by_name->at(name).get(); |
| 43 | CHECK(op1->type() == type) << "while verifying '" << name << "'."; |
| 44 | |
| 45 | CHECK(by_type->count(type)) |
| 46 | << "No operator for '" << OperatorTypeName(type) << "'."; |
| 47 | BaseOperator* op2 = by_type->at(type).get(); |
| 48 | CHECK(op2->name() == name) |
| 49 | << "while verifying '" << OperatorTypeName(type) << "'."; |
| 50 | |
| 51 | return *op1; |
| 52 | } |
| 53 | |
| 54 | // Use the given BaseOperator to serialize the tf.mini operator into a set of |
| 55 | // TF Lite options. Proceed to deserialize the options back into a new |
| 56 | // tf.mini operator, which is then returned. If `options` is given, it will |
| 57 | // be populated with the serialized options. |
| 58 | template <typename T> |
| 59 | std::unique_ptr<T> SerializeAndDeserialize(const BaseOperator& op, |
| 60 | const T& toco_op, |
| 61 | Options* options = nullptr) { |
| 62 | flatbuffers::FlatBufferBuilder builder; |
| 63 | Options input_options = op.Serialize(toco_op, &builder); |
| 64 | |
| 65 | if (options) { |
| 66 | *options = input_options; |
| 67 | } |
| 68 | |
| 69 | builder.Finish(CreateOperator(builder, 0, 0, 0, input_options.type, |
| 70 | input_options.builtin, input_options.custom, |
| 71 | ::tflite::CustomOptionsFormat_FLEXBUFFERS)); |
| 72 | auto* output_options = |
| 73 | flatbuffers::GetRoot<::tflite::Operator>(builder.GetBufferPointer()); |
| 74 | auto new_toco_op = op.Deserialize(output_options->builtin_options(), |
| 75 | output_options->custom_options()); |
| 76 | |
| 77 | CHECK(new_toco_op->type == toco_op.type) |
| 78 | << "The type of the serialized and deserialized" |
| 79 | << HelpfulOperatorTypeName(*new_toco_op) |
| 80 | << " does not match the type of the original " |
| 81 | << HelpfulOperatorTypeName(toco_op); |
| 82 | |
| 83 | return std::unique_ptr<T>(dynamic_cast<T*>(new_toco_op.release())); |
| 84 | } |
| 85 | |
| 86 | // Verify serialization and deserialization of simple operators (those |
| 87 | // that don't have any configuration parameters). |
nothing calls this directly
no test coverage detected