| 46 | } |
| 47 | |
| 48 | Offset<Vector<Offset<Operator>>> InterpreterWriter::ExportOperators( |
| 49 | FlatBufferBuilder* fbb) { |
| 50 | std::vector<Offset<Operator>> operators; |
| 51 | |
| 52 | std::vector<int> operator_to_opcode; |
| 53 | // TODO(aselle): Augment this once we put execution plan in schema. |
| 54 | operator_to_opcode.resize(interpreter_->nodes_size(), -1); |
| 55 | for (int op_index : interpreter_->execution_plan()) { |
| 56 | const auto* node_and_registration = |
| 57 | interpreter_->node_and_registration(op_index); |
| 58 | const TfLiteRegistration* registration = &node_and_registration->second; |
| 59 | if (!registration->custom_name) { |
| 60 | operator_to_opcode[op_index] = |
| 61 | GetOpCodeForBuiltin(registration->builtin_code); |
| 62 | } else { |
| 63 | operator_to_opcode[op_index] = |
| 64 | GetOpCodeForCustom(registration->custom_name); |
| 65 | } |
| 66 | } |
| 67 | // second pass serialize operators |
| 68 | for (int op_index : interpreter_->execution_plan()) { |
| 69 | const auto* node_and_registration = |
| 70 | interpreter_->node_and_registration(op_index); |
| 71 | const TfLiteNode& node = node_and_registration->first; |
| 72 | const TfLiteRegistration& registration = node_and_registration->second; |
| 73 | Offset<void> builtin_options; |
| 74 | BuiltinOptions builtin_options_type = BuiltinOptions_NONE; |
| 75 | // Custom data |
| 76 | // TODO(aselle): Custom options format is not known by default. Just assume |
| 77 | // for now. |
| 78 | auto custom_options_format = CustomOptionsFormat_FLEXBUFFERS; |
| 79 | Offset<Vector<uint8_t>> custom_options = 0; |
| 80 | |
| 81 | if (!registration.custom_name) { |
| 82 | // builtin |
| 83 | auto builtin_options_and_type = CreateBuiltinUnion( |
| 84 | fbb, static_cast<enum BuiltinOperator>(registration.builtin_code), |
| 85 | node.builtin_data); |
| 86 | builtin_options = builtin_options_and_type.second; |
| 87 | builtin_options_type = builtin_options_and_type.first; |
| 88 | } else { |
| 89 | auto custom_writer = custom_op_to_writer_.find(registration.custom_name); |
| 90 | if (custom_writer != custom_op_to_writer_.end() && |
| 91 | custom_writer->second) { |
| 92 | // delegate to custom writer if it exists |
| 93 | custom_writer->second(fbb, interpreter_, op_index, &custom_options, |
| 94 | &custom_options_format); |
| 95 | } else { |
| 96 | // use the custom data as fact |
| 97 | custom_options = fbb->CreateVector( |
| 98 | reinterpret_cast<const uint8_t*>(node.custom_initial_data), |
| 99 | node.custom_initial_data_size); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | int opcode_index = operator_to_opcode[op_index]; |
| 104 | std::vector<int> written_inputs = |
| 105 | RemapTensorIndicesToWritten(TfLiteIntArrayView(node.inputs)); |
nothing calls this directly
no test coverage detected